Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798// What an assistant did while it worked, as the pane shows it live and keeps// it with the reply: the steps in order (where the request went, the tools// an agent used and what they answered), the words as they came, the// reasoning when the road carried it, and what it all cost. Built from the// assistant's events by a pure reducer, so a host can show it any way it// likes.import type { AssistantEvent } from './assistant/types.ts';
export type ActivityStep = { /** Milliseconds after the request began. */ at: number; kind: 'status' | 'tool' | 'error'; text: string; /** For a tool: what it was given. */ input?: string; /** For a tool: what it answered, shortened. */ result?: string; /** The tool failed, or the step is an error. */ error?: boolean; id?: string;};
export type ActivityUsage = { input?: number; output?: number; cost?: number; duration?: number };
export type Activity = { /** When the request began (epoch ms). */ started: number; /** When it ended, once it has. */ ended?: number; steps: ActivityStep[]; /** The reply as it came. */ text: string; /** The model's reasoning, when the road carried it. */ thinking: string; usage?: ActivityUsage; /** When the first words came, ms after the start. */ firstWords?: number; /** The writer stopped it. */ stopped?: boolean;};
export const newActivity = (started = Date.now()): Activity => ({ started, steps: [], text: '', thinking: '' });
const TEXT_LIMIT = 200_000;
/** The activity with the event folded in (a new object: signals compare by identity). */export function applyEvent(a: Activity, e: AssistantEvent, now = Date.now()): Activity { const at = Math.max(0, now - a.started); switch (e.kind) { case 'status': return { ...a, steps: [...a.steps, { at, kind: 'status', text: e.text }] }; case 'text': { const first = a.text === '' && e.delta !== '' && a.firstWords === undefined; return { ...a, text: (a.text + e.delta).slice(0, TEXT_LIMIT), ...(first ? { firstWords: at } : {}) }; } case 'thinking': return { ...a, thinking: (a.thinking + e.delta).slice(0, TEXT_LIMIT) }; case 'tool': return { ...a, steps: [...a.steps, { at, kind: 'tool', text: e.name, input: e.input, ...(e.id ? { id: e.id } : {}) }] }; case 'tool-result': { // the answer goes on the tool that asked (by id, else the last tool still waiting) const steps = a.steps.slice(); let i = e.id ? steps.findIndex((s) => s.kind === 'tool' && s.id === e.id) : -1; if (i < 0) for (i = steps.length - 1; i >= 0; i--) if (steps[i]!.kind === 'tool' && steps[i]!.result === undefined) break; if (i < 0) return { ...a, steps: [...steps, { at, kind: e.error ? 'error' : 'status', text: e.text, ...(e.error ? { error: true } : {}) }] }; steps[i] = { ...steps[i]!, result: e.text, ...(e.error ? { error: true } : {}) }; return { ...a, steps }; } case 'usage': return { ...a, usage: { ...(a.usage ?? {}), ...Object.fromEntries(Object.entries(e).filter(([k, v]) => k !== 'kind' && typeof v === 'number')) } }; }}
/** The activity, over: with its end, and the error that ended it when there was one. */export function endActivity(a: Activity, { error, stopped, now = Date.now() }: { error?: string; stopped?: boolean; now?: number } = {}): Activity { const steps = error ? [...a.steps, { at: Math.max(0, now - a.started), kind: 'error' as const, text: error, error: true }] : a.steps; return { ...a, ended: now, steps, ...(stopped ? { stopped: true } : {}) };}
/** Seconds, as a pane shows them: "12 s", "1 min 30 s". */export const seconds = (ms: number): string => { const s = Math.max(0, Math.round(ms / 1000)); return s < 60 ? `${s} s` : `${Math.floor(s / 60)} min ${s % 60} s`;};
/** One line on the whole: "42 s · 3 tools used · 1,230 tokens out · $0.04". */export function summariseActivity(a: Activity, now = Date.now()): string { const parts: string[] = []; parts.push(a.stopped ? `stopped after ${seconds((a.ended ?? now) - a.started)}` : seconds((a.ended ?? now) - a.started)); const tools = a.steps.filter((s) => s.kind === 'tool').length; if (tools) parts.push(`${tools} tool${tools === 1 ? '' : 's'} used`); const u = a.usage; if (u?.output) parts.push(`${u.output.toLocaleString()} tokens out`); else if (a.text) parts.push(`${a.text.length.toLocaleString()} characters`); if (u?.cost) parts.push(u.cost < 0.01 ? `under 1¢` : `$${u.cost.toFixed(2)}`); return parts.join(' · ');}