macro-memory 0.0.0-stage → 0.9.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/LICENSE +21 -0
- package/README.md +384 -2
- package/bin/macro-memory.mjs +732 -0
- package/docs/CHANGELOG.md +250 -0
- package/docs/MAC.md +156 -0
- package/docs/images/_architecture.svg +70 -0
- package/docs/images/_memory-flow.svg +75 -0
- package/docs/images/_token-budget.svg +40 -0
- package/docs/images/architecture.png +0 -0
- package/docs/images/hero.png +0 -0
- package/docs/images/memory-flow.png +0 -0
- package/docs/images/src/graph.html +78 -0
- package/docs/images/src/hero.html +88 -0
- package/docs/images/src/panel.html +63 -0
- package/docs/images/src/pipeline.html +97 -0
- package/docs/images/token-budget.png +0 -0
- package/lib/client.js +3811 -0
- package/lib/core/agents-file.js +95 -0
- package/lib/core/client.js +313 -0
- package/lib/core/compat/dsh-llm.js +78 -0
- package/lib/core/config.js +117 -0
- package/lib/core/engine.js +456 -0
- package/lib/core/fence.js +109 -0
- package/lib/core/index.js +102 -0
- package/lib/core/migrate.js +70 -0
- package/lib/core/mini-host.js +324 -0
- package/lib/core/owner.js +347 -0
- package/lib/core/paths.js +95 -0
- package/lib/core/serve.js +378 -0
- package/lib/core/trust.js +173 -0
- package/lib/index.js +567 -0
- package/lib/macro/brand.js +82 -0
- package/lib/macro/bridge.js +158 -0
- package/lib/macro/chain.js +206 -0
- package/lib/macro/contract.js +471 -0
- package/lib/macro/cost.js +164 -0
- package/lib/macro/defs/admin.js +528 -0
- package/lib/macro/defs/dream.js +24 -0
- package/lib/macro/defs/link.js +408 -0
- package/lib/macro/defs/project.js +237 -0
- package/lib/macro/defs/read.js +160 -0
- package/lib/macro/defs/remember.js +411 -0
- package/lib/macro/defs/search.js +899 -0
- package/lib/macro/defs/update.js +161 -0
- package/lib/macro/dreamledger.js +1398 -0
- package/lib/macro/facade.js +135 -0
- package/lib/macro/fusion.js +188 -0
- package/lib/macro/injectledger.js +843 -0
- package/lib/macro/linkflow.js +391 -0
- package/lib/macro/links.js +448 -0
- package/lib/macro/neighbor.js +1083 -0
- package/lib/macro/panel.js +96 -0
- package/lib/macro/platform.js +134 -0
- package/lib/macro/prompt.js +139 -0
- package/lib/macro/provenance.js +148 -0
- package/lib/macro/render.js +35 -0
- package/lib/macro/revisions.js +253 -0
- package/lib/macro/shared.js +113 -0
- package/lib/macro/shortid.js +114 -0
- package/lib/macro/snapshot.js +700 -0
- package/lib/macro/spend.js +312 -0
- package/lib/macro/store.js +238 -0
- package/lib/macro/supersede.js +197 -0
- package/lib/macro/tokencost.js +90 -0
- package/lib/macro/tools.js +67 -0
- package/lib/mcp/server.js +196 -0
- package/lib/mcp/stdio.js +151 -0
- package/lib/ui/page.js +300 -0
- package/lib/ui/server.js +226 -0
- package/package.json +56 -4
- package/vendor/VENDOR.json +39 -0
- package/vendor/cosmokit/LICENSE +21 -0
- package/vendor/cosmokit/README.md +30 -0
- package/vendor/cosmokit/lib/index.js +418 -0
- package/vendor/cosmokit/lib/types/array.d.ts +15 -0
- package/vendor/cosmokit/lib/types/array.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/index.d.ts +13 -0
- package/vendor/cosmokit/lib/types/index.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/misc.d.ts +41 -0
- package/vendor/cosmokit/lib/types/misc.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/string.d.ts +89 -0
- package/vendor/cosmokit/lib/types/string.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/time.d.ts +19 -0
- package/vendor/cosmokit/lib/types/time.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/types.d.ts +42 -0
- package/vendor/cosmokit/lib/types/types.d.ts.map +1 -0
- package/vendor/cosmokit/lib/types/volatile.d.ts +40 -0
- package/vendor/cosmokit/lib/types/volatile.d.ts.map +1 -0
- package/vendor/cosmokit/package.json +32 -0
- package/vendor/cosmokit/src/array.ts +42 -0
- package/vendor/cosmokit/src/index.ts +12 -0
- package/vendor/cosmokit/src/misc.ts +78 -0
- package/vendor/cosmokit/src/string.ts +113 -0
- package/vendor/cosmokit/src/time.ts +92 -0
- package/vendor/cosmokit/src/types.ts +168 -0
- package/vendor/cosmokit/src/volatile.ts +86 -0
- package/vendor/dsh-memory-nebula/CONTRIBUTING.md +48 -0
- package/vendor/dsh-memory-nebula/LICENSE +21 -0
- package/vendor/dsh-memory-nebula/README.en.md +203 -0
- package/vendor/dsh-memory-nebula/README.md +253 -0
- package/vendor/dsh-memory-nebula/SECURITY.md +43 -0
- package/vendor/dsh-memory-nebula/SUPPORT.md +33 -0
- package/vendor/dsh-memory-nebula/cordis.patch.yml +4 -0
- package/vendor/dsh-memory-nebula/lib/client.js +105 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/index.js +279 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/ingest.js +230 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/meow.js +320 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/notes.js +305 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/sqlite.js +287 -0
- package/vendor/dsh-memory-nebula/lib/host/adapters/util.js +231 -0
- package/vendor/dsh-memory-nebula/lib/host/bridge.js +351 -0
- package/vendor/dsh-memory-nebula/lib/host/compress.js +97 -0
- package/vendor/dsh-memory-nebula/lib/host/demo.js +357 -0
- package/vendor/dsh-memory-nebula/lib/host/diagnostics.js +125 -0
- package/vendor/dsh-memory-nebula/lib/host/doctor.js +270 -0
- package/vendor/dsh-memory-nebula/lib/host/entities.js +230 -0
- package/vendor/dsh-memory-nebula/lib/host/graph.js +1762 -0
- package/vendor/dsh-memory-nebula/lib/host/graphcache.js +290 -0
- package/vendor/dsh-memory-nebula/lib/host/http.js +993 -0
- package/vendor/dsh-memory-nebula/lib/host/injectcfg.js +95 -0
- package/vendor/dsh-memory-nebula/lib/host/ledger.js +175 -0
- package/vendor/dsh-memory-nebula/lib/host/live.js +204 -0
- package/vendor/dsh-memory-nebula/lib/host/nebula.js +2105 -0
- package/vendor/dsh-memory-nebula/lib/host/page.js +75 -0
- package/vendor/dsh-memory-nebula/lib/host/path.js +83 -0
- package/vendor/dsh-memory-nebula/lib/host/recall.js +526 -0
- package/vendor/dsh-memory-nebula/lib/host/share.js +633 -0
- package/vendor/dsh-memory-nebula/lib/host/sources.js +266 -0
- package/vendor/dsh-memory-nebula/lib/host/store.js +333 -0
- package/vendor/dsh-memory-nebula/lib/host/text.js +410 -0
- package/vendor/dsh-memory-nebula/lib/host/transcripts.js +396 -0
- package/vendor/dsh-memory-nebula/lib/index.js +782 -0
- package/vendor/dsh-memory-nebula/lib/standalone.mjs +315 -0
- package/vendor/dsh-memory-nebula/lib/web/api.js +426 -0
- package/vendor/dsh-memory-nebula/lib/web/app.js +4172 -0
- package/vendor/dsh-memory-nebula/lib/web/i18n.js +580 -0
- package/vendor/dsh-memory-nebula/lib/web/index.html +174 -0
- package/vendor/dsh-memory-nebula/lib/web/nav.js +132 -0
- package/vendor/dsh-memory-nebula/lib/web/render.js +1890 -0
- package/vendor/dsh-memory-nebula/lib/web/space3d.js +1883 -0
- package/vendor/dsh-memory-nebula/lib/web/style.css +1402 -0
- package/vendor/dsh-memory-nebula/locale/en.json +7 -0
- package/vendor/dsh-memory-nebula/locale/zh.json +7 -0
- package/vendor/dsh-memory-nebula/market/assets/icon-180.png +0 -0
- package/vendor/dsh-memory-nebula/market/assets/icon.svg +40 -0
- package/vendor/dsh-memory-nebula/market/index.json +101 -0
- package/vendor/dsh-memory-nebula/package.json +87 -0
- package/vendor/dsh-memory-nebula/sidecar.ps1 +119 -0
- package/vendor/meow-memory/LICENSE +21 -0
- package/vendor/meow-memory/README.en.md +192 -0
- package/vendor/meow-memory/README.md +174 -0
- package/vendor/meow-memory/README.pt-br.md +180 -0
- package/vendor/meow-memory/cordis.patch.yml +7 -0
- package/vendor/meow-memory/lib/client.js +3354 -0
- package/vendor/meow-memory/lib/client.js.map +7 -0
- package/vendor/meow-memory/lib/index.js +13500 -0
- package/vendor/meow-memory/lib/index.js.map +7 -0
- package/vendor/meow-memory/lib/prompts/README.md +65 -0
- package/vendor/meow-memory/lib/prompts/en/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/en/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/en/dream-project-summary.md +12 -0
- package/vendor/meow-memory/lib/prompts/en/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/en/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/en/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/en/labels.md +104 -0
- package/vendor/meow-memory/lib/prompts/en/reflect.md +31 -0
- package/vendor/meow-memory/lib/prompts/en/system-guide.md +121 -0
- package/vendor/meow-memory/lib/prompts/en/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/en/welcome-guide.md +12 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-project-summary.md +12 -0
- package/vendor/meow-memory/lib/prompts/pt-br/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/pt-br/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/pt-br/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/pt-br/labels.md +104 -0
- package/vendor/meow-memory/lib/prompts/pt-br/reflect.md +31 -0
- package/vendor/meow-memory/lib/prompts/pt-br/system-guide.md +121 -0
- package/vendor/meow-memory/lib/prompts/pt-br/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/pt-br/welcome-guide.md +12 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-atomic.md +36 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-header.md +9 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-project-summary.md +19 -0
- package/vendor/meow-memory/lib/prompts/zh/dream-topic.md +27 -0
- package/vendor/meow-memory/lib/prompts/zh/external-memory-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/zh/first-create-notice.md +8 -0
- package/vendor/meow-memory/lib/prompts/zh/labels.md +104 -0
- package/vendor/meow-memory/lib/prompts/zh/reflect.md +30 -0
- package/vendor/meow-memory/lib/prompts/zh/system-guide.md +119 -0
- package/vendor/meow-memory/lib/prompts/zh/tools.md +83 -0
- package/vendor/meow-memory/lib/prompts/zh/welcome-guide.md +12 -0
- package/vendor/meow-memory/package.json +64 -0
- package/vendor/schemastery/LICENSE +21 -0
- package/vendor/schemastery/README.md +393 -0
- package/vendor/schemastery/lib/index.cjs +635 -0
- package/vendor/schemastery/lib/index.mjs +637 -0
- package/vendor/schemastery/lib/types/index.d.ts +211 -0
- package/vendor/schemastery/lib/types/index.d.ts.map +1 -0
- package/vendor/schemastery/package.json +39 -0
- package/vendor/schemastery/src/index.ts +962 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [macro-fusion 0.6.70] 注入额度账(spend):钱花在哪几条上。
|
|
3
|
+
*
|
|
4
|
+
* 病:0.6.55 起「每轮一共允许多少字」已经是用户可调的旋钮(`injectTurnMaxChars`),
|
|
5
|
+
* 0.6.46 起「这几小时注入了多少字、被挡的是哪一类」也有账(`action="inject"`)。
|
|
6
|
+
* 但两份账都只回答「一共花了多少」——**说不出是哪几条在花**。于是最典型的现场是:
|
|
7
|
+
* 每轮都注入十几条、token 烧得心疼,翻面板只看到「本轮 3,412 字」,想动手却不知道
|
|
8
|
+
* 该动哪一条:是那条每次都在的 900 字长文,还是十几条「被召回、从没被读过」的短句?
|
|
9
|
+
*
|
|
10
|
+
* 处方:按条目结账。库里有现成的三个数——`hit_count`(注入即 +1)、`read_count`
|
|
11
|
+
* (真被 `memory_read` 读过才 +1)、`content` 长度——乘一乘就是每条的大致消耗。
|
|
12
|
+
* 面板把它们排成「最吃额度的 N 条」,每条配一句**能直接执行**的出路(归档 / 降权 /
|
|
13
|
+
* 拆条),并单列两个口子:注入了却没人读的、单条比基准长好几倍的。
|
|
14
|
+
*
|
|
15
|
+
* 三条口径上的自觉:
|
|
16
|
+
* ① 这是**估算**,而且是有意取的上界:每轮按名次只给每条「基准的一部分」(最贴问的
|
|
17
|
+
* 两倍、其余六成),所以真实消耗只会比 `注入次数 × 全文字数` 小。要日志实测的
|
|
18
|
+
* 数字就问 `action="inject"`,两边摆在一起看,别拿估算当流水。
|
|
19
|
+
* ② 只读:用 `openReadOnly` 打开现库(与 0.6.69 快照对比同一条路),不建表、不迁移、
|
|
20
|
+
* 不写一行;库坏掉在这里也只说「读不出来」,不动手。
|
|
21
|
+
* ③ 已归档的不进榜(它们本就不该再注入),但会单独报个数——否则「归档了怎么还在花」
|
|
22
|
+
* 这种疑问没有答案。
|
|
23
|
+
*/
|
|
24
|
+
import { existsSync, readFileSync, statSync } from 'node:fs'
|
|
25
|
+
import { join } from 'node:path'
|
|
26
|
+
import * as Meow from '../../vendor/meow-memory/lib/index.js'
|
|
27
|
+
import { parseInjectLog } from './injectledger.js'
|
|
28
|
+
import { SNAPSHOT_LEVELS, humanStamp, openReadOnly } from './snapshot.js'
|
|
29
|
+
|
|
30
|
+
/** 每条正文的出厂基准(与引擎 `injectBodyMax` 默认值同源;配置写了就用配置)。 */
|
|
31
|
+
const BODY_MAX_DEFAULT = 240
|
|
32
|
+
/** 「注入了却没人读」的判定线:至少被注入这么多回还没人读,才值得点出来。 */
|
|
33
|
+
export const WASTE_MIN_HITS = 3
|
|
34
|
+
/** 「单条超长」的判定线:正文超过每条基准的两倍就算贵(面板里明写这条线怎么来的)。 */
|
|
35
|
+
const longLine = (bodyMax) => Math.max(200, bodyMax * 2)
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* [0.6.71] 日志实测:与 `action="inject"` 读的是同一份 `dream-debug.log`(含 `.old`)。
|
|
39
|
+
* 只回答「最近这些轮一共塞进去多少字、其中多少条次有按条记录」——它替不了估算
|
|
40
|
+
* (估算是每条一辈子的账),但两者摆在一起,用户能立刻看出估算偏大多少。
|
|
41
|
+
* 纯读,读不出来就返回 null,面板改口说「还没有日志」,不编数字。
|
|
42
|
+
*/
|
|
43
|
+
function measuredInject(workspace, dir) {
|
|
44
|
+
const base = join(String(workspace ?? ''), String(dir || '.dsh-meow'))
|
|
45
|
+
const parts = []
|
|
46
|
+
let bytes = 0
|
|
47
|
+
for (const name of ['dream-debug.log.old', 'dream-debug.log']) {
|
|
48
|
+
const file = join(base, name)
|
|
49
|
+
if (!existsSync(file)) continue
|
|
50
|
+
try {
|
|
51
|
+
bytes += statSync(file).size
|
|
52
|
+
parts.push(readFileSync(file, 'utf8'))
|
|
53
|
+
} catch {
|
|
54
|
+
/* 这一段读不出来就当它没有;账照样能用估算出 */
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (parts.length === 0) return null
|
|
58
|
+
const { events } = parseInjectLog(parts.join('\n'))
|
|
59
|
+
const hits = events.filter((e) => e.kind === 'hit' && e.hit === 'yes')
|
|
60
|
+
const chars = hits.reduce((a, e) => a + e.len, 0)
|
|
61
|
+
const picks = hits.reduce((a, e) => a + (Array.isArray(e.picks) ? e.picks.length : 0), 0)
|
|
62
|
+
return { rounds: hits.length, chars, picks, bytes }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function numOf(v, fallback = 0) {
|
|
66
|
+
const n = Number(v)
|
|
67
|
+
return Number.isFinite(n) && n >= 0 ? Math.floor(n) : fallback
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const comma = (n) => Number(n).toLocaleString('en-US')
|
|
71
|
+
|
|
72
|
+
/** 面板上的一行标题:压到 28 字,空了退回正文首句。 */
|
|
73
|
+
function shortTitle(row) {
|
|
74
|
+
const raw = String(row.title ?? '').trim() || String(row.content ?? '').trim()
|
|
75
|
+
const flat = raw.replace(/\s+/g, ' ').trim()
|
|
76
|
+
if (flat === '') return '(无标题)'
|
|
77
|
+
return flat.length > 28 ? `${flat.slice(0, 28)}…` : flat
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 把库摊成一条条账单。表不在(老库 / 半张 schema)就跳过那层——坏一张不该让整份读数作废。
|
|
82
|
+
* @returns {{rows: object[], byLevel: object[], missing: string[], archived: number}}
|
|
83
|
+
*/
|
|
84
|
+
export function readSpend(handle) {
|
|
85
|
+
const rows = []
|
|
86
|
+
const byLevel = []
|
|
87
|
+
const missing = []
|
|
88
|
+
let archived = 0
|
|
89
|
+
for (const level of SNAPSHOT_LEVELS) {
|
|
90
|
+
let raw = []
|
|
91
|
+
try {
|
|
92
|
+
raw = handle
|
|
93
|
+
.prepare(`SELECT id, title, content, hit_count, read_count, importance, status, last_accessed_at FROM ${level}`)
|
|
94
|
+
.all()
|
|
95
|
+
} catch {
|
|
96
|
+
missing.push(level)
|
|
97
|
+
continue
|
|
98
|
+
}
|
|
99
|
+
const box = { level, count: 0, chars: 0, hit: 0, spend: 0, archived: 0 }
|
|
100
|
+
for (const r of raw) {
|
|
101
|
+
const id = String(r.id ?? '')
|
|
102
|
+
if (id === '') continue
|
|
103
|
+
const chars = String(r.content ?? '').length
|
|
104
|
+
const hits = numOf(r.hit_count)
|
|
105
|
+
const reads = numOf(r.read_count)
|
|
106
|
+
const status = String(r.status ?? 'active')
|
|
107
|
+
const spend = hits * chars
|
|
108
|
+
box.count += 1
|
|
109
|
+
box.chars += chars
|
|
110
|
+
if (hits > 0) box.hit += 1
|
|
111
|
+
box.spend += spend
|
|
112
|
+
if (status === 'archived') {
|
|
113
|
+
box.archived += 1
|
|
114
|
+
archived += 1
|
|
115
|
+
continue
|
|
116
|
+
}
|
|
117
|
+
rows.push({
|
|
118
|
+
id,
|
|
119
|
+
level,
|
|
120
|
+
title: shortTitle(r),
|
|
121
|
+
chars,
|
|
122
|
+
hits,
|
|
123
|
+
reads,
|
|
124
|
+
importance: numOf(r.importance),
|
|
125
|
+
status,
|
|
126
|
+
last: numOf(r.last_accessed_at),
|
|
127
|
+
spend,
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
byLevel.push(box)
|
|
131
|
+
}
|
|
132
|
+
return { rows, byLevel, missing, archived }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 每层一行的对齐(层名固定 7 个,短的补空格;面板是给人扫的,列不齐就不成表)。 */
|
|
136
|
+
function levelLine(box) {
|
|
137
|
+
const name = box.level.padEnd(7, ' ')
|
|
138
|
+
const avg = box.count > 0 ? Math.round(box.chars / box.count) : 0
|
|
139
|
+
const tail = box.archived > 0 ? ` 归档 ${box.archived}` : ''
|
|
140
|
+
return ` ${name} ${String(box.count).padStart(4, ' ')} 条 ${comma(box.chars).padStart(9, ' ')} 字 均 ${String(avg).padStart(4, ' ')} 注入过 ${box.hit}${tail}`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 一条的出路:三种里挑一种,永远给现成命令,不给「建议优化」。 */
|
|
144
|
+
function exitFor(row, bodyMax) {
|
|
145
|
+
const arg = `id="${row.id}"`
|
|
146
|
+
if (row.reads === 0 && row.hits >= WASTE_MIN_HITS) {
|
|
147
|
+
return `从没读过:memory_update(${arg}, status="archived") 归档;只想降权用 importance=1`
|
|
148
|
+
}
|
|
149
|
+
if (row.chars > longLine(bodyMax)) {
|
|
150
|
+
return `比每条基准(${bodyMax} 字)长 ${(row.chars / bodyMax).toFixed(1)} 倍:memory_read(${arg}) 看全文,再 memory_update 压短或拆成两条`
|
|
151
|
+
}
|
|
152
|
+
return `嫌贵就降权:memory_update(${arg}, importance=1)(注入仍会带,但排在后面)`
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* 面板正文。数字全部念库里的列,不算「应该花多少」。
|
|
157
|
+
* @returns {{body: string, scope: string}}
|
|
158
|
+
*/
|
|
159
|
+
export function readSpendPanel({ workspace = '', dir = '.dsh-meow', limit = 5, config = {}, now = Date.now() } = {}) {
|
|
160
|
+
const ws = String(workspace ?? '').trim()
|
|
161
|
+
if (ws === '') {
|
|
162
|
+
return { body: '拿不到工作区——额度账是按工作区结的,这次一条都算不出来(不是「还没花过」)。', scope: '' }
|
|
163
|
+
}
|
|
164
|
+
const want = Math.max(1, Math.min(50, numOf(limit, 5)))
|
|
165
|
+
let dbPath = ''
|
|
166
|
+
try {
|
|
167
|
+
dbPath = Meow.memoryDbPath(ws, dir)
|
|
168
|
+
} catch {
|
|
169
|
+
dbPath = ''
|
|
170
|
+
}
|
|
171
|
+
let dbBytes = 0
|
|
172
|
+
if (dbPath !== '') {
|
|
173
|
+
try {
|
|
174
|
+
dbBytes = statSync(dbPath).size
|
|
175
|
+
} catch {
|
|
176
|
+
dbBytes = 0
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (dbBytes === 0) {
|
|
180
|
+
return {
|
|
181
|
+
body: `这个工作区还没有记忆库(找的是 ${dbPath === '' ? '(路径取不到)' : dbPath})。\n一行额度都还没花过——不是「花的都是 0」。`,
|
|
182
|
+
scope: '',
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const probe = openReadOnly(dbPath)
|
|
186
|
+
if (probe.status !== 'ok') {
|
|
187
|
+
return {
|
|
188
|
+
body:
|
|
189
|
+
`库在(${comma(dbBytes)} B),但读不出来:${probe.status === 'no-file' ? '文件不在了' : (probe.error ?? '原因未记')}。\n` +
|
|
190
|
+
'额度账是纯读的,不会为了出数去动库;先看 memory_admin(action="snapshot") 能不能退一份回来。',
|
|
191
|
+
scope: '',
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
let spend
|
|
195
|
+
try {
|
|
196
|
+
spend = readSpend(probe.db)
|
|
197
|
+
} finally {
|
|
198
|
+
try {
|
|
199
|
+
probe.db.close()
|
|
200
|
+
} catch {
|
|
201
|
+
/* 关不掉不影响读数 */
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const bodyMax = numOf(config.injectBodyMax, BODY_MAX_DEFAULT) || BODY_MAX_DEFAULT
|
|
206
|
+
const perHit = numOf(config.injectMaxChars, 0)
|
|
207
|
+
const perTurn = numOf(config.injectTurnMaxChars, 0)
|
|
208
|
+
const quota = [
|
|
209
|
+
`每轮 ${perTurn > 0 ? `${comma(perTurn)} 字` : '不限'}`,
|
|
210
|
+
`单次命中 ${perHit > 0 ? `${comma(perHit)} 字` : '不限'}`,
|
|
211
|
+
`每条基准 ${comma(bodyMax)} 字`,
|
|
212
|
+
].join(' · ')
|
|
213
|
+
|
|
214
|
+
const lines = []
|
|
215
|
+
lines.push(`库:${dbPath}`)
|
|
216
|
+
const live = spend.rows
|
|
217
|
+
const total = live.length
|
|
218
|
+
const totalChars = live.reduce((a, r) => a + r.chars, 0)
|
|
219
|
+
const injected = live.filter((r) => r.hits > 0)
|
|
220
|
+
const spendSum = live.reduce((a, r) => a + r.spend, 0)
|
|
221
|
+
lines.push(
|
|
222
|
+
` ${comma(total)} 条 · ${comma(totalChars)} 字 · 注入过 ${comma(injected.length)} 条,估算累计注入 ${comma(spendSum)} 字` +
|
|
223
|
+
(spend.archived > 0 ? `(另有 ${spend.archived} 条已归档,不进榜也不计)` : ''),
|
|
224
|
+
)
|
|
225
|
+
lines.push(' 估算 = 注入计数 × 本条字数,是上界(每轮按名次只给每条基准的一部分)。')
|
|
226
|
+
// [0.6.71] 估算旁边摆实测:同一个工作区的注入日志,命中轮数与总字数。
|
|
227
|
+
// 「一对账」是这一版的目的——用户不必再在两份面板之间自己换算。
|
|
228
|
+
const measured = measuredInject(ws, dir)
|
|
229
|
+
if (measured === null) {
|
|
230
|
+
lines.push(' 日志实测:这个工作区还没有注入日志(dream-debug.log)——真实花了多少还不知道;按轮的实测账看 memory_admin(action="inject")。')
|
|
231
|
+
} else if (measured.rounds === 0) {
|
|
232
|
+
lines.push(
|
|
233
|
+
` 日志实测:日志在(${comma(measured.bytes)} B),但还没有一轮注入成功——估算那边有数也只说明历史上注入过;按轮的实测账看 memory_admin(action="inject")。`,
|
|
234
|
+
)
|
|
235
|
+
} else {
|
|
236
|
+
const avgRound = Math.round(measured.chars / measured.rounds)
|
|
237
|
+
lines.push(
|
|
238
|
+
` 日志实测(同一份 dream-debug.log):${comma(measured.rounds)} 轮命中注入共 ${comma(measured.chars)} 字 / 平均 ${avgRound} 字每轮` +
|
|
239
|
+
(measured.picks > 0
|
|
240
|
+
? `;每轮进了哪几条有按条记录(${comma(measured.picks)} 条次),逐条看 memory_admin(action="inject")`
|
|
241
|
+
: ';还没有按条记录(0.6.71 起 hit-chain 才写 picks=),想按轮对账就跑一轮新版'),
|
|
242
|
+
)
|
|
243
|
+
lines.push(' 估算偏大是设计如此(按排名分档给字数,真塞进去的更短);两边对不上时以日志为准。')
|
|
244
|
+
}
|
|
245
|
+
lines.push(`配额(面板读设置,0 = 不限):${quota}`)
|
|
246
|
+
const table = spend.byLevel.filter((b) => b.count > 0)
|
|
247
|
+
if (table.length > 0) {
|
|
248
|
+
lines.push('按层:')
|
|
249
|
+
for (const box of table) lines.push(levelLine(box))
|
|
250
|
+
}
|
|
251
|
+
if (spend.missing.length > 0) {
|
|
252
|
+
lines.push(` (${spend.missing.join(' / ')} 这 ${spend.missing.length} 层没有表,跳过了——老库或半张 schema 才会这样)`)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (injected.length === 0) {
|
|
256
|
+
lines.push('')
|
|
257
|
+
lines.push('还没有条目被注入过(注入计数全是 0)——要么库是新的,要么注入一直没走到这一步。')
|
|
258
|
+
const scope = total > 0 ? `${comma(total)} 条 / 全库 0 条花过额度` : ''
|
|
259
|
+
return { body: lines.join('\n'), scope }
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const ranked = injected.slice().sort((a, b) => b.spend - a.spend || b.chars - a.chars || a.id.localeCompare(b.id))
|
|
263
|
+
const top = ranked.slice(0, want)
|
|
264
|
+
lines.push('')
|
|
265
|
+
lines.push(
|
|
266
|
+
ranked.length < want ? `最吃额度的 ${ranked.length} 条(全库只有 ${ranked.length} 条注入过):` : `最吃额度的 ${want} 条:`,
|
|
267
|
+
)
|
|
268
|
+
top.forEach((row, i) => {
|
|
269
|
+
const meta = [
|
|
270
|
+
`${comma(row.spend)} 字(估)`,
|
|
271
|
+
row.level,
|
|
272
|
+
`${comma(row.chars)} 字/条 × ${comma(row.hits)} 次`,
|
|
273
|
+
`读过 ${comma(row.reads)} 次`,
|
|
274
|
+
row.last > 0 ? `最近活动 ${humanStamp(row.last)}` : '没有时间戳',
|
|
275
|
+
].join(' · ')
|
|
276
|
+
lines.push(` ${i + 1}. ${meta}`)
|
|
277
|
+
lines.push(` ${row.title}`)
|
|
278
|
+
lines.push(` id=${row.id}`)
|
|
279
|
+
lines.push(` ${exitFor(row, bodyMax)}`)
|
|
280
|
+
})
|
|
281
|
+
if (ranked.length > want) {
|
|
282
|
+
lines.push(` …后面还有 ${ranked.length - want} 条(加大 limit 看全)`)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const waste = ranked.filter((r) => r.reads === 0 && r.hits >= WASTE_MIN_HITS)
|
|
286
|
+
const wasteSpend = waste.reduce((a, r) => a + r.spend, 0)
|
|
287
|
+
const longOnes = ranked.filter((r) => r.chars > longLine(bodyMax))
|
|
288
|
+
const longest = longOnes.reduce((m, r) => Math.max(m, r.chars), 0)
|
|
289
|
+
lines.push('')
|
|
290
|
+
lines.push('两个口子:')
|
|
291
|
+
if (waste.length === 0) {
|
|
292
|
+
lines.push(` · 注入了却没人读:没有(判据是注入 ≥ ${WASTE_MIN_HITS} 次且读过 0 次)`)
|
|
293
|
+
} else {
|
|
294
|
+
const share = spendSum > 0 ? Math.round((wasteSpend / spendSum) * 100) : 0
|
|
295
|
+
lines.push(
|
|
296
|
+
` · 注入了却没人读:${waste.length} 条,估 ${comma(wasteSpend)} 字(占 ${share}%)——名单看 memory_admin(action="hygiene", kind="unused")`,
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
if (longOnes.length === 0) {
|
|
300
|
+
lines.push(` · 单条超长(> ${comma(longLine(bodyMax))} 字):没有`)
|
|
301
|
+
} else {
|
|
302
|
+
lines.push(
|
|
303
|
+
` · 单条超长(> ${comma(longLine(bodyMax))} 字):${longOnes.length} 条,最长 ${comma(longest)} 字——先 memory_read 看全文,再压短或拆条`,
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
lines.push('')
|
|
307
|
+
lines.push(`(榜单口径:命中 ${comma(injected.length)} 条按估算降序,同额按字数;已归档的不进榜)`)
|
|
308
|
+
|
|
309
|
+
const scope = `${comma(total)} 条 · 列了 ${top.length} 条${ranked.length > want ? `(前 ${want})` : ''}`
|
|
310
|
+
return { body: lines.join('\n'), scope }
|
|
311
|
+
}
|
|
312
|
+
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 存储层:macro 自己的两个 jsonl 文件「在哪、怎么写、怎么读」。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单开一个文件(0.2.21):路径与 jsonl 的读写原本散在 `cost.js`(成本账本)与 `links.js`
|
|
5
|
+
* (待补连线队列)里,于是队列要从成本账本 import 一个 `macroHome()` —— 依赖方向与现实语义
|
|
6
|
+
* 正好反了(队列不是成本的一部分),而且「读文件→按行 JSON.parse→跳过坏行」这段一模一样的
|
|
7
|
+
* 循环写了两遍。两处各自都正确,合起来就是两个错:谁都能改路径,谁都能改解析规则。
|
|
8
|
+
*
|
|
9
|
+
* 三条约定,改这个文件之前先读:
|
|
10
|
+
* 1. **`$DSH_HOME` 只由 `platform.js` 读一次**(0.7.0 前在本文件)。别处的路径一律由
|
|
11
|
+
* `ledgerPath()` / `pendingPath()` 给出,这样「换个 home」只需要改一处,doctor 也能用
|
|
12
|
+
* 一行断言盯住。
|
|
13
|
+
* 2. **jsonl 的坏行一律跳过,不抛**。这两个文件都是「降级不影响记忆主功能」的类型:
|
|
14
|
+
* 一行坏掉的账不该让整个账本读不出来。
|
|
15
|
+
* 3. **这层不认识业务**。它不知道「成本」和「待补连线」是什么,只知道文件名与行格式。
|
|
16
|
+
*/
|
|
17
|
+
import { appendFileSync, mkdirSync, readFileSync, readdirSync, statSync, unlinkSync } from 'node:fs'
|
|
18
|
+
import { join } from 'node:path'
|
|
19
|
+
import { resolveMacroHome } from './platform.js'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* macro 的数据目录:`$DSH_HOME/macro-memory`(没设 DSH_HOME 时退回 `<home>/.dsh/macro-memory`)。
|
|
23
|
+
*
|
|
24
|
+
* [macro-fusion 0.7.0 · mac 适配] 推导本身搬进了 `platform.js`,这里只留一个别名。
|
|
25
|
+
* 搬家的理由不是「代码更漂亮」:mac 上 `homedir()` 是 `/Users/<name>`、分隔符是 `/`,
|
|
26
|
+
* 而这段逻辑原先和「读 DSH_HOME」搅在一起,探针没法在不改环境变量的前提下推演另一个平台。
|
|
27
|
+
* 现在三平台的推导都是一个带 `platform` 参数的纯函数,Windows 上就能验 mac 的结果。
|
|
28
|
+
*/
|
|
29
|
+
export function macroHome() {
|
|
30
|
+
return resolveMacroHome()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 成本账本。 */
|
|
34
|
+
export function ledgerPath() {
|
|
35
|
+
return join(macroHome(), 'cost.jsonl')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 待补连线队列。 */
|
|
39
|
+
export function pendingPath() {
|
|
40
|
+
return join(macroHome(), 'pending-links.jsonl')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* [macro-fusion 0.6.43] 邻域注入的「这一会话已经带过哪些条」账本,一个会话一个文件。
|
|
45
|
+
*
|
|
46
|
+
* 为什么要有(0.6.43):邻域注入器原来的 `seen` 是内存里的 `Map<sessionId, Set>`,
|
|
47
|
+
* 一旦宿主重启/续接会话,账本就归零——同一个会话接着聊,上一轮刚注入的邻居又原样来一遍。
|
|
48
|
+
* 而这一类重复是最纯的浪费:模型在历史里已经看过那条摘要,第二遍纯计费。
|
|
49
|
+
*
|
|
50
|
+
* 文件名按会话 id 派生并做字符收敛:session id 理论上可能带路径分隔符或冒号
|
|
51
|
+
* (跨工作区那种 `ws:session-…` 写法),直接当文件名会在 Windows 上写不进去。
|
|
52
|
+
*/
|
|
53
|
+
export function neighborSeenPath(sessionId) {
|
|
54
|
+
const raw = String(sessionId ?? '').trim()
|
|
55
|
+
const safe = raw.replace(/[^A-Za-z0-9_.-]+/g, '_').slice(0, 96)
|
|
56
|
+
return join(macroHome(), 'neighbor-seen', `${safe === '' ? 'unknown' : safe}.json`)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* [macro-fusion 0.6.54] 错边观察账:一个**工作区**一份(星图是按工作区建的,不是按会话)。
|
|
61
|
+
*
|
|
62
|
+
* 为什么要独立一份:0.6.44 起邻域注入会给模型一条现成的 `nebula_link(action="mute")` 命令,
|
|
63
|
+
* 但「模型有没有动手」没有任何记录——同一跳错边在没被断掉之前,每轮都会重新被词面闸标一次
|
|
64
|
+
* 「未沾词」再带进上下文。要让它「连标几轮就暂时不带」,就得跨轮记住这一跳标过几次;
|
|
65
|
+
* 会话账本(neighbor-seen)记的是「带过哪些条」,不是「哪一跳反复出问题」,两者生命周期也不同。
|
|
66
|
+
*
|
|
67
|
+
* 文件名同样做字符收敛(工作区是绝对路径,含 : \ / 全都不合法),取尾部 96 字符:
|
|
68
|
+
* 同一台机器上工作区路径的尾部才是区分度所在。
|
|
69
|
+
*/
|
|
70
|
+
export function neighborSuspectPath(workspace) {
|
|
71
|
+
const raw = String(workspace ?? '').trim()
|
|
72
|
+
const safe = raw.replace(/[^A-Za-z0-9_.-]+/g, '_').slice(-96)
|
|
73
|
+
return join(macroHome(), 'neighbor-suspect', `${safe === '' ? 'unknown' : safe}.json`)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* [0.6.66] 修过账:一个工作区一份,记「哪几跳被摘掉了、第几轮、凭什么摘的」。
|
|
78
|
+
*
|
|
79
|
+
* 为什么错边账之外还要这一份:0.6.54/0.6.65 让模型能销账,销完这一跳就从观察账里消失了——
|
|
80
|
+
* 于是「这条边上周已经判过无关」这件事在本地一个字都不剩。三处会因此难受:
|
|
81
|
+
* ① 面板看不出「已经修过几跳」,看起来永远像没动过手;
|
|
82
|
+
* ② 模型隔几天再遇到同一批邻居,会重新判一遍、重新摘一遍;
|
|
83
|
+
* ③ 摘错了没有回头路可讲——`unmute` 得知道两端的 id,而账已经没了。
|
|
84
|
+
* 星图自己记得 mute(重建也不会再连),但它只记得「断了」,不记得「为什么断」。
|
|
85
|
+
*/
|
|
86
|
+
export function neighborFixesPath(workspace) {
|
|
87
|
+
const raw = String(workspace ?? '').trim()
|
|
88
|
+
const safe = raw.replace(/[^A-Za-z0-9_.-]+/g, '_').slice(-96)
|
|
89
|
+
return join(macroHome(), 'neighbor-fixes', `${safe === '' ? 'unknown' : safe}.json`)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* [macro-fusion 0.6.44] 单个会话账本里最多留多少条 id。
|
|
94
|
+
*
|
|
95
|
+
* 为什么要上限(0.6.44):这份账本是「这一会话已经带过哪些邻居」,只进不出——
|
|
96
|
+
* 一个跑了几百轮的会话会把文件写成几百 KB,每次 decide 还要整份 parse 一遍。
|
|
97
|
+
* 400 条已经远超任何真实会话的有效记忆量(一轮最多 4 条邻域),再往前的
|
|
98
|
+
* 早就被窗口挤出去了,留在账本里只是让读盘越来越慢。
|
|
99
|
+
*/
|
|
100
|
+
export const NEIGHBOR_SEEN_MAX_IDS = 400
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* [macro-fusion 0.6.44] 清理邻域账本目录:按天数与文件数两条线,都只删**本插件自己的缓存**。
|
|
104
|
+
*
|
|
105
|
+
* 为什么要有(0.6.44):`neighbor-seen/` 一个会话一个文件,宿主重启、临时会话、
|
|
106
|
+
* 探针、子代理都会留下文件,从来没有人删。跑久了目录里躺着几百个几十字节的小文件,
|
|
107
|
+
* 每次列目录/备份都拖慢。这里给两条线:太老的(默认 30 天)与太多的(默认留最近 200 个)。
|
|
108
|
+
*
|
|
109
|
+
* 三条约束:
|
|
110
|
+
* ① 只碰 `<macroHome>/neighbor-seen/*.json`,别的路径一个字节都不动;
|
|
111
|
+
* ② 任何一步失败都只回在返回值里,不抛——这是缓存清理,不是主流程;
|
|
112
|
+
* ③ `maxFiles <= 0` 表示不限文件数(只按天数删)。
|
|
113
|
+
*
|
|
114
|
+
* @param {{maxAgeDays?: number, maxFiles?: number, now?: number}} opts
|
|
115
|
+
* @returns {{scanned: number, removedByAge: number, removedByCount: number, error: string}}
|
|
116
|
+
*/
|
|
117
|
+
export function pruneNeighborSeen(opts = {}) {
|
|
118
|
+
const maxAgeDays = Number.isFinite(opts.maxAgeDays) ? Math.max(0, opts.maxAgeDays) : 30
|
|
119
|
+
const maxFiles = Number.isFinite(opts.maxFiles) ? Math.max(0, Math.floor(opts.maxFiles)) : 200
|
|
120
|
+
const now = Number.isFinite(opts.now) ? opts.now : Date.now()
|
|
121
|
+
const out = { scanned: 0, removedByAge: 0, removedByCount: 0, error: '' }
|
|
122
|
+
const dir = join(macroHome(), 'neighbor-seen')
|
|
123
|
+
let names = []
|
|
124
|
+
try {
|
|
125
|
+
names = readdirSync(dir).filter((n) => n.endsWith('.json'))
|
|
126
|
+
} catch (err) {
|
|
127
|
+
// ENOENT 是常态(还没写过账本),不当错误。
|
|
128
|
+
if (err && err.code !== 'ENOENT') out.error = String(err?.message ?? err)
|
|
129
|
+
return out
|
|
130
|
+
}
|
|
131
|
+
const files = []
|
|
132
|
+
for (const name of names) {
|
|
133
|
+
const file = join(dir, name)
|
|
134
|
+
try {
|
|
135
|
+
const st = statSync(file)
|
|
136
|
+
if (!st.isFile()) continue
|
|
137
|
+
files.push({ file, mtime: st.mtimeMs })
|
|
138
|
+
out.scanned += 1
|
|
139
|
+
} catch {
|
|
140
|
+
// 列目录与 stat 之间被删掉了:跳过就行。
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const cutoff = now - maxAgeDays * 86400000
|
|
144
|
+
const survivors = []
|
|
145
|
+
for (const f of files) {
|
|
146
|
+
if (maxAgeDays > 0 && f.mtime < cutoff) {
|
|
147
|
+
try {
|
|
148
|
+
unlinkSync(f.file)
|
|
149
|
+
out.removedByAge += 1
|
|
150
|
+
continue
|
|
151
|
+
} catch {
|
|
152
|
+
// 删不动就留着,下次数的时候再说。
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
survivors.push(f)
|
|
156
|
+
}
|
|
157
|
+
if (maxFiles > 0 && survivors.length > maxFiles) {
|
|
158
|
+
survivors.sort((a, b) => b.mtime - a.mtime)
|
|
159
|
+
for (const f of survivors.slice(maxFiles)) {
|
|
160
|
+
try {
|
|
161
|
+
unlinkSync(f.file)
|
|
162
|
+
out.removedByCount += 1
|
|
163
|
+
} catch {
|
|
164
|
+
// 同上。
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return out
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 读一个 jsonl 文件,**并把「为什么没读到」一起回出来**(0.2.55)。
|
|
173
|
+
*
|
|
174
|
+
* [macro-fusion 0.2.55] 一处 `[]` 原来同时代表四种来路:
|
|
175
|
+
* 从没写过 / 文件读不出来(EACCES、EISDIR)/ 整份都是坏行 / 写了但一条都没落成。
|
|
176
|
+
* 上游面板只能拿这个「空」去编一句话,就变成「账本还是空的,调用几次再看」——
|
|
177
|
+
* 明明是我们写不进去,说出来却像用户没调用过。
|
|
178
|
+
*
|
|
179
|
+
* @returns {{entries: any[], exists: boolean, unreadable: boolean, badLines: number, lines: number, error: string}}
|
|
180
|
+
*/
|
|
181
|
+
export function readJsonlDetail(file) {
|
|
182
|
+
let raw = ''
|
|
183
|
+
try {
|
|
184
|
+
raw = readFileSync(file, 'utf8')
|
|
185
|
+
} catch (err) {
|
|
186
|
+
const missing = err && err.code === 'ENOENT'
|
|
187
|
+
// ENOENT 才是「真的没有」;别的错误(权限、把目录当文件读)都是「这次读不出来」
|
|
188
|
+
return {
|
|
189
|
+
entries: [],
|
|
190
|
+
exists: !missing,
|
|
191
|
+
unreadable: !missing,
|
|
192
|
+
badLines: 0,
|
|
193
|
+
lines: 0,
|
|
194
|
+
error: missing ? '' : String(err?.message ?? err),
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const entries = []
|
|
198
|
+
let badLines = 0
|
|
199
|
+
let lines = 0
|
|
200
|
+
for (const line of raw.split('\n')) {
|
|
201
|
+
if (line.trim().length === 0) continue
|
|
202
|
+
lines += 1
|
|
203
|
+
try {
|
|
204
|
+
entries.push(JSON.parse(line))
|
|
205
|
+
} catch {
|
|
206
|
+
badLines += 1
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return { entries, exists: true, unreadable: false, badLines, lines, error: '' }
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* 读一个 jsonl 文件,返回能解析的条目数组。
|
|
214
|
+
* 文件不存在 → 空数组;空行与坏行 → 跳过(约定 2)。
|
|
215
|
+
*/
|
|
216
|
+
export function readJsonl(file) {
|
|
217
|
+
return readJsonlDetail(file).entries
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 追加一行 JSON(目录不存在就先建),并把失败原因带出来(0.2.55)。
|
|
222
|
+
* 约定 2 是「写不进去不打断调用方」,不是「写不进去不许说」——调用方可以继续省略,
|
|
223
|
+
* 但面板需要知道这批账到底存下来了没有。
|
|
224
|
+
*/
|
|
225
|
+
export function appendJsonlDetail(file, entry) {
|
|
226
|
+
try {
|
|
227
|
+
mkdirSync(macroHome(), { recursive: true })
|
|
228
|
+
appendFileSync(file, `${JSON.stringify(entry)}\n`, 'utf8')
|
|
229
|
+
return { ok: true, error: '' }
|
|
230
|
+
} catch (err) {
|
|
231
|
+
return { ok: false, error: String(err?.message ?? err) }
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** 追加一行 JSON(目录不存在就先建);写不进去就算了,绝不打断调用方。 */
|
|
236
|
+
export function appendJsonl(file, entry) {
|
|
237
|
+
return appendJsonlDetail(file, entry).ok
|
|
238
|
+
}
|