Skip to content

Commit 0459cb0

Browse files
committed
Initial commit: Learn Claude Code by Java
A Java 1.8 implementation of shareAI-lab/learn-claude-code 12 lessons to build your own AI Agent from scratch. Features: - JDK 1.8 compatible, minimal dependencies (only Gson) - Bilingual comments (Chinese + English) - Interactive visualization (docs/diagrams/) - Compatible with any Anthropic Messages API provider Based on: https://github.com/shareAI-lab/learn-claude-code License: MIT
0 parents  commit 0459cb0

40 files changed

+17217
-0
lines changed

.gitignore

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Compiled
2+
target/
3+
*.class
4+
*.jar
5+
*.war
6+
*.ear
7+
8+
# IDE
9+
.idea/
10+
*.iml
11+
*.ipr
12+
*.iws
13+
.vscode/
14+
.settings/
15+
.project
16+
.classpath
17+
18+
# OS
19+
.DS_Store
20+
Thumbs.db
21+
22+
# Config (may contain secrets - NEVER commit these!)
23+
.env
24+
claude.properties
25+
*.local
26+
27+
# Project specific
28+
.tasks/
29+
.team/
30+
.transcripts/
31+
.worktrees/
32+
33+
# Logs
34+
*.log
35+
36+
# Temporary files
37+
*.tmp
38+
*.swp
39+
*~

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 更新日志
2+
3+
本项目的所有重要更改都将记录在此文件中。
4+
5+
格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.0.0/)
6+
并且本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)
7+
8+
## [1.0.0] - 2025-01-XX
9+
10+
### 新增
11+
- Java 1.8 完整实现,对应原 Python 版 12 节课程
12+
- 原生 HttpURLConnection 实现 API 调用,零第三方依赖(除 Gson)
13+
- 交互式可视化教学页面 (`docs/diagrams/`)
14+
- 中英双语源码注释
15+
- 支持任何兼容 Anthropic Messages API 的服务商
16+
17+
### 变更(相对于原项目)
18+
- 语言:Python → Java 1.8
19+
- HTTP 客户端:httpx/requests → HttpURLConnection
20+
- JSON 处理:内置 json → Gson
21+
- 异步模型:asyncio → Thread + ExecutorService
22+
- 配置方式:.env → claude.properties
23+
24+
### 致谢
25+
- 原项目:[shareAI-lab/learn-claude-code](https://github.com/shareAI-lab/learn-claude-code)

CONTRIBUTING.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 贡献指南
2+
3+
感谢你考虑为本项目做出贡献!
4+
5+
## 如何贡献
6+
7+
### 报告问题
8+
9+
如果你发现了 bug 或有功能建议,请:
10+
11+
1.[Issues](https://github.com/arrayadd/learn-claude-code-by-java/issues) 中搜索是否已有相关问题
12+
2. 如果没有,创建新的 Issue,详细描述问题或建议
13+
14+
### 提交代码
15+
16+
1. Fork 本仓库
17+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
18+
3. 确保代码符合项目风格:
19+
- JDK 1.8 兼容(不用 `var``List.of()` 等)
20+
- 中英双语注释
21+
- 每个课程独立可运行
22+
4. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
23+
5. 推送到分支 (`git push origin feature/AmazingFeature`)
24+
6. 创建 Pull Request
25+
26+
### 代码规范
27+
28+
- 遵循 Java 命名约定
29+
- 保持最小依赖原则
30+
- 添加必要的注释(中英双语)
31+
- 确保所有课程可以独立编译运行
32+
33+
## 许可证
34+
35+
通过提交代码,你同意你的贡献将按照 [MIT License](LICENSE) 授权。

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2024 shareAI Lab
4+
Copyright (c) 2025 arrayadd
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

NOTICE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Learn Claude Code by Java
2+
Copyright (c) 2025 arrayadd
3+
4+
This software is licensed under the MIT License.
5+
6+
---
7+
8+
This project is a Java rewrite of:
9+
shareAI-lab/learn-claude-code
10+
Copyright (c) 2024 shareAI Lab
11+
https://github.com/shareAI-lab/learn-claude-code
12+
13+
---
14+
15+
This project includes the following third-party software:
16+
17+
Google Gson
18+
Copyright (c) 2008 Google Inc.
19+
Licensed under the Apache License, Version 2.0
20+
https://github.com/google/gson

README.md

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# Learn Claude Code by Java
2+
3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4+
[![Java](https://img.shields.io/badge/Java-1.8+-orange.svg)](https://www.oracle.com/java/)
5+
6+
> **用 12 节课从零拆解 AI Agent 的秘密,从零打造一个自己的 Claude Code**
7+
>
8+
> Java (JDK 1.8) 实现,保持逻辑完全一致,方便 Java 开发者学习。
9+
10+
**[English](#english-version)** | **中文**
11+
12+
## 项目简介
13+
14+
本项目是对 [shareAI-lab/learn-claude-code](https://github.com/shareAI-lab/learn-claude-code) 的 Java 重写版本,旨在帮助 Java 开发者深入理解 AI Agent 的工作原理。
15+
16+
### 原项目致谢
17+
18+
本项目基于以下开源项目改造:
19+
20+
- **原项目**: [shareAI-lab/learn-claude-code](https://github.com/shareAI-lab/learn-claude-code)
21+
- **原作者**: shareAI Lab
22+
- **原项目协议**: MIT License
23+
24+
本仓库遵循相同的 MIT 开源协议,在保留原作者版权声明的基础上进行了 Java 重写和功能扩展。
25+
26+
## 核心理念
27+
28+
```
29+
模型是驾驶者,Harness 是载具。
30+
31+
Agent 是模型。不是框架。不是提示词链。不是拖拽式工作流。
32+
```
33+
34+
Claude Code 的本质就是一个 **while 循环**
35+
36+
```java
37+
while ("tool_use".equals(stopReason)) {
38+
response = client.createMessage(system, messages, tools, maxTokens);
39+
executeTools(response);
40+
appendResults(messages);
41+
}
42+
```
43+
44+
12 节课逐步增加能力,但循环本身从未改变。
45+
46+
## 可视化教学
47+
48+
打开 `docs/diagrams/index.html` 查看交互式可视化教学页面,包含:
49+
50+
- 架构全景图
51+
- 每节课的流程图和代码对照
52+
- Anthropic Messages API 完整字段说明
53+
54+
## 课程目录
55+
56+
| 编号 | 课程 | 核心概念 | 新增内容 |
57+
|------|------|----------|----------|
58+
| S00 | Prerequisites | Anthropic Messages API | 请求/响应字段说明 |
59+
| S01 | Agent Loop | while 循环 | 1 个工具 (bash) |
60+
| S02 | Tool Use | 分派表模式 | +3 个工具 (read/write/edit) |
61+
| S03 | TodoWrite | 自我跟踪 | TodoManager + Nag 提醒 |
62+
| S04 | Subagent | 上下文隔离 | 子代理 (fresh messages=[]) |
63+
| S05 | Skill Loading | 按需加载 | 两层注入 (目录→load_skill) |
64+
| S06 | Context Compact | 策略性遗忘 | 三层压缩管道 |
65+
| S07 | Task System | 持久化状态 | JSON 文件 + 依赖图 |
66+
| S08 | Background Tasks | 异步执行 | 线程池 + 通知注入 |
67+
| S09 | Agent Teams | 多 Agent 协作 | JSONL 消息总线 |
68+
| S10 | Team Protocols | 协议模式 | request_id 关联 (关闭/审批) |
69+
| S11 | Autonomous Agents | 自主行为 | 空闲轮询 + 自动认领 |
70+
| S12 | Worktree Isolation | 目录级隔离 | Git Worktree + 双平面架构 |
71+
72+
## 项目结构
73+
74+
```
75+
learn_claude_code_by_java/
76+
├── pom.xml # Maven 配置 (JDK 1.8 + Gson)
77+
├── src/main/java/learn/claude/code/
78+
│ ├── common/
79+
│ │ ├── AnthropicClient.java # API 客户端 (原生 HttpURLConnection)
80+
│ │ ├── BaseTools.java # 4 个基础工具 (bash/read/write/edit)
81+
│ │ └── ToolHandler.java # 工具处理器函数式接口
82+
│ ├── s01_agent_loop/ # 课程 01: Agent Loop
83+
│ ├── s02_tool_use/ # 课程 02: Tool Use
84+
│ ├── s03_todo_write/ # 课程 03: TodoWrite
85+
│ ├── s04_subagent/ # 课程 04: Subagent
86+
│ ├── s05_skill_loading/ # 课程 05: Skill Loading
87+
│ ├── s06_context_compact/ # 课程 06: Context Compact
88+
│ ├── s07_task_system/ # 课程 07: Task System
89+
│ ├── s08_background_tasks/ # 课程 08: Background Tasks
90+
│ ├── s09_agent_teams/ # 课程 09: Agent Teams
91+
│ ├── s10_team_protocols/ # 课程 10: Team Protocols
92+
│ ├── s11_autonomous_agents/ # 课程 11: Autonomous Agents
93+
│ └── s12_worktree_task_isolation/ # 课程 12: Worktree Isolation
94+
├── src/main/resources/
95+
│ ├── claude.properties # API 配置文件
96+
│ └── skills/ # S05 的技能文件
97+
└── docs/diagrams/ # 可视化教学页面 (HTML)
98+
```
99+
100+
## 快速开始
101+
102+
### 1. 环境要求
103+
104+
- JDK 1.8+
105+
- Maven 3.x
106+
- 一个兼容 Anthropic Messages API 的 API Key
107+
108+
### 2. 配置 API
109+
110+
编辑 `src/main/resources/claude.properties`
111+
112+
```properties
113+
ANTHROPIC_API_KEY=你的API密钥
114+
ANTHROPIC_BASE_URL=https://api.anthropic.com
115+
MODEL_ID=claude-sonnet-4-20250514
116+
```
117+
118+
> 支持任何兼容 Anthropic Messages API 的服务商(如智谱 GLM、第三方代理等)。
119+
120+
### 3. 编译
121+
122+
```bash
123+
mvn compile
124+
```
125+
126+
### 4. 运行任意课程
127+
128+
每个课程都有独立的 `main` 方法,可直接运行,然后对话。
129+
130+
## 设计原则
131+
132+
| 原则 | 说明 |
133+
|------|------|
134+
| **JDK 1.8 兼容** | 不用 `var``List.of()`、lambda(用匿名内部类) |
135+
| **零第三方依赖** | 仅 Gson (JSON),HTTP 用原生 `HttpURLConnection` |
136+
| **每课独立可运行** | 每个 `SxxXxx.java` 有自己的 `main` 方法 |
137+
| **渐进式复杂度** | 后续课程在前面基础上只增不改核心循环 |
138+
| **中英双语注释** | 所有源码包含详细的中文 + 英文注释 |
139+
140+
## 与原项目的差异
141+
142+
| 项目 | 原项目 (Python) | 本项目 (Java) |
143+
|------|----------------|---------------|
144+
| 语言 | Python 3.x | Java 1.8 |
145+
| HTTP 客户端 | httpx / requests | HttpURLConnection |
146+
| JSON 库 | 内置 json | Gson |
147+
| 异步 | asyncio | Thread + ExecutorService |
148+
| 配置 | .env | claude.properties |
149+
| 可视化 || HTML 交互式教学页面 |
150+
151+
## 贡献指南
152+
153+
欢迎提交 Issue 和 Pull Request!
154+
155+
1. Fork 本仓库
156+
2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
157+
3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
158+
4. 推送到分支 (`git push origin feature/AmazingFeature`)
159+
5. 创建 Pull Request
160+
161+
## 开源协议
162+
163+
本项目采用 [MIT License](LICENSE) 开源协议。
164+
165+
### 版权声明
166+
167+
| 项目 | 版权所有者 | 年份 |
168+
|------|-----------|------|
169+
| 原项目 (Python) | shareAI Lab | 2024 |
170+
| Java 版本 | arrayadd | 2025 |
171+
172+
本项目基于 [shareAI-lab/learn-claude-code](https://github.com/shareAI-lab/learn-claude-code) 进行 Java 重写,
173+
遵循 MIT 协议,保留原作者版权声明。
174+
175+
### 第三方依赖
176+
177+
| 依赖 | 协议 | 用途 |
178+
|------|------|------|
179+
| [Gson](https://github.com/google/gson) | Apache 2.0 | JSON 序列化 |
180+
181+
## 致谢
182+
183+
- 原版 Python 教程:[shareAI-lab/learn-claude-code](https://github.com/shareAI-lab/learn-claude-code)
184+
- Anthropic Claude: [anthropic.com](https://www.anthropic.com)
185+
186+
---
187+
188+
如果这个项目对你有帮助,欢迎 Star ⭐
189+
190+
---
191+
192+
## English Version
193+
194+
> **12 lessons to deconstruct the secrets of AI Agents from scratch, build your own Claude Code**
195+
>
196+
> Java (JDK 1.8) implementation, keeping logic fully consistent for Java developers.
197+
198+
### About This Project
199+
200+
This is a Java rewrite of [shareAI-lab/learn-claude-code](https://github.com/shareAI-lab/learn-claude-code),
201+
designed to help Java developers deeply understand how AI Agents work.
202+
203+
### Key Features
204+
205+
- **JDK 1.8 Compatible**: No modern Java features, maximum compatibility
206+
- **Minimal Dependencies**: Only Gson for JSON, native HttpURLConnection for HTTP
207+
- **Bilingual Comments**: Chinese + English comments in all source files
208+
- **Interactive Visualization**: HTML-based teaching diagrams in `docs/diagrams/`
209+
210+
### Quick Start
211+
212+
1. **Requirements**: JDK 1.8+, Maven 3.x
213+
2. **Configure**: Copy `claude.properties.example` to `claude.properties` and add your API key
214+
3. **Build**: `mvn compile`
215+
4. **Run**: Each lesson has its own `main()` method
216+
217+
### License
218+
219+
This project is licensed under the [MIT License](LICENSE).
220+
221+
### Acknowledgments
222+
223+
- Original Python tutorial: [shareAI-lab/learn-claude-code](https://github.com/shareAI-lab/learn-claude-code)

0 commit comments

Comments
 (0)