{"id":47949262,"url":"https://github.com/lgolgo/lancode","last_synced_at":"2026-04-09T13:01:15.845Z","repository":{"id":348915537,"uuid":"1200266256","full_name":"LgoLgo/lancode","owner":"LgoLgo","description":"用 Java 实现的极简 Claude Code Agent Loop","archived":false,"fork":false,"pushed_at":"2026-04-03T10:54:09.000Z","size":207,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-04-07T11:02:30.380Z","etag":null,"topics":["claude-code","java-agent"],"latest_commit_sha":null,"homepage":"https://lgolgo.github.io/lancode","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LgoLgo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-03T08:03:14.000Z","updated_at":"2026-04-07T02:16:35.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/LgoLgo/lancode","commit_stats":null,"previous_names":["lgolgo/lancode"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LgoLgo/lancode","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LgoLgo%2Flancode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LgoLgo%2Flancode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LgoLgo%2Flancode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LgoLgo%2Flancode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LgoLgo","download_url":"https://codeload.github.com/LgoLgo/lancode/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LgoLgo%2Flancode/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31554109,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T10:21:54.569Z","status":"ssl_error","status_checked_at":"2026-04-08T10:21:38.171Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["claude-code","java-agent"],"created_at":"2026-04-04T08:57:08.374Z","updated_at":"2026-04-08T12:01:06.233Z","avatar_url":"https://github.com/LgoLgo.png","language":"Java","readme":"\u003cdiv align=\"center\"\u003e\n\n\u003cimg src=\"img/logo.png\" alt=\"lancode\" width=\"480\" /\u003e\n\n**用 Java 实现的极简 Claude Code Agent Loop。**\n\n[![CI](https://github.com/LgoLgo/lancode/actions/workflows/ci.yml/badge.svg)](https://github.com/LgoLgo/lancode/actions/workflows/ci.yml)\n[![Java 17](https://img.shields.io/badge/Java-17-blue?logo=openjdk)](https://openjdk.org/projects/jdk/17/)\n[![License](https://img.shields.io/badge/License-Apache_2.0-green.svg)](LICENSE)\n\n~900 行。可对接任何 Anthropic 兼容 API。\n\n\u003c/div\u003e\n\n---\n\n从 Claude Code 的架构中提炼而来 —— Agent Loop、工具系统、权限模型、上下文管理 —— 剥离到最核心的结构，方便阅读和学习。\n\n## 课程\n\n配套课程逐章解析各子系统的实现原理，适合已有 LLM 基础、想深入了解 Agent Loop 内部机制的工程师。\n\n| 章节 | 内容 | 链接 |\n|------|------|------|\n| 第一章：Agent Loop | 核心循环逻辑 | [阅读](https://lgolgo.github.io/lancode/01-agent-loop) |\n| 第二章：工具系统 | Tool 接口、Registry、6 个内置工具 | [阅读](https://lgolgo.github.io/lancode/02-tool-system) |\n| 第三章：权限模型 | PermissionGate 双层检查机制 | [阅读](https://lgolgo.github.io/lancode/03-permission) |\n| 第四章：对话上下文 | ConversationContext 消息管理与截断 | [阅读](https://lgolgo.github.io/lancode/04-context) |\n| 第五章：系统提示词 | SystemPrompt 组装逻辑 | [阅读](https://lgolgo.github.io/lancode/05-system-prompt) |\n\n## 系统设计\n\n### 架构\n\n\u003cimg src=\"img/lancode-architecture.svg\" alt=\"lancode 架构图\" width=\"100%\" /\u003e\n\n### 时序\n\n一次典型 Agent Loop 的执行过程：\n\n```mermaid\n---\ntitle: lancode — 一次典型 Agent Loop\n---\n%%{\n  init: {\n    'theme': 'base',\n    'themeVariables': {\n      'fontFamily': 'Georgia, serif',\n      'actorBkg': '#E6E2DA',\n      'actorBorder': '#8C867F',\n      'actorTextColor': '#2C2C2C',\n      'actorLineColor': '#8C867F',\n      'signalColor': '#8C867F',\n      'signalTextColor': '#2C2C2C',\n      'labelBoxBkgColor': '#E6E2DA',\n      'labelBoxBorderColor': '#8C867F',\n      'labelTextColor': '#2C2C2C',\n      'loopTextColor': '#2C2C2C',\n      'activationBkgColor': '#CFE8D7',\n      'activationBorderColor': '#71AE88',\n      'noteBkgColor': '#F3E4DA',\n      'noteBorderColor': '#C88E6A',\n      'noteTextColor': '#2C2C2C'\n    }\n  }\n}%%\nsequenceDiagram\n    participant 用户\n    participant Main as Main（REPL）\n    participant AgentLoop\n    participant API as Anthropic API\n    participant ToolRegistry\n    participant Tool\n    participant ConversationContext\n\n    用户 -\u003e\u003e Main: 输入请求\n    Main -\u003e\u003e AgentLoop: run(userMessage)\n    AgentLoop -\u003e\u003e ConversationContext: addUserMessage()\n\n    loop 循环直到无 tool_use 或达到 maxTurns\n        AgentLoop -\u003e\u003e API: callApi()（含消息、系统提示、工具 schema）\n        API --\u003e\u003e AgentLoop: 返回响应（text 或 tool_use）\n\n        alt 返回 tool_use\n            AgentLoop -\u003e\u003e ToolRegistry: get(toolName)\n            ToolRegistry --\u003e\u003e AgentLoop: 返回 Tool 实例\n            AgentLoop -\u003e\u003e Tool: execute(input)（先经 PermissionGate 检查）\n            Tool --\u003e\u003e AgentLoop: ToolResult\n            AgentLoop -\u003e\u003e ConversationContext: addToolResults()\n        else 返回纯文本\n            AgentLoop -\u003e\u003e ConversationContext: addAssistantMessage()\n        end\n    end\n\n    AgentLoop --\u003e\u003e Main: 返回最终文本\n    Main --\u003e\u003e 用户: 输出结果\n```\n\n循环在模型返回不含 `tool_use` 的响应时退出，或达到 `maxTurns` 上限时终止。\n\n### 实现范围\n\n\u003cimg src=\"img/lancode-scope.png\" alt=\"lancode 子系统覆盖范围\" width=\"100%\" /\u003e\n\n## 安装\n\n需要 Java 17+ 和 Maven。\n\n```bash\ngit clone https://github.com/LgoLgo/lancode\ncd lancode\nmvn package -q -DskipTests\n```\n\n## 配置\n\n**官方 Anthropic API**\n\n```bash\nmkdir -p ~/.lancode\ncat \u003e ~/.lancode/settings.json \u003c\u003c 'EOF'\n{\n  \"model\": \"claude-opus-4-5\",\n  \"apiKey\": \"sk-ant-...\",\n  \"permissionMode\": \"AUTO\"\n}\nEOF\n```\n\n**第三方兼容 API**（如 LongCat、OpenRouter、自托管服务等）\n\n```bash\nmkdir -p ~/.lancode\ncat \u003e ~/.lancode/settings.json \u003c\u003c 'EOF'\n{\n  \"model\": \"your-model-name\",\n  \"baseUrl\": \"https://your-api-endpoint\",\n  \"authToken\": \"your-key-here\",\n  \"permissionMode\": \"AUTO\"\n}\nEOF\n```\n\n第三方 API 通常使用 `Authorization: Bearer` 认证，应使用 `authToken` 字段。`apiKey` 仅用于官方 Anthropic API（发 `x-api-key` 头）。\n\n未提供配置文件时，从环境变量 `ANTHROPIC_API_KEY` 读取密钥，使用官方 Anthropic 端点。\n\n## AGENT.md\n\n在项目根目录放置 `AGENT.md` 文件，可为 lancode 提供项目专属指令，启动时自动加载并注入系统提示。\n\n```\nyour-project/\n├── AGENT.md        ← lancode 自动读取\n└── src/\n```\n\n这是 lancode 对 Claude Code `CLAUDE.md` 机制的等价实现——命名为 `AGENT.md` 以区分：这是给 agent 的指令，而非给 Claude Code 工具本身的配置。\n\n## 运行\n\n```bash\n# 交互式 REPL\njava -jar target/lancode-0.1.0.jar\n\n# 单次执行\njava -jar target/lancode-0.1.0.jar \"列出当前目录的文件\"\n\n# 运行时覆盖配置\njava -jar target/lancode-0.1.0.jar --model claude-opus-4-5 --mode ask\n```\n\n## REPL 命令\n\n| 命令 | 说明 |\n|------|------|\n| `/tools` | 列出可用工具 |\n| `/mode [ask\\|auto\\|plan]` | 查看或切换权限模式 |\n| `/help` | 显示帮助 |\n| `/quit` | 退出 |\n\n## settings.json 字段说明\n\n| 字段 | 默认值 | 说明 |\n|------|--------|------|\n| `model` | `claude-opus-4-5` | 传给 API 的模型名 |\n| `baseUrl` | Anthropic 官方端点 | 自定义 API 地址 |\n| `apiKey` | `$ANTHROPIC_API_KEY` | 官方 Anthropic API，发 `x-api-key` 头 |\n| `authToken` | — | 第三方 API，发 `Authorization: Bearer` 头 |\n| `permissionMode` | `AUTO` | `AUTO` \\| `ASK` \\| `PLAN` |\n| `maxTurns` | `30` | 每条消息最大 Agent Loop 轮数 |\n| `maxContextMessages` | `100` | 触发截断前的消息历史上限 |\n\n**权限模式说明**\n\n- `AUTO` — 所有工具自动执行，无需确认\n- `ASK` — 不在安全列表中的 bash 命令会提示 `[y/N]`\n- `PLAN` — 只读模式；bash、write_file、edit_file 被禁用\n\n## 工具列表\n\n| 工具 | 说明 |\n|------|------|\n| `bash` | 通过 `ProcessBuilder` 执行 shell 命令 |\n| `read_file` | 读取文件内容 |\n| `write_file` | 写入或创建文件 |\n| `edit_file` | 精确字符串替换（StrReplace） |\n| `glob` | 按 glob 模式查找文件 |\n| `grep` | 按正则搜索文件内容 |\n\n## 开发\n\n```bash\nmvn test                   # 运行测试\nmvn compile                # 仅编译\nmvn package -DskipTests    # 构建 fat jar\n```\n\n测试位于 `src/test/java/com/lancode/tools/`。\n\n## 参考\n\n- [ultraworkers/claw-code](https://github.com/ultraworkers/claw-code)\n- [bcefghj/miniClaudeCode](https://github.com/bcefghj/miniClaudeCode)\n- Special Thanks: [LinuxDO](https://linux.do/)\n\n## 许可证\n\nApache 2.0\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flgolgo%2Flancode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flgolgo%2Flancode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flgolgo%2Flancode/lists"}