@restregular/dsh-memory 0.34.1 → 0.35.0
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 +15 -0
- package/README.md +36 -1
- package/lib/client.js +381 -15
- package/lib/index.js +6 -1
- package/lib/inject/apply.js +43 -12
- package/lib/panel.js +1717 -1616
- package/lib/provider/apply.js +54 -0
- package/lib/provider/approval-audit.js +1 -1
- package/lib/provider/hooks.js +597 -0
- package/lib/provider/index.js +292 -3
- package/lib/skills.js +150 -0
- package/lib/tools/apply.js +6 -1
- package/lib/tools/index.js +436 -1
- package/lib/tools/render.js +78 -1
- package/package.json +2 -1
- package/skills/memory-hook-scripts/SKILL.md +142 -0
- package/skills/memory-hook-scripts/references/examples.md +159 -0
package/lib/tools/index.js
CHANGED
|
@@ -305,6 +305,74 @@ import { canonicalWorkspacePath, defaultScopeDescription, normalizeScopeInput, r
|
|
|
305
305
|
|
|
306
306
|
|
|
307
307
|
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
// ---------------------------------------------------------------- 记忆钩子(0.35.0)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
/** provider 的一条钩子(代码会随 list/get 返回,但**不**进记忆检索)。 */
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
/** 一次钩子运行的结果(试跑与真实运行同形)。 */
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
308
376
|
|
|
309
377
|
|
|
310
378
|
|
|
@@ -867,6 +935,15 @@ function resolveScope(
|
|
|
867
935
|
|
|
868
936
|
|
|
869
937
|
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
870
947
|
|
|
871
948
|
|
|
872
949
|
|
|
@@ -877,6 +954,8 @@ function resolveScope(
|
|
|
877
954
|
|
|
878
955
|
|
|
879
956
|
|
|
957
|
+
|
|
958
|
+
|
|
880
959
|
|
|
881
960
|
|
|
882
961
|
/**
|
|
@@ -909,6 +988,19 @@ export function memorySaveTool(opts )
|
|
|
909
988
|
triggers: { type: 'array', items: { type: 'string' }, description: '流程触发词(kind=procedure):工具名/关键词/路径;缺省用 tags' },
|
|
910
989
|
scopeDescription: { type: 'string', description: '新建自定义作用域时必填:一句话说明这个作用域用来做什么(已有作用域带上它 = 更新用途)' },
|
|
911
990
|
scopeRule: { type: 'string', description: '初始化时可选写一条初始规则(kind=rule):只对新自定义作用域生效,用于约定该作用域只记录什么、不记录什么' },
|
|
991
|
+
hookCode: {
|
|
992
|
+
type: 'string',
|
|
993
|
+
description: '给这条规则附带的校验脚本源码(0.35.0):同步函数,按 hookPhase 在对应时机运行。'
|
|
994
|
+
+ '代码存在 hooks.json,不进记忆检索;首次保存会向用户申请批准一次。'
|
|
995
|
+
+ '写脚本前先加载技能 memory-hook-scripts(`skill` 工具,名字就叫 memory-hook-scripts)——它写了入口函数名、可用 API、返回约定与现成范例。',
|
|
996
|
+
},
|
|
997
|
+
hookPhase: {
|
|
998
|
+
type: 'string',
|
|
999
|
+
enum: ['before-save', 'after-save', 'before-inject'],
|
|
1000
|
+
description: '脚本运行时机(默认 before-save):before-save=保存前校验,返回 {ok:false} 直接拒绝这次保存;'
|
|
1001
|
+
+ 'after-save=保存后观察,只能附注不阻断;before-inject=注入前解析,可改写正文或 drop 掉整条。',
|
|
1002
|
+
},
|
|
1003
|
+
hookName: { type: 'string', description: '脚本的显示名(面板与审批卡用);缺省取内容前 24 字' },
|
|
912
1004
|
description: { type: 'string', description: DESCRIPTION_PARAM_DESCRIPTION },
|
|
913
1005
|
},
|
|
914
1006
|
async execute(args , exec ) {
|
|
@@ -1017,7 +1109,29 @@ export function memorySaveTool(opts )
|
|
|
1017
1109
|
: {}),
|
|
1018
1110
|
source: { origin: 'explicit' },
|
|
1019
1111
|
})
|
|
1020
|
-
|
|
1112
|
+
// 0.35.0:带代码的规则 —— 记忆先落盘(脚本可能正是用来校验别人写的记忆),
|
|
1113
|
+
// 再登记钩子并申请一次批准。登记失败不该让保存变成"失败",只附一句告知。
|
|
1114
|
+
let hookInfo
|
|
1115
|
+
const hookCode = typeof args.hookCode === 'string' ? args.hookCode.trim() : ''
|
|
1116
|
+
if (hookCode !== '') {
|
|
1117
|
+
hookInfo = await registerHookForSave(service, agent, exec, {
|
|
1118
|
+
code: hookCode,
|
|
1119
|
+
phase: args.hookPhase ?? 'before-save',
|
|
1120
|
+
name: typeof args.hookName === 'string' && args.hookName.trim() !== ''
|
|
1121
|
+
? args.hookName.trim()
|
|
1122
|
+
: content.replace(/\s+/g, ' ').slice(0, 24),
|
|
1123
|
+
ruleId: record.id,
|
|
1124
|
+
scopes: [record.scope],
|
|
1125
|
+
description: args.description,
|
|
1126
|
+
})
|
|
1127
|
+
}
|
|
1128
|
+
return {
|
|
1129
|
+
id: record.id,
|
|
1130
|
+
scope: record.scope,
|
|
1131
|
+
...(alias !== undefined ? { scopeName: alias } : {}),
|
|
1132
|
+
deduped: false,
|
|
1133
|
+
...(hookInfo !== undefined ? { hook: hookInfo } : {}),
|
|
1134
|
+
}
|
|
1021
1135
|
} catch (error) {
|
|
1022
1136
|
throw toolError(error)
|
|
1023
1137
|
}
|
|
@@ -1025,6 +1139,57 @@ export function memorySaveTool(opts )
|
|
|
1025
1139
|
}
|
|
1026
1140
|
}
|
|
1027
1141
|
|
|
1142
|
+
/**
|
|
1143
|
+
* 登记一条随记忆保存的钩子,并向用户申请一次批准。
|
|
1144
|
+
*
|
|
1145
|
+
* 顺序很关键:**先保存钩子(未批准)再申请审批**。反过来的话,用户批准时
|
|
1146
|
+
* 钩子还不存在,批准就无处可记;而且 `requireApproval=false` 时也不需要弹窗。
|
|
1147
|
+
*/
|
|
1148
|
+
async function registerHookForSave(
|
|
1149
|
+
service ,
|
|
1150
|
+
agent ,
|
|
1151
|
+
exec ,
|
|
1152
|
+
input
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
,
|
|
1160
|
+
) {
|
|
1161
|
+
if (typeof service.saveHook !== 'function') {
|
|
1162
|
+
return { id: '', name: input.name, phase: input.phase, approval: 'unsupported' }
|
|
1163
|
+
}
|
|
1164
|
+
let hook
|
|
1165
|
+
try {
|
|
1166
|
+
hook = service.saveHook({
|
|
1167
|
+
name: input.name,
|
|
1168
|
+
phase: input.phase,
|
|
1169
|
+
code: input.code,
|
|
1170
|
+
scopes: input.scopes,
|
|
1171
|
+
ruleId: input.ruleId,
|
|
1172
|
+
createdBy: 'agent',
|
|
1173
|
+
})
|
|
1174
|
+
} catch (error) {
|
|
1175
|
+
return { id: '', name: input.name, phase: input.phase, approval: `save-failed: ${toolError(error).message}` }
|
|
1176
|
+
}
|
|
1177
|
+
let approval = 'not-needed'
|
|
1178
|
+
if (typeof service.requestHookApproval === 'function') {
|
|
1179
|
+
try {
|
|
1180
|
+
approval = await service.requestHookApproval(agent, hook.id, {
|
|
1181
|
+
...(input.description !== undefined ? { description: input.description } : {}),
|
|
1182
|
+
...((exec )?.signal !== undefined
|
|
1183
|
+
? { signal: (exec ).signal }
|
|
1184
|
+
: {}),
|
|
1185
|
+
})
|
|
1186
|
+
} catch {
|
|
1187
|
+
approval = 'unavailable'
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return { id: hook.id, name: hook.name, phase: hook.phase, approval }
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1028
1193
|
/** memory_search / memory_get(summary) 的默认摘要长度(字符)。 */
|
|
1029
1194
|
export const DEFAULT_SUMMARY_CHARS = 160
|
|
1030
1195
|
|
|
@@ -2127,6 +2292,275 @@ export function memoryBlockTool(opts )
|
|
|
2127
2292
|
}
|
|
2128
2293
|
}
|
|
2129
2294
|
|
|
2295
|
+
/** `memory_hooks` 支持的 action。 */
|
|
2296
|
+
|
|
2297
|
+
|
|
2298
|
+
|
|
2299
|
+
|
|
2300
|
+
|
|
2301
|
+
|
|
2302
|
+
|
|
2303
|
+
|
|
2304
|
+
|
|
2305
|
+
|
|
2306
|
+
|
|
2307
|
+
|
|
2308
|
+
|
|
2309
|
+
|
|
2310
|
+
|
|
2311
|
+
|
|
2312
|
+
|
|
2313
|
+
|
|
2314
|
+
|
|
2315
|
+
|
|
2316
|
+
|
|
2317
|
+
|
|
2318
|
+
|
|
2319
|
+
|
|
2320
|
+
|
|
2321
|
+
|
|
2322
|
+
|
|
2323
|
+
|
|
2324
|
+
|
|
2325
|
+
|
|
2326
|
+
|
|
2327
|
+
|
|
2328
|
+
|
|
2329
|
+
/** 按 id 或唯一前缀找钩子(模型看不到完整 uuid 时也能操作)。 */
|
|
2330
|
+
function findHook(service , raw ) {
|
|
2331
|
+
const needle = raw.trim()
|
|
2332
|
+
if (needle === '') throw new MemoryToolError('HOOK_ID_REQUIRED', '需要 id:先用 action="list" 看现有钩子')
|
|
2333
|
+
if (typeof service.listHooks !== 'function') {
|
|
2334
|
+
throw new MemoryToolError('HOOKS_UNSUPPORTED', '当前 memory provider 不支持记忆钩子(请升级 @restregular/dsh-memory)')
|
|
2335
|
+
}
|
|
2336
|
+
const all = service.listHooks()
|
|
2337
|
+
const exact = all.find((hook) => hook.id === needle)
|
|
2338
|
+
if (exact !== undefined) return exact
|
|
2339
|
+
const matches = all.filter((hook) => hook.id.startsWith(needle) || hook.name === needle)
|
|
2340
|
+
if (matches.length === 1) return matches[0]
|
|
2341
|
+
if (matches.length === 0) throw new MemoryToolError('HOOK_NOT_FOUND', `没有找到钩子 ${needle}`)
|
|
2342
|
+
throw new MemoryToolError('HOOK_AMBIGUOUS', `前缀 ${needle} 命中 ${matches.length} 个钩子,请给更长的 id`)
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
const HOOKS_TOOL_DESCRIPTION =
|
|
2346
|
+
'Manage executable memory hooks: scripts attached to rule memories that the plugin runs at a fixed phase. '
|
|
2347
|
+
+ 'before-save validates a memory and can REJECT the save; after-save only observes; before-inject parses text before it enters context. '
|
|
2348
|
+
+ 'Hook code lives in hooks.json and is NEVER part of memory search/list/digest. '
|
|
2349
|
+
+ 'Load the `memory-hook-scripts` skill (via the `skill` tool) before writing code — it documents the entry-function names, the sandbox API, the return contract and ready-made examples.'
|
|
2350
|
+
|
|
2351
|
+
export function memoryHooksTool(opts ) {
|
|
2352
|
+
return {
|
|
2353
|
+
name: 'memory_hooks',
|
|
2354
|
+
description: HOOKS_TOOL_DESCRIPTION,
|
|
2355
|
+
parameters: {
|
|
2356
|
+
action: {
|
|
2357
|
+
type: 'string',
|
|
2358
|
+
enum: ['list', 'get', 'save', 'delete', 'approve', 'enable', 'disable', 'run'],
|
|
2359
|
+
required: true,
|
|
2360
|
+
description: 'list=列全部(不含代码);get=读代码;save=新建/更新;delete=删除;approve=请用户批准代码(首次或改代码后);'
|
|
2361
|
+
+ 'enable/disable=开关;run=拿一段正文试跑(不写记忆)。',
|
|
2362
|
+
},
|
|
2363
|
+
id: { type: 'string', description: '钩子 id(或唯一前缀 / 名字);get/delete/approve/enable/disable/run 必填' },
|
|
2364
|
+
code: { type: 'string', description: '脚本源码(save 必填)。入口函数名与返回约定见技能 memory-hook-scripts。' },
|
|
2365
|
+
phase: {
|
|
2366
|
+
type: 'string',
|
|
2367
|
+
enum: ['before-save', 'after-save', 'before-inject'],
|
|
2368
|
+
description: '运行时机;省略 = 更新时沿用旧值,新建默认 before-save。',
|
|
2369
|
+
},
|
|
2370
|
+
name: { type: 'string', description: '显示名(面板与审批卡用)' },
|
|
2371
|
+
scopes: { type: 'array', items: { type: 'string' }, description: '适用作用域键;省略 = 全部适用(更新时保留旧值)' },
|
|
2372
|
+
kinds: { type: 'array', items: { type: 'string' }, description: '适用记忆类型(fact/rule/...);省略 = 全部适用' },
|
|
2373
|
+
content: { type: 'string', description: 'run 试跑用的正文(只跑脚本,不写记忆)' },
|
|
2374
|
+
description: { type: 'string', description: DESCRIPTION_PARAM_DESCRIPTION },
|
|
2375
|
+
},
|
|
2376
|
+
async execute(args , exec ) {
|
|
2377
|
+
const service = svc(opts)
|
|
2378
|
+
const agent = agentOf(exec)
|
|
2379
|
+
requireDescription(service, agent, args.description)
|
|
2380
|
+
if (typeof service.listHooks !== 'function') {
|
|
2381
|
+
throw new MemoryToolError('HOOKS_UNSUPPORTED', '当前 memory provider 不支持记忆钩子(请升级 @restregular/dsh-memory)')
|
|
2382
|
+
}
|
|
2383
|
+
const action = args.action
|
|
2384
|
+
const settings = typeof service.hookSettings === 'function' ? service.hookSettings() : undefined
|
|
2385
|
+
const withApproval = (hook ) =>
|
|
2386
|
+
typeof service.hookPendingApproval === 'function'
|
|
2387
|
+
? { ...hook, pendingApproval: service.hookPendingApproval(hook.id) }
|
|
2388
|
+
: hook
|
|
2389
|
+
|
|
2390
|
+
if (action === 'list') {
|
|
2391
|
+
return {
|
|
2392
|
+
action,
|
|
2393
|
+
hooks: service.listHooks().map(withApproval),
|
|
2394
|
+
...(settings !== undefined ? { settings } : {}),
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
|
|
2398
|
+
if (action === 'get') {
|
|
2399
|
+
const hook = findHook(service, args.id ?? '')
|
|
2400
|
+
return { action, hooks: [withApproval(hook)], ...(settings !== undefined ? { settings } : {}) }
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
if (action === 'save') {
|
|
2404
|
+
const code = typeof args.code === 'string' ? args.code : ''
|
|
2405
|
+
if (code.trim() === '') throw new MemoryToolError('HOOK_CODE_REQUIRED', 'save 需要 code(脚本源码)')
|
|
2406
|
+
if (typeof service.saveHook !== 'function') {
|
|
2407
|
+
throw new MemoryToolError('HOOKS_UNSUPPORTED', '当前 memory provider 不支持记忆钩子(请升级 @restregular/dsh-memory)')
|
|
2408
|
+
}
|
|
2409
|
+
// 更新时省略的字段沿用旧值:脚本改了但作用域不想动是最常见的情况。
|
|
2410
|
+
const existing = args.id !== undefined && args.id !== '' ? findHook(service, args.id) : undefined
|
|
2411
|
+
const phase = args.phase ?? existing?.phase ?? 'before-save'
|
|
2412
|
+
let saved
|
|
2413
|
+
try {
|
|
2414
|
+
saved = service.saveHook({
|
|
2415
|
+
...(existing !== undefined ? { id: existing.id } : {}),
|
|
2416
|
+
...(args.name !== undefined ? { name: args.name } : existing !== undefined ? { name: existing.name } : {}),
|
|
2417
|
+
phase,
|
|
2418
|
+
code,
|
|
2419
|
+
...(args.scopes !== undefined ? { scopes: args.scopes.map(String) } : { scopes: existing?.scopes ?? [] }),
|
|
2420
|
+
...(args.kinds !== undefined ? { kinds: args.kinds.map(String) } : { kinds: existing?.kinds ?? [] }),
|
|
2421
|
+
...(args.description !== undefined ? { description: args.description } : {}),
|
|
2422
|
+
createdBy: 'agent',
|
|
2423
|
+
...(existing?.ruleId !== undefined ? { ruleId: existing.ruleId } : {}),
|
|
2424
|
+
})
|
|
2425
|
+
} catch (error) {
|
|
2426
|
+
throw toolError(error)
|
|
2427
|
+
}
|
|
2428
|
+
// 保存即申请批准:代码改过 / 首次创建都要用户点头。
|
|
2429
|
+
let approval = 'not-needed'
|
|
2430
|
+
if (typeof service.requestHookApproval === 'function') {
|
|
2431
|
+
try {
|
|
2432
|
+
approval = await service.requestHookApproval(agent, saved.id, {
|
|
2433
|
+
...(args.description !== undefined ? { description: args.description } : {}),
|
|
2434
|
+
...((exec )?.signal !== undefined
|
|
2435
|
+
? { signal: (exec ).signal }
|
|
2436
|
+
: {}),
|
|
2437
|
+
})
|
|
2438
|
+
} catch {
|
|
2439
|
+
approval = 'unavailable'
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
return {
|
|
2443
|
+
action,
|
|
2444
|
+
hooks: [withApproval(saved)],
|
|
2445
|
+
...(settings !== undefined ? { settings } : {}),
|
|
2446
|
+
note: approvalNote(approval),
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
|
|
2450
|
+
if (action === 'delete') {
|
|
2451
|
+
const hook = findHook(service, args.id ?? '')
|
|
2452
|
+
if (typeof service.deleteHook !== 'function') {
|
|
2453
|
+
throw new MemoryToolError('HOOKS_UNSUPPORTED', '当前 memory provider 不支持记忆钩子')
|
|
2454
|
+
}
|
|
2455
|
+
const deleted = service.deleteHook(hook.id)
|
|
2456
|
+
return {
|
|
2457
|
+
action,
|
|
2458
|
+
hooks: service.listHooks().map(withApproval),
|
|
2459
|
+
removed: deleted ? 1 : 0,
|
|
2460
|
+
note: deleted ? `已删除钩子 ${hook.name}。` : '这个钩子已经不存在了。',
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2464
|
+
if (action === 'approve') {
|
|
2465
|
+
const hook = findHook(service, args.id ?? '')
|
|
2466
|
+
if (typeof service.requestHookApproval !== 'function') {
|
|
2467
|
+
throw new MemoryToolError('HOOKS_UNSUPPORTED', '当前 memory provider 不支持记忆钩子审批')
|
|
2468
|
+
}
|
|
2469
|
+
let outcome
|
|
2470
|
+
try {
|
|
2471
|
+
outcome = await service.requestHookApproval(agent, hook.id, {
|
|
2472
|
+
...(args.description !== undefined ? { description: args.description } : {}),
|
|
2473
|
+
...((exec )?.signal !== undefined
|
|
2474
|
+
? { signal: (exec ).signal }
|
|
2475
|
+
: {}),
|
|
2476
|
+
})
|
|
2477
|
+
} catch {
|
|
2478
|
+
outcome = 'unavailable'
|
|
2479
|
+
}
|
|
2480
|
+
const fresh = typeof service.getHook === 'function' ? service.getHook(hook.id) : undefined
|
|
2481
|
+
return {
|
|
2482
|
+
action,
|
|
2483
|
+
hooks: fresh !== undefined ? [withApproval(fresh)] : [],
|
|
2484
|
+
note: approvalNote(outcome),
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
|
|
2488
|
+
if (action === 'enable' || action === 'disable') {
|
|
2489
|
+
const hook = findHook(service, args.id ?? '')
|
|
2490
|
+
if (typeof service.saveHook !== 'function') {
|
|
2491
|
+
throw new MemoryToolError('HOOKS_UNSUPPORTED', '当前 memory provider 不支持记忆钩子')
|
|
2492
|
+
}
|
|
2493
|
+
const saved = service.saveHook({
|
|
2494
|
+
id: hook.id,
|
|
2495
|
+
name: hook.name,
|
|
2496
|
+
phase: hook.phase,
|
|
2497
|
+
code: hook.code,
|
|
2498
|
+
enabled: action === 'enable',
|
|
2499
|
+
scopes: hook.scopes,
|
|
2500
|
+
kinds: hook.kinds,
|
|
2501
|
+
...(hook.description !== undefined ? { description: hook.description } : {}),
|
|
2502
|
+
...(hook.ruleId !== undefined ? { ruleId: hook.ruleId } : {}),
|
|
2503
|
+
})
|
|
2504
|
+
return {
|
|
2505
|
+
action,
|
|
2506
|
+
hooks: [withApproval(saved)],
|
|
2507
|
+
note: action === 'enable' ? `已启用「${hook.name}」。` : `已停用「${hook.name}」(代码保留,随时可再启用)。`,
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
|
|
2511
|
+
// run:拿一段正文试跑,不写记忆。这是唯一能"先看效果再上线"的入口。
|
|
2512
|
+
const hook = findHook(service, args.id ?? '')
|
|
2513
|
+
if (typeof service.runHooks !== 'function') {
|
|
2514
|
+
throw new MemoryToolError('HOOKS_UNSUPPORTED', '当前 memory provider 不支持记忆钩子试跑')
|
|
2515
|
+
}
|
|
2516
|
+
const content = typeof args.content === 'string' && args.content.trim() !== ''
|
|
2517
|
+
? args.content
|
|
2518
|
+
: throwErr(new MemoryToolError('HOOK_CONTENT_REQUIRED', 'run 需要 content:拿一段正文让脚本跑一次'))
|
|
2519
|
+
const outcomes = service.runHooks(hook.phase, {
|
|
2520
|
+
memory: {
|
|
2521
|
+
scope: (args.scopes ?? hook.scopes)[0] ?? 'global',
|
|
2522
|
+
kind: (args.kinds ?? hook.kinds)[0] ?? 'rule',
|
|
2523
|
+
content,
|
|
2524
|
+
tags: [],
|
|
2525
|
+
importance: 0.8,
|
|
2526
|
+
},
|
|
2527
|
+
tool: 'memory_hooks',
|
|
2528
|
+
})
|
|
2529
|
+
const mine = outcomes.filter((outcome) => outcome.hook.id === hook.id)
|
|
2530
|
+
const verdict = mine[0]
|
|
2531
|
+
const note = verdict === undefined
|
|
2532
|
+
? '脚本没有命中这次输入(作用域/类型过滤把它排除了)。'
|
|
2533
|
+
: verdict.error !== undefined
|
|
2534
|
+
? `脚本运行失败:${verdict.error}`
|
|
2535
|
+
: verdict.ok
|
|
2536
|
+
? '脚本判定:通过。'
|
|
2537
|
+
: `脚本判定:不通过 —— ${verdict.message ?? '(脚本没给 message)'}`
|
|
2538
|
+
return { action, hooks: [withApproval(hook)], outcomes: mine, note }
|
|
2539
|
+
},
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
|
|
2543
|
+
/** 抛出一个值(给不能在表达式位置写 throw 的地方用)。 */
|
|
2544
|
+
function throwErr(error ) {
|
|
2545
|
+
throw error
|
|
2546
|
+
}
|
|
2547
|
+
|
|
2548
|
+
/** 审批结局 → 给模型的一句话。 */
|
|
2549
|
+
function approvalNote(outcome ) {
|
|
2550
|
+
switch (outcome) {
|
|
2551
|
+
case 'approved':
|
|
2552
|
+
return '用户已批准这段代码,从现在起会按这个时机运行。'
|
|
2553
|
+
case 'not-needed':
|
|
2554
|
+
return '这段代码之前已经批准过(同一份代码不会重复打扰用户)。'
|
|
2555
|
+
case 'rejected':
|
|
2556
|
+
return '用户拒绝运行这段代码:钩子已保存但**处于未批准状态,不会运行**。改脚本或让用户批准后才能生效。'
|
|
2557
|
+
case 'timeout':
|
|
2558
|
+
return '等待用户批准超时:钩子已保存但**不会运行**,需要用户批准后才生效。'
|
|
2559
|
+
default:
|
|
2560
|
+
return '当前环境无法发起代码审批:钩子已保存但**不会运行**。'
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
|
|
2130
2564
|
export function memoryTools(opts ) {
|
|
2131
2565
|
return {
|
|
2132
2566
|
memory_save: memorySaveTool(opts),
|
|
@@ -2136,6 +2570,7 @@ export function memoryTools(opts ) {
|
|
|
2136
2570
|
memory_scopes: memoryScopesTool(opts),
|
|
2137
2571
|
memory_get: memoryGetTool(opts),
|
|
2138
2572
|
memory_block: memoryBlockTool(opts),
|
|
2573
|
+
memory_hooks: memoryHooksTool(opts),
|
|
2139
2574
|
}
|
|
2140
2575
|
}
|
|
2141
2576
|
|
package/lib/tools/render.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* 0.28.0:范围外清单只给作用域(名字 / 条数 / 用途),**不再给条目摘要**;清单与
|
|
14
14
|
* withheld 说明不再重复讲同一件事(「索引已列在下方」那句直接去掉)。
|
|
15
15
|
*/
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
import { scopeDisplayName } from '../provider/scope.js'
|
|
18
18
|
|
|
19
19
|
/** 把 scope 键渲染成展示文案(宿主能查到会话标题时用它,否则用短 id)。 */
|
|
@@ -337,5 +337,82 @@ export function renderBlockResult(value , label )
|
|
|
337
337
|
return `${head}${records}${note}\n(屏蔽不删除任何记忆,数据原样保留;解除后立刻复原。)`
|
|
338
338
|
}
|
|
339
339
|
|
|
340
|
+
/**
|
|
341
|
+
* memory_hooks 的渲染(0.35.0)。
|
|
342
|
+
*
|
|
343
|
+
* 三件事必须让模型看懂,否则它会误判「脚本已经生效」:
|
|
344
|
+
* 1. 代码是否已获用户批准(未批准 = 不会运行);
|
|
345
|
+
* 2. 运行时机与适用范围;
|
|
346
|
+
* 3. 最近的运行结局(校验器坏了 / 一直在拒,都要一眼看见)。
|
|
347
|
+
*
|
|
348
|
+
* 代码正文**不在 list 里打印**(那是 get 的事):一次列十几段脚本会把上下文撑爆。
|
|
349
|
+
*/
|
|
350
|
+
export function renderHooksResult(value , label ) {
|
|
351
|
+
const phaseText = {
|
|
352
|
+
'before-save': '保存前校验(可拒绝保存)',
|
|
353
|
+
'after-save': '保存后观察(不阻断)',
|
|
354
|
+
'before-inject': '注入前解析(可改写/丢弃)',
|
|
355
|
+
}
|
|
356
|
+
const pending = (hook ) => hook.pendingApproval === true
|
|
357
|
+
|
|
358
|
+
const describe = (hook ) => {
|
|
359
|
+
const parts = [`[${shortId(hook.id)}] ${hook.name}`, phaseText[hook.phase] ?? hook.phase]
|
|
360
|
+
if (!hook.enabled) parts.push('已停用')
|
|
361
|
+
if (pending(hook)) parts.push('⏳ 待用户批准(不会运行)')
|
|
362
|
+
if (hook.scopes.length > 0) parts.push(`作用域 ${hook.scopes.map(label).join('/')}`)
|
|
363
|
+
if (hook.kinds.length > 0) parts.push(`类型 ${hook.kinds.join('/')}`)
|
|
364
|
+
if (hook.runCount > 0) parts.push(`运行 ${hook.runCount}${hook.rejectCount > 0 ? ` / 拒 ${hook.rejectCount}` : ''}${hook.errorCount > 0 ? ` / 错 ${hook.errorCount}` : ''}`)
|
|
365
|
+
if (hook.lastOutcome === 'error') parts.push(`最近出错:${hook.lastMessage ?? '(无说明)'}`)
|
|
366
|
+
else if (hook.lastOutcome === 'rejected') parts.push(`最近拒绝:${hook.lastMessage ?? '(无说明)'}`)
|
|
367
|
+
return `- ${parts.join(' · ')}`
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const settingsLine = () => {
|
|
371
|
+
const s = value.settings
|
|
372
|
+
if (s === undefined) return ''
|
|
373
|
+
const state = s.enabled ? '开启' : '关闭'
|
|
374
|
+
return `钩子总开关:${state};超时 ${s.timeoutMs}ms;带代码的规则${s.requireApproval ? '需要用户批准一次' : '不要求批准(已关闭审批)'}。`
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (value.action === 'get') {
|
|
378
|
+
const hook = value.hooks[0]
|
|
379
|
+
if (hook === undefined) return '这个钩子不存在。'
|
|
380
|
+
const head = describe(hook)
|
|
381
|
+
const body = hook.code.split('\n').map((line, index) => `${String(index + 1).padStart(3, ' ')}| ${line}`).join('\n')
|
|
382
|
+
const tail = pending(hook) ? '\n\n⚠ 这段代码还没被用户批准,**当前不会运行**。' : ''
|
|
383
|
+
return `${head}\n\n\`\`\`js\n${body}\n\`\`\`${tail}`
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (value.action === 'run') {
|
|
387
|
+
const hook = value.hooks[0]
|
|
388
|
+
const head = hook !== undefined ? describe(hook) : ''
|
|
389
|
+
const outcomes = value.outcomes ?? []
|
|
390
|
+
const detail = outcomes.length === 0
|
|
391
|
+
? '(脚本没有命中这次输入)'
|
|
392
|
+
: outcomes.map((outcome) => {
|
|
393
|
+
const lines = []
|
|
394
|
+
if (outcome.error !== undefined) lines.push(`运行失败:${outcome.error}`)
|
|
395
|
+
else lines.push(outcome.ok ? '判定:通过' : `判定:不通过${outcome.message !== undefined ? ` —— ${outcome.message}` : ''}`)
|
|
396
|
+
if (outcome.content !== undefined) lines.push(`改写后正文:${outcome.content.slice(0, 400)}`)
|
|
397
|
+
if (outcome.logs !== undefined && outcome.logs.length > 0) lines.push(`日志:${outcome.logs.join(' / ')}`)
|
|
398
|
+
lines.push(`耗时 ${outcome.durationMs}ms`)
|
|
399
|
+
return lines.join('\n')
|
|
400
|
+
}).join('\n')
|
|
401
|
+
return [head, detail, value.note ?? ''].filter((part) => part !== '').join('\n')
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const list = value.hooks
|
|
405
|
+
if (list.length === 0) {
|
|
406
|
+
return [value.note ?? '', '当前没有任何记忆钩子。']
|
|
407
|
+
.filter((part) => part !== '')
|
|
408
|
+
.concat('在 memory_save 里带上 hookCode 就能随规则一起登记一段校验脚本。')
|
|
409
|
+
.join('\n')
|
|
410
|
+
}
|
|
411
|
+
const body = list.map(describe).join('\n')
|
|
412
|
+
const note = value.note !== undefined ? `${value.note}\n` : ''
|
|
413
|
+
const settings = settingsLine()
|
|
414
|
+
return `${body}${settings !== '' ? `\n\n${settings}` : ''}${note !== '' ? `\n${note}` : ''}`
|
|
415
|
+
}
|
|
416
|
+
|
|
340
417
|
|
|
341
418
|
//# sourceURL=source/tool-memory/src/render.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@restregular/dsh-memory",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.35.0",
|
|
4
4
|
"description": "DeepSeek Harness 记忆系统(单包单入口):JSONL 存储 + 本地向量检索(BGE-M3)+ 五个模型工具 + 自动召回注入 + 管理面板。",
|
|
5
5
|
"icon": "./icon.svg",
|
|
6
6
|
"type": "module",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"lib/",
|
|
16
16
|
"locale/",
|
|
17
|
+
"skills/",
|
|
17
18
|
"cordis.patch.yml",
|
|
18
19
|
"icon.svg",
|
|
19
20
|
"README.md",
|