"use client";
import { useTranslation } from "react-i18next";
import { IconPencil, IconPlus, IconTrash } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { CardContent } from "@kandev/ui/card";
import type { UtilityAgent } from "@/lib/api/domains/utility-api";
import type { AgentProfileOption } from "@/lib/state/slices/settings/types";
import { SettingsCard } from "@/components/settings/settings-card";
import { useFeature } from "@/hooks/domains/features/use-feature";
import { SETTINGS_TARGETS } from "@/lib/settings-discovery/catalog/standalone";
import { isUtilityAgentDirty } from "@/components/settings/utility-dirty";
import {
UtilityAgentProfilePicker,
utilityProfileEligibility,
} from "@/components/settings/utility-agent-profile-picker";
import { SettingsCardHeader } from "@/components/settings/settings-card-header";
import { SettingsFieldLabel } from "@/components/settings/settings-typography";
import { settingsActionClassName } from "@/components/settings/settings-control";
export const USE_DEFAULT = "__USE_DEFAULT__";
const UNCONFIGURED = "__UNCONFIGURED__";
export type BuiltinActionProfileSelection = {
value: string;
unavailableValue?: string;
};
export function getBuiltinActionProfileSelection(
agent: UtilityAgent,
): BuiltinActionProfileSelection {
const profileId = agent.agent_profile_id || "";
if (agent.profile_binding_state === "unconfigured") {
const unavailableValue = profileId || UNCONFIGURED;
return { value: unavailableValue, unavailableValue };
}
if (agent.profile_binding_state !== "inherit" && profileId) {
return { value: profileId };
}
return { value: USE_DEFAULT };
}
export function DefaultModelSection({
profiles,
profileId,
onProfileChange,
isDirty,
}: {
profiles: AgentProfileOption[];
profileId: string;
onProfileChange: (profileId: string) => void;
isDirty: boolean;
}) {
const { t } = useTranslation();
const dynamicRoutingEnabled = useFeature("dynamicAgentRouting");
const eligibleProfiles = profiles.filter((profile) =>
utilityProfileEligibility(profile, dynamicRoutingEnabled),
);
const selected = eligibleProfiles.find((profile) => profile.id === profileId);
return (
{t("settings:utilityDefaultModelDescription")}
{t("settings:utilityAgentProfile")}
onProfileChange(value === USE_DEFAULT ? "" : value)}
fallback={{ value: USE_DEFAULT, label: t("settings:utilityNoDefaultProfile") }}
unavailableValue={profileId && !selected ? profileId : undefined}
testId="utility-profile-picker-default"
triggerClassName="w-full max-w-sm font-normal"
/>
{profileId && !selected && (
{t("settings:utilityProfileNeedsRepair")}
)}
);
}
export function BuiltinActionRow({
agent,
profiles,
defaultLabel,
onProfileChange,
onEdit,
isDirty,
}: {
agent: UtilityAgent;
profiles: AgentProfileOption[];
defaultLabel: string;
onProfileChange: (agent: UtilityAgent, value: string) => void;
onEdit: (agent: UtilityAgent) => void;
isDirty: boolean;
}) {
const { t } = useTranslation();
const selection = getBuiltinActionProfileSelection(agent);
return (
{agent.name}
{agent.description}
onProfileChange(agent, value)}
fallback={{ value: USE_DEFAULT, label: defaultLabel }}
unavailableValue={selection.unavailableValue}
unavailableLabel={
selection.unavailableValue ? t("settings:utilityProfileNeedsRepair") : undefined
}
testId={`utility-profile-picker-action-${agent.id}`}
triggerClassName="min-w-0 flex-1 md:w-[280px] md:flex-none"
/>
);
}
export function PerActionOverridesSection({
builtins,
profiles,
defaultLabel,
onProfileChange,
onEdit,
savedBuiltins,
}: {
builtins: UtilityAgent[];
profiles: AgentProfileOption[];
defaultLabel: string;
onProfileChange: (agent: UtilityAgent, value: string) => void;
onEdit: (agent: UtilityAgent) => void;
savedBuiltins: UtilityAgent[];
}) {
const { t } = useTranslation();
if (builtins.length === 0) return null;
return (
isUtilityAgentDirty(
agent,
savedBuiltins.find((saved) => saved.id === agent.id),
),
)}
discoveryTargetId={SETTINGS_TARGETS.utilityActions}
data-testid="utility-actions-card"
>
{t("settings:utilityActionsDescription")}
{builtins.map((agent) => (
saved.id === agent.id),
)}
/>
))}
);
}
export function CustomAgentRow({
agent,
profiles,
onEdit,
onDelete,
}: {
agent: UtilityAgent;
profiles: AgentProfileOption[];
onEdit: (agent: UtilityAgent) => void;
onDelete: (agent: UtilityAgent) => void;
}) {
const { t } = useTranslation();
const profile = profiles.find((item) => item.id === agent.agent_profile_id);
return (
{agent.name}
{agent.description}
{profile ? profile.label || profile.id : t("settings:utilityProfileNeedsRepair")}
);
}
export function CustomAgentsSection({
agents,
profiles,
onAdd,
onEdit,
onDelete,
}: {
agents: UtilityAgent[];
profiles: AgentProfileOption[];
onAdd: () => void;
onEdit: (agent: UtilityAgent) => void;
onDelete: (agent: UtilityAgent) => void;
}) {
const { t } = useTranslation();
return (
{t("settings:utilityAddCustomAgent")}
}
/>
{t("settings:utilityCustomAgentsDescription")}
{agents.length === 0 ? (
{t("settings:utilityCustomAgentsEmpty")}
) : (
{agents.map((agent) => (
))}
)}
);
}