import {background, deployStep} from '@/lib/stream' import type {Hosted, LogItem} from '@/lib/stream' /** What the agent is doing now: in words, since when (unix seconds), and how long it takes as a rule, where that is known. */ export interface Doing { text: string since: number | null typical: number | null } /** * What the agent is doing now, while it works: the tool it is running; else, its turn * over, what it left running in the background; else writing or thinking. As the hub's * `doing` says it (sessions.rs), which the Home card shows: a session waiting on a * landing it started read here as thinking for as long as the landing took. */ export function activity(items: LogItem[], s: Hosted): Doing | null { // Its runner stopped asking for work: nothing runs, whatever its status last said. if (s.runner_away) return {text: 'Paused: its runner is away', since: s.runner_away, typical: null} if (s.status === 'queued' || s.status === 'starting') return {text: 'Starting', since: s.updated, typical: null} // Idle, but something it started goes on: the deploy it waits for. if (s.status !== 'working') { const d = s.status === 'waiting' ? s.deploys?.[0] : undefined if (!d) return null // The step it is at, and that step's clock: what takes the time. const at = deployStep(d) return at ? {text: `${d.target} deploy, step ${at.text}`, since: at.step.since ?? null, typical: at.step.typical ?? null} : {text: background(s) ?? '', since: Date.parse(d.since) / 1000 || null, typical: d.typical ?? null} } const running = new Map() // What runs in the background, as last said; when each task was first seen; whether a turn has ended since a prompt. let tasks: NonNullable = [] const seen = new Map() let between = false for (const it of items) { if (it.kind === 'tool') running.set(it.id ?? '', it) if (it.kind === 'tool_update' && (it.status === 'completed' || it.status === 'failed')) running.delete(it.id ?? '') if (it.kind === 'turn_end') { running.clear() between = true } if (it.kind === 'user') between = false if (it.kind === 'background') { tasks = it.tasks ?? [] for (const t of tasks) if (!seen.has(t.id)) seen.set(t.id, it.at) } } const usual = (id: string | undefined) => (id && s.expect?.[id]) || null const now = [...running.values()].pop() if (now) return { text: (typeof now.input?.description === 'string' && now.input.description) || now.title || 'Working', since: now.at, typical: usual(now.id), } if (between && tasks.length) { const first = tasks[0].description?.split('\n')[0] || 'a task' const more = tasks.length > 1 ? ` and ${tasks.length - 1} more` : '' return {text: `In the background: ${first}${more}`, since: seen.get(tasks[0].id) ?? null, typical: usual(tasks[0].id)} } // Writing since its reply began; thinking since the last thing that happened. let i = items.length - 1 while (i > 0 && (items[i].kind === 'usage' || items[i].kind === 'background')) i-- const writing = items[i]?.kind === 'agent' if (writing) while (i > 0 && items[i - 1].kind === 'agent') i-- return {text: writing ? 'Writing' : 'Thinking', since: items[i]?.at ?? null, typical: null} }