@restregular/dsh-memory 0.4.0 → 0.5.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.
- package/README.md +231 -36
- package/lib/client.js +19 -7
- package/lib/inject/apply.js +27 -8
- package/lib/inject/inject.js +75 -3
- package/lib/panel.js +106 -12
- package/lib/provider/apply.js +40 -1
- package/lib/provider/index.js +270 -31
- package/lib/provider/scope.js +338 -0
- package/lib/provider/store.js +56 -9
- package/lib/tools/apply.js +123 -103
- package/lib/tools/index.js +370 -221
- package/package.json +1 -1
package/lib/panel.js
CHANGED
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
* - apply 整体兜底 catch:任何意外都不允许向外抛。
|
|
9
9
|
*
|
|
10
10
|
* 提供管理员 API(前缀路由 /memory/api):
|
|
11
|
-
* GET /status 各 scope 记录数 + 待嵌入数
|
|
11
|
+
* GET /status 各 scope 记录数 + 待嵌入数 + 展示标签
|
|
12
12
|
* GET /records ?scope=&q= 枚举/过滤记录
|
|
13
13
|
* POST /update {id, content?, kind?, importance?, tags?, supersededBy?}
|
|
14
14
|
* POST /delete {id}
|
|
15
|
-
* scope 惰性加载是同步的(store.
|
|
16
|
-
*
|
|
15
|
+
* scope 惰性加载是同步的(store.peek(),**读路径不创建**),无需 ready 门。内容编辑会作废旧
|
|
16
|
+
* 向量并加入 embedPending,由 provider 的回填队列重新嵌入。
|
|
17
17
|
*
|
|
18
18
|
* 安全(每个路由都过 refuseRequest,见下):
|
|
19
19
|
* 注册在裸 webServer 上的路由**既不在宿主的 /api 围栏之后,也不在浏览器登录
|
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
* 可被无 session 调用 = 任意代码执行)。这里的读接口能读出全部记忆,写接口能改写、
|
|
23
23
|
* 删除、清空,还能拉起/卸载向量服务,所以必须自己问一次。
|
|
24
24
|
*/
|
|
25
|
+
// scope 规范化与 provider 共用同一份实现。路径按**源码树位置**写:构建脚本的改写表
|
|
26
|
+
// 会把它落成产物里的 `../provider/scope.js`(`lib/panel.js` → `lib/provider/scope.js`),
|
|
27
|
+
// 在源码树里同一路径直接命中 source/memory-local/src —— 测试与真实加载都能解析。
|
|
28
|
+
import { normalizeScopeInput } from './provider/scope.js'
|
|
29
|
+
|
|
25
30
|
|
|
26
31
|
function writeJson(res, body, status = 200) {
|
|
27
32
|
try {
|
|
@@ -125,6 +130,62 @@ async function readJsonBody(req) {
|
|
|
125
130
|
return raw.length > 0 ? JSON.parse(raw) : {}
|
|
126
131
|
}
|
|
127
132
|
|
|
133
|
+
/**
|
|
134
|
+
* scope 键 → **不需要查宿主服务**的展示标签。
|
|
135
|
+
* global → 「全局」
|
|
136
|
+
* project:<cwd> → cwd
|
|
137
|
+
* session:<id> → 「会话 <id 前 8 位>」
|
|
138
|
+
* 其他 → 原名(自定义 scope)
|
|
139
|
+
*/
|
|
140
|
+
function fallbackScopeLabel(scope) {
|
|
141
|
+
if (scope === 'global') return '全局'
|
|
142
|
+
if (scope.startsWith('project:')) return scope.slice('project:'.length)
|
|
143
|
+
if (scope.startsWith('session:')) return `会话 ${scope.slice('session:'.length, 'session:'.length + 8)}`
|
|
144
|
+
return scope
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* scope 键 → 展示标签。在 fallback 之上多一步:会话 scope 尽力取会话标题
|
|
149
|
+
* (宿主 `sessionTitle` 服务的 `get(session)` → `{ title }`,见 @deepseek-ai/dsh-session-title)。
|
|
150
|
+
* 所有宿主服务访问都在 try/catch 里:面板绝不能因为拿不到标题而失败。
|
|
151
|
+
*/
|
|
152
|
+
function scopeLabel(ctx, scope) {
|
|
153
|
+
if (!scope.startsWith('session:')) return fallbackScopeLabel(scope)
|
|
154
|
+
const id = scope.slice('session:'.length)
|
|
155
|
+
const short = id.slice(0, 8)
|
|
156
|
+
try {
|
|
157
|
+
const session = ctx?.get?.('sessions')?.get?.(id)
|
|
158
|
+
if (session !== undefined) {
|
|
159
|
+
const title = ctx.get('sessionTitle')?.get?.(session)?.title
|
|
160
|
+
if (typeof title === 'string' && title.length > 0) return `${title}(${short})`
|
|
161
|
+
}
|
|
162
|
+
} catch { /* 标题是尽力而为 */ }
|
|
163
|
+
return `会话 ${short}`
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** 会话 scope 查标题的上限:标题要读会话事件日志,scope 多了它就成了 /status 的大头。 */
|
|
167
|
+
const TITLE_LOOKUP_LIMIT = 50
|
|
168
|
+
/** discovery(读每个 scope 文件的第一条记录)结果的缓存时长。 */
|
|
169
|
+
const DISCOVER_CACHE_MS = 3000
|
|
170
|
+
/** 每个 memory 服务的发现缓存。面板看不到进程数,所以按服务实例各存一份。 */
|
|
171
|
+
const discoveryCache = new WeakMap()
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 发现磁盘上的 scope,3 秒内只做一次。
|
|
175
|
+
* 面板每个请求都想要一份完整清单(别的进程写下的 scope 也要看得见),而 discovery 要
|
|
176
|
+
* 读每个文件的第一条记录 —— 没有缓存的话刷新一下就是一次全目录扫描。
|
|
177
|
+
*/
|
|
178
|
+
function discoverScopes(memory, store) {
|
|
179
|
+
const now = Date.now()
|
|
180
|
+
const cached = discoveryCache.get(memory)
|
|
181
|
+
if (cached !== undefined && now - cached.at < DISCOVER_CACHE_MS) return
|
|
182
|
+
discoveryCache.set(memory, { at: now })
|
|
183
|
+
try {
|
|
184
|
+
if (typeof memory.discoverScopes === 'function') memory.discoverScopes()
|
|
185
|
+
else store.loadDiscovered?.()
|
|
186
|
+
} catch { /* discovery is best-effort */ }
|
|
187
|
+
}
|
|
188
|
+
|
|
128
189
|
function wireRecord(rec, scopeStore) {
|
|
129
190
|
return {
|
|
130
191
|
id: rec.id,
|
|
@@ -164,14 +225,18 @@ async function handle(ctx, routePath, req, res) {
|
|
|
164
225
|
const store = memory.store
|
|
165
226
|
// Scopes are created lazily, so a fresh process only knows the ones it has
|
|
166
227
|
// touched. A management view must see everything written on disk (project
|
|
167
|
-
// memories from earlier sessions included) — best-effort discovery.
|
|
168
|
-
|
|
228
|
+
// memories from earlier sessions included) — best-effort discovery, cached.
|
|
229
|
+
discoverScopes(memory, store)
|
|
169
230
|
|
|
170
231
|
if (method === 'GET' && path === '/status') {
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
records: s.records.size,
|
|
174
|
-
|
|
232
|
+
const stats = typeof memory.scopeStats === 'function'
|
|
233
|
+
? memory.scopeStats()
|
|
234
|
+
: store.allScopes().map((s) => ({ scope: s.scope, records: s.records.size, pendingEmbeds: s.embedPending.size }))
|
|
235
|
+
// 会话标题是尽力而为的增强,而且要读会话事件日志:只给最近活跃的前 N 个查,
|
|
236
|
+
// 其余直接用「会话 <id 前 8 位>」。列表本身**始终**完整(不受这个上限影响)。
|
|
237
|
+
const scopes = stats.map((s, index) => ({
|
|
238
|
+
...s,
|
|
239
|
+
label: index < TITLE_LOOKUP_LIMIT ? scopeLabel(ctx, s.scope) : fallbackScopeLabel(s.scope),
|
|
175
240
|
}))
|
|
176
241
|
let embedServer = null
|
|
177
242
|
// older providers have no lifecycle API — the panel simply omits the card
|
|
@@ -259,10 +324,34 @@ async function handle(ctx, routePath, req, res) {
|
|
|
259
324
|
return
|
|
260
325
|
}
|
|
261
326
|
if (method === 'GET' && path === '/records') {
|
|
262
|
-
const
|
|
327
|
+
const rawScope = url.searchParams.get('scope')
|
|
263
328
|
const query = (url.searchParams.get('q') ?? '').trim().toLowerCase()
|
|
264
|
-
|
|
265
|
-
|
|
329
|
+
// scope 先过同一套规范化:非法 → 400(而不是当成一个陌生 scope 去建文件);
|
|
330
|
+
// 合法但不存在的 scope → 空列表。`peek`/`peekScope`,**绝不通过 store.scope() 创建**。
|
|
331
|
+
let scope = null
|
|
332
|
+
if (rawScope !== null && rawScope.trim() !== '') {
|
|
333
|
+
const normalized = normalizeScopeInput(rawScope)
|
|
334
|
+
if (!normalized.ok) throw new ApiError(400, normalized.code, normalized.message)
|
|
335
|
+
if (normalized.kind === 'all') {
|
|
336
|
+
scope = null // 全部
|
|
337
|
+
} else if (normalized.kind === 'current-session' || normalized.kind === 'current-project') {
|
|
338
|
+
// 面板不在某个 agent 里:按 provider 的读取集合解析('project' → 当前项目键);
|
|
339
|
+
// 解析不出会话时就退化为全部,而不是报错。
|
|
340
|
+
const refs = typeof memory.resolveReadScopes === 'function' ? memory.resolveReadScopes(undefined) : []
|
|
341
|
+
scope = refs.length > 0 ? refs : null
|
|
342
|
+
} else {
|
|
343
|
+
scope = normalized.scope ?? null
|
|
344
|
+
}
|
|
345
|
+
} else {
|
|
346
|
+
scope = null
|
|
347
|
+
}
|
|
348
|
+
const stores = scope === null
|
|
349
|
+
? store.allScopes()
|
|
350
|
+
: (Array.isArray(scope) ? scope : [scope])
|
|
351
|
+
.map((s) => store.peek(s))
|
|
352
|
+
.filter((s) => s !== undefined)
|
|
353
|
+
let records = []
|
|
354
|
+
for (const s of stores) records.push(...collectScope(s))
|
|
266
355
|
if (query) {
|
|
267
356
|
records = records.filter((r) =>
|
|
268
357
|
r.content.toLowerCase().includes(query)
|
|
@@ -321,6 +410,8 @@ async function handle(ctx, routePath, req, res) {
|
|
|
321
410
|
}
|
|
322
411
|
|
|
323
412
|
export const name = 'dsh-memory-panel'
|
|
413
|
+
/** 路由处理器(导出供测试直接调用:围栏、规范化、peek 行为都在这里)。 */
|
|
414
|
+
export { handle as handleRequest }
|
|
324
415
|
export function apply(ctx, config = {}) {
|
|
325
416
|
const routePath = String(config.route || '/memory/api').replace(/\/$/, '')
|
|
326
417
|
try {
|
|
@@ -351,3 +442,6 @@ export function apply(ctx, config = {}) {
|
|
|
351
442
|
console.error('[dsh-memory-panel] apply 意外失败(已吞掉,不影响启动):', error?.message)
|
|
352
443
|
}
|
|
353
444
|
}
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
//# sourceURL=source/memory-panel/lib/index.js
|
package/lib/provider/apply.js
CHANGED
|
@@ -14,6 +14,7 @@ import { Service, } from '@deepseek-ai/cordis'
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
|
|
17
|
+
|
|
17
18
|
|
|
18
19
|
import { LocalMemoryService, } from './index.js'
|
|
19
20
|
|
|
@@ -111,19 +112,57 @@ export class CordisMemoryService extends Service {
|
|
|
111
112
|
return this.impl.search(query)
|
|
112
113
|
}
|
|
113
114
|
|
|
114
|
-
forget(input
|
|
115
|
+
forget(input ) {
|
|
115
116
|
return this.impl.forget(input)
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
list(input ) {
|
|
119
120
|
return this.impl.list(input)
|
|
120
121
|
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 当前 agent 上下文 → 默认合并读取范围(会话 + 项目 + global,按开关与 key 去重)。
|
|
125
|
+
* 工具与召回注入都走它,保证与 `resolveWriteScope` 是同一套口径。
|
|
126
|
+
*/
|
|
127
|
+
resolveReadScopes(agent ) {
|
|
128
|
+
return this.impl.resolveReadScopes(agent)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 写路径默认 scope(省略 scope 时落到哪)。 */
|
|
132
|
+
resolveWriteScope(agent , requested ) {
|
|
133
|
+
return this.impl.resolveWriteScope(agent, requested)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** 作用域清单(名字 + 条数),面板 /status 与 memory_list 共用。 */
|
|
137
|
+
scopeStats() {
|
|
138
|
+
return this.impl.scopeStats()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 作用域清单的一行文字;SCOPE_LIMIT 错误提示里会带上它。 */
|
|
142
|
+
scopeInventory(limit ) {
|
|
143
|
+
return this.impl.scopeInventory(limit)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 把磁盘上已有的 scope 全部发现并加载(读 "all" / 面板枚举前调用;带 3 秒缓存)。 */
|
|
147
|
+
discoverScopes() {
|
|
148
|
+
return this.impl.discoverScopes()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 当前进程已知的 scope 键(只读,不触发发现)。 */
|
|
152
|
+
knownScopes() {
|
|
153
|
+
return this.impl.knownScopes()
|
|
154
|
+
}
|
|
121
155
|
}
|
|
122
156
|
|
|
123
157
|
export function apply(ctx , config = {}) {
|
|
124
158
|
class ScopedMemoryService extends CordisMemoryService {
|
|
125
159
|
constructor(ctx2 ) {
|
|
126
160
|
super(ctx2, config)
|
|
161
|
+
// 写路径默认口径来自 tools 小节(defaultScope 缺省 = 会话);项目键缺省
|
|
162
|
+
// project:<cwd>,即 DSH 进程的启动目录 = 这个 profile 目录。
|
|
163
|
+
const tools = (config ).tools
|
|
164
|
+
this.impl.setProjectScope((tools?.projectScope ?? `project:${process.cwd()}`) )
|
|
165
|
+
if (tools?.defaultScope !== undefined) this.impl.setDefaultScope(tools.defaultScope)
|
|
127
166
|
}
|
|
128
167
|
}
|
|
129
168
|
ctx.plugin(ScopedMemoryService)
|
package/lib/provider/index.js
CHANGED
|
@@ -14,7 +14,15 @@
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
|
|
17
|
-
import { MemoryStore, } from './store.js'
|
|
17
|
+
import { MemoryStore, scopeFileName, } from './store.js'
|
|
18
|
+
import {
|
|
19
|
+
isCustomScope,
|
|
20
|
+
resolveCurrentScopes,
|
|
21
|
+
scopeKind,
|
|
22
|
+
scopeTier,
|
|
23
|
+
writeScopeForDefault,
|
|
24
|
+
|
|
25
|
+
} from './scope.js'
|
|
18
26
|
import { buildStats, lexicalScore, recordScore, tokenize, cosine, normalizeInPlace, hasStrongHit } from './scoring.js'
|
|
19
27
|
import { EmbedClient } from './embed.js'
|
|
20
28
|
import { DailyBudget, blendScores, rerank, } from './rerank.js'
|
|
@@ -57,7 +65,16 @@ function isInside(parent , child ) {
|
|
|
57
65
|
|
|
58
66
|
|
|
59
67
|
|
|
60
|
-
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
61
78
|
|
|
62
79
|
|
|
63
80
|
|
|
@@ -136,6 +153,12 @@ function isInside(parent , child ) {
|
|
|
136
153
|
|
|
137
154
|
|
|
138
155
|
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
139
162
|
|
|
140
163
|
|
|
141
164
|
|
|
@@ -358,10 +381,20 @@ export function resolveConfig(config = {}) {
|
|
|
358
381
|
const configured = endpointConfigured || spawnConfigured
|
|
359
382
|
const vectorEnabled = v.enabled ?? configured
|
|
360
383
|
const flavor = selection.flavor ?? 'cpu'
|
|
384
|
+
// scopes.custom 既可能是布尔(简写)也可能是对象 —— 两种都要认,旧配置(只有
|
|
385
|
+
// global/project 两个布尔)当然照常工作。
|
|
386
|
+
const customCfg = config.scopes?.custom
|
|
387
|
+
const customEnabled = typeof customCfg === 'boolean' ? customCfg : (customCfg?.enabled ?? true)
|
|
388
|
+
const customMax = typeof customCfg === 'object' && customCfg !== null && typeof customCfg.max === 'number'
|
|
389
|
+
? customCfg.max
|
|
390
|
+
: 200
|
|
361
391
|
return {
|
|
362
392
|
storeDir,
|
|
363
393
|
globalEnabled: config.scopes?.global ?? true,
|
|
364
394
|
projectEnabled: config.scopes?.project ?? true,
|
|
395
|
+
sessionEnabled: config.scopes?.session ?? true,
|
|
396
|
+
customEnabled,
|
|
397
|
+
customMaxScopes: customMax,
|
|
365
398
|
topK: config.retrieval?.topK ?? 5,
|
|
366
399
|
recallN: config.retrieval?.recallN ?? 30,
|
|
367
400
|
globalThreshold: config.retrieval?.globalThreshold ?? 0.12,
|
|
@@ -448,8 +481,12 @@ export class LocalMemoryService {
|
|
|
448
481
|
rerankCache = new RerankCache()
|
|
449
482
|
/** Host-provided one-shot LLM call; null = rerank disabled (recall order). */
|
|
450
483
|
llmInvoke = null
|
|
451
|
-
/**
|
|
452
|
-
|
|
484
|
+
/** 写路径的默认口径(tools.defaultScope);'session' 拿不到会话时回退 project。 */
|
|
485
|
+
defaultWriteScope = 'session'
|
|
486
|
+
/** 当前项目 scope 键;缺省 project:<启动 cwd>。 */
|
|
487
|
+
projectScope = `project:${process.cwd()}`
|
|
488
|
+
/** 上次发现磁盘 scope 的时间(限流用;0 = 还没发现过)。 */
|
|
489
|
+
discoveredAt = 0
|
|
453
490
|
|
|
454
491
|
constructor(config = {}) {
|
|
455
492
|
this.cfg = resolveConfig(config)
|
|
@@ -461,7 +498,13 @@ export class LocalMemoryService {
|
|
|
461
498
|
// whenever the dimensions matched — and the next flush would then rewrite the
|
|
462
499
|
// header as the new model, making the mismatch permanent.
|
|
463
500
|
this.store.setVecIdentity(this.cfg.vector.model, this.cfg.vector.dims)
|
|
464
|
-
|
|
501
|
+
// 会话 scope 是按会话动态生成的,**不能**在这里 eager 加载(加载谁?);只预载
|
|
502
|
+
// project 与 global 这两个稳定作用域。其余按需 peek/loadDiscovered。
|
|
503
|
+
// eager 只加载"已存在"的 scope(peek),不会为不存在的 scope 建空文件。
|
|
504
|
+
const eager = []
|
|
505
|
+
if (this.cfg.projectEnabled) eager.push(this.projectScope)
|
|
506
|
+
if (this.cfg.globalEnabled) eager.push('global')
|
|
507
|
+
for (const scope of eager) this.store.peek(scope)
|
|
465
508
|
this.embed = new EmbedClient({
|
|
466
509
|
endpoint: this.cfg.vector.endpoint,
|
|
467
510
|
model: this.cfg.vector.model,
|
|
@@ -936,8 +979,22 @@ export class LocalMemoryService {
|
|
|
936
979
|
return this.backfillAll({ throttleMs: 100 })
|
|
937
980
|
}
|
|
938
981
|
|
|
939
|
-
|
|
940
|
-
|
|
982
|
+
/**
|
|
983
|
+
* 写路径默认 scope 口径(tools.defaultScope 取值)。
|
|
984
|
+
* 兼容:旧调用方传的是**具体 scope 键**(如 'project:<cwd>'),此时按"固定项目"处理。
|
|
985
|
+
*/
|
|
986
|
+
setDefaultScope(scope ) {
|
|
987
|
+
if (scope === 'session' || scope === 'project' || scope === 'global') {
|
|
988
|
+
this.defaultWriteScope = scope
|
|
989
|
+
return
|
|
990
|
+
}
|
|
991
|
+
this.defaultWriteScope = 'project'
|
|
992
|
+
this.projectScope = scope
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/** 固定项目 scope(默认 project:<启动 cwd>);宿主装配用它对齐 session.header.cwd。 */
|
|
996
|
+
setProjectScope(scope ) {
|
|
997
|
+
this.projectScope = scope
|
|
941
998
|
}
|
|
942
999
|
|
|
943
1000
|
/**
|
|
@@ -950,22 +1007,104 @@ export class LocalMemoryService {
|
|
|
950
1007
|
this.llmInvoke = invoke
|
|
951
1008
|
}
|
|
952
1009
|
|
|
953
|
-
|
|
954
|
-
|
|
1010
|
+
/** 写路径默认口径('session' | 'project' | 'global')。 */
|
|
1011
|
+
get activeDefaultScope() {
|
|
1012
|
+
return this.defaultWriteScope
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
get activeProjectScope() {
|
|
1016
|
+
return this.projectScope
|
|
955
1017
|
}
|
|
956
1018
|
|
|
957
1019
|
assertScopeAllowed(scope ) {
|
|
958
|
-
if (scope === 'global'
|
|
959
|
-
throw memoryError('SCOPE_DISABLED', 'global scope is disabled by configuration')
|
|
1020
|
+
if (scope === 'global') {
|
|
1021
|
+
if (!this.cfg.globalEnabled) throw memoryError('SCOPE_DISABLED', 'global scope is disabled by configuration')
|
|
1022
|
+
return
|
|
960
1023
|
}
|
|
961
|
-
if (scope.startsWith('project:')
|
|
962
|
-
throw memoryError('SCOPE_DISABLED', 'project scope is disabled by configuration')
|
|
1024
|
+
if (scope.startsWith('project:')) {
|
|
1025
|
+
if (!this.cfg.projectEnabled) throw memoryError('SCOPE_DISABLED', 'project scope is disabled by configuration')
|
|
1026
|
+
return
|
|
963
1027
|
}
|
|
1028
|
+
if (scope.startsWith('session:')) {
|
|
1029
|
+
if (!this.cfg.sessionEnabled) throw memoryError('SCOPE_DISABLED', 'session scope is disabled by configuration')
|
|
1030
|
+
return
|
|
1031
|
+
}
|
|
1032
|
+
if (!this.cfg.customEnabled) throw memoryError('SCOPE_DISABLED', 'custom scopes are disabled by configuration')
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
/**
|
|
1036
|
+
* scopes.custom.max 只统计**新建**的自定义 scope,而且只统计自定义的
|
|
1037
|
+
* (会话 / project / global 既不计入也不受限)。
|
|
1038
|
+
*
|
|
1039
|
+
* 计数走 `store.customScopeCount()`,它会顺带扫一遍磁盘 —— 只在"内存里的已加载数已经触到
|
|
1040
|
+
* 上限"时才真扫,否则直接放行,避免每次保存都读一遍 projects/ 目录。
|
|
1041
|
+
*/
|
|
1042
|
+
assertCustomScopeBudget(scope ) {
|
|
1043
|
+
if (!isCustomScope(scope)) return
|
|
1044
|
+
if (this.store.peek(scope) !== undefined) return // 已存在(内存或磁盘)→ 写入不受限
|
|
1045
|
+
const loaded = this.store.allScopes().filter((s) => isCustomScope(s.scope)).length
|
|
1046
|
+
if (loaded < this.cfg.customMaxScopes && this.store.customScopeCount() < this.cfg.customMaxScopes) return
|
|
1047
|
+
throw memoryError(
|
|
1048
|
+
'SCOPE_LIMIT',
|
|
1049
|
+
`custom scope limit reached (${this.cfg.customMaxScopes}); reuse an existing scope instead. ${this.scopeInventory(5)}`,
|
|
1050
|
+
)
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/** 作用域清单(名字 + 条数),给 SCOPE_LIMIT 之类的错误提示用。 */
|
|
1054
|
+
scopeInventory(limit = 20) {
|
|
1055
|
+
const scopes = this.scopeStats()
|
|
1056
|
+
const lines = scopes.slice(0, limit).map((s) => `${s.scope}(${s.records})`)
|
|
1057
|
+
const rest = scopes.length - lines.length
|
|
1058
|
+
return rest > 0 ? `${lines.join(', ')}, …另有 ${rest} 个` : lines.join(', ')
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* 写路径的 scope 解析:agent 上下文 → 当前会话 / 当前项目;模型省略 scope 时按
|
|
1063
|
+
* `tools.defaultScope`(默认 'session')落点。拿不到会话信息就回退 project,**不抛错**。
|
|
1064
|
+
*/
|
|
1065
|
+
resolveWriteScope(agent , requested ) {
|
|
1066
|
+
const refs = this.scopeRefs(agent)
|
|
1067
|
+
const want = requested?.trim()
|
|
1068
|
+
if (want === undefined || want === '' || want.toLowerCase() === 'session') {
|
|
1069
|
+
// scopes.session 关掉时"当前会话"不存在:按 defaultScope 口径回退(默认口径是
|
|
1070
|
+
// session 而会话被关 → project),而不是让写撞上 SCOPE_DISABLED。
|
|
1071
|
+
const session = this.cfg.sessionEnabled ? refs.session : undefined
|
|
1072
|
+
return writeScopeForDefault(this.defaultWriteScope, { ...refs, session })
|
|
1073
|
+
}
|
|
1074
|
+
if (want.toLowerCase() === 'project') return refs.project
|
|
1075
|
+
if (want.toLowerCase() === 'global') return 'global'
|
|
1076
|
+
return requested // 具体键(project:/session:/自定义名)由工具层 normalize 后传入
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/** agent 上下文 → { session?, project }(会话能否用由调用方按 scopes.session 决定)。 */
|
|
1080
|
+
scopeRefs(agent ) {
|
|
1081
|
+
return resolveCurrentScopes(agent, { projectScope: this.projectScope })
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
/**
|
|
1085
|
+
* 无显式 scope 时的**读取**合并集合:当前会话 + 当前项目 + global(各自开关生效、按 key 去重)。
|
|
1086
|
+
* 工具 / 注入都用它,保证"写进会话的东西,默认读得回来"。
|
|
1087
|
+
*/
|
|
1088
|
+
resolveReadScopes(agent ) {
|
|
1089
|
+
const refs = this.scopeRefs(agent)
|
|
1090
|
+
const out = []
|
|
1091
|
+
const push = (scope , enabled ) => {
|
|
1092
|
+
if (scope === undefined || !enabled || out.includes(scope)) return
|
|
1093
|
+
out.push(scope)
|
|
1094
|
+
}
|
|
1095
|
+
push(refs.session, this.cfg.sessionEnabled)
|
|
1096
|
+
push(refs.project, this.cfg.projectEnabled)
|
|
1097
|
+
push('global', this.cfg.globalEnabled)
|
|
1098
|
+
return out
|
|
964
1099
|
}
|
|
965
1100
|
|
|
966
1101
|
async save(input ) {
|
|
967
|
-
|
|
1102
|
+
// 省略 scope 时按当前会话落点 —— 会话上下文必须**从调用方带进来**
|
|
1103
|
+
// (工具 handler 拿得到 exec.agent,provider 自己看不到)。
|
|
1104
|
+
const scope = input.scope ?? this.resolveWriteScope((input ).agent)
|
|
968
1105
|
this.assertScopeAllowed(scope)
|
|
1106
|
+
// 自定义 scope 的**新建**受 scopes.custom.max 限制;写已存在的 scope 不受限。
|
|
1107
|
+
this.assertCustomScopeBudget(scope)
|
|
969
1108
|
let content = input.content?.trim()
|
|
970
1109
|
if (!content) throw memoryError('CONTENT_EMPTY', 'content must be a non-empty string')
|
|
971
1110
|
if (content.length > this.cfg.maxRecordChars) {
|
|
@@ -987,7 +1126,7 @@ export class LocalMemoryService {
|
|
|
987
1126
|
const { record } = this.store.save(
|
|
988
1127
|
{ ...input, scope, content },
|
|
989
1128
|
{ importance: input.source?.origin === 'auto' ? 0.4 : 0.8, source: { origin: 'explicit' } },
|
|
990
|
-
)
|
|
1129
|
+
)
|
|
991
1130
|
void this.embedRecord(record).catch(() => {})
|
|
992
1131
|
this.store.scheduleFlush()
|
|
993
1132
|
return record
|
|
@@ -1059,18 +1198,30 @@ export class LocalMemoryService {
|
|
|
1059
1198
|
}
|
|
1060
1199
|
|
|
1061
1200
|
async search(query ) {
|
|
1201
|
+
// 多 scope 合并由调用方显式给出(工具层传"会话 + 项目 + global");宿主类型里的
|
|
1202
|
+
// MemoryQuery 只有单值 scope,所以这里按结构化读一次可选数组,保持原语义不变。
|
|
1203
|
+
const queryScopes = (query ).scopes
|
|
1062
1204
|
const topK = query.topK ?? this.cfg.topK
|
|
1063
1205
|
const recallN = query.recallN ?? this.cfg.recallN
|
|
1064
1206
|
const now = Date.now()
|
|
1065
1207
|
const queryTokens = [...new Set(tokenize(query.text))]
|
|
1066
1208
|
|
|
1067
|
-
//
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1209
|
+
// 候选 scope:显式 pin(单值)优先;否则用调用方给的合并集合(query.scopes,
|
|
1210
|
+
// 工具 / 注入传"会话 + 项目 + global");两者都没有才退回旧的 project + global。
|
|
1211
|
+
// `peek` 而不是 `scope`:读路径不创建,不存在的 scope 直接跳过。
|
|
1212
|
+
const wanted = query.scope
|
|
1213
|
+
? [query.scope]
|
|
1214
|
+
: (queryScopes ?? []).length > 0
|
|
1215
|
+
? queryScopes
|
|
1216
|
+
: [this.projectScope, 'global']
|
|
1217
|
+
const stores = []
|
|
1218
|
+
const seen = new Set ()
|
|
1219
|
+
for (const scope of wanted) {
|
|
1220
|
+
if (seen.has(scope)) continue // 按 scope key 去重(也顺手消掉 defaultScope==='global' 时的重复 push)
|
|
1221
|
+
seen.add(scope)
|
|
1222
|
+
const store = this.store.peek(scope)
|
|
1223
|
+
if (!store) continue
|
|
1224
|
+
stores.push({ store, scope, isGlobal: scope === 'global' })
|
|
1074
1225
|
}
|
|
1075
1226
|
|
|
1076
1227
|
// --- channel 1: lexical recall, scored per scope with local IDF ---
|
|
@@ -1087,6 +1238,8 @@ export class LocalMemoryService {
|
|
|
1087
1238
|
if (lex <= 0) continue
|
|
1088
1239
|
// Weak-only overlap (no shared word/bigram) must clear the "real hit"
|
|
1089
1240
|
// floor, not the permissive threshold — kills single-char collisions.
|
|
1241
|
+
// 阈值只分两档:global 用 globalThreshold,其余(session / project / 自定义)用
|
|
1242
|
+
// projectThreshold —— 不新增配置项。
|
|
1090
1243
|
const floor = isGlobal ? this.cfg.globalThreshold : this.cfg.projectThreshold
|
|
1091
1244
|
const effective = hasStrongHit(queryTokens, tokenSets[i] ) ? floor : Math.max(floor, REAL_HIT_FLOOR)
|
|
1092
1245
|
if (lex < effective) continue
|
|
@@ -1136,7 +1289,13 @@ export class LocalMemoryService {
|
|
|
1136
1289
|
superseded: supersededIds.has(c.record.id),
|
|
1137
1290
|
})
|
|
1138
1291
|
|
|
1139
|
-
|
|
1292
|
+
// 稳定 tie-break:同分时 session > project > 自定义 > global,再同分按 record id ——
|
|
1293
|
+
// 否则 Map 的插入顺序(取决于 scope 顺序)会让同一查询在不同进程间给出不同结果。
|
|
1294
|
+
const ranked = [...candidates.values()]
|
|
1295
|
+
.sort((a, b) => shape(b) - shape(a)
|
|
1296
|
+
|| scopeTier(a.record.scope) - scopeTier(b.record.scope)
|
|
1297
|
+
|| a.record.id.localeCompare(b.record.id))
|
|
1298
|
+
.slice(0, recallN)
|
|
1140
1299
|
|
|
1141
1300
|
// --- channel 3: LLM rerank (optional; any skip/failure keeps recall order) ---
|
|
1142
1301
|
const finalScores = new Map (ranked.map((c) => [c.id, shape(c)]))
|
|
@@ -1176,19 +1335,32 @@ export class LocalMemoryService {
|
|
|
1176
1335
|
return undefined
|
|
1177
1336
|
}
|
|
1178
1337
|
|
|
1179
|
-
async forget(input
|
|
1338
|
+
async forget(input ) {
|
|
1180
1339
|
const ids = []
|
|
1181
1340
|
if (input.id) {
|
|
1341
|
+
// 按 id 删除天然跨 scope:调用方给的是精确 id,不存在误删别的 scope 的风险。
|
|
1182
1342
|
for (const s of this.store.allScopes()) {
|
|
1183
1343
|
if (s.remove(input.id)) ids.push(input.id)
|
|
1184
1344
|
}
|
|
1185
1345
|
if (ids.length === 0) throw memoryError('RECORD_NOT_FOUND', `id ${input.id} not found`)
|
|
1186
1346
|
} else if (input.query) {
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
const
|
|
1190
|
-
|
|
1191
|
-
|
|
1347
|
+
// 按 query 删除的默认范围由工具层显式给出(默认只有当前会话);这里不再回退到
|
|
1348
|
+
// provider 的默认 scope —— 那正是"以为删了会话记忆、其实删了 global"的来源。
|
|
1349
|
+
const targets = input.scope !== undefined
|
|
1350
|
+
? [input.scope]
|
|
1351
|
+
: (input.scopes ?? []).length > 0
|
|
1352
|
+
? input.scopes
|
|
1353
|
+
: [this.projectScope]
|
|
1354
|
+
const seen = new Set ()
|
|
1355
|
+
for (const scope of targets) {
|
|
1356
|
+
if (seen.has(scope)) continue
|
|
1357
|
+
seen.add(scope)
|
|
1358
|
+
const store = this.store.peek(scope) // 读/删路径不创建
|
|
1359
|
+
if (!store) continue
|
|
1360
|
+
const hit = this.store.findIdsByQuery(scope, input.query)
|
|
1361
|
+
for (const id of hit) if (store.remove(id)) ids.push(id)
|
|
1362
|
+
}
|
|
1363
|
+
if (ids.length === 0) throw memoryError('RECORD_NOT_FOUND', `no record matching query in ${targets.join(', ')}`)
|
|
1192
1364
|
} else {
|
|
1193
1365
|
throw memoryError('RECORD_NOT_FOUND', 'forget requires id or query')
|
|
1194
1366
|
}
|
|
@@ -1196,14 +1368,78 @@ export class LocalMemoryService {
|
|
|
1196
1368
|
return { removed: ids.length, ids }
|
|
1197
1369
|
}
|
|
1198
1370
|
|
|
1371
|
+
/**
|
|
1372
|
+
* 列记录。显式 scope 只列它;`scopes` 数组用于"全部作用域";两者都没有时退回项目 scope。
|
|
1373
|
+
* 一律走 `peek`,所以列一个不存在的 scope 只会得到空数组,不会造出文件。
|
|
1374
|
+
*/
|
|
1199
1375
|
async list(input = {}) {
|
|
1200
|
-
const
|
|
1201
|
-
|
|
1376
|
+
const wanted = input.scope !== undefined
|
|
1377
|
+
? [input.scope]
|
|
1378
|
+
: (input.scopes ?? []).length > 0
|
|
1379
|
+
? input.scopes
|
|
1380
|
+
: [this.projectScope]
|
|
1381
|
+
const seen = new Set ()
|
|
1382
|
+
let records = []
|
|
1383
|
+
for (const scope of wanted) {
|
|
1384
|
+
if (seen.has(scope)) continue
|
|
1385
|
+
seen.add(scope)
|
|
1386
|
+
const store = this.store.peek(scope)
|
|
1387
|
+
if (store) records.push(...store.records.values())
|
|
1388
|
+
}
|
|
1202
1389
|
if (input.kind) records = records.filter((r) => r.kind === input.kind)
|
|
1203
1390
|
records.sort((a, b) => b.updatedAt - a.updatedAt)
|
|
1204
1391
|
return records.slice(0, input.limit ?? 50)
|
|
1205
1392
|
}
|
|
1206
1393
|
|
|
1394
|
+
/**
|
|
1395
|
+
* 作用域清单(名字 + 条数 + 待嵌入 + 最近活跃时间),给面板 /status 与 memory_list 的尾部清单用。
|
|
1396
|
+
*
|
|
1397
|
+
* 先 `discoverScopes()`:进程内 store 是懒加载的,只列"已加载"的 scope 会让面板/清单
|
|
1398
|
+
* 漏掉别的进程(上一次会话、另一个 agent)写下的作用域。
|
|
1399
|
+
* 排序键是"最近活跃"(`updatedAt`):面板据此只给最近活跃的前 N 个会话 scope 查标题。
|
|
1400
|
+
*/
|
|
1401
|
+
scopeStats() {
|
|
1402
|
+
this.discoverScopes()
|
|
1403
|
+
return this.store.allScopes()
|
|
1404
|
+
.map((s) => ({
|
|
1405
|
+
scope: s.scope,
|
|
1406
|
+
records: s.records.size,
|
|
1407
|
+
pendingEmbeds: s.embedPending.size,
|
|
1408
|
+
updatedAt: [...s.records.values()].reduce((max, r) => Math.max(max, r.updatedAt), 0),
|
|
1409
|
+
}))
|
|
1410
|
+
.sort((a, b) => b.updatedAt - a.updatedAt || b.records - a.records || a.scope.localeCompare(b.scope))
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
/**
|
|
1414
|
+
* 发现磁盘上的 scope(带时间缓存)。面板每次 /status、每次 memory_list 都要一份完整
|
|
1415
|
+
* 清单,而 discovery 要读每个文件的第一条记录 —— 限流到最短 3 秒一次,既保证清单够新,
|
|
1416
|
+
* 也不至于把 projects/ 目录读成热路径(面板侧还有一层同样时长的缓存,旧 provider 也能受益)。
|
|
1417
|
+
*/
|
|
1418
|
+
/**
|
|
1419
|
+
* 当前进程已知的 scope 键:**只读**,不触发发现(要完整清单用 discoverScopes()/scopeStats())。
|
|
1420
|
+
*/
|
|
1421
|
+
knownScopes() {
|
|
1422
|
+
return this.store.allScopes().map((s) => s.scope)
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
/**
|
|
1426
|
+
* 发现磁盘上的 scope(带时间缓存)。面板每次 /status、每次 memory_list 都要一份完整
|
|
1427
|
+
* 清单,而 discovery 要读每个文件的第一条记录 —— 限流到最短 3 秒一次,既保证清单够新,
|
|
1428
|
+
* 也不至于把 projects/ 目录读成热路径(面板侧还有一层同样时长的缓存,旧 provider 也能受益)。
|
|
1429
|
+
*/
|
|
1430
|
+
discoverScopes() {
|
|
1431
|
+
const now = Date.now()
|
|
1432
|
+
if (this.discoveredAt === 0 || now - this.discoveredAt >= DISCOVER_TTL_MS) {
|
|
1433
|
+
this.discoveredAt = now
|
|
1434
|
+
try {
|
|
1435
|
+
this.store.loadDiscovered()
|
|
1436
|
+
} catch {
|
|
1437
|
+
// 发现失败不影响读写,清单里少几个而已
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
return this.knownScopes()
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1207
1443
|
/** Force a synchronous flush (dispose/HMR path). */
|
|
1208
1444
|
flushSync() {
|
|
1209
1445
|
if (this.maintenanceTimer) {
|
|
@@ -1265,6 +1501,9 @@ function memoryError(code , message ) {
|
|
|
1265
1501
|
return err
|
|
1266
1502
|
}
|
|
1267
1503
|
|
|
1504
|
+
/** discoverScopes() 的最短间隔:清单要够新,但 projects/ 目录不该被读成热路径。 */
|
|
1505
|
+
const DISCOVER_TTL_MS = 3000
|
|
1506
|
+
|
|
1268
1507
|
export function createMemoryService(config = {}) {
|
|
1269
1508
|
return new LocalMemoryService(config)
|
|
1270
1509
|
}
|