/** * ============================================================ * plugins/workflow-runner/plugin.ts —— 代码编排引擎的插件入口 * ------------------------------------------------------------ * 接管 /workflow 与 /workflows:不走声明式 markdown→LLM 通道,直接执行 * `workflows//workflow.mjs`。 * * 契约:default export 一个 PluginApi。框架按 pluginRoot 缓存这次 import。 * ============================================================ */ import type { PluginApi } from '../../src/plugin-sdk.js'; import { ALL_TOOLS, buildTransientPromptBundle, createAgentRuntimeContext, derivePromptCacheContext, filterToolsForRuntime, getWorkingDir, } from '../../src/plugin-sdk.js'; import { lookupWorkflow } from './src/discovery.js'; import { runWorkflowFromCommand, runWorkflowsList } from './src/index.js'; const api: PluginApi = { async resolveSlashFallback(commandName, args, { cwd }) { const { script: matched } = await lookupWorkflow(commandName, cwd, true); if (!matched) return null; return { commandName: 'workflow', args: `${matched.name}${args ? ` ${args}` : ''}`, message: `▸ /${commandName} → 工作流「${matched.name}」`, }; }, async *runCommand(commandName, args, ctx) { const cwd = getWorkingDir(); if (commandName === 'workflows') { yield* runWorkflowsList(cwd); return; } if (commandName === 'workflow') { const workflowRuntime = createAgentRuntimeContext({ ...(ctx.runtimeContext ?? { profile: 'main' as const, interactiveAsk: false, }), // 命令跑在工作流自己的 async 作用域里;父级 Desktop/CLI runtime 可能是 // 在那个作用域建立之前创建的,cwd 要以这里为准。 cwd, surface: 'workflow', owner: ctx.runtimeContext?.owner ?? 'untrusted', memoryAccess: ctx.runtimeContext?.memoryAccess === 'denied' ? 'denied' : 'read-only', }); const hostToolNames = ctx.allowedTools ? new Set(ctx.allowedTools) : undefined; const hostPool = ctx.availableTools ?? ALL_TOOLS; const hostTools = hostToolNames ? hostPool.filter((tool) => hostToolNames.has(tool.name)) : hostPool; const workflowTools = filterToolsForRuntime(hostTools, workflowRuntime); const transientBundle = ctx.transientBundle ?? (await buildTransientPromptBundle({ cwd, tools: workflowTools, profile: 'main', includeProjectInstructions: true, includePlugins: true, runtime: { surface: 'workflow', cwd, workspaceId: workflowRuntime.workspaceId, interactiveAsk: workflowRuntime.interactiveAsk, memoryAccess: workflowRuntime.memoryAccess, currentUserInput: args, }, }).catch(() => undefined)); yield* runWorkflowFromCommand(args, { presentArtifacts: ctx.presentArtifacts, provider: ctx.provider, history: ctx.history, signal: ctx.signal, sessionState: ctx.sessionState, cwd, tools: workflowTools, runtimeContext: workflowRuntime, managedContext: { provenance: ctx.messageProvenance }, executeTool: ctx.executeTool, toolContext: { history: ctx.history, sessionState: ctx.sessionState, provider: ctx.provider, toolGate: ctx.host?.toolGate, promptCache: ctx.promptCache, workspaceId: workflowRuntime.workspaceId, sessionId: workflowRuntime.sessionId, memoryAccess: workflowRuntime.memoryAccess, }, // 宿主授权上下文原样带上;由调用点按显式声明切片后才转给工具。 host: ctx.host, inputResources: ctx.inputResources, executionId: ctx.executionId, transientContext: ctx.transientContext, transientBundle, promptCache: derivePromptCacheContext( ctx.promptCache, `workflow:${args.trim() || 'interactive'}`, ), }); return; } yield { type: 'error', error: `workflow-runner: 未知命令 /${commandName}`, }; }, }; export default api;