dsh-aurorix-agentbook 1.10.1 → 1.12.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/README.md +18 -6
- package/cordis.patch.yml +2 -2
- package/lib/capability.js +36 -3
- package/lib/host.js +14 -8
- package/lib/snapshot.js +68 -201
- package/lib/theme.js +225 -0
- package/lib/view.js +1 -1
- package/lib/words.js +2 -2
- package/package.json +1 -1
- package/vendor/SHA256SUMS +15 -15
- package/vendor/abook-aarch64-apple-darwin +0 -0
- package/vendor/abook-aarch64-linux-android +0 -0
- package/vendor/abook-aarch64-pc-windows-gnullvm.exe +0 -0
- package/vendor/abook-aarch64-unknown-linux-musl +0 -0
- package/vendor/abook-armv7-linux-androideabi +0 -0
- package/vendor/abook-armv7-unknown-linux-musleabihf +0 -0
- package/vendor/abook-i686-linux-android +0 -0
- package/vendor/abook-i686-pc-windows-gnullvm.exe +0 -0
- package/vendor/abook-i686-unknown-linux-musl +0 -0
- package/vendor/abook-loongarch64-unknown-linux-gnu +0 -0
- package/vendor/abook-riscv64gc-unknown-linux-gnu +0 -0
- package/vendor/abook-x86_64-apple-darwin +0 -0
- package/vendor/abook-x86_64-linux-android +0 -0
- package/vendor/abook-x86_64-pc-windows-gnullvm.exe +0 -0
- package/vendor/abook-x86_64-unknown-linux-musl +0 -0
package/README.md
CHANGED
|
@@ -32,13 +32,24 @@ Harness Web 上,顶部右侧那组操作按钮里会多一个记忆库按钮
|
|
|
32
32
|
|
|
33
33
|
1. **协议段落**——告诉模型读用 `aurorix_recall`、写用 `aurorix_remember`,以及什么该写进哪个平面。
|
|
34
34
|
2. **分层快照**——把库压成一段能放进上下文的文本:
|
|
35
|
-
- 常驻层(soul / profile / explicit
|
|
36
|
-
-
|
|
37
|
-
-
|
|
35
|
+
- 常驻层(soul / profile / explicit)永久全量正文;
|
|
36
|
+
- `rules` 永远逐条列标题:规则名本身就是索引,折成一个词等于把硬约束藏起来;
|
|
37
|
+
- `long_term` / `episodic` 按「平面基准 + 使用加成 − 时间衰减」排序,标题交给
|
|
38
|
+
`abook memory themes` 聚成主题簇,同一主题的若干条折成一个标签加条数;
|
|
39
|
+
- 没折进簇的条目逐条列标题,一条都不会消失。
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
主题簇的标签后面带簇 id,例如 `环境变量·库根·程序×3#theme-3f9a1c07d2b4`。
|
|
42
|
+
想看这一簇到底有哪几条:
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
```json
|
|
45
|
+
["theme-members", "--algorithm", "aplusplus", "--input", "title", "--cluster", "theme-3f9a1c07d2b4", "--json"]
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
这条走的是簇 id,不是关键词检索,所以不受召回率影响:簇里有几条就展开几条。
|
|
49
|
+
|
|
50
|
+
在默认配置下,主题簇直接替换模型快照里的一组标题,不需要另开面板。跨平面的主题放在共同的句云区;未折叠的记录仍在各自平面逐条列出。主题过时或使用不带该能力的自定义后端时,可用普通召回找原文。
|
|
51
|
+
|
|
52
|
+
分组只在库变化时重算;库没变就复用快照。
|
|
42
53
|
|
|
43
54
|
## 后端二进制
|
|
44
55
|
|
|
@@ -127,7 +138,8 @@ node --test test/*.test.js
|
|
|
127
138
|
| `cordis.patch.yml` | 17 项配置与默认值 |
|
|
128
139
|
| `lib/index.js` | 界面层:面板、状态条、胶囊、消息菜单 |
|
|
129
140
|
| `lib/host.js` | 宿主层:提示词注入、工具、斜杠命令 |
|
|
130
|
-
| `lib/snapshot.js` |
|
|
141
|
+
| `lib/snapshot.js` | 分层快照、热度衰减、主题簇折叠 |
|
|
142
|
+
| `lib/theme.js` | 主题簇:调 `memory themes` / `theme-members`、能力探测与缓存 |
|
|
131
143
|
| `lib/abook.js` | 找二进制、跑命令、解析 JSON |
|
|
132
144
|
| `lib/view.js` | 面板视图与数据模型 |
|
|
133
145
|
| `lib/words.js` | 中英文案与平面名 |
|
package/cordis.patch.yml
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
# residentLimit 常驻层每平面最多注入多少条
|
|
11
11
|
# fullCount 衰减层里注入全量正文的条数
|
|
12
12
|
# briefCount 衰减层里注入摘要的条数
|
|
13
|
-
# indexCount
|
|
13
|
+
# indexCount 每个平面最多列多少条未折叠标题;0 表示全部
|
|
14
14
|
# briefChars 摘要档每条取多少字
|
|
15
15
|
# decayHalfLifeDays 热度半衰期(天);0 表示不衰减
|
|
16
16
|
# accessBoost 被用过一次的加成
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
residentLimit: 40
|
|
31
31
|
fullCount: 6
|
|
32
32
|
briefCount: 12
|
|
33
|
-
indexCount:
|
|
33
|
+
indexCount: 0
|
|
34
34
|
briefChars: 110
|
|
35
35
|
decayHalfLifeDays: 30
|
|
36
36
|
accessBoost: 80
|
package/lib/capability.js
CHANGED
|
@@ -52,6 +52,26 @@ export const COMMAND_GROUPS = [
|
|
|
52
52
|
{ name: 'index', what: 'Index.md 路由 + 预加载平面;--plane D5 取该条全文' },
|
|
53
53
|
{ name: 'identity', what: '凭据身份索引(只有标识,不含任何凭据值)' },
|
|
54
54
|
{ name: 'refs', what: '检查正文里的引用是否指得着;--fix 只修能唯一推导的' },
|
|
55
|
+
// 这两条只有新版二进制才有。旧二进制(以及用户自己配的老 abook)没有它们,
|
|
56
|
+
// 所以标 optional:对账时不算「地图写错了」,单独列一行说明当前这份跑不了。
|
|
57
|
+
{
|
|
58
|
+
name: 'memory themes',
|
|
59
|
+
what: '把 long_term / episodic 的标题聚成主题簇(本地算,不写库)。带稳定簇 id,可拿去展开',
|
|
60
|
+
args: 'memory themes --algorithm aplusplus --input title --json',
|
|
61
|
+
optional: true,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'theme-search',
|
|
65
|
+
what: '按查询给记录标题排序;精确展开已知主题仍用 theme-members',
|
|
66
|
+
args: 'theme-search --algorithm aplusplus --input title --query <词> --limit 10 --json',
|
|
67
|
+
optional: true,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: 'theme-members',
|
|
71
|
+
what: '展开一簇的成员:--cluster <簇id> 精确取,或 --id <记录id> 看它落在哪几簇',
|
|
72
|
+
args: 'theme-members --algorithm aplusplus --input title --cluster <id> --json',
|
|
73
|
+
optional: true,
|
|
74
|
+
},
|
|
55
75
|
],
|
|
56
76
|
},
|
|
57
77
|
{
|
|
@@ -170,7 +190,9 @@ export function capabilityText() {
|
|
|
170
190
|
const blocks = COMMAND_GROUPS.map((group) => {
|
|
171
191
|
const lines = group.commands.map((c) => {
|
|
172
192
|
const base = `- \`${c.name}\`:${c.what}`
|
|
173
|
-
|
|
193
|
+
const tail = c.args === undefined ? base : `${base}\n 用法:\`${c.args}\``
|
|
194
|
+
// 需要新版二进制的命令要写明,否则读者会在旧二进制上试了再回来问。
|
|
195
|
+
return c.optional === true ? `${tail}\n 需要新版 abook;当前二进制没有时会退回纯标题` : tail
|
|
174
196
|
})
|
|
175
197
|
return `**${group.title}**\n${group.hint}\n${lines.join('\n')}`
|
|
176
198
|
})
|
|
@@ -242,21 +264,32 @@ export function driftOf(described) {
|
|
|
242
264
|
if (described?.ok !== true) return { ok: false, reason: described?.reason ?? '没有契约数据' }
|
|
243
265
|
const known = new Set(described.names)
|
|
244
266
|
const mapped = new Set()
|
|
267
|
+
const optional = new Set()
|
|
245
268
|
const subcommands = new Set()
|
|
246
269
|
for (const group of COMMAND_GROUPS) {
|
|
247
270
|
for (const c of group.commands) {
|
|
248
271
|
for (const alt of c.name.split('|').map((x) => x.trim()).filter(Boolean)) {
|
|
249
272
|
const [head, ...rest] = alt.split(/\s+/)
|
|
250
273
|
mapped.add(head)
|
|
274
|
+
if (c.optional === true) optional.add(head)
|
|
251
275
|
// 「vault import」「memory search」里的第二个词是子命令,不是顶层命令,
|
|
252
276
|
// 不能拿去和 `commands` 的输出对账——那边只有顶层命令名。
|
|
253
277
|
if (rest.length > 0) subcommands.add(`${head} ${rest.join(' ')}`)
|
|
254
278
|
}
|
|
255
279
|
}
|
|
256
280
|
}
|
|
257
|
-
|
|
281
|
+
// 硬缺失:地图说有、二进制确实没有,且不是「需要新版」的那类——这是真写错了。
|
|
282
|
+
const missing = [...mapped].filter((n) => !known.has(n) && !optional.has(n))
|
|
283
|
+
// 软缺失:需要新版二进制,当前这份没有。不算错,但要让人看见。
|
|
284
|
+
const optionalMissing = [...optional].filter((n) => !known.has(n))
|
|
258
285
|
const uncovered = [...known].filter((n) => !mapped.has(n))
|
|
259
|
-
return {
|
|
286
|
+
return {
|
|
287
|
+
ok: missing.length === 0 && uncovered.length === 0,
|
|
288
|
+
missing,
|
|
289
|
+
optionalMissing: optionalMissing.sort(),
|
|
290
|
+
uncovered,
|
|
291
|
+
subcommands: [...subcommands].length,
|
|
292
|
+
}
|
|
260
293
|
}
|
|
261
294
|
|
|
262
295
|
/** 地图里每条命令拼成 CLI 手册可用的简表。 */
|
package/lib/host.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// 每个服务都用 ctx.inject 软依赖:宿主没有 systemPrompt(或 tools、commands)
|
|
7
7
|
// 时,那一块就不装,其余部分照常工作,整个插件不会因此起不来。
|
|
8
8
|
import { spawn } from 'node:child_process'
|
|
9
|
-
import { delimiter, dirname } from 'node:path'
|
|
9
|
+
import { delimiter, dirname, join } from 'node:path'
|
|
10
10
|
// 二进制真实存在的 attach 子命令只有这五个:sync/refs 是别处的命令,放这里会静默出错。
|
|
11
11
|
const ATTACH_ACTIONS = ['list', 'add', 'get', 'rm', 'path', 'check']
|
|
12
12
|
const describeCache = { at: 0, value: undefined }
|
|
@@ -55,8 +55,8 @@ import {
|
|
|
55
55
|
DEFAULT_PROTOCOL,
|
|
56
56
|
resolveStatePath,
|
|
57
57
|
librarySignature,
|
|
58
|
-
makeAnchorProbe,
|
|
59
58
|
} from './snapshot.js'
|
|
59
|
+
import { createThemeClient } from './theme.js'
|
|
60
60
|
import { GUIDE, GUIDE_INDEX } from './guide.js'
|
|
61
61
|
import { CURATION_MANUAL } from './curation.js'
|
|
62
62
|
import { COMMAND_GROUPS, capabilityText, describeAll, driftOf } from './capability.js'
|
|
@@ -151,9 +151,12 @@ export function attachHost(ctx, deps) {
|
|
|
151
151
|
|
|
152
152
|
const doRefresh = async (current) => {
|
|
153
153
|
try {
|
|
154
|
-
//
|
|
155
|
-
//
|
|
156
|
-
const
|
|
154
|
+
// 主题簇由二进制算(`memory themes`),插件不再自己按锚点词跑几十次检索。
|
|
155
|
+
// 只在库变化重建时跑一次,约半秒;库没变时整条刷新链根本不会走到这里。
|
|
156
|
+
const themeClient = createThemeClient(backend, {
|
|
157
|
+
// 探测结论按二进制身份缓存:旧二进制只会失败一次,不是每轮都重探。
|
|
158
|
+
cachePath: join(dirname(resolveStatePath(cfg().statePath)), 'theme-probe.json'),
|
|
159
|
+
})
|
|
157
160
|
const built = await buildSnapshot((plane) => backend.list(plane, { limit: 2000, full: true }), {
|
|
158
161
|
indexCount: current.indexCount,
|
|
159
162
|
residentLimit: current.residentLimit,
|
|
@@ -161,9 +164,8 @@ export function attachHost(ctx, deps) {
|
|
|
161
164
|
accessBoost: current.accessBoost,
|
|
162
165
|
maxContextBytes: current.maxContextBytes,
|
|
163
166
|
access,
|
|
164
|
-
|
|
167
|
+
themes: () => themeClient.themes(),
|
|
165
168
|
})
|
|
166
|
-
if (typeof probeAnchor?.save === 'function') probeAnchor.save()
|
|
167
169
|
snapshot.text = built.text
|
|
168
170
|
snapshot.stats = built.stats
|
|
169
171
|
snapshot.error = ''
|
|
@@ -457,10 +459,14 @@ export function attachHost(ctx, deps) {
|
|
|
457
459
|
const described = await describeAll(backend, { cache: describeCache })
|
|
458
460
|
const drift = driftOf(described)
|
|
459
461
|
if (drift.ok !== true && drift.reason !== undefined) return { code: 2, text: `对账失败:${drift.reason}` }
|
|
462
|
+
const optionalMissing = Array.isArray(drift.optionalMissing) ? drift.optionalMissing : []
|
|
460
463
|
const lines = [
|
|
461
464
|
`二进制:${described.names.length} 个命令`,
|
|
462
465
|
`地图有、二进制没有:${drift.missing.length === 0 ? '无' : drift.missing.join(', ')}`,
|
|
463
466
|
`二进制有、地图没写:${drift.uncovered.length === 0 ? '无' : drift.uncovered.join(', ')}`,
|
|
467
|
+
// 需要新版二进制的命令单独一行:它不影响对账结论,但用户会想知道
|
|
468
|
+
// 为什么主题簇在这台机器上跑不了。
|
|
469
|
+
`需要新版二进制、当前没有:${optionalMissing.length === 0 ? '无' : optionalMissing.join(', ')}`,
|
|
464
470
|
]
|
|
465
471
|
return { code: drift.ok ? 0 : 3, text: lines.join('\n') }
|
|
466
472
|
}
|
|
@@ -868,7 +874,7 @@ export function attachHost(ctx, deps) {
|
|
|
868
874
|
ageSeconds: snapshot.fetchedAt ? Math.max(0, Math.round((Date.now() - snapshot.fetchedAt) / 1000)) : undefined,
|
|
869
875
|
changed: enabled && snapshot.signature !== '' && currentSig !== '' && currentSig !== snapshot.signature,
|
|
870
876
|
tierLabel: snapshot.stats
|
|
871
|
-
? `${snapshot.stats.resident}常驻/${snapshot.stats.
|
|
877
|
+
? `${snapshot.stats.resident}常驻/${snapshot.stats.index}索引`
|
|
872
878
|
: '',
|
|
873
879
|
locale: typeof locale === 'string' ? locale : undefined,
|
|
874
880
|
}
|
package/lib/snapshot.js
CHANGED
|
@@ -28,7 +28,7 @@ const PLANE_LABEL = {
|
|
|
28
28
|
// rules 是规则类:标题就是规则的索引名,折叠成锚点词会让人看不出是哪几条规则,
|
|
29
29
|
// 等于把硬约束藏进一个词后面。所以规则永远纯标题,逐条列出。
|
|
30
30
|
// 另外两层是经验与过程记录,按主题折叠收益最大。
|
|
31
|
-
const
|
|
31
|
+
const THEME_PLANES = new Set(['long_term', 'episodic'])
|
|
32
32
|
|
|
33
33
|
// 排序基准:身份/规则/偏好在最前,经验其次,过程记录最轻。
|
|
34
34
|
const PLANE_BASE = { soul: 1000, rules: 900, profile: 800, explicit: 700, long_term: 300, episodic: 120 }
|
|
@@ -93,7 +93,7 @@ export const DEFAULT_MANUAL = [
|
|
|
93
93
|
"",
|
|
94
94
|
"它每轮重发、不留在历史里,所以对话被压缩之后照样在。",
|
|
95
95
|
"",
|
|
96
|
-
"
|
|
96
|
+
"常驻层保留正文,规则逐条列标题。长期经验和事件的标题按句云折叠,未折叠的标题仍单列。句云按快照中的命令展开,其他标题可用 `aurorix_recall` 找记录,再读正文。采用前核对正文里的版本与适用范围。",
|
|
97
97
|
].join('\n')
|
|
98
98
|
|
|
99
99
|
// 旧版把「协议段落」与「手册」分成两份注入,内容大量重复。
|
|
@@ -255,164 +255,47 @@ export function librarySignature(root) {
|
|
|
255
255
|
return count === 0 ? '0:0' : `${count}:${hash >>> 0}`
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
/**
|
|
259
|
-
|
|
260
|
-
* @param {(plane: string) => Promise<{ ok: boolean, items?: object[], reason?: string }>} listPlane
|
|
261
|
-
*/
|
|
262
|
-
/**
|
|
263
|
-
* 主题簇折叠的检索探针,按库指纹缓存到磁盘。
|
|
264
|
-
*
|
|
265
|
-
* 验证一个锚点要跑一次检索(约 0.5 秒),锚点几十个就是几秒。库没变时验证结果
|
|
266
|
-
* 必然相同,所以同一份库只该验证一次。缓存里存「锚点 → 命中的记录 ID」。
|
|
267
|
-
*
|
|
268
|
-
* 库指纹一变整份作废——宁可重算,也不拿旧指纹的结论去折叠新库:那会让模型
|
|
269
|
-
* 搜不到本来存在的条目。
|
|
270
|
-
*/
|
|
271
|
-
export function makeAnchorProbe(backend, signature, cachePath) {
|
|
272
|
-
if (typeof backend?.search !== 'function') return null
|
|
273
|
-
if (!signature) return null
|
|
274
|
-
|
|
275
|
-
const file = cachePath || join(dirname(resolveStatePath('')), `clusters-${signature}.json`)
|
|
276
|
-
let cache = {}
|
|
277
|
-
try {
|
|
278
|
-
const raw = JSON.parse(readFileSync(file, 'utf8'))
|
|
279
|
-
// 指纹对不上就整份丢掉。
|
|
280
|
-
if (raw && raw.signature === signature && raw.anchors && typeof raw.anchors === 'object') cache = raw.anchors
|
|
281
|
-
} catch {
|
|
282
|
-
cache = {}
|
|
283
|
-
}
|
|
284
|
-
const inFlight = new Map()
|
|
285
|
-
|
|
286
|
-
const probe = async (anchor) => {
|
|
287
|
-
const key = String(anchor).toLowerCase()
|
|
288
|
-
if (cache[key]) return new Set(cache[key])
|
|
289
|
-
if (inFlight.has(key)) return inFlight.get(key) // 同轮同锚点只查一次
|
|
290
|
-
const task = (async () => {
|
|
291
|
-
const result = await backend.search(key, 60)
|
|
292
|
-
// 两种形状都要认:插件 backend.search 返回 {ok,items},裸 abook JSON 是 {items,total}。
|
|
293
|
-
// 只看 ok 会在后者上永远判成失败,折叠静默归零——这正是最初把折叠数做成 0 的原因。
|
|
294
|
-
const items = Array.isArray(result?.items) ? result.items : []
|
|
295
|
-
const ok = result?.ok !== false && items.length >= 0 && Array.isArray(result?.items)
|
|
296
|
-
const ids = ok ? items.map((x) => String(x.id ?? '')).filter(Boolean) : []
|
|
297
|
-
if (ok) cache[key] = ids
|
|
298
|
-
return new Set(ids)
|
|
299
|
-
})()
|
|
300
|
-
inFlight.set(key, task)
|
|
301
|
-
try {
|
|
302
|
-
return await task
|
|
303
|
-
} finally {
|
|
304
|
-
inFlight.delete(key)
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// 构建结束后由调用方落盘:避免每个锚点都写一次文件。
|
|
309
|
-
probe.save = () => {
|
|
310
|
-
try {
|
|
311
|
-
mkdirSync(dirname(file), { recursive: true })
|
|
312
|
-
const tmp = `${file}.tmp`
|
|
313
|
-
writeFileSync(tmp, JSON.stringify({ signature, anchors: cache }), 'utf8')
|
|
314
|
-
renameSync(tmp, file)
|
|
315
|
-
} catch {
|
|
316
|
-
// 缓存写不进去不影响正确性,下次重算而已。
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
return probe
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// 主题簇折叠。
|
|
323
|
-
//
|
|
324
|
-
// 只认「具体技术实体」当锚点:`abook`、`zygisk`、`xqdk` 这类词检索起来能精准命中
|
|
325
|
-
// 同一批记录;`验证`、`必须`、`android` 这类泛词不行——实测 android 只能召回 19%,
|
|
326
|
-
// 拿它当锚点等于告诉模型「去搜这个词」然后什么也找不到。
|
|
327
|
-
//
|
|
328
|
-
// 判定可验证:锚点词检索回来必须覆盖该簇至少 80% 的条目。达不到就整簇放弃、原样列出。
|
|
329
|
-
const CLUSTER_MIN_RECALL = 0.8
|
|
330
|
-
const CLUSTER_MIN_SIZE = 2
|
|
331
|
-
// 验证成本 = 锚点数 × 单次检索耗时(约 0.5 秒)。这两个上限把最坏情况压到
|
|
332
|
-
// 「几十次检索、几秒」,而不是「几百次、几十秒」。
|
|
333
|
-
const CLUSTER_MAX_ANCHORS = 60
|
|
334
|
-
const CLUSTER_CONCURRENCY = 6
|
|
335
|
-
|
|
336
|
-
// 不当作锚点的英文虚词。
|
|
337
|
-
const EN_STOP = new Set(['the', 'and', 'for', 'with', 'that', 'this', 'from', 'not', 'are', 'was', 'you', 'can', 'all', 'any', 'but', 'how', 'why'])
|
|
338
|
-
|
|
339
|
-
function latinAnchors(title) {
|
|
340
|
-
const out = []
|
|
341
|
-
for (const m of String(title).matchAll(/[A-Za-z][A-Za-z0-9_.\-]{2,}/g)) {
|
|
342
|
-
const key = m[0].toLowerCase()
|
|
343
|
-
if (!EN_STOP.has(key)) out.push(key)
|
|
344
|
-
}
|
|
345
|
-
return [...new Set(out)]
|
|
346
|
-
}
|
|
258
|
+
/** 一簇至少几条才值得折叠:单条折过去只剩一个标签,信息量为零。 */
|
|
259
|
+
const FOLD_MIN_SIZE = 2
|
|
347
260
|
|
|
348
261
|
/**
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
262
|
+
* Select complete clusters from the experience planes. Mixed-plane groups are
|
|
263
|
+
* shown under their own heading. A record covered by a selected group is never
|
|
264
|
+
* hidden by a second, partial group, since expansion returns the complete group.
|
|
352
265
|
*/
|
|
353
|
-
export
|
|
354
|
-
const
|
|
355
|
-
for (const r of records) {
|
|
356
|
-
for (const a of latinAnchors(r.title)) {
|
|
357
|
-
if (!byAnchor.has(a)) byAnchor.set(a, [])
|
|
358
|
-
const list = byAnchor.get(a)
|
|
359
|
-
if (!list.includes(r.id)) list.push(r.id)
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
// 先按成员数从大到小验证,让大簇优先占住自己的成员。
|
|
364
|
-
// 每个锚点要跑一次检索,单次约 0.5 秒。锚点上百个串行就是几十秒,
|
|
365
|
-
// 所以按固定并发度并行;上限压住是为了不和正在跑的其它 abook 调用抢资源。
|
|
366
|
-
const candidates = [...byAnchor]
|
|
367
|
-
.filter(([, ids]) => ids.length >= CLUSTER_MIN_SIZE)
|
|
368
|
-
.sort((a, b) => b[1].length - a[1].length || a[0].localeCompare(b[0]))
|
|
369
|
-
.slice(0, CLUSTER_MAX_ANCHORS) // 只验证最可能成簇的那些,剩下的本来也不值得折叠
|
|
370
|
-
|
|
371
|
-
// 验证结果先按键存进 Map,最后再按 candidates 的原顺序取回。
|
|
372
|
-
// 直接用 push 收集会让顺序取决于哪个 worker 先跑完——同一份库两次构建
|
|
373
|
-
// 可能折出不同结果,快照文本一变宿主就在历史里多留一份副本。
|
|
374
|
-
const passed = new Map()
|
|
375
|
-
let cursor = 0
|
|
376
|
-
const worker = async () => {
|
|
377
|
-
while (cursor < candidates.length) {
|
|
378
|
-
const [anchor, ids] = candidates[cursor++]
|
|
379
|
-
let hit
|
|
380
|
-
try {
|
|
381
|
-
hit = await probe(anchor)
|
|
382
|
-
} catch {
|
|
383
|
-
continue // 检索失败就当这个锚点不可用,宁可不成簇
|
|
384
|
-
}
|
|
385
|
-
if (!hit || hit.size === 0) continue
|
|
386
|
-
const covered = ids.filter((id) => hit.has(id)).length
|
|
387
|
-
if (covered / ids.length >= CLUSTER_MIN_RECALL) passed.set(anchor, ids)
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
// 注意:worker 数量超过 1 时 probe 会被并发调用,调用方必须能承受并发
|
|
391
|
-
// (插件侧走的是 async 子进程,不是 execFileSync)。
|
|
392
|
-
await Promise.all(Array.from({ length: Math.min(CLUSTER_CONCURRENCY, candidates.length) }, worker))
|
|
393
|
-
|
|
394
|
-
// 按原顺序(成员数从多到少,同数按锚点字典序)重放,确保确定性。
|
|
395
|
-
const verified = candidates
|
|
396
|
-
.filter(([anchor]) => passed.has(anchor))
|
|
397
|
-
.map(([anchor, ids]) => ({ anchor, ids }))
|
|
398
|
-
|
|
266
|
+
export function foldThemes(rows, clusters) {
|
|
267
|
+
const byId = new Map(rows.filter((r) => THEME_PLANES.has(r.plane)).map((r) => [String(r.id), r]))
|
|
399
268
|
const ownerOf = new Map()
|
|
400
|
-
|
|
401
|
-
|
|
269
|
+
const folds = []
|
|
270
|
+
|
|
271
|
+
// 顺序必须定死:同样大小的簇按短语排,短语再相同按 id 排。
|
|
272
|
+
// 否则两份等价输入可能折出不同结果,快照文本一变宿主就多留一份副本。
|
|
273
|
+
const ordered = [...clusters].sort((a, b) => {
|
|
274
|
+
const size = (b.members?.length ?? 0) - (a.members?.length ?? 0)
|
|
275
|
+
if (size !== 0) return size
|
|
276
|
+
const phrase = String(a.phrase ?? '').localeCompare(String(b.phrase ?? ''))
|
|
277
|
+
if (phrase !== 0) return phrase
|
|
278
|
+
return String(a.id ?? '').localeCompare(String(b.id ?? ''))
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
for (const cluster of ordered) {
|
|
282
|
+
const members = Array.isArray(cluster.members) ? cluster.members : []
|
|
283
|
+
const id = String(cluster.id ?? '')
|
|
284
|
+
const phrase = String(cluster.phrase ?? '').trim()
|
|
285
|
+
if (!id || !phrase || members.length < FOLD_MIN_SIZE) continue
|
|
286
|
+
// Do not silently turn a mixed or overlapping cluster into a partial one:
|
|
287
|
+
// the ID expands to the complete group, so the label and count must agree.
|
|
288
|
+
const ids = new Set(members.map((member) => String(member?.id ?? '')))
|
|
289
|
+
if (ids.size !== members.length || ids.has('')) continue
|
|
290
|
+
if (members.some(({ id, plane }) => !byId.has(id) || byId.get(id).plane !== plane || ownerOf.has(id))) continue
|
|
291
|
+
const label = `${phrase}×${members.length}#${id}`
|
|
292
|
+
const titles = members.map((member) => byId.get(String(member.id)).title || '(无标题)')
|
|
293
|
+
if (Buffer.byteLength(label, 'utf8') >= Buffer.byteLength(titles.join(' | '), 'utf8')) continue
|
|
294
|
+
for (const memberId of ids) ownerOf.set(memberId, id)
|
|
295
|
+
folds.push({ id, phrase, count: members.length })
|
|
402
296
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
const counts = new Map()
|
|
406
|
-
for (const anchor of ownerOf.values()) counts.set(anchor, (counts.get(anchor) || 0) + 1)
|
|
407
|
-
for (const [anchor, count] of [...counts]) {
|
|
408
|
-
if (count >= CLUSTER_MIN_SIZE) continue
|
|
409
|
-
for (const [id, a] of [...ownerOf]) if (a === anchor) ownerOf.delete(id)
|
|
410
|
-
counts.delete(anchor)
|
|
411
|
-
}
|
|
412
|
-
const clusters = [...counts]
|
|
413
|
-
.map(([anchor, count]) => ({ anchor, count }))
|
|
414
|
-
.sort((a, b) => b.count - a.count || a.anchor.localeCompare(b.anchor))
|
|
415
|
-
return { clusters, ownerOf, folded: ownerOf.size }
|
|
297
|
+
|
|
298
|
+
return { folds, ownerOf }
|
|
416
299
|
}
|
|
417
300
|
|
|
418
301
|
export async function buildSnapshot(listPlane, options = {}) {
|
|
@@ -422,8 +305,9 @@ export async function buildSnapshot(listPlane, options = {}) {
|
|
|
422
305
|
decayHalfLifeDays = 30,
|
|
423
306
|
accessBoost = 80,
|
|
424
307
|
access = { records: () => ({}) },
|
|
425
|
-
//
|
|
426
|
-
|
|
308
|
+
// 主题簇来源:`async () => ({ ok, clusters, unclustered, reason })`。
|
|
309
|
+
// 不传就不折叠、标题原样列出——这条是给测试和「不想跑聚类」的环境留的。
|
|
310
|
+
themes = null,
|
|
427
311
|
cluster = true,
|
|
428
312
|
} = options
|
|
429
313
|
|
|
@@ -469,58 +353,41 @@ export async function buildSnapshot(listPlane, options = {}) {
|
|
|
469
353
|
// 而输入顺序一变快照就变、又会让上下文多一份副本。ID 是稳定的。
|
|
470
354
|
.sort((a, b) => b.score - a.score || String(a.record.id).localeCompare(String(b.record.id)))
|
|
471
355
|
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
const plane = entry.record.plane
|
|
486
|
-
if (!perPlane.has(plane)) perPlane.set(plane, [])
|
|
487
|
-
perPlane.get(plane).push(entry.record)
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
for (const plane of DECAY_PLANES) {
|
|
491
|
-
const rows = perPlane.get(plane) || []
|
|
492
|
-
if (rows.length === 0) continue
|
|
493
|
-
const mode = PLANE_INDEX_MODE[plane] || 'cluster'
|
|
494
|
-
const wantCluster = cluster && mode === 'cluster' && typeof probeAnchor === 'function'
|
|
495
|
-
|
|
496
|
-
if (!wantCluster) {
|
|
497
|
-
const titles = rows.map((r) => r.title || '(无标题)')
|
|
356
|
+
const themeState = cluster && typeof themes === 'function' ? await themes() : undefined
|
|
357
|
+
const available = themeState?.ok === true && themeState.supportsCluster === true
|
|
358
|
+
const selected = available
|
|
359
|
+
? foldThemes(decay.map((entry) => entry.record), themeState.clusters)
|
|
360
|
+
: { folds: [], ownerOf: new Map() }
|
|
361
|
+
|
|
362
|
+
const perPlane = new Map(DECAY_PLANES.map((plane) => [plane,
|
|
363
|
+
decay.filter((entry) => entry.record.plane === plane).map((entry) => entry.record)]))
|
|
364
|
+
function titleLines(ownerOf) {
|
|
365
|
+
return DECAY_PLANES.flatMap((plane) => {
|
|
366
|
+
const rows = perPlane.get(plane)
|
|
367
|
+
if (!rows.length) return []
|
|
368
|
+
const titles = rows.filter((r) => !ownerOf.has(String(r.id))).map((r) => r.title || '(无标题)')
|
|
498
369
|
const shown = indexCount > 0 ? titles.slice(0, indexCount) : titles
|
|
499
370
|
const rest = titles.length - shown.length
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
const { clusters, ownerOf, folded } = await clusterTitles(rows, probeAnchor)
|
|
505
|
-
foldedTotal += folded
|
|
506
|
-
// 未成簇的条目按原排序列出;它们没有可靠的检索锚点,必须逐条给标题。
|
|
507
|
-
const rest = rows.filter((r) => !ownerOf.has(r.id)).map((r) => r.title || '(无标题)')
|
|
508
|
-
const shownRest = indexCount > 0 ? rest.slice(0, indexCount) : rest
|
|
509
|
-
const restNote = rest.length - shownRest.length
|
|
510
|
-
const clusterNote = clusters.length
|
|
511
|
-
? `;主题簇 ${clusters.map((c) => `${c.anchor}×${c.count}`).join(' ')}(用该词检索可取回)`
|
|
512
|
-
: ''
|
|
513
|
-
const body = shownRest.length ? `:${shownRest.join(' | ')}` : ''
|
|
514
|
-
const tail = restNote > 0 ? ` …另有 ${restNote} 条` : ''
|
|
515
|
-
clusterLines.push(`**${PLANE_LABEL[plane]}**(${rows.length} 条${clusterNote})${body}${tail}`)
|
|
371
|
+
return [`**${PLANE_LABEL[plane]}**(${rows.length} 条)${shown.length ? `:${shown.join(' | ')}` : ''}${rest > 0 ? ` …另有 ${rest} 条,可检索` : ''}`]
|
|
372
|
+
})
|
|
516
373
|
}
|
|
517
374
|
|
|
518
|
-
|
|
375
|
+
// 独立的主题行容纳跨平面簇;每个成员仍可从展开结果查到原平面。
|
|
376
|
+
const labels = selected.folds.map((f) => `${f.phrase}×${f.count}#${f.id}`).join(' ')
|
|
377
|
+
const instructions = '用 aurorix_abook ["theme-members","--algorithm","aplusplus","--input","title","--cluster","<簇ID>","--json"] 展开(# 后是簇 ID)。若已变化,重新取句云或检索。'
|
|
378
|
+
const plainIndex = titleLines(new Map()).join('\n')
|
|
379
|
+
const foldedIndex = labels ? [`**句云(长期事实与事件)**:${labels}`, instructions, ...titleLines(selected.ownerOf)].join('\n') : ''
|
|
380
|
+
const useFolded = foldedIndex !== '' && Buffer.byteLength(foldedIndex) < Buffer.byteLength(plainIndex)
|
|
381
|
+
const foldedTotal = useFolded ? selected.ownerOf.size : 0
|
|
382
|
+
const failure = themeState !== undefined && !available
|
|
383
|
+
? `主题簇不可用:${String(themeState.reason || '当前 abook 不支持按簇 ID 展开').replace(/\s+/g, ' ')};${indexCount > 0 ? '标题按索引设置列出' : '标题已全部逐条列出'}。`
|
|
384
|
+
: ''
|
|
385
|
+
const indexBlock = useFolded ? foldedIndex : [failure, plainIndex].filter(Boolean).join('\n')
|
|
519
386
|
|
|
520
387
|
const parts = [
|
|
521
388
|
'### 常驻层(永久全量:soul / profile / explicit)',
|
|
522
389
|
resident,
|
|
523
|
-
indexBlock ? '### 可召回索引(仅标题;正文用 aurorix_abook ["memory","read","--id","
|
|
390
|
+
indexBlock ? '### 可召回索引(仅标题;正文用 aurorix_abook ["memory","read","--id","<记录ID>","--json"] 取)' : '',
|
|
524
391
|
indexBlock,
|
|
525
392
|
].filter((part) => part && part.length > 0)
|
|
526
393
|
|
package/lib/theme.js
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// 主题簇:把「哪些记录在讲同一件事」这件事交给 abook 自己算。
|
|
2
|
+
//
|
|
3
|
+
// 这里只做三件事:探一下当前二进制认不认这几条命令、把 `memory themes` 的结果
|
|
4
|
+
// 取回来、把某一簇的成员精确展开。分组、标签、相似度全在二进制里算——
|
|
5
|
+
// 插件侧再写一套启发式,就等于有两套判据,谁对谁错说不清。
|
|
6
|
+
//
|
|
7
|
+
// 旧二进制(1.8.243 及更早,包括用户自己配置的那份)没有 `memory themes`,
|
|
8
|
+
// 这种情况必须能被上层看见:返回 unavailable 而不是空结果,否则「没有主题」
|
|
9
|
+
// 和「算不出主题」会变成同一件事,标题就悄悄丢了。
|
|
10
|
+
|
|
11
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
12
|
+
import { dirname } from 'node:path'
|
|
13
|
+
|
|
14
|
+
/** abook 自己的诊断噪声:linker 警告与截断提示都不是判断依据。 */
|
|
15
|
+
const NOISE = [/^linker: Warning:/, /^WARNING: linker:/, /^\[abook\] 检测到输出/]
|
|
16
|
+
|
|
17
|
+
/** 从 stderr 里挑第一行有意义的话当失败原因。 */
|
|
18
|
+
function firstReason(text) {
|
|
19
|
+
for (const line of String(text ?? '').split('\n')) {
|
|
20
|
+
const trimmed = line.trim()
|
|
21
|
+
if (trimmed === '') continue
|
|
22
|
+
if (NOISE.some((re) => re.test(trimmed))) continue
|
|
23
|
+
return trimmed
|
|
24
|
+
}
|
|
25
|
+
return ''
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 失败原因:优先用 abook 自己的 error.message,其次 stderr,最后退码。 */
|
|
29
|
+
function reasonOf(result) {
|
|
30
|
+
const err = result?.json?.error
|
|
31
|
+
if (err !== null && typeof err === 'object' && typeof err.message === 'string' && err.message !== '') return err.message
|
|
32
|
+
const line = firstReason(result?.stderr)
|
|
33
|
+
if (line !== '') return line
|
|
34
|
+
if (result?.code === 'timeout') return 'abook 计算主题超过时限'
|
|
35
|
+
return `退出码 ${String(result?.code ?? '未知')}`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 二进制身份:`--version` 的原文。
|
|
40
|
+
*
|
|
41
|
+
* 用它当能力探测的缓存键——换了二进制(升级插件、用户改 abookPath)身份就变,
|
|
42
|
+
* 旧的「不支持」结论自动作废,不会一直卡在降级状态里。
|
|
43
|
+
*/
|
|
44
|
+
export function createThemeClient(backend, options = {}) {
|
|
45
|
+
const timeoutMs = Math.max(1000, Number(options.timeoutMs) || 30_000)
|
|
46
|
+
const algorithm = String(options.algorithm || 'aplusplus')
|
|
47
|
+
const input = String(options.input || 'title')
|
|
48
|
+
|
|
49
|
+
let identityPromise
|
|
50
|
+
let capabilityPromise
|
|
51
|
+
|
|
52
|
+
const identity = () => {
|
|
53
|
+
if (identityPromise !== undefined) return identityPromise
|
|
54
|
+
identityPromise = (async () => {
|
|
55
|
+
if (backend === undefined) return ''
|
|
56
|
+
const r = await backend.raw(['--version'])
|
|
57
|
+
return String(r?.stdout ?? '').trim()
|
|
58
|
+
})()
|
|
59
|
+
return identityPromise
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 探一次:当前二进制有没有主题命令。
|
|
64
|
+
*
|
|
65
|
+
* 用 `describe` 而不是真跑一次 `memory themes`——探测几毫秒,真算一遍要几百毫秒,
|
|
66
|
+
* 不能拿它当探测。结论按二进制身份缓存:升级二进制后身份变,旧结论自动作废,
|
|
67
|
+
* 不会一直卡在「不支持」里。
|
|
68
|
+
*/
|
|
69
|
+
const capability = () => {
|
|
70
|
+
if (capabilityPromise !== undefined) return capabilityPromise
|
|
71
|
+
capabilityPromise = (async () => {
|
|
72
|
+
if (backend === undefined) {
|
|
73
|
+
return { ok: false, unavailable: true, reason: '找不到 abook,主题簇无法计算' }
|
|
74
|
+
}
|
|
75
|
+
const key = await identity()
|
|
76
|
+
const cached = readProbeCache(options.cachePath, key)
|
|
77
|
+
if (cached !== undefined) return cached
|
|
78
|
+
|
|
79
|
+
const described = await backend.raw(['describe', 'theme-members', '--json'])
|
|
80
|
+
let answer
|
|
81
|
+
if (described?.ok === true) {
|
|
82
|
+
// 新契约在簇上带稳定 id,`theme-members --cluster <id>` 才能精确展开。
|
|
83
|
+
// 契约本身从 `params` 读;拿不到就按「没有 --cluster」处理(展开退回 --id)。
|
|
84
|
+
const params = String(payloadOf(described)?.params ?? '')
|
|
85
|
+
answer = { ok: true, supportsCluster: params.split(/\s+/).includes('--cluster') }
|
|
86
|
+
} else {
|
|
87
|
+
answer = {
|
|
88
|
+
ok: false,
|
|
89
|
+
unavailable: true,
|
|
90
|
+
reason: reasonOf(described) || '当前 abook 没有主题簇命令',
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
writeProbeCache(options.cachePath, key, answer)
|
|
94
|
+
return answer
|
|
95
|
+
})()
|
|
96
|
+
return capabilityPromise
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 取主题簇。
|
|
101
|
+
*
|
|
102
|
+
* 返回 `{ ok: true, clusters, unclustered, stats }`,或
|
|
103
|
+
* `{ ok: false, unavailable, reason }`——后者表示「这些二进制算不了」,
|
|
104
|
+
* 上层据此把标题原样列出并显示一行原因。
|
|
105
|
+
*/
|
|
106
|
+
async function themes() {
|
|
107
|
+
const cap = await capability()
|
|
108
|
+
if (cap.ok !== true) return { ok: false, unavailable: cap.unavailable === true, reason: cap.reason }
|
|
109
|
+
|
|
110
|
+
const result = await backend.raw(
|
|
111
|
+
['memory', 'themes', '--algorithm', algorithm, '--input', input, '--json'],
|
|
112
|
+
{ timeoutMs },
|
|
113
|
+
)
|
|
114
|
+
if (result?.ok !== true || result?.json === null || typeof result?.json !== 'object') {
|
|
115
|
+
// 命令存在但跑不动(库损坏、超时、库根不对)也算不可用:
|
|
116
|
+
// 不能把它和「这次恰好没有主题」混为一谈。
|
|
117
|
+
return { ok: false, unavailable: false, reason: reasonOf(result) }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const json = result.json
|
|
121
|
+
const clusters = Array.isArray(json.clusters) ? json.clusters.filter(isCluster) : []
|
|
122
|
+
const unclustered = Array.isArray(json.unclustered)
|
|
123
|
+
? json.unclustered.map((id) => String(id)).filter((id) => id !== '')
|
|
124
|
+
: []
|
|
125
|
+
const schema = String(json.schema_version ?? '')
|
|
126
|
+
if (schema !== '' && schema !== 'theme-v1') {
|
|
127
|
+
return { ok: false, unavailable: false, reason: `主题输出格式不认识(${schema})` }
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
ok: true,
|
|
131
|
+
supportsCluster: cap.supportsCluster === true,
|
|
132
|
+
clusters,
|
|
133
|
+
unclustered,
|
|
134
|
+
stats: json.stats !== null && typeof json.stats === 'object' ? json.stats : {},
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 精确展开一簇:`theme-members --cluster <id>`。
|
|
140
|
+
*
|
|
141
|
+
* 老二进制没有 `--cluster`,这时返回失败并带原文,让调用方去用 `--id <记录id>`
|
|
142
|
+
* ——那条路在旧契约里就有,不能假装不存在。
|
|
143
|
+
*/
|
|
144
|
+
async function members({ cluster, id } = {}) {
|
|
145
|
+
const selector = typeof cluster === 'string' && cluster !== ''
|
|
146
|
+
? ['--cluster', cluster]
|
|
147
|
+
: (typeof id === 'string' && id !== '' ? ['--id', id] : null)
|
|
148
|
+
if (selector === null) return { ok: false, reason: '要么给 --cluster,要么给 --id' }
|
|
149
|
+
|
|
150
|
+
const result = await backend.raw(
|
|
151
|
+
['theme-members', '--algorithm', algorithm, '--input', input, ...selector, '--json'],
|
|
152
|
+
{ timeoutMs },
|
|
153
|
+
)
|
|
154
|
+
if (result?.ok !== true || result?.json === null || typeof result?.json !== 'object') {
|
|
155
|
+
return { ok: false, reason: reasonOf(result) }
|
|
156
|
+
}
|
|
157
|
+
const json = result.json
|
|
158
|
+
return {
|
|
159
|
+
ok: true,
|
|
160
|
+
clusters: Array.isArray(json.clusters) ? json.clusters.filter(isCluster) : [],
|
|
161
|
+
unclustered: Array.isArray(json.unclustered) ? json.unclustered.map(String) : [],
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return { themes, members, capability, identity, algorithm, input }
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 能力探测的磁盘缓存:`{ version, identity, answer }`。
|
|
170
|
+
*
|
|
171
|
+
* 存在的意义只有一个——旧二进制每轮都要走一次「探测失败」,用户会看到同一句
|
|
172
|
+
* 失败说明被反复执行。缓存让这件事在同一份二进制上只发生一次。
|
|
173
|
+
* 读不出来就当没有(重探一次而已),绝不因为缓存坏掉而假定「支持」。
|
|
174
|
+
*/
|
|
175
|
+
function readProbeCache(file, identity) {
|
|
176
|
+
if (typeof file !== 'string' || file === '' || identity === '') return undefined
|
|
177
|
+
try {
|
|
178
|
+
const raw = JSON.parse(readFileSync(file, 'utf8'))
|
|
179
|
+
if (raw?.version !== 1) return undefined
|
|
180
|
+
if (raw.identity !== identity) return undefined
|
|
181
|
+
if (raw.answer === null || typeof raw.answer !== 'object') return undefined
|
|
182
|
+
if (typeof raw.answer.ok !== 'boolean') return undefined
|
|
183
|
+
return raw.answer
|
|
184
|
+
} catch {
|
|
185
|
+
return undefined
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function writeProbeCache(file, identity, answer) {
|
|
190
|
+
if (typeof file !== 'string' || file === '' || identity === '') return
|
|
191
|
+
try {
|
|
192
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
193
|
+
const tmp = `${file}.${process.pid}.tmp`
|
|
194
|
+
writeFileSync(tmp, JSON.stringify({ version: 1, identity, answer }), 'utf8')
|
|
195
|
+
renameSync(tmp, file)
|
|
196
|
+
} catch {
|
|
197
|
+
// 写不进去只影响下次重探,不影响正确性。
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 从一次 raw 调用里取 JSON 载荷。
|
|
203
|
+
*
|
|
204
|
+
* 正常路径上 `raw` 已经把 stdout 解成 `json`;这里再兜一层 stdout 原文,
|
|
205
|
+
* 是因为解析失败时 `json` 会缺席,而「契约读不到」不该被当成「契约不存在」。
|
|
206
|
+
*/
|
|
207
|
+
function payloadOf(result) {
|
|
208
|
+
if (result?.json !== null && typeof result?.json === 'object') return result.json
|
|
209
|
+
const text = String(result?.stdout ?? '').trim()
|
|
210
|
+
if (text === '' || !text.startsWith('{')) return undefined
|
|
211
|
+
try {
|
|
212
|
+
const parsed = JSON.parse(text)
|
|
213
|
+
return parsed !== null && typeof parsed === 'object' ? parsed : undefined
|
|
214
|
+
} catch {
|
|
215
|
+
return undefined
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 一条簇必须至少有短语和成员,否则渲染出来是个空壳。 */
|
|
220
|
+
function isCluster(value) {
|
|
221
|
+
if (value === null || typeof value !== 'object') return false
|
|
222
|
+
if (typeof value.phrase !== 'string' || value.phrase.trim() === '') return false
|
|
223
|
+
if (!Array.isArray(value.members) || value.members.length === 0) return false
|
|
224
|
+
return value.members.every((m) => m !== null && typeof m === 'object' && typeof m.id === 'string' && m.id !== '')
|
|
225
|
+
}
|
package/lib/view.js
CHANGED
|
@@ -399,7 +399,7 @@ function injectModel(state, caps) {
|
|
|
399
399
|
}
|
|
400
400
|
const bytes = Number(info.bytes) || 0
|
|
401
401
|
const stats = info.stats
|
|
402
|
-
? w.injectStats(info.stats.resident, info.stats.
|
|
402
|
+
? w.injectStats(info.stats.resident, info.stats.index, info.stats.folded ?? 0)
|
|
403
403
|
: ''
|
|
404
404
|
if (bytes === 0) {
|
|
405
405
|
return { on: true, note: info.error ? `${w.injectOff}:${info.error}` : w.injectNever, stats, tone: info.error ? 'warn' : 'muted' }
|
package/lib/words.js
CHANGED
|
@@ -42,7 +42,7 @@ export const WORDS = {
|
|
|
42
42
|
injectOn: '正在注入',
|
|
43
43
|
injectOff: '未启用注入',
|
|
44
44
|
injectSize: (kb, tier) => `${kb} KB · ${tier}`,
|
|
45
|
-
injectStats: (resident,
|
|
45
|
+
injectStats: (resident, index, folded) => `常驻 ${resident} · 索引 ${index} · 折叠 ${folded}`,
|
|
46
46
|
injectAge: (seconds) => seconds < 60 ? `${seconds} 秒前` : `${Math.round(seconds / 60)} 分钟前`,
|
|
47
47
|
injectNever: '还没构建过快照',
|
|
48
48
|
injectChanged: '库有变化,下一轮会重建',
|
|
@@ -169,7 +169,7 @@ export const WORDS = {
|
|
|
169
169
|
injectOn: 'Injecting',
|
|
170
170
|
injectOff: 'Injection off',
|
|
171
171
|
injectSize: (kb, tier) => `${kb} KB · ${tier}`,
|
|
172
|
-
injectStats: (resident,
|
|
172
|
+
injectStats: (resident, index, folded) => `resident ${resident} · index ${index} · folded ${folded}`,
|
|
173
173
|
injectAge: (seconds) => seconds < 60 ? `${seconds}s ago` : `${Math.round(seconds / 60)}m ago`,
|
|
174
174
|
injectNever: 'No snapshot built yet',
|
|
175
175
|
injectChanged: 'Library changed, rebuilds next turn',
|
package/package.json
CHANGED
package/vendor/SHA256SUMS
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
757242e84e7d38b0d1a88462c4052d5b7ad7f70345a33de882198657afb39134 abook-aarch64-apple-darwin
|
|
2
|
+
1e9a4540328f2de981d1be17378b9242ea4d3854be670218db3191107886a832 abook-aarch64-linux-android
|
|
3
|
+
606828f16e271b316f671bac6621b9e909eb7735d8fe90dd65c1070a921e8b33 abook-aarch64-pc-windows-gnullvm.exe
|
|
4
|
+
fce0ee15a769dc804a80565215bdbfcaf290910b67381277ff1b85ae89fd80a3 abook-aarch64-unknown-linux-musl
|
|
5
|
+
8f89656858a194a13b4cb8e78d29e9f803728b6d86138fc399c68ac1ac4bfab1 abook-armv7-linux-androideabi
|
|
6
|
+
4da6ec7b47efcc2974f646541283856e8f58f52c6effa59106b313b0bf4191dc abook-armv7-unknown-linux-musleabihf
|
|
7
|
+
43936f08b09154fc7cfe09f77998f4a211b7d4488c0e5586040590b18339ab79 abook-i686-linux-android
|
|
8
|
+
9dfeb2aac6bb6a82f74fb217e964fd59b0486496dfbce67d1cff0a385e1682c1 abook-i686-pc-windows-gnullvm.exe
|
|
9
|
+
6ded2bb6b89a44c242191ed886531234441d004ec0089f6aca450fb41b330328 abook-i686-unknown-linux-musl
|
|
10
|
+
fcb684e22a3f6d64cc31ca82670e037d0908ab90ef923e05025bdf01291f3ea2 abook-loongarch64-unknown-linux-gnu
|
|
11
|
+
d24e8e6166790ce71be60633f6a83745cb7fdfad6b0be8e010b518a9decf8469 abook-riscv64gc-unknown-linux-gnu
|
|
12
|
+
2c7617e72d9d2c1dade8ad756cbaf0a11a7b219e12e8112891cefb9e95dc1819 abook-x86_64-apple-darwin
|
|
13
|
+
1de66736568b0b3d76d25584688b1afd36b925f610d7a59f58a28a85c6a0e25e abook-x86_64-linux-android
|
|
14
|
+
4c6e43052359f3262ada403573b6f231b703c325ce6dccf4aae89ac31f56905a abook-x86_64-pc-windows-gnullvm.exe
|
|
15
|
+
e626e9d731eeb5f6ac79c00afa9dd230112f0957d07133092959ce586ab1412e abook-x86_64-unknown-linux-musl
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|