@restregular/dsh-memory 0.26.0 → 0.28.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 +737 -719
- package/README.md +647 -634
- package/lib/client.js +116 -4
- package/lib/inject/apply.js +1 -1
- package/lib/inject/policy.js +2 -0
- package/lib/panel.js +47 -8
- package/lib/provider/index.js +5 -1
- package/lib/provider/scope.js +14 -0
- package/lib/tools/index.js +107 -5
- package/lib/tools/render.js +46 -4
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -184,6 +184,19 @@ window.__ModuleLoader__.load({
|
|
|
184
184
|
'.memp .tab{background:none;border:0;border-radius:7px;padding:4px 14px;color:var(--dsw-alias-label-secondary);cursor:pointer;font-size:13px;line-height:1.6;transition:background .12s ease,color .12s ease}',
|
|
185
185
|
'.memp .tab:hover{color:var(--dsw-alias-label-primary)}',
|
|
186
186
|
'.memp .tab.on{background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-brand-primary);font-weight:600;box-shadow:0 1px 2px rgba(0,0,0,.10)}',
|
|
187
|
+
// 标签页里的二级切换(记录 → 记忆 / 作用域):比主标签栏小一档,跟在主标签栏下面
|
|
188
|
+
'.memp .subtabs{display:inline-flex;gap:2px;padding:2px;border-radius:8px;background:var(--dsw-alias-bg-layer-2);border:1px solid var(--dsw-alias-border-l1)}',
|
|
189
|
+
'.memp .subtab{background:none;border:0;border-radius:6px;padding:2px 12px;color:var(--dsw-alias-label-secondary);cursor:pointer;font-size:12px;line-height:1.7;transition:background .12s ease,color .12s ease}',
|
|
190
|
+
'.memp .subtab:hover{color:var(--dsw-alias-label-primary)}',
|
|
191
|
+
'.memp .subtab.on{background:var(--dsw-alias-bg-layer-1);color:var(--dsw-alias-brand-primary);font-weight:600}',
|
|
192
|
+
// 作用域清单:一行一个作用域(名称 + 条数 + 用途 + 真实键)
|
|
193
|
+
'.memp .scope-row{display:flex;flex-direction:column;gap:5px;padding:9px 12px;border:1px solid var(--dsw-alias-border-l1);border-radius:10px;background:var(--dsw-alias-bg-layer-1)}',
|
|
194
|
+
'.memp .scope-row:hover{border-color:var(--dsw-alias-brand-primary)}',
|
|
195
|
+
'.memp .scope-row-head{display:flex;align-items:center;gap:7px;flex-wrap:wrap;font-size:11.5px;color:var(--dsw-alias-label-secondary);min-width:0}',
|
|
196
|
+
'.memp .scope-row-name{font-size:12.5px;font-weight:600;color:var(--dsw-alias-label-primary);max-width:320px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
197
|
+
'.memp .scope-row-desc{font-size:11.5px;line-height:1.55;color:var(--dsw-alias-label-secondary)}',
|
|
198
|
+
'.memp .scope-row-foot{display:flex;align-items:center;gap:8px;flex-wrap:wrap;font-size:10.5px;color:var(--dsw-alias-label-secondary)}',
|
|
199
|
+
'.memp .scope-row-key{font-family:ui-monospace,SFMono-Regular,Consolas,monospace;font-size:10.5px;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
187
200
|
// 记录视图:各区块之间留出呼吸感(之前全贴在一起)
|
|
188
201
|
'.memp .memp-body > * + *{margin-top:14px}',
|
|
189
202
|
'.memp .stats{margin-bottom:0}',
|
|
@@ -3535,6 +3548,10 @@ window.__ModuleLoader__.load({
|
|
|
3535
3548
|
const [loading, setLoading] = React.useState(true)
|
|
3536
3549
|
const [error, setError] = React.useState('')
|
|
3537
3550
|
const [tab, setTab] = React.useState('data')
|
|
3551
|
+
// 「记录」页里的二级标签页(0.28.0):记忆(列表) / 作用域(名称 + 用途 + 条数)。
|
|
3552
|
+
// 两个视图共用 /status 的那次拉取,切换不重新请求。
|
|
3553
|
+
const [dataView, setDataView] = React.useState('memories')
|
|
3554
|
+
const [scopeQuery, setScopeQuery] = React.useState('')
|
|
3538
3555
|
const [pendingTick, setPendingTick] = React.useState(0)
|
|
3539
3556
|
const [creating, setCreating] = React.useState(false)
|
|
3540
3557
|
|
|
@@ -3595,6 +3612,8 @@ window.__ModuleLoader__.load({
|
|
|
3595
3612
|
// 列表里没有时补一张卡,保证跳过去一定能展开。
|
|
3596
3613
|
setFocusRecordData(record && record.id ? record : null)
|
|
3597
3614
|
setTab('data')
|
|
3615
|
+
// 「记录」页现在是 记忆 / 作用域 两个子标签页:跳转必须落在记忆列表上。
|
|
3616
|
+
setDataView('memories')
|
|
3598
3617
|
setQ('')
|
|
3599
3618
|
// 语义模式是按查询排名的:跳转要看到那条记录,必须回到普通列表。
|
|
3600
3619
|
setSemantic(false)
|
|
@@ -3603,6 +3622,16 @@ window.__ModuleLoader__.load({
|
|
|
3603
3622
|
setScopeFilter(nextScope)
|
|
3604
3623
|
load('', nextScope, false)
|
|
3605
3624
|
}
|
|
3625
|
+
/** 作用域行的「查看记忆」:切回记忆子标签页并只看这个作用域。 */
|
|
3626
|
+
const openScopeRecords = (scope) => {
|
|
3627
|
+
setDataView('memories')
|
|
3628
|
+
setScopeFilter(scope)
|
|
3629
|
+
setQ('')
|
|
3630
|
+
setSupersededFilter('hide')
|
|
3631
|
+
setSemantic(false)
|
|
3632
|
+
setSearchInfo(null)
|
|
3633
|
+
load('', scope, false)
|
|
3634
|
+
}
|
|
3606
3635
|
// 审批浮层 / 其它入口点击记忆 ID:挂载时消费一次 pending,之后靠同一事件。
|
|
3607
3636
|
React.useEffect(() => {
|
|
3608
3637
|
const applyFocus = () => {
|
|
@@ -3999,14 +4028,97 @@ window.__ModuleLoader__.load({
|
|
|
3999
4028
|
h('a', { href: 'https://github.com/RestRegular/dsh-memory/issues/new', target: '_blank', rel: 'noreferrer' }, '💬 反馈建议 / 提 issue'),
|
|
4000
4029
|
)
|
|
4001
4030
|
const overviewBody = h('div', { className: 'memp-body' }, serverCard, maintenanceCard, footer)
|
|
4031
|
+
|
|
4032
|
+
// ---- 「记录」页的二级标签页:记忆 / 作用域(0.28.0) ----
|
|
4033
|
+
// 作用域子标签页回答的是「有哪些域、各自干什么、多少条」——用途与
|
|
4034
|
+
// memory_scopes 目录、memory_search 的范围外清单同源(服务端 /status 已合并)。
|
|
4035
|
+
const scopeRows = (() => {
|
|
4036
|
+
const needle = scopeQuery.trim().toLowerCase()
|
|
4037
|
+
return scopes
|
|
4038
|
+
.map((s) => ({
|
|
4039
|
+
scope: s.scope,
|
|
4040
|
+
label: scopeLabel(s.scope, labels),
|
|
4041
|
+
description: typeof s.description === 'string' ? s.description.trim() : '',
|
|
4042
|
+
records: Number(s.records) || 0,
|
|
4043
|
+
pendingEmbeds: Number(s.pendingEmbeds) || 0,
|
|
4044
|
+
updatedAt: Number(s.updatedAt) || 0,
|
|
4045
|
+
}))
|
|
4046
|
+
.filter((row) => needle === ''
|
|
4047
|
+
|| row.label.toLowerCase().includes(needle)
|
|
4048
|
+
|| row.scope.toLowerCase().includes(needle)
|
|
4049
|
+
|| row.description.toLowerCase().includes(needle))
|
|
4050
|
+
.sort((a, b) => b.records - a.records || b.updatedAt - a.updatedAt || a.label.localeCompare(b.label))
|
|
4051
|
+
})()
|
|
4052
|
+
const viewSwitch = h('div', { className: 'row' },
|
|
4053
|
+
h('div', { className: 'subtabs' },
|
|
4054
|
+
h('button', {
|
|
4055
|
+
className: 'subtab' + (dataView === 'memories' ? ' on' : ''),
|
|
4056
|
+
onClick: () => setDataView('memories'),
|
|
4057
|
+
}, '记忆'),
|
|
4058
|
+
h('button', {
|
|
4059
|
+
className: 'subtab' + (dataView === 'scopes' ? ' on' : ''),
|
|
4060
|
+
onClick: () => setDataView('scopes'),
|
|
4061
|
+
}, '作用域'),
|
|
4062
|
+
),
|
|
4063
|
+
)
|
|
4064
|
+
const scopeToolbar = h('div', { className: 'row toolbar' },
|
|
4065
|
+
h('input', {
|
|
4066
|
+
type: 'text',
|
|
4067
|
+
placeholder: '搜索作用域名称 / 用途 / 键',
|
|
4068
|
+
value: scopeQuery,
|
|
4069
|
+
onChange: (e) => setScopeQuery(e.target.value),
|
|
4070
|
+
}),
|
|
4071
|
+
h('button', { onClick: () => load(), disabled: loading }, loading ? '刷新中…' : '刷新'),
|
|
4072
|
+
scopeQuery.trim() !== '' ? h('button', { className: 'linkbtn', onClick: () => setScopeQuery('') }, '清空筛选') : null,
|
|
4073
|
+
)
|
|
4074
|
+
const scopeListBody = !loading && !error && scopeRows.length === 0
|
|
4075
|
+
? h('div', { className: 'empty' }, scopeQuery.trim() !== '' ? '没有匹配的作用域' : '还没有作用域')
|
|
4076
|
+
: h('div', { className: 'list' },
|
|
4077
|
+
scopeRows.map((row) => h('div', { className: 'scope-row', key: row.scope },
|
|
4078
|
+
h('div', { className: 'scope-row-head' },
|
|
4079
|
+
h('span', { className: 'scope-row-name', title: scopeTitle(row.scope, row.label) }, row.label),
|
|
4080
|
+
h('span', { className: 'badge' }, `${row.records} 条记忆`),
|
|
4081
|
+
row.pendingEmbeds > 0
|
|
4082
|
+
? h('span', { className: 'pending', title: '待嵌入向量:向量服务就绪后会自动补上' }, `待嵌入 ${row.pendingEmbeds}`)
|
|
4083
|
+
: null,
|
|
4084
|
+
h('span', {
|
|
4085
|
+
className: 'time',
|
|
4086
|
+
title: row.updatedAt > 0 ? `最近更新 ${fmtTime(row.updatedAt)}` : '还没有记录',
|
|
4087
|
+
}, row.updatedAt > 0 ? fmtTime(row.updatedAt) : '—'),
|
|
4088
|
+
),
|
|
4089
|
+
h('div', { className: 'scope-row-desc' }, row.description === '' ? '(未填写用途)' : row.description),
|
|
4090
|
+
h('div', { className: 'scope-row-foot' },
|
|
4091
|
+
h('span', { className: 'scope-row-key', title: '作用域键:工具参数直接用它(自定义作用域传裸名)' }, row.scope),
|
|
4092
|
+
h('span', { style: { flex: 1 } }),
|
|
4093
|
+
row.records > 0
|
|
4094
|
+
? h('button', {
|
|
4095
|
+
className: 'linkbtn',
|
|
4096
|
+
title: '切到「记忆」并只看这个作用域',
|
|
4097
|
+
onClick: () => openScopeRecords(row.scope),
|
|
4098
|
+
}, '查看记忆')
|
|
4099
|
+
: null,
|
|
4100
|
+
),
|
|
4101
|
+
)),
|
|
4102
|
+
)
|
|
4103
|
+
// 旧 provider 没有 scope-meta 用途:说明一句,而不是让人以为作用域都没有用途。
|
|
4104
|
+
const scopeDescriptionMissing = scopes.length > 0
|
|
4105
|
+
&& scopes.every((s) => !(typeof s.description === 'string' && s.description.trim() !== ''))
|
|
4106
|
+
|
|
4002
4107
|
// 记录页:统计 + 工具栏 + 新建表单固定在上面,只有列表在滚动容器里 —— 记忆多了也不把面板撑长。
|
|
4003
4108
|
const dataBody = h('div', { className: 'memp-body' },
|
|
4109
|
+
viewSwitch,
|
|
4004
4110
|
statsCard,
|
|
4005
|
-
toolbar,
|
|
4006
|
-
searchNoteNode,
|
|
4007
|
-
|
|
4111
|
+
dataView === 'scopes' ? scopeToolbar : toolbar,
|
|
4112
|
+
dataView === 'scopes' ? null : searchNoteNode,
|
|
4113
|
+
dataView === 'scopes'
|
|
4114
|
+
? (scopeDescriptionMissing
|
|
4115
|
+
? h('div', { className: 'hint' }, '当前 provider 没有返回作用域用途(升级 @restregular/dsh-memory 后即可显示)。')
|
|
4116
|
+
: null)
|
|
4117
|
+
: createForm,
|
|
4008
4118
|
error ? h('div', { className: 'err' }, error) : null,
|
|
4009
|
-
h('div', {
|
|
4119
|
+
h('div', {
|
|
4120
|
+
className: 'scroll' + ((dataView === 'scopes' ? scopeRows.length > 0 : records.length > 0) || loading ? '' : ' is-empty'),
|
|
4121
|
+
}, dataView === 'scopes' ? scopeListBody : listBody),
|
|
4010
4122
|
)
|
|
4011
4123
|
// 记忆树是自己的过滤维度(客户端即时筛选),进来时先拉一份不带服务端筛选的全量记录,
|
|
4012
4124
|
// 否则从「记录」页带着搜索词切过来会只剩一部分树。
|
package/lib/inject/apply.js
CHANGED
|
@@ -131,7 +131,7 @@ const NUDGE_TEXT =
|
|
|
131
131
|
|
|
132
132
|
const VISIBILITY_LEVEL_TEXT = {
|
|
133
133
|
absolute: '绝对限制:可见域外的记忆一律查不到,即使工具内部命中也不会返回;范围外也写不进去。',
|
|
134
|
-
normal: '
|
|
134
|
+
normal: '普通限制:可见域外的记忆命中时默认只返回作用域清单(outOfScope 字段:名字 / 条数 / 用途,不含记忆内容),不自动弹审批;Agent 确认这些作用域确实相关后带 requestApproval=true 才向用户发起一次审批。写入 / 删除范围外同样需要审批。',
|
|
135
135
|
none: '无限制:可以读写所有作用域(当前选择仅作为下次切换的偏好保留)。',
|
|
136
136
|
}
|
|
137
137
|
|
package/lib/inject/policy.js
CHANGED
|
@@ -8,6 +8,8 @@ export const POLICY_TEXT = `## 记忆系统
|
|
|
8
8
|
|
|
9
9
|
memory_search 只返回印象(ID、作用域、类型、标签和少量摘要);判断相关后再用 memory_get(ids, fields) 取正文。fields 省略 = content + scope + kind + tags;只要摘要用 ["summary"],只要正文用 ["content"],需要重要度 / 时间 / 触发词等再显式加上对应字段。
|
|
10
10
|
|
|
11
|
+
搜索命中作用域外记忆时默认**不弹审批**:结果里的 outOfScope 字段只列出命中的作用域——各几条、以及这些作用域的**用途描述**,不含任何记忆内容(也就没有摘要可引用)。确认某个作用域的用途确实与本次任务相关时,再用 memory_search 带 requestApproval=true 重新发起同一次搜索(这时才弹审批),或用 memory_scopes(action="request") 申请该作用域的本会话权限。
|
|
12
|
+
|
|
11
13
|
需要知道有哪些作用域、各自用来做什么、当前能不能读/写时,用 memory_scopes(action="list")。它只返回目录元信息(名字 / 用途 / 访问状态)与初始化记录正文,不返回普通记忆正文;范围外作用域在绝对限制下仍可发现,但正文(含初始化记录)永不可读、不可申请。
|
|
12
14
|
|
|
13
15
|
需要长期使用范围外作用域时,用 memory_scopes 的 action="request" 申请本会话权限:默认只读,写权限必须在 permissions 里显式列出;审批卡默认只勾只读。批准后本会话(及子 Agent)可直接读写该作用域,不必每次审批;一次性访问仍直接调用 memory_search / memory_get。
|
package/lib/panel.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - apply 整体兜底 catch:任何意外都不允许向外抛。
|
|
9
9
|
*
|
|
10
10
|
* 提供管理员 API(前缀路由 /memory/api):
|
|
11
|
-
* GET /status 各 scope 记录数 + 待嵌入数 + 展示标签
|
|
11
|
+
* GET /status 各 scope 记录数 + 待嵌入数 + 展示标签 + 用途描述(0.28.0)
|
|
12
12
|
* GET /records ?scope=&q= 枚举/过滤记录
|
|
13
13
|
* GET /search ?scope=&q=&topK= 语义检索(向量 + 词法,provider 排序)
|
|
14
14
|
* POST /update {id, content?, kind?, importance?, tags?, triggers?, expiresAt?/ttlMs?}
|
|
@@ -882,15 +882,54 @@ async function handle(ctx, routePath, req, res) {
|
|
|
882
882
|
const stats = typeof memory.scopeStats === 'function'
|
|
883
883
|
? memory.scopeStats()
|
|
884
884
|
: store.allScopes().map((s) => ({ scope: s.scope, records: s.records.size, pendingEmbeds: s.embedPending.size }))
|
|
885
|
+
// 作用域用途(0.28.0):面板「记录 → 作用域」子标签页要展示的「这个域是干什么的」。
|
|
886
|
+
// 与 memory_scopes 目录 / memory_search 的范围外清单同源(scope-meta 手写优先,
|
|
887
|
+
// 没写过按类型给默认描述),所以三处看到的用途永远一致。
|
|
888
|
+
const catalog = new Map()
|
|
889
|
+
try {
|
|
890
|
+
if (typeof memory.scopeCatalog === 'function') {
|
|
891
|
+
for (const entry of memory.scopeCatalog(undefined) ?? []) {
|
|
892
|
+
if (entry && typeof entry.scope === 'string') catalog.set(entry.scope, entry)
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
} catch { /* 目录是增强:拿不到就只有名称与条数 */ }
|
|
885
896
|
// 会话标题是尽力而为的增强,而且要读会话事件日志:只给最近活跃的前 N 个查,
|
|
886
897
|
// 其余直接用「会话 <id 前 16 位>」。列表本身**始终**完整(不受这个上限影响)。
|
|
887
|
-
const scopes = stats.map((s, index) =>
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
:
|
|
893
|
-
|
|
898
|
+
const scopes = stats.map((s, index) => {
|
|
899
|
+
const entry = catalog.get(s.scope)
|
|
900
|
+
return {
|
|
901
|
+
...s,
|
|
902
|
+
// 别名(项目 / 自定义作用域)也要带类型前缀;没有别名时退回按类型算的展示名。
|
|
903
|
+
label: typeof s.name === 'string' && s.name !== ''
|
|
904
|
+
? scopeDisplayName(s.scope, s.name)
|
|
905
|
+
: (index < TITLE_LOOKUP_LIMIT ? scopeLabel(ctx, s.scope) : fallbackScopeLabel(s.scope)),
|
|
906
|
+
...(entry !== undefined && typeof entry.description === 'string' && entry.description.trim() !== ''
|
|
907
|
+
? { description: entry.description.trim() }
|
|
908
|
+
: {}),
|
|
909
|
+
...(entry !== undefined && typeof entry.origin === 'string' ? { origin: entry.origin } : {}),
|
|
910
|
+
}
|
|
911
|
+
})
|
|
912
|
+
// 删光记录不等于作用域消失:目录里有用途、却没有记录的作用域也列出来。
|
|
913
|
+
// 只收**有元数据**的(updatedAt 来自 scope-meta):默认三路里的 global / 当前项目
|
|
914
|
+
// 也会出现在目录里,但它们未必真的落过盘,全塞进列表就是凭空多出两个作用域。
|
|
915
|
+
// 别的会话的 session scope 不列 —— 它们的用途是空的,只会把清单变长。
|
|
916
|
+
const seen = new Set(scopes.map((s) => s.scope))
|
|
917
|
+
for (const [key, entry] of catalog) {
|
|
918
|
+
if (seen.has(key) || key.startsWith('session:')) continue
|
|
919
|
+
if (typeof entry.description !== 'string' || entry.description.trim() === '') continue
|
|
920
|
+
if (!(typeof entry.updatedAt === 'number' && entry.updatedAt > 0)) continue
|
|
921
|
+
scopes.push({
|
|
922
|
+
scope: key,
|
|
923
|
+
records: 0,
|
|
924
|
+
pendingEmbeds: 0,
|
|
925
|
+
updatedAt: entry.updatedAt,
|
|
926
|
+
label: typeof entry.name === 'string' && entry.name !== ''
|
|
927
|
+
? scopeDisplayName(key, entry.name)
|
|
928
|
+
: fallbackScopeLabel(key),
|
|
929
|
+
description: entry.description.trim(),
|
|
930
|
+
...(typeof entry.origin === 'string' ? { origin: entry.origin } : {}),
|
|
931
|
+
})
|
|
932
|
+
}
|
|
894
933
|
let embedServer = null
|
|
895
934
|
// older providers have no lifecycle API — the panel simply omits the card
|
|
896
935
|
try { embedServer = (await memory.embedServerStatus?.()) ?? null } catch { embedServer = null }
|
package/lib/provider/index.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import { MemoryStore, newId, scopeFileName, } from './store.js'
|
|
18
18
|
import {
|
|
19
19
|
canonicalWorkspacePath,
|
|
20
|
+
defaultScopeDescription,
|
|
20
21
|
isCustomScope,
|
|
21
22
|
normalizeScopeInput,
|
|
22
23
|
normalizeScopeKey,
|
|
@@ -3329,10 +3330,13 @@ export class LocalMemoryService {
|
|
|
3329
3330
|
: 'restricted'
|
|
3330
3331
|
const name = meta?.name ?? this.names.nameOf(scope)
|
|
3331
3332
|
const initialRecords = policy.level === 'absolute' ? undefined : this.initialRecordsOf(scope)
|
|
3333
|
+
// 0.27.0:scope-meta 没写用途时按作用域类型给默认描述,
|
|
3334
|
+
// 让目录里每一条都自带「这个域是干什么的」。
|
|
3335
|
+
const description = meta?.description ?? defaultScopeDescription(scope)
|
|
3332
3336
|
out.push({
|
|
3333
3337
|
scope,
|
|
3334
3338
|
...(name !== undefined ? { name } : {}),
|
|
3335
|
-
...(
|
|
3339
|
+
...(description !== '' ? { description } : {}),
|
|
3336
3340
|
origin: meta?.origin ?? scopeKind(scope),
|
|
3337
3341
|
...(inScope ? { records: stat?.records ?? 0 } : {}),
|
|
3338
3342
|
inScope,
|
package/lib/provider/scope.js
CHANGED
|
@@ -292,6 +292,20 @@ export function scopeDisplayName(scope , humanName ) {
|
|
|
292
292
|
return `${scopeTypePrefix(scope)}${name}`
|
|
293
293
|
}
|
|
294
294
|
|
|
295
|
+
/**
|
|
296
|
+
* 0.27.0:内置作用域的**默认用途描述**。scope-meta 里没写过 description 时,
|
|
297
|
+
* 作用域目录(memory_scopes list)用它兜底,让每类作用域条目都带一句
|
|
298
|
+
* 「这个域是干什么的」,而不是统一的「(未填写用途)」。
|
|
299
|
+
* 自定义作用域的用途应该由创建者写(memory_save 的 scopeDescription),
|
|
300
|
+
* 这里只给最通用的说明。
|
|
301
|
+
*/
|
|
302
|
+
export function defaultScopeDescription(scope ) {
|
|
303
|
+
if (scope === 'global') return '全局作用域:跨项目共享的通用事实与偏好;适合放与具体仓库无关的长期记忆。'
|
|
304
|
+
if (scope.startsWith('project:')) return '项目作用域:绑定当前工作区路径的共享记忆;同一路径下的所有会话可读写。'
|
|
305
|
+
if (scope.startsWith('session:')) return '会话作用域:仅本会话可见的过程记忆;换会话后经目录发现、不默认读取。'
|
|
306
|
+
return '自定义作用域:按名字聚合的跨会话记忆;创建时建议用 memory_save 的 scopeDescription 写明用途。'
|
|
307
|
+
}
|
|
308
|
+
|
|
295
309
|
/** 写路径遇到只读 scope 时的错误码('all' 不能写)。 */
|
|
296
310
|
export const SCOPE_NOT_WRITABLE = 'SCOPE_NOT_WRITABLE'
|
|
297
311
|
|
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
|
|
|
@@ -62,11 +62,12 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
|
|
|
62
62
|
|
|
63
63
|
/**
|
|
64
64
|
* 被可见域挡住、没有返回给模型的原因。
|
|
65
|
+
* - out-of-scope = 普通限制下「作用域不在范围内」,检索默认不自动弹审批(0.27);
|
|
65
66
|
* - rejected / timeout / unavailable / partial = 普通限制审批的结局;
|
|
66
67
|
* - absolute = 绝对限制下作用域根本不在范围内(0.23 起不再假装"不存在");
|
|
67
68
|
* - read-disabled = 作用域在范围内但「可读」开关被关掉(0.23)。
|
|
68
69
|
*/
|
|
69
|
-
|
|
70
|
+
|
|
70
71
|
|
|
71
72
|
|
|
72
73
|
|
|
@@ -461,6 +462,27 @@ function withheldReasonFor(
|
|
|
461
462
|
return 'read-disabled'
|
|
462
463
|
}
|
|
463
464
|
|
|
465
|
+
/**
|
|
466
|
+
* 0.27.0:普通限制下**没有发起审批**的范围外命中给模型的原因。
|
|
467
|
+
* 与 withheldReasonFor 的差别:「整个作用域不在范围内」(普通限制下本可审批、
|
|
468
|
+
* 现在只给索引)不再报成 read-disabled,而是 'out-of-scope';
|
|
469
|
+
* absolute / read-disabled / scope-disabled 照旧如实聚合。
|
|
470
|
+
*/
|
|
471
|
+
function outOfScopeReason(
|
|
472
|
+
decide ,
|
|
473
|
+
policy ,
|
|
474
|
+
denied ,
|
|
475
|
+
) {
|
|
476
|
+
if (denied.length === 0) return undefined
|
|
477
|
+
const reasons = denied.map((item) => readDenyReason(decide, policy, item.scope, item.kind))
|
|
478
|
+
if (reasons.includes('absolute')) return 'absolute'
|
|
479
|
+
if (reasons.includes('scope-disabled')) return 'scope-disabled'
|
|
480
|
+
// readDenyReason 把「SCOPE_NOT_IN_RANGE + 普通限制」也归成 read-disabled;
|
|
481
|
+
// 这里按"作用域是否在范围里"细分:不在范围 = out-of-scope(可申请)。
|
|
482
|
+
const hasOutOfScope = denied.some((item) => !scopeInPolicy(policy, item.scope))
|
|
483
|
+
return hasOutOfScope ? 'out-of-scope' : 'read-disabled'
|
|
484
|
+
}
|
|
485
|
+
|
|
464
486
|
/**
|
|
465
487
|
* memory_save 的 supersedes 参数说明(模型可见)。
|
|
466
488
|
* 检索 / 列表 / 摘要 / 规则索引里给的 id 都是 8 位短 id,而 supersedes 在 provider
|
|
@@ -973,6 +995,12 @@ function applySummaryDedupe(opts , exec , records
|
|
|
973
995
|
|
|
974
996
|
|
|
975
997
|
|
|
998
|
+
|
|
999
|
+
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
|
|
1003
|
+
|
|
976
1004
|
|
|
977
1005
|
|
|
978
1006
|
|
|
@@ -991,6 +1019,23 @@ function applySummaryDedupe(opts , exec , records
|
|
|
991
1019
|
|
|
992
1020
|
|
|
993
1021
|
|
|
1022
|
+
/**
|
|
1023
|
+
* 0.28.0:范围外命中的**作用域级**索引——只给「命中了哪个作用域、几条、这个作用域
|
|
1024
|
+
* 是干什么的」,**不再给条目摘要**。
|
|
1025
|
+
*
|
|
1026
|
+
* 为什么收窄:0.27.0 的条目索引会带短摘要,模型很容易把那段摘要当成「已经读到的东西」
|
|
1027
|
+
* 直接引用;而它其实是可见域外的内容。现在范围外只暴露作用域用途,模型据此判断
|
|
1028
|
+
* 要不要为某个作用域显式发起审批(requestApproval / memory_scopes request)。
|
|
1029
|
+
*/
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
|
|
1038
|
+
|
|
994
1039
|
|
|
995
1040
|
|
|
996
1041
|
|
|
@@ -998,12 +1043,35 @@ function applySummaryDedupe(opts , exec , records
|
|
|
998
1043
|
|
|
999
1044
|
|
|
1000
1045
|
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
|
|
1001
1051
|
|
|
1002
1052
|
|
|
1053
|
+
/**
|
|
1054
|
+
* 范围外清单要展示的作用域用途:优先取 provider 目录里手写过的 description,
|
|
1055
|
+
* 拿不到(旧 provider / 目录里没有这个 scope)时按类型给默认描述。
|
|
1056
|
+
*/
|
|
1057
|
+
function scopeDescriptions(service , agent ) {
|
|
1058
|
+
const out = new Map ()
|
|
1059
|
+
if (typeof service.scopeCatalog !== 'function') return out
|
|
1060
|
+
try {
|
|
1061
|
+
for (const entry of service.scopeCatalog(agent)) {
|
|
1062
|
+
const description = typeof entry.description === 'string' ? entry.description.trim() : ''
|
|
1063
|
+
if (description !== '') out.set(entry.scope, description)
|
|
1064
|
+
}
|
|
1065
|
+
} catch {
|
|
1066
|
+
// 目录是增强:拿不到就退回类型默认描述,不让检索因此失败。
|
|
1067
|
+
}
|
|
1068
|
+
return out
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1003
1071
|
export function memorySearchTool(opts ) {
|
|
1004
1072
|
return {
|
|
1005
1073
|
name: 'memory_search',
|
|
1006
|
-
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.',
|
|
1074
|
+
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.',
|
|
1007
1075
|
parameters: {
|
|
1008
1076
|
query: { type: 'string', required: true },
|
|
1009
1077
|
scope: { type: 'string', description: SCOPE_DESCRIPTION },
|
|
@@ -1011,6 +1079,7 @@ export function memorySearchTool(opts )
|
|
|
1011
1079
|
includeSuperseded: { type: 'boolean', description: '默认 false;true 时把已被 supersede 的旧版本也返回(历史核对)' },
|
|
1012
1080
|
summaryChars: { type: 'number', description: `每条摘要的字数上限(默认 ${DEFAULT_SUMMARY_CHARS},20~2000)` },
|
|
1013
1081
|
description: { type: 'string', description: DESCRIPTION_PARAM_DESCRIPTION },
|
|
1082
|
+
requestApproval: { type: 'boolean', description: '默认 false:范围外命中只返回作用域清单(scope + 条数 + 用途,不含任何记忆内容),不自动弹审批;true = 立即向用户发起审批,允许的条目并入 records。确认这些作用域的用途确实与本次任务相关后再传 true。' },
|
|
1014
1083
|
},
|
|
1015
1084
|
async execute(args , exec ) {
|
|
1016
1085
|
const service = svc(opts)
|
|
@@ -1059,8 +1128,12 @@ export function memorySearchTool(opts )
|
|
|
1059
1128
|
const outside = hits.filter((hit) => !recordAllowed(decide, policy, hit.record.scope, hit.record.kind))
|
|
1060
1129
|
// 只有“作用域不在范围里”才可能审批;absolute 与 read:false 都不 requestable。
|
|
1061
1130
|
const needsApproval = outside.filter((hit) => recordRequestable(decide, policy, hit.record.scope, hit.record.kind))
|
|
1131
|
+
// 0.27.0:普通限制下范围外命中**不再自动弹审批**。先把索引交给模型
|
|
1132
|
+
//(outOfScope 字段),要读正文时由 Agent 显式传 requestApproval=true。
|
|
1133
|
+
// 绝对限制 / 不可读 / 已关闭作用域照旧只进 withheld(它们没有审批路径)。
|
|
1134
|
+
const wantsApproval = args.requestApproval === true
|
|
1062
1135
|
let verdict
|
|
1063
|
-
if (needsApproval.length > 0 && level === 'normal') {
|
|
1136
|
+
if (needsApproval.length > 0 && level === 'normal' && wantsApproval) {
|
|
1064
1137
|
verdict = await askOutOfScope(
|
|
1065
1138
|
service,
|
|
1066
1139
|
agent,
|
|
@@ -1100,9 +1173,38 @@ export function memorySearchTool(opts )
|
|
|
1100
1173
|
if (denied > 0) {
|
|
1101
1174
|
const approvedIds = new Set(approvedOutside.map((hit) => hit.record.id))
|
|
1102
1175
|
const deniedRecords = outside.filter((hit) => !approvedIds.has(hit.record.id))
|
|
1103
|
-
|
|
1176
|
+
// 0.27.0:普通限制下没发起审批的范围外命中 = 'out-of-scope'(索引已在
|
|
1177
|
+
// outOfScope 字段给出);审批后的结局(partial/rejected/timeout)与
|
|
1178
|
+
// absolute / read-disabled / scope-disabled 沿用原口径。
|
|
1179
|
+
const reason = verdict !== undefined
|
|
1180
|
+
? withheldReasonFor(decide, policy, deniedRecords.map((hit) => ({ scope: hit.record.scope, kind: hit.record.kind })), verdict)
|
|
1181
|
+
: outOfScopeReason(decide, policy, deniedRecords.map((hit) => ({ scope: hit.record.scope, kind: hit.record.kind })))
|
|
1104
1182
|
if (reason !== undefined) result.withheld = { count: denied, reason }
|
|
1105
1183
|
}
|
|
1184
|
+
// 0.28.0:范围外清单(不含任何记忆内容)——命中了哪些作用域、各几条、
|
|
1185
|
+
// 这些作用域是干什么的。只在普通限制下给出:absolute 没有任何审批放行路径,
|
|
1186
|
+
// 清单只会引诱越权尝试。未发起审批(默认)或审批没全过时都会给出。
|
|
1187
|
+
// 条目摘要刻意**不再**返回:范围外的内容不进入上下文,避免模型把摘要当正文引用。
|
|
1188
|
+
const pendingOutside = outside.filter((hit) => !approvedOutsideIds.has(hit.record.id))
|
|
1189
|
+
if (pendingOutside.length > 0 && level === 'normal') {
|
|
1190
|
+
const requestableIds = new Set(needsApproval.map((hit) => hit.record.id))
|
|
1191
|
+
const byScope = new Map ()
|
|
1192
|
+
for (const hit of pendingOutside) {
|
|
1193
|
+
const entry = byScope.get(hit.record.scope) ?? { records: 0, requestable: false }
|
|
1194
|
+
entry.records += 1
|
|
1195
|
+
if (requestableIds.has(hit.record.id)) entry.requestable = true
|
|
1196
|
+
byScope.set(hit.record.scope, entry)
|
|
1197
|
+
}
|
|
1198
|
+
const descriptions = scopeDescriptions(service, agent)
|
|
1199
|
+
result.outOfScope = {
|
|
1200
|
+
scopes: [...byScope].map(([scope, entry]) => ({
|
|
1201
|
+
scope,
|
|
1202
|
+
records: entry.records,
|
|
1203
|
+
description: descriptions.get(scope) ?? defaultScopeDescription(scope),
|
|
1204
|
+
requestable: entry.requestable,
|
|
1205
|
+
})),
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1106
1208
|
if (wantedTopK !== undefined) result.records = result.records.slice(0, wantedTopK)
|
|
1107
1209
|
// 上下文去重:同一条记忆同版本已经出现过 → 只回 id,摘要让 memory_get 按需取。
|
|
1108
1210
|
applySummaryDedupe(opts, exec, result.records)
|
package/lib/tools/render.js
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
* 0.20.0:范围外命中被挡住时,把**真实原因**(用户拒绝 / 审批超时 / 无法审批)
|
|
9
9
|
* 写进渲染文本。
|
|
10
10
|
* 0.23.0:绝对限制与「不可读」开关也会被说明(不再假装"不存在"),并提示怎么调整。
|
|
11
|
+
* 0.27.0:普通限制下范围外命中不再自动弹审批 —— 检索结果带 outOfScope 清单
|
|
12
|
+
* (作用域聚合),审批改为 Agent 显式发起。
|
|
13
|
+
* 0.28.0:范围外清单只给作用域(名字 / 条数 / 用途),**不再给条目摘要**;清单与
|
|
14
|
+
* withheld 说明不再重复讲同一件事(「索引已列在下方」那句直接去掉)。
|
|
11
15
|
*/
|
|
12
16
|
|
|
13
17
|
import { scopeDisplayName } from '../provider/scope.js'
|
|
@@ -40,6 +44,8 @@ export function renderWithheldNote(withheld ) {
|
|
|
40
44
|
if (withheld === undefined || withheld.count <= 0) return ''
|
|
41
45
|
const count = withheld.count
|
|
42
46
|
switch (withheld.reason) {
|
|
47
|
+
case 'out-of-scope':
|
|
48
|
+
return `另有 ${count} 条记忆在本会话的可见域外(限制等级:普通限制),本次没有弹审批。要看正文需要显式发起:用 memory_search 带 requestApproval=true 重发同一次搜索(会弹审批),或用 memory_scopes(action="request") 申请该作用域的会话权限。`
|
|
43
49
|
case 'rejected':
|
|
44
50
|
return `另有 ${count} 条记忆在本会话的可见域外,用户拒绝了本次访问(限制等级:普通限制),未返回。如确实需要,请让用户调整「记忆范围」后重试。`
|
|
45
51
|
case 'timeout':
|
|
@@ -59,12 +65,48 @@ export function renderWithheldNote(withheld ) {
|
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
|
|
68
|
+
/**
|
|
69
|
+
* 范围外清单的尾部说明(0.27.0 / 0.28.0)。告诉模型这块**只有作用域**,不含任何
|
|
70
|
+
* 记忆内容;真要读时该走哪条路。
|
|
71
|
+
*/
|
|
72
|
+
export const OUT_OF_SCOPE_INDEX_NOTE =
|
|
73
|
+
'以上只列了作用域,不含任何记忆内容。需要正文时:用 memory_search 带 requestApproval=true 重新发起同一次搜索(会弹审批),或用 memory_scopes(action="request") 申请该作用域的本会话权限。'
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 范围外清单:每个作用域一行「名称(条数 · 可申请性):用途」。
|
|
77
|
+
* 刻意与可见域内的结果分开排版,并且**不出现任何条目 id / 摘要** ——
|
|
78
|
+
* 范围外的内容不该有机会被当成「已经读到的记忆」引用。
|
|
79
|
+
*/
|
|
80
|
+
function renderOutOfScopeIndex(
|
|
81
|
+
outOfScope ,
|
|
82
|
+
label ,
|
|
83
|
+
) {
|
|
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}`
|
|
91
|
+
})
|
|
92
|
+
const head = `范围外命中(只列作用域与用途,不含条目内容):${outOfScope.scopes.length} 个作用域 / ${total} 条`
|
|
93
|
+
return [head, ...lines, '', OUT_OF_SCOPE_INDEX_NOTE].join('\n')
|
|
94
|
+
}
|
|
95
|
+
|
|
62
96
|
/** 「[得分] id (作用域/类型 · 标签) 摘要」;没有任何一条命中时给明确空结果。 */
|
|
63
97
|
export function renderSearchResult(value , label ) {
|
|
64
|
-
const
|
|
98
|
+
const index = value.outOfScope !== undefined && value.outOfScope.scopes.length > 0
|
|
99
|
+
? renderOutOfScopeIndex(value.outOfScope, label)
|
|
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)
|
|
65
107
|
if (value.records.length === 0) {
|
|
66
|
-
if (note === '') return '没有找到相关记忆。'
|
|
67
|
-
return
|
|
108
|
+
if (note === '' && index === '') return '没有找到相关记忆。'
|
|
109
|
+
return [`没有找到可见范围内的相关记忆。`, index, note].filter((part) => part !== '').join('\n\n')
|
|
68
110
|
}
|
|
69
111
|
// 已出现过的条目(上下文去重)只回 id:不带作用域/类型/标签/摘要。
|
|
70
112
|
// 它们单独成块、与带摘要的条目之间空一行 —— 否则一串裸 id 看起来像上一条的
|
|
@@ -81,7 +123,7 @@ export function renderSearchResult(value , label )
|
|
|
81
123
|
shown.push(`[${hit.score.toFixed(3)}] ${shortId(hit.id)} (${meta}) ${hit.summary}`)
|
|
82
124
|
}
|
|
83
125
|
const body = [shown.join('\n'), bare.join('\n')].filter((part) => part !== '').join('\n\n')
|
|
84
|
-
const tail = [bare.length > 0 ? DEDUPE_NOTE : '', note].filter((part) => part !== '').join('\n\n')
|
|
126
|
+
const tail = [bare.length > 0 ? DEDUPE_NOTE : '', index, note].filter((part) => part !== '').join('\n\n')
|
|
85
127
|
return tail === '' ? body : `${body}\n\n${tail}`
|
|
86
128
|
}
|
|
87
129
|
|