@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/lib/client.js CHANGED
@@ -485,24 +485,69 @@ window.__ModuleLoader__.load({
485
485
  '.mem-traj-mark:focus-visible{outline:1px solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:1px}',
486
486
  '@media (max-width:900px){.memp .audit-split.has-detail{grid-template-columns:minmax(0,1fr)}.memp .audit-row{grid-template-columns:54px 120px 120px minmax(100px,1fr) 96px 100px 80px}.memp .audit-table-head{grid-template-columns:54px 120px 120px minmax(100px,1fr) 96px 100px 80px}}',
487
487
  '@media (max-width:620px){.memp .audit-stats{grid-template-columns:repeat(2,minmax(0,1fr))}.memp .audit-table-head{display:none}.memp .audit-row{display:flex;flex-wrap:wrap;gap:4px 8px}.memp .audit-row .col-trigger,.memp .audit-row .col-budget{display:none}.memp .audit-row .col-nums{margin-left:auto}.memp .audit-row .col-time{order:1}.memp .audit-row .col-channel{order:2}.memp .audit-row .col-delivery{order:3}.memp .audit-row .col-outcome{order:4;margin-left:auto}.memp .audit-filter-row input[type=text],.memp .audit-filter-row input[type=search]{max-width:none;flex:1}.memp .audit{height:calc(100vh - 150px);max-height:calc(100vh - 150px)}.memp .audit-detail{max-height:calc(100vh - 280px)}}',
488
- '.mem-scope-btn{display:inline-flex;align-items:center;gap:5px;height:28px;padding:0 8px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:transparent;color:var(--dsw-alias-label-secondary);font-size:12px;line-height:1;cursor:pointer;flex:none}',
488
+ '.mem-scope-wrap{display:inline-flex;min-width:0}',
489
+ '.mem-scope-btn{display:inline-flex;align-items:center;gap:5px;height:28px;padding:0 9px;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:color-mix(in srgb,var(--dsw-alias-bg-layer-1) 55%,transparent);color:var(--dsw-alias-label-secondary);font-size:12px;line-height:1;cursor:pointer;flex:none;transition:border-color .12s ease,color .12s ease,background .12s ease}',
489
490
  '.mem-scope-btn:hover{background:var(--dsw-alias-interactive-bg-hover);color:var(--dsw-alias-label-primary)}',
490
- '.mem-scope-btn.on{color:var(--dsw-alias-brand-primary);border-color:color-mix(in srgb,var(--dsw-alias-brand-primary) 45%,var(--dsw-alias-border-l1))}',
491
+ '.mem-scope-btn.on{color:var(--dsw-alias-brand-primary);border-color:color-mix(in srgb,var(--dsw-alias-brand-primary) 45%,var(--dsw-alias-border-l1));background:color-mix(in srgb,var(--dsw-alias-brand-primary) 9%,transparent)}',
491
492
  '.mem-scope-btn svg{flex:none}',
492
- '.mem-scope-pop{position:fixed;z-index:80;width:300px;max-height:min(460px,62vh);display:flex;flex-direction:column;gap:10px;padding:12px;border:1px solid var(--dsw-alias-border-l1);border-radius:12px;background:var(--dsw-specific-menu,var(--dsw-alias-bg-layer-1));box-shadow:var(--dsw-elevation-panel,0 10px 30px rgba(0,0,0,.22));color:var(--dsw-alias-label-primary);font-size:13px}',
493
- '.mem-scope-pop h4{margin:0;font-size:13px;font-weight:600;line-height:1.4}',
493
+ // 毛玻璃浮窗:半透明 + backdrop blur;不支持 backdrop-filter 时退回实心底。
494
+ '.mem-scope-pop{position:fixed;z-index:80;width:340px;max-height:min(600px,80vh);display:flex;flex-direction:column;gap:9px;padding:14px;border:1px solid color-mix(in srgb,var(--dsw-alias-border-l2,var(--dsw-alias-border-l1)) 60%,transparent);border-radius:14px;background:color-mix(in srgb,var(--dsw-alias-bg-layer-1) 78%,transparent);-webkit-backdrop-filter:blur(20px) saturate(1.35);backdrop-filter:blur(20px) saturate(1.35);box-shadow:0 10px 28px rgba(0,0,0,.18);color:var(--dsw-alias-label-primary);font-size:13px;overflow:hidden}',
495
+ '@supports not ((backdrop-filter:blur(1px)) or (-webkit-backdrop-filter:blur(1px))){.mem-scope-pop{background:var(--dsw-specific-menu,var(--dsw-alias-bg-layer-1))}}',
496
+ '.mem-scope-head{display:flex;align-items:center;gap:8px}',
497
+ '.mem-scope-head h4{margin:0;font-size:13px;font-weight:600;line-height:1.4}',
498
+ '.mem-scope-head .spacer{flex:1}',
499
+ '.mem-scope-count{flex:none;padding:1px 8px;border:1px solid var(--dsw-alias-border-l1);border-radius:999px;color:var(--dsw-alias-label-secondary);font-size:10.5px;line-height:1.7}',
494
500
  '.mem-scope-pop .hint{color:var(--dsw-alias-label-secondary);font-size:12px;line-height:1.55}',
495
- '.mem-scope-list{display:flex;flex-direction:column;gap:2px;overflow:auto;min-height:0;padding:2px 0}',
496
- '.mem-scope-item{display:flex;align-items:center;gap:8px;padding:6px;border-radius:7px;cursor:pointer}',
501
+ '.mem-scope-level{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:5px}',
502
+ '.mem-scope-level-btn{display:flex;flex-direction:column;align-items:flex-start;gap:1px;padding:7px 8px;border:1px solid var(--dsw-alias-border-l1);border-radius:10px;background:transparent;color:var(--dsw-alias-label-secondary);font-size:11px;text-align:left;cursor:pointer;transition:border-color .12s ease,background .12s ease}',
503
+ '.mem-scope-level-btn b{font-size:12px;font-weight:600;color:var(--dsw-alias-label-primary)}',
504
+ '.mem-scope-level-btn span{font-size:9.5px;line-height:1.4;color:var(--dsw-alias-label-secondary)}',
505
+ '.mem-scope-level-btn:hover{border-color:color-mix(in srgb,var(--dsw-alias-brand-primary) 40%,var(--dsw-alias-border-l1))}',
506
+ '.mem-scope-level-btn.on{border-color:color-mix(in srgb,var(--dsw-alias-brand-primary) 55%,var(--dsw-alias-border-l1));background:color-mix(in srgb,var(--dsw-alias-brand-primary) 11%,transparent)}',
507
+ '.mem-scope-level-btn.on b,.mem-scope-level-btn.on span{color:var(--dsw-alias-brand-primary)}',
508
+ '.mem-scope-note{padding:5px 8px;border-radius:8px;background:color-mix(in srgb,var(--dsw-alias-state-warn-primary,#d9a23b) 10%,transparent);color:var(--dsw-alias-label-secondary);font-size:11px;line-height:1.5}',
509
+ '.mem-scope-search{display:flex;align-items:center;gap:6px}',
510
+ '.mem-scope-search input[type=search]{flex:1;min-width:0;padding:5px 9px;font-size:12px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:color-mix(in srgb,var(--dsw-alias-bg-layer-1) 78%,transparent);color:var(--dsw-alias-label-primary);outline:none}',
511
+ '.mem-scope-search input[type=search]:focus{border-color:var(--dsw-alias-brand-primary)}',
512
+ '.mem-scope-search .linkbtn{white-space:nowrap;padding:2px 3px;border:none;background:none;color:var(--dsw-alias-label-secondary);font-size:11px;cursor:pointer}',
513
+ '.mem-scope-search .linkbtn:hover{color:var(--dsw-alias-brand-primary);border:none}',
514
+ '.mem-scope-list{display:flex;flex-direction:column;gap:3px;overflow:auto;min-height:0;max-height:min(300px,40vh);padding:2px 2px 2px 0;scrollbar-width:thin}',
515
+ '.mem-scope-list::-webkit-scrollbar{width:8px}',
516
+ '.mem-scope-list::-webkit-scrollbar-thumb{background:color-mix(in srgb,var(--dsw-alias-label-secondary) 35%,transparent);border-radius:999px;border:2px solid transparent;background-clip:padding-box}',
517
+ '.mem-scope-item{display:flex;flex-direction:column;gap:4px;padding:5px 6px;border:1px solid transparent;border-radius:10px}',
497
518
  '.mem-scope-item:hover{background:var(--dsw-alias-interactive-bg-hover)}',
498
- '.mem-scope-item input{margin:0;flex:none}',
499
- '.mem-scope-item .lab{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
500
- '.mem-scope-item .cnt{flex:none;color:var(--dsw-alias-label-secondary);font-size:11px}',
501
- '.mem-scope-foot{display:flex;align-items:center;gap:8px;justify-content:flex-end}',
519
+ '.mem-scope-item.on{border-color:color-mix(in srgb,var(--dsw-alias-brand-primary) 24%,var(--dsw-alias-border-l1));background:color-mix(in srgb,var(--dsw-alias-brand-primary) 7%,transparent)}',
520
+ '.mem-scope-row{display:flex;align-items:center;gap:8px}',
521
+ '.mem-scope-check{display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;padding:0;border:none;background:transparent;flex:none;cursor:pointer}',
522
+ '.mem-scope-check .box{display:flex;align-items:center;justify-content:center;width:16px;height:16px;border:1.5px solid var(--dsw-alias-border-l2,var(--dsw-alias-border-l1));border-radius:5px;background:transparent;color:var(--dsw-alias-label-on-brand,#fff);transition:background .12s ease,border-color .12s ease}',
523
+ '.mem-scope-check.on .box{background:var(--dsw-alias-brand-primary);border-color:var(--dsw-alias-brand-primary)}',
524
+ '.mem-scope-check:focus-visible{outline:1px solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:1px}',
525
+ '.mem-scope-label{flex:1;min-width:0;padding:0;border:none;background:transparent;color:var(--dsw-alias-label-primary);font-size:12.5px;text-align:left;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;cursor:pointer}',
526
+ '.mem-scope-label:hover{color:var(--dsw-alias-brand-primary);border:none}',
527
+ '.mem-scope-row .cnt{flex:none;color:var(--dsw-alias-label-secondary);font-size:10.5px}',
528
+ '.mem-scope-foot{display:flex;align-items:center;gap:8px}',
502
529
  '.mem-scope-foot .spacer{flex:1}',
503
- '.mem-scope-foot button{padding:5px 12px;font-size:12px}',
504
- '.mem-scope-foot button.primary{background:var(--dsw-alias-brand-primary);border-color:var(--dsw-alias-brand-primary);color:var(--dsw-alias-label-on-brand,#fff)}',
505
- '.mem-scope-err{color:var(--dsw-alias-state-error-primary,#d33);font-size:12px;line-height:1.5}',
530
+ '.mem-scope-reset{padding:6px 13px;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:transparent;color:var(--dsw-alias-label-secondary);font-size:12px;cursor:pointer;transition:border-color .12s ease,color .12s ease}',
531
+ '.mem-scope-reset:hover:not(:disabled){border-color:color-mix(in srgb,var(--dsw-alias-brand-primary) 55%,var(--dsw-alias-border-l1));color:var(--dsw-alias-brand-primary)}',
532
+ '.mem-scope-save{padding:6px 16px;border:1px solid var(--dsw-alias-brand-primary);border-radius:9px;background:var(--dsw-alias-brand-primary);color:var(--dsw-alias-label-on-brand,#fff);font-size:12px;font-weight:600;cursor:pointer;box-shadow:0 2px 8px color-mix(in srgb,var(--dsw-alias-brand-primary) 24%,transparent);transition:filter .12s ease}',
533
+ '.mem-scope-save:hover:not(:disabled){filter:brightness(1.06)}',
534
+ '.mem-scope-reset:disabled,.mem-scope-save:disabled{opacity:.5;cursor:default;filter:none}',
535
+ '.mem-scope-err{padding:5px 8px;border-radius:8px;background:color-mix(in srgb,var(--dsw-alias-state-error-primary,#d33) 10%,transparent);color:var(--dsw-alias-state-error-primary,#d33);font-size:12px;line-height:1.5}',
536
+ // 范围外访问审批浮层(全局 fixed;根节点带 .memp 以复用审计 hover 浮窗样式)
537
+ '.mem-approval{position:fixed;left:50%;bottom:18px;transform:translateX(-50%);z-index:90;width:min(520px,calc(100vw - 24px));max-height:min(70vh,620px);display:flex;flex-direction:column;pointer-events:none;padding:0;font-size:13px;color:var(--dsw-alias-label-primary)}',
538
+ '.mem-approval-card{pointer-events:auto;display:flex;flex-direction:column;gap:8px;padding:14px;border:1px solid color-mix(in srgb,var(--dsw-alias-state-warn-primary,#d9a23b) 42%,var(--dsw-alias-border-l1));border-radius:14px;background:color-mix(in srgb,var(--dsw-alias-bg-layer-1) 82%,transparent);-webkit-backdrop-filter:blur(20px) saturate(1.3);backdrop-filter:blur(20px) saturate(1.3);box-shadow:0 10px 26px rgba(0,0,0,.18);overflow:hidden}',
539
+ '@supports not ((backdrop-filter:blur(1px)) or (-webkit-backdrop-filter:blur(1px))){.mem-approval-card{background:var(--dsw-alias-bg-layer-1)}}',
540
+ '.mem-approval-head{display:flex;align-items:center;gap:7px}',
541
+ '.mem-approval-head b{font-size:13px;font-weight:600}',
542
+ '.mem-approval-head .spacer{flex:1}',
543
+ '.mem-approval-dot{width:8px;height:8px;border-radius:50%;flex:none;background:var(--dsw-alias-state-warn-primary,#d9a23b);box-shadow:0 0 0 3px color-mix(in srgb,var(--dsw-alias-state-warn-primary,#d9a23b) 18%,transparent)}',
544
+ '.mem-approval-query{padding:6px 9px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:color-mix(in srgb,var(--dsw-alias-bg-layer-2) 55%,transparent);color:var(--dsw-alias-label-secondary);font-size:11.5px;line-height:1.5;white-space:pre-wrap;word-break:break-word;max-height:80px;overflow:auto}',
545
+ '.mem-approval-list{display:flex;flex-direction:column;gap:6px;overflow:auto;min-height:0;max-height:min(300px,38vh)}',
546
+ '.mem-approval-item{display:flex;flex-direction:column;gap:4px;padding:7px 8px;border:1px solid var(--dsw-alias-border-l1);border-radius:10px;background:color-mix(in srgb,var(--dsw-alias-bg-layer-1) 55%,transparent)}',
547
+ '.mem-approval-row{display:flex;align-items:center;gap:6px;flex-wrap:wrap}',
548
+ '.mem-approval-summary{color:var(--dsw-alias-label-secondary);font-size:11.5px;line-height:1.5;word-break:break-word}',
549
+ '.mem-approval-foot{display:flex;align-items:center;gap:8px}',
550
+ '.mem-approval .spacer{flex:1}',
506
551
  ].join('')
507
552
 
508
553
  async function api(method, body) {
@@ -2475,6 +2520,53 @@ window.__ModuleLoader__.load({
2475
2520
  return target
2476
2521
  }
2477
2522
 
2523
+ // ===== 记录焦点总线:审批浮层里的记忆 ID → 面板「记录」页并展开卡片 =====
2524
+ const RECORD_FOCUS_EVENT = 'dsh-memory:record-focus'
2525
+ let pendingRecordFocus = null
2526
+
2527
+ function requestRecordFocus(target) {
2528
+ pendingRecordFocus = target && typeof target === 'object' ? { ...target, at: Date.now() } : null
2529
+ try {
2530
+ if (typeof window !== 'undefined' && typeof window.dispatchEvent === 'function' && typeof CustomEvent === 'function') {
2531
+ window.dispatchEvent(new CustomEvent(RECORD_FOCUS_EVENT, { detail: pendingRecordFocus }))
2532
+ }
2533
+ } catch { /* 没有 CustomEvent 时靠面板挂载时取一次 */ }
2534
+ }
2535
+
2536
+ function takeRecordFocus() {
2537
+ const target = pendingRecordFocus
2538
+ if (!target) return null
2539
+ pendingRecordFocus = null
2540
+ return target
2541
+ }
2542
+
2543
+ function peekRecordFocus() {
2544
+ return pendingRecordFocus
2545
+ }
2546
+
2547
+ /**
2548
+ * 打开记忆面板并定位到某条记录(审批浮层 hover 的点击动作)。
2549
+ * 优先侧栏主面板;没有侧栏入口时打开设置页。目标由面板挂载/事件消费。
2550
+ */
2551
+ function openMemoryRecordPanel(id, scope, record) {
2552
+ if (typeof id !== 'string' || id === '') return 'none'
2553
+ requestRecordFocus({ id, scope: typeof scope === 'string' ? scope : '', record: record || null })
2554
+ try {
2555
+ const main = safeSlots()?.entries?.('main') ?? []
2556
+ if (main.some((entry) => entry?.options?.key === 'memory')) {
2557
+ try { clientCtx?.get?.('layout')?.selectPanel?.('memory') } catch { /* 交给挂载消费 */ }
2558
+ return 'panel'
2559
+ }
2560
+ } catch { /* fallthrough */ }
2561
+ try {
2562
+ const settings = clientCtx?.get?.('settings')
2563
+ const openSection = settings?.store?.actions?.openSection ?? settings?.actions?.openSection ?? settings?.openSection
2564
+ if (typeof openSection === 'function') { openSection('memory'); return 'settings' }
2565
+ } catch { /* fallthrough */ }
2566
+ try { clientCtx?.get?.('layout')?.selectPanel?.('settings') } catch { /* 无布局服务 */ }
2567
+ return 'settings'
2568
+ }
2569
+
2478
2570
  /**
2479
2571
  * 从轨迹/头部入口打开记忆审计:优先主面板;没有侧栏入口时切到会话内的
2480
2572
  * 「记忆」视图;两者都不可用时至少打开设置页(面板挂载后会消费 pending)。
@@ -3358,6 +3450,18 @@ window.__ModuleLoader__.load({
3358
3450
  setScopeFilter(nextScope)
3359
3451
  load('', nextScope, false)
3360
3452
  }
3453
+ // 审批浮层 / 其它入口点击记忆 ID:挂载时消费一次 pending,之后靠同一事件。
3454
+ React.useEffect(() => {
3455
+ const applyFocus = () => {
3456
+ const target = takeRecordFocus()
3457
+ if (!target || typeof target.id !== 'string' || target.id === '') return
3458
+ openRecord(target.id, target.scope, target.record || null)
3459
+ }
3460
+ applyFocus()
3461
+ if (typeof window === 'undefined' || typeof window.addEventListener !== 'function') return undefined
3462
+ window.addEventListener(RECORD_FOCUS_EVENT, applyFocus)
3463
+ return () => window.removeEventListener(RECORD_FOCUS_EVENT, applyFocus)
3464
+ }, [])
3361
3465
  // 导入完成后从设置页切换回记录页时,列表要能看到新数据(事件由设置页发出)。
3362
3466
  React.useEffect(() => {
3363
3467
  if (typeof window === 'undefined' || typeof window.addEventListener !== 'function') return undefined
@@ -4342,13 +4446,46 @@ window.__ModuleLoader__.load({
4342
4446
  *
4343
4447
  * 弹层用 fixed 定位(按按钮 rect 往上开),避免被 composer 的 overflow 裁掉。
4344
4448
  */
4449
+ /**
4450
+ * 输入框里的「记忆可见域」按钮 + 浮窗。
4451
+ *
4452
+ * 宿主 `conversation.input.right` 是 session 作用域的 list 槽,owner 会把
4453
+ * `sessionId` 作为标准 prop 传进来,所以这里不需要 register 的 inject。
4454
+ *
4455
+ * 读写走 /memory/api/visibility:
4456
+ * GET ?session=<id> → { override, effective, inherited, scopes, kinds }
4457
+ * POST { session, visibility: { scopes: [{ scope, kinds? }], level } | null } → 同上
4458
+ * `override === null` = 使用默认三路集合;子 Agent 会话会继承父会话的 override。
4459
+ *
4460
+ * 弹层用 fixed 定位(按按钮 rect 往上开),避免被 composer 的 overflow 裁掉。
4461
+ * 视觉:毛玻璃背景 + 自定义勾选 + 类型 chips + 三档限制等级。
4462
+ */
4463
+ /**
4464
+ * 输入框里的「记忆范围」按钮 + 浮窗。
4465
+ *
4466
+ * 宿主 `conversation.input.right` 是 session 作用域的 list 槽,owner 会把
4467
+ * `sessionId` 作为标准 prop 传进来,所以这里不需要 register 的 inject。
4468
+ *
4469
+ * 0.19.1 起:
4470
+ * - 打开会话就 GET 一次,按钮直接显示该会话的范围数量(不再等点击才刷新);
4471
+ * - 按钮只显示数字,不再显示「记忆可见域」文字;
4472
+ * - 首次打开且没有显式覆盖时,按服务端给的 defaults 预选默认范围;
4473
+ * - 不再按记忆类型收窄,只选作用域。
4474
+ *
4475
+ * 读写走 /memory/api/visibility:
4476
+ * GET ?session=<id> → { override, effective, inherited, defaults, scopes }
4477
+ * POST { session, visibility: { scopes: [{ scope }], level } | null } → 同上
4478
+ */
4345
4479
  function MemoryVisibilityButton({ sessionId } = {}) {
4346
4480
  const [open, setOpen] = React.useState(false)
4347
4481
  const [loading, setLoading] = React.useState(false)
4348
4482
  const [busy, setBusy] = React.useState(false)
4349
4483
  const [error, setError] = React.useState('')
4350
4484
  const [data, setData] = React.useState(null)
4351
- const [selected, setSelected] = React.useState([])
4485
+ const [level, setLevel] = React.useState('normal')
4486
+ const [entries, setEntries] = React.useState({})
4487
+ const [catalog, setCatalog] = React.useState([])
4488
+ const [search, setSearch] = React.useState('')
4352
4489
  const [anchor, setAnchor] = React.useState(null)
4353
4490
  const wrapRef = React.useRef(null)
4354
4491
 
@@ -4372,15 +4509,49 @@ window.__ModuleLoader__.load({
4372
4509
  }
4373
4510
  }, [open])
4374
4511
 
4375
- if (typeof sessionId !== 'string' || sessionId === '') return null
4376
-
4377
4512
  const applyPayload = (value) => {
4378
4513
  if (!value || typeof value !== 'object') return
4379
4514
  setData(value)
4380
- const next = Array.isArray(value.override)
4515
+ const config = (value.override && typeof value.override === 'object' && !Array.isArray(value.override))
4381
4516
  ? value.override
4382
- : Array.isArray(value.effective) ? value.effective : []
4383
- setSelected([...next])
4517
+ : (value.effective && typeof value.effective === 'object' && !Array.isArray(value.effective) ? value.effective : null)
4518
+ setLevel(config && (config.level === 'absolute' || config.level === 'none') ? config.level : 'normal')
4519
+ const selected = new Set()
4520
+ const selectedRules = config && Array.isArray(config.scopes) ? config.scopes : []
4521
+ for (const rule of selectedRules) {
4522
+ const scope = typeof rule === 'string' ? rule : rule && typeof rule.scope === 'string' ? rule.scope : ''
4523
+ if (scope !== '') selected.add(scope)
4524
+ }
4525
+ // 没有显式覆盖 / 生效策略时用 defaults(默认会话 + 项目 + global)预选。
4526
+ // 服务端可能没把 defaults 放进目录(旧版或宿主缺 sessions),这里自己补齐。
4527
+ if (selected.size === 0 && Array.isArray(value.defaults)) {
4528
+ for (const scope of value.defaults) if (typeof scope === 'string' && scope !== '') selected.add(scope)
4529
+ }
4530
+ const nextCatalog = []
4531
+ const seen = new Set()
4532
+ for (const item of Array.isArray(value.scopes) ? value.scopes : []) {
4533
+ if (!item || typeof item.scope !== 'string' || item.scope === '' || seen.has(item.scope)) continue
4534
+ seen.add(item.scope)
4535
+ nextCatalog.push(item)
4536
+ }
4537
+ for (const scope of selected) {
4538
+ if (seen.has(scope)) continue
4539
+ seen.add(scope)
4540
+ nextCatalog.push({ scope, label: scope, records: 0 })
4541
+ }
4542
+ // 旧服务端没有 defaults 时的兜底:至少把默认三路(会话 / 项目 / global)选上,
4543
+ // 不要再出现"一打开什么都没选"。
4544
+ if (selected.size === 0) {
4545
+ for (const item of nextCatalog) {
4546
+ if (item.scope === 'global' || item.scope.startsWith('session:') || item.scope.startsWith('project:')) {
4547
+ selected.add(item.scope)
4548
+ }
4549
+ }
4550
+ }
4551
+ const nextEntries = {}
4552
+ for (const item of nextCatalog) nextEntries[item.scope] = selected.has(item.scope)
4553
+ setCatalog(nextCatalog)
4554
+ setEntries(nextEntries)
4384
4555
  }
4385
4556
 
4386
4557
  const load = () => {
@@ -4392,6 +4563,12 @@ window.__ModuleLoader__.load({
4392
4563
  .finally(() => setLoading(false))
4393
4564
  }
4394
4565
 
4566
+ // 打开会话就加载一次:按钮上的数字来自这个会话的设置,不需要用户先点按钮。
4567
+ React.useEffect(() => {
4568
+ if (typeof sessionId !== 'string' || sessionId === '') return
4569
+ load()
4570
+ }, [sessionId])
4571
+
4395
4572
  const toggle = (event) => {
4396
4573
  if (open) { setOpen(false); return }
4397
4574
  let nextAnchor = null
@@ -4402,18 +4579,35 @@ window.__ModuleLoader__.load({
4402
4579
  if (rect) nextAnchor = { left: rect.left, top: rect.top }
4403
4580
  } catch { nextAnchor = null }
4404
4581
  setAnchor(nextAnchor)
4582
+ setSearch('')
4405
4583
  setOpen(true)
4406
4584
  load()
4407
4585
  }
4408
4586
 
4409
4587
  const toggleScope = (scope) => {
4410
- setSelected((prev) => prev.includes(scope) ? prev.filter((item) => item !== scope) : [...prev, scope])
4588
+ setEntries((prev) => ({ ...prev, [scope]: !prev[scope] }))
4589
+ }
4590
+
4591
+ const selectAll = () => {
4592
+ setEntries((prev) => {
4593
+ const next = { ...prev }
4594
+ for (const key of Object.keys(next)) next[key] = true
4595
+ return next
4596
+ })
4597
+ }
4598
+
4599
+ const clearAll = () => {
4600
+ setEntries((prev) => {
4601
+ const next = { ...prev }
4602
+ for (const key of Object.keys(next)) next[key] = false
4603
+ return next
4604
+ })
4411
4605
  }
4412
4606
 
4413
- const save = (scopes) => {
4607
+ const save = (config) => {
4414
4608
  setBusy(true)
4415
4609
  setError('')
4416
- api('visibility', { session: sessionId, scopes })
4610
+ api('visibility', { session: sessionId, visibility: config })
4417
4611
  .then((value) => {
4418
4612
  applyPayload(value)
4419
4613
  setOpen(false)
@@ -4422,12 +4616,32 @@ window.__ModuleLoader__.load({
4422
4616
  .finally(() => setBusy(false))
4423
4617
  }
4424
4618
 
4425
- const scopes = data && Array.isArray(data.scopes) ? data.scopes : []
4426
- const hasOverride = Boolean(data && Array.isArray(data.override))
4427
- const count = selected.length
4619
+ const saveDraft = () => {
4620
+ const scopes = catalog.filter((item) => entries[item.scope]).map((item) => ({ scope: item.scope }))
4621
+ if (scopes.length === 0) { setError('至少选择一个可见范围'); return }
4622
+ save({ scopes, level })
4623
+ }
4624
+
4625
+ if (typeof sessionId !== 'string' || sessionId === '') return null
4626
+
4627
+ const override = data && data.override && typeof data.override === 'object' && !Array.isArray(data.override)
4628
+ ? data.override
4629
+ : null
4630
+ const hasOverride = override !== null
4631
+ const effectiveConfig = override
4632
+ ?? (data && data.effective && typeof data.effective === 'object' && !Array.isArray(data.effective) ? data.effective : null)
4633
+ const effectiveCount = effectiveConfig && Array.isArray(effectiveConfig.scopes)
4634
+ ? effectiveConfig.scopes.length
4635
+ : (data && Array.isArray(data.defaults) ? data.defaults.length : 0)
4636
+ const selectedCount = catalog.filter((item) => entries[item.scope]).length
4637
+ const needle = search.trim().toLowerCase()
4638
+ const visibleScopes = catalog.filter((item) => {
4639
+ if (!needle) return true
4640
+ return String(item.label || item.scope).toLowerCase().includes(needle) || String(item.scope).toLowerCase().includes(needle)
4641
+ })
4428
4642
  let popStyle = {}
4429
4643
  if (anchor) {
4430
- const width = 300
4644
+ const width = 340
4431
4645
  const margin = 8
4432
4646
  const viewportW = (typeof window !== 'undefined' && Number(window.innerWidth)) || 1024
4433
4647
  const viewportH = (typeof window !== 'undefined' && Number(window.innerHeight)) || 768
@@ -4437,60 +4651,282 @@ window.__ModuleLoader__.load({
4437
4651
  }
4438
4652
  }
4439
4653
 
4654
+ const levelMeta = [
4655
+ { value: 'absolute', label: '绝对限制', hint: '范围外查不到' },
4656
+ { value: 'normal', label: '普通限制', hint: '范围外需审批' },
4657
+ { value: 'none', label: '无限制', hint: '不限制读取' },
4658
+ ]
4659
+
4440
4660
  return h('span', { className: 'mem-scope-wrap', ref: wrapRef },
4441
4661
  h('button', {
4442
4662
  type: 'button',
4443
4663
  className: `mem-scope-btn${hasOverride ? ' on' : ''}`,
4444
- title: '记忆可见范围(本会话及子 Agent)',
4445
- 'aria-label': '记忆可见范围',
4664
+ title: hasOverride
4665
+ ? `本会话已设置记忆范围:${effectiveCount} 个作用域`
4666
+ : `本会话使用默认记忆范围:${effectiveCount} 个作用域`,
4667
+ 'aria-label': `记忆范围 ${effectiveCount}`,
4446
4668
  'aria-expanded': open,
4447
4669
  onMouseDown: (event) => { try { event.preventDefault() } catch { /* keepFocus */ } },
4448
4670
  onClick: toggle,
4449
4671
  },
4450
4672
  h(NavLineIcon, { size: 14 }),
4451
- h('span', null, count > 0 ? `记忆 ${count}` : '记忆范围'),
4673
+ data === null ? null : h('span', null, String(effectiveCount)),
4452
4674
  ),
4453
- open ? h('div', { className: 'mem-scope-pop', style: popStyle, role: 'dialog', 'aria-label': '记忆可见范围' },
4454
- h('h4', null, '记忆可见范围'),
4675
+ open ? h('div', { className: 'mem-scope-pop', style: popStyle, role: 'dialog', 'aria-label': '记忆范围' },
4676
+ h('div', { className: 'mem-scope-head' },
4677
+ h('h4', null, '记忆范围'),
4678
+ h('span', { className: 'spacer' }),
4679
+ h('span', { className: 'mem-scope-count' }, `已选 ${selectedCount}`),
4680
+ ),
4455
4681
  h('div', { className: 'hint' },
4456
- '本会话的 Agent 及其子 Agent 只能检索和读取勾选的范围;未勾选的范围不会被注入。'),
4682
+ 'Agent 默认只能检索和读取勾选的作用域;限制等级决定范围外怎么处理。'),
4457
4683
  data && data.inherited
4458
4684
  ? h('div', { className: 'hint' }, '当前继承自父会话;保存后在本会话独立生效。')
4459
4685
  : null,
4460
- loading
4686
+ h('div', { className: 'mem-scope-level', role: 'radiogroup', 'aria-label': '限制等级' },
4687
+ levelMeta.map((item) => h('button', {
4688
+ key: item.value,
4689
+ type: 'button',
4690
+ role: 'radio',
4691
+ 'aria-checked': level === item.value,
4692
+ className: `mem-scope-level-btn${level === item.value ? ' on' : ''}`,
4693
+ onClick: () => setLevel(item.value),
4694
+ title: item.hint,
4695
+ }, h('b', null, item.label), h('span', null, item.hint))),
4696
+ ),
4697
+ level === 'none'
4698
+ ? h('div', { className: 'mem-scope-note' }, '无限制:范围选择会保存,但暂不参与读取限制。')
4699
+ : null,
4700
+ h('div', { className: 'mem-scope-search' },
4701
+ h('input', {
4702
+ type: 'search',
4703
+ placeholder: '搜索作用域…',
4704
+ value: search,
4705
+ 'aria-label': '搜索作用域',
4706
+ onChange: (event) => setSearch(event.target.value),
4707
+ }),
4708
+ h('button', { type: 'button', className: 'linkbtn', onClick: selectAll }, '全选'),
4709
+ h('button', { type: 'button', className: 'linkbtn', onClick: clearAll }, '取消全选'),
4710
+ ),
4711
+ loading && data === null
4461
4712
  ? h('div', { className: 'hint' }, '读取中…')
4462
4713
  : h('div', { className: 'mem-scope-list' },
4463
- scopes.length === 0
4464
- ? h('div', { className: 'hint' }, '还没有可选的范围。')
4465
- : scopes.map((item) => h('label', { className: 'mem-scope-item', key: item.scope },
4466
- h('input', {
4467
- type: 'checkbox',
4468
- checked: selected.includes(item.scope),
4469
- onChange: () => toggleScope(item.scope),
4470
- }),
4471
- h('span', { className: 'lab', title: item.scope }, item.label || item.scope),
4472
- h('span', { className: 'cnt' }, `${item.records ?? 0}`),
4714
+ visibleScopes.length === 0
4715
+ ? h('div', { className: 'hint' }, needle ? '没有匹配的作用域。' : '还没有可选的范围。')
4716
+ : visibleScopes.map((item) => h('div', { className: `mem-scope-item${entries[item.scope] ? ' on' : ''}`, key: item.scope },
4717
+ h('div', { className: 'mem-scope-row' },
4718
+ h('button', {
4719
+ type: 'button',
4720
+ role: 'checkbox',
4721
+ 'aria-checked': Boolean(entries[item.scope]),
4722
+ className: `mem-scope-check${entries[item.scope] ? ' on' : ''}`,
4723
+ onClick: () => toggleScope(item.scope),
4724
+ 'aria-label': entries[item.scope] ? '取消选择' : '选择',
4725
+ }, h('span', { className: 'box' }, entries[item.scope] ? h('svg', { width: 11, height: 11, viewBox: '0 0 12 12', 'aria-hidden': 'true' }, h('path', { d: 'M2 6.2 4.7 9 10 3.4', fill: 'none', stroke: 'currentColor', strokeWidth: 1.8, strokeLinecap: 'round', strokeLinejoin: 'round' })) : null)),
4726
+ h('button', {
4727
+ type: 'button',
4728
+ className: 'mem-scope-label',
4729
+ title: item.scope,
4730
+ onClick: () => toggleScope(item.scope),
4731
+ }, item.label || item.scope),
4732
+ h('span', { className: 'cnt' }, `${item.records ?? 0}`),
4733
+ ),
4473
4734
  )),
4474
4735
  ),
4475
4736
  error ? h('div', { className: 'mem-scope-err' }, error) : null,
4476
4737
  h('div', { className: 'mem-scope-foot' },
4477
4738
  h('button', {
4478
4739
  type: 'button',
4479
- className: 'linkbtn',
4740
+ className: 'mem-scope-reset',
4480
4741
  disabled: busy || !data || data.override === null,
4481
4742
  onClick: () => save(null),
4482
4743
  }, '恢复默认'),
4483
4744
  h('span', { className: 'spacer' }),
4484
4745
  h('button', {
4485
4746
  type: 'button',
4486
- className: 'primary',
4487
- disabled: busy || selected.length === 0,
4488
- onClick: () => save(selected),
4747
+ className: 'mem-scope-save',
4748
+ disabled: busy || selectedCount === 0,
4749
+ onClick: saveDraft,
4489
4750
  }, busy ? '保存中…' : '保存'),
4490
4751
  ),
4491
4752
  ) : null,
4492
4753
  )
4493
4754
  }
4755
+ /**
4756
+ * 普通限制下的「范围外记忆访问审批」浮层。
4757
+ *
4758
+ * 工具在 memory_search / memory_get / memory_list 命中可见域外的记录时会在
4759
+ * provider 里挂起一个 Promise,这里轮询 /memory/api/approvals 把它显示给用户。
4760
+ * 用户点「拒绝 / 允许一次」→ POST /memory/api/approvals → 工具拿到结果继续。
4761
+ *
4762
+ * 视觉沿用审计详情里的 hover 浮窗(.audit-tip / .audit-id / .audit-pill):
4763
+ * 把根节点标成 .memp,这些样式就会按同一套 token 生效。
4764
+ */
4765
+ function MemoryApprovalOverlay() {
4766
+ const [approvals, setApprovals] = React.useState([])
4767
+ const [busy, setBusy] = React.useState('')
4768
+ const [error, setError] = React.useState('')
4769
+ const [tip, setTip] = React.useState(null)
4770
+ const tipTimer = React.useRef(null)
4771
+ const hideTimer = React.useRef(null)
4772
+ const activeRef = React.useRef(true)
4773
+
4774
+ const clearTimers = () => {
4775
+ if (tipTimer.current !== null) { clearTimeout(tipTimer.current); tipTimer.current = null }
4776
+ if (hideTimer.current !== null) { clearTimeout(hideTimer.current); hideTimer.current = null }
4777
+ }
4778
+
4779
+ React.useEffect(() => {
4780
+ activeRef.current = true
4781
+ return () => {
4782
+ activeRef.current = false
4783
+ clearTimers()
4784
+ }
4785
+ }, [])
4786
+
4787
+ const refresh = () => {
4788
+ api('approvals')
4789
+ .then((value) => {
4790
+ if (!activeRef.current) return
4791
+ setApprovals(Array.isArray(value.approvals) ? value.approvals : [])
4792
+ })
4793
+ .catch(() => { /* 轮询失败静默重试 */ })
4794
+ }
4795
+
4796
+ React.useEffect(() => {
4797
+ refresh()
4798
+ if (typeof setInterval !== 'function') return undefined
4799
+ const timer = setInterval(() => {
4800
+ if (typeof document !== 'undefined' && document.hidden) return
4801
+ refresh()
4802
+ }, 1500)
4803
+ return () => clearInterval(timer)
4804
+ }, [])
4805
+
4806
+ const answer = (id, outcome) => {
4807
+ setBusy(id)
4808
+ setError('')
4809
+ api('approvals', { id, outcome })
4810
+ .then(() => {
4811
+ setApprovals((prev) => prev.filter((item) => item.id !== id))
4812
+ hideTip()
4813
+ })
4814
+ .catch((e) => setError(String((e && e.message) || e)))
4815
+ .finally(() => setBusy(''))
4816
+ }
4817
+
4818
+ const hideTip = () => { clearTimers(); setTip(null) }
4819
+ const cancelHideTip = () => {
4820
+ if (hideTimer.current !== null) { clearTimeout(hideTimer.current); hideTimer.current = null }
4821
+ }
4822
+ const scheduleHideTip = () => {
4823
+ if (tipTimer.current !== null) { clearTimeout(tipTimer.current); tipTimer.current = null }
4824
+ cancelHideTip()
4825
+ hideTimer.current = setTimeout(() => { hideTimer.current = null; setTip(null) }, 220)
4826
+ }
4827
+ const positionFor = (element) => {
4828
+ const width = typeof window !== 'undefined' ? window.innerWidth : 1200
4829
+ const height = typeof window !== 'undefined' ? window.innerHeight : 800
4830
+ const rect = element && typeof element.getBoundingClientRect === 'function' ? element.getBoundingClientRect() : null
4831
+ if (rect === null) return { x: 12, y: 12 }
4832
+ const tipWidth = Math.min(380, width - 24)
4833
+ const tipHeight = 260
4834
+ const x = Math.max(12, Math.min(rect.left, width - tipWidth - 12))
4835
+ let y = rect.bottom + 8
4836
+ if (y + tipHeight > height - 12) y = Math.max(12, rect.top - tipHeight - 8)
4837
+ return { x, y }
4838
+ }
4839
+ const fetchRecord = (id) => {
4840
+ api(`records?id=${encodeURIComponent(id)}`)
4841
+ .then((value) => {
4842
+ const record = Array.isArray(value.records) ? value.records[0] : undefined
4843
+ if (!record || !activeRef.current) return
4844
+ setTip((prev) => prev !== null && prev.item && prev.item.id === id
4845
+ ? { ...prev, item: { ...prev.item, content: record.content, tags: Array.isArray(record.tags) ? record.tags : prev.item.tags, scope: record.scope, kind: record.kind, importance: record.importance } }
4846
+ : prev)
4847
+ })
4848
+ .catch(() => { /* 悬浮详情是增强,失败就展示轮询里的摘要 */ })
4849
+ }
4850
+ const showTip = (item, element) => {
4851
+ clearTimers()
4852
+ tipTimer.current = setTimeout(() => {
4853
+ tipTimer.current = null
4854
+ setTip({ item, ...positionFor(element) })
4855
+ fetchRecord(item.id)
4856
+ }, 350)
4857
+ }
4858
+
4859
+ const pending = approvals.length > 0 ? approvals[0] : null
4860
+ const tipNode = tip === null ? null : (() => {
4861
+ const item = tip.item || {}
4862
+ return h('div', { className: 'audit-tip', style: { left: tip.x, top: tip.y }, onMouseEnter: cancelHideTip, onMouseLeave: hideTip, onClick: () => openMemoryRecordPanel(item.id, item.scope, item) },
4863
+ h('div', { className: 'audit-tip-head' }, h('span', null, '记忆'), h('span', { className: 'audit-tip-title' }, item.id || '未知')),
4864
+ h('div', { className: 'audit-tip-meta' },
4865
+ auditPill(item.kind ? kindMeta(item.kind).label : '未知', 'mute'),
4866
+ auditPill(item.scope || '', 'mute'),
4867
+ auditPill(`重要度 ${Number(item.importance ?? 0).toFixed(2)}`, 'mute'),
4868
+ ),
4869
+ item.tags && item.tags.length > 0
4870
+ ? h('div', { className: 'audit-flat' }, item.tags.map((tag) => h('span', { className: 'tag', key: tag }, tag)))
4871
+ : null,
4872
+ h('div', { className: 'audit-tip-content' }, String(item.content || '')),
4873
+ h('div', { className: 'audit-tip-foot' }, '点击跳转到「记录」页并展开'),
4874
+ )
4875
+ })()
4876
+
4877
+ if (pending === null) return null
4878
+
4879
+ return h('div', { className: 'memp mem-approval' },
4880
+ h('div', { className: 'mem-approval-card', role: 'dialog', 'aria-label': '记忆访问审批' },
4881
+ h('div', { className: 'mem-approval-head' },
4882
+ h('span', { className: 'mem-approval-dot' }),
4883
+ h('b', null, '记忆访问审批'),
4884
+ h('span', { className: 'muted' }, pending.tool || 'memory'),
4885
+ h('span', { className: 'spacer' }),
4886
+ approvals.length > 1 ? h('span', { className: 'muted' }, `还有 ${approvals.length - 1} 个待审批`) : null,
4887
+ ),
4888
+ h('div', { className: 'mem-approval-query' }, `Agent 查询:${pending.query || '(未提供)'}`),
4889
+ h('div', { className: 'hint' }, '以下记忆不在本会话的可见域内。允许一次只对本次调用生效,拒绝则工具拿不到这些内容。'),
4890
+ h('div', { className: 'mem-approval-list' },
4891
+ pending.items.map((item) => h('div', { className: 'mem-approval-item', key: item.id },
4892
+ h('div', { className: 'mem-approval-row' },
4893
+ h('button', {
4894
+ type: 'button',
4895
+ className: 'audit-id',
4896
+ 'aria-label': item.id,
4897
+ onMouseEnter: (event) => showTip(item, event.currentTarget),
4898
+ onMouseLeave: scheduleHideTip,
4899
+ onClick: () => openMemoryRecordPanel(item.id, item.scope, item),
4900
+ }, String(item.id).length > 12 ? `${item.id.slice(0, 8)}…${item.id.slice(-4)}` : item.id),
4901
+ h('span', { className: 'audit-pill mute' }, item.scope),
4902
+ h('span', { className: 'audit-pill mute' }, item.kind ? kindMeta(item.kind).label : '未知'),
4903
+ item.reason === 'kind'
4904
+ ? h('span', { className: 'audit-pill warn' }, '类型受限')
4905
+ : h('span', { className: 'audit-pill warn' }, '范围外'),
4906
+ ),
4907
+ h('div', { className: 'mem-approval-summary' }, String(item.content || '').replace(/\s+/g, ' ').slice(0, 180)),
4908
+ )),
4909
+ ),
4910
+ error ? h('div', { className: 'mem-scope-err' }, error) : null,
4911
+ h('div', { className: 'mem-approval-foot' },
4912
+ h('button', {
4913
+ type: 'button',
4914
+ className: 'mem-scope-reset',
4915
+ disabled: busy === pending.id,
4916
+ onClick: () => answer(pending.id, 'rejected'),
4917
+ }, '拒绝'),
4918
+ h('span', { className: 'spacer' }),
4919
+ h('button', {
4920
+ type: 'button',
4921
+ className: 'mem-scope-save',
4922
+ disabled: busy === pending.id,
4923
+ onClick: () => answer(pending.id, 'allowed-once'),
4924
+ }, busy === pending.id ? '处理中…' : '允许一次'),
4925
+ ),
4926
+ ),
4927
+ tipNode,
4928
+ )
4929
+ }
4494
4930
 
4495
4931
  function apply(ctx) {
4496
4932
  try {
@@ -4634,6 +5070,16 @@ window.__ModuleLoader__.load({
4634
5070
  } catch (error) {
4635
5071
  console.warn('[dsh-memory] 轨迹详情记忆页签注册失败:', error?.message)
4636
5072
  }
5073
+ // 普通限制下的范围外访问审批浮层:全局 fixed,自己轮询待审批。
5074
+ try {
5075
+ slots.inject('shell.overlay', () => slots.register({
5076
+ name: 'shell.overlay',
5077
+ id: 'memory-access-approval',
5078
+ order: 62,
5079
+ }, () => React.createElement(MemoryApprovalOverlay)))
5080
+ } catch (error) {
5081
+ console.warn('[dsh-memory] 记忆访问审批浮层注册失败:', error?.message)
5082
+ }
4637
5083
  } catch (error) {
4638
5084
  console.error('[dsh-memory] client apply 失败(已吞掉):', error?.message)
4639
5085
  }
@@ -4752,6 +5198,10 @@ window.__ModuleLoader__.load({
4752
5198
  SettingsTab: SettingsTab,
4753
5199
  AuditTab: AuditTab,
4754
5200
  MemoryVisibilityButton: MemoryVisibilityButton,
5201
+ MemoryApprovalOverlay: MemoryApprovalOverlay,
5202
+ openMemoryRecordPanel: openMemoryRecordPanel,
5203
+ requestRecordFocus: requestRecordFocus,
5204
+ takeRecordFocus: takeRecordFocus,
4755
5205
  buildMemoryTree: buildMemoryTree,
4756
5206
  KIND_META: KIND_META,
4757
5207
  CSS: CSS,