chatccc 0.2.228 → 0.2.229
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.
- package/package.json +1 -1
- package/src/__tests__/builtin-chat-session.test.ts +329 -350
- package/src/__tests__/builtin-file-tools.test.ts +240 -275
- package/src/__tests__/builtin-skills.test.ts +284 -252
- package/src/__tests__/builtin-web-tools.test.ts +220 -220
- package/src/__tests__/restart.test.ts +132 -0
- package/src/builtin/context.ts +333 -323
- package/src/builtin/file-tools.ts +17 -2
- package/src/builtin/index.ts +12 -5
- package/src/builtin/skills.ts +205 -190
- package/src/builtin/web-tools.ts +313 -313
- package/src/index.ts +315 -310
- package/src/orchestrator.ts +2489 -2388
|
@@ -1,350 +1,329 @@
|
|
|
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
|
-
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}))
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
await
|
|
62
|
-
await writeFile(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
expect(system).toContain("
|
|
78
|
-
expect(system).toContain("
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
{
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
expect(system).toContain("
|
|
118
|
-
expect(system).toContain("
|
|
119
|
-
expect(system).toContain("
|
|
120
|
-
|
|
121
|
-
expect(system
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
expect(system).
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
},
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
},
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
)
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
|
+
import { estimateBuiltinContextTokens } from "../builtin/context.ts";
|
|
9
|
+
|
|
10
|
+
const streamTextMock = vi.fn();
|
|
11
|
+
const generateTextMock = vi.fn();
|
|
12
|
+
const createRawStreamLogMock = vi.fn();
|
|
13
|
+
const rawLogWriteLineMock = vi.fn();
|
|
14
|
+
const rawLogCloseMock = vi.fn();
|
|
15
|
+
const originalRawStreamLogs = structuredClone(config.rawStreamLogs);
|
|
16
|
+
|
|
17
|
+
vi.mock("@ai-sdk/openai-compatible", () => ({
|
|
18
|
+
createOpenAICompatible: vi.fn(() => (modelId: string) => ({ modelId })),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock("ai", () => ({
|
|
22
|
+
streamText: streamTextMock,
|
|
23
|
+
generateText: generateTextMock,
|
|
24
|
+
isLoopFinished: vi.fn(() => ({ loopFinished: true })),
|
|
25
|
+
stepCountIs: vi.fn((count: number) => ({ count })),
|
|
26
|
+
jsonSchema: vi.fn((schema: unknown) => schema),
|
|
27
|
+
tool: vi.fn((definition: unknown) => definition),
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
vi.mock("../builtin/raw-stream-log.js", () => ({
|
|
31
|
+
createRawStreamLog: createRawStreamLogMock,
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
|
|
35
|
+
const events: unknown[] = [];
|
|
36
|
+
for await (const event of iterable) events.push(event);
|
|
37
|
+
return events;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function* textStream(...chunks: string[]): AsyncIterable<string> {
|
|
41
|
+
for (const chunk of chunks) yield chunk;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function* fullStream(...parts: unknown[]): AsyncIterable<unknown> {
|
|
45
|
+
for (const part of parts) yield part;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
streamTextMock.mockReset();
|
|
50
|
+
generateTextMock.mockReset();
|
|
51
|
+
createRawStreamLogMock.mockReset();
|
|
52
|
+
rawLogWriteLineMock.mockReset();
|
|
53
|
+
rawLogCloseMock.mockReset();
|
|
54
|
+
config.rawStreamLogs = structuredClone(originalRawStreamLogs);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("ChatSession context management", () => {
|
|
58
|
+
it("injects cwd project instruction files before runtime workspace details", async () => {
|
|
59
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
60
|
+
const dir = await mkdtemp(join(tmpdir(), "deepccc-session-instructions-"));
|
|
61
|
+
await writeFile(join(dir, "AGENTS.md"), "agents root guidance", "utf-8");
|
|
62
|
+
await writeFile(join(dir, "AGENTS.local.md"), "agents local guidance", "utf-8");
|
|
63
|
+
await writeFile(join(dir, "CLAUDE.md"), "claude root guidance", "utf-8");
|
|
64
|
+
await writeFile(join(dir, "CLAUDE.local.md"), "claude local guidance", "utf-8");
|
|
65
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream() });
|
|
66
|
+
|
|
67
|
+
const session = new ChatSession(
|
|
68
|
+
{ apiKey: "sk-test" },
|
|
69
|
+
{
|
|
70
|
+
cwd: dir,
|
|
71
|
+
sessionId: "project-instructions",
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
await collect(session.chat("hi"));
|
|
75
|
+
|
|
76
|
+
const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
|
|
77
|
+
expect(system).toContain("## Project Instructions");
|
|
78
|
+
expect(system).toContain("### AGENTS.md");
|
|
79
|
+
expect(system).toContain("agents root guidance");
|
|
80
|
+
expect(system).toContain("### AGENTS.local.md");
|
|
81
|
+
expect(system).toContain("agents local guidance");
|
|
82
|
+
expect(system).toContain("### CLAUDE.md");
|
|
83
|
+
expect(system).toContain("claude root guidance");
|
|
84
|
+
expect(system).toContain("### CLAUDE.local.md");
|
|
85
|
+
expect(system).toContain("claude local guidance");
|
|
86
|
+
|
|
87
|
+
expect(system.indexOf("agents root guidance")).toBeLessThan(system.indexOf("agents local guidance"));
|
|
88
|
+
expect(system.indexOf("agents local guidance")).toBeLessThan(system.indexOf("claude root guidance"));
|
|
89
|
+
expect(system.indexOf("claude root guidance")).toBeLessThan(system.indexOf("claude local guidance"));
|
|
90
|
+
expect(system.indexOf("claude local guidance")).toBeLessThan(system.indexOf(dir));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("places the volatile skills index at the very end of system prompt", async () => {
|
|
94
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
95
|
+
const dir = await mkdtemp(join(tmpdir(), "deepccc-session-skill-order-"));
|
|
96
|
+
const skillsDir = join(dir, "skills");
|
|
97
|
+
await mkdir(join(skillsDir, "demo-skill"), { recursive: true });
|
|
98
|
+
await writeFile(
|
|
99
|
+
join(skillsDir, "demo-skill", "SKILL.md"),
|
|
100
|
+
"---\nname: demo-skill\ndescription: demo description\n---\n\nbody\n",
|
|
101
|
+
"utf-8",
|
|
102
|
+
);
|
|
103
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("ok") });
|
|
104
|
+
|
|
105
|
+
const session = new ChatSession(
|
|
106
|
+
{ apiKey: "sk-test" },
|
|
107
|
+
{
|
|
108
|
+
cwd: dir,
|
|
109
|
+
sessionId: "skill-order",
|
|
110
|
+
systemPrompt: "CUSTOM PROMPT MARKER",
|
|
111
|
+
skillsDirs: [skillsDir],
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
await collect(session.chat("hi"));
|
|
115
|
+
|
|
116
|
+
const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
|
|
117
|
+
expect(system).toContain("demo-skill");
|
|
118
|
+
expect(system).toContain("CUSTOM PROMPT MARKER");
|
|
119
|
+
expect(system).toContain("Current working directory");
|
|
120
|
+
// 稳定性排序:固定规则 → 项目指令 → runtime → custom → 技能索引(最后)
|
|
121
|
+
expect(system.indexOf("CUSTOM PROMPT MARKER")).toBeGreaterThan(
|
|
122
|
+
system.indexOf("Current working directory"),
|
|
123
|
+
);
|
|
124
|
+
expect(system.indexOf("demo-skill")).toBeGreaterThan(system.indexOf("CUSTOM PROMPT MARKER"));
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("does not read project instruction files from parent directories", async () => {
|
|
128
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
129
|
+
const parent = await mkdtemp(join(tmpdir(), "deepccc-session-parent-instructions-"));
|
|
130
|
+
const child = join(parent, "child");
|
|
131
|
+
await mkdir(child);
|
|
132
|
+
await writeFile(join(parent, "AGENTS.md"), "parent-only guidance", "utf-8");
|
|
133
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream() });
|
|
134
|
+
|
|
135
|
+
const session = new ChatSession(
|
|
136
|
+
{ apiKey: "sk-test" },
|
|
137
|
+
{
|
|
138
|
+
cwd: child,
|
|
139
|
+
sessionId: "no-parent-instructions",
|
|
140
|
+
},
|
|
141
|
+
);
|
|
142
|
+
await collect(session.chat("hi"));
|
|
143
|
+
|
|
144
|
+
const system = streamTextMock.mock.calls.at(-1)?.[0].system as string;
|
|
145
|
+
expect(system).not.toContain("parent-only guidance");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("uses loop-finished stopping by default", async () => {
|
|
149
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
150
|
+
const dir = await mkdtemp(join(tmpdir(), "deepccc-session-unlimited-"));
|
|
151
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
|
|
152
|
+
|
|
153
|
+
const session = new ChatSession(
|
|
154
|
+
{ apiKey: "sk-test" },
|
|
155
|
+
{
|
|
156
|
+
cwd: dir,
|
|
157
|
+
sessionId: "unlimited-steps",
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
await collect(session.chat("run a multi-stage workflow"));
|
|
161
|
+
|
|
162
|
+
expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
|
|
163
|
+
stopWhen: { loopFinished: true },
|
|
164
|
+
}));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("uses a configured tool step limit when provided", async () => {
|
|
168
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
169
|
+
const dir = await mkdtemp(join(tmpdir(), "deepccc-session-step-budget-"));
|
|
170
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("done") });
|
|
171
|
+
|
|
172
|
+
const session = new ChatSession(
|
|
173
|
+
{ apiKey: "sk-test" },
|
|
174
|
+
{
|
|
175
|
+
cwd: dir,
|
|
176
|
+
sessionId: "step-budget",
|
|
177
|
+
maxSteps: 7,
|
|
178
|
+
},
|
|
179
|
+
);
|
|
180
|
+
await collect(session.chat("run a bounded workflow"));
|
|
181
|
+
|
|
182
|
+
expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
|
|
183
|
+
stopWhen: { count: 7 },
|
|
184
|
+
}));
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("loads persisted context, compacts older messages, and persists the new assistant reply", async () => {
|
|
188
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
189
|
+
const dir = await mkdtemp(join(tmpdir(), "deepccc-session-context-"));
|
|
190
|
+
|
|
191
|
+
const seed = new ChatSession(
|
|
192
|
+
{ apiKey: "sk-test" },
|
|
193
|
+
{
|
|
194
|
+
persist: true,
|
|
195
|
+
contextDir: dir,
|
|
196
|
+
sessionId: "integration",
|
|
197
|
+
compactAtTokens: 10_000,
|
|
198
|
+
},
|
|
199
|
+
);
|
|
200
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("old answer") });
|
|
201
|
+
await collect(seed.chat("old question"));
|
|
202
|
+
|
|
203
|
+
generateTextMock.mockResolvedValueOnce({ text: "## Current Task\n- old question summarized" });
|
|
204
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("new answer") });
|
|
205
|
+
|
|
206
|
+
const restored = new ChatSession(
|
|
207
|
+
{ apiKey: "sk-test" },
|
|
208
|
+
{
|
|
209
|
+
persist: true,
|
|
210
|
+
contextDir: dir,
|
|
211
|
+
sessionId: "integration",
|
|
212
|
+
compactAtTokens: 1,
|
|
213
|
+
keepRecentMessages: 1,
|
|
214
|
+
},
|
|
215
|
+
);
|
|
216
|
+
const events = await collect(restored.chat("new question"));
|
|
217
|
+
|
|
218
|
+
expect(generateTextMock).toHaveBeenCalledOnce();
|
|
219
|
+
expect(generateTextMock).toHaveBeenCalledWith(expect.objectContaining({ temperature: 0 }));
|
|
220
|
+
expect(streamTextMock).toHaveBeenLastCalledWith(expect.objectContaining({
|
|
221
|
+
messages: expect.arrayContaining([
|
|
222
|
+
expect.objectContaining({ content: expect.stringContaining("old question summarized") }),
|
|
223
|
+
expect.objectContaining({ role: "user", content: "new question" }),
|
|
224
|
+
]),
|
|
225
|
+
}));
|
|
226
|
+
expect(events).toContainEqual({ type: "compact", compactedMessages: 2 });
|
|
227
|
+
expect(restored.history.map((m) => m.content).join("\n")).toContain("new answer");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("streams tool calls and tool results from fullStream", async () => {
|
|
231
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
232
|
+
const dir = await mkdtemp(join(tmpdir(), "deepccc-session-tools-"));
|
|
233
|
+
const session = new ChatSession(
|
|
234
|
+
{ apiKey: "sk-test" },
|
|
235
|
+
{
|
|
236
|
+
persist: true,
|
|
237
|
+
contextDir: dir,
|
|
238
|
+
sessionId: "tools",
|
|
239
|
+
},
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
streamTextMock.mockReturnValueOnce({
|
|
243
|
+
fullStream: fullStream(
|
|
244
|
+
{ type: "tool-call", toolCallId: "call-1", toolName: "read_file", input: { path: "package.json" } },
|
|
245
|
+
{ type: "tool-result", toolCallId: "call-1", toolName: "read_file", output: { content: "{}" } },
|
|
246
|
+
{ type: "text-delta", text: "done" },
|
|
247
|
+
),
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const events = await collect(session.chat("read package"));
|
|
251
|
+
|
|
252
|
+
expect(events).toContainEqual({
|
|
253
|
+
type: "tool_use",
|
|
254
|
+
id: "call-1",
|
|
255
|
+
name: "read_file",
|
|
256
|
+
input: { path: "package.json" },
|
|
257
|
+
});
|
|
258
|
+
expect(events).toContainEqual({
|
|
259
|
+
type: "tool_result",
|
|
260
|
+
tool_use_id: "call-1",
|
|
261
|
+
name: "read_file",
|
|
262
|
+
content: { content: "{}" },
|
|
263
|
+
is_error: false,
|
|
264
|
+
});
|
|
265
|
+
expect(events).toContainEqual({ type: "text", text: "done", accumulated: "done" });
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("writes raw DeepCCC fullStream parts when raw stream logs are enabled", async () => {
|
|
269
|
+
const { ChatSession } = await import("../builtin/index.ts");
|
|
270
|
+
config.rawStreamLogs = {
|
|
271
|
+
enabled: true,
|
|
272
|
+
maxBytesPerTurn: 4096,
|
|
273
|
+
retentionDays: 3,
|
|
274
|
+
keepCompleted: true,
|
|
275
|
+
};
|
|
276
|
+
createRawStreamLogMock.mockResolvedValueOnce({
|
|
277
|
+
filePath: "raw.jsonl.gz",
|
|
278
|
+
writeLine: rawLogWriteLineMock,
|
|
279
|
+
close: rawLogCloseMock,
|
|
280
|
+
});
|
|
281
|
+
const session = new ChatSession(
|
|
282
|
+
{ apiKey: "sk-test" },
|
|
283
|
+
{
|
|
284
|
+
sessionId: "raw-log-session",
|
|
285
|
+
},
|
|
286
|
+
);
|
|
287
|
+
const textPart = { type: "text-delta", text: "hello" };
|
|
288
|
+
const toolPart = {
|
|
289
|
+
type: "tool-call",
|
|
290
|
+
toolCallId: "call-1",
|
|
291
|
+
toolName: "read_file",
|
|
292
|
+
input: { path: "package.json" },
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
streamTextMock.mockReturnValueOnce({
|
|
296
|
+
fullStream: fullStream(textPart, toolPart),
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
await collect(session.chat("hi"));
|
|
300
|
+
|
|
301
|
+
expect(createRawStreamLogMock).toHaveBeenCalledWith(expect.objectContaining({
|
|
302
|
+
enabled: true,
|
|
303
|
+
tool: "deepccc",
|
|
304
|
+
sessionId: "raw-log-session",
|
|
305
|
+
label: "prompt",
|
|
306
|
+
maxBytesPerTurn: 4096,
|
|
307
|
+
retentionDays: 3,
|
|
308
|
+
}));
|
|
309
|
+
expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(1, JSON.stringify(textPart));
|
|
310
|
+
expect(rawLogWriteLineMock).toHaveBeenNthCalledWith(2, JSON.stringify(toolPart));
|
|
311
|
+
expect(rawLogCloseMock).toHaveBeenCalledWith({ keep: true });
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
describe("estimateBuiltinContextTokens", () => {
|
|
316
|
+
it("weights CJK characters closer to one token per character", () => {
|
|
317
|
+
const messages = [{ role: "user" as const, content: "你好".repeat(100) }];
|
|
318
|
+
const estimate = estimateBuiltinContextTokens("", messages);
|
|
319
|
+
// 200 个汉字 ≈ 200 tokens(旧算法 chars/3 只估到 ~70,明显低估)
|
|
320
|
+
expect(estimate).toBeGreaterThanOrEqual(180);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("keeps latin text near chars/3.5", () => {
|
|
324
|
+
const messages = [{ role: "user" as const, content: "a".repeat(1000) }];
|
|
325
|
+
const estimate = estimateBuiltinContextTokens("", messages);
|
|
326
|
+
expect(estimate).toBeGreaterThan(250);
|
|
327
|
+
expect(estimate).toBeLessThan(340);
|
|
328
|
+
});
|
|
329
|
+
});
|