"use client"; import { useId } from "react"; import { useTranslation } from "react-i18next"; import type { TFunction } from "i18next"; import { IconAlertCircle, IconAlertTriangle, IconRefresh } from "@tabler/icons-react"; import { NoAuthPanel, ProbingPanel } from "@/components/settings/profile-status-panels"; import { Button } from "@kandev/ui/button"; import { Input } from "@kandev/ui/input"; import { Label } from "@kandev/ui/label"; import { Skeleton } from "@kandev/ui/skeleton"; import { Switch } from "@kandev/ui/switch"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { ModeCombobox } from "@/components/settings/mode-combobox"; import { configOptionToModelOptions, isModelConfigOption, ModelConfigSelector, type SelectConfigOption, usableConfigOptions, } from "@/components/model-config-selector"; import { useAgentCapabilities } from "@/hooks/domains/settings/use-dynamic-models"; import { PERMISSION_APPLY_AGENTCTL_AUTO_APPROVE, PERMISSION_KEYS, readPermissionValue, type PermissionKey, } from "@/lib/agent-permissions"; import { CLIFlagsField } from "@/components/settings/cli-flags-field"; import { CommandPrefixField } from "@/components/settings/command-prefix-field"; import { CommandsButton, findActiveMode, profileModeIsDirty, profileModelIsDirty, } from "@/components/settings/profile-capability-helpers"; import type { CLIFlag, CommandEntry, ModelConfig, ModeEntry, ModelEntry, PermissionSetting, PassthroughConfig, } from "@/lib/types/http"; export type ProfileFormData = { name: string; model: string; mode: string; config_options?: Record; cli_passthrough: boolean; cli_flags: CLIFlag[]; command_prefix?: string; } & Record; export type ProfileFormFieldsProps = { profile: ProfileFormData; baselineProfile?: ProfileFormData; onChange: (patch: Partial) => void; modelConfig: ModelConfig; permissionSettings: Record; passthroughConfig: PassthroughConfig | null; agentName: string; onRemove?: () => void; canRemove?: boolean; variant?: "default" | "compact"; hideNameField?: boolean; lockPassthrough?: boolean; /** * When true, the custom-flag list + Add form on CLIFlagsField is * hidden. Curated predefined toggles still render. Used by the * onboarding flow to keep the first-run UI narrow. */ hideCustomCLIFlags?: boolean; }; type PermissionToggleProps = { profile: ProfileFormData; baselineProfile?: ProfileFormData; onChange: (patch: Partial) => void; permissionSettings: Record; passthroughConfig: PassthroughConfig | null; variant: "default" | "compact"; lockPassthrough?: boolean; }; function permissionToggleWrapperClass(isDanger: boolean, compact: boolean): string { if (isDanger) { return "flex items-center justify-between gap-3 rounded-md border border-destructive/40 bg-destructive/5 p-3"; } if (compact) { return "flex items-center justify-between gap-2"; } return "flex items-center justify-between rounded-md border p-3"; } function PermissionToggleRow({ settingKey, setting, checked, onCheckedChange, compact, isDirty, }: { settingKey: string; setting: PermissionSetting; checked: boolean; onCheckedChange: (checked: boolean) => void; compact: boolean; isDirty: boolean; }) { const isDanger = setting.apply_method === PERMISSION_APPLY_AGENTCTL_AUTO_APPROVE; const switchSize = compact ? ("sm" as const) : ("default" as const); const labelCls = compact ? "text-xs" : undefined; const wrapperCls = permissionToggleWrapperClass(isDanger, compact); const instanceId = useId(); const switchId = `${instanceId}-permission-toggle-${settingKey}`; return (

{setting.description}

); } function PermissionToggles({ profile, onChange, permissionSettings, passthroughConfig, variant, lockPassthrough, baselineProfile, }: PermissionToggleProps) { const isCompact = variant === "compact"; const switchSize = isCompact ? ("sm" as const) : ("default" as const); if (isCompact) { return ( <> {PERMISSION_KEYS.map((key) => { const setting = permissionSettings[key]; if (!setting?.supported) return null; if (setting.apply_method === "cli_flag") return null; const checked = readPermissionValue(profile, key, permissionSettings); return ( onChange({ [key]: checked })} compact isDirty={ Boolean(baselineProfile) && checked !== readPermissionValue(baselineProfile!, key, permissionSettings) } /> ); })} {passthroughConfig?.supported && (

{passthroughConfig.description}

onChange({ cli_passthrough: checked })} disabled={lockPassthrough} data-settings-dirty={ Boolean(baselineProfile) && profile.cli_passthrough !== baselineProfile?.cli_passthrough } />
)} ); } return (
{PERMISSION_KEYS.map((key) => { const setting = permissionSettings[key]; if (!setting?.supported) return null; if (setting.apply_method === "cli_flag") return null; return ( onChange({ [key]: checked })} compact={false} isDirty={ Boolean(baselineProfile) && readPermissionValue(profile, key, permissionSettings) !== readPermissionValue(baselineProfile!, key, permissionSettings) } /> ); })} {passthroughConfig?.supported && (

{passthroughConfig.description}

onChange({ cli_passthrough: checked })} disabled={lockPassthrough} data-settings-dirty={ Boolean(baselineProfile) && profile.cli_passthrough !== baselineProfile?.cli_passthrough } />
)}
); } // The probe `status` values are the wire enum; only the messages are copy, so // they travel as catalog keys and resolve at render. const CAPABILITY_STATUS_KEYS: Record = { probing: "agents:capabilityProbing", auth_required: "agents:capabilityAuthRequired", not_installed: "agents:capabilityNotInstalled", failed: "agents:capabilityProbeFailed", }; function capabilityStatusMessage(t: TFunction, status: ModelConfig["status"]): string | null { const key = status ? CAPABILITY_STATUS_KEYS[status] : undefined; return key ? t(key) : null; } function CapabilityStatusMessage({ status }: { status: ModelConfig["status"] }) { const { t } = useTranslation(); const msg = capabilityStatusMessage(t, status); if (!msg) return null; return (

{msg}

); } function RefreshCapabilitiesButton({ onRefresh, isLoading, error, }: { onRefresh: () => Promise; isLoading: boolean; error: string | null; }) { const { t } = useTranslation(); return (

{t("agents:refreshCapabilitiesTooltip")}

{error && (

{t("agents:failedToRefresh", { error })}

)}
); } function ModelPicker({ profile, models, currentModelId, configOptions, onChange, }: { profile: ProfileFormData; models: ModelEntry[]; currentModelId: string | undefined; configOptions: SelectConfigOption[]; onChange: (patch: Partial) => void; }) { const { t } = useTranslation(); const modelConfig = configOptions.find(isModelConfigOption); const modelOptions = modelConfig ? configOptionToModelOptions(modelConfig) : models.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 currentModel = profile.model || modelConfig?.currentValue || currentModelId || null; const selectedConfigOptions = configOptions.map((option) => ({ ...option, currentValue: isModelConfigOption(option) ? profile.model || option.currentValue : profile.config_options?.[option.id] || option.currentValue, })); return ( onChange({ model: value })} onConfigChange={(configId, value) => onChange({ config_options: { ...(profile.config_options ?? {}), [configId]: value } }) } placeholder={t("agents:selectAModel")} ariaLabel={t("agents:profileStartModelSettings")} /> ); } function ModePicker({ profile, modes, currentModeId, onChange, }: { profile: ProfileFormData; modes: ModeEntry[]; currentModeId: string | undefined; onChange: (patch: Partial) => void; }) { return ( onChange({ mode: value })} modes={modes} currentModeId={currentModeId} /> ); } function modelConfigOptions(modelConfig: ModelConfig): SelectConfigOption[] { return usableConfigOptions( modelConfig.config_options?.map((option) => ({ type: option.type, id: option.id, name: option.name, currentValue: option.current_value, category: option.category, options: option.options, })), ); } type CapabilitiesRowProps = { profile: ProfileFormData; models: ModelEntry[]; modes: ModeEntry[]; commands: CommandEntry[]; currentModelId: string | undefined; currentModeId: string | undefined; status: ModelConfig["status"]; onChange: (patch: Partial) => void; isCompact: boolean; isLoading: boolean; onRefresh: () => Promise; error: string | null; modelConfig: ModelConfig; agentName: string; baselineProfile?: ProfileFormData; }; function CapabilitiesRow({ profile, models, modes, commands, currentModelId, currentModeId, status, onChange, isCompact, isLoading, onRefresh, error, modelConfig, agentName, baselineProfile, }: CapabilitiesRowProps) { const { t } = useTranslation(); const hasModes = modes.length > 0; const configOptions = modelConfigOptions(modelConfig); const activeMode = findActiveMode(modes, profile.mode, currentModeId); const labelCls = isCompact ? "text-xs text-muted-foreground" : undefined; const gapCls = isCompact ? "space-y-1.5" : "space-y-2"; if (isLoading && models.length === 0) { return (
); } if (status === "probing") { return ; } if (status === "auth_required" || status === "not_installed") { return ( ); } return (
{hasModes && (
)}
{activeMode?.description && (

{activeMode.description}

)} {commands.length > 0 && }
); } function NameField({ profile, onChange, canRemove, onRemove, baselineName, }: { profile: ProfileFormData; onChange: (patch: Partial) => void; canRemove?: boolean; onRemove?: () => void; baselineName?: string; }) { const { t } = useTranslation(); return (
onChange({ name: event.target.value })} placeholder={t("agents:defaultProfile")} data-settings-dirty={baselineName !== undefined && profile.name !== baselineName} />
{canRemove && onRemove && ( )}
); } export function ProfileFormFields({ profile, baselineProfile, onChange, modelConfig, permissionSettings, passthroughConfig, agentName, onRemove, canRemove = false, variant = "default", hideNameField = false, lockPassthrough = false, hideCustomCLIFlags = false, }: ProfileFormFieldsProps) { const isCompact = variant === "compact"; const caps = useAgentCapabilities(agentName, modelConfig); return (
{!hideNameField && ( )}
onChange({ cli_flags: next })} permissionSettings={permissionSettings} variant={variant} hideCustomFlags={hideCustomCLIFlags} />
{!profile.cli_passthrough && ( )}
); }