chatccc 0.2.252 → 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 (280) 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/os-prompts/darwin.md +8 -8
  5. package/deepccc-agent/os-prompts/linux.md +8 -8
  6. package/deepccc-agent/os-prompts/win32.md +11 -11
  7. package/deepccc-agent/package.json +65 -65
  8. package/dist/deepccc-agent/src/cli.js +634 -0
  9. package/dist/deepccc-agent/src/config.js +76 -0
  10. package/dist/deepccc-agent/src/context.js +348 -0
  11. package/dist/deepccc-agent/src/file-log.js +34 -0
  12. package/dist/deepccc-agent/src/file-tools.js +1208 -0
  13. package/dist/deepccc-agent/src/index.js +571 -0
  14. package/dist/deepccc-agent/src/permissions.js +170 -0
  15. package/dist/deepccc-agent/src/privacy.js +124 -0
  16. package/dist/deepccc-agent/src/proc-tree-kill.js +60 -0
  17. package/dist/deepccc-agent/src/progress/cards-helpers.js +70 -0
  18. package/dist/deepccc-agent/src/progress/reducer.js +102 -0
  19. package/dist/deepccc-agent/src/progress/terminal-renderer.js +264 -0
  20. package/dist/deepccc-agent/src/progress/view.js +30 -0
  21. package/dist/deepccc-agent/src/raw-stream-log.js +106 -0
  22. package/dist/deepccc-agent/src/session-search.js +276 -0
  23. package/dist/deepccc-agent/src/session-select.js +23 -0
  24. package/dist/deepccc-agent/src/sigint.js +26 -0
  25. package/dist/deepccc-agent/src/skills.js +178 -0
  26. package/dist/deepccc-agent/src/web-tools.js +246 -0
  27. package/dist/src/adapters/adapter-interface.js +19 -0
  28. package/dist/src/adapters/ccc-adapter.js +112 -0
  29. package/dist/src/adapters/claude-adapter.js +497 -0
  30. package/dist/src/adapters/claude-session-meta-store.js +92 -0
  31. package/dist/src/adapters/codex-adapter.js +279 -0
  32. package/dist/src/adapters/codex-session-meta-store.js +94 -0
  33. package/dist/src/adapters/cursor-adapter.js +491 -0
  34. package/dist/src/adapters/cursor-session-meta-store.js +116 -0
  35. package/dist/src/adapters/jsonl-stream.js +104 -0
  36. package/{src/adapters/proc-tree-kill.ts → dist/src/adapters/proc-tree-kill.js} +94 -97
  37. package/dist/src/adapters/raw-stream-log.js +106 -0
  38. package/dist/src/adapters/resource-monitor.js +113 -0
  39. package/dist/src/agent-activity.js +133 -0
  40. package/dist/src/agent-delegate-task-rpc.js +129 -0
  41. package/dist/src/agent-delegate-task.js +48 -0
  42. package/dist/src/agent-file-rpc.js +152 -0
  43. package/dist/src/agent-image-rpc.js +148 -0
  44. package/dist/src/agent-platform-routing.js +13 -0
  45. package/dist/src/agent-reload-config-rpc.js +23 -0
  46. package/dist/src/agent-rpc-body.js +87 -0
  47. package/dist/src/agent-stop-stuck.js +110 -0
  48. package/dist/src/card-action-routing.js +7 -0
  49. package/dist/src/card-plain-text.js +101 -0
  50. package/dist/src/cardkit.js +158 -0
  51. package/dist/src/cards.js +573 -0
  52. package/dist/src/chatgpt-subscription-rpc.js +18 -0
  53. package/dist/src/chatgpt-subscription.js +199 -0
  54. package/dist/src/chrome-devtools-guard.js +238 -0
  55. package/dist/src/claude-sdk-installer.js +249 -0
  56. package/dist/src/codex-reset-actions.js +143 -0
  57. package/dist/src/config-utils.js +149 -0
  58. package/dist/src/config.js +804 -0
  59. package/dist/src/cursor-usage.js +77 -0
  60. package/dist/src/exit-banner.js +28 -0
  61. package/dist/src/feishu-api.js +1404 -0
  62. package/dist/src/feishu-message-ingress.js +137 -0
  63. package/dist/src/feishu-platform.js +97 -0
  64. package/dist/src/format-message.js +252 -0
  65. package/dist/src/git-command.js +155 -0
  66. package/dist/src/im-skills.js +121 -0
  67. package/dist/src/index.js +833 -0
  68. package/dist/src/litellm-proxy.js +300 -0
  69. package/dist/src/orchestrator.js +2078 -0
  70. package/dist/src/package-root.js +26 -0
  71. package/dist/src/platform-adapter.js +7 -0
  72. package/dist/src/platform-startup.js +6 -0
  73. package/dist/src/privacy.js +100 -0
  74. package/dist/src/progress/reducer.js +102 -0
  75. package/dist/src/progress/terminal-renderer.js +264 -0
  76. package/dist/src/progress/view.js +30 -0
  77. package/dist/src/response-stall.js +14 -0
  78. package/dist/src/runtime-entry.js +13 -0
  79. package/dist/src/runtime-reload.js +19 -0
  80. package/dist/src/session-chat-binding.js +183 -0
  81. package/dist/src/session-name.js +7 -0
  82. package/dist/src/session.js +2144 -0
  83. package/dist/src/shared-prefix.js +16 -0
  84. package/dist/src/shared.js +493 -0
  85. package/dist/src/sim-agent.js +105 -0
  86. package/dist/src/sim-platform.js +142 -0
  87. package/dist/src/sim-store.js +231 -0
  88. package/dist/src/simplify.js +99 -0
  89. package/dist/src/startup-lifecycle.js +209 -0
  90. package/dist/src/stream-state.js +141 -0
  91. package/dist/src/terminal-error.js +100 -0
  92. package/dist/src/trace.js +50 -0
  93. package/dist/src/turn-cards.js +92 -0
  94. package/dist/src/update-command-guard.js +114 -0
  95. package/{src/web-ui.ts → dist/src/web-ui.js} +749 -823
  96. package/dist/src/wechat-platform.js +545 -0
  97. package/package.json +76 -74
  98. package/deepccc-agent/LICENSE +0 -201
  99. package/deepccc-agent/bin/deepccc.mjs +0 -26
  100. package/deepccc-agent/docs/cache-hit-rate-1.jpg +0 -0
  101. package/deepccc-agent/docs/cache-hit-rate-2.jpg +0 -0
  102. package/deepccc-agent/package-lock.json +0 -2027
  103. package/deepccc-agent/src/__tests__/chat-session.test.ts +0 -877
  104. package/deepccc-agent/src/__tests__/cli-json.test.ts +0 -49
  105. package/deepccc-agent/src/__tests__/config.test.ts +0 -34
  106. package/deepccc-agent/src/__tests__/context.test.ts +0 -341
  107. package/deepccc-agent/src/__tests__/file-tools.test.ts +0 -240
  108. package/deepccc-agent/src/__tests__/permissions.test.ts +0 -199
  109. package/deepccc-agent/src/__tests__/privacy.test.ts +0 -318
  110. package/deepccc-agent/src/__tests__/progress-reducer.test.ts +0 -121
  111. package/deepccc-agent/src/__tests__/session-search.test.ts +0 -262
  112. package/deepccc-agent/src/__tests__/session-select.test.ts +0 -116
  113. package/deepccc-agent/src/__tests__/sigint.test.ts +0 -56
  114. package/deepccc-agent/src/__tests__/skills.test.ts +0 -284
  115. package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +0 -247
  116. package/deepccc-agent/src/__tests__/web-tools.test.ts +0 -220
  117. package/deepccc-agent/src/cli.ts +0 -682
  118. package/deepccc-agent/src/config.ts +0 -101
  119. package/deepccc-agent/src/context.ts +0 -465
  120. package/deepccc-agent/src/file-log.ts +0 -38
  121. package/deepccc-agent/src/file-tools.ts +0 -1493
  122. package/deepccc-agent/src/index.ts +0 -710
  123. package/deepccc-agent/src/permissions.ts +0 -226
  124. package/deepccc-agent/src/privacy.ts +0 -141
  125. package/deepccc-agent/src/proc-tree-kill.ts +0 -61
  126. package/deepccc-agent/src/progress/cards-helpers.ts +0 -76
  127. package/deepccc-agent/src/progress/reducer.ts +0 -113
  128. package/deepccc-agent/src/progress/terminal-renderer.ts +0 -294
  129. package/deepccc-agent/src/progress/view.ts +0 -77
  130. package/deepccc-agent/src/raw-stream-log.ts +0 -124
  131. package/deepccc-agent/src/session-search.ts +0 -370
  132. package/deepccc-agent/src/session-select.ts +0 -48
  133. package/deepccc-agent/src/sigint.ts +0 -50
  134. package/deepccc-agent/src/skills.ts +0 -205
  135. package/deepccc-agent/src/web-tools.ts +0 -313
  136. package/deepccc-agent/tsconfig.build.json +0 -13
  137. package/deepccc-agent/tsconfig.json +0 -13
  138. package/deepccc-agent/vitest.config.ts +0 -7
  139. package/src/__tests__/adapter-interface.test.ts +0 -152
  140. package/src/__tests__/agent-activity.test.ts +0 -86
  141. package/src/__tests__/agent-delegate-task-rpc.test.ts +0 -165
  142. package/src/__tests__/agent-image-rpc.test.ts +0 -34
  143. package/src/__tests__/agent-platform-routing.test.ts +0 -26
  144. package/src/__tests__/agent-reload-config-rpc.test.ts +0 -99
  145. package/src/__tests__/agent-rpc-body.test.ts +0 -42
  146. package/src/__tests__/builtin-chat-session.test.ts +0 -532
  147. package/src/__tests__/builtin-cli-json.test.ts +0 -39
  148. package/src/__tests__/builtin-config.test.ts +0 -26
  149. package/src/__tests__/builtin-context.test.ts +0 -319
  150. package/src/__tests__/builtin-file-tools.test.ts +0 -240
  151. package/src/__tests__/builtin-permissions.test.ts +0 -219
  152. package/src/__tests__/builtin-session-search.test.ts +0 -262
  153. package/src/__tests__/builtin-session-select.test.ts +0 -116
  154. package/src/__tests__/builtin-sigint.test.ts +0 -56
  155. package/src/__tests__/builtin-skills.test.ts +0 -284
  156. package/src/__tests__/builtin-web-tools.test.ts +0 -220
  157. package/src/__tests__/card-action-routing.test.ts +0 -18
  158. package/src/__tests__/card-plain-text.test.ts +0 -45
  159. package/src/__tests__/cardkit.test.ts +0 -60
  160. package/src/__tests__/cards.test.ts +0 -607
  161. package/src/__tests__/ccc-adapter.test.ts +0 -194
  162. package/src/__tests__/chatgpt-subscription-rpc.test.ts +0 -89
  163. package/src/__tests__/chatgpt-subscription.test.ts +0 -135
  164. package/src/__tests__/chrome-devtools-guard.test.ts +0 -165
  165. package/src/__tests__/claude-adapter.test.ts +0 -614
  166. package/src/__tests__/claude-raw-stream-log.test.ts +0 -96
  167. package/src/__tests__/claude-sdk-installer.test.ts +0 -285
  168. package/src/__tests__/codex-adapter.test.ts +0 -331
  169. package/src/__tests__/codex-raw-stream-log.test.ts +0 -170
  170. package/src/__tests__/codex-reset-actions.test.ts +0 -146
  171. package/src/__tests__/config-reload.test.ts +0 -284
  172. package/src/__tests__/config-sample.test.ts +0 -97
  173. package/src/__tests__/config-utils.test.ts +0 -40
  174. package/src/__tests__/config.test.ts +0 -395
  175. package/src/__tests__/crash-logging.test.ts +0 -360
  176. package/src/__tests__/cursor-adapter.test.ts +0 -890
  177. package/src/__tests__/cursor-session-meta-store.test.ts +0 -212
  178. package/src/__tests__/feishu-api.test.ts +0 -60
  179. package/src/__tests__/feishu-avatar.test.ts +0 -504
  180. package/src/__tests__/feishu-message-ingress.test.ts +0 -138
  181. package/src/__tests__/feishu-platform.test.ts +0 -75
  182. package/src/__tests__/fixtures/codex_simple_text.jsonl +0 -4
  183. package/src/__tests__/fixtures/codex_with_tool.jsonl +0 -6
  184. package/src/__tests__/fixtures/cursor_partial_only.jsonl +0 -5
  185. package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +0 -13
  186. package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +0 -12
  187. package/src/__tests__/format-message.test.ts +0 -316
  188. package/src/__tests__/git-command.test.ts +0 -288
  189. package/src/__tests__/im-skills.test.ts +0 -125
  190. package/src/__tests__/jsonl-stream.test.ts +0 -79
  191. package/src/__tests__/orchestrator.test.ts +0 -1268
  192. package/src/__tests__/package-files.test.ts +0 -24
  193. package/src/__tests__/platform-startup.test.ts +0 -19
  194. package/src/__tests__/privacy.test.ts +0 -198
  195. package/src/__tests__/proc-tree-kill.test.ts +0 -108
  196. package/src/__tests__/progress-reducer.test.ts +0 -121
  197. package/src/__tests__/raw-stream-log.test.ts +0 -106
  198. package/src/__tests__/response-stall.test.ts +0 -49
  199. package/src/__tests__/restart.test.ts +0 -232
  200. package/src/__tests__/session-ccc-config.test.ts +0 -66
  201. package/src/__tests__/session.test.ts +0 -3004
  202. package/src/__tests__/shared-prefix.test.ts +0 -36
  203. package/src/__tests__/sim-agent.test.ts +0 -174
  204. package/src/__tests__/sim-platform.test.ts +0 -93
  205. package/src/__tests__/sim-store.test.ts +0 -214
  206. package/src/__tests__/simplify.test.ts +0 -283
  207. package/src/__tests__/startup-lifecycle.test.ts +0 -231
  208. package/src/__tests__/stop-session.test.ts +0 -162
  209. package/src/__tests__/stream-state.test.ts +0 -164
  210. package/src/__tests__/terminal-error.test.ts +0 -54
  211. package/src/__tests__/terminal-renderer.test.ts +0 -247
  212. package/src/__tests__/update-command-guard.test.ts +0 -144
  213. package/src/__tests__/web-ui.test.ts +0 -438
  214. package/src/__tests__/wechat-platform.test.ts +0 -111
  215. package/src/adapters/adapter-interface.ts +0 -217
  216. package/src/adapters/ccc-adapter.ts +0 -150
  217. package/src/adapters/claude-adapter.ts +0 -673
  218. package/src/adapters/claude-session-meta-store.ts +0 -120
  219. package/src/adapters/codex-adapter.ts +0 -426
  220. package/src/adapters/codex-session-meta-store.ts +0 -131
  221. package/src/adapters/cursor-adapter.ts +0 -681
  222. package/src/adapters/cursor-session-meta-store.ts +0 -154
  223. package/src/adapters/jsonl-stream.ts +0 -157
  224. package/src/adapters/raw-stream-log.ts +0 -124
  225. package/src/adapters/resource-monitor.ts +0 -141
  226. package/src/agent-activity.ts +0 -175
  227. package/src/agent-delegate-task-rpc.ts +0 -153
  228. package/src/agent-delegate-task.ts +0 -91
  229. package/src/agent-file-rpc.ts +0 -172
  230. package/src/agent-image-rpc.ts +0 -168
  231. package/src/agent-platform-routing.ts +0 -28
  232. package/src/agent-reload-config-rpc.ts +0 -34
  233. package/src/agent-rpc-body.ts +0 -92
  234. package/src/agent-stop-stuck.ts +0 -129
  235. package/src/card-action-routing.ts +0 -14
  236. package/src/card-plain-text.ts +0 -108
  237. package/src/cardkit.ts +0 -179
  238. package/src/cards.ts +0 -684
  239. package/src/chatgpt-subscription-rpc.ts +0 -27
  240. package/src/chatgpt-subscription.ts +0 -299
  241. package/src/chrome-devtools-guard.ts +0 -318
  242. package/src/claude-sdk-installer.ts +0 -324
  243. package/src/codex-reset-actions.ts +0 -184
  244. package/src/config-utils.ts +0 -211
  245. package/src/config.ts +0 -1063
  246. package/src/cursor-usage.ts +0 -128
  247. package/src/exit-banner.ts +0 -33
  248. package/src/feishu-api.ts +0 -1616
  249. package/src/feishu-message-ingress.ts +0 -195
  250. package/src/feishu-platform.ts +0 -159
  251. package/src/format-message.ts +0 -293
  252. package/src/git-command.ts +0 -202
  253. package/src/im-skills.ts +0 -149
  254. package/src/index.ts +0 -1089
  255. package/src/litellm-proxy.ts +0 -374
  256. package/src/orchestrator.ts +0 -2543
  257. package/src/platform-adapter.ts +0 -70
  258. package/src/platform-startup.ts +0 -16
  259. package/src/privacy.ts +0 -118
  260. package/src/progress/reducer.ts +0 -113
  261. package/src/progress/terminal-renderer.ts +0 -294
  262. package/src/progress/view.ts +0 -77
  263. package/src/response-stall.ts +0 -28
  264. package/src/runtime-reload.ts +0 -34
  265. package/src/session-chat-binding.ts +0 -292
  266. package/src/session-name.ts +0 -8
  267. package/src/session.ts +0 -2659
  268. package/src/shared-prefix.ts +0 -29
  269. package/src/shared.ts +0 -552
  270. package/src/sim-agent.ts +0 -167
  271. package/src/sim-platform.ts +0 -177
  272. package/src/sim-store.ts +0 -317
  273. package/src/simplify.ts +0 -120
  274. package/src/startup-lifecycle.ts +0 -250
  275. package/src/stream-state.ts +0 -177
  276. package/src/terminal-error.ts +0 -129
  277. package/src/trace.ts +0 -51
  278. package/src/turn-cards.ts +0 -118
  279. package/src/update-command-guard.ts +0 -165
  280. package/src/wechat-platform.ts +0 -680
@@ -1,331 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { readFileSync } from "node:fs";
3
- import { join, dirname } from "node:path";
4
- import { fileURLToPath } from "node:url";
5
- import {
6
- buildCodexInvocationArgs,
7
- normalizeCodexMessage,
8
- createCodexAdapter,
9
- type CreateCodexAdapterOptions,
10
- } from "../adapters/codex-adapter.ts";
11
- import type { UnifiedStreamMessage } from "../adapters/adapter-interface.ts";
12
- import {
13
- type CodexSessionMeta,
14
- type CodexSessionMetaStore,
15
- } from "../adapters/codex-session-meta-store.ts";
16
- import { accumulateBlockContent, pickFinalReply, type AccumulatorState } from "../session.ts";
17
-
18
- const __dirname = dirname(fileURLToPath(import.meta.url));
19
-
20
- describe("buildCodexInvocationArgs", () => {
21
- it("forces the priority service tier when Fast mode is on", () => {
22
- expect(buildCodexInvocationArgs(["exec", "--json"], "", "", true)).toContain(
23
- 'service_tier="fast"',
24
- );
25
- });
26
-
27
- it("forces the standard service tier when Fast mode is off", () => {
28
- const args = buildCodexInvocationArgs(["exec", "--json"], "", "", false);
29
- expect(args).toContain('service_tier="default"');
30
- expect(args).not.toContain('service_tier="fast"');
31
- });
32
- });
33
-
34
- function readFixture(name: string): unknown[] {
35
- const raw = readFileSync(join(__dirname, "fixtures", name), "utf-8");
36
- return raw
37
- .split(/\r?\n/)
38
- .filter(Boolean)
39
- .map((line) => JSON.parse(line));
40
- }
41
-
42
- // 进程内内存版 meta store
43
- function createInMemoryMetaStore(
44
- initial: Record<string, CodexSessionMeta> = {},
45
- ): CodexSessionMetaStore & { snapshot(): Record<string, CodexSessionMeta> } {
46
- const map = new Map<string, CodexSessionMeta>(Object.entries(initial));
47
- return {
48
- async get(sid) {
49
- return map.get(sid);
50
- },
51
- async set(sid, partial) {
52
- const existing = map.get(sid) ?? { cwd: "" };
53
- const merged: CodexSessionMeta = { ...existing, ...partial };
54
- if (typeof merged.cwd !== "string" || merged.cwd.length === 0) return;
55
- map.set(sid, merged);
56
- },
57
- async setThreadId(sid, threadId) {
58
- const existing = map.get(sid);
59
- if (existing) {
60
- map.set(sid, { ...existing, threadId });
61
- }
62
- },
63
- snapshot() {
64
- return Object.fromEntries(map);
65
- },
66
- };
67
- }
68
-
69
- // ---------------------------------------------------------------------------
70
- // normalizeCodexMessage — 核心映射逻辑测试(纯函数)
71
- // ---------------------------------------------------------------------------
72
-
73
- describe("normalizeCodexMessage", () => {
74
- it("normalizes agent_message into assistant text block", () => {
75
- const result = normalizeCodexMessage({
76
- type: "item.completed",
77
- item: { id: "item_0", type: "agent_message", text: "hello" },
78
- });
79
- expect(result).not.toBeNull();
80
- expect(result!.type).toBe("assistant");
81
- expect(result!.blocks).toEqual([{ type: "text", text: "hello" }]);
82
- expect(result!.isFinalResponse).toBeUndefined();
83
- });
84
-
85
- it("normalizes command_execution start into tool_use block", () => {
86
- const result = normalizeCodexMessage({
87
- type: "item.started",
88
- item: {
89
- id: "item_0",
90
- type: "command_execution",
91
- command: "powershell.exe -Command ls",
92
- status: "in_progress",
93
- },
94
- });
95
- expect(result).not.toBeNull();
96
- expect(result!.type).toBe("assistant");
97
- expect(result!.blocks).toEqual([
98
- { type: "tool_use", id: "item_0", name: "Bash", input: { command: "powershell.exe -Command ls" } },
99
- ]);
100
- });
101
-
102
- it("normalizes command_execution completion as tool_result (success)", () => {
103
- const result = normalizeCodexMessage({
104
- type: "item.completed",
105
- item: {
106
- id: "item_0",
107
- type: "command_execution",
108
- command: "ls",
109
- aggregated_output: "file1\nfile2\n",
110
- exit_code: 0,
111
- status: "completed",
112
- },
113
- });
114
- expect(result).not.toBeNull();
115
- expect(result!.blocks).toEqual([
116
- {
117
- type: "tool_result",
118
- tool_use_id: "item_0",
119
- content: "file1\nfile2\n",
120
- is_error: undefined,
121
- },
122
- ]);
123
- });
124
-
125
- it("normalizes command_execution completion as tool_result (error)", () => {
126
- const result = normalizeCodexMessage({
127
- type: "item.completed",
128
- item: {
129
- id: "item_err",
130
- type: "command_execution",
131
- command: "nonexistent",
132
- aggregated_output: "command not found",
133
- exit_code: 127,
134
- status: "completed",
135
- },
136
- });
137
- expect(result).not.toBeNull();
138
- expect(result!.blocks).toEqual([
139
- {
140
- type: "tool_result",
141
- tool_use_id: "item_err",
142
- content: "command not found",
143
- is_error: true,
144
- },
145
- ]);
146
- });
147
-
148
- it("returns null for thread.started events", () => {
149
- expect(
150
- normalizeCodexMessage({
151
- type: "thread.started",
152
- thread_id: "abc-123",
153
- }),
154
- ).toBeNull();
155
- });
156
-
157
- it("returns null for turn.started events", () => {
158
- expect(normalizeCodexMessage({ type: "turn.started" })).toBeNull();
159
- });
160
-
161
- it("marks turn.completed as the authoritative final response", () => {
162
- expect(
163
- normalizeCodexMessage({
164
- type: "turn.completed",
165
- usage: { input_tokens: 100, output_tokens: 50 },
166
- }),
167
- ).toEqual({
168
- type: "assistant",
169
- blocks: [],
170
- isFinalResponse: true,
171
- });
172
- });
173
-
174
- it("returns null for unknown event types", () => {
175
- expect(normalizeCodexMessage({ type: "unknown" })).toBeNull();
176
- expect(normalizeCodexMessage({} as Parameters<typeof normalizeCodexMessage>[0])).toBeNull();
177
- });
178
-
179
- it("returns null for agent_message with empty text", () => {
180
- const result = normalizeCodexMessage({
181
- type: "item.completed",
182
- item: { id: "item_0", type: "agent_message", text: "" },
183
- });
184
- expect(result).toBeNull();
185
- });
186
-
187
- it("returns null for command_execution start without command text", () => {
188
- const result = normalizeCodexMessage({
189
- type: "item.started",
190
- item: { id: "item_0", type: "command_execution", status: "in_progress" },
191
- });
192
- expect(result).toBeNull();
193
- });
194
- });
195
-
196
- // ---------------------------------------------------------------------------
197
- // Fixture 端到端测试
198
- // ---------------------------------------------------------------------------
199
-
200
- describe("Codex stream fixtures", () => {
201
- it("simple text: 流结束后 pickFinalReply 返回正确文本", () => {
202
- const lines = readFixture("codex_simple_text.jsonl");
203
- const state: AccumulatorState = {
204
- accumulatedContent: "",
205
- finalText: "",
206
- finalCompleteText: "",
207
- chunkCount: 0,
208
- };
209
- for (const raw of lines) {
210
- const normalized = normalizeCodexMessage(
211
- raw as Parameters<typeof normalizeCodexMessage>[0],
212
- );
213
- if (!normalized) continue;
214
- for (const block of normalized.blocks) {
215
- accumulateBlockContent(block, state);
216
- }
217
- }
218
-
219
- expect(pickFinalReply(state)).toBe("hello");
220
- });
221
-
222
- it("with tool: 流结束后 pickFinalReply 返回最终文本(不含工具输出在 finalText 中)", () => {
223
- const lines = readFixture("codex_with_tool.jsonl");
224
- const state: AccumulatorState = {
225
- accumulatedContent: "",
226
- finalText: "",
227
- finalCompleteText: "",
228
- chunkCount: 0,
229
- };
230
- for (const raw of lines) {
231
- const normalized = normalizeCodexMessage(
232
- raw as Parameters<typeof normalizeCodexMessage>[0],
233
- );
234
- if (!normalized) continue;
235
- for (const block of normalized.blocks) {
236
- accumulateBlockContent(block, state);
237
- }
238
- }
239
-
240
- // finalText 应该只包含最终的 agent_message
241
- expect(pickFinalReply(state)).toBe("tool_test");
242
- // accumulatedContent 包含工具调用信息
243
- expect(state.accumulatedContent).toContain("Bash");
244
- expect(state.accumulatedContent).toContain("tool_test");
245
- });
246
-
247
- it("with tool: 普通输出不标终态,只有 turn.completed 标记终态", () => {
248
- const lines = readFixture("codex_with_tool.jsonl");
249
- const messages: UnifiedStreamMessage[] = [];
250
- for (const raw of lines) {
251
- const normalized = normalizeCodexMessage(
252
- raw as Parameters<typeof normalizeCodexMessage>[0],
253
- );
254
- if (normalized) messages.push(normalized);
255
- }
256
-
257
- // 应有: tool_use + tool_result + text + turn.completed = 4 条消息
258
- expect(messages.length).toBe(4);
259
- expect(messages[0].blocks[0].type).toBe("tool_use");
260
- expect(messages[1].blocks[0].type).toBe("tool_result");
261
- expect(messages[2].blocks[0].type).toBe("text");
262
- expect(messages[2].isFinalResponse).toBeUndefined();
263
- expect(messages[3]).toEqual({
264
- type: "assistant",
265
- blocks: [],
266
- isFinalResponse: true,
267
- });
268
- });
269
- });
270
-
271
- // ---------------------------------------------------------------------------
272
- // createCodexAdapter — 工厂函数测试
273
- // ---------------------------------------------------------------------------
274
-
275
- describe("createCodexAdapter", () => {
276
- it("returns adapter with correct displayName and sessionDescPrefix", () => {
277
- const adapter = createCodexAdapter();
278
- expect(adapter.displayName).toBe("Codex");
279
- expect(adapter.sessionDescPrefix).toBe("Codex Session:");
280
- });
281
-
282
- it("closeSession does not throw", async () => {
283
- const adapter = createCodexAdapter();
284
- await expect(adapter.closeSession("any-id")).resolves.toBeUndefined();
285
- });
286
-
287
- it("getSessionInfo: store 中无该 sessionId 时返回 undefined", async () => {
288
- const store = createInMemoryMetaStore();
289
- const adapter = createCodexAdapter({ metaStore: store });
290
- const info = await adapter.getSessionInfo("unknown-sid");
291
- expect(info).toBeUndefined();
292
- });
293
-
294
- it("getSessionInfo: store 有 cwd 时返回 cwd,无 threadId", async () => {
295
- const store = createInMemoryMetaStore({
296
- "sid-known": { cwd: "F:/proj/Foo" },
297
- });
298
- const adapter = createCodexAdapter({ metaStore: store });
299
- const info = await adapter.getSessionInfo("sid-known");
300
- expect(info).toEqual({ sessionId: "sid-known", cwd: "F:/proj/Foo" });
301
- });
302
-
303
- it("getSessionInfo: 有 cwd + threadId 时一并返回", async () => {
304
- const store = createInMemoryMetaStore({
305
- "sid-known": { cwd: "F:/proj/Foo", threadId: "thread-123" },
306
- });
307
- const adapter = createCodexAdapter({ metaStore: store });
308
- const info = await adapter.getSessionInfo("sid-known");
309
- expect(info).toEqual({
310
- sessionId: "sid-known",
311
- cwd: "F:/proj/Foo",
312
- });
313
- });
314
-
315
- it("getSessionInfo: 不同 sessionId 互不影响", async () => {
316
- const store = createInMemoryMetaStore({
317
- "sid-A": { cwd: "/a", threadId: "tA" },
318
- "sid-B": { cwd: "/b" },
319
- });
320
- const adapter = createCodexAdapter({ metaStore: store });
321
- expect(await adapter.getSessionInfo("sid-A")).toEqual({
322
- sessionId: "sid-A",
323
- cwd: "/a",
324
- });
325
- expect(await adapter.getSessionInfo("sid-B")).toEqual({
326
- sessionId: "sid-B",
327
- cwd: "/b",
328
- });
329
- expect(await adapter.getSessionInfo("sid-C")).toBeUndefined();
330
- });
331
- });
@@ -1,170 +0,0 @@
1
- import { EventEmitter } from "node:events";
2
- import { PassThrough } from "node:stream";
3
-
4
- import { afterEach, describe, expect, it, vi } from "vitest";
5
-
6
- const spawnMock = vi.hoisted(() => vi.fn());
7
- const createRawStreamLogMock = vi.hoisted(() => vi.fn());
8
- const rawLogWriteLineMock = vi.hoisted(() => vi.fn());
9
- const rawLogCloseMock = vi.hoisted(() => vi.fn());
10
- const killProcessTreeMock = vi.hoisted(() => vi.fn(async () => {}));
11
-
12
- vi.mock("node:child_process", () => ({
13
- spawn: spawnMock,
14
- }));
15
-
16
- vi.mock("../adapters/raw-stream-log.ts", () => ({
17
- createRawStreamLog: createRawStreamLogMock,
18
- }));
19
-
20
- vi.mock("../adapters/proc-tree-kill.ts", () => ({
21
- killProcessTree: killProcessTreeMock,
22
- }));
23
-
24
- import { config } from "../config.ts";
25
- import { createCodexAdapter } from "../adapters/codex-adapter.ts";
26
- import { BadJsonIdleTimeoutError } from "../adapters/jsonl-stream.ts";
27
- import type { CodexSessionMetaStore } from "../adapters/codex-session-meta-store.ts";
28
-
29
- const originalRawStreamLogs = structuredClone(config.rawStreamLogs);
30
-
31
- function createProc(lines: string[]): EventEmitter & {
32
- stdout: PassThrough;
33
- stderr: PassThrough;
34
- stdin: PassThrough;
35
- pid: number;
36
- } {
37
- const proc = new EventEmitter() as EventEmitter & {
38
- stdout: PassThrough;
39
- stderr: PassThrough;
40
- stdin: PassThrough;
41
- pid: number;
42
- };
43
- proc.stdout = new PassThrough();
44
- proc.stderr = new PassThrough();
45
- proc.stdin = new PassThrough();
46
- proc.pid = 4242;
47
- queueMicrotask(() => {
48
- for (const line of lines) proc.stdout.write(`${line}\n`);
49
- proc.stdout.end();
50
- proc.stderr.end();
51
- proc.emit("close", 0);
52
- });
53
- return proc;
54
- }
55
-
56
- function createHangingProc(lines: string[]): EventEmitter & {
57
- stdout: PassThrough;
58
- stderr: PassThrough;
59
- stdin: PassThrough;
60
- pid: number;
61
- } {
62
- const proc = new EventEmitter() as EventEmitter & {
63
- stdout: PassThrough;
64
- stderr: PassThrough;
65
- stdin: PassThrough;
66
- pid: number;
67
- };
68
- proc.stdout = new PassThrough();
69
- proc.stderr = new PassThrough();
70
- proc.stdin = new PassThrough();
71
- proc.pid = 4242;
72
- queueMicrotask(() => {
73
- for (const line of lines) proc.stdout.write(`${line}\n`);
74
- });
75
- return proc;
76
- }
77
-
78
- async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
79
- const events: unknown[] = [];
80
- for await (const event of iterable) events.push(event);
81
- return events;
82
- }
83
-
84
- function metaStore(): CodexSessionMetaStore {
85
- return {
86
- async get(sessionId) {
87
- return sessionId === "sid-raw" ? { cwd: "F:/project" } : undefined;
88
- },
89
- async set() {},
90
- async setThreadId() {},
91
- };
92
- }
93
-
94
- afterEach(() => {
95
- vi.useRealTimers();
96
- spawnMock.mockReset();
97
- createRawStreamLogMock.mockReset();
98
- rawLogWriteLineMock.mockReset();
99
- rawLogCloseMock.mockReset();
100
- killProcessTreeMock.mockClear();
101
- config.rawStreamLogs = structuredClone(originalRawStreamLogs);
102
- });
103
-
104
- describe("Codex raw stream logs", () => {
105
- it("writes raw Codex JSONL stdout lines when enabled", async () => {
106
- const lines = [
107
- JSON.stringify({ type: "thread.started", thread_id: "thread-1" }),
108
- JSON.stringify({ type: "item.completed", item: { id: "item-1", type: "agent_message", text: "hello" } }),
109
- JSON.stringify({ type: "turn.completed" }),
110
- ];
111
- spawnMock.mockReturnValueOnce(createProc(lines));
112
- createRawStreamLogMock.mockResolvedValueOnce({
113
- filePath: "codex.jsonl.gz",
114
- writeLine: rawLogWriteLineMock,
115
- close: rawLogCloseMock,
116
- });
117
- config.rawStreamLogs.codex = {
118
- enabled: true,
119
- maxBytesPerTurn: 2048,
120
- retentionDays: 2,
121
- keepCompleted: false,
122
- };
123
-
124
- const adapter = createCodexAdapter({ metaStore: metaStore() });
125
- const events = await collect(adapter.prompt("sid-raw", "hi", "F:/project"));
126
-
127
- expect(createRawStreamLogMock).toHaveBeenCalledWith(expect.objectContaining({
128
- enabled: true,
129
- tool: "codex",
130
- sessionId: "sid-raw",
131
- label: "prompt",
132
- maxBytesPerTurn: 2048,
133
- retentionDays: 2,
134
- }));
135
- expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(1, lines[0]);
136
- expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(2, lines[1]);
137
- expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(3, lines[2]);
138
- expect(rawLogCloseMock).toHaveBeenCalledWith({ keep: false });
139
- expect(events).toEqual([
140
- {
141
- type: "assistant",
142
- blocks: [{ type: "text", text: "hello" }],
143
- },
144
- {
145
- type: "assistant",
146
- blocks: [],
147
- isFinalResponse: true,
148
- },
149
- ]);
150
- });
151
-
152
- it("fails the turn and kills the process tree when bad JSON is followed by idle stdout", async () => {
153
- vi.useFakeTimers();
154
- spawnMock.mockReturnValueOnce(createHangingProc([
155
- "{\"type\":\"item.started\",\"item\":{\"type\":\"command_execution\"",
156
- ]));
157
- createRawStreamLogMock.mockResolvedValueOnce(null);
158
-
159
- const adapter = createCodexAdapter({ metaStore: metaStore() });
160
- const pending = collect(adapter.prompt("sid-raw", "hi", "F:/project"))
161
- .catch((error: unknown) => error);
162
-
163
- await Promise.resolve();
164
- await vi.advanceTimersByTimeAsync(120_000);
165
-
166
- const error = await pending;
167
- expect(error).toBeInstanceOf(BadJsonIdleTimeoutError);
168
- expect(killProcessTreeMock).toHaveBeenCalledWith(4242);
169
- });
170
- });
@@ -1,146 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- import {
4
- _resetCodexResetRequestsForTest,
5
- handleCodexResetCardAction,
6
- type CodexResetActionDeps,
7
- } from "../codex-reset-actions.ts";
8
-
9
- function deps(): CodexResetActionDeps & {
10
- getTenantAccessToken: ReturnType<typeof vi.fn>;
11
- sendRawCard: ReturnType<typeof vi.fn>;
12
- sendTextReply: ReturnType<typeof vi.fn>;
13
- sendCardReply: ReturnType<typeof vi.fn>;
14
- updateCardMessage: ReturnType<typeof vi.fn>;
15
- recallMessage: ReturnType<typeof vi.fn>;
16
- consumeCodexRateLimitResetCredit: ReturnType<typeof vi.fn>;
17
- createRequestId: ReturnType<typeof vi.fn>;
18
- } {
19
- return {
20
- getTenantAccessToken: vi.fn(async () => "tenant-token"),
21
- sendRawCard: vi.fn(async () => true),
22
- sendTextReply: vi.fn(async () => true),
23
- sendCardReply: vi.fn(async () => true),
24
- updateCardMessage: vi.fn(async () => true),
25
- recallMessage: vi.fn(async () => true),
26
- consumeCodexRateLimitResetCredit: vi.fn(async () => ({ code: "reset" as const, windowsReset: 2 })),
27
- createRequestId: vi.fn(() => "request-1"),
28
- };
29
- }
30
-
31
- function event(value: Record<string, unknown>, messageId: string) {
32
- return {
33
- event: {
34
- open_message_id: messageId,
35
- open_chat_id: "chat-1",
36
- operator: { open_id: "ou-user" },
37
- action: { value },
38
- },
39
- };
40
- }
41
-
42
- describe("Codex reset card actions", () => {
43
- beforeEach(() => {
44
- _resetCodexResetRequestsForTest();
45
- });
46
-
47
- it("sends a confirmation card tied to the clicked usage card", async () => {
48
- const d = deps();
49
-
50
- await expect(handleCodexResetCardAction(
51
- event({ action: "codex_reset_request" }, "usage-message"),
52
- d,
53
- )).resolves.toBe(true);
54
-
55
- expect(d.sendRawCard).toHaveBeenCalledTimes(1);
56
- expect(d.sendRawCard.mock.calls[0][1]).toBe("chat-1");
57
- const confirmCard = JSON.parse(d.sendRawCard.mock.calls[0][2]);
58
- const action = confirmCard.elements.find((element: any) => element.tag === "action");
59
- expect(action.actions[0].value).toEqual({
60
- action: "codex_reset_confirm",
61
- decision: "yes",
62
- parentMessageId: "usage-message",
63
- requestId: "request-1",
64
- });
65
- });
66
-
67
- it("consumes a reset credit, sends the result, and collapses the confirmation card when user confirms", async () => {
68
- const d = deps();
69
- await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
70
-
71
- await expect(handleCodexResetCardAction(
72
- event({
73
- action: "codex_reset_confirm",
74
- decision: "yes",
75
- parentMessageId: "usage-message",
76
- requestId: "request-1",
77
- }, "confirm-message"),
78
- d,
79
- )).resolves.toBe(true);
80
-
81
- expect(d.consumeCodexRateLimitResetCredit).toHaveBeenCalledWith("request-1");
82
- expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
83
- expect(d.updateCardMessage.mock.calls[0][1]).toBe("confirm-message");
84
- expect(JSON.parse(d.updateCardMessage.mock.calls[0][2]).elements).toEqual([
85
- { tag: "markdown", content: " " },
86
- ]);
87
- expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message");
88
- expect(d.sendTextReply).toHaveBeenCalledWith(
89
- "tenant-token",
90
- "chat-1",
91
- expect.stringContaining("重置成功"),
92
- );
93
- expect(d.sendCardReply).not.toHaveBeenCalled();
94
- });
95
-
96
- it("does not consume a reset credit and sends a cancellation message when user declines", async () => {
97
- const d = deps();
98
- await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
99
-
100
- await expect(handleCodexResetCardAction(
101
- event({
102
- action: "codex_reset_confirm",
103
- decision: "no",
104
- parentMessageId: "usage-message",
105
- requestId: "request-1",
106
- }, "confirm-message"),
107
- d,
108
- )).resolves.toBe(true);
109
-
110
- expect(d.consumeCodexRateLimitResetCredit).not.toHaveBeenCalled();
111
- expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
112
- expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message");
113
- expect(d.sendTextReply).toHaveBeenCalledWith(
114
- "tenant-token",
115
- "chat-1",
116
- "Codex 主动重置:用户取消了重置。",
117
- );
118
- expect(d.sendCardReply).not.toHaveBeenCalled();
119
- });
120
-
121
- it("can collapse a confirmation card using context.message_id fallback", async () => {
122
- const d = deps();
123
- await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
124
-
125
- await expect(handleCodexResetCardAction({
126
- event: {
127
- context: {
128
- message_id: "confirm-message-from-context",
129
- open_chat_id: "chat-1",
130
- },
131
- action: {
132
- value: {
133
- action: "codex_reset_confirm",
134
- decision: "no",
135
- parentMessageId: "usage-message",
136
- requestId: "request-1",
137
- },
138
- },
139
- },
140
- }, d)).resolves.toBe(true);
141
-
142
- expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
143
- expect(d.updateCardMessage.mock.calls[0][1]).toBe("confirm-message-from-context");
144
- expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message-from-context");
145
- });
146
- });