import { tr } from '../lib/i18n'; import { useRef, useState } from 'react'; import { Btn, Dialog, type IconName } from './ui'; import { api } from '../lib/client'; import { plainFailureText } from '../lib/plain-errors'; import { compactTokens, elapsed } from '../lib/chat-metrics'; import type { Session } from '../lib/types'; type Agent = NonNullable[number]; const states: Record = { working: { label: 'In progress', icon: 'clock' }, completed: { label: 'Completed', icon: 'check' }, failed: { label: 'Failed', icon: 'error' }, queued: { label: 'Queued', icon: 'clock' }, cancelled: { label: 'Cancelled', icon: 'x' }, }; export function SubagentRow({ agent: a, now = Date.now(), sessionId }: { agent: Agent; now?: number; /** The chat the helper belongs to: with it the row can take a message for the helper (#4749). */ sessionId?: string; }) { const [log, setLog] = useState(false); const [writing, setWriting] = useState(false), [draft, setDraft] = useState(''), [busy, setBusy] = useState(false), [sent, setSent] = useState(false), [problem, setProblem] = useState(''); // A working helper gets it at its next step (steer); one that has finished or waits gets a new turn with it (queue). async function send() { const message = draft.trim(); if (!message || !sessionId || busy) return; setBusy(true); setProblem(''); setSent(false); try { await api.post('/api/chat/helper', { sessionId, helperId: a.id, message, delivery: a.status === 'working' ? 'steer' : 'queue' }); setDraft(''); setSent(true); } catch (e) { setProblem(plainFailureText(e instanceof Error ? e.message : String(e)).message); } finally { setBusy(false); } } const observedAt = useRef(Date.now()); const duration = a.durationMs + (a.status === 'working' ? Math.max(0, now - (a.updatedAt ?? observedAt.current)) : 0); const state = states[a.status] ?? states.queued; return
{a.name}{tr(state.label)}

{a.description}

{tr(a.status === 'failed' ? : null)}{tr(sessionId && a.status !== 'queued' && a.status !== 'cancelled' ? <>{writing ?
{ e.preventDefault(); void send(); }}> { setDraft(e.target.value); setSent(false); }}/>{tr("Send")}
: }{tr(sent ?

{tr("Sent to {0}.", [a.name])}

: null)}{tr(problem ?

{problem}

: null)} : null)}
{tr(compactTokens(a.tokens))}{tr(" tok")}{tr(elapsed(duration))}
setLog(false)} actions={ setLog(false)}>{tr("Done")}}>

{a.description}

{tr("Status: ")}{tr(state.label)} · {tr(elapsed(a.durationMs))} · {tr(compactTokens(a.tokens))}{tr(" tokens")}

; }