chatccc 0.2.252 → 0.2.254
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.
- package/README.md +10 -1
- package/bin/cccagent.mjs +12 -3
- package/bin/chatccc.mjs +15 -6
- package/config.sample.json +2 -1
- package/deepccc-agent/README.md +40 -35
- package/deepccc-agent/os-prompts/darwin.md +8 -8
- package/deepccc-agent/os-prompts/linux.md +8 -8
- package/deepccc-agent/os-prompts/win32.md +11 -11
- package/deepccc-agent/package.json +65 -65
- package/dist/deepccc-agent/src/cli.js +634 -0
- package/dist/deepccc-agent/src/config.js +81 -0
- package/dist/deepccc-agent/src/context.js +348 -0
- package/dist/deepccc-agent/src/file-log.js +34 -0
- package/dist/deepccc-agent/src/file-tools.js +1208 -0
- package/dist/deepccc-agent/src/index.js +601 -0
- package/dist/deepccc-agent/src/permissions.js +170 -0
- package/dist/deepccc-agent/src/privacy.js +124 -0
- package/dist/deepccc-agent/src/proc-tree-kill.js +60 -0
- package/dist/deepccc-agent/src/progress/cards-helpers.js +70 -0
- package/dist/deepccc-agent/src/progress/reducer.js +102 -0
- package/dist/deepccc-agent/src/progress/terminal-renderer.js +264 -0
- package/dist/deepccc-agent/src/progress/view.js +30 -0
- package/dist/deepccc-agent/src/raw-stream-log.js +106 -0
- package/dist/deepccc-agent/src/session-search.js +276 -0
- package/dist/deepccc-agent/src/session-select.js +23 -0
- package/dist/deepccc-agent/src/sigint.js +26 -0
- package/dist/deepccc-agent/src/skills.js +178 -0
- package/dist/deepccc-agent/src/web-tools.js +246 -0
- package/dist/src/adapters/adapter-interface.js +19 -0
- package/dist/src/adapters/ccc-adapter.js +112 -0
- package/dist/src/adapters/claude-adapter.js +497 -0
- package/dist/src/adapters/claude-session-meta-store.js +92 -0
- package/dist/src/adapters/codex-adapter.js +279 -0
- package/dist/src/adapters/codex-session-meta-store.js +94 -0
- package/dist/src/adapters/cursor-adapter.js +491 -0
- package/dist/src/adapters/cursor-session-meta-store.js +116 -0
- package/dist/src/adapters/jsonl-stream.js +104 -0
- package/{src/adapters/proc-tree-kill.ts → dist/src/adapters/proc-tree-kill.js} +94 -97
- package/dist/src/adapters/raw-stream-log.js +106 -0
- package/dist/src/adapters/resource-monitor.js +113 -0
- package/dist/src/agent-activity.js +133 -0
- package/dist/src/agent-delegate-task-rpc.js +129 -0
- package/dist/src/agent-delegate-task.js +48 -0
- package/dist/src/agent-file-rpc.js +152 -0
- package/dist/src/agent-image-rpc.js +148 -0
- package/dist/src/agent-platform-routing.js +13 -0
- package/dist/src/agent-reload-config-rpc.js +23 -0
- package/dist/src/agent-rpc-body.js +87 -0
- package/dist/src/agent-stop-stuck.js +110 -0
- package/dist/src/agent-team/application/board-service.js +142 -0
- package/dist/src/agent-team/application/main-agent-service.js +151 -0
- package/dist/src/agent-team/domain/board.js +69 -0
- package/dist/src/agent-team/http/board-routes.js +213 -0
- package/dist/src/agent-team/infrastructure/filesystem-browser.js +112 -0
- package/dist/src/agent-team/infrastructure/main-agent-session-runtime.js +30 -0
- package/dist/src/agent-team/main-agent-bootstrap.js +15 -0
- package/dist/src/agent-team/repositories/board-repository.js +10 -0
- package/dist/src/agent-team/repositories/feishu-p2p-contact-store.js +64 -0
- package/dist/src/agent-team/repositories/json-board-repository.js +209 -0
- package/dist/src/agent-team/repositories/main-agent-binding-repository.js +84 -0
- package/dist/src/agent-team/sync/board-sync-adapter.js +1 -0
- package/dist/src/agent-team/web/agent-team-page.js +219 -0
- package/dist/src/agent-tool.js +10 -0
- package/dist/src/card-action-routing.js +7 -0
- package/dist/src/card-plain-text.js +101 -0
- package/dist/src/cardkit.js +158 -0
- package/dist/src/cards.js +573 -0
- package/dist/src/chatgpt-subscription-rpc.js +18 -0
- package/dist/src/chatgpt-subscription.js +199 -0
- package/dist/src/chrome-devtools-guard.js +238 -0
- package/dist/src/claude-sdk-installer.js +249 -0
- package/dist/src/codex-reset-actions.js +143 -0
- package/dist/src/config-utils.js +149 -0
- package/dist/src/config.js +808 -0
- package/dist/src/cursor-usage.js +77 -0
- package/dist/src/exit-banner.js +28 -0
- package/dist/src/feishu-api.js +1404 -0
- package/dist/src/feishu-message-ingress.js +137 -0
- package/dist/src/feishu-platform.js +97 -0
- package/dist/src/format-message.js +252 -0
- package/dist/src/git-command.js +155 -0
- package/dist/src/im-skills.js +121 -0
- package/dist/src/index.js +835 -0
- package/dist/src/litellm-proxy.js +300 -0
- package/dist/src/orchestrator.js +2096 -0
- package/dist/src/package-root.js +26 -0
- package/dist/src/platform-adapter.js +7 -0
- package/dist/src/platform-startup.js +6 -0
- package/dist/src/privacy.js +100 -0
- package/dist/src/progress/reducer.js +102 -0
- package/dist/src/progress/terminal-renderer.js +264 -0
- package/dist/src/progress/view.js +30 -0
- package/dist/src/response-stall.js +14 -0
- package/dist/src/runtime-entry.js +13 -0
- package/dist/src/runtime-reload.js +19 -0
- package/dist/src/session-chat-binding.js +183 -0
- package/dist/src/session-name.js +7 -0
- package/dist/src/session.js +2147 -0
- package/dist/src/shared-prefix.js +16 -0
- package/dist/src/shared.js +493 -0
- package/dist/src/sim-agent.js +105 -0
- package/dist/src/sim-platform.js +142 -0
- package/dist/src/sim-store.js +231 -0
- package/dist/src/simplify.js +99 -0
- package/dist/src/startup-lifecycle.js +209 -0
- package/dist/src/stream-state.js +141 -0
- package/dist/src/terminal-error.js +100 -0
- package/dist/src/trace.js +50 -0
- package/dist/src/turn-cards.js +92 -0
- package/dist/src/update-command-guard.js +114 -0
- package/{src/web-ui.ts → dist/src/web-ui.js} +753 -839
- package/dist/src/wechat-platform.js +545 -0
- package/package.json +76 -74
- package/deepccc-agent/LICENSE +0 -201
- package/deepccc-agent/bin/deepccc.mjs +0 -26
- package/deepccc-agent/docs/cache-hit-rate-1.jpg +0 -0
- package/deepccc-agent/docs/cache-hit-rate-2.jpg +0 -0
- package/deepccc-agent/package-lock.json +0 -2027
- package/deepccc-agent/src/__tests__/chat-session.test.ts +0 -877
- package/deepccc-agent/src/__tests__/cli-json.test.ts +0 -49
- package/deepccc-agent/src/__tests__/config.test.ts +0 -34
- package/deepccc-agent/src/__tests__/context.test.ts +0 -341
- package/deepccc-agent/src/__tests__/file-tools.test.ts +0 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +0 -199
- package/deepccc-agent/src/__tests__/privacy.test.ts +0 -318
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +0 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +0 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +0 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +0 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +0 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +0 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +0 -220
- package/deepccc-agent/src/cli.ts +0 -682
- package/deepccc-agent/src/config.ts +0 -101
- package/deepccc-agent/src/context.ts +0 -465
- package/deepccc-agent/src/file-log.ts +0 -38
- package/deepccc-agent/src/file-tools.ts +0 -1493
- package/deepccc-agent/src/index.ts +0 -710
- package/deepccc-agent/src/permissions.ts +0 -226
- package/deepccc-agent/src/privacy.ts +0 -141
- package/deepccc-agent/src/proc-tree-kill.ts +0 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +0 -76
- package/deepccc-agent/src/progress/reducer.ts +0 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +0 -294
- package/deepccc-agent/src/progress/view.ts +0 -77
- package/deepccc-agent/src/raw-stream-log.ts +0 -124
- package/deepccc-agent/src/session-search.ts +0 -370
- package/deepccc-agent/src/session-select.ts +0 -48
- package/deepccc-agent/src/sigint.ts +0 -50
- package/deepccc-agent/src/skills.ts +0 -205
- package/deepccc-agent/src/web-tools.ts +0 -313
- package/deepccc-agent/tsconfig.build.json +0 -13
- package/deepccc-agent/tsconfig.json +0 -13
- package/deepccc-agent/vitest.config.ts +0 -7
- package/src/__tests__/adapter-interface.test.ts +0 -152
- package/src/__tests__/agent-activity.test.ts +0 -86
- package/src/__tests__/agent-delegate-task-rpc.test.ts +0 -165
- package/src/__tests__/agent-image-rpc.test.ts +0 -34
- package/src/__tests__/agent-platform-routing.test.ts +0 -26
- package/src/__tests__/agent-reload-config-rpc.test.ts +0 -99
- package/src/__tests__/agent-rpc-body.test.ts +0 -42
- package/src/__tests__/builtin-chat-session.test.ts +0 -532
- package/src/__tests__/builtin-cli-json.test.ts +0 -39
- package/src/__tests__/builtin-config.test.ts +0 -26
- package/src/__tests__/builtin-context.test.ts +0 -319
- package/src/__tests__/builtin-file-tools.test.ts +0 -240
- package/src/__tests__/builtin-permissions.test.ts +0 -219
- package/src/__tests__/builtin-session-search.test.ts +0 -262
- package/src/__tests__/builtin-session-select.test.ts +0 -116
- package/src/__tests__/builtin-sigint.test.ts +0 -56
- package/src/__tests__/builtin-skills.test.ts +0 -284
- package/src/__tests__/builtin-web-tools.test.ts +0 -220
- package/src/__tests__/card-action-routing.test.ts +0 -18
- package/src/__tests__/card-plain-text.test.ts +0 -45
- package/src/__tests__/cardkit.test.ts +0 -60
- package/src/__tests__/cards.test.ts +0 -607
- package/src/__tests__/ccc-adapter.test.ts +0 -194
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +0 -89
- package/src/__tests__/chatgpt-subscription.test.ts +0 -135
- package/src/__tests__/chrome-devtools-guard.test.ts +0 -165
- package/src/__tests__/claude-adapter.test.ts +0 -614
- package/src/__tests__/claude-raw-stream-log.test.ts +0 -96
- package/src/__tests__/claude-sdk-installer.test.ts +0 -285
- package/src/__tests__/codex-adapter.test.ts +0 -331
- package/src/__tests__/codex-raw-stream-log.test.ts +0 -170
- package/src/__tests__/codex-reset-actions.test.ts +0 -146
- package/src/__tests__/config-reload.test.ts +0 -284
- package/src/__tests__/config-sample.test.ts +0 -97
- package/src/__tests__/config-utils.test.ts +0 -40
- package/src/__tests__/config.test.ts +0 -395
- package/src/__tests__/crash-logging.test.ts +0 -360
- package/src/__tests__/cursor-adapter.test.ts +0 -890
- package/src/__tests__/cursor-session-meta-store.test.ts +0 -212
- package/src/__tests__/feishu-api.test.ts +0 -60
- package/src/__tests__/feishu-avatar.test.ts +0 -504
- package/src/__tests__/feishu-message-ingress.test.ts +0 -138
- package/src/__tests__/feishu-platform.test.ts +0 -75
- package/src/__tests__/fixtures/codex_simple_text.jsonl +0 -4
- package/src/__tests__/fixtures/codex_with_tool.jsonl +0 -6
- package/src/__tests__/fixtures/cursor_partial_only.jsonl +0 -5
- package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +0 -13
- package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +0 -12
- package/src/__tests__/format-message.test.ts +0 -316
- package/src/__tests__/git-command.test.ts +0 -288
- package/src/__tests__/im-skills.test.ts +0 -125
- package/src/__tests__/jsonl-stream.test.ts +0 -79
- package/src/__tests__/orchestrator.test.ts +0 -1268
- package/src/__tests__/package-files.test.ts +0 -24
- package/src/__tests__/platform-startup.test.ts +0 -19
- package/src/__tests__/privacy.test.ts +0 -198
- package/src/__tests__/proc-tree-kill.test.ts +0 -108
- package/src/__tests__/progress-reducer.test.ts +0 -121
- package/src/__tests__/raw-stream-log.test.ts +0 -106
- package/src/__tests__/response-stall.test.ts +0 -49
- package/src/__tests__/restart.test.ts +0 -232
- package/src/__tests__/session-ccc-config.test.ts +0 -66
- package/src/__tests__/session.test.ts +0 -3004
- package/src/__tests__/shared-prefix.test.ts +0 -36
- package/src/__tests__/sim-agent.test.ts +0 -174
- package/src/__tests__/sim-platform.test.ts +0 -93
- package/src/__tests__/sim-store.test.ts +0 -214
- package/src/__tests__/simplify.test.ts +0 -283
- package/src/__tests__/startup-lifecycle.test.ts +0 -231
- package/src/__tests__/stop-session.test.ts +0 -162
- package/src/__tests__/stream-state.test.ts +0 -164
- package/src/__tests__/terminal-error.test.ts +0 -54
- package/src/__tests__/terminal-renderer.test.ts +0 -247
- package/src/__tests__/update-command-guard.test.ts +0 -144
- package/src/__tests__/web-ui.test.ts +0 -438
- package/src/__tests__/wechat-platform.test.ts +0 -111
- package/src/adapters/adapter-interface.ts +0 -217
- package/src/adapters/ccc-adapter.ts +0 -150
- package/src/adapters/claude-adapter.ts +0 -673
- package/src/adapters/claude-session-meta-store.ts +0 -120
- package/src/adapters/codex-adapter.ts +0 -426
- package/src/adapters/codex-session-meta-store.ts +0 -131
- package/src/adapters/cursor-adapter.ts +0 -681
- package/src/adapters/cursor-session-meta-store.ts +0 -154
- package/src/adapters/jsonl-stream.ts +0 -157
- package/src/adapters/raw-stream-log.ts +0 -124
- package/src/adapters/resource-monitor.ts +0 -141
- package/src/agent-activity.ts +0 -175
- package/src/agent-delegate-task-rpc.ts +0 -153
- package/src/agent-delegate-task.ts +0 -91
- package/src/agent-file-rpc.ts +0 -172
- package/src/agent-image-rpc.ts +0 -168
- package/src/agent-platform-routing.ts +0 -28
- package/src/agent-reload-config-rpc.ts +0 -34
- package/src/agent-rpc-body.ts +0 -92
- package/src/agent-stop-stuck.ts +0 -129
- package/src/card-action-routing.ts +0 -14
- package/src/card-plain-text.ts +0 -108
- package/src/cardkit.ts +0 -179
- package/src/cards.ts +0 -684
- package/src/chatgpt-subscription-rpc.ts +0 -27
- package/src/chatgpt-subscription.ts +0 -299
- package/src/chrome-devtools-guard.ts +0 -318
- package/src/claude-sdk-installer.ts +0 -324
- package/src/codex-reset-actions.ts +0 -184
- package/src/config-utils.ts +0 -211
- package/src/config.ts +0 -1063
- package/src/cursor-usage.ts +0 -128
- package/src/exit-banner.ts +0 -33
- package/src/feishu-api.ts +0 -1616
- package/src/feishu-message-ingress.ts +0 -195
- package/src/feishu-platform.ts +0 -159
- package/src/format-message.ts +0 -293
- package/src/git-command.ts +0 -202
- package/src/im-skills.ts +0 -149
- package/src/index.ts +0 -1089
- package/src/litellm-proxy.ts +0 -374
- package/src/orchestrator.ts +0 -2543
- package/src/platform-adapter.ts +0 -70
- package/src/platform-startup.ts +0 -16
- package/src/privacy.ts +0 -118
- package/src/progress/reducer.ts +0 -113
- package/src/progress/terminal-renderer.ts +0 -294
- package/src/progress/view.ts +0 -77
- package/src/response-stall.ts +0 -28
- package/src/runtime-reload.ts +0 -34
- package/src/session-chat-binding.ts +0 -292
- package/src/session-name.ts +0 -8
- package/src/session.ts +0 -2659
- package/src/shared-prefix.ts +0 -29
- package/src/shared.ts +0 -552
- package/src/sim-agent.ts +0 -167
- package/src/sim-platform.ts +0 -177
- package/src/sim-store.ts +0 -317
- package/src/simplify.ts +0 -120
- package/src/startup-lifecycle.ts +0 -250
- package/src/stream-state.ts +0 -177
- package/src/terminal-error.ts +0 -129
- package/src/trace.ts +0 -51
- package/src/turn-cards.ts +0 -118
- package/src/update-command-guard.ts +0 -165
- package/src/wechat-platform.ts +0 -680
|
@@ -1,324 +0,0 @@
|
|
|
1
|
-
// =============================================================================
|
|
2
|
-
// claude-sdk-installer.ts — Claude Code 引擎(Agent SDK)按需安装器
|
|
3
|
-
// =============================================================================
|
|
4
|
-
// 背景:@anthropic-ai/claude-agent-sdk 通过 optionalDependencies 内置各平台
|
|
5
|
-
// 的原生 CLI 二进制(Windows 上 claude.exe 约 215MB),若作为 chatccc 的硬依赖,
|
|
6
|
-
// 所有用户(包括不用 Claude Code 的)都要白白下载 220MB+。
|
|
7
|
-
//
|
|
8
|
-
// 方案:把 SDK 从 chatccc 的 dependencies 中移除,改为**按需安装**到
|
|
9
|
-
// `~/.chatccc/claude-sdk/`:
|
|
10
|
-
// - 用户未启用 Claude Code → 完全不下载,npm 包体积大幅下降;
|
|
11
|
-
// - 用户在设置页打开 Claude Code 开关时,点「安装引擎」触发后台安装,
|
|
12
|
-
// 带进度条(状态式:检测环境 → 下载中 → 安装中 → 完成/失败)。
|
|
13
|
-
//
|
|
14
|
-
// 设计约束:
|
|
15
|
-
// - **不 import config.ts**(config.ts 顶层有 loadConfig 副作用,web-ui.ts
|
|
16
|
-
// 依赖本模块,间接 import config.ts 会污染依赖 web-ui.ts 的单测)。
|
|
17
|
-
// - 始终使用 SDK 内置的 CLI 二进制(用户拍板档位 b),不依赖用户自装 CLI。
|
|
18
|
-
// - 安装目录可注入(dir 参数),便于单测用临时目录,不碰真实用户目录。
|
|
19
|
-
// =============================================================================
|
|
20
|
-
|
|
21
|
-
import { spawn } from "node:child_process";
|
|
22
|
-
import {
|
|
23
|
-
existsSync,
|
|
24
|
-
mkdirSync,
|
|
25
|
-
readdirSync,
|
|
26
|
-
readFileSync,
|
|
27
|
-
rmSync,
|
|
28
|
-
statSync,
|
|
29
|
-
unlinkSync,
|
|
30
|
-
writeFileSync,
|
|
31
|
-
type Dirent,
|
|
32
|
-
} from "node:fs";
|
|
33
|
-
import { homedir } from "node:os";
|
|
34
|
-
import { join } from "node:path";
|
|
35
|
-
import { pathToFileURL } from "node:url";
|
|
36
|
-
|
|
37
|
-
/** 与当前代码配套的 SDK 版本(package.json 移除依赖后此常量成为唯一版本来源) */
|
|
38
|
-
export const CLAUDE_SDK_VERSION = "0.2.133";
|
|
39
|
-
|
|
40
|
-
/** 按需安装目录:~/.chatccc/claude-sdk */
|
|
41
|
-
export const CLAUDE_SDK_DIR = join(homedir(), ".chatccc", "claude-sdk");
|
|
42
|
-
|
|
43
|
-
const SDK_PKG_REL = join("node_modules", "@anthropic-ai", "claude-agent-sdk", "package.json");
|
|
44
|
-
const SDK_ENTRY_REL = join("node_modules", "@anthropic-ai", "claude-agent-sdk", "sdk.mjs");
|
|
45
|
-
const LOCK_FILE = ".installing";
|
|
46
|
-
|
|
47
|
-
/** 估算的完整安装体积(含内置 CLI 二进制),用于进度条展示 */
|
|
48
|
-
export const CLAUDE_SDK_EXPECTED_BYTES = 240 * 1024 * 1024;
|
|
49
|
-
|
|
50
|
-
export type SdkInstallPhase =
|
|
51
|
-
| "idle"
|
|
52
|
-
| "detecting"
|
|
53
|
-
| "downloading"
|
|
54
|
-
| "installing"
|
|
55
|
-
| "done"
|
|
56
|
-
| "error";
|
|
57
|
-
|
|
58
|
-
export interface SdkInstallProgress {
|
|
59
|
-
phase: SdkInstallPhase;
|
|
60
|
-
/** 0-100 */
|
|
61
|
-
percent: number;
|
|
62
|
-
message: string;
|
|
63
|
-
error?: string;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export interface SdkInstallOptions {
|
|
67
|
-
/** 安装目录(测试注入临时目录) */
|
|
68
|
-
dir?: string;
|
|
69
|
-
/** npm 命令(测试注入 fake) */
|
|
70
|
-
npmCommand?: string;
|
|
71
|
-
/** 期望版本(默认 CLAUDE_SDK_VERSION) */
|
|
72
|
-
expectedVersion?: string;
|
|
73
|
-
/** 进度回调 */
|
|
74
|
-
onProgress?: (p: SdkInstallProgress) => void;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// ---------------------------------------------------------------------------
|
|
78
|
-
// 状态查询
|
|
79
|
-
// ---------------------------------------------------------------------------
|
|
80
|
-
|
|
81
|
-
/** SDK 入口文件绝对路径(供动态 import) */
|
|
82
|
-
export function getClaudeSdkEntryPath(dir: string = CLAUDE_SDK_DIR): string {
|
|
83
|
-
return join(dir, SDK_ENTRY_REL);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** 是否已安装(node_modules 里存在 SDK package.json 即视为已装) */
|
|
87
|
-
export function isClaudeSdkInstalled(dir: string = CLAUDE_SDK_DIR): boolean {
|
|
88
|
-
return existsSync(join(dir, SDK_PKG_REL));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** 已安装版本号;未安装/解析失败返回 null */
|
|
92
|
-
export function getClaudeSdkInstalledVersion(dir: string = CLAUDE_SDK_DIR): string | null {
|
|
93
|
-
try {
|
|
94
|
-
const raw = readFileSync(join(dir, SDK_PKG_REL), "utf8");
|
|
95
|
-
const pkg = JSON.parse(raw) as { version?: unknown };
|
|
96
|
-
return typeof pkg.version === "string" ? pkg.version : null;
|
|
97
|
-
} catch {
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/** 是否正在安装(锁文件存在且 pid 存活);pid 已死视为过期锁并自动清理 */
|
|
103
|
-
export function isClaudeSdkInstalling(dir: string = CLAUDE_SDK_DIR): boolean {
|
|
104
|
-
const lockPath = join(dir, LOCK_FILE);
|
|
105
|
-
if (!existsSync(lockPath)) return false;
|
|
106
|
-
try {
|
|
107
|
-
const pid = Number.parseInt(readFileSync(lockPath, "utf8").trim(), 10);
|
|
108
|
-
if (Number.isFinite(pid) && pid > 0 && isPidAlive(pid)) return true;
|
|
109
|
-
} catch {
|
|
110
|
-
// 锁文件损坏 → 按过期处理,走清理
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
unlinkSync(lockPath);
|
|
114
|
-
} catch {
|
|
115
|
-
// 清理失败不阻塞判断
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function isPidAlive(pid: number): boolean {
|
|
121
|
-
try {
|
|
122
|
-
process.kill(pid, 0);
|
|
123
|
-
return true;
|
|
124
|
-
} catch (err) {
|
|
125
|
-
return (err as NodeJS.ErrnoException).code === "EPERM";
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// ---------------------------------------------------------------------------
|
|
130
|
-
// 安装(后台任务 + 进度)
|
|
131
|
-
// ---------------------------------------------------------------------------
|
|
132
|
-
|
|
133
|
-
/** 最近一次安装的进度快照,供 web-ui 轮询 */
|
|
134
|
-
let lastProgress: SdkInstallProgress = { phase: "idle", percent: 0, message: "" };
|
|
135
|
-
let installInProgress = false;
|
|
136
|
-
|
|
137
|
-
export function getLastInstallProgress(): SdkInstallProgress {
|
|
138
|
-
return { ...lastProgress, ...(lastProgress.error ? { error: lastProgress.error } : {}) };
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export function isInstallRunning(): boolean {
|
|
142
|
-
return installInProgress;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function setProgress(p: SdkInstallProgress, onProgress?: (p: SdkInstallProgress) => void): void {
|
|
146
|
-
lastProgress = { ...p };
|
|
147
|
-
onProgress?.(lastProgress);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 启动按需安装。返回 Promise,安装完成 resolve、失败 reject。
|
|
152
|
-
* 调用方通常不 await(后台执行),通过 onProgress / getLastInstallProgress() 获取进度。
|
|
153
|
-
*/
|
|
154
|
-
export async function installClaudeSdk(options: SdkInstallOptions = {}): Promise<void> {
|
|
155
|
-
const dir = options.dir ?? CLAUDE_SDK_DIR;
|
|
156
|
-
const npmCommand = options.npmCommand ?? "npm";
|
|
157
|
-
const expected = options.expectedVersion ?? CLAUDE_SDK_VERSION;
|
|
158
|
-
const onProgress = options.onProgress;
|
|
159
|
-
|
|
160
|
-
if (installInProgress) {
|
|
161
|
-
throw new Error("Claude Code 引擎正在安装中,请等待完成后再试。");
|
|
162
|
-
}
|
|
163
|
-
if (isClaudeSdkInstalling(dir)) {
|
|
164
|
-
throw new Error("检测到另一个安装任务正在进行(锁文件存在),请稍后再试。");
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const installedVersion = isClaudeSdkInstalled(dir) ? getClaudeSdkInstalledVersion(dir) : null;
|
|
168
|
-
if (installedVersion === expected) {
|
|
169
|
-
setProgress({ phase: "done", percent: 100, message: `已安装 v${installedVersion}` }, onProgress);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
if (installedVersion !== null && installedVersion !== expected) {
|
|
173
|
-
setProgress(
|
|
174
|
-
{
|
|
175
|
-
phase: "detecting",
|
|
176
|
-
percent: 3,
|
|
177
|
-
message: `已装 v${installedVersion} ≠ 期望 v${expected},开始重装…`,
|
|
178
|
-
},
|
|
179
|
-
onProgress,
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
installInProgress = true;
|
|
184
|
-
mkdirSync(dir, { recursive: true });
|
|
185
|
-
writeFileSync(join(dir, LOCK_FILE), String(process.pid), "utf8");
|
|
186
|
-
|
|
187
|
-
setProgress({ phase: "detecting", percent: 2, message: "检测环境…" }, onProgress);
|
|
188
|
-
|
|
189
|
-
return new Promise<void>((resolve, reject) => {
|
|
190
|
-
const child = spawn(
|
|
191
|
-
npmCommand,
|
|
192
|
-
[
|
|
193
|
-
"install",
|
|
194
|
-
"--prefix",
|
|
195
|
-
dir,
|
|
196
|
-
`@anthropic-ai/claude-agent-sdk@${expected}`,
|
|
197
|
-
"--no-audit",
|
|
198
|
-
"--no-fund",
|
|
199
|
-
"--loglevel=http",
|
|
200
|
-
],
|
|
201
|
-
{
|
|
202
|
-
stdio: ["ignore", "pipe", "pipe"],
|
|
203
|
-
windowsHide: true,
|
|
204
|
-
// Windows 下 npm 是 npm.cmd,需要 shell 才能解析
|
|
205
|
-
shell: process.platform === "win32",
|
|
206
|
-
},
|
|
207
|
-
);
|
|
208
|
-
|
|
209
|
-
let stderrBuf = "";
|
|
210
|
-
let requestCount = 0;
|
|
211
|
-
let sizeTimer: ReturnType<typeof setInterval> | null = null;
|
|
212
|
-
|
|
213
|
-
const updateBySize = (): void => {
|
|
214
|
-
const size = dirSizeBytes(dir);
|
|
215
|
-
const sizePercent = Math.min((size / CLAUDE_SDK_EXPECTED_BYTES) * 90, 90);
|
|
216
|
-
const requestPercent = Math.min(5 + requestCount * 4, 70);
|
|
217
|
-
const percent = Math.max(sizePercent, requestPercent);
|
|
218
|
-
const mb = (size / 1048576).toFixed(0);
|
|
219
|
-
setProgress(
|
|
220
|
-
{
|
|
221
|
-
phase: "downloading",
|
|
222
|
-
percent: Math.min(percent, 94),
|
|
223
|
-
message: `下载中… 已下载 ${mb} MB / 约 220 MB`,
|
|
224
|
-
},
|
|
225
|
-
onProgress,
|
|
226
|
-
);
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
child.stdout.on("data", (chunk: Buffer) => {
|
|
230
|
-
const text = chunk.toString();
|
|
231
|
-
// npm --loglevel=http 每完成一个请求输出一行 "npm http fetch GET 200 ..."
|
|
232
|
-
requestCount += (text.match(/http fetch GET|GET \d{3}/g) ?? []).length;
|
|
233
|
-
updateBySize();
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
child.stderr.on("data", (chunk: Buffer) => {
|
|
237
|
-
stderrBuf += chunk.toString();
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
child.on("error", (err) => {
|
|
241
|
-
cleanupAfterFailure(dir, err, onProgress, () => {
|
|
242
|
-
installInProgress = false;
|
|
243
|
-
reject(err);
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
child.on("close", (code) => {
|
|
248
|
-
if (sizeTimer) clearInterval(sizeTimer);
|
|
249
|
-
if (code === 0 && isClaudeSdkInstalled(dir)) {
|
|
250
|
-
const version = getClaudeSdkInstalledVersion(dir);
|
|
251
|
-
installInProgress = false;
|
|
252
|
-
try {
|
|
253
|
-
unlinkSync(join(dir, LOCK_FILE));
|
|
254
|
-
} catch {
|
|
255
|
-
// 锁文件不存在可忽略
|
|
256
|
-
}
|
|
257
|
-
setProgress(
|
|
258
|
-
{ phase: "done", percent: 100, message: `安装完成(v${version ?? expected})` },
|
|
259
|
-
onProgress,
|
|
260
|
-
);
|
|
261
|
-
resolve();
|
|
262
|
-
} else {
|
|
263
|
-
const tail = stderrBuf.trim().split("\n").slice(-5).join(" | ");
|
|
264
|
-
const detail = code !== 0 ? `npm 退出码 ${code}` : "安装后校验未通过";
|
|
265
|
-
const message = tail ? `${detail}:${tail.slice(0, 400)}` : detail;
|
|
266
|
-
cleanupAfterFailure(dir, new Error(message), onProgress, () => {
|
|
267
|
-
installInProgress = false;
|
|
268
|
-
reject(new Error(message));
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
// 定期按目录体积刷新进度(大文件下载时 stdout 事件稀疏)
|
|
274
|
-
sizeTimer = setInterval(() => {
|
|
275
|
-
if (installInProgress) updateBySize();
|
|
276
|
-
}, 400);
|
|
277
|
-
if (typeof sizeTimer.unref === "function") sizeTimer.unref();
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function cleanupAfterFailure(
|
|
282
|
-
dir: string,
|
|
283
|
-
err: Error,
|
|
284
|
-
onProgress: ((p: SdkInstallProgress) => void) | undefined,
|
|
285
|
-
after: () => void,
|
|
286
|
-
): void {
|
|
287
|
-
setProgress(
|
|
288
|
-
{ phase: "error", percent: 0, message: "安装失败", error: err.message.slice(0, 500) },
|
|
289
|
-
onProgress,
|
|
290
|
-
);
|
|
291
|
-
try {
|
|
292
|
-
rmSync(dir, { recursive: true, force: true });
|
|
293
|
-
} catch {
|
|
294
|
-
// 清理失败不阻塞
|
|
295
|
-
}
|
|
296
|
-
after();
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
function dirSizeBytes(dir: string): number {
|
|
300
|
-
let total = 0;
|
|
301
|
-
const stack = [dir];
|
|
302
|
-
while (stack.length > 0) {
|
|
303
|
-
const current = stack.pop()!;
|
|
304
|
-
let entries: Dirent[];
|
|
305
|
-
try {
|
|
306
|
-
entries = readdirSync(current, { withFileTypes: true });
|
|
307
|
-
} catch {
|
|
308
|
-
continue;
|
|
309
|
-
}
|
|
310
|
-
for (const entry of entries) {
|
|
311
|
-
const full = join(current, entry.name);
|
|
312
|
-
if (entry.isDirectory()) {
|
|
313
|
-
stack.push(full);
|
|
314
|
-
} else if (entry.isFile()) {
|
|
315
|
-
try {
|
|
316
|
-
total += statSync(full).size;
|
|
317
|
-
} catch {
|
|
318
|
-
// 文件被并发删除可忽略
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return total;
|
|
324
|
-
}
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
|
|
3
|
-
import { buildCodexResetConfirmCard } from "./cards.ts";
|
|
4
|
-
import type { CodexResetConsumeResult } from "./feishu-api.ts";
|
|
5
|
-
|
|
6
|
-
type CodexResetDecision = "yes" | "no";
|
|
7
|
-
|
|
8
|
-
interface PendingCodexResetRequest {
|
|
9
|
-
chatId: string;
|
|
10
|
-
parentMessageId: string;
|
|
11
|
-
status: "pending" | "handled";
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface CodexResetActionDeps {
|
|
15
|
-
getTenantAccessToken(): Promise<string>;
|
|
16
|
-
sendRawCard(token: string, chatId: string, cardJson: string): Promise<boolean>;
|
|
17
|
-
sendTextReply(token: string, chatId: string, text: string): Promise<boolean>;
|
|
18
|
-
sendCardReply(token: string, chatId: string, title: string, content: string, template?: string): Promise<boolean>;
|
|
19
|
-
updateCardMessage(token: string, messageId: string, content: string): Promise<boolean>;
|
|
20
|
-
recallMessage(token: string, messageId: string): Promise<boolean>;
|
|
21
|
-
consumeCodexRateLimitResetCredit(redeemRequestId: string): Promise<CodexResetConsumeResult>;
|
|
22
|
-
createRequestId?: () => string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const pendingCodexResetRequests = new Map<string, PendingCodexResetRequest>();
|
|
26
|
-
|
|
27
|
-
function rawEvent(data: unknown): Record<string, unknown> {
|
|
28
|
-
return ((data as Record<string, unknown>)?.event ?? data) as Record<string, unknown>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function actionValue(raw: Record<string, unknown>): Record<string, unknown> | null {
|
|
32
|
-
const action = raw.action as { value?: unknown } | undefined;
|
|
33
|
-
const value = action?.value;
|
|
34
|
-
if (!value || typeof value !== "object") return null;
|
|
35
|
-
return value as Record<string, unknown>;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function eventMessageId(raw: Record<string, unknown>): string {
|
|
39
|
-
return typeof raw.open_message_id === "string"
|
|
40
|
-
? raw.open_message_id
|
|
41
|
-
: typeof (raw.context as Record<string, unknown> | undefined)?.open_message_id === "string"
|
|
42
|
-
? (raw.context as Record<string, string>).open_message_id
|
|
43
|
-
: typeof raw.message_id === "string"
|
|
44
|
-
? raw.message_id
|
|
45
|
-
: typeof (raw.context as Record<string, unknown> | undefined)?.message_id === "string"
|
|
46
|
-
? (raw.context as Record<string, string>).message_id
|
|
47
|
-
: typeof (raw.message as Record<string, unknown> | undefined)?.message_id === "string"
|
|
48
|
-
? (raw.message as Record<string, string>).message_id
|
|
49
|
-
: "";
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function eventChatId(raw: Record<string, unknown>): string {
|
|
53
|
-
return typeof raw.open_chat_id === "string"
|
|
54
|
-
? raw.open_chat_id
|
|
55
|
-
: typeof (raw.context as Record<string, unknown> | undefined)?.open_chat_id === "string"
|
|
56
|
-
? (raw.context as Record<string, string>).open_chat_id
|
|
57
|
-
: typeof (raw.message as Record<string, unknown> | undefined)?.chat_id === "string"
|
|
58
|
-
? (raw.message as Record<string, string>).chat_id
|
|
59
|
-
: "";
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function collapsedCard(): string {
|
|
63
|
-
return JSON.stringify({
|
|
64
|
-
config: { wide_screen_mode: true },
|
|
65
|
-
elements: [{ tag: "markdown", content: " " }],
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function resetResultMessage(result: CodexResetConsumeResult): { content: string; template: string } {
|
|
70
|
-
if (result.code === "reset") {
|
|
71
|
-
return {
|
|
72
|
-
content: `重置成功。已重置 ${result.windowsReset} 个 Codex 用量窗口。`,
|
|
73
|
-
template: "green",
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
if (result.code === "nothing_to_reset") {
|
|
77
|
-
return {
|
|
78
|
-
content: "没有需要重置的 Codex 用量窗口。",
|
|
79
|
-
template: "yellow",
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
if (result.code === "no_credit") {
|
|
83
|
-
return {
|
|
84
|
-
content: "没有可用的 Codex 主动重置次数。",
|
|
85
|
-
template: "red",
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return {
|
|
89
|
-
content: "这次 Codex 主动重置请求已经处理过。",
|
|
90
|
-
template: "yellow",
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function sendResetRequestConfirmation(
|
|
95
|
-
raw: Record<string, unknown>,
|
|
96
|
-
value: Record<string, unknown>,
|
|
97
|
-
deps: CodexResetActionDeps,
|
|
98
|
-
): Promise<boolean> {
|
|
99
|
-
const parentMessageId = eventMessageId(raw);
|
|
100
|
-
const chatId = eventChatId(raw);
|
|
101
|
-
if (!parentMessageId || !chatId) return true;
|
|
102
|
-
const availableCount = Number(value.availableCount);
|
|
103
|
-
|
|
104
|
-
const requestId = (deps.createRequestId ?? randomUUID)();
|
|
105
|
-
pendingCodexResetRequests.set(requestId, {
|
|
106
|
-
chatId,
|
|
107
|
-
parentMessageId,
|
|
108
|
-
status: "pending",
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const token = await deps.getTenantAccessToken();
|
|
112
|
-
await deps.sendRawCard(token, chatId, buildCodexResetConfirmCard({
|
|
113
|
-
availableCount: Number.isFinite(availableCount) ? Math.max(1, Math.trunc(availableCount)) : 1,
|
|
114
|
-
parentMessageId,
|
|
115
|
-
requestId,
|
|
116
|
-
}));
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async function handleResetConfirmation(
|
|
121
|
-
raw: Record<string, unknown>,
|
|
122
|
-
value: Record<string, unknown>,
|
|
123
|
-
deps: CodexResetActionDeps,
|
|
124
|
-
): Promise<boolean> {
|
|
125
|
-
const decision = value.decision;
|
|
126
|
-
const requestId = value.requestId;
|
|
127
|
-
const parentMessageId = value.parentMessageId;
|
|
128
|
-
if ((decision !== "yes" && decision !== "no") || typeof requestId !== "string" || typeof parentMessageId !== "string") {
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const token = await deps.getTenantAccessToken();
|
|
133
|
-
const confirmMessageId = eventMessageId(raw);
|
|
134
|
-
if (confirmMessageId) {
|
|
135
|
-
const collapsed = await deps.updateCardMessage(token, confirmMessageId, collapsedCard());
|
|
136
|
-
console.log(`[Codex Reset] collapse confirmation card ${collapsed ? "OK" : "FAILED"}: messageId=${confirmMessageId}`);
|
|
137
|
-
const recalled = await deps.recallMessage(token, confirmMessageId);
|
|
138
|
-
console.log(`[Codex Reset] recall confirmation card ${recalled ? "OK" : "FAILED"}: messageId=${confirmMessageId}`);
|
|
139
|
-
} else {
|
|
140
|
-
console.error("[Codex Reset] missing confirmation card message id; cannot collapse");
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const pending = pendingCodexResetRequests.get(requestId);
|
|
144
|
-
const chatId = pending?.chatId ?? eventChatId(raw);
|
|
145
|
-
if (!chatId) return true;
|
|
146
|
-
|
|
147
|
-
if (!pending || pending.status !== "pending" || pending.parentMessageId !== parentMessageId) {
|
|
148
|
-
await deps.sendTextReply(token, chatId, "Codex 主动重置:这张确认卡片已经失效或已处理。");
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
pending.status = "handled";
|
|
152
|
-
|
|
153
|
-
if (decision === "no") {
|
|
154
|
-
await deps.sendTextReply(token, chatId, "Codex 主动重置:用户取消了重置。");
|
|
155
|
-
return true;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
try {
|
|
159
|
-
const result = await deps.consumeCodexRateLimitResetCredit(requestId);
|
|
160
|
-
const message = resetResultMessage(result);
|
|
161
|
-
await deps.sendTextReply(token, chatId, `Codex 主动重置:${message.content}`);
|
|
162
|
-
} catch (err) {
|
|
163
|
-
await deps.sendTextReply(token, chatId, `Codex 主动重置失败:${(err as Error).message}`);
|
|
164
|
-
}
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export async function handleCodexResetCardAction(data: unknown, deps: CodexResetActionDeps): Promise<boolean> {
|
|
169
|
-
const raw = rawEvent(data);
|
|
170
|
-
const value = actionValue(raw);
|
|
171
|
-
if (!value) return false;
|
|
172
|
-
const action = value?.action;
|
|
173
|
-
if (action === "codex_reset_request") {
|
|
174
|
-
return sendResetRequestConfirmation(raw, value, deps);
|
|
175
|
-
}
|
|
176
|
-
if (action === "codex_reset_confirm") {
|
|
177
|
-
return handleResetConfirmation(raw, value, deps);
|
|
178
|
-
}
|
|
179
|
-
return false;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export function _resetCodexResetRequestsForTest(): void {
|
|
183
|
-
pendingCodexResetRequests.clear();
|
|
184
|
-
}
|