@restregular/dsh-memory 0.14.7 → 0.15.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/CHANGELOG.md +30 -0
- package/README.md +24 -5
- package/lib/client.js +107 -5
- package/lib/inject/apply.js +76 -2
- package/lib/inject/audit-types.js +1 -0
- package/lib/inject/digest.js +26 -7
- package/lib/inject/inject.js +10 -2
- package/lib/inject/procedures.js +188 -0
- package/lib/inject/rules-delta.js +3 -0
- package/lib/inject/rules.js +5 -0
- package/lib/panel.js +11 -0
- package/lib/provider/index.js +64 -20
- package/lib/provider/inject-settings.js +32 -0
- package/lib/provider/store.js +13 -0
- package/lib/tools/index.js +18 -2
- package/package.json +1 -1
package/lib/inject/digest.js
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
* 挑几条,让模型一开始就知道有哪些旧结论可复用,而不是先搜一遍。
|
|
6
6
|
*
|
|
7
7
|
* 0.9.6 起,摘要不再静默截断:
|
|
8
|
-
* - 返回完整的候选/注入/省略统计与每条省略原因(top_k / budget / superseded / expired);
|
|
8
|
+
* - 返回完整的候选/注入/省略统计与每条省略原因(top_k / budget / superseded / expired / rule);
|
|
9
9
|
* - 被省略的正文会生成一段**独立的标签索引**,让模型知道哪些领域还有未展开的记忆;
|
|
10
10
|
* - 标签索引有自己的 token 预算与条数上限,不会反过来挤爆正文;
|
|
11
|
-
* - 退役(superseded)
|
|
11
|
+
* - 退役(superseded)记录默认不进入摘要正文,也不生成索引;
|
|
12
|
+
* - kind=rule 的记录不进摘要:硬性规则已经常驻系统提示,再列一遍是跨通道重复。
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
import { estimateTokens } from '../provider/rerank.js'
|
|
@@ -22,7 +23,7 @@ const HINT_HEADER = '相关记忆索引(正文未注入):'
|
|
|
22
23
|
const HINT_FOOTER = '这些条目的正文尚未加载;遇到相关任务时,按标签或 ID 用 memory_search 取正文。'
|
|
23
24
|
|
|
24
25
|
/** 一条正文被省略的原因。统计口径互斥:每条记录只归入一个原因。 */
|
|
25
|
-
|
|
26
|
+
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
|
|
@@ -50,6 +51,7 @@ const HINT_FOOTER = '这些条目的正文尚未加载;遇到相关任务时
|
|
|
50
51
|
|
|
51
52
|
|
|
52
53
|
|
|
54
|
+
|
|
53
55
|
|
|
54
56
|
|
|
55
57
|
|
|
@@ -191,11 +193,20 @@ function renderHintIndex(omitted , cfg )
|
|
|
191
193
|
return { text, tokens, injected: lines.length, omittedTopK, omittedBudget, injectedIds }
|
|
192
194
|
}
|
|
193
195
|
|
|
196
|
+
/** 一条记录在摘要/召回里的行首标签。状态型记忆必须提示“使用前验证”。 */
|
|
197
|
+
export function recordKindPrefix(record ) {
|
|
198
|
+
return record.kind === 'state' ? '【状态,使用前验证】' : ''
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function digestLine(record ) {
|
|
202
|
+
return `- ${record.id.slice(0, 8)} [${digestScopeLabel(record.scope)}/${record.kind}] ${recordKindPrefix(record)}${record.content.replace(/\s+/g, ' ').slice(0, 160)}`
|
|
203
|
+
}
|
|
204
|
+
|
|
194
205
|
/**
|
|
195
206
|
* 摘要的完整计划:正文 + 截断统计 + 标签索引。
|
|
196
207
|
*
|
|
197
|
-
*
|
|
198
|
-
* 只会归入 `top_k`、`budget`、`superseded`、`expired` 之一。
|
|
208
|
+
* 统计口径:先按规则/退役/过期过滤,再按排序依次尝试注入;`selected` 以外的记录
|
|
209
|
+
* 只会归入 `top_k`、`budget`、`superseded`、`expired`、`rule` 之一。
|
|
199
210
|
*/
|
|
200
211
|
export function renderDigest(records , cfg , now = Date.now()) {
|
|
201
212
|
if (records.length === 0) return null
|
|
@@ -205,7 +216,14 @@ export function renderDigest(records , cfg
|
|
|
205
216
|
const omitted = []
|
|
206
217
|
let omittedSuperseded = 0
|
|
207
218
|
let omittedExpired = 0
|
|
219
|
+
let omittedRules = 0
|
|
208
220
|
for (const record of records) {
|
|
221
|
+
// 规则已经常驻系统提示(rules 段);摘要再列一遍就是同一个 id 在两个通道出现。
|
|
222
|
+
if (record.kind === 'rule') {
|
|
223
|
+
omittedRules += 1
|
|
224
|
+
omitted.push({ id: record.id, reason: 'rule', kind: record.kind, tags: tagsOf(record) })
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
209
227
|
if (retired.has(record.id)) {
|
|
210
228
|
omittedSuperseded += 1
|
|
211
229
|
omitted.push({ id: record.id, reason: 'superseded', kind: record.kind, tags: tagsOf(record) })
|
|
@@ -239,7 +257,7 @@ export function renderDigest(records , cfg
|
|
|
239
257
|
omitted.push({ id: record.id, reason: 'budget', kind: record.kind, tags: tagsOf(record) })
|
|
240
258
|
continue
|
|
241
259
|
}
|
|
242
|
-
const line =
|
|
260
|
+
const line = digestLine(record)
|
|
243
261
|
const cost = estimateTokens(line) + 1
|
|
244
262
|
if (tokens + cost > cfg.tokenBudget) {
|
|
245
263
|
budgetExhausted = true
|
|
@@ -254,7 +272,7 @@ export function renderDigest(records , cfg
|
|
|
254
272
|
|
|
255
273
|
const hint = renderHintIndex(hintCandidates, cfg)
|
|
256
274
|
const bodyText = selected.length > 0
|
|
257
|
-
? `${HEADER}\n${selected.map((record) =>
|
|
275
|
+
? `${HEADER}\n${selected.map((record) => digestLine(record)).join('\n')}`
|
|
258
276
|
: ''
|
|
259
277
|
const text = [bodyText, hint.text].filter((part) => part !== null && part !== '').join('\n\n')
|
|
260
278
|
if (text === '') return null
|
|
@@ -267,6 +285,7 @@ export function renderDigest(records , cfg
|
|
|
267
285
|
omittedBudget,
|
|
268
286
|
omittedSuperseded,
|
|
269
287
|
omittedExpired,
|
|
288
|
+
omittedRules,
|
|
270
289
|
truncated: omitted.length > 0,
|
|
271
290
|
omitted,
|
|
272
291
|
hint: {
|
package/lib/inject/inject.js
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import { estimateTokens } from '../provider/rerank.js'
|
|
10
10
|
import { normalizeScopeInput } from '../provider/scope.js'
|
|
11
11
|
import { textHasTag } from './signals.js'
|
|
12
|
+
import { recordKindPrefix } from './digest.js'
|
|
12
13
|
import { InjectionState } from './state.js'
|
|
13
14
|
|
|
14
15
|
|
|
@@ -230,7 +231,7 @@ export function renderRecallBlock(
|
|
|
230
231
|
omitted.push({ id: h.record.id, reason: 'budget' })
|
|
231
232
|
continue
|
|
232
233
|
}
|
|
233
|
-
const line = `- ${h.record.id.slice(0, 8)} ${h.record.content.replace(/\s+/g, ' ').slice(0, chars)}`
|
|
234
|
+
const line = `- ${h.record.id.slice(0, 8)} ${recordKindPrefix(h.record)}${h.record.content.replace(/\s+/g, ' ').slice(0, chars)}`
|
|
234
235
|
const cost = estimateTokens(line) + 1
|
|
235
236
|
if (tokens + cost > cfg.tokenBudget) {
|
|
236
237
|
budgetExhausted = true
|
|
@@ -289,6 +290,7 @@ export function recallText(text ) {
|
|
|
289
290
|
|
|
290
291
|
|
|
291
292
|
|
|
293
|
+
|
|
292
294
|
|
|
293
295
|
|
|
294
296
|
|
|
@@ -353,6 +355,12 @@ export class MemoryInjector {
|
|
|
353
355
|
retrieval: { ok: false, count: 0, error: 'retrieval_failed', errorCode: 'retrieval_failed' },
|
|
354
356
|
}
|
|
355
357
|
}
|
|
358
|
+
// 过期状态由 provider 的检索层过滤;这里再兜一道,防止旧 provider 把过期
|
|
359
|
+
// state 当成当前事实注入。memory_get(id) 仍可直接取到历史正文。
|
|
360
|
+
const now = Date.now()
|
|
361
|
+
const retrieved = hits
|
|
362
|
+
hits = hits.filter((hit) => !(hit.record.expiredAt !== undefined && hit.record.expiredAt < now))
|
|
363
|
+
const expiredExcluded = retrieved.length - hits.length
|
|
356
364
|
// 成功检索后记住查询(即使没有新正文):同一 query 的后续 step 不再重复全量检索。
|
|
357
365
|
this.state.setLastQuery(sessionId, query)
|
|
358
366
|
|
|
@@ -379,7 +387,7 @@ export class MemoryInjector {
|
|
|
379
387
|
const block = renderRecallBlock(freshHits, cfg, optionsForRender)
|
|
380
388
|
const base = {
|
|
381
389
|
ids: [], candidates: candidates.length, dedupSkipped: deduplicated.length, updated, omitted: [], query,
|
|
382
|
-
tokens: 0, rawHits: hits.length, tagFiltered, supersededExcluded,
|
|
390
|
+
tokens: 0, rawHits: hits.length, tagFiltered, supersededExcluded, expiredExcluded,
|
|
383
391
|
retrieval: { ok: true, count: hits.length },
|
|
384
392
|
}
|
|
385
393
|
if (!block) return base
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 流程索引:系统提示里的「相关流程」目录。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不直接注入 procedure 全文:
|
|
5
|
+
* - 流程通常很长,全部常驻会挤爆系统提示;
|
|
6
|
+
* - 注入点在 agent/pre-step,模型决定调用工具之前拿不到工具名;
|
|
7
|
+
* - 所以这里只给"当前工作区 + 任务/工具触发词"命中的流程目录,每行
|
|
8
|
+
* `id · 触发词 → 标题`;模型需要细节时用 memory_get 取全文。
|
|
9
|
+
*
|
|
10
|
+
* 匹配口径:
|
|
11
|
+
* - 当前工作区的 procedure 总是可见(工作区本身就是触发条件);
|
|
12
|
+
* - 其它 scope(global/session/自定义)必须命中 triggers,没有 triggers 时用 tags;
|
|
13
|
+
* - 既没有 triggers/tags 又不在当前工作区的流程不出现,避免把无关项目带进来。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { estimateTokens } from '../provider/rerank.js'
|
|
17
|
+
|
|
18
|
+
import { supersededIds } from './digest.js'
|
|
19
|
+
import { textHasTag } from './signals.js'
|
|
20
|
+
|
|
21
|
+
export const PROCEDURES_HEADER =
|
|
22
|
+
'以下是当前工作区 / 任务相关的流程索引(按需用 memory_get <id> 取全文):'
|
|
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
|
+
function triggersOf(record ) {
|
|
57
|
+
const raw = (record ).triggers
|
|
58
|
+
if (!Array.isArray(raw)) return []
|
|
59
|
+
const out = []
|
|
60
|
+
const seen = new Set ()
|
|
61
|
+
for (const value of raw) {
|
|
62
|
+
if (typeof value !== 'string') continue
|
|
63
|
+
const term = value.trim()
|
|
64
|
+
if (term === '') continue
|
|
65
|
+
const key = term.toLowerCase()
|
|
66
|
+
if (seen.has(key)) continue
|
|
67
|
+
seen.add(key)
|
|
68
|
+
out.push(term)
|
|
69
|
+
}
|
|
70
|
+
return out
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 触发词:显式 triggers 优先,没有就用 tags。 */
|
|
74
|
+
export function procedureTerms(record ) {
|
|
75
|
+
const triggers = triggersOf(record)
|
|
76
|
+
if (triggers.length > 0) return triggers
|
|
77
|
+
return (record.tags ?? []).map((tag) => String(tag).trim()).filter((tag) => tag !== '')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function titleOf(record ) {
|
|
81
|
+
return record.content.replace(/\s+/g, ' ').trim().slice(0, 80)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 一行目录:ID · 触发词 → 标题。不复制正文。 */
|
|
85
|
+
export function procedureLine(record ) {
|
|
86
|
+
const terms = procedureTerms(record).slice(0, 3)
|
|
87
|
+
const trigger = terms.length > 0 ? terms.join('/') : '流程'
|
|
88
|
+
return `- \`${record.id.slice(0, 8)}\` · ${trigger} → ${titleOf(record)}`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function matches(record , terms , haystack ) {
|
|
92
|
+
if (terms.length === 0) return false
|
|
93
|
+
return terms.some((term) => textHasTag(haystack, term))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 渲染流程索引。没有命中项时返回 null(系统提示 section 用空串表示不注入)。
|
|
98
|
+
*/
|
|
99
|
+
export function renderProcedureIndex(
|
|
100
|
+
records ,
|
|
101
|
+
cfg ,
|
|
102
|
+
context = {},
|
|
103
|
+
) {
|
|
104
|
+
if (!cfg.enabled) return null
|
|
105
|
+
const now = context.now ?? Date.now()
|
|
106
|
+
const retired = supersededIds(records)
|
|
107
|
+
const projectScopes = new Set(context.projectScopes ?? [])
|
|
108
|
+
const haystack = [
|
|
109
|
+
context.query ?? '',
|
|
110
|
+
...(context.tools ?? []),
|
|
111
|
+
...projectScopes,
|
|
112
|
+
].join(' ')
|
|
113
|
+
|
|
114
|
+
const candidates = []
|
|
115
|
+
const omitted = []
|
|
116
|
+
for (const record of records) {
|
|
117
|
+
if (record.kind !== 'procedure') continue
|
|
118
|
+
if (retired.has(record.id)) continue
|
|
119
|
+
if (record.expiredAt !== undefined && record.expiredAt < now) continue
|
|
120
|
+
candidates.push(record)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const matched = []
|
|
124
|
+
for (const record of candidates) {
|
|
125
|
+
const terms = procedureTerms(record)
|
|
126
|
+
const isProject = projectScopes.has(record.scope)
|
|
127
|
+
if (isProject || matches(record, terms, haystack)) matched.push(record)
|
|
128
|
+
else omitted.push({ id: record.id, reason: 'unmatch' })
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 当前工作区优先,其次重要度 / 新鲜度;稳定排序保证同一输入同样输出。
|
|
132
|
+
const ordered = [...matched].sort(
|
|
133
|
+
(a, b) =>
|
|
134
|
+
Number(projectScopes.has(b.scope)) - Number(projectScopes.has(a.scope)) ||
|
|
135
|
+
b.importance - a.importance ||
|
|
136
|
+
b.updatedAt - a.updatedAt ||
|
|
137
|
+
a.content.localeCompare(b.content),
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
let tokens = estimateTokens(PROCEDURES_HEADER) + 4
|
|
141
|
+
const lines = []
|
|
142
|
+
const ids = []
|
|
143
|
+
let omittedTopK = 0
|
|
144
|
+
let omittedBudget = 0
|
|
145
|
+
let budgetExhausted = false
|
|
146
|
+
for (const [index, record] of ordered.entries()) {
|
|
147
|
+
if (index >= cfg.maxItems) {
|
|
148
|
+
omittedTopK += 1
|
|
149
|
+
omitted.push({ id: record.id, reason: 'top_k' })
|
|
150
|
+
continue
|
|
151
|
+
}
|
|
152
|
+
if (budgetExhausted) {
|
|
153
|
+
omittedBudget += 1
|
|
154
|
+
omitted.push({ id: record.id, reason: 'budget' })
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
const line = procedureLine(record)
|
|
158
|
+
const cost = estimateTokens(line) + 1
|
|
159
|
+
if (tokens + cost > cfg.tokenBudget) {
|
|
160
|
+
budgetExhausted = true
|
|
161
|
+
omittedBudget += 1
|
|
162
|
+
omitted.push({ id: record.id, reason: 'budget' })
|
|
163
|
+
continue
|
|
164
|
+
}
|
|
165
|
+
lines.push(line)
|
|
166
|
+
ids.push(record.id)
|
|
167
|
+
tokens += cost
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (lines.length === 0) return null
|
|
171
|
+
return {
|
|
172
|
+
text: `${PROCEDURES_HEADER}\n${lines.join('\n')}`,
|
|
173
|
+
tokens,
|
|
174
|
+
count: lines.length,
|
|
175
|
+
ids,
|
|
176
|
+
stats: {
|
|
177
|
+
candidates: candidates.length,
|
|
178
|
+
matched: matched.length,
|
|
179
|
+
injected: lines.length,
|
|
180
|
+
tokens,
|
|
181
|
+
ids,
|
|
182
|
+
omitted,
|
|
183
|
+
},
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
//# sourceURL=source/memory-inject/src/procedures.ts
|
|
@@ -60,6 +60,8 @@ export function derivedHistoryOf(agent )
|
|
|
60
60
|
|
|
61
61
|
|
|
62
62
|
|
|
63
|
+
|
|
64
|
+
|
|
63
65
|
|
|
64
66
|
|
|
65
67
|
/**
|
|
@@ -121,6 +123,7 @@ maybeInjectDetailed(agent , memory , cfg
|
|
|
121
123
|
records: unseen,
|
|
122
124
|
changes,
|
|
123
125
|
...(text !== undefined ? { tokens: rendered.tokens } : {}),
|
|
126
|
+
...(text !== undefined ? { injectedEntries: rendered.stats.injectedEntries } : {}),
|
|
124
127
|
}
|
|
125
128
|
}
|
|
126
129
|
|
package/lib/inject/rules.js
CHANGED
|
@@ -69,6 +69,8 @@ export function orderRules(records ) {
|
|
|
69
69
|
|
|
70
70
|
|
|
71
71
|
|
|
72
|
+
|
|
73
|
+
|
|
72
74
|
|
|
73
75
|
|
|
74
76
|
|
|
@@ -103,6 +105,7 @@ function renderLines(
|
|
|
103
105
|
let tokens = estimateTokens(header) + 4
|
|
104
106
|
const lines = []
|
|
105
107
|
const injectedIds = []
|
|
108
|
+
const injectedEntries = []
|
|
106
109
|
let omittedTopK = 0
|
|
107
110
|
let omittedBudget = 0
|
|
108
111
|
let budgetExhausted = false
|
|
@@ -135,6 +138,7 @@ function renderLines(
|
|
|
135
138
|
}
|
|
136
139
|
lines.push(line)
|
|
137
140
|
injectedIds.push(record.id)
|
|
141
|
+
injectedEntries.push({ id: record.id, updatedAt: record.updatedAt })
|
|
138
142
|
tokens += cost
|
|
139
143
|
if (mandatory) mandatoryInjected += 1
|
|
140
144
|
}
|
|
@@ -151,6 +155,7 @@ function renderLines(
|
|
|
151
155
|
mandatoryOmitted,
|
|
152
156
|
truncated: omitted.length > 0,
|
|
153
157
|
injectedIds,
|
|
158
|
+
injectedEntries,
|
|
154
159
|
omitted,
|
|
155
160
|
omittedIds: omitted.map((entry) => entry.id),
|
|
156
161
|
}
|
package/lib/panel.js
CHANGED
|
@@ -208,6 +208,9 @@ function wireRecord(rec, scopeStore, supersededBy = null) {
|
|
|
208
208
|
accessCount: rec.accessCount ?? 0,
|
|
209
209
|
supersededBy,
|
|
210
210
|
supersedes: rec.supersedes ?? null,
|
|
211
|
+
triggers: rec.triggers ?? [],
|
|
212
|
+
expiredAt: rec.expiredAt ?? null,
|
|
213
|
+
expired: rec.expiredAt !== undefined && rec.expiredAt < Date.now(),
|
|
211
214
|
embedded: !scopeStore.embedPending.has(rec.id) && scopeStore.vectors.has(rec.id),
|
|
212
215
|
}
|
|
213
216
|
}
|
|
@@ -574,6 +577,9 @@ async function handle(ctx, routePath, req, res) {
|
|
|
574
577
|
...(typeof body.content === 'string' ? { content: body.content } : {}),
|
|
575
578
|
...(Array.isArray(body.tags) ? { tags: body.tags.map(String) } : {}),
|
|
576
579
|
...(typeof body.importance === 'number' ? { importance: body.importance } : {}),
|
|
580
|
+
...(typeof body.ttlMs === 'number' ? { ttlMs: body.ttlMs } : {}),
|
|
581
|
+
...(typeof body.expiresAt === 'number' ? { expiresAt: body.expiresAt } : {}),
|
|
582
|
+
...(Array.isArray(body.triggers) ? { triggers: body.triggers.map(String) } : {}),
|
|
577
583
|
})
|
|
578
584
|
console.log(`[dsh-memory-panel] 新建 ${record.kind} @ ${record.scope}: ${record.id}`)
|
|
579
585
|
writeJson(res, { ok: true, value: { record } })
|
|
@@ -805,7 +811,12 @@ async function handle(ctx, routePath, req, res) {
|
|
|
805
811
|
if (body.kind !== undefined) rec.kind = String(body.kind)
|
|
806
812
|
if (body.importance !== undefined) rec.importance = Math.min(1, Math.max(0, Number(body.importance) || 0))
|
|
807
813
|
if (body.tags !== undefined) rec.tags = Array.isArray(body.tags) ? body.tags.map(String) : []
|
|
814
|
+
if (body.triggers !== undefined) rec.triggers = Array.isArray(body.triggers) ? body.triggers.map(String) : []
|
|
808
815
|
if (body.supersededBy !== undefined) rec.supersedes = body.supersededBy === null ? undefined : String(body.supersededBy)
|
|
816
|
+
// 过期时间:null 清除;expiresAt 绝对时间优先;ttlMs 相对当前时间。
|
|
817
|
+
if (body.expiredAt === null) delete rec.expiredAt
|
|
818
|
+
else if (typeof body.expiresAt === 'number') rec.expiredAt = body.expiresAt
|
|
819
|
+
else if (typeof body.ttlMs === 'number' && body.ttlMs > 0) rec.expiredAt = Date.now() + body.ttlMs
|
|
809
820
|
rec.updatedAt = Date.now()
|
|
810
821
|
// 改动本身要进增量(flush 只落增量,不再整份快照覆盖);向量作废靠 vectorsDirty。
|
|
811
822
|
s.insert(rec)
|
package/lib/provider/index.js
CHANGED
|
@@ -686,7 +686,7 @@ export class LocalMemoryService {
|
|
|
686
686
|
*
|
|
687
687
|
* 默认排除已被 supersede 的旧记录;需要历史/审计时显式 includeSuperseded。
|
|
688
688
|
*/
|
|
689
|
-
listSync(input
|
|
689
|
+
listSync(input = {}) {
|
|
690
690
|
const wanted = (input.scopes ?? []).map((scope) => this.canonicalScope(scope))
|
|
691
691
|
const seen = new Set ()
|
|
692
692
|
const stores = []
|
|
@@ -697,10 +697,13 @@ export class LocalMemoryService {
|
|
|
697
697
|
if (s) stores.push(s)
|
|
698
698
|
}
|
|
699
699
|
const retired = input.includeSuperseded ? new Set () : new Set(this.supersededIds(wanted))
|
|
700
|
+
const now = Date.now()
|
|
700
701
|
const out = []
|
|
701
702
|
for (const s of stores) {
|
|
702
703
|
for (const rec of s.records.values()) {
|
|
703
704
|
if (retired.has(rec.id)) continue
|
|
705
|
+
// 过期 state 默认不参与任何自动注入/规则渲染;memory_get(id) 与面板仍可取。
|
|
706
|
+
if (!input.includeExpired && rec.expiredAt !== undefined && rec.expiredAt < now) continue
|
|
704
707
|
if (input.kind !== undefined && rec.kind !== input.kind) continue
|
|
705
708
|
out.push(rec)
|
|
706
709
|
}
|
|
@@ -1336,6 +1339,20 @@ export class LocalMemoryService {
|
|
|
1336
1339
|
if (input.supersedes && !this.store.scope(scope).records.get(input.supersedes)) {
|
|
1337
1340
|
throw memoryError('SUPERSEDES_UNKNOWN', `supersedes target ${input.supersedes} not found in ${scope}`)
|
|
1338
1341
|
}
|
|
1342
|
+
// 状态型记忆(state)默认带 TTL:写入时算一次绝对 expiresAt,读取时只比较时间。
|
|
1343
|
+
// 显式 expiresAt 优先;ttlMs 对任何 kind 都生效(便于测试/特殊用途)。
|
|
1344
|
+
const timing = input
|
|
1345
|
+
const explicitExpiresAt = typeof timing.expiresAt === 'number' && Number.isFinite(timing.expiresAt)
|
|
1346
|
+
? timing.expiresAt
|
|
1347
|
+
: undefined
|
|
1348
|
+
const explicitTtl = typeof timing.ttlMs === 'number' && Number.isFinite(timing.ttlMs) && timing.ttlMs > 0
|
|
1349
|
+
? timing.ttlMs
|
|
1350
|
+
: undefined
|
|
1351
|
+
const stateTtl = input.kind === 'state' ? this.injectSettings().state.ttlMs : undefined
|
|
1352
|
+
const expiresAt = explicitExpiresAt
|
|
1353
|
+
?? (explicitTtl !== undefined ? Date.now() + explicitTtl
|
|
1354
|
+
: stateTtl !== undefined ? Date.now() + stateTtl : undefined)
|
|
1355
|
+
const saveInput = expiresAt !== undefined ? { ...input, expiredAt: expiresAt } : input
|
|
1339
1356
|
// design §11: keep credentials out of the plaintext store and the embedder
|
|
1340
1357
|
if (this.cfg.secrets.enabled) {
|
|
1341
1358
|
const findings = scanSecrets(content)
|
|
@@ -1347,7 +1364,7 @@ export class LocalMemoryService {
|
|
|
1347
1364
|
}
|
|
1348
1365
|
}
|
|
1349
1366
|
const { record } = this.store.save(
|
|
1350
|
-
{ ...
|
|
1367
|
+
{ ...saveInput, scope, content },
|
|
1351
1368
|
{ importance: input.source?.origin === 'auto' ? 0.4 : 0.8, source: { origin: 'explicit' } },
|
|
1352
1369
|
)
|
|
1353
1370
|
void this.embedRecord(record).catch(() => {})
|
|
@@ -1475,8 +1492,10 @@ export class LocalMemoryService {
|
|
|
1475
1492
|
// 面板的语义检索复用这条流水线,但它是"看一眼"的浏览:不写访问计数,也不在
|
|
1476
1493
|
// 没有任何向量时白跑一次嵌入请求。这两个开关只给面板的 searchForPanel 用,
|
|
1477
1494
|
// 工具 / 注入调用不会传。
|
|
1478
|
-
const internal = query
|
|
1495
|
+
const internal = query
|
|
1479
1496
|
const allowVector = internal.vector !== false && this.cfg.vector.enabled && this.cfg.vector.configured
|
|
1497
|
+
const includeSuperseded = internal.includeSuperseded === true
|
|
1498
|
+
const includeExpired = internal.includeExpired === true
|
|
1480
1499
|
|
|
1481
1500
|
// 候选 scope:显式 pin(单值)优先;否则用调用方给的合并集合(query.scopes,
|
|
1482
1501
|
// 工具 / 注入传"会话 + 项目 + global");两者都没有才退回旧的 project + global。
|
|
@@ -1496,6 +1515,15 @@ export class LocalMemoryService {
|
|
|
1496
1515
|
stores.push({ store, scope, isGlobal: scope === 'global' })
|
|
1497
1516
|
}
|
|
1498
1517
|
|
|
1518
|
+
// 退役(superseded)记录默认不参与召回:它们只是被新记录修正过的历史版本,
|
|
1519
|
+
// 继续返回会让模型拿旧结论当现行事实。memory_get(id)/面板历史仍可直接取。
|
|
1520
|
+
const retired = new Set ()
|
|
1521
|
+
for (const { store } of stores) {
|
|
1522
|
+
for (const record of store.records.values()) {
|
|
1523
|
+
if (record.supersedes) retired.add(record.supersedes)
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1499
1527
|
// --- channel 1: lexical recall, scored per scope with local IDF ---
|
|
1500
1528
|
|
|
1501
1529
|
const candidates = new Map ()
|
|
@@ -1509,7 +1537,8 @@ export class LocalMemoryService {
|
|
|
1509
1537
|
const stats = buildStats(tokenSets)
|
|
1510
1538
|
for (let i = 0; i < all.length; i++) {
|
|
1511
1539
|
const rec = all[i]
|
|
1512
|
-
if (rec.expiredAt && rec.expiredAt < now) continue
|
|
1540
|
+
if (!includeExpired && rec.expiredAt && rec.expiredAt < now) continue
|
|
1541
|
+
if (!includeSuperseded && retired.has(rec.id)) continue
|
|
1513
1542
|
const lex = lexicalScore(queryTokens, tokenSets[i] , stats)
|
|
1514
1543
|
if (lex <= 0) continue
|
|
1515
1544
|
// Weak-only overlap (no shared word/bigram) must clear the "real hit"
|
|
@@ -1539,30 +1568,27 @@ export class LocalMemoryService {
|
|
|
1539
1568
|
}
|
|
1540
1569
|
}
|
|
1541
1570
|
for (const [id, sim] of vectorScores) {
|
|
1571
|
+
if (!includeSuperseded && retired.has(id)) continue
|
|
1542
1572
|
const existing = candidates.get(id)
|
|
1543
1573
|
if (existing) {
|
|
1544
1574
|
existing.recallScore = Math.max(existing.recallScore, sim) // best channel wins
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
}
|
|
1575
|
+
continue
|
|
1576
|
+
}
|
|
1577
|
+
const rec = this.findById(id)
|
|
1578
|
+
if (!rec) continue
|
|
1579
|
+
if (!includeExpired && rec.expiredAt !== undefined && rec.expiredAt < now) continue
|
|
1580
|
+
if (!(rec.scope === 'global' && sim < this.cfg.globalThreshold)) {
|
|
1581
|
+
candidates.set(id, { record: rec, recallScore: sim, isGlobal: rec.scope === 'global' })
|
|
1552
1582
|
}
|
|
1553
1583
|
}
|
|
1554
1584
|
}
|
|
1555
1585
|
|
|
1556
1586
|
// shape with recency/importance, sort, cut
|
|
1557
|
-
const supersededIds = new Set ()
|
|
1558
|
-
for (const { store } of stores) {
|
|
1559
|
-
for (const r of store.records.values()) if (r.supersedes) supersededIds.add(r.supersedes)
|
|
1560
|
-
}
|
|
1561
1587
|
const shape = (c ) =>
|
|
1562
1588
|
recordScore(c.recallScore, c.record.importance, c.record.accessCount, c.record.lastAccessedAt, {
|
|
1563
1589
|
now,
|
|
1564
1590
|
halfLifeDays: this.cfg.halfLifeDays,
|
|
1565
|
-
superseded:
|
|
1591
|
+
superseded: retired.has(c.record.id),
|
|
1566
1592
|
})
|
|
1567
1593
|
|
|
1568
1594
|
// 稳定 tie-break:同分时 session > project > 自定义 > global,再同分按 record id ——
|
|
@@ -1657,9 +1683,12 @@ export class LocalMemoryService {
|
|
|
1657
1683
|
text: input.text,
|
|
1658
1684
|
scopes,
|
|
1659
1685
|
topK: input.topK ?? 20,
|
|
1686
|
+
// 面板是**历史浏览器**:退役/过期记录也要能搜到,否则用户无法从面板确认旧版本。
|
|
1687
|
+
includeSuperseded: true,
|
|
1688
|
+
includeExpired: true,
|
|
1660
1689
|
vector,
|
|
1661
1690
|
touch: false,
|
|
1662
|
-
}
|
|
1691
|
+
} )
|
|
1663
1692
|
// 嵌入请求失败会被 EmbedClient 熔断(markDown),所以"跑完还健康"就等于向量通道
|
|
1664
1693
|
// 真的产出了分数;否则这次只能算词法排序。
|
|
1665
1694
|
const vectorRan = vector && this.embed.isHealthy
|
|
@@ -1718,18 +1747,29 @@ export class LocalMemoryService {
|
|
|
1718
1747
|
* 一律走 `peek`,所以列一个不存在的 scope 只会得到空数组,不会造出文件。
|
|
1719
1748
|
*/
|
|
1720
1749
|
async list(input = {}) {
|
|
1750
|
+
const internal = input
|
|
1721
1751
|
const wanted = (input.scope !== undefined
|
|
1722
1752
|
? [input.scope]
|
|
1723
1753
|
: (input.scopes ?? []).length > 0
|
|
1724
1754
|
? input.scopes
|
|
1725
1755
|
: [this.defaultProjectScope]).map((scope) => this.canonicalScope(scope))
|
|
1726
1756
|
const seen = new Set ()
|
|
1727
|
-
|
|
1757
|
+
const stores = []
|
|
1728
1758
|
for (const scope of wanted) {
|
|
1729
1759
|
if (seen.has(scope)) continue
|
|
1730
1760
|
seen.add(scope)
|
|
1731
1761
|
const store = this.store.peek(scope)
|
|
1732
|
-
if (store)
|
|
1762
|
+
if (store) stores.push(store)
|
|
1763
|
+
}
|
|
1764
|
+
const retired = internal.includeSuperseded ? new Set () : new Set(this.supersededIds(wanted))
|
|
1765
|
+
const now = Date.now()
|
|
1766
|
+
let records = []
|
|
1767
|
+
for (const store of stores) {
|
|
1768
|
+
for (const rec of store.records.values()) {
|
|
1769
|
+
if (retired.has(rec.id)) continue
|
|
1770
|
+
if (internal.includeExpired !== true && rec.expiredAt !== undefined && rec.expiredAt < now) continue
|
|
1771
|
+
records.push(rec)
|
|
1772
|
+
}
|
|
1733
1773
|
}
|
|
1734
1774
|
if (input.kind) records = records.filter((r) => r.kind === input.kind)
|
|
1735
1775
|
records.sort((a, b) => b.updatedAt - a.updatedAt)
|
|
@@ -1771,6 +1811,8 @@ export class LocalMemoryService {
|
|
|
1771
1811
|
|
|
1772
1812
|
|
|
1773
1813
|
|
|
1814
|
+
|
|
1815
|
+
|
|
1774
1816
|
) {
|
|
1775
1817
|
const normalized = normalizeScopeInput(input.scope)
|
|
1776
1818
|
if (!normalized.ok) throw memoryError('SCOPE_INVALID', normalized.message)
|
|
@@ -1783,7 +1825,9 @@ export class LocalMemoryService {
|
|
|
1783
1825
|
content: String(input.content ?? ''),
|
|
1784
1826
|
...(input.tags !== undefined ? { tags: input.tags } : {}),
|
|
1785
1827
|
...(input.importance !== undefined ? { importance: input.importance } : {}),
|
|
1786
|
-
|
|
1828
|
+
...(input.ttlMs !== undefined ? { ttlMs: input.ttlMs } : {}),
|
|
1829
|
+
...(input.expiresAt !== undefined ? { expiresAt: input.expiresAt } : {}),
|
|
1830
|
+
} )
|
|
1787
1831
|
}
|
|
1788
1832
|
|
|
1789
1833
|
/**
|
|
@@ -33,6 +33,19 @@
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
|
|
36
|
+
/** 状态型记忆(kind=state)的默认有效期。过期后默认隐藏,但 memory_get 仍可取。 */
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
/** 流程索引:系统提示里按当前工作区/触发词列出的 procedure 目录。 */
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
36
49
|
|
|
37
50
|
|
|
38
51
|
|
|
@@ -94,15 +107,21 @@ export const DEFAULT_AUTO_RECALL_TAGS = [
|
|
|
94
107
|
|
|
95
108
|
|
|
96
109
|
|
|
110
|
+
|
|
111
|
+
|
|
97
112
|
|
|
98
113
|
|
|
99
114
|
|
|
100
115
|
|
|
116
|
+
export const DEFAULT_STATE_TTL_MS = 7 * 86_400_000
|
|
117
|
+
|
|
101
118
|
export const defaultInjectSettings = {
|
|
102
119
|
recall: { enabled: true, topK: 5, tokenBudget: 500 },
|
|
103
120
|
digest: { enabled: true, topK: 8, tokenBudget: 800, hintBudget: 160, hintMaxItems: 6 },
|
|
104
121
|
impression: { enabled: true, chars: 120 },
|
|
105
122
|
policy: { enabled: true },
|
|
123
|
+
state: { ttlMs: DEFAULT_STATE_TTL_MS },
|
|
124
|
+
procedures: { enabled: true, maxItems: 10, tokenBudget: 500 },
|
|
106
125
|
rules: { enabled: true, maxItems: 10, tokenBudget: 300, delta: true },
|
|
107
126
|
autoRecall: {
|
|
108
127
|
enabled: true,
|
|
@@ -148,10 +167,15 @@ export function normalizeInjectSettings(raw , base = def
|
|
|
148
167
|
const digest = section('digest')
|
|
149
168
|
const impression = section('impression')
|
|
150
169
|
const policy = section('policy')
|
|
170
|
+
const state = section('state')
|
|
171
|
+
const procedures = section('procedures')
|
|
151
172
|
const rules = section('rules')
|
|
152
173
|
const autoRecall = section('autoRecall')
|
|
153
174
|
const turnEnd = section('turnEnd')
|
|
154
175
|
const sidebar = section('sidebar')
|
|
176
|
+
// 兼容方案里的 `inject.stateTtlMs` 写法;新配置统一放到 state.ttlMs。
|
|
177
|
+
const legacyStateTtl = (r ).stateTtlMs
|
|
178
|
+
const stateTtlInput = state.ttlMs ?? legacyStateTtl
|
|
155
179
|
const rawTags = Array.isArray(autoRecall.tags) ? autoRecall.tags : base.autoRecall.tags
|
|
156
180
|
const normalizedTags = []
|
|
157
181
|
const seenTags = new Set ()
|
|
@@ -182,6 +206,14 @@ export function normalizeInjectSettings(raw , base = def
|
|
|
182
206
|
chars: clampInt(impression.chars, 40, 800, base.impression.chars),
|
|
183
207
|
},
|
|
184
208
|
policy: { enabled: bool(policy.enabled, base.policy.enabled) },
|
|
209
|
+
state: {
|
|
210
|
+
ttlMs: clampInt(stateTtlInput, 60_000, 365 * 86_400_000, base.state.ttlMs),
|
|
211
|
+
},
|
|
212
|
+
procedures: {
|
|
213
|
+
enabled: bool(procedures.enabled, base.procedures.enabled),
|
|
214
|
+
maxItems: clampInt(procedures.maxItems, 1, 30, base.procedures.maxItems),
|
|
215
|
+
tokenBudget: clampInt(procedures.tokenBudget, 50, 4000, base.procedures.tokenBudget),
|
|
216
|
+
},
|
|
185
217
|
rules: {
|
|
186
218
|
enabled: bool(rules.enabled, base.rules.enabled),
|
|
187
219
|
maxItems: clampInt(rules.maxItems, 1, 40, base.rules.maxItems),
|