chatccc 0.2.231 → 0.2.232

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 (127) hide show
  1. package/.agents/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  2. package/.claude/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  3. package/.cursor/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  4. package/README.md +90 -90
  5. package/agent-prompts/claude_specific.md +45 -45
  6. package/agent-prompts/codex_specific.md +2 -2
  7. package/agent-prompts/cursor_specific.md +13 -13
  8. package/bin/cccagent.mjs +17 -17
  9. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  10. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  11. package/im-skills/feishu-skill/skill.md +3 -3
  12. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  13. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  14. package/im-skills/wechat-file-skill/skill.md +10 -10
  15. package/im-skills/wechat-image-skill/skill.md +10 -10
  16. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  17. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  18. package/im-skills/wechat-video-skill/skill.md +10 -10
  19. package/package.json +1 -1
  20. package/scripts/postinstall-sharp-check.mjs +58 -58
  21. package/src/__tests__/agent-activity.test.ts +86 -86
  22. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  23. package/src/__tests__/agent-reload-config-rpc.test.ts +99 -99
  24. package/src/__tests__/builtin-chat-session.test.ts +449 -393
  25. package/src/__tests__/builtin-cli-json.test.ts +39 -39
  26. package/src/__tests__/builtin-config.test.ts +26 -26
  27. package/src/__tests__/builtin-context.test.ts +319 -202
  28. package/src/__tests__/builtin-file-tools.test.ts +240 -240
  29. package/src/__tests__/builtin-permissions.test.ts +211 -211
  30. package/src/__tests__/builtin-session-search.test.ts +228 -0
  31. package/src/__tests__/builtin-session-select.test.ts +116 -116
  32. package/src/__tests__/builtin-sigint.test.ts +56 -56
  33. package/src/__tests__/builtin-skills.test.ts +284 -284
  34. package/src/__tests__/builtin-web-tools.test.ts +220 -220
  35. package/src/__tests__/card-action-routing.test.ts +18 -18
  36. package/src/__tests__/cardkit.test.ts +60 -60
  37. package/src/__tests__/ccc-adapter.test.ts +170 -170
  38. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  39. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  40. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  41. package/src/__tests__/claude-adapter.test.ts +614 -614
  42. package/src/__tests__/claude-raw-stream-log.test.ts +87 -87
  43. package/src/__tests__/codex-adapter.test.ts +58 -58
  44. package/src/__tests__/codex-raw-stream-log.test.ts +170 -170
  45. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  46. package/src/__tests__/cursor-adapter.test.ts +268 -268
  47. package/src/__tests__/feishu-api.test.ts +60 -60
  48. package/src/__tests__/feishu-avatar.test.ts +164 -164
  49. package/src/__tests__/feishu-message-ingress.test.ts +138 -138
  50. package/src/__tests__/feishu-platform.test.ts +22 -22
  51. package/src/__tests__/format-message.test.ts +47 -47
  52. package/src/__tests__/jsonl-stream.test.ts +79 -79
  53. package/src/__tests__/package-files.test.ts +24 -24
  54. package/src/__tests__/privacy.test.ts +198 -198
  55. package/src/__tests__/progress-reducer.test.ts +121 -121
  56. package/src/__tests__/raw-stream-log.test.ts +106 -106
  57. package/src/__tests__/response-stall.test.ts +49 -49
  58. package/src/__tests__/restart.test.ts +132 -132
  59. package/src/__tests__/session.test.ts +199 -199
  60. package/src/__tests__/shared-prefix.test.ts +36 -36
  61. package/src/__tests__/sim-platform.test.ts +16 -16
  62. package/src/__tests__/startup-lifecycle.test.ts +231 -231
  63. package/src/__tests__/stop-session.test.ts +34 -34
  64. package/src/__tests__/stream-state.test.ts +42 -42
  65. package/src/__tests__/terminal-error.test.ts +54 -54
  66. package/src/__tests__/terminal-renderer.test.ts +247 -247
  67. package/src/__tests__/update-command-guard.test.ts +144 -144
  68. package/src/__tests__/web-ui.test.ts +326 -326
  69. package/src/adapters/adapter-interface.ts +33 -33
  70. package/src/adapters/ccc-adapter.ts +141 -141
  71. package/src/adapters/claude-adapter.ts +620 -620
  72. package/src/adapters/claude-session-meta-store.ts +120 -120
  73. package/src/adapters/codex-adapter.ts +426 -426
  74. package/src/adapters/cursor-adapter.ts +681 -681
  75. package/src/adapters/jsonl-stream.ts +157 -157
  76. package/src/adapters/raw-stream-log.ts +124 -124
  77. package/src/adapters/resource-monitor.ts +140 -140
  78. package/src/agent-activity.ts +175 -175
  79. package/src/agent-delegate-task-rpc.ts +153 -153
  80. package/src/agent-delegate-task.ts +91 -91
  81. package/src/agent-reload-config-rpc.ts +34 -34
  82. package/src/agent-stop-stuck.ts +129 -129
  83. package/src/builtin/cli.ts +6 -0
  84. package/src/builtin/config.ts +84 -84
  85. package/src/builtin/context.ts +465 -391
  86. package/src/builtin/file-log.ts +38 -38
  87. package/src/builtin/file-tools.ts +34 -0
  88. package/src/builtin/index.ts +24 -23
  89. package/src/builtin/proc-tree-kill.ts +61 -61
  90. package/src/builtin/progress/cards-helpers.ts +76 -76
  91. package/src/builtin/progress/reducer.ts +113 -113
  92. package/src/builtin/progress/terminal-renderer.ts +294 -294
  93. package/src/builtin/progress/view.ts +77 -77
  94. package/src/builtin/raw-stream-log.ts +124 -124
  95. package/src/builtin/session-search.ts +363 -0
  96. package/src/builtin/session-select.ts +48 -48
  97. package/src/builtin/sigint.ts +50 -50
  98. package/src/builtin/skills.ts +205 -205
  99. package/src/builtin/web-tools.ts +313 -313
  100. package/src/card-action-routing.ts +14 -14
  101. package/src/chatgpt-subscription-rpc.ts +27 -27
  102. package/src/chatgpt-subscription.ts +299 -299
  103. package/src/chrome-devtools-guard.ts +318 -318
  104. package/src/codex-reset-actions.ts +184 -184
  105. package/src/feishu-api.ts +193 -193
  106. package/src/feishu-message-ingress.ts +195 -195
  107. package/src/feishu-platform.ts +20 -20
  108. package/src/format-message.ts +293 -293
  109. package/src/litellm-proxy.ts +374 -374
  110. package/src/orchestrator.ts +2494 -2494
  111. package/src/platform-adapter.ts +6 -6
  112. package/src/privacy.ts +118 -118
  113. package/src/progress/reducer.ts +113 -113
  114. package/src/progress/terminal-renderer.ts +294 -294
  115. package/src/progress/view.ts +77 -77
  116. package/src/response-stall.ts +28 -28
  117. package/src/runtime-reload.ts +34 -34
  118. package/src/session-chat-binding.ts +82 -82
  119. package/src/session-name.ts +8 -8
  120. package/src/session.ts +201 -201
  121. package/src/shared-prefix.ts +29 -29
  122. package/src/sim-platform.ts +20 -20
  123. package/src/startup-lifecycle.ts +250 -250
  124. package/src/stream-state.ts +21 -21
  125. package/src/terminal-error.ts +129 -129
  126. package/src/turn-cards.ts +117 -117
  127. package/src/update-command-guard.ts +165 -165
@@ -1,198 +1,198 @@
1
- import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from "vitest";
2
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
-
6
- const TEST_DATA_DIR = await mkdtemp(join(tmpdir(), "chatccc-privacy-test-"));
7
- vi.mock("../config.ts", async () => {
8
- const actual = await vi.importActual<typeof import("../config.ts")>("../config.ts");
9
- return {
10
- ...actual,
11
- USER_DATA_DIR: TEST_DATA_DIR,
12
- ts: () => "test-ts",
13
- };
14
- });
15
-
16
- let applyPrivacy: (text: string) => string;
17
- let reloadPrivacyRules: () => void;
18
- let getPrivacyRules: () => Record<string, string>;
19
- let getPrivacyConfig: () => { enabled: boolean; rules: Record<string, string> };
20
-
21
- beforeEach(async () => {
22
- vi.resetModules();
23
- try {
24
- await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
25
- } catch {}
26
- const mod = await import("../privacy.ts");
27
- applyPrivacy = mod.applyPrivacy;
28
- reloadPrivacyRules = mod.reloadPrivacyRules;
29
- getPrivacyRules = mod.getPrivacyRules;
30
- getPrivacyConfig = mod.getPrivacyConfig;
31
- });
32
-
33
- afterEach(async () => {
34
- try {
35
- await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
36
- } catch {}
37
- });
38
-
39
- afterAll(async () => {
40
- try {
41
- await rm(TEST_DATA_DIR, { recursive: true, force: true });
42
- } catch {}
43
- });
44
-
45
- describe("applyPrivacy", () => {
46
- it("returns original text when privacy.json is missing", () => {
47
- reloadPrivacyRules();
48
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
49
- });
50
-
51
- it("supports legacy flat privacy rules", async () => {
52
- await writeFile(
53
- join(TEST_DATA_DIR, "privacy.json"),
54
- JSON.stringify({ weizhangjian: "wzj", secret: "***" }),
55
- "utf-8",
56
- );
57
- reloadPrivacyRules();
58
-
59
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
60
- expect(applyPrivacy("my secret is safe")).toBe("my *** is safe");
61
- expect(applyPrivacy("weizhangjian and secret")).toBe("wzj and ***");
62
- });
63
-
64
- it("supports privacy.json schema with enabled=false", async () => {
65
- await writeFile(
66
- join(TEST_DATA_DIR, "privacy.json"),
67
- JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } }),
68
- "utf-8",
69
- );
70
- reloadPrivacyRules();
71
-
72
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
73
- expect(getPrivacyRules()).toEqual({ weizhangjian: "wzj" });
74
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
75
- });
76
-
77
- it("supports privacy.json schema with enabled=true", async () => {
78
- await writeFile(
79
- join(TEST_DATA_DIR, "privacy.json"),
80
- JSON.stringify({ enabled: true, rules: { weizhangjian: "wzj" } }),
81
- "utf-8",
82
- );
83
- reloadPrivacyRules();
84
-
85
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
86
- });
87
-
88
- it("accepts UTF-8 BOM in privacy.json", async () => {
89
- await writeFile(
90
- join(TEST_DATA_DIR, "privacy.json"),
91
- `\uFEFF${JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } })}`,
92
- "utf-8",
93
- );
94
- reloadPrivacyRules();
95
-
96
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
97
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
98
- });
99
-
100
- it("auto reloads privacy.json changes without explicit reload", async () => {
101
- await writeFile(
102
- join(TEST_DATA_DIR, "privacy.json"),
103
- JSON.stringify({ weizhangjian: "wzj" }),
104
- "utf-8",
105
- );
106
- reloadPrivacyRules();
107
-
108
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
109
-
110
- await writeFile(
111
- join(TEST_DATA_DIR, "privacy.json"),
112
- JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj-disabled" } }),
113
- "utf-8",
114
- );
115
-
116
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
117
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj-disabled" } });
118
- });
119
-
120
- it("replaces multiple rules and repeated occurrences", async () => {
121
- await writeFile(
122
- join(TEST_DATA_DIR, "privacy.json"),
123
- JSON.stringify({ a: "A", b: "B" }),
124
- "utf-8",
125
- );
126
- reloadPrivacyRules();
127
-
128
- expect(applyPrivacy("a b a b")).toBe("A B A B");
129
- });
130
-
131
- it("returns empty text directly", async () => {
132
- await writeFile(
133
- join(TEST_DATA_DIR, "privacy.json"),
134
- JSON.stringify({ x: "y" }),
135
- "utf-8",
136
- );
137
- reloadPrivacyRules();
138
-
139
- expect(applyPrivacy("")).toBe("");
140
- });
141
-
142
- it("treats special characters in rule keys literally", async () => {
143
- await writeFile(
144
- join(TEST_DATA_DIR, "privacy.json"),
145
- JSON.stringify({ "a.b": "X", "(test)": "Y", "*star": "Z" }),
146
- "utf-8",
147
- );
148
- reloadPrivacyRules();
149
-
150
- expect(applyPrivacy("hello a.b world")).toBe("hello X world");
151
- expect(applyPrivacy("text (test) here")).toBe("text Y here");
152
- expect(applyPrivacy("a *star shines")).toBe("a Z shines");
153
- });
154
-
155
- it("reloadPrivacyRules forces a reload", async () => {
156
- await writeFile(
157
- join(TEST_DATA_DIR, "privacy.json"),
158
- JSON.stringify({ old: "OLD" }),
159
- "utf-8",
160
- );
161
- reloadPrivacyRules();
162
-
163
- expect(applyPrivacy("old")).toBe("OLD");
164
- expect(getPrivacyRules()).toEqual({ old: "OLD" });
165
-
166
- await writeFile(
167
- join(TEST_DATA_DIR, "privacy.json"),
168
- JSON.stringify({ new: "NEW" }),
169
- "utf-8",
170
- );
171
- reloadPrivacyRules();
172
-
173
- expect(applyPrivacy("new")).toBe("NEW");
174
- expect(getPrivacyRules()).toEqual({ new: "NEW" });
175
- });
176
-
177
- it("returns original text for malformed JSON", async () => {
178
- await writeFile(
179
- join(TEST_DATA_DIR, "privacy.json"),
180
- "not json",
181
- "utf-8",
182
- );
183
- reloadPrivacyRules();
184
-
185
- expect(applyPrivacy("hello")).toBe("hello");
186
- });
187
-
188
- it("returns original text for array JSON", async () => {
189
- await writeFile(
190
- join(TEST_DATA_DIR, "privacy.json"),
191
- JSON.stringify(["a", "b"]),
192
- "utf-8",
193
- );
194
- reloadPrivacyRules();
195
-
196
- expect(applyPrivacy("hello")).toBe("hello");
197
- });
198
- });
1
+ import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from "vitest";
2
+ import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+
6
+ const TEST_DATA_DIR = await mkdtemp(join(tmpdir(), "chatccc-privacy-test-"));
7
+ vi.mock("../config.ts", async () => {
8
+ const actual = await vi.importActual<typeof import("../config.ts")>("../config.ts");
9
+ return {
10
+ ...actual,
11
+ USER_DATA_DIR: TEST_DATA_DIR,
12
+ ts: () => "test-ts",
13
+ };
14
+ });
15
+
16
+ let applyPrivacy: (text: string) => string;
17
+ let reloadPrivacyRules: () => void;
18
+ let getPrivacyRules: () => Record<string, string>;
19
+ let getPrivacyConfig: () => { enabled: boolean; rules: Record<string, string> };
20
+
21
+ beforeEach(async () => {
22
+ vi.resetModules();
23
+ try {
24
+ await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
25
+ } catch {}
26
+ const mod = await import("../privacy.ts");
27
+ applyPrivacy = mod.applyPrivacy;
28
+ reloadPrivacyRules = mod.reloadPrivacyRules;
29
+ getPrivacyRules = mod.getPrivacyRules;
30
+ getPrivacyConfig = mod.getPrivacyConfig;
31
+ });
32
+
33
+ afterEach(async () => {
34
+ try {
35
+ await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
36
+ } catch {}
37
+ });
38
+
39
+ afterAll(async () => {
40
+ try {
41
+ await rm(TEST_DATA_DIR, { recursive: true, force: true });
42
+ } catch {}
43
+ });
44
+
45
+ describe("applyPrivacy", () => {
46
+ it("returns original text when privacy.json is missing", () => {
47
+ reloadPrivacyRules();
48
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
49
+ });
50
+
51
+ it("supports legacy flat privacy rules", async () => {
52
+ await writeFile(
53
+ join(TEST_DATA_DIR, "privacy.json"),
54
+ JSON.stringify({ weizhangjian: "wzj", secret: "***" }),
55
+ "utf-8",
56
+ );
57
+ reloadPrivacyRules();
58
+
59
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
60
+ expect(applyPrivacy("my secret is safe")).toBe("my *** is safe");
61
+ expect(applyPrivacy("weizhangjian and secret")).toBe("wzj and ***");
62
+ });
63
+
64
+ it("supports privacy.json schema with enabled=false", async () => {
65
+ await writeFile(
66
+ join(TEST_DATA_DIR, "privacy.json"),
67
+ JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } }),
68
+ "utf-8",
69
+ );
70
+ reloadPrivacyRules();
71
+
72
+ expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
73
+ expect(getPrivacyRules()).toEqual({ weizhangjian: "wzj" });
74
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
75
+ });
76
+
77
+ it("supports privacy.json schema with enabled=true", async () => {
78
+ await writeFile(
79
+ join(TEST_DATA_DIR, "privacy.json"),
80
+ JSON.stringify({ enabled: true, rules: { weizhangjian: "wzj" } }),
81
+ "utf-8",
82
+ );
83
+ reloadPrivacyRules();
84
+
85
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
86
+ });
87
+
88
+ it("accepts UTF-8 BOM in privacy.json", async () => {
89
+ await writeFile(
90
+ join(TEST_DATA_DIR, "privacy.json"),
91
+ `\uFEFF${JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } })}`,
92
+ "utf-8",
93
+ );
94
+ reloadPrivacyRules();
95
+
96
+ expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
97
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
98
+ });
99
+
100
+ it("auto reloads privacy.json changes without explicit reload", async () => {
101
+ await writeFile(
102
+ join(TEST_DATA_DIR, "privacy.json"),
103
+ JSON.stringify({ weizhangjian: "wzj" }),
104
+ "utf-8",
105
+ );
106
+ reloadPrivacyRules();
107
+
108
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
109
+
110
+ await writeFile(
111
+ join(TEST_DATA_DIR, "privacy.json"),
112
+ JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj-disabled" } }),
113
+ "utf-8",
114
+ );
115
+
116
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
117
+ expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj-disabled" } });
118
+ });
119
+
120
+ it("replaces multiple rules and repeated occurrences", async () => {
121
+ await writeFile(
122
+ join(TEST_DATA_DIR, "privacy.json"),
123
+ JSON.stringify({ a: "A", b: "B" }),
124
+ "utf-8",
125
+ );
126
+ reloadPrivacyRules();
127
+
128
+ expect(applyPrivacy("a b a b")).toBe("A B A B");
129
+ });
130
+
131
+ it("returns empty text directly", async () => {
132
+ await writeFile(
133
+ join(TEST_DATA_DIR, "privacy.json"),
134
+ JSON.stringify({ x: "y" }),
135
+ "utf-8",
136
+ );
137
+ reloadPrivacyRules();
138
+
139
+ expect(applyPrivacy("")).toBe("");
140
+ });
141
+
142
+ it("treats special characters in rule keys literally", async () => {
143
+ await writeFile(
144
+ join(TEST_DATA_DIR, "privacy.json"),
145
+ JSON.stringify({ "a.b": "X", "(test)": "Y", "*star": "Z" }),
146
+ "utf-8",
147
+ );
148
+ reloadPrivacyRules();
149
+
150
+ expect(applyPrivacy("hello a.b world")).toBe("hello X world");
151
+ expect(applyPrivacy("text (test) here")).toBe("text Y here");
152
+ expect(applyPrivacy("a *star shines")).toBe("a Z shines");
153
+ });
154
+
155
+ it("reloadPrivacyRules forces a reload", async () => {
156
+ await writeFile(
157
+ join(TEST_DATA_DIR, "privacy.json"),
158
+ JSON.stringify({ old: "OLD" }),
159
+ "utf-8",
160
+ );
161
+ reloadPrivacyRules();
162
+
163
+ expect(applyPrivacy("old")).toBe("OLD");
164
+ expect(getPrivacyRules()).toEqual({ old: "OLD" });
165
+
166
+ await writeFile(
167
+ join(TEST_DATA_DIR, "privacy.json"),
168
+ JSON.stringify({ new: "NEW" }),
169
+ "utf-8",
170
+ );
171
+ reloadPrivacyRules();
172
+
173
+ expect(applyPrivacy("new")).toBe("NEW");
174
+ expect(getPrivacyRules()).toEqual({ new: "NEW" });
175
+ });
176
+
177
+ it("returns original text for malformed JSON", async () => {
178
+ await writeFile(
179
+ join(TEST_DATA_DIR, "privacy.json"),
180
+ "not json",
181
+ "utf-8",
182
+ );
183
+ reloadPrivacyRules();
184
+
185
+ expect(applyPrivacy("hello")).toBe("hello");
186
+ });
187
+
188
+ it("returns original text for array JSON", async () => {
189
+ await writeFile(
190
+ join(TEST_DATA_DIR, "privacy.json"),
191
+ JSON.stringify(["a", "b"]),
192
+ "utf-8",
193
+ );
194
+ reloadPrivacyRules();
195
+
196
+ expect(applyPrivacy("hello")).toBe("hello");
197
+ });
198
+ });
@@ -1,121 +1,121 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import type { ChatEvent } from "../builtin/index.ts";
4
- import { reduceProgress, summarizeToolInput, summarizeToolResult } from "../progress/reducer.ts";
5
- import { progressView } from "../progress/view.ts";
6
-
7
- function feed(events: ChatEvent[]) {
8
- return events.reduce(reduceProgress, progressView({ headerTitle: "生成中..." }));
9
- }
10
-
11
- describe("reduceProgress", () => {
12
- it("renders explicit compaction and generation phases", () => {
13
- const compacting = reduceProgress(
14
- progressView({ headerTitle: "Generating..." }),
15
- { type: "status", phase: "compacting" },
16
- );
17
- expect(compacting.headerTitle).toBe("压缩上下文中...");
18
-
19
- const generating = reduceProgress(compacting, { type: "status", phase: "generating" });
20
- expect(generating.headerTitle).toBe("生成回复中...");
21
- });
22
-
23
- it("accumulates text via accumulated field", () => {
24
- const view = feed([
25
- { type: "text", text: "Hello", accumulated: "Hello" },
26
- { type: "text", text: " world", accumulated: "Hello world" },
27
- ]);
28
- expect(view.text).toBe("Hello world");
29
- expect(view.status).toBe("generating");
30
- expect(view.showStop).toBe(true);
31
- });
32
-
33
- it("appends tool_use as running and resolves status on tool_result", () => {
34
- const view = feed([
35
- { type: "tool_use", id: "t1", name: "edit_file", input: { path: "a.ts" } },
36
- { type: "tool_result", tool_use_id: "t1", name: "edit_file", content: "ok", is_error: false },
37
- ]);
38
- expect(view.tools).toHaveLength(1);
39
- expect(view.tools[0]).toMatchObject({
40
- id: "t1",
41
- name: "edit_file",
42
- status: "ok",
43
- summary: "ok",
44
- });
45
- });
46
-
47
- it("marks tool as error when is_error is true", () => {
48
- const view = feed([
49
- { type: "tool_use", id: "t2", name: "run_command", input: { command: "npm test" } },
50
- { type: "tool_result", tool_use_id: "t2", name: "run_command", content: "boom", is_error: true },
51
- ]);
52
- expect(view.tools[0].status).toBe("error");
53
- expect(view.tools[0].summary).toBe("boom");
54
- });
55
-
56
- it("falls back to last running tool when tool_use_id mismatches", () => {
57
- const view = feed([
58
- { type: "tool_use", id: undefined, name: "list_dir", input: {} },
59
- { type: "tool_result", tool_use_id: "unknown-id", name: "list_dir", content: "ok", is_error: false },
60
- ]);
61
- expect(view.tools[0].status).toBe("ok");
62
- });
63
-
64
- it("keeps multiple tool calls independently", () => {
65
- const view = feed([
66
- { type: "tool_use", id: "a", name: "read_file", input: { path: "a" } },
67
- { type: "tool_use", id: "b", name: "read_file", input: { path: "b" } },
68
- { type: "tool_result", tool_use_id: "b", name: "read_file", content: "bb", is_error: false },
69
- ]);
70
- expect(view.tools[0].status).toBe("running");
71
- expect(view.tools[1].status).toBe("ok");
72
- });
73
-
74
- it("marks done: status done, showStop false, text finalized", () => {
75
- const view = feed([
76
- { type: "text", text: "part", accumulated: "part" },
77
- { type: "done", text: "final answer" },
78
- ]);
79
- expect(view.status).toBe("done");
80
- expect(view.showStop).toBe(false);
81
- expect(view.text).toBe("final answer");
82
- });
83
-
84
- it("marks error status and keeps text", () => {
85
- const view = feed([
86
- { type: "text", text: "x", accumulated: "x" },
87
- { type: "error", message: "boom" },
88
- ]);
89
- expect(view.status).toBe("error");
90
- expect(view.showStop).toBe(false);
91
- expect(view.text).toBe("x");
92
- });
93
-
94
- it("ignores compact events without changing the view identity", () => {
95
- const base = progressView({ headerTitle: "生成中..." });
96
- const result = reduceProgress(base, { type: "compact", compactedMessages: 5 });
97
- expect(result).toBe(base);
98
- });
99
-
100
- it("does not mutate the previous view (immutable updates)", () => {
101
- const base = progressView({ headerTitle: "生成中..." });
102
- const next = reduceProgress(base, { type: "text", text: "hi", accumulated: "hi" });
103
- expect(base.text).toBe("");
104
- expect(next.text).toBe("hi");
105
- expect(next).not.toBe(base);
106
- });
107
- });
108
-
109
- describe("summarizeToolInput / summarizeToolResult", () => {
110
- it("flattens multiline input to one line", () => {
111
- const s = summarizeToolInput({ path: "a\nb", big: "x".repeat(200) });
112
- expect(s).not.toContain("\n");
113
- expect(s.length).toBeLessThanOrEqual(121);
114
- expect(s.endsWith("…")).toBe(true);
115
- });
116
-
117
- it("takes first line of result content", () => {
118
- expect(summarizeToolResult("line1\nline2\n")).toBe("line1");
119
- expect(summarizeToolResult({ ok: true })).toBe('{"ok":true}');
120
- });
121
- });
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import type { ChatEvent } from "../builtin/index.ts";
4
+ import { reduceProgress, summarizeToolInput, summarizeToolResult } from "../progress/reducer.ts";
5
+ import { progressView } from "../progress/view.ts";
6
+
7
+ function feed(events: ChatEvent[]) {
8
+ return events.reduce(reduceProgress, progressView({ headerTitle: "生成中..." }));
9
+ }
10
+
11
+ describe("reduceProgress", () => {
12
+ it("renders explicit compaction and generation phases", () => {
13
+ const compacting = reduceProgress(
14
+ progressView({ headerTitle: "Generating..." }),
15
+ { type: "status", phase: "compacting" },
16
+ );
17
+ expect(compacting.headerTitle).toBe("压缩上下文中...");
18
+
19
+ const generating = reduceProgress(compacting, { type: "status", phase: "generating" });
20
+ expect(generating.headerTitle).toBe("生成回复中...");
21
+ });
22
+
23
+ it("accumulates text via accumulated field", () => {
24
+ const view = feed([
25
+ { type: "text", text: "Hello", accumulated: "Hello" },
26
+ { type: "text", text: " world", accumulated: "Hello world" },
27
+ ]);
28
+ expect(view.text).toBe("Hello world");
29
+ expect(view.status).toBe("generating");
30
+ expect(view.showStop).toBe(true);
31
+ });
32
+
33
+ it("appends tool_use as running and resolves status on tool_result", () => {
34
+ const view = feed([
35
+ { type: "tool_use", id: "t1", name: "edit_file", input: { path: "a.ts" } },
36
+ { type: "tool_result", tool_use_id: "t1", name: "edit_file", content: "ok", is_error: false },
37
+ ]);
38
+ expect(view.tools).toHaveLength(1);
39
+ expect(view.tools[0]).toMatchObject({
40
+ id: "t1",
41
+ name: "edit_file",
42
+ status: "ok",
43
+ summary: "ok",
44
+ });
45
+ });
46
+
47
+ it("marks tool as error when is_error is true", () => {
48
+ const view = feed([
49
+ { type: "tool_use", id: "t2", name: "run_command", input: { command: "npm test" } },
50
+ { type: "tool_result", tool_use_id: "t2", name: "run_command", content: "boom", is_error: true },
51
+ ]);
52
+ expect(view.tools[0].status).toBe("error");
53
+ expect(view.tools[0].summary).toBe("boom");
54
+ });
55
+
56
+ it("falls back to last running tool when tool_use_id mismatches", () => {
57
+ const view = feed([
58
+ { type: "tool_use", id: undefined, name: "list_dir", input: {} },
59
+ { type: "tool_result", tool_use_id: "unknown-id", name: "list_dir", content: "ok", is_error: false },
60
+ ]);
61
+ expect(view.tools[0].status).toBe("ok");
62
+ });
63
+
64
+ it("keeps multiple tool calls independently", () => {
65
+ const view = feed([
66
+ { type: "tool_use", id: "a", name: "read_file", input: { path: "a" } },
67
+ { type: "tool_use", id: "b", name: "read_file", input: { path: "b" } },
68
+ { type: "tool_result", tool_use_id: "b", name: "read_file", content: "bb", is_error: false },
69
+ ]);
70
+ expect(view.tools[0].status).toBe("running");
71
+ expect(view.tools[1].status).toBe("ok");
72
+ });
73
+
74
+ it("marks done: status done, showStop false, text finalized", () => {
75
+ const view = feed([
76
+ { type: "text", text: "part", accumulated: "part" },
77
+ { type: "done", text: "final answer" },
78
+ ]);
79
+ expect(view.status).toBe("done");
80
+ expect(view.showStop).toBe(false);
81
+ expect(view.text).toBe("final answer");
82
+ });
83
+
84
+ it("marks error status and keeps text", () => {
85
+ const view = feed([
86
+ { type: "text", text: "x", accumulated: "x" },
87
+ { type: "error", message: "boom" },
88
+ ]);
89
+ expect(view.status).toBe("error");
90
+ expect(view.showStop).toBe(false);
91
+ expect(view.text).toBe("x");
92
+ });
93
+
94
+ it("ignores compact events without changing the view identity", () => {
95
+ const base = progressView({ headerTitle: "生成中..." });
96
+ const result = reduceProgress(base, { type: "compact", compactedMessages: 5 });
97
+ expect(result).toBe(base);
98
+ });
99
+
100
+ it("does not mutate the previous view (immutable updates)", () => {
101
+ const base = progressView({ headerTitle: "生成中..." });
102
+ const next = reduceProgress(base, { type: "text", text: "hi", accumulated: "hi" });
103
+ expect(base.text).toBe("");
104
+ expect(next.text).toBe("hi");
105
+ expect(next).not.toBe(base);
106
+ });
107
+ });
108
+
109
+ describe("summarizeToolInput / summarizeToolResult", () => {
110
+ it("flattens multiline input to one line", () => {
111
+ const s = summarizeToolInput({ path: "a\nb", big: "x".repeat(200) });
112
+ expect(s).not.toContain("\n");
113
+ expect(s.length).toBeLessThanOrEqual(121);
114
+ expect(s.endsWith("…")).toBe(true);
115
+ });
116
+
117
+ it("takes first line of result content", () => {
118
+ expect(summarizeToolResult("line1\nline2\n")).toBe("line1");
119
+ expect(summarizeToolResult({ ok: true })).toBe('{"ok":true}');
120
+ });
121
+ });