whale-memory-core 0.14.0 → 0.18.2
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 +14 -0
- package/bin/whale-memory-mcp.mjs +276 -0
- package/demo.mjs +44 -33
- package/index.d.ts +16 -13
- package/index.js +18 -15
- package/lib/archive.d.ts +22 -0
- package/lib/archive.js +171 -0
- package/lib/as-of.d.ts +5 -5
- package/lib/as-of.js +32 -32
- package/lib/book.d.ts +4 -4
- package/lib/book.js +75 -75
- package/lib/chapter-events.d.ts +39 -39
- package/lib/chapter-events.js +89 -89
- package/lib/detail-recall.d.ts +15 -0
- package/lib/detail-recall.js +174 -0
- package/lib/event-log.d.ts +10 -10
- package/lib/event-log.js +61 -61
- package/lib/evidence-from-experience.d.ts +8 -8
- package/lib/evidence-from-experience.js +62 -62
- package/lib/evidence-policy.js +2 -2
- package/lib/fact-timeline-contract.d.ts +73 -0
- package/lib/fact-timeline-contract.js +226 -0
- package/lib/fact-timeline-query.d.ts +29 -0
- package/lib/fact-timeline-query.js +97 -0
- package/lib/file-lock.js +1 -1
- package/lib/later-events.d.ts +21 -21
- package/lib/later-events.js +80 -80
- package/lib/link-events.d.ts +15 -15
- package/lib/link-events.js +33 -33
- package/lib/query-match.js +55 -0
- package/lib/recall-events.d.ts +24 -24
- package/lib/recall-events.js +97 -97
- package/lib/recap.d.ts +23 -23
- package/lib/recap.js +79 -79
- package/lib/seal-events.d.ts +25 -25
- package/lib/seal-events.js +82 -82
- package/lib/self-experience.d.ts +180 -175
- package/lib/self-experience.js +624 -560
- package/lib/self-memory.js +70 -68
- package/lib/self-prompt.js +8 -4
- package/lib/self-settings.js +2 -2
- package/lib/unfinished-events.d.ts +26 -26
- package/lib/unfinished-events.js +105 -105
- package/lib/unsaid.d.ts +28 -0
- package/lib/unsaid.js +105 -0
- package/lib/waking.d.ts +1 -1
- package/lib/waking.js +99 -92
- package/package.json +127 -105
package/lib/recap.js
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
// 回顾材料:把这几天发生过的整理出来,给她写回想用
|
|
2
|
-
// 只列事实——写下了什么、改过什么想法、感觉怎么变的、哪些事想通或放下了、哪些还没弄完
|
|
3
|
-
// 不调模型、不下结论、不替她归纳;她封存了的不列。要不要写回想、从哪几件事想到的,由她定
|
|
4
|
-
import { loadAll } from './self-experience.js'
|
|
5
|
-
import { parseAsOf, dayOf } from './as-of.js'
|
|
6
|
-
|
|
7
|
-
const snippet = (s, n = 40) => { const c = [...String(s || '')]; return c.length > n ? c.slice(0, n).join('') + '…' : c.join('') }
|
|
8
|
-
|
|
9
|
-
// days:往回看几天(1–366);asOf:以那一刻为"今天"往回看(只看那时的她)
|
|
10
|
-
export function recap(dir, { days = 7, asOf = null, now = new Date() } = {}) {
|
|
11
|
-
if (!Number.isInteger(days) || days < 1 || days > 366) throw new Error('days 要是 1 到 366
|
|
12
|
-
const cut = parseAsOf(asOf)
|
|
13
|
-
const end = cut === null ? now.getTime() : cut
|
|
14
|
-
// 按自然日:含"今天"在内往回 days 天,从那天本地零点算起
|
|
15
|
-
const e = new Date(end)
|
|
16
|
-
const start = new Date(e.getFullYear(), e.getMonth(), e.getDate() - (days - 1)).getTime() - 1
|
|
17
|
-
const within = (at) => { const t = Date.parse(String(at || '')); return Number.isFinite(t) && t > start && t <= end }
|
|
18
|
-
const rows = loadAll(dir, { asOf: cut === null ? null : asOf }).filter((r) => !r.sealed)
|
|
19
|
-
const head = (r) => ({ id: r.id, day: r.day, text: snippet(r.then.text) })
|
|
20
|
-
|
|
21
|
-
const written = rows.filter((r) => within(r.at)).map((r) => ({ ...head(r), kind: r.kind, feeling: r.then.feeling || [], refs: r.then.refs || [] }))
|
|
22
|
-
const revised = []
|
|
23
|
-
const feelingShifts = []
|
|
24
|
-
for (const r of rows) {
|
|
25
|
-
const inRange = (r.later || []).filter((l) => within(l.at))
|
|
26
|
-
for (const l of inRange) revised.push({ ...head(r), thought: String(l.thought || ''), feeling: l.feeling || [], at: l.at, laterDay: l.day || String(l.at || '').slice(0, 10) })
|
|
27
|
-
const felt = [...inRange].reverse().find((l) => Array.isArray(l.feeling) && l.feeling.length)
|
|
28
|
-
if (felt) feelingShifts.push({ ...head(r), then: r.then.feeling || [], now: felt.feeling, laterDay: felt.day || String(felt.at || '').slice(0, 10) })
|
|
29
|
-
}
|
|
30
|
-
const settled = []
|
|
31
|
-
const reopened = []
|
|
32
|
-
for (const r of rows) {
|
|
33
|
-
const st = r.unfinishedState
|
|
34
|
-
if (!st || !within(st.at)) continue
|
|
35
|
-
const item = { ...head(r), unfinished: r.then.unfinished, choice: st.choice, stateDay: st.day }
|
|
36
|
-
if (st.choice === 'reopen') reopened.push(item)
|
|
37
|
-
else settled.push(item)
|
|
38
|
-
}
|
|
39
|
-
const stillOpen = rows
|
|
40
|
-
.filter((r) => r.then.unfinished && !(r.unfinishedState && (r.unfinishedState.choice === 'done' || r.unfinishedState.choice === 'shelved')))
|
|
41
|
-
.slice(-5)
|
|
42
|
-
.map((r) => ({ ...head(r), unfinished: r.then.unfinished }))
|
|
43
|
-
|
|
44
|
-
return { from: dayOf(start + 1), to: dayOf(end), days, written, revised, feelingShifts, settled, reopened, stillOpen }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const at = (x) => (x.day || '') + '「' + x.text + '」(' + x.id + ')'
|
|
48
|
-
|
|
49
|
-
export function renderRecap(rc) {
|
|
50
|
-
const lines = ['【这几天(' + rc.from + ' ~ ' + rc.to + ')】']
|
|
51
|
-
const empty = !rc.written.length && !rc.revised.length && !rc.settled.length && !rc.reopened.length
|
|
52
|
-
if (empty) lines.push('这几天没有新写下的,也没有改过想法。')
|
|
53
|
-
if (rc.written.length) {
|
|
54
|
-
lines.push('写下了 ' + rc.written.length + ' 件:')
|
|
55
|
-
for (const x of rc.written) lines.push('- ' + at(x) + (x.feeling.length ? ' 当时的感觉:' + x.feeling.join('、') : '') + (x.kind === '回想' ? '(这是一条回想)' : ''))
|
|
56
|
-
}
|
|
57
|
-
if (rc.revised.length) {
|
|
58
|
-
lines.push('回头想过的:')
|
|
59
|
-
for (const x of rc.revised) lines.push('- ' + at(x) + ' → ' + x.laterDay + ' 写了「' + snippet(x.thought, 60) + '」')
|
|
60
|
-
}
|
|
61
|
-
if (rc.feelingShifts.length) {
|
|
62
|
-
lines.push('感觉变了的:')
|
|
63
|
-
for (const x of rc.feelingShifts) lines.push('- ' + at(x) + ':当时 ' + (x.then.length ? x.then.join('、') : '没写') + ' → ' + x.laterDay + ' ' + x.now.join('、'))
|
|
64
|
-
}
|
|
65
|
-
if (rc.settled.length) {
|
|
66
|
-
lines.push('有了决定的:')
|
|
67
|
-
for (const x of rc.settled) lines.push('- 「' + x.unfinished + '」' + (x.choice === 'done' ? '想通了' : '先放下了') + '(' + x.stateDay + ',' + x.id + ')')
|
|
68
|
-
}
|
|
69
|
-
if (rc.reopened.length) {
|
|
70
|
-
lines.push('又拾起来的:')
|
|
71
|
-
for (const x of rc.reopened) lines.push('- 「' + x.unfinished + '」(' + x.stateDay + ',' + x.id + ')')
|
|
72
|
-
}
|
|
73
|
-
if (rc.stillOpen.length) {
|
|
74
|
-
lines.push('还没弄完的:')
|
|
75
|
-
for (const x of rc.stillOpen) lines.push('- 「' + x.unfinished + '」(' + x.id + ')')
|
|
76
|
-
}
|
|
77
|
-
lines.push('(这些是记录,不是结论。想写一条回想,就用 write --kind 回想 --refs 带上你想到的那几条 id;不想写也可以。)')
|
|
78
|
-
return lines.join('\n')
|
|
79
|
-
}
|
|
1
|
+
// 回顾材料:把这几天发生过的整理出来,给她写回想用
|
|
2
|
+
// 只列事实——写下了什么、改过什么想法、感觉怎么变的、哪些事想通或放下了、哪些还没弄完
|
|
3
|
+
// 不调模型、不下结论、不替她归纳;她封存了的不列。要不要写回想、从哪几件事想到的,由她定
|
|
4
|
+
import { loadAll } from './self-experience.js'
|
|
5
|
+
import { parseAsOf, dayOf } from './as-of.js'
|
|
6
|
+
|
|
7
|
+
const snippet = (s, n = 40) => { const c = [...String(s || '')]; return c.length > n ? c.slice(0, n).join('') + '…' : c.join('') }
|
|
8
|
+
|
|
9
|
+
// days:往回看几天(1–366);asOf:以那一刻为"今天"往回看(只看那时的她)
|
|
10
|
+
export function recap(dir, { days = 7, asOf = null, now = new Date() } = {}) {
|
|
11
|
+
if (!Number.isInteger(days) || days < 1 || days > 366) throw new Error('days must be an integer from 1 to 366.(days 要是 1 到 366 之间的整数。)')
|
|
12
|
+
const cut = parseAsOf(asOf)
|
|
13
|
+
const end = cut === null ? now.getTime() : cut
|
|
14
|
+
// 按自然日:含"今天"在内往回 days 天,从那天本地零点算起
|
|
15
|
+
const e = new Date(end)
|
|
16
|
+
const start = new Date(e.getFullYear(), e.getMonth(), e.getDate() - (days - 1)).getTime() - 1
|
|
17
|
+
const within = (at) => { const t = Date.parse(String(at || '')); return Number.isFinite(t) && t > start && t <= end }
|
|
18
|
+
const rows = loadAll(dir, { asOf: cut === null ? null : asOf }).filter((r) => !r.sealed)
|
|
19
|
+
const head = (r) => ({ id: r.id, day: r.day, text: snippet(r.then.text) })
|
|
20
|
+
|
|
21
|
+
const written = rows.filter((r) => within(r.at)).map((r) => ({ ...head(r), kind: r.kind, feeling: r.then.feeling || [], refs: r.then.refs || [] }))
|
|
22
|
+
const revised = []
|
|
23
|
+
const feelingShifts = []
|
|
24
|
+
for (const r of rows) {
|
|
25
|
+
const inRange = (r.later || []).filter((l) => within(l.at))
|
|
26
|
+
for (const l of inRange) revised.push({ ...head(r), thought: String(l.thought || ''), feeling: l.feeling || [], at: l.at, laterDay: l.day || String(l.at || '').slice(0, 10) })
|
|
27
|
+
const felt = [...inRange].reverse().find((l) => Array.isArray(l.feeling) && l.feeling.length)
|
|
28
|
+
if (felt) feelingShifts.push({ ...head(r), then: r.then.feeling || [], now: felt.feeling, laterDay: felt.day || String(felt.at || '').slice(0, 10) })
|
|
29
|
+
}
|
|
30
|
+
const settled = []
|
|
31
|
+
const reopened = []
|
|
32
|
+
for (const r of rows) {
|
|
33
|
+
const st = r.unfinishedState
|
|
34
|
+
if (!st || !within(st.at)) continue
|
|
35
|
+
const item = { ...head(r), unfinished: r.then.unfinished, choice: st.choice, stateDay: st.day }
|
|
36
|
+
if (st.choice === 'reopen') reopened.push(item)
|
|
37
|
+
else settled.push(item)
|
|
38
|
+
}
|
|
39
|
+
const stillOpen = rows
|
|
40
|
+
.filter((r) => r.then.unfinished && !(r.unfinishedState && (r.unfinishedState.choice === 'done' || r.unfinishedState.choice === 'shelved')))
|
|
41
|
+
.slice(-5)
|
|
42
|
+
.map((r) => ({ ...head(r), unfinished: r.then.unfinished }))
|
|
43
|
+
|
|
44
|
+
return { from: dayOf(start + 1), to: dayOf(end), days, written, revised, feelingShifts, settled, reopened, stillOpen }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const at = (x) => (x.day || '') + '「' + x.text + '」(' + x.id + ')'
|
|
48
|
+
|
|
49
|
+
export function renderRecap(rc) {
|
|
50
|
+
const lines = ['【这几天(' + rc.from + ' ~ ' + rc.to + ')】']
|
|
51
|
+
const empty = !rc.written.length && !rc.revised.length && !rc.settled.length && !rc.reopened.length
|
|
52
|
+
if (empty) lines.push('这几天没有新写下的,也没有改过想法。')
|
|
53
|
+
if (rc.written.length) {
|
|
54
|
+
lines.push('写下了 ' + rc.written.length + ' 件:')
|
|
55
|
+
for (const x of rc.written) lines.push('- ' + at(x) + (x.feeling.length ? ' 当时的感觉:' + x.feeling.join('、') : '') + (x.kind === '回想' ? '(这是一条回想)' : ''))
|
|
56
|
+
}
|
|
57
|
+
if (rc.revised.length) {
|
|
58
|
+
lines.push('回头想过的:')
|
|
59
|
+
for (const x of rc.revised) lines.push('- ' + at(x) + ' → ' + x.laterDay + ' 写了「' + snippet(x.thought, 60) + '」')
|
|
60
|
+
}
|
|
61
|
+
if (rc.feelingShifts.length) {
|
|
62
|
+
lines.push('感觉变了的:')
|
|
63
|
+
for (const x of rc.feelingShifts) lines.push('- ' + at(x) + ':当时 ' + (x.then.length ? x.then.join('、') : '没写') + ' → ' + x.laterDay + ' ' + x.now.join('、'))
|
|
64
|
+
}
|
|
65
|
+
if (rc.settled.length) {
|
|
66
|
+
lines.push('有了决定的:')
|
|
67
|
+
for (const x of rc.settled) lines.push('- 「' + x.unfinished + '」' + (x.choice === 'done' ? '想通了' : '先放下了') + '(' + x.stateDay + ',' + x.id + ')')
|
|
68
|
+
}
|
|
69
|
+
if (rc.reopened.length) {
|
|
70
|
+
lines.push('又拾起来的:')
|
|
71
|
+
for (const x of rc.reopened) lines.push('- 「' + x.unfinished + '」(' + x.stateDay + ',' + x.id + ')')
|
|
72
|
+
}
|
|
73
|
+
if (rc.stillOpen.length) {
|
|
74
|
+
lines.push('还没弄完的:')
|
|
75
|
+
for (const x of rc.stillOpen) lines.push('- 「' + x.unfinished + '」(' + x.id + ')')
|
|
76
|
+
}
|
|
77
|
+
lines.push('(这些是记录,不是结论。想写一条回想,就用 write --kind 回想 --refs 带上你想到的那几条 id;不想写也可以。)')
|
|
78
|
+
return lines.join('\n')
|
|
79
|
+
}
|
package/lib/seal-events.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
export type SealAction = 'seal' | 'unseal'
|
|
2
|
-
|
|
3
|
-
export const ACTIONS: Set<SealAction>
|
|
4
|
-
|
|
5
|
-
export interface SealEvent {
|
|
6
|
-
id: string
|
|
7
|
-
at: string
|
|
8
|
-
day: string
|
|
9
|
-
action: SealAction
|
|
10
|
-
note: string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface SealedInfo {
|
|
14
|
-
at: string
|
|
15
|
-
day: string
|
|
16
|
-
note: string
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function eventsFile(memDir: string): string
|
|
20
|
-
/** 同一把锁里重读 → 判断 → 追加;已是这个状态就 noop,不写第二条 */
|
|
21
|
-
export function decide(memDir: string, opts: { id: string; action: SealAction; note?: string }, lockOpts?: Record<string, unknown>): { row: SealEvent | null; noop: boolean }
|
|
22
|
-
export function readAll(memDir: string): { rows: SealEvent[]; bad: number }
|
|
23
|
-
/** 现在(或截至 until)仍封存着的经历 */
|
|
24
|
-
export function sealedByExp(memDir: string, opts?: { until?: number | null }): Map<string, SealedInfo>
|
|
25
|
-
export const internals: { FILE: string }
|
|
1
|
+
export type SealAction = 'seal' | 'unseal'
|
|
2
|
+
|
|
3
|
+
export const ACTIONS: Set<SealAction>
|
|
4
|
+
|
|
5
|
+
export interface SealEvent {
|
|
6
|
+
id: string
|
|
7
|
+
at: string
|
|
8
|
+
day: string
|
|
9
|
+
action: SealAction
|
|
10
|
+
note: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface SealedInfo {
|
|
14
|
+
at: string
|
|
15
|
+
day: string
|
|
16
|
+
note: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function eventsFile(memDir: string): string
|
|
20
|
+
/** 同一把锁里重读 → 判断 → 追加;已是这个状态就 noop,不写第二条 */
|
|
21
|
+
export function decide(memDir: string, opts: { id: string; action: SealAction; note?: string }, lockOpts?: Record<string, unknown>): { row: SealEvent | null; noop: boolean }
|
|
22
|
+
export function readAll(memDir: string): { rows: SealEvent[]; bad: number }
|
|
23
|
+
/** 现在(或截至 until)仍封存着的经历 */
|
|
24
|
+
export function sealedByExp(memDir: string, opts?: { until?: number | null }): Map<string, SealedInfo>
|
|
25
|
+
export const internals: { FILE: string }
|
package/lib/seal-events.js
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
// 她选择的遗忘:封存 / 解封。一行一个 { id, at, day, action, note? },只追加
|
|
2
|
-
// 封存不是删除:原文、后记、选择都还在;只是不再被主动想起(召回、醒来、别的回想里的摘要都避开它)
|
|
3
|
-
// 要真的抹掉,仍然只有 purge 整库删除
|
|
4
|
-
import fs from 'node:fs'
|
|
5
|
-
import path from 'node:path'
|
|
6
|
-
import { withFileLock } from './file-lock.js'
|
|
7
|
-
import { before } from './as-of.js'
|
|
8
|
-
|
|
9
|
-
const FILE = 'seal-events.jsonl'
|
|
10
|
-
export const ACTIONS = new Set(['seal', 'unseal'])
|
|
11
|
-
|
|
12
|
-
export function eventsFile(memDir) {
|
|
13
|
-
return path.join(memDir, FILE)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function makeRow({ id, action, note }) {
|
|
17
|
-
const now = new Date()
|
|
18
|
-
return { id: String(id), at: now.toISOString(), day: now.toLocaleDateString('sv-SE'), action, note: String(note || '').slice(0, 200) }
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// 调用前必须已经持有锁
|
|
22
|
-
function appendUnlocked(memDir, row) {
|
|
23
|
-
const file = eventsFile(memDir)
|
|
24
|
-
let prefix = ''
|
|
25
|
-
try {
|
|
26
|
-
const st = fs.statSync(file)
|
|
27
|
-
if (st.size) {
|
|
28
|
-
const fd = fs.openSync(file, 'r')
|
|
29
|
-
const buf = Buffer.alloc(1)
|
|
30
|
-
fs.readSync(fd, buf, 0, 1, st.size - 1)
|
|
31
|
-
fs.closeSync(fd)
|
|
32
|
-
if (buf[0] !== 10) prefix = '\n'
|
|
33
|
-
}
|
|
34
|
-
} catch { }
|
|
35
|
-
fs.appendFileSync(file, prefix + JSON.stringify(row) + '\n', 'utf8')
|
|
36
|
-
return row
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// 同一把锁里「重读当前状态 → 判断 → 追加」:已经封存的再封存、没封存的解封,都是空操作,不写第二条
|
|
40
|
-
export function decide(memDir, { id, action, note = '' } = {}, opts = {}) {
|
|
41
|
-
if (!String(id || '').trim()) throw new Error('id
|
|
42
|
-
if (!ACTIONS.has(action)) throw new Error('action 只能是 seal / unseal')
|
|
43
|
-
fs.mkdirSync(memDir, { recursive: true })
|
|
44
|
-
return withFileLock(eventsFile(memDir) + '.lock', () => {
|
|
45
|
-
const rows = readAll(memDir).rows.filter((r) => r.id === String(id))
|
|
46
|
-
const last = rows[rows.length - 1] || null
|
|
47
|
-
const sealedNow = Boolean(last && last.action === 'seal')
|
|
48
|
-
if ((action === 'seal' && sealedNow) || (action === 'unseal' && !sealedNow)) return { row: last, noop: true }
|
|
49
|
-
return { row: appendUnlocked(memDir, makeRow({ id, action, note })), noop: false }
|
|
50
|
-
}, opts)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function readAll(memDir) {
|
|
54
|
-
let raw = ''
|
|
55
|
-
try { raw = fs.readFileSync(eventsFile(memDir), 'utf8') } catch { return { rows: [], bad: 0 } }
|
|
56
|
-
const rows = []
|
|
57
|
-
let bad = 0
|
|
58
|
-
for (const line of raw.split('\n')) {
|
|
59
|
-
if (!line.trim()) continue
|
|
60
|
-
try {
|
|
61
|
-
const r = JSON.parse(line)
|
|
62
|
-
if (r && r.id && ACTIONS.has(r.action)) rows.push(r)
|
|
63
|
-
else bad += 1
|
|
64
|
-
} catch { bad += 1 }
|
|
65
|
-
}
|
|
66
|
-
return { rows, bad }
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// 每个经历 id 现在是否封存着:只收最后一次是 seal 的;until 用于翻回那天(那时还没封存就不算)
|
|
70
|
-
export function sealedByExp(memDir, { until = null } = {}) {
|
|
71
|
-
const { rows } = readAll(memDir)
|
|
72
|
-
const last = new Map()
|
|
73
|
-
for (const r of rows) {
|
|
74
|
-
if (!before(r, until)) continue
|
|
75
|
-
last.set(r.id, r)
|
|
76
|
-
}
|
|
77
|
-
const map = new Map()
|
|
78
|
-
for (const [id, r] of last) if (r.action === 'seal') map.set(id, { at: r.at, day: r.day, note: r.note || '' })
|
|
79
|
-
return map
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export const internals = { FILE }
|
|
1
|
+
// 她选择的遗忘:封存 / 解封。一行一个 { id, at, day, action, note? },只追加
|
|
2
|
+
// 封存不是删除:原文、后记、选择都还在;只是不再被主动想起(召回、醒来、别的回想里的摘要都避开它)
|
|
3
|
+
// 要真的抹掉,仍然只有 purge 整库删除
|
|
4
|
+
import fs from 'node:fs'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import { withFileLock } from './file-lock.js'
|
|
7
|
+
import { before } from './as-of.js'
|
|
8
|
+
|
|
9
|
+
const FILE = 'seal-events.jsonl'
|
|
10
|
+
export const ACTIONS = new Set(['seal', 'unseal'])
|
|
11
|
+
|
|
12
|
+
export function eventsFile(memDir) {
|
|
13
|
+
return path.join(memDir, FILE)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function makeRow({ id, action, note }) {
|
|
17
|
+
const now = new Date()
|
|
18
|
+
return { id: String(id), at: now.toISOString(), day: now.toLocaleDateString('sv-SE'), action, note: String(note || '').slice(0, 200) }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 调用前必须已经持有锁
|
|
22
|
+
function appendUnlocked(memDir, row) {
|
|
23
|
+
const file = eventsFile(memDir)
|
|
24
|
+
let prefix = ''
|
|
25
|
+
try {
|
|
26
|
+
const st = fs.statSync(file)
|
|
27
|
+
if (st.size) {
|
|
28
|
+
const fd = fs.openSync(file, 'r')
|
|
29
|
+
const buf = Buffer.alloc(1)
|
|
30
|
+
fs.readSync(fd, buf, 0, 1, st.size - 1)
|
|
31
|
+
fs.closeSync(fd)
|
|
32
|
+
if (buf[0] !== 10) prefix = '\n'
|
|
33
|
+
}
|
|
34
|
+
} catch { }
|
|
35
|
+
fs.appendFileSync(file, prefix + JSON.stringify(row) + '\n', 'utf8')
|
|
36
|
+
return row
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 同一把锁里「重读当前状态 → 判断 → 追加」:已经封存的再封存、没封存的解封,都是空操作,不写第二条
|
|
40
|
+
export function decide(memDir, { id, action, note = '' } = {}, opts = {}) {
|
|
41
|
+
if (!String(id || '').trim()) throw new Error('id cannot be empty(id 不能为空)')
|
|
42
|
+
if (!ACTIONS.has(action)) throw new Error('action must be seal / unseal(action 只能是 seal / unseal)')
|
|
43
|
+
fs.mkdirSync(memDir, { recursive: true })
|
|
44
|
+
return withFileLock(eventsFile(memDir) + '.lock', () => {
|
|
45
|
+
const rows = readAll(memDir).rows.filter((r) => r.id === String(id))
|
|
46
|
+
const last = rows[rows.length - 1] || null
|
|
47
|
+
const sealedNow = Boolean(last && last.action === 'seal')
|
|
48
|
+
if ((action === 'seal' && sealedNow) || (action === 'unseal' && !sealedNow)) return { row: last, noop: true }
|
|
49
|
+
return { row: appendUnlocked(memDir, makeRow({ id, action, note })), noop: false }
|
|
50
|
+
}, opts)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function readAll(memDir) {
|
|
54
|
+
let raw = ''
|
|
55
|
+
try { raw = fs.readFileSync(eventsFile(memDir), 'utf8') } catch { return { rows: [], bad: 0 } }
|
|
56
|
+
const rows = []
|
|
57
|
+
let bad = 0
|
|
58
|
+
for (const line of raw.split('\n')) {
|
|
59
|
+
if (!line.trim()) continue
|
|
60
|
+
try {
|
|
61
|
+
const r = JSON.parse(line)
|
|
62
|
+
if (r && r.id && ACTIONS.has(r.action)) rows.push(r)
|
|
63
|
+
else bad += 1
|
|
64
|
+
} catch { bad += 1 }
|
|
65
|
+
}
|
|
66
|
+
return { rows, bad }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 每个经历 id 现在是否封存着:只收最后一次是 seal 的;until 用于翻回那天(那时还没封存就不算)
|
|
70
|
+
export function sealedByExp(memDir, { until = null } = {}) {
|
|
71
|
+
const { rows } = readAll(memDir)
|
|
72
|
+
const last = new Map()
|
|
73
|
+
for (const r of rows) {
|
|
74
|
+
if (!before(r, until)) continue
|
|
75
|
+
last.set(r.id, r)
|
|
76
|
+
}
|
|
77
|
+
const map = new Map()
|
|
78
|
+
for (const [id, r] of last) if (r.action === 'seal') map.set(id, { at: r.at, day: r.day, note: r.note || '' })
|
|
79
|
+
return map
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const internals = { FILE }
|