基于 2026-03-31 公开的源码快照,~512,000 行代码
一、整体启动流程
Claude Code 的启动链路非常清晰:
main.tsx(入口)
├── Commander.js CLI 解析
├── 并行预加载(MDM配置/Keychain/API预连接/GrowthBook)
├── Ink React 渲染
│ ├── commands.ts 注册 Slash Commands
│ ├── tools.ts 注册 Tool
│ ├── QueryEngine 初始化
│ └── REPL.tsx 主交互界面
二、main.tsx — 入口编排
文件:
src/main.tsx,约 4,000 行
2.1 启动顺序
第一步:模块加载前的副作用(必须先执行)
| |
这三个调用 firing 了三个独立子进程并行获取配置数据,为后续初始化节省 ~65ms。
第二步:Commander.js CLI 解析
| |
CLI 解析后,通过 program.opts() 提取所有配置,准备初始化 AppState。
第三步:并行预加载(Prefetch)
| |
每个 prefetch 都是独立网络请求,共用 Promise.all 并行发出。
2.2 GrowthBook 初始化
| |
2.3 权限模式解析
CLI 解析后从多个来源确定权限模式:
| |
三、Feature Flag 编译时裁剪
本节使用
bun:bundle的feature()函数,在编译时消除未激活代码
3.1 条件导入模式
| |
如果 VOICE_MODE 未激活,./commands/voice/ 整个目录的代码不会打包进最终二进制。
3.2 主要 Feature Flags
| Flag | 控制模块 |
|---|---|
PROACTIVE / KAIROS | 主动建议 Agent |
BRIDGE_MODE | VSCode/JetBrains 桥接 |
DAEMON | 后台驻守模式 |
VOICE_MODE | 语音输入 |
COORDINATOR_MODE | 多 Agent 编排 |
WORKFLOW_SCRIPTS | 工作流脚本 |
AGENT_TRIGGERS | 定时任务触发 |
UDS_INBOX | 进程间消息队列 |
FORK_SUBAGENT | Sub-agent 派生 |
BUDDY | 对话伙伴模式 |
ULTRAPLAN | 增强规划模式 |
TORCH | 批量操作模式 |
3.3 命令注册条件过滤
| |
四、commands.ts — 命令注册中心
文件:
src/commands.ts,约 25,000 行,~100 个命令
4.1 命令类型
| |
两种核心类型:
prompt类型:由 LLM 处理,通过getPromptForCommand生成 promptbuiltin类型:直接执行原生逻辑(如/clear,/help)
4.2 命令注册流程
| |
4.3 Skills 命令加载
| |
所有命令在 REPL 渲染前汇聚完毕。
4.4 核心内置命令 TOP 10
| 命令 | 文件 | 功能 |
|---|---|---|
/commit | commands/commit.ts | Git 提交(含自动 staging) |
/review | commands/review.ts | PR/code review |
/ask | commands/ask.ts | 回答代码问题 |
/diff | commands/diff/index.ts | 查看变更 |
/test | commands/test.ts | 生成/运行测试 |
/explain | bundled/index.ts | 解释代码(内置 skill) |
/search | commands/search.ts | 代码库搜索 |
/debug | commands/debug.ts | 调试辅助 |
/refactor | commands/refactor.ts | 重构建议 |
/session | commands/session/index.ts | 会话管理 |
五、replLauncher.tsx — REPL 启动器
文件:
src/replLauncher.tsx,约 50 行
| |
启动器做两件事:
- 动态导入
App和REPL组件(懒加载) - 调用
renderAndRun执行 Ink 渲染
六、screens/REPL.tsx — 主交互界面
文件:
src/screens/REPL.tsx,约 5,000 行,React 组件
6.1 核心状态
| |
6.2 核心 Hooks 使用
| |
6.3 渲染树
<App>
<KeybindingSetup>
<GlobalKeybindingHandlers>
<CommandKeybindingHandlers>
<REPL>
<Messages> {/* 对话消息列表 */}
<TaskListV2> {/* 后台任务 */}
<PromptInput> {/* 用户输入框 */}
<PromptInputQueuedCommands /> {/* 队列命令 */}
</PromptInput>
<CostThresholdDialog /> {/* 费用上限对话框 */}
<IdleReturnDialog /> {/* 空闲返回对话框 */}
<PermissionRequest /> {/* 权限请求 */}
<WorkerPendingPermission /> {/* Agent 权限等待 */}
</REPL>
</CommandKeybindingHandlers>
</GlobalKeybindingHandlers>
</KeybindingSetup>
</App>
6.4 主要功能分区
| 区域 | 组件 | 职责 |
|---|---|---|
| 消息区 | Messages.tsx | 历史消息、工具输出、Agent 输出 |
| 输入区 | PromptInput/ | 用户命令输入、Tab 补全、Anarchy 模式前缀 |
| 任务栏 | TaskListV2.tsx | 后台任务状态、子 Agent 管理 |
| 权限区 | PermissionRequest.tsx | 工具权限确认、Auto Mode 权限流 |
| 对话框 | CostThresholdDialog.tsx | 费用超限警告 |
| 快捷键 | KeybindingSetup | 全局键盘处理 |
七、启动参数与环境变量
7.1 核心 CLI 参数
| 参数 | 说明 |
|---|---|
--model <model> | 指定模型 |
--no-input | 非交互模式 |
--output-format <json|stream-json> | 输出格式 |
--resume <sessionId> | 恢复会话 |
--print | 仅打印 response |
--add-dir <path> | 添加附加代码目录 |
--AllowedTools <tool1,tool2> | 限制可用工具 |
| `–permission-mode <auto | ask |
--dangerously-permit-aws | 允许 AWS 操作 |
7.2 环境变量映射
| |
八、启动时序图(简化)
main.tsx
│
├─ profileCheckpoint('main_tsx_entry')
├─ startMdmRawRead() [并行子进程]
├─ startKeychainPrefetch() [并行子进程]
│
├─ program.parse(args) [Commander.js]
│
├─ loadGlobalConfig() [配置文件]
├─ validateApiKey() [API Key 检查]
│
├─ initializeGrowthBook() [Feature Flags]
├─ refreshPolicyLimits() [策略限制]
│
├─ getTools() [工具注册]
├─ getCommands() [命令注册]
├─ getSkills(cwd) [Skill 加载]
│
├─ createRoot(renderOptions) [Ink 根]
│
├─ fetchBootstrapData() [预加载]
├─ prefetchPassesEligibility()
├─ prefetchOfficialMcpUrls()
└─ launchRepl(root, appProps, replProps)
│
├─ import('./components/App.js')
├─ import('./screens/REPL.js')
│
└─ renderAndRun(root, <App><REPL>...</REPL></App>)
│
└─ REPL.tsx 渲染 → 等待用户输入
下一步
下一篇:03 - Bridge 模块:主进程与渲染进程的通信机制,深入 IDE 扩展与 CLI 之间的双向通信层。
Claude Code 源码研究系列 · 2026 · skyseraph

