"use client"; import { useTranslation } from "react-i18next"; import type { TFunction } from "i18next"; import { IconAlertTriangle, IconChevronDown, IconLoader2, IconRefresh } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@kandev/ui/dialog"; import { Drawer, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, } from "@kandev/ui/drawer"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint"; import { canApproveAgentRuntimeUpdate, resolveRuntimeActiveVersion, resolveRuntimeOperation, resolveRuntimeVersionPair, latestRuntimeVersions, runtimeOperationLabelKey, } from "@/lib/agent-runtime-update"; import type { AgentUpdateJob, AgentUpdatePreview, InstallJob } from "@/lib/api"; import type { RuntimeUpdate } from "@/lib/types/http"; import { useAgentUpdateDialogState } from "./use-agent-update-dialog-state"; const UPDATE_AGENT_KEY = "agents:updateAgent"; const ACTIVE_UPDATE_STATUSES = new Set([ "queued", "resolving", "updating", "refreshing", ]); // The job `status` values are the wire enum; only the phase labels are copy, so // they travel as catalog keys and resolve at render. const UPDATE_PHASE_KEYS: Partial> = { queued: "agents:updatePhaseQueued", resolving: "agents:updatePhaseResolving", updating: "agents:updatePhaseUpdating", refreshing: "agents:updatePhaseRefreshing", }; function updatePhase(t: TFunction, status: AgentUpdateJob["status"] | undefined): string | null { const key = status ? UPDATE_PHASE_KEYS[status] : undefined; return key ? t(key) : null; } function UpdateResult({ agentName, job }: { agentName: string; job?: AgentUpdateJob }) { const { t } = useTranslation(); if (!job) return null; const isUpToDate = job.operation === "up_to_date"; if (job.status === "succeeded" && job.refresh_error) { return (

{t("agents:runtimeUpdatedRefreshFailed", { error: job.refresh_error })}

); } if (job.status === "succeeded") { return (

{isUpToDate ? t("agents:runtimeAlreadyUpToDate") : t("agents:runtimeUpdatedSuccess")}

); } if (job.status === "failed") { return (

{job.error || t("agents:runtimeUpdateFailed")}

); } return null; } type UpdateBodyProps = { agentName: string; preview: AgentUpdatePreview | null; loading: boolean; previewError: string | null; approveError: string | null; job?: AgentUpdateJob; onRetryPreview: () => void; selectedTarget: string; onSelectTarget: (targetVersion: string) => void; starting: boolean; }; function RuntimeVersionSummary({ agentName, preview, job, }: { agentName: string; preview: AgentUpdatePreview; job?: AgentUpdateJob; }) { const { t } = useTranslation(); const { currentVersion, targetVersion } = resolveRuntimeVersionPair(preview, job); const activeVersion = resolveRuntimeActiveVersion(preview, job); const operation = resolveRuntimeOperation(preview, job); const isUpToDate = operation === "up_to_date"; return (

{t(runtimeOperationLabelKey(operation))}

{isUpToDate ? currentVersion : `${currentVersion} → ${targetVersion}`}

{activeVersion && (

{t("agents:activeRuntimeVersion", { version: activeVersion })}

)} {isUpToDate && (

{t("agents:upToDate")}

)}
); } function RuntimeVersionSelector({ agentName, preview, selectedTarget, loading, starting, job, onSelectTarget, }: { agentName: string; preview: AgentUpdatePreview; selectedTarget: string; loading: boolean; starting: boolean; job?: AgentUpdateJob; onSelectTarget: (targetVersion: string) => void; }) { const { t } = useTranslation(); const versions = latestRuntimeVersions( preview.available_versions ?? [{ version: preview.target_version, latest: false }], ); const activeVersion = resolveRuntimeActiveVersion(preview, job); return (
{loading ? ( ) : (
); } function UpdateBody({ agentName, preview, loading, previewError, approveError, job, onRetryPreview, selectedTarget, onSelectTarget, starting, }: UpdateBodyProps) { const { t } = useTranslation(); const phase = updatePhase(t, job?.status); return (
{loading && !preview && (

{t("agents:checkingLatestRuntimeVersion")}

)} {previewError && (

{previewError}

)} {approveError && (

{t("agents:unableToStartUpdate", { error: approveError })}

)} {preview && ( <>

{t("agents:runtimeUpdateExplainer")}

{t("agents:runtimeUpdateSessionsNote")}

{t("agents:commandThatWillRun")}

              {preview.command_string}
            
)} {phase && (

{phase}

)} {job?.output && (
          {job.output}
        
)}
); } type UpdateFooterProps = { agentName: string; preview: AgentUpdatePreview | null; previewError: string | null; job?: AgentUpdateJob; loading: boolean; starting: boolean; installInFlight: boolean; onApprove: () => void; onClose: () => void; mobile?: boolean; }; function UpdateFooter({ agentName, preview, previewError, job, loading, starting, installInFlight, onApprove, onClose, mobile, }: UpdateFooterProps) { const { t } = useTranslation(); const updateInFlight = Boolean(job && ACTIVE_UPDATE_STATUSES.has(job.status)); const canRetry = job?.status === "failed"; const canApprove = canApproveAgentRuntimeUpdate({ preview, job, previewError, loading, updateInFlight, starting, installInFlight, }); const showApprove = !job || canRetry; const content = ( <> {showApprove && ( )} ); if (mobile) { return ( {content} ); } return {content}; } function UpdateTrigger({ agentName, displayName, installInFlight, onOpen, }: { agentName: string; displayName: string; installInFlight: boolean; onOpen: () => void; }) { const { t } = useTranslation(); return ( {installInFlight ? t("agents:agentInstallationInProgress") : t(UPDATE_AGENT_KEY, { name: displayName })} ); } export function AgentRuntimeUpdateControl({ agentName, displayName, runtimeUpdate, job, installJob, onPreview, onUpdate, }: { agentName: string; displayName: string; runtimeUpdate: RuntimeUpdate; job?: AgentUpdateJob; installJob?: InstallJob; onPreview: (agentName: string, targetVersion?: string) => Promise; onUpdate: (agentName: string, targetVersion: string) => Promise; }) { const { t } = useTranslation(); const { isMobile } = useResponsiveBreakpoint(); const { activeJob, approve, approveError, handleOpenChange, loading, loadPreview, open, preview, previewError, selectTarget, selectedTarget, starting, } = useAgentUpdateDialogState({ agentName, job, onPreview, onUpdate }); const installInFlight = installJob?.status === "queued" || installJob?.status === "running"; if (!runtimeUpdate.supported) return null; const body = ( void loadPreview(selectedTarget || undefined)} selectedTarget={selectedTarget} onSelectTarget={selectTarget} starting={starting} /> ); const footer = (mobile = false) => ( void approve()} onClose={() => handleOpenChange(false)} mobile={mobile} /> ); return ( <> handleOpenChange(true)} /> {isMobile ? ( {t(UPDATE_AGENT_KEY, { name: displayName })} {t("agents:reviewUpdateBeforeApplying")} {body} {footer(true)} ) : ( {t(UPDATE_AGENT_KEY, { name: displayName })} {t("agents:reviewUpdateBeforeApplying")} {body} {footer()} )} ); }