chatccc 0.2.226 → 0.2.227

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 (64) 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/package.json +1 -1
  6. package/src/__tests__/agent-activity.test.ts +76 -76
  7. package/src/__tests__/builtin-chat-session.test.ts +350 -350
  8. package/src/__tests__/builtin-config.test.ts +26 -26
  9. package/src/__tests__/builtin-context.test.ts +163 -163
  10. package/src/__tests__/builtin-file-tools.test.ts +275 -275
  11. package/src/__tests__/builtin-permissions.test.ts +211 -211
  12. package/src/__tests__/builtin-session-select.test.ts +116 -116
  13. package/src/__tests__/builtin-skills.test.ts +185 -74
  14. package/src/__tests__/card-action-routing.test.ts +18 -18
  15. package/src/__tests__/ccc-adapter.test.ts +136 -136
  16. package/src/__tests__/claude-adapter.test.ts +614 -614
  17. package/src/__tests__/codex-adapter.test.ts +58 -58
  18. package/src/__tests__/codex-raw-stream-log.test.ts +170 -170
  19. package/src/__tests__/cursor-adapter.test.ts +268 -268
  20. package/src/__tests__/feishu-avatar.test.ts +164 -164
  21. package/src/__tests__/feishu-message-ingress.test.ts +138 -138
  22. package/src/__tests__/package-files.test.ts +24 -24
  23. package/src/__tests__/progress-reducer.test.ts +110 -110
  24. package/src/__tests__/response-stall.test.ts +49 -49
  25. package/src/__tests__/sim-platform.test.ts +16 -16
  26. package/src/__tests__/startup-lifecycle.test.ts +231 -231
  27. package/src/__tests__/stop-session.test.ts +34 -34
  28. package/src/__tests__/terminal-renderer.test.ts +247 -247
  29. package/src/__tests__/update-command-guard.test.ts +144 -144
  30. package/src/__tests__/web-ui.test.ts +326 -326
  31. package/src/adapters/adapter-interface.ts +18 -18
  32. package/src/adapters/ccc-adapter.ts +131 -131
  33. package/src/adapters/claude-adapter.ts +620 -620
  34. package/src/adapters/codex-adapter.ts +426 -426
  35. package/src/adapters/cursor-adapter.ts +681 -681
  36. package/src/agent-activity.ts +170 -170
  37. package/src/agent-delegate-task.ts +91 -91
  38. package/src/builtin/cli.ts +61 -2
  39. package/src/builtin/config.ts +84 -84
  40. package/src/builtin/context.ts +323 -323
  41. package/src/builtin/file-log.ts +38 -38
  42. package/src/builtin/index.ts +44 -24
  43. package/src/builtin/proc-tree-kill.ts +61 -61
  44. package/src/builtin/progress/cards-helpers.ts +76 -76
  45. package/src/builtin/progress/reducer.ts +108 -108
  46. package/src/builtin/progress/terminal-renderer.ts +294 -294
  47. package/src/builtin/progress/view.ts +77 -77
  48. package/src/builtin/raw-stream-log.ts +124 -124
  49. package/src/builtin/session-select.ts +48 -48
  50. package/src/builtin/skills.ts +126 -44
  51. package/src/card-action-routing.ts +14 -14
  52. package/src/feishu-api.ts +193 -193
  53. package/src/feishu-message-ingress.ts +195 -195
  54. package/src/index.ts +306 -306
  55. package/src/orchestrator.ts +2388 -2388
  56. package/src/platform-adapter.ts +6 -6
  57. package/src/progress/reducer.ts +108 -108
  58. package/src/progress/terminal-renderer.ts +294 -294
  59. package/src/progress/view.ts +77 -77
  60. package/src/response-stall.ts +28 -28
  61. package/src/session-chat-binding.ts +82 -82
  62. package/src/startup-lifecycle.ts +250 -250
  63. package/src/stream-state.ts +18 -18
  64. package/src/update-command-guard.ts +165 -165
@@ -1,350 +1,350 @@
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 "../builtin/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("../builtin/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 codex-style skills", () => {
57
- it("injects skill index into system prompt from skillsDirs", async () => {
58
- const { ChatSession } = await import("../builtin/index.ts");
59
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-skills-"));
60
- const skillsDir = join(dir, ".codex", "skills");
61
- await mkdir(join(skillsDir, "demo-skill"), { recursive: true });
62
- await writeFile(
63
- join(skillsDir, "demo-skill", "SKILL.md"),
64
- "---\nname: demo-skill\ndescription: 演示技能\n---\n\n# 演示",
65
- "utf-8",
66
- );
67
- streamTextMock.mockReturnValueOnce({ textStream: textStream() });
68
-
69
- const session = new ChatSession(
70
- { apiKey: "sk-test" },
71
- { cwd: dir, sessionId: "skill-index", skillsDirs: [skillsDir] },
72
- );
73
- await collect(session.chat("hi"));
74
-
75
- const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
76
- expect(system).toContain("## Available Skills");
77
- expect(system).toContain("demo-skill");
78
- expect(system).toContain("演示技能");
79
- });
80
-
81
- it("skips skill injection when no skills are found", async () => {
82
- const { ChatSession } = await import("../builtin/index.ts");
83
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-no-skills-"));
84
- streamTextMock.mockReturnValueOnce({ textStream: textStream() });
85
-
86
- const session = new ChatSession(
87
- { apiKey: "sk-test" },
88
- { cwd: dir, sessionId: "no-skills", skillsDirs: [join(dir, "missing")] },
89
- );
90
- await collect(session.chat("hi"));
91
-
92
- const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
93
- expect(system).not.toContain("## Available Skills");
94
- });
95
- });
96
- describe("ChatSession context management", () => {
97
- it("injects cwd project instruction files before runtime workspace details", async () => {
98
- const { ChatSession } = await import("../builtin/index.ts");
99
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-instructions-"));
100
- await writeFile(join(dir, "AGENTS.md"), "agents root guidance", "utf-8");
101
- await writeFile(join(dir, "AGENTS.local.md"), "agents local guidance", "utf-8");
102
- await writeFile(join(dir, "CLAUDE.md"), "claude root guidance", "utf-8");
103
- await writeFile(join(dir, "CLAUDE.local.md"), "claude local guidance", "utf-8");
104
- streamTextMock.mockReturnValueOnce({ textStream: textStream() });
105
-
106
- const session = new ChatSession(
107
- { apiKey: "sk-test" },
108
- {
109
- cwd: dir,
110
- sessionId: "project-instructions",
111
- },
112
- );
113
- await collect(session.chat("hi"));
114
-
115
- const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
116
- expect(system).toContain("## Project Instructions");
117
- expect(system).toContain("### AGENTS.md");
118
- expect(system).toContain("agents root guidance");
119
- expect(system).toContain("### AGENTS.local.md");
120
- expect(system).toContain("agents local guidance");
121
- expect(system).toContain("### CLAUDE.md");
122
- expect(system).toContain("claude root guidance");
123
- expect(system).toContain("### CLAUDE.local.md");
124
- expect(system).toContain("claude local guidance");
125
-
126
- expect(system.indexOf("agents root guidance")).toBeLessThan(system.indexOf("agents local guidance"));
127
- expect(system.indexOf("agents local guidance")).toBeLessThan(system.indexOf("claude root guidance"));
128
- expect(system.indexOf("claude root guidance")).toBeLessThan(system.indexOf("claude local guidance"));
129
- expect(system.indexOf("claude local guidance")).toBeLessThan(system.indexOf(dir));
130
- });
131
-
132
- it("does not read project instruction files from parent directories", async () => {
133
- const { ChatSession } = await import("../builtin/index.ts");
134
- const parent = await mkdtemp(join(tmpdir(), "chatccc-session-parent-instructions-"));
135
- const child = join(parent, "child");
136
- await mkdir(child);
137
- await writeFile(join(parent, "AGENTS.md"), "parent-only guidance", "utf-8");
138
- streamTextMock.mockReturnValueOnce({ textStream: textStream() });
139
-
140
- const session = new ChatSession(
141
- { apiKey: "sk-test" },
142
- {
143
- cwd: child,
144
- sessionId: "no-parent-instructions",
145
- },
146
- );
147
- await collect(session.chat("hi"));
148
-
149
- const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
150
- expect(system).not.toContain("parent-only guidance");
151
- });
152
-
153
- it("uses loop-finished stopping by default", async () => {
154
- const { ChatSession } = await import("../builtin/index.ts");
155
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-unlimited-"));
156
- streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
157
-
158
- const session = new ChatSession(
159
- { apiKey: "sk-test" },
160
- {
161
- cwd: dir,
162
- sessionId: "unlimited-steps",
163
- },
164
- );
165
- await collect(session.chat("run a multi-stage workflow"));
166
-
167
- expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
168
- stopWhen: { loopFinished: true },
169
- }));
170
- });
171
-
172
- it("uses a configured tool step limit when provided", async () => {
173
- const { ChatSession } = await import("../builtin/index.ts");
174
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-step-budget-"));
175
- streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
176
-
177
- const session = new ChatSession(
178
- { apiKey: "sk-test" },
179
- {
180
- cwd: dir,
181
- sessionId: "step-budget",
182
- maxSteps: 7,
183
- },
184
- );
185
- await collect(session.chat("run a bounded workflow"));
186
-
187
- expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
188
- stopWhen: { count: 7 },
189
- }));
190
- });
191
-
192
- it("loads persisted context, compacts older messages, and persists the new assistant reply", async () => {
193
- const { ChatSession } = await import("../builtin/index.ts");
194
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-context-"));
195
-
196
- const seed = new ChatSession(
197
- { apiKey: "sk-test" },
198
- {
199
- persist: true,
200
- contextDir: dir,
201
- sessionId: "integration",
202
- compactAtTokens: 10_000,
203
- },
204
- );
205
- streamTextMock.mockReturnValueOnce({ textStream: textStream("old answer") });
206
- await collect(seed.chat("old question"));
207
-
208
- generateTextMock.mockResolvedValueOnce({ text: "## 当前任务状态\n- 旧问题已总结" });
209
- streamTextMock.mockReturnValueOnce({ textStream: textStream("new answer") });
210
-
211
- const restored = new ChatSession(
212
- { apiKey: "sk-test" },
213
- {
214
- persist: true,
215
- contextDir: dir,
216
- sessionId: "integration",
217
- compactAtTokens: 1,
218
- keepRecentMessages: 1,
219
- },
220
- );
221
- const events = await collect(restored.chat("new question"));
222
-
223
- expect(generateTextMock).toHaveBeenCalledOnce();
224
- expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
225
- messages: expect.arrayContaining([
226
- expect.objectContaining({ content: expect.stringContaining("旧问题已总结") }),
227
- expect.objectContaining({ role: "user", content: "new question" }),
228
- ]),
229
- }));
230
- expect(events).toContainEqual({ type: "compact", compactedMessages: 2 });
231
- expect(restored.history.map((m) => m.content).join("\n")).toContain("new answer");
232
- });
233
-
234
- it("streams tool calls and tool results from fullStream", async () => {
235
- const { ChatSession } = await import("../builtin/index.ts");
236
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-tools-"));
237
- const session = new ChatSession(
238
- { apiKey: "sk-test" },
239
- {
240
- persist: true,
241
- contextDir: dir,
242
- sessionId: "tools",
243
- },
244
- );
245
-
246
- streamTextMock.mockReturnValueOnce({
247
- fullStream: fullStream(
248
- { type: "tool-call", toolCallId: "call-1", toolName: "read_file", input: { path: "package.json" } },
249
- { type: "tool-result", toolCallId: "call-1", toolName: "read_file", output: { content: "{}" } },
250
- { type: "text-delta", text: "done" },
251
- ),
252
- });
253
-
254
- const events = await collect(session.chat("read package"));
255
-
256
- expect(events).toContainEqual({
257
- type: "tool_use",
258
- id: "call-1",
259
- name: "read_file",
260
- input: { path: "package.json" },
261
- });
262
- expect(events).toContainEqual({
263
- type: "tool_result",
264
- tool_use_id: "call-1",
265
- name: "read_file",
266
- content: { content: "{}" },
267
- is_error: false,
268
- });
269
- expect(events).toContainEqual({ type: "text", text: "done", accumulated: "done" });
270
- });
271
-
272
- it("writes raw CCC fullStream parts when raw stream logs are enabled", async () => {
273
- const { ChatSession } = await import("../builtin/index.ts");
274
- config.rawStreamLogs = {
275
- enabled: true,
276
- maxBytesPerTurn: 4096,
277
- retentionDays: 3,
278
- keepCompleted: true,
279
- };
280
- createRawStreamLogMock.mockResolvedValueOnce({
281
- filePath: "raw.jsonl.gz",
282
- writeLine: rawLogWriteLineMock,
283
- close: rawLogCloseMock,
284
- });
285
- const session = new ChatSession(
286
- { apiKey: "sk-test" },
287
- {
288
- sessionId: "raw-log-session",
289
- },
290
- );
291
- const textPart = { type: "text-delta", text: "hello" };
292
- const toolPart = {
293
- type: "tool-call",
294
- toolCallId: "call-1",
295
- toolName: "read_file",
296
- input: { path: "package.json" },
297
- };
298
-
299
- streamTextMock.mockReturnValueOnce({
300
- fullStream: fullStream(textPart, toolPart),
301
- });
302
-
303
- await collect(session.chat("hi"));
304
-
305
- expect(createRawStreamLogMock).toHaveBeenCalledWith(expect.objectContaining({
306
- enabled: true,
307
- tool: "deepccc",
308
- sessionId: "raw-log-session",
309
- label: "prompt",
310
- maxBytesPerTurn: 4096,
311
- retentionDays: 3,
312
- }));
313
- expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(1, JSON.stringify(textPart));
314
- expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(2, JSON.stringify(toolPart));
315
- expect(rawLogCloseMock).toHaveBeenCalledWith({ keep: true });
316
- });
317
- });
318
-
319
- describe("ChatSession effort passthrough", () => {
320
- it("passes reasoningEffort via providerOptions when effort is configured", async () => {
321
- const { ChatSession } = await import("../builtin/index.ts");
322
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-effort-high-"));
323
- streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
324
-
325
- const session = new ChatSession(
326
- { apiKey: "sk-test", effort: "high" },
327
- { cwd: dir, sessionId: "effort-high" },
328
- );
329
- await collect(session.chat("hi"));
330
-
331
- expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
332
- providerOptions: { deepseek: { reasoningEffort: "high" } },
333
- }));
334
- });
335
-
336
- it("omits providerOptions when effort is not set", async () => {
337
- const { ChatSession } = await import("../builtin/index.ts");
338
- const dir = await mkdtemp(join(tmpdir(), "chatccc-session-effort-none-"));
339
- streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
340
-
341
- const session = new ChatSession(
342
- { apiKey: "sk-test", effort: "" },
343
- { cwd: dir, sessionId: "effort-none" },
344
- );
345
- await collect(session.chat("hi"));
346
-
347
- const lastCall = streamTextMock.mock.calls.at(-1)?.[0] as Record<string, unknown>;
348
- expect(lastCall.providerOptions).toBeUndefined();
349
- });
350
- });
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 "../builtin/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("../builtin/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 codex-style skills", () => {
57
+ it("injects skill index into system prompt from skillsDirs", async () => {
58
+ const { ChatSession } = await import("../builtin/index.ts");
59
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-skills-"));
60
+ const skillsDir = join(dir, ".codex", "skills");
61
+ await mkdir(join(skillsDir, "demo-skill"), { recursive: true });
62
+ await writeFile(
63
+ join(skillsDir, "demo-skill", "SKILL.md"),
64
+ "---\nname: demo-skill\ndescription: 演示技能\n---\n\n# 演示",
65
+ "utf-8",
66
+ );
67
+ streamTextMock.mockReturnValueOnce({ textStream: textStream() });
68
+
69
+ const session = new ChatSession(
70
+ { apiKey: "sk-test" },
71
+ { cwd: dir, sessionId: "skill-index", skillsDirs: [skillsDir] },
72
+ );
73
+ await collect(session.chat("hi"));
74
+
75
+ const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
76
+ expect(system).toContain("## Available Skills");
77
+ expect(system).toContain("demo-skill");
78
+ expect(system).toContain("演示技能");
79
+ });
80
+
81
+ it("skips skill injection when no skills are found", async () => {
82
+ const { ChatSession } = await import("../builtin/index.ts");
83
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-no-skills-"));
84
+ streamTextMock.mockReturnValueOnce({ textStream: textStream() });
85
+
86
+ const session = new ChatSession(
87
+ { apiKey: "sk-test" },
88
+ { cwd: dir, sessionId: "no-skills", skillsDirs: [join(dir, "missing")] },
89
+ );
90
+ await collect(session.chat("hi"));
91
+
92
+ const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
93
+ expect(system).not.toContain("## Available Skills");
94
+ });
95
+ });
96
+ describe("ChatSession context management", () => {
97
+ it("injects cwd project instruction files before runtime workspace details", async () => {
98
+ const { ChatSession } = await import("../builtin/index.ts");
99
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-instructions-"));
100
+ await writeFile(join(dir, "AGENTS.md"), "agents root guidance", "utf-8");
101
+ await writeFile(join(dir, "AGENTS.local.md"), "agents local guidance", "utf-8");
102
+ await writeFile(join(dir, "CLAUDE.md"), "claude root guidance", "utf-8");
103
+ await writeFile(join(dir, "CLAUDE.local.md"), "claude local guidance", "utf-8");
104
+ streamTextMock.mockReturnValueOnce({ textStream: textStream() });
105
+
106
+ const session = new ChatSession(
107
+ { apiKey: "sk-test" },
108
+ {
109
+ cwd: dir,
110
+ sessionId: "project-instructions",
111
+ },
112
+ );
113
+ await collect(session.chat("hi"));
114
+
115
+ const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
116
+ expect(system).toContain("## Project Instructions");
117
+ expect(system).toContain("### AGENTS.md");
118
+ expect(system).toContain("agents root guidance");
119
+ expect(system).toContain("### AGENTS.local.md");
120
+ expect(system).toContain("agents local guidance");
121
+ expect(system).toContain("### CLAUDE.md");
122
+ expect(system).toContain("claude root guidance");
123
+ expect(system).toContain("### CLAUDE.local.md");
124
+ expect(system).toContain("claude local guidance");
125
+
126
+ expect(system.indexOf("agents root guidance")).toBeLessThan(system.indexOf("agents local guidance"));
127
+ expect(system.indexOf("agents local guidance")).toBeLessThan(system.indexOf("claude root guidance"));
128
+ expect(system.indexOf("claude root guidance")).toBeLessThan(system.indexOf("claude local guidance"));
129
+ expect(system.indexOf("claude local guidance")).toBeLessThan(system.indexOf(dir));
130
+ });
131
+
132
+ it("does not read project instruction files from parent directories", async () => {
133
+ const { ChatSession } = await import("../builtin/index.ts");
134
+ const parent = await mkdtemp(join(tmpdir(), "chatccc-session-parent-instructions-"));
135
+ const child = join(parent, "child");
136
+ await mkdir(child);
137
+ await writeFile(join(parent, "AGENTS.md"), "parent-only guidance", "utf-8");
138
+ streamTextMock.mockReturnValueOnce({ textStream: textStream() });
139
+
140
+ const session = new ChatSession(
141
+ { apiKey: "sk-test" },
142
+ {
143
+ cwd: child,
144
+ sessionId: "no-parent-instructions",
145
+ },
146
+ );
147
+ await collect(session.chat("hi"));
148
+
149
+ const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
150
+ expect(system).not.toContain("parent-only guidance");
151
+ });
152
+
153
+ it("uses loop-finished stopping by default", async () => {
154
+ const { ChatSession } = await import("../builtin/index.ts");
155
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-unlimited-"));
156
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
157
+
158
+ const session = new ChatSession(
159
+ { apiKey: "sk-test" },
160
+ {
161
+ cwd: dir,
162
+ sessionId: "unlimited-steps",
163
+ },
164
+ );
165
+ await collect(session.chat("run a multi-stage workflow"));
166
+
167
+ expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
168
+ stopWhen: { loopFinished: true },
169
+ }));
170
+ });
171
+
172
+ it("uses a configured tool step limit when provided", async () => {
173
+ const { ChatSession } = await import("../builtin/index.ts");
174
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-step-budget-"));
175
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
176
+
177
+ const session = new ChatSession(
178
+ { apiKey: "sk-test" },
179
+ {
180
+ cwd: dir,
181
+ sessionId: "step-budget",
182
+ maxSteps: 7,
183
+ },
184
+ );
185
+ await collect(session.chat("run a bounded workflow"));
186
+
187
+ expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
188
+ stopWhen: { count: 7 },
189
+ }));
190
+ });
191
+
192
+ it("loads persisted context, compacts older messages, and persists the new assistant reply", async () => {
193
+ const { ChatSession } = await import("../builtin/index.ts");
194
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-context-"));
195
+
196
+ const seed = new ChatSession(
197
+ { apiKey: "sk-test" },
198
+ {
199
+ persist: true,
200
+ contextDir: dir,
201
+ sessionId: "integration",
202
+ compactAtTokens: 10_000,
203
+ },
204
+ );
205
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("old answer") });
206
+ await collect(seed.chat("old question"));
207
+
208
+ generateTextMock.mockResolvedValueOnce({ text: "## 当前任务状态\n- 旧问题已总结" });
209
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("new answer") });
210
+
211
+ const restored = new ChatSession(
212
+ { apiKey: "sk-test" },
213
+ {
214
+ persist: true,
215
+ contextDir: dir,
216
+ sessionId: "integration",
217
+ compactAtTokens: 1,
218
+ keepRecentMessages: 1,
219
+ },
220
+ );
221
+ const events = await collect(restored.chat("new question"));
222
+
223
+ expect(generateTextMock).toHaveBeenCalledOnce();
224
+ expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
225
+ messages: expect.arrayContaining([
226
+ expect.objectContaining({ content: expect.stringContaining("旧问题已总结") }),
227
+ expect.objectContaining({ role: "user", content: "new question" }),
228
+ ]),
229
+ }));
230
+ expect(events).toContainEqual({ type: "compact", compactedMessages: 2 });
231
+ expect(restored.history.map((m) => m.content).join("\n")).toContain("new answer");
232
+ });
233
+
234
+ it("streams tool calls and tool results from fullStream", async () => {
235
+ const { ChatSession } = await import("../builtin/index.ts");
236
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-tools-"));
237
+ const session = new ChatSession(
238
+ { apiKey: "sk-test" },
239
+ {
240
+ persist: true,
241
+ contextDir: dir,
242
+ sessionId: "tools",
243
+ },
244
+ );
245
+
246
+ streamTextMock.mockReturnValueOnce({
247
+ fullStream: fullStream(
248
+ { type: "tool-call", toolCallId: "call-1", toolName: "read_file", input: { path: "package.json" } },
249
+ { type: "tool-result", toolCallId: "call-1", toolName: "read_file", output: { content: "{}" } },
250
+ { type: "text-delta", text: "done" },
251
+ ),
252
+ });
253
+
254
+ const events = await collect(session.chat("read package"));
255
+
256
+ expect(events).toContainEqual({
257
+ type: "tool_use",
258
+ id: "call-1",
259
+ name: "read_file",
260
+ input: { path: "package.json" },
261
+ });
262
+ expect(events).toContainEqual({
263
+ type: "tool_result",
264
+ tool_use_id: "call-1",
265
+ name: "read_file",
266
+ content: { content: "{}" },
267
+ is_error: false,
268
+ });
269
+ expect(events).toContainEqual({ type: "text", text: "done", accumulated: "done" });
270
+ });
271
+
272
+ it("writes raw CCC fullStream parts when raw stream logs are enabled", async () => {
273
+ const { ChatSession } = await import("../builtin/index.ts");
274
+ config.rawStreamLogs = {
275
+ enabled: true,
276
+ maxBytesPerTurn: 4096,
277
+ retentionDays: 3,
278
+ keepCompleted: true,
279
+ };
280
+ createRawStreamLogMock.mockResolvedValueOnce({
281
+ filePath: "raw.jsonl.gz",
282
+ writeLine: rawLogWriteLineMock,
283
+ close: rawLogCloseMock,
284
+ });
285
+ const session = new ChatSession(
286
+ { apiKey: "sk-test" },
287
+ {
288
+ sessionId: "raw-log-session",
289
+ },
290
+ );
291
+ const textPart = { type: "text-delta", text: "hello" };
292
+ const toolPart = {
293
+ type: "tool-call",
294
+ toolCallId: "call-1",
295
+ toolName: "read_file",
296
+ input: { path: "package.json" },
297
+ };
298
+
299
+ streamTextMock.mockReturnValueOnce({
300
+ fullStream: fullStream(textPart, toolPart),
301
+ });
302
+
303
+ await collect(session.chat("hi"));
304
+
305
+ expect(createRawStreamLogMock).toHaveBeenCalledWith(expect.objectContaining({
306
+ enabled: true,
307
+ tool: "deepccc",
308
+ sessionId: "raw-log-session",
309
+ label: "prompt",
310
+ maxBytesPerTurn: 4096,
311
+ retentionDays: 3,
312
+ }));
313
+ expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(1, JSON.stringify(textPart));
314
+ expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(2, JSON.stringify(toolPart));
315
+ expect(rawLogCloseMock).toHaveBeenCalledWith({ keep: true });
316
+ });
317
+ });
318
+
319
+ describe("ChatSession effort passthrough", () => {
320
+ it("passes reasoningEffort via providerOptions when effort is configured", async () => {
321
+ const { ChatSession } = await import("../builtin/index.ts");
322
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-effort-high-"));
323
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
324
+
325
+ const session = new ChatSession(
326
+ { apiKey: "sk-test", effort: "high" },
327
+ { cwd: dir, sessionId: "effort-high" },
328
+ );
329
+ await collect(session.chat("hi"));
330
+
331
+ expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
332
+ providerOptions: { deepseek: { reasoningEffort: "high" } },
333
+ }));
334
+ });
335
+
336
+ it("omits providerOptions when effort is not set", async () => {
337
+ const { ChatSession } = await import("../builtin/index.ts");
338
+ const dir = await mkdtemp(join(tmpdir(), "chatccc-session-effort-none-"));
339
+ streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
340
+
341
+ const session = new ChatSession(
342
+ { apiKey: "sk-test", effort: "" },
343
+ { cwd: dir, sessionId: "effort-none" },
344
+ );
345
+ await collect(session.chat("hi"));
346
+
347
+ const lastCall = streamTextMock.mock.calls.at(-1)?.[0] as Record<string, unknown>;
348
+ expect(lastCall.providerOptions).toBeUndefined();
349
+ });
350
+ });