claude-code 源码解读-整体架构、目录

claude-code 源码解读-整体架构、目录

架构

文件结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
┌─────────────────────────────────────────────────────────────────────────┐
│ main.tsx 入口流程 │
└─────────────────────────────────────────────────────────────────────────┘

┌───────────────────────────┼───────────────────────────┐
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ 启动预检 │ │ 命令行解析 │ │ 初始化系统 │
│ (行 1-20) │ │ (行 900+) │ │ (行 2500+) │
└───────────────┘ └───────────────┘ └───────────────┘
│ │ │
▼ ▼ ▼
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ profileCheck │ │ Commander.js │ │ AppState │
│ startMdmRaw │ │ .hook() │ │ launchRepl() │
│ startKeychain │ │ .option() │ │ │
│ Prefetch │ │ .command() │ │ │
└───────────────┘ └───────────────┘ └───────────────┘

主流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
CLI 输入 (args)


program.parse() ─────────────────────┐
│ │
▼ ▼
preAction hook ┌─────────────────────┐
│ │ 继续对话 (--continue)│
▼ │ 直接连接 (connect) │
┌──────────────┐ │ SSH 远程 (ssh) │
│ 解析选项 │ │ Resume │
│ - model │ └──────────┬──────────┘
│ - effort │ │
│ - task-budget│ ▼
│ - debug │ ┌─────────────────────┐
└──────────────┘ │ launchRepl() │
│ └──────────┬──────────┘
▼ │
┌──────────────┐ ▼
│ 构建 State │ ┌─────────────────────┐
│ - context │ │ REPL 循环 │
│ - tools │ │ (交互式对话) │
│ - commands │ └─────────────────────┘
└──────────────┘


launchRepl()

项目目录数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
.
├── assistant
├── bootstrap
├── bridge
├── buddy
├── cli
├── commands
├── components
├── constants
├── context
├── coordinator
├── entrypoints
├── hooks
├── ink
├── keybindings
├── memdir
├── migrations
├── moreright
├── native-ts
├── outputStyles
├── plugins
├── query
├── remote
├── schemas
├── screens
├── server
├── services
├── skills
├── state
├── tasks
├── tools
├── types
├── upstreamproxy
├── utils
├── vim
└── voice