import { asRecord, type ChannelTurnProgressBuilderOptions, firstNonEmptyString, formatToolProgressDetails, formatToolProgressTitle, MAX_PROGRESS_DETAILS_LENGTH, parseToolArguments, sanitizeChannelProgressIdentifier, sanitizeChannelProgressText, type ToolCallSummary, type ToolReturnSummary, } from "./progress-formatting"; import type { ChannelTurnProgressUpdate } from "./progress-types"; function getMessageType(delta: Record): string | null { return firstNonEmptyString(delta.message_type, delta.messageType) ?? null; } function getRunId(delta: Record): string | undefined { return firstNonEmptyString(delta.run_id, delta.runId); } function withRunId( update: Omit, runId?: string, ): ChannelTurnProgressUpdate { return { ...update, ...(runId ? { runId } : {}), }; } function getCommandId(delta: Record): string { const command = firstNonEmptyString(delta.command_id, delta.commandId); return sanitizeChannelProgressIdentifier(command, "command"); } function getSlashCommand(delta: Record): string { const commandId = firstNonEmptyString(delta.command_id, delta.commandId); const command = commandId ? `/${commandId.replace(/^\/+/, "")}` : "command"; return sanitizeChannelProgressIdentifier(command, "command"); } function getToolStatus(delta: Record): "completed" | "error" { const status = firstNonEmptyString(delta.status)?.toLowerCase(); return status === "error" || status === "failed" ? "error" : "completed"; } function stringifyProgressValue(value: unknown): string | undefined { if (typeof value === "string") { return value; } if (value === undefined || value === null) { return undefined; } try { return JSON.stringify(value); } catch { return String(value); } } function formatToolErrorDetails( record: Record, ): string | undefined { const toolReturn = record.tool_return ?? record.toolReturn; const toolReturnRecord = asRecord(toolReturn); const preview = firstNonEmptyString( record.stderr, record.error, record.message, toolReturnRecord?.stderr, toolReturnRecord?.error, toolReturnRecord?.message, toolReturnRecord?.output, stringifyProgressValue(toolReturn), ); const sanitized = sanitizeChannelProgressText( preview, MAX_PROGRESS_DETAILS_LENGTH, ); return sanitized || undefined; } function toolNameForMessage(summary: ToolCallSummary | undefined): string { return summary?.name ? `: ${summary.name}` : ""; } export type ChannelTurnProgressBuilder = { buildUpdates(delta: unknown): ChannelTurnProgressUpdate[]; }; export function createChannelTurnProgressBuilder( options: ChannelTurnProgressBuilderOptions = {}, ): ChannelTurnProgressBuilder { const argumentsByToolCallId = new Map(); const namesByToolCallId = new Map(); const batchesByStep = new Map(); const batchesByToolCallId = new Map(); function addToolBatchTitles( delta: unknown, updates: ChannelTurnProgressUpdate[], ): ChannelTurnProgressUpdate[] { const record = asRecord(delta); if (!record) return updates; const messageType = getMessageType(record); const isToolRequest = messageType === "tool_call_message" || messageType === "approval_request_message"; const stepKey = isToolRequest ? firstNonEmptyString(record.step_id, record.id) : undefined; let requestBatch = stepKey ? batchesByStep.get(stepKey) : undefined; if (stepKey && !requestBatch) { requestBatch = { title: null }; batchesByStep.set(stepKey, requestBatch); } return updates.map((update) => { if (update.kind !== "tool") return update; const batch = (update.toolCallId ? batchesByToolCallId.get(update.toolCallId) : undefined) ?? requestBatch; if (!batch) return update; if (update.toolCallId) batchesByToolCallId.set(update.toolCallId, batch); if (batch.title === null && update.state === "started") { batch.title = firstNonEmptyString(update.toolTitle, update.toolDetails) ?? null; } return { ...update, toolBatchTitle: batch.title }; }); } function extractToolCallSummary(value: unknown): ToolCallSummary | null { const record = asRecord(value); if (!record) { return null; } const id = firstNonEmptyString(record.tool_call_id); const cacheId = id ? sanitizeChannelProgressIdentifier(id, "tool-call") : undefined; const extractedName = firstNonEmptyString(record.name); if (cacheId && extractedName) { namesByToolCallId.set(cacheId, extractedName); } const resolvedName = extractedName ?? (cacheId ? namesByToolCallId.get(cacheId) : undefined); const rawArguments = typeof record.arguments === "string" && record.arguments.length > 0 ? record.arguments : asRecord(record.arguments) ? JSON.stringify(record.arguments) : undefined; let argumentsText: string | undefined; if (cacheId && rawArguments !== undefined) { const existing = argumentsByToolCallId.get(cacheId); if (parseToolArguments(rawArguments)) { argumentsByToolCallId.set(cacheId, rawArguments); argumentsText = rawArguments; } else if (existing) { if (parseToolArguments(existing)) { argumentsText = existing; } else { const accumulated = existing + rawArguments; argumentsByToolCallId.set(cacheId, accumulated); argumentsText = accumulated; } } else { argumentsByToolCallId.set(cacheId, rawArguments); argumentsText = rawArguments; } } else if (!id && rawArguments !== undefined) { argumentsText = rawArguments; } if (!id && !resolvedName) { return null; } return { ...(cacheId ? { id: cacheId } : {}), ...(resolvedName ? { name: sanitizeChannelProgressIdentifier(resolvedName, "tool") } : {}), ...(argumentsText ? { argumentsText } : {}), }; } function extractClientToolSummary( record: Record, ): ToolCallSummary | null { const id = firstNonEmptyString(record.tool_call_id, record.toolCallId); const cacheId = id ? sanitizeChannelProgressIdentifier(id, "tool-call") : undefined; const extractedName = firstNonEmptyString( record.tool_name, record.toolName, record.name, ); if (cacheId && extractedName) { namesByToolCallId.set(cacheId, extractedName); } const resolvedName = extractedName ?? (cacheId ? namesByToolCallId.get(cacheId) : undefined); const rawArguments = firstNonEmptyString( record.tool_args, record.toolArgs, record.arguments, ); if (cacheId && rawArguments) { argumentsByToolCallId.set(cacheId, rawArguments); } const argumentsText = rawArguments ?? (cacheId ? argumentsByToolCallId.get(cacheId) : undefined); if (!cacheId && !resolvedName) { return null; } return { ...(cacheId ? { id: cacheId } : {}), ...(resolvedName ? { name: sanitizeChannelProgressIdentifier(resolvedName, "tool") } : {}), ...(argumentsText ? { argumentsText } : {}), }; } function extractToolCalls(delta: Record): ToolCallSummary[] { const candidates: unknown[] = Array.isArray(delta.tool_calls) ? delta.tool_calls : delta.tool_calls ? [delta.tool_calls] : delta.tool_call ? [delta.tool_call] : []; const summaries: ToolCallSummary[] = []; const seen = new Set(); for (const candidate of candidates) { const summary = extractToolCallSummary(candidate); if (!summary) { continue; } const key = `${summary.id ?? ""}:${summary.name ?? ""}`; if (seen.has(key)) { continue; } seen.add(key); summaries.push(summary); } return summaries; } function extractToolReturns( delta: Record, ): ToolReturnSummary[] { const candidates: unknown[] = Array.isArray(delta.tool_returns) ? delta.tool_returns : [delta]; const summaries: ToolReturnSummary[] = []; const seen = new Set(); for (const candidate of candidates) { const record = asRecord(candidate); if (!record) { continue; } const summary = extractToolCallSummary(record); if (!summary) { continue; } const key = `${summary.id ?? ""}:${summary.name ?? ""}`; if (seen.has(key)) { continue; } seen.add(key); const status = getToolStatus(record); summaries.push({ summary, status, ...(status === "error" ? { errorDetails: formatToolErrorDetails(record) } : {}), }); } return summaries; } function buildToolCallUpdates( record: Record, runId: string | undefined, ): ChannelTurnProgressUpdate[] { const tools = extractToolCalls(record); const updates: ChannelTurnProgressUpdate[] = []; for (const tool of tools) { const toolDetails = formatToolProgressDetails(tool, options); const toolTitle = formatToolProgressTitle(tool, "started"); updates.push( withRunId( { kind: "tool", state: "started", message: `Preparing tool${toolNameForMessage(tool)}`, ...(tool.id ? { toolCallId: tool.id } : {}), ...(tool.name ? { toolName: tool.name } : {}), ...(toolDetails ? { toolDetails } : {}), ...(toolTitle ? { toolTitle } : {}), }, runId, ), ); } return updates; } function buildUpdates(delta: unknown): ChannelTurnProgressUpdate[] { const record = asRecord(delta); if (!record) { return []; } const messageType = getMessageType(record); const runId = getRunId(record); switch (messageType) { case "reasoning_message": return [ withRunId( { kind: "thinking", state: "updated", message: "Thinking", }, runId, ), ]; case "assistant_message": return [ withRunId( { kind: "responding", state: "updated", message: "Writing reply", }, runId, ), ]; case "approval_request_message": return buildToolCallUpdates(record, runId); case "tool_call_message": { const updates = buildToolCallUpdates(record, runId); if (updates.length === 0) { return [ withRunId( { kind: "tool", state: "started", message: "Preparing tool call", }, runId, ), ]; } return updates; } case "tool_return_message": { const toolReturns = extractToolReturns(record); const updates: ChannelTurnProgressUpdate[] = []; for (const { summary, status, errorDetails } of toolReturns) { const accumulatedArgs = summary.id ? argumentsByToolCallId.get(summary.id) : undefined; if (summary.id) { argumentsByToolCallId.delete(summary.id); namesByToolCallId.delete(summary.id); } const toolWithAccumulatedArgs = accumulatedArgs ? { ...summary, argumentsText: accumulatedArgs } : summary; const toolDetails = formatToolProgressDetails( toolWithAccumulatedArgs, options, ); const toolTitle = formatToolProgressTitle( toolWithAccumulatedArgs, status, ); updates.push( withRunId( { kind: "tool", state: status, message: status === "error" ? "Tool failed" : "Tool finished", ...(summary.id ? { toolCallId: summary.id } : {}), ...(summary.name ? { toolName: summary.name } : {}), ...(toolDetails ? { toolDetails } : {}), ...(status === "error" && errorDetails ? { errorDetails } : {}), ...(toolTitle ? { toolTitle } : {}), }, runId, ), ); } return updates; } case "client_tool_start": { const tool = extractClientToolSummary(record); const toolDetails = tool ? formatToolProgressDetails(tool, options) : undefined; const toolTitle = tool ? formatToolProgressTitle(tool, "started") : undefined; return [ withRunId( { kind: "tool", state: "started", message: "Running tool", ...(tool?.id ? { toolCallId: tool.id } : {}), ...(tool?.name ? { toolName: tool.name } : {}), ...(toolDetails ? { toolDetails } : {}), ...(toolTitle ? { toolTitle } : {}), }, runId, ), ]; } case "client_tool_end": { const state = getToolStatus(record); const tool = extractClientToolSummary(record); const accumulatedArgs = tool?.id ? argumentsByToolCallId.get(tool.id) : undefined; if (tool?.id) { argumentsByToolCallId.delete(tool.id); namesByToolCallId.delete(tool.id); } const toolWithAccumulatedArgs = tool && accumulatedArgs ? { ...tool, argumentsText: accumulatedArgs } : tool; const toolDetails = toolWithAccumulatedArgs ? formatToolProgressDetails(toolWithAccumulatedArgs, options) : undefined; const errorDetails = state === "error" ? formatToolErrorDetails(record) : undefined; const toolTitle = toolWithAccumulatedArgs ? formatToolProgressTitle(toolWithAccumulatedArgs, state) : undefined; return [ withRunId( { kind: "tool", state, message: state === "error" ? "Tool failed" : "Tool finished", ...(tool?.id ? { toolCallId: tool.id } : {}), ...(tool?.name ? { toolName: tool.name } : {}), ...(toolDetails ? { toolDetails } : {}), ...(errorDetails ? { errorDetails } : {}), ...(toolTitle ? { toolTitle } : {}), }, runId, ), ]; } case "slash_command_start": { const command = getSlashCommand(record); return [ withRunId( { kind: "command", state: "started", message: `Running ${command}`, command, }, runId, ), ]; } case "slash_command_end": { const command = getSlashCommand(record); const success = record.success !== false; return [ withRunId( { kind: "command", state: success ? "completed" : "error", message: success ? `${command} finished` : `${command} failed`, command, }, runId, ), ]; } case "command_start": { const command = getCommandId(record); return [ withRunId( { kind: "command", state: "started", message: "Running command", command, }, runId, ), ]; } case "command_end": { const command = getCommandId(record); const success = record.success !== false; return [ withRunId( { kind: "command", state: success ? "completed" : "error", message: success ? "Command finished" : "Command failed", command, }, runId, ), ]; } case "status": { const message = sanitizeChannelProgressText(record.message); if (!message) { return []; } return [ withRunId( { kind: "status", state: "updated", message, }, runId, ), ]; } case "retry": { const attempt = Number(record.attempt); const maxAttempts = Number(record.max_attempts ?? record.maxAttempts); const suffix = Number.isFinite(attempt) && Number.isFinite(maxAttempts) ? ` (${attempt}/${maxAttempts})` : ""; return [ withRunId( { kind: "retry", state: "updated", message: `Retrying request${suffix}`, }, runId, ), ]; } case "loop_error": return [ withRunId( { kind: "error", state: "error", message: "Encountered an error", }, runId, ), ]; default: return []; } } return { buildUpdates: (delta) => addToolBatchTitles(delta, buildUpdates(delta)), }; }