import type { LoopEvent, TurnResourceUsage, } from '../../../../src/plugin-sdk.js'; export interface ModelRecoveryEvidence { partialText: string; toolNames: string[]; toolResults: { name: string; ok: boolean; content: string }[]; artifacts: string[]; usage?: TurnResourceUsage; } /** Failed execution evidence is diagnostic data, never a successful node value. */ export class WorkflowModelError extends Error { readonly name = 'WorkflowModelError'; constructor( message: string, readonly code: string, readonly recovery: ModelRecoveryEvidence, ) { super(message); } } export class ModelExecutionOutcome { completed = false; interrupted = false; failure?: { code: string; message: string }; usage?: TurnResourceUsage; readonly toolResults: ModelRecoveryEvidence['toolResults'] = []; observe(event: LoopEvent): void { if (event.type === 'tool_end') { this.toolResults.push({ name: event.toolName, ok: event.ok, content: event.content.slice(0, 4000), }); if (this.toolResults.length > 20) this.toolResults.shift(); } // The core explicitly continues after this diagnostic. Success still needs // a normal terminal event; all other execution errors remain failures. if (event.type === 'error' && event.code !== 'compact_failed') this.failure = { code: event.code ?? 'execution_error', message: event.error, }; if ((event.type === 'error' || event.type === 'interrupted') && event.usage) this.usage = event.usage; if (event.type === 'interrupted') this.interrupted = true; if (event.type === 'turn_done') { this.usage = event.usage; this.completed = !event.completionStatus || event.completionStatus === 'completed'; if (!this.completed) this.failure ??= { code: event.completionStatus === 'no_answer' ? 'no_answer' : 'delivery_incomplete', message: event.completionStatus === 'no_answer' ? '模型没有产出最终答复;已有进展保留供恢复,不作为成功结果。' : '阶段未正常完成交付', }; } } assertSuccess( partialText: string, toolNames: string[], artifacts: string[], ): void { if (this.interrupted) throw Object.assign(new Error('阶段执行已中断'), { name: 'AbortError' }); const failure = this.failure ?? (!this.completed ? { code: 'missing_completion', message: '模型执行没有正常结束事件' } : undefined); if (failure) throw new WorkflowModelError(failure.message, failure.code, { partialText, toolNames, toolResults: this.toolResults, artifacts, usage: this.usage, }); } } export function assertWorkflowTimeout(timeoutMs: number | undefined): void { if ( timeoutMs !== undefined && (!Number.isFinite(timeoutMs) || timeoutMs <= 0) ) { throw new Error('timeoutMs 必须为正的有限毫秒数'); } } /** Stop waiting for a loader that ignores cancellation; its scheduler lease is * still held by the actual work until it settles. Never start a model afterward. */ export async function awaitNodePreparation(work: Promise, signal?: AbortSignal): Promise { if (!signal) return work; let abort: () => void = () => {}; const interrupted = new Promise((_, reject) => { abort = () => reject(signal.reason); signal.addEventListener('abort', abort, { once: true }); if (signal.aborted) abort(); }); try { return await Promise.race([work, interrupted]); } finally { signal.removeEventListener('abort', abort); } }