OpenCode Agent & Plugin
Agent 系统
text
packages/core/src/agent.tsOpenCode 的 Agent 是一个 Effect Service:
typescript
class AgentRegistry extends Service<AgentRegistry>() {
register(agent: Agent): Effect<void>;
spawn(agentName: string, task: string): Effect<AgentResult>;
list(): Effect<Agent[]>;
}Agent 定义
typescript
interface Agent {
name: string;
description: string;
systemPrompt: string;
tools: string[];
model?: string;
}Sub-agent 执行
AgentRegistry.spawn("code-reviewer", "review this PR")
↓
创建新 SessionV2
↓
注入 agent system prompt
↓
限制工具集(只给 agent.tools)
↓
独立 SessionRunner
↓
返回结果给主 agent这与 Hermes 的 delegate_task 工具设计相似,但 OpenCode 把 agent 定义和注册做成了独立的 Service。
Plugin 系统
text
packages/core/src/plugin.tstypescript
interface Plugin {
name: string;
version: string;
// 工具
tools?: Tool[];
// 生命周期 hooks
onInit?(): Effect<void>;
onCleanup?(): Effect<void>;
// 挂钩钩子
hooks?: {
prePrompt?(ctx: PromptContext): Effect<PromptContext>;
postToolCall?(result: ToolResult): Effect<ToolResult>;
preLLMRequest?(req: LLMRequest): Effect<LLMRequest>;
onSessionStart?(session: SessionV2): Effect<void>;
onSessionEnd?(session: SessionV2): Effect<void>;
};
// 资源
resources?: Resource[];
// Agent
agents?: Agent[];
// MCP servers
mcpServers?: MCPServerConfig[];
}Plugin 加载链
Package discovery (npm / local dir)
↓
PluginLifecycle.load(plugin)
↓
onInit()
↓
register tools / agents / resources / MCP
↓
hooks 注入到各个 service与 Hermes Plugin 对比
| 维度 | OpenCode | Hermes |
|---|---|---|
| 运行环境 | Effect (TypeScript) | Python |
| 注册方式 | Service-based | Plugin context + hooks |
| Hooks | prePrompt, preLLMRequest, postToolCall, onSessionStart/End | pre/post_tool_call, transform_*, pre/post_llm_call, 20+ hooks |
| 工具注册 | Plugin.tools | plugin ctx.register_tool() |
| Agent | Plugin.agents | 无 plugin agent 注册 |
| MCP | Plugin.mcpServers | 单独配置 |
| 来源 | npm / dir / git | bundled / user / project / pip entry-point |