OpenCode 核心入口 & 启动流程
CLI 入口
text
packages/opencode/src/index.tsOpenCode 的 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 streamServe 模式启动
opencode serve
↓
创建 HTTP Server (Hono/Bun)
↓
注册路由: /api/sessions /api/messages /api/tools ...
↓
启动 localhost server
↓
(可选)expose to networkOpenCode vs Pi vs Hermes 入口对比
| 维度 | Pi | OpenCode | Hermes |
|---|---|---|---|
| CLI 框架 | Yargs | Effect CLI | argparse |
| 入口文件 | cli.ts | index.ts → effect-cmd.ts | hermes_cli/main.py |
| 启动方式 | JS 直接执行 | Effect Runtime | Python 直接执行 |
| Server 模式 | RPC 模式 | 完整 HTTP server | Gateway runner |
| TUI 模式 | 自研 | SDK → server | cli.py / Ink TUI |
| Desktop | 无 | Electron | Electron |