chatccc 0.2.230 → 0.2.231

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 (116) 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 -76
  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 +65 -1
  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 +202 -163
  28. package/src/__tests__/builtin-permissions.test.ts +211 -211
  29. package/src/__tests__/builtin-session-select.test.ts +116 -116
  30. package/src/__tests__/builtin-sigint.test.ts +56 -56
  31. package/src/__tests__/builtin-web-tools.test.ts +220 -220
  32. package/src/__tests__/card-action-routing.test.ts +18 -18
  33. package/src/__tests__/cardkit.test.ts +60 -60
  34. package/src/__tests__/ccc-adapter.test.ts +170 -136
  35. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  36. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  37. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  38. package/src/__tests__/claude-adapter.test.ts +614 -614
  39. package/src/__tests__/claude-raw-stream-log.test.ts +87 -87
  40. package/src/__tests__/codex-adapter.test.ts +58 -58
  41. package/src/__tests__/codex-raw-stream-log.test.ts +170 -170
  42. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  43. package/src/__tests__/cursor-adapter.test.ts +268 -268
  44. package/src/__tests__/feishu-api.test.ts +60 -60
  45. package/src/__tests__/feishu-avatar.test.ts +164 -164
  46. package/src/__tests__/feishu-message-ingress.test.ts +138 -138
  47. package/src/__tests__/feishu-platform.test.ts +22 -22
  48. package/src/__tests__/format-message.test.ts +47 -47
  49. package/src/__tests__/jsonl-stream.test.ts +79 -79
  50. package/src/__tests__/package-files.test.ts +24 -24
  51. package/src/__tests__/privacy.test.ts +198 -198
  52. package/src/__tests__/progress-reducer.test.ts +121 -110
  53. package/src/__tests__/raw-stream-log.test.ts +106 -106
  54. package/src/__tests__/response-stall.test.ts +49 -49
  55. package/src/__tests__/session.test.ts +11 -16
  56. package/src/__tests__/shared-prefix.test.ts +36 -36
  57. package/src/__tests__/sim-platform.test.ts +16 -16
  58. package/src/__tests__/startup-lifecycle.test.ts +231 -231
  59. package/src/__tests__/stop-session.test.ts +34 -34
  60. package/src/__tests__/stream-state.test.ts +42 -42
  61. package/src/__tests__/terminal-renderer.test.ts +247 -247
  62. package/src/__tests__/update-command-guard.test.ts +144 -144
  63. package/src/__tests__/web-ui.test.ts +326 -326
  64. package/src/adapters/adapter-interface.ts +24 -18
  65. package/src/adapters/ccc-adapter.ts +141 -131
  66. package/src/adapters/claude-adapter.ts +620 -620
  67. package/src/adapters/claude-session-meta-store.ts +120 -120
  68. package/src/adapters/codex-adapter.ts +426 -426
  69. package/src/adapters/cursor-adapter.ts +681 -681
  70. package/src/adapters/jsonl-stream.ts +157 -157
  71. package/src/adapters/raw-stream-log.ts +124 -124
  72. package/src/adapters/resource-monitor.ts +140 -140
  73. package/src/agent-activity.ts +175 -170
  74. package/src/agent-delegate-task-rpc.ts +153 -153
  75. package/src/agent-delegate-task.ts +91 -91
  76. package/src/agent-reload-config-rpc.ts +34 -34
  77. package/src/agent-stop-stuck.ts +129 -129
  78. package/src/builtin/cli.ts +9 -2
  79. package/src/builtin/config.ts +84 -84
  80. package/src/builtin/context.ts +62 -4
  81. package/src/builtin/file-log.ts +38 -38
  82. package/src/builtin/index.ts +83 -19
  83. package/src/builtin/proc-tree-kill.ts +61 -61
  84. package/src/builtin/progress/cards-helpers.ts +76 -76
  85. package/src/builtin/progress/reducer.ts +113 -108
  86. package/src/builtin/progress/terminal-renderer.ts +294 -294
  87. package/src/builtin/progress/view.ts +77 -77
  88. package/src/builtin/raw-stream-log.ts +124 -124
  89. package/src/builtin/session-select.ts +48 -48
  90. package/src/builtin/sigint.ts +50 -50
  91. package/src/builtin/web-tools.ts +313 -313
  92. package/src/card-action-routing.ts +14 -14
  93. package/src/chatgpt-subscription-rpc.ts +27 -27
  94. package/src/chatgpt-subscription.ts +299 -299
  95. package/src/chrome-devtools-guard.ts +318 -318
  96. package/src/codex-reset-actions.ts +184 -184
  97. package/src/feishu-api.ts +193 -193
  98. package/src/feishu-message-ingress.ts +195 -195
  99. package/src/feishu-platform.ts +20 -20
  100. package/src/format-message.ts +293 -293
  101. package/src/litellm-proxy.ts +374 -374
  102. package/src/platform-adapter.ts +6 -6
  103. package/src/privacy.ts +118 -118
  104. package/src/progress/reducer.ts +113 -108
  105. package/src/progress/terminal-renderer.ts +294 -294
  106. package/src/progress/view.ts +77 -77
  107. package/src/response-stall.ts +28 -28
  108. package/src/runtime-reload.ts +34 -34
  109. package/src/session-chat-binding.ts +82 -82
  110. package/src/session-name.ts +8 -8
  111. package/src/session.ts +12 -11
  112. package/src/shared-prefix.ts +29 -29
  113. package/src/sim-platform.ts +20 -20
  114. package/src/startup-lifecycle.ts +250 -250
  115. package/src/turn-cards.ts +117 -117
  116. package/src/update-command-guard.ts +165 -165
@@ -1,170 +1,170 @@
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
+ 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 +1,146 @@
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
- });
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
+ });