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
|
@@ -3,7 +3,8 @@ import { createHash, randomBytes } from "node:crypto";
|
|
|
3
3
|
import { createReadStream } from "node:fs";
|
|
4
4
|
import { copyFile, mkdir, open, readFile, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
|
|
5
5
|
import { createRequire } from "node:module";
|
|
6
|
-
import {
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
7
8
|
import { createInterface } from "node:readline";
|
|
8
9
|
|
|
9
10
|
import { jsonSchema, tool, type ToolSet } from "ai";
|
|
@@ -188,10 +189,24 @@ export interface ApplyPatchOutput {
|
|
|
188
189
|
changedFiles: ApplyPatchFileChange[];
|
|
189
190
|
}
|
|
190
191
|
|
|
192
|
+
/**
|
|
193
|
+
* 把 `~` / `~/x`(含反斜杠 `~\\x`)展开为用户主目录绝对路径。
|
|
194
|
+
* `~user/x` 形式不展开(Node 无内置支持),保持原样交给后续解析。
|
|
195
|
+
* 供路径工具共用:技能索引里 `~/...` 路径可直接传给 read_file 等工具。
|
|
196
|
+
*/
|
|
197
|
+
export function expandHomePath(value: string): string {
|
|
198
|
+
if (value === "~") return homedir();
|
|
199
|
+
if (value.startsWith("~/") || value.startsWith("~\\")) {
|
|
200
|
+
return join(homedir(), value.slice(2));
|
|
201
|
+
}
|
|
202
|
+
return value;
|
|
203
|
+
}
|
|
204
|
+
|
|
191
205
|
function resolveToolPath(cwd: string, value: string | undefined): string {
|
|
192
206
|
const raw = value?.trim();
|
|
193
207
|
if (!raw) return resolve(cwd);
|
|
194
|
-
|
|
208
|
+
const expanded = expandHomePath(raw);
|
|
209
|
+
return isAbsolute(expanded) ? resolve(expanded) : resolve(cwd, expanded);
|
|
195
210
|
}
|
|
196
211
|
|
|
197
212
|
function toPositiveInt(value: number | undefined): number | undefined {
|
package/src/builtin/index.ts
CHANGED
|
@@ -230,21 +230,26 @@ export class ChatSession {
|
|
|
230
230
|
);
|
|
231
231
|
}
|
|
232
232
|
|
|
233
|
-
/**
|
|
233
|
+
/**
|
|
234
|
+
* 组装系统提示词。顺序遵循“稳定性优先”原则(缓存命中友好):
|
|
235
|
+
* 固定规则 → 项目指令 → runtime 上下文 → 用户补充 → 技能索引(最后)。
|
|
236
|
+
* 技能索引是最易变的部分(热加载,任何 SKILL.md 变化都会改前缀),
|
|
237
|
+
* 放最后可以让前面的稳定内容尽量命中缓存,只丢尾段。
|
|
238
|
+
*/
|
|
234
239
|
private buildSystemPrompt(skills: BuiltinSkill[]): string {
|
|
235
240
|
const systemContent = [SYSTEM_PROMPT];
|
|
236
241
|
const projectInstructions = readProjectInstructionFiles(this.cwd);
|
|
237
242
|
if (projectInstructions) {
|
|
238
243
|
systemContent.push("", projectInstructions);
|
|
239
244
|
}
|
|
245
|
+
systemContent.push("", buildRuntimeWorkspacePrompt(this.cwd));
|
|
246
|
+
if (this.customSystemPrompt) {
|
|
247
|
+
systemContent.push("", this.customSystemPrompt);
|
|
248
|
+
}
|
|
240
249
|
const skillsPrompt = buildSkillsIndexPrompt(skills);
|
|
241
250
|
if (skillsPrompt) {
|
|
242
251
|
systemContent.push("", skillsPrompt);
|
|
243
252
|
}
|
|
244
|
-
if (this.customSystemPrompt) {
|
|
245
|
-
systemContent.push("", this.customSystemPrompt);
|
|
246
|
-
}
|
|
247
|
-
systemContent.push("", buildRuntimeWorkspacePrompt(this.cwd));
|
|
248
253
|
return systemContent.join("\n");
|
|
249
254
|
}
|
|
250
255
|
|
|
@@ -405,6 +410,8 @@ export class ChatSession {
|
|
|
405
410
|
system: SUMMARY_SYSTEM_PROMPT,
|
|
406
411
|
messages: [{ role: "user", content: buildSummaryPrompt(plan) }],
|
|
407
412
|
abortSignal: signal,
|
|
413
|
+
// 温度 0:相同输入尽量产出相同摘要,避免压缩后上下文前缀随机漂移破坏缓存
|
|
414
|
+
temperature: 0,
|
|
408
415
|
});
|
|
409
416
|
|
|
410
417
|
if (!result.text.trim()) return 0;
|
package/src/builtin/skills.ts
CHANGED
|
@@ -1,190 +1,205 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* skills.ts — 多来源技能扫描(DeepCCC 统一入口)
|
|
3
|
-
*
|
|
4
|
-
* 并行扫描 Claude / Codex / Cursor / DeepCCC 四套目录式技能
|
|
5
|
-
* (<name>/SKILL.md + YAML frontmatter,三套生态同构)。
|
|
6
|
-
*
|
|
7
|
-
* 同名优先级(高 → 低):deepccc > codex > cursor > claude;
|
|
8
|
-
* 同来源内:project(项目级)> global(用户级)。
|
|
9
|
-
* 目录列表按低 → 高排列,扫描时后者覆盖前者,天然实现优先级。
|
|
10
|
-
*
|
|
11
|
-
* 热加载:SKILL.md 内容带 mtime 缓存,文件变化时自动重读;
|
|
12
|
-
* 目录枚举每次都做(新技能目录立即被发现)。因此"创建技能 →
|
|
13
|
-
* 下一次对话自动生效",无需重启,也无需常驻 watcher。
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { readdirSync } from "node:fs";
|
|
17
|
-
import { readFile, stat } from "node:fs/promises";
|
|
18
|
-
import { homedir } from "node:os";
|
|
19
|
-
import { join } from "node:path";
|
|
20
|
-
|
|
21
|
-
export type
|
|
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
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
//
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
.
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
{ dir: join(
|
|
132
|
-
{ dir: join(
|
|
133
|
-
{ dir: join(
|
|
134
|
-
{ dir: join(
|
|
135
|
-
{ dir: join(h, ".
|
|
136
|
-
{ dir: join(
|
|
137
|
-
{ dir: join(
|
|
138
|
-
{ dir: join(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (
|
|
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
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* skills.ts — 多来源技能扫描(DeepCCC 统一入口)
|
|
3
|
+
*
|
|
4
|
+
* 并行扫描 Claude / Codex / Cursor / DeepCCC 四套目录式技能
|
|
5
|
+
* (<name>/SKILL.md + YAML frontmatter,三套生态同构)。
|
|
6
|
+
*
|
|
7
|
+
* 同名优先级(高 → 低):deepccc > codex > cursor > claude;
|
|
8
|
+
* 同来源内:project(项目级)> global(用户级)。
|
|
9
|
+
* 目录列表按低 → 高排列,扫描时后者覆盖前者,天然实现优先级。
|
|
10
|
+
*
|
|
11
|
+
* 热加载:SKILL.md 内容带 mtime 缓存,文件变化时自动重读;
|
|
12
|
+
* 目录枚举每次都做(新技能目录立即被发现)。因此"创建技能 →
|
|
13
|
+
* 下一次对话自动生效",无需重启,也无需常驻 watcher。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { readdirSync } from "node:fs";
|
|
17
|
+
import { readFile, stat } from "node:fs/promises";
|
|
18
|
+
import { homedir } from "node:os";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
export type SkillSource = "deepccc" | "codex" | "cursor" | "claude";
|
|
21
|
+
export type SkillScope = "global" | "project";
|
|
22
|
+
|
|
23
|
+
export interface BuiltinSkill {
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
/** SKILL.md 的绝对路径 */
|
|
27
|
+
skillPath: string;
|
|
28
|
+
/** 来源(优先级 deepccc > codex > cursor > claude) */
|
|
29
|
+
source: SkillSource;
|
|
30
|
+
/** global = 用户级;project = 项目级(同来源内 project > global) */
|
|
31
|
+
scope: SkillScope;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SkillDirSpec {
|
|
35
|
+
dir: string;
|
|
36
|
+
source: SkillSource;
|
|
37
|
+
scope: SkillScope;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 解析 SKILL.md frontmatter(兼容 CRLF),返回 name + description;无 frontmatter 返回 null */
|
|
41
|
+
export function parseSkillFrontmatter(
|
|
42
|
+
content: string,
|
|
43
|
+
): { name: string; description: string } | null {
|
|
44
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---\s*(?:\r?\n|$)/.exec(content);
|
|
45
|
+
if (!match) return null;
|
|
46
|
+
const fm = match[1];
|
|
47
|
+
const nameMatch = /^name:\s*(.+?)\s*$/m.exec(fm);
|
|
48
|
+
if (!nameMatch) return null;
|
|
49
|
+
const descMatch = /^description:\s*(.+?)\s*$/m.exec(fm);
|
|
50
|
+
return {
|
|
51
|
+
name: nameMatch[1].trim(),
|
|
52
|
+
description: descMatch?.[1].trim() ?? "",
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
// mtime 热加载缓存:同一进程内重复扫描命中缓存(stat 校验),文件变化自动重读
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
const skillFileCache = new Map<string, { mtimeMs: number; content: string }>();
|
|
61
|
+
|
|
62
|
+
async function readSkillFile(skillPath: string): Promise<string | null> {
|
|
63
|
+
let st;
|
|
64
|
+
try {
|
|
65
|
+
st = await stat(skillPath);
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
if (!st.isFile()) return null;
|
|
70
|
+
const cached = skillFileCache.get(skillPath);
|
|
71
|
+
if (cached && cached.mtimeMs === st.mtimeMs) return cached.content;
|
|
72
|
+
const content = await readFile(skillPath, "utf8");
|
|
73
|
+
skillFileCache.set(skillPath, { mtimeMs: st.mtimeMs, content });
|
|
74
|
+
return content;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 扫描多个 skill 目录(并行读取),按 name 去重。
|
|
79
|
+
* 同名技能:后面的 spec(高优先级)覆盖前面的(低优先级)。
|
|
80
|
+
* 隐藏目录(.system 等)和无 SKILL.md 的目录会被跳过;缺失目录跳过。
|
|
81
|
+
*/
|
|
82
|
+
export async function scanSkillsDirs(dirs: SkillDirSpec[]): Promise<BuiltinSkill[]> {
|
|
83
|
+
const byName = new Map<string, BuiltinSkill>();
|
|
84
|
+
|
|
85
|
+
for (const spec of dirs) {
|
|
86
|
+
let entries;
|
|
87
|
+
try {
|
|
88
|
+
entries = readdirSync(spec.dir, { withFileTypes: true });
|
|
89
|
+
} catch {
|
|
90
|
+
continue; // 目录不存在或不可读:跳过
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 目录内所有候选 SKILL.md 并行读取(读文件是扫描的瓶颈)
|
|
94
|
+
const results = await Promise.all(
|
|
95
|
+
entries
|
|
96
|
+
.filter((entry) => entry.isDirectory() && !entry.name.startsWith("."))
|
|
97
|
+
.map(async (entry) => {
|
|
98
|
+
const skillPath = join(spec.dir, entry.name, "SKILL.md");
|
|
99
|
+
const content = await readSkillFile(skillPath);
|
|
100
|
+
if (content === null) return null;
|
|
101
|
+
const parsed = parseSkillFrontmatter(content);
|
|
102
|
+
if (!parsed) return null;
|
|
103
|
+
return { ...parsed, skillPath };
|
|
104
|
+
}),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
for (const result of results) {
|
|
108
|
+
if (!result) continue;
|
|
109
|
+
byName.set(result.name, {
|
|
110
|
+
name: result.name,
|
|
111
|
+
description: result.description,
|
|
112
|
+
skillPath: result.skillPath,
|
|
113
|
+
source: spec.source,
|
|
114
|
+
scope: spec.scope,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return [...byName.values()];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 默认技能扫描目录(按优先级从低到高排列,扫描时后者覆盖前者):
|
|
124
|
+
* claude(global→project) < cursor(global→project) < codex(global→project) < deepccc(global→project)
|
|
125
|
+
* 其中 codex 全局包含 ~/.codex/skills(CLI 旧路径)与 ~/.agents/skills(标准全局目录)。
|
|
126
|
+
*/
|
|
127
|
+
export function buildDefaultSkillDirs(cwd: string): SkillDirSpec[] {
|
|
128
|
+
const h = homedir();
|
|
129
|
+
return [
|
|
130
|
+
{ dir: join(h, ".claude", "skills"), source: "claude", scope: "global" },
|
|
131
|
+
{ dir: join(cwd, ".claude", "skills"), source: "claude", scope: "project" },
|
|
132
|
+
{ dir: join(h, ".cursor", "skills"), source: "cursor", scope: "global" },
|
|
133
|
+
{ dir: join(cwd, ".cursor", "skills"), source: "cursor", scope: "project" },
|
|
134
|
+
{ dir: join(h, ".codex", "skills"), source: "codex", scope: "global" },
|
|
135
|
+
{ dir: join(h, ".agents", "skills"), source: "codex", scope: "global" },
|
|
136
|
+
{ dir: join(cwd, ".codex", "skills"), source: "codex", scope: "project" },
|
|
137
|
+
{ dir: join(h, ".deepccc", "skills"), source: "deepccc", scope: "global" },
|
|
138
|
+
{ dir: join(cwd, ".deepccc", "skills"), source: "deepccc", scope: "project" },
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 把技能绝对路径转成 prompt 展示形式:主目录下的路径缩写为 `~/...`(跨机器、
|
|
144
|
+
* 跨用户稳定,避免用户名/盘符差异导致 system 前缀变化破坏缓存命中);
|
|
145
|
+
* 分隔符统一为 `/`。主目录外的路径保持绝对路径(仅统一分隔符)。
|
|
146
|
+
*/
|
|
147
|
+
export function normalizeSkillPathForPrompt(skillPath: string): string {
|
|
148
|
+
const home = homedir().replace(/[\\/]+$/, "").replace(/\\/g, "/");
|
|
149
|
+
const normalized = skillPath.replace(/\\/g, "/");
|
|
150
|
+
if (normalized === home) return "~";
|
|
151
|
+
if (normalized.startsWith(home + "/")) {
|
|
152
|
+
return "~" + normalized.slice(home.length);
|
|
153
|
+
}
|
|
154
|
+
return normalized;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 生成 skill 索引提示词(注入 system prompt)。
|
|
159
|
+
* 索引只含 name + description + 来源 + 路径,并指示模型在任务匹配时
|
|
160
|
+
* 先用 read_file 读取 SKILL.md 全文再执行;同时声明"创建技能"约定
|
|
161
|
+
* (模型在对话中应把新技能创建到 ~/.deepccc/skills 或项目 .deepccc/skills)。
|
|
162
|
+
* 路径经 normalizeSkillPathForPrompt 缩写,保持跨机器前缀稳定。
|
|
163
|
+
*/
|
|
164
|
+
export function buildSkillsIndexPrompt(skills: BuiltinSkill[]): string {
|
|
165
|
+
if (skills.length === 0) return "";
|
|
166
|
+
|
|
167
|
+
const lines = [
|
|
168
|
+
"## Available Skills",
|
|
169
|
+
"Skills are scanned in parallel from Claude/Codex/Cursor/DeepCCC skill directories.",
|
|
170
|
+
"On name conflicts: DeepCCC > Codex > Cursor > Claude wins; within one source, project scope wins over global.",
|
|
171
|
+
"When a user request matches a skill's description, first read its full SKILL.md with read_file, then follow the instructions in it exactly.",
|
|
172
|
+
"",
|
|
173
|
+
...skills.map((s) => `- **${s.name}** [${s.source}:${s.scope}] (\`${normalizeSkillPathForPrompt(s.skillPath)}\`): ${s.description || "(no description)"}`),
|
|
174
|
+
"",
|
|
175
|
+
"## Creating Skills",
|
|
176
|
+
"When the user asks to create a skill, create it as a Codex-style directory skill at",
|
|
177
|
+
"~/.deepccc/skills/<name>/SKILL.md (global, default) or <cwd>/.deepccc/skills/<name>/SKILL.md (project, only when the user explicitly asks for a project-scoped skill).",
|
|
178
|
+
"SKILL.md format:",
|
|
179
|
+
"```",
|
|
180
|
+
"---",
|
|
181
|
+
"name: <skill-name>",
|
|
182
|
+
"description: <one-line description>",
|
|
183
|
+
"---",
|
|
184
|
+
"",
|
|
185
|
+
"<instructions>",
|
|
186
|
+
"```",
|
|
187
|
+
"New skills are picked up automatically on the next message (hot reload); no restart is needed.",
|
|
188
|
+
];
|
|
189
|
+
return lines.join("\n");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** 生成 Codex 结构的新技能 SKILL.md 模板(供 CLI skill create / 对话创建约定共用) */
|
|
193
|
+
export function buildSkillTemplate(name: string, description: string): string {
|
|
194
|
+
return [
|
|
195
|
+
"---",
|
|
196
|
+
`name: ${name}`,
|
|
197
|
+
`description: ${description}`,
|
|
198
|
+
"---",
|
|
199
|
+
"",
|
|
200
|
+
`# ${name}`,
|
|
201
|
+
"",
|
|
202
|
+
"<skill instructions>",
|
|
203
|
+
"",
|
|
204
|
+
].join("\n");
|
|
205
|
+
}
|