@restregular/dsh-memory 0.9.5 → 0.9.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/README.md +23 -2
- package/lib/client.js +3 -1
- package/lib/inject/apply.js +211 -14
- package/lib/inject/audit.js +87 -0
- package/lib/inject/digest.js +252 -15
- package/lib/inject/inject.js +419 -294
- package/lib/inject/rules.js +134 -9
- package/lib/inject/signals.js +176 -0
- package/lib/inject/state.js +163 -0
- package/lib/panel.js +6 -0
- package/lib/provider/index.js +52 -6
- package/lib/provider/inject-settings.js +7 -1
- package/package.json +1 -1
package/lib/inject/inject.js
CHANGED
|
@@ -1,297 +1,422 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory injection logic (M3) — pure functions, no cordis imports.
|
|
3
|
-
*
|
|
4
|
-
* Injection shape follows the official docs: a user-role message whose content
|
|
5
|
-
* is a single `context` block with `contextForm: 'recall'`, prepended to the
|
|
6
|
-
* messages that enter the step via the agent/pre-step waterfall.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { estimateTokens } from '../provider/rerank.js'
|
|
10
|
-
import { normalizeScopeInput } from '../provider/scope.js'
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
*
|
|
259
|
-
*
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Memory injection logic (M3) — pure functions, no cordis imports.
|
|
3
|
+
*
|
|
4
|
+
* Injection shape follows the official docs: a user-role message whose content
|
|
5
|
+
* is a single `context` block with `contextForm: 'recall'`, prepended to the
|
|
6
|
+
* messages that enter the step via the agent/pre-step waterfall.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { estimateTokens } from '../provider/rerank.js'
|
|
10
|
+
import { normalizeScopeInput } from '../provider/scope.js'
|
|
11
|
+
import { InjectionState } from './state.js'
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
export const defaultInjectConfig = {
|
|
27
|
+
enabled: true,
|
|
28
|
+
tokenBudget: 500,
|
|
29
|
+
topK: 5,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 召回注入能用到的那部分 provider 能力。 */
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
/** 宿主里"按会话 id 找 live session"的那一小部分能力(拿不到就不做父会话召回)。 */
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 把 config.extraScopes 规范化成 scope 键列表;非法项忽略并记一行日志
|
|
47
|
+
* (配置写错不该让整个注入半边失效)。
|
|
48
|
+
*/
|
|
49
|
+
export function normalizeExtraScopes(raw , warn = console.warn) {
|
|
50
|
+
const out = []
|
|
51
|
+
for (const item of raw) {
|
|
52
|
+
const normalized = normalizeScopeInput(item)
|
|
53
|
+
if (!normalized.ok) {
|
|
54
|
+
warn(`[dsh-memory-inject] 忽略非法的 inject.extraScopes 项 ${JSON.stringify(item)}: ${normalized.message}`)
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
if (normalized.scope !== undefined && !out.includes(normalized.scope)) out.push(normalized.scope)
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 无显式 scope 时的召回范围:当前会话 + 当前项目 + global(provider 解析)
|
|
64
|
+
* + inject.extraScopes + 父会话(fork 时,只读召回)。
|
|
65
|
+
* 任何一步拿不到都只降级,绝不抛(注入路径抛错会打断整个 agent 循环)。
|
|
66
|
+
*/
|
|
67
|
+
export function resolveInjectScopes(
|
|
68
|
+
memory ,
|
|
69
|
+
agent ,
|
|
70
|
+
extraScopes ,
|
|
71
|
+
sessions ,
|
|
72
|
+
) {
|
|
73
|
+
const scopes = []
|
|
74
|
+
try {
|
|
75
|
+
for (const scope of memory.resolveReadScopes?.(agent) ?? []) {
|
|
76
|
+
if (!scopes.includes(scope)) scopes.push(scope)
|
|
77
|
+
}
|
|
78
|
+
} catch { /* 降级为 extraScopes */ }
|
|
79
|
+
for (const scope of extraScopes) if (!scopes.includes(scope)) scopes.push(scope)
|
|
80
|
+
// fork 出来的会话:父会话的记忆也值得召回(只读)。父会话可能已不在内存里,拿不到就跳过。
|
|
81
|
+
try {
|
|
82
|
+
const header = (agent )?.session?.header
|
|
83
|
+
const parent = typeof header?.parentSession === 'string' ? header.parentSession : undefined
|
|
84
|
+
if (parent !== undefined && header?.origin !== 'subagent') {
|
|
85
|
+
if (sessions?.get?.(parent) !== undefined) {
|
|
86
|
+
const scope = `session:${parent}`
|
|
87
|
+
if (!scopes.includes(scope)) scopes.push(scope)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} catch { /* 父会话召回是可选增强 */ }
|
|
91
|
+
return scopes.length > 0 ? scopes : undefined
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Minimal structural view of the messages we inspect (role + text blocks). */
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Extract the retrieval query: the text of the LAST user message. Synthetic
|
|
119
|
+
* context injections (file notices etc.) are user-role too, so scanning from
|
|
120
|
+
* the end finds the freshest human-visible input; empty text → no injection.
|
|
121
|
+
*/
|
|
122
|
+
export function pickQueryText(messages ) {
|
|
123
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
124
|
+
const m = messages[i]
|
|
125
|
+
if (m.role !== 'user') continue
|
|
126
|
+
const text = (m.content ?? [])
|
|
127
|
+
.filter((b) => b.type === 'text' && typeof b.text === 'string')
|
|
128
|
+
.map((b) => b.text )
|
|
129
|
+
.join(' ')
|
|
130
|
+
.trim()
|
|
131
|
+
if (text) return text
|
|
132
|
+
}
|
|
133
|
+
return ''
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 这一步的 messages 里是否已经有一条内容完全相同的召回块。
|
|
138
|
+
*
|
|
139
|
+
* 正常情况下永远不会命中:一个激活只有一个 MemoryInjector,而它自己用
|
|
140
|
+
* 会话级 lastText 去重。但只要宿主把这条 entry 应用了两次(热重载/重新启用时
|
|
141
|
+
* 旧 fiber 的 agent/pre-step 监听没被清掉),两个注入器就会各追加一条逐字相同的消息 ——
|
|
142
|
+
* 表现在会话轨迹里就是同一轮「上下轮」卡片连着出现两次(2026-10-17 实况)。
|
|
143
|
+
* 注入本身是"多一条也无害、少一条也不坏"的提示块,所以这里按内容做一次幂等:
|
|
144
|
+
* 另一个实例已经放进去的同一段,就不要再放。
|
|
145
|
+
*/
|
|
146
|
+
export function hasRecallMessage(
|
|
147
|
+
messages ,
|
|
148
|
+
text ,
|
|
149
|
+
) {
|
|
150
|
+
for (const m of messages) {
|
|
151
|
+
if (m.role !== 'user') continue
|
|
152
|
+
if (m.source?.kind !== 'memory') continue
|
|
153
|
+
const joined = (m.content ?? [])
|
|
154
|
+
.filter((b) => b.type === 'text' && typeof b.text === 'string')
|
|
155
|
+
.map((b) => b.text )
|
|
156
|
+
.join('\n')
|
|
157
|
+
if (joined === text) return true
|
|
158
|
+
}
|
|
159
|
+
return false
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** 召回的两种形态:旧的长文(300 字)与新的"印象"(短摘要 + 指路取全文)。 */
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
/** Render hits into a recall text block, greedily within the token budget. */
|
|
184
|
+
export function renderRecallBlock(
|
|
185
|
+
hits ,
|
|
186
|
+
cfg ,
|
|
187
|
+
options = {},
|
|
188
|
+
) {
|
|
189
|
+
if (hits.length === 0) return null
|
|
190
|
+
const impression = options.impression === true
|
|
191
|
+
const chars = impression ? (options.chars ?? 120) : 300
|
|
192
|
+
const header = impression
|
|
193
|
+
? '以下是与你这次请求可能相关的记忆摘要:'
|
|
194
|
+
: '以下是与此请求可能相关的长期记忆,供参考(可用 memory_search 核实,用 memory_forget 删除):'
|
|
195
|
+
let tokens = estimateTokens(header) + 4
|
|
196
|
+
const lines = []
|
|
197
|
+
const ids = []
|
|
198
|
+
const omitted = []
|
|
199
|
+
let budgetExhausted = false
|
|
200
|
+
for (const [index, h] of hits.entries()) {
|
|
201
|
+
if (index >= cfg.topK) {
|
|
202
|
+
omitted.push({ id: h.record.id, reason: 'top_k' })
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
if (budgetExhausted) {
|
|
206
|
+
omitted.push({ id: h.record.id, reason: 'budget' })
|
|
207
|
+
continue
|
|
208
|
+
}
|
|
209
|
+
const line = `- ${h.record.id.slice(0, 8)} ${h.record.content.replace(/\s+/g, ' ').slice(0, chars)}`
|
|
210
|
+
const cost = estimateTokens(line) + 1
|
|
211
|
+
if (tokens + cost > cfg.tokenBudget) {
|
|
212
|
+
budgetExhausted = true
|
|
213
|
+
omitted.push({ id: h.record.id, reason: 'budget' })
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
lines.push(line)
|
|
217
|
+
ids.push(h.record.id)
|
|
218
|
+
tokens += cost
|
|
219
|
+
}
|
|
220
|
+
if (lines.length === 0) return null
|
|
221
|
+
return { text: `${header}\n${lines.join('\n')}`, tokens, ids, omitted }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The context block we prepend (matches the documented ContextForm 'recall'). */
|
|
225
|
+
export function recallText(text ) {
|
|
226
|
+
return text
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** 配置既可以给静态值,也可以给 getter —— 面板改了设置,下一步就生效。 */
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Turn-agnostic injector: searches memory for the step's query and returns the
|
|
259
|
+
* recall text to append. The host middleware (apply.ts) wraps that text into a
|
|
260
|
+
* real message via createUserMessage.
|
|
261
|
+
*
|
|
262
|
+
* 去重从 0.9.6 起按 sessionId + 记忆 id/版本管理:
|
|
263
|
+
* - 已注入正文且内容未变 → 跳过;
|
|
264
|
+
* - 只注入过标签 → 允许补正文;
|
|
265
|
+
* - 内容更新 → 重新注入;
|
|
266
|
+
* - 同一段文本 = 最后一道幂等。
|
|
267
|
+
*/
|
|
268
|
+
export class MemoryInjector {
|
|
269
|
+
search
|
|
270
|
+
cfg
|
|
271
|
+
renderOptions
|
|
272
|
+
state
|
|
273
|
+
|
|
274
|
+
constructor(
|
|
275
|
+
search ,
|
|
276
|
+
cfg ,
|
|
277
|
+
renderOptions = {},
|
|
278
|
+
state = new InjectionState(),
|
|
279
|
+
) {
|
|
280
|
+
this.search = search
|
|
281
|
+
this.cfg = cfg
|
|
282
|
+
this.renderOptions = renderOptions
|
|
283
|
+
this.state = state
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
async maybeInject(
|
|
287
|
+
input ,
|
|
288
|
+
_decision ,
|
|
289
|
+
options = {},
|
|
290
|
+
) {
|
|
291
|
+
const result = await this.maybeInjectDetailed(input, _decision, options)
|
|
292
|
+
return result?.text
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async maybeInjectDetailed(
|
|
296
|
+
input ,
|
|
297
|
+
_decision ,
|
|
298
|
+
options = {},
|
|
299
|
+
) {
|
|
300
|
+
const cfg = typeof this.cfg === 'function' ? this.cfg() : this.cfg
|
|
301
|
+
if (!cfg.enabled) return undefined
|
|
302
|
+
const query = options.query ?? pickQueryText(input.messages)
|
|
303
|
+
if (!query) return undefined
|
|
304
|
+
const sessionId = options.sessionId ?? input.sessionId
|
|
305
|
+
|
|
306
|
+
let hits
|
|
307
|
+
try {
|
|
308
|
+
hits = await this.search(query, input.signal, input.scopes)
|
|
309
|
+
} catch {
|
|
310
|
+
// 检索失败不能打断 agent 循环;审计会区分"失败"与"成功但无结果"。
|
|
311
|
+
return {
|
|
312
|
+
ids: [], candidates: 0, dedupSkipped: 0, updated: [], omitted: [], query,
|
|
313
|
+
tokens: 0, retrieval: { ok: false, count: 0, error: 'retrieval_failed' },
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// 成功检索后记住查询(即使没有新正文):同一 query 的后续 step 不再重复全量检索。
|
|
317
|
+
this.state.setLastQuery(sessionId, query)
|
|
318
|
+
|
|
319
|
+
const seenIds = options.excludeIds
|
|
320
|
+
const candidates = seenIds === undefined ? hits : hits.filter((hit) => !seenIds.has(hit.record.id))
|
|
321
|
+
const { fresh, deduplicated, updated } = this.state.filterNewContent(
|
|
322
|
+
sessionId,
|
|
323
|
+
candidates.map((hit) => hit.record),
|
|
324
|
+
)
|
|
325
|
+
const freshIds = new Set(fresh.map((record) => record.id))
|
|
326
|
+
const freshHits = candidates.filter((hit) => freshIds.has(hit.record.id))
|
|
327
|
+
const optionsForRender = typeof this.renderOptions === 'function' ? this.renderOptions() : this.renderOptions
|
|
328
|
+
const block = renderRecallBlock(freshHits, cfg, optionsForRender)
|
|
329
|
+
const base = {
|
|
330
|
+
ids: [], candidates: candidates.length, dedupSkipped: deduplicated.length, updated, omitted: [], query,
|
|
331
|
+
tokens: 0, retrieval: { ok: true, count: hits.length },
|
|
332
|
+
}
|
|
333
|
+
if (!block) return base
|
|
334
|
+
if (block.text === this.state.lastTextOf(sessionId)) {
|
|
335
|
+
return { ...base, ids: block.ids, omitted: block.omitted, tokens: block.tokens, dedupSkipped: deduplicated.length + block.ids.length }
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
this.state.markContent(sessionId, block.ids.map((id) => {
|
|
339
|
+
const record = freshHits.find((hit) => hit.record.id === id) .record
|
|
340
|
+
return { id, updatedAt: record.updatedAt }
|
|
341
|
+
}))
|
|
342
|
+
this.state.setLastText(sessionId, block.text)
|
|
343
|
+
this.state.setLastQuery(sessionId, query)
|
|
344
|
+
return { ...base, text: block.text, ids: block.ids, omitted: block.omitted, tokens: block.tokens }
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 从 agent 上取会话 id;取不到就没有"每个会话只发一次摘要"的锚点。 */
|
|
349
|
+
export function sessionIdOf(agent ) {
|
|
350
|
+
const id = (agent )?.session?.id
|
|
351
|
+
return typeof id === 'string' && id.length > 0 ? id : undefined
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** 摘要注入最多记住多少个会话(一个进程里开过的会话上界,防止 Set 无限长)。 */
|
|
355
|
+
const MAX_DIGESTED_SESSIONS = 500
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* 会话摘要注入器:一个会话只注入一次"已有哪些记忆"的摘要。
|
|
359
|
+
* 检索失败或结果为空也标记为已完成 —— 摘要不是必需品,不能每步重试。
|
|
360
|
+
*/
|
|
361
|
+
export class MemoryDigester {
|
|
362
|
+
done = new Set ()
|
|
363
|
+
list
|
|
364
|
+
|
|
365
|
+
constructor(
|
|
366
|
+
list ,
|
|
367
|
+
) {
|
|
368
|
+
this.list = list
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async maybeDigest(
|
|
372
|
+
sessionId ,
|
|
373
|
+
scopes ,
|
|
374
|
+
render ,
|
|
375
|
+
) {
|
|
376
|
+
if (sessionId === undefined || this.done.has(sessionId)) return undefined
|
|
377
|
+
this.markDone(sessionId)
|
|
378
|
+
let records
|
|
379
|
+
try {
|
|
380
|
+
records = await this.list(scopes)
|
|
381
|
+
} catch {
|
|
382
|
+
return undefined
|
|
383
|
+
}
|
|
384
|
+
return render(records) ?? undefined
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* 与 maybeDigest 相同,但把渲染结果原样返回(含统计与 hintIds),
|
|
389
|
+
* 供 apply.ts 写审计、标记 hint/content 状态。
|
|
390
|
+
*/
|
|
391
|
+
async maybeDigestDetailed (
|
|
392
|
+
sessionId ,
|
|
393
|
+
scopes ,
|
|
394
|
+
render ,
|
|
395
|
+
) {
|
|
396
|
+
if (sessionId === undefined || this.done.has(sessionId)) return undefined
|
|
397
|
+
this.markDone(sessionId)
|
|
398
|
+
let records
|
|
399
|
+
try {
|
|
400
|
+
records = await this.list(scopes)
|
|
401
|
+
} catch {
|
|
402
|
+
return undefined
|
|
403
|
+
}
|
|
404
|
+
return render(records) ?? undefined
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
markDone(sessionId ) {
|
|
408
|
+
this.done.add(sessionId)
|
|
409
|
+
if (this.done.size > MAX_DIGESTED_SESSIONS) {
|
|
410
|
+
const oldest = this.done.values().next()
|
|
411
|
+
if (!oldest.done) this.done.delete(oldest.value)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** 上下文被压缩/重建后重新允许摘要评估(旧摘要可能已不在上下文里)。 */
|
|
416
|
+
reset(sessionId ) {
|
|
417
|
+
if (sessionId !== undefined) this.done.delete(sessionId)
|
|
418
|
+
}
|
|
419
|
+
}
|
|
295
420
|
|
|
296
421
|
|
|
297
422
|
//# sourceURL=source/memory-inject/src/inject.ts
|