import { deliveryNotes } from './activityPresentation.js'; /** Observe the live stream: history may be compacted during a turn. */ export function createTurnSummary(now: () => number = () => performance.now()) { const startedAt = now(); const tools = new Set(); let emitted = false; let workflow = false; return (event: { type: string; toolCallId?: string; completionStatus?: unknown; pendingRequired?: unknown }): string | null => { if (event.type === 'workflow_progress') workflow = true; if (event.type === 'tool_start' && event.toolCallId) tools.add(event.toolCallId); if (event.type !== 'turn_done' || emitted) return null; emitted = true; const seconds = Math.max(0, now() - startedAt) / 1000; const duration = seconds < 1 ? '<1s' : seconds < 60 ? `${seconds.toFixed(1)}s` : `${Math.floor(seconds / 60)}m ${Math.floor(seconds % 60)}s`; // "结束" is not a claim that delivery or human acceptance succeeded. // Workflows invoke tools inside their own runner; main-loop start events // are not an authoritative count of the work performed. return [`本轮结束 · 耗时 ${duration}${workflow ? '' : ` · ${tools.size} 次工具调用`}`, ...deliveryNotes(event.completionStatus, event.pendingRequired)].join('\n'); }; }