import { incompleteDeliveryBody, incompleteDeliveryWarning, missingFormalDeliveryWarning, type DeliveryGate, } from '../runtime/deliveryGate.js'; /** * 最终回答的发布判定(纯函数)。 * * 输入是「门禁结果 + 候选正文的显示状态」,输出是这次回答该怎么发布: * 标成 final 还是 progress、给用户看的正文是什么、状态警告怎么写、回执里 * `shownChars` 记多少。loop.ts 只负责把计划落成事件与历史,不再自己拼这些。 * * 规则: * - 门禁关闭 ≠ 沉默:发布一条明确标注的 progress 消息加原因与恢复路径,永不标 final。 * - 候选没有实时显示过 → 整段不发布,由运行时按真实账本写状态正文(模型自己的 * 「全部完成」不能被当成最终报告);已经流出的文字保持可见,只改标签。 * - 结构化输出保留 schema 形状的正文(塞散文会破坏调用方 schema),但同样不标 final。 */ export interface FinalPublicationInput { gate: DeliveryGate; formalDeliveryRequired: boolean; resultPresented: boolean; structuredOutput: boolean; /** 候选正文是否已经实时流给了用户。 */ liveStarted: boolean; candidate: string; /** 实时流出的字符数(liveStarted 时有意义)。 */ shownLength: number; } export interface FinalPublicationPlan { presentation: 'final' | 'progress'; /** 门禁关闭时的状态警告;开放时为空串。 */ warning: string; /** 用户实际看到的正文:候选原文,或运行时状态正文。 */ body: string; /** 候选被运行时状态正文取代(调用方据此改写消息并补发 text 事件)。 */ replaced: boolean; /** 回执里的 shownChars:用户实际看到的字符数。 */ shownChars: number; } export function planFinalPublication(input: FinalPublicationInput): FinalPublicationPlan { const { gate, formalDeliveryRequired, resultPresented, structuredOutput, liveStarted } = input; const missingFormal = formalDeliveryRequired && !resultPresented; const blocked = gate.kind !== 'ready' || missingFormal; if (!blocked) return { presentation: 'final', warning: '', body: input.candidate, replaced: false, shownChars: liveStarted ? input.shownLength : input.candidate.length, }; // Only a ledger-backed closed gate may withhold the candidate. "No file was // presented" comes from an intent heuristic, and the candidate is often the // deliverable itself, so that case publishes it labelled as progress. const replaced = !structuredOutput && !liveStarted && gate.kind !== 'ready'; const candidateVisible = !replaced; const warning = gate.kind !== 'ready' ? incompleteDeliveryWarning(gate, candidateVisible) : missingFormalDeliveryWarning(candidateVisible); const body = replaced ? incompleteDeliveryBody(gate) : input.candidate; return { presentation: 'progress', warning, body, replaced, shownChars: liveStarted ? input.shownLength : body.length, }; }