dsh-aurorix-agentbook 1.10.0 → 1.11.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/snapshot.js +116 -41
- package/package.json +1 -1
package/lib/snapshot.js
CHANGED
|
@@ -23,6 +23,13 @@ const PLANE_LABEL = {
|
|
|
23
23
|
episodic: 'episodic 事件与过程',
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
// 下三层各自的索引渲染方式。
|
|
27
|
+
//
|
|
28
|
+
// rules 是规则类:标题就是规则的索引名,折叠成锚点词会让人看不出是哪几条规则,
|
|
29
|
+
// 等于把硬约束藏进一个词后面。所以规则永远纯标题,逐条列出。
|
|
30
|
+
// 另外两层是经验与过程记录,按主题折叠收益最大。
|
|
31
|
+
const PLANE_INDEX_MODE = { rules: 'titles', long_term: 'cluster', episodic: 'cluster' }
|
|
32
|
+
|
|
26
33
|
// 排序基准:身份/规则/偏好在最前,经验其次,过程记录最轻。
|
|
27
34
|
const PLANE_BASE = { soul: 1000, rules: 900, profile: 800, explicit: 700, long_term: 300, episodic: 120 }
|
|
28
35
|
|
|
@@ -329,13 +336,56 @@ const CLUSTER_CONCURRENCY = 6
|
|
|
329
336
|
// 不当作锚点的英文虚词。
|
|
330
337
|
const EN_STOP = new Set(['the', 'and', 'for', 'with', 'that', 'this', 'from', 'not', 'are', 'was', 'you', 'can', 'all', 'any', 'but', 'how', 'why'])
|
|
331
338
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
339
|
+
// 中文里不能当锚点的字词:
|
|
340
|
+
// - CJK_STOP 是虚词,n-gram 含它就丢(否则会挖出「的三」「加了」这种残片)。
|
|
341
|
+
// - RELATIONAL 是关系动词与泛化名词,出现频次高但成员之间毫无关系,
|
|
342
|
+
// 拿它当锚点等于把不相关的条目并成一簇。这份表是封闭集合,靠枚举维护。
|
|
343
|
+
const CJK_STOP = /[的了是和与及也都就要会能不对个条这那我你他它其之有没已可应该把被从向于为以所而且但或如果因]/
|
|
344
|
+
const RELATIONAL = new Set([
|
|
345
|
+
'导致', '删除', '失效', '收益', '批量', '格式', '同步', '接口', '搜索', '判断', '确认',
|
|
346
|
+
'增加', '减少', '出现', '存在', '包含', '支持', '使用', '实现', '完成', '发现', '需要',
|
|
347
|
+
'可以', '改成', '换成', '退还', '还原', '修复', '恢复', '返回', '调用', '写入', '读取',
|
|
348
|
+
'执行', '运行', '处理', '检查', '选择', '决定', '说明', '进行', '通过', '根据', '按照',
|
|
349
|
+
'由于', '因为', '所以', '但是', '而且', '或者', '如果', '虽然', '然后', '现在', '之后',
|
|
350
|
+
'之前', '时候', '东西', '问题', '方法', '方式', '情况', '部分', '内容', '结果', '原因',
|
|
351
|
+
'过程', '地方', '上面', '下面', '里面', '外面', '一个', '两个', '三个', '我们', '自己',
|
|
352
|
+
// 以下来自真实库的实测漏网:都是「某动作的泛称」,成员之间没有共同主题。
|
|
353
|
+
'先查', '区分', '生效', '安装', '验收', '接入', '本地', '原生', '助手', '代理',
|
|
354
|
+
'检测', '静默', '入口', '清单', '口径', '学分', '晋升', '精简', '凭据',
|
|
355
|
+
])
|
|
356
|
+
|
|
357
|
+
// 中文锚点长度上限。2 字太碎(「插件」可以但「的三」也会进来),
|
|
358
|
+
// 5 字以上几乎不会重复出现,取 2–4 字最平衡。
|
|
359
|
+
const CJK_MIN = 2
|
|
360
|
+
const CJK_MAX = 4
|
|
361
|
+
// 一个锚点覆盖的条目数上限:超过就说明它太泛(`必须` 能覆盖 43 条),
|
|
362
|
+
// 折成一簇会让模型以为「这一簇是同主题」,实际只是共享一个常用词。
|
|
363
|
+
const ANCHOR_MAX_MEMBERS = 18
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* 从标题里取候选锚点:拉丁技术实体 + 中文 2–4 字 n-gram。
|
|
367
|
+
*
|
|
368
|
+
* 中文 n-gram 会挖出大量残片,所以这里只做粗筛(去虚词、去关系词),
|
|
369
|
+
* 真正的判据是 clusterTitles 里的「检索召回率」——词不对就召不回成员,
|
|
370
|
+
* 那一簇自然被放弃。
|
|
371
|
+
*/
|
|
372
|
+
function anchorCandidates(title) {
|
|
373
|
+
const out = new Set()
|
|
374
|
+
const text = String(title)
|
|
375
|
+
for (const m of text.matchAll(/[A-Za-z][A-Za-z0-9_.\-]{2,}/g)) {
|
|
335
376
|
const key = m[0].toLowerCase()
|
|
336
|
-
if (!EN_STOP.has(key)) out.
|
|
377
|
+
if (!EN_STOP.has(key)) out.add(key)
|
|
337
378
|
}
|
|
338
|
-
|
|
379
|
+
const zh = text.replace(/[^\u4e00-\u9fa5]/g, '')
|
|
380
|
+
for (let n = CJK_MIN; n <= CJK_MAX; n++) {
|
|
381
|
+
for (let i = 0; i + n <= zh.length; i++) {
|
|
382
|
+
const gram = zh.slice(i, i + n)
|
|
383
|
+
if (CJK_STOP.test(gram)) continue
|
|
384
|
+
if (RELATIONAL.has(gram)) continue
|
|
385
|
+
out.add(gram)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return [...out]
|
|
339
389
|
}
|
|
340
390
|
|
|
341
391
|
/**
|
|
@@ -346,7 +396,7 @@ function latinAnchors(title) {
|
|
|
346
396
|
export async function clusterTitles(records, probe) {
|
|
347
397
|
const byAnchor = new Map()
|
|
348
398
|
for (const r of records) {
|
|
349
|
-
for (const a of
|
|
399
|
+
for (const a of anchorCandidates(r.title)) {
|
|
350
400
|
if (!byAnchor.has(a)) byAnchor.set(a, [])
|
|
351
401
|
const list = byAnchor.get(a)
|
|
352
402
|
if (!list.includes(r.id)) list.push(r.id)
|
|
@@ -356,8 +406,28 @@ export async function clusterTitles(records, probe) {
|
|
|
356
406
|
// 先按成员数从大到小验证,让大簇优先占住自己的成员。
|
|
357
407
|
// 每个锚点要跑一次检索,单次约 0.5 秒。锚点上百个串行就是几十秒,
|
|
358
408
|
// 所以按固定并发度并行;上限压住是为了不和正在跑的其它 abook 调用抢资源。
|
|
409
|
+
// 残片检测:如果存在一个更长、且出现次数完全相同的 n-gram,说明当前这个是
|
|
410
|
+
// 从更长词里截出来的——`忆库` 之于 `记忆库`。拿残片当锚点,模型搜出来的是
|
|
411
|
+
// 「所有带这两个字的标题」,不是一簇同主题记录。
|
|
412
|
+
//
|
|
413
|
+
// 判据很简单也很可靠:短词的出现集合是长词的超集,若两者次数相同,
|
|
414
|
+
// 说明短词每次出现都伴随那个长词,它就是残片。
|
|
415
|
+
const fragmentOf = (anchor, ids) => {
|
|
416
|
+
if (anchor.length > CJK_MAX) return false // 拉丁词不参与
|
|
417
|
+
for (const [other, otherIds] of byAnchor) {
|
|
418
|
+
if (other.length !== anchor.length + 1) continue
|
|
419
|
+
if (!other.includes(anchor)) continue
|
|
420
|
+
if (otherIds.length !== ids.length) continue
|
|
421
|
+
// 次数相同还要看集合是否包含:避免偶然同频的误判
|
|
422
|
+
const set = new Set(otherIds)
|
|
423
|
+
if (ids.every((id) => set.has(id))) return true
|
|
424
|
+
}
|
|
425
|
+
return false
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// 成员太多(覆盖大半个平面)的词太泛,折成一簇会骗人;成员太少的没有折叠价值。
|
|
359
429
|
const candidates = [...byAnchor]
|
|
360
|
-
.filter(([, ids]) => ids.length >= CLUSTER_MIN_SIZE)
|
|
430
|
+
.filter(([anchor, ids]) => ids.length >= CLUSTER_MIN_SIZE && ids.length <= ANCHOR_MAX_MEMBERS && !fragmentOf(anchor, ids))
|
|
361
431
|
.sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]))
|
|
362
432
|
.slice(0, CLUSTER_MAX_ANCHORS) // 只验证最可能成簇的那些,剩下的本来也不值得折叠
|
|
363
433
|
|
|
@@ -462,47 +532,52 @@ export async function buildSnapshot(listPlane, options = {}) {
|
|
|
462
532
|
// 而输入顺序一变快照就变、又会让上下文多一份副本。ID 是稳定的。
|
|
463
533
|
.sort((a, b) => b.score - a.score || String(a.record.id).localeCompare(String(b.record.id)))
|
|
464
534
|
|
|
465
|
-
//
|
|
466
|
-
|
|
535
|
+
// 按平面分别渲染索引。
|
|
536
|
+
//
|
|
537
|
+
// 每个平面怎么渲染由 PLANE_INDEX_MODE 决定,不是一刀切:
|
|
538
|
+
// - rules 是「规则类」,规则标题本身就是规则的索引,折叠成 `aurorix×3`
|
|
539
|
+
// 会让人看不出是哪三条规则——等于把硬约束藏起来。所以规则一律纯标题。
|
|
540
|
+
// - long_term / episodic 是经验与过程记录,按主题折叠收益最大,用锚点词。
|
|
541
|
+
//
|
|
542
|
+
// 分组全部在构建时算出来,不落库、不依赖谁事先预处理:同一份库每次算出的
|
|
543
|
+
// 结果都一样,新增一条记录下一次构建就自动归进对应主题。
|
|
467
544
|
let foldedTotal = 0
|
|
468
545
|
const clusterLines = []
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
const
|
|
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) {
|
|
499
|
-
const titles = indexByPlane.get(plane)
|
|
500
|
-
if (!titles) continue
|
|
546
|
+
const perPlane = new Map()
|
|
547
|
+
for (const entry of decay) {
|
|
548
|
+
const plane = entry.record.plane
|
|
549
|
+
if (!perPlane.has(plane)) perPlane.set(plane, [])
|
|
550
|
+
perPlane.get(plane).push(entry.record)
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
for (const plane of DECAY_PLANES) {
|
|
554
|
+
const rows = perPlane.get(plane) || []
|
|
555
|
+
if (rows.length === 0) continue
|
|
556
|
+
const mode = PLANE_INDEX_MODE[plane] || 'cluster'
|
|
557
|
+
const wantCluster = cluster && mode === 'cluster' && typeof probeAnchor === 'function'
|
|
558
|
+
|
|
559
|
+
if (!wantCluster) {
|
|
560
|
+
const titles = rows.map((r) => r.title || '(无标题)')
|
|
501
561
|
const shown = indexCount > 0 ? titles.slice(0, indexCount) : titles
|
|
502
562
|
const rest = titles.length - shown.length
|
|
503
563
|
clusterLines.push(`**${PLANE_LABEL[plane]}**(${titles.length} 条):${shown.join(' | ')}${rest > 0 ? ` …另有 ${rest} 条` : ''}`)
|
|
564
|
+
continue
|
|
504
565
|
}
|
|
566
|
+
|
|
567
|
+
const { clusters, ownerOf, folded } = await clusterTitles(rows, probeAnchor)
|
|
568
|
+
foldedTotal += folded
|
|
569
|
+
// 未成簇的条目按原排序列出;它们没有可靠的检索锚点,必须逐条给标题。
|
|
570
|
+
const rest = rows.filter((r) => !ownerOf.has(r.id)).map((r) => r.title || '(无标题)')
|
|
571
|
+
const shownRest = indexCount > 0 ? rest.slice(0, indexCount) : rest
|
|
572
|
+
const restNote = rest.length - shownRest.length
|
|
573
|
+
const clusterNote = clusters.length
|
|
574
|
+
? `;主题簇 ${clusters.map((c) => `${c.anchor}×${c.count}`).join(' ')}(用该词检索可取回)`
|
|
575
|
+
: ''
|
|
576
|
+
const body = shownRest.length ? `:${shownRest.join(' | ')}` : ''
|
|
577
|
+
const tail = restNote > 0 ? ` …另有 ${restNote} 条` : ''
|
|
578
|
+
clusterLines.push(`**${PLANE_LABEL[plane]}**(${rows.length} 条${clusterNote})${body}${tail}`)
|
|
505
579
|
}
|
|
580
|
+
|
|
506
581
|
const indexBlock = clusterLines.join('\n')
|
|
507
582
|
|
|
508
583
|
const parts = [
|
package/package.json
CHANGED