@frogmoo/dsh-supermemory 0.0.0-stage → 1.4.1
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/CHANGELOG.md +442 -0
- package/INSTALL.md +193 -0
- package/LICENSE +21 -0
- package/README.md +220 -2
- package/cordis.patch.yml +9 -0
- package/lib/capability-T5x_17iK.js +2971 -0
- package/lib/client.js +4217 -0
- package/lib/clip-kqqR05NC.js +279 -0
- package/lib/export-XY8Bfglx.js +144 -0
- package/lib/filemap-CsF4oYtT.js +166 -0
- package/lib/group-CkFv7052.js +313 -0
- package/lib/hub-sessions-pzURuUmH.js +1010 -0
- package/lib/memory-collections-DaSVGbrR.js +686 -0
- package/lib/model-Z4_-9sAj.js +735 -0
- package/lib/rolldown-runtime-BJohB4LM.js +13 -0
- package/lib/skill-forge-DHxPo_w6.js +2348 -0
- package/lib/store-DD2L3GDr.js +531 -0
- package/lib/supermemory.js +15015 -0
- package/lib/toolize-B9Mbl7sp.js +426 -0
- package/package.json +67 -4
|
@@ -0,0 +1,2348 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-BJohB4LM.js";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
const MAX_FILES = 10;
|
|
6
|
+
const MAX_COMMANDS = 8;
|
|
7
|
+
const MAX_OBJECTS = 3;
|
|
8
|
+
const TOOL_NAMES = /* @__PURE__ */ new Set([
|
|
9
|
+
"bash",
|
|
10
|
+
"read",
|
|
11
|
+
"write",
|
|
12
|
+
"edit",
|
|
13
|
+
"grep",
|
|
14
|
+
"glob",
|
|
15
|
+
"run_code",
|
|
16
|
+
"run-code",
|
|
17
|
+
"run",
|
|
18
|
+
"tool",
|
|
19
|
+
"tools",
|
|
20
|
+
"todo_write",
|
|
21
|
+
"todo",
|
|
22
|
+
"skill",
|
|
23
|
+
"present",
|
|
24
|
+
"web_search",
|
|
25
|
+
"web_fetch",
|
|
26
|
+
"memory_recall",
|
|
27
|
+
"memory_remember",
|
|
28
|
+
"memory_dispatch",
|
|
29
|
+
"memory_relay",
|
|
30
|
+
"memory_toolize",
|
|
31
|
+
"memory_ui",
|
|
32
|
+
"memory_scan",
|
|
33
|
+
"memory_topicsets",
|
|
34
|
+
"memoryset",
|
|
35
|
+
"plugin_manager"
|
|
36
|
+
]);
|
|
37
|
+
const STOP_WORDS = /* @__PURE__ */ new Set([
|
|
38
|
+
"the",
|
|
39
|
+
"and",
|
|
40
|
+
"for",
|
|
41
|
+
"with",
|
|
42
|
+
"from",
|
|
43
|
+
"that",
|
|
44
|
+
"this",
|
|
45
|
+
"when",
|
|
46
|
+
"then",
|
|
47
|
+
"than",
|
|
48
|
+
"into",
|
|
49
|
+
"over",
|
|
50
|
+
"under",
|
|
51
|
+
"about",
|
|
52
|
+
"after",
|
|
53
|
+
"before",
|
|
54
|
+
"while",
|
|
55
|
+
"where",
|
|
56
|
+
"which",
|
|
57
|
+
"what",
|
|
58
|
+
"who",
|
|
59
|
+
"how",
|
|
60
|
+
"why",
|
|
61
|
+
"all",
|
|
62
|
+
"any",
|
|
63
|
+
"each",
|
|
64
|
+
"every",
|
|
65
|
+
"some",
|
|
66
|
+
"not",
|
|
67
|
+
"but",
|
|
68
|
+
"but",
|
|
69
|
+
"can",
|
|
70
|
+
"could",
|
|
71
|
+
"should",
|
|
72
|
+
"would",
|
|
73
|
+
"will",
|
|
74
|
+
"must",
|
|
75
|
+
"need",
|
|
76
|
+
"needs",
|
|
77
|
+
"needed",
|
|
78
|
+
"use",
|
|
79
|
+
"used",
|
|
80
|
+
"using",
|
|
81
|
+
"make",
|
|
82
|
+
"makes",
|
|
83
|
+
"made",
|
|
84
|
+
"get",
|
|
85
|
+
"gets",
|
|
86
|
+
"got",
|
|
87
|
+
"set",
|
|
88
|
+
"sets",
|
|
89
|
+
"run",
|
|
90
|
+
"runs",
|
|
91
|
+
"ran",
|
|
92
|
+
"task",
|
|
93
|
+
"tasks",
|
|
94
|
+
"work",
|
|
95
|
+
"works",
|
|
96
|
+
"thing",
|
|
97
|
+
"things",
|
|
98
|
+
"case",
|
|
99
|
+
"cases",
|
|
100
|
+
"way",
|
|
101
|
+
"ways",
|
|
102
|
+
"file",
|
|
103
|
+
"files",
|
|
104
|
+
"path",
|
|
105
|
+
"paths",
|
|
106
|
+
"line",
|
|
107
|
+
"lines",
|
|
108
|
+
"code",
|
|
109
|
+
"true",
|
|
110
|
+
"false",
|
|
111
|
+
"null",
|
|
112
|
+
"http",
|
|
113
|
+
"https",
|
|
114
|
+
"json",
|
|
115
|
+
"stdio",
|
|
116
|
+
"stdout",
|
|
117
|
+
"stderr",
|
|
118
|
+
"exit",
|
|
119
|
+
"note",
|
|
120
|
+
"notes",
|
|
121
|
+
"bash",
|
|
122
|
+
"shell",
|
|
123
|
+
"command",
|
|
124
|
+
"commands",
|
|
125
|
+
"cmd",
|
|
126
|
+
"pnpm",
|
|
127
|
+
"npm",
|
|
128
|
+
"yarn",
|
|
129
|
+
"node",
|
|
130
|
+
"git",
|
|
131
|
+
"cd",
|
|
132
|
+
"ls",
|
|
133
|
+
"cat",
|
|
134
|
+
"sed",
|
|
135
|
+
"grep",
|
|
136
|
+
"awk",
|
|
137
|
+
"find",
|
|
138
|
+
"echo",
|
|
139
|
+
"head",
|
|
140
|
+
"tail",
|
|
141
|
+
"wc",
|
|
142
|
+
"mkdir",
|
|
143
|
+
"src",
|
|
144
|
+
"lib",
|
|
145
|
+
"dist",
|
|
146
|
+
"build",
|
|
147
|
+
"docs",
|
|
148
|
+
"scripts",
|
|
149
|
+
"test",
|
|
150
|
+
"tests",
|
|
151
|
+
"index",
|
|
152
|
+
"main"
|
|
153
|
+
]);
|
|
154
|
+
/** Names that can never be an object of a readable task skill. */
|
|
155
|
+
const GENERIC_OBJECTS = /* @__PURE__ */ new Set([
|
|
156
|
+
"bash",
|
|
157
|
+
"shell",
|
|
158
|
+
"command",
|
|
159
|
+
"commands",
|
|
160
|
+
"cmd",
|
|
161
|
+
"tool",
|
|
162
|
+
"tools",
|
|
163
|
+
"flow",
|
|
164
|
+
"seq",
|
|
165
|
+
"sequence",
|
|
166
|
+
"run",
|
|
167
|
+
"code",
|
|
168
|
+
"run-code",
|
|
169
|
+
"read",
|
|
170
|
+
"write",
|
|
171
|
+
"edit",
|
|
172
|
+
"grep",
|
|
173
|
+
"glob",
|
|
174
|
+
"task",
|
|
175
|
+
"work",
|
|
176
|
+
"file",
|
|
177
|
+
"files",
|
|
178
|
+
"path",
|
|
179
|
+
"paths",
|
|
180
|
+
"src",
|
|
181
|
+
"lib",
|
|
182
|
+
"dist",
|
|
183
|
+
"build",
|
|
184
|
+
"docs",
|
|
185
|
+
"scripts",
|
|
186
|
+
"test",
|
|
187
|
+
"tests",
|
|
188
|
+
"index",
|
|
189
|
+
"main",
|
|
190
|
+
"package-json",
|
|
191
|
+
"tsconfig",
|
|
192
|
+
"readme",
|
|
193
|
+
"changelog",
|
|
194
|
+
"license",
|
|
195
|
+
"install",
|
|
196
|
+
"dev",
|
|
197
|
+
"start",
|
|
198
|
+
"tmp",
|
|
199
|
+
"temp",
|
|
200
|
+
"output",
|
|
201
|
+
"assets"
|
|
202
|
+
]);
|
|
203
|
+
/**
|
|
204
|
+
* Action words are NOT objects: a task named 「修复 X」 must not become
|
|
205
|
+
* `fix-fix-x`. These tokens are dropped from text-object candidates.
|
|
206
|
+
*/
|
|
207
|
+
const VERB_TOKENS = /* @__PURE__ */ new Set([
|
|
208
|
+
"fix",
|
|
209
|
+
"fixing",
|
|
210
|
+
"repair",
|
|
211
|
+
"debug",
|
|
212
|
+
"add",
|
|
213
|
+
"adding",
|
|
214
|
+
"implement",
|
|
215
|
+
"create",
|
|
216
|
+
"build",
|
|
217
|
+
"refactor",
|
|
218
|
+
"restructure",
|
|
219
|
+
"rework",
|
|
220
|
+
"rewrite",
|
|
221
|
+
"migrate",
|
|
222
|
+
"cleanup",
|
|
223
|
+
"clean",
|
|
224
|
+
"archive",
|
|
225
|
+
"remove",
|
|
226
|
+
"delete",
|
|
227
|
+
"release",
|
|
228
|
+
"publish",
|
|
229
|
+
"packageing",
|
|
230
|
+
"ship",
|
|
231
|
+
"sync",
|
|
232
|
+
"push",
|
|
233
|
+
"pull",
|
|
234
|
+
"commit",
|
|
235
|
+
"rebase",
|
|
236
|
+
"verify",
|
|
237
|
+
"test",
|
|
238
|
+
"validate",
|
|
239
|
+
"smoke",
|
|
240
|
+
"check",
|
|
241
|
+
"review",
|
|
242
|
+
"audit",
|
|
243
|
+
"inspect",
|
|
244
|
+
"merge",
|
|
245
|
+
"integrate",
|
|
246
|
+
"configure",
|
|
247
|
+
"config",
|
|
248
|
+
"enable",
|
|
249
|
+
"disable",
|
|
250
|
+
"update",
|
|
251
|
+
"refresh",
|
|
252
|
+
"upgrade",
|
|
253
|
+
"bump",
|
|
254
|
+
"investigate",
|
|
255
|
+
"analyze",
|
|
256
|
+
"analyse",
|
|
257
|
+
"compare",
|
|
258
|
+
"diff",
|
|
259
|
+
"diagnose",
|
|
260
|
+
"run",
|
|
261
|
+
"runs",
|
|
262
|
+
"build",
|
|
263
|
+
"built",
|
|
264
|
+
"use",
|
|
265
|
+
"using",
|
|
266
|
+
"make",
|
|
267
|
+
"change"
|
|
268
|
+
]);
|
|
269
|
+
/**
|
|
270
|
+
* Context/architecture words: useful for retrieval, but never the task's
|
|
271
|
+
* object when a concrete domain noun exists (repo name, client/server half).
|
|
272
|
+
*/
|
|
273
|
+
const CONTEXT_OBJECT_NAMES = /* @__PURE__ */ new Set([
|
|
274
|
+
"supermemory",
|
|
275
|
+
"dsh-supermemory",
|
|
276
|
+
"@frogmoo/dsh-supermemory",
|
|
277
|
+
"dsh",
|
|
278
|
+
"client",
|
|
279
|
+
"server",
|
|
280
|
+
"frontend",
|
|
281
|
+
"backend"
|
|
282
|
+
]);
|
|
283
|
+
const GENERIC_FILE_NAMES = /* @__PURE__ */ new Set([
|
|
284
|
+
"index",
|
|
285
|
+
"main",
|
|
286
|
+
"package",
|
|
287
|
+
"package-lock",
|
|
288
|
+
"pnpm-lock",
|
|
289
|
+
"tsconfig",
|
|
290
|
+
"readme",
|
|
291
|
+
"changelog",
|
|
292
|
+
"license",
|
|
293
|
+
"vite",
|
|
294
|
+
"tsdown",
|
|
295
|
+
"vitest",
|
|
296
|
+
"eslint",
|
|
297
|
+
"prettier",
|
|
298
|
+
"config",
|
|
299
|
+
"types",
|
|
300
|
+
"type",
|
|
301
|
+
"utils",
|
|
302
|
+
"util",
|
|
303
|
+
"helper",
|
|
304
|
+
"helpers",
|
|
305
|
+
"common",
|
|
306
|
+
"constants",
|
|
307
|
+
"consts",
|
|
308
|
+
"env",
|
|
309
|
+
"gitignore",
|
|
310
|
+
"node_modules",
|
|
311
|
+
"dist",
|
|
312
|
+
"lib",
|
|
313
|
+
"src",
|
|
314
|
+
"scripts",
|
|
315
|
+
"test",
|
|
316
|
+
"tests",
|
|
317
|
+
"docs",
|
|
318
|
+
"build",
|
|
319
|
+
"out",
|
|
320
|
+
"tmp",
|
|
321
|
+
"temp",
|
|
322
|
+
"output",
|
|
323
|
+
"assets",
|
|
324
|
+
"public",
|
|
325
|
+
"coverage",
|
|
326
|
+
"skill",
|
|
327
|
+
"skills",
|
|
328
|
+
"session"
|
|
329
|
+
]);
|
|
330
|
+
/**
|
|
331
|
+
* Chinese NOUNS → stable English object slugs (longest phrase wins).
|
|
332
|
+
* Action words belong to VERB_RULES; keeping them out of this table is what
|
|
333
|
+
* stops names like `release-release-pack`.
|
|
334
|
+
*/
|
|
335
|
+
const OBJECT_LEXICON = [
|
|
336
|
+
["发布资产", "release-assets"],
|
|
337
|
+
["版本控制", "version-control"],
|
|
338
|
+
["组合工具", "composite"],
|
|
339
|
+
["固化能力", "capability"],
|
|
340
|
+
["话题集", "topicset"],
|
|
341
|
+
["工作区", "workspace"],
|
|
342
|
+
["管理台", "webui"],
|
|
343
|
+
["页签", "tab"],
|
|
344
|
+
["标签", "tag"],
|
|
345
|
+
["面板", "panel"],
|
|
346
|
+
["记忆", "memory"],
|
|
347
|
+
["版本", "version"],
|
|
348
|
+
["分支", "branch"],
|
|
349
|
+
["会话", "session"],
|
|
350
|
+
["中枢", "hub"],
|
|
351
|
+
["派发", "dispatch"],
|
|
352
|
+
["技能", "skill"],
|
|
353
|
+
["固化", "toolize"],
|
|
354
|
+
["能力", "capability"],
|
|
355
|
+
["检索", "recall"],
|
|
356
|
+
["召回", "recall"],
|
|
357
|
+
["注入", "inject"],
|
|
358
|
+
["路由", "route"],
|
|
359
|
+
["仓库", "repo"],
|
|
360
|
+
["文档", "docs"],
|
|
361
|
+
["脚本", "script"],
|
|
362
|
+
["依赖", "deps"],
|
|
363
|
+
["闸门", "gate"],
|
|
364
|
+
["回归", "regression"],
|
|
365
|
+
["设置", "settings"],
|
|
366
|
+
["压缩包", "package"],
|
|
367
|
+
["安装包", "package"],
|
|
368
|
+
["包体", "package"],
|
|
369
|
+
["双远端", "dual-remote"]
|
|
370
|
+
];
|
|
371
|
+
/** Verb rules: first matching rule wins (specific before broad). */
|
|
372
|
+
const VERB_RULES = [
|
|
373
|
+
{
|
|
374
|
+
re: /重构|改造|重排|调整|迁移|切换|替换|改成|改为|改名|改/,
|
|
375
|
+
verb: "refactor",
|
|
376
|
+
zh: "改造"
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
re: /清理|归档|删除|移除|清掉|清空|干掉/,
|
|
380
|
+
verb: "cleanup",
|
|
381
|
+
zh: "清理"
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
re: /发布|发版|打包|出包|上传|发包/,
|
|
385
|
+
verb: "release",
|
|
386
|
+
zh: "发布"
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
re: /同步|推送|提交|双推|拉取|合并远端/,
|
|
390
|
+
verb: "sync",
|
|
391
|
+
zh: "同步"
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
re: /验证|测试|回归|闸门|跑闸门|自测/,
|
|
395
|
+
verb: "verify",
|
|
396
|
+
zh: "验证"
|
|
397
|
+
},
|
|
398
|
+
{
|
|
399
|
+
re: /审查|评审|审阅|检查|核对|复核/,
|
|
400
|
+
verb: "review",
|
|
401
|
+
zh: "审查"
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
re: /合并|整合|归并|汇总/,
|
|
405
|
+
verb: "merge",
|
|
406
|
+
zh: "合并"
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
re: /实现|新增|增加|添加|加入|加上|编写|开发|补上|补齐/,
|
|
410
|
+
verb: "add",
|
|
411
|
+
zh: "实现"
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
re: /修复|修正|修改|修|排查|诊断|定位|解决|处理|修掉|搞掉|搞定/,
|
|
415
|
+
verb: "fix",
|
|
416
|
+
zh: "修复"
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
re: /配置|设置|开启|关闭|打开|启用|停用/,
|
|
420
|
+
verb: "config",
|
|
421
|
+
zh: "配置"
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
re: /更新|刷新|升级|升版/,
|
|
425
|
+
verb: "update",
|
|
426
|
+
zh: "更新"
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
re: /说明|文档|记录|写文档/,
|
|
430
|
+
verb: "docs",
|
|
431
|
+
zh: "说明"
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
re: /调研|分析|确认|评估|比较|对比/,
|
|
435
|
+
verb: "investigate",
|
|
436
|
+
zh: "调研"
|
|
437
|
+
}
|
|
438
|
+
];
|
|
439
|
+
const ENGLISH_VERBS = [
|
|
440
|
+
[
|
|
441
|
+
/\b(refactor|restructure|rework|rewrite|reorgani[sz]e|migrate|switch)\b/i,
|
|
442
|
+
"refactor",
|
|
443
|
+
"改造"
|
|
444
|
+
],
|
|
445
|
+
[
|
|
446
|
+
/\b(cleanup|clean-up|archive|remove|delete|prune|purge)\b/i,
|
|
447
|
+
"cleanup",
|
|
448
|
+
"清理"
|
|
449
|
+
],
|
|
450
|
+
[
|
|
451
|
+
/\b(release|publish|package|bundle|ship)\b/i,
|
|
452
|
+
"release",
|
|
453
|
+
"发布"
|
|
454
|
+
],
|
|
455
|
+
[
|
|
456
|
+
/\b(sync|push|pull|commit|rebase|merge-remote)\b/i,
|
|
457
|
+
"sync",
|
|
458
|
+
"同步"
|
|
459
|
+
],
|
|
460
|
+
[
|
|
461
|
+
/\b(verify|test|validate|smoke|check|gate)\b/i,
|
|
462
|
+
"verify",
|
|
463
|
+
"验证"
|
|
464
|
+
],
|
|
465
|
+
[
|
|
466
|
+
/\b(review|audit|inspect)\b/i,
|
|
467
|
+
"review",
|
|
468
|
+
"审查"
|
|
469
|
+
],
|
|
470
|
+
[
|
|
471
|
+
/\b(merge|integrate|consolidate)\b/i,
|
|
472
|
+
"merge",
|
|
473
|
+
"合并"
|
|
474
|
+
],
|
|
475
|
+
[
|
|
476
|
+
/\b(add|implement|create|build|introduce|support)\b/i,
|
|
477
|
+
"add",
|
|
478
|
+
"实现"
|
|
479
|
+
],
|
|
480
|
+
[
|
|
481
|
+
/\b(fix|repair|debug|diagnose|resolve|patch|correct)\b/i,
|
|
482
|
+
"fix",
|
|
483
|
+
"修复"
|
|
484
|
+
],
|
|
485
|
+
[
|
|
486
|
+
/\b(configure|config|setting|enable|disable|toggle)\b/i,
|
|
487
|
+
"config",
|
|
488
|
+
"配置"
|
|
489
|
+
],
|
|
490
|
+
[
|
|
491
|
+
/\b(update|refresh|upgrade|bump)\b/i,
|
|
492
|
+
"update",
|
|
493
|
+
"更新"
|
|
494
|
+
],
|
|
495
|
+
[
|
|
496
|
+
/\b(docs?|document|documentation|readme)\b/i,
|
|
497
|
+
"docs",
|
|
498
|
+
"说明"
|
|
499
|
+
],
|
|
500
|
+
[
|
|
501
|
+
/\b(investigate|analyze|analyse|compare|diff|diagnose)\b/i,
|
|
502
|
+
"investigate",
|
|
503
|
+
"调研"
|
|
504
|
+
]
|
|
505
|
+
];
|
|
506
|
+
const WRAPPER_COMMANDS = /* @__PURE__ */ new Set([
|
|
507
|
+
"bash",
|
|
508
|
+
"sh",
|
|
509
|
+
"zsh",
|
|
510
|
+
"cmd",
|
|
511
|
+
"powershell",
|
|
512
|
+
"pwsh",
|
|
513
|
+
"wsl"
|
|
514
|
+
]);
|
|
515
|
+
const INSPECT_COMMANDS = /* @__PURE__ */ new Set([
|
|
516
|
+
"cd",
|
|
517
|
+
"ls",
|
|
518
|
+
"dir",
|
|
519
|
+
"cat",
|
|
520
|
+
"sed",
|
|
521
|
+
"awk",
|
|
522
|
+
"grep",
|
|
523
|
+
"rg",
|
|
524
|
+
"find",
|
|
525
|
+
"head",
|
|
526
|
+
"tail",
|
|
527
|
+
"wc",
|
|
528
|
+
"which",
|
|
529
|
+
"where",
|
|
530
|
+
"pwd",
|
|
531
|
+
"echo",
|
|
532
|
+
"printf",
|
|
533
|
+
"env",
|
|
534
|
+
"export",
|
|
535
|
+
"set",
|
|
536
|
+
"test",
|
|
537
|
+
"true",
|
|
538
|
+
"false",
|
|
539
|
+
"sleep",
|
|
540
|
+
"date",
|
|
541
|
+
"touch",
|
|
542
|
+
"stat",
|
|
543
|
+
"file",
|
|
544
|
+
"du",
|
|
545
|
+
"df",
|
|
546
|
+
"tree",
|
|
547
|
+
"type",
|
|
548
|
+
"more",
|
|
549
|
+
"less"
|
|
550
|
+
]);
|
|
551
|
+
const GENERIC_SCRIPTS = /* @__PURE__ */ new Set([
|
|
552
|
+
"typecheck",
|
|
553
|
+
"build",
|
|
554
|
+
"smoke",
|
|
555
|
+
"test",
|
|
556
|
+
"tests",
|
|
557
|
+
"lint",
|
|
558
|
+
"format",
|
|
559
|
+
"install",
|
|
560
|
+
"dev",
|
|
561
|
+
"start",
|
|
562
|
+
"clean",
|
|
563
|
+
"check",
|
|
564
|
+
"tsdown",
|
|
565
|
+
"tsc",
|
|
566
|
+
"vitest",
|
|
567
|
+
"prepare",
|
|
568
|
+
"prepublish",
|
|
569
|
+
"release:dry",
|
|
570
|
+
"pack",
|
|
571
|
+
"release",
|
|
572
|
+
"publish",
|
|
573
|
+
"bundle",
|
|
574
|
+
"ship"
|
|
575
|
+
]);
|
|
576
|
+
const WINDOWS_PATH_RE = /(?:[A-Za-z]:[\\/])[^\s'"`,;:)\]}]+/g;
|
|
577
|
+
const FILE_PATH_RE = /(?:[\w.@+-]+[\\/])*[\w@.-]+\.(?:ts|tsx|js|mjs|cjs|json|md|yml|yaml|sh|ps1|html|css|scss|toml|lock|py|go|rs|java|kt|tgz|zip|svg|png|jpg)\b/gi;
|
|
578
|
+
const POSIX_PATH_RE = /(?:^|[\s'"`(=])((?:\.{0,2}\/)[\w.@%+-]+(?:\/[\w.@%+-]+)+|[\w.@%+-]+\/[\w.@%+-]+(?:\/[\w.@%+-]+)*)/g;
|
|
579
|
+
const ASCII_TOKEN_RE = /[A-Za-z][A-Za-z0-9]*(?:[-_.:][A-Za-z0-9]+)*/g;
|
|
580
|
+
/** Position of a needle in a text (9999 = not mentioned). */
|
|
581
|
+
function positionIn(text, needle) {
|
|
582
|
+
if (!text || !needle) return 9999;
|
|
583
|
+
const pos = text.toLowerCase().indexOf(needle.toLowerCase());
|
|
584
|
+
return pos < 0 ? 9999 : pos;
|
|
585
|
+
}
|
|
586
|
+
/** Trim and collapse whitespace. */
|
|
587
|
+
function clean(text) {
|
|
588
|
+
return String(text ?? "").replace(/\s+/g, " ").trim();
|
|
589
|
+
}
|
|
590
|
+
function looksLikeUrl(value) {
|
|
591
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(value) || /(?:^|[^\w])(?:https?|ftp):\/\//i.test(value);
|
|
592
|
+
}
|
|
593
|
+
/** Normalize a path-looking string (posix separators, no quotes, no drive tail). */
|
|
594
|
+
function normalizePathText(value) {
|
|
595
|
+
let out = clean(value).replace(/^['"`]+|['"`]+$/g, "");
|
|
596
|
+
if (looksLikeUrl(out)) return "";
|
|
597
|
+
out = out.replace(/\\/g, "/");
|
|
598
|
+
out = out.replace(/#L\d+(-L?\d+)?$/i, "");
|
|
599
|
+
out = out.replace(/^file:\/\//i, "");
|
|
600
|
+
out = out.replace(/[),.;]+$/g, "");
|
|
601
|
+
return out.trim();
|
|
602
|
+
}
|
|
603
|
+
function basenameOf(path) {
|
|
604
|
+
const parts = path.split("/").filter(Boolean);
|
|
605
|
+
return parts[parts.length - 1] ?? path;
|
|
606
|
+
}
|
|
607
|
+
function stemOf(file) {
|
|
608
|
+
return file.replace(/\.[a-z0-9]+$/i, "");
|
|
609
|
+
}
|
|
610
|
+
function isAbsolutePath(path) {
|
|
611
|
+
return /^[A-Za-z]:\//.test(path) || path.startsWith("/");
|
|
612
|
+
}
|
|
613
|
+
function isGenericName(name) {
|
|
614
|
+
return GENERIC_FILE_NAMES.has(name.toLowerCase()) || name.length < 3;
|
|
615
|
+
}
|
|
616
|
+
function slugify(text, max = 32) {
|
|
617
|
+
return String(text ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, max).replace(/-+$/g, "");
|
|
618
|
+
}
|
|
619
|
+
/** Every path-looking token in one text (task, command or argument). */
|
|
620
|
+
function extractPathTokens(text) {
|
|
621
|
+
const raw = String(text ?? "");
|
|
622
|
+
if (!raw) return [];
|
|
623
|
+
const out = /* @__PURE__ */ new Set();
|
|
624
|
+
for (const match of raw.matchAll(WINDOWS_PATH_RE)) {
|
|
625
|
+
const path = normalizePathText(match[0]);
|
|
626
|
+
if (path) out.add(path);
|
|
627
|
+
}
|
|
628
|
+
for (const match of raw.matchAll(FILE_PATH_RE)) {
|
|
629
|
+
const path = normalizePathText(match[0]);
|
|
630
|
+
if (path && !looksLikeUrl(path)) out.add(path);
|
|
631
|
+
}
|
|
632
|
+
for (const match of raw.matchAll(POSIX_PATH_RE)) {
|
|
633
|
+
const path = normalizePathText(match[1] ?? "");
|
|
634
|
+
if (path && !looksLikeUrl(path) && path.includes("/")) out.add(path);
|
|
635
|
+
}
|
|
636
|
+
return [...out].slice(0, 40);
|
|
637
|
+
}
|
|
638
|
+
/** Command families observed in bash-like arguments. */
|
|
639
|
+
function extractCommandFamilies(samples) {
|
|
640
|
+
const commands = /* @__PURE__ */ new Set();
|
|
641
|
+
const objects = /* @__PURE__ */ new Map();
|
|
642
|
+
const addObject = (slug, display, source, score) => {
|
|
643
|
+
const key = slug.toLowerCase();
|
|
644
|
+
if (!key || GENERIC_OBJECTS.has(key) || VERB_TOKENS.has(key)) return;
|
|
645
|
+
const existing = objects.get(key);
|
|
646
|
+
if (!existing || existing.score < score) objects.set(key, {
|
|
647
|
+
slug: key,
|
|
648
|
+
score,
|
|
649
|
+
source,
|
|
650
|
+
display,
|
|
651
|
+
pos: 9999
|
|
652
|
+
});
|
|
653
|
+
};
|
|
654
|
+
for (const sample of samples) sample.tools.forEach((tool, index) => {
|
|
655
|
+
if (tool !== "bash" && tool !== "terminal" && tool !== "shell") return;
|
|
656
|
+
const args = sample.args[index] ?? {};
|
|
657
|
+
const command = typeof args.command === "string" ? args.command : "";
|
|
658
|
+
if (!command) return;
|
|
659
|
+
for (const family of commandFamiliesOf(command)) commands.add(family.family);
|
|
660
|
+
for (const object of commandObjectsOf(command)) addObject(object.slug, object.display, "command", object.score);
|
|
661
|
+
});
|
|
662
|
+
return {
|
|
663
|
+
commands: [...commands].slice(0, MAX_COMMANDS),
|
|
664
|
+
objects: [...objects.values()]
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
function stripCommandPrefix(segment) {
|
|
668
|
+
let out = segment.trim();
|
|
669
|
+
out = out.replace(/^(?:sudo|time|env\s+[\w-]+=\S*)\s+/gi, "");
|
|
670
|
+
out = out.replace(/^(?:[A-Za-z_][A-Za-z0-9_]*=\S*\s+)+/, "");
|
|
671
|
+
return out.trim();
|
|
672
|
+
}
|
|
673
|
+
/** Split one shell command into (normalized) simple command segments. */
|
|
674
|
+
function commandSegments(command) {
|
|
675
|
+
return String(command ?? "").split(/&&|\|\||;|\n|\|/).map((part) => stripCommandPrefix(part)).filter(Boolean);
|
|
676
|
+
}
|
|
677
|
+
function commandFamilyOf(segment) {
|
|
678
|
+
const words = segment.match(/(?:[^\s"']+|"[^"]*"|'[^']*')+/g)?.map((word) => word.replace(/^["']|["']$/g, "")) ?? [];
|
|
679
|
+
if (words.length === 0) return void 0;
|
|
680
|
+
const executable = basenameOf(words[0].replace(/\\\\/g, "/")).replace(/\.(?:exe|cmd|bat)$/i, "").toLowerCase();
|
|
681
|
+
if (!executable) return void 0;
|
|
682
|
+
if (WRAPPER_COMMANDS.has(executable) || INSPECT_COMMANDS.has(executable)) return void 0;
|
|
683
|
+
if (executable === "pnpm" || executable === "npm" || executable === "yarn" || executable === "bun") {
|
|
684
|
+
let script = words[1] ?? "";
|
|
685
|
+
if (script === "run" || script === "exec" || script === "dlx") script = words[2] ?? "";
|
|
686
|
+
if (!script) return { family: executable };
|
|
687
|
+
const slug = slugify(script.replace(/^:/, ""), 28);
|
|
688
|
+
const generic = GENERIC_SCRIPTS.has(slug) || script.startsWith("-");
|
|
689
|
+
return {
|
|
690
|
+
family: executable + " " + script,
|
|
691
|
+
...slug && !generic ? { object: {
|
|
692
|
+
slug,
|
|
693
|
+
display: script,
|
|
694
|
+
score: 2
|
|
695
|
+
} } : {}
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
if (executable === "node" || executable === "tsx" || executable === "ts-node") {
|
|
699
|
+
const script = words.slice(1).find((word) => !word.startsWith("-")) ?? "";
|
|
700
|
+
if (!script) return { family: executable };
|
|
701
|
+
const file = basenameOf(script.replace(/\\\\/g, "/"));
|
|
702
|
+
const name = stemOf(file);
|
|
703
|
+
return {
|
|
704
|
+
family: executable + " " + file,
|
|
705
|
+
...name && !isGenericName(name) ? { object: {
|
|
706
|
+
slug: slugify(name, 32),
|
|
707
|
+
display: name,
|
|
708
|
+
score: 2
|
|
709
|
+
} } : {}
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
if (executable === "git") {
|
|
713
|
+
const sub = words.slice(1).find((word) => !word.startsWith("-")) ?? "";
|
|
714
|
+
if (!sub) return { family: "git" };
|
|
715
|
+
return { family: "git " + sub };
|
|
716
|
+
}
|
|
717
|
+
if (executable === "gh") {
|
|
718
|
+
const sub = words.slice(1).filter((word) => !word.startsWith("-")).slice(0, 2).join(" ");
|
|
719
|
+
return { family: sub ? "gh " + sub : "gh" };
|
|
720
|
+
}
|
|
721
|
+
return { family: executable };
|
|
722
|
+
}
|
|
723
|
+
function commandFamiliesOf(command) {
|
|
724
|
+
return commandSegments(command).map((segment) => commandFamilyOf(segment)).filter((value) => Boolean(value));
|
|
725
|
+
}
|
|
726
|
+
function commandObjectsOf(command) {
|
|
727
|
+
const out = [];
|
|
728
|
+
for (const family of commandFamiliesOf(command)) if (family.object) out.push({
|
|
729
|
+
slug: family.object.slug,
|
|
730
|
+
score: family.object.score,
|
|
731
|
+
source: "command",
|
|
732
|
+
display: family.object.display,
|
|
733
|
+
pos: 9999
|
|
734
|
+
});
|
|
735
|
+
return out;
|
|
736
|
+
}
|
|
737
|
+
/** Files/directories + object candidates from every recorded argument/command. */
|
|
738
|
+
function extractFileFacts(samples) {
|
|
739
|
+
const files = /* @__PURE__ */ new Set();
|
|
740
|
+
const dirs = /* @__PURE__ */ new Set();
|
|
741
|
+
const objects = /* @__PURE__ */ new Map();
|
|
742
|
+
const addObject = (slug, display, source, score, pos = 9999) => {
|
|
743
|
+
const key = slug.toLowerCase();
|
|
744
|
+
if (!key || GENERIC_OBJECTS.has(key) || VERB_TOKENS.has(key)) return;
|
|
745
|
+
const existing = objects.get(key);
|
|
746
|
+
if (!existing || existing.score < score || existing.score === score && pos < existing.pos) objects.set(key, {
|
|
747
|
+
slug: key,
|
|
748
|
+
score,
|
|
749
|
+
source,
|
|
750
|
+
display,
|
|
751
|
+
pos: Math.min(existing?.pos ?? 9999, pos)
|
|
752
|
+
});
|
|
753
|
+
};
|
|
754
|
+
const considerPath = (raw, taskText = "") => {
|
|
755
|
+
const path = normalizePathText(raw);
|
|
756
|
+
if (!path || looksLikeUrl(path) || path.length > 240) return;
|
|
757
|
+
const display = isAbsolutePath(path) ? basenameOf(path) : path;
|
|
758
|
+
if (/\.(?:ts|tsx|js|mjs|cjs|json|md|yml|yaml|sh|ps1|html|css|scss|toml|lock|py|go|rs|tgz|zip)$/i.test(path)) {
|
|
759
|
+
files.add(display);
|
|
760
|
+
const base = basenameOf(path);
|
|
761
|
+
const name = stemOf(base).toLowerCase();
|
|
762
|
+
if (/\.(?:tgz|zip|tar|gz|7z)$/i.test(path)) addObject("package", stemOf(base), "file", 3, positionIn(taskText, base));
|
|
763
|
+
else if (!isGenericName(name)) addObject(slugify(name, 32), stemOf(base), "file", 3, positionIn(taskText, base));
|
|
764
|
+
}
|
|
765
|
+
const dirParts = path.split("/").filter(Boolean).filter((part) => !/^[A-Za-z]:$/.test(part)).slice(0, -1);
|
|
766
|
+
for (const dir of dirParts.slice(-3)) {
|
|
767
|
+
const lowered = dir.toLowerCase();
|
|
768
|
+
if (isGenericName(lowered) || lowered.startsWith(".")) continue;
|
|
769
|
+
dirs.add(dir);
|
|
770
|
+
addObject(slugify(dir, 28), dir, "dir", 2, positionIn(taskText, dir));
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
for (const sample of samples) {
|
|
774
|
+
const taskText = sample.task ?? "";
|
|
775
|
+
sample.tools.forEach((tool, index) => {
|
|
776
|
+
const args = sample.args[index] ?? {};
|
|
777
|
+
for (const key of [
|
|
778
|
+
"file_path",
|
|
779
|
+
"path",
|
|
780
|
+
"cwd",
|
|
781
|
+
"directory",
|
|
782
|
+
"pattern",
|
|
783
|
+
"glob",
|
|
784
|
+
"description"
|
|
785
|
+
]) {
|
|
786
|
+
const value = args[key];
|
|
787
|
+
if (typeof value === "string" && value.length > 0) for (const token of extractPathTokens(value)) considerPath(token, taskText);
|
|
788
|
+
}
|
|
789
|
+
if (typeof args.command === "string") for (const token of extractPathTokens(args.command)) considerPath(token, taskText);
|
|
790
|
+
});
|
|
791
|
+
if (sample.task) for (const token of extractPathTokens(sample.task)) considerPath(token, sample.task);
|
|
792
|
+
}
|
|
793
|
+
return {
|
|
794
|
+
files: [...files].slice(0, MAX_FILES),
|
|
795
|
+
dirs: [...dirs].slice(0, 6),
|
|
796
|
+
objects: [...objects.values()]
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
/** Domain-noun objects from the task text (Chinese lexicon + ASCII identifiers). */
|
|
800
|
+
function extractTextObjects(text) {
|
|
801
|
+
const raw = String(text ?? "");
|
|
802
|
+
const objects = /* @__PURE__ */ new Map();
|
|
803
|
+
const keywords = /* @__PURE__ */ new Set();
|
|
804
|
+
const add = (slug, display, score, pos) => {
|
|
805
|
+
const key = slug.toLowerCase();
|
|
806
|
+
if (!key || GENERIC_OBJECTS.has(key) || STOP_WORDS.has(key) || VERB_TOKENS.has(key)) return;
|
|
807
|
+
const weight = CONTEXT_OBJECT_NAMES.has(key) ? Math.min(score, 1) : score;
|
|
808
|
+
const existing = objects.get(key);
|
|
809
|
+
if (!existing || existing.score < weight || existing.score === weight && pos < existing.pos) objects.set(key, {
|
|
810
|
+
slug: key,
|
|
811
|
+
score: weight,
|
|
812
|
+
source: "text",
|
|
813
|
+
display,
|
|
814
|
+
pos
|
|
815
|
+
});
|
|
816
|
+
if (keywords.size < 12) keywords.add(display);
|
|
817
|
+
};
|
|
818
|
+
for (const [term, slug] of OBJECT_LEXICON) {
|
|
819
|
+
const pos = raw.indexOf(term);
|
|
820
|
+
if (pos >= 0) add(slug, slug, term.length >= 4 ? 3 : 2, pos);
|
|
821
|
+
}
|
|
822
|
+
for (const match of raw.matchAll(ASCII_TOKEN_RE)) {
|
|
823
|
+
const token = match[0].replace(/\.(?:ts|tsx|js|mjs|cjs|json|md|yml|yaml|sh|ps1|html|css|scss|toml)$/i, "");
|
|
824
|
+
if (token.length < 3 || token.length > 40) continue;
|
|
825
|
+
if (/^[rv]?\d+(?:\.\d+)*$/i.test(token)) continue;
|
|
826
|
+
const lowered = token.toLowerCase();
|
|
827
|
+
if (STOP_WORDS.has(lowered) || TOOL_NAMES.has(lowered) || VERB_TOKENS.has(lowered)) continue;
|
|
828
|
+
const identifier = /[-_.:]/.test(token);
|
|
829
|
+
const slug = slugify(token, 36);
|
|
830
|
+
if (!slug) continue;
|
|
831
|
+
add(slug, token, identifier ? 3 : 2, match.index ?? 9999);
|
|
832
|
+
}
|
|
833
|
+
return {
|
|
834
|
+
objects: [...objects.values()],
|
|
835
|
+
keywords: [...keywords]
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* Pick the verb from the EARLIEST match in the task text (ties go to the more
|
|
840
|
+
* specific rule), then fall back to command/tool evidence. Earliest-match
|
|
841
|
+
* matters: 「修X,改为Y」 is a fix task, not a refactor task.
|
|
842
|
+
*/
|
|
843
|
+
function detectTaskVerb(text, tools, commands) {
|
|
844
|
+
const raw = String(text ?? "");
|
|
845
|
+
let best;
|
|
846
|
+
VERB_RULES.forEach((rule, rank) => {
|
|
847
|
+
const match = rule.re.exec(raw);
|
|
848
|
+
if (!match || match.index < 0) return;
|
|
849
|
+
if (!best || match.index < best.index || match.index === best.index && rank < best.rank) best = {
|
|
850
|
+
index: match.index,
|
|
851
|
+
verb: rule.verb,
|
|
852
|
+
zh: rule.zh,
|
|
853
|
+
rank
|
|
854
|
+
};
|
|
855
|
+
});
|
|
856
|
+
ENGLISH_VERBS.forEach(([re, verb, zh], rank) => {
|
|
857
|
+
const match = re.exec(raw);
|
|
858
|
+
if (!match || match.index < 0) return;
|
|
859
|
+
if (!best || match.index < best.index || match.index === best.index && rank < best.rank) best = {
|
|
860
|
+
index: match.index,
|
|
861
|
+
verb,
|
|
862
|
+
zh,
|
|
863
|
+
rank
|
|
864
|
+
};
|
|
865
|
+
});
|
|
866
|
+
if (best !== void 0) return {
|
|
867
|
+
verb: best.verb,
|
|
868
|
+
verbZh: best.zh,
|
|
869
|
+
fromText: true
|
|
870
|
+
};
|
|
871
|
+
const joined = commands.join(" ");
|
|
872
|
+
if (/\bgit\s+(?:push|commit|rebase|fetch|pull)\b/.test(joined) || /git:sync/.test(joined)) return {
|
|
873
|
+
verb: "sync",
|
|
874
|
+
verbZh: "同步",
|
|
875
|
+
fromText: false
|
|
876
|
+
};
|
|
877
|
+
if (/\bpnpm\s+(?:build|tsdown)\b|\btsdown\b/.test(joined)) return {
|
|
878
|
+
verb: "build",
|
|
879
|
+
verbZh: "构建",
|
|
880
|
+
fromText: false
|
|
881
|
+
};
|
|
882
|
+
if (/\bpnpm\s+(?:typecheck|smoke|test|lint)\b|\btsc\b/.test(joined)) return {
|
|
883
|
+
verb: "verify",
|
|
884
|
+
verbZh: "验证",
|
|
885
|
+
fromText: false
|
|
886
|
+
};
|
|
887
|
+
if (/\b(?:tar|zip|7z|gzip)\b|\bgh release\b/.test(joined)) return {
|
|
888
|
+
verb: "release",
|
|
889
|
+
verbZh: "发布",
|
|
890
|
+
fromText: false
|
|
891
|
+
};
|
|
892
|
+
if (tools.some((tool) => tool === "edit" || tool === "write")) return {
|
|
893
|
+
verb: "fix",
|
|
894
|
+
verbZh: "修复",
|
|
895
|
+
fromText: false
|
|
896
|
+
};
|
|
897
|
+
if (tools.some((tool) => tool === "read" || tool === "grep" || tool === "glob")) return {
|
|
898
|
+
verb: "review",
|
|
899
|
+
verbZh: "审查",
|
|
900
|
+
fromText: false
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
function sortObjects(objects) {
|
|
904
|
+
const byScore = [...objects].sort((left, right) => right.score - left.score || left.pos - right.pos || right.slug.length - left.slug.length || left.slug.localeCompare(right.slug));
|
|
905
|
+
const out = [];
|
|
906
|
+
for (const candidate of byScore) if (!out.some((existing) => existing.slug === candidate.slug || existing.slug.split("-").includes(candidate.slug) || candidate.slug.split("-").includes(existing.slug))) out.push(candidate);
|
|
907
|
+
return out;
|
|
908
|
+
}
|
|
909
|
+
/** Extract a stable task signature; undefined means "not worth a skill". */
|
|
910
|
+
/** First non-empty line, clipped: a dispatch/user request states its topic there. */
|
|
911
|
+
function taskHeadline(task) {
|
|
912
|
+
return (String(task ?? "").split(/\r?\n/).map((line) => line.trim()).find((line) => line.length > 0) ?? "").slice(0, 180).trim();
|
|
913
|
+
}
|
|
914
|
+
function extractTaskSignature(input) {
|
|
915
|
+
const rawTask = String(input.task ?? "");
|
|
916
|
+
const task = clean(rawTask);
|
|
917
|
+
const tools = (input.tools ?? []).filter((tool) => typeof tool === "string" && tool.length > 0);
|
|
918
|
+
if (tools.length < 2) return void 0;
|
|
919
|
+
if (task.length < 6) return void 0;
|
|
920
|
+
const samples = [{
|
|
921
|
+
...input,
|
|
922
|
+
tools,
|
|
923
|
+
args: input.args ?? []
|
|
924
|
+
}];
|
|
925
|
+
const headline = taskHeadline(rawTask);
|
|
926
|
+
const headlineText = extractTextObjects(headline);
|
|
927
|
+
const fullText = extractTextObjects(task);
|
|
928
|
+
const text = headlineText.objects.some((object) => object.score >= 2) ? headlineText : fullText;
|
|
929
|
+
const fileFacts = extractFileFacts(samples);
|
|
930
|
+
const commandFacts = extractCommandFamilies(samples);
|
|
931
|
+
let verb = detectTaskVerb(headline, tools, commandFacts.commands) ?? detectTaskVerb(task, tools, commandFacts.commands);
|
|
932
|
+
if (!verb) return void 0;
|
|
933
|
+
const artifactFile = fileFacts.files.find((file) => /\.(?:tgz|zip|7z|tar\.gz)$/i.test(file));
|
|
934
|
+
if (artifactFile && /release|发布|发版|打包|发包/i.test(task)) verb = {
|
|
935
|
+
verb: "release",
|
|
936
|
+
verbZh: "发布",
|
|
937
|
+
fromText: true
|
|
938
|
+
};
|
|
939
|
+
const textObjects = sortObjects(text.objects).filter((object) => object.score >= 2);
|
|
940
|
+
const evidenceObjects = sortObjects([...fileFacts.objects, ...commandFacts.objects]).filter((object) => object.score >= 2);
|
|
941
|
+
const chosen = textObjects.length > 0 ? textObjects : evidenceObjects;
|
|
942
|
+
if (chosen.length === 0) return void 0;
|
|
943
|
+
let objects = chosen.slice(0, MAX_OBJECTS);
|
|
944
|
+
if (verb.verb === "release" && artifactFile) {
|
|
945
|
+
const ext = artifactFile.toLowerCase().endsWith(".tar.gz") ? "tar-gz" : artifactFile.toLowerCase().split(".").pop() ?? "";
|
|
946
|
+
const artifactObjects = [{
|
|
947
|
+
slug: "package",
|
|
948
|
+
score: 3,
|
|
949
|
+
source: "file",
|
|
950
|
+
display: "package",
|
|
951
|
+
pos: 9999
|
|
952
|
+
}, ...ext && /^[a-z0-9]{2,8}$/.test(ext) ? [{
|
|
953
|
+
slug: ext,
|
|
954
|
+
score: 2,
|
|
955
|
+
source: "file",
|
|
956
|
+
display: ext,
|
|
957
|
+
pos: 9999
|
|
958
|
+
}] : []];
|
|
959
|
+
const artifactSlugs = new Set(artifactObjects.map((object) => object.slug));
|
|
960
|
+
objects = [...artifactObjects, ...objects.filter((object) => !artifactSlugs.has(object.slug))].slice(0, MAX_OBJECTS);
|
|
961
|
+
}
|
|
962
|
+
const primary = objects[0];
|
|
963
|
+
const dimensions = new Set(objects.map((object) => object.source));
|
|
964
|
+
if (evidenceObjects.some((object) => object.slug.includes(primary.slug) || primary.slug.includes(object.slug))) dimensions.add("file");
|
|
965
|
+
if (commandFacts.commands.length > 0) dimensions.add("command");
|
|
966
|
+
let score = 0;
|
|
967
|
+
if (task.length >= 8) score += 2;
|
|
968
|
+
if (verb.fromText) score += 2;
|
|
969
|
+
score += Math.min(3, primary.score + (primary.score >= 3 ? 1 : 0));
|
|
970
|
+
if (dimensions.size >= 2) score += 2;
|
|
971
|
+
if (commandFacts.commands.some(isVerificationCommand)) score += 1;
|
|
972
|
+
if (objects.length >= 2) score += 1;
|
|
973
|
+
const confidence = Math.min(1, score / 11);
|
|
974
|
+
if (confidence < .5) return void 0;
|
|
975
|
+
const key = verb.verb + "/" + primary.slug;
|
|
976
|
+
const nameBase = [verb.verb, ...objects.slice(0, 2).map((object) => object.slug)].join("-");
|
|
977
|
+
const title = verb.verbZh + " " + objects.slice(0, 2).map((object) => object.display).join(" / ");
|
|
978
|
+
const keywords = [.../* @__PURE__ */ new Set([...objects.map((object) => object.display), ...text.keywords])].slice(0, 8);
|
|
979
|
+
return {
|
|
980
|
+
key,
|
|
981
|
+
verb: verb.verb,
|
|
982
|
+
verbZh: verb.verbZh,
|
|
983
|
+
object: primary.slug,
|
|
984
|
+
objects: objects.map((object) => object.slug),
|
|
985
|
+
nameBase,
|
|
986
|
+
title,
|
|
987
|
+
files: fileFacts.files,
|
|
988
|
+
dirs: fileFacts.dirs,
|
|
989
|
+
commands: commandFacts.commands,
|
|
990
|
+
keywords,
|
|
991
|
+
hasTaskText: true,
|
|
992
|
+
confidence
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
/** Verification-flavoured command families (used by the body + confidence). */
|
|
996
|
+
function isVerificationCommand(family) {
|
|
997
|
+
return /\b(?:pnpm|npm|yarn)\s+(?:typecheck|build|smoke|test|lint|check)\b|\btsc\b|\bvitest\b|\bnode\s+scripts\/[\w.-]*smoke/i.test(family);
|
|
998
|
+
}
|
|
999
|
+
/** True when the name only talks about a tool flow (defensive check). */
|
|
1000
|
+
function looksLikeToolFlowName(name) {
|
|
1001
|
+
const lowered = String(name ?? "").toLowerCase();
|
|
1002
|
+
if (/^(?:sm-)?(?:ws[a-z0-9]{5}-)?flow(?:-|$)/.test(lowered)) return true;
|
|
1003
|
+
return /(?:^|-)(?:bash|read|write|edit|grep|glob|run-code|run_code|tool)(?:-|$)/.test(lowered);
|
|
1004
|
+
}
|
|
1005
|
+
// src/skill-forge.ts
|
|
1006
|
+
/**
|
|
1007
|
+
* Skill forge (R45 / R48): repeated WORK becomes a REAL DSH skill.
|
|
1008
|
+
*
|
|
1009
|
+
* 背景:P3 把重复流程编译成 `sm_composite_*` 组合工具——「按什么顺序调哪些
|
|
1010
|
+
* 工具」。R45 把沉淀改到 DSH 的 skill 机制(`<project>/.dsh/skills/<name>/SKILL.md`,
|
|
1011
|
+
* 项目根判定见 dsh-skill-filesystem:最近含 `.git` 的祖先),但自动生成退化成
|
|
1012
|
+
* 了按工具序列命名(`sm-flow-bash-bash-…`):没有对象、没有参数、没有坑,还会
|
|
1013
|
+
* 挤占 available_skills 的路由注意力。
|
|
1014
|
+
*
|
|
1015
|
+
* R48 起识别的主体是「做过的事」,不是「连续调了哪些工具」:
|
|
1016
|
+
* - 任务签名来自任务/派发文本(动词 + 对象)、真实文件/目录族、命令家族与关键
|
|
1017
|
+
* 参数(`pnpm git:sync`、`pnpm typecheck`、`git push`…);
|
|
1018
|
+
* - 同一任务签名跨 ≥N 次、≥2 个会话/派发才产出草稿;工具序列只作为正文里
|
|
1019
|
+
* 的历史事实,不参与命名与聚合;
|
|
1020
|
+
* - 名称是 `sm-<verb>-<object>`(`fix-skill-forge`、`sync-gitee-github`),
|
|
1021
|
+
* 描述写「做什么任务时加载」,正文六节全部来自真实样本,缺信息写「待补充」;
|
|
1022
|
+
* - 历史遗留的工具序列壳子由 archiveShellSkills 归档(不删除,写 manifest)。
|
|
1023
|
+
*
|
|
1024
|
+
* 文件是事实来源(重启后仍在),tool_memory 的 kind='skill' 行只承载版本号、
|
|
1025
|
+
* 使用计数与启用状态(不新增存储表,旧介质保持 v1 兼容)。同名更新 = version+1
|
|
1026
|
+
* 并保留 changelog;内容完全一致 = 幂等不升版;检测到人工编辑 = 只更新统计,
|
|
1027
|
+
* 绝不覆盖正文。
|
|
1028
|
+
* @module supermemory/skill-forge
|
|
1029
|
+
*/
|
|
1030
|
+
var skill_forge_exports = /* @__PURE__ */ __exportAll({ discoverSkillRoots: () => discoverSkillRoots });
|
|
1031
|
+
/** Kebab-case contract enforced by the host skill registry. */
|
|
1032
|
+
const SKILL_NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
1033
|
+
/** Changelog bound (newest first), same spirit as capabilities. */
|
|
1034
|
+
const SKILL_CHANGELOG_MAX = 20;
|
|
1035
|
+
/** Reserved frontmatter keys (host ignores unknown metadata keys). */
|
|
1036
|
+
const META = {
|
|
1037
|
+
version: "sm-version",
|
|
1038
|
+
updatedAt: "sm-updated-at",
|
|
1039
|
+
source: "sm-source",
|
|
1040
|
+
occurrences: "sm-occurrences",
|
|
1041
|
+
taskKey: "sm-task-key",
|
|
1042
|
+
renderedHash: "sm-rendered-hash",
|
|
1043
|
+
changelog: "sm-changelog",
|
|
1044
|
+
enabled: "sm-enabled",
|
|
1045
|
+
bodyHash: "sm-body-hash",
|
|
1046
|
+
factsHash: "sm-facts-hash"
|
|
1047
|
+
};
|
|
1048
|
+
/** Short deterministic hash (FNV-1a, base36) for stable names. */
|
|
1049
|
+
function shortHash(text, length = 6) {
|
|
1050
|
+
let hash = 2166136261;
|
|
1051
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
1052
|
+
hash ^= text.charCodeAt(index);
|
|
1053
|
+
hash = Math.imul(hash, 16777619) >>> 0;
|
|
1054
|
+
}
|
|
1055
|
+
return hash.toString(36).padStart(Math.max(1, Math.min(8, length)), "0").slice(0, length);
|
|
1056
|
+
}
|
|
1057
|
+
/** Lower-case ASCII slug; empty when the source has no ASCII letters/digits. */
|
|
1058
|
+
function kebabCase(text, max = 40) {
|
|
1059
|
+
const slug = String(text ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
1060
|
+
if (slug.length <= max) return slug;
|
|
1061
|
+
const cut = slug.slice(0, max);
|
|
1062
|
+
const boundary = cut.lastIndexOf("-");
|
|
1063
|
+
return (boundary > 0 ? cut.slice(0, boundary) : cut).replace(/-+$/g, "");
|
|
1064
|
+
}
|
|
1065
|
+
/** Clamp a name to the host's 64-ish practical bound without splitting a word. */
|
|
1066
|
+
function clampName(name, max = 64) {
|
|
1067
|
+
if (name.length <= max) return name;
|
|
1068
|
+
const cut = name.slice(0, max);
|
|
1069
|
+
const boundary = cut.lastIndexOf("-");
|
|
1070
|
+
return (boundary > 0 ? cut.slice(0, boundary) : cut).replace(/-+$/g, "");
|
|
1071
|
+
}
|
|
1072
|
+
/** True when the name satisfies the host's kebab-case contract. */
|
|
1073
|
+
function isSkillName(name) {
|
|
1074
|
+
return SKILL_NAME_RE.test(name) && name.startsWith("sm-");
|
|
1075
|
+
}
|
|
1076
|
+
/** `sm-<verb>-<object>` — stable for one repeated TASK (never a tool flow). */
|
|
1077
|
+
function skillNameForTask(signature, sharedToken) {
|
|
1078
|
+
return clampName(kebabCase("sm-" + (sharedToken ? sharedToken + "-" : "") + signature.nameBase, 64), 64);
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Readability gate: a task skill name must talk about an OBJECT, not a tool
|
|
1082
|
+
* flow. Legacy shells (`sm-flow-bash-bash-…`) and names built only from tool
|
|
1083
|
+
* words are rejected outright.
|
|
1084
|
+
*/
|
|
1085
|
+
function isReadableTaskSkillName(name) {
|
|
1086
|
+
if (!isSkillName(name)) return false;
|
|
1087
|
+
if (looksLikeToolFlowName(name)) return false;
|
|
1088
|
+
const segments = name.replace(/^sm-/, "").replace(/^ws[a-z0-9]{5}-/, "").split("-").filter(Boolean);
|
|
1089
|
+
return segments.length >= 2 && segments.some((segment) => segment.length >= 3);
|
|
1090
|
+
}
|
|
1091
|
+
/** Name token that keeps the shared user-level skill dir workspace-isolated. */
|
|
1092
|
+
function workspaceSkillToken(workspaceKey) {
|
|
1093
|
+
return "ws" + shortHash(workspaceKey, 5);
|
|
1094
|
+
}
|
|
1095
|
+
/** Parse durable task samples (broken/legacy entries skipped). */
|
|
1096
|
+
function parseTaskSamples(raw) {
|
|
1097
|
+
const out = [];
|
|
1098
|
+
for (const entry of raw) try {
|
|
1099
|
+
const parsed = JSON.parse(entry);
|
|
1100
|
+
if (parsed && Array.isArray(parsed.tools) && Array.isArray(parsed.args)) out.push(parsed);
|
|
1101
|
+
} catch {}
|
|
1102
|
+
return out;
|
|
1103
|
+
}
|
|
1104
|
+
/** Extract human-relevant facts from recorded samples (no secrets copied). */
|
|
1105
|
+
function extractWorkflowFacts(samples) {
|
|
1106
|
+
const facts = workflowFacts(samples);
|
|
1107
|
+
return {
|
|
1108
|
+
commands: facts.commands,
|
|
1109
|
+
files: facts.files,
|
|
1110
|
+
patterns: facts.patterns
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
function countMapPush(map, value, max = 160) {
|
|
1114
|
+
if (typeof value !== "string") return;
|
|
1115
|
+
const text = value.replace(/\s+/g, " ").trim();
|
|
1116
|
+
if (!text || text.length > max) return;
|
|
1117
|
+
map.set(text, (map.get(text) ?? 0) + 1);
|
|
1118
|
+
}
|
|
1119
|
+
function topEntries(map, limit) {
|
|
1120
|
+
return [...map.entries()].slice(0, limit).map(([value, count]) => ({
|
|
1121
|
+
value,
|
|
1122
|
+
count
|
|
1123
|
+
}));
|
|
1124
|
+
}
|
|
1125
|
+
/** One sample's fact extraction (exported for smoke assertions). */
|
|
1126
|
+
function workflowFacts(samples) {
|
|
1127
|
+
const commands = /* @__PURE__ */ new Map();
|
|
1128
|
+
const files = /* @__PURE__ */ new Map();
|
|
1129
|
+
const patterns = /* @__PURE__ */ new Map();
|
|
1130
|
+
const taskTexts = /* @__PURE__ */ new Set();
|
|
1131
|
+
for (const sample of samples) {
|
|
1132
|
+
if (typeof sample.task === "string" && sample.task.trim()) {
|
|
1133
|
+
const task = sample.task.replace(/\s+/g, " ").trim();
|
|
1134
|
+
if (task) taskTexts.add(task.slice(0, 200));
|
|
1135
|
+
}
|
|
1136
|
+
sample.tools.forEach((tool, index) => {
|
|
1137
|
+
const args = sample.args[index] ?? {};
|
|
1138
|
+
if (tool === "bash" || tool === "terminal" || tool === "shell") countMapPush(commands, args.command);
|
|
1139
|
+
if (tool === "grep" || tool === "glob") countMapPush(patterns, args.pattern, 120);
|
|
1140
|
+
if (tool === "read" || tool === "edit" || tool === "write") {
|
|
1141
|
+
const path = args.file_path ?? args.path;
|
|
1142
|
+
countMapPush(files, path, 200);
|
|
1143
|
+
}
|
|
1144
|
+
});
|
|
1145
|
+
}
|
|
1146
|
+
for (const file of extractFileFacts(samples).files) if (!files.has(file)) files.set(file, 1);
|
|
1147
|
+
const commandFamilies = extractCommandFamilies(samples).commands;
|
|
1148
|
+
const commandEntries = topEntries(commands, 8);
|
|
1149
|
+
const fileEntries = topEntries(files, 10);
|
|
1150
|
+
return {
|
|
1151
|
+
commands: commandEntries.map((entry) => entry.value),
|
|
1152
|
+
files: fileEntries.map((entry) => entry.value),
|
|
1153
|
+
patterns: topEntries(patterns, 6).map((entry) => entry.value),
|
|
1154
|
+
taskTexts: [...taskTexts].slice(-4),
|
|
1155
|
+
commandCounts: commandEntries.map((entry) => ({
|
|
1156
|
+
command: entry.value,
|
|
1157
|
+
count: entry.count
|
|
1158
|
+
})),
|
|
1159
|
+
fileCounts: fileEntries.map((entry) => ({
|
|
1160
|
+
file: entry.value,
|
|
1161
|
+
count: entry.count
|
|
1162
|
+
})),
|
|
1163
|
+
commandFamilies
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* Routing description: short, specific, "when to load", never a tool order.
|
|
1168
|
+
* Statistics deliberately stay OUT of the description/body so repeated
|
|
1169
|
+
* detection of the same facts stays byte-identical (frontmatter occurrences
|
|
1170
|
+
* carries the counters instead).
|
|
1171
|
+
*/
|
|
1172
|
+
function taskSkillDescription(signature, facts) {
|
|
1173
|
+
const scope = facts.files.length > 0 ? "涉及 " + facts.files.slice(0, 2).join("、") : signature.commands.length > 0 ? "常用命令 " + signature.commands.slice(0, 2).join("、") : "任务对象 " + signature.objects.join("、");
|
|
1174
|
+
return clipDescription("当任务属于「" + signature.title + "」这一类(" + scope + ")时加载:真实步骤、关键坑与验证方式(任务级经验,不是工具序列)。");
|
|
1175
|
+
}
|
|
1176
|
+
/** Routing hint for the catalog (frontmatter whenToUse). */
|
|
1177
|
+
function taskSkillWhenToUse(signature) {
|
|
1178
|
+
return "任务文本与对象和「" + signature.title + "」相近时加载;只是工具顺序相同、任务对象不同,不要套用。";
|
|
1179
|
+
}
|
|
1180
|
+
function confidenceWord(confidence) {
|
|
1181
|
+
if (confidence >= .8) return "高";
|
|
1182
|
+
if (confidence >= .6) return "中";
|
|
1183
|
+
return "低";
|
|
1184
|
+
}
|
|
1185
|
+
function code(text) {
|
|
1186
|
+
return "`" + text.replace(/`/g, "\\`") + "`";
|
|
1187
|
+
}
|
|
1188
|
+
/**
|
|
1189
|
+
* Markdown body for one repeated TASK. The six required sections are built
|
|
1190
|
+
* from recorded commands/files/requests; anything the samples do not justify
|
|
1191
|
+
* says 「待补充」 instead of inventing a trap or a step.
|
|
1192
|
+
*/
|
|
1193
|
+
function renderTaskSkillBody(draft, signature, samples, meta) {
|
|
1194
|
+
const facts = workflowFacts(samples);
|
|
1195
|
+
const tools = new Set(samples.flatMap((sample) => sample.tools));
|
|
1196
|
+
const verification = facts.commands.filter((command) => extractCommandFamilies([{
|
|
1197
|
+
tools: ["bash"],
|
|
1198
|
+
args: [{ command }]
|
|
1199
|
+
}]).commands.some((family) => /typecheck|build|smoke|test|lint|check/i.test(family)));
|
|
1200
|
+
const lines = [];
|
|
1201
|
+
lines.push("# " + draft.title);
|
|
1202
|
+
lines.push("");
|
|
1203
|
+
lines.push("> 由 SuperMemory 从重复任务自动沉淀(任务级经验,不是工具序列)。可以手工编辑:再次识别到同一任务时只更新统计,不会覆盖人工修改。");
|
|
1204
|
+
lines.push("> 置信度:" + confidenceWord(signature.confidence) + "(样本信息" + (signature.confidence >= .8 ? "充分" : "有限,缺的细节标为「待补充」") + ")。");
|
|
1205
|
+
lines.push("");
|
|
1206
|
+
lines.push("## 何时用(触发条件)");
|
|
1207
|
+
lines.push("");
|
|
1208
|
+
lines.push("- 任务属于「" + draft.title + "」这一类" + (facts.files.length > 0 ? ",通常涉及 " + facts.files.slice(0, 3).map(code).join("、") : "") + "。");
|
|
1209
|
+
if (facts.taskTexts.length > 0) {
|
|
1210
|
+
lines.push("- 历史请求样本(原样截取,用于判断是否同类):");
|
|
1211
|
+
for (const task of facts.taskTexts) lines.push(" - 「" + task.slice(0, 90) + (task.length > 90 ? "…" : "") + "」");
|
|
1212
|
+
} else lines.push("- 待补充:样本没有记录任务文本,无法判断触发条件。");
|
|
1213
|
+
if (facts.commandFamilies.length > 0) lines.push("- 历史命令家族:" + facts.commandFamilies.slice(0, 5).map(code).join("、") + "。");
|
|
1214
|
+
lines.push("- 只是工具顺序相同、任务对象不同(不同仓库/目录/文件族)时不要套用。");
|
|
1215
|
+
lines.push("");
|
|
1216
|
+
lines.push("## 决策清单");
|
|
1217
|
+
lines.push("");
|
|
1218
|
+
let step = 1;
|
|
1219
|
+
if (facts.files.length > 0) {
|
|
1220
|
+
lines.push(step + ". 先核对当前实现/现状:读 " + facts.files.slice(0, 3).map(code).join("、") + ",确认这次改动是否落在同一对象上。");
|
|
1221
|
+
step += 1;
|
|
1222
|
+
} else {
|
|
1223
|
+
lines.push(step + ". 待补充:样本没有记录具体文件;先定位这次任务的真实对象,再决定是否复用。");
|
|
1224
|
+
step += 1;
|
|
1225
|
+
}
|
|
1226
|
+
if (facts.commands.length > 0) {
|
|
1227
|
+
lines.push(step + ". 需要执行命令时优先复用历史命令形态:" + facts.commands.slice(0, 3).map(code).join("、") + ";先确认它们对当前目录/分支仍然成立。");
|
|
1228
|
+
step += 1;
|
|
1229
|
+
} else {
|
|
1230
|
+
lines.push(step + ". 待补充:样本没有记录可复用命令。");
|
|
1231
|
+
step += 1;
|
|
1232
|
+
}
|
|
1233
|
+
lines.push(step + ". 中途出现失败或与样本不同的分支时,按「关键坑」停下判断,不要机械跑完整个步骤。");
|
|
1234
|
+
step += 1;
|
|
1235
|
+
lines.push(step + ". 完成后按「验证方式」核对,并把新发现的坑补进本文件(同名更新会升版并保留 changelog)。");
|
|
1236
|
+
lines.push("");
|
|
1237
|
+
lines.push("## 步骤纲要");
|
|
1238
|
+
lines.push("");
|
|
1239
|
+
let outline = 1;
|
|
1240
|
+
if (facts.files.length > 0) {
|
|
1241
|
+
lines.push(outline + ". 读取/定位:" + facts.files.slice(0, 3).map((file) => code(file)).join("、") + "。");
|
|
1242
|
+
outline += 1;
|
|
1243
|
+
}
|
|
1244
|
+
if (facts.commandCounts.length > 0) {
|
|
1245
|
+
lines.push(outline + ". 执行/修改:按历史命令形态推进,样本中的命令:");
|
|
1246
|
+
for (const entry of facts.commandCounts.slice(0, 6)) lines.push(" - " + code(entry.command));
|
|
1247
|
+
outline += 1;
|
|
1248
|
+
}
|
|
1249
|
+
if (verification.length > 0) {
|
|
1250
|
+
lines.push(outline + ". 验证:" + [...new Set(verification)].slice(0, 4).map(code).join("、") + "。");
|
|
1251
|
+
outline += 1;
|
|
1252
|
+
}
|
|
1253
|
+
if (outline === 1) lines.push("1. 待补充:样本没有记录文件、命令或验证步骤,无法给出可复用纲要。");
|
|
1254
|
+
lines.push("");
|
|
1255
|
+
lines.push("## 关键坑");
|
|
1256
|
+
lines.push("");
|
|
1257
|
+
const repeated = facts.commandCounts.filter((entry) => entry.count >= 2);
|
|
1258
|
+
for (const entry of repeated.slice(0, 3)) lines.push("- " + code(entry.command) + " 在样本里多次出现:重跑前先确认上一次是否失败/是否幂等,不要为了「重试」重复有副作用的命令。");
|
|
1259
|
+
if (tools.has("edit") || tools.has("write")) lines.push("- 样本里有精确编辑:先读目标文件,再只改必要片段,避免整文件覆盖并行改动。");
|
|
1260
|
+
if (tools.has("grep") || tools.has("glob")) lines.push("- 检索空结果不等于不存在:换关键词,或直接读目标文件核对。");
|
|
1261
|
+
if (repeated.length === 0) lines.push("- 待补充:样本没有记录失败与回退细节,不要臆造具体报错或解决方式。");
|
|
1262
|
+
lines.push("- 把 skill 当成免确认的写操作快捷方式:写操作仍需按原流程确认与验证。");
|
|
1263
|
+
lines.push("");
|
|
1264
|
+
lines.push("## 验证方式");
|
|
1265
|
+
lines.push("");
|
|
1266
|
+
if (verification.length > 0) lines.push("- 历史样本实际跑过的验证命令:" + [...new Set(verification)].slice(0, 5).map(code).join("、") + "。");
|
|
1267
|
+
else lines.push("- 待补充:历史样本没有记录验证命令;按仓库约定跑 typecheck / 相关 smoke 并记录结果。");
|
|
1268
|
+
lines.push("- 用可观察结果核对(命令 exit code、文件内容、测试输出),不要凭印象宣称完成。");
|
|
1269
|
+
lines.push("");
|
|
1270
|
+
lines.push("## 反例(不要这样用)");
|
|
1271
|
+
lines.push("");
|
|
1272
|
+
lines.push("- 只是工具序列相同(例如同样是 read → edit → bash)但任务对象不同:这不是同一件事,不要加载。");
|
|
1273
|
+
lines.push("- 一次性任务、或目标文件/目录族明显不同:按新情况判断,不要套历史步骤。");
|
|
1274
|
+
lines.push("- 把 skill 当捷径跳过验证或用户确认:步骤可以复用,闸门不能跳过。");
|
|
1275
|
+
lines.push("");
|
|
1276
|
+
lines.push("## 调用与命中统计");
|
|
1277
|
+
lines.push("");
|
|
1278
|
+
lines.push("- 任务出现次数:由插件写入 frontmatter 的 sm-occurrences(面板可见)。");
|
|
1279
|
+
lines.push("- 模型加载次数:由宿主 skill 工具加载时累加。");
|
|
1280
|
+
lines.push("- 最近识别时间:见 frontmatter 的 sm-updated-at(插件更新,正文不随时间变化)。");
|
|
1281
|
+
return lines.join("\n");
|
|
1282
|
+
}
|
|
1283
|
+
/** Full SKILL.md text: YAML frontmatter + body. */
|
|
1284
|
+
function renderSkillMarkdown(draft, meta) {
|
|
1285
|
+
const q = (value) => JSON.stringify(String(value ?? ""));
|
|
1286
|
+
return [
|
|
1287
|
+
"---",
|
|
1288
|
+
"name: " + draft.name,
|
|
1289
|
+
"description: " + q(clipDescription(draft.description)),
|
|
1290
|
+
...draft.whenToUse ? ["whenToUse: " + q(clipDescription(draft.whenToUse))] : [],
|
|
1291
|
+
"disable-model-invocation: " + String(!meta.enabled),
|
|
1292
|
+
META.enabled + ": " + String(meta.enabled),
|
|
1293
|
+
META.version + ": " + String(meta.version),
|
|
1294
|
+
META.updatedAt + ": " + String(meta.updatedAt),
|
|
1295
|
+
META.source + ": " + q(draft.source.kind),
|
|
1296
|
+
META.occurrences + ": " + String(Math.max(0, draft.source.occurrences)),
|
|
1297
|
+
...draft.taskKey ? [META.taskKey + ": " + q(draft.taskKey)] : [],
|
|
1298
|
+
META.renderedHash + ": " + q(meta.renderedHash),
|
|
1299
|
+
...meta.bodyHash ? [META.bodyHash + ": " + q(meta.bodyHash)] : [],
|
|
1300
|
+
...meta.factsHash ? [META.factsHash + ": " + q(meta.factsHash)] : [],
|
|
1301
|
+
META.changelog + ": " + JSON.stringify(meta.changelog.slice(0, SKILL_CHANGELOG_MAX)),
|
|
1302
|
+
"---",
|
|
1303
|
+
"",
|
|
1304
|
+
draft.body.trim(),
|
|
1305
|
+
""
|
|
1306
|
+
].join("\n");
|
|
1307
|
+
}
|
|
1308
|
+
/** Description clamp shared by the catalog and the panel. */
|
|
1309
|
+
function clipDescription(text, max = 500) {
|
|
1310
|
+
const flat = String(text ?? "").replace(/\s+/g, " ").trim();
|
|
1311
|
+
if (flat.length <= max) return flat;
|
|
1312
|
+
return flat.slice(0, Math.max(1, max - 1)).replace(/[\s,,;;]\S*$/, "") + "…";
|
|
1313
|
+
}
|
|
1314
|
+
/** Parse one SKILL.md (missing/invalid metadata never throws). */
|
|
1315
|
+
function parseSkillMarkdown(text, fallbackName = "") {
|
|
1316
|
+
const raw = String(text ?? "");
|
|
1317
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(raw);
|
|
1318
|
+
if (!match) return void 0;
|
|
1319
|
+
const fields = /* @__PURE__ */ new Map();
|
|
1320
|
+
for (const line of match[1].split(/\r?\n/)) {
|
|
1321
|
+
const colon = line.indexOf(":");
|
|
1322
|
+
if (colon <= 0) continue;
|
|
1323
|
+
fields.set(line.slice(0, colon).trim(), line.slice(colon + 1).trim());
|
|
1324
|
+
}
|
|
1325
|
+
const str = (key) => {
|
|
1326
|
+
const value = fields.get(key);
|
|
1327
|
+
if (value === void 0) return void 0;
|
|
1328
|
+
if (value.startsWith("\"")) try {
|
|
1329
|
+
return String(JSON.parse(value));
|
|
1330
|
+
} catch {
|
|
1331
|
+
return value.replace(/^"|"$/g, "");
|
|
1332
|
+
}
|
|
1333
|
+
return value;
|
|
1334
|
+
};
|
|
1335
|
+
const num = (key, fallback) => {
|
|
1336
|
+
const value = Number(fields.get(key));
|
|
1337
|
+
return Number.isFinite(value) ? value : fallback;
|
|
1338
|
+
};
|
|
1339
|
+
const name = str("name") ?? fallbackName;
|
|
1340
|
+
const description = str("description");
|
|
1341
|
+
if (!name || !SKILL_NAME_RE.test(name) || description === void 0) return void 0;
|
|
1342
|
+
let changelog = [];
|
|
1343
|
+
const rawChangelog = fields.get(META.changelog);
|
|
1344
|
+
if (rawChangelog) try {
|
|
1345
|
+
const parsed = JSON.parse(rawChangelog);
|
|
1346
|
+
if (Array.isArray(parsed)) changelog = parsed.map((entry) => String(entry)).slice(0, SKILL_CHANGELOG_MAX);
|
|
1347
|
+
} catch {}
|
|
1348
|
+
return {
|
|
1349
|
+
name,
|
|
1350
|
+
description,
|
|
1351
|
+
...str("whenToUse") ? { whenToUse: str("whenToUse") } : {},
|
|
1352
|
+
enabled: fields.get("disable-model-invocation") !== "true" && fields.get(META.enabled) !== "false",
|
|
1353
|
+
version: num(META.version, 1),
|
|
1354
|
+
updatedAt: num(META.updatedAt, 0),
|
|
1355
|
+
...str(META.source) ? { source: str(META.source) } : {},
|
|
1356
|
+
occurrences: num(META.occurrences, 0),
|
|
1357
|
+
...str(META.taskKey) ? { taskKey: str(META.taskKey) } : {},
|
|
1358
|
+
...str(META.renderedHash) ? { renderedHash: str(META.renderedHash) } : {},
|
|
1359
|
+
...str(META.bodyHash) ? { bodyHash: str(META.bodyHash) } : {},
|
|
1360
|
+
...str(META.factsHash) ? { factsHash: str(META.factsHash) } : {},
|
|
1361
|
+
changelog,
|
|
1362
|
+
body: raw.slice(match[0].length).trim()
|
|
1363
|
+
};
|
|
1364
|
+
}
|
|
1365
|
+
/** Content hash used to detect human edits (body only; metadata excluded). */
|
|
1366
|
+
function skillContentHash(description, body) {
|
|
1367
|
+
return shortHash(description.trim() + "\0" + body.trim(), 8);
|
|
1368
|
+
}
|
|
1369
|
+
/** Nearest ancestor containing `.git` (the host's project-root rule). */
|
|
1370
|
+
function findProjectRoot(cwd) {
|
|
1371
|
+
let dir = resolve(cwd);
|
|
1372
|
+
for (let depth = 0; depth < 24; depth += 1) {
|
|
1373
|
+
try {
|
|
1374
|
+
if (existsSync(join(dir, ".git"))) return dir;
|
|
1375
|
+
} catch {}
|
|
1376
|
+
const parent = dirname(dir);
|
|
1377
|
+
if (parent === dir) break;
|
|
1378
|
+
dir = parent;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* Resolve the skill root for one workspace.
|
|
1383
|
+
*
|
|
1384
|
+
* Priority mirrors the host provider: `<projectRoot>/.dsh/skills` (rank 100) is
|
|
1385
|
+
* workspace-scoped, so a git workspace needs no name prefix. Without a git
|
|
1386
|
+
* root the user-level `<dshHome>/skills` is shared by every workspace, so
|
|
1387
|
+
* generated names carry a workspace token (isolation by namespace).
|
|
1388
|
+
*/
|
|
1389
|
+
function resolveSkillRoot(cwd, dshHome) {
|
|
1390
|
+
const home = dshHome ?? process.env.DSH_HOME ?? join(homedir(), ".dsh");
|
|
1391
|
+
const projectRoot = cwd ? findProjectRoot(cwd) : void 0;
|
|
1392
|
+
if (projectRoot) return {
|
|
1393
|
+
root: join(projectRoot, ".dsh", "skills"),
|
|
1394
|
+
scope: "project",
|
|
1395
|
+
shared: false,
|
|
1396
|
+
note: "项目级 skill 目录(<仓库根>/.dsh/skills,宿主本地发现的最高优先级,按工作区隔离)"
|
|
1397
|
+
};
|
|
1398
|
+
return {
|
|
1399
|
+
root: join(home, "skills"),
|
|
1400
|
+
scope: "user",
|
|
1401
|
+
shared: true,
|
|
1402
|
+
note: "用户级 skill 目录(<DSH_HOME>/skills;无 git 工作区共用,靠 sm-ws<token>- 前缀隔离)"
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1405
|
+
/**
|
|
1406
|
+
* R52: capability KNOWLEDGE lives outside every skill provider root.
|
|
1407
|
+
*
|
|
1408
|
+
* Workspace: `<repo>/.dsh/capabilities/<name>.md` (never SKILL.md).
|
|
1409
|
+
* No git root: `<DSH_HOME>/capabilities/<workspace-token>/<name>.md`, so the
|
|
1410
|
+
* shared user root stays workspace-isolated without a skill-looking directory.
|
|
1411
|
+
*/
|
|
1412
|
+
function resolveCapabilityRoot(workspaceKey, dshHome) {
|
|
1413
|
+
const key = String(workspaceKey ?? "").trim();
|
|
1414
|
+
const home = dshHome ?? process.env.DSH_HOME ?? join(homedir(), ".dsh");
|
|
1415
|
+
const projectRoot = key ? findProjectRoot(key) : void 0;
|
|
1416
|
+
if (projectRoot) return {
|
|
1417
|
+
root: join(projectRoot, ".dsh", "capabilities"),
|
|
1418
|
+
scope: "project",
|
|
1419
|
+
shared: false,
|
|
1420
|
+
note: "工作区能力知识面(<仓库根>/.dsh/capabilities,不在任何 skill provider 扫描根内)"
|
|
1421
|
+
};
|
|
1422
|
+
const token = workspaceSkillToken(key || "unknown");
|
|
1423
|
+
return {
|
|
1424
|
+
root: join(home, "capabilities", token),
|
|
1425
|
+
scope: "user",
|
|
1426
|
+
shared: false,
|
|
1427
|
+
token,
|
|
1428
|
+
note: "用户级能力知识面(<DSH_HOME>/capabilities/<workspace-token>;无 git 工作区按工作区隔离)"
|
|
1429
|
+
};
|
|
1430
|
+
}
|
|
1431
|
+
/** File name of one capability's knowledge face (never SKILL.md). */
|
|
1432
|
+
function capabilityDocFileName(name) {
|
|
1433
|
+
return (kebabCase(String(name ?? "").replace(/^sm_cap_|^sm_composite_|^sm-/, ""), 60) || "capability") + ".md";
|
|
1434
|
+
}
|
|
1435
|
+
/** Absolute path of one capability knowledge file. */
|
|
1436
|
+
function capabilityDocPath(root, name) {
|
|
1437
|
+
return join(root, capabilityDocFileName(name));
|
|
1438
|
+
}
|
|
1439
|
+
/** Find and parse one capability knowledge file (missing/invalid = undefined). */
|
|
1440
|
+
function findCapabilityDoc(root, name) {
|
|
1441
|
+
const file = capabilityDocPath(root, name);
|
|
1442
|
+
if (!existsSync(file)) return void 0;
|
|
1443
|
+
try {
|
|
1444
|
+
const parsed = parseSkillMarkdown(readFileSync(file, "utf8"), "sm-" + capabilityDocFileName(name).slice(0, -3));
|
|
1445
|
+
if (!parsed) return void 0;
|
|
1446
|
+
return {
|
|
1447
|
+
path: file,
|
|
1448
|
+
parsed
|
|
1449
|
+
};
|
|
1450
|
+
} catch {
|
|
1451
|
+
return;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* R54: sm-version of ONE knowledge-face file (0 when missing/unparsable).
|
|
1456
|
+
*
|
|
1457
|
+
* The version ledger is monotonic: a migration may only ever raise it. Callers
|
|
1458
|
+
* take max(record.version, current file, archived files) — see
|
|
1459
|
+
* `archivedCapabilityDocFiles` and `adoptCapabilityDocVersion`.
|
|
1460
|
+
*/
|
|
1461
|
+
function readSkillVersionAt(file) {
|
|
1462
|
+
const path = String(file ?? "").trim();
|
|
1463
|
+
if (!path || !existsSync(path)) return 0;
|
|
1464
|
+
try {
|
|
1465
|
+
const parsed = parseSkillMarkdown(readFileSync(path, "utf8"), "");
|
|
1466
|
+
return parsed && Number.isFinite(parsed.version) ? Math.max(0, Math.floor(parsed.version)) : 0;
|
|
1467
|
+
} catch {
|
|
1468
|
+
return 0;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
/**
|
|
1472
|
+
* R54: archived knowledge-face files that can restore one capability's version.
|
|
1473
|
+
*
|
|
1474
|
+
* The R52 move copied <skills root>/sm-<name>/SKILL.md to
|
|
1475
|
+
* <capabilities root>/<name>.md and archived the directory FIRST in some boot
|
|
1476
|
+
* orders. When the live file was then regenerated from the (older) record
|
|
1477
|
+
* version, the file version regressed 8 → 6 while the archive still carried 8.
|
|
1478
|
+
* Discovery looks in both archive locations so `reconcileCapabilityVersions`
|
|
1479
|
+
* can restore the highest version instead of trusting the live file alone.
|
|
1480
|
+
*/
|
|
1481
|
+
function archivedCapabilityDocFiles(root, name, skillsRoot) {
|
|
1482
|
+
const out = [];
|
|
1483
|
+
const slug = capabilityDocFileName(name).replace(/\.md$/, "");
|
|
1484
|
+
const docArchive = archiveDirForRoot(root);
|
|
1485
|
+
try {
|
|
1486
|
+
if (existsSync(docArchive)) for (const entry of readdirSync(docArchive)) {
|
|
1487
|
+
if (!entry.endsWith(".md")) continue;
|
|
1488
|
+
const base = entry.slice(0, -3);
|
|
1489
|
+
if (base === slug || base.startsWith(slug + "-cap-")) out.push(join(docArchive, entry));
|
|
1490
|
+
}
|
|
1491
|
+
} catch {}
|
|
1492
|
+
if (skillsRoot) {
|
|
1493
|
+
const skillsArchive = archiveDirForRoot(skillsRoot);
|
|
1494
|
+
const skillName = capabilitySkillNames(name).skillName;
|
|
1495
|
+
try {
|
|
1496
|
+
if (existsSync(skillsArchive)) for (const entry of readdirSync(skillsArchive)) {
|
|
1497
|
+
if (entry !== skillName && !entry.startsWith(skillName + "-cap-") && !entry.startsWith(skillName + "-dup-")) continue;
|
|
1498
|
+
const file = join(skillsArchive, entry, "SKILL.md");
|
|
1499
|
+
if (existsSync(file)) out.push(file);
|
|
1500
|
+
}
|
|
1501
|
+
} catch {}
|
|
1502
|
+
}
|
|
1503
|
+
return out;
|
|
1504
|
+
}
|
|
1505
|
+
/**
|
|
1506
|
+
* R54: rewrite one capability knowledge face at a MONOTONIC version.
|
|
1507
|
+
*
|
|
1508
|
+
* Only metadata changes: the prose is byte-identical, the facts block is kept,
|
|
1509
|
+
* and the migration is recorded in the file changelog. Idempotent — repeating
|
|
1510
|
+
* the call with the same version+line reports 'unchanged' and writes nothing,
|
|
1511
|
+
* which is what makes the boot migration safe to run on every start.
|
|
1512
|
+
*/
|
|
1513
|
+
function adoptCapabilityDocVersion(options) {
|
|
1514
|
+
const path = String(options.path ?? "").trim();
|
|
1515
|
+
const version = Math.max(1, Math.floor(Number(options.version) || 1));
|
|
1516
|
+
const line = String(options.changelogLine ?? "").trim().slice(0, 200);
|
|
1517
|
+
if (!path || !existsSync(path)) return {
|
|
1518
|
+
status: "error",
|
|
1519
|
+
version,
|
|
1520
|
+
path,
|
|
1521
|
+
message: "知识面文件不存在:" + (path || "(未生成)")
|
|
1522
|
+
};
|
|
1523
|
+
let parsed;
|
|
1524
|
+
try {
|
|
1525
|
+
parsed = parseSkillMarkdown(readFileSync(path, "utf8"), capabilitySkillNames(options.name).skillName);
|
|
1526
|
+
} catch {
|
|
1527
|
+
parsed = void 0;
|
|
1528
|
+
}
|
|
1529
|
+
if (!parsed) return {
|
|
1530
|
+
status: "error",
|
|
1531
|
+
version,
|
|
1532
|
+
path,
|
|
1533
|
+
message: "知识面文件解析失败:" + path
|
|
1534
|
+
};
|
|
1535
|
+
const history = [...new Set([
|
|
1536
|
+
line,
|
|
1537
|
+
...options.extraChangelog ?? [],
|
|
1538
|
+
...parsed.changelog
|
|
1539
|
+
].filter(Boolean))];
|
|
1540
|
+
if (parsed.version >= version && parsed.changelog[0] === line) return {
|
|
1541
|
+
status: "unchanged",
|
|
1542
|
+
version: parsed.version,
|
|
1543
|
+
path,
|
|
1544
|
+
...parsed.bodyHash ? { bodyHash: parsed.bodyHash } : {},
|
|
1545
|
+
...parsed.factsHash ? { factsHash: parsed.factsHash } : {},
|
|
1546
|
+
message: "版本已对齐,未改写。"
|
|
1547
|
+
};
|
|
1548
|
+
const nextVersion = Math.max(parsed.version, version);
|
|
1549
|
+
const draft = {
|
|
1550
|
+
name: parsed.name,
|
|
1551
|
+
title: options.title || parsed.name,
|
|
1552
|
+
description: parsed.description,
|
|
1553
|
+
...parsed.whenToUse ? { whenToUse: parsed.whenToUse } : {},
|
|
1554
|
+
tags: (options.tags && options.tags.length > 0 ? options.tags : ["skill", "capability"]).slice(0, 8),
|
|
1555
|
+
body: parsed.body,
|
|
1556
|
+
...parsed.taskKey ? { taskKey: parsed.taskKey } : {},
|
|
1557
|
+
source: {
|
|
1558
|
+
kind: "capability",
|
|
1559
|
+
key: "capability/" + options.name,
|
|
1560
|
+
label: options.title,
|
|
1561
|
+
occurrences: Math.max(1, parsed.occurrences)
|
|
1562
|
+
}
|
|
1563
|
+
};
|
|
1564
|
+
try {
|
|
1565
|
+
writeFileAtomic(path, renderSkillMarkdown(draft, {
|
|
1566
|
+
version: nextVersion,
|
|
1567
|
+
updatedAt: options.now ?? Date.now(),
|
|
1568
|
+
renderedHash: skillContentHash(parsed.description, parsed.body),
|
|
1569
|
+
changelog: history,
|
|
1570
|
+
enabled: parsed.enabled,
|
|
1571
|
+
...parsed.bodyHash ? { bodyHash: parsed.bodyHash } : {},
|
|
1572
|
+
...parsed.factsHash ? { factsHash: parsed.factsHash } : {}
|
|
1573
|
+
}));
|
|
1574
|
+
} catch (error) {
|
|
1575
|
+
return {
|
|
1576
|
+
status: "error",
|
|
1577
|
+
version,
|
|
1578
|
+
path,
|
|
1579
|
+
message: "写入知识面失败:" + String(error).slice(0, 160)
|
|
1580
|
+
};
|
|
1581
|
+
}
|
|
1582
|
+
return {
|
|
1583
|
+
status: "adopted",
|
|
1584
|
+
version: nextVersion,
|
|
1585
|
+
path,
|
|
1586
|
+
...parsed.bodyHash ? { bodyHash: parsed.bodyHash } : {},
|
|
1587
|
+
...parsed.factsHash ? { factsHash: parsed.factsHash } : {},
|
|
1588
|
+
message: "知识面版本对齐为 v" + nextVersion + "。"
|
|
1589
|
+
};
|
|
1590
|
+
}
|
|
1591
|
+
async function archiveCapabilityDoc(root, name, options = {}) {
|
|
1592
|
+
const from = capabilityDocPath(root, name);
|
|
1593
|
+
if (!existsSync(from)) return void 0;
|
|
1594
|
+
const archiveDir = archiveDirForRoot(root);
|
|
1595
|
+
mkdirSync(archiveDir, { recursive: true });
|
|
1596
|
+
const now = options.now ?? Date.now();
|
|
1597
|
+
let target = join(archiveDir, capabilityDocFileName(name));
|
|
1598
|
+
if (existsSync(target)) target = join(archiveDir, capabilityDocFileName(name).replace(/\.md$/, "") + "-cap-" + shortHash(from + ":" + now, 4) + ".md");
|
|
1599
|
+
try {
|
|
1600
|
+
renameSync(from, target);
|
|
1601
|
+
} catch {
|
|
1602
|
+
return;
|
|
1603
|
+
}
|
|
1604
|
+
const record = {
|
|
1605
|
+
root,
|
|
1606
|
+
name,
|
|
1607
|
+
from,
|
|
1608
|
+
archivePath: target,
|
|
1609
|
+
archivedAt: now,
|
|
1610
|
+
reason: options.reason ?? "能力删除:知识面文件归档(可回滚,不硬删)"
|
|
1611
|
+
};
|
|
1612
|
+
const manifestFile = join(archiveDir, "manifest.json");
|
|
1613
|
+
const manifest = readArchiveManifest(manifestFile);
|
|
1614
|
+
manifest.entries.push({
|
|
1615
|
+
name,
|
|
1616
|
+
archivePath: target,
|
|
1617
|
+
version: 0,
|
|
1618
|
+
description: "",
|
|
1619
|
+
archivedAt: now,
|
|
1620
|
+
reason: record.reason,
|
|
1621
|
+
from
|
|
1622
|
+
});
|
|
1623
|
+
try {
|
|
1624
|
+
writeFileAtomic(manifestFile, JSON.stringify({
|
|
1625
|
+
version: 1,
|
|
1626
|
+
entries: manifest.entries.slice(-500)
|
|
1627
|
+
}, null, 2));
|
|
1628
|
+
} catch {}
|
|
1629
|
+
return record;
|
|
1630
|
+
}
|
|
1631
|
+
/** Recover the task signature stored in one durable task signal. */
|
|
1632
|
+
function signatureFromSignal(signal, samples) {
|
|
1633
|
+
const withTask = [...samples].reverse().find((sample) => typeof sample.task === "string" && sample.task.trim().length >= 6);
|
|
1634
|
+
if (!withTask) return void 0;
|
|
1635
|
+
const signature = extractTaskSignature({
|
|
1636
|
+
task: withTask.task,
|
|
1637
|
+
tools: samples.flatMap((sample) => sample.tools),
|
|
1638
|
+
args: samples.flatMap((sample) => sample.args)
|
|
1639
|
+
});
|
|
1640
|
+
if (!signature) return void 0;
|
|
1641
|
+
if (signal.pattern.startsWith("task:") && signal.pattern.slice(5) !== signature.key) return void 0;
|
|
1642
|
+
return signature;
|
|
1643
|
+
}
|
|
1644
|
+
/**
|
|
1645
|
+
* Build a draft from one repeated TASK signal. The tool sequence in the signal
|
|
1646
|
+
* is evidence for the body only — the name/description/aggregation all come
|
|
1647
|
+
* from the task signature.
|
|
1648
|
+
*/
|
|
1649
|
+
function draftSkillFromTaskSignal(signal, samples, options = {}) {
|
|
1650
|
+
const signature = options.signature ?? signatureFromSignal(signal, samples);
|
|
1651
|
+
if (!signature) return void 0;
|
|
1652
|
+
const name = skillNameForTask(signature, options.sharedToken);
|
|
1653
|
+
if (!isReadableTaskSkillName(name)) return void 0;
|
|
1654
|
+
const facts = workflowFacts(samples);
|
|
1655
|
+
const occurrences = Math.max(1, signal.count);
|
|
1656
|
+
const sessions = Math.max(1, options.sessions ?? signal.sessions?.length ?? 1);
|
|
1657
|
+
const description = taskSkillDescription(signature, facts);
|
|
1658
|
+
const draft = {
|
|
1659
|
+
name,
|
|
1660
|
+
title: signature.title,
|
|
1661
|
+
description,
|
|
1662
|
+
whenToUse: taskSkillWhenToUse(signature),
|
|
1663
|
+
tags: [
|
|
1664
|
+
"skill",
|
|
1665
|
+
"task",
|
|
1666
|
+
...signature.keywords
|
|
1667
|
+
].slice(0, 8),
|
|
1668
|
+
body: "",
|
|
1669
|
+
taskKey: signature.key,
|
|
1670
|
+
source: {
|
|
1671
|
+
kind: "task",
|
|
1672
|
+
key: "task/" + signature.key,
|
|
1673
|
+
label: signature.title,
|
|
1674
|
+
occurrences,
|
|
1675
|
+
sessions
|
|
1676
|
+
},
|
|
1677
|
+
facts: {
|
|
1678
|
+
commands: facts.commands,
|
|
1679
|
+
files: facts.files,
|
|
1680
|
+
patterns: facts.patterns,
|
|
1681
|
+
taskTexts: facts.taskTexts,
|
|
1682
|
+
commandCounts: facts.commandCounts,
|
|
1683
|
+
fileCounts: facts.fileCounts
|
|
1684
|
+
}
|
|
1685
|
+
};
|
|
1686
|
+
draft.body = renderTaskSkillBody(draft, signature, samples, {
|
|
1687
|
+
occurrences,
|
|
1688
|
+
sessions
|
|
1689
|
+
});
|
|
1690
|
+
return draft;
|
|
1691
|
+
}
|
|
1692
|
+
/** Distinct sessions/dispatches recorded for one task signal. */
|
|
1693
|
+
function signalSessionCount(signal, samples = []) {
|
|
1694
|
+
const recorded = signal.sessions ?? [];
|
|
1695
|
+
if (recorded.length > 0) return recorded.length;
|
|
1696
|
+
const fromSamples = samples.map((sample) => sample.sessionId).filter((value) => Boolean(value));
|
|
1697
|
+
return new Set(fromSamples).size;
|
|
1698
|
+
}
|
|
1699
|
+
/** Read every generated skill bundle under one root. */
|
|
1700
|
+
function readSkillFiles(root) {
|
|
1701
|
+
const out = [];
|
|
1702
|
+
if (!existsSync(root)) return out;
|
|
1703
|
+
let entries;
|
|
1704
|
+
try {
|
|
1705
|
+
entries = readdirSync(root);
|
|
1706
|
+
} catch {
|
|
1707
|
+
return out;
|
|
1708
|
+
}
|
|
1709
|
+
for (const entry of entries) {
|
|
1710
|
+
if (!entry.startsWith("sm-")) continue;
|
|
1711
|
+
const dir = join(root, entry);
|
|
1712
|
+
try {
|
|
1713
|
+
if (!statSync(dir).isDirectory()) continue;
|
|
1714
|
+
} catch {
|
|
1715
|
+
continue;
|
|
1716
|
+
}
|
|
1717
|
+
const file = join(dir, "SKILL.md");
|
|
1718
|
+
if (!existsSync(file)) continue;
|
|
1719
|
+
let text;
|
|
1720
|
+
try {
|
|
1721
|
+
text = readFileSync(file, "utf8");
|
|
1722
|
+
} catch {
|
|
1723
|
+
continue;
|
|
1724
|
+
}
|
|
1725
|
+
const parsed = parseSkillMarkdown(text, entry);
|
|
1726
|
+
if (!parsed) continue;
|
|
1727
|
+
out.push({
|
|
1728
|
+
name: parsed.name,
|
|
1729
|
+
path: file,
|
|
1730
|
+
parsed
|
|
1731
|
+
});
|
|
1732
|
+
}
|
|
1733
|
+
return out;
|
|
1734
|
+
}
|
|
1735
|
+
/** Atomic-ish write: temp file in the same directory, then rename. */
|
|
1736
|
+
function writeFileAtomic(file, text) {
|
|
1737
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
1738
|
+
const tmp = file + ".tmp-" + process.pid + "-" + Date.now().toString(36);
|
|
1739
|
+
writeFileSync(tmp, text, "utf8");
|
|
1740
|
+
renameSync(tmp, file);
|
|
1741
|
+
}
|
|
1742
|
+
function versionLine(version, note) {
|
|
1743
|
+
return "v" + version + ": " + note;
|
|
1744
|
+
}
|
|
1745
|
+
/** Durable signal key for one task signature. */
|
|
1746
|
+
function taskSignalKey(workspaceKey, signatureKey) {
|
|
1747
|
+
return "ws/" + workspaceKey + "/task/" + signatureKey;
|
|
1748
|
+
}
|
|
1749
|
+
/** Only keep arguments the body can actually use; never store result blobs. */
|
|
1750
|
+
function compactArgs(args) {
|
|
1751
|
+
const out = {};
|
|
1752
|
+
for (const key of [
|
|
1753
|
+
"command",
|
|
1754
|
+
"description",
|
|
1755
|
+
"file_path",
|
|
1756
|
+
"path",
|
|
1757
|
+
"pattern",
|
|
1758
|
+
"glob",
|
|
1759
|
+
"cwd",
|
|
1760
|
+
"query",
|
|
1761
|
+
"name"
|
|
1762
|
+
]) {
|
|
1763
|
+
const value = args[key];
|
|
1764
|
+
if (typeof value === "string" && value.length > 0 && value.length <= 400) out[key] = value;
|
|
1765
|
+
}
|
|
1766
|
+
return out;
|
|
1767
|
+
}
|
|
1768
|
+
function uniqueCompact(values, cap) {
|
|
1769
|
+
const out = [];
|
|
1770
|
+
for (const value of values) {
|
|
1771
|
+
if (!value || out.includes(value)) continue;
|
|
1772
|
+
out.push(value);
|
|
1773
|
+
}
|
|
1774
|
+
return out.slice(-cap);
|
|
1775
|
+
}
|
|
1776
|
+
/**
|
|
1777
|
+
* Record ONE task occurrence for this workspace. Unlike the old tool-sequence
|
|
1778
|
+
* counter, the key is `<verb>/<object>` — different tool orders of the same
|
|
1779
|
+
* task land on the same signal and therefore merge into one skill.
|
|
1780
|
+
*/
|
|
1781
|
+
async function recordTaskObservation(store, workspaceKey, observation, options = {}) {
|
|
1782
|
+
if (!workspaceKey || workspaceKey === "unknown") return void 0;
|
|
1783
|
+
const signature = extractTaskSignature(observation);
|
|
1784
|
+
if (!signature) return void 0;
|
|
1785
|
+
const now = options.now ?? Date.now();
|
|
1786
|
+
const key = signature.key;
|
|
1787
|
+
const signalKey = taskSignalKey(workspaceKey, key);
|
|
1788
|
+
const existing = store.signals.get(signalKey);
|
|
1789
|
+
const sample = {
|
|
1790
|
+
tools: observation.tools.slice(0, 60),
|
|
1791
|
+
args: observation.args.slice(0, 60).map(compactArgs),
|
|
1792
|
+
...observation.task ? { task: observation.task.replace(/\s+/g, " ").slice(0, 2e3) } : {},
|
|
1793
|
+
...observation.sessionId ? { sessionId: observation.sessionId } : {},
|
|
1794
|
+
...observation.dispatchId ? { dispatchId: observation.dispatchId } : {},
|
|
1795
|
+
...observation.topicSetId ? { topicSetId: observation.topicSetId } : {},
|
|
1796
|
+
at: now
|
|
1797
|
+
};
|
|
1798
|
+
const samples = [...existing?.samples ?? [], JSON.stringify(sample)].slice(-5);
|
|
1799
|
+
const sessions = uniqueCompact([...existing?.sessions ?? [], observation.sessionId], 20);
|
|
1800
|
+
const dispatches = uniqueCompact([...existing?.dispatches ?? [], observation.dispatchId], 10);
|
|
1801
|
+
const count = (existing?.count ?? 0) + 1;
|
|
1802
|
+
const signal = {
|
|
1803
|
+
workspaceKey,
|
|
1804
|
+
pattern: "task:" + key,
|
|
1805
|
+
count,
|
|
1806
|
+
lastSeen: now,
|
|
1807
|
+
examples: [...existing?.examples ?? [], signature.title].slice(-3),
|
|
1808
|
+
sequence: observation.tools.slice(0, 8),
|
|
1809
|
+
samples,
|
|
1810
|
+
sessions,
|
|
1811
|
+
dispatches
|
|
1812
|
+
};
|
|
1813
|
+
await store.signals.put(signalKey, signal);
|
|
1814
|
+
const threshold = Math.max(1, options.threshold ?? 3);
|
|
1815
|
+
const minSessions = Math.max(1, options.minSessions ?? 2);
|
|
1816
|
+
return {
|
|
1817
|
+
signature,
|
|
1818
|
+
key,
|
|
1819
|
+
count,
|
|
1820
|
+
sessions: sessions.length,
|
|
1821
|
+
crossed: count >= threshold && sessions.length >= minSessions
|
|
1822
|
+
};
|
|
1823
|
+
}
|
|
1824
|
+
/**
|
|
1825
|
+
* R51: names minted from a tool ORDER (R45 legacy): sm-flow-*, sm-tool-*,
|
|
1826
|
+
* sm-ws<token>-flow-*. Accepts the logical form without the sm- prefix too,
|
|
1827
|
+
* because capability records normalise names that way.
|
|
1828
|
+
*/
|
|
1829
|
+
function isToolFlowName(name) {
|
|
1830
|
+
return /^(?:sm-)?(?:ws[a-z0-9]{4,10}-)?(?:flow|tool)(?:-|$)/.test(String(name ?? "").trim().toLowerCase());
|
|
1831
|
+
}
|
|
1832
|
+
/** R51: the R45 shell prose signature (tool order, no business object). */
|
|
1833
|
+
function isToolOrderProse(text) {
|
|
1834
|
+
const value = String(text ?? "");
|
|
1835
|
+
return /当任务需要按/.test(value) && /的顺序反复处理同类改动/.test(value);
|
|
1836
|
+
}
|
|
1837
|
+
/**
|
|
1838
|
+
* R51 shell identity — survives R50's knowledge-face rewrite.
|
|
1839
|
+
*
|
|
1840
|
+
* R50's migration refreshed EVERY migrated row's SKILL.md, overwriting
|
|
1841
|
+
* `sm-source: sequence` with `sm-source: capability`; the old detector (source
|
|
1842
|
+
* only) then treated the shell as a fresh knowledge unit. Detection therefore
|
|
1843
|
+
* also keys on the generated name and the tool-order prose, both of which the
|
|
1844
|
+
* rewrite preserves.
|
|
1845
|
+
*/
|
|
1846
|
+
function isShellIdentity(identity) {
|
|
1847
|
+
if (String(identity.source ?? "") === "sequence") return true;
|
|
1848
|
+
if (isToolFlowName(identity.name)) return true;
|
|
1849
|
+
if (isToolOrderProse(identity.description)) return true;
|
|
1850
|
+
return isToolOrderProse(identity.body);
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Legacy shell detection (R48 cleanup, hardened in R51): an auto-generated
|
|
1854
|
+
* skill whose NAME or DESCRIPTION is just a tool order. Human/manual skills are
|
|
1855
|
+
* only reclassified when the NAME itself is a tool order — a body quote of the
|
|
1856
|
+
* shell template never archives a human file.
|
|
1857
|
+
*/
|
|
1858
|
+
function isShellSkill(parsed) {
|
|
1859
|
+
if (parsed.source === "manual" && !isToolFlowName(parsed.name) && !isToolOrderProse(parsed.description)) return false;
|
|
1860
|
+
return isShellIdentity({
|
|
1861
|
+
name: parsed.name,
|
|
1862
|
+
description: parsed.description,
|
|
1863
|
+
source: parsed.source,
|
|
1864
|
+
body: parsed.body
|
|
1865
|
+
});
|
|
1866
|
+
}
|
|
1867
|
+
/** Archive directory is a SIBLING of the skill root: <root>-archive. */
|
|
1868
|
+
function archiveDirForRoot(root) {
|
|
1869
|
+
return root + "-archive";
|
|
1870
|
+
}
|
|
1871
|
+
function readArchiveManifest(file) {
|
|
1872
|
+
if (!existsSync(file)) return {
|
|
1873
|
+
version: 1,
|
|
1874
|
+
entries: []
|
|
1875
|
+
};
|
|
1876
|
+
try {
|
|
1877
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
1878
|
+
if (parsed && Array.isArray(parsed.entries)) return {
|
|
1879
|
+
version: 1,
|
|
1880
|
+
entries: parsed.entries
|
|
1881
|
+
};
|
|
1882
|
+
} catch {}
|
|
1883
|
+
return {
|
|
1884
|
+
version: 1,
|
|
1885
|
+
entries: []
|
|
1886
|
+
};
|
|
1887
|
+
}
|
|
1888
|
+
/**
|
|
1889
|
+
* Archive auto-generated tool-sequence shells under every given root.
|
|
1890
|
+
*
|
|
1891
|
+
* - moves `<root>/<name>` to `<root>-archive/<name>` (never deletes);
|
|
1892
|
+
* - appends a manifest entry (name/time/reason/version/description/from);
|
|
1893
|
+
* - retires the mirror tool_memory row when a store is supplied;
|
|
1894
|
+
* - IDEMPOTENT: a second run finds no shell skills and archives 0.
|
|
1895
|
+
*/
|
|
1896
|
+
async function archiveShellSkills(options) {
|
|
1897
|
+
const now = options.now ?? Date.now();
|
|
1898
|
+
const dryRun = Boolean(options.dryRun);
|
|
1899
|
+
const archived = [];
|
|
1900
|
+
const rootReports = [];
|
|
1901
|
+
let totalScanned = 0;
|
|
1902
|
+
for (const root of [...new Set(options.roots)]) {
|
|
1903
|
+
const entries = readSkillFiles(root);
|
|
1904
|
+
const report = {
|
|
1905
|
+
root,
|
|
1906
|
+
scanned: entries.length,
|
|
1907
|
+
archived: 0,
|
|
1908
|
+
skippedManual: 0,
|
|
1909
|
+
skippedOther: 0
|
|
1910
|
+
};
|
|
1911
|
+
totalScanned += entries.length;
|
|
1912
|
+
const archiveDir = archiveDirForRoot(root);
|
|
1913
|
+
const toArchive = [];
|
|
1914
|
+
for (const entry of entries) {
|
|
1915
|
+
if (!isShellSkill(entry.parsed)) {
|
|
1916
|
+
if (entry.parsed.source === "manual") report.skippedManual += 1;
|
|
1917
|
+
else report.skippedOther += 1;
|
|
1918
|
+
continue;
|
|
1919
|
+
}
|
|
1920
|
+
toArchive.push({
|
|
1921
|
+
name: entry.name,
|
|
1922
|
+
parsed: entry.parsed,
|
|
1923
|
+
from: dirname(entry.path)
|
|
1924
|
+
});
|
|
1925
|
+
}
|
|
1926
|
+
if (toArchive.length === 0) {
|
|
1927
|
+
rootReports.push(report);
|
|
1928
|
+
continue;
|
|
1929
|
+
}
|
|
1930
|
+
if (!dryRun) mkdirSync(archiveDir, { recursive: true });
|
|
1931
|
+
const manifestFile = join(archiveDir, "manifest.json");
|
|
1932
|
+
const manifest = readArchiveManifest(manifestFile);
|
|
1933
|
+
for (const item of toArchive) {
|
|
1934
|
+
let target = join(archiveDir, item.name);
|
|
1935
|
+
if (existsSync(target)) target = join(archiveDir, item.name + "-dup-" + shortHash(root + ":" + now + ":" + item.name, 4));
|
|
1936
|
+
if (!dryRun) try {
|
|
1937
|
+
renameSync(item.from, target);
|
|
1938
|
+
} catch {
|
|
1939
|
+
report.skippedOther += 1;
|
|
1940
|
+
continue;
|
|
1941
|
+
}
|
|
1942
|
+
const record = {
|
|
1943
|
+
root,
|
|
1944
|
+
name: item.name,
|
|
1945
|
+
from: item.from,
|
|
1946
|
+
archivePath: target,
|
|
1947
|
+
version: item.parsed.version,
|
|
1948
|
+
description: item.parsed.description,
|
|
1949
|
+
archivedAt: now,
|
|
1950
|
+
reason: "自动生成的工具序列壳子(R48 清理:归档保留,可回滚)"
|
|
1951
|
+
};
|
|
1952
|
+
archived.push(record);
|
|
1953
|
+
report.archived += 1;
|
|
1954
|
+
manifest.entries.push({
|
|
1955
|
+
name: record.name,
|
|
1956
|
+
archivePath: record.archivePath,
|
|
1957
|
+
version: record.version,
|
|
1958
|
+
description: record.description,
|
|
1959
|
+
archivedAt: record.archivedAt,
|
|
1960
|
+
reason: record.reason,
|
|
1961
|
+
from: record.from
|
|
1962
|
+
});
|
|
1963
|
+
if (options.store !== void 0) await retireSkillRow(options.store, item.from);
|
|
1964
|
+
}
|
|
1965
|
+
if (!dryRun && manifest.entries.length > 0) try {
|
|
1966
|
+
writeFileAtomic(manifestFile, JSON.stringify({
|
|
1967
|
+
version: 1,
|
|
1968
|
+
entries: manifest.entries.slice(-500)
|
|
1969
|
+
}, null, 2));
|
|
1970
|
+
} catch {}
|
|
1971
|
+
rootReports.push(report);
|
|
1972
|
+
}
|
|
1973
|
+
return {
|
|
1974
|
+
archived,
|
|
1975
|
+
roots: rootReports,
|
|
1976
|
+
totalScanned,
|
|
1977
|
+
totalArchived: archived.length,
|
|
1978
|
+
dryRun
|
|
1979
|
+
};
|
|
1980
|
+
}
|
|
1981
|
+
/** Skill path of a mirror row: dedicated column, legacy path entry, or JSON. */
|
|
1982
|
+
function skillPathOfMirrorRow(row) {
|
|
1983
|
+
if (typeof row.skillPath === "string" && row.skillPath) return row.skillPath;
|
|
1984
|
+
const entry = String(row.entry ?? "");
|
|
1985
|
+
if (!entry.startsWith("{")) return entry;
|
|
1986
|
+
try {
|
|
1987
|
+
const parsed = JSON.parse(entry);
|
|
1988
|
+
const path = parsed.skillPath ?? parsed.entryPath;
|
|
1989
|
+
return typeof path === "string" ? path : "";
|
|
1990
|
+
} catch {
|
|
1991
|
+
return "";
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
/**
|
|
1995
|
+
* Retire the durable mirror row of an archived skill (best effort).
|
|
1996
|
+
*
|
|
1997
|
+
* R51: also covers kind='knowledge' rows (R50 migrated shell rows) and the
|
|
1998
|
+
* schema-JSON / skillPath column shapes — the old skill-only, entry-only check
|
|
1999
|
+
* left adopted shells active and they were re-materialised on the next boot.
|
|
2000
|
+
*/
|
|
2001
|
+
async function retireSkillRow(store, fromDir) {
|
|
2002
|
+
const marker = normalizeDir(fromDir) + "/";
|
|
2003
|
+
const target = normalizeDir(join(fromDir, "SKILL.md"));
|
|
2004
|
+
for (const [key, row] of store.toolMemory.entries()) {
|
|
2005
|
+
if (row.kind !== "skill" && row.kind !== "knowledge") continue;
|
|
2006
|
+
const entry = normalizeDir(skillPathOfMirrorRow(row));
|
|
2007
|
+
if (!entry) continue;
|
|
2008
|
+
if (entry !== target && !entry.startsWith(marker)) continue;
|
|
2009
|
+
try {
|
|
2010
|
+
await store.toolMemory.update(key, (record) => ({
|
|
2011
|
+
...record,
|
|
2012
|
+
status: "retired",
|
|
2013
|
+
enabled: false
|
|
2014
|
+
}));
|
|
2015
|
+
} catch {}
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
function normalizeDir(path) {
|
|
2019
|
+
return resolve(String(path ?? "")).replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase();
|
|
2020
|
+
}
|
|
2021
|
+
/**
|
|
2022
|
+
* Every skill root this machine is likely to have used: <DSH_HOME>/skills plus
|
|
2023
|
+
* `<project>/.dsh/skills` for every workspace key found in the store and the
|
|
2024
|
+
* caller's cwd. Missing roots are ignored.
|
|
2025
|
+
*/
|
|
2026
|
+
function discoverSkillRoots(options = {}) {
|
|
2027
|
+
const dshHome = options.dshHome ?? process.env.DSH_HOME ?? join(homedir(), ".dsh");
|
|
2028
|
+
const workspaces = /* @__PURE__ */ new Set();
|
|
2029
|
+
if (options.cwd) workspaces.add(resolve(options.cwd));
|
|
2030
|
+
for (const workspace of options.workspaces ?? []) if (workspace) workspaces.add(workspace);
|
|
2031
|
+
const store = options.store;
|
|
2032
|
+
if (store) for (const table of [
|
|
2033
|
+
"memories",
|
|
2034
|
+
"signals",
|
|
2035
|
+
"toolMemory",
|
|
2036
|
+
"filemap",
|
|
2037
|
+
"topicsets"
|
|
2038
|
+
]) {
|
|
2039
|
+
const handle = store[table];
|
|
2040
|
+
if (!handle || typeof handle.entries !== "function") continue;
|
|
2041
|
+
try {
|
|
2042
|
+
for (const [, row] of handle.entries()) {
|
|
2043
|
+
const workspaceKey = row?.workspaceKey;
|
|
2044
|
+
if (typeof workspaceKey === "string" && workspaceKey.length > 0) workspaces.add(workspaceKey);
|
|
2045
|
+
}
|
|
2046
|
+
} catch {}
|
|
2047
|
+
}
|
|
2048
|
+
const roots = /* @__PURE__ */ new Set();
|
|
2049
|
+
roots.add(join(dshHome, "skills"));
|
|
2050
|
+
for (const workspace of workspaces) {
|
|
2051
|
+
const projectRoot = findProjectRoot(workspace) ?? (existsSync(join(workspace, ".git")) ? workspace : void 0);
|
|
2052
|
+
if (projectRoot) roots.add(join(projectRoot, ".dsh", "skills"));
|
|
2053
|
+
}
|
|
2054
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2055
|
+
const out = [];
|
|
2056
|
+
for (const root of roots) {
|
|
2057
|
+
let key;
|
|
2058
|
+
try {
|
|
2059
|
+
key = normalizeDir(root);
|
|
2060
|
+
} catch {
|
|
2061
|
+
continue;
|
|
2062
|
+
}
|
|
2063
|
+
if (seen.has(key)) continue;
|
|
2064
|
+
seen.add(key);
|
|
2065
|
+
try {
|
|
2066
|
+
if (existsSync(root) && statSync(root).isDirectory()) out.push(root);
|
|
2067
|
+
} catch {}
|
|
2068
|
+
}
|
|
2069
|
+
return out.sort();
|
|
2070
|
+
}
|
|
2071
|
+
const FACTS_START = "<!-- sm:facts:start -->";
|
|
2072
|
+
const FACTS_END = "<!-- sm:facts:end -->";
|
|
2073
|
+
/** Render the deterministic facts block (marker included, no surrounding prose). */
|
|
2074
|
+
function renderFactsBlock(facts) {
|
|
2075
|
+
const lines = [
|
|
2076
|
+
FACTS_START,
|
|
2077
|
+
"### 事实(自动维护)",
|
|
2078
|
+
""
|
|
2079
|
+
];
|
|
2080
|
+
const bullet = (label, value) => {
|
|
2081
|
+
lines.push("- " + label + ":" + value);
|
|
2082
|
+
};
|
|
2083
|
+
const inline = (values) => values.filter((value) => Boolean(value && value.trim())).join(" · ");
|
|
2084
|
+
if (facts.entry) bullet("入口", "`" + facts.entry + "`");
|
|
2085
|
+
else bullet("入口", "仅知识(无入口命令)");
|
|
2086
|
+
if (facts.scope) bullet("适用范围", "`" + facts.scope + "`");
|
|
2087
|
+
if (facts.dryRunArg) bullet("预演参数", "`" + facts.dryRunArg + "`(默认预演)");
|
|
2088
|
+
if (facts.writeConfirm !== void 0) bullet("写操作确认", facts.writeConfirm ? "真实执行必须 confirm=true" : "非写操作");
|
|
2089
|
+
if (facts.verify && facts.verify.length > 0) bullet("验证命令", facts.verify.map((item) => "`" + item + "`").join("、"));
|
|
2090
|
+
if (facts.commands && facts.commands.length > 0) bullet("历史命令", facts.commands.map((item) => "`" + item + "`").join("、"));
|
|
2091
|
+
if (facts.files && facts.files.length > 0) bullet("涉及文件", facts.files.map((item) => "`" + item + "`").join("、"));
|
|
2092
|
+
if (facts.pitfalls && facts.pitfalls.length > 0) bullet("失败坑", inline(facts.pitfalls));
|
|
2093
|
+
lines.push(FACTS_END);
|
|
2094
|
+
return lines.join("\n");
|
|
2095
|
+
}
|
|
2096
|
+
/** Stable hash of the fact block (order-normalised input). */
|
|
2097
|
+
function factsHashOf(facts) {
|
|
2098
|
+
return shortHash(JSON.stringify({
|
|
2099
|
+
entry: facts.entry ?? "",
|
|
2100
|
+
scope: facts.scope ?? "",
|
|
2101
|
+
dryRunArg: facts.dryRunArg ?? "",
|
|
2102
|
+
writeConfirm: facts.writeConfirm === void 0 ? null : facts.writeConfirm,
|
|
2103
|
+
verify: [...facts.verify ?? []],
|
|
2104
|
+
commands: [...facts.commands ?? []],
|
|
2105
|
+
files: [...facts.files ?? []],
|
|
2106
|
+
pitfalls: [...facts.pitfalls ?? []]
|
|
2107
|
+
}), 8);
|
|
2108
|
+
}
|
|
2109
|
+
/** Split a body into prose and the facts block (no marker = hasBlock false). */
|
|
2110
|
+
function splitFactsBlock(body) {
|
|
2111
|
+
const text = String(body ?? "");
|
|
2112
|
+
const start = text.indexOf(FACTS_START);
|
|
2113
|
+
const end = text.indexOf(FACTS_END);
|
|
2114
|
+
if (start < 0 || end < start) return {
|
|
2115
|
+
prose: text.trim(),
|
|
2116
|
+
facts: "",
|
|
2117
|
+
hasBlock: false
|
|
2118
|
+
};
|
|
2119
|
+
return {
|
|
2120
|
+
prose: (text.slice(0, start) + text.slice(end + 21)).trim(),
|
|
2121
|
+
facts: text.slice(start, end + 21),
|
|
2122
|
+
hasBlock: true
|
|
2123
|
+
};
|
|
2124
|
+
}
|
|
2125
|
+
/** Replace (or append) the facts block; prose is never rewritten. */
|
|
2126
|
+
function applyFactsBlock(body, block) {
|
|
2127
|
+
const split = splitFactsBlock(body);
|
|
2128
|
+
const head = split.prose.trim();
|
|
2129
|
+
if (split.hasBlock) return (head ? head + "\n\n" : "") + block;
|
|
2130
|
+
if (head.includes("## 事实(自动维护)")) return (head ? head + "\n\n" : "") + block;
|
|
2131
|
+
return (head ? head + "\n\n" : "") + "## 事实(自动维护)\n\n" + block;
|
|
2132
|
+
}
|
|
2133
|
+
/** Hash of the human prose (facts block excluded) — detects human edits. */
|
|
2134
|
+
function bodyHashOf(body) {
|
|
2135
|
+
return shortHash(splitFactsBlock(body).prose.replace(/\s+/g, " ").trim(), 8);
|
|
2136
|
+
}
|
|
2137
|
+
/** `git-sync-supermemory` → skill `sm-git-sync-supermemory`, tool `sm_cap_git_sync_supermemory`. */
|
|
2138
|
+
function capabilitySkillNames(name) {
|
|
2139
|
+
const slug = kebabCase(String(name ?? "").replace(/^sm_cap_|^sm_composite_|^sm-/, ""), 40) || "capability";
|
|
2140
|
+
return {
|
|
2141
|
+
skillName: "sm-" + slug,
|
|
2142
|
+
toolName: "sm_cap_" + slug.replace(/-/g, "_")
|
|
2143
|
+
};
|
|
2144
|
+
}
|
|
2145
|
+
/** Six-section prose for a capability skill (no facts block; facts are appended). */
|
|
2146
|
+
function renderCapabilityProse(input) {
|
|
2147
|
+
input.entry || input.entryDraft;
|
|
2148
|
+
const lines = [];
|
|
2149
|
+
lines.push("# " + input.title);
|
|
2150
|
+
lines.push("");
|
|
2151
|
+
lines.push("> 由 SuperMemory 固化为单一能力:本文件是知识面(何时用、怎么判断),执行面是 " + (input.runnable && input.entry ? "工具 `" + capabilitySkillNames(input.name).toolName + "`" : "未开放(仅知识)") + "。");
|
|
2152
|
+
lines.push(">");
|
|
2153
|
+
lines.push("> 本文件位于 `.dsh/capabilities/<name>.md`,不进入宿主 available_skills;模型在会话里按需调用 `sm_cap_doc` 读取。");
|
|
2154
|
+
lines.push("> 自动维护只刷新文末「事实」块;块外散文可以手工编辑,不会被自动覆盖。");
|
|
2155
|
+
lines.push("");
|
|
2156
|
+
lines.push("## 何时用(触发条件)");
|
|
2157
|
+
lines.push("");
|
|
2158
|
+
lines.push("- " + (input.description || "待补充:写清什么任务下加载它。"));
|
|
2159
|
+
if (input.routeNote) lines.push("- " + input.routeNote);
|
|
2160
|
+
if (input.scope) lines.push("- 当前工作区在适用范围 `" + input.scope + "` 内,且已有该能力的入口(或仅需要这套判断)。");
|
|
2161
|
+
lines.push("");
|
|
2162
|
+
lines.push("## 决策清单");
|
|
2163
|
+
lines.push("");
|
|
2164
|
+
lines.push("1. 先确认本能力与当前任务同类(对象/目录/文件族一致),再看事实块里的入口与范围。");
|
|
2165
|
+
lines.push("2. " + (input.writeConfirm ? "真实执行可能写远端/改状态:先默认预演,确认真实执行时必须显式 confirm=true。" : "先默认预演(有 dryRunArg 时自动附加),确认输出与退出码符合预期后再实跑。"));
|
|
2166
|
+
lines.push("3. 连续失败或事实块与现状不符时,更新能力定义(version+1、changelog 记录原因),不要手敲命令绕过。");
|
|
2167
|
+
lines.push("4. 完成后按「验证方式」核对,并把新发现的坑补进本文件(自动刷新只动事实块)。");
|
|
2168
|
+
lines.push("");
|
|
2169
|
+
lines.push("## 步骤纲要");
|
|
2170
|
+
lines.push("");
|
|
2171
|
+
lines.push("1. 读取事实块,确认入口、scope、预演参数与验证命令。");
|
|
2172
|
+
lines.push("2. " + (input.entry ? "调用 `" + capabilitySkillNames(input.name).toolName + "`(固定入口 `" + input.entry + "`,不接受任意参数透传)。" : "按事实块里的判断推进;本能力没有执行面,不要凭空执行命令。"));
|
|
2173
|
+
lines.push("3. 失败时按「关键坑」停下判断,不要机械重试。");
|
|
2174
|
+
lines.push("");
|
|
2175
|
+
lines.push("## 关键坑");
|
|
2176
|
+
lines.push("");
|
|
2177
|
+
lines.push("- scope 外的会话不会执行该能力(工作区隔离);先确认当前目录在适用范围。");
|
|
2178
|
+
if (input.writeConfirm) lines.push("- writeConfirm 能力没有 confirm=true 时只返回「需要确认」,不会执行。");
|
|
2179
|
+
lines.push("- 连续失败时先更新能力定义/事实块,不要绕过闸门。");
|
|
2180
|
+
lines.push("");
|
|
2181
|
+
lines.push("## 验证方式");
|
|
2182
|
+
lines.push("");
|
|
2183
|
+
if (input.doc) lines.push("- 详细流程见 `" + input.doc + "`;执行结果看 exitCode/stdout/stderr,而不是「命令似乎跑了」。");
|
|
2184
|
+
else lines.push("- 看入口命令的 exitCode/stdout/stderr;没有可观察结果就不要宣称完成。");
|
|
2185
|
+
lines.push("- 事实块与文件内容不一致时,以文件/命令输出为准并更新事实块。");
|
|
2186
|
+
lines.push("");
|
|
2187
|
+
lines.push("## 反例(不要这样用)");
|
|
2188
|
+
lines.push("");
|
|
2189
|
+
lines.push("- 一次性任务、对象不同、或当前工作区不在 scope 内:不要套用。");
|
|
2190
|
+
lines.push("- 把知识面当免确认快捷方式:写操作仍需 confirm 与验证。");
|
|
2191
|
+
return lines.join("\n");
|
|
2192
|
+
}
|
|
2193
|
+
/** Deterministic six-section + facts bundle for one capability. */
|
|
2194
|
+
function buildCapabilitySkill(input) {
|
|
2195
|
+
const names = capabilitySkillNames(input.name);
|
|
2196
|
+
const extra = input.facts ?? {};
|
|
2197
|
+
const union = (left, right) => [.../* @__PURE__ */ new Set([...left ?? [], ...right ?? []])];
|
|
2198
|
+
const facts = {
|
|
2199
|
+
...input.entry ? { entry: input.entry } : {},
|
|
2200
|
+
...input.scope ? { scope: input.scope } : {},
|
|
2201
|
+
...input.dryRunArg ? { dryRunArg: input.dryRunArg } : {},
|
|
2202
|
+
...input.writeConfirm !== void 0 ? { writeConfirm: input.writeConfirm } : {},
|
|
2203
|
+
verify: union(input.entry ? [input.entry] : [], extra.verify),
|
|
2204
|
+
commands: [...extra.commands ?? []],
|
|
2205
|
+
files: union(input.doc ? [input.doc] : [], extra.files),
|
|
2206
|
+
pitfalls: union(input.entry ? ["scope 外拒绝执行;连续失败先更新能力定义/事实块,不要绕过闸门。"] : ["仅知识能力:没有执行面,不要凭空执行命令。"], extra.pitfalls)
|
|
2207
|
+
};
|
|
2208
|
+
const factsBlock = renderFactsBlock(facts);
|
|
2209
|
+
const prose = renderCapabilityProse(input);
|
|
2210
|
+
const body = applyFactsBlock(prose, factsBlock);
|
|
2211
|
+
return {
|
|
2212
|
+
name: names.skillName,
|
|
2213
|
+
title: input.title,
|
|
2214
|
+
description: clipDescription(input.description),
|
|
2215
|
+
...input.scope ? { whenToUse: "在 " + input.scope + " 内遇到「" + input.title + "」类任务时加载。" } : {},
|
|
2216
|
+
prose,
|
|
2217
|
+
body,
|
|
2218
|
+
facts,
|
|
2219
|
+
factsBlock,
|
|
2220
|
+
bodyHash: bodyHashOf(body),
|
|
2221
|
+
factsHash: factsHashOf(facts)
|
|
2222
|
+
};
|
|
2223
|
+
}
|
|
2224
|
+
/**
|
|
2225
|
+
* Write/update one capability's SKILL.md using facts-block merge.
|
|
2226
|
+
*
|
|
2227
|
+
* - existing file: prose kept, facts block replaced;
|
|
2228
|
+
* - no markers (manual skill): prose kept, block appended (first touch);
|
|
2229
|
+
* - changed description/facts: version+1 + changelog;
|
|
2230
|
+
* - human-edited prose: status 'preserved' (only the facts block is refreshed).
|
|
2231
|
+
*/
|
|
2232
|
+
async function upsertCapabilitySkill(options) {
|
|
2233
|
+
const now = options.now ?? Date.now();
|
|
2234
|
+
const bundle = buildCapabilitySkill(options.input);
|
|
2235
|
+
const found = findCapabilityDoc(options.root, options.input.name);
|
|
2236
|
+
const factsBlock = bundle.factsBlock;
|
|
2237
|
+
const nextProse = found ? splitFactsBlock(found.parsed.body).prose : String(options.draftBody ?? "").trim() || bundle.prose;
|
|
2238
|
+
const bodyHash = bodyHashOf(nextProse);
|
|
2239
|
+
const nextBody = applyFactsBlock(nextProse, factsBlock);
|
|
2240
|
+
const description = clipDescription(options.input.description || bundle.description);
|
|
2241
|
+
const path = capabilityDocPath(options.root, options.input.name);
|
|
2242
|
+
const humanEdited = Boolean(found && options.previousBodyHash && bodyHashOf(found.parsed.body) !== options.previousBodyHash);
|
|
2243
|
+
if (found && found.parsed.body === nextBody && found.parsed.description === description) return {
|
|
2244
|
+
status: "unchanged",
|
|
2245
|
+
name: bundle.name,
|
|
2246
|
+
version: found.parsed.version,
|
|
2247
|
+
path,
|
|
2248
|
+
message: "内容与事实块均未变化,未升版。",
|
|
2249
|
+
bodyHash,
|
|
2250
|
+
factsHash: bundle.factsHash,
|
|
2251
|
+
body: nextBody,
|
|
2252
|
+
factsBlock
|
|
2253
|
+
};
|
|
2254
|
+
const recordedVersion = Math.max(1, Number(options.input.version) || 1);
|
|
2255
|
+
let version = found ? Math.max(found.parsed.version, recordedVersion) + 1 : recordedVersion;
|
|
2256
|
+
const changelog = [versionLine(version, String(options.changelog ?? "").trim().slice(0, 200) || (found ? "刷新事实块/路由描述(正文人工散文保留)" : "生成能力知识面(六节 + 事实块)")), ...found?.parsed.changelog ?? []].slice(0, SKILL_CHANGELOG_MAX);
|
|
2257
|
+
const draft = {
|
|
2258
|
+
name: bundle.name,
|
|
2259
|
+
title: options.input.title,
|
|
2260
|
+
description,
|
|
2261
|
+
...bundle.whenToUse ? { whenToUse: bundle.whenToUse } : {},
|
|
2262
|
+
tags: (options.input.tags ?? ["skill", "capability"]).slice(0, 8),
|
|
2263
|
+
body: nextBody,
|
|
2264
|
+
...options.input.taskKey ? { taskKey: options.input.taskKey } : {},
|
|
2265
|
+
source: {
|
|
2266
|
+
kind: "capability",
|
|
2267
|
+
key: "capability/" + options.input.name,
|
|
2268
|
+
label: options.input.title,
|
|
2269
|
+
occurrences: 1
|
|
2270
|
+
}
|
|
2271
|
+
};
|
|
2272
|
+
try {
|
|
2273
|
+
writeFileAtomic(path, renderSkillMarkdown(draft, {
|
|
2274
|
+
version,
|
|
2275
|
+
updatedAt: now,
|
|
2276
|
+
renderedHash: skillContentHash(description, nextBody),
|
|
2277
|
+
changelog,
|
|
2278
|
+
enabled: found?.parsed.enabled !== false,
|
|
2279
|
+
bodyHash,
|
|
2280
|
+
factsHash: bundle.factsHash
|
|
2281
|
+
}));
|
|
2282
|
+
} catch (error) {
|
|
2283
|
+
return {
|
|
2284
|
+
status: "error",
|
|
2285
|
+
name: bundle.name,
|
|
2286
|
+
version,
|
|
2287
|
+
path,
|
|
2288
|
+
message: "写入知识面文件失败:" + String(error).slice(0, 160)
|
|
2289
|
+
};
|
|
2290
|
+
}
|
|
2291
|
+
return {
|
|
2292
|
+
status: found ? humanEdited ? "preserved" : "updated" : "created",
|
|
2293
|
+
name: bundle.name,
|
|
2294
|
+
version,
|
|
2295
|
+
path,
|
|
2296
|
+
message: humanEdited ? "检测到人工编辑,保留正文,仅刷新事实块(已升版)。" : found ? "已刷新事实块并升版。" : "已生成知识面。",
|
|
2297
|
+
bodyHash,
|
|
2298
|
+
factsHash: bundle.factsHash,
|
|
2299
|
+
body: nextBody,
|
|
2300
|
+
factsBlock
|
|
2301
|
+
};
|
|
2302
|
+
}
|
|
2303
|
+
/**
|
|
2304
|
+
* Archive one capability skill directory (never hard-delete). Idempotent: a
|
|
2305
|
+
* missing directory reports undefined, an existing archive target gets a suffix.
|
|
2306
|
+
*/
|
|
2307
|
+
async function archiveCapabilitySkill(root, name, options = {}) {
|
|
2308
|
+
if (!isSkillName(name)) return void 0;
|
|
2309
|
+
const from = join(root, name);
|
|
2310
|
+
if (!existsSync(from)) return void 0;
|
|
2311
|
+
const archiveDir = archiveDirForRoot(root);
|
|
2312
|
+
mkdirSync(archiveDir, { recursive: true });
|
|
2313
|
+
const now = options.now ?? Date.now();
|
|
2314
|
+
let target = join(archiveDir, name);
|
|
2315
|
+
if (existsSync(target)) target = join(archiveDir, name + "-cap-" + shortHash(from + ":" + now, 4));
|
|
2316
|
+
try {
|
|
2317
|
+
renameSync(from, target);
|
|
2318
|
+
} catch {
|
|
2319
|
+
return;
|
|
2320
|
+
}
|
|
2321
|
+
const record = {
|
|
2322
|
+
root,
|
|
2323
|
+
name,
|
|
2324
|
+
from,
|
|
2325
|
+
archivePath: target,
|
|
2326
|
+
archivedAt: now,
|
|
2327
|
+
reason: options.reason ?? "能力删除:知识面目录归档(可回滚,不硬删)"
|
|
2328
|
+
};
|
|
2329
|
+
const manifestFile = join(archiveDir, "manifest.json");
|
|
2330
|
+
const manifest = readArchiveManifest(manifestFile);
|
|
2331
|
+
manifest.entries.push({
|
|
2332
|
+
name,
|
|
2333
|
+
archivePath: target,
|
|
2334
|
+
version: 0,
|
|
2335
|
+
description: "",
|
|
2336
|
+
archivedAt: now,
|
|
2337
|
+
reason: record.reason,
|
|
2338
|
+
from
|
|
2339
|
+
});
|
|
2340
|
+
try {
|
|
2341
|
+
writeFileAtomic(manifestFile, JSON.stringify({
|
|
2342
|
+
version: 1,
|
|
2343
|
+
entries: manifest.entries.slice(-500)
|
|
2344
|
+
}, null, 2));
|
|
2345
|
+
} catch {}
|
|
2346
|
+
return record;
|
|
2347
|
+
}
|
|
2348
|
+
export { upsertCapabilitySkill as S, resolveSkillRoot as _, archivedCapabilityDocFiles as a, splitFactsBlock as b, capabilitySkillNames as c, isShellIdentity as d, parseSkillMarkdown as f, resolveCapabilityRoot as g, recordTaskObservation as h, archiveShellSkills as i, draftSkillFromTaskSignal as l, readSkillVersionAt as m, archiveCapabilityDoc as n, bodyHashOf as o, parseTaskSamples as p, archiveCapabilitySkill as r, capabilityDocPath as s, adoptCapabilityDocVersion as t, extractWorkflowFacts as u, signalSessionCount as v, taskSignalKey as x, skill_forge_exports as y };
|