Skip to content

OpenCode Agent & Plugin ​

Agent 系统 ​

text
packages/core/src/agent.ts

OpenCode 的 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.ts
typescript
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 对比 ​

维度OpenCodeHermes
运行环境Effect (TypeScript)Python
注册方式Service-basedPlugin context + hooks
HooksprePrompt, preLLMRequest, postToolCall, onSessionStart/Endpre/post_tool_call, transform_*, pre/post_llm_call, 20+ hooks
工具注册Plugin.toolsplugin ctx.register_tool()
AgentPlugin.agents无 plugin agent 注册
MCPPlugin.mcpServers单独配置
来源npm / dir / gitbundled / user / project / pip entry-point

Powered by VitePress