import { randomUUID } from 'node:crypto'; import { getWorkingDir } from '../bootstrap/workingDir.js'; import { filterToolsForRuntime } from '../runtime/agentRuntimeContext.js'; import { ALL_TOOLS, executeCatalogTool, getToolGateDetails } from '../tools/index.js'; import type { LoopEvent, Message, ToolCallContext, ToolResult } from '../types.js'; import type { PluginRunnerOptions } from './pluginRunner.js'; /** Root plugin completion uses the same validated, durable delivery boundary as * PresentResult. Artifact declarations are candidates, never verification proof. * Kept outside workflow scripts so children cannot publish a parent's result. */ export async function* presentPluginArtifacts( paths: readonly string[], options: PluginRunnerOptions, ): AsyncGenerator { if (!paths.length) return; const runtime = options.runtimeContext; const candidates = (options.availableTools ?? ALL_TOOLS).filter(tool => options.allowedTools === undefined || options.allowedTools.includes(tool.name)); const catalog = runtime ? filterToolsForRuntime(candidates, runtime) : candidates; const definition = catalog.find(tool => tool.name === 'PresentResult'); if (!definition || (runtime && runtime.profile !== 'main')) { yield { type: 'warning', message: '工作流文件已生成,但当前宿主未授权正式交付;未登记文件卡片。' }; return; } options.signal?.throwIfAborted(); const id = randomUUID(); const args = JSON.stringify({ summary: '工作流产物(内容未独立验证)', files: [...new Set(paths)].map(path => ({ path, verification: 'not_run' })), }); const context: ToolCallContext = { cwd: options.sessionState?.activeWorktree?.path ?? runtime?.cwd ?? getWorkingDir(), history: options.history, sessionState: options.sessionState, provider: options.provider, sessionId: runtime?.sessionId, workspaceId: runtime?.workspaceId, surface: runtime?.surface, conversationMode: runtime?.conversationMode, userMessageId: options.userMessageId, deliveryId: options.deliveryId ?? options.userMessageId, toolCallId: id, toolGate: options.toolGate ?? runtime?.toolGate, }; const message: Message = { role: 'assistant', content: '', id: randomUUID(), timestamp: Date.now(), ...(options.messageProvenance ? { provenance: options.messageProvenance } : {}), tool_calls: [{ id, type: 'function', function: { name: 'PresentResult', arguments: args } }] }; options.history.push(message); yield { type: 'assistant_message', message }; yield { type: 'tool_start', toolName: 'PresentResult', toolCallId: id, argsPreview: args, argsFriendly: '登记工作流文件' }; let result: ToolResult; let executed = false; try { const checked = definition.inputSchema.safeParse(JSON.parse(args)); if (!checked.success) throw new Error('工作流交付清单无效:最多 50 个文件,路径不能为空'); const decision = await context.toolGate?.({ name: definition.name, argsJson: args, isReadOnly: definition.isReadOnly ?? false, toolCallId: id, ...getToolGateDetails(definition, args, context) }, options.signal); if (decision && !decision.allow) throw new Error(decision.reason); options.signal?.throwIfAborted(); executed = true; result = await (options.executeTool ? options.executeTool('PresentResult', args, options.signal, context) : executeCatalogTool(catalog, 'PresentResult', args, options.signal, context)); options.signal?.throwIfAborted(); if (result.ok && result.metadata?.kind === 'result_manifest' && options.commitFormalDelivery) { if (!options.userMessageId) throw new Error('缺少交付所属的用户回合'); await options.commitFormalDelivery({ toolCallId: id, turnId: options.userMessageId, manifest: result.metadata.manifest }); } } catch (error) { result = { ok: false, error: `工作流产物未能正式交付:${(error as Error).message}` }; } const content = result.ok ? result.content : result.error; options.history.push({ role: 'tool', id: randomUUID(), timestamp: Date.now(), tool_call_id: id, content, display_ok: result.ok, ...(result.metadata ? { display_metadata: result.metadata } : {}), toolResultDisposition: executed ? 'executed' : 'not_executed', ...(options.messageProvenance ? { provenance: options.messageProvenance } : {}) }); yield { type: 'tool_end', toolName: 'PresentResult', toolCallId: id, executed, ok: result.ok, content, ...(result.metadata ? { metadata: result.metadata } : {}) }; yield { type: 'tool_messages_committed' }; if (!result.ok) yield { type: 'warning', message: content }; options.signal?.throwIfAborted(); }