import { stream } from '../src/index.js'; import type { QoreEventStream, QoreStream, StreamInput } from '../src/index.js'; export type AgentStatus = 'queued' | 'thinking' | 'calling_tool' | 'streaming' | 'repairing' | 'done'; export type AgentEvent = | { type: 'status'; value: AgentStatus; label: string } | { type: 'text'; text: string } | { type: 'tool_call'; name: string; input: string } | { type: 'tool_result'; name: string; output: string } | { type: 'diff'; patch: string } | { type: 'artifact'; title: string; content: string } | { type: 'retry'; attempt: number; reason: string } | { type: 'error'; message: string; recoverable: boolean }; type EventOf = Extract; export interface AgentEventDemo { events: QoreEventStream; markdown: QoreStream, string>; statuses: QoreStream, Array>>; toolCalls: QoreStream, Array>>; toolResults: QoreStream, Array>>; diff: QoreStream, string>; artifacts: QoreStream, Array>>; retries: QoreStream, Array>>; errors: QoreStream, Array>>; } function collectEvents(name: string) { return { name, seed: [] as TEvent[], reduce: (currentValue: TEvent[], event: TEvent) => [...currentValue, event] }; } export const agentEventTimeline: AgentEvent[] = [ { type: 'status', value: 'queued', label: 'Queued request' }, { type: 'status', value: 'thinking', label: 'Planning answer surfaces' }, { type: 'tool_call', name: 'search_docs', input: 'Qore stream.events selectors' }, { type: 'tool_result', name: 'search_docs', output: 'Found runtime, API, and benchmark notes.' }, { type: 'status', value: 'streaming', label: 'Streaming markdown' }, { type: 'text', text: '### Agent Event Stream\n' }, { type: 'text', text: 'One stream feeds timeline, markdown, tools, diff, artifacts, and status.\n' }, { type: 'diff', patch: '+ const events = stream.events(agent.run(task))\n' }, { type: 'diff', patch: '+ const text = events.select(\'text\', reducer)\n' }, { type: 'retry', attempt: 1, reason: 'tool result arrived late; continuing from event history' }, { type: 'status', value: 'repairing', label: 'Repairing partial UI state' }, { type: 'artifact', title: 'Runtime trace', content: 'status -> tool_call -> tool_result -> text -> diff -> artifact' }, { type: 'error', message: 'Recovered transient tool timeout', recoverable: true }, { type: 'status', value: 'done', label: 'Completed' } ]; export function createAgentEventDemo( source: StreamInput = agentEventTimeline ): AgentEventDemo { const events = stream.events(source, { name: 'agent-events' }); const markdown = events.select('text', { name: 'agent-markdown', seed: '', reduce: (currentValue, event) => currentValue + event.text }); const statuses = events.select('status', collectEvents>('agent-status')); const toolCalls = events.select('tool_call', collectEvents>('agent-tool-calls')); const toolResults = events.select('tool_result', collectEvents>('agent-tool-results')); const diff = events.select('diff', { name: 'agent-diff', seed: '', reduce: (currentValue, event) => currentValue + event.patch }); const artifacts = events.select('artifact', collectEvents>('agent-artifacts')); const retries = events.select('retry', collectEvents>('agent-retries')); const errors = events.select('error', collectEvents>('agent-errors')); return { events, markdown, statuses, toolCalls, toolResults, diff, artifacts, retries, errors }; }