Skip to content

OpenCode 核心入口 & 启动流程 ​

CLI 入口 ​

text
packages/opencode/src/index.ts

OpenCode 的 CLI 支持多种命令模式:

typescript
// effect-cmd.ts
export const TuiCommand    // 终端 UI 模式
export const RunCommand    // 单次 prompt 模式
export const ServeCommand  // HTTP Server 模式
export const AttachCommand // 附加到运行中 session
export const AcpCommand    // ACP Protocol 模式
export const McpCommand    // MCP Server 模式

CLI 命令构建 ​

typescript
// 基于 Effect CLI
const cli = Command.make("opencode")
  .pipe(Command.withSubcommands([
    TuiCommand,
    RunCommand,
    ServeCommand,
    AttachCommand,
    AcpCommand,
    McpCommand,
  ]));

全链路启动流程 ​

opencode tui
  ↓
packages/opencode/src/cli/cmd/tui.ts
  ↓
Effect AppRuntime 启动
  ↓
InstanceStore(管理 App 实例)
  ↓
创建 local server 实例
  ↓
SDK client 连接 local server
  ↓
TUI app 启动(监听 event stream)
  ↓
用户输入 → SDK → server → core → 返回 event stream

Serve 模式启动 ​

opencode serve
  ↓
创建 HTTP Server (Hono/Bun)
  ↓
注册路由: /api/sessions /api/messages /api/tools ...
  ↓
启动 localhost server
  ↓
(可选)expose to network

OpenCode vs Pi vs Hermes 入口对比 ​

维度PiOpenCodeHermes
CLI 框架YargsEffect CLIargparse
入口文件cli.tsindex.ts → effect-cmd.tshermes_cli/main.py
启动方式JS 直接执行Effect RuntimePython 直接执行
Server 模式RPC 模式完整 HTTP serverGateway runner
TUI 模式自研SDK → servercli.py / Ink TUI
Desktop无ElectronElectron

Powered by VitePress