@restregular/dsh-memory 0.34.0 → 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.
@@ -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)