@restregular/dsh-memory 0.31.0 → 0.33.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 +24 -0
- package/README.md +7 -4
- package/lib/client.js +310 -49
- package/lib/inject/apply.js +21 -4
- package/lib/inject/inject.js +3 -3
- package/lib/panel.js +273 -2
- package/lib/provider/apply.js +100 -0
- package/lib/provider/approval-audit.js +9 -1
- package/lib/provider/blocks.js +279 -0
- package/lib/provider/index.js +629 -29
- package/lib/provider/scope-archive.js +229 -0
- package/lib/tools/apply.js +6 -1
- package/lib/tools/index.js +217 -1
- package/lib/tools/render.js +50 -2
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 项目作用域的归档(0.32.0)。
|
|
3
|
+
*
|
|
4
|
+
* 0.30.0 已经实现了**会话作用域**的归档,但那是宿主生命周期驱动的:
|
|
5
|
+
* 会话在侧边栏里被归档 → `workspaceRegistry.archivedSessionIds` 里有它 →
|
|
6
|
+
* 记忆读写冻结,恢复会话后自动复原。项目作用域没有对应的宿主概念,
|
|
7
|
+
* 所以这里给它一份自己的持久化。
|
|
8
|
+
*
|
|
9
|
+
* 产品口径(与需求一一对应):
|
|
10
|
+
* - 只有 **session** 与 **project** 作用域可以归档;global / 自定义作用域不提供
|
|
11
|
+
* 归档按钮(它们的生命周期不属于某个工作区,归档只会变成"悄悄藏起来");
|
|
12
|
+
* - 项目作用域**绑定工作区**:工作区从侧边栏删除时,归档记录转为 dormant
|
|
13
|
+
* (记忆仍然保留、仍然不参与检索),工作区重新加入时归档自动解除 ——
|
|
14
|
+
* 也就是"删除时自动归档、重新加入时自动恢复";
|
|
15
|
+
* - 归档不等于屏蔽:归档是用户层面的生命周期状态,没有审批路径,解除也无需审批;
|
|
16
|
+
* 屏蔽是 Agent 主动降噪,两者在 UI 与拒绝原因上都分开。
|
|
17
|
+
*
|
|
18
|
+
* 文件:`<storeDir>/archive/project.json`,内容
|
|
19
|
+
* `{ version: 1, entries: ScopeArchiveEntry[], updatedAt }`
|
|
20
|
+
*
|
|
21
|
+
* 容错:读写失败一律退化为「没有归档」,绝不把异常抛回检索 / 注入链路。
|
|
22
|
+
*/
|
|
23
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
24
|
+
import { join } from 'node:path'
|
|
25
|
+
|
|
26
|
+
export const SCOPE_ARCHIVE_VERSION = 1
|
|
27
|
+
|
|
28
|
+
/** 可归档的作用域类型(session 由宿主驱动,这里只持久化 project)。 */
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
function cleanEntry(raw ) {
|
|
54
|
+
if (raw === null || typeof raw !== 'object') return undefined
|
|
55
|
+
const item = raw
|
|
56
|
+
const scope = typeof item.scope === 'string' ? item.scope.trim() : ''
|
|
57
|
+
if (scope === '' || !scope.startsWith('project:')) return undefined
|
|
58
|
+
const archivedAt = typeof item.archivedAt === 'number' && Number.isFinite(item.archivedAt) ? item.archivedAt : Date.now()
|
|
59
|
+
const createdBy = typeof item.createdBy === 'string' && item.createdBy !== '' ? item.createdBy : 'user'
|
|
60
|
+
const note = typeof item.note === 'string' && item.note.trim() !== '' ? item.note.trim() : undefined
|
|
61
|
+
return {
|
|
62
|
+
scope,
|
|
63
|
+
kind: 'project',
|
|
64
|
+
archivedAt,
|
|
65
|
+
createdBy,
|
|
66
|
+
...(note !== undefined ? { note } : {}),
|
|
67
|
+
...(item.dormant === true ? { dormant: true } : {}),
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
export class ScopeArchiveStore {
|
|
78
|
+
dir
|
|
79
|
+
file
|
|
80
|
+
warn
|
|
81
|
+
/** undefined = 还没读过盘。 */
|
|
82
|
+
cache
|
|
83
|
+
|
|
84
|
+
constructor(options ) {
|
|
85
|
+
this.dir = join(options.root, 'archive')
|
|
86
|
+
this.file = join(this.dir, 'project.json')
|
|
87
|
+
this.warn = options.warn ?? ((message) => console.warn(message))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 全部归档记录(拷贝)。 */
|
|
91
|
+
list() {
|
|
92
|
+
if (this.cache !== undefined) return this.cache.map((entry) => ({ ...entry }))
|
|
93
|
+
let entries = []
|
|
94
|
+
try {
|
|
95
|
+
if (existsSync(this.file)) {
|
|
96
|
+
const parsed = JSON.parse(readFileSync(this.file, 'utf8'))
|
|
97
|
+
if (Array.isArray(parsed?.entries)) {
|
|
98
|
+
for (const raw of parsed.entries) {
|
|
99
|
+
const entry = cleanEntry(raw)
|
|
100
|
+
if (entry === undefined) continue
|
|
101
|
+
if (entries.some((existing) => existing.scope === entry.scope)) continue
|
|
102
|
+
entries.push(entry)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} catch (error) {
|
|
107
|
+
// 损坏的归档文件不能让检索挂掉:当成"没有归档"(记忆恢复可见是更安全的方向,
|
|
108
|
+
// 用户能立刻看到并在面板里重新归档)。
|
|
109
|
+
this.warn(`[dsh-memory-local] 忽略损坏的作用域归档 ${this.file}: ${error instanceof Error ? error.message : String(error)}`)
|
|
110
|
+
entries = []
|
|
111
|
+
}
|
|
112
|
+
this.cache = entries
|
|
113
|
+
return entries.map((entry) => ({ ...entry }))
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
get(scope ) {
|
|
117
|
+
const key = typeof scope === 'string' ? scope.trim() : ''
|
|
118
|
+
if (key === '') return undefined
|
|
119
|
+
return this.list().find((entry) => entry.scope === key)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
has(scope ) {
|
|
123
|
+
return this.get(scope) !== undefined
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 归档一个项目作用域(幂等)。 */
|
|
127
|
+
archive(scope , by , note ) {
|
|
128
|
+
const key = typeof scope === 'string' ? scope.trim() : ''
|
|
129
|
+
if (key === '' || !key.startsWith('project:')) return undefined
|
|
130
|
+
const entries = this.list()
|
|
131
|
+
const existing = entries.find((entry) => entry.scope === key)
|
|
132
|
+
if (existing !== undefined) {
|
|
133
|
+
// 重新归档时清掉 dormant:用户显式要求归档,就按"已归档"处理。
|
|
134
|
+
if (existing.dormant !== true) return existing
|
|
135
|
+
existing.dormant = undefined
|
|
136
|
+
this.write(entries)
|
|
137
|
+
return { ...existing }
|
|
138
|
+
}
|
|
139
|
+
const entry = {
|
|
140
|
+
scope: key,
|
|
141
|
+
kind: 'project',
|
|
142
|
+
archivedAt: Date.now(),
|
|
143
|
+
createdBy: typeof by === 'string' && by !== '' ? by : 'user',
|
|
144
|
+
...(note !== undefined && note.trim() !== '' ? { note: note.trim() } : {}),
|
|
145
|
+
}
|
|
146
|
+
entries.push(entry)
|
|
147
|
+
this.write(entries)
|
|
148
|
+
return { ...entry }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 解除归档;返回是否真的删掉了一条。 */
|
|
152
|
+
restore(scope ) {
|
|
153
|
+
const key = typeof scope === 'string' ? scope.trim() : ''
|
|
154
|
+
if (key === '') return false
|
|
155
|
+
const entries = this.list()
|
|
156
|
+
const next = entries.filter((entry) => entry.scope !== key)
|
|
157
|
+
if (next.length === entries.length) return false
|
|
158
|
+
this.write(next)
|
|
159
|
+
return true
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 工作区存在性对账(0.32.0 的"删除自动归档 / 重新加入自动恢复"):
|
|
164
|
+
* - 归档时工作区还在 → 正常归档;
|
|
165
|
+
* - 工作区被删掉 → 记录标 dormant(记忆继续隐藏,但归档状态可被复原);
|
|
166
|
+
* - 工作区被重新加入(此前 dormant)→ **自动解除归档**,记忆立刻回到检索里。
|
|
167
|
+
*
|
|
168
|
+
* @param exists 判断项目作用域对应的工作区当前是否还在侧边栏里。
|
|
169
|
+
* @returns 本次对账里被自动解除归档的作用域。
|
|
170
|
+
*/
|
|
171
|
+
reconcile(exists ) {
|
|
172
|
+
const entries = this.list()
|
|
173
|
+
if (entries.length === 0) return []
|
|
174
|
+
const restored = []
|
|
175
|
+
let changed = false
|
|
176
|
+
const next = []
|
|
177
|
+
for (const entry of entries) {
|
|
178
|
+
let present = false
|
|
179
|
+
try {
|
|
180
|
+
present = exists(entry.scope) === true
|
|
181
|
+
} catch {
|
|
182
|
+
// 宿主服务不可用:保持现状,绝不在查询失败时改动用户的归档。
|
|
183
|
+
next.push(entry)
|
|
184
|
+
continue
|
|
185
|
+
}
|
|
186
|
+
if (!present) {
|
|
187
|
+
if (entry.dormant !== true) {
|
|
188
|
+
entry.dormant = true
|
|
189
|
+
changed = true
|
|
190
|
+
}
|
|
191
|
+
next.push(entry)
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
if (entry.dormant === true) {
|
|
195
|
+
// 工作区回来了:自动恢复。
|
|
196
|
+
restored.push(entry.scope)
|
|
197
|
+
changed = true
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
200
|
+
next.push(entry)
|
|
201
|
+
}
|
|
202
|
+
if (changed) this.write(next)
|
|
203
|
+
return restored
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** 清掉缓存(下一次读盘);测试 / 维护用。 */
|
|
207
|
+
invalidate() {
|
|
208
|
+
this.cache = undefined
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
write(entries ) {
|
|
212
|
+
this.cache = entries.map((entry) => ({ ...entry }))
|
|
213
|
+
try {
|
|
214
|
+
mkdirSync(this.dir, { recursive: true })
|
|
215
|
+
const tmp = `${this.file}.${process.pid}.${Date.now()}.tmp`
|
|
216
|
+
writeFileSync(tmp, `${JSON.stringify({
|
|
217
|
+
version: SCOPE_ARCHIVE_VERSION,
|
|
218
|
+
entries,
|
|
219
|
+
updatedAt: Date.now(),
|
|
220
|
+
}, null, 2)}\n`, 'utf8')
|
|
221
|
+
renameSync(tmp, this.file)
|
|
222
|
+
} catch (error) {
|
|
223
|
+
this.warn(`[dsh-memory-local] 无法写入作用域归档 ${this.file}: ${error instanceof Error ? error.message : String(error)}`)
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
//# sourceURL=source/memory-local/src/scope-archive.ts
|
package/lib/tools/apply.js
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
} from './index.js'
|
|
23
|
-
import { renderGetResult, renderListResult, renderScopesResult, renderSearchResult } from './render.js'
|
|
23
|
+
import { renderBlockResult, renderGetResult, renderListResult, renderScopesResult, renderSearchResult } from './render.js'
|
|
24
24
|
import { resolveCurrentScopes, scopeDisplayName, sessionShortId, } from '../provider/scope.js'
|
|
25
25
|
|
|
26
26
|
export const name = 'dsh-tool-memory'
|
|
@@ -166,6 +166,11 @@ export function apply(ctx , config = {}) {
|
|
|
166
166
|
const agent = (exec )?.agent
|
|
167
167
|
return renderGetResult(value, (scope) => renderScope(scope, agent))
|
|
168
168
|
})
|
|
169
|
+
|
|
170
|
+
register(tools.memory_block, (value, _args, exec) => {
|
|
171
|
+
const agent = (exec )?.agent
|
|
172
|
+
return renderBlockResult(value, (scope) => renderScope(scope, agent))
|
|
173
|
+
})
|
|
169
174
|
}
|
|
170
175
|
|
|
171
176
|
|
package/lib/tools/index.js
CHANGED
|
@@ -67,7 +67,7 @@ import { canonicalWorkspacePath, defaultScopeDescription, normalizeScopeInput, r
|
|
|
67
67
|
* - absolute = 绝对限制下作用域根本不在范围内(0.23 起不再假装"不存在");
|
|
68
68
|
* - read-disabled = 作用域在范围内但「可读」开关被关掉(0.23)。
|
|
69
69
|
*/
|
|
70
|
-
|
|
70
|
+
|
|
71
71
|
|
|
72
72
|
|
|
73
73
|
|
|
@@ -139,10 +139,43 @@ import { canonicalWorkspacePath, defaultScopeDescription, normalizeScopeInput, r
|
|
|
139
139
|
|
|
140
140
|
|
|
141
141
|
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
142
148
|
|
|
143
149
|
|
|
144
150
|
|
|
145
151
|
|
|
152
|
+
// ---------------------------------------------------------------- 屏蔽(0.32.0)
|
|
153
|
+
|
|
154
|
+
/** 屏蔽的作用层级:会话级只影响本会话;工作区级影响该工作区所有会话。 */
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
/** provider 一条屏蔽的视图。 */
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
/** `requestBlock` 的结果。 */
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
146
179
|
/** provider 统一授权判定的结果(0.24.0)。 */
|
|
147
180
|
|
|
148
181
|
|
|
@@ -250,6 +283,28 @@ import { canonicalWorkspacePath, defaultScopeDescription, normalizeScopeInput, r
|
|
|
250
283
|
|
|
251
284
|
|
|
252
285
|
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
253
308
|
|
|
254
309
|
|
|
255
310
|
|
|
@@ -1912,6 +1967,166 @@ export function memoryGetTool(opts )
|
|
|
1912
1967
|
}
|
|
1913
1968
|
}
|
|
1914
1969
|
|
|
1970
|
+
|
|
1971
|
+
|
|
1972
|
+
|
|
1973
|
+
|
|
1974
|
+
|
|
1975
|
+
|
|
1976
|
+
|
|
1977
|
+
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
|
|
1982
|
+
|
|
1983
|
+
|
|
1984
|
+
|
|
1985
|
+
|
|
1986
|
+
|
|
1987
|
+
|
|
1988
|
+
|
|
1989
|
+
|
|
1990
|
+
|
|
1991
|
+
|
|
1992
|
+
|
|
1993
|
+
|
|
1994
|
+
|
|
1995
|
+
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
|
|
1999
|
+
|
|
2000
|
+
|
|
2001
|
+
|
|
2002
|
+
|
|
2003
|
+
|
|
2004
|
+
|
|
2005
|
+
const BLOCK_NOTE_SESSION =
|
|
2006
|
+
'会话级屏蔽只影响本会话(含子 Agent),随时用 memory_block({action:"unblock"}) 解除。'
|
|
2007
|
+
const BLOCK_NOTE_WORKSPACE =
|
|
2008
|
+
'工作区级屏蔽影响该工作区所有会话(含历史与将来),需要用户审批;解除同样需要审批。'
|
|
2009
|
+
const BLOCK_NOTE_RULE =
|
|
2010
|
+
'目标里含「硬性规则」:规则被屏蔽必须由用户确认,已提交审批。'
|
|
2011
|
+
|
|
2012
|
+
/**
|
|
2013
|
+
* 屏蔽工具(0.32.0)。
|
|
2014
|
+
*
|
|
2015
|
+
* 用途:Agent 发现某段记忆与本轮任务无关时,主动让它**不再出现**在检索、注入
|
|
2016
|
+
* 与目录里,而不是每次都被噪声打断。与 `memory_forget` 的区别:屏蔽不删数据,
|
|
2017
|
+
* 解除后原样回来;与「可见域」的区别:屏蔽不改读写权限,只把它藏起来。
|
|
2018
|
+
*
|
|
2019
|
+
* 审批矩阵(产品口径,不能改):
|
|
2020
|
+
* - 按记录 id / 前缀屏蔽 → **不弹审批**;
|
|
2021
|
+
* - 按作用域屏蔽 → **弹审批**;
|
|
2022
|
+
* - 目标是规则(kind='rule')→ **一律弹审批**,即使按 id 屏蔽;
|
|
2023
|
+
* - 会话级 → 不弹审批(只影响自己);
|
|
2024
|
+
* - 工作区级 → **一律弹审批**(影响所有会话)。
|
|
2025
|
+
*/
|
|
2026
|
+
export function memoryBlockTool(opts ) {
|
|
2027
|
+
return {
|
|
2028
|
+
name: 'memory_block',
|
|
2029
|
+
description: 'Hide memories that are irrelevant to the current task so they stop appearing in search, injection and the scope catalog — without deleting anything. Blocking by record id/prefix needs no approval; blocking a whole scope, a rule (kind="rule"), or anything at workspace level raises an approval dialog for the user. action="unblock" removes a block; action="list" shows the active blocks. Blocks are recoverable: the data is never touched.',
|
|
2030
|
+
parameters: {
|
|
2031
|
+
action: {
|
|
2032
|
+
type: 'string',
|
|
2033
|
+
enum: ['block', 'unblock', 'list'],
|
|
2034
|
+
description: 'block = 屏蔽(默认);unblock = 解除;list = 查看当前屏蔽清单',
|
|
2035
|
+
},
|
|
2036
|
+
scope: {
|
|
2037
|
+
type: 'string',
|
|
2038
|
+
description: '要屏蔽 / 解除的作用域键(如 global / project:<路径> / session:<id> / 自定义名)。按作用域屏蔽需要用户审批。',
|
|
2039
|
+
},
|
|
2040
|
+
id: {
|
|
2041
|
+
type: 'string',
|
|
2042
|
+
description: '要屏蔽 / 解除的记录 id 或 8 位前缀。按 id 屏蔽不需要审批;但目标是规则时仍需审批。',
|
|
2043
|
+
},
|
|
2044
|
+
level: {
|
|
2045
|
+
type: 'string',
|
|
2046
|
+
enum: ['session', 'workspace'],
|
|
2047
|
+
description: 'session(默认)= 只影响本会话,不审批;workspace = 影响该工作区所有会话(含历史与将来),必须审批。',
|
|
2048
|
+
},
|
|
2049
|
+
reason: { type: 'string', description: '为什么屏蔽(一句话,展示在面板与审批卡上)' },
|
|
2050
|
+
description: { type: 'string', description: '本次调用的用途(普通限制下必填,审批弹窗会展示给用户)' },
|
|
2051
|
+
},
|
|
2052
|
+
async execute(args, exec) {
|
|
2053
|
+
const service = svc(opts)
|
|
2054
|
+
const agent = agentOf(exec)
|
|
2055
|
+
requireDescription(service, agent, args.description)
|
|
2056
|
+
const action = args.action ?? 'block'
|
|
2057
|
+
if (action === 'list') {
|
|
2058
|
+
const blocks = typeof service.listBlocks === 'function' ? service.listBlocks(agent) : []
|
|
2059
|
+
return { action, outcome: 'listed', blocks }
|
|
2060
|
+
}
|
|
2061
|
+
if (typeof service.requestBlock !== 'function') {
|
|
2062
|
+
throw toolError(new MemoryToolError('UNSUPPORTED', '当前 provider 不支持屏蔽(请升级 @restregular/dsh-memory 到 0.32.0+)'))
|
|
2063
|
+
}
|
|
2064
|
+
const level = args.level === 'workspace' ? 'workspace' : 'session'
|
|
2065
|
+
const scope = typeof args.scope === 'string' ? args.scope.trim() : ''
|
|
2066
|
+
const id = typeof args.id === 'string' ? args.id.trim() : ''
|
|
2067
|
+
if (action === 'unblock') {
|
|
2068
|
+
if (scope === '' && id === '') {
|
|
2069
|
+
throw toolError(new MemoryToolError('TARGET_REQUIRED', 'unblock 需要 scope 或 id'))
|
|
2070
|
+
}
|
|
2071
|
+
if (typeof service.unblock !== 'function') {
|
|
2072
|
+
throw toolError(new MemoryToolError('UNSUPPORTED', '当前 provider 不支持解除屏蔽(请升级 @restregular/dsh-memory 到 0.32.0+)'))
|
|
2073
|
+
}
|
|
2074
|
+
const results = service.unblock(agent, {
|
|
2075
|
+
...(args.level !== undefined ? { level } : {}),
|
|
2076
|
+
...(scope !== '' ? { scope } : {}),
|
|
2077
|
+
...(id !== '' ? { recordId: id } : {}),
|
|
2078
|
+
})
|
|
2079
|
+
const removed = results.reduce((sum, item) => sum + item.removed, 0)
|
|
2080
|
+
const blocks = typeof service.listBlocks === 'function' ? service.listBlocks(agent) : []
|
|
2081
|
+
return {
|
|
2082
|
+
action,
|
|
2083
|
+
outcome: removed > 0 ? 'unblocked' : 'unavailable',
|
|
2084
|
+
blocks,
|
|
2085
|
+
removed,
|
|
2086
|
+
...(removed === 0 ? { note: '没有找到匹配的屏蔽记录(可能已经被解除)。' } : {}),
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
if (scope === '' && id === '') {
|
|
2090
|
+
throw toolError(new MemoryToolError('TARGET_REQUIRED', 'block 需要 scope 或 id:按 id 屏蔽不需要审批,按作用域屏蔽会弹审批'))
|
|
2091
|
+
}
|
|
2092
|
+
try {
|
|
2093
|
+
const result = await service.requestBlock(agent, {
|
|
2094
|
+
...(scope !== '' ? { scope } : {}),
|
|
2095
|
+
...(id !== '' ? { id } : {}),
|
|
2096
|
+
level,
|
|
2097
|
+
...(args.reason !== undefined ? { reason: args.reason } : {}),
|
|
2098
|
+
...(args.description !== undefined ? { description: args.description } : {}),
|
|
2099
|
+
tool: 'memory_block',
|
|
2100
|
+
...((exec )?.signal !== undefined
|
|
2101
|
+
? { signal: (exec ).signal }
|
|
2102
|
+
: {}),
|
|
2103
|
+
})
|
|
2104
|
+
const notes = []
|
|
2105
|
+
if (result.outcome === 'blocked') {
|
|
2106
|
+
notes.push(level === 'workspace' ? BLOCK_NOTE_WORKSPACE : BLOCK_NOTE_SESSION)
|
|
2107
|
+
if (result.containsRule === true) notes.push(BLOCK_NOTE_RULE)
|
|
2108
|
+
} else if (result.outcome === 'rejected') {
|
|
2109
|
+
notes.push('用户拒绝了这次屏蔽;记忆保持原样。')
|
|
2110
|
+
} else if (result.outcome === 'timeout') {
|
|
2111
|
+
notes.push('审批超时,屏蔽没有生效;记忆保持原样。')
|
|
2112
|
+
} else {
|
|
2113
|
+
notes.push('当前环境无法发起屏蔽(缺少会话或工作区上下文)。')
|
|
2114
|
+
}
|
|
2115
|
+
return {
|
|
2116
|
+
action,
|
|
2117
|
+
outcome: result.outcome,
|
|
2118
|
+
blocks: result.blocks,
|
|
2119
|
+
...(result.records.length > 0 ? { records: result.records } : {}),
|
|
2120
|
+
...(result.containsRule === true ? { containsRule: true } : {}),
|
|
2121
|
+
note: notes.join(' '),
|
|
2122
|
+
}
|
|
2123
|
+
} catch (error) {
|
|
2124
|
+
throw toolError(error)
|
|
2125
|
+
}
|
|
2126
|
+
},
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
|
|
1915
2130
|
export function memoryTools(opts ) {
|
|
1916
2131
|
return {
|
|
1917
2132
|
memory_save: memorySaveTool(opts),
|
|
@@ -1920,6 +2135,7 @@ export function memoryTools(opts ) {
|
|
|
1920
2135
|
memory_list: memoryListTool(opts),
|
|
1921
2136
|
memory_scopes: memoryScopesTool(opts),
|
|
1922
2137
|
memory_get: memoryGetTool(opts),
|
|
2138
|
+
memory_block: memoryBlockTool(opts),
|
|
1923
2139
|
}
|
|
1924
2140
|
}
|
|
1925
2141
|
|
package/lib/tools/render.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* 0.28.0:范围外清单只给作用域(名字 / 条数 / 用途),**不再给条目摘要**;清单与
|
|
14
14
|
* withheld 说明不再重复讲同一件事(「索引已列在下方」那句直接去掉)。
|
|
15
15
|
*/
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
import { scopeDisplayName } from '../provider/scope.js'
|
|
18
18
|
|
|
19
19
|
/** 把 scope 键渲染成展示文案(宿主能查到会话标题时用它,否则用短 id)。 */
|
|
@@ -60,6 +60,8 @@ export function renderWithheldNote(withheld ) {
|
|
|
60
60
|
return `另有 ${count} 条记忆所在的作用域在本会话记忆范围里被标记为「不可读」(可读/可写按钮),未返回。如确实需要,请让用户打开该作用域的「可读」状态后重试。`
|
|
61
61
|
case 'scope-disabled':
|
|
62
62
|
return `另有 ${count} 条记忆所在的作用域已被配置关闭(global / project / session / custom 开关),未返回。如确实需要,请让用户打开对应类型的作用域。`
|
|
63
|
+
case 'blocked':
|
|
64
|
+
return `另有 ${count} 条记忆被你主动屏蔽了(会话级或工作区级),未返回。要恢复请用 memory_block({action:"unblock", ...}) 解除,或用 memory_block({action:"list"}) 看当前清单。`
|
|
63
65
|
default:
|
|
64
66
|
return ''
|
|
65
67
|
}
|
|
@@ -208,7 +210,12 @@ export function renderScopesResult(value , label )
|
|
|
208
210
|
if (item.records !== undefined) meta.push(`${item.records} 条`)
|
|
209
211
|
if (item.archived === true) {
|
|
210
212
|
// 0.30.0:归档不是权限拒绝,而是会话生命周期 —— 数据仍在,恢复会话即复原。
|
|
211
|
-
meta.push(
|
|
213
|
+
meta.push(item.archivedKind === 'project'
|
|
214
|
+
? '已归档 · 面板里解除归档即恢复'
|
|
215
|
+
: '已归档 · 恢复会话后自动恢复读写')
|
|
216
|
+
} else if (item.blocked === true) {
|
|
217
|
+
// 0.32.0:屏蔽不是权限拒绝,是 Agent 主动降噪 —— 数据仍在,解除后立刻复原。
|
|
218
|
+
meta.push(`已屏蔽(${item.blockedLevel === 'workspace' ? '工作区级' : '会话级'})· 解除后立刻复原`)
|
|
212
219
|
} else if (item.inScope) {
|
|
213
220
|
meta.push(item.accessStatus === 'default'
|
|
214
221
|
? '范围内'
|
|
@@ -289,5 +296,46 @@ export function renderGetResult(value , label ) {
|
|
|
289
296
|
return tail === '' ? body : `${body}\n\n${tail}`
|
|
290
297
|
}
|
|
291
298
|
|
|
299
|
+
/**
|
|
300
|
+
* memory_block 的渲染(0.32.0)。让模型一眼看清:屏蔽生效在哪个层级、影响什么、
|
|
301
|
+
* 怎么解除 —— 屏蔽是 Agent 自己做的降噪动作,它必须知道这是可逆的。
|
|
302
|
+
*/
|
|
303
|
+
export function renderBlockResult(value , label ) {
|
|
304
|
+
const levelText = (level ) => level === 'workspace' ? '工作区级(影响该工作区所有会话)' : '会话级(只影响本会话)'
|
|
305
|
+
const describe = (block ) => {
|
|
306
|
+
const target = block.scope !== undefined
|
|
307
|
+
? label(block.scope)
|
|
308
|
+
: block.recordId !== undefined
|
|
309
|
+
? `记录 ${shortId(block.recordId)}${block.recordKind !== undefined ? `(${block.recordKind})` : ''}`
|
|
310
|
+
: `前缀 ${block.prefix ?? '?'}`
|
|
311
|
+
return `- ${target} · ${levelText(block.level)}${block.reason !== undefined ? ` · ${block.reason}` : ''}`
|
|
312
|
+
}
|
|
313
|
+
if (value.action === 'list') {
|
|
314
|
+
if (value.blocks.length === 0) return '当前没有屏蔽任何记忆。'
|
|
315
|
+
return `当前屏蔽(${value.blocks.length} 条):\n${value.blocks.map(describe).join('\n')}`
|
|
316
|
+
}
|
|
317
|
+
if (value.action === 'unblock') {
|
|
318
|
+
if (value.outcome !== 'unblocked') return value.note ?? '没有找到匹配的屏蔽记录。'
|
|
319
|
+
const rest = value.blocks.length === 0 ? '现在没有生效的屏蔽。' : `剩余 ${value.blocks.length} 条屏蔽:\n${value.blocks.map(describe).join('\n')}`
|
|
320
|
+
return `已解除 ${value.removed ?? 0} 条屏蔽。${rest}`
|
|
321
|
+
}
|
|
322
|
+
if (value.outcome !== 'blocked') {
|
|
323
|
+
return value.note ?? (value.outcome === 'rejected'
|
|
324
|
+
? '用户拒绝了这次屏蔽;记忆保持原样。'
|
|
325
|
+
: value.outcome === 'timeout'
|
|
326
|
+
? '审批超时,屏蔽没有生效。'
|
|
327
|
+
: '当前环境无法发起屏蔽。')
|
|
328
|
+
}
|
|
329
|
+
const head = value.blocks.length === 1
|
|
330
|
+
? `已屏蔽 ${describe(value.blocks[0] ).slice(2)}。`
|
|
331
|
+
: `已屏蔽 ${value.blocks.length} 条:\n${value.blocks.map(describe).join('\n')}`
|
|
332
|
+
const records = value.records !== undefined && value.records.length > 0
|
|
333
|
+
? `\n涉及记录:${value.records.map((record) => `[${shortId(record.id)}] ${label(record.scope)}/${record.kind}`).join('、')}`
|
|
334
|
+
: ''
|
|
335
|
+
const note = value.note !== undefined ? `\n${value.note}` : ''
|
|
336
|
+
// 屏蔽是「藏起来」而不是删除:这句话必须出现,否则模型容易把屏蔽当成不可逆操作而不敢用。
|
|
337
|
+
return `${head}${records}${note}\n(屏蔽不删除任何记忆,数据原样保留;解除后立刻复原。)`
|
|
338
|
+
}
|
|
339
|
+
|
|
292
340
|
|
|
293
341
|
//# sourceURL=source/tool-memory/src/render.ts
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "Memory System",
|
|
4
|
-
"description": "Local memory system: automatic recall, hard rules,
|
|
4
|
+
"description": "Local memory system: automatic recall, hard rules, seven memory tools (including proactive blocking and scope archiving), JSONL storage and local vector search."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED