@restregular/dsh-memory 0.19.2 → 0.21.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.
@@ -123,9 +123,9 @@ const NUDGE_TEXT =
123
123
 
124
124
 
125
125
  const VISIBILITY_LEVEL_TEXT = {
126
- absolute: '绝对限制:可见域外的记忆一律查不到,即使工具内部命中也不会返回。',
127
- normal: '普通限制:可见域外的记忆命中时会向用户发起一次审批;用户拒绝或超时则不返回。',
128
- none: '无限制:可以读取所有作用域与类型(当前选择仅作为下次切换的偏好保留)。',
126
+ absolute: '绝对限制:可见域外的记忆一律查不到,即使工具内部命中也不会返回;范围外也写不进去。',
127
+ normal: '普通限制:可见域外的记忆命中时会向用户发起一次审批;用户拒绝或超时则不返回。写入 / 删除范围外同样需要审批。',
128
+ none: '无限制:可以读写所有作用域(当前选择仅作为下次切换的偏好保留)。',
129
129
  }
130
130
 
131
131
  /**
@@ -134,7 +134,9 @@ const VISIBILITY_LEVEL_TEXT = {
134
134
  */
135
135
  export function renderVisibilityNotice(config ) {
136
136
  if (config === null) {
137
- return '【记忆可见范围已恢复默认】本会话不再有显式可见域;memory_search / memory_get / memory_list 回到"会话 + 项目 + global"的默认读取集合。'
137
+ return '【记忆可见范围已恢复默认】'
138
+ + '本会话不再有显式可见域,改回默认设置:当前会话 + 当前项目 + global 三个作用域,限制等级 = 普通限制'
139
+ + '(范围外命中会先向用户审批;写入 / 删除范围外同样需要审批)。'
138
140
  }
139
141
  const scopes = config.scopes.length > 0
140
142
  ? config.scopes.map((rule) => (rule.kinds !== undefined && rule.kinds.length > 0
@@ -180,6 +182,34 @@ export function apply(ctx , config = {}) {
180
182
 
181
183
  /** 会话级注入状态(正文/标签/版本)与审计。 */
182
184
  const injectionState = new InjectionState()
185
+ /**
186
+ * 给工具半边用的会话上下文去重视图(0.21.0)。
187
+ *
188
+ * memory_search / memory_list 返回的摘要也属于“条目已进入上下文”:同一条记忆
189
+ * 同版本再次命中时只回 id,正文仍由 memory_get 显式取。状态只有一份(注入半边
190
+ * 的 InjectionState),工具侧通过 ctx.get('memoryContextDedup') 惰性读取 ——
191
+ * 拿不到(注入半边被禁用/旧版本)就自然退化为不去重。
192
+ *
193
+ * 用 ctx.provide 而不是把方法挂到 memory 服务上:provider 不需要知道去重实现,
194
+ * 工具半边也不需要依赖注入半边的内部类。
195
+ */
196
+ try {
197
+ ctx.provide('memoryContextDedup', {
198
+ /** 这批记录里哪些已经以同版本进过上下文(摘要或正文)。 */
199
+ seen(sessionId , records ) {
200
+ return injectionState.seenInContext(sessionId, records)
201
+ },
202
+ /** memory_search 返回了摘要 / memory_list 返回了正文。 */
203
+ markSummary(sessionId , records ) {
204
+ injectionState.markSummary(sessionId, records)
205
+ },
206
+ markContent(sessionId , records ) {
207
+ injectionState.markContent(sessionId, records)
208
+ },
209
+ })
210
+ } catch (error) {
211
+ console.warn(`[dsh-memory] 会话上下文去重服务未能注册(工具将不去重): ${error instanceof Error ? error.message : String(error)}`)
212
+ }
183
213
  const audit = new InjectAuditStore()
184
214
  const signals = new BehaviorSignalStore()
185
215
  const callMeta = new Map ()
@@ -8,6 +8,8 @@ export const POLICY_TEXT = `## 记忆系统
8
8
 
9
9
  memory_search 只返回印象(ID、作用域、类型、标签和少量摘要);判断相关后再用 memory_get(ids, fields) 取正文。fields 省略 = content + scope + kind + tags;只要摘要用 ["summary"],只要正文用 ["content"],需要重要度 / 时间 / 触发词等再显式加上对应字段。
10
10
 
11
+ 同一会话里已经出现过的记忆,memory_search / memory_list 只回 ID 并注明“摘要因上下文去重被省略”;那不是没找到,需要正文时直接用 memory_get 取。
12
+
11
13
  遇到以下情况先查记忆,不要凭猜测开始:
12
14
  - 不确定这个项目以前怎么做、有没有既定约定、某个坑是否踩过;
13
15
  - 用户的话里出现「上次 / 之前 / 还是那样」这类指代。
@@ -13,7 +13,7 @@
13
13
  * 行格式(短键,减小体积):
14
14
  * 快照 {"v":1,"d":1,"e":[["id",2,version,at],...]}
15
15
  * 增量 {"d":1,"e":[...]}
16
- * status: 1=hint_injected, 2=content_injected; d=1 表示本会话摘要已做过
16
+ * status: 1=hint_injected, 2=content_injected, 3=summary_injected; d=1 表示本会话摘要已做过
17
17
  *
18
18
  * 只存记录 id / 状态 / 版本 / 时间戳与摘要标记,不落正文,也不落 lastQuery
19
19
  * (查询可能含凭据)。
@@ -29,10 +29,12 @@ import { createHash } from 'node:crypto'
29
29
  const STATUS_CODE = {
30
30
  hint_injected: 1,
31
31
  content_injected: 2,
32
+ summary_injected: 3,
32
33
  }
33
34
  const STATUS_BY_CODE = {
34
35
  1: 'hint_injected',
35
36
  2: 'content_injected',
37
+ 3: 'summary_injected',
36
38
  }
37
39
  const SNAPSHOT_VERSION = 1
38
40
  const DEFAULT_DEBOUNCE_MS = 250
@@ -5,17 +5,21 @@
5
5
  * "正文已注入"与"只给了标签"。这里按 sessionId 保存:
6
6
  * - not_seen → 没有任何记录(默认)
7
7
  * - hint_injected → 只进过截断标签索引,正文仍未加载
8
+ * - summary_injected→ 记忆摘要(memory_search / memory_list 的短摘要)已进入上下文
8
9
  * - content_injected→ 正文已进入当前上下文
9
10
  *
10
11
  * 版本 = record.updatedAt。内容更新后允许重新注入;上下文重建(压缩/新会话)
11
12
  * 由调用方 clear(),绝不用"历史上出现过"代替"当前仍在上下文里"。
12
13
  *
14
+ * 0.21.0 加 summary_injected:memory_search / memory_list 返回过的摘要也算
15
+ * "条目已出现",再次命中同版本时只回 id(见 memoryContextDedup 服务)。
16
+ *
13
17
  * 0.17.0 起可挂一个按会话的持久化 store(见 session-state-store.ts):状态在
14
18
  * 插件重载 / 宿主重启后仍能恢复;读盘只在第一次接触该会话时发生,写入由 store
15
19
  * 自己做合并与阈值 compact。
16
20
  */
17
21
 
18
-
22
+
19
23
 
20
24
 
21
25
 
@@ -28,6 +32,7 @@
28
32
 
29
33
 
30
34
 
35
+
31
36
 
32
37
 
33
38
 
@@ -160,7 +165,8 @@ export class InjectionState {
160
165
  const changed = []
161
166
  for (const record of records) {
162
167
  const previous = state.entries.get(record.id)
163
- if (previous?.status === 'content_injected') continue
168
+ // 标签是比摘要/正文更弱的证据:已经进过摘要或正文时不能往回降级。
169
+ if (previous?.status === 'content_injected' || previous?.status === 'summary_injected') continue
164
170
  const entry = { status: 'hint_injected' , version: record.updatedAt, at: now }
165
171
  state.entries.set(record.id, entry)
166
172
  changed.push({ id: record.id, ...entry })
@@ -169,6 +175,26 @@ export class InjectionState {
169
175
  this.trim(state, sessionId)
170
176
  }
171
177
 
178
+ /**
179
+ * 记录"摘要已进入上下文"(memory_search / memory_list 返回过的条目)。
180
+ * 同版本的正文已注入时不降级:正文比摘要更强。
181
+ */
182
+ markSummary(sessionId , records ) {
183
+ const state = this.stateOf(sessionId)
184
+ const now = Date.now()
185
+ const changed = []
186
+ for (const record of records) {
187
+ const previous = state.entries.get(record.id)
188
+ if (previous?.status === 'content_injected' && previous.version === record.updatedAt) continue
189
+ if (previous?.status === 'summary_injected' && previous.version === record.updatedAt) continue
190
+ const entry = { status: 'summary_injected' , version: record.updatedAt, at: now }
191
+ state.entries.set(record.id, entry)
192
+ changed.push({ id: record.id, ...entry })
193
+ }
194
+ if (changed.length > 0) this.storeOf(sessionId)?.append(changed)
195
+ this.trim(state, sessionId)
196
+ }
197
+
172
198
  /** 记录"正文已注入"(hint 状态被正文覆盖)。 */
173
199
  markContent(sessionId , records ) {
174
200
  const state = this.stateOf(sessionId)
@@ -207,6 +233,27 @@ export class InjectionState {
207
233
  return { fresh, deduplicated, updated }
208
234
  }
209
235
 
236
+ /**
237
+ * 从工具结果去重的角度:这批记录里哪些已经以同版本进入过上下文。
238
+ *
239
+ * 只认摘要 / 正文(content_injected、summary_injected)——标签索引只给了
240
+ * id + tags,不包含摘要,所以 hint_injected 不算“摘要已出现”。
241
+ * 版本(updatedAt)变了等于内容变了,允许重新返回摘要。
242
+ */
243
+ seenInContext(
244
+ sessionId ,
245
+ records ,
246
+ ) {
247
+ const state = this.stateOf(sessionId)
248
+ const seen = new Set ()
249
+ for (const record of records) {
250
+ const entry = state.entries.get(record.id)
251
+ if (entry === undefined || entry.version !== record.updatedAt) continue
252
+ if (entry.status === 'content_injected' || entry.status === 'summary_injected') seen.add(record.id)
253
+ }
254
+ return seen
255
+ }
256
+
210
257
  /** 同一上下文里是否已经注入过同一段文本(最后一道按内容幂等)。 */
211
258
  lastTextOf(sessionId ) {
212
259
  return this.stateOf(sessionId).lastText
@@ -260,12 +307,13 @@ export class InjectionState {
260
307
  snapshot(sessionId ) {
261
308
  const state = this.sessions.get(sessionId)
262
309
  if (state === undefined) {
263
- return { sessionId, hintCount: 0, contentCount: 0, lastText: '', lastQuery: '', entries: [] }
310
+ return { sessionId, hintCount: 0, summaryCount: 0, contentCount: 0, lastText: '', lastQuery: '', entries: [] }
264
311
  }
265
312
  const entries = [...state.entries.entries()].map(([id, entry]) => ({ id, ...entry }))
266
313
  return {
267
314
  sessionId,
268
315
  hintCount: entries.filter((entry) => entry.status === 'hint_injected').length,
316
+ summaryCount: entries.filter((entry) => entry.status === 'summary_injected').length,
269
317
  contentCount: entries.filter((entry) => entry.status === 'content_injected').length,
270
318
  lastText: state.lastText,
271
319
  lastQuery: state.lastQuery,
package/lib/panel.js CHANGED
@@ -487,12 +487,18 @@ function visibilityPayload(ctx, memory, sessionId) {
487
487
  const own = typeof memory.sessionVisibility === 'function' ? memory.sessionVisibility(sessionId) : null
488
488
  let effective = null
489
489
  let inherited = false
490
+ // 0.20.0 起 provider 永远给一份生效策略(没有显式覆盖时是默认策略)。
491
+ // source 告诉面板这份策略是"自己的 / 继承的 / 默认的"。
492
+ let source = own === null ? 'default' : 'explicit'
490
493
  try {
491
494
  if (typeof memory.visibilityPolicy === 'function') {
492
495
  const policy = memory.visibilityPolicy(agentLike)
493
496
  if (policy !== undefined && policy !== null) {
494
497
  effective = policy
495
- inherited = own === null
498
+ source = policy.source === 'explicit' || policy.source === 'inherited' || policy.source === 'default'
499
+ ? policy.source
500
+ : (own === null ? 'default' : 'explicit')
501
+ inherited = source === 'inherited'
496
502
  }
497
503
  }
498
504
  } catch { /* 可见域是增强;拿不到就只返回自己的覆盖 */ }
@@ -533,6 +539,7 @@ function visibilityPayload(ctx, memory, sessionId) {
533
539
  override: own,
534
540
  effective,
535
541
  inherited,
542
+ source,
536
543
  defaults,
537
544
  scopes,
538
545
  }
@@ -676,7 +683,15 @@ async function handle(ctx, routePath, req, res) {
676
683
  if (id === '') throw new ApiError(400, 'APPROVAL_ID_REQUIRED', 'id 不能为空')
677
684
  const outcome = body?.outcome === 'allowed-once' ? 'allowed-once' : body?.outcome === 'rejected' ? 'rejected' : ''
678
685
  if (outcome === '') throw new ApiError(400, 'APPROVAL_OUTCOME_INVALID', 'outcome 必须是 allowed-once 或 rejected')
679
- const answered = memory.answerVisibilityApproval(id, outcome)
686
+ // 0.21.0:逐条允许。省略 = 全部允许;空数组 = 一条都不允许(按拒绝处理)。
687
+ const rawIds = body?.allowedIds
688
+ if (rawIds !== undefined && rawIds !== null && !Array.isArray(rawIds)) {
689
+ throw new ApiError(400, 'APPROVAL_IDS_INVALID', 'allowedIds 必须是字符串数组')
690
+ }
691
+ const allowedIds = Array.isArray(rawIds) ? rawIds.filter((value) => typeof value === 'string' && value !== '') : undefined
692
+ const answered = allowedIds === undefined
693
+ ? memory.answerVisibilityApproval(id, outcome)
694
+ : memory.answerVisibilityApproval(id, outcome, allowedIds)
680
695
  writeJson(res, { ok: true, value: { answered } })
681
696
  return
682
697
  }
@@ -243,11 +243,16 @@ export class CordisMemoryService extends Service {
243
243
  return this.impl.visibleScopes(agent)
244
244
  }
245
245
 
246
- /** 完整可见域(等级 + 每个 scope 的类型白名单)。 */
246
+ /** 完整可见域(等级 + 每个 scope 的类型白名单 + 来源)。0.20 起永远有值。 */
247
247
  visibilityPolicy(agent ) {
248
248
  return this.impl.visibilityPolicy(agent)
249
249
  }
250
250
 
251
+ /** 可见范围 = 可操作范围:写入/删除前用它判断一个 scope 是否可操作。 */
252
+ isScopeOperable(agent , scope ) {
253
+ return this.impl.isScopeOperable(agent, scope)
254
+ }
255
+
251
256
  /** 某个 scope 下允许的记忆类型(undefined = 不限制)。 */
252
257
  visibleKinds(agent , scope ) {
253
258
  return this.impl.visibleKinds(agent, scope)
@@ -288,9 +293,9 @@ export class CordisMemoryService extends Service {
288
293
  return this.impl.pendingVisibilityApprovals(sessionId)
289
294
  }
290
295
 
291
- /** 面板:回答一条待审批。 */
292
- answerVisibilityApproval(id , outcome ) {
293
- return this.impl.answerVisibilityApproval(id, outcome)
296
+ /** 面板:回答一条待审批(可带逐条勾选的 allowedIds)。 */
297
+ answerVisibilityApproval(id , outcome , allowedIds ) {
298
+ return this.impl.answerVisibilityApproval(id, outcome, allowedIds)
294
299
  }
295
300
 
296
301
  /** 写路径默认 scope(省略 scope 时落到哪)。 */