'use client'; import { useEffect, useMemo, useState } from 'react'; import { IconCheck, IconLoader2, IconAlertCircle, IconPlayerStopFilled, IconPlus, IconX } from '@tabler/icons-react'; import type { TaskSession, TaskSessionState } from '@/lib/types/http'; import { Badge } from '@kandev/ui/badge'; import { Command, CommandGroup, CommandItem, CommandList } from '@kandev/ui/command'; import { Tooltip, TooltipContent, TooltipTrigger } from '@kandev/ui/tooltip'; import { useAppStore } from '@/components/state-provider'; type SessionSwitcherProps = { taskId: string | null; sessions: TaskSession[]; onSelectSession: (taskId: string, sessionId: string) => void; showHeader?: boolean; onCreateSession?: () => void; }; const STATUS_ORDER: Record = { RUNNING: 1, STARTING: 1, WAITING_FOR_INPUT: 2, CREATED: 3, COMPLETED: 4, FAILED: 5, CANCELLED: 6, }; type SessionStatus = 'running' | 'waiting_input' | 'complete' | 'failed' | 'cancelled' | 'starting'; function getSessionStatus(state: TaskSessionState): SessionStatus { switch (state) { case 'RUNNING': return 'running'; case 'STARTING': return 'starting'; case 'WAITING_FOR_INPUT': return 'waiting_input'; case 'COMPLETED': return 'complete'; case 'FAILED': return 'failed'; case 'CANCELLED': return 'cancelled'; default: return 'waiting_input'; } } function getSessionIcon(status: SessionStatus) { switch (status) { case 'running': case 'starting': return ; case 'waiting_input': return ; case 'complete': return ; case 'failed': case 'cancelled': return ; default: return null; } } function formatSessionLabel(index: number, total: number) { const number = total - index; return `#${number}`; } function formatDuration(startedAt: string, isRunning: boolean, now: number) { const start = new Date(startedAt).getTime(); const diff = Math.floor(((isRunning ? now : start) - start) / 1000); const hours = Math.floor(diff / 3600); const minutes = Math.floor((diff % 3600) / 60); const seconds = diff % 60; if (hours > 0) { return `${hours}h ${minutes}m`; } if (minutes > 0) { return `${minutes}m ${seconds}s`; } return `${seconds}s`; } export function TaskSessionSwitcher({ taskId, sessions, onSelectSession, showHeader = true, onCreateSession, }: SessionSwitcherProps) { const activeSessionId = useAppStore((state) => state.tasks.activeSessionId); const agentProfiles = useAppStore((state) => state.agentProfiles.items); const agentLabelsById = useMemo(() => { return Object.fromEntries(agentProfiles.map((profile) => [profile.id, profile.label])); }, [agentProfiles]); const [currentTime, setCurrentTime] = useState(() => Date.now()); const sortedSessions = useMemo(() => { return [...sessions].sort((a, b) => { const orderDelta = (STATUS_ORDER[a.state] ?? 99) - (STATUS_ORDER[b.state] ?? 99); if (orderDelta !== 0) return orderDelta; return new Date(b.started_at).getTime() - new Date(a.started_at).getTime(); }); }, [sessions]); const resolveAgentLabel = (session: TaskSession) => { if (session.agent_profile_id && agentLabelsById[session.agent_profile_id]) { return agentLabelsById[session.agent_profile_id]; } return 'Unknown agent'; }; useEffect(() => { const hasRunning = sortedSessions.some((session) => ['RUNNING', 'STARTING'].includes(session.state) ); if (!hasRunning) return; const interval = setInterval(() => { setCurrentTime(Date.now()); }, 1000); return () => clearInterval(interval); }, [sortedSessions]); return (
{showHeader ? (
Sessions
{onCreateSession ? ( New session ) : null} {sortedSessions.length}
) : null} {sortedSessions.length === 0 ? (
No sessions yet.
) : ( {sortedSessions.map((session, index) => { const isActive = session.id === activeSessionId; const agentLabel = resolveAgentLabel(session); const status = getSessionStatus(session.state); const duration = formatDuration( session.started_at, status === 'running' || status === 'starting', currentTime ); return ( { if (!taskId) return; onSelectSession(taskId, session.id); }} className={`group flex w-full cursor-pointer items-center gap-3 px-2 py-1.5 rounded-sm text-left transition-colors ${ isActive ? 'bg-muted' : 'hover:bg-muted/30' }`} > {formatSessionLabel(index, sortedSessions.length)} {agentLabel} {duration}
{getSessionIcon(status)}
{(status === 'running' || status === 'starting') && ( Stop session )}
); })}
)}
); }