dsh-aurorix-agentbook 1.9.5 → 1.10.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/lib/host.js +19 -15
- package/lib/snapshot.js +216 -76
- package/package.json +1 -1
package/lib/host.js
CHANGED
|
@@ -55,6 +55,7 @@ import {
|
|
|
55
55
|
DEFAULT_PROTOCOL,
|
|
56
56
|
resolveStatePath,
|
|
57
57
|
librarySignature,
|
|
58
|
+
makeAnchorProbe,
|
|
58
59
|
} from './snapshot.js'
|
|
59
60
|
import { GUIDE, GUIDE_INDEX } from './guide.js'
|
|
60
61
|
import { CURATION_MANUAL } from './curation.js'
|
|
@@ -73,19 +74,16 @@ function readConfig(config) {
|
|
|
73
74
|
enabled: get('enabled', true),
|
|
74
75
|
injectSection: get('injectSection', false),
|
|
75
76
|
injectSnapshot: get('injectSnapshot', true),
|
|
76
|
-
//
|
|
77
|
-
// 所以它们同时是费用上限和信息完整度两件事。
|
|
77
|
+
// 常驻层(soul / profile / explicit)给全量正文,下三层只给标题索引。
|
|
78
78
|
//
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
// 这几个值沿用 1.9.3,没有下调。
|
|
84
|
-
fullCount: Number(get('fullCount', 6)) || 0,
|
|
85
|
-
briefCount: Number(get('briefCount', 12)) || 0,
|
|
86
|
-
indexCount: Number(get('indexCount', 120)) || 0,
|
|
79
|
+
// 体积不从这里省:标题索引是「知道库里有什么」的入口,正文一律走检索。
|
|
80
|
+
// 曾经为了省字节把下三层压成摘要档甚至只留标题,前者等于把同一件事说半遍、
|
|
81
|
+
// 真正要正文时还得 recall;后者在 rules 上会删掉执行必需的标识符。现在
|
|
82
|
+
// 结构上就没有摘要档——要么常驻层全量,要么标题。
|
|
87
83
|
residentLimit: Number(get('residentLimit', 40)) || 40,
|
|
88
|
-
|
|
84
|
+
// 0 = 全部标题。下三层 538 条标题共约 37KB,全给也就这么多;截断反而让
|
|
85
|
+
// 模型以为「库里没有这条」而漏检索。
|
|
86
|
+
indexCount: Number(get('indexCount', 0)),
|
|
89
87
|
decayHalfLifeDays: Number(get('decayHalfLifeDays', 30)),
|
|
90
88
|
accessBoost: Number(get('accessBoost', 80)),
|
|
91
89
|
maxContextBytes: Number(get('maxContextBytes', 96000)) || 96000,
|
|
@@ -153,17 +151,19 @@ export function attachHost(ctx, deps) {
|
|
|
153
151
|
|
|
154
152
|
const doRefresh = async (current) => {
|
|
155
153
|
try {
|
|
154
|
+
// 主题簇验证要按锚点词跑检索,单次约 0.5 秒。库没变时结果必然相同,
|
|
155
|
+
// 所以按库指纹缓存到磁盘:同一份库只在第一次花这几秒,之后直接复用。
|
|
156
|
+
const probeAnchor = makeAnchorProbe(backend, librarySignature(libraryRoot()))
|
|
156
157
|
const built = await buildSnapshot((plane) => backend.list(plane, { limit: 2000, full: true }), {
|
|
157
|
-
fullCount: current.fullCount,
|
|
158
|
-
briefCount: current.briefCount,
|
|
159
158
|
indexCount: current.indexCount,
|
|
160
159
|
residentLimit: current.residentLimit,
|
|
161
|
-
briefChars: current.briefChars,
|
|
162
160
|
decayHalfLifeDays: current.decayHalfLifeDays,
|
|
163
161
|
accessBoost: current.accessBoost,
|
|
164
162
|
maxContextBytes: current.maxContextBytes,
|
|
165
163
|
access,
|
|
164
|
+
probeAnchor,
|
|
166
165
|
})
|
|
166
|
+
if (typeof probeAnchor?.save === 'function') probeAnchor.save()
|
|
167
167
|
snapshot.text = built.text
|
|
168
168
|
snapshot.stats = built.stats
|
|
169
169
|
snapshot.error = ''
|
|
@@ -305,7 +305,11 @@ export function attachHost(ctx, deps) {
|
|
|
305
305
|
'- 上下文被压缩过,或这是跨会话续接\n' +
|
|
306
306
|
'- 准备写记忆之前(先查有没有同一条,避免重复开新条)\n' +
|
|
307
307
|
'禁把「这轮简单」「刚聊过」「我应该记得」当跳过理由:漏查的代价是重复踩坑,查一次的代价是一次工具调用。\n' +
|
|
308
|
-
'
|
|
308
|
+
'检索词用话题词,不要用整句;推荐先广后窄:先一个词看面上有什么,再收窄。\n' +
|
|
309
|
+
'\n' +
|
|
310
|
+
'注入的快照里下三层(rules / long_term / episodic)只有标题,正文不注入。\n' +
|
|
311
|
+
'看到相关标题后要正文,用 `aurorix_abook` 按 ID 取:`["memory","read","--id","<id>","--json"]`。\n' +
|
|
312
|
+
'本工具返回的 preview 只截前 220 字,够判断是不是这条,不够当正文用。',
|
|
309
313
|
parameters: {
|
|
310
314
|
type: 'object',
|
|
311
315
|
properties: {
|
package/lib/snapshot.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// 分层快照:把记忆库压成一段能进提示词的文本。
|
|
2
2
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
3
|
+
// 两档:常驻层(soul/profile/explicit)永久给全量正文;下三层(rules/long_term/
|
|
4
|
+
// episodic)只给标题,正文一律按 ID 取。
|
|
5
|
+
//
|
|
6
|
+
// 下三层的标题会先按「主题簇」折叠:同一主题的若干条只留一个锚点词加条数,
|
|
7
|
+
// 想看那一簇就用锚点词检索——检索能把这些条目全部找回来,所以不丢信息。
|
|
8
|
+
//
|
|
9
|
+
// 折叠的前提是可验证:只有当锚点词检索确实覆盖该簇全部条目(召回 >= 80%)时
|
|
10
|
+
// 才折叠;否则原样列出。宁可多占几百字节,也不让模型以为「库里没有这条」。
|
|
6
11
|
import { mkdirSync, readFileSync, readdirSync, renameSync, statSync, writeFileSync } from 'node:fs'
|
|
7
12
|
import { dirname, join } from 'node:path'
|
|
8
13
|
|
|
@@ -21,11 +26,6 @@ const PLANE_LABEL = {
|
|
|
21
26
|
// 排序基准:身份/规则/偏好在最前,经验其次,过程记录最轻。
|
|
22
27
|
const PLANE_BASE = { soul: 1000, rules: 900, profile: 800, explicit: 700, long_term: 300, episodic: 120 }
|
|
23
28
|
|
|
24
|
-
// 约束类永不完全淡出:最低保到摘要档,避免规则被排到只剩标题。
|
|
25
|
-
const PLANE_FLOOR = { rules: 'brief' }
|
|
26
|
-
|
|
27
|
-
const TIER_ORDER = { index: 0, brief: 1, full: 2 }
|
|
28
|
-
|
|
29
29
|
// 每步注入的记忆说明:只在装了本插件的环境里出现。
|
|
30
30
|
// 系统提示词里不再提记忆——没装插件的环境本就没有这个能力,不该被暗示。
|
|
31
31
|
//
|
|
@@ -146,28 +146,6 @@ function scoreOf(record, access, now, halfLifeDays, accessBoost) {
|
|
|
146
146
|
return base + boost - decay
|
|
147
147
|
}
|
|
148
148
|
|
|
149
|
-
// 各平面摘要长度:给足能认出「是不是这条」的上下文。
|
|
150
|
-
// 曾经按 rules 标题已足够概括为由只列标题,结果把执行时必需的标识符删了——
|
|
151
|
-
// 「记忆机制分类与固定黑名单」这个标题看不到 `ENVIRONMENT_MEMORY` /
|
|
152
|
-
// `AURORIX_MEMORY` 两个分类名。体积不从记忆内容里省。
|
|
153
|
-
const BRIEF_CHARS_BY_PLANE = {}
|
|
154
|
-
|
|
155
|
-
function renderTier(record, tier, briefChars) {
|
|
156
|
-
const limit = BRIEF_CHARS_BY_PLANE[record.plane] ?? briefChars
|
|
157
|
-
const title = record.title || '(无标题)'
|
|
158
|
-
const content = String(record.content || '').trim()
|
|
159
|
-
if (tier === 'full') return `- 【${title}】${content}`
|
|
160
|
-
if (tier === 'brief') {
|
|
161
|
-
// 限额为 0:标题即摘要。不附正文,也不附结构目录(同一个道理,标题已经说明是什么)。
|
|
162
|
-
if (limit <= 0) return `- ${title}`
|
|
163
|
-
const gist = gistOf(content, limit)
|
|
164
|
-
const heads = headingsOf(content)
|
|
165
|
-
const structure = heads.length > 1 ? `\n └ ${heads.slice(0, 5).join(' / ')}` : ''
|
|
166
|
-
return `- 【${title}】${gist}${structure}`
|
|
167
|
-
}
|
|
168
|
-
return title
|
|
169
|
-
}
|
|
170
|
-
|
|
171
149
|
/** 热度状态:记录哪个 ID 被真正用过。只影响排序,不影响库。 */
|
|
172
150
|
export function createAccessStore(statePath) {
|
|
173
151
|
let store = { version: 1, records: {} }
|
|
@@ -274,16 +252,172 @@ export function librarySignature(root) {
|
|
|
274
252
|
* 读全库并压成快照文本。
|
|
275
253
|
* @param {(plane: string) => Promise<{ ok: boolean, items?: object[], reason?: string }>} listPlane
|
|
276
254
|
*/
|
|
255
|
+
/**
|
|
256
|
+
* 主题簇折叠的检索探针,按库指纹缓存到磁盘。
|
|
257
|
+
*
|
|
258
|
+
* 验证一个锚点要跑一次检索(约 0.5 秒),锚点几十个就是几秒。库没变时验证结果
|
|
259
|
+
* 必然相同,所以同一份库只该验证一次。缓存里存「锚点 → 命中的记录 ID」。
|
|
260
|
+
*
|
|
261
|
+
* 库指纹一变整份作废——宁可重算,也不拿旧指纹的结论去折叠新库:那会让模型
|
|
262
|
+
* 搜不到本来存在的条目。
|
|
263
|
+
*/
|
|
264
|
+
export function makeAnchorProbe(backend, signature, cachePath) {
|
|
265
|
+
if (typeof backend?.search !== 'function') return null
|
|
266
|
+
if (!signature) return null
|
|
267
|
+
|
|
268
|
+
const file = cachePath || join(dirname(resolveStatePath('')), `clusters-${signature}.json`)
|
|
269
|
+
let cache = {}
|
|
270
|
+
try {
|
|
271
|
+
const raw = JSON.parse(readFileSync(file, 'utf8'))
|
|
272
|
+
// 指纹对不上就整份丢掉。
|
|
273
|
+
if (raw && raw.signature === signature && raw.anchors && typeof raw.anchors === 'object') cache = raw.anchors
|
|
274
|
+
} catch {
|
|
275
|
+
cache = {}
|
|
276
|
+
}
|
|
277
|
+
const inFlight = new Map()
|
|
278
|
+
|
|
279
|
+
const probe = async (anchor) => {
|
|
280
|
+
const key = String(anchor).toLowerCase()
|
|
281
|
+
if (cache[key]) return new Set(cache[key])
|
|
282
|
+
if (inFlight.has(key)) return inFlight.get(key) // 同轮同锚点只查一次
|
|
283
|
+
const task = (async () => {
|
|
284
|
+
const result = await backend.search(key, 60)
|
|
285
|
+
// 两种形状都要认:插件 backend.search 返回 {ok,items},裸 abook JSON 是 {items,total}。
|
|
286
|
+
// 只看 ok 会在后者上永远判成失败,折叠静默归零——这正是最初把折叠数做成 0 的原因。
|
|
287
|
+
const items = Array.isArray(result?.items) ? result.items : []
|
|
288
|
+
const ok = result?.ok !== false && items.length >= 0 && Array.isArray(result?.items)
|
|
289
|
+
const ids = ok ? items.map((x) => String(x.id ?? '')).filter(Boolean) : []
|
|
290
|
+
if (ok) cache[key] = ids
|
|
291
|
+
return new Set(ids)
|
|
292
|
+
})()
|
|
293
|
+
inFlight.set(key, task)
|
|
294
|
+
try {
|
|
295
|
+
return await task
|
|
296
|
+
} finally {
|
|
297
|
+
inFlight.delete(key)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// 构建结束后由调用方落盘:避免每个锚点都写一次文件。
|
|
302
|
+
probe.save = () => {
|
|
303
|
+
try {
|
|
304
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
305
|
+
const tmp = `${file}.tmp`
|
|
306
|
+
writeFileSync(tmp, JSON.stringify({ signature, anchors: cache }), 'utf8')
|
|
307
|
+
renameSync(tmp, file)
|
|
308
|
+
} catch {
|
|
309
|
+
// 缓存写不进去不影响正确性,下次重算而已。
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return probe
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// 主题簇折叠。
|
|
316
|
+
//
|
|
317
|
+
// 只认「具体技术实体」当锚点:`abook`、`zygisk`、`xqdk` 这类词检索起来能精准命中
|
|
318
|
+
// 同一批记录;`验证`、`必须`、`android` 这类泛词不行——实测 android 只能召回 19%,
|
|
319
|
+
// 拿它当锚点等于告诉模型「去搜这个词」然后什么也找不到。
|
|
320
|
+
//
|
|
321
|
+
// 判定可验证:锚点词检索回来必须覆盖该簇至少 80% 的条目。达不到就整簇放弃、原样列出。
|
|
322
|
+
const CLUSTER_MIN_RECALL = 0.8
|
|
323
|
+
const CLUSTER_MIN_SIZE = 2
|
|
324
|
+
// 验证成本 = 锚点数 × 单次检索耗时(约 0.5 秒)。这两个上限把最坏情况压到
|
|
325
|
+
// 「几十次检索、几秒」,而不是「几百次、几十秒」。
|
|
326
|
+
const CLUSTER_MAX_ANCHORS = 60
|
|
327
|
+
const CLUSTER_CONCURRENCY = 6
|
|
328
|
+
|
|
329
|
+
// 不当作锚点的英文虚词。
|
|
330
|
+
const EN_STOP = new Set(['the', 'and', 'for', 'with', 'that', 'this', 'from', 'not', 'are', 'was', 'you', 'can', 'all', 'any', 'but', 'how', 'why'])
|
|
331
|
+
|
|
332
|
+
function latinAnchors(title) {
|
|
333
|
+
const out = []
|
|
334
|
+
for (const m of String(title).matchAll(/[A-Za-z][A-Za-z0-9_.\-]{2,}/g)) {
|
|
335
|
+
const key = m[0].toLowerCase()
|
|
336
|
+
if (!EN_STOP.has(key)) out.push(key)
|
|
337
|
+
}
|
|
338
|
+
return [...new Set(out)]
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* @param {{id:string,title:string}[]} records 下三层记录
|
|
343
|
+
* @param {(anchor:string) => Promise<Set<string>>} probe 用锚点词检索,返回命中的记录 ID 集合
|
|
344
|
+
* @returns {Promise<{clusters:{anchor:string,count:number}[], ownerOf:Map<string,string>, folded:number}>}
|
|
345
|
+
*/
|
|
346
|
+
export async function clusterTitles(records, probe) {
|
|
347
|
+
const byAnchor = new Map()
|
|
348
|
+
for (const r of records) {
|
|
349
|
+
for (const a of latinAnchors(r.title)) {
|
|
350
|
+
if (!byAnchor.has(a)) byAnchor.set(a, [])
|
|
351
|
+
const list = byAnchor.get(a)
|
|
352
|
+
if (!list.includes(r.id)) list.push(r.id)
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// 先按成员数从大到小验证,让大簇优先占住自己的成员。
|
|
357
|
+
// 每个锚点要跑一次检索,单次约 0.5 秒。锚点上百个串行就是几十秒,
|
|
358
|
+
// 所以按固定并发度并行;上限压住是为了不和正在跑的其它 abook 调用抢资源。
|
|
359
|
+
const candidates = [...byAnchor]
|
|
360
|
+
.filter(([, ids]) => ids.length >= CLUSTER_MIN_SIZE)
|
|
361
|
+
.sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]))
|
|
362
|
+
.slice(0, CLUSTER_MAX_ANCHORS) // 只验证最可能成簇的那些,剩下的本来也不值得折叠
|
|
363
|
+
|
|
364
|
+
// 验证结果先按键存进 Map,最后再按 candidates 的原顺序取回。
|
|
365
|
+
// 直接用 push 收集会让顺序取决于哪个 worker 先跑完——同一份库两次构建
|
|
366
|
+
// 可能折出不同结果,快照文本一变宿主就在历史里多留一份副本。
|
|
367
|
+
const passed = new Map()
|
|
368
|
+
let cursor = 0
|
|
369
|
+
const worker = async () => {
|
|
370
|
+
while (cursor < candidates.length) {
|
|
371
|
+
const [anchor, ids] = candidates[cursor++]
|
|
372
|
+
let hit
|
|
373
|
+
try {
|
|
374
|
+
hit = await probe(anchor)
|
|
375
|
+
} catch {
|
|
376
|
+
continue // 检索失败就当这个锚点不可用,宁可不成簇
|
|
377
|
+
}
|
|
378
|
+
if (!hit || hit.size === 0) continue
|
|
379
|
+
const covered = ids.filter((id) => hit.has(id)).length
|
|
380
|
+
if (covered / ids.length >= CLUSTER_MIN_RECALL) passed.set(anchor, ids)
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
// 注意:worker 数量超过 1 时 probe 会被并发调用,调用方必须能承受并发
|
|
384
|
+
// (插件侧走的是 async 子进程,不是 execFileSync)。
|
|
385
|
+
await Promise.all(Array.from({ length: Math.min(CLUSTER_CONCURRENCY, candidates.length) }, worker))
|
|
386
|
+
|
|
387
|
+
// 按原顺序(成员数从多到少,同数按锚点字典序)重放,确保确定性。
|
|
388
|
+
const verified = candidates
|
|
389
|
+
.filter(([anchor]) => passed.has(anchor))
|
|
390
|
+
.map(([anchor, ids]) => ({ anchor, ids }))
|
|
391
|
+
|
|
392
|
+
const ownerOf = new Map()
|
|
393
|
+
for (const { anchor, ids } of verified) {
|
|
394
|
+
for (const id of ids) if (!ownerOf.has(id)) ownerOf.set(id, anchor)
|
|
395
|
+
}
|
|
396
|
+
// 贪心分配后可能剩单条成员(成员被更大的簇抢走了)。只剩一条的簇没有折叠价值,
|
|
397
|
+
// 把它列出来反而让模型以为「去搜这个词能拿到好几条」,所以退回标题列表。
|
|
398
|
+
const counts = new Map()
|
|
399
|
+
for (const anchor of ownerOf.values()) counts.set(anchor, (counts.get(anchor) || 0) + 1)
|
|
400
|
+
for (const [anchor, count] of [...counts]) {
|
|
401
|
+
if (count >= CLUSTER_MIN_SIZE) continue
|
|
402
|
+
for (const [id, a] of [...ownerOf]) if (a === anchor) ownerOf.delete(id)
|
|
403
|
+
counts.delete(anchor)
|
|
404
|
+
}
|
|
405
|
+
const clusters = [...counts]
|
|
406
|
+
.map(([anchor, count]) => ({ anchor, count }))
|
|
407
|
+
.sort((a, b) => b.count - a.count || a.anchor.localeCompare(b.anchor))
|
|
408
|
+
return { clusters, ownerOf, folded: ownerOf.size }
|
|
409
|
+
}
|
|
410
|
+
|
|
277
411
|
export async function buildSnapshot(listPlane, options = {}) {
|
|
278
412
|
const {
|
|
279
|
-
|
|
280
|
-
briefCount = 12,
|
|
281
|
-
indexCount = 120,
|
|
413
|
+
indexCount = 0, // 0 = 全部标题,不截断
|
|
282
414
|
residentLimit = 40,
|
|
283
|
-
briefChars = 140,
|
|
284
415
|
decayHalfLifeDays = 30,
|
|
285
416
|
accessBoost = 80,
|
|
286
417
|
access = { records: () => ({}) },
|
|
418
|
+
// 主题簇折叠用的检索探针。不传就不折叠,标题原样列出。
|
|
419
|
+
probeAnchor = null,
|
|
420
|
+
cluster = true,
|
|
287
421
|
} = options
|
|
288
422
|
|
|
289
423
|
const records = []
|
|
@@ -307,6 +441,7 @@ export async function buildSnapshot(listPlane, options = {}) {
|
|
|
307
441
|
const accessMap = access.records()
|
|
308
442
|
const now = Math.floor(Date.now() / 1000)
|
|
309
443
|
|
|
444
|
+
// 常驻层:全量正文。这几层是身份、画像和用户明确交代的短要点,每轮都该看得见全文。
|
|
310
445
|
const resident = RESIDENT_PLANES.map((plane) => {
|
|
311
446
|
const rows = records.filter((r) => r.plane === plane).slice(0, residentLimit)
|
|
312
447
|
if (rows.length === 0) return ''
|
|
@@ -314,6 +449,12 @@ export async function buildSnapshot(listPlane, options = {}) {
|
|
|
314
449
|
return `**${PLANE_LABEL[plane]}**(${rows.length} 条)\n${body}`
|
|
315
450
|
}).filter(Boolean).join('\n\n')
|
|
316
451
|
|
|
452
|
+
// 下三层:只给标题索引,一条正文都不注入。
|
|
453
|
+
//
|
|
454
|
+
// 之前把这里做成「前几条全量 + 一批摘要 + 其余标题」的三档,摘要档吃掉 26.9KB,
|
|
455
|
+
// 而摘要的价值很低——标题本来就说清了「这条讲什么」,摘要只是把同一件事再说半遍;
|
|
456
|
+
// 真正要正文的时候一定会去 recall,注入的摘要在那一刻也用不上。
|
|
457
|
+
// 只留标题后:体积降下来,语义也干净——注入负责「知道有什么」,检索负责「读什么」。
|
|
317
458
|
const decay = records
|
|
318
459
|
.filter((r) => DECAY_PLANES.includes(r.plane))
|
|
319
460
|
.map((r) => ({ record: r, score: scoreOf(r, accessMap[r.id], now, decayHalfLifeDays, accessBoost) }))
|
|
@@ -321,53 +462,53 @@ export async function buildSnapshot(listPlane, options = {}) {
|
|
|
321
462
|
// 而输入顺序一变快照就变、又会让上下文多一份副本。ID 是稳定的。
|
|
322
463
|
.sort((a, b) => b.score - a.score || String(a.record.id).localeCompare(String(b.record.id)))
|
|
323
464
|
|
|
324
|
-
|
|
325
|
-
decay.forEach((entry, index) => {
|
|
326
|
-
let tier = index < fullCount ? 'full' : index < fullCount + briefCount ? 'brief' : 'index'
|
|
327
|
-
const floor = PLANE_FLOOR[entry.record.plane]
|
|
328
|
-
if (floor && TIER_ORDER[tier] < TIER_ORDER[floor]) tier = floor
|
|
329
|
-
tiers.set(entry.record.id, tier)
|
|
330
|
-
})
|
|
331
|
-
|
|
332
|
-
const fullRows = []
|
|
333
|
-
const briefByPlane = new Map()
|
|
465
|
+
// 按平面归集,并把可验证的主题簇折叠成「锚点词×条数」。
|
|
334
466
|
const indexByPlane = new Map()
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
if (!indexByPlane.has(record.plane)) indexByPlane.set(record.plane, [])
|
|
344
|
-
indexByPlane.get(record.plane).push(record.title || '(无标题)')
|
|
467
|
+
let foldedTotal = 0
|
|
468
|
+
const clusterLines = []
|
|
469
|
+
if (cluster && typeof probeAnchor === 'function') {
|
|
470
|
+
const perPlane = new Map()
|
|
471
|
+
for (const entry of decay) {
|
|
472
|
+
const plane = entry.record.plane
|
|
473
|
+
if (!perPlane.has(plane)) perPlane.set(plane, [])
|
|
474
|
+
perPlane.get(plane).push(entry.record)
|
|
345
475
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
476
|
+
for (const plane of DECAY_PLANES) {
|
|
477
|
+
const rows = perPlane.get(plane) || []
|
|
478
|
+
if (rows.length === 0) continue
|
|
479
|
+
const { clusters, ownerOf, folded } = await clusterTitles(rows, probeAnchor)
|
|
480
|
+
foldedTotal += folded
|
|
481
|
+
const rest = rows.filter((r) => !ownerOf.has(r.id)).map((r) => r.title || '(无标题)')
|
|
482
|
+
const shownRest = indexCount > 0 ? rest.slice(0, indexCount) : rest
|
|
483
|
+
const restNote = rest.length - shownRest.length
|
|
484
|
+
const head = `**${PLANE_LABEL[plane]}**(${rows.length} 条`
|
|
485
|
+
const clusterNote = clusters.length
|
|
486
|
+
? `;${clusters.map((c) => `${c.anchor}×${c.count}`).join(' ')} 可用锚点词检索取回`
|
|
487
|
+
: ''
|
|
488
|
+
const body = shownRest.length ? `:${shownRest.join(' | ')}` : ''
|
|
489
|
+
const tail = restNote > 0 ? ` …另有 ${restNote} 条` : ''
|
|
490
|
+
clusterLines.push(`${head}${clusterNote})${body}${tail}`)
|
|
491
|
+
}
|
|
492
|
+
} else {
|
|
493
|
+
for (const entry of decay) {
|
|
494
|
+
const plane = entry.record.plane
|
|
495
|
+
if (!indexByPlane.has(plane)) indexByPlane.set(plane, [])
|
|
496
|
+
indexByPlane.get(plane).push(entry.record.title || '(无标题)')
|
|
497
|
+
}
|
|
498
|
+
for (const plane of DECAY_PLANES) {
|
|
356
499
|
const titles = indexByPlane.get(plane)
|
|
357
|
-
|
|
500
|
+
if (!titles) continue
|
|
501
|
+
const shown = indexCount > 0 ? titles.slice(0, indexCount) : titles
|
|
358
502
|
const rest = titles.length - shown.length
|
|
359
|
-
|
|
360
|
-
}
|
|
361
|
-
|
|
503
|
+
clusterLines.push(`**${PLANE_LABEL[plane]}**(${titles.length} 条):${shown.join(' | ')}${rest > 0 ? ` …另有 ${rest} 条` : ''}`)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
const indexBlock = clusterLines.join('\n')
|
|
362
507
|
|
|
363
508
|
const parts = [
|
|
364
509
|
'### 常驻层(永久全量:soul / profile / explicit)',
|
|
365
510
|
resident,
|
|
366
|
-
|
|
367
|
-
fullRows.join('\n'),
|
|
368
|
-
briefBlock ? '### 摘要(原文在库里,需要细节就检索)' : '',
|
|
369
|
-
briefBlock,
|
|
370
|
-
indexBlock ? '### 可召回索引(仅标题,用 aurorix_recall 取正文)' : '',
|
|
511
|
+
indexBlock ? '### 可召回索引(仅标题;正文用 aurorix_abook ["memory","read","--id","<id>","--json"] 取)' : '',
|
|
371
512
|
indexBlock,
|
|
372
513
|
].filter((part) => part && part.length > 0)
|
|
373
514
|
|
|
@@ -376,9 +517,8 @@ export async function buildSnapshot(listPlane, options = {}) {
|
|
|
376
517
|
stats: {
|
|
377
518
|
total: records.length,
|
|
378
519
|
resident: records.filter((r) => RESIDENT_PLANES.includes(r.plane)).length,
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
index: [...tiers.values()].filter((t) => t === 'index').length,
|
|
520
|
+
index: records.filter((r) => DECAY_PLANES.includes(r.plane)).length,
|
|
521
|
+
folded: foldedTotal,
|
|
382
522
|
},
|
|
383
523
|
}
|
|
384
524
|
}
|
package/package.json
CHANGED