@restregular/dsh-memory 0.27.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 -728
- package/README.md +647 -643
- package/lib/client.js +116 -4
- package/lib/inject/apply.js +1 -1
- package/lib/inject/policy.js +1 -1
- package/lib/panel.js +47 -8
- package/lib/tools/index.js +50 -29
- package/lib/tools/render.js +27 -16
- 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,7 +8,7 @@ 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
|
|
11
|
+
搜索命中作用域外记忆时默认**不弹审批**:结果里的 outOfScope 字段只列出命中的作用域——各几条、以及这些作用域的**用途描述**,不含任何记忆内容(也就没有摘要可引用)。确认某个作用域的用途确实与本次任务相关时,再用 memory_search 带 requestApproval=true 重新发起同一次搜索(这时才弹审批),或用 memory_scopes(action="request") 申请该作用域的本会话权限。
|
|
12
12
|
|
|
13
13
|
需要知道有哪些作用域、各自用来做什么、当前能不能读/写时,用 memory_scopes(action="list")。它只返回目录元信息(名字 / 用途 / 访问状态)与初始化记录正文,不返回普通记忆正文;范围外作用域在绝对限制下仍可发现,但正文(含初始化记录)永不可读、不可申请。
|
|
14
14
|
|
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/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
|
|
|
@@ -1020,19 +1020,20 @@ function applySummaryDedupe(opts , exec , records
|
|
|
1020
1020
|
|
|
1021
1021
|
|
|
1022
1022
|
/**
|
|
1023
|
-
* 0.
|
|
1024
|
-
*
|
|
1025
|
-
*
|
|
1023
|
+
* 0.28.0:范围外命中的**作用域级**索引——只给「命中了哪个作用域、几条、这个作用域
|
|
1024
|
+
* 是干什么的」,**不再给条目摘要**。
|
|
1025
|
+
*
|
|
1026
|
+
* 为什么收窄:0.27.0 的条目索引会带短摘要,模型很容易把那段摘要当成「已经读到的东西」
|
|
1027
|
+
* 直接引用;而它其实是可见域外的内容。现在范围外只暴露作用域用途,模型据此判断
|
|
1028
|
+
* 要不要为某个作用域显式发起审批(requestApproval / memory_scopes request)。
|
|
1026
1029
|
*/
|
|
1027
1030
|
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
1031
|
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
1032
|
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
|
|
1036
1037
|
|
|
1037
1038
|
|
|
1038
1039
|
|
|
@@ -1043,16 +1044,34 @@ function applySummaryDedupe(opts , exec , records
|
|
|
1043
1044
|
|
|
1044
1045
|
|
|
1045
1046
|
|
|
1046
|
-
|
|
1047
|
-
|
|
1047
|
+
|
|
1048
|
+
|
|
1048
1049
|
|
|
1049
|
-
|
|
1050
|
+
|
|
1050
1051
|
|
|
1051
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
|
+
|
|
1052
1071
|
export function memorySearchTool(opts ) {
|
|
1053
1072
|
return {
|
|
1054
1073
|
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
|
|
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.',
|
|
1056
1075
|
parameters: {
|
|
1057
1076
|
query: { type: 'string', required: true },
|
|
1058
1077
|
scope: { type: 'string', description: SCOPE_DESCRIPTION },
|
|
@@ -1060,7 +1079,7 @@ export function memorySearchTool(opts )
|
|
|
1060
1079
|
includeSuperseded: { type: 'boolean', description: '默认 false;true 时把已被 supersede 的旧版本也返回(历史核对)' },
|
|
1061
1080
|
summaryChars: { type: 'number', description: `每条摘要的字数上限(默认 ${DEFAULT_SUMMARY_CHARS},20~2000)` },
|
|
1062
1081
|
description: { type: 'string', description: DESCRIPTION_PARAM_DESCRIPTION },
|
|
1063
|
-
requestApproval: { type: 'boolean', description: '默认 false
|
|
1082
|
+
requestApproval: { type: 'boolean', description: '默认 false:范围外命中只返回作用域清单(scope + 条数 + 用途,不含任何记忆内容),不自动弹审批;true = 立即向用户发起审批,允许的条目并入 records。确认这些作用域的用途确实与本次任务相关后再传 true。' },
|
|
1064
1083
|
},
|
|
1065
1084
|
async execute(args , exec ) {
|
|
1066
1085
|
const service = svc(opts)
|
|
@@ -1162,25 +1181,27 @@ export function memorySearchTool(opts )
|
|
|
1162
1181
|
: outOfScopeReason(decide, policy, deniedRecords.map((hit) => ({ scope: hit.record.scope, kind: hit.record.kind })))
|
|
1163
1182
|
if (reason !== undefined) result.withheld = { count: denied, reason }
|
|
1164
1183
|
}
|
|
1165
|
-
// 0.
|
|
1166
|
-
//
|
|
1167
|
-
//
|
|
1184
|
+
// 0.28.0:范围外清单(不含任何记忆内容)——命中了哪些作用域、各几条、
|
|
1185
|
+
// 这些作用域是干什么的。只在普通限制下给出:absolute 没有任何审批放行路径,
|
|
1186
|
+
// 清单只会引诱越权尝试。未发起审批(默认)或审批没全过时都会给出。
|
|
1187
|
+
// 条目摘要刻意**不再**返回:范围外的内容不进入上下文,避免模型把摘要当正文引用。
|
|
1168
1188
|
const pendingOutside = outside.filter((hit) => !approvedOutsideIds.has(hit.record.id))
|
|
1169
1189
|
if (pendingOutside.length > 0 && level === 'normal') {
|
|
1170
|
-
const
|
|
1190
|
+
const requestableIds = new Set(needsApproval.map((hit) => hit.record.id))
|
|
1191
|
+
const byScope = new Map ()
|
|
1171
1192
|
for (const hit of pendingOutside) {
|
|
1172
|
-
|
|
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)
|
|
1173
1197
|
}
|
|
1198
|
+
const descriptions = scopeDescriptions(service, agent)
|
|
1174
1199
|
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),
|
|
1200
|
+
scopes: [...byScope].map(([scope, entry]) => ({
|
|
1201
|
+
scope,
|
|
1202
|
+
records: entry.records,
|
|
1203
|
+
description: descriptions.get(scope) ?? defaultScopeDescription(scope),
|
|
1204
|
+
requestable: entry.requestable,
|
|
1184
1205
|
})),
|
|
1185
1206
|
}
|
|
1186
1207
|
}
|
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')
|