/** * ============================================================ * plugins/workflow-runner/src/types.ts —— 代码编排引擎的核心类型 * ------------------------------------------------------------ * V2 只定义 ESM 元数据、宿主上下文和运行时边界;业务控制流由 JavaScript 本身 * 表达,Runner 不维护具体步骤类型。 * * ⚠️ 这里**没有** name 字段:工作流名恒等于目录名(`workflows//`)。 * legacy 时代 yaml 里的 `name:` 与文件名是两处真值,实测漂移过(workflow-evolve * 的配置里专门写了「`name` 取 yaml 头部字段,不是文件名」这条注意事项)。 * ============================================================ */ import type { ManagedExecutionBridge, ManagedExecutionContext, } from '../../../src/managedExecution/types.js'; import type { AgentRuntimeContext, ContextBundle, InvocationResource, LlmStreamEvent, Message, PluginContext, PromptCacheContext, Provider, SessionState, Tool, ToolCallContext, } from '../../../src/plugin-sdk.js'; /** 一个声明的输入参数。形状沿用 legacy,CLI 位置参数映射语义必须保持一致。 */ export interface InputDef { name: string; description?: string; required?: boolean; default?: unknown; type?: 'string' | 'number' | 'enum' | 'resource'; /** MIME globs/extensions accepted by a resource input. */ accepts?: string[]; /** Resource input may receive files/directories and multiple attachments. */ allowFile?: boolean; allowDirectory?: boolean; multiple?: boolean; /** Bind compatible current-turn attachments when no explicit path is given. */ attachmentFallback?: boolean; maxItems?: number; /** Task identity basis for resources: stable reference id or content hash. */ identity?: 'reference' | 'content'; /** Present attached/explicit files to the workflow as a staged directory. */ materialize?: 'auto' | 'directory'; /** type === 'enum' 时的合法取值。 */ values?: string[]; /** * 贪婪首参:名后**全部**位置参数空格 join 进它,不逐位拆映射。 * * 只许标在第 0 位。存在理由是真实事故:ppt 的 `topic` 不贪婪时,用户打一句 * 自然语言会被空格拆碎,第二段位置参数撞上 number 类型的 `pages` 报「必须是数字」。 */ greedy?: boolean; } /** `export const meta = {...}` 的形状(执行前静态解析得到)。 */ export interface WorkflowMeta { /** V2真实ESM工作流契约版本。 */ apiVersion: 2; /** Statically declared ordinary child workflows; included in bundle identity. */ dependencies?: string[]; /** Bare ESM package imports used by this workflow. node: and relative imports need no declaration. */ packages?: string[]; /** 一句话说明,显示在 /workflows 列表里。必填。 */ description: string; inputs?: InputDef[]; /** * 声明产物(相对工作目录)。**只是种子** —— 逐迭代产物由运行时的 * `declareArtifact(p)` 追加进同一集合。 * * 原因:meta 是执行前静态求值的纯字面量,写不出 `out/${session}/page-${i}.png` * 这种带循环下标的路径;而 ppt/illustrate 恰恰是这个形状。只留静态声明会让 * new-task 清理漏掉它们,且**表现为静默漏档而不是报错**。 */ artifacts?: string[]; /** * resume 时额外回退的调用数(承接 legacy 的 `self_heal.backoff`)。 * 缺省 0。用于「最后一次成功调用可能有未完成副作用」的场景。 */ resumeBackoff?: number; /** 可选外层控制器;Runner 只消费其通用执行租约。 */ managedRuntime?: string; } /** 磁盘上发现的一个工作流。 */ export interface WorkflowScript { /** 工作流名 = 目录名(唯一真值)。 */ name: string; /** 工作流目录绝对路径。 */ dir: string; /** `/workflow.mjs` 绝对路径。 */ scriptPath: string; /** 脚本全文(journal 的脚本指纹按它算)。 */ source: string; meta: WorkflowMeta; /** meta 字面量之后的位置,仅供静态校验器报告源码位置。 */ bodyOffset: number; } /** 发现阶段跳过的坏工作流 —— 静默跳过是不可接受的,必须能报给用户。 */ export interface WorkflowLoadFailure { name: string; scriptPath: string; errors: string[]; } export interface DiscoveryResult { workflows: WorkflowScript[]; failures: WorkflowLoadFailure[]; } // --------------------------------------------------------------------------- // 运行上下文 // --------------------------------------------------------------------------- /** * 一次工作流运行需要的全部宿主上下文。 * * 相对 legacy 的同名类型删掉了 `selfHealMaxRetries` / `retryDelayMs` / * `maxTurnsRetryDelayMs` —— 那三个是 DSL 的 `self_heal:` 声明,代码形态里 * 「重试三次、每次退避一点」就是一个 `for` 加 `try`,不需要引擎参与。 */ export interface RunContext { presentArtifacts?: PluginContext['presentArtifacts']; executionId?: string; provider: Provider; runtimeContext?: AgentRuntimeContext; signal?: AbortSignal; /** * 宿主授权上下文(浏览器 / 电脑操控)。 * * ⚠️ **不整份下发给每次调用** —— 由调用点按显式声明切片。一份无人值守的脚本 * 隐式拿到全权是危险的,这条从 legacy 原样继承。 */ host?: PluginContext['host']; /** Host-authorized resources for this invocation; never persisted or published. */ inputResources?: readonly InvocationResource[]; /** Resources bound to declared input names (the public workflow global). */ inputResourceMap?: Record; /** 当前工作目录。注意实际文件操作要走 `effectiveWorkflowCwd`(可能已进 worktree)。 */ cwd: string; /** * 可用工具列表 —— 宿主授权的上限。 * * 提示词、schema 与真实执行都以这同一份列表为准,脚本不能从 ALL_TOOLS * 恢复被宿主藏起来的能力。 */ tools: Tool[]; /** * 共享 TAOR history 引用。 * * agent/llm/skill 调用经 runQuery 直接写进它,上下文自然积累 —— * 工作流每步的执行过程对后续对话完全可见(单一上下文原则)。 */ history: Message[]; transientContext?: string; transientBundle?: ContextBundle; promptCache?: PromptCacheContext; /** 会话状态:驱动自动压缩,且 `activeWorktree` 决定工具的有效 cwd。 */ sessionState?: SessionState; /** 工作流总目标(取自 meta.description 或首个 required 输入)。 */ goal?: string; /** * 目标锚定串:每次 agent/llm/skill 调用自动前置注入。 * * 存在理由是真实失效模式:多步长跑(尤其上下文压缩过后),模型会「忘记」 * 用户的原始目标而逐渐偏航。锚定由引擎注入,**对脚本作者透明** —— * 作者不需要在每个 prompt 里手写目标。 */ goalAnchor?: string; /** 外层运行时分配的通用执行租约;普通工作流不设置。 */ managedExecution?: ManagedExecutionContext; /** 外层控制器与 Attempt Executor 之间的窄桥。 */ managedBridge?: ManagedExecutionBridge; /** Children may account usage, but never receive the outer lifecycle bridge. */ usageRecorder?: ManagedExecutionBridge['recordUsage']; /** * 本次运行的资源账本(`budget()` 的数据源)。 * * ⚠️ 有外层控制器时以**它返回的持久化快照**为准,进程内这份只是没有控制器时的兜底: * 断点续跑时账本重放不重新执行 agent/llm,进程内计数会从 0 起, * 而 Goal 侧的 `goal.usage` 是跨进程累计的。 * `trustworthy=false` 表示至少有一次调用的 provider 没回报 usage,这份账偏低。 */ resourceLedger?: { inputTokens: number; outputTokens: number; limit: number | null; trustworthy: boolean; }; /** * 用户补充要求,与原始目标**同等贯穿始终**。三个来源,引擎自动累加: * ① `ask()` 审批闸门里用户的自由输入 * ② 显式调用 `addGoalSupplement()` 的产出(如开头澄清问答的结论) * ③ 仲裁判为 resume 时用户给的新指令(「继续,但改成 12 页」) * 渲染成「📌 用户补充要求」跟在 goalAnchor 之后。持久化到状态目录, * 跨进程 resume 不丢。 */ goalSupplements?: string[]; /** * 本次运行声明的产物(相对 cwd)。**运行时可增长** —— `meta.artifacts` * 只是种子,循环体里的逐迭代产物经 `declareArtifact()` 加进来。 * * 用途:内核 Write/Edit 有「文件已存在必须先 Read」守卫(进程级状态, * 跨进程 resume 必空)。写自家声明产物时引擎自动 pre-Read 解除守卫; * 非声明产物的盲写保护原样生效。 */ declaredArtifacts?: Set; /** DI:chat 实现,仅测试用。生产不传 → runQuery 走默认 llm/client.ts。 */ chat?: (args: { provider: Provider; messages: Message[]; tools: Tool[]; signal?: AbortSignal; promptCache?: PromptCacheContext; }) => AsyncGenerator; /** DI:executeTool 实现,仅测试用。 */ executeTool?: PluginContext['executeTool']; /** * 与模型无关的确定性工具上下文。浏览器/电脑能力**刻意排除在外**, * 只由调用点按显式声明追加。 */ toolContext?: Omit< ToolCallContext, | 'browserRun' | 'computerRun' | 'computerIntervention' | 'reportProgress' | 'toolCallId' | 'cwd' >; }