@restregular/dsh-memory 0.27.0 → 0.29.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 +747 -728
- package/README.md +655 -643
- package/lib/client.js +212 -4
- package/lib/inject/apply.js +1 -1
- package/lib/inject/policy.js +3 -1
- package/lib/panel.js +1344 -1245
- package/lib/provider/apply.js +6 -1
- package/lib/provider/index.js +33 -4
- package/lib/tools/index.js +159 -33
- package/lib/tools/render.js +43 -17
- package/package.json +1 -1
package/lib/provider/apply.js
CHANGED
|
@@ -201,11 +201,16 @@ export class CordisMemoryService extends Service {
|
|
|
201
201
|
return this.impl.scopeMeta(scope)
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
/** 写入 /
|
|
204
|
+
/** 写入 / 更新 / 清空作用域用途(不创建记录)。 */
|
|
205
205
|
describeScope(scope , description , by ) {
|
|
206
206
|
return this.impl.describeScope(scope, description, by)
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
+
/** 设置 / 移除作用域别名(只影响显示与引用,不改变作用域键)。 */
|
|
210
|
+
renameScope(scope , name ) {
|
|
211
|
+
return this.impl.renameScope(scope, name)
|
|
212
|
+
}
|
|
213
|
+
|
|
209
214
|
/** 作用域目录:名字 / 用途 / 当前可读可写状态(工具 memory_scopes 与面板共用)。 */
|
|
210
215
|
scopeCatalog(agent ) {
|
|
211
216
|
return this.impl.scopeCatalog(agent)
|
package/lib/provider/index.js
CHANGED
|
@@ -3189,21 +3189,48 @@ export class LocalMemoryService {
|
|
|
3189
3189
|
}
|
|
3190
3190
|
|
|
3191
3191
|
/**
|
|
3192
|
-
* 写入 /
|
|
3193
|
-
* memory_save 的 scopeDescription
|
|
3192
|
+
* 写入 / 更新 / 清空作用域用途。**不会**创建记录,也不触发初始化;给面板、
|
|
3193
|
+
* `memory_scopes` 的 edit 动作与 memory_save 的 scopeDescription 用。
|
|
3194
|
+
*
|
|
3195
|
+
* 0.29.0:空白描述 = **清空**(保留元数据与 generation —— 删元数据会让按
|
|
3196
|
+
* generation 绑定的会话授权失效,所以清空只把 description 写成空串)。
|
|
3197
|
+
* 清空后目录按类型补默认描述,与"从没写过"的展示一致。
|
|
3194
3198
|
*/
|
|
3195
3199
|
describeScope(scope , description , by ) {
|
|
3196
3200
|
const key = this.canonicalScope(scope)
|
|
3197
3201
|
this.assertScopeAllowed(key)
|
|
3198
3202
|
this.assertCustomScopeBudget(key)
|
|
3199
3203
|
const text = typeof description === 'string' ? description.replace(/\s+/g, ' ').trim() : ''
|
|
3200
|
-
if (text === '') throw memoryError('SCOPE_DESCRIPTION_EMPTY', '作用域用途不能为空')
|
|
3201
3204
|
if (text.length > MAX_SCOPE_DESCRIPTION_CHARS) {
|
|
3202
3205
|
throw memoryError('SCOPE_DESCRIPTION_TOO_LONG', `作用域用途最多 ${MAX_SCOPE_DESCRIPTION_CHARS} 字`)
|
|
3203
3206
|
}
|
|
3204
3207
|
return this.meta.put(key, { description: text, createdBy: by, status: 'ready' })
|
|
3205
3208
|
}
|
|
3206
3209
|
|
|
3210
|
+
/**
|
|
3211
|
+
* 设置 / 移除作用域别名(0.29.0)。别名只影响**显示与引用**,不改变作用域键:
|
|
3212
|
+
* 项目作用域的身份永远是 `project:<绝对路径>`,自定义作用域永远是名字本身。
|
|
3213
|
+
*
|
|
3214
|
+
* - 非空名字 → `ScopeNames.claim`(统一校验、去重;重名报 SCOPE_NAME_TAKEN);
|
|
3215
|
+
* - 空串 / undefined → 移除别名,显示名回退到键本身(项目路径 / 自定义名);
|
|
3216
|
+
* - global 与 session 不支持别名:它们的显示名跟随类型与会话 id,改名只会让
|
|
3217
|
+
* 同一个域出现两种叫法,没有收益。
|
|
3218
|
+
*/
|
|
3219
|
+
renameScope(scope , name ) {
|
|
3220
|
+
const key = this.canonicalScope(scope)
|
|
3221
|
+
this.assertScopeAllowed(key)
|
|
3222
|
+
const kind = scopeKind(key)
|
|
3223
|
+
if (kind === 'global' || kind === 'session') {
|
|
3224
|
+
throw memoryError('SCOPE_NAME_UNSUPPORTED', 'global 与 session 作用域不支持别名:它们的显示名跟随类型与会话 id(项目 / 自定义作用域可以设别名)')
|
|
3225
|
+
}
|
|
3226
|
+
const text = typeof name === 'string' ? name.replace(/\s+/g, ' ').trim() : ''
|
|
3227
|
+
if (text === '') {
|
|
3228
|
+
this.names.release(key)
|
|
3229
|
+
return { scope: key }
|
|
3230
|
+
}
|
|
3231
|
+
return { scope: key, name: this.names.claim(key, text) }
|
|
3232
|
+
}
|
|
3233
|
+
|
|
3207
3234
|
/** 名字 → 作用域键:读路径允许直接用别名找作用域。 */
|
|
3208
3235
|
scopeKeyForName(name ) {
|
|
3209
3236
|
return this.names.scopeOf(name)
|
|
@@ -3332,7 +3359,9 @@ export class LocalMemoryService {
|
|
|
3332
3359
|
const initialRecords = policy.level === 'absolute' ? undefined : this.initialRecordsOf(scope)
|
|
3333
3360
|
// 0.27.0:scope-meta 没写用途时按作用域类型给默认描述,
|
|
3334
3361
|
// 让目录里每一条都自带「这个域是干什么的」。
|
|
3335
|
-
|
|
3362
|
+
// 0.29.0:被显式清空(描述为空串)与从没写过同样处理,都回退到默认描述。
|
|
3363
|
+
const written = typeof meta?.description === 'string' ? meta.description.trim() : ''
|
|
3364
|
+
const description = written !== '' ? written : defaultScopeDescription(scope)
|
|
3336
3365
|
out.push({
|
|
3337
3366
|
scope,
|
|
3338
3367
|
...(name !== undefined ? { name } : {}),
|
package/lib/tools/index.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import { isAbsolute, resolve } from 'node:path'
|
|
15
|
-
import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '../provider/scope.js'
|
|
15
|
+
import { canonicalWorkspacePath, defaultScopeDescription, normalizeScopeInput, resolveScopeRef } from '../provider/scope.js'
|
|
16
16
|
|
|
17
17
|
/** Shape each tool shares: JSON-schema parameters + canonical output value. */
|
|
18
18
|
|
|
@@ -191,6 +191,15 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
|
|
|
191
191
|
|
|
192
192
|
|
|
193
193
|
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
194
203
|
|
|
195
204
|
|
|
196
205
|
|
|
@@ -290,6 +299,20 @@ function agentOf(exec ) {
|
|
|
290
299
|
return (exec )?.agent
|
|
291
300
|
}
|
|
292
301
|
|
|
302
|
+
/**
|
|
303
|
+
* agent → 会话 id(只给需要"这是谁改的"的元数据写入用,如作用域用途的 createdBy)。
|
|
304
|
+
* 与 provider 的 rawSessionIdOf 同口径:header.id 优先,其次 session.id。
|
|
305
|
+
*/
|
|
306
|
+
function sessionIdOf(agent ) {
|
|
307
|
+
try {
|
|
308
|
+
const session = (agent )?.session
|
|
309
|
+
const id = session?.header?.id ?? session?.id
|
|
310
|
+
return typeof id === 'string' && id !== '' ? id : undefined
|
|
311
|
+
} catch {
|
|
312
|
+
return undefined
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
293
316
|
/**
|
|
294
317
|
* 当前项目 key。优先级:**服务端解析的会话 cwd** → 显式配置 → process.cwd()。
|
|
295
318
|
*
|
|
@@ -1020,19 +1043,20 @@ function applySummaryDedupe(opts , exec , records
|
|
|
1020
1043
|
|
|
1021
1044
|
|
|
1022
1045
|
/**
|
|
1023
|
-
* 0.
|
|
1024
|
-
*
|
|
1025
|
-
*
|
|
1046
|
+
* 0.28.0:范围外命中的**作用域级**索引——只给「命中了哪个作用域、几条、这个作用域
|
|
1047
|
+
* 是干什么的」,**不再给条目摘要**。
|
|
1048
|
+
*
|
|
1049
|
+
* 为什么收窄:0.27.0 的条目索引会带短摘要,模型很容易把那段摘要当成「已经读到的东西」
|
|
1050
|
+
* 直接引用;而它其实是可见域外的内容。现在范围外只暴露作用域用途,模型据此判断
|
|
1051
|
+
* 要不要为某个作用域显式发起审批(requestApproval / memory_scopes request)。
|
|
1026
1052
|
*/
|
|
1027
1053
|
|
|
1028
|
-
|
|
1029
1054
|
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
1055
|
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
|
|
1059
|
+
|
|
1036
1060
|
|
|
1037
1061
|
|
|
1038
1062
|
|
|
@@ -1043,16 +1067,34 @@ function applySummaryDedupe(opts , exec , records
|
|
|
1043
1067
|
|
|
1044
1068
|
|
|
1045
1069
|
|
|
1046
|
-
|
|
1047
|
-
|
|
1070
|
+
|
|
1071
|
+
|
|
1048
1072
|
|
|
1049
|
-
|
|
1073
|
+
|
|
1050
1074
|
|
|
1051
1075
|
|
|
1076
|
+
/**
|
|
1077
|
+
* 范围外清单要展示的作用域用途:优先取 provider 目录里手写过的 description,
|
|
1078
|
+
* 拿不到(旧 provider / 目录里没有这个 scope)时按类型给默认描述。
|
|
1079
|
+
*/
|
|
1080
|
+
function scopeDescriptions(service , agent ) {
|
|
1081
|
+
const out = new Map ()
|
|
1082
|
+
if (typeof service.scopeCatalog !== 'function') return out
|
|
1083
|
+
try {
|
|
1084
|
+
for (const entry of service.scopeCatalog(agent)) {
|
|
1085
|
+
const description = typeof entry.description === 'string' ? entry.description.trim() : ''
|
|
1086
|
+
if (description !== '') out.set(entry.scope, description)
|
|
1087
|
+
}
|
|
1088
|
+
} catch {
|
|
1089
|
+
// 目录是增强:拿不到就退回类型默认描述,不让检索因此失败。
|
|
1090
|
+
}
|
|
1091
|
+
return out
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1052
1094
|
export function memorySearchTool(opts ) {
|
|
1053
1095
|
return {
|
|
1054
1096
|
name: 'memory_search',
|
|
1055
|
-
description: 'Search saved memories by keyword or meaning (lexical + local vector recall). Returns id/scope/kind/tags and a short summary; use memory_get for full content. Out-of-scope hits return
|
|
1097
|
+
description: 'Search saved memories by keyword or meaning (lexical + local vector recall). Returns id/scope/kind/tags and a short summary; use memory_get for full content. Out-of-scope hits return only the list of scopes (name, hit count, purpose — no record summaries) in the outOfScope field instead of auto-raising approval; pass requestApproval=true only when those scopes are genuinely relevant.',
|
|
1056
1098
|
parameters: {
|
|
1057
1099
|
query: { type: 'string', required: true },
|
|
1058
1100
|
scope: { type: 'string', description: SCOPE_DESCRIPTION },
|
|
@@ -1060,7 +1102,7 @@ export function memorySearchTool(opts )
|
|
|
1060
1102
|
includeSuperseded: { type: 'boolean', description: '默认 false;true 时把已被 supersede 的旧版本也返回(历史核对)' },
|
|
1061
1103
|
summaryChars: { type: 'number', description: `每条摘要的字数上限(默认 ${DEFAULT_SUMMARY_CHARS},20~2000)` },
|
|
1062
1104
|
description: { type: 'string', description: DESCRIPTION_PARAM_DESCRIPTION },
|
|
1063
|
-
requestApproval: { type: 'boolean', description: '默认 false
|
|
1105
|
+
requestApproval: { type: 'boolean', description: '默认 false:范围外命中只返回作用域清单(scope + 条数 + 用途,不含任何记忆内容),不自动弹审批;true = 立即向用户发起审批,允许的条目并入 records。确认这些作用域的用途确实与本次任务相关后再传 true。' },
|
|
1064
1106
|
},
|
|
1065
1107
|
async execute(args , exec ) {
|
|
1066
1108
|
const service = svc(opts)
|
|
@@ -1162,25 +1204,27 @@ export function memorySearchTool(opts )
|
|
|
1162
1204
|
: outOfScopeReason(decide, policy, deniedRecords.map((hit) => ({ scope: hit.record.scope, kind: hit.record.kind })))
|
|
1163
1205
|
if (reason !== undefined) result.withheld = { count: denied, reason }
|
|
1164
1206
|
}
|
|
1165
|
-
// 0.
|
|
1166
|
-
//
|
|
1167
|
-
//
|
|
1207
|
+
// 0.28.0:范围外清单(不含任何记忆内容)——命中了哪些作用域、各几条、
|
|
1208
|
+
// 这些作用域是干什么的。只在普通限制下给出:absolute 没有任何审批放行路径,
|
|
1209
|
+
// 清单只会引诱越权尝试。未发起审批(默认)或审批没全过时都会给出。
|
|
1210
|
+
// 条目摘要刻意**不再**返回:范围外的内容不进入上下文,避免模型把摘要当正文引用。
|
|
1168
1211
|
const pendingOutside = outside.filter((hit) => !approvedOutsideIds.has(hit.record.id))
|
|
1169
1212
|
if (pendingOutside.length > 0 && level === 'normal') {
|
|
1170
|
-
const
|
|
1213
|
+
const requestableIds = new Set(needsApproval.map((hit) => hit.record.id))
|
|
1214
|
+
const byScope = new Map ()
|
|
1171
1215
|
for (const hit of pendingOutside) {
|
|
1172
|
-
|
|
1216
|
+
const entry = byScope.get(hit.record.scope) ?? { records: 0, requestable: false }
|
|
1217
|
+
entry.records += 1
|
|
1218
|
+
if (requestableIds.has(hit.record.id)) entry.requestable = true
|
|
1219
|
+
byScope.set(hit.record.scope, entry)
|
|
1173
1220
|
}
|
|
1221
|
+
const descriptions = scopeDescriptions(service, agent)
|
|
1174
1222
|
result.outOfScope = {
|
|
1175
|
-
scopes: [...
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
kind: h.record.kind,
|
|
1181
|
-
tags: Array.isArray(h.record.tags) ? h.record.tags : [],
|
|
1182
|
-
updatedAt: h.record.updatedAt,
|
|
1183
|
-
summary: summarize(h.record.content, chars),
|
|
1223
|
+
scopes: [...byScope].map(([scope, entry]) => ({
|
|
1224
|
+
scope,
|
|
1225
|
+
records: entry.records,
|
|
1226
|
+
description: descriptions.get(scope) ?? defaultScopeDescription(scope),
|
|
1227
|
+
requestable: entry.requestable,
|
|
1184
1228
|
})),
|
|
1185
1229
|
}
|
|
1186
1230
|
}
|
|
@@ -1464,7 +1508,7 @@ export function memoryListTool(opts )
|
|
|
1464
1508
|
}
|
|
1465
1509
|
|
|
1466
1510
|
|
|
1467
|
-
|
|
1511
|
+
|
|
1468
1512
|
|
|
1469
1513
|
|
|
1470
1514
|
|
|
@@ -1478,6 +1522,15 @@ export function memoryListTool(opts )
|
|
|
1478
1522
|
|
|
1479
1523
|
|
|
1480
1524
|
|
|
1525
|
+
|
|
1526
|
+
|
|
1527
|
+
|
|
1528
|
+
|
|
1529
|
+
|
|
1530
|
+
|
|
1531
|
+
|
|
1532
|
+
|
|
1533
|
+
|
|
1481
1534
|
|
|
1482
1535
|
|
|
1483
1536
|
|
|
@@ -1490,7 +1543,17 @@ export function memoryListTool(opts )
|
|
|
1490
1543
|
|
|
1491
1544
|
|
|
1492
1545
|
|
|
1493
|
-
|
|
1546
|
+
/**
|
|
1547
|
+
* action='edit' 的结果(0.29.0):只回「改成了什么」,不回记忆内容。
|
|
1548
|
+
* 字段缺省 = 这次没动它;空串 = 已清空(用途)或已移除(别名)。
|
|
1549
|
+
*/
|
|
1550
|
+
|
|
1551
|
+
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
|
|
1555
|
+
|
|
1556
|
+
|
|
1494
1557
|
|
|
1495
1558
|
/**
|
|
1496
1559
|
* 作用域目录 / 权限申请工具(0.24.0)。
|
|
@@ -1504,9 +1567,9 @@ export function memoryListTool(opts )
|
|
|
1504
1567
|
export function memoryScopesTool(opts ) {
|
|
1505
1568
|
return {
|
|
1506
1569
|
name: 'memory_scopes',
|
|
1507
|
-
description: 'List memory scopes with their purpose and access status (metadata only, no content),
|
|
1570
|
+
description: 'List memory scopes with their purpose and access status (metadata only, no content), request session access to out-of-scope scopes, or edit a scope\'s purpose and alias. action="edit" updates the scope description (empty = clear, falls back to the type default) and/or alias (empty = remove); the scope must be writable in this session. action="request" defaults to read-only and requires a reason; absolute restriction never grants access.',
|
|
1508
1571
|
parameters: {
|
|
1509
|
-
action: { type: 'string', enum: ['list', 'request'], description: 'list = 目录;request =
|
|
1572
|
+
action: { type: 'string', enum: ['list', 'request', 'edit'], description: 'list = 目录;request = 申请范围外作用域的会话权限(默认只读);edit = 编辑某个作用域的描述 / 别名(需要该作用域可写)' },
|
|
1510
1573
|
includeOutOfScope: { type: 'boolean', description: 'list:默认 true;false 时只列当前可见域内的作用域。' },
|
|
1511
1574
|
limit: { type: 'number', description: 'list:1~200,默认 50' },
|
|
1512
1575
|
scopes: {
|
|
@@ -1520,6 +1583,9 @@ export function memoryScopesTool(opts )
|
|
|
1520
1583
|
description: 'request:申请哪些权限,省略 = 只读;写权限必须显式加 "write"',
|
|
1521
1584
|
},
|
|
1522
1585
|
reason: { type: 'string', description: 'request:一句话申请理由,展示在审批卡上' },
|
|
1586
|
+
scope: { type: 'string', description: 'edit:要编辑的作用域键(目录里显示的那个,如 handbook / project:<路径>)。当前会话范围外时先用 action="request" 申请写权限' },
|
|
1587
|
+
scopeDescription: { type: 'string', description: 'edit:新的用途(一句话,最多 500 字)。空串 = 清空并回退到按类型的默认描述;与本参数一起省略 scopeName 则报错。' },
|
|
1588
|
+
scopeName: { type: 'string', description: 'edit:新的别名(只影响显示与引用,不改作用域键;项目 / 自定义作用域支持)。空串 = 移除别名;省略 = 这次不改别名。' },
|
|
1523
1589
|
description: { type: 'string', description: DESCRIPTION_PARAM_DESCRIPTION },
|
|
1524
1590
|
},
|
|
1525
1591
|
async execute(args , exec ) {
|
|
@@ -1527,6 +1593,66 @@ export function memoryScopesTool(opts )
|
|
|
1527
1593
|
const agent = agentOf(exec)
|
|
1528
1594
|
requireDescription(service, agent, args.description)
|
|
1529
1595
|
|
|
1596
|
+
// ---- action='edit':编辑作用域描述 / 别名(0.29.0)----
|
|
1597
|
+
// 只改**目录元数据**,不碰任何记忆记录;需要该作用域可写(范围外先用
|
|
1598
|
+
// action='request' 拿写权限,不走隐式审批 —— 审批卡里没有"一条记忆"可展示)。
|
|
1599
|
+
if (args.action === 'edit') {
|
|
1600
|
+
const rawScope = typeof args.scope === 'string' ? args.scope.trim() : ''
|
|
1601
|
+
if (rawScope === '') throw new MemoryToolError('SCOPE_REQUIRED', 'action="edit" 需要 scope(要编辑的作用域键,目录里显示的那个)')
|
|
1602
|
+
const wantDescription = typeof args.scopeDescription === 'string'
|
|
1603
|
+
const wantName = typeof args.scopeName === 'string'
|
|
1604
|
+
if (!wantDescription && !wantName) {
|
|
1605
|
+
throw new MemoryToolError('EDIT_NOTHING_TO_UPDATE', 'action="edit" 至少要给 scopeDescription 或 scopeName 之一(想清空用途传空串,想移除别名传空串)')
|
|
1606
|
+
}
|
|
1607
|
+
if (typeof service.describeScope !== 'function' && wantDescription) {
|
|
1608
|
+
throw new MemoryToolError('SCOPES_UNAVAILABLE', '当前 provider 不支持编辑作用域用途(请升级 @restregular/dsh-memory)')
|
|
1609
|
+
}
|
|
1610
|
+
if (typeof service.renameScope !== 'function' && wantName) {
|
|
1611
|
+
throw new MemoryToolError('SCOPES_UNAVAILABLE', '当前 provider 不支持编辑作用域别名(请升级 @restregular/dsh-memory)')
|
|
1612
|
+
}
|
|
1613
|
+
const resolved = resolveScope(opts, service, exec, rawScope, 'write')
|
|
1614
|
+
if (resolved.all === true) {
|
|
1615
|
+
throw new MemoryToolError('SCOPE_NOT_WRITABLE', "scope 'all' cannot be edited; name one scope")
|
|
1616
|
+
}
|
|
1617
|
+
const scope = resolved.scope ?? currentSessionScope(service, exec)
|
|
1618
|
+
if (scope === undefined) {
|
|
1619
|
+
throw new MemoryToolError('SCOPE_INVALID', `scope "${rawScope}" could not be resolved; use the concrete scope key shown by action="list"`)
|
|
1620
|
+
}
|
|
1621
|
+
// 可写护栏:与写入记忆同一把锁(write 开关 / absolute / 范围外)。
|
|
1622
|
+
const policy = visibilityPolicy(service, agent)
|
|
1623
|
+
const decide = decisionMaker(service, agent)
|
|
1624
|
+
const level = policy?.level ?? 'none'
|
|
1625
|
+
if (level !== 'none' && !scopeWritableBy(decide, service, agent, policy, scope)) {
|
|
1626
|
+
const decision = decide('write', scope)
|
|
1627
|
+
if (decision !== undefined) {
|
|
1628
|
+
if (!decision.requestable) throw writeDecisionError(decision, scope, 'write')
|
|
1629
|
+
throw new MemoryToolError(
|
|
1630
|
+
'SCOPE_NOT_WRITABLE',
|
|
1631
|
+
`作用域 ${scope} 不在本会话的可写范围内;编辑作用域信息需要写权限,先用 memory_scopes({ action: "request", scopes: ["${scope}"], permissions: ["write"] }) 申请,批准后重试。`,
|
|
1632
|
+
)
|
|
1633
|
+
}
|
|
1634
|
+
if (policy?.level === 'normal') {
|
|
1635
|
+
// 老 provider 没有统一判定:普通限制下范围外就是"可申请",别报成绝对限制。
|
|
1636
|
+
throw new MemoryToolError(
|
|
1637
|
+
'SCOPE_NOT_WRITABLE',
|
|
1638
|
+
`作用域 ${scope} 不在本会话的可写范围内;编辑作用域信息需要写权限,先用 memory_scopes({ action: "request", scopes: ["${scope}"], permissions: ["write"] }) 申请,批准后重试。`,
|
|
1639
|
+
)
|
|
1640
|
+
}
|
|
1641
|
+
throw scopeWriteBlockedError(policy, scope, 'write')
|
|
1642
|
+
}
|
|
1643
|
+
const description = wantDescription ? String(args.scopeDescription) : undefined
|
|
1644
|
+
const name = wantName ? String(args.scopeName) : undefined
|
|
1645
|
+
const by = sessionIdOf(agent) ?? 'agent'
|
|
1646
|
+
try {
|
|
1647
|
+
const edited = { scope }
|
|
1648
|
+
if (description !== undefined) edited.description = service.describeScope (scope, description, by).description
|
|
1649
|
+
if (name !== undefined) edited.name = service.renameScope (scope, name).name ?? ''
|
|
1650
|
+
return edited
|
|
1651
|
+
} catch (error) {
|
|
1652
|
+
throw toolError(error)
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1530
1656
|
if (args.action === 'request') {
|
|
1531
1657
|
if (typeof service.requestScopeGrants !== 'function') {
|
|
1532
1658
|
throw new MemoryToolError('SCOPES_UNAVAILABLE', '当前 provider 不支持作用域权限申请(请升级 @restregular/dsh-memory)')
|
package/lib/tools/render.js
CHANGED
|
@@ -8,8 +8,10 @@
|
|
|
8
8
|
* 0.20.0:范围外命中被挡住时,把**真实原因**(用户拒绝 / 审批超时 / 无法审批)
|
|
9
9
|
* 写进渲染文本。
|
|
10
10
|
* 0.23.0:绝对限制与「不可读」开关也会被说明(不再假装"不存在"),并提示怎么调整。
|
|
11
|
-
* 0.27.0:普通限制下范围外命中不再自动弹审批 —— 检索结果带 outOfScope
|
|
12
|
-
*
|
|
11
|
+
* 0.27.0:普通限制下范围外命中不再自动弹审批 —— 检索结果带 outOfScope 清单
|
|
12
|
+
* (作用域聚合),审批改为 Agent 显式发起。
|
|
13
|
+
* 0.28.0:范围外清单只给作用域(名字 / 条数 / 用途),**不再给条目摘要**;清单与
|
|
14
|
+
* withheld 说明不再重复讲同一件事(「索引已列在下方」那句直接去掉)。
|
|
13
15
|
*/
|
|
14
16
|
|
|
15
17
|
import { scopeDisplayName } from '../provider/scope.js'
|
|
@@ -43,7 +45,7 @@ export function renderWithheldNote(withheld ) {
|
|
|
43
45
|
const count = withheld.count
|
|
44
46
|
switch (withheld.reason) {
|
|
45
47
|
case 'out-of-scope':
|
|
46
|
-
return `另有 ${count}
|
|
48
|
+
return `另有 ${count} 条记忆在本会话的可见域外(限制等级:普通限制),本次没有弹审批。要看正文需要显式发起:用 memory_search 带 requestApproval=true 重发同一次搜索(会弹审批),或用 memory_scopes(action="request") 申请该作用域的会话权限。`
|
|
47
49
|
case 'rejected':
|
|
48
50
|
return `另有 ${count} 条记忆在本会话的可见域外,用户拒绝了本次访问(限制等级:普通限制),未返回。如确实需要,请让用户调整「记忆范围」后重试。`
|
|
49
51
|
case 'timeout':
|
|
@@ -64,35 +66,44 @@ export function renderWithheldNote(withheld ) {
|
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
+
* 范围外清单的尾部说明(0.27.0 / 0.28.0)。告诉模型这块**只有作用域**,不含任何
|
|
70
|
+
* 记忆内容;真要读时该走哪条路。
|
|
69
71
|
*/
|
|
70
72
|
export const OUT_OF_SCOPE_INDEX_NOTE =
|
|
71
|
-
'
|
|
73
|
+
'以上只列了作用域,不含任何记忆内容。需要正文时:用 memory_search 带 requestApproval=true 重新发起同一次搜索(会弹审批),或用 memory_scopes(action="request") 申请该作用域的本会话权限。'
|
|
72
74
|
|
|
73
75
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
+
* 范围外清单:每个作用域一行「名称(条数 · 可申请性):用途」。
|
|
77
|
+
* 刻意与可见域内的结果分开排版,并且**不出现任何条目 id / 摘要** ——
|
|
78
|
+
* 范围外的内容不该有机会被当成「已经读到的记忆」引用。
|
|
76
79
|
*/
|
|
77
80
|
function renderOutOfScopeIndex(
|
|
78
81
|
outOfScope ,
|
|
79
82
|
label ,
|
|
80
83
|
) {
|
|
81
|
-
const
|
|
82
|
-
const lines = outOfScope.
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
|
|
84
|
+
const total = outOfScope.scopes.reduce((sum, entry) => sum + entry.records, 0)
|
|
85
|
+
const lines = outOfScope.scopes.map((entry) => {
|
|
86
|
+
const text = typeof entry.description === 'string' ? entry.description.trim() : ''
|
|
87
|
+
const description = text === '' ? '(未填写用途)' : text
|
|
88
|
+
const meta = [`${entry.records} 条`]
|
|
89
|
+
if (entry.requestable === false) meta.push('不可申请')
|
|
90
|
+
return `- ${label(entry.scope)}(${meta.join(' · ')}):${description}`
|
|
86
91
|
})
|
|
87
|
-
|
|
92
|
+
const head = `范围外命中(只列作用域与用途,不含条目内容):${outOfScope.scopes.length} 个作用域 / ${total} 条`
|
|
93
|
+
return [head, ...lines, '', OUT_OF_SCOPE_INDEX_NOTE].join('\n')
|
|
88
94
|
}
|
|
89
95
|
|
|
90
96
|
/** 「[得分] id (作用域/类型 · 标签) 摘要」;没有任何一条命中时给明确空结果。 */
|
|
91
97
|
export function renderSearchResult(value , label ) {
|
|
92
|
-
const
|
|
93
|
-
const index = value.outOfScope !== undefined && (value.outOfScope.records.length > 0 || value.outOfScope.scopes.length > 0)
|
|
98
|
+
const index = value.outOfScope !== undefined && value.outOfScope.scopes.length > 0
|
|
94
99
|
? renderOutOfScopeIndex(value.outOfScope, label)
|
|
95
100
|
: ''
|
|
101
|
+
// 0.28.0:清单自己已经把「几条 / 为什么没返回 / 怎么取」讲清楚了,
|
|
102
|
+
// 再叠一段 'out-of-scope' 的 withheld 说明就是同一件事说两遍(而且旧文案还
|
|
103
|
+
// 写着「索引已列在下方」,实际排在最后 —— 结构更乱)。有清单时不重复。
|
|
104
|
+
const note = index !== '' && value.withheld?.reason === 'out-of-scope'
|
|
105
|
+
? ''
|
|
106
|
+
: renderWithheldNote(value.withheld)
|
|
96
107
|
if (value.records.length === 0) {
|
|
97
108
|
if (note === '' && index === '') return '没有找到相关记忆。'
|
|
98
109
|
return [`没有找到可见范围内的相关记忆。`, index, note].filter((part) => part !== '').join('\n\n')
|
|
@@ -138,7 +149,8 @@ export function renderListResult(value , label ) {
|
|
|
138
149
|
|
|
139
150
|
/**
|
|
140
151
|
* memory_scopes 的渲染:list 只讲“有什么、干什么、能不能碰”;request 讲每个
|
|
141
|
-
* scope +
|
|
152
|
+
* scope + 权限的申请结局;edit(0.29.0)讲这次改了描述 / 别名的什么。
|
|
153
|
+
* 三者共用同一个工具入口,但文案完全分开。
|
|
142
154
|
*/
|
|
143
155
|
export function renderScopesResult(value , label ) {
|
|
144
156
|
if ('results' in value) {
|
|
@@ -170,6 +182,20 @@ export function renderScopesResult(value , label )
|
|
|
170
182
|
).join('\n')
|
|
171
183
|
return `作用域权限申请结果:\n${body}`
|
|
172
184
|
}
|
|
185
|
+
// edit(0.29.0):只回「改成了什么」。没动过的字段不出现,空串表示清空 / 移除。
|
|
186
|
+
if ('scope' in value) {
|
|
187
|
+
const name = value.name !== undefined && value.name !== '' ? scopeDisplayName(value.scope, value.name) : label(value.scope)
|
|
188
|
+
const lines = [`作用域已更新:${name}(${value.scope})`]
|
|
189
|
+
if (value.description !== undefined) {
|
|
190
|
+
lines.push(value.description === ''
|
|
191
|
+
? '- 用途:已清空(回退到按类型的默认描述)'
|
|
192
|
+
: `- 用途:${value.description}`)
|
|
193
|
+
}
|
|
194
|
+
if (value.name !== undefined) {
|
|
195
|
+
lines.push(value.name === '' ? '- 别名:已移除(显示名回退到作用域键)' : `- 别名:${value.name}`)
|
|
196
|
+
}
|
|
197
|
+
return lines.join('\n')
|
|
198
|
+
}
|
|
173
199
|
if (value.scopes.length === 0) return '当前没有可列出的作用域。'
|
|
174
200
|
const body = value.scopes.map((item) => {
|
|
175
201
|
// 别名(项目 / 自定义作用域)也带类型前缀:自定义作用域的键本身没有前缀,
|