Skip to content

Commit c4d9217

Browse files
feat: 完成大部分操作
1 parent 3d4cb09 commit c4d9217

22 files changed

Lines changed: 561 additions & 98 deletions

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Claude Code (Reverse-Engineered)
2+
3+
Anthropic 官方 [Claude Code](https://docs.anthropic.com/en/docs/claude-code) CLI 工具的源码反编译/逆向还原项目。目标是将 Claude Code 核心功能跑通,必要时删减次级能力。
4+
5+
## 核心能力
6+
7+
- API 通信(Anthropic SDK / Bedrock / Vertex)
8+
- Bash / FileRead / FileWrite / FileEdit 等核心工具
9+
- REPL 交互界面(ink 终端渲染)
10+
- 对话历史与会话管理
11+
- 权限系统
12+
- Agent / 子代理系统
13+
14+
## 已删减模块
15+
16+
| 模块 | 处理方式 |
17+
|------|----------|
18+
| Computer Use (`@ant/computer-use-*`) | stub |
19+
| Claude for Chrome MCP | stub |
20+
| Magic Docs / Voice Mode / LSP Server | 移除 |
21+
| Analytics / GrowthBook / Sentry | 空实现 |
22+
| Plugins / Marketplace / Desktop Upsell | 移除 |
23+
| Ultraplan / Tungsten / Auto Dream | 移除 |
24+
| MCP OAuth/IDP | 简化 |
25+
| DAEMON / BRIDGE / BG_SESSIONS / TEMPLATES 等 | feature flag 关闭 |
26+
27+
## 快速开始
28+
29+
### 环境要求
30+
31+
- [Bun](https://bun.sh/) >= 1.0
32+
- Node.js >= 18(部分依赖需要)
33+
- 有效的 Anthropic API Key(或 Bedrock / Vertex 凭据)
34+
35+
### 安装
36+
37+
```bash
38+
bun install
39+
```
40+
41+
### 运行
42+
43+
```bash
44+
# 开发模式(watch)
45+
bun run dev
46+
47+
# 直接运行
48+
bun run src/entrypoints/cli.tsx
49+
50+
# 管道模式(-p)
51+
echo "say hello" | bun run src/entrypoints/cli.tsx -p
52+
53+
# 构建
54+
bun run build
55+
```
56+
57+
构建产物输出到 `dist/cli.js`~25 MB,5300+ 模块)。
58+
59+
## 项目结构
60+
61+
```
62+
claude-code/
63+
├── src/
64+
│ ├── entrypoints/
65+
│ │ ├── cli.tsx # 入口文件(含 MACRO/feature polyfill)
66+
│ │ └── sdk/ # SDK 子模块 stub
67+
│ ├── main.tsx # 主 CLI 逻辑(Commander 定义)
68+
│ └── types/
69+
│ ├── global.d.ts # 全局变量/宏声明
70+
│ └── internal-modules.d.ts # 内部 npm 包类型声明
71+
├── packages/ # Monorepo workspace 包
72+
│ ├── color-diff-napi/ # 完整实现(终端 color diff)
73+
│ ├── modifiers-napi/ # stub(macOS 修饰键检测)
74+
│ ├── audio-capture-napi/ # stub
75+
│ ├── image-processor-napi/# stub
76+
│ ├── url-handler-napi/ # stub
77+
│ └── @ant/ # Anthropic 内部包 stub
78+
│ ├── claude-for-chrome-mcp/
79+
│ ├── computer-use-mcp/
80+
│ ├── computer-use-input/
81+
│ └── computer-use-swift/
82+
├── scripts/ # 自动化 stub 生成脚本
83+
├── dist/ # 构建输出
84+
└── package.json # Bun workspaces monorepo 配置
85+
```
86+
87+
## 技术说明
88+
89+
### 运行时 Polyfill
90+
91+
入口文件 `src/entrypoints/cli.tsx` 顶部注入了必要的 polyfill:
92+
93+
- `feature()` — 所有 feature flag 返回 `false`,跳过未实现分支
94+
- `globalThis.MACRO` — 模拟构建时宏注入(VERSION 等)
95+
96+
### 类型状态
97+
98+
仍有 ~1341 个 tsc 错误,均为反编译产生的源码级类型问题(`unknown` / `never` / `{}`),**不影响 Bun 运行时**
99+
100+
### Monorepo
101+
102+
项目采用 Bun workspaces 管理内部包。原先手工放在 `node_modules/` 下的 stub 已统一迁入 `packages/`,通过 `workspace:*` 解析。
103+
104+
## 许可证
105+
106+
本项目仅供学习研究用途。Claude Code 的所有权利归 [Anthropic](https://www.anthropic.com/) 所有。

RECORD.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,57 @@ const feature = (_name: string) => false; // 所有 feature flag 分支被跳
103103
| `scripts/create-type-stubs.mjs` | 自动 stub 生成脚本 |
104104
| `scripts/fix-default-stubs.mjs` | 修复默认导出 stub |
105105
| `scripts/fix-missing-exports.mjs` | 补全缺失导出 |
106+
107+
---
108+
109+
## 五、Monorepo 改造(2026-03-31)
110+
111+
### 5.1 背景
112+
113+
`color-diff-napi` 原先是手工放在 `node_modules/` 下的 stub 文件,导出的是普通对象而非 class,导致 `new ColorDiff(...)` 报错:
114+
```
115+
ERROR Object is not a constructor (evaluating 'new ColorDiff(patch, firstLine, filePath, fileContent)')
116+
```
117+
同时 `@ant/*`、其他 `*-napi` 包也只有 `declare module` 类型声明,无运行时实现。
118+
119+
### 5.2 方案
120+
121+
将项目改造为 **Bun workspaces monorepo**,所有内部包统一放在 `packages/` 下,通过 `workspace:*` 依赖解析。
122+
123+
### 5.3 创建的 workspace 包
124+
125+
| 包名 | 路径 | 类型 |
126+
|------|------|------|
127+
| `color-diff-napi` | `packages/color-diff-napi/` | 完整实现(~1000行 TS,从 `src/native-ts/color-diff/` 移入) |
128+
| `modifiers-napi` | `packages/modifiers-napi/` | stub(macOS 修饰键检测) |
129+
| `audio-capture-napi` | `packages/audio-capture-napi/` | stub |
130+
| `image-processor-napi` | `packages/image-processor-napi/` | stub |
131+
| `url-handler-napi` | `packages/url-handler-napi/` | stub |
132+
| `@ant/claude-for-chrome-mcp` | `packages/@ant/claude-for-chrome-mcp/` | stub |
133+
| `@ant/computer-use-mcp` | `packages/@ant/computer-use-mcp/` | stub(含 subpath exports: sentinelApps, types) |
134+
| `@ant/computer-use-input` | `packages/@ant/computer-use-input/` | stub |
135+
| `@ant/computer-use-swift` | `packages/@ant/computer-use-swift/` | stub |
136+
137+
### 5.4 新增的 npm 依赖
138+
139+
| 包名 | 原因 |
140+
|------|------|
141+
| `@opentelemetry/semantic-conventions` | 构建报错缺失 |
142+
| `fflate` | `src/utils/dxt/zip.ts` 动态 import |
143+
| `vscode-jsonrpc` | `src/services/lsp/LSPClient.ts` import |
144+
| `@aws-sdk/credential-provider-node` | `src/utils/proxy.ts` 动态 import |
145+
146+
### 5.5 关键变更
147+
148+
- `package.json`:添加 `workspaces`,添加所有 workspace 包和缺失 npm 依赖
149+
- `src/types/internal-modules.d.ts`:删除已移入 monorepo 的 `declare module` 块,仅保留 `bun:bundle``bun:ffi``@anthropic-ai/mcpb`
150+
- `src/native-ts/color-diff/``packages/color-diff-napi/src/`:移动并内联了对 `stringWidth``logError` 的依赖
151+
- 删除 `node_modules/color-diff-napi/` 手工 stub
152+
153+
### 5.6 构建验证
154+
155+
```
156+
$ bun run build
157+
Bundled 5326 modules in 491ms
158+
cli.js 25.74 MB (entry point)
159+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"scripts": {
1111
"build": "bun build src/entrypoints/cli.tsx --outdir dist --target bun",
12-
"dev": "bun run --watch src/entrypoints/cli.tsx"
12+
"dev": "bun run src/entrypoints/cli.tsx"
1313
},
1414
"dependencies": {
1515
"@alcalzone/ansi-tokenize": "^0.3.0",
Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,44 @@
1-
export class ComputerUseInput {}
2-
export class ComputerUseInputAPI {}
1+
interface FrontmostAppInfo {
2+
bundleId: string
3+
appName: string
4+
}
5+
6+
export class ComputerUseInputAPI {
7+
declare moveMouse: (
8+
x: number,
9+
y: number,
10+
animated: boolean,
11+
) => Promise<void>
12+
13+
declare key: (
14+
key: string,
15+
action: 'press' | 'release',
16+
) => Promise<void>
17+
18+
declare keys: (parts: string[]) => Promise<void>
19+
20+
declare mouseLocation: () => Promise<{ x: number; y: number }>
21+
22+
declare mouseButton: (
23+
button: 'left' | 'right' | 'middle',
24+
action: 'click' | 'press' | 'release',
25+
count?: number,
26+
) => Promise<void>
27+
28+
declare mouseScroll: (
29+
amount: number,
30+
direction: 'vertical' | 'horizontal',
31+
) => Promise<void>
32+
33+
declare typeText: (text: string) => Promise<void>
34+
35+
declare getFrontmostAppInfo: () => FrontmostAppInfo | null
36+
37+
declare isSupported: true
38+
}
39+
40+
interface ComputerUseInputUnsupported {
41+
isSupported: false
42+
}
43+
44+
export type ComputerUseInput = ComputerUseInputAPI | ComputerUseInputUnsupported
Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,112 @@
1-
export class ComputerUseAPI {}
1+
interface DisplayGeometry {
2+
width: number
3+
height: number
4+
scaleFactor: number
5+
displayId: number
6+
}
7+
8+
interface PrepareDisplayResult {
9+
activated: string
10+
hidden: string[]
11+
}
12+
13+
interface AppInfo {
14+
bundleId: string
15+
displayName: string
16+
}
17+
18+
interface InstalledApp {
19+
bundleId: string
20+
displayName: string
21+
path: string
22+
iconDataUrl?: string
23+
}
24+
25+
interface RunningApp {
26+
bundleId: string
27+
displayName: string
28+
}
29+
30+
interface ScreenshotResult {
31+
base64: string
32+
width: number
33+
height: number
34+
}
35+
36+
interface ResolvePrepareCaptureResult {
37+
base64: string
38+
width: number
39+
height: number
40+
}
41+
42+
interface WindowDisplayInfo {
43+
bundleId: string
44+
displayIds: number[]
45+
}
46+
47+
interface AppsAPI {
48+
prepareDisplay(
49+
allowlistBundleIds: string[],
50+
surrogateHost: string,
51+
displayId?: number,
52+
): Promise<PrepareDisplayResult>
53+
previewHideSet(
54+
bundleIds: string[],
55+
displayId?: number,
56+
): Promise<Array<AppInfo>>
57+
findWindowDisplays(
58+
bundleIds: string[],
59+
): Promise<Array<WindowDisplayInfo>>
60+
appUnderPoint(
61+
x: number,
62+
y: number,
63+
): Promise<AppInfo | null>
64+
listInstalled(): Promise<InstalledApp[]>
65+
iconDataUrl(path: string): string | null
66+
listRunning(): RunningApp[]
67+
open(bundleId: string): Promise<void>
68+
unhide(bundleIds: string[]): Promise<void>
69+
}
70+
71+
interface DisplayAPI {
72+
getSize(displayId?: number): DisplayGeometry
73+
listAll(): DisplayGeometry[]
74+
}
75+
76+
interface ScreenshotAPI {
77+
captureExcluding(
78+
allowedBundleIds: string[],
79+
quality: number,
80+
targetW: number,
81+
targetH: number,
82+
displayId?: number,
83+
): Promise<ScreenshotResult>
84+
captureRegion(
85+
allowedBundleIds: string[],
86+
x: number,
87+
y: number,
88+
w: number,
89+
h: number,
90+
outW: number,
91+
outH: number,
92+
quality: number,
93+
displayId?: number,
94+
): Promise<ScreenshotResult>
95+
}
96+
97+
export class ComputerUseAPI {
98+
declare apps: AppsAPI
99+
declare display: DisplayAPI
100+
declare screenshot: ScreenshotAPI
101+
102+
declare resolvePrepareCapture: (
103+
allowedBundleIds: string[],
104+
surrogateHost: string,
105+
quality: number,
106+
targetW: number,
107+
targetH: number,
108+
displayId?: number,
109+
autoResolve?: boolean,
110+
doHide?: boolean,
111+
) => Promise<ResolvePrepareCaptureResult>
112+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
// Auto-generated stub — replace with real implementation
2-
export {};
2+
export function useFrustrationDetection(
3+
_messages: unknown[],
4+
_isLoading: boolean,
5+
_hasActivePrompt: boolean,
6+
_otherSurveyOpen: boolean,
7+
): { state: 'closed' | 'open'; handleTranscriptSelect: () => void } {
8+
return { state: 'closed', handleTranscriptSelect: () => {} };
9+
}

0 commit comments

Comments
 (0)