chatccc 0.2.243 → 0.2.244
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/README.md +5 -5
- package/bin/cccagent.mjs +17 -17
- package/deepccc-agent/bin/deepccc.mjs +26 -26
- package/deepccc-agent/os-prompts/darwin.md +8 -0
- package/deepccc-agent/os-prompts/linux.md +8 -0
- package/deepccc-agent/os-prompts/win32.md +9 -0
- package/deepccc-agent/package.json +2 -1
- package/deepccc-agent/src/__tests__/chat-session.test.ts +39 -0
- package/deepccc-agent/src/__tests__/cli-json.test.ts +49 -49
- package/deepccc-agent/src/__tests__/config.test.ts +26 -26
- package/deepccc-agent/src/__tests__/context.test.ts +319 -319
- package/deepccc-agent/src/__tests__/file-tools.test.ts +240 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +195 -195
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +121 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +262 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +116 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +56 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +284 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +247 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +220 -220
- package/deepccc-agent/src/config.ts +84 -84
- package/deepccc-agent/src/context.ts +465 -465
- package/deepccc-agent/src/file-log.ts +38 -38
- package/deepccc-agent/src/index.ts +46 -16
- package/deepccc-agent/src/proc-tree-kill.ts +61 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +76 -76
- package/deepccc-agent/src/progress/reducer.ts +113 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +294 -294
- package/deepccc-agent/src/progress/view.ts +77 -77
- package/deepccc-agent/src/raw-stream-log.ts +124 -124
- package/deepccc-agent/src/session-search.ts +370 -370
- package/deepccc-agent/src/session-select.ts +48 -48
- package/deepccc-agent/src/sigint.ts +50 -50
- package/deepccc-agent/src/skills.ts +205 -205
- package/deepccc-agent/src/web-tools.ts +313 -313
- package/deepccc-agent/tsconfig.build.json +13 -13
- package/deepccc-agent/tsconfig.json +13 -13
- package/deepccc-agent/vitest.config.ts +7 -7
- package/package.json +73 -73
- package/src/__tests__/builtin-chat-session.test.ts +522 -522
- package/src/__tests__/builtin-config.test.ts +26 -26
- package/src/__tests__/builtin-context.test.ts +319 -319
- package/src/__tests__/builtin-file-tools.test.ts +240 -240
- package/src/__tests__/builtin-permissions.test.ts +211 -211
- package/src/__tests__/builtin-session-search.test.ts +262 -262
- package/src/__tests__/builtin-session-select.test.ts +116 -116
- package/src/__tests__/builtin-sigint.test.ts +56 -56
- package/src/__tests__/builtin-skills.test.ts +284 -284
- package/src/__tests__/builtin-web-tools.test.ts +220 -220
- package/src/__tests__/config.test.ts +17 -17
- package/src/__tests__/progress-reducer.test.ts +121 -121
- package/src/__tests__/session-ccc-config.test.ts +45 -45
- package/src/__tests__/session.test.ts +298 -298
- package/src/adapters/ccc-adapter.ts +145 -145
- package/src/config-utils.ts +13 -13
- package/src/config.ts +13 -13
- package/src/progress/reducer.ts +113 -113
- package/src/session-chat-binding.ts +83 -83
- package/src/session.ts +311 -311
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DEFAULT_BUILTIN_CONTEXT_DIR,
|
|
3
|
-
getBuiltinContextSession,
|
|
4
|
-
latestBuiltinSessionForCwd,
|
|
5
|
-
newBuiltinSessionId,
|
|
6
|
-
normalizeBuiltinSessionId,
|
|
7
|
-
} from "./context.js";
|
|
8
|
-
|
|
9
|
-
export type BuiltinResumeRequest = true | string | undefined;
|
|
10
|
-
|
|
11
|
-
export interface ResolveBuiltinSessionOptions {
|
|
12
|
-
cwd: string;
|
|
13
|
-
contextDir?: string;
|
|
14
|
-
resume?: BuiltinResumeRequest;
|
|
15
|
-
now?: Date;
|
|
16
|
-
randomSuffix?: string;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface ResolvedBuiltinSession {
|
|
20
|
-
mode: "new" | "resumed";
|
|
21
|
-
sessionId: string;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function resolveBuiltinSession(options: ResolveBuiltinSessionOptions): ResolvedBuiltinSession {
|
|
25
|
-
const contextDir = options.contextDir ?? DEFAULT_BUILTIN_CONTEXT_DIR;
|
|
26
|
-
|
|
27
|
-
if (options.resume === true) {
|
|
28
|
-
const latest = latestBuiltinSessionForCwd(options.cwd, contextDir);
|
|
29
|
-
if (!latest) {
|
|
30
|
-
throw new Error(`No resumable DeepCCC session found for cwd: ${options.cwd}`);
|
|
31
|
-
}
|
|
32
|
-
return { mode: "resumed", sessionId: latest.sessionId };
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (typeof options.resume === "string") {
|
|
36
|
-
const sessionId = normalizeBuiltinSessionId(options.resume);
|
|
37
|
-
const existing = getBuiltinContextSession(sessionId, contextDir);
|
|
38
|
-
if (!existing) {
|
|
39
|
-
throw new Error(`DeepCCC session not found: ${sessionId}`);
|
|
40
|
-
}
|
|
41
|
-
return { mode: "resumed", sessionId };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
mode: "new",
|
|
46
|
-
sessionId: newBuiltinSessionId(options.now, options.randomSuffix),
|
|
47
|
-
};
|
|
48
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_BUILTIN_CONTEXT_DIR,
|
|
3
|
+
getBuiltinContextSession,
|
|
4
|
+
latestBuiltinSessionForCwd,
|
|
5
|
+
newBuiltinSessionId,
|
|
6
|
+
normalizeBuiltinSessionId,
|
|
7
|
+
} from "./context.js";
|
|
8
|
+
|
|
9
|
+
export type BuiltinResumeRequest = true | string | undefined;
|
|
10
|
+
|
|
11
|
+
export interface ResolveBuiltinSessionOptions {
|
|
12
|
+
cwd: string;
|
|
13
|
+
contextDir?: string;
|
|
14
|
+
resume?: BuiltinResumeRequest;
|
|
15
|
+
now?: Date;
|
|
16
|
+
randomSuffix?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ResolvedBuiltinSession {
|
|
20
|
+
mode: "new" | "resumed";
|
|
21
|
+
sessionId: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function resolveBuiltinSession(options: ResolveBuiltinSessionOptions): ResolvedBuiltinSession {
|
|
25
|
+
const contextDir = options.contextDir ?? DEFAULT_BUILTIN_CONTEXT_DIR;
|
|
26
|
+
|
|
27
|
+
if (options.resume === true) {
|
|
28
|
+
const latest = latestBuiltinSessionForCwd(options.cwd, contextDir);
|
|
29
|
+
if (!latest) {
|
|
30
|
+
throw new Error(`No resumable DeepCCC session found for cwd: ${options.cwd}`);
|
|
31
|
+
}
|
|
32
|
+
return { mode: "resumed", sessionId: latest.sessionId };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (typeof options.resume === "string") {
|
|
36
|
+
const sessionId = normalizeBuiltinSessionId(options.resume);
|
|
37
|
+
const existing = getBuiltinContextSession(sessionId, contextDir);
|
|
38
|
+
if (!existing) {
|
|
39
|
+
throw new Error(`DeepCCC session not found: ${sessionId}`);
|
|
40
|
+
}
|
|
41
|
+
return { mode: "resumed", sessionId };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
mode: "new",
|
|
46
|
+
sessionId: newBuiltinSessionId(options.now, options.randomSuffix),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
export const CTRL_C_CONFIRM_WINDOW_MS = 2000;
|
|
2
|
-
|
|
3
|
-
export type CtrlCAction =
|
|
4
|
-
| "arm-interrupt"
|
|
5
|
-
| "interrupt"
|
|
6
|
-
| "arm-exit"
|
|
7
|
-
| "exit";
|
|
8
|
-
|
|
9
|
-
type PendingCtrlCAction = "interrupt" | "exit";
|
|
10
|
-
|
|
11
|
-
export interface CtrlCStateOptions {
|
|
12
|
-
windowMs?: number;
|
|
13
|
-
now?: () => number;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface CtrlCState {
|
|
17
|
-
press(isGenerating: boolean): CtrlCAction;
|
|
18
|
-
reset(): void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function createCtrlCState(options: CtrlCStateOptions = {}): CtrlCState {
|
|
22
|
-
const windowMs = options.windowMs ?? CTRL_C_CONFIRM_WINDOW_MS;
|
|
23
|
-
const now = options.now ?? Date.now;
|
|
24
|
-
|
|
25
|
-
let pending: PendingCtrlCAction | null = null;
|
|
26
|
-
let lastPressAt = 0;
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
press(isGenerating: boolean): CtrlCAction {
|
|
30
|
-
const action: PendingCtrlCAction = isGenerating ? "interrupt" : "exit";
|
|
31
|
-
const current = now();
|
|
32
|
-
const isConfirmed = pending === action && current - lastPressAt <= windowMs;
|
|
33
|
-
|
|
34
|
-
if (isConfirmed) {
|
|
35
|
-
pending = null;
|
|
36
|
-
lastPressAt = 0;
|
|
37
|
-
return action;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
pending = action;
|
|
41
|
-
lastPressAt = current;
|
|
42
|
-
return isGenerating ? "arm-interrupt" : "arm-exit";
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
reset(): void {
|
|
46
|
-
pending = null;
|
|
47
|
-
lastPressAt = 0;
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
}
|
|
1
|
+
export const CTRL_C_CONFIRM_WINDOW_MS = 2000;
|
|
2
|
+
|
|
3
|
+
export type CtrlCAction =
|
|
4
|
+
| "arm-interrupt"
|
|
5
|
+
| "interrupt"
|
|
6
|
+
| "arm-exit"
|
|
7
|
+
| "exit";
|
|
8
|
+
|
|
9
|
+
type PendingCtrlCAction = "interrupt" | "exit";
|
|
10
|
+
|
|
11
|
+
export interface CtrlCStateOptions {
|
|
12
|
+
windowMs?: number;
|
|
13
|
+
now?: () => number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CtrlCState {
|
|
17
|
+
press(isGenerating: boolean): CtrlCAction;
|
|
18
|
+
reset(): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function createCtrlCState(options: CtrlCStateOptions = {}): CtrlCState {
|
|
22
|
+
const windowMs = options.windowMs ?? CTRL_C_CONFIRM_WINDOW_MS;
|
|
23
|
+
const now = options.now ?? Date.now;
|
|
24
|
+
|
|
25
|
+
let pending: PendingCtrlCAction | null = null;
|
|
26
|
+
let lastPressAt = 0;
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
press(isGenerating: boolean): CtrlCAction {
|
|
30
|
+
const action: PendingCtrlCAction = isGenerating ? "interrupt" : "exit";
|
|
31
|
+
const current = now();
|
|
32
|
+
const isConfirmed = pending === action && current - lastPressAt <= windowMs;
|
|
33
|
+
|
|
34
|
+
if (isConfirmed) {
|
|
35
|
+
pending = null;
|
|
36
|
+
lastPressAt = 0;
|
|
37
|
+
return action;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
pending = action;
|
|
41
|
+
lastPressAt = current;
|
|
42
|
+
return isGenerating ? "arm-interrupt" : "arm-exit";
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
reset(): void {
|
|
46
|
+
pending = null;
|
|
47
|
+
lastPressAt = 0;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -1,205 +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
|
-
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
|
-
}
|
|
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
|
+
}
|