@restregular/dsh-memory 0.15.2 → 0.16.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 +8 -0
- package/README.md +13 -3
- package/lib/inject/digest.js +2 -2
- package/lib/inject/inject.js +1 -1
- package/lib/inject/policy.js +2 -0
- package/lib/tools/apply.js +3 -11
- package/lib/tools/index.js +140 -11
- package/lib/tools/render.js +82 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
本项目的版本记录。安装 id 与 npm 版本一一对应。
|
|
4
4
|
|
|
5
|
+
## 0.16.0 — memory_search 只回印象,memory_get 按字段取
|
|
6
|
+
|
|
7
|
+
- **`memory_search` 不再返回正文**:每条结果只给 8 位 id、作用域、类型、标签和一段短摘要(默认 160 字),避免一次检索把整篇记忆塞进上下文。摘要长度可用 `summaryChars` 调整(20~2000)。返回结构从 `content` 改为 `summary`,正文统一走 `memory_get`。
|
|
8
|
+
- **`memory_get` 新增 `fields`**:可选择性地取 `content` / `summary` / `scope` / `kind` / `tags` / `importance` / `createdAt` / `updatedAt` / `expiresAt` / `triggers` / `supersedes`。省略时保持旧视图(`content + scope + kind + tags`);`id` 始终返回。未知字段明确报 `FIELDS_INVALID`,空列表报 `FIELDS_EMPTY`。`summary` 支持 `summaryChars`(默认 160,20~2000)。
|
|
9
|
+
- 渲染逻辑抽到不依赖宿主的 `tool-memory/src/render.ts`:检索一行一条、取记录时按 fields 拼元数据,只取元数据时不输出正文行;单测直接断言模型看到的文字。
|
|
10
|
+
- 提示文案同步:系统提示的「记忆使用策略」、会话摘要页脚、旧式召回头部都改为“先 `memory_search` 定位、再 `memory_get` 取正文”,不再让模型去 `memory_search` 里找原文。
|
|
11
|
+
- 回归测试:搜索不带正文、摘要截断与上下界;`fields` 的选择性取用 / 逗号字符串 / 未知字段 / 空列表 / missing;默认视图不变;搜索与取记录的文本渲染格式。
|
|
12
|
+
|
|
5
13
|
## 0.15.2 — 导出 / 导入保留流程触发词与状态过期时间
|
|
6
14
|
|
|
7
15
|
- 0.15.0 / 0.15.1 新增的 `triggers` 与 `expiredAt` 之前没有进入 `exportAll`,导出再导入会把流程触发词丢掉、把状态记忆重新当成新状态(TTL 重算)。现在两者都随纯文本导出往返;`expiredAt` 导入时作为绝对时间写回,不再重算 TTL。
|
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ JSONL 落盘 · 本机向量检索 · 五个模型工具 · 自动召回 · 硬
|
|
|
39
39
|
|
|
40
40
|
| 能力 | 说明 |
|
|
41
41
|
| --- | --- |
|
|
42
|
-
| **五个工具** | `memory_save` / `memory_search` / `memory_list` / `memory_get` / `memory_forget
|
|
42
|
+
| **五个工具** | `memory_save` / `memory_search` / `memory_list` / `memory_get` / `memory_forget`。`memory_search` 只回印象(8 位 id / 作用域 / 类型 / 标签 / 短摘要),正文和元数据用 `memory_get` 的 `fields` 按需取 |
|
|
43
43
|
| **四种作用域** | 会话 / 项目 / `global` / 自定义名。默认写进当前会话,读默认合并会话 + 项目 + global;项目作用域可起别名 |
|
|
44
44
|
| **双通道检索** | 词法(IDF + 中文 bigram)**+** 向量(本机 llama-server 的 OpenAI 兼容 `/v1/embeddings`)。向量缺席自动降级,不报错、不阻塞 |
|
|
45
45
|
| **自动召回** | 每轮按用户问题召回;新会话第一步注入项目 / global 摘要;发布、部署、工具失败等行为按标签触发定向召回 |
|
|
@@ -164,13 +164,23 @@ memory_save({
|
|
|
164
164
|
### 3. 检索、取全文、列出、忘记
|
|
165
165
|
|
|
166
166
|
```jsonc
|
|
167
|
-
memory_search({ "query": "pnpm 装依赖", "topK": 5 })
|
|
167
|
+
memory_search({ "query": "pnpm 装依赖", "topK": 5 }) // 只回 id / 作用域 / 类型 / 标签 / 摘要
|
|
168
168
|
memory_search({ "query": "旧的发布流程", "includeSuperseded": true }) // 默认隐藏被修正/覆盖的旧版本
|
|
169
|
-
memory_get({ "ids": ["3f2a9c1d"] })
|
|
169
|
+
memory_get({ "ids": ["3f2a9c1d"] }) // 默认:正文 + scope + kind + tags
|
|
170
|
+
memory_get({ "ids": ["3f2a9c1d"], "fields": ["summary", "updatedAt"] })
|
|
171
|
+
memory_get({ "ids": ["3f2a9c1d"], "fields": ["content"] }) // 只要正文
|
|
170
172
|
memory_list({ "limit": 20 })
|
|
171
173
|
memory_forget({ "id": "3f2a9c1d" }) // 按内容删:memory_forget({ query, scope })
|
|
172
174
|
```
|
|
173
175
|
|
|
176
|
+
`memory_search` 不再返回正文:每条只有 8 位 id、作用域、类型、标签和摘要(默认 160 字,
|
|
177
|
+
`summaryChars` 可调 20~2000)。判断相关后用 `memory_get` 取正文或元数据:
|
|
178
|
+
|
|
179
|
+
- `fields` 省略 = `content + scope + kind + tags`(与旧版看到的一致);
|
|
180
|
+
- 只取摘要:`["summary"]`;只取正文:`["content"]`;
|
|
181
|
+
- 元数据可选 `importance` / `createdAt` / `updatedAt` / `expiresAt` / `triggers` / `supersedes`;
|
|
182
|
+
- `id` 始终返回;未知字段会明确报 `FIELDS_INVALID`,不会静默忽略。
|
|
183
|
+
|
|
174
184
|
`memory_search` 省略 `scope` 时合并**会话 + 项目 + global**(按来源标注、按 id 去重);
|
|
175
185
|
`scope: "all"` 查所有已发现的作用域,写入路径会明确拒绝 `all`。
|
|
176
186
|
|
package/lib/inject/digest.js
CHANGED
|
@@ -17,10 +17,10 @@ import { estimateTokens } from '../provider/rerank.js'
|
|
|
17
17
|
|
|
18
18
|
const HEADER =
|
|
19
19
|
'以下是本项目 / 全局已有的记忆摘要(按重要度挑出的前几条;' +
|
|
20
|
-
'需要原文用 memory_search
|
|
20
|
+
'需要原文用 memory_get(先用 memory_search 定位),删除用 memory_forget):'
|
|
21
21
|
|
|
22
22
|
const HINT_HEADER = '相关记忆索引(正文未注入):'
|
|
23
|
-
const HINT_FOOTER = '
|
|
23
|
+
const HINT_FOOTER = '这些条目的正文尚未加载;遇到相关任务时,按 ID 用 memory_get 取正文,按标签用 memory_search 定位。'
|
|
24
24
|
|
|
25
25
|
/** 一条正文被省略的原因。统计口径互斥:每条记录只归入一个原因。 */
|
|
26
26
|
|
package/lib/inject/inject.js
CHANGED
|
@@ -216,7 +216,7 @@ export function renderRecallBlock(
|
|
|
216
216
|
const chars = impression ? (options.chars ?? 120) : 300
|
|
217
217
|
const header = impression
|
|
218
218
|
? '以下是与你这次请求可能相关的记忆摘要:'
|
|
219
|
-
: '
|
|
219
|
+
: '以下是与此请求可能相关的记忆,供参考(需要正文用 memory_get,用 memory_forget 删除):'
|
|
220
220
|
let tokens = estimateTokens(header) + 4
|
|
221
221
|
const lines = []
|
|
222
222
|
const ids = []
|
package/lib/inject/policy.js
CHANGED
|
@@ -6,6 +6,8 @@ export const POLICY_TEXT = `## 记忆系统
|
|
|
6
6
|
|
|
7
7
|
本会话挂着一个本地记忆库,工具:memory_search / memory_list / memory_get / memory_save / memory_forget(可按会话、项目、全局等作用域读写)。
|
|
8
8
|
|
|
9
|
+
memory_search 只返回印象(ID、作用域、类型、标签和少量摘要);判断相关后再用 memory_get(ids, fields) 取正文。fields 省略 = content + scope + kind + tags;只要摘要用 ["summary"],只要正文用 ["content"],需要重要度 / 时间 / 触发词等再显式加上对应字段。
|
|
10
|
+
|
|
9
11
|
遇到以下情况先查记忆,不要凭猜测开始:
|
|
10
12
|
- 不确定这个项目以前怎么做、有没有既定约定、某个坑是否踩过;
|
|
11
13
|
- 用户的话里出现「上次 / 之前 / 还是那样」这类指代。
|
package/lib/tools/apply.js
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
} from './index.js'
|
|
22
|
+
import { renderGetResult, renderSearchResult } from './render.js'
|
|
22
23
|
import { resolveCurrentScopes, scopeKind, } from '../provider/scope.js'
|
|
23
24
|
|
|
24
25
|
export const name = 'dsh-tool-memory'
|
|
@@ -104,10 +105,7 @@ export function apply(ctx , config = {}) {
|
|
|
104
105
|
|
|
105
106
|
register(tools.memory_search, (value, _args, exec) => {
|
|
106
107
|
const agent = (exec )?.agent
|
|
107
|
-
|
|
108
|
-
return value.records
|
|
109
|
-
.map((h) => `[${h.score.toFixed(3)}] (${renderScope(h.scope, agent)}/${h.kind}) ${h.content}`)
|
|
110
|
-
.join('\n')
|
|
108
|
+
return renderSearchResult(value, (scope) => renderScope(scope, agent))
|
|
111
109
|
})
|
|
112
110
|
|
|
113
111
|
register(tools.memory_forget, (value) => (value.removed === 0 ? '没有匹配的记忆。' : `已删除 ${value.removed} 条记忆。`))
|
|
@@ -123,13 +121,7 @@ export function apply(ctx , config = {}) {
|
|
|
123
121
|
|
|
124
122
|
register(tools.memory_get, (value, _args, exec) => {
|
|
125
123
|
const agent = (exec )?.agent
|
|
126
|
-
|
|
127
|
-
const tags = Array.isArray(r.tags) && r.tags.length > 0 ? ` · ${r.tags.join(', ')}` : ''
|
|
128
|
-
return `[${r.id}] (${renderScope(r.scope, agent)}/${r.kind}${tags})\n${r.content}`
|
|
129
|
-
}).join('\n\n')
|
|
130
|
-
const missing = value.missing.length > 0 ? `未找到:${value.missing.join(', ')}` : ''
|
|
131
|
-
if (body === '') return missing || '没有找到记忆。'
|
|
132
|
-
return missing === '' ? body : `${body}\n\n${missing}`
|
|
124
|
+
return renderGetResult(value, (scope) => renderScope(scope, agent))
|
|
133
125
|
})
|
|
134
126
|
}
|
|
135
127
|
|
package/lib/tools/index.js
CHANGED
|
@@ -22,10 +22,25 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
|
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
|
|
25
|
+
/** provider 的 getByIds 返回的记录形状(工具层只读这些字段)。 */
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
25
40
|
/** 只有这一层需要的能力子集(provider 在宿主里提供,测试里可以 mock)。 */
|
|
26
41
|
|
|
27
42
|
|
|
28
|
-
|
|
43
|
+
|
|
29
44
|
|
|
30
45
|
|
|
31
46
|
|
|
@@ -35,7 +50,7 @@ import { canonicalWorkspacePath, normalizeScopeInput, resolveScopeRef } from '..
|
|
|
35
50
|
|
|
36
51
|
|
|
37
52
|
|
|
38
|
-
|
|
53
|
+
|
|
39
54
|
|
|
40
55
|
|
|
41
56
|
|
|
@@ -255,12 +270,30 @@ export function memorySaveTool(opts )
|
|
|
255
270
|
}
|
|
256
271
|
}
|
|
257
272
|
|
|
273
|
+
/** memory_search / memory_get(summary) 的默认摘要长度(字符)。 */
|
|
274
|
+
export const DEFAULT_SUMMARY_CHARS = 160
|
|
275
|
+
|
|
276
|
+
/** 摘要字数上限:默认 160,允许调用方在 20~2000 之间调整。 */
|
|
277
|
+
export function resolveSummaryChars(value ) {
|
|
278
|
+
const n = typeof value === 'number' ? value : Number(value)
|
|
279
|
+
if (!Number.isFinite(n)) return DEFAULT_SUMMARY_CHARS
|
|
280
|
+
return Math.min(2000, Math.max(20, Math.round(n)))
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** 空白折叠 + 截断。memory_search 的“少量摘要”和 memory_get 的 summary 字段共用。 */
|
|
284
|
+
export function summarize(content , chars = DEFAULT_SUMMARY_CHARS) {
|
|
285
|
+
const flat = String(content).replace(/\s+/g, ' ').trim()
|
|
286
|
+
return flat.length <= chars ? flat : `${flat.slice(0, chars)}…`
|
|
287
|
+
}
|
|
288
|
+
|
|
258
289
|
|
|
259
290
|
|
|
260
291
|
|
|
261
292
|
|
|
262
293
|
|
|
263
294
|
|
|
295
|
+
|
|
296
|
+
|
|
264
297
|
|
|
265
298
|
|
|
266
299
|
|
|
@@ -268,7 +301,9 @@ export function memorySaveTool(opts )
|
|
|
268
301
|
|
|
269
302
|
|
|
270
303
|
|
|
304
|
+
|
|
271
305
|
|
|
306
|
+
|
|
272
307
|
|
|
273
308
|
|
|
274
309
|
|
|
@@ -279,12 +314,13 @@ export function memorySaveTool(opts )
|
|
|
279
314
|
export function memorySearchTool(opts ) {
|
|
280
315
|
return {
|
|
281
316
|
name: 'memory_search',
|
|
282
|
-
description: 'Search saved memories by keyword or meaning (lexical + local vector recall).',
|
|
317
|
+
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.',
|
|
283
318
|
parameters: {
|
|
284
319
|
query: { type: 'string', required: true },
|
|
285
320
|
scope: { type: 'string', description: SCOPE_DESCRIPTION },
|
|
286
321
|
topK: { type: 'number', description: '1~20' },
|
|
287
322
|
includeSuperseded: { type: 'boolean', description: '默认 false;true 时把已被 supersede 的旧版本也返回(历史核对)' },
|
|
323
|
+
summaryChars: { type: 'number', description: `每条摘要的字数上限(默认 ${DEFAULT_SUMMARY_CHARS},20~2000)` },
|
|
288
324
|
},
|
|
289
325
|
async execute(args , exec ) {
|
|
290
326
|
const service = svc(opts)
|
|
@@ -301,6 +337,7 @@ export function memorySearchTool(opts )
|
|
|
301
337
|
// 省略 scope:三路合并(会话 + 项目 + global)
|
|
302
338
|
scopes = readScopes(opts, service, agentOf(exec))
|
|
303
339
|
}
|
|
340
|
+
const chars = resolveSummaryChars(args.summaryChars)
|
|
304
341
|
try {
|
|
305
342
|
const hits = await service.search({
|
|
306
343
|
text: args.query,
|
|
@@ -315,8 +352,9 @@ export function memorySearchTool(opts )
|
|
|
315
352
|
score: h.score,
|
|
316
353
|
scope: h.record.scope,
|
|
317
354
|
kind: h.record.kind,
|
|
355
|
+
tags: Array.isArray(h.record.tags) ? h.record.tags : [],
|
|
318
356
|
updatedAt: h.record.updatedAt,
|
|
319
|
-
|
|
357
|
+
summary: summarize(h.record.content, chars),
|
|
320
358
|
})),
|
|
321
359
|
}
|
|
322
360
|
} catch (error) {
|
|
@@ -428,18 +466,109 @@ export function memoryListTool(opts )
|
|
|
428
466
|
}
|
|
429
467
|
}
|
|
430
468
|
|
|
431
|
-
|
|
469
|
+
/** memory_get 可按需取得的字段。id 始终返回,不在此列。 */
|
|
470
|
+
export const GET_FIELDS = [
|
|
471
|
+
'content',
|
|
472
|
+
'summary',
|
|
473
|
+
'scope',
|
|
474
|
+
'kind',
|
|
475
|
+
'tags',
|
|
476
|
+
'importance',
|
|
477
|
+
'createdAt',
|
|
478
|
+
'updatedAt',
|
|
479
|
+
'expiresAt',
|
|
480
|
+
'triggers',
|
|
481
|
+
'supersedes',
|
|
482
|
+
]
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
/** 省略 fields 时的默认视图:与旧版 memory_get 看到的内容一致。 */
|
|
487
|
+
export const DEFAULT_GET_FIELDS = ['content', 'scope', 'kind', 'tags']
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
|
|
432
499
|
|
|
433
|
-
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
|
|
434
503
|
|
|
435
504
|
|
|
436
505
|
|
|
506
|
+
const FIELD_BY_LOWER = new Map (GET_FIELDS.map((field) => [field.toLowerCase(), field]))
|
|
507
|
+
|
|
508
|
+
/** 解析并校验 fields:接受数组,也接受逗号/空格分隔的字符串。 */
|
|
509
|
+
export function resolveGetFields(raw ) {
|
|
510
|
+
if (raw === undefined || raw === null) return [...DEFAULT_GET_FIELDS]
|
|
511
|
+
const items = Array.isArray(raw) ? raw : typeof raw === 'string' ? raw.split(/[,\s]+/) : null
|
|
512
|
+
if (items === null) {
|
|
513
|
+
throw new MemoryToolError('FIELDS_INVALID', `fields must be an array or a comma-separated string; allowed: ${GET_FIELDS.join(', ')}`)
|
|
514
|
+
}
|
|
515
|
+
const fields = []
|
|
516
|
+
const invalid = []
|
|
517
|
+
for (const item of items) {
|
|
518
|
+
const key = String(item).trim().toLowerCase()
|
|
519
|
+
if (key === '') continue
|
|
520
|
+
const field = FIELD_BY_LOWER.get(key)
|
|
521
|
+
if (field !== undefined) {
|
|
522
|
+
if (!fields.includes(field)) fields.push(field)
|
|
523
|
+
} else if (!invalid.includes(key)) {
|
|
524
|
+
invalid.push(key)
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
if (invalid.length > 0) {
|
|
528
|
+
throw new MemoryToolError('FIELDS_INVALID', `unknown field(s): ${invalid.join(', ')}; allowed: ${GET_FIELDS.join(', ')}`)
|
|
529
|
+
}
|
|
530
|
+
if (fields.length === 0) {
|
|
531
|
+
throw new MemoryToolError('FIELDS_EMPTY', `fields 不能为空;可用:${GET_FIELDS.join(', ')}`)
|
|
532
|
+
}
|
|
533
|
+
return fields
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function pickGetFields(
|
|
537
|
+
record ,
|
|
538
|
+
fields ,
|
|
539
|
+
summaryChars ,
|
|
540
|
+
) {
|
|
541
|
+
const out = { id: record.id }
|
|
542
|
+
for (const field of fields) {
|
|
543
|
+
switch (field) {
|
|
544
|
+
case 'content': out.content = record.content; break
|
|
545
|
+
case 'summary': out.summary = summarize(record.content, summaryChars); break
|
|
546
|
+
case 'scope': out.scope = record.scope; break
|
|
547
|
+
case 'kind': out.kind = record.kind; break
|
|
548
|
+
case 'tags': out.tags = Array.isArray(record.tags) ? record.tags : []; break
|
|
549
|
+
case 'importance': out.importance = record.importance; break
|
|
550
|
+
case 'createdAt': out.createdAt = record.createdAt; break
|
|
551
|
+
case 'updatedAt': out.updatedAt = record.updatedAt; break
|
|
552
|
+
case 'expiresAt': if (record.expiredAt !== undefined) out.expiresAt = record.expiredAt; break
|
|
553
|
+
case 'triggers': if (Array.isArray(record.triggers)) out.triggers = record.triggers; break
|
|
554
|
+
case 'supersedes': if (record.supersedes !== undefined && record.supersedes !== '') out.supersedes = record.supersedes; break
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return out
|
|
558
|
+
}
|
|
559
|
+
|
|
437
560
|
export function memoryGetTool(opts ) {
|
|
438
561
|
return {
|
|
439
562
|
name: 'memory_get',
|
|
440
|
-
description: 'Fetch memories by id or by the 8-char id prefix shown in recall/digest
|
|
563
|
+
description: 'Fetch memories by id or by the 8-char id prefix shown in search/recall/digest output. Default fields: content + scope + kind + tags; pass fields to fetch only what you need (e.g. ["summary"], ["tags"], ["updatedAt"]).',
|
|
441
564
|
parameters: {
|
|
442
565
|
ids: { type: 'array', items: { type: 'string' }, description: '记忆 id 或摘要开头的 8 位前缀,1~50 个', required: true },
|
|
566
|
+
fields: {
|
|
567
|
+
type: 'array',
|
|
568
|
+
items: { type: 'string' },
|
|
569
|
+
description: `只取这些字段,省略=${DEFAULT_GET_FIELDS.join('+')};可选:${GET_FIELDS.join(' / ')}(id 始终返回)`,
|
|
570
|
+
},
|
|
571
|
+
summaryChars: { type: 'number', description: `summary 字段的字数上限(默认 ${DEFAULT_SUMMARY_CHARS},20~2000)` },
|
|
443
572
|
},
|
|
444
573
|
async execute(args ) {
|
|
445
574
|
const service = svc(opts)
|
|
@@ -450,13 +579,13 @@ export function memoryGetTool(opts )
|
|
|
450
579
|
if (typeof service.getByIds !== 'function') {
|
|
451
580
|
throw toolError(new Error('当前 provider 不支持 memory_get(请升级 @restregular/dsh-memory)'))
|
|
452
581
|
}
|
|
582
|
+
const fields = resolveGetFields(args.fields)
|
|
583
|
+
const chars = resolveSummaryChars(args.summaryChars)
|
|
453
584
|
try {
|
|
454
585
|
const found = service.getByIds(ids)
|
|
455
586
|
return {
|
|
456
|
-
records: found.map((r) => (
|
|
457
|
-
|
|
458
|
-
tags: r.tags, importance: r.importance, updatedAt: r.updatedAt,
|
|
459
|
-
})),
|
|
587
|
+
records: found.map((r) => pickGetFields(r, fields, chars)),
|
|
588
|
+
fields,
|
|
460
589
|
missing: ids.filter((token) => !found.some((r) => r.id === token || r.id.startsWith(token))),
|
|
461
590
|
}
|
|
462
591
|
} catch (error) {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具结果的纯文本渲染。刻意不 import cordis / defineTool:apply.ts 只负责把
|
|
3
|
+
* scope 键翻译成人看的标签,其余格式都在这里,单测可以直接断言模型看到的文字。
|
|
4
|
+
*
|
|
5
|
+
* 0.16.0 起 memory_search 只给“印象”(id / 作用域 / 类型 / 标签 / 短摘要),
|
|
6
|
+
* 正文走 memory_get;memory_get 的 fields 决定渲染哪些元数据。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/** 把 scope 键渲染成展示文案(宿主能查到会话标题时用它,否则用短 id)。 */
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const shortId = (id ) => id.slice(0, 8)
|
|
14
|
+
|
|
15
|
+
const day = (value ) =>
|
|
16
|
+
typeof value === 'number' && Number.isFinite(value) ? new Date(value).toISOString().slice(0, 10) : ''
|
|
17
|
+
|
|
18
|
+
const joinList = (value ) =>
|
|
19
|
+
Array.isArray(value) ? value.map(String).filter((v) => v !== '').join(', ') : ''
|
|
20
|
+
|
|
21
|
+
/** 「[得分] id (作用域/类型 · 标签) 摘要」;没有任何一条命中时给明确空结果。 */
|
|
22
|
+
export function renderSearchResult(value , label ) {
|
|
23
|
+
if (value.records.length === 0) return '没有找到相关记忆。'
|
|
24
|
+
return value.records.map((hit) => {
|
|
25
|
+
const tags = joinList(hit.tags)
|
|
26
|
+
const meta = `${label(hit.scope)}/${hit.kind}${tags === '' ? '' : ` · ${tags}`}`
|
|
27
|
+
return `[${hit.score.toFixed(3)}] ${shortId(hit.id)} (${meta}) ${hit.summary}`
|
|
28
|
+
}).join('\n')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* memory_get 的渲染:
|
|
33
|
+
* - 元数据按 fields 拼在首行括号里;
|
|
34
|
+
* - content 优先于 summary(两者都给时正文已经足够);
|
|
35
|
+
* - 只取元数据时不输出正文行。
|
|
36
|
+
*/
|
|
37
|
+
export function renderGetResult(value , label ) {
|
|
38
|
+
const fields = new Set (value.fields)
|
|
39
|
+
const body = value.records.map((record) => {
|
|
40
|
+
const meta = []
|
|
41
|
+
const scope = typeof record.scope === 'string' ? record.scope : ''
|
|
42
|
+
const kind = typeof record.kind === 'string' ? record.kind : ''
|
|
43
|
+
if (fields.has('scope') && fields.has('kind')) meta.push(`${label(scope)}/${kind}`)
|
|
44
|
+
else if (fields.has('scope')) meta.push(label(scope))
|
|
45
|
+
else if (fields.has('kind')) meta.push(kind)
|
|
46
|
+
if (fields.has('tags')) {
|
|
47
|
+
const tags = joinList(record.tags)
|
|
48
|
+
if (tags !== '') meta.push(`标签 ${tags}`)
|
|
49
|
+
}
|
|
50
|
+
if (fields.has('importance') && typeof record.importance === 'number') meta.push(`重要度 ${record.importance}`)
|
|
51
|
+
if (fields.has('createdAt')) {
|
|
52
|
+
const created = day(record.createdAt)
|
|
53
|
+
if (created !== '') meta.push(`创建 ${created}`)
|
|
54
|
+
}
|
|
55
|
+
if (fields.has('updatedAt')) {
|
|
56
|
+
const updated = day(record.updatedAt)
|
|
57
|
+
if (updated !== '') meta.push(`更新 ${updated}`)
|
|
58
|
+
}
|
|
59
|
+
if (fields.has('expiresAt')) {
|
|
60
|
+
const expires = day(record.expiresAt)
|
|
61
|
+
if (expires !== '') meta.push(`过期 ${expires}`)
|
|
62
|
+
}
|
|
63
|
+
if (fields.has('triggers')) {
|
|
64
|
+
const triggers = joinList(record.triggers)
|
|
65
|
+
if (triggers !== '') meta.push(`触发词 ${triggers}`)
|
|
66
|
+
}
|
|
67
|
+
if (fields.has('supersedes') && typeof record.supersedes === 'string' && record.supersedes !== '') {
|
|
68
|
+
meta.push(`修正 ${shortId(record.supersedes)}`)
|
|
69
|
+
}
|
|
70
|
+
const head = `[${record.id}]${meta.length > 0 ? ` (${meta.join(' · ')})` : ''}`
|
|
71
|
+
const content = typeof record.content === 'string' ? record.content : undefined
|
|
72
|
+
const summary = typeof record.summary === 'string' ? record.summary : undefined
|
|
73
|
+
const text = content ?? (summary === undefined ? '' : `摘要:${summary}`)
|
|
74
|
+
return text === '' ? head : `${head}\n${text}`
|
|
75
|
+
}).join('\n\n')
|
|
76
|
+
const missing = value.missing.length > 0 ? `未找到:${value.missing.join(', ')}` : ''
|
|
77
|
+
if (body === '') return missing || '没有找到记忆。'
|
|
78
|
+
return missing === '' ? body : `${body}\n\n${missing}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
//# sourceURL=source/tool-memory/src/render.ts
|