@restregular/dsh-memory 0.25.13 → 0.26.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 +15 -0
- package/README.md +12 -2
- package/lib/client.js +102 -0
- package/lib/inject/policy.js +2 -0
- package/lib/panel.js +7 -1
- package/lib/provider/apply.js +5 -0
- package/lib/provider/grants.js +15 -5
- package/lib/provider/index.js +49 -1
- package/lib/provider/scope.js +61 -3
- package/lib/tools/index.js +32 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
本项目的版本记录。安装 id 与 npm 版本一一对应。
|
|
4
4
|
|
|
5
|
+
## 0.26.0 — 作用域参数接受 `custom:` 展示前缀:自动归一,不再写「幽灵授权」
|
|
6
|
+
|
|
7
|
+
- **问题(真机复现)**:0.25.10 / 0.25.11 把所有工具输出、注入摘要与面板统一改成展示名 `<类型>:<名字>` 后,自定义作用域在 Agent 眼里只以 `custom:dsh-plugin-dev` 出现;但参数层只放行 `project:` / `session:` 两种前缀,`custom:` 属于键空间之外。于是 `memory_scopes request({ scopes: ['custom:dsh-plugin-dev'] })` 被 `canonicalScope` 原样收下、`grants.add` 只校验权限不校验键形态 —— 一条永远匹配不到记录的 grant 就这样落盘:申请结果渲染成 `custom:custom:dsh-plugin-dev → 已授予`(`scopeDisplayName` 对未知键无条件补 `custom:`),目录里又查不到(`scopeCatalog` 不含 grants),下一次 `memory_search` 重新弹审批。本机 `grants/session-91f4b9f7….json` 里同时躺着 `custom:dsh-plugin-dev` 与 `dsh-plugin-dev` 两条,相隔 35 秒。根因还包括:`memory_scopes request` 是唯一跳过 `normalizeScopeInput` 的作用域入口(其余工具都过 `resolveScope`)。
|
|
8
|
+
- **输入层归一**:`normalizeScopeInput` 新增 `custom:` 分支 —— 剥一层前缀后仍按自定义名严格校验(`custom:` / `custom:foo:bar` / `custom:global` 仍 `SCOPE_INVALID`,错误文案提示 `pass the bare scope name`)。`memory_scopes request` 现在和其他工具一样走 `resolveScope`:`custom:handbook` 与 `handbook` 合并成一条申请,`all` 与非法名在弹审批前就明确报错。
|
|
9
|
+
- **兜住旧数据**:`canonicalScope` 与 grants 解析用 `normalizeScopeKey` 把 `custom:` 折回裸键,0.25.x 写下的幽灵授权读取时自动生效,不用手工清理;`SessionGrantsStore.add` 落盘前用 `isWellFormedScopeKey` 卡形态;`scopeDefaultName` 对已带前缀的键幂等,不再渲染成 `custom:custom:...`。
|
|
10
|
+
- **讲清楚**:注入的「记忆系统」常驻段新增作用域写法说明;`scope` / `memory_scopes.scopes` 的参数说明补「自定义作用域传裸名,展示名会自动归一」;`scope name must not contain ':'` 的错误补可操作提示。README 第 4 节补同口径说明。
|
|
11
|
+
- 测试:新增 scope 归一 / 展示幂等 / grants 形态守卫 / 旧数据折回 / 工具端到端归一 / 非法名与 `all` 拒绝等 8 个用例;全套 518 tests / 517 pass / 1 skipped(`multiprocess.test.ts` 需真起子进程)。
|
|
12
|
+
|
|
13
|
+
## 0.25.14 — 侧边栏给等待记忆审批的会话显示黄点 /「待审批」
|
|
14
|
+
|
|
15
|
+
- **问题**:范围外记忆审批以前只是插件自己画在 `conversation.input.dock` 的卡片;宿主的侧边栏会话列表并不认识它,所以工具在等审批时,那一行没有系统审批 / ask 工具那样的黄点,也不会被「待审批」水线自动顶出来。宿主这两类阻塞都来自 `ctx.uiSession` 的 pending-interaction 注册表(`dsh-client-ui-approval` 发 `kind:'approval'`,`dsh-client-ui-user-questions` 发 `kind:'question'`)。
|
|
16
|
+
- **做法**:客户端新增 `installApprovalPresence`,通过 `ctx.inject(['uiSession'], …)`(等 `uiSession` 就绪,不在 apply 里一次性取样)注册一个 pending-interaction domain;轮询轻量审批摘要,为每条待审批发布一个**合成 interaction** `{ sessionId, kind:'approval', key:'memory-approval:<id>', toolName }`,回答 / 超时后撤销。它**不是** `PendingApproval` 实例,所以宿主的审批卡不会抢着渲染,我们自己的审批卡仍是唯一 UI;但侧边栏黄点、紧凑文案「待审批」、悬停「等待审批」、会话水线提升、以及其它插件对 `uiSession.sessionStatus` 的读取,都和系统审批完全同构。按审批 id 增删,同一会话多条审批互不顶掉;点「拒绝 / 允许」后立即同步一次,不必等下一次轮询。
|
|
17
|
+
- **轻量接口**:`GET /memory/api/approvals?summary=1` 新增摘要口径,只回 `id / sessionId / tool / action / mode / createdAt / expiresAt`,不复制条目正文与查询语句;provider 新增 `pendingVisibilityApprovalPresence()`。旧 provider 没有该方法时回退完整列表,客户端只读 id + sessionId,兼容不破。
|
|
18
|
+
- **测试**:客户端新增「待审批发布成 approval 类型、空 sessionId 跳过、同一会话多条、回答一条只撤一条、卸载清空」用例;面板新增 `/approvals?summary=1` 的「无正文 / 按会话过滤 / grant mode / 旧 provider 回退」用例。
|
|
19
|
+
|
|
5
20
|
## 0.25.13 — 「绑定」点一下立即保存;绑定不再往上下文注入提示
|
|
6
21
|
|
|
7
22
|
- **绑定即存**:记忆权限浮窗里每个作用域的【绑定】按钮以前只是草稿,必须再点「保存」才落盘——用户点完绑定看不出任何反馈,很容易以为没生效。现在点一下就走新接口 `POST /memory/api/bindings` 立即保存(再点一次 = 解除绑定),按钮先乐观更新、失败回滚原状态并把错误显示在浮窗里;「绑定」按钮的 `title` 与浮窗提示行都写清「点一下就保存」。绑定是工作区级设置(本工作区新会话的默认可见范围),与浮窗里「勾选范围 / 可读 / 可写」那份会话级草稿无关,所以**不需要**先设草稿;点「保存」时仍会幂等带上一次绑定,旧客户端 / 老服务端按原逻辑照旧生效。
|
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ JSONL 落盘 · 本机向量检索 · 五个模型工具 · 自动召回 · 硬
|
|
|
39
39
|
|
|
40
40
|
| 能力 | 说明 |
|
|
41
41
|
| --- | --- |
|
|
42
|
-
| **六个工具** | `memory_save` / `memory_search` / `memory_list` / `memory_get` / `memory_forget` / `memory_scopes`。`memory_search` 只回印象(8 位 id / 作用域 / 类型 / 标签 / 短摘要),正文和元数据用 `memory_get` 的 `fields` 按需取;同一会话里已出现过的条目只回 id。`description` 字段在普通限制下必填,审批卡会展示它。`memory_scopes` 只读目录:给作用域名字 / 用途 /
|
|
42
|
+
| **六个工具** | `memory_save` / `memory_search` / `memory_list` / `memory_get` / `memory_forget` / `memory_scopes`。`memory_search` 只回印象(8 位 id / 作用域 / 类型 / 标签 / 短摘要),正文和元数据用 `memory_get` 的 `fields` 按需取;同一会话里已出现过的条目只回 id。`description` 字段在普通限制下必填,审批卡会展示它。`memory_scopes` 只读目录:给作用域名字 / 用途 / 当前可读可写状态,不返回任何正文。**作用域参数传裸名**(`handbook`):0.26.0 起工具输出里的展示名 `custom:handbook` 粘进参数会被自动归一,`project:` / `session:` 本来就直接可用 |
|
|
43
43
|
| **四种作用域** | 会话 / 项目 / `global` / 自定义名。默认写进当前会话,读默认合并会话 + 项目 + global;项目作用域可起别名。自定义作用域**首次由 Agent 创建时必须带 `scopeDescription`**(一句话用途),可选带 `scopeRule` 写一条初始规则;元数据存在 `<storeDir>/scope-meta/`,删光记录也不会丢 |
|
|
44
44
|
| **作用域目录与绝对隔离** | `memory_scopes` 列出范围内 / 范围外的名字与用途;normal / none 下还带**初始化记录正文**(每个作用域最多 3 条,方便 Agent 直接读到“这个作用域主要放什么”)。绝对限制下范围外作用域**仍可发现**,但正文(含初始记录)永不可读、不可写、不可申请(没有任何审批放行路径);普通限制下范围外条目可申请,`memory_scopes request` 走审批后写入**本会话**授权(默认只读,写权限必须单独申请),子 Agent 沿父会话继承 |
|
|
45
45
|
| **会话授权(grants)** | Agent 申请、用户批准的例外单独存在 `<storeDir>/grants/<session>.json`,不并入可见域策略;只读 / 可写独立、可撤销、只在本会话(及子 Agent)生效;`absolute` 下授权不生效,作用域被删除重建后按创建代次自动失效 |
|
|
@@ -49,7 +49,7 @@ JSONL 落盘 · 本机向量检索 · 五个模型工具 · 自动召回 · 硬
|
|
|
49
49
|
| **硬性规则** | `kind: rule` 进系统提示常驻段;行首带 8 位 id,注入的是摘要,需要全文时用 `memory_get`;旧会话会收到新增 / 编辑提醒;超预算有明确警告,不静默丢弃 |
|
|
50
50
|
| **记忆权限** | 输入框旁的「记忆权限」按钮设置本会话可读的作用域 + 限制等级(绝对 / 普通 / 无限制);每个作用域一行就是一条设置:**勾选**表示本会话可访问,行内**【绑定】/【可读】/【可写】**控制工作区默认可见与读写权限——**【绑定】点一下立即保存**(0.25.13 起),绑定到当前工作区后新会话默认可见,可读 / 可写独立影响工具读写;Agent 通过 `memory_scopes request` 申请到的范围外授权**也用勾选呈现**(名称右边挂一个「AI」气泡,样式与你自己勾选的行区分开),取消勾选只是草稿,**点「保存」才真正撤销**(0.25.4 起),且按钮数字 / 「已选」都把 AI 授权算进去(0.25.5 起)并**实时轮询**——浮窗关着,Agent 刚拿到的授权也会让按钮数字自己变(0.25.6 起);**整张作用域卡片都能点选**,点名称 / 计数 / 留白等同点勾选框(0.25.7 起),本会话那一行直接叫「**session:当前会话**」(0.25.8 起,0.25.10 起带类型前缀);**所有作用域名都以 `global:` / `project:` / `session:` / `custom:` 开头**(0.25.10 起);浮窗挂在页面根层,不会被输入框上方的浮动按钮盖住(0.25.8 起);Agent 及其子 Agent 都按它检索、注入与 `memory_get` |
|
|
51
51
|
| **修正版编辑** | 面板里编辑一条记忆 = 新建一条修正版并通过 `supersedes` 指向旧记录,旧记录不原地修改;记录页「是否被修正」筛选默认隐藏旧版本,可切到全部 / 只看已修正 |
|
|
52
|
-
| **范围外审批** | 普通限制下,工具命中可见域外的记忆时会在**对应会话的输入框上方**出现审批卡(样式与系统权限审批一致):展示工具、Agent 说明、查询关键词与范围外记忆摘要,支持逐条勾选 / 全选 / 取消全选(「允许选中的 N 条」或整单「允许一次」)或「拒绝」;点记忆 ID
|
|
52
|
+
| **范围外审批** | 普通限制下,工具命中可见域外的记忆时会在**对应会话的输入框上方**出现审批卡(样式与系统权限审批一致):展示工具、Agent 说明、查询关键词与范围外记忆摘要,支持逐条勾选 / 全选 / 取消全选(「允许选中的 N 条」或整单「允许一次」)或「拒绝」;点记忆 ID 直接跳到面板「记录」页并展开该条。**0.25.14 起**审批等待期间还会像系统审批 / ask 工具一样,在该会话的侧边栏行显示黄色圆点与「待审批」(含后台会话;回答或超时后撤销),其它插件也能从宿主 `uiSession.sessionStatus` 读到这条阻塞 |
|
|
53
53
|
| **上下文去重** | `memory_search` / `memory_list` 返回过的条目记进会话上下文状态;同一版本再次命中只回记忆 ID(`memory_search` 的裸 ID 与带摘要条目之间空一行),并提示用 `memory_get` 取全文(内容更新后重新给摘要) |
|
|
54
54
|
| **状态记忆** | `kind: state` 默认 7 天 TTL;注入带「状态,使用前验证」;过期后默认不进检索 / 摘要 / 召回,但 `memory_get` 与面板仍可取 |
|
|
55
55
|
| **流程索引** | `kind: procedure` 的目录进系统提示:按当前工作区 + 触发词(工具名 / 关键词)列 `id · 触发词 → 标题`,需要时再用 `memory_get` 取全文 |
|
|
@@ -93,6 +93,7 @@ JSONL 落盘 · 本机向量检索 · 五个模型工具 · 自动召回 · 硬
|
|
|
93
93
|
- **默认范围**:没有显式设置时(新会话)按默认设置生效 —— **当前会话 + 当前项目 + global + 已绑定的作用域** + **普通限制**;面板按同一份默认范围预选,不会出现空面板。
|
|
94
94
|
- **本会话那一行的名字**(0.25.8):本会话 `session:<id>` 那一行固定显示「**当前会话**」(0.25.10 起叫 `session:当前会话`,原始 scope 键仍在 `title` 里),别的会话的 scope 仍按标题 / 短 id 显示;搜索也按显示名匹配,输入「当前会话」即可筛到这一行。
|
|
95
95
|
- **作用域名字带类型前缀**(0.25.10):所有展示名都以 `global:` / `project:` / `session:` / `custom:` 开头 —— `global:全局`、`project:D:\repositories\dsh-memory`、`session:session-1a2b3c4d`、`custom:handbook`。**global 与自定义作用域最容易被漏掉**:它们的 scope 键本身没有前缀(`global` 是个关键字、自定义就是裸名字),前缀必须按类型补。项目 / 自定义作用域有别名时是 `project:<别名>` / `custom:<别名>`,会话有标题时是 `session:<标题>(session-1a2b3c4d)`。
|
|
96
|
+
- **参数仍传裸名**(0.26.0):展示名只用于显示。Agent 把 `custom:handbook` 直接粘进作用域参数时会被**自动归一**成 `handbook`(不再是另一条永远匹配不到记录的授权);`custom:` 后面仍必须是合法自定义名,`custom:` / `custom:foo:bar` / `custom:global` 都会被 `SCOPE_INVALID` 拒绝。旧 grants 文件里遗留的 `custom:` 键在读取时也会折回裸键。
|
|
96
97
|
- **会话作用域的短 id**(0.25.9):会话 id 取**前 16 位**(`session-` + 8 位 uuid)。以前取前 8 位,而真机会话 id 恰好以 `session-` 开头(正好 8 个字符),结果所有会话作用域都显示成同一个名字——等于没给 id。
|
|
97
98
|
- **浮窗里能直接看到真实键**(0.25.11):显示名与真实键不一致时(项目别名 → 完整路径、会话短 id → 完整 uuid、自定义名 → 裸键),每行名称下面多一行等宽的完整键;**不做手工截断**,放不下就按容器宽度用 CSS 省略号收尾,窗口宽就多显示几个字符。键行是等宽小字、和勾选框左对齐。记录卡 / 记忆树 / 审批卡的作用域徽标都在 `title` 里给出「显示名 → 真实键」。
|
|
98
99
|
- **搜索与批量**:顶部可搜索作用域,支持「全选 / 取消全选」;列表最大高度受限,作用域多时在浮窗内滚动。**整张作用域卡片都能点**(0.25.7 起)——点名称、记录数或行内留白等同点勾选框,只有行内的按钮(勾选框 / 绑定 / 可读 / 可写)各管各的,点它们不会再多切换一次。
|
|
@@ -243,6 +244,13 @@ memory_forget({ "id": "3f2a9c1d", "description": "删除过期的临时结论" }
|
|
|
243
244
|
| `session:<id>` / `project:<path>` | 显式钉住某个会话 / 工作区 |
|
|
244
245
|
| `all` / `*` | **只在读取时可用**:跨所有已发现作用域 |
|
|
245
246
|
|
|
247
|
+
**参数写法与展示名(0.26.0)**:自定义作用域传**裸名**(`handbook`)。工具输出 / 注入摘要 / 面板里看到的
|
|
248
|
+
`custom:handbook` 是展示名——直接粘进参数也会被自动归一(剥一层 `custom:`),不会再写成一条匹配不到记录的
|
|
249
|
+
「幽灵授权」;归一后剩下的部分仍按自定义名严格校验(`custom:foo:bar`、`custom:` 都是 `SCOPE_INVALID`,
|
|
250
|
+
`custom:global` 这种包保留名的写法同样拒绝)。`project:` / `session:` 本来就既是展示名也是参数键。
|
|
251
|
+
`memory_scopes` 目录行形如 `custom:handbook(handbook)`,括号里就是该传的键;旧 grants 文件里遗留的
|
|
252
|
+
`custom:` 键在读取时也会自动折回裸键,不需要手工清理。
|
|
253
|
+
|
|
246
254
|
项目作用域的身份永远是 `project:<绝对路径>`,别名只影响显示与引用,不迁移数据。
|
|
247
255
|
同一路径重复用同一个名字是幂等的;换名字会覆盖原别名;另一个路径抢同一个名字会被拒绝
|
|
248
256
|
(`SCOPE_NAME_TAKEN`)。
|
|
@@ -456,6 +464,8 @@ retrieval:
|
|
|
456
464
|
scope 键有三种形态:`global`、`project:<cwd>`、`session:<会话 id>`,其余是自定义名
|
|
457
465
|
(小写折叠、内部空白折成一个空格、最长 64 字符,禁止 `:`、路径分隔符、控制字符、`..`
|
|
458
466
|
与保留名 `global/project/session/all`)。**没有任何数据迁移**:旧的 `project:<cwd>` 文件继续原样读写。
|
|
467
|
+
展示用的 `custom:<名字>` 不是键:0.26.0 起参数里粘进来会被自动剥掉一层前缀(`custom:` 后面仍是
|
|
468
|
+
合法自定义名才行),旧的 grants / 可见范围里遗留的 `custom:` 键读取时也折回裸键。
|
|
459
469
|
|
|
460
470
|
`installRoot` 默认 `~/.dsh/memory-vector`;显式配了 `storeDir` 时取它的父目录。
|
|
461
471
|
|
package/lib/client.js
CHANGED
|
@@ -5308,6 +5308,100 @@ window.__ModuleLoader__.load({
|
|
|
5308
5308
|
: (createPortal !== null && portalTarget !== null ? createPortal(popover, portalTarget) : popover),
|
|
5309
5309
|
)
|
|
5310
5310
|
}
|
|
5311
|
+
/** 0.25.14:全局待审批快照的轮询周期。与审批卡同频,避免黄点比卡片晚太多。 */
|
|
5312
|
+
const APPROVAL_PRESENCE_POLL_MS = 1500
|
|
5313
|
+
|
|
5314
|
+
/**
|
|
5315
|
+
* 0.25.14:把「本会话正在等待记忆审批」传播成宿主的 pending interaction。
|
|
5316
|
+
*
|
|
5317
|
+
* 为什么需要它:`MemoryApprovalDock` 只是我们自己画的卡片(conversation.input.dock),
|
|
5318
|
+
* 宿主的侧边栏会话列表并不认识它 —— 黄点 /「待审批」/ 自动把会话提到水线以上,
|
|
5319
|
+
* 全部来自 `ctx.uiSession` 的 pending interaction 注册表。宿主自己的
|
|
5320
|
+
* `dsh-client-ui-approval` 和 `dsh-client-ui-user-questions` 就是通过
|
|
5321
|
+
* `registerPendingInteraction` 发布 `kind: 'approval' | 'question'` 的;
|
|
5322
|
+
* 我们照抄同一条通道,发布一个**合成 interaction**(kind='approval'):
|
|
5323
|
+
* - 侧边栏拿到 warning 状态 → 左上角黄点、紧凑文案「待审批」、悬停「等待审批」;
|
|
5324
|
+
* - 其它插件(包括宿主会话本体)也能从 `uiSession.sessionStatus` 读到这个阻塞,
|
|
5325
|
+
* 和系统审批 / ask 工具完全同构;
|
|
5326
|
+
* - 它**不是** `PendingApproval` 实例,所以宿主的审批卡片不会抢着渲染它,
|
|
5327
|
+
* 我们自己的 dock 仍然是唯一可见的审批 UI。
|
|
5328
|
+
*
|
|
5329
|
+
* 发布按审批 id 增删(一个会话可能同时有多条),回答 / 超时后由下一次轮询移除。
|
|
5330
|
+
* `summary=1` 只拿 id + sessionId,不把正文搬来搬去。
|
|
5331
|
+
*/
|
|
5332
|
+
let approvalPresenceSync = null
|
|
5333
|
+
|
|
5334
|
+
function installApprovalPresence(ctx) {
|
|
5335
|
+
if (ctx === null || typeof ctx !== 'object' || typeof ctx.inject !== 'function') return
|
|
5336
|
+
try {
|
|
5337
|
+
// 不在 apply 里一次性 ctx.get('uiSession'):那会在服务尚未 provide 时拿到
|
|
5338
|
+
// undefined 且永不重试(插件冷启动丢注册的老坑)。inject 等就绪后再挂。
|
|
5339
|
+
ctx.inject(['uiSession'], (scope) => {
|
|
5340
|
+
const uiSession = scope.uiSession
|
|
5341
|
+
if (uiSession === null || typeof uiSession !== 'object'
|
|
5342
|
+
|| typeof uiSession.registerPendingInteraction !== 'function') return
|
|
5343
|
+
const publish = uiSession.registerPendingInteraction(() => 0)
|
|
5344
|
+
if (typeof publish !== 'function') return
|
|
5345
|
+
|
|
5346
|
+
// approval id → 撤销发布函数。按 id 增删,避免同一会话的多条审批互相顶掉。
|
|
5347
|
+
const live = new Map()
|
|
5348
|
+
let active = true
|
|
5349
|
+
|
|
5350
|
+
const sync = async () => {
|
|
5351
|
+
let value
|
|
5352
|
+
try { value = await api('approvals?summary=1') } catch { return }
|
|
5353
|
+
if (!active) return
|
|
5354
|
+
const list = value && Array.isArray(value.approvals) ? value.approvals : []
|
|
5355
|
+
const seen = new Set()
|
|
5356
|
+
for (const item of list) {
|
|
5357
|
+
if (item === null || typeof item !== 'object') continue
|
|
5358
|
+
const id = typeof item.id === 'string' ? item.id : ''
|
|
5359
|
+
const sessionId = typeof item.sessionId === 'string' ? item.sessionId : ''
|
|
5360
|
+
if (id === '' || sessionId === '' || seen.has(id)) continue
|
|
5361
|
+
seen.add(id)
|
|
5362
|
+
if (live.has(id)) continue
|
|
5363
|
+
try {
|
|
5364
|
+
const remove = publish({
|
|
5365
|
+
sessionId,
|
|
5366
|
+
kind: 'approval',
|
|
5367
|
+
key: `memory-approval:${id}`,
|
|
5368
|
+
toolName: typeof item.tool === 'string' && item.tool !== '' ? item.tool : 'memory',
|
|
5369
|
+
}, () => {})
|
|
5370
|
+
if (typeof remove === 'function') live.set(id, remove)
|
|
5371
|
+
} catch { /* 单条发布失败不影响其它会话的提醒 */ }
|
|
5372
|
+
}
|
|
5373
|
+
for (const [id, remove] of [...live]) {
|
|
5374
|
+
if (seen.has(id)) continue
|
|
5375
|
+
live.delete(id)
|
|
5376
|
+
try { remove() } catch { /* 组件/服务已释放 */ }
|
|
5377
|
+
}
|
|
5378
|
+
}
|
|
5379
|
+
|
|
5380
|
+
approvalPresenceSync = () => { void sync() }
|
|
5381
|
+
void sync()
|
|
5382
|
+
let timer
|
|
5383
|
+
if (typeof setInterval === 'function') {
|
|
5384
|
+
timer = setInterval(() => {
|
|
5385
|
+
if (typeof document !== 'undefined' && document.hidden) return
|
|
5386
|
+
void sync()
|
|
5387
|
+
}, APPROVAL_PRESENCE_POLL_MS)
|
|
5388
|
+
// Node 测试环境:不因为一个轮询句柄让进程永不退出;浏览器没有 unref。
|
|
5389
|
+
if (timer && typeof timer.unref === 'function') timer.unref()
|
|
5390
|
+
}
|
|
5391
|
+
const stop = () => {
|
|
5392
|
+
if (!active) return
|
|
5393
|
+
active = false
|
|
5394
|
+
if (timer !== undefined) clearInterval(timer)
|
|
5395
|
+
timer = undefined
|
|
5396
|
+
approvalPresenceSync = null
|
|
5397
|
+
for (const remove of live.values()) { try { remove() } catch { /* 已释放 */ } }
|
|
5398
|
+
live.clear()
|
|
5399
|
+
}
|
|
5400
|
+
try { scope.effect(() => stop, 'dsh-memory: approval presence') } catch { /* 旧宿主没有 effect 时随服务释放 */ }
|
|
5401
|
+
})
|
|
5402
|
+
} catch { /* 旧宿主没有 ctx.inject:少一个侧边栏提醒,绝不让 client apply 失败 */ }
|
|
5403
|
+
}
|
|
5404
|
+
|
|
5311
5405
|
/**
|
|
5312
5406
|
* 普通限制下的「范围外记忆访问审批」卡片(0.21.0)。
|
|
5313
5407
|
*
|
|
@@ -5439,6 +5533,8 @@ window.__ModuleLoader__.load({
|
|
|
5439
5533
|
delete next[pending.id]
|
|
5440
5534
|
return next
|
|
5441
5535
|
})
|
|
5536
|
+
// 立刻撤销侧边栏黄点,不必等下一次 1.5s 轮询。
|
|
5537
|
+
if (approvalPresenceSync !== null) approvalPresenceSync()
|
|
5442
5538
|
})
|
|
5443
5539
|
.catch((e) => setError(String((e && e.message) || e)))
|
|
5444
5540
|
.finally(() => setBusy(''))
|
|
@@ -5620,6 +5716,9 @@ window.__ModuleLoader__.load({
|
|
|
5620
5716
|
const stopNavWatch = wrapSettingsNav()
|
|
5621
5717
|
ctx.effect(() => () => { styleEl.remove(); stopNavWatch() }, 'dsh-memory: styles + nav icon')
|
|
5622
5718
|
|
|
5719
|
+
// 0.25.14:侧边栏黄点提示(独立于 slots;旧宿主没有 uiSession 时静默跳过)。
|
|
5720
|
+
installApprovalPresence(ctx)
|
|
5721
|
+
|
|
5623
5722
|
const slots = ctx.get('slots')
|
|
5624
5723
|
if (slots === undefined) return
|
|
5625
5724
|
clientCtx = ctx
|
|
@@ -5883,6 +5982,9 @@ window.__ModuleLoader__.load({
|
|
|
5883
5982
|
AuditTab: AuditTab,
|
|
5884
5983
|
MemoryVisibilityButton: MemoryVisibilityButton,
|
|
5885
5984
|
MemoryApprovalDock: MemoryApprovalDock,
|
|
5985
|
+
// 0.25.14:侧边栏阻塞提示的发布器(测试直接驱动,不等真实 1.5s 定时器)。
|
|
5986
|
+
installApprovalPresence: installApprovalPresence,
|
|
5987
|
+
approvalPresenceSync: () => approvalPresenceSync,
|
|
5886
5988
|
openMemoryRecordPanel: openMemoryRecordPanel,
|
|
5887
5989
|
requestRecordFocus: requestRecordFocus,
|
|
5888
5990
|
takeRecordFocus: takeRecordFocus,
|
package/lib/inject/policy.js
CHANGED
|
@@ -12,6 +12,8 @@ memory_search 只返回印象(ID、作用域、类型、标签和少量摘要
|
|
|
12
12
|
|
|
13
13
|
需要长期使用范围外作用域时,用 memory_scopes 的 action="request" 申请本会话权限:默认只读,写权限必须在 permissions 里显式列出;审批卡默认只勾只读。批准后本会话(及子 Agent)可直接读写该作用域,不必每次审批;一次性访问仍直接调用 memory_search / memory_get。
|
|
14
14
|
|
|
15
|
+
作用域参数怎么写:自定义作用域用**裸名**(dsh-plugin-dev)。工具输出、注入摘要与面板里看到的是展示名——custom:dsh-plugin-dev / project:<路径> / session:<id>;其中 project: 与 session: 可以直接当参数,custom: 前缀也会被自动归一成裸名,不必自己加,更不要叠成 custom:custom:...。memory_scopes 目录行形如 custom:handbook(handbook),括号里就是该传的参数。传 all 只能读,不能用于申请权限。
|
|
16
|
+
|
|
15
17
|
新建自定义作用域时,memory_save 必须带 scopeDescription(一句话用途);可选带 scopeRule 写一条初始规则。已有作用域带上 scopeDescription 表示更新用途,不会写初始记忆。
|
|
16
18
|
|
|
17
19
|
同一会话里已经出现过的记忆,memory_search / memory_list 只回 ID 并注明“摘要因上下文去重被省略”;那不是没找到,需要正文时直接用 memory_get 取。
|
package/lib/panel.js
CHANGED
|
@@ -816,12 +816,18 @@ async function handle(ctx, routePath, req, res) {
|
|
|
816
816
|
}
|
|
817
817
|
|
|
818
818
|
// 普通限制下的范围外访问审批:客户端浮层轮询待审批,用户点「拒绝 / 允许一次」后回写。
|
|
819
|
+
// `summary=1` 是 0.25.14 新增的轻量口径:侧边栏阻塞提醒只需要 id + sessionId,
|
|
820
|
+
// 不带条目正文,避免全局 1.5s 轮询把审批内容一遍遍传一遍。
|
|
819
821
|
if (method === 'GET' && path === '/approvals') {
|
|
820
822
|
if (typeof memory.pendingVisibilityApprovals !== 'function') {
|
|
821
823
|
throw new ApiError(501, 'NOT_SUPPORTED', 'provider 未提供可见域审批接口(请升级 @restregular/dsh-memory)')
|
|
822
824
|
}
|
|
823
825
|
const sessionId = (url.searchParams.get('session') ?? '').trim()
|
|
824
|
-
|
|
826
|
+
const summary = url.searchParams.get('summary') === '1'
|
|
827
|
+
const approvals = summary && typeof memory.pendingVisibilityApprovalPresence === 'function'
|
|
828
|
+
? memory.pendingVisibilityApprovalPresence(sessionId === '' ? undefined : sessionId)
|
|
829
|
+
: memory.pendingVisibilityApprovals(sessionId === '' ? undefined : sessionId)
|
|
830
|
+
writeJson(res, { ok: true, value: { approvals } })
|
|
825
831
|
return
|
|
826
832
|
}
|
|
827
833
|
if (method === 'POST' && path === '/approvals') {
|
package/lib/provider/apply.js
CHANGED
|
@@ -383,6 +383,11 @@ export class CordisMemoryService extends Service {
|
|
|
383
383
|
return this.impl.pendingVisibilityApprovals(sessionId)
|
|
384
384
|
}
|
|
385
385
|
|
|
386
|
+
/** 侧边栏阻塞提醒:待审批的轻量快照(不带条目正文)。 */
|
|
387
|
+
pendingVisibilityApprovalPresence(sessionId ) {
|
|
388
|
+
return this.impl.pendingVisibilityApprovalPresence(sessionId)
|
|
389
|
+
}
|
|
390
|
+
|
|
386
391
|
/** 面板:回答一条待审批(可带逐条勾选的 allowedIds)。 */
|
|
387
392
|
answerVisibilityApproval(id , outcome , allowedIds ) {
|
|
388
393
|
return this.impl.answerVisibilityApproval(id, outcome, allowedIds)
|
package/lib/provider/grants.js
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
20
20
|
import { createHash, randomUUID } from 'node:crypto'
|
|
21
21
|
import { join } from 'node:path'
|
|
22
|
+
import { isWellFormedScopeKey, normalizeScopeKey } from './scope.js'
|
|
22
23
|
|
|
23
24
|
export const GRANTS_VERSION = 1
|
|
24
25
|
|
|
@@ -70,9 +71,12 @@ function cleanPermissions(raw ) {
|
|
|
70
71
|
function parseGrant(raw , sessionId ) {
|
|
71
72
|
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return undefined
|
|
72
73
|
const value = raw
|
|
73
|
-
const
|
|
74
|
+
const rawScope = asString(value.scope)
|
|
74
75
|
const id = asString(value.id)
|
|
75
|
-
if (
|
|
76
|
+
if (rawScope === undefined || id === undefined) return undefined
|
|
77
|
+
// 0.26.0:旧数据里可能有展示形式 `custom:<名字>`(0.25.x 的 request 未规范化),
|
|
78
|
+
// 读的时候折回真实键,否则这条授权永远匹配不到记录。
|
|
79
|
+
const scope = normalizeScopeKey(rawScope)
|
|
76
80
|
const permissions = cleanPermissions(value.permissions)
|
|
77
81
|
if (permissions.length === 0) return undefined
|
|
78
82
|
return {
|
|
@@ -167,9 +171,15 @@ export class SessionGrantsStore {
|
|
|
167
171
|
add(sessionId , input ) {
|
|
168
172
|
const permissions = cleanPermissions(input.permissions)
|
|
169
173
|
if (permissions.length === 0) throw new Error('grant requires at least one permission')
|
|
174
|
+
// 0.26.0:落盘前把形态卡死。这一层是最后的护栏 —— 带展示前缀 / 带非法字符的
|
|
175
|
+
// 键写进 grants 只会变成"看起来授权成功、检索时又弹审批"的幽灵授权。
|
|
176
|
+
const scope = normalizeScopeKey(input.scope)
|
|
177
|
+
if (!isWellFormedScopeKey(scope)) {
|
|
178
|
+
throw new Error(`grant scope key is malformed: ${JSON.stringify(input.scope)}`)
|
|
179
|
+
}
|
|
170
180
|
const all = this.readAll(sessionId)
|
|
171
181
|
const now = Date.now()
|
|
172
|
-
const existing = all.grants.find((grant) => grant.scope ===
|
|
182
|
+
const existing = all.grants.find((grant) => grant.scope === scope
|
|
173
183
|
&& grant.revokedAt === undefined
|
|
174
184
|
&& (grant.expiresAt === undefined || grant.expiresAt > now)
|
|
175
185
|
&& (grant.scopeGeneration === input.scopeGeneration))
|
|
@@ -183,13 +193,13 @@ export class SessionGrantsStore {
|
|
|
183
193
|
}
|
|
184
194
|
// 同作用域但代次不同:旧代次的 grant 明确作废,避免“同名重建复活旧授权”。
|
|
185
195
|
for (const grant of all.grants) {
|
|
186
|
-
if (grant.scope ===
|
|
196
|
+
if (grant.scope === scope && grant.revokedAt === undefined && grant.scopeGeneration !== input.scopeGeneration) {
|
|
187
197
|
grant.revokedAt = now
|
|
188
198
|
}
|
|
189
199
|
}
|
|
190
200
|
const grant = {
|
|
191
201
|
id: randomUUID(),
|
|
192
|
-
scope
|
|
202
|
+
scope,
|
|
193
203
|
permissions,
|
|
194
204
|
duration: 'session',
|
|
195
205
|
sessionId,
|
package/lib/provider/index.js
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
canonicalWorkspacePath,
|
|
20
20
|
isCustomScope,
|
|
21
21
|
normalizeScopeInput,
|
|
22
|
+
normalizeScopeKey,
|
|
22
23
|
projectScopeIdentity,
|
|
23
24
|
resolveCurrentScopes,
|
|
24
25
|
scopeKind,
|
|
@@ -620,6 +621,24 @@ export function resolveConfig(config = {}) {
|
|
|
620
621
|
|
|
621
622
|
|
|
622
623
|
|
|
624
|
+
/**
|
|
625
|
+
* 侧边栏「阻塞提醒」用的轻量待审批快照(0.25.14)。
|
|
626
|
+
*
|
|
627
|
+
* 客户端要模仿宿主 approval / question 的传播方式:把「某会话正在等用户审批」
|
|
628
|
+
* 发布成宿主 `uiSession` 的 pending interaction,侧边栏才会显示黄点。这个轮询
|
|
629
|
+
* 只需要审批的身份与会话,**不需要条目正文** —— 正文仍由完整 `/approvals` 接口
|
|
630
|
+
* 按会话取,避免每 1.5 秒把几十 KB 的条目内容传到客户端。
|
|
631
|
+
*/
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
623
642
|
/** 一次审批的结局:允许(可只允许其中几条) / 拒绝 / 超时。 */
|
|
624
643
|
|
|
625
644
|
|
|
@@ -2035,6 +2054,30 @@ export class LocalMemoryService {
|
|
|
2035
2054
|
return out.sort((a, b) => a.createdAt - b.createdAt)
|
|
2036
2055
|
}
|
|
2037
2056
|
|
|
2057
|
+
/**
|
|
2058
|
+
* 侧边栏阻塞提醒用的轻量快照(0.25.14):只要审批身份 + 会话。
|
|
2059
|
+
*
|
|
2060
|
+
* 与 `pendingVisibilityApprovals` 同一份内存真值,但**不复制条目正文**:
|
|
2061
|
+
* 客户端全局轮询它来发布 `pendingInteraction`,让宿主侧边栏给等待审批的会话
|
|
2062
|
+
* 显示黄色圆点 /「待审批」;完整 payload 仍由 dock 按当前会话拉取。
|
|
2063
|
+
*/
|
|
2064
|
+
pendingVisibilityApprovalPresence(sessionId ) {
|
|
2065
|
+
const out = []
|
|
2066
|
+
for (const pending of this.pendingApprovals.values()) {
|
|
2067
|
+
if (sessionId !== undefined && sessionId !== '' && pending.sessionId !== sessionId) continue
|
|
2068
|
+
out.push({
|
|
2069
|
+
id: pending.id,
|
|
2070
|
+
sessionId: pending.sessionId,
|
|
2071
|
+
tool: pending.tool,
|
|
2072
|
+
action: pending.action,
|
|
2073
|
+
...(pending.mode !== undefined ? { mode: pending.mode } : {}),
|
|
2074
|
+
createdAt: pending.createdAt,
|
|
2075
|
+
expiresAt: pending.expiresAt,
|
|
2076
|
+
})
|
|
2077
|
+
}
|
|
2078
|
+
return out.sort((a, b) => a.createdAt - b.createdAt)
|
|
2079
|
+
}
|
|
2080
|
+
|
|
2038
2081
|
/** 面板:回答一条待审批。返回 false = 已经不存在/已超时。 */
|
|
2039
2082
|
answerVisibilityApproval(id , outcome , allowedIds ) {
|
|
2040
2083
|
const pending = this.pendingApprovals.get(id)
|
|
@@ -3166,7 +3209,8 @@ export class LocalMemoryService {
|
|
|
3166
3209
|
}
|
|
3167
3210
|
|
|
3168
3211
|
/**
|
|
3169
|
-
* 作用域键归一:自定义名可能是别名 → 转回真实键;`project:<路径>`
|
|
3212
|
+
* 作用域键归一:自定义名可能是别名 → 转回真实键;`project:<路径>` 统一路径写法;
|
|
3213
|
+
* 展示前缀 `custom:` 剥掉(0.26.0)。
|
|
3170
3214
|
*
|
|
3171
3215
|
* 项目键必须过 `canonicalWorkspacePath`:工具层的 `path`、模型显式给的
|
|
3172
3216
|
* `project:D:/x`、面板 HTTP 写入都从这里落盘,同一个目录不能因为 `/` 与 `\` 的差别
|
|
@@ -3180,6 +3224,10 @@ export class LocalMemoryService {
|
|
|
3180
3224
|
const canonical = canonicalWorkspacePath(path)
|
|
3181
3225
|
return (canonical === path ? scope : `project:${canonical}`)
|
|
3182
3226
|
}
|
|
3227
|
+
// 0.26.0:展示形式 `custom:<名字>` 与裸名是同一个作用域。旧数据(grants / visibility /
|
|
3228
|
+
// bindings)里若留下过带前缀的键,读的时候自动折回真实键,不再出现"幽灵授权"。
|
|
3229
|
+
const bare = normalizeScopeKey(scope)
|
|
3230
|
+
if (bare !== scope) return (this.names.scopeOf(bare) ) ?? (bare )
|
|
3183
3231
|
return (this.names.scopeOf(scope) ) ?? scope
|
|
3184
3232
|
}
|
|
3185
3233
|
|
package/lib/provider/scope.js
CHANGED
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
* session:<id> 一个会话(同一个 projects/<sha1>.jsonl 体系)
|
|
17
17
|
* <自定义名> 自由命名,内部键仍是自定义名本身
|
|
18
18
|
* 三者都由 `scopeFileName()` 映射到文件名,因此**自定义名里出现 ':' 一律拒绝**,否则
|
|
19
|
-
* `foo:bar` 会与保留前缀撞成同一个键空间,用户也能靠它伪造 `project:`
|
|
19
|
+
* `foo:bar` 会与保留前缀撞成同一个键空间,用户也能靠它伪造 `project:` 键。唯一的例外
|
|
20
|
+
* 是**展示前缀** `custom:`(0.26.0 起在 normalizeScopeInput 里剥掉一层再校验)——
|
|
21
|
+
* 它只会出现在工具输出 / 摘要 / 面板里,不属于键空间。
|
|
20
22
|
*/
|
|
21
23
|
import { createHash } from 'node:crypto'
|
|
22
24
|
import { posix, win32 } from 'node:path'
|
|
@@ -87,7 +89,9 @@ function normalizeCustomName(raw )
|
|
|
87
89
|
if (name === '..' || name === '.') return { ok: false, message: "scope name must not be '.' or '..'" }
|
|
88
90
|
if (/^[. ]+$/u.test(name)) return { ok: false, message: 'scope name must contain more than dots and spaces' }
|
|
89
91
|
if (name.includes('..')) return { ok: false, message: "scope name must not contain '..'" }
|
|
90
|
-
if (name.includes(':'))
|
|
92
|
+
if (name.includes(':')) {
|
|
93
|
+
return { ok: false, message: "scope name must not contain ':'; pass the bare scope name (e.g. 'dsh-plugin-dev')" }
|
|
94
|
+
}
|
|
91
95
|
if (name.includes('/') || name.includes('\\')) {
|
|
92
96
|
return { ok: false, message: 'scope name must not contain path separators' }
|
|
93
97
|
}
|
|
@@ -128,6 +132,7 @@ export function normalizeScopeName(raw )
|
|
|
128
132
|
* - 'global' | 'project' | 'session' → 各自的**种类**,不在这里解析 cwd/会话(纯函数拿不到);
|
|
129
133
|
* - 'all' | '*' → 跨所有作用域(仅读路径合法,写路径由 assertWritableScope 拒绝);
|
|
130
134
|
* - 'project:*' | 'session:*' → 原样放行(显式 pin 某个项目 / 某个会话);
|
|
135
|
+
* - 'custom:*' → 归一成裸名(0.26.0:展示层就是这么渲染自定义作用域的);
|
|
131
136
|
* - 其余 → 自定义名,严格校验。
|
|
132
137
|
*
|
|
133
138
|
* 不合法**不再静默回退**,一律返回 SCOPE_INVALID。
|
|
@@ -154,12 +159,63 @@ export function normalizeScopeInput(raw )
|
|
|
154
159
|
? { ok: true, kind: 'session', scope: prefixed.key }
|
|
155
160
|
: { ok: false, code: 'SCOPE_INVALID', message: prefixed.message }
|
|
156
161
|
}
|
|
162
|
+
// 展示形式 `custom:<名字>` 归一到裸名(0.26.0)。0.25.10 起工具输出、注入摘要、面板
|
|
163
|
+
// 全部用 `custom:<名字>` 渲染,而自定义作用域的**键本身没有前缀**;模型照抄回来时
|
|
164
|
+
// 不该落到另一个(永远匹配不到记录的)键上。这里只剥一层,剩下的部分仍按自定义名规则
|
|
165
|
+
// 严格校验 —— 所以 `custom:foo:bar` / `custom:` 依然是 SCOPE_INVALID。
|
|
166
|
+
if (lower.startsWith('custom:')) {
|
|
167
|
+
const bare = value.slice('custom:'.length).trim()
|
|
168
|
+
if (bare.length === 0) {
|
|
169
|
+
return { ok: false, code: 'SCOPE_INVALID', message: "custom: must be followed by a scope name (e.g. 'dsh-plugin-dev')" }
|
|
170
|
+
}
|
|
171
|
+
if (RESERVED_SCOPE_NAMES.has(bare.toLowerCase())) {
|
|
172
|
+
return { ok: false, code: 'SCOPE_INVALID', message: `custom: cannot wrap the reserved name "${bare.toLowerCase()}"; use the bare scope name instead` }
|
|
173
|
+
}
|
|
174
|
+
const custom = normalizeCustomName(bare)
|
|
175
|
+
return custom.ok
|
|
176
|
+
? { ok: true, kind: 'custom', scope: custom.name }
|
|
177
|
+
: { ok: false, code: 'SCOPE_INVALID', message: custom.message }
|
|
178
|
+
}
|
|
157
179
|
const custom = normalizeCustomName(value)
|
|
158
180
|
return custom.ok
|
|
159
181
|
? { ok: true, kind: 'custom', scope: custom.name }
|
|
160
182
|
: { ok: false, code: 'SCOPE_INVALID', message: custom.message }
|
|
161
183
|
}
|
|
162
184
|
|
|
185
|
+
/**
|
|
186
|
+
* 把"可能是展示形式"的作用域字符串归一到真实键(0.26.0)。
|
|
187
|
+
*
|
|
188
|
+
* 只剥**一层** `custom:`:自定义作用域的键本身没有前缀,`custom:` 是 `scopeDisplayName`
|
|
189
|
+
* 按类型补的展示前缀。用于读既有数据(grants / visibility / 面板旧键)与渲染兜底;
|
|
190
|
+
* 不做存在性检查,也不改 `project:` / `session:` 键。
|
|
191
|
+
*/
|
|
192
|
+
export function normalizeScopeKey(value ) {
|
|
193
|
+
const trimmed = value.trim()
|
|
194
|
+
if (!trimmed.toLowerCase().startsWith('custom:')) return trimmed
|
|
195
|
+
const bare = trimmed.slice('custom:'.length).trim()
|
|
196
|
+
return bare === '' ? trimmed : bare
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 形态是否是一个合法的作用域键(不做存在性检查,0.26.0)。
|
|
201
|
+
*
|
|
202
|
+
* 用途:grant 落盘前拒绝"永远匹配不到任何记录"的幽灵键 —— 0.25.x 里
|
|
203
|
+
* `memory_scopes request` 接受过 `custom:dsh-plugin-dev` 并把它原样写进 grants,
|
|
204
|
+
* 结果授权看起来成功、检索时却重新弹审批。`custom:` 展示前缀不算合法键。
|
|
205
|
+
*/
|
|
206
|
+
export function isWellFormedScopeKey(value ) {
|
|
207
|
+
if (value === 'global') return true
|
|
208
|
+
if (value.startsWith('project:')) {
|
|
209
|
+
const path = value.slice('project:'.length).trim()
|
|
210
|
+
return path !== '' && !hasControlChar(value) && codePointLength(value) <= MAX_PREFIXED_SCOPE_CHARS
|
|
211
|
+
}
|
|
212
|
+
if (value.startsWith('session:')) {
|
|
213
|
+
const id = value.slice('session:'.length).trim()
|
|
214
|
+
return id !== '' && !hasControlChar(value) && codePointLength(value) <= MAX_PREFIXED_SCOPE_CHARS
|
|
215
|
+
}
|
|
216
|
+
return normalizeCustomName(value).ok
|
|
217
|
+
}
|
|
218
|
+
|
|
163
219
|
/** scope 的种类(用于命名空间 / 阈值 / 排序);未知形态按自定义处理。 */
|
|
164
220
|
export function scopeKind(scope ) {
|
|
165
221
|
if (scope === 'global') return 'global'
|
|
@@ -218,7 +274,9 @@ export function scopeDefaultName(scope ) {
|
|
|
218
274
|
if (scope === 'global') return '全局'
|
|
219
275
|
if (scope.startsWith('project:')) return scope.slice('project:'.length)
|
|
220
276
|
if (scope.startsWith('session:')) return sessionShortId(scope.slice('session:'.length))
|
|
221
|
-
|
|
277
|
+
// 兜底:输入侧已把 `custom:<名字>` 归一,但旧数据 / 外部调用仍可能带着它进来,
|
|
278
|
+
// 展示时绝不能叠成 `custom:custom:<名字>`。
|
|
279
|
+
return normalizeScopeKey(String(scope)) || String(scope)
|
|
222
280
|
}
|
|
223
281
|
|
|
224
282
|
/**
|
package/lib/tools/index.js
CHANGED
|
@@ -258,6 +258,8 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
|
|
|
258
258
|
const SCOPE_DESCRIPTION =
|
|
259
259
|
"省略=当前会话;'global'=所有项目共享;'project'=当前工作区;其他任意名称=自定义作用域"
|
|
260
260
|
+ "(memory_save 时不存在则自动创建;由 Agent 创建时必须同时提供 scopeDescription 说明用途)。"
|
|
261
|
+
+ "自定义作用域按裸名传(如 'dsh-plugin-dev');工具输出 / 摘要里看到的展示名 "
|
|
262
|
+
+ "'custom:<名字>' 会被自动归一成裸名,不用自己加前缀。"
|
|
261
263
|
+ "读取时可用 'all' 跨所有作用域。"
|
|
262
264
|
|
|
263
265
|
export class MemoryToolError extends Error {
|
|
@@ -696,6 +698,15 @@ function readScopes(opts , service , agent
|
|
|
696
698
|
return [...new Set([fallback, 'global'])]
|
|
697
699
|
}
|
|
698
700
|
|
|
701
|
+
/**
|
|
702
|
+
* 当前会话的 scope 键(拿不到会话就 undefined)。给显式写成裸 `session` 的
|
|
703
|
+
* 请求兜底 —— `resolveScope` 在读路径上会把它解析成"合并集合",申请权限时必须
|
|
704
|
+
* 落到一个具体键。
|
|
705
|
+
*/
|
|
706
|
+
function currentSessionScope(service , exec ) {
|
|
707
|
+
return service.resolveReadScopes?.(agentOf(exec))?.find((scope) => scope.startsWith('session:'))
|
|
708
|
+
}
|
|
709
|
+
|
|
699
710
|
/** 写路径默认口径;provider 不支持时退回 project。 */
|
|
700
711
|
function writeDefault(opts ) {
|
|
701
712
|
return opts.defaultScope ?? 'session'
|
|
@@ -1420,7 +1431,7 @@ export function memoryScopesTool(opts )
|
|
|
1420
1431
|
scopes: {
|
|
1421
1432
|
type: 'array',
|
|
1422
1433
|
items: { type: 'string' },
|
|
1423
|
-
description: 'request
|
|
1434
|
+
description: 'request:要申请的作用域,最多 5 个(一次申请多个时共用同一组 permissions);自定义作用域传裸名,如 "dsh-plugin-dev"("custom:dsh-plugin-dev" 会自动归一)',
|
|
1424
1435
|
},
|
|
1425
1436
|
permissions: {
|
|
1426
1437
|
type: 'array',
|
|
@@ -1439,15 +1450,31 @@ export function memoryScopesTool(opts )
|
|
|
1439
1450
|
if (typeof service.requestScopeGrants !== 'function') {
|
|
1440
1451
|
throw new MemoryToolError('SCOPES_UNAVAILABLE', '当前 provider 不支持作用域权限申请(请升级 @restregular/dsh-memory)')
|
|
1441
1452
|
}
|
|
1442
|
-
const
|
|
1443
|
-
?
|
|
1453
|
+
const rawScopes = Array.isArray(args.scopes)
|
|
1454
|
+
? args.scopes.map((scope) => String(scope).trim()).filter((scope) => scope !== '')
|
|
1444
1455
|
: []
|
|
1445
|
-
if (
|
|
1456
|
+
if (rawScopes.length === 0) {
|
|
1446
1457
|
throw new MemoryToolError('REQUESTS_REQUIRED', 'action="request" 需要 scopes 数组(要申请的作用域键)')
|
|
1447
1458
|
}
|
|
1448
|
-
|
|
1459
|
+
// 0.26.0:作用域参数先过与其他 memory_* 相同的规范层。展示形式 `custom:<名字>`
|
|
1460
|
+
// 归一成裸名;非法名 / 'all' 在这里明确报错,而不是把 "custom:dsh-plugin-dev"
|
|
1461
|
+
// 这类键原样写进 grants —— 那种授权永远匹配不到记录,却会显示"已授予"。
|
|
1462
|
+
const normalizedScopes = []
|
|
1463
|
+
for (const raw of rawScopes) {
|
|
1464
|
+
const resolved = resolveScope(opts, service, exec, raw, 'read')
|
|
1465
|
+
if (resolved.all === true) {
|
|
1466
|
+
throw new MemoryToolError('SCOPE_NOT_WRITABLE', "scope 'all' cannot be requested; name one scope")
|
|
1467
|
+
}
|
|
1468
|
+
const scope = resolved.scope ?? currentSessionScope(service, exec)
|
|
1469
|
+
if (scope === undefined) {
|
|
1470
|
+
throw new MemoryToolError('SCOPE_INVALID', `scope "${raw}" could not be resolved; use a concrete scope name (e.g. 'dsh-plugin-dev')`)
|
|
1471
|
+
}
|
|
1472
|
+
if (!normalizedScopes.includes(scope)) normalizedScopes.push(scope)
|
|
1473
|
+
}
|
|
1474
|
+
if (normalizedScopes.length > 5) {
|
|
1449
1475
|
throw new MemoryToolError('REQUESTS_TOO_MANY', '一次最多申请 5 个作用域;请分批申请')
|
|
1450
1476
|
}
|
|
1477
|
+
const scopes = normalizedScopes
|
|
1451
1478
|
const permissions = Array.isArray(args.permissions)
|
|
1452
1479
|
? [...new Set(args.permissions.filter((permission) => permission === 'read' || permission === 'write'))]
|
|
1453
1480
|
: ['read' ]
|