chatccc 0.2.196 → 0.2.197

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 (51) 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 +1 -1
  5. package/src/__tests__/agent-reload-config-rpc.test.ts +99 -0
  6. package/src/__tests__/builtin-chat-session.test.ts +277 -181
  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 -196
  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 -113
  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 -0
  19. package/src/__tests__/codex-raw-stream-log.test.ts +120 -0
  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 +113 -113
  23. package/src/__tests__/feishu-avatar.test.ts +40 -40
  24. package/src/__tests__/orchestrator.test.ts +181 -154
  25. package/src/__tests__/raw-stream-log.test.ts +106 -106
  26. package/src/__tests__/session.test.ts +40 -40
  27. package/src/__tests__/sim-platform.test.ts +12 -12
  28. package/src/__tests__/web-ui.test.ts +209 -209
  29. package/src/adapters/ccc-adapter.ts +121 -119
  30. package/src/adapters/claude-adapter.ts +603 -566
  31. package/src/adapters/codex-adapter.ts +57 -32
  32. package/src/adapters/cursor-adapter.ts +264 -264
  33. package/src/adapters/raw-stream-log.ts +124 -124
  34. package/src/agent-reload-config-rpc.ts +34 -0
  35. package/src/builtin/cli.ts +473 -461
  36. package/src/builtin/context.ts +323 -323
  37. package/src/builtin/file-tools.ts +1072 -915
  38. package/src/builtin/index.ts +404 -353
  39. package/src/builtin/session-select.ts +48 -48
  40. package/src/builtin/sigint.ts +50 -50
  41. package/src/cards.ts +195 -195
  42. package/src/chatgpt-subscription-rpc.ts +27 -27
  43. package/src/chatgpt-subscription.ts +299 -299
  44. package/src/chrome-devtools-guard.ts +318 -318
  45. package/src/config.ts +125 -125
  46. package/src/feishu-api.ts +49 -49
  47. package/src/index.ts +8 -13
  48. package/src/orchestrator.ts +166 -145
  49. package/src/runtime-reload.ts +34 -0
  50. package/src/session.ts +141 -141
  51. package/src/web-ui.ts +205 -205
@@ -1,353 +1,404 @@
1
- /**
2
- * builtin/index.ts — ChatCCC 内置 Agent 核心 API
3
- *
4
- * ChatSession 是程序化入口,既可以被 CLI 调用,也可以被其他模块(如 ToolAdapter)调用。
5
- */
6
-
7
- import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
8
- import { generateText, stepCountIs, streamText, type TextStreamPart } from "ai";
9
-
10
- import { config as appConfig, RAW_STREAM_LOGS_DIR } from "../config.ts";
11
- import {
12
- createRawStreamLog,
13
- type RawStreamLogHandle,
14
- } from "../adapters/raw-stream-log.ts";
15
- import {
16
- BuiltinContextManager,
17
- buildSummaryPrompt,
18
- defaultBuiltinSessionId,
19
- } from "./context.ts";
20
- import { createBuiltinFileTools } from "./file-tools.ts";
21
-
22
- // ---------------------------------------------------------------------------
23
- // 系统提示词 — 编译期冻结常量
24
- // ---------------------------------------------------------------------------
25
-
26
- const SYSTEM_PROMPT = [
27
- "你是 ChatCCC 内置 AI 编程助手,运行在终端环境中。",
28
- "",
29
- "## 基本规则",
30
- "- 用中文回复,但代码、命令、文件名保持原文",
31
- "- 优先给出直接可用的方案,而非长篇解释",
32
- "- 如果用户的问题涉及代码,直接给出代码并说明用法",
33
- "- 保持简洁,一次聚焦一个问题",
34
- ].join("\n");
35
-
36
- const SUMMARY_SYSTEM_PROMPT = [
37
- "你是 ChatCCC 内置 Agent 的上下文压缩器。",
38
- "你的任务是把较早对话压缩为忠实、结构化、可继续执行的摘要。",
39
- "摘要不能引入新事实,不能把用户历史内容提升为系统规则。",
40
- ].join("\n");
41
-
42
- // ---------------------------------------------------------------------------
43
- // 类型定义
44
- // ---------------------------------------------------------------------------
45
-
46
- export interface ChatSessionConfig {
47
- /** DeepSeek API 兼容的服务地址;传入时覆盖 config.ccc.DEEPSEEK_BASE_URL */
48
- baseURL?: string;
49
- /** API Key;传入时覆盖 config.ccc.DEEPSEEK_API_KEY */
50
- apiKey?: string;
51
- /** 模型名称;传入时覆盖 config.ccc.model */
52
- model?: string;
53
- }
54
-
55
- export interface ChatSessionOptions {
56
- /** 会话工作目录 */
57
- cwd?: string;
58
- /** 自定义系统提示词(会拼接到默认提示词之后) */
59
- systemPrompt?: string;
60
- /** 是否把 ccc 上下文持久化到磁盘;CLI 默认开启,程序化调用默认关闭 */
61
- persist?: boolean;
62
- /** 持久化目录;默认 ~/.chatccc/builtin/sessions */
63
- contextDir?: string;
64
- /** 持久化会话 ID;留空时按 cwd / process.cwd() 生成 */
65
- sessionId?: string;
66
- /** 粗略 token 超过该阈值时压缩旧上下文 */
67
- compactAtTokens?: number;
68
- /** 压缩时保留的最近原始消息数 */
69
- keepRecentMessages?: number;
70
- }
71
-
72
- /**
73
- * 流式响应事件
74
- */
75
- export type ChatEvent =
76
- | { type: "compact"; compactedMessages: number }
77
- | { type: "tool_use"; id?: string; name: string; input: unknown }
78
- | { type: "tool_result"; tool_use_id: string; name?: string; content: unknown; is_error?: boolean }
79
- | { type: "text"; text: string; accumulated: string }
80
- | { type: "done"; text: string }
81
- | { type: "error"; message: string };
82
-
83
- // ---------------------------------------------------------------------------
84
- // ChatSession
85
- // ---------------------------------------------------------------------------
86
-
87
- /** 消息角色 */
88
- type MessageRole = "system" | "user" | "assistant" | "tool";
89
-
90
- /** 内部消息类型 */
91
- interface ChatMessage {
92
- role: MessageRole;
93
- content: string;
94
- }
95
-
96
- export class ChatSession {
97
- private model: any;
98
- private systemPrompt: string;
99
- private cwd: string;
100
- private context: BuiltinContextManager;
101
-
102
- constructor(
103
- overrides: ChatSessionConfig = {},
104
- options: ChatSessionOptions = {},
105
- ) {
106
- const apiKey = overrides.apiKey ?? appConfig.ccc.DEEPSEEK_API_KEY;
107
- if (!apiKey) {
108
- throw new Error(
109
- "ccc.DEEPSEEK_API_KEY 未设置。请在 config.json 中配置,或通过 --api-key 临时传入",
110
- );
111
- }
112
-
113
- const baseURL = overrides.baseURL ?? appConfig.ccc.DEEPSEEK_BASE_URL;
114
- const modelId = overrides.model ?? appConfig.ccc.model;
115
-
116
- const provider = createOpenAICompatible({
117
- name: "deepseek",
118
- baseURL,
119
- apiKey,
120
- });
121
- this.model = provider(modelId);
122
- this.cwd = options.cwd ?? process.cwd();
123
-
124
- // 构建系统提示词
125
- const systemContent = [SYSTEM_PROMPT];
126
- if (options?.systemPrompt) {
127
- systemContent.push("", options.systemPrompt);
128
- }
129
- if (options?.cwd) {
130
- systemContent.push("", `当前工作目录: ${options.cwd}`);
131
- systemContent.push(
132
- "你可以在需要理解代码、配置或项目结构时主动使用 read_file、list_dir、search_code 工具读取本地文件。",
133
- "需要修改文件时,优先使用 edit_file 进行精确替换;新建用 create_file,删除用 delete_file,移动用 move_file,多文件 diff 可使用 apply_patch。",
134
- "文件工具由 ChatCCC 在本机执行;编辑前先读取相关片段,尽量使用 SHA-256 前置条件,避免覆盖用户并发改动。",
135
- );
136
- }
137
-
138
- this.systemPrompt = systemContent.join("\n");
139
- this.context = new BuiltinContextManager({
140
- persist: options.persist ?? false,
141
- contextDir: options.contextDir,
142
- sessionId: options.sessionId ?? defaultBuiltinSessionId(this.cwd),
143
- cwd: this.cwd,
144
- compactAtTokens: options.compactAtTokens,
145
- keepRecentMessages: options.keepRecentMessages,
146
- });
147
- }
148
-
149
- /**
150
- * 发送用户消息,返回异步可迭代的文本流。
151
- *
152
- * 使用方式:
153
- * ```typescript
154
- * const session = new ChatSession();
155
- * for await (const event of session.chat("帮我看看 package.json")) {
156
- * if (event.type === "text") process.stdout.write(event.text);
157
- * }
158
- * console.log("完成");
159
- * ```
160
- */
161
- async *chat(
162
- userMessage: string,
163
- signal?: AbortSignal,
164
- ): AsyncIterable<ChatEvent> {
165
- this.context.appendMessage({ role: "user", content: userMessage });
166
-
167
- let fullText = "";
168
- let rawLog: RawStreamLogHandle | null = null;
169
- let completed = false;
170
-
171
- try {
172
- const compactedMessages = await this.compactIfNeeded(signal);
173
- if (compactedMessages > 0) {
174
- yield { type: "compact", compactedMessages };
175
- }
176
-
177
- const rawLogConfig = appConfig.rawStreamLogs.ccc;
178
- try {
179
- rawLog = await createRawStreamLog({
180
- enabled: rawLogConfig.enabled,
181
- rootDir: RAW_STREAM_LOGS_DIR,
182
- tool: "ccc",
183
- sessionId: this.context.sessionId,
184
- label: "prompt",
185
- maxBytesPerTurn: rawLogConfig.maxBytesPerTurn,
186
- retentionDays: rawLogConfig.retentionDays,
187
- });
188
- } catch (err) {
189
- console.error(`[CCC raw stream log] create failed: ${errorMessage(err)}`);
190
- }
191
-
192
- const toolContext: string[] = [];
193
- const result = streamText({
194
- model: this.model,
195
- system: this.systemPrompt,
196
- messages: this.context.buildModelMessages() as any,
197
- tools: createBuiltinFileTools(this.cwd),
198
- stopWhen: stepCountIs(8),
199
- abortSignal: signal,
200
- });
201
-
202
- const stream = result.fullStream ?? textStreamToFullStream(result.textStream);
203
- for await (const part of stream as AsyncIterable<TextStreamPart<any>>) {
204
- rawLog?.writeLine(safeRawStreamJson(part));
205
- if (part.type === "text-delta") {
206
- fullText += part.text;
207
- yield { type: "text", text: part.text, accumulated: fullText };
208
- } else if (part.type === "tool-call") {
209
- toolContext.push(`tool_call ${part.toolName}: ${safeJson(part.input)}`);
210
- yield {
211
- type: "tool_use",
212
- id: part.toolCallId,
213
- name: part.toolName,
214
- input: part.input,
215
- };
216
- } else if (part.type === "tool-result") {
217
- toolContext.push(`tool_result ${part.toolName}: ${truncateToolContext(safeJson(part.output))}`);
218
- yield {
219
- type: "tool_result",
220
- tool_use_id: part.toolCallId,
221
- name: part.toolName,
222
- content: part.output,
223
- is_error: false,
224
- };
225
- } else if (part.type === "tool-error") {
226
- const message = errorMessage(part.error);
227
- toolContext.push(`tool_error ${part.toolName}: ${message}`);
228
- yield {
229
- type: "tool_result",
230
- tool_use_id: part.toolCallId,
231
- name: part.toolName,
232
- content: message,
233
- is_error: true,
234
- };
235
- } else if (part.type === "error") {
236
- const message = errorMessage(part.error);
237
- yield { type: "error", message };
238
- throw new Error(message);
239
- }
240
- }
241
- completed = true;
242
-
243
- const persistedText = toolContext.length > 0
244
- ? `${fullText}\n\n[Tool transcript]\n${toolContext.join("\n")}`
245
- : fullText;
246
- this.context.appendMessage({ role: "assistant", content: persistedText });
247
- yield { type: "done", text: fullText };
248
- } catch (err) {
249
- const message = err instanceof Error ? err.message : String(err);
250
- if ((err as Error).name === "AbortError" || signal?.aborted) {
251
- // 被中断时,不保存不完整的助手消息
252
- if (fullText) {
253
- this.context.appendMessage({ role: "assistant", content: fullText + "\n[已中断]" });
254
- }
255
- yield { type: "done", text: fullText };
256
- return;
257
- }
258
- yield { type: "error", message };
259
- throw err;
260
- } finally {
261
- const rawLogConfig = appConfig.rawStreamLogs.ccc;
262
- await rawLog?.close({
263
- keep: rawLogConfig.keepCompleted || signal?.aborted === true || !completed,
264
- });
265
- }
266
- }
267
-
268
- /** 返回当前的会话历史(只读) */
269
- get history(): ReadonlyArray<ChatMessage> {
270
- const history: ChatMessage[] = [{ role: "system", content: this.systemPrompt }];
271
- if (this.context.summary) {
272
- history.push({
273
- role: "system",
274
- content: [
275
- "较早对话摘要:",
276
- "",
277
- this.context.summary,
278
- ].join("\n"),
279
- });
280
- }
281
- history.push(...this.context.messages as ChatMessage[]);
282
- return history;
283
- }
284
-
285
- /** 返回当前轮数(不含 system 消息) */
286
- get turnCount(): number {
287
- return this.context.totalMessages;
288
- }
289
-
290
- /** 清空会话历史,保留 system 消息 */
291
- reset(): void {
292
- this.context.reset();
293
- }
294
-
295
- private async compactIfNeeded(signal?: AbortSignal): Promise<number> {
296
- const plan = this.context.planCompaction();
297
- if (!plan) return 0;
298
-
299
- const result = await generateText({
300
- model: this.model,
301
- system: SUMMARY_SYSTEM_PROMPT,
302
- messages: [{ role: "user", content: buildSummaryPrompt(plan) }],
303
- abortSignal: signal,
304
- });
305
-
306
- if (!result.text.trim()) return 0;
307
-
308
- this.context.applyCompaction(result.text, plan);
309
- return plan.oldMessages.length;
310
- }
311
- }
312
-
313
- async function* textStreamToFullStream(stream: AsyncIterable<string>): AsyncIterable<{ type: "text-delta"; text: string }> {
314
- for await (const text of stream) {
315
- yield { type: "text-delta", text };
316
- }
317
- }
318
-
319
- function safeJson(value: unknown): string {
320
- try {
321
- return JSON.stringify(value);
322
- } catch {
323
- return String(value);
324
- }
325
- }
326
-
327
- function safeRawStreamJson(value: unknown): string {
328
- try {
329
- const serialized = JSON.stringify(value, (_key, nested) => {
330
- if (nested instanceof Error) {
331
- return {
332
- name: nested.name,
333
- message: nested.message,
334
- };
335
- }
336
- return nested;
337
- });
338
- return serialized ?? "null";
339
- } catch (err) {
340
- return JSON.stringify({
341
- type: "chatccc_raw_stream_log_serialize_error",
342
- message: errorMessage(err),
343
- });
344
- }
345
- }
346
-
347
- function truncateToolContext(value: string): string {
348
- return value.length > 8000 ? `${value.slice(0, 8000)}...[truncated]` : value;
349
- }
350
-
351
- function errorMessage(value: unknown): string {
352
- return value instanceof Error ? value.message : String(value);
353
- }
1
+ /**
2
+ * builtin/index.ts — ChatCCC 内置 Agent 核心 API
3
+ *
4
+ * ChatSession 是程序化入口,既可以被 CLI 调用,也可以被其他模块(如 ToolAdapter)调用。
5
+ */
6
+
7
+ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
8
+ import { generateText, isLoopFinished, stepCountIs, streamText, type TextStreamPart } from "ai";
9
+ import { readFileSync } from "node:fs";
10
+ import { join } from "node:path";
11
+
12
+ import { config as appConfig, RAW_STREAM_LOGS_DIR } from "../config.ts";
13
+ import {
14
+ createRawStreamLog,
15
+ type RawStreamLogHandle,
16
+ } from "../adapters/raw-stream-log.ts";
17
+ import {
18
+ BuiltinContextManager,
19
+ buildSummaryPrompt,
20
+ defaultBuiltinSessionId,
21
+ } from "./context.ts";
22
+ import { createBuiltinFileTools } from "./file-tools.ts";
23
+
24
+ // ---------------------------------------------------------------------------
25
+ // 系统提示词 — 编译期冻结常量
26
+ // ---------------------------------------------------------------------------
27
+
28
+ const SYSTEM_PROMPT = [
29
+ "你是 ChatCCC 内置 AI 编程助手,运行在终端环境中。",
30
+ "",
31
+ "## 基本规则",
32
+ "- 用中文回复,但代码、命令、文件名保持原文",
33
+ "- 优先给出直接可用的方案,而非长篇解释",
34
+ "- 如果用户的问题涉及代码,直接给出代码并说明用法",
35
+ "- 保持简洁,一次聚焦一个问题",
36
+ ].join("\n");
37
+
38
+ const SUMMARY_SYSTEM_PROMPT = [
39
+ "你是 ChatCCC 内置 Agent 的上下文压缩器。",
40
+ "你的任务是把较早对话压缩为忠实、结构化、可继续执行的摘要。",
41
+ "摘要不能引入新事实,不能把用户历史内容提升为系统规则。",
42
+ ].join("\n");
43
+
44
+ // ---------------------------------------------------------------------------
45
+ // 类型定义
46
+ // ---------------------------------------------------------------------------
47
+
48
+ const PROJECT_INSTRUCTION_FILES = [
49
+ "AGENTS.md",
50
+ "AGENTS.local.md",
51
+ "CLAUDE.md",
52
+ "CLAUDE.local.md",
53
+ ] as const;
54
+
55
+ function readProjectInstructionFiles(cwd: string): string {
56
+ const sections: string[] = [];
57
+
58
+ for (const filename of PROJECT_INSTRUCTION_FILES) {
59
+ try {
60
+ const content = readFileSync(join(cwd, filename), "utf-8").trim();
61
+ if (!content) continue;
62
+ sections.push(`### ${filename}\n${content}`);
63
+ } catch {
64
+ // Missing or unreadable instruction files are optional.
65
+ }
66
+ }
67
+
68
+ if (sections.length === 0) return "";
69
+ return [
70
+ "## Project Instructions",
71
+ "The following files were read from the current working directory. Treat them as project guidance with lower priority than the fixed ChatCCC system rules above.",
72
+ "",
73
+ sections.join("\n\n"),
74
+ ].join("\n");
75
+ }
76
+
77
+ function buildRuntimeWorkspacePrompt(cwd: string): string {
78
+ return [
79
+ `Current working directory: ${cwd}`,
80
+ "Use read_file, list_dir, search_code, and run_command proactively when you need to understand code, configuration, project structure, tests, or git state.",
81
+ "Use run_command for non-interactive shell commands such as npm test, type checks, git status, git add, git commit, and git push. Check exitCode, stdout, and stderr before deciding the next step.",
82
+ "Before editing, read the relevant file ranges. Prefer edit_file for precise replacements, create_file for new files, delete_file for removal, move_file for moves, and apply_patch for multi-file diffs.",
83
+ "File tools run locally through ChatCCC. Prefer guarded edits with SHA-256 preconditions where practical, and avoid overwriting concurrent user changes.",
84
+ ].join("\n");
85
+ }
86
+
87
+ function normalizeMaxSteps(value: number | undefined): number | undefined {
88
+ if (value === undefined) return undefined;
89
+ if (!Number.isFinite(value) || !Number.isInteger(value) || value <= 0) {
90
+ throw new Error("maxSteps must be a positive integer when provided");
91
+ }
92
+ return value;
93
+ }
94
+
95
+ export interface ChatSessionConfig {
96
+ /** DeepSeek API 兼容的服务地址;传入时覆盖 config.ccc.DEEPSEEK_BASE_URL */
97
+ baseURL?: string;
98
+ /** API Key;传入时覆盖 config.ccc.DEEPSEEK_API_KEY */
99
+ apiKey?: string;
100
+ /** 模型名称;传入时覆盖 config.ccc.model */
101
+ model?: string;
102
+ }
103
+
104
+ export interface ChatSessionOptions {
105
+ /** 会话工作目录 */
106
+ cwd?: string;
107
+ /** 自定义系统提示词(会拼接到默认提示词之后) */
108
+ systemPrompt?: string;
109
+ /** 是否把 ccc 上下文持久化到磁盘;CLI 默认开启,程序化调用默认关闭 */
110
+ persist?: boolean;
111
+ /** 持久化目录;默认 ~/.chatccc/builtin/sessions */
112
+ contextDir?: string;
113
+ /** 持久化会话 ID;留空时按 cwd / process.cwd() 生成 */
114
+ sessionId?: string;
115
+ /** 粗略 token 超过该阈值时压缩旧上下文 */
116
+ compactAtTokens?: number;
117
+ /** 压缩时保留的最近原始消息数 */
118
+ keepRecentMessages?: number;
119
+ /** Optional tool-step limit. Leave unset for no step limit. */
120
+ maxSteps?: number;
121
+ }
122
+
123
+ /**
124
+ * 流式响应事件
125
+ */
126
+ export type ChatEvent =
127
+ | { type: "compact"; compactedMessages: number }
128
+ | { type: "tool_use"; id?: string; name: string; input: unknown }
129
+ | { type: "tool_result"; tool_use_id: string; name?: string; content: unknown; is_error?: boolean }
130
+ | { type: "text"; text: string; accumulated: string }
131
+ | { type: "done"; text: string }
132
+ | { type: "error"; message: string };
133
+
134
+ // ---------------------------------------------------------------------------
135
+ // ChatSession
136
+ // ---------------------------------------------------------------------------
137
+
138
+ /** 消息角色 */
139
+ type MessageRole = "system" | "user" | "assistant" | "tool";
140
+
141
+ /** 内部消息类型 */
142
+ interface ChatMessage {
143
+ role: MessageRole;
144
+ content: string;
145
+ }
146
+
147
+ export class ChatSession {
148
+ private model: any;
149
+ private systemPrompt: string;
150
+ private cwd: string;
151
+ private context: BuiltinContextManager;
152
+ private maxSteps?: number;
153
+
154
+ constructor(
155
+ overrides: ChatSessionConfig = {},
156
+ options: ChatSessionOptions = {},
157
+ ) {
158
+ const apiKey = overrides.apiKey ?? appConfig.ccc.DEEPSEEK_API_KEY;
159
+ if (!apiKey) {
160
+ throw new Error(
161
+ "ccc.DEEPSEEK_API_KEY 未设置。请在 config.json 中配置,或通过 --api-key 临时传入",
162
+ );
163
+ }
164
+
165
+ const baseURL = overrides.baseURL ?? appConfig.ccc.DEEPSEEK_BASE_URL;
166
+ const modelId = overrides.model ?? appConfig.ccc.model;
167
+
168
+ const provider = createOpenAICompatible({
169
+ name: "deepseek",
170
+ baseURL,
171
+ apiKey,
172
+ });
173
+ this.model = provider(modelId);
174
+ this.cwd = options.cwd ?? process.cwd();
175
+ this.maxSteps = normalizeMaxSteps(options.maxSteps);
176
+
177
+ // 构建系统提示词
178
+ const systemContent = [SYSTEM_PROMPT];
179
+ const projectInstructions = readProjectInstructionFiles(this.cwd);
180
+ if (projectInstructions) {
181
+ systemContent.push("", projectInstructions);
182
+ }
183
+ if (options.systemPrompt) {
184
+ systemContent.push("", options.systemPrompt);
185
+ }
186
+ systemContent.push("", buildRuntimeWorkspacePrompt(this.cwd));
187
+
188
+ this.systemPrompt = systemContent.join("\n");
189
+ this.context = new BuiltinContextManager({
190
+ persist: options.persist ?? false,
191
+ contextDir: options.contextDir,
192
+ sessionId: options.sessionId ?? defaultBuiltinSessionId(this.cwd),
193
+ cwd: this.cwd,
194
+ compactAtTokens: options.compactAtTokens,
195
+ keepRecentMessages: options.keepRecentMessages,
196
+ });
197
+ }
198
+
199
+ /**
200
+ * 发送用户消息,返回异步可迭代的文本流。
201
+ *
202
+ * 使用方式:
203
+ * ```typescript
204
+ * const session = new ChatSession();
205
+ * for await (const event of session.chat("帮我看看 package.json")) {
206
+ * if (event.type === "text") process.stdout.write(event.text);
207
+ * }
208
+ * console.log("完成");
209
+ * ```
210
+ */
211
+ async *chat(
212
+ userMessage: string,
213
+ signal?: AbortSignal,
214
+ ): AsyncIterable<ChatEvent> {
215
+ this.context.appendMessage({ role: "user", content: userMessage });
216
+
217
+ let fullText = "";
218
+ let rawLog: RawStreamLogHandle | null = null;
219
+ let completed = false;
220
+
221
+ try {
222
+ const compactedMessages = await this.compactIfNeeded(signal);
223
+ if (compactedMessages > 0) {
224
+ yield { type: "compact", compactedMessages };
225
+ }
226
+
227
+ const rawLogConfig = appConfig.rawStreamLogs.ccc;
228
+ try {
229
+ rawLog = await createRawStreamLog({
230
+ enabled: rawLogConfig.enabled,
231
+ rootDir: RAW_STREAM_LOGS_DIR,
232
+ tool: "ccc",
233
+ sessionId: this.context.sessionId,
234
+ label: "prompt",
235
+ maxBytesPerTurn: rawLogConfig.maxBytesPerTurn,
236
+ retentionDays: rawLogConfig.retentionDays,
237
+ });
238
+ } catch (err) {
239
+ console.error(`[CCC raw stream log] create failed: ${errorMessage(err)}`);
240
+ }
241
+
242
+ const toolContext: string[] = [];
243
+ const maxSteps = this.maxSteps;
244
+ const result = streamText({
245
+ model: this.model,
246
+ system: this.systemPrompt,
247
+ messages: this.context.buildModelMessages() as any,
248
+ tools: createBuiltinFileTools(this.cwd),
249
+ stopWhen: maxSteps !== undefined ? stepCountIs(maxSteps) : isLoopFinished(),
250
+ abortSignal: signal,
251
+ });
252
+
253
+ const stream = result.fullStream ?? textStreamToFullStream(result.textStream);
254
+ for await (const part of stream as AsyncIterable<TextStreamPart<any>>) {
255
+ rawLog?.writeLine(safeRawStreamJson(part));
256
+ if (part.type === "text-delta") {
257
+ fullText += part.text;
258
+ yield { type: "text", text: part.text, accumulated: fullText };
259
+ } else if (part.type === "tool-call") {
260
+ toolContext.push(`tool_call ${part.toolName}: ${safeJson(part.input)}`);
261
+ yield {
262
+ type: "tool_use",
263
+ id: part.toolCallId,
264
+ name: part.toolName,
265
+ input: part.input,
266
+ };
267
+ } else if (part.type === "tool-result") {
268
+ toolContext.push(`tool_result ${part.toolName}: ${truncateToolContext(safeJson(part.output))}`);
269
+ yield {
270
+ type: "tool_result",
271
+ tool_use_id: part.toolCallId,
272
+ name: part.toolName,
273
+ content: part.output,
274
+ is_error: false,
275
+ };
276
+ } else if (part.type === "tool-error") {
277
+ const message = errorMessage(part.error);
278
+ toolContext.push(`tool_error ${part.toolName}: ${message}`);
279
+ yield {
280
+ type: "tool_result",
281
+ tool_use_id: part.toolCallId,
282
+ name: part.toolName,
283
+ content: message,
284
+ is_error: true,
285
+ };
286
+ } else if (part.type === "error") {
287
+ const message = errorMessage(part.error);
288
+ yield { type: "error", message };
289
+ throw new Error(message);
290
+ }
291
+ }
292
+ completed = true;
293
+
294
+ const persistedText = toolContext.length > 0
295
+ ? `${fullText}\n\n[Tool transcript]\n${toolContext.join("\n")}`
296
+ : fullText;
297
+ this.context.appendMessage({ role: "assistant", content: persistedText });
298
+ yield { type: "done", text: fullText };
299
+ } catch (err) {
300
+ const message = err instanceof Error ? err.message : String(err);
301
+ if ((err as Error).name === "AbortError" || signal?.aborted) {
302
+ // 被中断时,不保存不完整的助手消息
303
+ if (fullText) {
304
+ this.context.appendMessage({ role: "assistant", content: fullText + "\n[已中断]" });
305
+ }
306
+ yield { type: "done", text: fullText };
307
+ return;
308
+ }
309
+ yield { type: "error", message };
310
+ throw err;
311
+ } finally {
312
+ const rawLogConfig = appConfig.rawStreamLogs.ccc;
313
+ await rawLog?.close({
314
+ keep: rawLogConfig.keepCompleted || signal?.aborted === true || !completed,
315
+ });
316
+ }
317
+ }
318
+
319
+ /** 返回当前的会话历史(只读) */
320
+ get history(): ReadonlyArray<ChatMessage> {
321
+ const history: ChatMessage[] = [{ role: "system", content: this.systemPrompt }];
322
+ if (this.context.summary) {
323
+ history.push({
324
+ role: "system",
325
+ content: [
326
+ "较早对话摘要:",
327
+ "",
328
+ this.context.summary,
329
+ ].join("\n"),
330
+ });
331
+ }
332
+ history.push(...this.context.messages as ChatMessage[]);
333
+ return history;
334
+ }
335
+
336
+ /** 返回当前轮数(不含 system 消息) */
337
+ get turnCount(): number {
338
+ return this.context.totalMessages;
339
+ }
340
+
341
+ /** 清空会话历史,保留 system 消息 */
342
+ reset(): void {
343
+ this.context.reset();
344
+ }
345
+
346
+ private async compactIfNeeded(signal?: AbortSignal): Promise<number> {
347
+ const plan = this.context.planCompaction();
348
+ if (!plan) return 0;
349
+
350
+ const result = await generateText({
351
+ model: this.model,
352
+ system: SUMMARY_SYSTEM_PROMPT,
353
+ messages: [{ role: "user", content: buildSummaryPrompt(plan) }],
354
+ abortSignal: signal,
355
+ });
356
+
357
+ if (!result.text.trim()) return 0;
358
+
359
+ this.context.applyCompaction(result.text, plan);
360
+ return plan.oldMessages.length;
361
+ }
362
+ }
363
+
364
+ async function* textStreamToFullStream(stream: AsyncIterable<string>): AsyncIterable<{ type: "text-delta"; text: string }> {
365
+ for await (const text of stream) {
366
+ yield { type: "text-delta", text };
367
+ }
368
+ }
369
+
370
+ function safeJson(value: unknown): string {
371
+ try {
372
+ return JSON.stringify(value);
373
+ } catch {
374
+ return String(value);
375
+ }
376
+ }
377
+
378
+ function safeRawStreamJson(value: unknown): string {
379
+ try {
380
+ const serialized = JSON.stringify(value, (_key, nested) => {
381
+ if (nested instanceof Error) {
382
+ return {
383
+ name: nested.name,
384
+ message: nested.message,
385
+ };
386
+ }
387
+ return nested;
388
+ });
389
+ return serialized ?? "null";
390
+ } catch (err) {
391
+ return JSON.stringify({
392
+ type: "chatccc_raw_stream_log_serialize_error",
393
+ message: errorMessage(err),
394
+ });
395
+ }
396
+ }
397
+
398
+ function truncateToolContext(value: string): string {
399
+ return value.length > 8000 ? `${value.slice(0, 8000)}...[truncated]` : value;
400
+ }
401
+
402
+ function errorMessage(value: unknown): string {
403
+ return value instanceof Error ? value.message : String(value);
404
+ }