chatccc 0.2.251 → 0.2.253

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 (277) hide show
  1. package/README.md +1 -1
  2. package/bin/cccagent.mjs +12 -3
  3. package/bin/chatccc.mjs +15 -6
  4. package/deepccc-agent/package.json +1 -1
  5. package/dist/deepccc-agent/src/cli.js +634 -0
  6. package/dist/deepccc-agent/src/config.js +76 -0
  7. package/dist/deepccc-agent/src/context.js +348 -0
  8. package/dist/deepccc-agent/src/file-log.js +34 -0
  9. package/dist/deepccc-agent/src/file-tools.js +1208 -0
  10. package/dist/deepccc-agent/src/index.js +571 -0
  11. package/dist/deepccc-agent/src/permissions.js +170 -0
  12. package/dist/deepccc-agent/src/privacy.js +124 -0
  13. package/dist/deepccc-agent/src/proc-tree-kill.js +60 -0
  14. package/dist/deepccc-agent/src/progress/cards-helpers.js +70 -0
  15. package/dist/deepccc-agent/src/progress/reducer.js +102 -0
  16. package/dist/deepccc-agent/src/progress/terminal-renderer.js +264 -0
  17. package/dist/deepccc-agent/src/progress/view.js +30 -0
  18. package/dist/deepccc-agent/src/raw-stream-log.js +106 -0
  19. package/dist/deepccc-agent/src/session-search.js +276 -0
  20. package/dist/deepccc-agent/src/session-select.js +23 -0
  21. package/dist/deepccc-agent/src/sigint.js +26 -0
  22. package/dist/deepccc-agent/src/skills.js +178 -0
  23. package/dist/deepccc-agent/src/web-tools.js +246 -0
  24. package/dist/src/adapters/adapter-interface.js +19 -0
  25. package/dist/src/adapters/ccc-adapter.js +112 -0
  26. package/dist/src/adapters/claude-adapter.js +497 -0
  27. package/dist/src/adapters/claude-session-meta-store.js +92 -0
  28. package/dist/src/adapters/codex-adapter.js +279 -0
  29. package/dist/src/adapters/codex-session-meta-store.js +94 -0
  30. package/dist/src/adapters/cursor-adapter.js +491 -0
  31. package/dist/src/adapters/cursor-session-meta-store.js +116 -0
  32. package/dist/src/adapters/jsonl-stream.js +104 -0
  33. package/{src/adapters/proc-tree-kill.ts → dist/src/adapters/proc-tree-kill.js} +94 -97
  34. package/dist/src/adapters/raw-stream-log.js +106 -0
  35. package/dist/src/adapters/resource-monitor.js +113 -0
  36. package/dist/src/agent-activity.js +133 -0
  37. package/dist/src/agent-delegate-task-rpc.js +129 -0
  38. package/dist/src/agent-delegate-task.js +48 -0
  39. package/dist/src/agent-file-rpc.js +152 -0
  40. package/dist/src/agent-image-rpc.js +148 -0
  41. package/dist/src/agent-platform-routing.js +13 -0
  42. package/dist/src/agent-reload-config-rpc.js +23 -0
  43. package/dist/src/agent-rpc-body.js +87 -0
  44. package/dist/src/agent-stop-stuck.js +110 -0
  45. package/dist/src/card-action-routing.js +7 -0
  46. package/dist/src/card-plain-text.js +101 -0
  47. package/dist/src/cardkit.js +158 -0
  48. package/dist/src/cards.js +573 -0
  49. package/dist/src/chatgpt-subscription-rpc.js +18 -0
  50. package/dist/src/chatgpt-subscription.js +199 -0
  51. package/dist/src/chrome-devtools-guard.js +238 -0
  52. package/dist/src/claude-sdk-installer.js +249 -0
  53. package/dist/src/codex-reset-actions.js +143 -0
  54. package/dist/src/config-utils.js +149 -0
  55. package/dist/src/config.js +804 -0
  56. package/dist/src/cursor-usage.js +77 -0
  57. package/dist/src/exit-banner.js +28 -0
  58. package/dist/src/feishu-api.js +1404 -0
  59. package/dist/src/feishu-message-ingress.js +137 -0
  60. package/dist/src/feishu-platform.js +97 -0
  61. package/dist/src/format-message.js +252 -0
  62. package/dist/src/git-command.js +155 -0
  63. package/dist/src/im-skills.js +121 -0
  64. package/dist/src/index.js +833 -0
  65. package/dist/src/litellm-proxy.js +300 -0
  66. package/dist/src/orchestrator.js +2078 -0
  67. package/dist/src/package-root.js +26 -0
  68. package/dist/src/platform-adapter.js +7 -0
  69. package/dist/src/platform-startup.js +6 -0
  70. package/dist/src/privacy.js +100 -0
  71. package/dist/src/progress/reducer.js +102 -0
  72. package/dist/src/progress/terminal-renderer.js +264 -0
  73. package/dist/src/progress/view.js +30 -0
  74. package/dist/src/response-stall.js +14 -0
  75. package/dist/src/runtime-entry.js +13 -0
  76. package/dist/src/runtime-reload.js +19 -0
  77. package/dist/src/session-chat-binding.js +183 -0
  78. package/dist/src/session-name.js +7 -0
  79. package/dist/src/session.js +2144 -0
  80. package/dist/src/shared-prefix.js +16 -0
  81. package/dist/src/shared.js +493 -0
  82. package/dist/src/sim-agent.js +105 -0
  83. package/dist/src/sim-platform.js +142 -0
  84. package/dist/src/sim-store.js +231 -0
  85. package/dist/src/simplify.js +99 -0
  86. package/dist/src/startup-lifecycle.js +209 -0
  87. package/dist/src/stream-state.js +141 -0
  88. package/dist/src/terminal-error.js +100 -0
  89. package/dist/src/trace.js +50 -0
  90. package/dist/src/turn-cards.js +92 -0
  91. package/dist/src/update-command-guard.js +114 -0
  92. package/{src/web-ui.ts → dist/src/web-ui.js} +749 -823
  93. package/dist/src/wechat-platform.js +545 -0
  94. package/package.json +7 -5
  95. package/deepccc-agent/LICENSE +0 -201
  96. package/deepccc-agent/bin/deepccc.mjs +0 -26
  97. package/deepccc-agent/docs/cache-hit-rate-1.jpg +0 -0
  98. package/deepccc-agent/docs/cache-hit-rate-2.jpg +0 -0
  99. package/deepccc-agent/package-lock.json +0 -2027
  100. package/deepccc-agent/src/__tests__/chat-session.test.ts +0 -852
  101. package/deepccc-agent/src/__tests__/cli-json.test.ts +0 -49
  102. package/deepccc-agent/src/__tests__/config.test.ts +0 -34
  103. package/deepccc-agent/src/__tests__/context.test.ts +0 -341
  104. package/deepccc-agent/src/__tests__/file-tools.test.ts +0 -240
  105. package/deepccc-agent/src/__tests__/permissions.test.ts +0 -199
  106. package/deepccc-agent/src/__tests__/privacy.test.ts +0 -318
  107. package/deepccc-agent/src/__tests__/progress-reducer.test.ts +0 -121
  108. package/deepccc-agent/src/__tests__/session-search.test.ts +0 -262
  109. package/deepccc-agent/src/__tests__/session-select.test.ts +0 -116
  110. package/deepccc-agent/src/__tests__/sigint.test.ts +0 -56
  111. package/deepccc-agent/src/__tests__/skills.test.ts +0 -284
  112. package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +0 -247
  113. package/deepccc-agent/src/__tests__/web-tools.test.ts +0 -220
  114. package/deepccc-agent/src/cli.ts +0 -682
  115. package/deepccc-agent/src/config.ts +0 -101
  116. package/deepccc-agent/src/context.ts +0 -465
  117. package/deepccc-agent/src/file-log.ts +0 -38
  118. package/deepccc-agent/src/file-tools.ts +0 -1493
  119. package/deepccc-agent/src/index.ts +0 -676
  120. package/deepccc-agent/src/permissions.ts +0 -226
  121. package/deepccc-agent/src/privacy.ts +0 -141
  122. package/deepccc-agent/src/proc-tree-kill.ts +0 -61
  123. package/deepccc-agent/src/progress/cards-helpers.ts +0 -76
  124. package/deepccc-agent/src/progress/reducer.ts +0 -113
  125. package/deepccc-agent/src/progress/terminal-renderer.ts +0 -294
  126. package/deepccc-agent/src/progress/view.ts +0 -77
  127. package/deepccc-agent/src/raw-stream-log.ts +0 -124
  128. package/deepccc-agent/src/session-search.ts +0 -370
  129. package/deepccc-agent/src/session-select.ts +0 -48
  130. package/deepccc-agent/src/sigint.ts +0 -50
  131. package/deepccc-agent/src/skills.ts +0 -205
  132. package/deepccc-agent/src/web-tools.ts +0 -313
  133. package/deepccc-agent/tsconfig.build.json +0 -13
  134. package/deepccc-agent/tsconfig.json +0 -13
  135. package/deepccc-agent/vitest.config.ts +0 -7
  136. package/src/__tests__/adapter-interface.test.ts +0 -152
  137. package/src/__tests__/agent-activity.test.ts +0 -86
  138. package/src/__tests__/agent-delegate-task-rpc.test.ts +0 -165
  139. package/src/__tests__/agent-image-rpc.test.ts +0 -34
  140. package/src/__tests__/agent-platform-routing.test.ts +0 -26
  141. package/src/__tests__/agent-reload-config-rpc.test.ts +0 -99
  142. package/src/__tests__/agent-rpc-body.test.ts +0 -42
  143. package/src/__tests__/builtin-chat-session.test.ts +0 -532
  144. package/src/__tests__/builtin-cli-json.test.ts +0 -39
  145. package/src/__tests__/builtin-config.test.ts +0 -26
  146. package/src/__tests__/builtin-context.test.ts +0 -319
  147. package/src/__tests__/builtin-file-tools.test.ts +0 -240
  148. package/src/__tests__/builtin-permissions.test.ts +0 -219
  149. package/src/__tests__/builtin-session-search.test.ts +0 -262
  150. package/src/__tests__/builtin-session-select.test.ts +0 -116
  151. package/src/__tests__/builtin-sigint.test.ts +0 -56
  152. package/src/__tests__/builtin-skills.test.ts +0 -284
  153. package/src/__tests__/builtin-web-tools.test.ts +0 -220
  154. package/src/__tests__/card-action-routing.test.ts +0 -18
  155. package/src/__tests__/card-plain-text.test.ts +0 -45
  156. package/src/__tests__/cardkit.test.ts +0 -60
  157. package/src/__tests__/cards.test.ts +0 -607
  158. package/src/__tests__/ccc-adapter.test.ts +0 -194
  159. package/src/__tests__/chatgpt-subscription-rpc.test.ts +0 -89
  160. package/src/__tests__/chatgpt-subscription.test.ts +0 -135
  161. package/src/__tests__/chrome-devtools-guard.test.ts +0 -165
  162. package/src/__tests__/claude-adapter.test.ts +0 -614
  163. package/src/__tests__/claude-raw-stream-log.test.ts +0 -96
  164. package/src/__tests__/claude-sdk-installer.test.ts +0 -285
  165. package/src/__tests__/codex-adapter.test.ts +0 -331
  166. package/src/__tests__/codex-raw-stream-log.test.ts +0 -170
  167. package/src/__tests__/codex-reset-actions.test.ts +0 -146
  168. package/src/__tests__/config-reload.test.ts +0 -284
  169. package/src/__tests__/config-sample.test.ts +0 -97
  170. package/src/__tests__/config-utils.test.ts +0 -40
  171. package/src/__tests__/config.test.ts +0 -395
  172. package/src/__tests__/crash-logging.test.ts +0 -360
  173. package/src/__tests__/cursor-adapter.test.ts +0 -890
  174. package/src/__tests__/cursor-session-meta-store.test.ts +0 -212
  175. package/src/__tests__/feishu-api.test.ts +0 -60
  176. package/src/__tests__/feishu-avatar.test.ts +0 -504
  177. package/src/__tests__/feishu-message-ingress.test.ts +0 -138
  178. package/src/__tests__/feishu-platform.test.ts +0 -75
  179. package/src/__tests__/fixtures/codex_simple_text.jsonl +0 -4
  180. package/src/__tests__/fixtures/codex_with_tool.jsonl +0 -6
  181. package/src/__tests__/fixtures/cursor_partial_only.jsonl +0 -5
  182. package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +0 -13
  183. package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +0 -12
  184. package/src/__tests__/format-message.test.ts +0 -316
  185. package/src/__tests__/git-command.test.ts +0 -288
  186. package/src/__tests__/im-skills.test.ts +0 -125
  187. package/src/__tests__/jsonl-stream.test.ts +0 -79
  188. package/src/__tests__/orchestrator.test.ts +0 -1268
  189. package/src/__tests__/package-files.test.ts +0 -24
  190. package/src/__tests__/platform-startup.test.ts +0 -19
  191. package/src/__tests__/privacy.test.ts +0 -198
  192. package/src/__tests__/proc-tree-kill.test.ts +0 -108
  193. package/src/__tests__/progress-reducer.test.ts +0 -121
  194. package/src/__tests__/raw-stream-log.test.ts +0 -106
  195. package/src/__tests__/response-stall.test.ts +0 -49
  196. package/src/__tests__/restart.test.ts +0 -232
  197. package/src/__tests__/session-ccc-config.test.ts +0 -66
  198. package/src/__tests__/session.test.ts +0 -3004
  199. package/src/__tests__/shared-prefix.test.ts +0 -36
  200. package/src/__tests__/sim-agent.test.ts +0 -174
  201. package/src/__tests__/sim-platform.test.ts +0 -93
  202. package/src/__tests__/sim-store.test.ts +0 -214
  203. package/src/__tests__/simplify.test.ts +0 -283
  204. package/src/__tests__/startup-lifecycle.test.ts +0 -231
  205. package/src/__tests__/stop-session.test.ts +0 -162
  206. package/src/__tests__/stream-state.test.ts +0 -164
  207. package/src/__tests__/terminal-error.test.ts +0 -54
  208. package/src/__tests__/terminal-renderer.test.ts +0 -247
  209. package/src/__tests__/update-command-guard.test.ts +0 -144
  210. package/src/__tests__/web-ui.test.ts +0 -438
  211. package/src/__tests__/wechat-platform.test.ts +0 -111
  212. package/src/adapters/adapter-interface.ts +0 -217
  213. package/src/adapters/ccc-adapter.ts +0 -150
  214. package/src/adapters/claude-adapter.ts +0 -673
  215. package/src/adapters/claude-session-meta-store.ts +0 -120
  216. package/src/adapters/codex-adapter.ts +0 -426
  217. package/src/adapters/codex-session-meta-store.ts +0 -131
  218. package/src/adapters/cursor-adapter.ts +0 -681
  219. package/src/adapters/cursor-session-meta-store.ts +0 -154
  220. package/src/adapters/jsonl-stream.ts +0 -157
  221. package/src/adapters/raw-stream-log.ts +0 -124
  222. package/src/adapters/resource-monitor.ts +0 -141
  223. package/src/agent-activity.ts +0 -175
  224. package/src/agent-delegate-task-rpc.ts +0 -153
  225. package/src/agent-delegate-task.ts +0 -91
  226. package/src/agent-file-rpc.ts +0 -172
  227. package/src/agent-image-rpc.ts +0 -168
  228. package/src/agent-platform-routing.ts +0 -28
  229. package/src/agent-reload-config-rpc.ts +0 -34
  230. package/src/agent-rpc-body.ts +0 -92
  231. package/src/agent-stop-stuck.ts +0 -129
  232. package/src/card-action-routing.ts +0 -14
  233. package/src/card-plain-text.ts +0 -108
  234. package/src/cardkit.ts +0 -179
  235. package/src/cards.ts +0 -684
  236. package/src/chatgpt-subscription-rpc.ts +0 -27
  237. package/src/chatgpt-subscription.ts +0 -299
  238. package/src/chrome-devtools-guard.ts +0 -318
  239. package/src/claude-sdk-installer.ts +0 -324
  240. package/src/codex-reset-actions.ts +0 -184
  241. package/src/config-utils.ts +0 -211
  242. package/src/config.ts +0 -1063
  243. package/src/cursor-usage.ts +0 -128
  244. package/src/exit-banner.ts +0 -33
  245. package/src/feishu-api.ts +0 -1616
  246. package/src/feishu-message-ingress.ts +0 -195
  247. package/src/feishu-platform.ts +0 -159
  248. package/src/format-message.ts +0 -293
  249. package/src/git-command.ts +0 -202
  250. package/src/im-skills.ts +0 -149
  251. package/src/index.ts +0 -1089
  252. package/src/litellm-proxy.ts +0 -374
  253. package/src/orchestrator.ts +0 -2543
  254. package/src/platform-adapter.ts +0 -70
  255. package/src/platform-startup.ts +0 -16
  256. package/src/privacy.ts +0 -118
  257. package/src/progress/reducer.ts +0 -113
  258. package/src/progress/terminal-renderer.ts +0 -294
  259. package/src/progress/view.ts +0 -77
  260. package/src/response-stall.ts +0 -28
  261. package/src/runtime-reload.ts +0 -34
  262. package/src/session-chat-binding.ts +0 -292
  263. package/src/session-name.ts +0 -8
  264. package/src/session.ts +0 -2659
  265. package/src/shared-prefix.ts +0 -29
  266. package/src/shared.ts +0 -552
  267. package/src/sim-agent.ts +0 -167
  268. package/src/sim-platform.ts +0 -177
  269. package/src/sim-store.ts +0 -317
  270. package/src/simplify.ts +0 -120
  271. package/src/startup-lifecycle.ts +0 -250
  272. package/src/stream-state.ts +0 -177
  273. package/src/terminal-error.ts +0 -129
  274. package/src/trace.ts +0 -51
  275. package/src/turn-cards.ts +0 -118
  276. package/src/update-command-guard.ts +0 -165
  277. package/src/wechat-platform.ts +0 -680
@@ -1,199 +0,0 @@
1
- import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync } from "node:fs";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
-
5
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
-
7
- const testHome = vi.hoisted(() => ({ dir: "" }));
8
-
9
- const aiMocks = vi.hoisted(() => ({
10
- streamText: vi.fn(),
11
- generateText: vi.fn(),
12
- }));
13
-
14
- vi.mock("node:os", async () => {
15
- const actual = await vi.importActual<typeof import("node:os")>("node:os");
16
- const [{ mkdtempSync }, { join }] = await Promise.all([import("node:fs"), import("node:path")]);
17
- testHome.dir = mkdtempSync(join(actual.tmpdir(), "chatccc-builtin-perm-"));
18
- return { ...actual, homedir: () => testHome.dir };
19
- });
20
-
21
- vi.mock("@ai-sdk/openai-compatible", () => ({
22
- createOpenAICompatible: vi.fn(() => (modelId: string) => ({ modelId })),
23
- }));
24
-
25
- vi.mock("@ai-sdk/anthropic", () => ({
26
- createAnthropic: vi.fn(() => (modelId: string) => ({ modelId })),
27
- }));
28
-
29
- vi.mock("ai", () => ({
30
- streamText: aiMocks.streamText,
31
- generateText: aiMocks.generateText,
32
- isLoopFinished: vi.fn(() => ({ loopFinished: true })),
33
- stepCountIs: vi.fn((count: number) => ({ count })),
34
- jsonSchema: vi.fn((schema: unknown) => schema),
35
- tool: vi.fn((definition: unknown) => definition),
36
- }));
37
-
38
- vi.mock("../raw-stream-log.js", () => ({
39
- createRawStreamLog: vi.fn().mockResolvedValue(null),
40
- }));
41
-
42
- import {
43
- PermissionGate,
44
- getAllowRules,
45
- isDangerousCommand,
46
- matchRule,
47
- reloadAllowRules,
48
- } from "../permissions.js";
49
- import { createBuiltinFileTools } from "../file-tools.js";
50
- import { ChatSession } from "../index.js";
51
-
52
- const streamTextMock = aiMocks.streamText;
53
- const generateTextMock = aiMocks.generateText;
54
-
55
- const ALLOW_FILE = () => join(testHome.dir, ".deepccc", "allow.json");
56
-
57
- async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
58
- const events: unknown[] = [];
59
- for await (const event of iterable) events.push(event);
60
- return events;
61
- }
62
-
63
- async function* textStream(...chunks: string[]): AsyncIterable<string> {
64
- for (const chunk of chunks) yield chunk;
65
- }
66
-
67
- function lastRunCommandExecute(): (input: { command: string }, opts?: unknown) => Promise<unknown> {
68
- const call = streamTextMock.mock.calls.at(-1)?.[0] as { tools?: Record<string, { execute?: unknown }> };
69
- const execute = call?.tools?.run_command?.execute as ((input: { command: string }, opts?: unknown) => Promise<unknown>) | undefined;
70
- if (!execute) throw new Error("streamText was not called with tools.run_command.execute");
71
- return execute;
72
- }
73
-
74
- afterEach(() => {
75
- try {
76
- rmSync(ALLOW_FILE(), { force: true });
77
- } catch {}
78
- reloadAllowRules();
79
- streamTextMock.mockReset();
80
- generateTextMock.mockReset();
81
- });
82
-
83
- beforeEach(() => {
84
- try {
85
- rmSync(join(testHome.dir, ".deepccc"), { recursive: true, force: true });
86
- } catch {}
87
- mkdirSync(join(testHome.dir, ".deepccc"), { recursive: true });
88
- reloadAllowRules();
89
- });
90
-
91
- describe("builtin permissions module", () => {
92
- it("flags destructive commands and ignores normal ones", () => {
93
- expect(isDangerousCommand("rm -rf node_modules")).toBe(true);
94
- expect(isDangerousCommand("git push --force origin main")).toBe(true);
95
- expect(isDangerousCommand("git status")).toBe(false);
96
- expect(isDangerousCommand("npm test")).toBe(false);
97
- });
98
-
99
- it("matches allow/deny rules with wildcards and relative paths", () => {
100
- expect(matchRule("run_command:git status*", "run_command:git status --short")).toBe(true);
101
- expect(matchRule("edit_file:node_modules/**", "edit_file:node_modules/a/b.js")).toBe(true);
102
- expect(matchRule("run_command:npm test", "run_command:npm test extra")).toBe(false);
103
- });
104
-
105
- it("returns empty rules when allow.json is missing", () => {
106
- expect(getAllowRules()).toEqual({ allow: [], deny: [] });
107
- });
108
-
109
- it("loads rules from ~/.deepccc/allow.json", () => {
110
- writeFileSync(ALLOW_FILE(), JSON.stringify({ deny: ["run_command:npm publish*"] }), "utf-8");
111
- reloadAllowRules();
112
- expect(getAllowRules().deny).toEqual(["run_command:npm publish*"]);
113
- });
114
- });
115
-
116
- describe("builtin PermissionGate", () => {
117
- it("bypass mode allows everything", async () => {
118
- const gate = new PermissionGate("bypass");
119
- expect(await gate.check({ tool: "run_command", action: "rm -rf /", reason: "high-risk", detail: "x" })).toBe("allow");
120
- });
121
-
122
- it("ask mode denies high-risk without resolver", async () => {
123
- const gate = new PermissionGate("ask");
124
- expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("deny");
125
- });
126
-
127
- it("ask mode allows non-high-risk operations without asking", async () => {
128
- const resolver = vi.fn();
129
- const gate = new PermissionGate("ask", resolver as never);
130
- expect(await gate.check({ tool: "run_command", action: "npm test", reason: "rule", detail: "x" })).toBe("allow");
131
- expect(await gate.check({ tool: "edit_file", action: "src/a.ts", reason: "rule", detail: "x" })).toBe("allow");
132
- expect(resolver).not.toHaveBeenCalled();
133
- });
134
-
135
- it("follows resolver answer", async () => {
136
- let answer: "allow" | "deny" = "allow";
137
- const gate = new PermissionGate("ask", async () => answer);
138
- expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("allow");
139
- answer = "deny";
140
- expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("deny");
141
- });
142
-
143
- it("deny rule wins over allow rule", async () => {
144
- writeFileSync(
145
- ALLOW_FILE(),
146
- JSON.stringify({ allow: ["run_command:rm -rf /tmp*"], deny: ["run_command:rm -rf /tmp/forbidden*"] }),
147
- "utf-8",
148
- );
149
- reloadAllowRules();
150
- const gate = new PermissionGate("ask");
151
- expect(await gate.check({ tool: "run_command", action: "rm -rf /tmp/forbidden/x", reason: "high-risk", detail: "x" })).toBe("deny");
152
- });
153
- });
154
-
155
- describe("builtin file-tools permission integration", () => {
156
- it("run_command high-risk is denied by default gate", async () => {
157
- const tools = createBuiltinFileTools(testHome.dir, { permissionGate: new PermissionGate("ask") });
158
- const tool = tools.run_command as unknown as {
159
- execute: (input: { command: string }, opts?: unknown) => Promise<unknown>;
160
- };
161
- await expect(tool.execute({ command: "rm -rf node_modules" }, { abortSignal: undefined })).rejects.toThrow(/权限拒绝/);
162
- });
163
-
164
- it("no gate keeps original behavior", async () => {
165
- const tools = createBuiltinFileTools(testHome.dir);
166
- const tool = tools.run_command as unknown as {
167
- execute: (input: { command: string }, opts?: unknown) => Promise<{ exitCode: number | null }>;
168
- };
169
- const result = await tool.execute({ command: "node -e \"console.log('ok')\"" }, { abortSignal: undefined });
170
- expect(result.exitCode).toBe(0);
171
- });
172
- });
173
-
174
- describe("builtin ChatSession permission integration", () => {
175
- it("permissionMode bypass lets high-risk run_command execute", async () => {
176
- const session = new ChatSession(
177
- { apiKey: "sk-test" },
178
- { sessionId: "perm-bypass", permissionMode: "bypass" },
179
- );
180
- streamTextMock.mockReturnValueOnce({ textStream: textStream("hi") });
181
- await collect(session.chat("hi"));
182
-
183
- const result = (await lastRunCommandExecute()({ command: "node -e \"console.log('ok')\"" }, { abortSignal: undefined })) as { exitCode: number };
184
- expect(result.exitCode).toBe(0);
185
- });
186
-
187
- it("default ask mode without resolver denies high-risk run_command", async () => {
188
- const session = new ChatSession(
189
- { apiKey: "sk-test" },
190
- { sessionId: "perm-ask" },
191
- );
192
- streamTextMock.mockReturnValueOnce({ textStream: textStream("hi") });
193
- await collect(session.chat("hi"));
194
-
195
- await expect(
196
- lastRunCommandExecute()({ command: "rm -rf node_modules" }, { abortSignal: undefined }),
197
- ).rejects.toThrow(/权限拒绝/);
198
- });
199
- });
@@ -1,318 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from "vitest";
2
- import { mkdirSync, rmSync, writeFileSync, mkdtempSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
-
6
- const privacyState = vi.hoisted(() => ({ dir: "" }));
7
-
8
- const aiMocks = vi.hoisted(() => ({
9
- streamText: vi.fn(),
10
- generateText: vi.fn(),
11
- }));
12
-
13
- vi.mock("../config.js", async () => {
14
- const actual = await vi.importActual<typeof import("../config.js")>("../config.js");
15
- const [{ mkdtempSync }, { tmpdir }, { join }] = await Promise.all([
16
- import("node:fs"),
17
- import("node:os"),
18
- import("node:path"),
19
- ]);
20
- const dir = mkdtempSync(join(tmpdir(), "deepccc-privacy-test-"));
21
- privacyState.dir = dir;
22
- return {
23
- ...actual,
24
- DEEPCCC_HOME: dir,
25
- };
26
- });
27
-
28
- vi.mock("@ai-sdk/openai-compatible", () => ({
29
- createOpenAICompatible: vi.fn(() => (modelId: string) => ({ modelId })),
30
- }));
31
-
32
- vi.mock("@ai-sdk/anthropic", () => ({
33
- createAnthropic: vi.fn(() => (modelId: string) => ({ modelId })),
34
- }));
35
-
36
- vi.mock("ai", () => ({
37
- streamText: aiMocks.streamText,
38
- generateText: aiMocks.generateText,
39
- isLoopFinished: vi.fn(() => ({ loopFinished: true })),
40
- stepCountIs: vi.fn((count: number) => ({ count })),
41
- jsonSchema: vi.fn((schema: unknown) => schema),
42
- tool: vi.fn((definition: unknown) => definition),
43
- }));
44
-
45
- vi.mock("../raw-stream-log.js", () => ({
46
- createRawStreamLog: vi.fn().mockResolvedValue(null),
47
- }));
48
-
49
- import { config } from "../config.js";
50
- import {
51
- applyPrivacy,
52
- applyPrivacyToJson,
53
- getPrivacyConfig,
54
- getPrivacyRules,
55
- reloadPrivacyRules,
56
- } from "../privacy.js";
57
- import { ChatSession } from "../index.js";
58
-
59
- const streamTextMock = aiMocks.streamText;
60
- const generateTextMock = aiMocks.generateText;
61
-
62
- const originalRawStreamLogs = structuredClone(config.rawStreamLogs);
63
- const originalStreaming = config.streaming;
64
- const PRIVACY_FILE = join(privacyState.dir, "privacy.json");
65
-
66
- function writePrivacy(content: string): void {
67
- writeFileSync(PRIVACY_FILE, content, "utf-8");
68
- }
69
-
70
- async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
71
- const events: unknown[] = [];
72
- for await (const event of iterable) events.push(event);
73
- return events;
74
- }
75
-
76
- async function* fullStream(...parts: unknown[]): AsyncIterable<unknown> {
77
- for (const part of parts) yield part;
78
- }
79
-
80
- beforeEach(() => {
81
- try {
82
- rmSync(PRIVACY_FILE, { force: true });
83
- } catch {}
84
- reloadPrivacyRules();
85
- streamTextMock.mockReset();
86
- generateTextMock.mockReset();
87
- config.rawStreamLogs = structuredClone(originalRawStreamLogs);
88
- config.streaming = true;
89
- });
90
-
91
- afterEach(() => {
92
- try {
93
- rmSync(PRIVACY_FILE, { force: true });
94
- } catch {}
95
- reloadPrivacyRules();
96
- config.streaming = originalStreaming;
97
- });
98
-
99
- afterAll(() => {
100
- try {
101
- rmSync(privacyState.dir, { recursive: true, force: true });
102
- } catch {}
103
- });
104
-
105
- describe("applyPrivacy", () => {
106
- it("returns original text when privacy.json is missing", () => {
107
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
108
- });
109
-
110
- it("supports legacy flat privacy rules", () => {
111
- writePrivacy(JSON.stringify({ weizhangjian: "wzj", secret: "***" }));
112
- reloadPrivacyRules();
113
-
114
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
115
- expect(applyPrivacy("my secret is safe")).toBe("my *** is safe");
116
- expect(applyPrivacy("weizhangjian and secret")).toBe("wzj and ***");
117
- });
118
-
119
- it("supports privacy.json schema with enabled=false", () => {
120
- writePrivacy(JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } }));
121
- reloadPrivacyRules();
122
-
123
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
124
- expect(getPrivacyRules()).toEqual({ weizhangjian: "wzj" });
125
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
126
- });
127
-
128
- it("supports privacy.json schema with enabled=true", () => {
129
- writePrivacy(JSON.stringify({ enabled: true, rules: { weizhangjian: "wzj" } }));
130
- reloadPrivacyRules();
131
-
132
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
133
- });
134
-
135
- it("accepts UTF-8 BOM in privacy.json", () => {
136
- writePrivacy(`\uFEFF${JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } })}`);
137
- reloadPrivacyRules();
138
-
139
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
140
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
141
- });
142
-
143
- it("auto reloads privacy.json changes without explicit reload", () => {
144
- writePrivacy(JSON.stringify({ weizhangjian: "wzj" }));
145
- reloadPrivacyRules();
146
-
147
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
148
-
149
- writePrivacy(JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj-disabled" } }));
150
-
151
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
152
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj-disabled" } });
153
- });
154
-
155
- it("replaces multiple rules and repeated occurrences", () => {
156
- writePrivacy(JSON.stringify({ a: "A", b: "B" }));
157
- reloadPrivacyRules();
158
-
159
- expect(applyPrivacy("a b a b")).toBe("A B A B");
160
- });
161
-
162
- it("returns empty text directly", () => {
163
- writePrivacy(JSON.stringify({ x: "y" }));
164
- reloadPrivacyRules();
165
-
166
- expect(applyPrivacy("")).toBe("");
167
- });
168
-
169
- it("treats special characters in rule keys literally", () => {
170
- writePrivacy(JSON.stringify({ "a.b": "X", "(test)": "Y", "*star": "Z" }));
171
- reloadPrivacyRules();
172
-
173
- expect(applyPrivacy("hello a.b world")).toBe("hello X world");
174
- expect(applyPrivacy("text (test) here")).toBe("text Y here");
175
- expect(applyPrivacy("a *star shines")).toBe("a Z shines");
176
- });
177
-
178
- it("reloadPrivacyRules forces a reload", () => {
179
- writePrivacy(JSON.stringify({ old: "OLD" }));
180
- reloadPrivacyRules();
181
-
182
- expect(applyPrivacy("old")).toBe("OLD");
183
- expect(getPrivacyRules()).toEqual({ old: "OLD" });
184
-
185
- writePrivacy(JSON.stringify({ new: "NEW" }));
186
- reloadPrivacyRules();
187
-
188
- expect(applyPrivacy("new")).toBe("NEW");
189
- expect(getPrivacyRules()).toEqual({ new: "NEW" });
190
- });
191
-
192
- it("returns original text for malformed JSON", () => {
193
- writePrivacy("not json");
194
- reloadPrivacyRules();
195
-
196
- expect(applyPrivacy("hello")).toBe("hello");
197
- });
198
-
199
- it("returns original text for array JSON", () => {
200
- writePrivacy(JSON.stringify(["a", "b"]));
201
- reloadPrivacyRules();
202
-
203
- expect(applyPrivacy("hello")).toBe("hello");
204
- });
205
- });
206
-
207
- describe("applyPrivacyToJson", () => {
208
- it("recursively replaces string fields in nested objects and arrays", () => {
209
- writePrivacy(JSON.stringify({ weizhangjian: "wzj" }));
210
- reloadPrivacyRules();
211
-
212
- expect(applyPrivacyToJson({ path: "C:\\Users\\weizhangjian\\repo", tags: ["a-weizhangjian"] })).toEqual({
213
- path: "C:\\Users\\wzj\\repo",
214
- tags: ["a-wzj"],
215
- });
216
- });
217
-
218
- it("leaves non-string values unchanged", () => {
219
- writePrivacy(JSON.stringify({ weizhangjian: "wzj" }));
220
- reloadPrivacyRules();
221
-
222
- expect(applyPrivacyToJson({ n: 1, ok: true, nil: null, obj: { n: 2 } })).toEqual({
223
- n: 1,
224
- ok: true,
225
- nil: null,
226
- obj: { n: 2 },
227
- });
228
- });
229
-
230
- it("returns value unchanged when privacy is disabled", () => {
231
- writePrivacy(JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } }));
232
- reloadPrivacyRules();
233
-
234
- expect(applyPrivacyToJson({ path: "C:\\Users\\weizhangjian" })).toEqual({ path: "C:\\Users\\weizhangjian" });
235
- });
236
- });
237
-
238
- describe("ChatSession privacy integration", () => {
239
- it("replaces privacy tokens in text, done, tool_use and tool_result events", async () => {
240
- writePrivacy(JSON.stringify({ enabled: true, rules: { weizhangjian: "wzj" } }));
241
- reloadPrivacyRules();
242
-
243
- const session = new ChatSession(
244
- { apiKey: "sk-test" },
245
- { sessionId: "privacy-integration" },
246
- );
247
-
248
- streamTextMock.mockReturnValueOnce({
249
- fullStream: fullStream(
250
- { type: "tool-call", toolCallId: "call-1", toolName: "read_file", input: { path: "C:\\Users\\weizhangjian\\repo\\a.txt" } },
251
- { type: "tool-result", toolCallId: "call-1", toolName: "read_file", output: { content: "C:\\Users\\weizhangjian\\repo\\a.txt" } },
252
- { type: "text-delta", text: "see " },
253
- { type: "text-delta", text: "C:\\Users\\weizhangjian\\repo" },
254
- ),
255
- });
256
-
257
- const events = await collect(session.chat("read the file"));
258
-
259
- expect(events).toContainEqual({
260
- type: "tool_use",
261
- id: "call-1",
262
- name: "read_file",
263
- input: { path: "C:\\Users\\wzj\\repo\\a.txt" },
264
- });
265
- expect(events).toContainEqual({
266
- type: "tool_result",
267
- tool_use_id: "call-1",
268
- name: "read_file",
269
- content: { content: "C:\\Users\\wzj\\repo\\a.txt" },
270
- is_error: false,
271
- });
272
- expect(events).toContainEqual({
273
- type: "text",
274
- text: "see ",
275
- accumulated: "see ",
276
- });
277
- expect(events).toContainEqual({
278
- type: "text",
279
- text: "C:\\Users\\wzj\\repo",
280
- accumulated: "see C:\\Users\\wzj\\repo",
281
- });
282
- expect(events).toContainEqual({
283
- type: "done",
284
- text: "see C:\\Users\\wzj\\repo",
285
- });
286
- });
287
-
288
- it("keeps the original text in persisted history (display-only replacement)", async () => {
289
- writePrivacy(JSON.stringify({ enabled: true, rules: { weizhangjian: "wzj" } }));
290
- reloadPrivacyRules();
291
-
292
- const session = new ChatSession(
293
- { apiKey: "sk-test" },
294
- { persist: true, sessionId: "privacy-history" },
295
- );
296
- streamTextMock.mockReturnValueOnce({ textStream: (async function* () { yield "C:\\Users\\weizhangjian\\repo"; })() });
297
-
298
- await collect(session.chat("hi"));
299
-
300
- expect(streamTextMock).toHaveBeenCalledOnce();
301
- expect(session.history.map((m) => m.content).join("\n")).toContain("C:\\Users\\weizhangjian\\repo");
302
- });
303
-
304
- it("does not replace text when privacy is disabled", async () => {
305
- writePrivacy(JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } }));
306
- reloadPrivacyRules();
307
-
308
- const session = new ChatSession(
309
- { apiKey: "sk-test" },
310
- { sessionId: "privacy-disabled" },
311
- );
312
- streamTextMock.mockReturnValueOnce({ textStream: (async function* () { yield "C:\\Users\\weizhangjian\\repo"; })() });
313
-
314
- const events = await collect(session.chat("hi"));
315
-
316
- expect(events).toContainEqual({ type: "done", text: "C:\\Users\\weizhangjian\\repo" });
317
- });
318
- });
@@ -1,121 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import type { ChatEvent } from "../index.js";
4
- import { reduceProgress, summarizeToolInput, summarizeToolResult } from "../progress/reducer.js";
5
- import { progressView } from "../progress/view.js";
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
- });