# 插件运行时

插件是一份清单，列出技能、命令、智能体、钩子、MCP 服务端和 provider 目录。宿主通过一个 `ProgramContext` 挂载它们；不导入任何插件代码，也没有插件专用注册表。清单形式、来源、信任：[技能和插件](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/extensions/skills-and-plugins.zh.html)。模型：[Program Model](https://fzkuji.github.io/OpenProgram/docs/reference/design/function/program-model.zh.html#4-plugins)。

## 生命周期

| 步骤 | 发生什么 |
| --- | --- |
| `rescan` | 构建 `Plugin` 记录，代码拒绝作为错误。同根目录的已加载插件保留；根目录变化则先停用。启用集合：`~/.openprogram/plugins-enabled.json` |
| `load_plugin` 检查 | 无代码、信任、兼容性、`requires`（依赖先加载） |
| 激活 | `ProgramContext(name, root, options)`，所有者 `plugin:<name>`；清单键变成 effect。任何异常：`ctx.dispose()`，错误留在插件上 |
| 加载后 | 发出 `plugin.enable`，广播 `plugins:changed` |
| `unload_plugin` | 发出 `plugin.disable`，然后 `ctx.dispose()` |
| `reload_plugin` | 卸载、重扫、加载。新文件失败时仍保持启用 |

启动时 `interfaces/server/server.py` 的 `_load_plugins` 在 MCP 服务端之后运行，把事件循环交给 `set_host_loop`，并在线程中调用 `loader.load_enabled()`：按依赖顺序，单个失败只记录，不影响其他。

| 来源 | 位置 |
| --- | --- |
| npm | `~/.openprogram/plugins/node_modules/` |
| 路径 | `~/.openprogram/plugins/<name>`。`install path <dir>` 创建符号链接：修改在 reload 后生效，卸载只删除链接 |
| 项目固定 | 后面的来源在重名时胜出 |

## Effect

| 清单键 | Effect | 宿主注册表 | 撤销 |
| --- | --- | --- | --- |
| `skills` | `ctx.skills(dir)` | `skills` 插件来源 | 注销来源 |
| `commands` | `ctx.command(name, prompt, description)` | `contrib["_commands"]`，由命令注册表适配器读取 | 随贡献清空 |
| `hooks` | `ctx.gate`、`ctx.on`，配 shell 关卡或通知器 | `EventBus.subscribe_gate`、`subscribe` | 订阅的 disposer |
| `mcpServers` | `ctx.mcp_server(name, config)` | 在宿主循环上 `add_server` 为 `<plugin>_<name>`；不写入 `mcp_servers.json` | `remove_server` |
| `providers` | `ctx.provider_root(dir)` | `models/providers/catalog/plugin_dirs.py`，`provider.json` 查找路径。内置目录重名时胜出 | 移除根目录 |
| `agents` | 缺失时 `manager.create` 为 `<plugin>-<name>` | Agent 注册表 | 无：删除智能体会删除其会话 |

`ctx.dispose()` 逆序执行 disposer，收集错误而不中止，并清空 `ctx.contributions`（插件页和 `plugin` 工具显示的分类记录）。定义在 `programs/_host/context.py`。

## 拒绝代码

`extensions/plugins/manifest.py` 把指向代码的键记入 `code`：`python`、`module`、`setup`、`main`、`tools`、`client`、`web`、`sidebar`、形如 `module:name` 的 `hooks`、Hermes `plugin.yaml` 旁的 `__init__.py`。`code_refusal` 列出它们，并提示添加 `application.json` 后运行 `openprogram programs install <dir>`。

| 入口 | 拒绝方式 |
| --- | --- |
| `installer.install` | 拒绝 `path` 安装，删除 git 克隆，卸载 npm 包。`pip` 不是来源 |
| `rescan` | 列出插件，拒绝信息作为错误 |
| `_check`、`_activate` | 抛出 `PluginContainsCode`（`PermissionError`）；开关路由返回 `403`、`code: "contains_code"` |

## 钩子

| 方面 | 规则 |
| --- | --- |
| 形式（`plugins/hooks.py`） | 内联或 JSON 文件。OpenProgram：`{event: [{command, timeout, shell, args}]}`，同 `config.json`；`shell` 和 `args` 可选（[hook 用哪个 shell](https://fzkuji.github.io/OpenProgram/docs/reference/design/distribution/windows-command-shell.zh.html#hook-shell)）。Claude Code：`{hooks: {PreToolUse: [{matcher, hooks: [{type: "command", command}]}]}}` |
| `CLAUDE_EVENTS` | `PreToolUse` → `tool.before`，`PostToolUse` → `tool.after`，`UserPromptSubmit` → `chat.before_send`，`Stop` → `turn.stop`，`SessionStart` → `session.start` |
| 替换 | `${CLAUDE_PLUGIN_ROOT}` 是插件目录；`matcher` 是匹配事件 `tool` 的正则 |
| 执行（`runtime/events/shell_hooks.py`） | 事件以 JSON 传入 stdin。关卡事件上退出码 2 表示拒绝，stderr 为原因；其他退出码放行 |
| 跳过 | 未知事件、非 command 钩子类型、transform 事件 |

| 事件类型（`runtime/events/registry.py`） | 行为 |
| --- | --- |
| notify | 观察者；忽略返回值 |
| gate | 可否决（`tool.before`、`turn.stop`） |
| transform | 每个改写器收到 `(event, value)` 并返回新值；`None` 保持原值；抛异常的被跳过 |

`tool.result` 是 transform：`_rewrite_tool_result` 合并文本块并替换文本，保留图片和音频。应用用 `ctx.transform` 订阅；插件不能。

## 自描述、工具、CLI

| 部分 | 行为 |
| --- | --- |
| `extensions/plugins/inspect.py` | `describe()` 读取实时注册表：允许和拒绝的键、每个事件的类型和能否挂钩、Claude Code 映射、暴露层级、每个条目的所有者、层级、审批标记。`guide()` 是同内容文本。支撑 `GET /api/plugins/inspect`、`openprogram plugins inspect [--json]` 和 `plugin` 工具 |
| `plugin` 工具（`programs/tools/runtime/plugin.py`） | `guide`、`inspect`、`list`、`install`（链接、信任为 community、启用）、`enable`、`disable`、`reload`、`uninstall`。在 worker 中运行（`_run_in_worker`），需要审批，聊天频道和 plan 模式不可用。代码用同样的消息拒绝 |
| CLI | `enable`、`disable`、`install`、`uninstall`、`reload` 先调 worker 的 HTTP API，否则改状态文件 |
| Web | 插件不带 UI：没有 Web 模块、页面、侧栏行的路由或壳层插槽。插件页列出贡献，收到 `plugins:changed` 时刷新。独立界面属于应用的 `application.json` UI |

## 实现状态

已按上述实现，除了：清单未列出时自动发现 Claude Code 默认目录（`commands/`、`agents/`、`skills/`、`hooks/hooks.json`）；Claude Code 的 `prompt` 钩子类型；`lspServers` 和 `outputStyles`，解析后忽略。
