aka-claude 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. aka_claude-0.1.2/.gitignore +38 -0
  2. aka_claude-0.1.2/LICENSE +21 -0
  3. aka_claude-0.1.2/PKG-INFO +239 -0
  4. aka_claude-0.1.2/README.md +187 -0
  5. aka_claude-0.1.2/main.py +34 -0
  6. aka_claude-0.1.2/pyproject.toml +95 -0
  7. aka_claude-0.1.2/scripts/install.sh +122 -0
  8. aka_claude-0.1.2/src/claude_code/__init__.py +4 -0
  9. aka_claude-0.1.2/src/claude_code/__main__.py +6 -0
  10. aka_claude-0.1.2/src/claude_code/agents/__init__.py +20 -0
  11. aka_claude-0.1.2/src/claude_code/agents/background.py +285 -0
  12. aka_claude-0.1.2/src/claude_code/agents/manager.py +199 -0
  13. aka_claude-0.1.2/src/claude_code/agents/subagent.py +210 -0
  14. aka_claude-0.1.2/src/claude_code/cli.py +368 -0
  15. aka_claude-0.1.2/src/claude_code/core/__init__.py +38 -0
  16. aka_claude-0.1.2/src/claude_code/core/app.py +389 -0
  17. aka_claude-0.1.2/src/claude_code/core/config.py +282 -0
  18. aka_claude-0.1.2/src/claude_code/core/context.py +261 -0
  19. aka_claude-0.1.2/src/claude_code/core/loop_mode.py +49 -0
  20. aka_claude-0.1.2/src/claude_code/core/message.py +285 -0
  21. aka_claude-0.1.2/src/claude_code/core/query_engine.py +590 -0
  22. aka_claude-0.1.2/src/claude_code/core/session.py +248 -0
  23. aka_claude-0.1.2/src/claude_code/core/slash.py +373 -0
  24. aka_claude-0.1.2/src/claude_code/core/store.py +186 -0
  25. aka_claude-0.1.2/src/claude_code/core/system_prompt.py +436 -0
  26. aka_claude-0.1.2/src/claude_code/hooks/__init__.py +23 -0
  27. aka_claude-0.1.2/src/claude_code/hooks/events.py +176 -0
  28. aka_claude-0.1.2/src/claude_code/hooks/executor.py +163 -0
  29. aka_claude-0.1.2/src/claude_code/hooks/manager.py +170 -0
  30. aka_claude-0.1.2/src/claude_code/mcp/__init__.py +0 -0
  31. aka_claude-0.1.2/src/claude_code/mcp/client.py +347 -0
  32. aka_claude-0.1.2/src/claude_code/mcp/server_manager.py +303 -0
  33. aka_claude-0.1.2/src/claude_code/mcp/tool_discovery.py +162 -0
  34. aka_claude-0.1.2/src/claude_code/mcp/transport.py +482 -0
  35. aka_claude-0.1.2/src/claude_code/permissions/__init__.py +75 -0
  36. aka_claude-0.1.2/src/claude_code/permissions/manager.py +359 -0
  37. aka_claude-0.1.2/src/claude_code/permissions/modes.py +121 -0
  38. aka_claude-0.1.2/src/claude_code/permissions/prompt.py +257 -0
  39. aka_claude-0.1.2/src/claude_code/permissions/rules.py +279 -0
  40. aka_claude-0.1.2/src/claude_code/permissions/sandbox.py +329 -0
  41. aka_claude-0.1.2/src/claude_code/plugins/__init__.py +14 -0
  42. aka_claude-0.1.2/src/claude_code/plugins/lifecycle.py +268 -0
  43. aka_claude-0.1.2/src/claude_code/plugins/loader.py +219 -0
  44. aka_claude-0.1.2/src/claude_code/plugins/registry.py +106 -0
  45. aka_claude-0.1.2/src/claude_code/providers/__init__.py +103 -0
  46. aka_claude-0.1.2/src/claude_code/providers/anthropic_provider.py +372 -0
  47. aka_claude-0.1.2/src/claude_code/providers/azure.py +595 -0
  48. aka_claude-0.1.2/src/claude_code/providers/base.py +234 -0
  49. aka_claude-0.1.2/src/claude_code/providers/bedrock.py +472 -0
  50. aka_claude-0.1.2/src/claude_code/providers/openai_compat.py +520 -0
  51. aka_claude-0.1.2/src/claude_code/providers/retry.py +222 -0
  52. aka_claude-0.1.2/src/claude_code/providers/streaming.py +326 -0
  53. aka_claude-0.1.2/src/claude_code/providers/vertex.py +548 -0
  54. aka_claude-0.1.2/src/claude_code/services/__init__.py +22 -0
  55. aka_claude-0.1.2/src/claude_code/services/auth.py +337 -0
  56. aka_claude-0.1.2/src/claude_code/services/context_pruner.py +214 -0
  57. aka_claude-0.1.2/src/claude_code/services/cost_tracker.py +174 -0
  58. aka_claude-0.1.2/src/claude_code/services/lsp_client.py +429 -0
  59. aka_claude-0.1.2/src/claude_code/services/telemetry.py +37 -0
  60. aka_claude-0.1.2/src/claude_code/services/updater.py +210 -0
  61. aka_claude-0.1.2/src/claude_code/skills/__init__.py +10 -0
  62. aka_claude-0.1.2/src/claude_code/skills/commands/__init__.py +86 -0
  63. aka_claude-0.1.2/src/claude_code/skills/commands/add_dir.py +48 -0
  64. aka_claude-0.1.2/src/claude_code/skills/commands/agents.py +52 -0
  65. aka_claude-0.1.2/src/claude_code/skills/commands/base.py +80 -0
  66. aka_claude-0.1.2/src/claude_code/skills/commands/bug.py +36 -0
  67. aka_claude-0.1.2/src/claude_code/skills/commands/clear.py +19 -0
  68. aka_claude-0.1.2/src/claude_code/skills/commands/compact.py +26 -0
  69. aka_claude-0.1.2/src/claude_code/skills/commands/config.py +53 -0
  70. aka_claude-0.1.2/src/claude_code/skills/commands/cost.py +31 -0
  71. aka_claude-0.1.2/src/claude_code/skills/commands/doctor.py +67 -0
  72. aka_claude-0.1.2/src/claude_code/skills/commands/help.py +59 -0
  73. aka_claude-0.1.2/src/claude_code/skills/commands/hooks.py +41 -0
  74. aka_claude-0.1.2/src/claude_code/skills/commands/init.py +50 -0
  75. aka_claude-0.1.2/src/claude_code/skills/commands/loop.py +84 -0
  76. aka_claude-0.1.2/src/claude_code/skills/commands/mcp.py +36 -0
  77. aka_claude-0.1.2/src/claude_code/skills/commands/memory.py +48 -0
  78. aka_claude-0.1.2/src/claude_code/skills/commands/model.py +31 -0
  79. aka_claude-0.1.2/src/claude_code/skills/commands/permissions.py +64 -0
  80. aka_claude-0.1.2/src/claude_code/skills/commands/plugins.py +119 -0
  81. aka_claude-0.1.2/src/claude_code/skills/commands/rename.py +30 -0
  82. aka_claude-0.1.2/src/claude_code/skills/commands/review.py +40 -0
  83. aka_claude-0.1.2/src/claude_code/skills/commands/status.py +40 -0
  84. aka_claude-0.1.2/src/claude_code/skills/discovery.py +253 -0
  85. aka_claude-0.1.2/src/claude_code/skills/manager.py +154 -0
  86. aka_claude-0.1.2/src/claude_code/tools/__init__.py +120 -0
  87. aka_claude-0.1.2/src/claude_code/tools/ask_user.py +201 -0
  88. aka_claude-0.1.2/src/claude_code/tools/base.py +106 -0
  89. aka_claude-0.1.2/src/claude_code/tools/bash.py +295 -0
  90. aka_claude-0.1.2/src/claude_code/tools/bash_output.py +131 -0
  91. aka_claude-0.1.2/src/claude_code/tools/diagnostics.py +173 -0
  92. aka_claude-0.1.2/src/claude_code/tools/edit.py +184 -0
  93. aka_claude-0.1.2/src/claude_code/tools/execute_code.py +228 -0
  94. aka_claude-0.1.2/src/claude_code/tools/exit_plan_mode.py +51 -0
  95. aka_claude-0.1.2/src/claude_code/tools/factory.py +51 -0
  96. aka_claude-0.1.2/src/claude_code/tools/glob_tool.py +99 -0
  97. aka_claude-0.1.2/src/claude_code/tools/grep.py +272 -0
  98. aka_claude-0.1.2/src/claude_code/tools/kill_shell.py +137 -0
  99. aka_claude-0.1.2/src/claude_code/tools/ls.py +152 -0
  100. aka_claude-0.1.2/src/claude_code/tools/mcp_tool.py +108 -0
  101. aka_claude-0.1.2/src/claude_code/tools/multi_edit.py +163 -0
  102. aka_claude-0.1.2/src/claude_code/tools/notebook_edit.py +251 -0
  103. aka_claude-0.1.2/src/claude_code/tools/notebook_read.py +156 -0
  104. aka_claude-0.1.2/src/claude_code/tools/read.py +225 -0
  105. aka_claude-0.1.2/src/claude_code/tools/registry.py +213 -0
  106. aka_claude-0.1.2/src/claude_code/tools/task.py +163 -0
  107. aka_claude-0.1.2/src/claude_code/tools/todo_read.py +75 -0
  108. aka_claude-0.1.2/src/claude_code/tools/todo_write.py +147 -0
  109. aka_claude-0.1.2/src/claude_code/tools/web_fetch.py +348 -0
  110. aka_claude-0.1.2/src/claude_code/tools/web_search.py +224 -0
  111. aka_claude-0.1.2/src/claude_code/tools/write.py +103 -0
  112. aka_claude-0.1.2/src/claude_code/ui/__init__.py +6 -0
  113. aka_claude-0.1.2/src/claude_code/ui/app_ui.py +279 -0
  114. aka_claude-0.1.2/src/claude_code/ui/components/__init__.py +25 -0
  115. aka_claude-0.1.2/src/claude_code/ui/components/chat_display.py +213 -0
  116. aka_claude-0.1.2/src/claude_code/ui/components/input_box.py +198 -0
  117. aka_claude-0.1.2/src/claude_code/ui/components/progress.py +137 -0
  118. aka_claude-0.1.2/src/claude_code/ui/components/spinner.py +69 -0
  119. aka_claude-0.1.2/src/claude_code/ui/components/status_bar.py +137 -0
  120. aka_claude-0.1.2/src/claude_code/ui/components/theme.py +174 -0
  121. aka_claude-0.1.2/src/claude_code/ui/components/tool_output.py +145 -0
  122. aka_claude-0.1.2/src/claude_code/ui/renderer.py +320 -0
  123. aka_claude-0.1.2/src/claude_code/utils/__init__.py +1 -0
  124. aka_claude-0.1.2/src/claude_code/utils/file_utils.py +265 -0
  125. aka_claude-0.1.2/src/claude_code/utils/git_utils.py +155 -0
  126. aka_claude-0.1.2/src/claude_code/utils/logging.py +122 -0
  127. aka_claude-0.1.2/src/claude_code/utils/path_utils.py +154 -0
  128. aka_claude-0.1.2/src/claude_code/utils/platform.py +133 -0
  129. aka_claude-0.1.2/src/claude_code/utils/text.py +113 -0
  130. aka_claude-0.1.2/src/claude_code/worktree/__init__.py +14 -0
  131. aka_claude-0.1.2/src/claude_code/worktree/manager.py +220 -0
@@ -0,0 +1,38 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ *.egg-info/
7
+ dist/
8
+ build/
9
+ .eggs/
10
+
11
+ # Virtual environments
12
+ .venv/
13
+ venv/
14
+ ENV/
15
+
16
+ # IDE
17
+ .idea/
18
+ .vscode/
19
+ *.swp
20
+ *.swo
21
+ *~
22
+
23
+ # OS
24
+ .DS_Store
25
+ Thumbs.db
26
+
27
+ # Testing
28
+ .coverage
29
+ htmlcov/
30
+ .pytest_cache/
31
+
32
+ # MyPy
33
+ .mypy_cache/
34
+
35
+ # Project specific
36
+ .claude/
37
+ *.log
38
+ .env
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Claude Code Py
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,239 @@
1
+ Metadata-Version: 2.4
2
+ Name: aka_claude
3
+ Version: 0.1.2
4
+ Summary: Python implementation of Claude Code - an agentic coding assistant in your terminal
5
+ Project-URL: Homepage, https://github.com/anthropics/claude-code
6
+ Project-URL: Documentation, https://code.claude.com/docs
7
+ Author: Claude Code Py
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: ai,assistant,claude,cli,coding,terminal
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Topic :: Software Development
20
+ Classifier: Topic :: Software Development :: Code Generators
21
+ Requires-Python: >=3.11
22
+ Requires-Dist: aiofiles>=24.0.0
23
+ Requires-Dist: anthropic>=0.40.0
24
+ Requires-Dist: chardet>=5.0.0
25
+ Requires-Dist: click>=8.1.0
26
+ Requires-Dist: httpx>=0.27.0
27
+ Requires-Dist: openai>=1.50.0
28
+ Requires-Dist: prompt-toolkit>=3.0.0
29
+ Requires-Dist: pydantic>=2.0.0
30
+ Requires-Dist: pygments>=2.17.0
31
+ Requires-Dist: rich>=13.0.0
32
+ Provides-Extra: all
33
+ Requires-Dist: azure-ai-inference>=1.0.0b9; extra == 'all'
34
+ Requires-Dist: boto3>=1.35.0; extra == 'all'
35
+ Requires-Dist: google-cloud-aiplatform>=1.70.0; extra == 'all'
36
+ Requires-Dist: mcp>=1.0.0; extra == 'all'
37
+ Provides-Extra: aws
38
+ Requires-Dist: boto3>=1.35.0; extra == 'aws'
39
+ Provides-Extra: azure
40
+ Requires-Dist: azure-ai-inference>=1.0.0b9; extra == 'azure'
41
+ Provides-Extra: dev
42
+ Requires-Dist: mypy>=1.10.0; extra == 'dev'
43
+ Requires-Dist: pytest-asyncio>=0.24.0; extra == 'dev'
44
+ Requires-Dist: pytest-cov>=5.0.0; extra == 'dev'
45
+ Requires-Dist: pytest>=8.0.0; extra == 'dev'
46
+ Requires-Dist: ruff>=0.5.0; extra == 'dev'
47
+ Provides-Extra: gcp
48
+ Requires-Dist: google-cloud-aiplatform>=1.70.0; extra == 'gcp'
49
+ Provides-Extra: mcp
50
+ Requires-Dist: mcp>=1.0.0; extra == 'mcp'
51
+ Description-Content-Type: text/markdown
52
+
53
+ # Claude Code Py
54
+
55
+ Python 实现的 [Claude Code](https://github.com/anthropics/claude-code) — 终端中的 AI 编程助手,目标是 1:1 还原原版 TypeScript 版本的功能。
56
+
57
+ > **状态:Alpha。** 核心对话循环、工具、多后端、权限、会话等子系统均已实现并有测试覆盖,但仍在积极开发中,接口可能变动。
58
+
59
+ ## 快速安装
60
+
61
+ 一句话安装(二选一):
62
+
63
+ ```bash
64
+ # 方式 A:pip 直接从 GitHub 安装,装好 `aka` 命令(跨平台,推荐)
65
+ pip install "git+https://github.com/wstart/claude-code-py.git"
66
+
67
+ # 方式 B:一键脚本,装成自定义命令名并交互式配置 .env(默认命令名 aka)
68
+ curl -fsSL https://raw.githubusercontent.com/wstart/claude-code-py/main/scripts/install.sh | bash
69
+ # 指定命令名:
70
+ curl -fsSL https://raw.githubusercontent.com/wstart/claude-code-py/main/scripts/install.sh | bash -s -- mytool
71
+ ```
72
+
73
+ 方式 A 装完后命令是 **`aka`**,用环境变量配置 key 后即可运行;方式 B 会把仓库装到 `~/.aka` 并交互式写好 `~/.aka/.env`,之后直接用你指定的命令名启动。详细配置见下方 [配置](#配置)。
74
+
75
+ ## 功能
76
+
77
+ - 🤖 **Agentic 对话循环** — 流式输出、自动调用工具、处理结果、多轮持续对话,含 Token 预算与最大轮次限制
78
+ - 📁 **文件工具** — Read / Write / Edit / MultiEdit / Glob / Grep / LS(自动编码探测、保留 CRLF、大文件保护)
79
+ - 💻 **Shell 执行** — 持久化 Bash 会话、超时控制、后台任务(BashOutput / KillShell)
80
+ - 🌐 **多后端** — Anthropic、OpenAI 兼容(Ollama / vLLM 等)、Amazon Bedrock、Google Vertex AI、Azure OpenAI
81
+ - 🔁 **健壮性** — 对可重试错误(429 / 5xx / 过载)做指数退避重试,且仅在流出内容前重试以避免重复输出
82
+ - 🔐 **权限系统** — allow / deny / ask 规则(deny 优先),manual / auto / plan / bypass 四种模式,macOS 沙箱
83
+ - 🪝 **Hooks** — PreToolUse / PostToolUse 等事件钩子;安全 gate 钩子执行失败时 fail-closed
84
+ - 🔌 **MCP** — Model Context Protocol 客户端(stdio / SSE 传输),发现并调用外部工具
85
+ - 🧩 **子代理与后台任务** — Task 工具派生只读研究子代理
86
+ - 🌐 **Web 工具** — WebFetch(含 SSRF 防护)/ WebSearch
87
+ - 🖥️ **终端 UI** — prompt_toolkit + rich(Markdown、代码高亮、Diff、状态栏)
88
+ - 💾 **会话管理** — 创建 / 恢复 / 持久化,完整保留 tool_use / tool_result 历史
89
+ - 📊 **成本追踪** — 按当前模型价格实时统计 Token 用量与费用估算
90
+ - 🔧 **斜杠命令** — 20+ 命令(见下)
91
+ - 📝 **CLAUDE.md** — 全局 + 项目级指令加载
92
+
93
+ ## 安装
94
+
95
+ ```bash
96
+ # 从源码开发安装
97
+ pip install -e ".[dev]"
98
+
99
+ # 可选后端依赖
100
+ pip install -e ".[aws]" # Amazon Bedrock (boto3)
101
+ pip install -e ".[gcp]" # Google Vertex AI
102
+ pip install -e ".[azure]" # Azure OpenAI
103
+ pip install -e ".[mcp]" # MCP 支持
104
+ pip install -e ".[all]" # 全部可选依赖
105
+ ```
106
+
107
+ 也可用安装脚本以自定义命令名安装:
108
+
109
+ ```bash
110
+ ./scripts/install.sh # 安装为默认命令名
111
+ ./scripts/install.sh mytool # 安装为 mytool
112
+ ```
113
+
114
+ ### 配置
115
+
116
+ 通过环境变量(或 `~/.claude/.env`)配置:
117
+
118
+ ```bash
119
+ export ANTHROPIC_API_KEY="sk-ant-..."
120
+ export ANTHROPIC_BASE_URL="https://api.anthropic.com" # 可选,自定义/代理端点
121
+ export CLAUDE_MODEL="claude-sonnet-4-6" # 可选,默认模型
122
+ ```
123
+
124
+ 自定义 `base_url` 若是 **http 明文端点**可直接使用;若是**自签名 / 内网 HTTPS**证书导致连接失败,可显式关闭证书校验(不安全,默认开启校验):
125
+
126
+ ```bash
127
+ export CLAUDE_SKIP_SSL_VERIFY=1 # 跳过 TLS 证书验证,仅用于可信的内网/自签端点
128
+ ```
129
+
130
+ ## 使用
131
+
132
+ ```bash
133
+ # 交互式模式
134
+ aka
135
+
136
+ # 带初始提问
137
+ aka "解释这个项目的架构"
138
+
139
+ # 非交互模式(print)
140
+ aka -p "写一个 Python hello world"
141
+
142
+ # 指定模型(支持别名,见 --model)
143
+ aka --model claude-sonnet-4-6 "优化这段代码"
144
+
145
+ # 恢复上次会话 / 指定会话
146
+ aka -c
147
+ aka -r <session-id>
148
+
149
+ # JSON / 流式 JSON 输出
150
+ aka -p --output-format json "列出所有 TODO"
151
+
152
+ # 权限模式:manual(默认)| auto | plan | bypass
153
+ aka --permission-mode auto "重构这个模块"
154
+ ```
155
+
156
+ ## 斜杠命令
157
+
158
+ | 命令 | 说明 |
159
+ |------|------|
160
+ | `/help` | 显示帮助 |
161
+ | `/clear` | 清空对话 |
162
+ | `/compact` | 压缩上下文 |
163
+ | `/cost` | 显示 Token 用量与费用 |
164
+ | `/model [name]` | 查看 / 切换模型 |
165
+ | `/status` | 会话信息 |
166
+ | `/config` | 当前配置 |
167
+ | `/memory` | 显示 CLAUDE.md |
168
+ | `/init` | 生成项目 CLAUDE.md |
169
+ | `/permissions` | 查看 / 管理权限规则 |
170
+ | `/hooks` | 查看 hooks 配置 |
171
+ | `/mcp` | 管理 MCP 服务器 |
172
+ | `/agents` | 查看子代理 |
173
+ | `/plugins` | 管理插件 |
174
+ | `/add-dir` | 添加可访问目录 |
175
+ | `/review` | 代码审查 |
176
+ | `/rename` | 重命名会话 |
177
+ | `/doctor` | 诊断检查 |
178
+ | `/bug` | 反馈问题 |
179
+ | `/exit` | 退出 |
180
+
181
+ ## 架构
182
+
183
+ ```
184
+ src/claude_code/
185
+ ├── cli.py # CLI 入口 (click)
186
+ ├── core/ # 核心引擎
187
+ │ ├── app.py # 应用主类
188
+ │ ├── query_engine.py # Agentic 对话循环 + 重试
189
+ │ ├── message.py # 消息模型 (Pydantic)
190
+ │ ├── session.py # 会话持久化
191
+ │ ├── context.py # 上下文压缩
192
+ │ ├── config.py # 配置加载
193
+ │ └── store.py # 状态存储
194
+ ├── providers/ # LLM 后端
195
+ │ ├── anthropic_provider.py
196
+ │ ├── openai_compat.py # OpenAI 兼容 (Ollama/vLLM/…)
197
+ │ ├── bedrock.py # Amazon Bedrock
198
+ │ ├── vertex.py # Google Vertex AI
199
+ │ ├── azure.py # Azure OpenAI
200
+ │ ├── streaming.py # 流式事件归一化
201
+ │ └── retry.py # 指数退避重试
202
+ ├── tools/ # 21 个工具 (文件/Shell/搜索/Web/编排)
203
+ ├── ui/ # 终端 UI (prompt_toolkit + rich)
204
+ ├── permissions/ # 权限系统 (规则/模式/沙箱/提示)
205
+ ├── hooks/ # Hooks 系统
206
+ ├── mcp/ # MCP 协议客户端
207
+ ├── agents/ # 子代理 + 后台任务
208
+ ├── plugins/ # 插件系统
209
+ ├── skills/ # 斜杠命令 + Skills
210
+ ├── services/ # 认证 / 成本 / LSP
211
+ └── utils/ # 工具函数
212
+ ```
213
+
214
+ ## 开发
215
+
216
+ ```bash
217
+ # 运行测试
218
+ pytest tests/ -v
219
+
220
+ # Lint 与类型检查
221
+ ruff check src/ tests/
222
+ mypy src/claude_code
223
+ ```
224
+
225
+ 当前 141 个测试覆盖消息模型、上下文压缩、对话循环与重试、工具(文件/编码/搜索)、权限规则、provider 消息转换、会话恢复、hooks 等。
226
+
227
+ ## 技术栈
228
+
229
+ - Python 3.11+
230
+ - [click](https://click.palletsprojects.com/) — CLI
231
+ - [prompt_toolkit](https://python-prompt-toolkit.readthedocs.io/) — 交互式输入
232
+ - [rich](https://rich.readthedocs.io/) — 终端渲染
233
+ - [pydantic](https://docs.pydantic.dev/) v2 — 数据校验
234
+ - [anthropic](https://github.com/anthropics/anthropic-sdk-python) / [openai](https://github.com/openai/openai-python) — LLM SDK
235
+ - [httpx](https://www.python-httpx.org/) — HTTP 客户端
236
+
237
+ ## License
238
+
239
+ MIT
@@ -0,0 +1,187 @@
1
+ # Claude Code Py
2
+
3
+ Python 实现的 [Claude Code](https://github.com/anthropics/claude-code) — 终端中的 AI 编程助手,目标是 1:1 还原原版 TypeScript 版本的功能。
4
+
5
+ > **状态:Alpha。** 核心对话循环、工具、多后端、权限、会话等子系统均已实现并有测试覆盖,但仍在积极开发中,接口可能变动。
6
+
7
+ ## 快速安装
8
+
9
+ 一句话安装(二选一):
10
+
11
+ ```bash
12
+ # 方式 A:pip 直接从 GitHub 安装,装好 `aka` 命令(跨平台,推荐)
13
+ pip install "git+https://github.com/wstart/claude-code-py.git"
14
+
15
+ # 方式 B:一键脚本,装成自定义命令名并交互式配置 .env(默认命令名 aka)
16
+ curl -fsSL https://raw.githubusercontent.com/wstart/claude-code-py/main/scripts/install.sh | bash
17
+ # 指定命令名:
18
+ curl -fsSL https://raw.githubusercontent.com/wstart/claude-code-py/main/scripts/install.sh | bash -s -- mytool
19
+ ```
20
+
21
+ 方式 A 装完后命令是 **`aka`**,用环境变量配置 key 后即可运行;方式 B 会把仓库装到 `~/.aka` 并交互式写好 `~/.aka/.env`,之后直接用你指定的命令名启动。详细配置见下方 [配置](#配置)。
22
+
23
+ ## 功能
24
+
25
+ - 🤖 **Agentic 对话循环** — 流式输出、自动调用工具、处理结果、多轮持续对话,含 Token 预算与最大轮次限制
26
+ - 📁 **文件工具** — Read / Write / Edit / MultiEdit / Glob / Grep / LS(自动编码探测、保留 CRLF、大文件保护)
27
+ - 💻 **Shell 执行** — 持久化 Bash 会话、超时控制、后台任务(BashOutput / KillShell)
28
+ - 🌐 **多后端** — Anthropic、OpenAI 兼容(Ollama / vLLM 等)、Amazon Bedrock、Google Vertex AI、Azure OpenAI
29
+ - 🔁 **健壮性** — 对可重试错误(429 / 5xx / 过载)做指数退避重试,且仅在流出内容前重试以避免重复输出
30
+ - 🔐 **权限系统** — allow / deny / ask 规则(deny 优先),manual / auto / plan / bypass 四种模式,macOS 沙箱
31
+ - 🪝 **Hooks** — PreToolUse / PostToolUse 等事件钩子;安全 gate 钩子执行失败时 fail-closed
32
+ - 🔌 **MCP** — Model Context Protocol 客户端(stdio / SSE 传输),发现并调用外部工具
33
+ - 🧩 **子代理与后台任务** — Task 工具派生只读研究子代理
34
+ - 🌐 **Web 工具** — WebFetch(含 SSRF 防护)/ WebSearch
35
+ - 🖥️ **终端 UI** — prompt_toolkit + rich(Markdown、代码高亮、Diff、状态栏)
36
+ - 💾 **会话管理** — 创建 / 恢复 / 持久化,完整保留 tool_use / tool_result 历史
37
+ - 📊 **成本追踪** — 按当前模型价格实时统计 Token 用量与费用估算
38
+ - 🔧 **斜杠命令** — 20+ 命令(见下)
39
+ - 📝 **CLAUDE.md** — 全局 + 项目级指令加载
40
+
41
+ ## 安装
42
+
43
+ ```bash
44
+ # 从源码开发安装
45
+ pip install -e ".[dev]"
46
+
47
+ # 可选后端依赖
48
+ pip install -e ".[aws]" # Amazon Bedrock (boto3)
49
+ pip install -e ".[gcp]" # Google Vertex AI
50
+ pip install -e ".[azure]" # Azure OpenAI
51
+ pip install -e ".[mcp]" # MCP 支持
52
+ pip install -e ".[all]" # 全部可选依赖
53
+ ```
54
+
55
+ 也可用安装脚本以自定义命令名安装:
56
+
57
+ ```bash
58
+ ./scripts/install.sh # 安装为默认命令名
59
+ ./scripts/install.sh mytool # 安装为 mytool
60
+ ```
61
+
62
+ ### 配置
63
+
64
+ 通过环境变量(或 `~/.claude/.env`)配置:
65
+
66
+ ```bash
67
+ export ANTHROPIC_API_KEY="sk-ant-..."
68
+ export ANTHROPIC_BASE_URL="https://api.anthropic.com" # 可选,自定义/代理端点
69
+ export CLAUDE_MODEL="claude-sonnet-4-6" # 可选,默认模型
70
+ ```
71
+
72
+ 自定义 `base_url` 若是 **http 明文端点**可直接使用;若是**自签名 / 内网 HTTPS**证书导致连接失败,可显式关闭证书校验(不安全,默认开启校验):
73
+
74
+ ```bash
75
+ export CLAUDE_SKIP_SSL_VERIFY=1 # 跳过 TLS 证书验证,仅用于可信的内网/自签端点
76
+ ```
77
+
78
+ ## 使用
79
+
80
+ ```bash
81
+ # 交互式模式
82
+ aka
83
+
84
+ # 带初始提问
85
+ aka "解释这个项目的架构"
86
+
87
+ # 非交互模式(print)
88
+ aka -p "写一个 Python hello world"
89
+
90
+ # 指定模型(支持别名,见 --model)
91
+ aka --model claude-sonnet-4-6 "优化这段代码"
92
+
93
+ # 恢复上次会话 / 指定会话
94
+ aka -c
95
+ aka -r <session-id>
96
+
97
+ # JSON / 流式 JSON 输出
98
+ aka -p --output-format json "列出所有 TODO"
99
+
100
+ # 权限模式:manual(默认)| auto | plan | bypass
101
+ aka --permission-mode auto "重构这个模块"
102
+ ```
103
+
104
+ ## 斜杠命令
105
+
106
+ | 命令 | 说明 |
107
+ |------|------|
108
+ | `/help` | 显示帮助 |
109
+ | `/clear` | 清空对话 |
110
+ | `/compact` | 压缩上下文 |
111
+ | `/cost` | 显示 Token 用量与费用 |
112
+ | `/model [name]` | 查看 / 切换模型 |
113
+ | `/status` | 会话信息 |
114
+ | `/config` | 当前配置 |
115
+ | `/memory` | 显示 CLAUDE.md |
116
+ | `/init` | 生成项目 CLAUDE.md |
117
+ | `/permissions` | 查看 / 管理权限规则 |
118
+ | `/hooks` | 查看 hooks 配置 |
119
+ | `/mcp` | 管理 MCP 服务器 |
120
+ | `/agents` | 查看子代理 |
121
+ | `/plugins` | 管理插件 |
122
+ | `/add-dir` | 添加可访问目录 |
123
+ | `/review` | 代码审查 |
124
+ | `/rename` | 重命名会话 |
125
+ | `/doctor` | 诊断检查 |
126
+ | `/bug` | 反馈问题 |
127
+ | `/exit` | 退出 |
128
+
129
+ ## 架构
130
+
131
+ ```
132
+ src/claude_code/
133
+ ├── cli.py # CLI 入口 (click)
134
+ ├── core/ # 核心引擎
135
+ │ ├── app.py # 应用主类
136
+ │ ├── query_engine.py # Agentic 对话循环 + 重试
137
+ │ ├── message.py # 消息模型 (Pydantic)
138
+ │ ├── session.py # 会话持久化
139
+ │ ├── context.py # 上下文压缩
140
+ │ ├── config.py # 配置加载
141
+ │ └── store.py # 状态存储
142
+ ├── providers/ # LLM 后端
143
+ │ ├── anthropic_provider.py
144
+ │ ├── openai_compat.py # OpenAI 兼容 (Ollama/vLLM/…)
145
+ │ ├── bedrock.py # Amazon Bedrock
146
+ │ ├── vertex.py # Google Vertex AI
147
+ │ ├── azure.py # Azure OpenAI
148
+ │ ├── streaming.py # 流式事件归一化
149
+ │ └── retry.py # 指数退避重试
150
+ ├── tools/ # 21 个工具 (文件/Shell/搜索/Web/编排)
151
+ ├── ui/ # 终端 UI (prompt_toolkit + rich)
152
+ ├── permissions/ # 权限系统 (规则/模式/沙箱/提示)
153
+ ├── hooks/ # Hooks 系统
154
+ ├── mcp/ # MCP 协议客户端
155
+ ├── agents/ # 子代理 + 后台任务
156
+ ├── plugins/ # 插件系统
157
+ ├── skills/ # 斜杠命令 + Skills
158
+ ├── services/ # 认证 / 成本 / LSP
159
+ └── utils/ # 工具函数
160
+ ```
161
+
162
+ ## 开发
163
+
164
+ ```bash
165
+ # 运行测试
166
+ pytest tests/ -v
167
+
168
+ # Lint 与类型检查
169
+ ruff check src/ tests/
170
+ mypy src/claude_code
171
+ ```
172
+
173
+ 当前 141 个测试覆盖消息模型、上下文压缩、对话循环与重试、工具(文件/编码/搜索)、权限规则、provider 消息转换、会话恢复、hooks 等。
174
+
175
+ ## 技术栈
176
+
177
+ - Python 3.11+
178
+ - [click](https://click.palletsprojects.com/) — CLI
179
+ - [prompt_toolkit](https://python-prompt-toolkit.readthedocs.io/) — 交互式输入
180
+ - [rich](https://rich.readthedocs.io/) — 终端渲染
181
+ - [pydantic](https://docs.pydantic.dev/) v2 — 数据校验
182
+ - [anthropic](https://github.com/anthropics/anthropic-sdk-python) / [openai](https://github.com/openai/openai-python) — LLM SDK
183
+ - [httpx](https://www.python-httpx.org/) — HTTP 客户端
184
+
185
+ ## License
186
+
187
+ MIT
@@ -0,0 +1,34 @@
1
+ """Entry point: python main.py"""
2
+
3
+ import sys
4
+ import os
5
+
6
+ # 确保 src 在 Python 路径中
7
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
8
+
9
+
10
+ def _load_dotenv() -> None:
11
+ """Load .env file from project root into os.environ."""
12
+ env_path = os.path.join(os.path.dirname(__file__), ".env")
13
+ if not os.path.exists(env_path):
14
+ return
15
+ with open(env_path, encoding="utf-8") as f:
16
+ for line in f:
17
+ line = line.strip()
18
+ if not line or line.startswith("#"):
19
+ continue
20
+ if "=" in line:
21
+ key, _, value = line.partition("=")
22
+ key = key.strip()
23
+ value = value.strip().strip("'\"")
24
+ # Don't override existing env vars
25
+ if key and key not in os.environ:
26
+ os.environ[key] = value
27
+
28
+
29
+ _load_dotenv()
30
+
31
+ from claude_code.cli import main
32
+
33
+ if __name__ == "__main__":
34
+ main()
@@ -0,0 +1,95 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "aka_claude"
7
+ version = "0.1.2"
8
+ description = "Python implementation of Claude Code - an agentic coding assistant in your terminal"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.11"
12
+ authors = [
13
+ { name = "Claude Code Py" },
14
+ ]
15
+ keywords = ["claude", "ai", "coding", "assistant", "cli", "terminal"]
16
+ classifiers = [
17
+ "Development Status :: 3 - Alpha",
18
+ "Environment :: Console",
19
+ "Intended Audience :: Developers",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Programming Language :: Python :: 3",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Programming Language :: Python :: 3.13",
25
+ "Topic :: Software Development",
26
+ "Topic :: Software Development :: Code Generators",
27
+ ]
28
+ dependencies = [
29
+ "anthropic>=0.40.0",
30
+ "openai>=1.50.0",
31
+ "click>=8.1.0",
32
+ "prompt-toolkit>=3.0.0",
33
+ "rich>=13.0.0",
34
+ "pydantic>=2.0.0",
35
+ "httpx>=0.27.0",
36
+ "pygments>=2.17.0",
37
+ "chardet>=5.0.0",
38
+ "aiofiles>=24.0.0",
39
+ ]
40
+
41
+ [project.optional-dependencies]
42
+ aws = ["boto3>=1.35.0"]
43
+ gcp = ["google-cloud-aiplatform>=1.70.0"]
44
+ azure = ["azure-ai-inference>=1.0.0b9"]
45
+ mcp = ["mcp>=1.0.0"]
46
+ all = [
47
+ "boto3>=1.35.0",
48
+ "google-cloud-aiplatform>=1.70.0",
49
+ "azure-ai-inference>=1.0.0b9",
50
+ "mcp>=1.0.0",
51
+ ]
52
+ dev = [
53
+ "pytest>=8.0.0",
54
+ "pytest-asyncio>=0.24.0",
55
+ "pytest-cov>=5.0.0",
56
+ "ruff>=0.5.0",
57
+ "mypy>=1.10.0",
58
+ ]
59
+
60
+ [project.scripts]
61
+ aka = "claude_code.cli:main"
62
+
63
+ [project.urls]
64
+ Homepage = "https://github.com/anthropics/claude-code"
65
+ Documentation = "https://code.claude.com/docs"
66
+
67
+ [tool.hatch.build.targets.wheel]
68
+ packages = ["src/claude_code"]
69
+
70
+ [tool.hatch.build.targets.sdist]
71
+ # Whitelist source-dist contents so local dirs (env/, dist/, caches) are
72
+ # never packaged.
73
+ include = [
74
+ "/src",
75
+ "/main.py",
76
+ "/scripts",
77
+ "/README.md",
78
+ "/LICENSE",
79
+ "/pyproject.toml",
80
+ ]
81
+
82
+ [tool.pytest.ini_options]
83
+ testpaths = ["tests"]
84
+ asyncio_mode = "auto"
85
+
86
+ [tool.ruff]
87
+ target-version = "py311"
88
+ line-length = 100
89
+
90
+ [tool.ruff.lint]
91
+ select = ["E", "F", "W", "I", "N", "UP"]
92
+
93
+ [tool.mypy]
94
+ python_version = "3.11"
95
+ strict = true