|
| 1 | +# Learn Claude Code by Java |
| 2 | + |
| 3 | +[](https://opensource.org/licenses/MIT) |
| 4 | +[](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