Skip to content

Commit 2d96386

Browse files
anthhubclaude
andcommitted
fix: address review findings for Chapter 1
Infrastructure: - Fix devcontainer features to use existing community images - Add VitePress head/meta for SEO and social sharing - Add English locale outline and docFooter config Demo code: - Remove unnecessary @anthropic-ai/sdk dependency (defer to Ch3) - Add PermissionRuleSource type and source field to PermissionRule - Add ToolCategory type and category field to Tool interface - Extract DEFAULT_MODEL constant in config.ts Documentation: - Enhance Ch1 Section 7 with config explanations and troubleshooting FAQ - Make Ch1 "What's Next" preview more specific with concrete deliverables - Add "Key Milestones" section to both README files (zh-CN and en) - Sync all changes between Chinese and English versions Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d42a608 commit 2d96386

12 files changed

Lines changed: 272 additions & 17 deletions

File tree

.devcontainer/devcontainer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"name": "Learn Claude Code",
33
"image": "mcr.microsoft.com/devcontainers/typescript-node:22",
44
"features": {
5-
"ghcr.io/anthropics/devcontainer-features/bun:1": {},
6-
"ghcr.io/anthropics/devcontainer-features/deno:1": {}
5+
"ghcr.io/devcontainers/features/node:1": { "version": "22" },
6+
"ghcr.io/shyim/devcontainers-features/bun:0": {},
7+
"ghcr.io/nicklasxyz/devcontainer-features/deno:1": {}
78
},
89
"postCreateCommand": "bun install && cd demo && bun install",
910
"customizations": {

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Claude Code is more than a chatbot wrapper. It's a full-featured terminal applic
2424

2525
Understanding its source is a masterclass in building production-grade AI applications.
2626

27+
**Build as you learn.** This isn't just documentation — it's a progressive tutorial. Starting from Chapter 1, you'll build `mini-claude`, a working clone of Claude Code. Each chapter adds a new module to the demo. By Chapter 12, you'll have a fully functional AI coding assistant with tools, permissions, terminal UI, and more.
28+
2729
---
2830

2931
## Learning Roadmap
@@ -45,6 +47,87 @@ Understanding its source is a masterclass in building production-grade AI applic
4547

4648
---
4749

50+
## The Demo: mini-claude
51+
52+
As you read each chapter, you'll build `mini-claude` — a working AI coding assistant that mirrors Claude Code's real architecture. The demo lives in the `demo/` directory and grows chapter by chapter.
53+
54+
### Final Architecture
55+
56+
```
57+
demo/
58+
├── main.ts # CLI entry (Commander.js)
59+
├── context.ts # System prompt builder
60+
├── query.ts # Query loop (stream + tool calls)
61+
├── Tool.ts # Tool interface & factory
62+
├── tools.ts # Tool registry
63+
├── types/
64+
│ ├── message.ts # Message types
65+
│ └── permissions.ts # Permission types
66+
├── tools/
67+
│ ├── BashTool/
68+
│ ├── FileReadTool/
69+
│ ├── FileWriteTool/
70+
│ ├── FileEditTool/
71+
│ ├── GrepTool/
72+
│ ├── GlobTool/
73+
│ └── TodoWriteTool/
74+
├── services/
75+
│ ├── api/claude.ts # Anthropic SDK wrapper
76+
│ └── compact/compact.ts # Context compression
77+
├── screens/REPL.tsx # Terminal UI (Ink)
78+
├── components/
79+
│ ├── App.tsx
80+
│ ├── MessageList.tsx
81+
│ └── PermissionRequest.tsx
82+
├── commands/
83+
│ ├── clear.ts
84+
│ ├── help.ts
85+
│ └── compact.ts
86+
└── utils/
87+
├── permissions.ts
88+
├── messages.ts
89+
├── format.ts
90+
└── config.ts
91+
```
92+
93+
### What You Build Each Chapter
94+
95+
| Ch | Module Added | Demo Capability After |
96+
|----|-------------|----------------------|
97+
| 1 | Project scaffold + type system | Type definitions compile |
98+
| 2 | Tool.ts + tools.ts | Tool interface & registry |
99+
| 3 | services/api/ + context.ts | Streaming API calls |
100+
| 4 | query.ts + utils/messages.ts | Multi-turn tool-calling loop |
101+
| 5 | tools/BashTool, FileReadTool, GrepTool | Execute commands, read files, search |
102+
| 6 | tools/FileWriteTool, FileEditTool, GlobTool | Full file operations |
103+
| 7 | utils/permissions.ts | Dangerous command blocking |
104+
| 8 | screens/REPL.tsx + components/ | Interactive terminal UI |
105+
| 9 | main.ts (Commander.js) | Full CLI with args |
106+
| 10 | commands/ + compact service | /help, /clear, /compact |
107+
| 11 | components/PermissionRequest.tsx | Interactive permission dialogs |
108+
| 12 | History, retry, error handling | Production-ready demo |
109+
110+
### Key Milestones
111+
112+
- **After Chapter 2**: Tools can actually execute shell commands and read files
113+
- **After Chapter 4**: Complete Agentic Loop — AI automatically calls tools and reasons in a loop
114+
- **After Chapter 8**: Interactive terminal UI, experience close to real Claude Code
115+
- **After Chapter 12**: Fully functional AI coding assistant
116+
117+
### Architecture Correspondence
118+
119+
| Demo File | Real Claude Code Equivalent |
120+
|-----------|----------------------------|
121+
| `Tool.ts` | `src/Tool.ts` |
122+
| `tools.ts` | `src/tools/index.ts` |
123+
| `query.ts` | `src/query.ts` |
124+
| `context.ts` | `src/context.ts` |
125+
| `services/api/claude.ts` | `src/services/claude.ts` |
126+
| `screens/REPL.tsx` | `src/screens/REPL.tsx` |
127+
| `utils/permissions.ts` | `src/utils/permissions.ts` |
128+
129+
---
130+
48131
## Chapters
49132

50133
### Foundation (Chapters 1-2)
@@ -115,6 +198,11 @@ learn-claude-code/
115198
│ │ └── dependency-graph.ts
116199
│ ├── 02-cli-entrypoint/
117200
│ └── ...
201+
├── demo/ # mini-claude: the progressive demo you build
202+
│ ├── main.ts
203+
│ ├── query.ts
204+
│ ├── Tool.ts
205+
│ └── ...
118206
└── diagrams/ # Architecture diagrams
119207
```
120208

@@ -148,6 +236,11 @@ bun run ch1:structure
148236

149237
# Or run any example directly
150238
bun run examples/01-overview/project-structure.ts
239+
240+
# Run the demo (after completing chapters)
241+
cd demo
242+
bun install
243+
bun run main.ts
151244
```
152245

153246
Then open [docs/en/01-overview.md](docs/en/01-overview.md) and follow along.

README.zh-CN.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Claude Code 远不止是一个聊天机器人包装器,它是一个功能完
2424

2525
研究其源码,是构建生产级 AI 应用的绝佳实战教材。
2626

27+
**边学边做。** 这不只是文档——这是一个渐进式实战教程。从第 1 章开始,你将构建 `mini-claude`,一个可运行的 Claude Code 克隆版。每章都会为 demo 新增一个模块。到第 12 章结束,你将拥有一个功能完整的 AI 编程助手,包含工具系统、权限控制、终端 UI 等完整功能。
28+
2729
---
2830

2931
## 学习路线
@@ -45,6 +47,87 @@ Claude Code 远不止是一个聊天机器人包装器,它是一个功能完
4547

4648
---
4749

50+
## Demo:mini-claude
51+
52+
在阅读每一章的同时,你将构建 `mini-claude`——一个与 Claude Code 真实架构对应的可运行 AI 编程助手。demo 位于 `demo/` 目录中,随章节逐步完善。
53+
54+
### 最终目录结构
55+
56+
```
57+
demo/
58+
├── main.ts # CLI 入口(Commander.js)
59+
├── context.ts # 系统提示词构建器
60+
├── query.ts # 查询循环(流式 + 工具调用)
61+
├── Tool.ts # 工具接口与工厂
62+
├── tools.ts # 工具注册表
63+
├── types/
64+
│ ├── message.ts # 消息类型
65+
│ └── permissions.ts # 权限类型
66+
├── tools/
67+
│ ├── BashTool/
68+
│ ├── FileReadTool/
69+
│ ├── FileWriteTool/
70+
│ ├── FileEditTool/
71+
│ ├── GrepTool/
72+
│ ├── GlobTool/
73+
│ └── TodoWriteTool/
74+
├── services/
75+
│ ├── api/claude.ts # Anthropic SDK 封装
76+
│ └── compact/compact.ts # 上下文压缩
77+
├── screens/REPL.tsx # 终端 UI(Ink)
78+
├── components/
79+
│ ├── App.tsx
80+
│ ├── MessageList.tsx
81+
│ └── PermissionRequest.tsx
82+
├── commands/
83+
│ ├── clear.ts
84+
│ ├── help.ts
85+
│ └── compact.ts
86+
└── utils/
87+
├── permissions.ts
88+
├── messages.ts
89+
├── format.ts
90+
└── config.ts
91+
```
92+
93+
### 每章构建内容
94+
95+
|| 新增模块 | 完成后 Demo 能力 |
96+
|----|---------|----------------|
97+
| 1 | 项目脚手架 + 类型系统 | 类型定义可编译 |
98+
| 2 | Tool.ts + tools.ts | 工具接口与注册表 |
99+
| 3 | services/api/ + context.ts | 流式 API 调用 |
100+
| 4 | query.ts + utils/messages.ts | 多轮工具调用循环 |
101+
| 5 | tools/BashTool、FileReadTool、GrepTool | 执行命令、读取文件、搜索 |
102+
| 6 | tools/FileWriteTool、FileEditTool、GlobTool | 完整文件操作 |
103+
| 7 | utils/permissions.ts | 危险命令拦截 |
104+
| 8 | screens/REPL.tsx + components/ | 交互式终端 UI |
105+
| 9 | main.ts(Commander.js) | 完整 CLI 参数支持 |
106+
| 10 | commands/ + compact 服务 | /help、/clear、/compact 命令 |
107+
| 11 | components/PermissionRequest.tsx | 交互式权限确认对话框 |
108+
| 12 | 历史记录、重试、错误处理 | 生产就绪的 demo |
109+
110+
### 关键里程碑
111+
112+
- **第 2 章后**:工具可以实际执行 shell 命令和读取文件
113+
- **第 4 章后**:完整的 Agentic Loop——AI 自动调用工具并循环推理
114+
- **第 8 章后**:交互式终端 UI,体验接近真实 Claude Code
115+
- **第 12 章后**:功能完整的 AI 编程助手
116+
117+
### 架构对应关系
118+
119+
| Demo 文件 | 真实 Claude Code 对应文件 |
120+
|-----------|--------------------------|
121+
| `Tool.ts` | `src/Tool.ts` |
122+
| `tools.ts` | `src/tools/index.ts` |
123+
| `query.ts` | `src/query.ts` |
124+
| `context.ts` | `src/context.ts` |
125+
| `services/api/claude.ts` | `src/services/claude.ts` |
126+
| `screens/REPL.tsx` | `src/screens/REPL.tsx` |
127+
| `utils/permissions.ts` | `src/utils/permissions.ts` |
128+
129+
---
130+
48131
## 章节详情
49132

50133
### 基础篇(第 1-2 章)
@@ -115,6 +198,11 @@ learn-claude-code/
115198
│ │ └── dependency-graph.ts
116199
│ ├── 02-cli-entrypoint/
117200
│ └── ...
201+
├── demo/ # mini-claude:你逐章构建的渐进式 demo
202+
│ ├── main.ts
203+
│ ├── query.ts
204+
│ ├── Tool.ts
205+
│ └── ...
118206
└── diagrams/ # 架构图
119207
```
120208

@@ -148,6 +236,11 @@ bun run ch1:structure
148236

149237
# 或直接运行任意示例
150238
bun run examples/01-overview/project-structure.ts
239+
240+
# 运行 demo(完成相应章节后)
241+
cd demo
242+
bun install
243+
bun run main.ts
151244
```
152245

153246
然后打开 [docs/zh-CN/01-overview.md](docs/zh-CN/01-overview.md) 跟随学习。

demo/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
isToolUseBlock,
1717
isTextBlock,
1818
toolToAPIFormat,
19+
DEFAULT_MODEL,
1920
DEFAULT_CONFIG,
2021
} from "./types/index.js";
2122

demo/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
"typecheck": "tsc --noEmit",
88
"start": "bun run main.ts"
99
},
10-
"dependencies": {
11-
"@anthropic-ai/sdk": "^0.52.0"
12-
},
10+
"dependencies": {},
1311
"devDependencies": {
1412
"@types/bun": "^1.3.11",
1513
"typescript": "^5.7.0"

demo/types/config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ export interface AppConfig {
3737
systemPrompt?: string;
3838
}
3939

40+
/** 默认模型 ID — 更新模型时只需修改此处 */
41+
export const DEFAULT_MODEL = "claude-sonnet-4-20250514";
42+
4043
/**
4144
* 默认配置
4245
*/
4346
export const DEFAULT_CONFIG: Omit<AppConfig, "apiKey"> = {
44-
model: "claude-sonnet-4-20250514",
47+
model: DEFAULT_MODEL,
4548
maxTokens: 16384,
4649
permissionMode: "default",
4750
cwd: process.cwd(),

demo/types/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export {
3131
// 工具类型
3232
export type {
3333
Tool,
34+
ToolCategory,
3435
JSONSchema,
3536
JSONSchemaProperty,
3637
ToolRegistry,
@@ -48,10 +49,11 @@ export type {
4849
PermissionAskDecision,
4950
PermissionDecision,
5051
PermissionRule,
52+
PermissionRuleSource,
5153
PermissionContext,
5254
CheckPermissionFn,
5355
} from "./permissions.js";
5456

5557
// 配置类型
5658
export type { AppConfig } from "./config.js";
57-
export { DEFAULT_CONFIG } from "./config.js";
59+
export { DEFAULT_MODEL, DEFAULT_CONFIG } from "./config.js";

demo/types/permissions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ export type PermissionDecision =
7272

7373
// ─── 权限规则 ───────────────────────────────────────────────────────────────
7474

75+
/**
76+
* 权限规则来源
77+
*
78+
* 真实 Claude Code 中,权限规则可以来自多个来源,
79+
* source 字段用于溯源和优先级判断
80+
*/
81+
export type PermissionRuleSource = "userSettings" | "projectSettings" | "session" | "default";
82+
7583
/**
7684
* 权限规则 - 预定义的权限判断规则
7785
*
@@ -89,6 +97,8 @@ export interface PermissionRule {
8997
behavior: PermissionBehavior;
9098
/** 规则说明 */
9199
reason?: string;
100+
/** 规则来源,用于溯源和优先级判断 */
101+
source?: PermissionRuleSource;
92102
}
93103

94104
// ─── 权限上下文 ──────────────────────────────────────────────────────────────

demo/types/tool.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414

1515
import type { ToolResult } from "./message.js";
1616

17+
// ─── 工具分类 ───────────────────────────────────────────────────────────────
18+
19+
/**
20+
* 工具分类
21+
*
22+
* builtin: 内置工具(如 Bash、FileRead)
23+
* mcp: 通过 MCP 协议桥接的外部工具
24+
* skill: 通过技能系统加载的工具
25+
*/
26+
export type ToolCategory = "builtin" | "mcp" | "skill";
27+
1728
// ─── 工具接口 ───────────────────────────────────────────────────────────────
1829

1930
/**
@@ -57,6 +68,9 @@ export interface Tool {
5768
*/
5869
call(input: Record<string, unknown>): Promise<ToolResult>;
5970

71+
/** 工具分类,默认 builtin */
72+
category?: ToolCategory;
73+
6074
/**
6175
* 是否为只读操作
6276
*

docs/.vitepress/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ export default defineConfig({
44
title: "Learn Claude Code",
55
description: "深入理解 Claude Code 架构与实现的实战指南",
66

7+
head: [
8+
["link", { rel: "icon", type: "image/svg+xml", href: "/learn-claude-code/logo.svg" }],
9+
["meta", { property: "og:type", content: "website" }],
10+
["meta", { property: "og:title", content: "Learn Claude Code" }],
11+
["meta", { property: "og:description", content: "深入理解 Claude Code 架构与实现的实战指南" }],
12+
],
13+
lastUpdated: true,
14+
715
base: "/learn-claude-code/",
816

917
locales: {
@@ -113,6 +121,8 @@ export default defineConfig({
113121
"https://github.com/anthhub/learn-claude-code/edit/main/docs/:path",
114122
text: "Edit this page on GitHub",
115123
},
124+
outline: { label: "On this page" },
125+
docFooter: { prev: "Previous", next: "Next" },
116126
},
117127
},
118128
},

0 commit comments

Comments
 (0)