"use client"; import { useEffect, useId } from "react"; import { useTranslation } from "react-i18next"; import { IconAlertTriangle } from "@tabler/icons-react"; import type { SelectConfigOption } from "@/components/model-config-selector"; import { NoAuthPanel, ProbingPanel } from "@/components/settings/profile-status-panels"; import { Button } from "@kandev/ui/button"; import { Input } from "@kandev/ui/input"; import { Skeleton } from "@kandev/ui/skeleton"; import { Switch } from "@kandev/ui/switch"; import { useProfileModelCapabilities } from "@/hooks/domains/settings/use-profile-model-capabilities"; import { PERMISSION_APPLY_AGENTCTL_AUTO_APPROVE, PERMISSION_KEYS, readPermissionValue, type PermissionKey, } from "@/lib/agent-permissions"; import { CLIFlagsField } from "@/components/settings/cli-flags-field"; import { ProfileAdvancedOptions } from "@/components/settings/profile-advanced-options"; import { ModelConfigResolutionStatus } from "@/components/settings/model-config-resolution-status"; import { CapabilityStatusMessage, RefreshCapabilitiesButton, } from "@/components/settings/profile-capability-status"; import { CommandsButton, findActiveMode, profileModeIsDirty, profileModelIsDirty, } from "@/components/settings/profile-capability-helpers"; import { modelConfigOptions } from "@/components/settings/profile-model-config"; import { ModelFallbackSection, ModelPicker, ModePicker, } from "@/components/settings/profile-model-fields"; import { SettingsFieldDescription, SettingsFieldLabel, } from "@/components/settings/settings-typography"; import type { CLIFlag, CommandEntry, ModelConfig, ModeEntry, ModelEntry, PermissionSetting, PassthroughConfig, } from "@/lib/types/http"; export type ProfileFormData = { name: string; model: string; /** Optional single fallback model applied when `model` is unavailable. */ fallback_model?: string; /** Legacy automatic-fallback opt-in; hides the fallback_model field. */ auto_fallback?: boolean; 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; onModelConfigResolutionPendingChange?: (pending: boolean) => void; /** * 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 (
{isDanger && } {setting.label} {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.label} {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.label} {passthroughConfig.description}
onChange({ cli_passthrough: checked })} disabled={lockPassthrough} data-settings-dirty={ Boolean(baselineProfile) && profile.cli_passthrough !== baselineProfile?.cli_passthrough } />
)}
); } 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; configOptions: SelectConfigOption[]; configStatus: ModelConfig["status"]; configError: string | null; configIsLoading: boolean; onRetryConfig: () => Promise; agentName: string; baselineProfile?: ProfileFormData; }; function CapabilitiesRow(props: CapabilitiesRowProps) { const { t } = useTranslation(); const gapCls = props.isCompact ? "space-y-1.5" : "space-y-2"; if (props.isLoading && props.models.length === 0) { return (
{t("agents:startModel")}
); } if (props.status === "probing") { return ; } if (props.status === "auth_required" || props.status === "not_installed") { return ( ); } return ; } function CapabilitiesRowContent({ profile, models, modes, commands, currentModelId, currentModeId, status, onChange, isCompact, isLoading, onRefresh, error, modelConfig, configOptions, configStatus, configError, configIsLoading, onRetryConfig, baselineProfile, }: CapabilitiesRowProps) { const { t } = useTranslation(); const hasModes = modes.length > 0; const activeMode = findActiveMode(modes, profile.mode, currentModeId); const labelCls = isCompact ? "text-xs" : undefined; const gapCls = isCompact ? "space-y-1.5" : "space-y-2"; return (
{t("agents:startModel")}
{hasModes && (
{t("agents:startMode")}
)}
{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: value })} dirty={baselineName !== undefined && profile.name !== baselineName} /> {canRemove && onRemove && ( )}
); } export type ProfileNameFieldProps = { value: string; onChange: (value: string) => void; dirty?: boolean; id?: string; testId?: string; }; export function ProfileNameField({ value, onChange, dirty = false, id, testId = "profile-name-input", }: ProfileNameFieldProps) { const { t } = useTranslation(); return (
{t("agents:profileName")} onChange(event.target.value)} placeholder={t("agents:defaultProfile")} data-settings-dirty={dirty} />
); } export function ProfileFormFields({ profile, baselineProfile, onChange, modelConfig, permissionSettings, passthroughConfig, agentName, onRemove, canRemove = false, variant = "default", hideNameField = false, lockPassthrough = false, hideCustomCLIFlags = false, onModelConfigResolutionPendingChange, }: ProfileFormFieldsProps) { const isCompact = variant === "compact"; const { capabilities: caps, configOptions: resolvedConfigOptions, configStatus, configError, configIsLoading, isConfigResolutionPending, refreshModelConfig, refresh, } = useProfileModelCapabilities(agentName, profile, modelConfig, onChange); const configOptions = modelConfigOptions( resolvedConfigOptions ? { ...modelConfig, config_options: resolvedConfigOptions } : modelConfig, ); useEffect(() => { onModelConfigResolutionPendingChange?.(isConfigResolutionPending); }, [isConfigResolutionPending, onModelConfigResolutionPendingChange]); return (
{!hideNameField && ( )}
); } function ProfileFormFooter({ profile, baselineProfile, onChange, permissionSettings, variant, hideCustomCLIFlags, models, configOptions, isCompact, }: { profile: ProfileFormData; baselineProfile?: ProfileFormData; onChange: (patch: Partial) => void; permissionSettings: Record; variant: "default" | "compact"; hideCustomCLIFlags: boolean; models: ModelEntry[]; configOptions: SelectConfigOption[]; isCompact: boolean; }) { return ( <>
onChange({ cli_flags: next })} permissionSettings={permissionSettings} variant={variant} hideCustomFlags={hideCustomCLIFlags} />
{!profile.cli_passthrough && ( )}
); }