@restregular/dsh-memory 0.34.1 → 0.35.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/lib/index.js CHANGED
@@ -21,6 +21,7 @@ import { apply as providerApply } from './provider/apply.js'
21
21
  import { apply as toolsApply, } from './tools/apply.js'
22
22
  import { apply as injectApply, } from './inject/apply.js'
23
23
  import { apply as panelApply } from './panel.js'
24
+ import { mountBundledSkills } from './skills.js'
24
25
 
25
26
  export const name = 'dsh-memory'
26
27
 
@@ -60,7 +61,17 @@ export function apply(ctx , config = {}) {
60
61
  // 3) 面板服务端路由:内部 ctx.inject(['webServer']),没有 webServer 就只是没有 UI。
61
62
  part(ctx, 'panel', () => panelApply(ctx, config.panel ?? {}))
62
63
 
63
- // 4) 模型工具:同时需要工具注册表与本插件提供的 memory 服务。
64
+ // 4) 随包技能:记忆钩子的脚本 API 文档必须和代码同版本(0.35.0)。
65
+ // skills 服务由宿主核心提供,可能在本插件 apply 之后才提供 —— 用 ctx.inject
66
+ // 等它就绪再挂,不能在 apply 里 `ctx.get('skills')` 一次性取样(取到 undefined
67
+ // 就永久丢失,表现为「插件启用着但技能目录里没有」,重启或 toggle 才恢复)。
68
+ // 和 tools 一样放在独立 inject 回调里:技能是增强,缺它绝不能把整条 entry
69
+ // 拖成 PENDING(那会连 provider / 工具 / 面板一起起不来)。
70
+ ctx.inject(['skills'], (c ) => {
71
+ part(c, 'skills', () => mountBundledSkills(c ))
72
+ })
73
+
74
+ // 5) 模型工具:同时需要工具注册表与本插件提供的 memory 服务。
64
75
  // 用 ctx.inject 而不是模块级 inject —— 缺服务时只是这四个工具不出现,
65
76
  // 而不是让整条 entry 挂在 PENDING 上(那会连 provider 一起拖住)。
66
77
  ctx.inject(['tools', 'memory'], (c ) => {
@@ -116,6 +116,15 @@ import { canonicalWorkspacePath } from '../provider/scope.js'
116
116
 
117
117
                         
118
118
 
119
+
120
+                          
121
+                              
122
+
123
+
124
+
125
+
126
+
127
+
119
128
 
120
129
 
121
130
  /** 轮末提醒的文案:短、祈使句,不占预算。 */
@@ -400,22 +409,42 @@ export function apply(ctx , config = {}) {
400
409
  agent ,
401
410
  ) => {
402
411
  try {
403
- return {
404
- ok: true,
405
- render: renderRules(memory.listSync?.({
406
- scopes: [...scopes],
407
- kind: 'rule',
408
- limit: 100,
409
- ...(agent !== undefined ? { agent } : {}),
410
- // 0.32.0:规则段也走屏蔽过滤 —— 被屏蔽的规则不再每轮注入。
411
- ...(sessionIdOf(agent) !== undefined ? { sessionId: sessionIdOf(agent) } : {}),
412
- }) ?? [], cfg),
413
- }
412
+ const raw = memory.listSync?.({
413
+ scopes: [...scopes],
414
+ kind: 'rule',
415
+ limit: 100,
416
+ ...(agent !== undefined ? { agent } : {}),
417
+ // 0.32.0:规则段也走屏蔽过滤 —— 被屏蔽的规则不再每轮注入。
418
+ ...(sessionIdOf(agent) !== undefined ? { sessionId: sessionIdOf(agent) } : {}),
419
+ }) ?? []
420
+ // 0.35.0:注入前解析钩子 —— 规则段是"模型一定会读到"的地方,
421
+ // 让规则自带的脚本在这里改写摘要(比如补一句"违规示例")或整条丢弃。
422
+ return { ok: true, render: renderRules(withInjectHooks(memory, raw, agent), cfg) }
414
423
  } catch {
415
424
  return { ok: false, errorCode: 'source_load_failed' }
416
425
  }
417
426
  }
418
427
 
428
+ /**
429
+ * 把一批记录过一遍 before-inject 钩子(0.35.0)。
430
+ *
431
+ * provider 不支持(老版本)或钩子关掉时原样返回 —— 注入链路不能因为钩子不可用
432
+ * 就少注入记忆。解析失败也只是"没解析",记录原样进上下文。
433
+ */
434
+ const withInjectHooks = (
435
+ memory ,
436
+ records ,
437
+ agent ,
438
+ ) => {
439
+ if (records.length === 0 || typeof memory.parseForInject !== 'function') return records
440
+ try {
441
+ const parsed = memory.parseForInject(records, sessionIdOf(agent))
442
+ return parsed.records
443
+ } catch {
444
+ return records
445
+ }
446
+ }
447
+
419
448
  const rememberCallMeta = (callId , name , description ) => {
420
449
  if (typeof callId !== 'string' || callId === '') return
421
450
  callMeta.set(callId, { name, ...(description !== undefined ? { description } : {}) })
@@ -1121,7 +1150,9 @@ export function apply(ctx , config = {}) {
1121
1150
  if (settings.digest.enabled) {
1122
1151
  let digestStats
1123
1152
  const outcome = await digester.maybeDigestOutcome(sessionId, scopes, (records) => {
1124
- const rendered = renderDigest(records, settings.digest)
1153
+ // 0.35.0:摘要也走注入前解析 —— 正文可能被规则脚本改写,
1154
+ // 所以渲染的必须是解析后的那一份,否则改了等于没改。
1155
+ const rendered = renderDigest(withInjectHooks(memory, records, agent), settings.digest)
1125
1156
  digestStats = rendered?.stats
1126
1157
  return rendered
1127
1158
  }, agent)