dsh-metabolism 0.2.2 → 0.3.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.
@@ -0,0 +1,473 @@
1
+ /**
2
+ * dsh-metabolism 骨架工具:把「说明卡」按档位给出来,不一次倒全文。
3
+ *
4
+ * 四档(越往后越费上下文,按需爬):
5
+ * clue 一行:日期+标题+关键词+id (几十字)
6
+ * brief 卡片里的四样,不带原话 (两三百字)
7
+ * full 整张卡,含原话摘录 (一千字上下)
8
+ * evidence 回茅房读原文,按 seq 区间切 (最多几千字)
9
+ *
10
+ * 只读,不写任何东西;写卡由 engine 在压缩时顺手做。
11
+ *
12
+ * @module dsh-metabolism/骨架工具
13
+ */
14
+ import { readFileSync, readdirSync, statSync } from 'node:fs'
15
+ import { homedir } from 'node:os'
16
+ import { dirname, join } from 'node:path'
17
+ import { readIndex, rollup, skeletonRoot, writeL1 } from './骨架.js'
18
+
19
+ /** 本机覆盖:插件目录里的 本地配置.json(不进包,发布版没有这个文件)。 */
20
+ function localOverrides() {
21
+ try {
22
+ const parsed = JSON.parse(readFileSync(new URL('./本地配置.json', import.meta.url), 'utf8'))
23
+ return parsed !== null && typeof parsed === 'object' ? parsed : {}
24
+ } catch {
25
+ return {}
26
+ }
27
+ }
28
+
29
+ function defaultArchiveRoot() {
30
+ const local = localOverrides()
31
+ const fromLocal = typeof local.archiveRoot === 'string' ? local.archiveRoot.trim() : ''
32
+ if (fromLocal.length > 0) return fromLocal
33
+ const fromEnv = typeof process.env.DSH_METABOLISM_HOME === 'string' ? process.env.DSH_METABOLISM_HOME.trim() : ''
34
+ if (fromEnv.length > 0) return fromEnv
35
+ const base = process.env.DSH_PROFILE_DIR || process.env.DSH_HOME || homedir()
36
+ return join(base, '代谢', '茅房')
37
+ }
38
+
39
+ const PARAMETERS = {
40
+ type: 'object',
41
+ properties: {
42
+ action: {
43
+ type: 'string',
44
+ description: 'list(默认,看目录线索)、show(按 id 取某一档内容)或 rebuild(从茅房回填历史卡片,默认空跑)。',
45
+ },
46
+ id: { type: 'string', description: 'show 时必填:卡片 id,形如 L1-20261119-1528-3234。' },
47
+ level: {
48
+ type: 'string',
49
+ description: 'show 的档位:clue 一行 / brief 四样不带原话(默认)/ full 整张卡 / evidence 回茅房读原文。',
50
+ },
51
+ limit: { type: 'integer', description: 'list 返回几条,1 到 30,默认 10。' },
52
+ days: { type: 'integer', description: 'list 只看最近 N 天,1 到 3650;不填就全部。' },
53
+ query: { type: 'string', description: 'list 时按关键词过滤标题与关键词(可选)。' },
54
+ maxChars: { type: 'integer', description: 'evidence 最多取回多少字,默认 4000。' },
55
+ dryRun: { type: 'boolean', description: 'rebuild 时是否只出报告不写卡(默认 true;要真写必须显式传 false)。' },
56
+ limit: { type: 'integer', description: 'list 返回几条,1 到 30,默认 10;rebuild 一次最多写几张,默认 10。' },
57
+ from: { type: 'string', description: 'rebuild 时只回填这个日期(含)之后的,形如 2026-11-17。' },
58
+ },
59
+ required: [],
60
+ additionalProperties: false,
61
+ }
62
+
63
+ const OUTPUT = {
64
+ type: 'object',
65
+ properties: {
66
+ text: { type: 'string', description: '目录线索或某一档内容;没有时说明查了什么。' },
67
+ hits: { type: 'integer', description: '返回条数。' },
68
+ },
69
+ required: ['text', 'hits'],
70
+ additionalProperties: false,
71
+ }
72
+
73
+ function clockOf(iso) {
74
+ const match = String(iso).match(/T(\d{2}:\d{2})/)
75
+ return match === null ? '' : match[1]
76
+ }
77
+
78
+ function dayOf(iso) {
79
+ return String(iso).slice(0, 10)
80
+ }
81
+
82
+ function clueOf(entry) {
83
+ const span = `${clockOf(entry.from)}–${clockOf(entry.to)}`
84
+ const words = Array.isArray(entry.keywords) && entry.keywords.length > 0 ? ` | ${entry.keywords.join('、')}` : ''
85
+ return `${entry.day} ${span} | ${entry.title || '(无标题)'}${words} | ${entry.id}`
86
+ }
87
+
88
+ /** 把一张卡切成「不带原话」的简要版:去掉开头那截元数据,也去掉原话。 */
89
+ function briefOf(text) {
90
+ let body = text
91
+ if (body.startsWith('---')) {
92
+ const end = body.indexOf('\n---', 3)
93
+ if (end !== -1) body = body.slice(end + 4)
94
+ }
95
+ const marker = '**原话**'
96
+ const cut = body.indexOf(marker)
97
+ const trimmed = (cut === -1 ? body : body.slice(0, cut)).trim()
98
+ return `${trimmed}\n(原话与出处省略,要就看 full)`
99
+ }
100
+
101
+ /** 回茅房按 seq 区间读原文。 */
102
+ function evidenceOf(entry, maxChars) {
103
+ const file = String(entry.ref ?? '')
104
+ if (file.length === 0) return { text: '这张卡没记原文路径', hits: 0 }
105
+ let raw
106
+ try {
107
+ raw = readFileSync(file, 'utf8')
108
+ } catch (error) {
109
+ return { text: `原文读不到:${file}(${error?.message ?? error})`, hits: 0 }
110
+ }
111
+ const from = Number(entry.seqFrom)
112
+ const to = Number(entry.seqTo)
113
+ const kept = []
114
+ for (const line of raw.split('\n')) {
115
+ if (line.trim().length === 0) continue
116
+ let event
117
+ try {
118
+ event = JSON.parse(line)
119
+ } catch {
120
+ continue
121
+ }
122
+ const seq = Number(event?.seq)
123
+ if (Number.isFinite(from) && seq < from) continue
124
+ if (Number.isFinite(to) && seq > to) continue
125
+ const blocks = event?.data?.content ?? event?.data?.message?.content
126
+ const text = Array.isArray(blocks)
127
+ ? blocks.filter((block) => block?.type === 'text').map((block) => block.text).join(' ')
128
+ : ''
129
+ const who = event?.type === 'user/message' ? '老大' : event?.type === 'assistant/message' ? '我' : event?.type ?? '?'
130
+ const body = text.trim().length > 0 ? text.trim().replace(/\s+/g, ' ') : `(${event?.type})`
131
+ kept.push(`${seq} ${who}:${body.slice(0, 600)}`)
132
+ }
133
+ if (kept.length === 0) return { text: `这个区间里没有可读的文字(seq ${from}–${to})`, hits: 0 }
134
+ let out = ''
135
+ let used = 0
136
+ for (const line of kept) {
137
+ if (used + line.length > maxChars) { out += '\n…(还有更多,调大 maxChars 或缩小 seq 区间)'; break }
138
+ out += `${line}\n`
139
+ used += line.length
140
+ }
141
+ return { text: `原文 ${file}(seq ${from}–${to})\n${out}`, hits: kept.length }
142
+ }
143
+
144
+ /** 回填用的提示词:只要四样+关键词+原话,别写别的。 */
145
+ const REBUILD_SYSTEM = [
146
+ '你在给一段过去的对话写「说明卡」,以后别人只看这张卡,不会再看原文。',
147
+ '请按下面的格式输出,每一块都要有:',
148
+ '【短】一行三十字以内,说清这段在干什么',
149
+ '【在干什么】一到三行',
150
+ '【仍然有效的要求】一行一条,来自用户原话里的约束;没有就写「无」',
151
+ '【已经做到】一行一条;没有就写「无」',
152
+ '【下一步】一行',
153
+ '【关键词】三到八个词,逗号隔开',
154
+ '【原话】三到五条原文摘录,一行一条,每条不超过六十字;没有就写「无」',
155
+ '规矩:只写这段里真有的,不补不猜;拿不准写「不清楚」。',
156
+ ].join('\n')
157
+
158
+ const LABELS = ['短', '在干什么', '仍然有效的要求', '已经做到', '下一步', '关键词', '原话']
159
+
160
+ /** 抠出【标签】之间的内容。 */
161
+ function blockOf(text, label) {
162
+ const start = text.indexOf(`【${label}】`)
163
+ if (start === -1) return null
164
+ const bodyStart = start + label.length + 2
165
+ let end = text.length
166
+ for (const other of LABELS) {
167
+ if (other === label) continue
168
+ const at = text.indexOf(`【${other}】`, bodyStart)
169
+ if (at !== -1 && at < end) end = at
170
+ }
171
+ const body = text.slice(bodyStart, end).trim()
172
+ return body.length === 0 ? null : body
173
+ }
174
+
175
+ function lineList(block) {
176
+ if (block === null) return []
177
+ return block
178
+ .split('\n')
179
+ .map((line) => line.replace(/^[-*·\d.、]+\s*/, '').trim())
180
+ .filter((line) => line.length > 0 && line !== '无' && line !== '(无)')
181
+ }
182
+
183
+ function wordList(block) {
184
+ if (block === null) return []
185
+ return block
186
+ .split(/[,,、\s]+/)
187
+ .map((word) => word.replace(/[()()【】\[\]「」]/g, '').trim())
188
+ .filter((word) => word.length > 0)
189
+ .slice(0, 8)
190
+ }
191
+
192
+ /** 茅房里能回填的原文文件:跳过 dropped 转储与超大整包。 */
193
+ function archiveFiles(archiveRoot) {
194
+ const out = []
195
+ const walk = (dir) => {
196
+ for (const name of readdirSync(dir)) {
197
+ const full = join(dir, name)
198
+ const st = statSync(full)
199
+ if (st.isDirectory()) { walk(full); continue }
200
+ if (!name.endsWith('.jsonl')) continue
201
+ if (name === 'dropped.jsonl') continue
202
+ if (st.size > 12 * 1024 * 1024) continue
203
+ out.push({ file: full, day: dir.split(/[\\/]/).pop(), name, size: st.size })
204
+ }
205
+ }
206
+ walk(archiveRoot)
207
+ out.sort((a, b) => `${a.day}/${a.name}`.localeCompare(`${b.day}/${b.name}`))
208
+ return out
209
+ }
210
+
211
+ /** 从一段原文里拼出发给模型看的样本(只取人说的话,工具结果不进)。 */
212
+ function sampleOf(file, maxChars) {
213
+ const events = []
214
+ for (const line of readFileSync(file, 'utf8').split('\n')) {
215
+ if (line.trim().length === 0) continue
216
+ let event
217
+ try { event = JSON.parse(line) } catch { continue }
218
+ const blocks = event?.data?.content ?? event?.data?.message?.content
219
+ if (!Array.isArray(blocks)) continue
220
+ const text = blocks.filter((b) => b?.type === 'text').map((b) => b.text).join(' ').trim()
221
+ if (text.length === 0) continue
222
+ const who = event?.type === 'user/message' ? '老大' : '我'
223
+ events.push({ seq: Number(event?.seq), time: Number(event?.time), who, text })
224
+ }
225
+ if (events.length === 0) return null
226
+ const seqs = events.map((e) => e.seq).filter(Number.isFinite)
227
+ const times = events.map((e) => e.time).filter(Number.isFinite)
228
+ const body = []
229
+ let used = 0
230
+ for (const event of events) {
231
+ const cut = event.text.slice(0, 400)
232
+ if (used + cut.length > maxChars) break
233
+ used += cut.length
234
+ body.push(`${event.who}:${cut}`)
235
+ }
236
+ return {
237
+ text: body.join('\n'),
238
+ seqFrom: seqs.length > 0 ? Math.min(...seqs) : undefined,
239
+ seqTo: seqs.length > 0 ? Math.max(...seqs) : undefined,
240
+ fromMs: times.length > 0 ? Math.min(...times) : Date.now(),
241
+ toMs: times.length > 0 ? Math.max(...times) : Date.now(),
242
+ count: events.length,
243
+ }
244
+ }
245
+
246
+ async function writeCard(ctx, archiveRoot, model, sample, file, title) {
247
+ const parts = []
248
+ const stream = ctx.llm.stream({
249
+ provider: model.provider,
250
+ model: model.model,
251
+ messages: [
252
+ { role: 'system', content: [{ type: 'text', text: REBUILD_SYSTEM }] },
253
+ { role: 'user', content: [{ type: 'text', text: sample.text }] },
254
+ ],
255
+ maxTokens: 1200,
256
+ purpose: 'compaction',
257
+ })
258
+ for await (const chunk of stream) {
259
+ if (chunk?.type === 'text-delta' && typeof chunk.text === 'string') parts.push(chunk.text)
260
+ if (chunk?.type === 'finish' && chunk.error !== undefined && chunk.error !== null) {
261
+ throw new Error(String(chunk.error?.message ?? chunk.error))
262
+ }
263
+ }
264
+ const text = parts.join('').trim()
265
+ if (text.length === 0) throw new Error('模型没回内容')
266
+ const card = {
267
+ doing: blockOf(text, '在干什么') ?? '',
268
+ requirements: lineList(blockOf(text, '仍然有效的要求')),
269
+ done: lineList(blockOf(text, '已经做到')),
270
+ next: blockOf(text, '下一步') ?? '',
271
+ keywords: wordList(blockOf(text, '关键词')),
272
+ quotes: lineList(blockOf(text, '原话')),
273
+ }
274
+ const short = blockOf(text, '短')
275
+ return writeL1({
276
+ archiveRoot,
277
+ fromMs: sample.fromMs,
278
+ toMs: sample.toMs,
279
+ shortId: file.split(/[\\/]/).pop().replace(/\.jsonl$/, ''),
280
+ title: (short ?? title).slice(0, 60),
281
+ keywords: card.keywords,
282
+ doing: card.doing,
283
+ requirements: card.requirements,
284
+ done: card.done,
285
+ next: card.next,
286
+ quotes: card.quotes,
287
+ archivePath: file,
288
+ seqFrom: sample.seqFrom,
289
+ seqTo: sample.seqTo,
290
+ tokens: Math.round((sample.text?.length ?? 0) / 2.2),
291
+ compactionId: file.split(/[\\/]/).pop().replace(/\.jsonl$/, ''),
292
+ })
293
+ }
294
+
295
+ export function apply(ctx, config) {
296
+ const archiveRoot = typeof config?.archiveRoot === 'string' && config.archiveRoot.length > 0
297
+ ? config.archiveRoot
298
+ : defaultArchiveRoot()
299
+ const root = skeletonRoot(archiveRoot)
300
+ const log = (message) => {
301
+ try {
302
+ const logger = ctx.logger
303
+ if (logger && typeof logger.warn === 'function') logger.warn(`metabolism-skeleton: ${message}`)
304
+ } catch {
305
+ // 日志坏了不影响只读
306
+ }
307
+ }
308
+
309
+ const definition = {
310
+ name: 'metabolism_skeleton',
311
+ description: '看「骨架」目录:每次压缩留下的说明卡(在干什么/哪些要求还算数/做到哪/下一步+原话)。list 看线索,show 按档位取内容,evidence 再回茅房翻原文。比 recall 更省:不用你想起关键词。',
312
+ parameters: PARAMETERS,
313
+ output: {
314
+ schema: OUTPUT,
315
+ render: (_args, value) => [{ type: 'text', text: String(value?.text ?? '') }],
316
+ },
317
+ async execute(args) {
318
+ const action = String(args?.action ?? 'list').toLowerCase()
319
+ const index = readIndex(root)
320
+
321
+ if (action === 'show') {
322
+ const id = String(args?.id ?? '').trim()
323
+ if (id.length === 0) return { text: '要给 id,先用 action=list 看目录', hits: 0 }
324
+ const entry = index.entries.find((item) => item?.id === id)
325
+ if (entry === undefined) return { text: `目录里没有 ${id}`, hits: 0 }
326
+ const level = String(args?.level ?? 'brief').toLowerCase()
327
+ if (level === 'clue') return { text: clueOf(entry), hits: 1 }
328
+ if (level === 'evidence') {
329
+ const maxChars = Math.max(500, Math.min(20000, Number(args?.maxChars) || 4000))
330
+ return evidenceOf(entry, maxChars)
331
+ }
332
+ let card
333
+ try {
334
+ card = readFileSync(join(root, 'L1', `${id}.md`), 'utf8')
335
+ } catch (error) {
336
+ return { text: `卡片文件读不到:${error?.message ?? error}`, hits: 0 }
337
+ }
338
+ return { text: level === 'full' ? card : briefOf(card), hits: 1 }
339
+ }
340
+
341
+ if (action === 'rollup') {
342
+ const dryRun = args?.dryRun !== false
343
+ const result = rollup({
344
+ archiveRoot,
345
+ dryRun,
346
+ coldAfterDays: args?.coldAfterDays,
347
+ foldAfterDays: args?.foldAfterDays,
348
+ })
349
+ if (result.ok !== true) return { text: `降级失败:${result.error}`, hits: 0 }
350
+ const head = dryRun ? '空跑(没写任何文件):' : '已写:'
351
+ const body = result.report.length > 0 ? result.report.join('\n') : '(没有可降级的段)'
352
+ return {
353
+ text: `${head}\n${body}\n\n日摘要 ${result.l2Written} 个,标冷 ${result.coldMarked} 段,折指针 ${result.l3Made} 个。`,
354
+ hits: result.l2Written,
355
+ }
356
+ }
357
+
358
+ if (action === 'rebuild') {
359
+ const dryRun = args?.dryRun !== false
360
+ const limit = Math.max(1, Math.min(30, Number(args?.limit) || 10))
361
+ const floor = typeof args?.from === 'string' && args.from.length > 0 ? args.from : ''
362
+ const done = new Set(
363
+ index.entries
364
+ .map((entry) => {
365
+ if (entry?.compactionId) return String(entry.compactionId)
366
+ // 老卡没记 compactionId,就从原文路径的文件名倒推
367
+ return typeof entry?.ref === 'string' && entry.ref.length > 0
368
+ ? entry.ref.split(/[\\/]/).pop().replace(/\.jsonl$/, '')
369
+ : ''
370
+ })
371
+ .filter((key) => key.length > 0),
372
+ )
373
+ const all = archiveFiles(archiveRoot).filter((item) => floor === '' || item.day >= floor)
374
+ const todo = all.filter((item) => !done.has(item.name.replace(/\.jsonl$/, '')))
375
+ if (todo.length === 0) return { text: '茅房里的段都已经有卡了', hits: 0 }
376
+
377
+ if (dryRun) {
378
+ const picked = todo.slice(0, limit)
379
+ const lines = [`待回填 ${todo.length} 段,这次最多处理 ${picked.length} 段(空跑,不写卡):`]
380
+ for (const item of picked) {
381
+ const sample = sampleOf(item.file, 6000)
382
+ if (sample === null) { lines.push(`- ${item.day}/${item.name} | 没有可摘的文字,跳过`); continue }
383
+ const head = sample.text.split('\n')[0]?.slice(0, 46) ?? ''
384
+ lines.push(`- ${item.day}/${item.name} | seq ${sample.seqFrom}–${sample.seqTo} | ${sample.count} 条 | 约 ${Math.round(sample.text.length / 2.2)} tokens | ${head}`)
385
+ }
386
+ lines.push('')
387
+ lines.push('要真写,再调一次,带 dryRun=false。')
388
+ return { text: lines.join('\n'), hits: picked.length }
389
+ }
390
+
391
+ if (typeof ctx.llm?.stream !== 'function') {
392
+ return { text: '这一轮拿不到模型(ctx.llm 不在),先不写', hits: 0 }
393
+ }
394
+ const local = localOverrides()
395
+ const model = {
396
+ provider: typeof local.summaryProvider === 'string' && local.summaryProvider.length > 0 ? local.summaryProvider : 'deepseek-account',
397
+ model: typeof local.summaryModel === 'string' && local.summaryModel.length > 0 ? local.summaryModel : 'deepseek-flash',
398
+ }
399
+ const lines = []
400
+ let written = 0
401
+ let failed = 0
402
+ for (const item of todo.slice(0, limit)) {
403
+ const sample = sampleOf(item.file, 6000)
404
+ if (sample === null) { lines.push(`跳过 ${item.day}/${item.name}:没有可摘的文字`); continue }
405
+ try {
406
+ const card = await writeCard(ctx, archiveRoot, model, sample, item.file, item.name)
407
+ if (card?.ok) { written += 1; lines.push(`写好 ${card.id}`) }
408
+ else { failed += 1; lines.push(`失败 ${item.name}:${card?.error ?? '未知'}`) }
409
+ } catch (error) {
410
+ const message = String(error?.message ?? error)
411
+ // 模型对极短的段常常什么都不回:不空着,用原文首句落一张最薄的卡,标清楚是兜底
412
+ if (message.includes('模型没回内容')) {
413
+ const firstLine = sample.text.split('\n').find((line) => line.trim().length > 0) ?? ''
414
+ const thin = writeL1({
415
+ archiveRoot,
416
+ fromMs: sample.fromMs,
417
+ toMs: sample.toMs,
418
+ shortId: item.name.replace(/\.jsonl$/, ''),
419
+ compactionId: item.name.replace(/\.jsonl$/, ''),
420
+ title: firstLine.replace(/^\S+?:/, '').slice(0, 40) || item.name,
421
+ keywords: [],
422
+ doing: `(模型没回内容,按原文首句兜底)${firstLine.replace(/^\S+?:/, '').slice(0, 120)}`,
423
+ requirements: [],
424
+ done: [],
425
+ next: '(这段太短,没留下明确的下一步)',
426
+ quotes: sample.text.split('\n').filter((line) => line.trim().length > 0).slice(0, 3),
427
+ archivePath: item.file,
428
+ seqFrom: sample.seqFrom,
429
+ seqTo: sample.seqTo,
430
+ tokens: Math.round((sample.text?.length ?? 0) / 2.2),
431
+ })
432
+ if (thin?.ok) { written += 1; lines.push(`兜底写好 ${thin.id}`) }
433
+ else { failed += 1; lines.push(`兜底也失败 ${item.name}:${thin?.error ?? '未知'}`) }
434
+ continue
435
+ }
436
+ failed += 1
437
+ lines.push(`失败 ${item.name}:${message}`)
438
+ }
439
+ }
440
+ return { text: `写了 ${written} 张,失败 ${failed} 张。\n${lines.join('\n')}`, hits: written }
441
+ }
442
+
443
+ let entries = index.entries
444
+ const days = Number(args?.days)
445
+ if (Number.isFinite(days) && days > 0) {
446
+ const floor = new Date(Date.now() - days * 86400000).toISOString().slice(0, 10)
447
+ entries = entries.filter((entry) => String(entry?.day ?? '') >= floor)
448
+ }
449
+ const query = String(args?.query ?? '').trim()
450
+ if (query.length > 0) {
451
+ const needle = query.toLowerCase()
452
+ entries = entries.filter((entry) => {
453
+ const hay = `${entry?.title ?? ''} ${(entry?.keywords ?? []).join(' ')}`.toLowerCase()
454
+ return hay.includes(needle)
455
+ })
456
+ }
457
+ const limit = Math.max(1, Math.min(30, Number(args?.limit) || 10))
458
+ const picked = entries.slice(0, limit)
459
+ if (picked.length === 0) {
460
+ return { text: `骨架目录里没有符合条件的段(共 ${index.entries.length} 段,目录在 ${root})`, hits: 0 }
461
+ }
462
+ const lines = picked.map(clueOf)
463
+ const more = index.entries.length > picked.length ? `\n(共 ${index.entries.length} 段,这里只给最近 ${picked.length} 条)` : ''
464
+ return { text: lines.join('\n') + more, hits: picked.length }
465
+ },
466
+ }
467
+
468
+ if (typeof ctx.tools?.register !== 'function') {
469
+ log('没有 tools 服务,骨架工具没挂上')
470
+ return
471
+ }
472
+ ctx.tools.register(definition)
473
+ }