扩展
Comonad 的一切都是同一个 Cordis 上下文上的插件:内核、工具、 模型适配器、面板和内容层。插件是 cordis.yml 里的一行,它注册的一切都是随行关闭而消失的 effect。
行
项目行在 .comonad/cordis.yml;用户行在 ~/.comonad/cordis.yml。
# .comonad/cordis.yml
- { id: comonad-plugin-tavern, name: ./plugins/comonad-plugin-tavern/dist/index.js }
- { id: mine, name: ./mine.js, config: { greeting: hello } }
- { id: scratch, name: ./scratch.js, disabled: true }name 是路径时按文件读取:./x、../x 相对于声明它的 cordis.yml,~/x 从 home 开始, /abs/x 原样使用。其他名字按包解析。
写一个插件
// mine.js —— 一个策略和一个工具,都是插件的 effect
export const name = 'mine'
export const inject = ['tools'] // 必须声明,不会自动推断
export function apply(ctx, config) {
ctx.on('tools/pre-execute', (exec, next) =>
exec.name === 'run_shell' && /rm -rf/.test(String(exec.arguments?.command ?? ''))
? { kind: 'deny', reason: 'not that command' }
: next(),
)
ctx.tools.register({
name: 'hello',
description: 'Say hello.',
capability: 'ro',
needsNetwork: false,
needsWorkspace: false,
parameters: { type: 'object', properties: {}, required: [], additionalProperties: false },
execute: async () => config.greeting,
})
}- 读取服务需要
inject。 inject 的东西未加载时,行保持 pending —— 这就是“功能关掉了”的表达。 宿主自己的ctx.plugins是例外,用ctx.get('plugins')读取。 - 插件注册的一切都随它消失。 工具、命令、watcher 和面板都随行 dispose。
- 配置就是插件自己的 schema。 导出
Config(schemastery schema),apply前会校验。 - 坏行会被报告,不会拖垮整棵树。 其余插件照常加载。
引用 resolver
插件可以给 @ 增加一种文件类型,不需要改输入区、消息模型或 wire:
export const inject = ['references']
export function apply(ctx) {
ctx.effect(() => ctx.references.register({
id: 'my-format',
priority: 10,
mimes: ['application/x-my-format'],
extensions: ['.myf'],
claim: ({ head }) => head[0] === 0x25 && head[1] === 0x4d && head[2] === 0x59 && head[3] === 0x46, // "%MYF"
resolve: async (input) => {
const bytes = await input.readBytes(input.limits.maxDocumentBytes)
return [{ kind: 'text', form: 'file', source: { path: input.displayPath }, text: parse(bytes) }]
},
}))
}ctx.references 负责路径守卫、读取隔离、去重、预算,以及把 part 放到用户消息上的标记。resolver 只负责 识别格式并返回类型化内容;认领后失败会变成错误提示,不会回退成 UTF-8。comonad-plugin-pdf 是文档 resolver 的完整示例。
安装与管理
Settings → Plugins → Install… 按名字提供 Comonad 自带的插件,也接受你自己的文件夹。安装会把文件夹 复制到 ~/.comonad/plugins/<id>,并往 cordis.yml 写一行。Uninstall 在确认后删除该行和安装的文件夹。
面板按加载顺序列出每一行、来源、解析目标和状态(loaded、off,或 not loaded 加原因)。 Disable / Enable 在受信任时写工作区列表,否则写用户列表,并且只重新应用这一行。
Settings → Inspect → Plugins 显示依赖树:每个插件提供什么、需要什么、由谁提供,以及关掉它时会 连带停掉什么。
DeepSeek Harness 插件
harness 行重定向 @deepseek-ai/* 导入,所以为 DeepSeek Harness 写的插件可以原样加载:
{ "id": "permission-gate", "name": "./permission-gate.mjs" }import type { Context } from '@deepseek-ai/cordis'
export const name = 'permission-gate'
export const inject = ['tools']
export function apply(ctx: Context) {
ctx.on('tools/pre-execute', (exec, next) => {
if (String(exec.arguments?.command ?? '').includes('rm -rf')) {
return { kind: 'deny', reason: 'Denied by policy.' }
}
return next()
})
}Comonad 实现了 harness 的工具与策略半边:ctx.tools.register() 接受两种 spec,@deepseek-ai/cordis 就是同一个上下文类,四个瀑布 —— tools/pre-execute、tools/execute、tools/post-execute、 tools/result —— 使用 harness 的载荷与决策。harness 的 agent/session 内部未实现;缺失的名字解析为 空模块,并在日志中点名。
Hooks
Hooks 在生命周期点运行:SessionStart、UserPromptSubmit、PreToolUse、PostToolUse、 PostToolUseFailure、Stop。handler 可以是命令、HTTP、MCP 工具或 prompt。handler 可以返回权限决策 (deny / ask / allow)、附加上下文或修改后的输入。
{
"PreToolUse": [
{
"matcher": "run_shell",
"hooks": [
{
"type": "command",
"if": "run_shell(rm:*)",
"command": "echo '{\"hookSpecificOutput\":{\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"no rm\"}}'"
}
]
}
]
}项目 hooks 在 .comonad/hooks.json;用户 hooks 在 globalStorage/hooks.json。只在受信任工作区加载, 退出码 2 表示阻断。
其他扩展点
| 机制 | 位置 | 说明 |
|---|---|---|
| 指令 | COMONAD.md、AGENTS.md、.comonad/instructions.md | 分层加载;仅受信任工作区 |
| 规则 | .comonad/rules/*.md | 进入系统提示 |
| Skills | .comonad/skills/<name>/SKILL.md | 提示中列出,按需加载 |
| 子 Agent | .comonad/agents/*.md + subagents(设置) | frontmatter 选择工具;设置授予写能力 |
| 命令 | .comonad/commands/*.md | 自定义 slash 命令 |
| MCP | mcpServers(设置) | stdio 或 streamable HTTP;逐服务器能力与覆盖 |
| 记忆 | agent 自己的 MEMORY.md | 进入提示,用 memory 工具编辑 |
子 Agent 角色
十四个内置角色在代码里。自定义角色是 .comonad/agents/ 或 ~/.comonad/agents/ 下的 Markdown 文件:
---
name: writer
description: 写一篇聚焦的技术说明。
mode: sw
network: false
capabilities: [ro, rw, shell_ro, shell_test]
files: true
test: true
tools: [read_file, grep_files, write_file, edit_text, run_shell_test, message]
maxTurns: 20
maxTools: 60
maxTokens: 0
maxMilliseconds: 0
---
写这篇说明。所有项目写入都走文件工具;run_shell_test 只写 scratch 目录。角色 id 是小写 slug(name 或文件名)。mode(或 workMode)是收窄角色能力的输入,不再作为运行时模式 存储或展示。capabilities 是角色的有效能力,必须留在子代理硬上限内(ro、ro_plan、shell_ro、rw、 shell_test)。network: true 只是请求网络,父开关仍须开启。risk 由有效能力推导,不能在文件里覆盖。 自定义角色出现在编排块中,任务的 role 可以引用它。
frontmatter 选择工具;写能力还需要 subagents 设置中的授权(Settings → Subagents):files 启用 可跟踪文件工具(write_file、edit_text、edit_ranges),test 启用 run_shell_test。只读工具不需要 授权。计划读工具(list_plans、read_plan)使用 ro_plan,仅在父级有效能力含它时可用;计划写入 (write_plan、edit_plan)、run_shell/start_shell 和编排工具永远不会授予子级。 没有 tools 列表的定义视为只读。复用内置 id 的自定义角色不继承内置默认授权。见安全与 编排。
自定义角色可以设置扁平的 maxTurns、maxTools、maxTokens、maxMilliseconds 字段。它们是四个 独立原生上限:turns 是模型往返次数,tools 是工具调用次数,tokens 是输入 + 输出,milliseconds 是墙钟 毫秒;0 表示无限制。在 comonad.subagentBudgetMode: custom 下,每个字段只能收紧 comonad.subagentBudget,不会放宽;默认的 inherit 使用父级上限,忽略这些角色字段。任务的 budget 在两种模式下都能进一步收紧。被上限暂停的子代理保留会话与 transcript,可以用新的分段预算 resume。见设置。
MCP
MCP 服务器在设置文件的 mcpServers 设置中配置:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"],
"capability": "ro"
}
}
}每个服务器可设置 capability、writeTools、exclude 和逐工具 overrides。工具以 server__tool 出现,并经过与内置工具相同的策略门控。服务器是一个子插件:关掉它会关闭连接并移除其工具。