Skip to content

Commit d99ede4

Browse files
CrazyBoyMclaude
andcommitted
feat: add interactive web learning platform (Phase 1)
- Complete i18n: all hardcoded English strings replaced with t() calls across layers, compare, whats-new, and design-decisions components - Dark mode persistence via localStorage + inline script to prevent flash - Per-session feature visualizations (s01-s11) with Framer Motion animations - Multi-language docs: 11 Chinese + 11 Japanese translations of all sessions - Extract-content supports suffix-based locale detection (-zh, -ja) - Tutorial section expanded by default in doc-renderer - GitHub link updated to shareAI-lab/learn-claude-code - 81 static pages generated across en/zh/ja locales - Deployed to Vercel production Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 19d293f commit d99ede4

47 files changed

Lines changed: 5644 additions & 55 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/s01-the-agent-loop-ja.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# s01: The Agent Loop
2+
3+
> AIコーディングエージェントの秘密はすべて、モデルが「終了」と判断するまでツール結果をモデルにフィードバックし続けるwhileループにある。
4+
5+
## 問題
6+
7+
なぜ言語モデルは単体でコーディングの質問に答えられないのか。それはコーディングが「現実世界とのインタラクション」を必要とするからだ。モデルはファイルを読み、テストを実行し、エラーを確認し、反復する必要がある。一回のプロンプト-レスポンスのやり取りではこれは実現できない。
8+
9+
agent loopがなければ、ユーザーが自分でモデルの出力をコピーペーストして戻す必要がある。つまりユーザー自身がループの役割を果たすことになる。agent loopはこれを自動化する: モデルを呼び出し、モデルが要求したツールを実行し、結果をフィードバックし、モデルが「完了」と言うまで繰り返す。
10+
11+
単純なタスクを考えてみよう: 「helloと出力するPythonファイルを作成せよ」。モデルは(1)ファイルを書くことを決定し、(2)書き、(3)動作を検証する必要がある。最低でも3回のツール呼び出しが必要だ。ループがなければ、そのたびに手動の介入が必要になる。
12+
13+
## 解決策
14+
15+
```
16+
+----------+ +-------+ +---------+
17+
| User | ---> | LLM | ---> | Tool |
18+
| prompt | | | | execute |
19+
+----------+ +---+---+ +----+----+
20+
^ |
21+
| tool_result |
22+
+---------------+
23+
(loop continues)
24+
25+
The loop terminates when stop_reason != "tool_use".
26+
That single condition is the entire control flow.
27+
```
28+
29+
## 仕組み
30+
31+
1. ユーザーがプロンプトを入力する。これが最初のメッセージになる。
32+
33+
```python
34+
history.append({"role": "user", "content": query})
35+
```
36+
37+
2. メッセージ配列がツール定義と共にLLMに送信される。
38+
39+
```python
40+
response = client.messages.create(
41+
model=MODEL, system=SYSTEM, messages=messages,
42+
tools=TOOLS, max_tokens=8000,
43+
)
44+
```
45+
46+
3. アシスタントのレスポンスがメッセージに追加される。
47+
48+
```python
49+
messages.append({"role": "assistant", "content": response.content})
50+
```
51+
52+
4. stop reasonを確認する。モデルがツールを呼び出さなかった場合、ループは終了する。これが唯一の終了条件だ。
53+
54+
```python
55+
if response.stop_reason != "tool_use":
56+
return
57+
```
58+
59+
5. レスポンス中の各tool_useブロックについて、ツール(このセッションではbash)を実行し、結果を収集する。
60+
61+
```python
62+
for block in response.content:
63+
if block.type == "tool_use":
64+
output = run_bash(block.input["command"])
65+
results.append({
66+
"type": "tool_result",
67+
"tool_use_id": block.id,
68+
"content": output,
69+
})
70+
```
71+
72+
6. 結果がuserメッセージとして追加され、ループが続行する。
73+
74+
```python
75+
messages.append({"role": "user", "content": results})
76+
```
77+
78+
## 主要コード
79+
80+
最小限のエージェント -- パターン全体が30行未満
81+
(`agents/s01_agent_loop.py` 66-86行目):
82+
83+
```python
84+
def agent_loop(messages: list):
85+
while True:
86+
response = client.messages.create(
87+
model=MODEL, system=SYSTEM, messages=messages,
88+
tools=TOOLS, max_tokens=8000,
89+
)
90+
messages.append({"role": "assistant", "content": response.content})
91+
if response.stop_reason != "tool_use":
92+
return
93+
results = []
94+
for block in response.content:
95+
if block.type == "tool_use":
96+
output = run_bash(block.input["command"])
97+
results.append({
98+
"type": "tool_result",
99+
"tool_use_id": block.id,
100+
"content": output,
101+
})
102+
messages.append({"role": "user", "content": results})
103+
```
104+
105+
## 変更点
106+
107+
これはセッション1 -- 出発点である。前のセッションは存在しない。
108+
109+
| Component | Before | After |
110+
|---------------|------------|--------------------------------|
111+
| Agent loop | (none) | `while True` + stop_reason |
112+
| Tools | (none) | `bash` (one tool) |
113+
| Messages | (none) | Accumulating list |
114+
| Control flow | (none) | `stop_reason != "tool_use"` |
115+
116+
## 本番環境との対応
117+
118+
実際のClaude Codeでも、同じループがメインREPLの中で動いている。コアとなるループロジックは`agent.ts`にあり、同一のパターンに従う: メッセージをAPIに送り、`stop_reason`を確認し、ツールを実行し、結果を追加する。本番バージョンではエラーハンドリング、トークンカウント、ストリーミング、リトライロジックが追加されているが、根本的な構造は変わらない。すべてのClaude Codeセッションはこのwhileループである。
119+
120+
## 試してみる
121+
122+
```sh
123+
cd learn-claude-code
124+
python agents/s01_agent_loop.py
125+
```
126+
127+
試せるプロンプト例:
128+
129+
1. `Create a file called hello.py that prints "Hello, World!"`
130+
2. `List all Python files in this directory`
131+
3. `What is the current git branch?`
132+
4. `Create a directory called test_output and write 3 files in it`

docs/s01-the-agent-loop-zh.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# s01: Agent Loop (智能体循环)
2+
3+
> AI 编程智能体的全部秘密就是一个 while 循环 -- 把工具执行结果反馈给模型, 直到模型决定停止。
4+
5+
## 问题
6+
7+
为什么语言模型不能直接回答编程问题? 因为编程需要**与真实世界交互**。模型需要读取文件、运行测试、检查错误、反复迭代。单次的提示-响应交互无法做到这些。
8+
9+
没有 agent loop, 你就得手动把输出复制粘贴回模型。用户自己变成了那个循环。Agent loop 将这个过程自动化: 调用模型, 执行它要求的工具, 把结果送回去, 重复 -- 直到模型说 "我完成了"。
10+
11+
考虑一个简单任务: "创建一个打印 hello 的 Python 文件。" 模型需要 (1) 决定写文件, (2) 写入文件, (3) 验证是否正常工作。至少三次工具调用。没有循环的话, 每一次都需要人工干预。
12+
13+
## 解决方案
14+
15+
```
16+
+----------+ +-------+ +---------+
17+
| User | ---> | LLM | ---> | Tool |
18+
| prompt | | | | execute |
19+
+----------+ +---+---+ +----+----+
20+
^ |
21+
| tool_result |
22+
+---------------+
23+
(loop continues)
24+
25+
The loop terminates when stop_reason != "tool_use".
26+
That single condition is the entire control flow.
27+
```
28+
29+
## 工作原理
30+
31+
1. 用户提供一个 prompt, 成为第一条消息。
32+
33+
```python
34+
history.append({"role": "user", "content": query})
35+
```
36+
37+
2. 消息数组连同工具定义一起发送给 LLM。
38+
39+
```python
40+
response = client.messages.create(
41+
model=MODEL, system=SYSTEM, messages=messages,
42+
tools=TOOLS, max_tokens=8000,
43+
)
44+
```
45+
46+
3. 助手的响应被追加到消息列表中。
47+
48+
```python
49+
messages.append({"role": "assistant", "content": response.content})
50+
```
51+
52+
4. 检查 stop_reason。如果模型没有调用工具, 循环结束。这是唯一的退出条件。
53+
54+
```python
55+
if response.stop_reason != "tool_use":
56+
return
57+
```
58+
59+
5. 对响应中的每个 tool_use 块, 执行工具 (本节课中是 bash) 并收集结果。
60+
61+
```python
62+
for block in response.content:
63+
if block.type == "tool_use":
64+
output = run_bash(block.input["command"])
65+
results.append({
66+
"type": "tool_result",
67+
"tool_use_id": block.id,
68+
"content": output,
69+
})
70+
```
71+
72+
6. 结果作为 user 消息追加, 循环继续。
73+
74+
```python
75+
messages.append({"role": "user", "content": results})
76+
```
77+
78+
## 核心代码
79+
80+
最小可行智能体 -- 不到 30 行代码实现整个模式
81+
(来自 `agents/s01_agent_loop.py`, 第 66-86 行):
82+
83+
```python
84+
def agent_loop(messages: list):
85+
while True:
86+
response = client.messages.create(
87+
model=MODEL, system=SYSTEM, messages=messages,
88+
tools=TOOLS, max_tokens=8000,
89+
)
90+
messages.append({"role": "assistant", "content": response.content})
91+
if response.stop_reason != "tool_use":
92+
return
93+
results = []
94+
for block in response.content:
95+
if block.type == "tool_use":
96+
output = run_bash(block.input["command"])
97+
results.append({
98+
"type": "tool_result",
99+
"tool_use_id": block.id,
100+
"content": output,
101+
})
102+
messages.append({"role": "user", "content": results})
103+
```
104+
105+
## 变更内容
106+
107+
这是第 1 节课 -- 起点。没有前置课程。
108+
109+
| 组件 | 之前 | 之后 |
110+
|---------------|------------|--------------------------------|
111+
| Agent loop | (无) | `while True` + stop_reason |
112+
| Tools | (无) | `bash` (单一工具) |
113+
| Messages | (无) | 累积式消息列表 |
114+
| Control flow | (无) | `stop_reason != "tool_use"` |
115+
116+
## 生产环境参考
117+
118+
在真实的 Claude Code 中, 同样的循环运行在主 REPL 内。核心循环逻辑位于 `agent.ts`, 遵循完全相同的模式: 发送消息到 API, 检查 `stop_reason`, 执行工具, 追加结果。生产版本增加了错误处理、token 计数、流式输出和重试逻辑, 但基本结构没有变化。每一个 Claude Code 会话本质上都是这个 while 循环。
119+
120+
## 试一试
121+
122+
```sh
123+
cd learn-claude-code
124+
python agents/s01_agent_loop.py
125+
```
126+
127+
可以尝试的提示:
128+
129+
1. `Create a file called hello.py that prints "Hello, World!"`
130+
2. `List all Python files in this directory`
131+
3. `What is the current git branch?`
132+
4. `Create a directory called test_output and write 3 files in it`

0 commit comments

Comments
 (0)