@restregular/dsh-memory 0.18.0 → 0.19.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 +33 -0
- package/README.md +17 -7
- package/lib/client.js +497 -47
- package/lib/inject/apply.js +87 -9
- package/lib/inject/inject.js +11 -6
- package/lib/panel.js +75 -26
- package/lib/provider/apply.js +45 -5
- package/lib/provider/index.js +286 -33
- package/lib/provider/session-visibility.js +104 -30
- package/lib/tools/index.js +226 -30
- package/package.json +1 -1
package/lib/inject/apply.js
CHANGED
|
@@ -103,10 +103,12 @@ import { canonicalWorkspacePath } from '../provider/scope.js'
|
|
|
103
103
|
|
|
104
104
|
/** provider 在注入侧能提供的那部分能力(多余的方法都用可选判断)。 */
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
|
|
107
107
|
|
|
108
108
|
|
|
109
109
|
|
|
110
|
+
|
|
111
|
+
|
|
110
112
|
|
|
111
113
|
|
|
112
114
|
/** 轮末提醒的文案:短、祈使句,不占预算。 */
|
|
@@ -114,6 +116,39 @@ const NUDGE_TEXT =
|
|
|
114
116
|
'上一轮若产生了可复用的结论(决策、踩坑、流程、口径),先 memory_save 记一条再继续;' +
|
|
115
117
|
'没有就忽略这条提醒。'
|
|
116
118
|
|
|
119
|
+
/** 可见域配置的最小子集(避免注入半边依赖 provider 的实现类型)。 */
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
const VISIBILITY_LEVEL_TEXT = {
|
|
126
|
+
absolute: '绝对限制:可见域外的记忆一律查不到,即使工具内部命中也不会返回。',
|
|
127
|
+
normal: '普通限制:可见域外的记忆命中时会向用户发起一次审批;用户拒绝或超时则不返回。',
|
|
128
|
+
none: '无限制:可以读取所有作用域与类型(当前选择仅作为下次切换的偏好保留)。',
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 可见域变更后的上下文通知(以 user 消息注入)。
|
|
133
|
+
* 只描述规则,不回显任何记忆正文 —— 它要告诉模型"边界变了",不是给它数据。
|
|
134
|
+
*/
|
|
135
|
+
export function renderVisibilityNotice(config ) {
|
|
136
|
+
if (config === null) {
|
|
137
|
+
return '【记忆可见范围已恢复默认】本会话不再有显式可见域;memory_search / memory_get / memory_list 回到"会话 + 项目 + global"的默认读取集合。'
|
|
138
|
+
}
|
|
139
|
+
const scopes = config.scopes.length > 0
|
|
140
|
+
? config.scopes.map((rule) => (rule.kinds !== undefined && rule.kinds.length > 0
|
|
141
|
+
? `${rule.scope}(仅 ${rule.kinds.join(' / ')})`
|
|
142
|
+
: rule.scope)).join('、')
|
|
143
|
+
: '(无)'
|
|
144
|
+
return [
|
|
145
|
+
'【记忆可见域已更新】用户修改了本会话(及其子 Agent)的记忆可见范围。',
|
|
146
|
+
`可见域:${scopes}`,
|
|
147
|
+
`限制等级:${VISIBILITY_LEVEL_TEXT[config.level]}`,
|
|
148
|
+
'后续 memory_search / memory_get / memory_list 请按这个边界工作;需要范围外信息时,先向用户说明要查什么,不要绕过限制。',
|
|
149
|
+
].join('\n')
|
|
150
|
+
}
|
|
151
|
+
|
|
117
152
|
export const name = 'dsh-memory-inject'
|
|
118
153
|
|
|
119
154
|
export function apply(ctx , config = {}) {
|
|
@@ -253,13 +288,32 @@ export function apply(ctx , config = {}) {
|
|
|
253
288
|
const sameScopes = (a , b ) =>
|
|
254
289
|
a.length === b.length && a.every((scope, index) => scope === b[index])
|
|
255
290
|
|
|
291
|
+
/** 可见域的类型白名单(scope → kinds);undefined = 不按类型过滤。 */
|
|
292
|
+
const visibilityKindsFor = (memory , agent ) => {
|
|
293
|
+
try {
|
|
294
|
+
const policy = memory.visibilityPolicy?.(agent)
|
|
295
|
+
if (policy === undefined || policy.level === 'none') return undefined
|
|
296
|
+
const out = {}
|
|
297
|
+
for (const rule of policy.scopes) {
|
|
298
|
+
if (rule.kinds !== undefined && rule.kinds.length > 0) out[rule.scope] = [...rule.kinds]
|
|
299
|
+
}
|
|
300
|
+
return Object.keys(out).length > 0 ? out : undefined
|
|
301
|
+
} catch {
|
|
302
|
+
return undefined
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
256
306
|
const renderRulesFor = (
|
|
257
307
|
memory ,
|
|
258
308
|
scopes ,
|
|
259
309
|
cfg ,
|
|
310
|
+
agent ,
|
|
260
311
|
) => {
|
|
261
312
|
try {
|
|
262
|
-
return {
|
|
313
|
+
return {
|
|
314
|
+
ok: true,
|
|
315
|
+
render: renderRules(memory.listSync?.({ scopes: [...scopes], kind: 'rule', limit: 100, ...(agent !== undefined ? { agent } : {}) }) ?? [], cfg),
|
|
316
|
+
}
|
|
263
317
|
} catch {
|
|
264
318
|
return { ok: false, errorCode: 'source_load_failed' }
|
|
265
319
|
}
|
|
@@ -495,7 +549,7 @@ export function apply(ctx , config = {}) {
|
|
|
495
549
|
}
|
|
496
550
|
|
|
497
551
|
const injector = new MemoryInjector(
|
|
498
|
-
async (query, signal, scopes) => {
|
|
552
|
+
async (query, signal, scopes, kinds) => {
|
|
499
553
|
const memory = ctx.get('memory')
|
|
500
554
|
if (!memory?.search) return []
|
|
501
555
|
return memory.search({
|
|
@@ -503,6 +557,7 @@ export function apply(ctx , config = {}) {
|
|
|
503
557
|
signal,
|
|
504
558
|
topK: recallCfg().topK,
|
|
505
559
|
...(scopes !== undefined ? { scopes: [...scopes] } : {}),
|
|
560
|
+
...(kinds !== undefined ? { kinds } : {}),
|
|
506
561
|
})
|
|
507
562
|
},
|
|
508
563
|
recallCfg,
|
|
@@ -511,12 +566,13 @@ export function apply(ctx , config = {}) {
|
|
|
511
566
|
)
|
|
512
567
|
|
|
513
568
|
// 摘要:provider 没有 list(旧版/半边加载失败)时自动退化为"不发摘要"。
|
|
514
|
-
const digester = new MemoryDigester(async (scopes) => {
|
|
569
|
+
const digester = new MemoryDigester(async (scopes, agent) => {
|
|
515
570
|
const memory = ctx.get('memory')
|
|
516
571
|
if (!memory?.list) return []
|
|
517
572
|
return memory.list({
|
|
518
573
|
...(scopes !== undefined ? { scopes: [...scopes] } : {}),
|
|
519
574
|
limit: 200,
|
|
575
|
+
...(agent !== undefined ? { agent } : {}),
|
|
520
576
|
})
|
|
521
577
|
})
|
|
522
578
|
// 持久化状态恢复后,把「摘要已做过」同步给摘要器,避免插件重载后重复注入。
|
|
@@ -700,7 +756,7 @@ export function apply(ctx , config = {}) {
|
|
|
700
756
|
if (cached !== undefined && started !== undefined && cached.at >= started && sameScopes(cached.scopes, scopes)) {
|
|
701
757
|
return cached.render.text
|
|
702
758
|
}
|
|
703
|
-
const rendered = renderRulesFor(memory, scopes, settingsOf().rules)
|
|
759
|
+
const rendered = renderRulesFor(memory, scopes, settingsOf().rules, context?.agent)
|
|
704
760
|
if (!rendered.ok) return ''
|
|
705
761
|
if (sessionId !== undefined) rulesCache.set(sessionId, { at: Date.now(), scopes, render: rendered.render })
|
|
706
762
|
return rendered.render.text
|
|
@@ -717,7 +773,7 @@ export function apply(ctx , config = {}) {
|
|
|
717
773
|
const sessionId = sessionIdOf(context?.agent)
|
|
718
774
|
let records = []
|
|
719
775
|
try {
|
|
720
|
-
records = memory.listSync({ scopes: [...scopes], kind: 'procedure', limit: 200 })
|
|
776
|
+
records = memory.listSync({ scopes: [...scopes], kind: 'procedure', limit: 200, ...(context?.agent !== undefined ? { agent: context.agent } : {}) })
|
|
721
777
|
} catch {
|
|
722
778
|
return ''
|
|
723
779
|
}
|
|
@@ -769,6 +825,11 @@ export function apply(ctx , config = {}) {
|
|
|
769
825
|
attachSessionState(sessionId, workspaceOf(agent))
|
|
770
826
|
if (sessionId !== undefined) rulesStepStart.set(sessionId, stepStartedAt)
|
|
771
827
|
const scopes = readScopesFor(memory, agent)
|
|
828
|
+
// 可见域的类型白名单:召回/摘要/规则/流程索引都按它收窄,避免"只让看 global
|
|
829
|
+
// 的 rule"之后又从其它类型里召回正文。
|
|
830
|
+
const visibleKinds = visibilityKindsFor(memory, agent)
|
|
831
|
+
// 面板保存可见域后,下一次 pre-step 用一条 user 消息告知 Agent 边界已变。
|
|
832
|
+
const visibilityNotice = memory.takeVisibilityNotice?.(agent)
|
|
772
833
|
if (sessionId !== undefined && scopes !== undefined) sessionScopes.set(sessionId, [...scopes])
|
|
773
834
|
let autoScopes
|
|
774
835
|
try { autoScopes = autoRecallScopes(memory.resolveReadScopes?.(agent) ) } catch { autoScopes = undefined }
|
|
@@ -822,6 +883,17 @@ export function apply(ctx , config = {}) {
|
|
|
822
883
|
const messageChannels = new Set ()
|
|
823
884
|
const stage = (entry ) => { staged.push(entry) }
|
|
824
885
|
|
|
886
|
+
// 可见域刚被用户改过:先告知边界,再走后续召回/摘要。只注入规则说明,不回显记忆。
|
|
887
|
+
if (visibilityNotice !== undefined) {
|
|
888
|
+
const noticeText = renderVisibilityNotice(visibilityNotice.config)
|
|
889
|
+
parts.push(noticeText)
|
|
890
|
+
messageChannels.add('policy')
|
|
891
|
+
stage(auditBase('policy', {
|
|
892
|
+
kind: 'evaluation', stage: 'rendered', outcome: 'success', reasonCode: 'visibility_changed',
|
|
893
|
+
delivery: { status: 'unknown', appended: null, textChars: noticeText.length, textHash: hashAuditText(noticeText) },
|
|
894
|
+
}))
|
|
895
|
+
}
|
|
896
|
+
|
|
825
897
|
if (signal !== undefined && usableSignal === undefined) {
|
|
826
898
|
recordAudit(auditBase('signal', {
|
|
827
899
|
kind: 'signal', stage: 'signal_dropped', outcome: 'skipped',
|
|
@@ -909,7 +981,7 @@ export function apply(ctx , config = {}) {
|
|
|
909
981
|
const rendered = renderDigest(records, settings.digest)
|
|
910
982
|
digestStats = rendered?.stats
|
|
911
983
|
return rendered
|
|
912
|
-
})
|
|
984
|
+
}, agent)
|
|
913
985
|
// 失败也记 done(与内存行为一致:摘要不逐轮重试);落盘后重载不再重复发。
|
|
914
986
|
if (sessionId !== undefined && outcome.status !== 'skipped') injectionState.markDigestDone(sessionId)
|
|
915
987
|
if (outcome.status === 'failed') {
|
|
@@ -971,7 +1043,7 @@ export function apply(ctx , config = {}) {
|
|
|
971
1043
|
stage(auditBase('rules', { kind: 'evaluation', stage: 'skipped', outcome: 'skipped', reasonCode: 'system_prompt_unavailable' }))
|
|
972
1044
|
} else {
|
|
973
1045
|
const promptScopes = promptScopesFor(memory, agent)
|
|
974
|
-
const rendered = renderRulesFor(memory, promptScopes, settings.rules)
|
|
1046
|
+
const rendered = renderRulesFor(memory, promptScopes, settings.rules, agent)
|
|
975
1047
|
if (!rendered.ok) {
|
|
976
1048
|
stage(auditBase('rules', {
|
|
977
1049
|
kind: 'evaluation', stage: 'source_load', outcome: 'failed', reasonCode: rendered.errorCode,
|
|
@@ -1059,7 +1131,13 @@ export function apply(ctx , config = {}) {
|
|
|
1059
1131
|
})
|
|
1060
1132
|
}
|
|
1061
1133
|
const recall = await injector.maybeInjectDetailed(
|
|
1062
|
-
{
|
|
1134
|
+
{
|
|
1135
|
+
messages: input.messages ,
|
|
1136
|
+
signal: input.signal,
|
|
1137
|
+
...(recallScopes !== undefined ? { scopes: recallScopes } : {}),
|
|
1138
|
+
...(visibleKinds !== undefined ? { kinds: visibleKinds } : {}),
|
|
1139
|
+
...(sessionId !== undefined ? { sessionId } : {}),
|
|
1140
|
+
},
|
|
1063
1141
|
{ messages: decision.messages },
|
|
1064
1142
|
{
|
|
1065
1143
|
...(sessionId !== undefined ? { sessionId } : {}),
|
package/lib/inject/inject.js
CHANGED
|
@@ -40,6 +40,8 @@ export const defaultInjectConfig = {
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
|
|
43
|
+
|
|
44
|
+
|
|
43
45
|
|
|
44
46
|
|
|
45
47
|
|
|
@@ -140,6 +142,8 @@ export function resolveInjectScopes(
|
|
|
140
142
|
|
|
141
143
|
|
|
142
144
|
|
|
145
|
+
|
|
146
|
+
|
|
143
147
|
|
|
144
148
|
|
|
145
149
|
|
|
@@ -323,13 +327,13 @@ export function recallText(text ) {
|
|
|
323
327
|
* - 同一段文本 = 最后一道幂等。
|
|
324
328
|
*/
|
|
325
329
|
export class MemoryInjector {
|
|
326
|
-
search
|
|
330
|
+
search
|
|
327
331
|
cfg
|
|
328
332
|
renderOptions
|
|
329
333
|
state
|
|
330
334
|
|
|
331
335
|
constructor(
|
|
332
|
-
search
|
|
336
|
+
search ,
|
|
333
337
|
cfg ,
|
|
334
338
|
renderOptions = {},
|
|
335
339
|
state = new InjectionState(),
|
|
@@ -362,7 +366,7 @@ export class MemoryInjector {
|
|
|
362
366
|
|
|
363
367
|
let hits
|
|
364
368
|
try {
|
|
365
|
-
hits = await this.search(query, input.signal, input.scopes)
|
|
369
|
+
hits = await this.search(query, input.signal, input.scopes, input.kinds)
|
|
366
370
|
} catch {
|
|
367
371
|
// 检索失败不能打断 agent 循环;审计会区分"失败"与"成功但无结果"。
|
|
368
372
|
return {
|
|
@@ -460,10 +464,10 @@ const MAX_DIGESTED_SESSIONS = 500
|
|
|
460
464
|
*/
|
|
461
465
|
export class MemoryDigester {
|
|
462
466
|
done = new Set ()
|
|
463
|
-
list
|
|
467
|
+
list
|
|
464
468
|
|
|
465
469
|
constructor(
|
|
466
|
-
list
|
|
470
|
+
list ,
|
|
467
471
|
) {
|
|
468
472
|
this.list = list
|
|
469
473
|
}
|
|
@@ -505,6 +509,7 @@ export class MemoryDigester {
|
|
|
505
509
|
sessionId ,
|
|
506
510
|
scopes ,
|
|
507
511
|
render ,
|
|
512
|
+
agent ,
|
|
508
513
|
)
|
|
509
514
|
|
|
510
515
|
|
|
@@ -515,7 +520,7 @@ export class MemoryDigester {
|
|
|
515
520
|
this.markDone(sessionId)
|
|
516
521
|
let records
|
|
517
522
|
try {
|
|
518
|
-
records = await this.list(scopes)
|
|
523
|
+
records = await this.list(scopes, agent)
|
|
519
524
|
} catch {
|
|
520
525
|
return { status: 'failed', errorCode: 'source_load_failed' }
|
|
521
526
|
}
|
package/lib/panel.js
CHANGED
|
@@ -470,11 +470,15 @@ function resolveScopeSelection(memory, rawScope) {
|
|
|
470
470
|
return normalized.scope ?? null
|
|
471
471
|
}
|
|
472
472
|
|
|
473
|
+
/** 面板已知的记忆类型(与工具的 kind enum 一致)。 */
|
|
473
474
|
/**
|
|
474
|
-
*
|
|
475
|
+
* 会话「记忆可见域」的完整快照:自己的覆盖 + 生效策略 + 可选范围目录。
|
|
475
476
|
*
|
|
476
477
|
* 自己的覆盖(null = 没设置)用于面板上的"恢复默认";effective 用于预选(子 Agent
|
|
477
|
-
* 会话会继承父会话的覆盖);
|
|
478
|
+
* 会话会继承父会话的覆盖);defaults 是"没有显式覆盖时 Agent 实际会读的集合",
|
|
479
|
+
* 首次打开面板时按它预选,而不是显示成什么都没选。
|
|
480
|
+
*
|
|
481
|
+
* 0.19.1 起不再支持按记忆类型收窄:目录只返回 scope,不再返回 kinds。
|
|
478
482
|
*/
|
|
479
483
|
function visibilityPayload(ctx, memory, sessionId) {
|
|
480
484
|
let session
|
|
@@ -484,44 +488,52 @@ function visibilityPayload(ctx, memory, sessionId) {
|
|
|
484
488
|
let effective = null
|
|
485
489
|
let inherited = false
|
|
486
490
|
try {
|
|
487
|
-
if (typeof memory.
|
|
488
|
-
const
|
|
489
|
-
if (
|
|
490
|
-
effective =
|
|
491
|
+
if (typeof memory.visibilityPolicy === 'function') {
|
|
492
|
+
const policy = memory.visibilityPolicy(agentLike)
|
|
493
|
+
if (policy !== undefined && policy !== null) {
|
|
494
|
+
effective = policy
|
|
491
495
|
inherited = own === null
|
|
492
496
|
}
|
|
493
497
|
}
|
|
494
|
-
|
|
495
|
-
const resolved = memory.resolveReadScopes?.(session !== undefined ? { session } : undefined)
|
|
496
|
-
if (Array.isArray(resolved)) effective = resolved
|
|
497
|
-
}
|
|
498
|
-
} catch { /* 可见范围是增强;拿不到就只返回自己的覆盖 */ }
|
|
498
|
+
} catch { /* 可见域是增强;拿不到就只返回自己的覆盖 */ }
|
|
499
499
|
|
|
500
500
|
let refs
|
|
501
501
|
try { if (session !== undefined) refs = resolveCurrentScopes({ session }) } catch { refs = undefined }
|
|
502
|
+
// 没有显式覆盖时,面板仍预选"当前默认读取集合"(会话 + 项目 + global),
|
|
503
|
+
// 让用户看到 Agent 现在实际能看到什么;保存后才变成显式覆盖。
|
|
504
|
+
let defaults = []
|
|
505
|
+
try {
|
|
506
|
+
const resolved = memory.resolveReadScopes?.(agentLike)
|
|
507
|
+
if (Array.isArray(resolved)) defaults = resolved
|
|
508
|
+
} catch { /* 降级为空 */ }
|
|
509
|
+
|
|
502
510
|
const stats = typeof memory.scopeStats === 'function' ? memory.scopeStats() : []
|
|
503
511
|
const byScope = new Map()
|
|
504
512
|
for (const item of Array.isArray(stats) ? stats : []) {
|
|
505
|
-
if (item && typeof item.scope === 'string') byScope.set(item.scope, item)
|
|
513
|
+
if (item && typeof item.scope === 'string') byScope.set(item.scope, { ...item })
|
|
506
514
|
}
|
|
507
|
-
//
|
|
515
|
+
// 即使还没有任何记录 / 宿主没给 sessions,也要能把默认集合里的 scope 勾出来。
|
|
508
516
|
const ensure = (scope) => {
|
|
509
517
|
if (typeof scope === 'string' && scope !== '' && !byScope.has(scope)) {
|
|
510
518
|
byScope.set(scope, { scope, records: 0, pendingEmbeds: 0 })
|
|
511
519
|
}
|
|
512
520
|
}
|
|
513
|
-
|
|
514
|
-
|
|
521
|
+
const sessionScope = refs?.session ?? defaults.find((scope) => scope.startsWith('session:'))
|
|
522
|
+
const projectScope = refs?.project ?? defaults.find((scope) => scope.startsWith('project:'))
|
|
523
|
+
ensure(sessionScope)
|
|
524
|
+
ensure(projectScope)
|
|
515
525
|
ensure('global')
|
|
516
|
-
|
|
526
|
+
for (const scope of defaults) ensure(scope)
|
|
527
|
+
const rank = (scope) => scope === sessionScope ? 0 : scope === projectScope ? 1 : scope === 'global' ? 2 : 3
|
|
517
528
|
const scopes = [...byScope.values()]
|
|
518
529
|
.sort((a, b) => rank(a.scope) - rank(b.scope) || String(a.scope).localeCompare(String(b.scope)))
|
|
519
530
|
.map((item) => ({ ...item, label: item.name ?? scopeLabel(ctx, item.scope) }))
|
|
520
531
|
return {
|
|
521
532
|
sessionId,
|
|
522
533
|
override: own,
|
|
523
|
-
effective
|
|
534
|
+
effective,
|
|
524
535
|
inherited,
|
|
536
|
+
defaults,
|
|
525
537
|
scopes,
|
|
526
538
|
}
|
|
527
539
|
}
|
|
@@ -613,25 +625,62 @@ async function handle(ctx, routePath, req, res) {
|
|
|
613
625
|
const sessionId = typeof body?.session === 'string' ? body.session.trim() : ''
|
|
614
626
|
if (sessionId === '') throw new ApiError(400, 'SESSION_REQUIRED', 'session 不能为空')
|
|
615
627
|
let next = null
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
628
|
+
const rawConfig = body?.visibility
|
|
629
|
+
// 新协议:{ visibility: { scopes: [{ scope }], level } } | null
|
|
630
|
+
// 兼容 0.19.0 的 { scopes: [{ scope, kinds? }] } 与 0.18 的 { scopes: string[] }:
|
|
631
|
+
// 0.19.1 起不再限制记忆类型,提交里的 kinds 一律忽略。
|
|
632
|
+
const rawScopes = rawConfig !== null && rawConfig !== undefined && typeof rawConfig === 'object'
|
|
633
|
+
? rawConfig.scopes
|
|
634
|
+
: body?.scopes
|
|
635
|
+
if (rawScopes !== null && rawScopes !== undefined) {
|
|
636
|
+
if (!Array.isArray(rawScopes)) throw new ApiError(400, 'SCOPE_INVALID', 'scopes 必须是数组或 null')
|
|
637
|
+
if (rawScopes.length === 0) throw new ApiError(400, 'SCOPE_EMPTY', '至少选择一个可见范围')
|
|
638
|
+
const scopes = []
|
|
639
|
+
for (const raw of rawScopes) {
|
|
640
|
+
const scopeRaw = typeof raw === 'string' ? raw : raw?.scope
|
|
641
|
+
if (typeof scopeRaw !== 'string' || scopeRaw.trim() === '') {
|
|
642
|
+
throw new ApiError(400, 'SCOPE_INVALID', 'scope 必须是非空字符串')
|
|
643
|
+
}
|
|
644
|
+
const normalized = normalizeScopeInput(scopeRaw)
|
|
623
645
|
if (!normalized.ok) throw new ApiError(400, normalized.code, normalized.message)
|
|
624
646
|
if (normalized.kind === 'all' || normalized.kind === 'current-project' || normalized.kind === 'current-session') {
|
|
625
|
-
throw new ApiError(400, 'SCOPE_INVALID', `可见范围必须是具体作用域键: ${JSON.stringify(
|
|
647
|
+
throw new ApiError(400, 'SCOPE_INVALID', `可见范围必须是具体作用域键: ${JSON.stringify(scopeRaw)}`)
|
|
626
648
|
}
|
|
627
|
-
if (normalized.scope
|
|
649
|
+
if (normalized.scope === undefined || scopes.some((item) => item.scope === normalized.scope)) continue
|
|
650
|
+
scopes.push({ scope: normalized.scope })
|
|
628
651
|
}
|
|
652
|
+
const levelRaw = rawConfig !== null && rawConfig !== undefined && typeof rawConfig === 'object' ? rawConfig.level : 'absolute'
|
|
653
|
+
const level = levelRaw === 'normal' || levelRaw === 'none' ? levelRaw : 'absolute'
|
|
654
|
+
next = { scopes, level }
|
|
629
655
|
}
|
|
630
656
|
memory.setSessionVisibility(sessionId, next)
|
|
631
657
|
writeJson(res, { ok: true, value: visibilityPayload(ctx, memory, sessionId) })
|
|
632
658
|
return
|
|
633
659
|
}
|
|
634
660
|
|
|
661
|
+
// 普通限制下的范围外访问审批:客户端浮层轮询待审批,用户点「拒绝 / 允许一次」后回写。
|
|
662
|
+
if (method === 'GET' && path === '/approvals') {
|
|
663
|
+
if (typeof memory.pendingVisibilityApprovals !== 'function') {
|
|
664
|
+
throw new ApiError(501, 'NOT_SUPPORTED', 'provider 未提供可见域审批接口(请升级 @restregular/dsh-memory)')
|
|
665
|
+
}
|
|
666
|
+
const sessionId = (url.searchParams.get('session') ?? '').trim()
|
|
667
|
+
writeJson(res, { ok: true, value: { approvals: memory.pendingVisibilityApprovals(sessionId === '' ? undefined : sessionId) } })
|
|
668
|
+
return
|
|
669
|
+
}
|
|
670
|
+
if (method === 'POST' && path === '/approvals') {
|
|
671
|
+
if (typeof memory.answerVisibilityApproval !== 'function') {
|
|
672
|
+
throw new ApiError(501, 'NOT_SUPPORTED', 'provider 未提供可见域审批接口(请升级 @restregular/dsh-memory)')
|
|
673
|
+
}
|
|
674
|
+
const body = await readJsonBody(req)
|
|
675
|
+
const id = typeof body?.id === 'string' ? body.id.trim() : ''
|
|
676
|
+
if (id === '') throw new ApiError(400, 'APPROVAL_ID_REQUIRED', 'id 不能为空')
|
|
677
|
+
const outcome = body?.outcome === 'allowed-once' ? 'allowed-once' : body?.outcome === 'rejected' ? 'rejected' : ''
|
|
678
|
+
if (outcome === '') throw new ApiError(400, 'APPROVAL_OUTCOME_INVALID', 'outcome 必须是 allowed-once 或 rejected')
|
|
679
|
+
const answered = memory.answerVisibilityApproval(id, outcome)
|
|
680
|
+
writeJson(res, { ok: true, value: { answered } })
|
|
681
|
+
return
|
|
682
|
+
}
|
|
683
|
+
|
|
635
684
|
if (method === 'GET' && path === '/status') {
|
|
636
685
|
const stats = typeof memory.scopeStats === 'function'
|
|
637
686
|
? memory.scopeStats()
|
package/lib/provider/apply.js
CHANGED
|
@@ -243,14 +243,54 @@ export class CordisMemoryService extends Service {
|
|
|
243
243
|
return this.impl.visibleScopes(agent)
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
-
/**
|
|
247
|
-
|
|
246
|
+
/** 完整可见域(等级 + 每个 scope 的类型白名单)。 */
|
|
247
|
+
visibilityPolicy(agent ) {
|
|
248
|
+
return this.impl.visibilityPolicy(agent)
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** 某个 scope 下允许的记忆类型(undefined = 不限制)。 */
|
|
252
|
+
visibleKinds(agent , scope ) {
|
|
253
|
+
return this.impl.visibleKinds(agent, scope)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** 一条记录是否落在当前可见域内。 */
|
|
257
|
+
isRecordVisible(agent , scope , kind ) {
|
|
258
|
+
return this.impl.isRecordVisible(agent, scope, kind)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** 面板:读取某会话自己的可见域(null = 未设置)。 */
|
|
262
|
+
sessionVisibility(sessionId ) {
|
|
248
263
|
return this.impl.sessionVisibility(sessionId)
|
|
249
264
|
}
|
|
250
265
|
|
|
251
|
-
/**
|
|
252
|
-
setSessionVisibility(sessionId ,
|
|
253
|
-
return this.impl.setSessionVisibility(sessionId,
|
|
266
|
+
/** 面板:设置/清除某会话的可见域(null = 恢复默认)。 */
|
|
267
|
+
setSessionVisibility(sessionId , config ) {
|
|
268
|
+
return this.impl.setSessionVisibility(sessionId, config)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 注入半边:下一次 pre-step 要注入的可见域变更通知(取走即删)。 */
|
|
272
|
+
takeVisibilityNotice(agent ) {
|
|
273
|
+
return this.impl.takeVisibilityNotice(agent)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** 面板:每个 scope 下按类型统计条数。 */
|
|
277
|
+
scopeKindStats() {
|
|
278
|
+
return this.impl.scopeKindStats()
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** 普通限制:范围外访问审批(挂起,等面板回答)。 */
|
|
282
|
+
requestOutOfScope(agent , input ) {
|
|
283
|
+
return this.impl.requestOutOfScope(agent, input)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 面板:当前待审批列表。 */
|
|
287
|
+
pendingVisibilityApprovals(sessionId ) {
|
|
288
|
+
return this.impl.pendingVisibilityApprovals(sessionId)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** 面板:回答一条待审批。 */
|
|
292
|
+
answerVisibilityApproval(id , outcome ) {
|
|
293
|
+
return this.impl.answerVisibilityApproval(id, outcome)
|
|
254
294
|
}
|
|
255
295
|
|
|
256
296
|
/** 写路径默认 scope(省略 scope 时落到哪)。 */
|