/** * ============================================================ * plugins/workflow-runner/src/toolRuntime.ts —— 工具调用的运行上下文 * ------------------------------------------------------------ * 宿主能力由每次工具调用的显式 `uses` 数组收敛 —— * 代码形态里没有「step」这个东西,宿主能力由调用点在 `tool()`/`agent()` 的 * 选项里显式声明。 * * ⚠️ `effectiveWorkflowCwd` 是**红线级**的单一出处,别在别处再算一次: * 会话可能在运行途中进入 worktree(模型可自主调 EnterWorktree),此后写文件的 * 工具跟着 worktree 走。验收侧(verify/exists/readJson)必须用**同一个**函数取 cwd, * 否则就是「产物写进 worktree、验收去原工作区的空目录里找」——条件恒 false, * 引擎反复重试后莫名失败,而产物其实好好躺在 worktree 里。 * * 也正因为 worktree 是**运行途中**才进入的,取 cwd 必须在**每次调用时**现算, * 不能在运行开始时快照一次存起来。 * ============================================================ */ import { executeCatalogTool, getToolGateDetails, type ToolCallContext } from '../../../src/plugin-sdk.js'; import type { RunContext } from './types.js'; /** 宿主能力声明:需要浏览器或电脑操控时由调用点显式列出。 */ export type HostCapability = 'browser' | 'computer'; export function requireWorkflowTool(ctx: RunContext, name: string) { const tool = ctx.tools.find(item => item.name === name); if (!tool) throw new Error(`当前宿主未授权工作流工具:${name}`); return tool; } /** An injected transport is not a capability grant. With no transport use * the host-supplied definition, never its same-named global counterpart. */ export function workflowExecutor(ctx: RunContext): NonNullable { return (name, raw, signal, context) => { if (!ctx.tools.some(tool => tool.name === name)) return Promise.resolve({ ok: false, error: `当前宿主未授权工作流工具:${name}`, disposition: 'not_executed' }); return ctx.executeTool ? ctx.executeTool(name, raw, signal, context) : executeCatalogTool(ctx.tools, name, raw, signal, context); }; } /** Model tool calls already pass the host gate in runQuery. Script calls * have no model loop, so must make the same decision before dispatch. */ export async function authorizeScriptTool(ctx: RunContext, name: string, raw: string, built: ToolCallContext, signal?: AbortSignal) { const tool = requireWorkflowTool(ctx, name); const gate = built.toolGate; signal?.throwIfAborted(); if (gate) { const decision = await gate({ name, argsJson: raw, toolCallId: built.toolCallId!, isReadOnly: tool.isReadOnly === true, ...getToolGateDetails(tool, raw, built) }, signal); if (!decision.allow) throw new Error(`工作流调用 ${name} 被宿主拒绝:${decision.reason}`); } signal?.throwIfAborted(); } /** Consume the exact question/answer boundary, not a line matching "A1" * somewhere inside the question or the user's multi-line answer. */ export function singleWorkflowAnswer(content: string, header: string, question: string): string { const prefix = `用户已回答 AskUserQuestion:\n\nQ1 [${header}] ${question}\nA1: `; const answer = content.startsWith(prefix) ? content.slice(prefix.length).trim() : ''; if (!answer) throw new Error('未收到与本题匹配的有效回答'); return answer; } /** 当前有效工作目录 —— 进了 worktree 就跟着走。见文件头注的红线说明。 */ export function effectiveWorkflowCwd(ctx: RunContext): string { return ctx.sessionState?.activeWorktree?.path ?? ctx.cwd; } /** * 组装一次工具调用的 ToolCallContext。 * * @param uses 本次调用显式声明需要的宿主能力;缺省表示不需要任何授权切片。 * @returns 成功返回上下文;宿主没提供所声明的能力时返回 `{ error }`(不抛, * 让调用方把原因当 tool_result 回填给模型)。 */ export function buildWorkflowToolContext( ctx: RunContext, toolCallId: string, reportProgress: NonNullable, uses?: HostCapability[], ): ToolCallContext | { error: string } { const context: ToolCallContext = { ...ctx.toolContext, history: ctx.history, sessionState: ctx.sessionState, provider: ctx.provider, toolGate: ctx.host?.toolGate ?? ctx.toolContext?.toolGate ?? ctx.runtimeContext?.toolGate, promptCache: ctx.promptCache ?? ctx.toolContext?.promptCache, workspaceId: ctx.runtimeContext?.workspaceId ?? ctx.toolContext?.workspaceId, sessionId: ctx.runtimeContext?.sessionId ?? ctx.toolContext?.sessionId, memoryAccess: ctx.runtimeContext?.memoryAccess ?? ctx.toolContext?.memoryAccess, surface: ctx.runtimeContext?.surface ?? ctx.toolContext?.surface, conversationMode: ctx.runtimeContext?.conversationMode ?? ctx.toolContext?.conversationMode, cwd: effectiveWorkflowCwd(ctx), toolCallId, reportProgress, }; for (const capability of uses ?? []) { if (capability === 'computer') { if (!ctx.host?.computerRun) { return { error: "声明了 uses:['computer'](需要电脑操控能力),但本次运行的宿主没有提供 —— " + '请在支持 Computer Use 的宿主里启动,或去掉该声明。', }; } context.computerRun = ctx.host.computerRun; if (ctx.host.computerIntervention) { context.computerIntervention = ctx.host.computerIntervention; } continue; } if (capability === 'browser') { if (!ctx.host?.browserRun) { return { error: "声明了 uses:['browser'](需要浏览器授权上下文),但本次运行的宿主没有提供。", }; } context.browserRun = ctx.host.browserRun; } } return context; }