chatccc 0.2.197 → 0.2.199

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 (52) hide show
  1. package/agent-prompts/cursor_specific.md +13 -13
  2. package/bin/cccagent.mjs +17 -17
  3. package/config.sample.json +27 -27
  4. package/package.json +2 -1
  5. package/src/__tests__/agent-reload-config-rpc.test.ts +99 -99
  6. package/src/__tests__/builtin-chat-session.test.ts +277 -277
  7. package/src/__tests__/builtin-cli-json.test.ts +39 -39
  8. package/src/__tests__/builtin-config.test.ts +33 -33
  9. package/src/__tests__/builtin-context.test.ts +163 -163
  10. package/src/__tests__/builtin-file-tools.test.ts +224 -224
  11. package/src/__tests__/builtin-session-select.test.ts +116 -116
  12. package/src/__tests__/builtin-sigint.test.ts +56 -56
  13. package/src/__tests__/cards.test.ts +109 -109
  14. package/src/__tests__/ccc-adapter.test.ts +114 -114
  15. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  16. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  17. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  18. package/src/__tests__/claude-raw-stream-log.test.ts +87 -87
  19. package/src/__tests__/codex-raw-stream-log.test.ts +163 -120
  20. package/src/__tests__/config-reload.test.ts +10 -10
  21. package/src/__tests__/config-sample.test.ts +18 -18
  22. package/src/__tests__/cursor-adapter.test.ts +114 -1
  23. package/src/__tests__/feishu-avatar.test.ts +40 -40
  24. package/src/__tests__/jsonl-stream.test.ts +79 -0
  25. package/src/__tests__/orchestrator.test.ts +200 -200
  26. package/src/__tests__/raw-stream-log.test.ts +106 -106
  27. package/src/__tests__/session.test.ts +40 -40
  28. package/src/__tests__/sim-platform.test.ts +12 -12
  29. package/src/__tests__/web-ui.test.ts +209 -209
  30. package/src/adapters/ccc-adapter.ts +121 -121
  31. package/src/adapters/claude-adapter.ts +603 -603
  32. package/src/adapters/codex-adapter.ts +380 -392
  33. package/src/adapters/cursor-adapter.ts +56 -30
  34. package/src/adapters/jsonl-stream.ts +157 -0
  35. package/src/adapters/raw-stream-log.ts +124 -124
  36. package/src/agent-reload-config-rpc.ts +34 -34
  37. package/src/builtin/cli.ts +473 -473
  38. package/src/builtin/context.ts +323 -323
  39. package/src/builtin/file-tools.ts +1072 -1072
  40. package/src/builtin/index.ts +404 -404
  41. package/src/builtin/session-select.ts +48 -48
  42. package/src/builtin/sigint.ts +50 -50
  43. package/src/cards.ts +195 -195
  44. package/src/chatgpt-subscription-rpc.ts +27 -27
  45. package/src/chatgpt-subscription.ts +299 -299
  46. package/src/chrome-devtools-guard.ts +318 -318
  47. package/src/config.ts +125 -125
  48. package/src/feishu-api.ts +49 -49
  49. package/src/orchestrator.ts +179 -179
  50. package/src/runtime-reload.ts +34 -34
  51. package/src/session.ts +141 -141
  52. 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
+ });