"use client"; import { IconPencil, IconPlus, IconTrash } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Label } from "@kandev/ui/label"; import { Separator } from "@kandev/ui/separator"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue, } from "@kandev/ui/select"; import { configOptionToModelOptions, isModelConfigOption, ModelConfigSelector, type SelectConfigOption, usableConfigOptions, } from "@/components/model-config-selector"; import { InferenceAgentStatusNote } from "@/components/settings/inference-agent-status"; import type { UtilityAgent, InferenceAgent } from "@/lib/api/domains/utility-api"; const USE_DEFAULT = "__USE_DEFAULT__"; export type ModelOption = { value: string; label: string; agentName: string; modelName: string }; type ModelGroup = { agentName: string; models: ModelOption[] }; function groupModelsByAgent(models: ModelOption[]): ModelGroup[] { const map = new Map(); for (const m of models) { const list = map.get(m.agentName); if (list) list.push(m); else map.set(m.agentName, [m]); } return Array.from(map, ([agentName, items]) => ({ agentName, models: items })); } function utilityConfigOptions(agent: InferenceAgent | undefined): SelectConfigOption[] { return usableConfigOptions( agent?.config_options?.map((option) => ({ type: option.type, id: option.id, name: option.name, currentValue: option.current_value, category: option.category, options: option.options, })), ); } // Default model selector section type DefaultModelSectionProps = { inferenceAgents: InferenceAgent[]; defaultAgentId: string; defaultModel: string; onDefaultChange: (agentId: string, model: string) => void; onRefreshAgent: (agentId: string) => Promise | void; }; export function DefaultModelSection({ inferenceAgents, defaultAgentId, defaultModel, onDefaultChange, onRefreshAgent, }: DefaultModelSectionProps) { const selectedAgent = inferenceAgents.find((a) => a.id === defaultAgentId); const modelOptions = selectedAgent?.models ?? []; const currentModelId = modelOptions.find((m) => m.is_default)?.id; const configOptions = utilityConfigOptions(selectedAgent); const modelConfig = configOptions.find(isModelConfigOption); const selectorModels = modelConfig ? configOptionToModelOptions(modelConfig) : modelOptions.map((model) => ({ id: model.id, name: model.name, description: model.description || (model.id !== model.name ? model.id : undefined), usageMultiplier: typeof model.meta?.copilotUsage === "string" ? model.meta.copilotUsage : undefined, })); const selectedModel = defaultModel || modelConfig?.currentValue || currentModelId || null; const selectedConfigOptions = configOptions.map((option) => ({ ...option, currentValue: isModelConfigOption(option) && selectedModel ? selectedModel : option.currentValue, })); return (

Default utility agent model

Select the default model used by all built-in utility actions.

onDefaultChange(defaultAgentId, v)} disabled={!defaultAgentId} placeholder="Select model..." ariaLabel="Default utility model settings" />
{defaultAgentId && ( onRefreshAgent(defaultAgentId)} /> )}
); } // Builtin action row type BuiltinActionRowProps = { agent: UtilityAgent; allModels: ModelOption[]; defaultLabel: string; onModelChange: (agent: UtilityAgent, value: string) => void; onEdit: (agent: UtilityAgent) => void; }; export function BuiltinActionRow({ agent, allModels, defaultLabel, onModelChange, onEdit, }: BuiltinActionRowProps) { const currentValue = agent.agent_id && agent.model ? `${agent.agent_id}|${agent.model}` : USE_DEFAULT; return (
{agent.name}

{agent.description}

); } // Custom agent row type CustomAgentRowProps = { agent: UtilityAgent; onEdit: (agent: UtilityAgent) => void; onDelete: (agent: UtilityAgent) => void; }; export function CustomAgentRow({ agent, onEdit, onDelete }: CustomAgentRowProps) { return (
{agent.name}

{agent.description}

{agent.model || "Not configured"}
); } // Per-action overrides section type PerActionOverridesSectionProps = { builtins: UtilityAgent[]; allModels: ModelOption[]; defaultModel: string; onModelChange: (agent: UtilityAgent, value: string) => void; onEdit: (agent: UtilityAgent) => void; }; export function PerActionOverridesSection({ builtins, allModels, defaultModel, onModelChange, onEdit, }: PerActionOverridesSectionProps) { if (builtins.length === 0) return null; const defaultLabel = defaultModel ? `Default (${defaultModel})` : "Default"; return (

Actions

{builtins.map((agent) => ( ))}
); } // Custom agents section type CustomAgentsSectionProps = { agents: UtilityAgent[]; onAdd: () => void; onEdit: (agent: UtilityAgent) => void; onDelete: (agent: UtilityAgent) => void; }; export function CustomAgentsSection({ agents, onAdd, onEdit, onDelete }: CustomAgentsSectionProps) { return (

Custom utility agents

Create your own utility agents with custom prompts.

{agents.length === 0 && (

No custom utility agents.

)} {agents.length > 0 && (
{agents.map((agent) => ( ))}
)}
); } // Export the USE_DEFAULT constant export { USE_DEFAULT };