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,49 +0,0 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
|
-
import { existsSync } from "node:fs";
|
|
3
|
-
import { dirname, join } from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { promisify } from "node:util";
|
|
6
|
-
|
|
7
|
-
import { describe, expect, it } from "vitest";
|
|
8
|
-
|
|
9
|
-
const execFileAsync = promisify(execFile);
|
|
10
|
-
|
|
11
|
-
// 兼容两种布局:<root>/deepccc-agent/src/__tests__(chatccc 子目录)或 <root>/src/__tests__(deepccc 镜像)
|
|
12
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
13
|
-
const cliBin = [
|
|
14
|
-
join(here, "..", "..", "..", "bin", "deepccc.mjs"),
|
|
15
|
-
join(here, "..", "..", "bin", "deepccc.mjs"),
|
|
16
|
-
].find(existsSync);
|
|
17
|
-
|
|
18
|
-
describe("deepccc cli --stream-json", () => {
|
|
19
|
-
it("writes only JSON lines to stdout when startup fails", async () => {
|
|
20
|
-
let caught: unknown;
|
|
21
|
-
try {
|
|
22
|
-
await execFileAsync(process.execPath, [
|
|
23
|
-
cliBin!,
|
|
24
|
-
"--stream-json",
|
|
25
|
-
"--prompt",
|
|
26
|
-
"hello",
|
|
27
|
-
"--api-key",
|
|
28
|
-
"",
|
|
29
|
-
], {
|
|
30
|
-
cwd: dirname(cliBin!),
|
|
31
|
-
timeout: 10_000,
|
|
32
|
-
windowsHide: true,
|
|
33
|
-
});
|
|
34
|
-
} catch (err) {
|
|
35
|
-
caught = err;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
expect(caught).toMatchObject({ code: 1 });
|
|
39
|
-
const stdout = (caught as { stdout?: string }).stdout ?? "";
|
|
40
|
-
const lines = stdout.trim().split(/\r?\n/).filter(Boolean);
|
|
41
|
-
expect(lines.length).toBeGreaterThan(0);
|
|
42
|
-
for (const line of lines) {
|
|
43
|
-
expect(() => JSON.parse(line)).not.toThrow();
|
|
44
|
-
}
|
|
45
|
-
expect(JSON.parse(lines.at(-1)!)).toEqual(expect.objectContaining({
|
|
46
|
-
type: "error",
|
|
47
|
-
}));
|
|
48
|
-
});
|
|
49
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { ChatSession } from "../index.js";
|
|
4
|
-
import { config, normalizeDeepCccProvider } from "../config.js";
|
|
5
|
-
|
|
6
|
-
const originalDeepSeekApiKey = process.env.DEEPSEEK_API_KEY;
|
|
7
|
-
const originalDeepCccApiKey = config.apiKey;
|
|
8
|
-
|
|
9
|
-
afterEach(() => {
|
|
10
|
-
if (originalDeepSeekApiKey === undefined) {
|
|
11
|
-
delete process.env.DEEPSEEK_API_KEY;
|
|
12
|
-
} else {
|
|
13
|
-
process.env.DEEPSEEK_API_KEY = originalDeepSeekApiKey;
|
|
14
|
-
}
|
|
15
|
-
config.apiKey = originalDeepCccApiKey;
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
describe("builtin ChatSession config", () => {
|
|
19
|
-
it("defaults provider selection to openai and accepts anthropic case-insensitively", () => {
|
|
20
|
-
expect(normalizeDeepCccProvider(undefined)).toBe("openai");
|
|
21
|
-
expect(normalizeDeepCccProvider("")).toBe("openai");
|
|
22
|
-
expect(normalizeDeepCccProvider("OPENAI")).toBe("openai");
|
|
23
|
-
expect(normalizeDeepCccProvider("Anthropic")).toBe("anthropic");
|
|
24
|
-
expect(() => normalizeDeepCccProvider("antropic")).toThrow(/openai.*anthropic/i);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("uses the builtin ~/.deepccc config when no apiKey is passed", () => {
|
|
28
|
-
expect(() => new ChatSession()).not.toThrow();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("allows constructor parameters to override config defaults", () => {
|
|
32
|
-
expect(() => new ChatSession({ apiKey: "sk-test" })).not.toThrow();
|
|
33
|
-
});
|
|
34
|
-
});
|
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { tmpdir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
import { describe, expect, it } from "vitest";
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
buildPersistedAssistantMessage,
|
|
9
|
-
buildSummaryPrompt,
|
|
10
|
-
BuiltinContextManager,
|
|
11
|
-
estimateBuiltinContextTokens,
|
|
12
|
-
listBuiltinContextSessions,
|
|
13
|
-
newBuiltinSessionId,
|
|
14
|
-
serializeMessagesForSummary,
|
|
15
|
-
} from "../context.js";
|
|
16
|
-
|
|
17
|
-
describe("BuiltinContextManager", () => {
|
|
18
|
-
it("defaults the compaction threshold to 128K tokens (one third of the 384K model window)", () => {
|
|
19
|
-
const context = new BuiltinContextManager();
|
|
20
|
-
|
|
21
|
-
expect(context.compactAtTokens).toBe(128_000);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("caps existing summaries at 8K chars when rebuilding the compaction prompt", () => {
|
|
25
|
-
const context = new BuiltinContextManager({
|
|
26
|
-
compactAtTokens: 100,
|
|
27
|
-
keepRecentMessages: 1,
|
|
28
|
-
persist: false,
|
|
29
|
-
});
|
|
30
|
-
context.setSummary("previous ".repeat(30_000));
|
|
31
|
-
context.appendMessage({ role: "user", content: "earlier request" });
|
|
32
|
-
context.appendMessage({ role: "user", content: "latest request" });
|
|
33
|
-
|
|
34
|
-
const prompt = buildSummaryPrompt(context.planCompaction()!);
|
|
35
|
-
|
|
36
|
-
expect(prompt).toContain("已有摘要为压缩而截断");
|
|
37
|
-
expect(prompt.length).toBeLessThan(40_000);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("keeps recent messages within the token budget instead of a fixed count", () => {
|
|
41
|
-
const context = new BuiltinContextManager({
|
|
42
|
-
compactAtTokens: 300,
|
|
43
|
-
keepRecentMessages: 16,
|
|
44
|
-
persist: false,
|
|
45
|
-
});
|
|
46
|
-
for (let index = 0; index < 8; index += 1) {
|
|
47
|
-
context.appendMessage({
|
|
48
|
-
role: index % 2 === 0 ? "user" : "assistant",
|
|
49
|
-
content: `${index}:` + "x".repeat(400),
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const plan = context.planCompaction();
|
|
54
|
-
|
|
55
|
-
expect(plan).not.toBeNull();
|
|
56
|
-
expect(plan!.recentMessages.length).toBeLessThan(8);
|
|
57
|
-
expect(plan!.recentMessages.at(-1)?.content).toContain("7:");
|
|
58
|
-
expect(estimateBuiltinContextTokens("", plan!.recentMessages)).toBeLessThanOrEqual(300);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
it("bounds oversized source material sent to the compaction model", () => {
|
|
62
|
-
const context = new BuiltinContextManager({
|
|
63
|
-
compactAtTokens: 100,
|
|
64
|
-
keepRecentMessages: 1,
|
|
65
|
-
persist: false,
|
|
66
|
-
});
|
|
67
|
-
context.setSummary("previous ".repeat(30_000));
|
|
68
|
-
context.appendMessage({ role: "assistant", content: "a".repeat(200_000) });
|
|
69
|
-
context.appendMessage({ role: "user", content: "latest request" });
|
|
70
|
-
|
|
71
|
-
const prompt = buildSummaryPrompt(context.planCompaction()!);
|
|
72
|
-
|
|
73
|
-
expect(prompt.length).toBeLessThan(90_000);
|
|
74
|
-
expect(prompt).toContain("为压缩而截断");
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("persists and restores summary, messages, and total message count", async () => {
|
|
78
|
-
const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-context-"));
|
|
79
|
-
|
|
80
|
-
const first = new BuiltinContextManager({
|
|
81
|
-
persist: true,
|
|
82
|
-
contextDir: dir,
|
|
83
|
-
sessionId: "persisted",
|
|
84
|
-
});
|
|
85
|
-
first.setSummary("## 用户目标\n- 保留这个摘要");
|
|
86
|
-
first.appendMessage({ role: "user", content: "第一轮" });
|
|
87
|
-
first.appendMessage({ role: "assistant", content: "第一轮回复" });
|
|
88
|
-
first.save();
|
|
89
|
-
|
|
90
|
-
const restored = new BuiltinContextManager({
|
|
91
|
-
persist: true,
|
|
92
|
-
contextDir: dir,
|
|
93
|
-
sessionId: "persisted",
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
expect(restored.summary).toContain("保留这个摘要");
|
|
97
|
-
expect(restored.messages).toEqual([
|
|
98
|
-
{ role: "user", content: "第一轮" },
|
|
99
|
-
{ role: "assistant", content: "第一轮回复" },
|
|
100
|
-
]);
|
|
101
|
-
expect(restored.totalMessages).toBe(2);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("selects only older messages for compaction and keeps recent messages raw", () => {
|
|
105
|
-
const context = new BuiltinContextManager({
|
|
106
|
-
compactAtTokens: 100,
|
|
107
|
-
keepRecentMessages: 2,
|
|
108
|
-
persist: false,
|
|
109
|
-
});
|
|
110
|
-
context.setSummary("x".repeat(500));
|
|
111
|
-
context.appendMessage({ role: "user", content: "旧用户消息" });
|
|
112
|
-
context.appendMessage({ role: "assistant", content: "旧助手回复" });
|
|
113
|
-
context.appendMessage({ role: "user", content: "近期用户消息" });
|
|
114
|
-
context.appendMessage({ role: "assistant", content: "近期助手回复" });
|
|
115
|
-
|
|
116
|
-
const plan = context.planCompaction();
|
|
117
|
-
|
|
118
|
-
expect(plan).not.toBeNull();
|
|
119
|
-
expect(plan?.oldMessages).toEqual([
|
|
120
|
-
{ role: "user", content: "旧用户消息" },
|
|
121
|
-
{ role: "assistant", content: "旧助手回复" },
|
|
122
|
-
]);
|
|
123
|
-
expect(plan?.recentMessages).toEqual([
|
|
124
|
-
{ role: "user", content: "近期用户消息" },
|
|
125
|
-
{ role: "assistant", content: "近期助手回复" },
|
|
126
|
-
]);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it("applies a compacted summary and builds model messages with summary plus recent raw turns", () => {
|
|
130
|
-
const context = new BuiltinContextManager({
|
|
131
|
-
compactAtTokens: 1,
|
|
132
|
-
keepRecentMessages: 1,
|
|
133
|
-
persist: false,
|
|
134
|
-
});
|
|
135
|
-
context.appendMessage({ role: "user", content: "old" });
|
|
136
|
-
context.appendMessage({ role: "assistant", content: "recent" });
|
|
137
|
-
|
|
138
|
-
const plan = context.planCompaction();
|
|
139
|
-
expect(plan).not.toBeNull();
|
|
140
|
-
context.applyCompaction("## 当前任务状态\n- 已压缩旧上下文", plan!);
|
|
141
|
-
|
|
142
|
-
expect(context.summary).toContain("已压缩旧上下文");
|
|
143
|
-
expect(context.messages).toEqual([{ role: "assistant", content: "recent" }]);
|
|
144
|
-
expect(context.buildModelMessages()).toEqual([
|
|
145
|
-
{
|
|
146
|
-
role: "user",
|
|
147
|
-
content: expect.stringContaining("以下是更早的对话摘要"),
|
|
148
|
-
},
|
|
149
|
-
{ role: "assistant", content: "recent" },
|
|
150
|
-
]);
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("persists structured tool calls and restores them", async () => {
|
|
154
|
-
const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-context-tools-"));
|
|
155
|
-
|
|
156
|
-
const first = new BuiltinContextManager({
|
|
157
|
-
persist: true,
|
|
158
|
-
contextDir: dir,
|
|
159
|
-
sessionId: "tool-persisted",
|
|
160
|
-
});
|
|
161
|
-
first.appendMessage({
|
|
162
|
-
role: "assistant",
|
|
163
|
-
content: "回复\n\n[工具记录]\ntool_call run_command: {}\ntool_result run_command: {}",
|
|
164
|
-
toolCalls: [
|
|
165
|
-
{ name: "run_command", input: "{\"command\":\"npm test\"}", output: "{\"exitCode\":0}" },
|
|
166
|
-
{ name: "read_file", input: "{\"path\":\"a.ts\"}", output: "...", is_error: true },
|
|
167
|
-
],
|
|
168
|
-
});
|
|
169
|
-
first.save();
|
|
170
|
-
|
|
171
|
-
const restored = new BuiltinContextManager({
|
|
172
|
-
persist: true,
|
|
173
|
-
contextDir: dir,
|
|
174
|
-
sessionId: "tool-persisted",
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
expect(restored.messages[0].toolCalls).toEqual([
|
|
178
|
-
{ name: "run_command", input: "{\"command\":\"npm test\"}", output: "{\"exitCode\":0}" },
|
|
179
|
-
{ name: "read_file", input: "{\"path\":\"a.ts\"}", output: "...", is_error: true },
|
|
180
|
-
]);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it("loads legacy context files without toolCalls and filters malformed entries", async () => {
|
|
184
|
-
const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-context-legacy-"));
|
|
185
|
-
const state = {
|
|
186
|
-
version: 1,
|
|
187
|
-
createdAt: 1,
|
|
188
|
-
updatedAt: 2,
|
|
189
|
-
sessionId: "legacy",
|
|
190
|
-
summary: "",
|
|
191
|
-
totalMessages: 2,
|
|
192
|
-
compactedMessages: 0,
|
|
193
|
-
messages: [
|
|
194
|
-
{ role: "assistant", content: "老消息" },
|
|
195
|
-
{
|
|
196
|
-
role: "assistant",
|
|
197
|
-
content: "带 toolCalls",
|
|
198
|
-
toolCalls: [
|
|
199
|
-
{ name: "ok", input: "{\"a\":1}" },
|
|
200
|
-
{ name: "" }, // 非法:空 name 应被过滤
|
|
201
|
-
{ input: "no-name" }, // 非法:缺 name 应被过滤
|
|
202
|
-
{ name: "bad-type", input: 42 }, // 非法:input 非 string 应被忽略字段
|
|
203
|
-
],
|
|
204
|
-
},
|
|
205
|
-
],
|
|
206
|
-
};
|
|
207
|
-
await mkdir(join(dir, "legacy"));
|
|
208
|
-
await writeFile(join(dir, "legacy", "context.json"), JSON.stringify(state), "utf8");
|
|
209
|
-
|
|
210
|
-
const restored = new BuiltinContextManager({
|
|
211
|
-
persist: true,
|
|
212
|
-
contextDir: dir,
|
|
213
|
-
sessionId: "legacy",
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
expect(restored.messages[0].toolCalls).toBeUndefined();
|
|
217
|
-
expect(restored.messages[1].toolCalls).toEqual([
|
|
218
|
-
{ name: "ok", input: "{\"a\":1}" },
|
|
219
|
-
{ name: "bad-type" },
|
|
220
|
-
]);
|
|
221
|
-
expect(restored.totalMessages).toBe(2);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("builds a persisted assistant message with transcript text plus structured tool calls", () => {
|
|
225
|
-
const message = buildPersistedAssistantMessage({
|
|
226
|
-
fullText: "回复正文",
|
|
227
|
-
transcriptLines: [
|
|
228
|
-
"tool_call run_command: {\"command\":\"npm test\"}",
|
|
229
|
-
"tool_result run_command: {\"exitCode\":0}",
|
|
230
|
-
],
|
|
231
|
-
toolCalls: [
|
|
232
|
-
{ name: "run_command", input: "{\"command\":\"npm test\"}", output: "{\"exitCode\":0}" },
|
|
233
|
-
],
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
expect(message.role).toBe("assistant");
|
|
237
|
-
expect(message.content).toContain("回复正文");
|
|
238
|
-
expect(message.content).toContain("[工具记录]");
|
|
239
|
-
expect(message.content).toContain("tool_call run_command");
|
|
240
|
-
expect(message.toolCalls).toEqual([
|
|
241
|
-
{ name: "run_command", input: "{\"command\":\"npm test\"}", output: "{\"exitCode\":0}" },
|
|
242
|
-
]);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it("builds a plain assistant message without tool transcript when no tools ran", () => {
|
|
246
|
-
const message = buildPersistedAssistantMessage({
|
|
247
|
-
fullText: "纯文本回复",
|
|
248
|
-
transcriptLines: [],
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
expect(message.content).toBe("纯文本回复");
|
|
252
|
-
expect(message.content).not.toContain("[工具记录]");
|
|
253
|
-
expect(message.toolCalls).toBeUndefined();
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
it("caps both assistant text and tool transcript in persisted messages", () => {
|
|
257
|
-
const message = buildPersistedAssistantMessage({
|
|
258
|
-
fullText: "a".repeat(10_000),
|
|
259
|
-
transcriptLines: Array.from({ length: 12 }, () => "x".repeat(8_000)),
|
|
260
|
-
maxAssistantChars: 2_000,
|
|
261
|
-
maxTranscriptChars: 4_000,
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
expect(message.content.length).toBeLessThan(7_000);
|
|
265
|
-
expect(message.content).toContain("助手回复已在上下文中截断");
|
|
266
|
-
expect(message.content).toContain("工具记录已截断");
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it("reset clears memory and the persisted context file", async () => {
|
|
270
|
-
const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-context-reset-"));
|
|
271
|
-
const context = new BuiltinContextManager({
|
|
272
|
-
persist: true,
|
|
273
|
-
contextDir: dir,
|
|
274
|
-
sessionId: "reset",
|
|
275
|
-
});
|
|
276
|
-
context.setSummary("summary");
|
|
277
|
-
context.appendMessage({ role: "user", content: "hello" });
|
|
278
|
-
context.save();
|
|
279
|
-
|
|
280
|
-
context.reset();
|
|
281
|
-
|
|
282
|
-
expect(context.summary).toBe("");
|
|
283
|
-
expect(context.messages).toEqual([]);
|
|
284
|
-
expect(context.totalMessages).toBe(0);
|
|
285
|
-
|
|
286
|
-
const raw = await readFile(join(dir, "reset", "context.json"), "utf8");
|
|
287
|
-
const persisted = JSON.parse(raw) as { summary: string; messages: unknown[]; totalMessages: number };
|
|
288
|
-
expect(persisted.summary).toBe("");
|
|
289
|
-
expect(persisted.messages).toEqual([]);
|
|
290
|
-
expect(persisted.totalMessages).toBe(0);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
it("persists cwd metadata and lists saved sessions newest first", async () => {
|
|
294
|
-
const dir = await mkdtemp(join(tmpdir(), "chatccc-builtin-context-list-"));
|
|
295
|
-
|
|
296
|
-
const first = new BuiltinContextManager({
|
|
297
|
-
persist: true,
|
|
298
|
-
contextDir: dir,
|
|
299
|
-
sessionId: "older",
|
|
300
|
-
cwd: "C:\\repo-a",
|
|
301
|
-
});
|
|
302
|
-
first.appendMessage({ role: "user", content: "old" });
|
|
303
|
-
|
|
304
|
-
const second = new BuiltinContextManager({
|
|
305
|
-
persist: true,
|
|
306
|
-
contextDir: dir,
|
|
307
|
-
sessionId: "newer",
|
|
308
|
-
cwd: "C:\\repo-b",
|
|
309
|
-
});
|
|
310
|
-
second.appendMessage({ role: "user", content: "new" });
|
|
311
|
-
|
|
312
|
-
const sessions = listBuiltinContextSessions(dir);
|
|
313
|
-
|
|
314
|
-
expect(sessions.map((s) => s.sessionId)).toEqual(["newer", "older"]);
|
|
315
|
-
expect(sessions[0]).toEqual(expect.objectContaining({
|
|
316
|
-
cwd: "C:\\repo-b",
|
|
317
|
-
totalMessages: 1,
|
|
318
|
-
hasSummary: false,
|
|
319
|
-
}));
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
describe("builtin context helpers", () => {
|
|
324
|
-
it("creates readable timestamp-based session ids", () => {
|
|
325
|
-
const id = newBuiltinSessionId(new Date(2026, 6, 2, 12, 15, 30), "a1b2c3");
|
|
326
|
-
|
|
327
|
-
expect(id).toBe("session-20260702-121530-a1b2c3");
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
it("estimates tokens from summary and messages", () => {
|
|
331
|
-
expect(estimateBuiltinContextTokens("abc", [{ role: "user", content: "abcdef" }]))
|
|
332
|
-
.toBeGreaterThanOrEqual(3);
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
it("serializes messages for summarization with stable role labels", () => {
|
|
336
|
-
expect(serializeMessagesForSummary([
|
|
337
|
-
{ role: "user", content: "你好" },
|
|
338
|
-
{ role: "assistant", content: "收到" },
|
|
339
|
-
])).toContain("user");
|
|
340
|
-
});
|
|
341
|
-
});
|
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
4
|
-
import { homedir, tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { promisify } from "node:util";
|
|
7
|
-
|
|
8
|
-
import { afterEach, describe, expect, it } from "vitest";
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
applyPatchForTool,
|
|
12
|
-
createFileForTool,
|
|
13
|
-
deleteFileForTool,
|
|
14
|
-
editFileForTool,
|
|
15
|
-
expandHomePath,
|
|
16
|
-
listDirForTool,
|
|
17
|
-
moveFileForTool,
|
|
18
|
-
readFileForTool,
|
|
19
|
-
runCommandForTool,
|
|
20
|
-
searchCodeForTool,
|
|
21
|
-
} from "../file-tools.js";
|
|
22
|
-
|
|
23
|
-
const execFileAsync = promisify(execFile);
|
|
24
|
-
const tempDirs: string[] = [];
|
|
25
|
-
|
|
26
|
-
async function makeTempDir(): Promise<string> {
|
|
27
|
-
const dir = await mkdtemp(join(tmpdir(), "deepccc-tools-"));
|
|
28
|
-
tempDirs.push(dir);
|
|
29
|
-
return dir;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe("expandHomePath", () => {
|
|
33
|
-
it("expands ~ and ~/ (both separators) to the user home directory", () => {
|
|
34
|
-
const home = homedir();
|
|
35
|
-
expect(expandHomePath("~")).toBe(home);
|
|
36
|
-
expect(expandHomePath("~/x/y.txt")).toBe(join(home, "x", "y.txt"));
|
|
37
|
-
expect(expandHomePath("~\\x\\y.txt")).toBe(join(home, "x", "y.txt"));
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("leaves absolute paths and other inputs unchanged", () => {
|
|
41
|
-
expect(expandHomePath("C:/a/b")).toBe("C:/a/b");
|
|
42
|
-
expect(expandHomePath("~other/x")).toBe("~other/x");
|
|
43
|
-
expect(expandHomePath("")).toBe("");
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
async function hasRg(): Promise<boolean> {
|
|
48
|
-
try {
|
|
49
|
-
await execFileAsync("rg", ["--version"]);
|
|
50
|
-
return true;
|
|
51
|
-
} catch {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function sha256(text: string): string {
|
|
57
|
-
return createHash("sha256").update(text).digest("hex");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
afterEach(async () => {
|
|
61
|
-
await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })));
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe("DeepCCC file tools", () => {
|
|
65
|
-
it("reads a text file with line ranges", async () => {
|
|
66
|
-
const dir = await makeTempDir();
|
|
67
|
-
await writeFile(join(dir, ".secret.txt"), "one\ntwo\nthree\n", "utf8");
|
|
68
|
-
|
|
69
|
-
const result = await readFileForTool(dir, { path: ".secret.txt", startLine: 2, endLine: 3 });
|
|
70
|
-
|
|
71
|
-
expect(result).toEqual(expect.objectContaining({
|
|
72
|
-
sha256: sha256("one\ntwo\nthree\n"),
|
|
73
|
-
isBinary: false,
|
|
74
|
-
content: "two\nthree",
|
|
75
|
-
startLine: 2,
|
|
76
|
-
endLine: 3,
|
|
77
|
-
totalLines: 4,
|
|
78
|
-
}));
|
|
79
|
-
expect(result.path).toContain(".secret.txt");
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("lists directory entries including hidden files", async () => {
|
|
83
|
-
const dir = await makeTempDir();
|
|
84
|
-
await writeFile(join(dir, ".env"), "TOKEN=x", "utf8");
|
|
85
|
-
|
|
86
|
-
const result = await listDirForTool(dir);
|
|
87
|
-
|
|
88
|
-
expect(result.entries).toContainEqual(expect.objectContaining({
|
|
89
|
-
name: ".env",
|
|
90
|
-
type: "file",
|
|
91
|
-
}));
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("searches code with rg without using a shell", async () => {
|
|
95
|
-
if (!await hasRg()) return;
|
|
96
|
-
|
|
97
|
-
const dir = await makeTempDir();
|
|
98
|
-
await writeFile(join(dir, "a.ts"), "const marker = 1;\n", "utf8");
|
|
99
|
-
|
|
100
|
-
const result = await searchCodeForTool(dir, { query: "marker", glob: "*.ts" });
|
|
101
|
-
|
|
102
|
-
expect(result.matches).toEqual([
|
|
103
|
-
expect.objectContaining({
|
|
104
|
-
line: 1,
|
|
105
|
-
text: "const marker = 1;",
|
|
106
|
-
}),
|
|
107
|
-
]);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it("runs non-interactive shell commands in the requested cwd", async () => {
|
|
111
|
-
const dir = await makeTempDir();
|
|
112
|
-
|
|
113
|
-
const result = await runCommandForTool(dir, {
|
|
114
|
-
command: "node -e \"process.stdout.write(process.cwd())\"",
|
|
115
|
-
timeoutMs: 5_000,
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
expect(result.exitCode).toBe(0);
|
|
119
|
-
expect(result.timedOut).toBe(false);
|
|
120
|
-
expect(result.stdout.toLowerCase()).toBe(dir.toLowerCase());
|
|
121
|
-
expect(result.stderr).toBe("");
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("returns non-zero command exits without throwing", async () => {
|
|
125
|
-
const dir = await makeTempDir();
|
|
126
|
-
|
|
127
|
-
const result = await runCommandForTool(dir, {
|
|
128
|
-
command: "node -e \"process.stderr.write('failed'); process.exit(7)\"",
|
|
129
|
-
timeoutMs: 5_000,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
expect(result.exitCode).toBe(7);
|
|
133
|
-
expect(result.stderr).toBe("failed");
|
|
134
|
-
expect(result.timedOut).toBe(false);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it("edits a file with exact replacements and a SHA-256 precondition", async () => {
|
|
138
|
-
const dir = await makeTempDir();
|
|
139
|
-
const file = join(dir, "edit.txt");
|
|
140
|
-
await writeFile(file, "alpha\nbeta\ngamma\n", "utf8");
|
|
141
|
-
|
|
142
|
-
const result = await editFileForTool(dir, {
|
|
143
|
-
path: "edit.txt",
|
|
144
|
-
expectedSha256: sha256("alpha\nbeta\ngamma\n"),
|
|
145
|
-
edits: [{ oldText: "beta", newText: "BETA" }],
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
expect(result).toEqual(expect.objectContaining({
|
|
149
|
-
changed: true,
|
|
150
|
-
editsApplied: 1,
|
|
151
|
-
beforeSha256: sha256("alpha\nbeta\ngamma\n"),
|
|
152
|
-
afterSha256: sha256("alpha\nBETA\ngamma\n"),
|
|
153
|
-
}));
|
|
154
|
-
await expect(readFile(file, "utf8")).resolves.toBe("alpha\nBETA\ngamma\n");
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it("rejects edits when the SHA-256 precondition does not match", async () => {
|
|
158
|
-
const dir = await makeTempDir();
|
|
159
|
-
await writeFile(join(dir, "edit.txt"), "current\n", "utf8");
|
|
160
|
-
|
|
161
|
-
await expect(editFileForTool(dir, {
|
|
162
|
-
path: "edit.txt",
|
|
163
|
-
expectedSha256: sha256("stale\n"),
|
|
164
|
-
edits: [{ oldText: "current", newText: "next" }],
|
|
165
|
-
})).rejects.toThrow("SHA-256 mismatch");
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
it("creates and deletes files", async () => {
|
|
169
|
-
const dir = await makeTempDir();
|
|
170
|
-
|
|
171
|
-
const created = await createFileForTool(dir, {
|
|
172
|
-
path: "created.txt",
|
|
173
|
-
content: "created\n",
|
|
174
|
-
});
|
|
175
|
-
expect(created).toEqual(expect.objectContaining({
|
|
176
|
-
changed: true,
|
|
177
|
-
afterSha256: sha256("created\n"),
|
|
178
|
-
}));
|
|
179
|
-
await expect(readFile(join(dir, "created.txt"), "utf8")).resolves.toBe("created\n");
|
|
180
|
-
|
|
181
|
-
const deleted = await deleteFileForTool(dir, {
|
|
182
|
-
path: "created.txt",
|
|
183
|
-
expectedSha256: sha256("created\n"),
|
|
184
|
-
});
|
|
185
|
-
expect(deleted).toEqual(expect.objectContaining({
|
|
186
|
-
deleted: true,
|
|
187
|
-
beforeSha256: sha256("created\n"),
|
|
188
|
-
}));
|
|
189
|
-
await expect(stat(join(dir, "created.txt"))).rejects.toThrow();
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it("moves files and creates the destination directory", async () => {
|
|
193
|
-
const dir = await makeTempDir();
|
|
194
|
-
await writeFile(join(dir, "old.txt"), "move me\n", "utf8");
|
|
195
|
-
|
|
196
|
-
const result = await moveFileForTool(dir, {
|
|
197
|
-
sourcePath: "old.txt",
|
|
198
|
-
destinationPath: "nested/new.txt",
|
|
199
|
-
expectedSourceSha256: sha256("move me\n"),
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
expect(result).toEqual(expect.objectContaining({
|
|
203
|
-
moved: true,
|
|
204
|
-
sourceSha256: sha256("move me\n"),
|
|
205
|
-
}));
|
|
206
|
-
await expect(stat(join(dir, "old.txt"))).rejects.toThrow();
|
|
207
|
-
await expect(readFile(join(dir, "nested", "new.txt"), "utf8")).resolves.toBe("move me\n");
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
it("applies a unified diff patch", async () => {
|
|
211
|
-
const dir = await makeTempDir();
|
|
212
|
-
await writeFile(join(dir, "patch.txt"), "one\ntwo\nthree\n", "utf8");
|
|
213
|
-
|
|
214
|
-
const result = await applyPatchForTool(dir, {
|
|
215
|
-
patch: [
|
|
216
|
-
"--- a/patch.txt",
|
|
217
|
-
"+++ b/patch.txt",
|
|
218
|
-
"@@ -1,4 +1,4 @@",
|
|
219
|
-
" one",
|
|
220
|
-
"-two",
|
|
221
|
-
"+TWO",
|
|
222
|
-
" three",
|
|
223
|
-
" ",
|
|
224
|
-
"",
|
|
225
|
-
].join("\n"),
|
|
226
|
-
expectedSha256ByPath: {
|
|
227
|
-
"patch.txt": sha256("one\ntwo\nthree\n"),
|
|
228
|
-
},
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
expect(result.changedFiles).toEqual([
|
|
232
|
-
expect.objectContaining({
|
|
233
|
-
action: "edit",
|
|
234
|
-
beforeSha256: sha256("one\ntwo\nthree\n"),
|
|
235
|
-
afterSha256: sha256("one\nTWO\nthree\n"),
|
|
236
|
-
}),
|
|
237
|
-
]);
|
|
238
|
-
await expect(readFile(join(dir, "patch.txt"), "utf8")).resolves.toBe("one\nTWO\nthree\n");
|
|
239
|
-
});
|
|
240
|
-
});
|