import type { Message } from '../types.js'; import { activityText, agentNotice, systemNotice } from './activityPresentation.js'; import { contentToPlainText } from '../multimodal.js'; const object = (v: unknown): Record => v && typeof v === 'object' && !Array.isArray(v) ? v as Record : {}; // Transcript objects are stable across appends. Avoid repeatedly parsing large // receipts; WeakMap releases entries when compaction drops the original rows. const parsedReceipts = new WeakMap(); function receiptValue(message: Extract): unknown { const cached = parsedReceipts.get(message); if (cached?.content === message.content) return cached.value; const value: unknown = JSON.parse(message.content); parsedReceipts.set(message, { content: message.content, value }); return value; } /** Append-stable projection: never remove transcript rows or mutate tool receipts. * Only repeated visible summaries for the SAME attempt are hidden. /show retains * the raw result, and missing IDs, errors and evidence pages are never suppressed. */ export function repeatedAgentActivities(history: readonly Message[], toolNames: ReadonlyMap): Set { const seen = new Map(); const descriptions = new Map(); const hidden = new Set(); const fingerprint = (a: Record, key: string) => { const h = object(a.handoff); if (typeof a.description === 'string') descriptions.set(key, activityText(a.description)); // Raw reading/searching activity is already visible in the live section. // A new handoff, status or acceptance still produces a history card. return JSON.stringify([a.status, descriptions.get(key) ?? '', activityText(h.summary ?? (a.status === 'running' ? '' : a.progress)), activityText(h.needed), a.resolution ?? null, ['failed', 'interrupted', 'stopped'].includes(String(a.status)) ? activityText(a.result) : '']); }; history.forEach((m, index) => { // A fresh human request may ask for the same status again. if (m.role === 'user' && !systemNotice(m)) { seen.clear(); descriptions.clear(); return; } if (agentNotice(m)) { const lines = contentToPlainText(m.content).split('\n'); const header = /^子任务 \S+ \((.*)\) 已(\w+)。$/.exec(lines[1] ?? ''); const status = header?.[2]; const start = lines[3]?.startsWith('归属:') ? 4 : lines[2]?.startsWith('来源:') ? 3 : 2; try { const handoff = object(JSON.parse(lines[start] ?? '')); if (status && typeof handoff.summary === 'string') { const key = m.id!.slice('agent-notice:'.length); seen.set(key, fingerprint({ status, description: header?.[1], handoff }, key)); } } catch { /* Legacy prose is not a structured receipt. */ } return; } if (m.role !== 'tool' || m.display_ok === false || m.toolResultDisposition === 'deferred' || m.toolResultDisposition === 'not_executed' || !['AgentList', 'AgentWait', 'TaskOutput'].includes(toolNames.get(m.tool_call_id)?.toolName ?? '')) return; try { if (toolNames.get(m.tool_call_id)?.toolName === 'TaskOutput') return; const value = receiptValue(m), data = object(value); // TaskOutput is an explicit detail request, not a repeated progress poll. // Preserve its /show handle even when the summary matches a prior notice. if (data.findings || data.evidence || (Array.isArray(data.missing) && data.missing.length)) return; const rows: unknown[] = Array.isArray(value) ? value : Array.isArray(data.agents) ? data.agents : data.agent_id ? [data] : []; if (!rows.length) return; if (rows.some(raw => ['failed', 'blocked', 'interrupted', 'stopped'].includes(String(object(raw).status)) || object(object(raw).handoff).kind === 'help')) return; // These rows have no help/failure state (excluded above), so the card's // stable priority sort preserves input order. Only its first four appear. const entries = rows.slice(0, 4).map((raw: unknown) => { const a = object(raw); if (typeof a.agent_id !== 'string' || typeof a.attempt_id !== 'string' || typeof a.status !== 'string') return undefined; const key = `${a.agent_id}:${a.attempt_id}`; return { key, text: fingerprint(a, key) }; }); if (rows.length <= 4 && entries.every((e: { key: string; text: string } | undefined) => e && seen.get(e.key) === e.text)) hidden.add(index); for (const e of entries) if (e) seen.set(e.key, e.text); } catch { /* Raw reports and malformed results stay visible. */ } }); return hidden; }