chatccc 0.2.251 → 0.2.253
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 +1 -1
- package/bin/cccagent.mjs +12 -3
- package/bin/chatccc.mjs +15 -6
- package/deepccc-agent/package.json +1 -1
- package/dist/deepccc-agent/src/cli.js +634 -0
- package/dist/deepccc-agent/src/config.js +76 -0
- package/dist/deepccc-agent/src/context.js +348 -0
- package/dist/deepccc-agent/src/file-log.js +34 -0
- package/dist/deepccc-agent/src/file-tools.js +1208 -0
- package/dist/deepccc-agent/src/index.js +571 -0
- package/dist/deepccc-agent/src/permissions.js +170 -0
- package/dist/deepccc-agent/src/privacy.js +124 -0
- package/dist/deepccc-agent/src/proc-tree-kill.js +60 -0
- package/dist/deepccc-agent/src/progress/cards-helpers.js +70 -0
- package/dist/deepccc-agent/src/progress/reducer.js +102 -0
- package/dist/deepccc-agent/src/progress/terminal-renderer.js +264 -0
- package/dist/deepccc-agent/src/progress/view.js +30 -0
- package/dist/deepccc-agent/src/raw-stream-log.js +106 -0
- package/dist/deepccc-agent/src/session-search.js +276 -0
- package/dist/deepccc-agent/src/session-select.js +23 -0
- package/dist/deepccc-agent/src/sigint.js +26 -0
- package/dist/deepccc-agent/src/skills.js +178 -0
- package/dist/deepccc-agent/src/web-tools.js +246 -0
- package/dist/src/adapters/adapter-interface.js +19 -0
- package/dist/src/adapters/ccc-adapter.js +112 -0
- package/dist/src/adapters/claude-adapter.js +497 -0
- package/dist/src/adapters/claude-session-meta-store.js +92 -0
- package/dist/src/adapters/codex-adapter.js +279 -0
- package/dist/src/adapters/codex-session-meta-store.js +94 -0
- package/dist/src/adapters/cursor-adapter.js +491 -0
- package/dist/src/adapters/cursor-session-meta-store.js +116 -0
- package/dist/src/adapters/jsonl-stream.js +104 -0
- package/{src/adapters/proc-tree-kill.ts → dist/src/adapters/proc-tree-kill.js} +94 -97
- package/dist/src/adapters/raw-stream-log.js +106 -0
- package/dist/src/adapters/resource-monitor.js +113 -0
- package/dist/src/agent-activity.js +133 -0
- package/dist/src/agent-delegate-task-rpc.js +129 -0
- package/dist/src/agent-delegate-task.js +48 -0
- package/dist/src/agent-file-rpc.js +152 -0
- package/dist/src/agent-image-rpc.js +148 -0
- package/dist/src/agent-platform-routing.js +13 -0
- package/dist/src/agent-reload-config-rpc.js +23 -0
- package/dist/src/agent-rpc-body.js +87 -0
- package/dist/src/agent-stop-stuck.js +110 -0
- package/dist/src/card-action-routing.js +7 -0
- package/dist/src/card-plain-text.js +101 -0
- package/dist/src/cardkit.js +158 -0
- package/dist/src/cards.js +573 -0
- package/dist/src/chatgpt-subscription-rpc.js +18 -0
- package/dist/src/chatgpt-subscription.js +199 -0
- package/dist/src/chrome-devtools-guard.js +238 -0
- package/dist/src/claude-sdk-installer.js +249 -0
- package/dist/src/codex-reset-actions.js +143 -0
- package/dist/src/config-utils.js +149 -0
- package/dist/src/config.js +804 -0
- package/dist/src/cursor-usage.js +77 -0
- package/dist/src/exit-banner.js +28 -0
- package/dist/src/feishu-api.js +1404 -0
- package/dist/src/feishu-message-ingress.js +137 -0
- package/dist/src/feishu-platform.js +97 -0
- package/dist/src/format-message.js +252 -0
- package/dist/src/git-command.js +155 -0
- package/dist/src/im-skills.js +121 -0
- package/dist/src/index.js +833 -0
- package/dist/src/litellm-proxy.js +300 -0
- package/dist/src/orchestrator.js +2078 -0
- package/dist/src/package-root.js +26 -0
- package/dist/src/platform-adapter.js +7 -0
- package/dist/src/platform-startup.js +6 -0
- package/dist/src/privacy.js +100 -0
- package/dist/src/progress/reducer.js +102 -0
- package/dist/src/progress/terminal-renderer.js +264 -0
- package/dist/src/progress/view.js +30 -0
- package/dist/src/response-stall.js +14 -0
- package/dist/src/runtime-entry.js +13 -0
- package/dist/src/runtime-reload.js +19 -0
- package/dist/src/session-chat-binding.js +183 -0
- package/dist/src/session-name.js +7 -0
- package/dist/src/session.js +2144 -0
- package/dist/src/shared-prefix.js +16 -0
- package/dist/src/shared.js +493 -0
- package/dist/src/sim-agent.js +105 -0
- package/dist/src/sim-platform.js +142 -0
- package/dist/src/sim-store.js +231 -0
- package/dist/src/simplify.js +99 -0
- package/dist/src/startup-lifecycle.js +209 -0
- package/dist/src/stream-state.js +141 -0
- package/dist/src/terminal-error.js +100 -0
- package/dist/src/trace.js +50 -0
- package/dist/src/turn-cards.js +92 -0
- package/dist/src/update-command-guard.js +114 -0
- package/{src/web-ui.ts → dist/src/web-ui.js} +749 -823
- package/dist/src/wechat-platform.js +545 -0
- package/package.json +7 -5
- package/deepccc-agent/LICENSE +0 -201
- package/deepccc-agent/bin/deepccc.mjs +0 -26
- package/deepccc-agent/docs/cache-hit-rate-1.jpg +0 -0
- package/deepccc-agent/docs/cache-hit-rate-2.jpg +0 -0
- package/deepccc-agent/package-lock.json +0 -2027
- package/deepccc-agent/src/__tests__/chat-session.test.ts +0 -852
- package/deepccc-agent/src/__tests__/cli-json.test.ts +0 -49
- package/deepccc-agent/src/__tests__/config.test.ts +0 -34
- package/deepccc-agent/src/__tests__/context.test.ts +0 -341
- package/deepccc-agent/src/__tests__/file-tools.test.ts +0 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +0 -199
- package/deepccc-agent/src/__tests__/privacy.test.ts +0 -318
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +0 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +0 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +0 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +0 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +0 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +0 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +0 -220
- package/deepccc-agent/src/cli.ts +0 -682
- package/deepccc-agent/src/config.ts +0 -101
- package/deepccc-agent/src/context.ts +0 -465
- package/deepccc-agent/src/file-log.ts +0 -38
- package/deepccc-agent/src/file-tools.ts +0 -1493
- package/deepccc-agent/src/index.ts +0 -676
- package/deepccc-agent/src/permissions.ts +0 -226
- package/deepccc-agent/src/privacy.ts +0 -141
- package/deepccc-agent/src/proc-tree-kill.ts +0 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +0 -76
- package/deepccc-agent/src/progress/reducer.ts +0 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +0 -294
- package/deepccc-agent/src/progress/view.ts +0 -77
- package/deepccc-agent/src/raw-stream-log.ts +0 -124
- package/deepccc-agent/src/session-search.ts +0 -370
- package/deepccc-agent/src/session-select.ts +0 -48
- package/deepccc-agent/src/sigint.ts +0 -50
- package/deepccc-agent/src/skills.ts +0 -205
- package/deepccc-agent/src/web-tools.ts +0 -313
- package/deepccc-agent/tsconfig.build.json +0 -13
- package/deepccc-agent/tsconfig.json +0 -13
- package/deepccc-agent/vitest.config.ts +0 -7
- package/src/__tests__/adapter-interface.test.ts +0 -152
- package/src/__tests__/agent-activity.test.ts +0 -86
- package/src/__tests__/agent-delegate-task-rpc.test.ts +0 -165
- package/src/__tests__/agent-image-rpc.test.ts +0 -34
- package/src/__tests__/agent-platform-routing.test.ts +0 -26
- package/src/__tests__/agent-reload-config-rpc.test.ts +0 -99
- package/src/__tests__/agent-rpc-body.test.ts +0 -42
- package/src/__tests__/builtin-chat-session.test.ts +0 -532
- package/src/__tests__/builtin-cli-json.test.ts +0 -39
- package/src/__tests__/builtin-config.test.ts +0 -26
- package/src/__tests__/builtin-context.test.ts +0 -319
- package/src/__tests__/builtin-file-tools.test.ts +0 -240
- package/src/__tests__/builtin-permissions.test.ts +0 -219
- package/src/__tests__/builtin-session-search.test.ts +0 -262
- package/src/__tests__/builtin-session-select.test.ts +0 -116
- package/src/__tests__/builtin-sigint.test.ts +0 -56
- package/src/__tests__/builtin-skills.test.ts +0 -284
- package/src/__tests__/builtin-web-tools.test.ts +0 -220
- package/src/__tests__/card-action-routing.test.ts +0 -18
- package/src/__tests__/card-plain-text.test.ts +0 -45
- package/src/__tests__/cardkit.test.ts +0 -60
- package/src/__tests__/cards.test.ts +0 -607
- package/src/__tests__/ccc-adapter.test.ts +0 -194
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +0 -89
- package/src/__tests__/chatgpt-subscription.test.ts +0 -135
- package/src/__tests__/chrome-devtools-guard.test.ts +0 -165
- package/src/__tests__/claude-adapter.test.ts +0 -614
- package/src/__tests__/claude-raw-stream-log.test.ts +0 -96
- package/src/__tests__/claude-sdk-installer.test.ts +0 -285
- package/src/__tests__/codex-adapter.test.ts +0 -331
- package/src/__tests__/codex-raw-stream-log.test.ts +0 -170
- package/src/__tests__/codex-reset-actions.test.ts +0 -146
- package/src/__tests__/config-reload.test.ts +0 -284
- package/src/__tests__/config-sample.test.ts +0 -97
- package/src/__tests__/config-utils.test.ts +0 -40
- package/src/__tests__/config.test.ts +0 -395
- package/src/__tests__/crash-logging.test.ts +0 -360
- package/src/__tests__/cursor-adapter.test.ts +0 -890
- package/src/__tests__/cursor-session-meta-store.test.ts +0 -212
- package/src/__tests__/feishu-api.test.ts +0 -60
- package/src/__tests__/feishu-avatar.test.ts +0 -504
- package/src/__tests__/feishu-message-ingress.test.ts +0 -138
- package/src/__tests__/feishu-platform.test.ts +0 -75
- package/src/__tests__/fixtures/codex_simple_text.jsonl +0 -4
- package/src/__tests__/fixtures/codex_with_tool.jsonl +0 -6
- package/src/__tests__/fixtures/cursor_partial_only.jsonl +0 -5
- package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +0 -13
- package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +0 -12
- package/src/__tests__/format-message.test.ts +0 -316
- package/src/__tests__/git-command.test.ts +0 -288
- package/src/__tests__/im-skills.test.ts +0 -125
- package/src/__tests__/jsonl-stream.test.ts +0 -79
- package/src/__tests__/orchestrator.test.ts +0 -1268
- package/src/__tests__/package-files.test.ts +0 -24
- package/src/__tests__/platform-startup.test.ts +0 -19
- package/src/__tests__/privacy.test.ts +0 -198
- package/src/__tests__/proc-tree-kill.test.ts +0 -108
- package/src/__tests__/progress-reducer.test.ts +0 -121
- package/src/__tests__/raw-stream-log.test.ts +0 -106
- package/src/__tests__/response-stall.test.ts +0 -49
- package/src/__tests__/restart.test.ts +0 -232
- package/src/__tests__/session-ccc-config.test.ts +0 -66
- package/src/__tests__/session.test.ts +0 -3004
- package/src/__tests__/shared-prefix.test.ts +0 -36
- package/src/__tests__/sim-agent.test.ts +0 -174
- package/src/__tests__/sim-platform.test.ts +0 -93
- package/src/__tests__/sim-store.test.ts +0 -214
- package/src/__tests__/simplify.test.ts +0 -283
- package/src/__tests__/startup-lifecycle.test.ts +0 -231
- package/src/__tests__/stop-session.test.ts +0 -162
- package/src/__tests__/stream-state.test.ts +0 -164
- package/src/__tests__/terminal-error.test.ts +0 -54
- package/src/__tests__/terminal-renderer.test.ts +0 -247
- package/src/__tests__/update-command-guard.test.ts +0 -144
- package/src/__tests__/web-ui.test.ts +0 -438
- package/src/__tests__/wechat-platform.test.ts +0 -111
- package/src/adapters/adapter-interface.ts +0 -217
- package/src/adapters/ccc-adapter.ts +0 -150
- package/src/adapters/claude-adapter.ts +0 -673
- package/src/adapters/claude-session-meta-store.ts +0 -120
- package/src/adapters/codex-adapter.ts +0 -426
- package/src/adapters/codex-session-meta-store.ts +0 -131
- package/src/adapters/cursor-adapter.ts +0 -681
- package/src/adapters/cursor-session-meta-store.ts +0 -154
- package/src/adapters/jsonl-stream.ts +0 -157
- package/src/adapters/raw-stream-log.ts +0 -124
- package/src/adapters/resource-monitor.ts +0 -141
- package/src/agent-activity.ts +0 -175
- package/src/agent-delegate-task-rpc.ts +0 -153
- package/src/agent-delegate-task.ts +0 -91
- package/src/agent-file-rpc.ts +0 -172
- package/src/agent-image-rpc.ts +0 -168
- package/src/agent-platform-routing.ts +0 -28
- package/src/agent-reload-config-rpc.ts +0 -34
- package/src/agent-rpc-body.ts +0 -92
- package/src/agent-stop-stuck.ts +0 -129
- package/src/card-action-routing.ts +0 -14
- package/src/card-plain-text.ts +0 -108
- package/src/cardkit.ts +0 -179
- package/src/cards.ts +0 -684
- package/src/chatgpt-subscription-rpc.ts +0 -27
- package/src/chatgpt-subscription.ts +0 -299
- package/src/chrome-devtools-guard.ts +0 -318
- package/src/claude-sdk-installer.ts +0 -324
- package/src/codex-reset-actions.ts +0 -184
- package/src/config-utils.ts +0 -211
- package/src/config.ts +0 -1063
- package/src/cursor-usage.ts +0 -128
- package/src/exit-banner.ts +0 -33
- package/src/feishu-api.ts +0 -1616
- package/src/feishu-message-ingress.ts +0 -195
- package/src/feishu-platform.ts +0 -159
- package/src/format-message.ts +0 -293
- package/src/git-command.ts +0 -202
- package/src/im-skills.ts +0 -149
- package/src/index.ts +0 -1089
- package/src/litellm-proxy.ts +0 -374
- package/src/orchestrator.ts +0 -2543
- package/src/platform-adapter.ts +0 -70
- package/src/platform-startup.ts +0 -16
- package/src/privacy.ts +0 -118
- package/src/progress/reducer.ts +0 -113
- package/src/progress/terminal-renderer.ts +0 -294
- package/src/progress/view.ts +0 -77
- package/src/response-stall.ts +0 -28
- package/src/runtime-reload.ts +0 -34
- package/src/session-chat-binding.ts +0 -292
- package/src/session-name.ts +0 -8
- package/src/session.ts +0 -2659
- package/src/shared-prefix.ts +0 -29
- package/src/shared.ts +0 -552
- package/src/sim-agent.ts +0 -167
- package/src/sim-platform.ts +0 -177
- package/src/sim-store.ts +0 -317
- package/src/simplify.ts +0 -120
- package/src/startup-lifecycle.ts +0 -250
- package/src/stream-state.ts +0 -177
- package/src/terminal-error.ts +0 -129
- package/src/trace.ts +0 -51
- package/src/turn-cards.ts +0 -118
- package/src/update-command-guard.ts +0 -165
- package/src/wechat-platform.ts +0 -680
|
@@ -1,370 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* session-search.ts — DeepCCC 历史会话关键词检索
|
|
3
|
-
*
|
|
4
|
-
* 供 agent 通过 session_search 工具按需查找很久以前的原始消息/工具调用:
|
|
5
|
-
* - 主数据源:~/.deepccc/sessions/<sessionId>/context.json(明文 JSON,含 summary、
|
|
6
|
-
* messages.content 与结构化 toolCalls)
|
|
7
|
-
* - 可选数据源:~/.deepccc/raw-stream-logs/deepccc/<sessionId>/*.jsonl.gz(gzip 原始流,
|
|
8
|
-
* 逐行解压检索,默认关闭)
|
|
9
|
-
*
|
|
10
|
-
* 纯关键词匹配(多词 AND、大小写不敏感),不依赖向量索引。
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { createReadStream, existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
14
|
-
import { join } from "node:path";
|
|
15
|
-
import { createInterface } from "node:readline";
|
|
16
|
-
import { createGunzip } from "node:zlib";
|
|
17
|
-
|
|
18
|
-
import { RAW_STREAM_LOGS_DIR } from "./config.js";
|
|
19
|
-
import { DEFAULT_BUILTIN_CONTEXT_DIR, type BuiltinContextRole } from "./context.js";
|
|
20
|
-
|
|
21
|
-
export interface SessionSearchOptions {
|
|
22
|
-
/** 会话目录,默认 ~/.deepccc/sessions */
|
|
23
|
-
contextDir?: string;
|
|
24
|
-
/** raw-stream-logs 根目录,默认 ~/.deepccc/raw-stream-logs */
|
|
25
|
-
rawLogsDir?: string;
|
|
26
|
-
/** 是否搜索 gzip 原始流日志(默认 false,较慢) */
|
|
27
|
-
includeRawLogs?: boolean;
|
|
28
|
-
/** 只搜索指定 sessionId(目录名或 state.sessionId 匹配) */
|
|
29
|
-
sessionId?: string;
|
|
30
|
-
/** 结果上限(默认 20,上限 50) */
|
|
31
|
-
maxResults?: number;
|
|
32
|
-
/** 单条命中片段最大字符数(默认 400) */
|
|
33
|
-
maxSnippetChars?: number;
|
|
34
|
-
/** 单个 raw log 文件最多解压检索的字节数(默认 4MB) */
|
|
35
|
-
maxRawLogBytesPerFile?: number;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface SessionSearchInput {
|
|
39
|
-
query: string;
|
|
40
|
-
session_id?: string;
|
|
41
|
-
include_raw_logs?: boolean;
|
|
42
|
-
max_results?: number;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface SessionSearchMatch {
|
|
46
|
-
sessionId: string;
|
|
47
|
-
source: "context" | "summary" | "raw-log";
|
|
48
|
-
role?: BuiltinContextRole;
|
|
49
|
-
/** 在 messages 数组中的下标 */
|
|
50
|
-
messageIndex?: number;
|
|
51
|
-
/** 在 message.toolCalls 数组中的下标(仅结构化工具调用命中时) */
|
|
52
|
-
toolCallIndex?: number;
|
|
53
|
-
toolCallName?: string;
|
|
54
|
-
snippet: string;
|
|
55
|
-
filePath: string;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface SessionSearchOutput {
|
|
59
|
-
query: string;
|
|
60
|
-
terms: string[];
|
|
61
|
-
matches: SessionSearchMatch[];
|
|
62
|
-
truncated: boolean;
|
|
63
|
-
scannedSessions: number;
|
|
64
|
-
scannedRawLogFiles: number;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const DEFAULT_MAX_RESULTS = 20;
|
|
68
|
-
const MAX_RESULTS_CAP = 50;
|
|
69
|
-
const DEFAULT_MAX_SNIPPET_CHARS = 400;
|
|
70
|
-
const MAX_SNIPPET_CHARS_CAP = 2_000;
|
|
71
|
-
const DEFAULT_MAX_RAW_LOG_BYTES_PER_FILE = 4 * 1024 * 1024;
|
|
72
|
-
|
|
73
|
-
function clamp(value: number, min: number, max: number): number {
|
|
74
|
-
return Math.min(max, Math.max(min, value));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function tokenize(query: string): string[] {
|
|
78
|
-
return query
|
|
79
|
-
.toLowerCase()
|
|
80
|
-
.split(/\s+/)
|
|
81
|
-
.map((term) => term.trim())
|
|
82
|
-
.filter((term) => term.length > 0);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/** 多词 AND、大小写不敏感:候选文本必须包含全部关键词 */
|
|
86
|
-
function matchesTerms(text: string, terms: readonly string[]): boolean {
|
|
87
|
-
const lower = text.toLowerCase();
|
|
88
|
-
return terms.every((term) => lower.includes(term));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function makeSnippet(text: string, terms: readonly string[], maxChars: number): string {
|
|
92
|
-
if (text.length <= maxChars) return text;
|
|
93
|
-
const lower = text.toLowerCase();
|
|
94
|
-
let hitIndex = -1;
|
|
95
|
-
for (const term of terms) {
|
|
96
|
-
const index = lower.indexOf(term);
|
|
97
|
-
if (index !== -1) {
|
|
98
|
-
hitIndex = index;
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
const half = Math.floor(maxChars / 2);
|
|
103
|
-
if (hitIndex === -1) {
|
|
104
|
-
return `${text.slice(0, half)}…${text.slice(-half)}`;
|
|
105
|
-
}
|
|
106
|
-
const start = Math.max(0, hitIndex - half);
|
|
107
|
-
const end = Math.min(text.length, hitIndex + half);
|
|
108
|
-
const prefix = start > 0 ? "…" : "";
|
|
109
|
-
const suffix = end < text.length ? "…" : "";
|
|
110
|
-
return `${prefix}${text.slice(start, end)}${suffix}`;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
interface ContextSearchResult {
|
|
114
|
-
matches: SessionSearchMatch[];
|
|
115
|
-
scannedSessions: number;
|
|
116
|
-
/** 命中总数(含被 maxResults 截断的部分),用于精确的 truncated 判定 */
|
|
117
|
-
totalHits: number;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function searchContextDir(
|
|
121
|
-
query: string,
|
|
122
|
-
options: SessionSearchOptions,
|
|
123
|
-
maxResults: number,
|
|
124
|
-
maxSnippetChars: number,
|
|
125
|
-
): ContextSearchResult {
|
|
126
|
-
const dir = options.contextDir ?? DEFAULT_BUILTIN_CONTEXT_DIR;
|
|
127
|
-
const terms = tokenize(query);
|
|
128
|
-
const matches: SessionSearchMatch[] = [];
|
|
129
|
-
if (terms.length === 0 || !existsSync(dir)) {
|
|
130
|
-
return { matches, scannedSessions: 0, totalHits: 0 };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const restrictTo = options.sessionId?.trim();
|
|
134
|
-
|
|
135
|
-
let scannedSessions = 0;
|
|
136
|
-
let totalHits = 0;
|
|
137
|
-
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
138
|
-
if (!entry.isDirectory()) continue;
|
|
139
|
-
if (restrictTo && entry.name !== restrictTo) continue;
|
|
140
|
-
const filePath = join(dir, entry.name, "context.json");
|
|
141
|
-
if (!existsSync(filePath)) continue;
|
|
142
|
-
|
|
143
|
-
let state: {
|
|
144
|
-
sessionId?: unknown;
|
|
145
|
-
summary?: unknown;
|
|
146
|
-
messages?: unknown;
|
|
147
|
-
};
|
|
148
|
-
try {
|
|
149
|
-
state = JSON.parse(readFileSync(filePath, "utf8")) as typeof state;
|
|
150
|
-
} catch {
|
|
151
|
-
continue; // 损坏或非 JSON 的会话文件跳过
|
|
152
|
-
}
|
|
153
|
-
if (!state || typeof state !== "object") continue;
|
|
154
|
-
if (restrictTo && state.sessionId !== restrictTo && entry.name !== restrictTo) continue;
|
|
155
|
-
scannedSessions += 1;
|
|
156
|
-
const sessionId = typeof state.sessionId === "string" ? state.sessionId : entry.name;
|
|
157
|
-
|
|
158
|
-
if (typeof state.summary === "string" && matchesTerms(state.summary, terms)) {
|
|
159
|
-
totalHits += 1;
|
|
160
|
-
matches.push({
|
|
161
|
-
sessionId,
|
|
162
|
-
source: "summary",
|
|
163
|
-
snippet: makeSnippet(state.summary, terms, maxSnippetChars),
|
|
164
|
-
filePath,
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (Array.isArray(state.messages)) {
|
|
169
|
-
state.messages.forEach((message, messageIndex) => {
|
|
170
|
-
if (!message || typeof message !== "object") return;
|
|
171
|
-
const raw = message as { role?: unknown; content?: unknown; toolCalls?: unknown };
|
|
172
|
-
const role: BuiltinContextRole | undefined =
|
|
173
|
-
raw.role === "user" || raw.role === "assistant" ? raw.role : undefined;
|
|
174
|
-
|
|
175
|
-
if (typeof raw.content === "string" && matchesTerms(raw.content, terms)) {
|
|
176
|
-
totalHits += 1;
|
|
177
|
-
matches.push({
|
|
178
|
-
sessionId,
|
|
179
|
-
source: "context",
|
|
180
|
-
role,
|
|
181
|
-
messageIndex,
|
|
182
|
-
snippet: makeSnippet(raw.content, terms, maxSnippetChars),
|
|
183
|
-
filePath,
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (Array.isArray(raw.toolCalls)) {
|
|
188
|
-
raw.toolCalls.forEach((call, toolCallIndex) => {
|
|
189
|
-
if (!call || typeof call !== "object") return;
|
|
190
|
-
const callRaw = call as { name?: unknown; input?: unknown; output?: unknown };
|
|
191
|
-
const name = typeof callRaw.name === "string" ? callRaw.name : "";
|
|
192
|
-
const input = typeof callRaw.input === "string" ? callRaw.input : "";
|
|
193
|
-
const output = typeof callRaw.output === "string" ? callRaw.output : "";
|
|
194
|
-
if (name.length === 0 && input.length === 0 && output.length === 0) return;
|
|
195
|
-
const haystack = [name, input, output].join("\n");
|
|
196
|
-
if (!matchesTerms(haystack, terms)) return;
|
|
197
|
-
totalHits += 1;
|
|
198
|
-
const source = output || input || name;
|
|
199
|
-
matches.push({
|
|
200
|
-
sessionId,
|
|
201
|
-
source: "context",
|
|
202
|
-
role,
|
|
203
|
-
messageIndex,
|
|
204
|
-
toolCallIndex,
|
|
205
|
-
toolCallName: name,
|
|
206
|
-
snippet: makeSnippet(source, terms, maxSnippetChars),
|
|
207
|
-
filePath,
|
|
208
|
-
});
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return { matches: matches.slice(0, maxResults), scannedSessions, totalHits };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
interface RawLogSearchResult {
|
|
219
|
-
matches: SessionSearchMatch[];
|
|
220
|
-
scannedFiles: number;
|
|
221
|
-
/** raw log 流式解压提前截断(达到 maxResults 停止扫描剩余文件) */
|
|
222
|
-
truncated: boolean;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async function searchRawLogs(
|
|
226
|
-
query: string,
|
|
227
|
-
options: SessionSearchOptions,
|
|
228
|
-
maxResults: number,
|
|
229
|
-
maxSnippetChars: number,
|
|
230
|
-
): Promise<RawLogSearchResult> {
|
|
231
|
-
const terms = tokenize(query);
|
|
232
|
-
const rootDir = options.rawLogsDir ?? RAW_STREAM_LOGS_DIR;
|
|
233
|
-
const matches: SessionSearchMatch[] = [];
|
|
234
|
-
if (terms.length === 0 || !existsSync(rootDir)) {
|
|
235
|
-
return { matches, scannedFiles: 0, truncated: false };
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const toolRoot = join(rootDir, "deepccc");
|
|
239
|
-
if (!existsSync(toolRoot)) return { matches, scannedFiles: 0, truncated: false };
|
|
240
|
-
|
|
241
|
-
const restrictTo = options.sessionId?.trim();
|
|
242
|
-
const maxBytesPerFile = Math.max(0, options.maxRawLogBytesPerFile ?? DEFAULT_MAX_RAW_LOG_BYTES_PER_FILE);
|
|
243
|
-
|
|
244
|
-
// 收集所有 .jsonl.gz 文件,按 mtime 新→旧排序(最新轮次优先)
|
|
245
|
-
const files: { path: string; sessionId: string; mtimeMs: number }[] = [];
|
|
246
|
-
for (const sessionEntry of readdirSync(toolRoot, { withFileTypes: true })) {
|
|
247
|
-
if (!sessionEntry.isDirectory()) continue;
|
|
248
|
-
if (restrictTo && sessionEntry.name !== restrictTo) continue;
|
|
249
|
-
const sessionDir = join(toolRoot, sessionEntry.name);
|
|
250
|
-
let entries;
|
|
251
|
-
try {
|
|
252
|
-
entries = readdirSync(sessionDir, { withFileTypes: true });
|
|
253
|
-
} catch {
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
for (const fileEntry of entries) {
|
|
257
|
-
if (!fileEntry.isFile() || !fileEntry.name.endsWith(".jsonl.gz")) continue;
|
|
258
|
-
const filePath = join(sessionDir, fileEntry.name);
|
|
259
|
-
try {
|
|
260
|
-
const info = statSync(filePath);
|
|
261
|
-
files.push({ path: filePath, sessionId: sessionEntry.name, mtimeMs: info.mtimeMs });
|
|
262
|
-
} catch {
|
|
263
|
-
// 无法 stat 的文件跳过
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
files.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
268
|
-
|
|
269
|
-
let scannedFiles = 0;
|
|
270
|
-
let truncated = false;
|
|
271
|
-
for (const file of files) {
|
|
272
|
-
if (matches.length >= maxResults) {
|
|
273
|
-
truncated = true;
|
|
274
|
-
break;
|
|
275
|
-
}
|
|
276
|
-
scannedFiles += 1;
|
|
277
|
-
const snippets = await searchGzipFileLines(file.path, terms, maxBytesPerFile, maxSnippetChars);
|
|
278
|
-
for (const snippet of snippets) {
|
|
279
|
-
matches.push({
|
|
280
|
-
sessionId: file.sessionId,
|
|
281
|
-
source: "raw-log",
|
|
282
|
-
snippet,
|
|
283
|
-
filePath: file.path,
|
|
284
|
-
});
|
|
285
|
-
if (matches.length >= maxResults) break;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return { matches: matches.slice(0, maxResults), scannedFiles, truncated };
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
async function searchGzipFileLines(
|
|
293
|
-
filePath: string,
|
|
294
|
-
terms: readonly string[],
|
|
295
|
-
maxBytes: number,
|
|
296
|
-
maxSnippetChars: number,
|
|
297
|
-
): Promise<string[]> {
|
|
298
|
-
const hits: string[] = [];
|
|
299
|
-
let bytes = 0;
|
|
300
|
-
try {
|
|
301
|
-
const source = createReadStream(filePath);
|
|
302
|
-
const gunzip = createGunzip();
|
|
303
|
-
// 防崩溃(线上事故):截断/损坏的 gzip 会在 zlib 层 emit 'error'(如
|
|
304
|
-
// "unexpected end of file")。若无人监听,错误事件会升级为 uncaughtException
|
|
305
|
-
// 直接杀死整个服务(崩溃黑匣子 exit(1))。显式挂监听把错误降级为
|
|
306
|
-
// “跳过该文件”:destroy 流让 for await 正常结束,不再冒泡。
|
|
307
|
-
source.on("error", () => gunzip.destroy());
|
|
308
|
-
gunzip.on("error", () => gunzip.destroy());
|
|
309
|
-
const reader = createInterface({ input: source.pipe(gunzip), crlfDelay: Infinity });
|
|
310
|
-
// readline 会把 input 流的 error 转发到自己身上,同样需要监听避免冒泡
|
|
311
|
-
reader.on("error", () => {});
|
|
312
|
-
for await (const line of reader) {
|
|
313
|
-
bytes += Buffer.byteLength(line, "utf-8");
|
|
314
|
-
if (bytes > maxBytes) break;
|
|
315
|
-
if (!matchesTerms(line, terms)) continue;
|
|
316
|
-
hits.push(makeSnippet(line, terms, maxSnippetChars));
|
|
317
|
-
if (hits.length >= 50) break;
|
|
318
|
-
}
|
|
319
|
-
} catch {
|
|
320
|
-
// 损坏的 gzip 文件按 best-effort 跳过
|
|
321
|
-
}
|
|
322
|
-
return hits;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* 关键词检索历史会话存档。context.json 为同步扫描;raw-stream-logs 为
|
|
327
|
-
* gzip 逐行解压(异步,仅 options.includeRawLogs 时启用)。
|
|
328
|
-
*/
|
|
329
|
-
export async function searchBuiltinSessions(
|
|
330
|
-
query: string,
|
|
331
|
-
options: SessionSearchOptions = {},
|
|
332
|
-
): Promise<SessionSearchOutput> {
|
|
333
|
-
const terms = tokenize(query);
|
|
334
|
-
const maxResults = clamp(options.maxResults ?? DEFAULT_MAX_RESULTS, 1, MAX_RESULTS_CAP);
|
|
335
|
-
const maxSnippetChars = clamp(
|
|
336
|
-
options.maxSnippetChars ?? DEFAULT_MAX_SNIPPET_CHARS,
|
|
337
|
-
80,
|
|
338
|
-
MAX_SNIPPET_CHARS_CAP,
|
|
339
|
-
);
|
|
340
|
-
|
|
341
|
-
if (terms.length === 0) {
|
|
342
|
-
return {
|
|
343
|
-
query,
|
|
344
|
-
terms,
|
|
345
|
-
matches: [],
|
|
346
|
-
truncated: false,
|
|
347
|
-
scannedSessions: 0,
|
|
348
|
-
scannedRawLogFiles: 0,
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
const contextResult = searchContextDir(query, options, maxResults, maxSnippetChars);
|
|
353
|
-
let rawResult: RawLogSearchResult = { matches: [], scannedFiles: 0, truncated: false };
|
|
354
|
-
if (options.includeRawLogs) {
|
|
355
|
-
rawResult = await searchRawLogs(query, options, maxResults, maxSnippetChars);
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const matches = [...contextResult.matches, ...rawResult.matches].slice(0, maxResults);
|
|
359
|
-
const truncated =
|
|
360
|
-
contextResult.totalHits > maxResults || rawResult.truncated || rawResult.matches.length > maxResults;
|
|
361
|
-
|
|
362
|
-
return {
|
|
363
|
-
query,
|
|
364
|
-
terms,
|
|
365
|
-
matches,
|
|
366
|
-
truncated,
|
|
367
|
-
scannedSessions: contextResult.scannedSessions,
|
|
368
|
-
scannedRawLogFiles: rawResult.scannedFiles,
|
|
369
|
-
};
|
|
370
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
|
-
"## 可用技能",
|
|
169
|
-
"技能会从 Claude/Codex/Cursor/DeepCCC 技能目录并行扫描。",
|
|
170
|
-
"名称冲突时:DeepCCC > Codex > Cursor > Claude 优先;同一来源内,项目级优先于全局。",
|
|
171
|
-
"当用户请求匹配某个技能的描述时,先用 read_file 读取其完整 SKILL.md,然后严格按其指令执行。",
|
|
172
|
-
"",
|
|
173
|
-
...skills.map((s) => `- **${s.name}** [${s.source}:${s.scope}] (\`${normalizeSkillPathForPrompt(s.skillPath)}\`): ${s.description || "(无描述)"}`),
|
|
174
|
-
"",
|
|
175
|
-
"## 创建技能",
|
|
176
|
-
"当用户要求创建技能时,按 Codex 风格的目录技能创建:",
|
|
177
|
-
"~/.deepccc/skills/<name>/SKILL.md(全局,默认)或 <cwd>/.deepccc/skills/<name>/SKILL.md(项目级,仅当用户明确要求项目级技能时)。",
|
|
178
|
-
"SKILL.md 格式:",
|
|
179
|
-
"```",
|
|
180
|
-
"---",
|
|
181
|
-
"name: <skill-name>",
|
|
182
|
-
"description: <one-line description>",
|
|
183
|
-
"---",
|
|
184
|
-
"",
|
|
185
|
-
"<instructions>",
|
|
186
|
-
"```",
|
|
187
|
-
"新技能会在下一条消息自动生效(热加载);无需重启。",
|
|
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
|
-
}
|