chatccc 0.2.200 → 0.2.202

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 (92) hide show
  1. package/README.md +17 -15
  2. package/agent-prompts/claude_specific.md +45 -45
  3. package/agent-prompts/codex_specific.md +2 -2
  4. package/agent-prompts/cursor_specific.md +13 -13
  5. package/bin/cccagent.mjs +17 -17
  6. package/config.sample.json +27 -27
  7. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  8. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  9. package/im-skills/feishu-skill/skill.md +3 -3
  10. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  11. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  12. package/im-skills/wechat-file-skill/skill.md +10 -10
  13. package/im-skills/wechat-image-skill/skill.md +10 -10
  14. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  15. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  16. package/im-skills/wechat-video-skill/skill.md +10 -10
  17. package/package.json +1 -1
  18. package/scripts/postinstall-sharp-check.mjs +58 -58
  19. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  20. package/src/__tests__/agent-reload-config-rpc.test.ts +99 -99
  21. package/src/__tests__/builtin-chat-session.test.ts +277 -277
  22. package/src/__tests__/builtin-cli-json.test.ts +39 -39
  23. package/src/__tests__/builtin-config.test.ts +33 -33
  24. package/src/__tests__/builtin-context.test.ts +163 -163
  25. package/src/__tests__/builtin-file-tools.test.ts +224 -224
  26. package/src/__tests__/builtin-session-select.test.ts +116 -116
  27. package/src/__tests__/builtin-sigint.test.ts +56 -56
  28. package/src/__tests__/card-plain-text.test.ts +5 -5
  29. package/src/__tests__/cardkit.test.ts +60 -60
  30. package/src/__tests__/cards.test.ts +133 -122
  31. package/src/__tests__/ccc-adapter.test.ts +114 -114
  32. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  33. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  34. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  35. package/src/__tests__/claude-adapter.test.ts +592 -592
  36. package/src/__tests__/claude-raw-stream-log.test.ts +87 -87
  37. package/src/__tests__/codex-raw-stream-log.test.ts +163 -163
  38. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  39. package/src/__tests__/config-reload.test.ts +10 -10
  40. package/src/__tests__/config-sample.test.ts +18 -18
  41. package/src/__tests__/cursor-adapter.test.ts +226 -226
  42. package/src/__tests__/feishu-api.test.ts +60 -60
  43. package/src/__tests__/feishu-avatar.test.ts +90 -90
  44. package/src/__tests__/feishu-platform.test.ts +22 -22
  45. package/src/__tests__/format-message.test.ts +47 -47
  46. package/src/__tests__/jsonl-stream.test.ts +79 -79
  47. package/src/__tests__/orchestrator.test.ts +464 -301
  48. package/src/__tests__/privacy.test.ts +198 -198
  49. package/src/__tests__/raw-stream-log.test.ts +106 -106
  50. package/src/__tests__/session.test.ts +46 -44
  51. package/src/__tests__/shared-prefix.test.ts +36 -36
  52. package/src/__tests__/sim-platform.test.ts +12 -12
  53. package/src/__tests__/stream-state.test.ts +42 -42
  54. package/src/__tests__/web-ui.test.ts +209 -209
  55. package/src/adapters/ccc-adapter.ts +121 -121
  56. package/src/adapters/claude-adapter.ts +603 -603
  57. package/src/adapters/claude-session-meta-store.ts +120 -120
  58. package/src/adapters/codex-adapter.ts +380 -380
  59. package/src/adapters/cursor-adapter.ts +673 -673
  60. package/src/adapters/jsonl-stream.ts +157 -157
  61. package/src/adapters/raw-stream-log.ts +124 -124
  62. package/src/adapters/resource-monitor.ts +140 -140
  63. package/src/agent-delegate-task-rpc.ts +153 -153
  64. package/src/agent-delegate-task.ts +81 -81
  65. package/src/agent-reload-config-rpc.ts +34 -34
  66. package/src/agent-stop-stuck.ts +129 -129
  67. package/src/builtin/cli.ts +473 -473
  68. package/src/builtin/context.ts +323 -323
  69. package/src/builtin/file-tools.ts +1072 -1072
  70. package/src/builtin/index.ts +404 -404
  71. package/src/builtin/session-select.ts +48 -48
  72. package/src/builtin/sigint.ts +50 -50
  73. package/src/cards.ts +222 -210
  74. package/src/chatgpt-subscription-rpc.ts +27 -27
  75. package/src/chatgpt-subscription.ts +299 -299
  76. package/src/chrome-devtools-guard.ts +318 -318
  77. package/src/codex-reset-actions.ts +184 -184
  78. package/src/config.ts +125 -125
  79. package/src/feishu-api.ts +118 -118
  80. package/src/feishu-platform.ts +20 -20
  81. package/src/format-message.ts +293 -293
  82. package/src/litellm-proxy.ts +374 -374
  83. package/src/orchestrator.ts +348 -329
  84. package/src/privacy.ts +118 -118
  85. package/src/runtime-reload.ts +34 -34
  86. package/src/session-chat-binding.ts +6 -6
  87. package/src/session-name.ts +8 -8
  88. package/src/session.ts +177 -169
  89. package/src/shared-prefix.ts +29 -29
  90. package/src/sim-platform.ts +20 -20
  91. package/src/turn-cards.ts +117 -117
  92. package/src/web-ui.ts +205 -205
@@ -1,277 +1,277 @@
1
- import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
2
- import { tmpdir } from "node:os";
3
- import { join } from "node:path";
4
-
5
- import { afterEach, describe, expect, it, vi } from "vitest";
6
-
7
- import { config } from "../config.ts";
8
-
9
- const streamTextMock = vi.fn();
10
- const generateTextMock = vi.fn();
11
- const createRawStreamLogMock = vi.fn();
12
- const rawLogWriteLineMock = vi.fn();
13
- const rawLogCloseMock = vi.fn();
14
- const originalRawStreamLogs = structuredClone(config.rawStreamLogs);
15
-
16
- vi.mock("@ai-sdk/openai-compatible", () => ({
17
- createOpenAICompatible: vi.fn(() => (modelId: string) => ({ modelId })),
18
- }));
19
-
20
- vi.mock("ai", () => ({
21
- streamText: streamTextMock,
22
- generateText: generateTextMock,
23
- isLoopFinished: vi.fn(() => ({ loopFinished: true })),
24
- stepCountIs: vi.fn((count: number) => ({ count })),
25
- jsonSchema: vi.fn((schema: unknown) => schema),
26
- tool: vi.fn((definition: unknown) => definition),
27
- }));
28
-
29
- vi.mock("../adapters/raw-stream-log.ts", () => ({
30
- createRawStreamLog: createRawStreamLogMock,
31
- }));
32
-
33
- async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
34
- const events: unknown[] = [];
35
- for await (const event of iterable) events.push(event);
36
- return events;
37
- }
38
-
39
- async function* textStream(...chunks: string[]): AsyncIterable<string> {
40
- for (const chunk of chunks) yield chunk;
41
- }
42
-
43
- async function* fullStream(...parts: unknown[]): AsyncIterable<unknown> {
44
- for (const part of parts) yield part;
45
- }
46
-
47
- afterEach(() => {
48
- streamTextMock.mockReset();
49
- generateTextMock.mockReset();
50
- createRawStreamLogMock.mockReset();
51
- rawLogWriteLineMock.mockReset();
52
- rawLogCloseMock.mockReset();
53
- config.rawStreamLogs = structuredClone(originalRawStreamLogs);
54
- });
55
-
56
- describe("ChatSession context management", () => {
57
- it("injects cwd project instruction files before runtime workspace details", async () => {
58
- const { ChatSession } = await import("../builtin/index.ts");
59
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-instructions-"));
60
- await writeFile(join(dir, "AGENTS.md"), "agents root guidance", "utf-8");
61
- await writeFile(join(dir, "AGENTS.local.md"), "agents local guidance", "utf-8");
62
- await writeFile(join(dir, "CLAUDE.md"), "claude root guidance", "utf-8");
63
- await writeFile(join(dir, "CLAUDE.local.md"), "claude local guidance", "utf-8");
64
- streamTextMock.mockReturnValueOnce({ textStream: textStream() });
65
-
66
- const session = new ChatSession(
67
- { apiKey: "sk-test" },
68
- {
69
- cwd: dir,
70
- sessionId: "project-instructions",
71
- },
72
- );
73
- await collect(session.chat("hi"));
74
-
75
- const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
76
- expect(system).toContain("## Project Instructions");
77
- expect(system).toContain("### AGENTS.md");
78
- expect(system).toContain("agents root guidance");
79
- expect(system).toContain("### AGENTS.local.md");
80
- expect(system).toContain("agents local guidance");
81
- expect(system).toContain("### CLAUDE.md");
82
- expect(system).toContain("claude root guidance");
83
- expect(system).toContain("### CLAUDE.local.md");
84
- expect(system).toContain("claude local guidance");
85
-
86
- expect(system.indexOf("agents root guidance")).toBeLessThan(system.indexOf("agents local guidance"));
87
- expect(system.indexOf("agents local guidance")).toBeLessThan(system.indexOf("claude root guidance"));
88
- expect(system.indexOf("claude root guidance")).toBeLessThan(system.indexOf("claude local guidance"));
89
- expect(system.indexOf("claude local guidance")).toBeLessThan(system.indexOf(dir));
90
- });
91
-
92
- it("does not read project instruction files from parent directories", async () => {
93
- const { ChatSession } = await import("../builtin/index.ts");
94
- const parent = await mkdtemp(join(tmpdir(), "chatccc-session-parent-instructions-"));
95
- const child = join(parent, "child");
96
- await mkdir(child);
97
- await writeFile(join(parent, "AGENTS.md"), "parent-only guidance", "utf-8");
98
- streamTextMock.mockReturnValueOnce({ textStream: textStream() });
99
-
100
- const session = new ChatSession(
101
- { apiKey: "sk-test" },
102
- {
103
- cwd: child,
104
- sessionId: "no-parent-instructions",
105
- },
106
- );
107
- await collect(session.chat("hi"));
108
-
109
- const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
110
- expect(system).not.toContain("parent-only guidance");
111
- });
112
-
113
- it("uses loop-finished stopping by default", async () => {
114
- const { ChatSession } = await import("../builtin/index.ts");
115
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-unlimited-"));
116
- streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
117
-
118
- const session = new ChatSession(
119
- { apiKey: "sk-test" },
120
- {
121
- cwd: dir,
122
- sessionId: "unlimited-steps",
123
- },
124
- );
125
- await collect(session.chat("run a multi-stage workflow"));
126
-
127
- expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
128
- stopWhen: { loopFinished: true },
129
- }));
130
- });
131
-
132
- it("uses a configured tool step limit when provided", async () => {
133
- const { ChatSession } = await import("../builtin/index.ts");
134
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-step-budget-"));
135
- streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
136
-
137
- const session = new ChatSession(
138
- { apiKey: "sk-test" },
139
- {
140
- cwd: dir,
141
- sessionId: "step-budget",
142
- maxSteps: 7,
143
- },
144
- );
145
- await collect(session.chat("run a bounded workflow"));
146
-
147
- expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
148
- stopWhen: { count: 7 },
149
- }));
150
- });
151
-
152
- it("loads persisted context, compacts older messages, and persists the new assistant reply", async () => {
153
- const { ChatSession } = await import("../builtin/index.ts");
154
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-context-"));
155
-
156
- const seed = new ChatSession(
157
- { apiKey: "sk-test" },
158
- {
159
- persist: true,
160
- contextDir: dir,
161
- sessionId: "integration",
162
- compactAtTokens: 10_000,
163
- },
164
- );
165
- streamTextMock.mockReturnValueOnce({ textStream: textStream("old answer") });
166
- await collect(seed.chat("old question"));
167
-
168
- generateTextMock.mockResolvedValueOnce({ text: "## 当前任务状态\n- 旧问题已总结" });
169
- streamTextMock.mockReturnValueOnce({ textStream: textStream("new answer") });
170
-
171
- const restored = new ChatSession(
172
- { apiKey: "sk-test" },
173
- {
174
- persist: true,
175
- contextDir: dir,
176
- sessionId: "integration",
177
- compactAtTokens: 1,
178
- keepRecentMessages: 1,
179
- },
180
- );
181
- const events = await collect(restored.chat("new question"));
182
-
183
- expect(generateTextMock).toHaveBeenCalledOnce();
184
- expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
185
- messages: expect.arrayContaining([
186
- expect.objectContaining({ content: expect.stringContaining("旧问题已总结") }),
187
- expect.objectContaining({ role: "user", content: "new question" }),
188
- ]),
189
- }));
190
- expect(events).toContainEqual({ type: "compact", compactedMessages: 2 });
191
- expect(restored.history.map((m) => m.content).join("\n")).toContain("new answer");
192
- });
193
-
194
- it("streams tool calls and tool results from fullStream", async () => {
195
- const { ChatSession } = await import("../builtin/index.ts");
196
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-tools-"));
197
- const session = new ChatSession(
198
- { apiKey: "sk-test" },
199
- {
200
- persist: true,
201
- contextDir: dir,
202
- sessionId: "tools",
203
- },
204
- );
205
-
206
- streamTextMock.mockReturnValueOnce({
207
- fullStream: fullStream(
208
- { type: "tool-call", toolCallId: "call-1", toolName: "read_file", input: { path: "package.json" } },
209
- { type: "tool-result", toolCallId: "call-1", toolName: "read_file", output: { content: "{}" } },
210
- { type: "text-delta", text: "done" },
211
- ),
212
- });
213
-
214
- const events = await collect(session.chat("read package"));
215
-
216
- expect(events).toContainEqual({
217
- type: "tool_use",
218
- id: "call-1",
219
- name: "read_file",
220
- input: { path: "package.json" },
221
- });
222
- expect(events).toContainEqual({
223
- type: "tool_result",
224
- tool_use_id: "call-1",
225
- name: "read_file",
226
- content: { content: "{}" },
227
- is_error: false,
228
- });
229
- expect(events).toContainEqual({ type: "text", text: "done", accumulated: "done" });
230
- });
231
-
232
- it("writes raw CCC fullStream parts when raw stream logs are enabled", async () => {
233
- const { ChatSession } = await import("../builtin/index.ts");
234
- config.rawStreamLogs.ccc = {
235
- enabled: true,
236
- maxBytesPerTurn: 4096,
237
- retentionDays: 3,
238
- keepCompleted: true,
239
- };
240
- createRawStreamLogMock.mockResolvedValueOnce({
241
- filePath: "raw.jsonl.gz",
242
- writeLine: rawLogWriteLineMock,
243
- close: rawLogCloseMock,
244
- });
245
- const session = new ChatSession(
246
- { apiKey: "sk-test" },
247
- {
248
- sessionId: "raw-log-session",
249
- },
250
- );
251
- const textPart = { type: "text-delta", text: "hello" };
252
- const toolPart = {
253
- type: "tool-call",
254
- toolCallId: "call-1",
255
- toolName: "read_file",
256
- input: { path: "package.json" },
257
- };
258
-
259
- streamTextMock.mockReturnValueOnce({
260
- fullStream: fullStream(textPart, toolPart),
261
- });
262
-
263
- await collect(session.chat("hi"));
264
-
265
- expect(createRawStreamLogMock).toHaveBeenCalledWith(expect.objectContaining({
266
- enabled: true,
267
- tool: "ccc",
268
- sessionId: "raw-log-session",
269
- label: "prompt",
270
- maxBytesPerTurn: 4096,
271
- retentionDays: 3,
272
- }));
273
- expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(1, JSON.stringify(textPart));
274
- expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(2, JSON.stringify(toolPart));
275
- expect(rawLogCloseMock).toHaveBeenCalledWith({ keep: true });
276
- });
277
- });
1
+ import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+
5
+ import { afterEach, describe, expect, it, vi } from "vitest";
6
+
7
+ import { config } from "../config.ts";
8
+
9
+ const streamTextMock = vi.fn();
10
+ const generateTextMock = vi.fn();
11
+ const createRawStreamLogMock = vi.fn();
12
+ const rawLogWriteLineMock = vi.fn();
13
+ const rawLogCloseMock = vi.fn();
14
+ const originalRawStreamLogs = structuredClone(config.rawStreamLogs);
15
+
16
+ vi.mock("@ai-sdk/openai-compatible", () => ({
17
+ createOpenAICompatible: vi.fn(() => (modelId: string) => ({ modelId })),
18
+ }));
19
+
20
+ vi.mock("ai", () => ({
21
+ streamText: streamTextMock,
22
+ generateText: generateTextMock,
23
+ isLoopFinished: vi.fn(() => ({ loopFinished: true })),
24
+ stepCountIs: vi.fn((count: number) => ({ count })),
25
+ jsonSchema: vi.fn((schema: unknown) => schema),
26
+ tool: vi.fn((definition: unknown) => definition),
27
+ }));
28
+
29
+ vi.mock("../adapters/raw-stream-log.ts", () => ({
30
+ createRawStreamLog: createRawStreamLogMock,
31
+ }));
32
+
33
+ async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
34
+ const events: unknown[] = [];
35
+ for await (const event of iterable) events.push(event);
36
+ return events;
37
+ }
38
+
39
+ async function* textStream(...chunks: string[]): AsyncIterable<string> {
40
+ for (const chunk of chunks) yield chunk;
41
+ }
42
+
43
+ async function* fullStream(...parts: unknown[]): AsyncIterable<unknown> {
44
+ for (const part of parts) yield part;
45
+ }
46
+
47
+ afterEach(() => {
48
+ streamTextMock.mockReset();
49
+ generateTextMock.mockReset();
50
+ createRawStreamLogMock.mockReset();
51
+ rawLogWriteLineMock.mockReset();
52
+ rawLogCloseMock.mockReset();
53
+ config.rawStreamLogs = structuredClone(originalRawStreamLogs);
54
+ });
55
+
56
+ describe("ChatSession context management", () => {
57
+ it("injects cwd project instruction files before runtime workspace details", async () => {
58
+ const { ChatSession } = await import("../builtin/index.ts");
59
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-instructions-"));
60
+ await writeFile(join(dir, "AGENTS.md"), "agents root guidance", "utf-8");
61
+ await writeFile(join(dir, "AGENTS.local.md"), "agents local guidance", "utf-8");
62
+ await writeFile(join(dir, "CLAUDE.md"), "claude root guidance", "utf-8");
63
+ await writeFile(join(dir, "CLAUDE.local.md"), "claude local guidance", "utf-8");
64
+ streamTextMock.mockReturnValueOnce({ textStream: textStream() });
65
+
66
+ const session = new ChatSession(
67
+ { apiKey: "sk-test" },
68
+ {
69
+ cwd: dir,
70
+ sessionId: "project-instructions",
71
+ },
72
+ );
73
+ await collect(session.chat("hi"));
74
+
75
+ const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
76
+ expect(system).toContain("## Project Instructions");
77
+ expect(system).toContain("### AGENTS.md");
78
+ expect(system).toContain("agents root guidance");
79
+ expect(system).toContain("### AGENTS.local.md");
80
+ expect(system).toContain("agents local guidance");
81
+ expect(system).toContain("### CLAUDE.md");
82
+ expect(system).toContain("claude root guidance");
83
+ expect(system).toContain("### CLAUDE.local.md");
84
+ expect(system).toContain("claude local guidance");
85
+
86
+ expect(system.indexOf("agents root guidance")).toBeLessThan(system.indexOf("agents local guidance"));
87
+ expect(system.indexOf("agents local guidance")).toBeLessThan(system.indexOf("claude root guidance"));
88
+ expect(system.indexOf("claude root guidance")).toBeLessThan(system.indexOf("claude local guidance"));
89
+ expect(system.indexOf("claude local guidance")).toBeLessThan(system.indexOf(dir));
90
+ });
91
+
92
+ it("does not read project instruction files from parent directories", async () => {
93
+ const { ChatSession } = await import("../builtin/index.ts");
94
+ const parent = await mkdtemp(join(tmpdir(), "chatccc-session-parent-instructions-"));
95
+ const child = join(parent, "child");
96
+ await mkdir(child);
97
+ await writeFile(join(parent, "AGENTS.md"), "parent-only guidance", "utf-8");
98
+ streamTextMock.mockReturnValueOnce({ textStream: textStream() });
99
+
100
+ const session = new ChatSession(
101
+ { apiKey: "sk-test" },
102
+ {
103
+ cwd: child,
104
+ sessionId: "no-parent-instructions",
105
+ },
106
+ );
107
+ await collect(session.chat("hi"));
108
+
109
+ const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
110
+ expect(system).not.toContain("parent-only guidance");
111
+ });
112
+
113
+ it("uses loop-finished stopping by default", async () => {
114
+ const { ChatSession } = await import("../builtin/index.ts");
115
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-unlimited-"));
116
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
117
+
118
+ const session = new ChatSession(
119
+ { apiKey: "sk-test" },
120
+ {
121
+ cwd: dir,
122
+ sessionId: "unlimited-steps",
123
+ },
124
+ );
125
+ await collect(session.chat("run a multi-stage workflow"));
126
+
127
+ expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
128
+ stopWhen: { loopFinished: true },
129
+ }));
130
+ });
131
+
132
+ it("uses a configured tool step limit when provided", async () => {
133
+ const { ChatSession } = await import("../builtin/index.ts");
134
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-step-budget-"));
135
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
136
+
137
+ const session = new ChatSession(
138
+ { apiKey: "sk-test" },
139
+ {
140
+ cwd: dir,
141
+ sessionId: "step-budget",
142
+ maxSteps: 7,
143
+ },
144
+ );
145
+ await collect(session.chat("run a bounded workflow"));
146
+
147
+ expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
148
+ stopWhen: { count: 7 },
149
+ }));
150
+ });
151
+
152
+ it("loads persisted context, compacts older messages, and persists the new assistant reply", async () => {
153
+ const { ChatSession } = await import("../builtin/index.ts");
154
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-context-"));
155
+
156
+ const seed = new ChatSession(
157
+ { apiKey: "sk-test" },
158
+ {
159
+ persist: true,
160
+ contextDir: dir,
161
+ sessionId: "integration",
162
+ compactAtTokens: 10_000,
163
+ },
164
+ );
165
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("old answer") });
166
+ await collect(seed.chat("old question"));
167
+
168
+ generateTextMock.mockResolvedValueOnce({ text: "## 当前任务状态\n- 旧问题已总结" });
169
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("new answer") });
170
+
171
+ const restored = new ChatSession(
172
+ { apiKey: "sk-test" },
173
+ {
174
+ persist: true,
175
+ contextDir: dir,
176
+ sessionId: "integration",
177
+ compactAtTokens: 1,
178
+ keepRecentMessages: 1,
179
+ },
180
+ );
181
+ const events = await collect(restored.chat("new question"));
182
+
183
+ expect(generateTextMock).toHaveBeenCalledOnce();
184
+ expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
185
+ messages: expect.arrayContaining([
186
+ expect.objectContaining({ content: expect.stringContaining("旧问题已总结") }),
187
+ expect.objectContaining({ role: "user", content: "new question" }),
188
+ ]),
189
+ }));
190
+ expect(events).toContainEqual({ type: "compact", compactedMessages: 2 });
191
+ expect(restored.history.map((m) => m.content).join("\n")).toContain("new answer");
192
+ });
193
+
194
+ it("streams tool calls and tool results from fullStream", async () => {
195
+ const { ChatSession } = await import("../builtin/index.ts");
196
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-tools-"));
197
+ const session = new ChatSession(
198
+ { apiKey: "sk-test" },
199
+ {
200
+ persist: true,
201
+ contextDir: dir,
202
+ sessionId: "tools",
203
+ },
204
+ );
205
+
206
+ streamTextMock.mockReturnValueOnce({
207
+ fullStream: fullStream(
208
+ { type: "tool-call", toolCallId: "call-1", toolName: "read_file", input: { path: "package.json" } },
209
+ { type: "tool-result", toolCallId: "call-1", toolName: "read_file", output: { content: "{}" } },
210
+ { type: "text-delta", text: "done" },
211
+ ),
212
+ });
213
+
214
+ const events = await collect(session.chat("read package"));
215
+
216
+ expect(events).toContainEqual({
217
+ type: "tool_use",
218
+ id: "call-1",
219
+ name: "read_file",
220
+ input: { path: "package.json" },
221
+ });
222
+ expect(events).toContainEqual({
223
+ type: "tool_result",
224
+ tool_use_id: "call-1",
225
+ name: "read_file",
226
+ content: { content: "{}" },
227
+ is_error: false,
228
+ });
229
+ expect(events).toContainEqual({ type: "text", text: "done", accumulated: "done" });
230
+ });
231
+
232
+ it("writes raw CCC fullStream parts when raw stream logs are enabled", async () => {
233
+ const { ChatSession } = await import("../builtin/index.ts");
234
+ config.rawStreamLogs.ccc = {
235
+ enabled: true,
236
+ maxBytesPerTurn: 4096,
237
+ retentionDays: 3,
238
+ keepCompleted: true,
239
+ };
240
+ createRawStreamLogMock.mockResolvedValueOnce({
241
+ filePath: "raw.jsonl.gz",
242
+ writeLine: rawLogWriteLineMock,
243
+ close: rawLogCloseMock,
244
+ });
245
+ const session = new ChatSession(
246
+ { apiKey: "sk-test" },
247
+ {
248
+ sessionId: "raw-log-session",
249
+ },
250
+ );
251
+ const textPart = { type: "text-delta", text: "hello" };
252
+ const toolPart = {
253
+ type: "tool-call",
254
+ toolCallId: "call-1",
255
+ toolName: "read_file",
256
+ input: { path: "package.json" },
257
+ };
258
+
259
+ streamTextMock.mockReturnValueOnce({
260
+ fullStream: fullStream(textPart, toolPart),
261
+ });
262
+
263
+ await collect(session.chat("hi"));
264
+
265
+ expect(createRawStreamLogMock).toHaveBeenCalledWith(expect.objectContaining({
266
+ enabled: true,
267
+ tool: "ccc",
268
+ sessionId: "raw-log-session",
269
+ label: "prompt",
270
+ maxBytesPerTurn: 4096,
271
+ retentionDays: 3,
272
+ }));
273
+ expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(1, JSON.stringify(textPart));
274
+ expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(2, JSON.stringify(toolPart));
275
+ expect(rawLogCloseMock).toHaveBeenCalledWith({ keep: true });
276
+ });
277
+ });
@@ -1,39 +1,39 @@
1
- import { execFile } from "node:child_process";
2
- import { promisify } from "node:util";
3
-
4
- import { describe, expect, it } from "vitest";
5
-
6
- const execFileAsync = promisify(execFile);
7
-
8
- describe("builtin cli --stream-json", () => {
9
- it("writes only JSON lines to stdout when startup fails", async () => {
10
- let caught: unknown;
11
- try {
12
- await execFileAsync(process.execPath, [
13
- "bin/cccagent.mjs",
14
- "--stream-json",
15
- "--prompt",
16
- "hello",
17
- "--api-key",
18
- "",
19
- ], {
20
- cwd: process.cwd(),
21
- timeout: 10_000,
22
- windowsHide: true,
23
- });
24
- } catch (err) {
25
- caught = err;
26
- }
27
-
28
- expect(caught).toMatchObject({ code: 1 });
29
- const stdout = (caught as { stdout?: string }).stdout ?? "";
30
- const lines = stdout.trim().split(/\r?\n/).filter(Boolean);
31
- expect(lines.length).toBeGreaterThan(0);
32
- for (const line of lines) {
33
- expect(() => JSON.parse(line)).not.toThrow();
34
- }
35
- expect(JSON.parse(lines.at(-1)!)).toEqual(expect.objectContaining({
36
- type: "error",
37
- }));
38
- });
39
- });
1
+ import { execFile } from "node:child_process";
2
+ import { promisify } from "node:util";
3
+
4
+ import { describe, expect, it } from "vitest";
5
+
6
+ const execFileAsync = promisify(execFile);
7
+
8
+ describe("builtin cli --stream-json", () => {
9
+ it("writes only JSON lines to stdout when startup fails", async () => {
10
+ let caught: unknown;
11
+ try {
12
+ await execFileAsync(process.execPath, [
13
+ "bin/cccagent.mjs",
14
+ "--stream-json",
15
+ "--prompt",
16
+ "hello",
17
+ "--api-key",
18
+ "",
19
+ ], {
20
+ cwd: process.cwd(),
21
+ timeout: 10_000,
22
+ windowsHide: true,
23
+ });
24
+ } catch (err) {
25
+ caught = err;
26
+ }
27
+
28
+ expect(caught).toMatchObject({ code: 1 });
29
+ const stdout = (caught as { stdout?: string }).stdout ?? "";
30
+ const lines = stdout.trim().split(/\r?\n/).filter(Boolean);
31
+ expect(lines.length).toBeGreaterThan(0);
32
+ for (const line of lines) {
33
+ expect(() => JSON.parse(line)).not.toThrow();
34
+ }
35
+ expect(JSON.parse(lines.at(-1)!)).toEqual(expect.objectContaining({
36
+ type: "error",
37
+ }));
38
+ });
39
+ });