OpenProgram Docs

插件运行时

插件是一份清单,列出技能、命令、智能体、钩子、MCP 服务端和 provider 目录。宿主通过一个 ProgramContext 挂载它们;不导入任何插件代码,也没有插件专用注册表。清单形式、来源、信任:技能和插件。模型:Program Model。

生命周期

步骤发生什么
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宿主注册表撤销
skillsctx.skills(dir)skills 插件来源注销来源
commandsctx.command(name, prompt, description)contrib["_commands"],由命令注册表适配器读取随贡献清空
hooksctx.gate、ctx.on,配 shell 关卡或通知器EventBus.subscribe_gate、subscribe订阅的 disposer
mcpServersctx.mcp_server(name, config)在宿主循环上 add_server 为 <plugin>_<name>;不写入 mcp_servers.jsonremove_server
providersctx.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)。Claude Code:{hooks: {PreToolUse: [{matcher, hooks: [{type: "command", command}]}]}}
CLAUDE_EVENTSPreToolUse → 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.pydescribe() 读取实时注册表:允许和拒绝的键、每个事件的类型和能否挂钩、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 模式不可用。代码用同样的消息拒绝
CLIenable、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,解析后忽略。

Last updated · 2026-11-25