@restregular/dsh-memory 0.9.6 → 0.9.8
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/README.md +23 -5
- package/lib/client.js +217 -58
- package/lib/inject/apply.js +533 -430
- package/lib/inject/audit.js +1 -1
- package/lib/inject/signals.js +393 -173
- package/lib/provider/index.js +5 -1
- package/lib/provider/inject-settings.js +59 -0
- package/package.json +1 -1
package/lib/inject/audit.js
CHANGED
package/lib/inject/signals.js
CHANGED
|
@@ -1,176 +1,396 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 行为信号 → 自动召回查询。
|
|
3
|
-
*
|
|
4
|
-
* 只消费宿主**公开**的可观测信号:
|
|
5
|
-
* - session/event 的 `tool/call`(name + arguments)
|
|
6
|
-
* - session/event 的 `tool/result`(error / message.isError)
|
|
7
|
-
* - compaction 事件(上下文重建)
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
]
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 行为信号 → 自动召回查询。
|
|
3
|
+
*
|
|
4
|
+
* 只消费宿主**公开**的可观测信号:
|
|
5
|
+
* - session/event 的 `tool/call`(name + arguments,含工具自带的 description)
|
|
6
|
+
* - session/event 的 `tool/result`(error / message.isError)
|
|
7
|
+
* - compaction 事件(上下文重建)
|
|
8
|
+
*
|
|
9
|
+
* “模式”就是一组标签(release / deploy / database / credentials ...),
|
|
10
|
+
* 由设置提供、来自记忆里实际使用的标签;工具名/description/参数命中任一标签
|
|
11
|
+
* 就触发一次定向召回。失败调用始终触发,并优先用 description 作为查询。
|
|
12
|
+
* 不读取隐藏思维链,也不把原始参数写进审计。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
const DEFAULT_MAX_QUERY = 240
|
|
51
|
+
|
|
52
|
+
const RELEASE_TAGS = new Set(['release', 'publish', 'push', '发布', '推送'])
|
|
53
|
+
const DEPLOY_TAGS = new Set([
|
|
54
|
+
'deploy', 'deployment', 'rollback', 'restore', 'backup',
|
|
55
|
+
'database', 'migration', 'migrate', 'schema', 'production', 'prod',
|
|
56
|
+
'部署', '回滚', '恢复', '备份', '数据库', '迁移', '生产',
|
|
57
|
+
])
|
|
58
|
+
const SPECIFIC_TAGS = new Set([
|
|
59
|
+
'database', 'migration', 'migrate', 'schema', 'credentials', 'credential',
|
|
60
|
+
'auth', 'token', 'secret', 'permission', 'security', 'destructive', 'drop', 'truncate',
|
|
61
|
+
'数据库', '迁移', '凭据', '认证', '权限', '安全', '删除', '清空',
|
|
62
|
+
])
|
|
63
|
+
|
|
64
|
+
function truncate(value , max ) {
|
|
65
|
+
const limit = Math.max(20, max ?? DEFAULT_MAX_QUERY)
|
|
66
|
+
return value.replace(/\s+/g, ' ').trim().slice(0, limit)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** 中英标签的最小同义词表:记忆标签常是英文,Agent 文本常是中文。 */
|
|
70
|
+
const TAG_ALIASES = {
|
|
71
|
+
release: ['发布'],
|
|
72
|
+
publish: ['发布', '推送'],
|
|
73
|
+
deploy: ['部署'],
|
|
74
|
+
deployment: ['部署'],
|
|
75
|
+
database: ['数据库'],
|
|
76
|
+
migration: ['迁移'],
|
|
77
|
+
migrate: ['迁移'],
|
|
78
|
+
schema: ['结构', '迁移'],
|
|
79
|
+
credentials: ['凭据'],
|
|
80
|
+
credential: ['凭据'],
|
|
81
|
+
auth: ['认证'],
|
|
82
|
+
permission: ['权限'],
|
|
83
|
+
security: ['安全'],
|
|
84
|
+
destructive: ['删除', '清空'],
|
|
85
|
+
delete: ['删除'],
|
|
86
|
+
drop: ['删除'],
|
|
87
|
+
truncate: ['清空'],
|
|
88
|
+
backup: ['备份'],
|
|
89
|
+
restore: ['恢复'],
|
|
90
|
+
rollback: ['回滚'],
|
|
91
|
+
install: ['安装'],
|
|
92
|
+
upgrade: ['升级'],
|
|
93
|
+
production: ['生产'],
|
|
94
|
+
server: ['服务器'],
|
|
95
|
+
network: ['网络'],
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 标签及其同义词(双向:英文标签匹配中文文本,中文标签也匹配英文文本)。 */
|
|
99
|
+
function aliasesFor(tag ) {
|
|
100
|
+
const needle = tag.trim().toLowerCase()
|
|
101
|
+
const out = new Set ([needle])
|
|
102
|
+
for (const alias of TAG_ALIASES[needle] ?? []) out.add(alias.toLowerCase())
|
|
103
|
+
for (const [key, aliases] of Object.entries(TAG_ALIASES)) {
|
|
104
|
+
if (aliases.some((alias) => alias.toLowerCase() === needle)) out.add(key)
|
|
105
|
+
}
|
|
106
|
+
return [...out]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 标签是否出现在文本里。ASCII 标签按词边界匹配并容忍复数 s;中文按子串。 */
|
|
110
|
+
export function textHasTag(text , tag ) {
|
|
111
|
+
const haystack = text.toLowerCase()
|
|
112
|
+
for (const needle of aliasesFor(tag)) {
|
|
113
|
+
if (needle === '') continue
|
|
114
|
+
if (/^[a-z0-9][a-z0-9_-]*$/.test(needle)) {
|
|
115
|
+
const escaped = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
116
|
+
if (new RegExp(`(^|[^a-z0-9_])${escaped}s?(?=$|[^a-z0-9_])`).test(haystack)) return true
|
|
117
|
+
} else if (haystack.includes(needle)) {
|
|
118
|
+
return true
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return false
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 工具参数里的 description。很多工具(ops_bash / workflow / run_code 等)要求
|
|
126
|
+
* 模型写一句 "这个命令在干什么",它比命令本身更适合做记忆检索查询。
|
|
127
|
+
* 解析失败或没有该字段时返回 undefined。
|
|
128
|
+
*/
|
|
129
|
+
export function toolCallDescription(argumentsText ) {
|
|
130
|
+
let value = argumentsText
|
|
131
|
+
if (typeof argumentsText === 'string') {
|
|
132
|
+
const text = argumentsText.trim()
|
|
133
|
+
if (text === '') return undefined
|
|
134
|
+
try { value = JSON.parse(text) } catch { return undefined }
|
|
135
|
+
}
|
|
136
|
+
if (!value || typeof value !== 'object') return undefined
|
|
137
|
+
const description = (value ).description
|
|
138
|
+
if (typeof description !== 'string') return undefined
|
|
139
|
+
const trimmed = description.replace(/\s+/g, ' ').trim()
|
|
140
|
+
return trimmed === '' ? undefined : trimmed
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 稳定短 hash:让不同失败描述各自触发一次召回,又不在状态里保存原文。 */
|
|
144
|
+
function hashKey(value ) {
|
|
145
|
+
let hash = 2166136261
|
|
146
|
+
for (let i = 0; i < value.length; i++) {
|
|
147
|
+
hash ^= value.charCodeAt(i)
|
|
148
|
+
hash = Math.imul(hash, 16777619)
|
|
149
|
+
}
|
|
150
|
+
return (hash >>> 0).toString(36)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 从用户可见文本推断任务阶段;不确定就返回 unknown,不伪造。 */
|
|
154
|
+
export function detectPhase(text ) {
|
|
155
|
+
const value = String(text ?? '').toLowerCase()
|
|
156
|
+
if (/(?:npm|pnpm|yarn)\s+(?:publish|stage)|git\s+push|\brelease\b|\bpublish\b|发布|推送/.test(value)) return 'release'
|
|
157
|
+
if (/(?:deploy|rollout|kubectl|terraform\s+apply|部署)/.test(value)) return 'deploy'
|
|
158
|
+
if (/(?:implement|refactor|fix|add)\b|开发|实现|修复|新增|重构|添加/.test(value)) return 'development'
|
|
159
|
+
return 'unknown'
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function phaseForTag(tag , fallback ) {
|
|
163
|
+
const value = tag.toLowerCase()
|
|
164
|
+
if (RELEASE_TAGS.has(value)) return 'release'
|
|
165
|
+
if (DEPLOY_TAGS.has(value)) return 'deploy'
|
|
166
|
+
return fallback
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** 同一段文本命中多个标签时,优先保留更具体的语义(release > database > deploy > 普通)。 */
|
|
170
|
+
function tagPriority(tag ) {
|
|
171
|
+
const value = tag.toLowerCase()
|
|
172
|
+
if (RELEASE_TAGS.has(value)) return 4
|
|
173
|
+
if (SPECIFIC_TAGS.has(value)) return 3
|
|
174
|
+
if (DEPLOY_TAGS.has(value)) return 2
|
|
175
|
+
return 1
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 工具名 + description + 参数是否命中某个可配置触发标签。
|
|
180
|
+
* 命中后用 description(若开启)或标签本身作为检索查询。
|
|
181
|
+
*/
|
|
182
|
+
export function highRiskSignal(tool , argumentsText , options = {}) {
|
|
183
|
+
const tags = options.tags ?? []
|
|
184
|
+
const description = options.useToolDescription === false ? undefined : options.description
|
|
185
|
+
const haystack = `${tool} ${description ?? ''} ${argumentsText}`
|
|
186
|
+
const ordered = [...tags]
|
|
187
|
+
.map((tag, index) => ({ tag, index, priority: tagPriority(tag) }))
|
|
188
|
+
.sort((a, b) => b.priority - a.priority || a.index - b.index)
|
|
189
|
+
for (const { tag } of ordered) {
|
|
190
|
+
if (!textHasTag(haystack, tag)) continue
|
|
191
|
+
return {
|
|
192
|
+
key: `auto-tag:${tag.toLowerCase()}`,
|
|
193
|
+
reason: 'before_high_risk_tool',
|
|
194
|
+
query: truncate(description ?? tag, options.maxQueryChars),
|
|
195
|
+
phase: phaseForTag(tag, 'unknown'),
|
|
196
|
+
tool,
|
|
197
|
+
tag,
|
|
198
|
+
source: 'tool',
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return undefined
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 任意可见文本(assistant 输出、todo、命令、goal)命中记忆标签时的召回信号。
|
|
206
|
+
* 查询 = 标签 + 命中位置附近的一小段文本,既保留标签的精确性,也给检索一点上下文。
|
|
207
|
+
*/
|
|
208
|
+
export function textTagSignal(
|
|
209
|
+
text ,
|
|
210
|
+
options = {},
|
|
211
|
+
) {
|
|
212
|
+
const tags = options.tags ?? []
|
|
213
|
+
const body = String(text ?? '').replace(/\s+/g, ' ').trim()
|
|
214
|
+
if (body === '') return undefined
|
|
215
|
+
const ordered = [...tags]
|
|
216
|
+
.map((tag, index) => ({ tag, index, priority: tagPriority(tag) }))
|
|
217
|
+
.sort((a, b) => b.priority - a.priority || a.index - b.index)
|
|
218
|
+
for (const { tag } of ordered) {
|
|
219
|
+
if (!textHasTag(body, tag)) continue
|
|
220
|
+
const lower = body.toLowerCase()
|
|
221
|
+
const at = lower.indexOf(tag.toLowerCase())
|
|
222
|
+
const start = Math.max(0, at - 80)
|
|
223
|
+
const snippet = body.slice(start, Math.min(body.length, at + tag.length + 120))
|
|
224
|
+
const source = options.source ?? 'text'
|
|
225
|
+
return {
|
|
226
|
+
key: `text-tag:${source}:${tag.toLowerCase()}`,
|
|
227
|
+
reason: 'text_tag_match',
|
|
228
|
+
query: truncate(`${tag} ${snippet}`, options.maxQueryChars),
|
|
229
|
+
phase: phaseForTag(tag, 'unknown'),
|
|
230
|
+
tag,
|
|
231
|
+
source,
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return undefined
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** 从 assistant message 的 content 里取**可见文本**块;不读 reasoning / 隐藏 CoT。 */
|
|
238
|
+
export function visibleTextContent(content ) {
|
|
239
|
+
if (!Array.isArray(content)) return ''
|
|
240
|
+
return content
|
|
241
|
+
.filter((block) => block && typeof block === 'object'
|
|
242
|
+
&& (block ).type === 'text'
|
|
243
|
+
&& typeof (block ).text === 'string')
|
|
244
|
+
.map((block) => (block ).text)
|
|
245
|
+
.join('\n')
|
|
246
|
+
.trim()
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** `assistant/message` 的可见文本。 */
|
|
250
|
+
export function assistantVisibleText(event ) {
|
|
251
|
+
const data = (event )?.data
|
|
252
|
+
const message = data?.message
|
|
253
|
+
const text = visibleTextContent(message?.content)
|
|
254
|
+
return text === '' ? undefined : text
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** `todo/write` 的任务清单文本。 */
|
|
258
|
+
export function todoText(event ) {
|
|
259
|
+
const data = (event )?.data
|
|
260
|
+
const todos = data?.todos
|
|
261
|
+
if (!Array.isArray(todos)) return undefined
|
|
262
|
+
const text = todos
|
|
263
|
+
.map((todo) => (todo && typeof todo === 'object' ? (todo ).content : undefined))
|
|
264
|
+
.filter((content) => typeof content === 'string' && content.trim() !== '')
|
|
265
|
+
.join('\n')
|
|
266
|
+
.trim()
|
|
267
|
+
return text === '' ? undefined : text
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** `command/run` 的命令名与参数。 */
|
|
271
|
+
export function commandText(event ) {
|
|
272
|
+
const data = (event )?.data
|
|
273
|
+
const name = typeof data?.name === 'string' ? data.name : ''
|
|
274
|
+
const args = typeof data?.args === 'string' ? data.args : ''
|
|
275
|
+
const text = `${name} ${args}`.trim()
|
|
276
|
+
return text === '' ? undefined : text
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** `goal/change` 的目标文本(只取明确的 objective/title/description)。 */
|
|
280
|
+
export function goalText(event ) {
|
|
281
|
+
const data = (event )?.data
|
|
282
|
+
const goal = data?.goal
|
|
283
|
+
if (!goal || typeof goal !== 'object') return undefined
|
|
284
|
+
const values = [goal.objective, goal.title, goal.description]
|
|
285
|
+
.filter((value) => typeof value === 'string' && value.trim() !== '')
|
|
286
|
+
return values.length > 0 ? values.join('\n') : undefined
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* 工具失败分类。失败时**优先用工具调用自己的 description** 做检索查询;
|
|
291
|
+
* 没有 description 才退回错误类别关键词。只根据 code/消息里的类别词判断,
|
|
292
|
+
* 不保存原始消息。
|
|
293
|
+
*/
|
|
294
|
+
export function failureSignal(
|
|
295
|
+
tool ,
|
|
296
|
+
code ,
|
|
297
|
+
message ,
|
|
298
|
+
options = {},
|
|
299
|
+
) {
|
|
300
|
+
const haystack = `${code ?? ''} ${message ?? ''}`.toLowerCase()
|
|
301
|
+
let errorClass = 'TOOL_FAILED'
|
|
302
|
+
let fallbackQuery = `failure diagnostics ${tool ?? 'tool'}`
|
|
303
|
+
let phase = 'unknown'
|
|
304
|
+
if (/credential|missing.*token|no.*token|unauthori[sz]ed|e401|401|auth(?:entication)?\s+required/.test(haystack)) {
|
|
305
|
+
errorClass = 'MISSING_CREDENTIAL'
|
|
306
|
+
fallbackQuery = 'credentials npm publish devcred token missing'
|
|
307
|
+
phase = 'release'
|
|
308
|
+
} else if (/permission|denied|forbidden|e403|403|eacces|eperm/.test(haystack)) {
|
|
309
|
+
errorClass = 'PERMISSION_DENIED'
|
|
310
|
+
fallbackQuery = 'permission denied sandbox approval credentials'
|
|
311
|
+
} else if (/timeout|timed\s*out|etimedout/.test(haystack)) {
|
|
312
|
+
errorClass = 'TIMEOUT'
|
|
313
|
+
fallbackQuery = 'timeout retry long running command'
|
|
314
|
+
} else if (/abort|cancel|interrupt/.test(haystack)) {
|
|
315
|
+
errorClass = 'ABORTED_UNKNOWN'
|
|
316
|
+
fallbackQuery = 'aborted command diagnosis retry'
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const description = options.description?.replace(/\s+/g, ' ').trim()
|
|
320
|
+
const extra = errorClass === 'MISSING_CREDENTIAL' ? 'credentials token auth'
|
|
321
|
+
: errorClass === 'PERMISSION_DENIED' ? 'permission sandbox approval'
|
|
322
|
+
: errorClass === 'TIMEOUT' ? 'timeout retry'
|
|
323
|
+
: errorClass === 'ABORTED_UNKNOWN' ? 'aborted retry diagnosis'
|
|
324
|
+
: 'failure diagnostics'
|
|
325
|
+
const query = truncate(description !== undefined && description !== ''
|
|
326
|
+
? `${description} ${extra}`
|
|
327
|
+
: fallbackQuery, options.maxQueryChars)
|
|
328
|
+
const key = `tool-failure:${tool ?? 'unknown'}:${errorClass}:${hashKey(description ?? message ?? '')}`
|
|
329
|
+
return {
|
|
330
|
+
key,
|
|
331
|
+
reason: 'tool_failure',
|
|
332
|
+
query,
|
|
333
|
+
phase,
|
|
334
|
+
...(tool !== undefined ? { tool } : {}),
|
|
335
|
+
errorClass,
|
|
336
|
+
source: 'failure',
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** 从一个 tool/result 事件里取错误信息(已做防御性读取)。 */
|
|
341
|
+
export function toolResultFailure(event ) {
|
|
342
|
+
const data = (event )?.data
|
|
343
|
+
const error = data?.error
|
|
344
|
+
const message = data?.message
|
|
345
|
+
const text = Array.isArray(message?.content)
|
|
346
|
+
? message.content.filter((block) => block?.type === 'text' && typeof block.text === 'string').map((block) => block.text ).join(' ')
|
|
347
|
+
: ''
|
|
348
|
+
const code = typeof error?.code === 'string'
|
|
349
|
+
? error.code
|
|
350
|
+
: typeof error?.name === 'string' ? error.name : undefined
|
|
351
|
+
return {
|
|
352
|
+
...(code !== undefined ? { code } : {}),
|
|
353
|
+
...(text !== '' ? { message: text.slice(0, 240) } : {}),
|
|
354
|
+
isError: message?.isError === true || error !== undefined,
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* 每个会话只保留**最新一条未消费**的信号;相同 key 不重复排队。
|
|
360
|
+
* `take()` 消费后保留 lastKey,同一失败不会因为重复事件反复搜索。
|
|
361
|
+
*/
|
|
362
|
+
export class BehaviorSignalStore {
|
|
363
|
+
bySession = new Map ()
|
|
364
|
+
|
|
365
|
+
note(sessionId , signal ) {
|
|
366
|
+
if (sessionId === undefined) return false
|
|
367
|
+
const key = sessionId
|
|
368
|
+
const state = this.bySession.get(key) ?? { lastKey: '' }
|
|
369
|
+
if (signal.key === state.lastKey) return false
|
|
370
|
+
state.pending = signal
|
|
371
|
+
state.lastKey = signal.key
|
|
372
|
+
this.bySession.set(key, state)
|
|
373
|
+
if (this.bySession.size > 500) {
|
|
374
|
+
const oldest = this.bySession.keys().next()
|
|
375
|
+
if (!oldest.done) this.bySession.delete(oldest.value)
|
|
376
|
+
}
|
|
377
|
+
return true
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
take(sessionId ) {
|
|
381
|
+
if (sessionId === undefined) return undefined
|
|
382
|
+
const state = this.bySession.get(sessionId)
|
|
383
|
+
if (state?.pending === undefined) return undefined
|
|
384
|
+
const signal = state.pending
|
|
385
|
+
state.pending = undefined
|
|
386
|
+
return signal
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
clear(sessionId ) {
|
|
390
|
+
if (sessionId === undefined) return
|
|
391
|
+
this.bySession.delete(sessionId)
|
|
392
|
+
}
|
|
393
|
+
}
|
|
174
394
|
|
|
175
395
|
|
|
176
396
|
//# sourceURL=source/memory-inject/src/signals.ts
|
package/lib/provider/index.js
CHANGED
|
@@ -1415,7 +1415,11 @@ export class LocalMemoryService {
|
|
|
1415
1415
|
const candidates = new Map ()
|
|
1416
1416
|
for (const { store, isGlobal } of stores) {
|
|
1417
1417
|
const all = [...store.records.values()]
|
|
1418
|
-
|
|
1418
|
+
// 词法通道把标签也当可检索文本:自动召回用标签做触发词时,记忆正文里
|
|
1419
|
+
// 不一定出现这个标签(例如 tags=['release'] 但正文是中文),不并进来就搜不到。
|
|
1420
|
+
const searchableText = (record ) =>
|
|
1421
|
+
`${record.content} ${(record.tags ?? []).join(' ')}`
|
|
1422
|
+
const tokenSets = all.map((r) => new Set(tokenize(searchableText(r))))
|
|
1419
1423
|
const stats = buildStats(tokenSets)
|
|
1420
1424
|
for (let i = 0; i < all.length; i++) {
|
|
1421
1425
|
const rec = all[i]
|