@yolk_vat-y/dsh-project-memory 0.6.4 → 0.6.6
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 +111 -1
- package/README.md +35 -7
- package/README.zh-CN.md +34 -7
- package/cordis.patch.yml +6 -1
- package/package.json +6 -4
- package/scripts/bench-edits.mjs +87 -0
- package/skills/project-memory/SKILL.md +72 -0
- package/skills/project-memory/scripts/pm-digest.mjs +138 -0
- package/src/audit.js +6 -0
- package/src/auto-inject.js +72 -24
- package/src/commands/insight-actions.js +1 -1
- package/src/commands/task-actions.js +1 -1
- package/src/commands/tasks.js +1 -1
- package/src/doc-index.js +7 -5
- package/src/doc-pipeline.js +2 -0
- package/src/index.js +25 -11
- package/src/insight-store.js +56 -6
- package/src/lazy.js +5 -3
- package/src/ops.js +226 -36
- package/src/readiness.js +60 -9
- package/src/recall.js +5 -1
- package/src/setup/taskbridge.js +3 -3
- package/src/skill.js +84 -0
- package/src/store.js +123 -28
- package/src/tools/index-doc.js +11 -3
- package/src/tools/index-repo.js +4 -4
- package/src/tools/watch-repo.js +3 -3
- package/src/util/flush.js +62 -0
- package/src/util/fs.js +602 -71
- package/src/util/persist.js +27 -0
- package/src/util/ru.js +201 -0
- package/src/util/search.js +20 -0
- package/src/vector-index.js +4 -0
- package/src/watch.js +48 -17
package/src/ops.js
CHANGED
|
@@ -72,38 +72,212 @@ const FETCH_TOOLS = new Set(['web_fetch', 'web_search'])
|
|
|
72
72
|
const SHELL_TOOLS = new Set(['bash', 'shell', 'pwsh', 'powershell', 'run', 'exec', 'job_run'])
|
|
73
73
|
const QUERY_TOOLS = new Set(['query_memory', 'recall'])
|
|
74
74
|
|
|
75
|
-
/** bash 命令 → op。顺序有意义:先具体后兜底。 */
|
|
76
|
-
const SHELL_OP_RULES = [
|
|
77
|
-
[/\bnpm\s+(publish|pack)\b/, 'npm-publish'],
|
|
78
|
-
[/\bnpm\s+version\b/, 'release'],
|
|
79
|
-
[/\bgit\s+tag\b/, 'release'],
|
|
80
|
-
[/\bgit\s+push\b/, 'git-push'],
|
|
81
|
-
[/\bgit\s+(commit|add)\b/, 'git-commit'],
|
|
82
|
-
[/\bgit\s+(checkout|restore|reset)\b/, 'file-write'],
|
|
83
|
-
// `\brm\b` 而不是 `rm\s+-[rf]`:后者被 `\b` 卡在 'rm -rf' 的 r 后面(f 还是词字符),永远匹配不上。
|
|
84
|
-
[/\b(rm|git\s+rm|unlink|rmdir|shred)\b/, 'file-delete'],
|
|
85
|
-
[/\b(kubectl|docker\s+push|systemctl\s+restart|pm2\s+(deploy|restart)|vercel|netlify\s+deploy)\b/, 'deploy'],
|
|
86
|
-
[/\b(migrate|alembic|prisma\s+migrate|db:migrate)\b/, 'migrate'],
|
|
87
|
-
// 注意:只认"渲染器",不认 .pptx 扩展名——否则给 pptx 改个时间戳也会命中"做 PPT"
|
|
88
|
-
[/(pptxgenjs|soffice|libreoffice|render\.sh|marp|pandoc|slidev)/, 'render-doc'],
|
|
89
|
-
[/(bench\.mjs|\bbench(mark)?\b)/, 'run-bench'],
|
|
90
|
-
[/\b(curl|wget|web_fetch)\b/, 'fetch-web'],
|
|
91
|
-
]
|
|
92
|
-
|
|
93
75
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* 重定向这一支要排除"丢弃输出",否则 `2>/dev/null`、`2>&1`、`>/dev/null` 这些
|
|
97
|
-
* **纯读命令的惯用写法**会被当成写盘:实测 `ls .agents 2>/dev/null | head` 抽出
|
|
98
|
-
* targets=['.agents']、`cat package.json 2>&1` 抽出 ['package.json'] —— 写目标正是
|
|
99
|
-
* `when.writes` 的匹配面,于是"读/列目录"会误触发一条 `writes:['package.json']` 的教训。
|
|
76
|
+
* 会写盘的文件类命令头。**只作用于命令头**,不再对整条命令做无锚点正则。
|
|
100
77
|
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
78
|
+
* 为什么改:旧实现对整条命令跑 `/\b(bench|migrate|rm|删除)\b/`,于是
|
|
79
|
+
* `find src client test scripts bench docs -type f` 里的目录名 `bench` → `run-bench`(实测注入
|
|
80
|
+
* 了 4 条基准 procedure、1777 字符);`grep -rn "migrate" docs` → `migrate`;子代理提示词里的
|
|
81
|
+
* "删除/迁移" → `file-delete`/`migrate`(实测父会话连吃 8 步)。
|
|
82
|
+
* 命令头是"这个 shell 到底在做什么"的唯一可靠位置;参数、路径、引号里的词都是**提及**。
|
|
83
|
+
*/
|
|
84
|
+
const WRITE_HEADS = new Set(['rm', 'rmdir', 'unlink', 'shred', 'mv', 'cp', 'touch', 'tee', 'truncate', 'mkdir'])
|
|
85
|
+
/** 包在真命令前面的词:跳过它们再取命令头。 */
|
|
86
|
+
const WRAPPERS = new Set(['sudo', 'env', 'command', 'builtin', 'exec', 'time', 'nohup', 'nice', 'stdbuf'])
|
|
87
|
+
/** 递归解析 `bash -c "…"` 的壳;深度上限防 `bash -c 'bash -c …'`。 */
|
|
88
|
+
const SHELLS = new Set(['bash', 'sh', 'zsh', 'dash', 'ksh'])
|
|
89
|
+
/** 分段:按未加引号的 `\n ; && || | &` 切。引号里的分隔符是正文,不是命令边界。 */
|
|
90
|
+
function unquotedSegments(text) {
|
|
91
|
+
const out = []
|
|
92
|
+
let cur = ''
|
|
93
|
+
let quote = null
|
|
94
|
+
for (let i = 0; i < text.length; i++) {
|
|
95
|
+
const c = text[i]
|
|
96
|
+
if (quote) {
|
|
97
|
+
cur += c
|
|
98
|
+
if (c === '\\' && quote === '"' && i + 1 < text.length) { cur += text[++i]; continue }
|
|
99
|
+
if (c === quote) quote = null
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
if (c === "'" || c === '"' || c === '`') { quote = c; cur += c; continue }
|
|
103
|
+
// `#` 只有在词首才是注释:URL 的 fragment、`#!` 之类不能当注释切掉。
|
|
104
|
+
if (c === '#' && (i === 0 || /\s/.test(text[i - 1]))) {
|
|
105
|
+
while (i < text.length && text[i] !== '\n') i++
|
|
106
|
+
out.push(cur)
|
|
107
|
+
cur = ''
|
|
108
|
+
continue
|
|
109
|
+
}
|
|
110
|
+
if (c === '\n' || c === ';') { out.push(cur); cur = ''; continue }
|
|
111
|
+
if ((c === '&' || c === '|') && text[i + 1] === c) { out.push(cur); cur = ''; i++; continue }
|
|
112
|
+
if (c === '|' || c === '&') { out.push(cur); cur = ''; continue }
|
|
113
|
+
cur += c
|
|
114
|
+
}
|
|
115
|
+
out.push(cur)
|
|
116
|
+
return out.map((s) => s.trim()).filter(Boolean)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 剥掉 heredoc 正文,只留下 opener 那一行。
|
|
121
|
+
* 正文是脚本内容(可能是 python/js),不是 shell 命令——旧实现会把里面的 `mkdir`/`rm` 当动作。
|
|
122
|
+
*/
|
|
123
|
+
function stripHeredocBodies(text) {
|
|
124
|
+
return text.replace(/<<-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1[^\n]*\n[\s\S]*?\n\2(?=\s*(?:$|\n))/g, (m) => `${m.split('\n')[0]}\n`)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 简单词法切分(去引号、保转义)。只用于取命令头与参数,不追求完整 shell 语义。 */
|
|
128
|
+
function tokenizeWords(segment) {
|
|
129
|
+
const out = []
|
|
130
|
+
let cur = ''
|
|
131
|
+
let quote = null
|
|
132
|
+
for (let i = 0; i < segment.length; i++) {
|
|
133
|
+
const c = segment[i]
|
|
134
|
+
if (quote) {
|
|
135
|
+
if (c === quote) { quote = null; continue }
|
|
136
|
+
if (c === '\\' && quote === '"' && i + 1 < segment.length) { cur += segment[++i]; continue }
|
|
137
|
+
cur += c
|
|
138
|
+
continue
|
|
139
|
+
}
|
|
140
|
+
if (c === "'" || c === '"' || c === '`') { quote = c; continue }
|
|
141
|
+
if (/\s/.test(c)) { if (cur) { out.push(cur); cur = '' } continue }
|
|
142
|
+
if (c === '\\' && i + 1 < segment.length) { cur += segment[++i]; continue }
|
|
143
|
+
cur += c
|
|
144
|
+
}
|
|
145
|
+
if (cur) out.push(cur)
|
|
146
|
+
return out
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 跳过环境变量赋值与 sudo/env/time 之类包装,返回真正的命令头与参数。 */
|
|
150
|
+
function commandHead(tokens) {
|
|
151
|
+
let i = 0
|
|
152
|
+
while (i < tokens.length) {
|
|
153
|
+
const t = tokens[i]
|
|
154
|
+
if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(t) || WRAPPERS.has(t) || t === '(') { i++; continue }
|
|
155
|
+
break
|
|
156
|
+
}
|
|
157
|
+
const raw = tokens[i] || ''
|
|
158
|
+
const head = raw.split('/').pop().toLowerCase()
|
|
159
|
+
let rest = tokens.slice(i + 1)
|
|
160
|
+
if (rest.length && rest[rest.length - 1] === ')') rest = rest.slice(0, -1)
|
|
161
|
+
return { head, rest }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const BENCH_ARG = /(^|\/)bench(mark)?(\/|\.(mjs|js|ts|cjs|mts|json)$)/i
|
|
165
|
+
const RENDER_ARG = /(^|\/)(pptxgenjs|soffice|libreoffice|pandoc|marp|slidev)(\b|\/)/i
|
|
166
|
+
|
|
167
|
+
/** 包管理器 / npx 之后的参数里识别动作。只有命令头是 runner 时才走这里。 */
|
|
168
|
+
function runnerOps(args) {
|
|
169
|
+
const ops = []
|
|
170
|
+
if (args.some((a) => RENDER_ARG.test(a))) ops.push('render-doc')
|
|
171
|
+
if (args.some((a) => BENCH_ARG.test(a))) ops.push('run-bench')
|
|
172
|
+
if (args[0] === 'migrate' || args.some((a) => /^db:migrate$/.test(a))) ops.push('migrate')
|
|
173
|
+
return ops
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 命令头 → op。空数组表示"认得这个头,但它不是这些动作"。 */
|
|
177
|
+
function opsOfHead(head, rest) {
|
|
178
|
+
const sub = String(rest[0] || '').toLowerCase()
|
|
179
|
+
switch (head) {
|
|
180
|
+
case 'npm': case 'pnpm': case 'yarn': case 'bun': {
|
|
181
|
+
if (sub === 'publish' || sub === 'pack') return ['npm-publish']
|
|
182
|
+
if (sub === 'version') return ['release']
|
|
183
|
+
if (sub === 'run' || sub === 'exec' || sub === 'dlx' || sub === 'x') return runnerOps(rest.slice(1))
|
|
184
|
+
return []
|
|
185
|
+
}
|
|
186
|
+
case 'npx': case 'pnpx': case 'bunx':
|
|
187
|
+
return runnerOps(rest)
|
|
188
|
+
case 'node': case 'tsx': case 'deno':
|
|
189
|
+
return rest.some((a) => BENCH_ARG.test(a)) ? ['run-bench'] : []
|
|
190
|
+
case 'git': {
|
|
191
|
+
const gsub = firstSubcommand(rest)
|
|
192
|
+
if (gsub === 'commit' || gsub === 'add') return ['git-commit']
|
|
193
|
+
if (gsub === 'push') return ['git-push']
|
|
194
|
+
if (gsub === 'tag') return ['release']
|
|
195
|
+
if (gsub === 'rm') return ['file-delete']
|
|
196
|
+
if (gsub === 'checkout' || gsub === 'restore' || gsub === 'reset') return ['file-write']
|
|
197
|
+
return []
|
|
198
|
+
}
|
|
199
|
+
case 'rm': case 'rmdir': case 'unlink': case 'shred':
|
|
200
|
+
return ['file-delete']
|
|
201
|
+
case 'kubectl': case 'systemctl': case 'pm2': case 'vercel': case 'netlify':
|
|
202
|
+
return ['deploy']
|
|
203
|
+
case 'docker':
|
|
204
|
+
return sub === 'push' || sub === 'compose' || sub === 'stack' ? ['deploy'] : []
|
|
205
|
+
case 'migrate': case 'alembic':
|
|
206
|
+
return ['migrate']
|
|
207
|
+
case 'prisma':
|
|
208
|
+
return sub === 'migrate' || sub === 'db' ? ['migrate'] : []
|
|
209
|
+
case 'soffice': case 'libreoffice': case 'pandoc': case 'marp': case 'slidev':
|
|
210
|
+
return ['render-doc']
|
|
211
|
+
case 'bench': case 'benchmark':
|
|
212
|
+
return ['run-bench']
|
|
213
|
+
case 'curl': case 'wget':
|
|
214
|
+
return ['fetch-web']
|
|
215
|
+
default:
|
|
216
|
+
return []
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** 未加引号的 `>` / `>>` 目标。引号里的 `>` 是正文(`echo "a > b"`),`<<` 不是写。 */
|
|
221
|
+
function redirectTargets(segment) {
|
|
222
|
+
const out = []
|
|
223
|
+
let quote = null
|
|
224
|
+
for (let i = 0; i < segment.length; i++) {
|
|
225
|
+
const c = segment[i]
|
|
226
|
+
if (quote) {
|
|
227
|
+
if (c === '\\' && quote === '"') { i++; continue }
|
|
228
|
+
if (c === quote) quote = null
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
if (c === "'" || c === '"' || c === '`') { quote = c; continue }
|
|
232
|
+
if (c === '\\') { i++; continue }
|
|
233
|
+
if (c !== '>') continue
|
|
234
|
+
if (segment[i + 1] === '>') i++
|
|
235
|
+
let j = i + 1
|
|
236
|
+
while (j < segment.length && /\s/.test(segment[j])) j++
|
|
237
|
+
let tok = ''
|
|
238
|
+
while (j < segment.length && !/[\s;|&)]/.test(segment[j])) { tok += segment[j]; j++ }
|
|
239
|
+
i = Math.max(i, j - 1)
|
|
240
|
+
if (!tok) continue
|
|
241
|
+
if (tok.startsWith('&') || tok === '/dev/null' || tok.startsWith('/dev/stdout') || tok.startsWith('/dev/stderr')) continue
|
|
242
|
+
if (/^\d+$/.test(tok)) continue
|
|
243
|
+
out.push(tok)
|
|
244
|
+
}
|
|
245
|
+
return out
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** 这个段落在写盘吗?写盘命令头、`sed -i`、重定向都算。 */
|
|
249
|
+
function isWriteSegment(head, rest, redirects) {
|
|
250
|
+
if (redirects.length) return true
|
|
251
|
+
if (WRITE_HEADS.has(head)) return true
|
|
252
|
+
if (head === 'sed' && rest.includes('-i')) return true
|
|
253
|
+
if (head === 'git' && String(rest[0] || '').toLowerCase() === 'rm') return true
|
|
254
|
+
return false
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* git 的子命令要跳过全局 flag(`git -C repo commit` 不能被读成 `-C`)。
|
|
259
|
+
* @param {string[]} args
|
|
260
|
+
* @returns {string} 小写子命令;没有则空串
|
|
103
261
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
262
|
+
function firstSubcommand(args) {
|
|
263
|
+
const takesValue = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path'])
|
|
264
|
+
for (let i = 0; i < args.length; i++) {
|
|
265
|
+
const arg = args[i]
|
|
266
|
+
if (arg.startsWith('-')) {
|
|
267
|
+
if (takesValue.has(arg)) i++
|
|
268
|
+
continue
|
|
269
|
+
}
|
|
270
|
+
return arg.toLowerCase()
|
|
271
|
+
}
|
|
272
|
+
return ''
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** 合并去重(保持顺序)。 */
|
|
276
|
+
function pushUnique(list, value) {
|
|
277
|
+
if (!list.includes(value)) list.push(value)
|
|
278
|
+
}
|
|
106
279
|
|
|
280
|
+
/** WSL 路径或 Windows 壳的签名:只用于 hosts,不参与动作判定。 */
|
|
107
281
|
const WSL_HINT_RE = /\/mnt\/[a-z]\//i
|
|
108
282
|
|
|
109
283
|
function safeJson(text) {
|
|
@@ -129,21 +303,37 @@ function argsFields(argsText) {
|
|
|
129
303
|
}
|
|
130
304
|
}
|
|
131
305
|
|
|
132
|
-
/** bash
|
|
133
|
-
|
|
134
|
-
const
|
|
306
|
+
/** 内部的递归实现:`bash -c` 会再解析一层。 */
|
|
307
|
+
function classifyCommand(command, depth) {
|
|
308
|
+
const raw = String(command || '')
|
|
135
309
|
const ops = []
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
310
|
+
const targets = []
|
|
311
|
+
for (const segment of unquotedSegments(stripHeredocBodies(raw))) {
|
|
312
|
+
const { head, rest } = commandHead(tokenizeWords(segment))
|
|
313
|
+
if (SHELLS.has(head) && depth < 2) {
|
|
314
|
+
const ci = rest.indexOf('-c')
|
|
315
|
+
if (ci >= 0 && rest[ci + 1]) {
|
|
316
|
+
const inner = classifyCommand(rest.slice(ci + 1).join(' '), depth + 1)
|
|
317
|
+
for (const op of inner.ops) if (op !== 'shell-run') pushUnique(ops, op)
|
|
318
|
+
for (const t of inner.targets) pushUnique(targets, t)
|
|
319
|
+
}
|
|
320
|
+
} else {
|
|
321
|
+
for (const op of opsOfHead(head, rest)) pushUnique(ops, op)
|
|
322
|
+
}
|
|
323
|
+
const redirects = redirectTargets(segment)
|
|
324
|
+
if (isWriteSegment(head, rest, redirects)) {
|
|
325
|
+
for (const t of extractPathTokens(segment)) pushUnique(targets, t)
|
|
139
326
|
}
|
|
140
327
|
}
|
|
141
328
|
if (!ops.length) ops.push('shell-run')
|
|
142
|
-
const
|
|
143
|
-
const hosts = WSL_HINT_RE.test(cmd) || /\b(powershell|pwsh|cmd)\.exe\b/i.test(cmd) ? ['wsl'] : []
|
|
329
|
+
const hosts = WSL_HINT_RE.test(raw) || /\b(powershell|pwsh|cmd)\.exe\b/i.test(raw) ? ['wsl'] : []
|
|
144
330
|
return { ops, targets, hosts }
|
|
145
331
|
}
|
|
146
332
|
|
|
333
|
+
export function classifyShellCommand(command) {
|
|
334
|
+
return classifyCommand(command, 0)
|
|
335
|
+
}
|
|
336
|
+
|
|
147
337
|
/**
|
|
148
338
|
* 一次工具调用 → { ops, targets, hosts }。
|
|
149
339
|
* @param {string} name 工具名
|
package/src/readiness.js
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
import { FALLBACK_OPS, WEAK_OPS, activityFromText, opForLegacyAction } from './ops.js'
|
|
20
20
|
import { extractPathTokens } from './util/path-token.js'
|
|
21
21
|
import { CJK_RANGE, tokenize } from './util/search.js'
|
|
22
|
+
import { CYRILLIC_RANGE, RU_STOPWORDS, normalizeRuWord } from './util/ru.js'
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* 动作词典:动作 id → 匹配模式。命令形态与人类意图词都归一成同一套 id,
|
|
@@ -29,13 +30,28 @@ export const ACTION_LEXICON = [
|
|
|
29
30
|
['git-commit', [/git\s+commit\b/i, /提交/]],
|
|
30
31
|
['git-push', [/git\s+push\b/i, /推送/]],
|
|
31
32
|
['npm-publish', [/npm\s+publish\b/i, /发包/, /发布\s*到\s*npm/i]],
|
|
32
|
-
|
|
33
|
+
// 英文 `release` 只在带版本号时才算动作:`release notes` 是文档任务,不是发版。
|
|
34
|
+
['release', [/git\s+tag\b/i, /发版/, /发布版本/, /\brelease\s+v?\d/i]],
|
|
33
35
|
['go-public', [/公开/, /开源/, /对外/, /上线/]],
|
|
34
36
|
['delete', [/rm\s+-rf\b/, /git\s+rm\b/, /删除/, /清理/]],
|
|
35
37
|
['migrate', [/migrate\b/i, /迁移/]],
|
|
36
38
|
['deploy', [/\bdeploy\b/i, /部署/]],
|
|
37
39
|
]
|
|
38
40
|
|
|
41
|
+
/**
|
|
42
|
+
* 允许**人类消息里的动作词**触发全文注入的 op 白名单。
|
|
43
|
+
*
|
|
44
|
+
* 判据是"这条记忆必须在动作**之前**到达,晚了就没用,且动作不可逆":
|
|
45
|
+
* 提交 / 发 npm 包 / 发版 / 推远端 / 部署 / 公开仓库。这几个词在散文里也基本只有一种意思,
|
|
46
|
+
* 而且都发生在"动作之前才有用"的位置(提交后再说"检查公开面"就晚了)。
|
|
47
|
+
*
|
|
48
|
+
* 反过来,`删除 / 清理 / 渲染 / 基准 / 取网页 / 查询记忆` 这些词在人类消息里经常只是
|
|
49
|
+
* **提及**("删除后的悬空状态"、"清理一下 README 的措辞"、"这个渲染是错的"),
|
|
50
|
+
* 用它们做确定性全文注入实测代价是 1200 字符一条的无关 procedure。
|
|
51
|
+
* 这些动作仍可由**已观察到的工具调用**触发(`rm` 真的执行了、`soffice` 真的跑了)。
|
|
52
|
+
*/
|
|
53
|
+
export const PRE_ACTION_OPS = new Set(['npm-publish', 'release', 'git-commit', 'git-push', 'deploy', 'go-public'])
|
|
54
|
+
|
|
39
55
|
/**
|
|
40
56
|
* 文本 → 动作 id 列表(确定性、去重、词典序稳定)。
|
|
41
57
|
* @param {string} text - 人类消息或工具调用参数。
|
|
@@ -64,17 +80,27 @@ export function extractPaths(text) {
|
|
|
64
80
|
|
|
65
81
|
/**
|
|
66
82
|
* 组装就绪上下文:人类意图 + 已观察动作。三处来源都做动作/路径归一,调用方只需给文本。
|
|
83
|
+
*
|
|
84
|
+
* **证据来源决定准入**(2026-10-25):observed(结构化工具调用)可以触发全文注入;
|
|
85
|
+
* 人类消息里的动作词只允许 4 个"不可逆外部动作"触发全文(见 {@link PRE_ACTION_OPS}),
|
|
86
|
+
* 其余只进提示通道。原因见 PRE_ACTION_OPS 的注释。
|
|
67
87
|
* @param {object} input
|
|
68
88
|
* @param {string} [input.humanText] - 本轮人类消息(先发窗口)。
|
|
69
|
-
* @param {string} [input.actionText] -
|
|
89
|
+
* @param {string} [input.actionText] - 旧调用方的工具调用文本(测试/老宿主;生产路径不再传)。
|
|
90
|
+
* @param {string[]} [input.ops] - 结构化工具调用归一出的动作(可信)。
|
|
91
|
+
* @param {string[]} [input.targets] - 结构化写目标(可信)。
|
|
70
92
|
* @param {string[]} [input.actions] - 调用方已归一化的动作 id(可选,会被并集)。
|
|
71
93
|
* @param {string[]} [input.paths] - 调用方已归一化的路径(可选,会被并集)。
|
|
72
|
-
* @returns {{ humanText: string, actionText: string, actions: string[], paths: string[] }}
|
|
94
|
+
* @returns {{ humanText: string, actionText: string, actions: string[], paths: string[], humanPaths: string[], ops: string[], opsObserved: string[], intentOps: string[], targets: string[], hosts: string[], intent: string, tags: string[] }}
|
|
73
95
|
*/
|
|
74
96
|
export function buildReadinessContext(input = {}) {
|
|
75
97
|
const humanText = String(input.humanText ?? input.query ?? '')
|
|
76
98
|
const actionText = String(input.actionText || '')
|
|
77
|
-
|
|
99
|
+
// `input.actions` 是调用方**已归一化**的动作(测试/老宿主),当作已观察证据;
|
|
100
|
+
// 只有从 humanText 里现猜出来的词才受 PRE_ACTION_OPS 约束。
|
|
101
|
+
const declaredActions = new Set(input.actions || [])
|
|
102
|
+
const proseActions = detectActions(humanText)
|
|
103
|
+
const actions = new Set([...declaredActions, ...proseActions])
|
|
78
104
|
// 人类消息里提到的路径 = **动手前的写意图**("帮我改 src/x.js");动作文本里的路径不区分读写,
|
|
79
105
|
// 读一个文件不该算写目标(否则 `read README.md` 会命中 writes:['README.md'])。
|
|
80
106
|
// 两者对外仍合并成 `paths`(兼容既有语义),写通道单看 humanPaths。
|
|
@@ -87,11 +113,20 @@ export function buildReadinessContext(input = {}) {
|
|
|
87
113
|
const paths = new Set([...humanPaths, ...extractPaths(actionText)])
|
|
88
114
|
// 动作平面(S1):结构化调用优先;没有结构化调用时对 actionText 跑一遍 shell 规则兜底。
|
|
89
115
|
const fromText = activityFromText(actionText)
|
|
90
|
-
const
|
|
91
|
-
for (const a of
|
|
116
|
+
const opsObserved = new Set([...(input.ops || []), ...fromText.ops])
|
|
117
|
+
for (const a of declaredActions) {
|
|
92
118
|
const op = opForLegacyAction(a)
|
|
93
|
-
if (op)
|
|
119
|
+
if (op) opsObserved.add(op)
|
|
94
120
|
}
|
|
121
|
+
// 人类消息里的动作词:`删除/清理/渲染/基准/取网页` 在散文里太容易是**提及**
|
|
122
|
+
// (实测 "删除后的悬空状态"、"清理一下 README 的措辞" 都命中过 file-delete,
|
|
123
|
+
// 后者注入了一条 1200 字符的恢复 procedure)。只有没法等的不可逆外部动作才允许先发。
|
|
124
|
+
const intentOps = new Set()
|
|
125
|
+
for (const a of proseActions) {
|
|
126
|
+
const op = opForLegacyAction(a)
|
|
127
|
+
if (op) intentOps.add(op)
|
|
128
|
+
}
|
|
129
|
+
const ops = new Set([...opsObserved, ...[...intentOps].filter((op) => PRE_ACTION_OPS.has(op))])
|
|
95
130
|
const targets = new Set([...(input.targets || []), ...fromText.targets])
|
|
96
131
|
const hosts = new Set([...(input.hosts || []), ...fromText.hosts])
|
|
97
132
|
return {
|
|
@@ -101,6 +136,8 @@ export function buildReadinessContext(input = {}) {
|
|
|
101
136
|
paths: [...paths],
|
|
102
137
|
humanPaths: [...humanPaths],
|
|
103
138
|
ops: [...ops],
|
|
139
|
+
opsObserved: [...opsObserved],
|
|
140
|
+
intentOps: [...intentOps],
|
|
104
141
|
targets: [...targets],
|
|
105
142
|
hosts: [...hosts],
|
|
106
143
|
intent: intentText(humanText),
|
|
@@ -213,7 +250,7 @@ export function intentText(text) {
|
|
|
213
250
|
.replace(/[A-Za-z]:\\[^\s"']*/g, ' ')
|
|
214
251
|
.replace(/(?:[A-Za-z0-9_.@-]+\/)+[A-Za-z0-9_.@-]+/g, ' ')
|
|
215
252
|
// 带扩展名的**非 ASCII 文件名**(`示例调研.pptx` / `docs/示例.md`):
|
|
216
|
-
// 只挡 ASCII 路径不够——中文文件名整块留下,"调研""
|
|
253
|
+
// 只挡 ASCII 路径不够——中文文件名整块留下,"调研""演示"这类子串照样触发 when.intents,
|
|
217
254
|
// 正是引号剥离要防的那类假阳性。ASCII 裸名(如 notes.md)保持原语义,不在这里动。
|
|
218
255
|
.replace(/\S*[^\x00-\x7F]\S*\.[A-Za-z][A-Za-z0-9]{0,7}\b/g, ' ')
|
|
219
256
|
// 仓库里的裸文件名(README / CHANGELOG …)也是语料,不是意图
|
|
@@ -232,6 +269,13 @@ export function isIntentWord(word) {
|
|
|
232
269
|
const s = String(word || '').trim()
|
|
233
270
|
if (!s) return false
|
|
234
271
|
if (CJK_RANGE.test(s)) return s.length >= 2
|
|
272
|
+
// 俄语:作者写的意图词是**原形**,不经过检索侧的词干;只要 ≥3 字符、不是虚词、
|
|
273
|
+
// 不含路径/标识符字符即可。不能用拉丁的 ≥5:`мост` / `код` / `файл` 都是 3–4 字母的实义词,
|
|
274
|
+
// 而俄语的三字母词几乎都是实词(虚词已经被 RU_STOPWORDS 挡掉),没有拉丁那种缩写歧义。
|
|
275
|
+
if (CYRILLIC_RANGE.test(s)) {
|
|
276
|
+
const w = normalizeRuWord(s)
|
|
277
|
+
return w.length >= 3 && !RU_STOPWORDS.has(w) && !/[_./\\*]/.test(w) && !/\s/.test(w)
|
|
278
|
+
}
|
|
235
279
|
if (s.length < 5) return false
|
|
236
280
|
if (/[_./\\*]/.test(s)) return false
|
|
237
281
|
if (/\s/.test(s)) return s.length >= 8
|
|
@@ -258,8 +302,15 @@ export function matchIntent(word, text) {
|
|
|
258
302
|
const hay = String(text || '')
|
|
259
303
|
if (!w || !hay) return false
|
|
260
304
|
if (CJK_RANGE.test(w)) return w.length >= 2 && hay.includes(w)
|
|
261
|
-
if (w.length < 4) return false
|
|
262
305
|
const esc = w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
306
|
+
// 俄语用 Unicode 词边界:`[^A-Za-z0-9_]` 会把"俄文词内部的命中"当成边界
|
|
307
|
+
// (`город` 命中 `городской`),那正是这条匹配要防的假阳性。
|
|
308
|
+
// 长度门槛与 isIntentWord 保持一致(≥3),不套用拉丁的 ≥4。
|
|
309
|
+
if (CYRILLIC_RANGE.test(w)) {
|
|
310
|
+
if (w.length < 3) return false
|
|
311
|
+
return new RegExp(`(^|[^\\p{L}\\p{N}_])${esc}(?![\\p{L}\\p{N}_])`, 'iu').test(hay)
|
|
312
|
+
}
|
|
313
|
+
if (w.length < 4) return false
|
|
263
314
|
return new RegExp(`(^|[^A-Za-z0-9_])${esc}([^A-Za-z0-9_]|$)`, 'i').test(hay)
|
|
264
315
|
}
|
|
265
316
|
|
package/src/recall.js
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* @module dsh-project-memory/recall
|
|
19
19
|
*/
|
|
20
20
|
import { rankEntriesMergedScored, rankEntriesStreaming, tokenizeRaw, CJK_RANGE } from './util/search.js'
|
|
21
|
+
import { CYRILLIC_RANGE } from './util/ru.js'
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
* 短拉丁片段不算检索证据(只用于经验/洞察两层)。
|
|
@@ -28,11 +29,14 @@ import { rankEntriesMergedScored, rankEntriesStreaming, tokenizeRaw, CJK_RANGE }
|
|
|
28
29
|
*
|
|
29
30
|
* 为什么只在这两层:doc/symbol 层是代码与路径,`js` / `id` 这类短标识符本来就该命中
|
|
30
31
|
* (见上方 `matchedFloor` 的注释)。两种模式(有/无向量)一律适用——闸门不能随向量层开关变形。
|
|
32
|
+
*
|
|
33
|
+
* 西里尔(俄语)不吃这条规则:俄语没有"两字母缩写"这种噪声源,且 `ии` / `бд` 这类
|
|
34
|
+
* 两字母词是实义词;虚词在分词阶段已经由 `RU_STOPWORDS` 去掉了,这里再按长度砍只会误伤。
|
|
31
35
|
*/
|
|
32
36
|
const MIN_LATIN_QUERY_TERM = 3
|
|
33
37
|
function gatedQueryText(q) {
|
|
34
38
|
return tokenizeRaw(String(q ?? ''))
|
|
35
|
-
.filter((t) => CJK_RANGE.test(t) || t.length >= MIN_LATIN_QUERY_TERM)
|
|
39
|
+
.filter((t) => CJK_RANGE.test(t) || CYRILLIC_RANGE.test(t) || t.length >= MIN_LATIN_QUERY_TERM)
|
|
36
40
|
.join(' ')
|
|
37
41
|
}
|
|
38
42
|
|
package/src/setup/taskbridge.js
CHANGED
|
@@ -44,13 +44,13 @@ export const NO_PROJECT_ROOT_NOTE =
|
|
|
44
44
|
* "会话工作目录"归属,没有标记的普通目录也要能用);cwd 是家目录/系统/包管理器前缀
|
|
45
45
|
* 且上方没有标记时返回 null。
|
|
46
46
|
*/
|
|
47
|
-
export function projectRootFor(cwd) {
|
|
48
|
-
return resolveProjectMemoryRoot(cwd || process.cwd())
|
|
47
|
+
export function projectRootFor(cwd, config) {
|
|
48
|
+
return resolveProjectMemoryRoot(cwd || process.cwd(), config)
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/** 没有可用项目根时返回 `{ root: null, store: null }`,调用方据此整段跳过。 */
|
|
52
52
|
export function taskStoreFor(cwd, config) {
|
|
53
|
-
const root = projectRootFor(cwd)
|
|
53
|
+
const root = projectRootFor(cwd, config)
|
|
54
54
|
if (!root) return { root: null, store: null }
|
|
55
55
|
return { root, store: new ProjectMemoryStore(memoryRootFor(root, config.memoryDir)).load() }
|
|
56
56
|
}
|
package/src/skill.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// 把"怎么用 project-memory"作为宿主 skill 暴露,而不是文档里的一段说明。
|
|
2
|
+
//
|
|
3
|
+
// 为什么不写 `.dsh/skills/`:那会把文件写进用户项目或全局 home,且与插件版本脱钩
|
|
4
|
+
// (升级插件后 skill 还是旧的)。运行时注册随包升级,`resourceBase` 指向包内目录,
|
|
5
|
+
// 模型能按相对路径取 `scripts/pm-digest.mjs`。
|
|
6
|
+
//
|
|
7
|
+
// 宿主没有 `skills` 服务时静默跳过 —— 与 `/tasks` 同一套软依赖写法(顶层 inject 会让整个
|
|
8
|
+
// 半边在缺服务时**不挂载**,那是最坏的失败形态,见 src/client/services.ts 的同类注释)。
|
|
9
|
+
import { readFileSync } from 'node:fs'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import { fileURLToPath } from 'node:url'
|
|
12
|
+
|
|
13
|
+
export const SKILL_NAME = 'project-memory'
|
|
14
|
+
/** 包内目录:`skills/project-memory/`(npm files 白名单里有 `skills`)。 */
|
|
15
|
+
export const SKILL_DIR = fileURLToPath(new URL('../skills/project-memory/', import.meta.url))
|
|
16
|
+
|
|
17
|
+
const FRONTMATTER = /^---\r?\n([\s\S]*?)\r?\n---\r?\n/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 拆 frontmatter 与正文。SKILL.md 是**唯一事实源**(name / description / 正文 / 资源目录),
|
|
21
|
+
* 注册时只解析它,避免"文件里一份、代码里一份"的漂移。
|
|
22
|
+
* @param {string} text
|
|
23
|
+
* @returns {{ meta: Record<string,string>, body: string }}
|
|
24
|
+
*/
|
|
25
|
+
export function parseSkillFile(text) {
|
|
26
|
+
const source = String(text || '')
|
|
27
|
+
const match = FRONTMATTER.exec(source)
|
|
28
|
+
const meta = {}
|
|
29
|
+
if (match) {
|
|
30
|
+
for (const line of match[1].split(/\r?\n/)) {
|
|
31
|
+
const at = line.indexOf(':')
|
|
32
|
+
if (at <= 0) continue
|
|
33
|
+
const key = line.slice(0, at).trim()
|
|
34
|
+
let value = line.slice(at + 1).trim()
|
|
35
|
+
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
|
|
36
|
+
value = value.slice(1, -1)
|
|
37
|
+
}
|
|
38
|
+
if (key) meta[key] = value
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { meta, body: match ? source.slice(match[0].length) : source }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 构造 `ctx.skills.register()` 接受的运行时 skill。
|
|
46
|
+
* @param {{ readFile?: (file: string, enc: string) => string, dir?: string }} [opts]
|
|
47
|
+
* @returns {{ name: string, description: string, whenToUse?: string, source: string, path: string, content: string, resourceBase: { kind: 'directory', path: string } }}
|
|
48
|
+
*/
|
|
49
|
+
export function buildProjectMemorySkill({ readFile = readFileSync, dir = SKILL_DIR } = {}) {
|
|
50
|
+
const file = path.join(dir, 'SKILL.md')
|
|
51
|
+
const { meta, body } = parseSkillFile(readFile(file, 'utf8'))
|
|
52
|
+
const skill = {
|
|
53
|
+
name: meta.name || SKILL_NAME,
|
|
54
|
+
description: meta.description || 'Recall and maintain this project\'s long-term memory: indexed docs and code symbols, lessons, decisions, procedures, and cross-session tasks.',
|
|
55
|
+
source: 'bundled',
|
|
56
|
+
path: file,
|
|
57
|
+
content: body.trim(),
|
|
58
|
+
resourceBase: { kind: 'directory', path: dir },
|
|
59
|
+
}
|
|
60
|
+
if (meta.whenToUse) skill.whenToUse = meta.whenToUse
|
|
61
|
+
return skill
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 把 skill 注册进宿主。`ctx.inject(['skills'], …)` 是可选依赖:没有该服务时回调不执行,
|
|
66
|
+
* 插件其余部分照常工作;注册失败只告警,不影响工具与注入。
|
|
67
|
+
* @param {object} ctx
|
|
68
|
+
* @param {{ build?: () => object, log?: { error: Function } }} [opts]
|
|
69
|
+
*/
|
|
70
|
+
export function setupSkill(ctx, { build = buildProjectMemorySkill, log = console } = {}) {
|
|
71
|
+
const register = (skillsCtx) => {
|
|
72
|
+
try {
|
|
73
|
+
skillsCtx.skills.register(build())
|
|
74
|
+
} catch (err) {
|
|
75
|
+
log.error(`[dsh-project-memory] skill registration skipped: ${err?.message || err}`)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
if (typeof ctx.inject === 'function') ctx.inject(['skills'], register)
|
|
80
|
+
else if (typeof ctx.get === 'function' && ctx.get('skills')) register(ctx)
|
|
81
|
+
} catch (err) {
|
|
82
|
+
log.error(`[dsh-project-memory] skill registration skipped: ${err?.message || err}`)
|
|
83
|
+
}
|
|
84
|
+
}
|