@restregular/dsh-memory 0.17.1 → 0.19.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/CHANGELOG.md +37 -0
- package/README.md +29 -2
- package/lib/client.js +663 -0
- package/lib/inject/apply.js +92 -9
- package/lib/inject/inject.js +25 -6
- package/lib/inject/rules.js +8 -3
- package/lib/panel.js +153 -2
- package/lib/provider/apply.js +64 -0
- package/lib/provider/index.js +374 -4
- package/lib/provider/session-visibility.js +206 -0
- package/lib/tools/index.js +271 -20
- package/package.json +1 -1
package/lib/provider/index.js
CHANGED
|
@@ -27,6 +27,13 @@ import {
|
|
|
27
27
|
|
|
28
28
|
} from './scope.js'
|
|
29
29
|
import { ScopeNames } from './scope-names.js'
|
|
30
|
+
import {
|
|
31
|
+
SessionVisibilityStore,
|
|
32
|
+
cleanVisibilityRules,
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
} from './session-visibility.js'
|
|
30
37
|
import { mergeInjectSettings, normalizeInjectSettings, } from './inject-settings.js'
|
|
31
38
|
import { buildStats, lexicalScore, recordScore, tokenize, cosine, normalizeInPlace, hasStrongHit } from './scoring.js'
|
|
32
39
|
import { EmbedClient } from './embed.js'
|
|
@@ -495,6 +502,46 @@ export function resolveConfig(config = {}) {
|
|
|
495
502
|
|
|
496
503
|
|
|
497
504
|
|
|
505
|
+
/** 生效中的可见域(已剔除被 scope 开关禁用的项)。 */
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
/** 一条"范围外"命中:给审批浮层展示的完整快照(不落盘,进程内存)。 */
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
/** 普通限制下一次审批最多等多久;超时按拒绝处理(工具不会无限挂住)。 */
|
|
541
|
+
const VISIBILITY_APPROVAL_TTL_MS = 5 * 60_000
|
|
542
|
+
/** 审批浮层一次最多展示多少条范围外命中(工具侧仍会返回全部允许的命中)。 */
|
|
543
|
+
const VISIBILITY_APPROVAL_MAX_ITEMS = 20
|
|
544
|
+
|
|
498
545
|
export class LocalMemoryService {
|
|
499
546
|
cfg
|
|
500
547
|
store
|
|
@@ -515,6 +562,18 @@ export class LocalMemoryService {
|
|
|
515
562
|
names
|
|
516
563
|
/** 注入设置(面板「设置」标签页可改;落盘在 <storeDir>/settings.json)。 */
|
|
517
564
|
inject
|
|
565
|
+
/** 会话级「记忆可见范围」覆盖;没有覆盖时沿用默认三路集合。 */
|
|
566
|
+
visibility
|
|
567
|
+
/** 可见域变更通知:下一次 pre-step 注入给 Agent 后删除(按会话)。 */
|
|
568
|
+
visibilityNotices = new Map ()
|
|
569
|
+
/** 普通限制下等待用户审批的范围外访问(按审批 id)。 */
|
|
570
|
+
pendingApprovals = new Map ()
|
|
571
|
+
approvalSeq = 0
|
|
572
|
+
/**
|
|
573
|
+
* 会话 id → live session 的只读查询(用于沿 parentSession 链继承可见范围)。
|
|
574
|
+
* 宿主侧 apply.ts 注入;旧宿主 / 测试里没有时退化为只看当前会话。
|
|
575
|
+
*/
|
|
576
|
+
sessionLookup
|
|
518
577
|
/** 注入半边的审计收集器(可选;没有注入半边时保持 null)。 */
|
|
519
578
|
injectAuditSink
|
|
520
579
|
|
|
@@ -528,6 +587,7 @@ export class LocalMemoryService {
|
|
|
528
587
|
this.cfg = resolveConfig(config)
|
|
529
588
|
this.inject = this.loadInjectSettings((config ).inject)
|
|
530
589
|
this.names = new ScopeNames(join(this.cfg.storeDir, 'scopes.json'))
|
|
590
|
+
this.visibility = new SessionVisibilityStore({ root: this.cfg.storeDir })
|
|
531
591
|
this.rerankBudget = new DailyBudget(this.cfg.llm.dailyBudgetTokens)
|
|
532
592
|
this.store = new MemoryStore(this.cfg.storeDir, { onWarn: (m) => console.warn(`[dsh-memory-local] ${m}`) })
|
|
533
593
|
// Identity BEFORE the eager load (issue #1): ScopeStore skips its sidecar
|
|
@@ -686,8 +746,10 @@ export class LocalMemoryService {
|
|
|
686
746
|
*
|
|
687
747
|
* 默认排除已被 supersede 的旧记录;需要历史/审计时显式 includeSuperseded。
|
|
688
748
|
*/
|
|
689
|
-
listSync(input
|
|
749
|
+
listSync(input = {}) {
|
|
690
750
|
const wanted = (input.scopes ?? []).map((scope) => this.canonicalScope(scope))
|
|
751
|
+
// 注入侧必须按可见域的类型白名单过滤;工具侧不传 agent(它需要范围外命中来发起审批)。
|
|
752
|
+
const visibleFilter = input.agent !== undefined ? this.visibilityFilterFor(input.agent) : undefined
|
|
691
753
|
const seen = new Set ()
|
|
692
754
|
const stores = []
|
|
693
755
|
for (const scope of wanted) {
|
|
@@ -705,6 +767,7 @@ export class LocalMemoryService {
|
|
|
705
767
|
// 过期 state 默认不参与任何自动注入/规则渲染;memory_get(id) 与面板仍可取。
|
|
706
768
|
if (!input.includeExpired && rec.expiredAt !== undefined && rec.expiredAt < now) continue
|
|
707
769
|
if (input.kind !== undefined && rec.kind !== input.kind) continue
|
|
770
|
+
if (visibleFilter !== undefined && !visibleFilter(rec.scope, rec.kind)) continue
|
|
708
771
|
out.push(rec)
|
|
709
772
|
}
|
|
710
773
|
}
|
|
@@ -1298,11 +1361,300 @@ export class LocalMemoryService {
|
|
|
1298
1361
|
return this.scopeRefs(agent).project
|
|
1299
1362
|
}
|
|
1300
1363
|
|
|
1364
|
+
/** 宿主注入 live session 查询(用于沿 parentSession 链继承可见范围)。 */
|
|
1365
|
+
setSessionLookup(lookup ) {
|
|
1366
|
+
this.sessionLookup = lookup
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
/** 只保留当前配置允许的 scope(覆盖集合里若含被禁用的 scope,静默剔除)。 */
|
|
1370
|
+
filterEnabledRules(rules ) {
|
|
1371
|
+
const out = []
|
|
1372
|
+
for (const raw of rules) {
|
|
1373
|
+
const scope = this.canonicalScope(raw.scope)
|
|
1374
|
+
if (out.some((rule) => rule.scope === scope)) continue
|
|
1375
|
+
switch (scopeKind(scope)) {
|
|
1376
|
+
case 'global': if (!this.cfg.globalEnabled) continue; break
|
|
1377
|
+
case 'project': if (!this.cfg.projectEnabled) continue; break
|
|
1378
|
+
case 'session': if (!this.cfg.sessionEnabled) continue; break
|
|
1379
|
+
default: if (!this.cfg.customEnabled) continue; break
|
|
1380
|
+
}
|
|
1381
|
+
const kinds = Array.isArray(raw.kinds) ? raw.kinds.filter((kind) => typeof kind === 'string' && kind !== '') : []
|
|
1382
|
+
out.push(kinds.length > 0 ? { scope, kinds } : { scope })
|
|
1383
|
+
}
|
|
1384
|
+
return out
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
/**
|
|
1388
|
+
* 从会话 id 沿 parentSession 链找最近的显式可见域。
|
|
1389
|
+
*
|
|
1390
|
+
* 起点用**当前会话自己的 id**(不是 resolveCurrentScopes 映射后的父会话):
|
|
1391
|
+
* 子 Agent 自己也可以有一份覆盖;没有时才沿链继承父会话 / 根会话。
|
|
1392
|
+
*/
|
|
1393
|
+
visibilityForSession(sessionId ) {
|
|
1394
|
+
let id = sessionId ?? ''
|
|
1395
|
+
const seen = new Set ()
|
|
1396
|
+
while (id !== '' && !seen.has(id)) {
|
|
1397
|
+
seen.add(id)
|
|
1398
|
+
const found = this.visibility.get(id)
|
|
1399
|
+
if (found !== undefined) return found
|
|
1400
|
+
let parent
|
|
1401
|
+
try {
|
|
1402
|
+
const session = this.sessionLookup?.(id)
|
|
1403
|
+
parent = typeof session?.header?.parentSession === 'string' && session.header.parentSession !== ''
|
|
1404
|
+
? session.header.parentSession
|
|
1405
|
+
: undefined
|
|
1406
|
+
} catch { parent = undefined }
|
|
1407
|
+
id = parent ?? ''
|
|
1408
|
+
}
|
|
1409
|
+
return undefined
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
/** agent 上真实的会话 id(不映射到父会话);用于可见范围自己的文件。 */
|
|
1413
|
+
rawSessionIdOf(agent ) {
|
|
1414
|
+
try {
|
|
1415
|
+
const session = (agent )?.session
|
|
1416
|
+
const id = session?.header?.id ?? session?.id
|
|
1417
|
+
return typeof id === 'string' && id !== '' ? id : undefined
|
|
1418
|
+
} catch {
|
|
1419
|
+
return undefined
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
/**
|
|
1424
|
+
* 显式「记忆可见域」的完整策略(含等级与每个 scope 的类型白名单)。
|
|
1425
|
+
* undefined = 没有覆盖;level='none' 时 scopes 仍保留(供面板/下次切换),
|
|
1426
|
+
* 但**不参与**读取限制。
|
|
1427
|
+
*/
|
|
1428
|
+
visibilityPolicy(agent ) {
|
|
1429
|
+
const explicit = this.visibilityForSession(this.rawSessionIdOf(agent))
|
|
1430
|
+
if (explicit === undefined) return undefined
|
|
1431
|
+
return { level: explicit.level, scopes: this.filterEnabledRules(explicit.scopes) }
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
/**
|
|
1435
|
+
* 显式「记忆可见范围」的 scope 键列表。
|
|
1436
|
+
* undefined = 不限制(没有覆盖,或等级为 none);
|
|
1437
|
+
* [] = 显式设置了范围但全部被 scope 开关禁用,调用方必须按空处理。
|
|
1438
|
+
*/
|
|
1439
|
+
visibleScopes(agent ) {
|
|
1440
|
+
const policy = this.visibilityPolicy(agent)
|
|
1441
|
+
if (policy === undefined || policy.level === 'none') return undefined
|
|
1442
|
+
return policy.scopes.map((rule) => this.canonicalScope(rule.scope))
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
/** 某个 scope 下允许的记忆类型;undefined = 不限制(可见且没有类型白名单)。 */
|
|
1446
|
+
visibleKinds(agent , scope ) {
|
|
1447
|
+
const policy = this.visibilityPolicy(agent)
|
|
1448
|
+
if (policy === undefined || policy.level === 'none') return undefined
|
|
1449
|
+
const key = this.canonicalScope(scope)
|
|
1450
|
+
const rule = policy.scopes.find((item) => this.canonicalScope(item.scope) === key)
|
|
1451
|
+
if (rule === undefined) return []
|
|
1452
|
+
return rule.kinds !== undefined && rule.kinds.length > 0 ? [...rule.kinds] : undefined
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
/** 一条记录(scope + kind)是否落在当前可见域内;等级 none / 无覆盖时恒为 true。 */
|
|
1456
|
+
isRecordVisible(agent , scope , kind ) {
|
|
1457
|
+
const policy = this.visibilityPolicy(agent)
|
|
1458
|
+
if (policy === undefined || policy.level === 'none') return true
|
|
1459
|
+
const key = this.canonicalScope(scope)
|
|
1460
|
+
const rule = policy.scopes.find((item) => this.canonicalScope(item.scope) === key)
|
|
1461
|
+
if (rule === undefined) return false
|
|
1462
|
+
if (rule.kinds === undefined || rule.kinds.length === 0) return true
|
|
1463
|
+
return rule.kinds.includes(kind)
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* 批量过滤用的可见域判定(策略只解析一次,避免每条记录都走一遍 parentSession 链)。
|
|
1468
|
+
* undefined = 不限制。
|
|
1469
|
+
*/
|
|
1470
|
+
visibilityFilterFor(agent ) {
|
|
1471
|
+
const policy = this.visibilityPolicy(agent)
|
|
1472
|
+
if (policy === undefined || policy.level === 'none') return undefined
|
|
1473
|
+
const byScope = new Map ()
|
|
1474
|
+
for (const rule of policy.scopes) {
|
|
1475
|
+
byScope.set(this.canonicalScope(rule.scope), rule.kinds !== undefined && rule.kinds.length > 0 ? rule.kinds : undefined)
|
|
1476
|
+
}
|
|
1477
|
+
return (scope, kind) => {
|
|
1478
|
+
const key = this.canonicalScope(scope)
|
|
1479
|
+
if (!byScope.has(key)) return false
|
|
1480
|
+
const kinds = byScope.get(key)
|
|
1481
|
+
return kinds === undefined || kinds.includes(kind)
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
/** 面板:读取某会话自己的覆盖(null = 没有覆盖,继续用默认集合)。 */
|
|
1486
|
+
sessionVisibility(sessionId ) {
|
|
1487
|
+
if (typeof sessionId !== 'string' || sessionId.trim() === '') return null
|
|
1488
|
+
const config = this.visibility.get(sessionId.trim())
|
|
1489
|
+
return config === undefined ? null : config
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/**
|
|
1493
|
+
* 面板:写入/清除某会话的覆盖。null = 恢复默认。
|
|
1494
|
+
* 也接受 0.18 的 `string[]`(按绝对限制 + 全部类型),避免外部调用方升级即断。
|
|
1495
|
+
*/
|
|
1496
|
+
setSessionVisibility(sessionId , config ) {
|
|
1497
|
+
if (typeof sessionId !== 'string' || sessionId.trim() === '') return null
|
|
1498
|
+
const normalized = config === null
|
|
1499
|
+
? null
|
|
1500
|
+
: Array.isArray(config)
|
|
1501
|
+
? { scopes: (config ).map((scope) => ({ scope })), level: 'absolute' }
|
|
1502
|
+
: config
|
|
1503
|
+
const cleaned = normalized === null ? null : this.normalizeVisibilityConfig(normalized)
|
|
1504
|
+
const written = this.visibility.set(sessionId.trim(), cleaned)
|
|
1505
|
+
// 变更通知:下一次 pre-step 以 user 消息注入,让 Agent 知道新范围与新规则。
|
|
1506
|
+
this.visibilityNotices.set(sessionId.trim(), { config: written, at: Date.now() })
|
|
1507
|
+
return written
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
/** 校验并规范化面板提交的可见域(必须是具体作用域键;不接受 all/current-*)。 */
|
|
1511
|
+
normalizeVisibilityConfig(config ) {
|
|
1512
|
+
const rules = cleanVisibilityRules(config.scopes)
|
|
1513
|
+
if (rules === undefined || rules.length === 0) {
|
|
1514
|
+
throw memoryError('SCOPE_EMPTY', '至少选择一个可见的记忆范围')
|
|
1515
|
+
}
|
|
1516
|
+
const out = []
|
|
1517
|
+
for (const rule of rules) {
|
|
1518
|
+
const normalized = normalizeScopeInput(rule.scope)
|
|
1519
|
+
if (!normalized.ok) throw memoryError(normalized.code, normalized.message)
|
|
1520
|
+
if (normalized.kind === 'all' || normalized.kind === 'current-session' || normalized.kind === 'current-project') {
|
|
1521
|
+
throw memoryError('SCOPE_INVALID', `可见范围必须是具体作用域键: ${JSON.stringify(rule.scope)}`)
|
|
1522
|
+
}
|
|
1523
|
+
if (normalized.scope === undefined) continue
|
|
1524
|
+
const scope = this.canonicalScope(normalized.scope)
|
|
1525
|
+
if (out.some((item) => item.scope === scope)) continue
|
|
1526
|
+
const kinds = Array.isArray(rule.kinds)
|
|
1527
|
+
? [...new Set(rule.kinds.filter((kind) => typeof kind === 'string' && kind.trim() !== '').map((kind) => kind.trim()))]
|
|
1528
|
+
: []
|
|
1529
|
+
out.push(kinds.length > 0 ? { scope, kinds } : { scope })
|
|
1530
|
+
}
|
|
1531
|
+
if (out.length === 0) throw memoryError('SCOPE_EMPTY', '至少选择一个可见的记忆范围')
|
|
1532
|
+
const level = config.level === 'normal' || config.level === 'none' ? config.level : 'absolute'
|
|
1533
|
+
return { scopes: out, level }
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* 取出并删除某会话待注入的「可见域变更」通知。
|
|
1538
|
+
* 沿 parentSession 链找:子 Agent 继承父会话的范围,也应该收到父会话的变更。
|
|
1539
|
+
*/
|
|
1540
|
+
takeVisibilityNotice(agent ) {
|
|
1541
|
+
let id = this.rawSessionIdOf(agent) ?? ''
|
|
1542
|
+
const seen = new Set ()
|
|
1543
|
+
while (id !== '' && !seen.has(id)) {
|
|
1544
|
+
seen.add(id)
|
|
1545
|
+
const found = this.visibilityNotices.get(id)
|
|
1546
|
+
if (found !== undefined) {
|
|
1547
|
+
this.visibilityNotices.delete(id)
|
|
1548
|
+
return { config: found.config }
|
|
1549
|
+
}
|
|
1550
|
+
let parent
|
|
1551
|
+
try {
|
|
1552
|
+
const session = this.sessionLookup?.(id)
|
|
1553
|
+
parent = typeof session?.header?.parentSession === 'string' && session.header.parentSession !== ''
|
|
1554
|
+
? session.header.parentSession
|
|
1555
|
+
: undefined
|
|
1556
|
+
} catch { parent = undefined }
|
|
1557
|
+
id = parent ?? ''
|
|
1558
|
+
}
|
|
1559
|
+
return undefined
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
/** 每个 scope 下按类型统计条数(面板「类型」选择器用)。 */
|
|
1563
|
+
scopeKindStats() {
|
|
1564
|
+
const out = []
|
|
1565
|
+
for (const scope of this.store.allScopes()) {
|
|
1566
|
+
const counts = new Map ()
|
|
1567
|
+
for (const record of scope.records.values()) counts.set(record.kind, (counts.get(record.kind) ?? 0) + 1)
|
|
1568
|
+
for (const [kind, records] of [...counts].sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
1569
|
+
out.push({ scope: scope.scope, kind, records })
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
return out
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
/** 普通限制:范围外命中时创建一条待审批,等待面板回答(超时按拒绝)。 */
|
|
1576
|
+
requestOutOfScope(
|
|
1577
|
+
agent ,
|
|
1578
|
+
input ,
|
|
1579
|
+
) {
|
|
1580
|
+
if (input.items.length === 0) return Promise.resolve('allowed-once')
|
|
1581
|
+
if (input.signal?.aborted) return Promise.resolve('rejected')
|
|
1582
|
+
const sessionId = this.rawSessionIdOf(agent) ?? ''
|
|
1583
|
+
const id = `vis-${Date.now().toString(36)}-${++this.approvalSeq}`
|
|
1584
|
+
const createdAt = Date.now()
|
|
1585
|
+
const expiresAt = createdAt + VISIBILITY_APPROVAL_TTL_MS
|
|
1586
|
+
return new Promise((resolve) => {
|
|
1587
|
+
let timer
|
|
1588
|
+
const onAbort = () => finish('rejected')
|
|
1589
|
+
const finish = (outcome ) => {
|
|
1590
|
+
const pending = this.pendingApprovals.get(id)
|
|
1591
|
+
if (pending === undefined) return
|
|
1592
|
+
this.pendingApprovals.delete(id)
|
|
1593
|
+
if (timer !== undefined) clearTimeout(timer)
|
|
1594
|
+
try { input.signal?.removeEventListener('abort', onAbort) } catch { /* 已卸载 */ }
|
|
1595
|
+
pending.resolve(outcome)
|
|
1596
|
+
}
|
|
1597
|
+
timer = setTimeout(() => finish('rejected'), VISIBILITY_APPROVAL_TTL_MS)
|
|
1598
|
+
timer.unref?.()
|
|
1599
|
+
this.pendingApprovals.set(id, {
|
|
1600
|
+
id,
|
|
1601
|
+
sessionId,
|
|
1602
|
+
tool: input.tool,
|
|
1603
|
+
query: input.query,
|
|
1604
|
+
items: input.items.slice(0, VISIBILITY_APPROVAL_MAX_ITEMS).map((item) => ({
|
|
1605
|
+
...item,
|
|
1606
|
+
tags: [...item.tags],
|
|
1607
|
+
})),
|
|
1608
|
+
createdAt,
|
|
1609
|
+
expiresAt,
|
|
1610
|
+
resolve: resolve,
|
|
1611
|
+
timer,
|
|
1612
|
+
})
|
|
1613
|
+
try { input.signal?.addEventListener('abort', onAbort, { once: true }) } catch { /* 已中止 */ }
|
|
1614
|
+
})
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
/** 面板:当前所有待审批(可按会话过滤)。 */
|
|
1618
|
+
pendingVisibilityApprovals(sessionId ) {
|
|
1619
|
+
const out = []
|
|
1620
|
+
for (const pending of this.pendingApprovals.values()) {
|
|
1621
|
+
if (sessionId !== undefined && sessionId !== '' && pending.sessionId !== sessionId) continue
|
|
1622
|
+
out.push({
|
|
1623
|
+
id: pending.id,
|
|
1624
|
+
sessionId: pending.sessionId,
|
|
1625
|
+
tool: pending.tool,
|
|
1626
|
+
query: pending.query,
|
|
1627
|
+
// 轮询载荷保持轻量:正文截到 1200 字,悬浮需要全文时面板再按 id 取一次。
|
|
1628
|
+
items: pending.items.map((item) => ({
|
|
1629
|
+
...item,
|
|
1630
|
+
content: item.content.length > 1200 ? `${item.content.slice(0, 1200)}…` : item.content,
|
|
1631
|
+
tags: [...item.tags],
|
|
1632
|
+
})),
|
|
1633
|
+
createdAt: pending.createdAt,
|
|
1634
|
+
expiresAt: pending.expiresAt,
|
|
1635
|
+
})
|
|
1636
|
+
}
|
|
1637
|
+
return out.sort((a, b) => a.createdAt - b.createdAt)
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
/** 面板:回答一条待审批。返回 false = 已经不存在/已超时。 */
|
|
1641
|
+
answerVisibilityApproval(id , outcome ) {
|
|
1642
|
+
const pending = this.pendingApprovals.get(id)
|
|
1643
|
+
if (pending === undefined) return false
|
|
1644
|
+
this.pendingApprovals.delete(id)
|
|
1645
|
+
if (pending.timer !== undefined) clearTimeout(pending.timer)
|
|
1646
|
+
pending.resolve(outcome)
|
|
1647
|
+
return true
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1301
1650
|
/**
|
|
1302
1651
|
* 无显式 scope 时的**读取**合并集合:当前会话 + 当前项目 + global(各自开关生效、按 key 去重)。
|
|
1303
|
-
* 工具 /
|
|
1652
|
+
* 工具 / 注入都用它。会话设置了「可见范围」时以覆盖为准(不再并入默认集合)。
|
|
1304
1653
|
*/
|
|
1305
1654
|
resolveReadScopes(agent ) {
|
|
1655
|
+
const explicit = this.visibleScopes(agent)
|
|
1656
|
+
// level='none' 时 visibleScopes 返回 undefined:不限制读取,继续用默认三路集合。
|
|
1657
|
+
if (explicit !== undefined) return explicit
|
|
1306
1658
|
const refs = this.scopeRefs(agent)
|
|
1307
1659
|
const out = []
|
|
1308
1660
|
const push = (scope , enabled ) => {
|
|
@@ -1485,6 +1837,9 @@ export class LocalMemoryService {
|
|
|
1485
1837
|
// 多 scope 合并由调用方显式给出(工具层传"会话 + 项目 + global");宿主类型里的
|
|
1486
1838
|
// MemoryQuery 只有单值 scope,所以这里按结构化读一次可选数组,保持原语义不变。
|
|
1487
1839
|
const queryScopes = (query ).scopes
|
|
1840
|
+
// 显式空集合 = 什么都不允许读(会话可见范围把所有勾选项都禁用了)。不能退回
|
|
1841
|
+
// 默认项目 + global,否则"看不见任何范围"反而会搜出默认范围的记录。
|
|
1842
|
+
if (query.scope === undefined && queryScopes !== undefined && queryScopes.length === 0) return []
|
|
1488
1843
|
const topK = query.topK ?? this.cfg.topK
|
|
1489
1844
|
const recallN = query.recallN ?? this.cfg.recallN
|
|
1490
1845
|
const now = Date.now()
|
|
@@ -1492,10 +1847,18 @@ export class LocalMemoryService {
|
|
|
1492
1847
|
// 面板的语义检索复用这条流水线,但它是"看一眼"的浏览:不写访问计数,也不在
|
|
1493
1848
|
// 没有任何向量时白跑一次嵌入请求。这两个开关只给面板的 searchForPanel 用,
|
|
1494
1849
|
// 工具 / 注入调用不会传。
|
|
1495
|
-
const internal = query
|
|
1850
|
+
const internal = query
|
|
1496
1851
|
const allowVector = internal.vector !== false && this.cfg.vector.enabled && this.cfg.vector.configured
|
|
1497
1852
|
const includeSuperseded = internal.includeSuperseded === true
|
|
1498
1853
|
const includeExpired = internal.includeExpired === true
|
|
1854
|
+
// 注入侧按可见域的类型白名单检索(scope → 允许的类型;未列出的 scope 不限制)。
|
|
1855
|
+
// 工具侧不传 kinds:它需要看到范围外命中,才能发起审批。
|
|
1856
|
+
const kindFilter = internal.kinds === undefined
|
|
1857
|
+
? undefined
|
|
1858
|
+
: (scope , kind ) => {
|
|
1859
|
+
const allowed = internal.kinds [this.canonicalScope(scope)]
|
|
1860
|
+
return allowed === undefined || allowed.length === 0 || allowed.includes(kind)
|
|
1861
|
+
}
|
|
1499
1862
|
|
|
1500
1863
|
// 候选 scope:显式 pin(单值)优先;否则用调用方给的合并集合(query.scopes,
|
|
1501
1864
|
// 工具 / 注入传"会话 + 项目 + global");两者都没有才退回旧的 project + global。
|
|
@@ -1539,6 +1902,7 @@ export class LocalMemoryService {
|
|
|
1539
1902
|
const rec = all[i]
|
|
1540
1903
|
if (!includeExpired && rec.expiredAt && rec.expiredAt < now) continue
|
|
1541
1904
|
if (!includeSuperseded && retired.has(rec.id)) continue
|
|
1905
|
+
if (kindFilter !== undefined && !kindFilter(rec.scope, rec.kind)) continue
|
|
1542
1906
|
const lex = lexicalScore(queryTokens, tokenSets[i] , stats)
|
|
1543
1907
|
if (lex <= 0) continue
|
|
1544
1908
|
// Weak-only overlap (no shared word/bigram) must clear the "real hit"
|
|
@@ -1577,6 +1941,7 @@ export class LocalMemoryService {
|
|
|
1577
1941
|
const rec = this.findById(id)
|
|
1578
1942
|
if (!rec) continue
|
|
1579
1943
|
if (!includeExpired && rec.expiredAt !== undefined && rec.expiredAt < now) continue
|
|
1944
|
+
if (kindFilter !== undefined && !kindFilter(rec.scope, rec.kind)) continue
|
|
1580
1945
|
if (!(rec.scope === 'global' && sim < this.cfg.globalThreshold)) {
|
|
1581
1946
|
candidates.set(id, { record: rec, recallScore: sim, isGlobal: rec.scope === 'global' })
|
|
1582
1947
|
}
|
|
@@ -1747,7 +2112,11 @@ export class LocalMemoryService {
|
|
|
1747
2112
|
* 一律走 `peek`,所以列一个不存在的 scope 只会得到空数组,不会造出文件。
|
|
1748
2113
|
*/
|
|
1749
2114
|
async list(input = {}) {
|
|
1750
|
-
const internal = input
|
|
2115
|
+
const internal = input
|
|
2116
|
+
// 注入侧(摘要)传入 agent 时按可见域的类型白名单过滤;工具侧不传,以便处理范围外审批。
|
|
2117
|
+
const visibleFilter = internal.agent !== undefined ? this.visibilityFilterFor(internal.agent) : undefined
|
|
2118
|
+
// 显式空集合 = 无可见范围(见 search 的同口径说明)。
|
|
2119
|
+
if (input.scope === undefined && input.scopes !== undefined && input.scopes.length === 0) return []
|
|
1751
2120
|
const wanted = (input.scope !== undefined
|
|
1752
2121
|
? [input.scope]
|
|
1753
2122
|
: (input.scopes ?? []).length > 0
|
|
@@ -1772,6 +2141,7 @@ export class LocalMemoryService {
|
|
|
1772
2141
|
}
|
|
1773
2142
|
}
|
|
1774
2143
|
if (input.kind) records = records.filter((r) => r.kind === input.kind)
|
|
2144
|
+
if (visibleFilter !== undefined) records = records.filter((r) => visibleFilter(r.scope, r.kind))
|
|
1775
2145
|
records.sort((a, b) => b.updatedAt - a.updatedAt)
|
|
1776
2146
|
return records.slice(0, input.limit ?? 50)
|
|
1777
2147
|
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话级「记忆可见范围」的持久化。
|
|
3
|
+
*
|
|
4
|
+
* 位置:`<storeDir>/visibility/<session>.json`(与记忆数据同目录,便于一起备份/清理)。
|
|
5
|
+
*
|
|
6
|
+
* 为什么放在 provider 而不是注入半边:
|
|
7
|
+
* resolveReadScopes / memory_search / memory_get 都以 provider 为**唯一口径**;
|
|
8
|
+
* 如果把覆盖范围放在注入半边,工具与注入就会各读一份,模型能看到的范围页面
|
|
9
|
+
* 和工具实际能读到的范围可能不一致。provider 落盘、注入与工具都通过它解析。
|
|
10
|
+
*
|
|
11
|
+
* 为什么一个会话一个文件,而不是一个 JSON map:
|
|
12
|
+
* 同一个 storeDir 可能被多个进程共用(见 store.ts 的 issue #3)。单文件 map 在并发
|
|
13
|
+
* 写入时会互相覆盖;按会话分文件后,一个会话的写入不影响另一个会话。
|
|
14
|
+
*
|
|
15
|
+
* 语义(0.19.0 起):
|
|
16
|
+
* - 文件存在 = 该会话显式指定了可见域;`level` 决定范围外访问怎么处理:
|
|
17
|
+
* absolute = 一律查不到;normal = 范围外命中时向用户发起一次审批;
|
|
18
|
+
* none = 不限制(选择仅作为下次切换的偏好保留)。
|
|
19
|
+
* - 文件不存在 = 没有覆盖,继续用默认的三路集合(会话 + 项目 + global);
|
|
20
|
+
* - scopes 为空数组视为"无覆盖"(面板在保存时也会拒绝空选择),不会把会话锁死。
|
|
21
|
+
* - 每个 scope 可以带 `kinds`:只允许这些记忆类型;省略/空数组 = 该 scope 全部类型。
|
|
22
|
+
*
|
|
23
|
+
* 向后兼容:0.18.x 的 v1 文件只有 `scopes: string[]`,按"绝对限制 + 全部类型"读取;
|
|
24
|
+
* 这样升级当天不会把原来被锁住的范围突然放开。
|
|
25
|
+
*
|
|
26
|
+
* 容错:读写失败都只退化为"没有覆盖",绝不把异常抛回检索/注入链路。
|
|
27
|
+
*/
|
|
28
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
29
|
+
import { join } from 'node:path'
|
|
30
|
+
import { createHash } from 'node:crypto'
|
|
31
|
+
|
|
32
|
+
export const VISIBILITY_VERSION = 2
|
|
33
|
+
|
|
34
|
+
/** 范围外访问的处理方式。 */
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
export const VISIBILITY_LEVELS = ['absolute', 'normal', 'none']
|
|
38
|
+
|
|
39
|
+
/** 一个可见作用域;`kinds` 省略/为空 = 该作用域下所有类型都可见。 */
|
|
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
|
+
/** 会话 id 通常已经是 UUID;异常字符换成 `_`,改写过的再加 8 位 hash 防碰撞。 */
|
|
65
|
+
function safeName(sessionId ) {
|
|
66
|
+
const cleaned = sessionId.replace(/[^A-Za-z0-9._-]+/g, '_').slice(0, 80)
|
|
67
|
+
if (cleaned === sessionId && cleaned !== '') return cleaned
|
|
68
|
+
const base = cleaned === '' ? 'session' : cleaned.slice(0, 64)
|
|
69
|
+
return `${base}-${createHash('sha1').update(sessionId).digest('hex').slice(0, 8)}`
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 只保留非空字符串并去重(顺序即用户勾选顺序);空数组返回 undefined。 */
|
|
73
|
+
function cleanStrings(raw ) {
|
|
74
|
+
if (!Array.isArray(raw)) return undefined
|
|
75
|
+
const out = []
|
|
76
|
+
for (const item of raw) {
|
|
77
|
+
if (typeof item !== 'string') continue
|
|
78
|
+
const value = item.trim()
|
|
79
|
+
if (value === '' || out.includes(value)) continue
|
|
80
|
+
out.push(value)
|
|
81
|
+
}
|
|
82
|
+
return out.length > 0 ? out : undefined
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function cleanLevel(raw , fallback = 'absolute') {
|
|
86
|
+
return raw === 'absolute' || raw === 'normal' || raw === 'none' ? raw : fallback
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 规范化 scopes 字段。接受两种形状:
|
|
91
|
+
* - v2: `[{ scope, kinds? }]`
|
|
92
|
+
* - v1: `['project:<path>', 'global']`
|
|
93
|
+
* 非法项跳过;返回 undefined 表示没有任何有效范围。
|
|
94
|
+
*/
|
|
95
|
+
export function cleanVisibilityRules(raw ) {
|
|
96
|
+
if (!Array.isArray(raw)) return undefined
|
|
97
|
+
const out = []
|
|
98
|
+
for (const item of raw) {
|
|
99
|
+
if (typeof item === 'string') {
|
|
100
|
+
const scope = item.trim()
|
|
101
|
+
if (scope !== '' && !out.some((rule) => rule.scope === scope)) out.push({ scope })
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
if (item === null || typeof item !== 'object') continue
|
|
105
|
+
const scopeRaw = (item ).scope
|
|
106
|
+
if (typeof scopeRaw !== 'string') continue
|
|
107
|
+
const scope = scopeRaw.trim()
|
|
108
|
+
if (scope === '' || out.some((rule) => rule.scope === scope)) continue
|
|
109
|
+
const kinds = cleanStrings((item ).kinds)
|
|
110
|
+
out.push(kinds === undefined ? { scope } : { scope, kinds })
|
|
111
|
+
}
|
|
112
|
+
return out.length > 0 ? out : undefined
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** 读盘结果统一成不可变配置(拷贝一层数组,避免调用方改到缓存)。 */
|
|
116
|
+
function cloneConfig(config ) {
|
|
117
|
+
return {
|
|
118
|
+
level: config.level,
|
|
119
|
+
scopes: config.scopes.map((rule) => (rule.kinds === undefined ? { scope: rule.scope } : { scope: rule.scope, kinds: [...rule.kinds] })),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export class SessionVisibilityStore {
|
|
124
|
+
dir
|
|
125
|
+
warn
|
|
126
|
+
/** sessionId -> 显式配置;null = 已确认没有文件(避免反复 stat)。 */
|
|
127
|
+
cache = new Map ()
|
|
128
|
+
|
|
129
|
+
constructor(options ) {
|
|
130
|
+
this.dir = join(options.root, 'visibility')
|
|
131
|
+
this.warn = options.warn ?? ((message) => console.warn(message))
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fileOf(sessionId ) {
|
|
135
|
+
return join(this.dir, `${safeName(sessionId)}.json`)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** 显式可见域;undefined = 没有覆盖(用默认集合)。 */
|
|
139
|
+
get(sessionId ) {
|
|
140
|
+
if (sessionId === '') return undefined
|
|
141
|
+
const cached = this.cache.get(sessionId)
|
|
142
|
+
if (cached !== undefined) return cached === null ? undefined : cloneConfig(cached)
|
|
143
|
+
let config
|
|
144
|
+
try {
|
|
145
|
+
const file = this.fileOf(sessionId)
|
|
146
|
+
if (existsSync(file)) {
|
|
147
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'))
|
|
148
|
+
const rules = cleanVisibilityRules(parsed?.scopes)
|
|
149
|
+
if (rules !== undefined) {
|
|
150
|
+
// v1 没有 level:沿用旧的"白名单"语义(绝对限制),避免升级把范围放开。
|
|
151
|
+
config = { scopes: rules, level: cleanLevel(parsed?.level, 'absolute') }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
} catch (error) {
|
|
155
|
+
// 损坏文件不应该让会话失去默认可见范围;只告警一次(缓存 null)。
|
|
156
|
+
this.warn(`[dsh-memory-local] 忽略损坏的会话可见范围 ${this.fileOf(sessionId)}: ${error instanceof Error ? error.message : String(error)}`)
|
|
157
|
+
config = undefined
|
|
158
|
+
}
|
|
159
|
+
this.cache.set(sessionId, config ?? null)
|
|
160
|
+
return config === undefined ? undefined : cloneConfig(config)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 写入/清除显式配置。null 或空 scopes = 清除(恢复默认)。返回写入后的值。 */
|
|
164
|
+
set(sessionId , config ) {
|
|
165
|
+
if (sessionId === '') return null
|
|
166
|
+
const scopes = config === null ? undefined : cleanVisibilityRules(config.scopes)
|
|
167
|
+
if (config === null || scopes === undefined) {
|
|
168
|
+
try {
|
|
169
|
+
rmSync(this.fileOf(sessionId), { force: true })
|
|
170
|
+
this.cache.set(sessionId, null)
|
|
171
|
+
return null
|
|
172
|
+
} catch (error) {
|
|
173
|
+
this.warn(`[dsh-memory-local] 无法清除会话可见范围 ${this.fileOf(sessionId)}: ${error instanceof Error ? error.message : String(error)}`)
|
|
174
|
+
return this.cache.get(sessionId) ?? null
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const cleaned = { scopes, level: cleanLevel(config.level, 'normal') }
|
|
178
|
+
try {
|
|
179
|
+
const file = this.fileOf(sessionId)
|
|
180
|
+
mkdirSync(this.dir, { recursive: true })
|
|
181
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`
|
|
182
|
+
writeFileSync(tmp, `${JSON.stringify({
|
|
183
|
+
version: VISIBILITY_VERSION,
|
|
184
|
+
scopes: cleaned.scopes,
|
|
185
|
+
level: cleaned.level,
|
|
186
|
+
updatedAt: Date.now(),
|
|
187
|
+
}, null, 2)}\n`, 'utf8')
|
|
188
|
+
renameSync(tmp, file)
|
|
189
|
+
this.cache.set(sessionId, cloneConfig(cleaned))
|
|
190
|
+
return cloneConfig(cleaned)
|
|
191
|
+
} catch (error) {
|
|
192
|
+
// 写盘失败:内存里的旧值不清空,但也不能假装成功。调用方会看到旧值。
|
|
193
|
+
this.warn(`[dsh-memory-local] 无法写入会话可见范围 ${this.fileOf(sessionId)}: ${error instanceof Error ? error.message : String(error)}`)
|
|
194
|
+
const previous = this.cache.get(sessionId) ?? null
|
|
195
|
+
return previous === null ? null : cloneConfig(previous)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** 把缓存里这个会话的值清掉(下一次 get 重新读盘);维护/测试用。 */
|
|
200
|
+
invalidate(sessionId ) {
|
|
201
|
+
this.cache.delete(sessionId)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
//# sourceURL=source/memory-local/src/session-visibility.ts
|