"use client"; import { IconAlertTriangle, 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 type { AgentUpdateJob, AgentUpdatePreview, InstallJob } from "@/lib/api"; import type { RuntimeUpdate } from "@/lib/types/http"; import { useAgentUpdateDialogState } from "./use-agent-update-dialog-state"; const ACTIVE_UPDATE_STATUSES = new Set([ "queued", "resolving", "updating", "refreshing", ]); function updatePhase(status: AgentUpdateJob["status"] | undefined): string | null { switch (status) { case "queued": return "Update queued…"; case "resolving": return "Checking latest version…"; case "updating": return "Updating runtime…"; case "refreshing": return "Refreshing agent capabilities…"; default: return null; } } function UpdateResult({ agentName, job }: { agentName: string; job?: AgentUpdateJob }) { if (!job) return null; if (job.status === "succeeded" && job.refresh_error) { return (

Runtime updated, but capabilities could not be refreshed: {job.refresh_error}

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

Runtime updated successfully.

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

{job.error || "The runtime update failed."}

); } return null; } type UpdateBodyProps = { agentName: string; preview: AgentUpdatePreview | null; loading: boolean; previewError: string | null; approveError: string | null; job?: AgentUpdateJob; onRetryPreview: () => void; }; function UpdateBody({ agentName, preview, loading, previewError, approveError, job, onRetryPreview, }: UpdateBodyProps) { const phase = updatePhase(job?.status); return (
{loading && (

Checking the latest runtime version…

)} {previewError && (

{previewError}

)} {approveError && (

Unable to start update: {approveError}

)} {preview && ( <>

Runtime version

{(job?.current_version || preview.current_version || "Unknown") + " → " + (job?.target_version || preview.target_version)}

This updates the managed runtime on this Kandev host, then refreshes its models and capabilities.

Active sessions keep running; only later probes and launches use the refreshed runtime.

Command that will run

              {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 canApproveUpdate({ preview, previewError, loading, updateInFlight, starting, installInFlight, }: { preview: AgentUpdatePreview | null; previewError: string | null; loading: boolean; updateInFlight: boolean; starting: boolean; installInFlight: boolean; }) { return ( Boolean(preview?.current_version) && !previewError && !loading && !updateInFlight && !starting && !installInFlight ); } function UpdateFooter({ agentName, preview, previewError, job, loading, starting, installInFlight, onApprove, onClose, mobile, }: UpdateFooterProps) { const updateInFlight = Boolean(job && ACTIVE_UPDATE_STATUSES.has(job.status)); const canRetry = job?.status === "failed"; const canApprove = canApproveUpdate({ preview, 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; }) { return ( {installInFlight ? "Agent installation is in progress" : `Update ${displayName}`} ); } export function AgentRuntimeUpdateControl({ agentName, displayName, runtimeUpdate, job, installJob, onPreview, onUpdate, }: { agentName: string; displayName: string; runtimeUpdate: RuntimeUpdate; job?: AgentUpdateJob; installJob?: InstallJob; onPreview: (agentName: string) => Promise; onUpdate: (agentName: string) => Promise; }) { const { isMobile } = useResponsiveBreakpoint(); const { activeJob, approve, approveError, handleOpenChange, loading, loadPreview, open, preview, previewError, starting, } = useAgentUpdateDialogState({ agentName, job, onPreview, onUpdate }); const installInFlight = installJob?.status === "queued" || installJob?.status === "running"; if (!runtimeUpdate.supported) return null; const body = ( void loadPreview()} /> ); const footer = (mobile = false) => ( void approve()} onClose={() => handleOpenChange(false)} mobile={mobile} /> ); return ( <> handleOpenChange(true)} /> {isMobile ? ( Update {displayName} Review the update before Kandev changes this host runtime. {body} {footer(true)} ) : ( Update {displayName} Review the update before Kandev changes this host runtime. {body} {footer()} )} ); }