/** * ============================================================ * src/plugins/pluginApi.ts —— 插件 plugin.ts 的契约 * ------------------------------------------------------------ * 双层插件契约的"代码层"接口: * * plugins//.claude-plugin/plugin.json → 元数据 * plugins//commands/*.md → 声明式 slash 命令(默认走 LLM) * plugins//hooks/hooks.json → 声明式 hook * plugins//plugin.ts → 可选!要接管命令分发时才写 * * 纯 markdown 插件(无 plugin.ts)零修改 drop-in,覆盖 99% Anthropic 上游用例; * 富插件写 plugin.ts 接管 runCommand,把全部逻辑放在自己目录里,外部 src/ 一行不改。 * * PluginContext 是框架"愿意暴露给插件"的最小运行时上下文: * - provider / history / signal / sessionState 都是 runQuery 本来就接受的 * - 插件作者通过 src/plugin-sdk.ts 取 runQuery、chat、executeTool 等 * * PluginEvent 是事件层的"开放契约":插件可以 yield 任意带 type 字段的自定义事件 * ——框架原样透传,UI 不识别就静默忽略。让插件不必把私有事件挤进 LoopEvent。 * ============================================================ */ import type { ContextBundle } from '../context/contextBundle.js'; import type { AgentRuntimeContext } from '../runtime/agentRuntimeContext.js'; import type { RunAgentTurnOptions, RunAgentTurnResult, } from '../session/runAgentTurn.js'; import type { SessionState } from '../session/sessionState.js'; import type { LoopEvent, Message, PromptCacheContext, Provider, ToolCallContext, ToolResult, } from '../types.js'; /** 框架传给插件的运行时上下文(仅暴露执行 LLM/工具 所需的最小集合)。 */ export interface PluginContext { /** Host-controlled root artifact delivery; never a script/child capability. */ presentArtifacts?: (paths: readonly string[]) => AsyncGenerator; /** Stable identity of this externally initiated execution (not the conversation). */ executionId?: string; provider: Provider; /** 调用方持有的消息历史,插件可读可写(mutate 同一引用) */ history: Message[]; /** 用户取消信号 */ signal?: AbortSignal; /** 多租户场景注入的隔离 session 状态;缺省走模块单例 */ sessionState?: SessionState; /** maxTurns 透传 */ maxTurns?: number; /** 当前回合的瞬时外部上下文;插件不得把它持久化到 history。 */ transientContext?: string; /** 本轮宿主明确授权给命令插件的文件资源;绝对路径仅限本机运行时。 */ inputResources?: readonly import('../types.js').InvocationResource[]; transientBundle?: ContextBundle; promptCache?: PromptCacheContext; /** Canonical host-issued trust context; plugins may narrow it but never elevate it. */ runtimeContext?: AgentRuntimeContext; /** Host tool names are an upper bound. Plugins may narrow, never expand. */ allowedTools?: readonly string[]; /** Concrete per-turn tools, including host-bound group capabilities. */ availableTools?: readonly import('../types.js').Tool[]; /** Host-stamped origin; never inferred from command arguments. */ messageProvenance?: import('../types.js').ConversationProvenance; /** * Host tool dispatcher. Rich plugins must use the same injected executor as * the parent run so tests, embedders and host-specific dispatch remain on one * execution path. */ executeTool?: ( name: string, rawArgsJson: string, signal?: AbortSignal, context?: ToolCallContext, ) => Promise; /** * 宿主的**授权与可见性上下文**,原样转交给工具层(`executeTool` 第四参)。 * * ⚠️ 为什么是**一个** `Pick` 而不是逐个平铺四个字段: * · 它就是「工具接收什么」那个**已有**抽象的子集 —— 用 `Pick` 绑定, * `ToolCallContext` 改了这里跟着改,不会出现两份各自漂移的定义; * · 将来新增宿主能力(移动端、远程桌面…)时这个接口零改动(Open/Closed)。 * * ⚠️ 此前缺席的后果:`pluginRunner` 的三处下传里,两条 runQuery 路径都带了 * browserRun/computerRun,**唯独 `api.runCommand(...)` 那一处漏了** —— 于是 * `PluginContext` 比 `RunQueryOptions` 窄,富插件(如 workflow-runner)永远拿不到 * 宿主授权。表现是 workflow 里调 ComputerUse 恒返回「当前宿主未启用 Computer Use」, * 而同一个工具在主循环里好好的。 * * 插件**不应**无条件把它转给每一步:能力要由各步显式声明后按需转发 * (见 workflow-runner 的逐次 `uses` 选项),否则一份无人值守的脚本就隐式拿到了 * 完整的机器控制权。 */ host?: Pick< ToolCallContext, 'browserRun' | 'computerRun' | 'computerIntervention' | 'toolGate' >; } /** TUI/App 生命周期传给富插件的宿主能力。 */ export interface PluginRuntimeContext { /** Optional host presentation sink; notifications never become user instructions. */ notify?: (message: string) => void; provider: Provider; signal?: AbortSignal; runAgentTurn: (opts: RunAgentTurnOptions) => Promise; } /** * 插件自定义事件的开放形状。 * * 任何带 `type: string` 字段的对象都允许;payload 字段由插件自己定义。 * 框架 yield 时原样透传,UI 的 handleEvent 用 switch 派发—— * 无 case 匹配的 type 走 default no-op,等同静默忽略。 * * 例:workflow-runner 的 WorkflowEvent(`type: 'workflow_start'` 等) * 在结构上自动满足 PluginEvent,无需 cast。 */ export interface PluginEvent { type: string; [key: string]: unknown; } export interface PluginSlashFallback { /** Existing command owned by this plugin, without the leading slash. */ commandName: string; /** Rewritten arguments for that command. */ args: string; message?: string; } /** * 插件 plugin.ts 的 default export 形状。 * * `runCommand` 接管声明式 markdown fallback:返回 AsyncGenerator 让框架直接 yield* 出去。 * 返回类型允许 LoopEvent(框架已识别)+ PluginEvent(插件私有事件)混合 yield。 * 不实现 runCommand → 框架回退到 buildCommandInput + runQuery(纯 prompt 模式)。 */ export interface PluginApi { activate?( ctx: PluginRuntimeContext, ): // biome-ignore lint/suspicious/noConfusingVoidType: lifecycle hooks intentionally accept synchronous void or an async disposer. Promise void)> | void | (() => void); runCommand?( commandName: string, args: string, ctx: PluginContext, ): AsyncGenerator; resolveSlashFallback?( commandName: string, args: string, context: { cwd: string }, ): Promise | PluginSlashFallback | null; }