export type AgentActivity = { state: "idle" | "queued" | "running" | "waiting_input"; running: boolean; queued: boolean; observed_at_ms: number; active_task_ids: string[]; question?: { question?: string; reason?: string; options?: unknown[] } | null; }; export type AgentRow = { id: string; name: string; expertise?: string; preview?: string; running?: boolean; activity?: AgentActivity; }; export type TranscriptItem = { role: "user" | "assistant"; content: string; at?: string }; export type AgentDetail = { id: string; name: string; expertise: string; preview: string; transcript: TranscriptItem[]; running: boolean; activity: AgentActivity; }; export type TeamEvent = { id: number; kind: string; task_id?: string | null; payload: Record; }; async function request(path: string, init?: RequestInit): Promise { const res = await fetch(path, { ...init, headers: { "content-type": "application/json", ...(init?.headers || {}) }, }); const raw = await res.text(); let data: unknown = null; try { data = raw ? JSON.parse(raw) : null; } catch { data = raw; } if (!res.ok) { const err = data as { error?: string } | null; throw new Error(err?.error || raw || String(res.status)); } return data as T; } export const api = { health: () => request<{ ok: boolean; service: boolean; model: string }>("/api/health"), agents: async () => { const data = await request("/api/agents"); const agents = Array.isArray(data) ? data : data.agents || []; return { agents }; }, createAgent: (name: string) => request("/api/agents", { method: "POST", body: JSON.stringify({ name }) }), deleteAgent: (id: string) => request<{ ok: boolean; id: string; name: string }>(`/api/agents/${id}`, { method: "DELETE" }), agent: (id: string) => request(`/api/agents/${id}`), activity: (id: string) => request(`/api/agents/${id}/activity`, { signal: AbortSignal.timeout(8000) }), send: (id: string, text: string) => request<{ queued?: string }>(`/api/agents/${id}/messages`, { method: "POST", body: JSON.stringify({ text }) }), stop: (id: string) => request(`/api/agents/${id}/stop`, { method: "POST", body: "{}" }), computer: (action: "start" | "restart" | "update" = "start") => request<{ ready: boolean; viewer_url: string; error?: string; updated?: boolean; revision?: string; }>("/api/computer", { method: "POST", body: JSON.stringify({ action }), }), };