whale-memory-core 0.11.1 → 0.14.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/index.d.ts +13 -7
- package/index.js +15 -10
- package/lib/as-of.d.ts +5 -0
- package/lib/as-of.js +32 -0
- package/lib/book.d.ts +4 -0
- package/lib/book.js +75 -0
- package/lib/chapter-events.d.ts +39 -0
- package/lib/chapter-events.js +89 -0
- package/lib/event-log.d.ts +10 -0
- package/lib/event-log.js +61 -0
- package/lib/evidence-from-experience.d.ts +8 -6
- package/lib/evidence-from-experience.js +62 -40
- package/lib/later-events.d.ts +21 -18
- package/lib/later-events.js +80 -75
- package/lib/link-events.d.ts +15 -0
- package/lib/link-events.js +33 -0
- package/lib/recall-events.d.ts +24 -23
- package/lib/recall-events.js +97 -94
- package/lib/recap.d.ts +23 -0
- package/lib/recap.js +79 -0
- package/lib/seal-events.d.ts +25 -0
- package/lib/seal-events.js +82 -0
- package/lib/self-experience.d.ts +175 -118
- package/lib/self-experience.js +560 -393
- package/lib/self-memory.js +68 -67
- package/lib/unfinished-events.d.ts +26 -25
- package/lib/unfinished-events.js +105 -81
- package/lib/waking.js +92 -91
- package/package.json +105 -85
|
@@ -0,0 +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 }
|
package/lib/self-experience.d.ts
CHANGED
|
@@ -1,119 +1,176 @@
|
|
|
1
|
-
export type Weight = 'important' | 'normal' | 'minor'
|
|
2
|
-
|
|
3
|
-
export interface Quote {
|
|
4
|
-
text: string
|
|
5
|
-
source?: string
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface LaterEntry {
|
|
9
|
-
at: string
|
|
10
|
-
day?: string
|
|
11
|
-
thought: string
|
|
12
|
-
reason?: string
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
export function
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
1
|
+
export type Weight = 'important' | 'normal' | 'minor'
|
|
2
|
+
|
|
3
|
+
export interface Quote {
|
|
4
|
+
text: string
|
|
5
|
+
source?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface LaterEntry {
|
|
9
|
+
at: string
|
|
10
|
+
day?: string
|
|
11
|
+
thought: string
|
|
12
|
+
reason?: string
|
|
13
|
+
/** 她后来再想起时的感觉(0.12 起);当时的感觉在 then.feeling 里,永远不动 */
|
|
14
|
+
feeling?: string[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 出处 / 反向引用里的一条:指路用的一句话摘要 */
|
|
18
|
+
export interface ExperienceBrief {
|
|
19
|
+
id: string
|
|
20
|
+
day?: string
|
|
21
|
+
text?: string
|
|
22
|
+
/** 出处指向的经历找不到了(例如只恢复了部分文件) */
|
|
23
|
+
missing?: boolean
|
|
24
|
+
/** 那条经历她封存了:不给摘要 */
|
|
25
|
+
sealed?: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** 「翻回那天的自己」:2026-11-15(截到那天本地时间的最后一刻)、完整 ISO 时间或 Date */
|
|
29
|
+
export type AsOf = string | Date | null
|
|
30
|
+
|
|
31
|
+
export interface Experience {
|
|
32
|
+
id: string
|
|
33
|
+
at: string
|
|
34
|
+
day: string
|
|
35
|
+
author?: string
|
|
36
|
+
source?: string
|
|
37
|
+
kind: string
|
|
38
|
+
weight: Weight
|
|
39
|
+
then: {
|
|
40
|
+
text: string
|
|
41
|
+
scene?: string
|
|
42
|
+
feeling?: string[]
|
|
43
|
+
thought?: string
|
|
44
|
+
unfinished?: string
|
|
45
|
+
quote?: Quote | null
|
|
46
|
+
state?: Record<string, unknown> | null
|
|
47
|
+
/** 这条是从哪几件事想到的(经历 id,写入时校验过存在) */
|
|
48
|
+
refs?: string[]
|
|
49
|
+
}
|
|
50
|
+
later: LaterEntry[]
|
|
51
|
+
/** 由 then.refs 派生:出处的摘要 */
|
|
52
|
+
refsInfo?: ExperienceBrief[]
|
|
53
|
+
/** 反向引用:后来有哪些回想把这条写进了出处 */
|
|
54
|
+
citedBy?: ExperienceBrief[]
|
|
55
|
+
/** 用 asOf 读出来时,截到的那一刻(ISO) */
|
|
56
|
+
viewAsOf?: string
|
|
57
|
+
/** 她封存了这件事(0.13 起):不会被召回、不进醒来块,但还在 */
|
|
58
|
+
sealed?: { at: string; day: string; note: string }
|
|
59
|
+
/** 事后联想(0.14 起):她亲手连起来的另一件事,两头都能看到 */
|
|
60
|
+
links?: Array<ExperienceBrief & { reason: string; linkDay: string }>
|
|
61
|
+
/** 这一天落在她起过名字的哪段日子里(0.14 起) */
|
|
62
|
+
chapters?: Array<{ id: string; name: string }>
|
|
63
|
+
recalls?: number
|
|
64
|
+
lastRecalledAt?: string
|
|
65
|
+
matchedIn?: Array<'then' | 'quote' | 'later'>
|
|
66
|
+
/** 没弄完的事后来怎么样了:done=想通,shelved=先放下,reopen=又接着想 */
|
|
67
|
+
unfinishedState?: {
|
|
68
|
+
id: string
|
|
69
|
+
at: string
|
|
70
|
+
day: string
|
|
71
|
+
choice: 'done' | 'shelved' | 'reopen'
|
|
72
|
+
note: string
|
|
73
|
+
/** 完整的选择历史,按追加顺序 */
|
|
74
|
+
history: Array<'done' | 'shelved' | 'reopen'>
|
|
75
|
+
}
|
|
76
|
+
/** recall 返回:是哪几条后记让这条被想起 */
|
|
77
|
+
matchedLater?: LaterEntry[]
|
|
78
|
+
/** 0.10.0 之前的旧 id(只在唯一对应时出现);用它也能 revise / currentView */
|
|
79
|
+
formerId?: string
|
|
80
|
+
status?: string
|
|
81
|
+
legacy?: boolean
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export const LIMITS: Record<string, number>
|
|
85
|
+
|
|
86
|
+
export function loadAll(dir: string, opts?: { asOf?: AsOf }): Experience[]
|
|
87
|
+
|
|
88
|
+
export function addExperience(dir: string, opts: {
|
|
89
|
+
text: string
|
|
90
|
+
scene?: string
|
|
91
|
+
feeling?: string[]
|
|
92
|
+
thought?: string
|
|
93
|
+
unfinished?: string
|
|
94
|
+
quote?: Quote | null
|
|
95
|
+
weight?: Weight
|
|
96
|
+
kind?: string
|
|
97
|
+
state?: Record<string, unknown> | null
|
|
98
|
+
source?: string
|
|
99
|
+
/** 出处:数组,或逗号分隔的字符串;引用不存在的经历会抛错,整条不写 */
|
|
100
|
+
refs?: string[] | string
|
|
101
|
+
}): Experience
|
|
102
|
+
|
|
103
|
+
export function reviseExperience(dir: string, opts: { id: string; thought: string; reason?: string; feeling?: string[] | string }): {
|
|
104
|
+
ok: boolean
|
|
105
|
+
id?: string
|
|
106
|
+
at?: string
|
|
107
|
+
feeling?: string[]
|
|
108
|
+
error?: string
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function markRecalled(dir: string, id: string): boolean
|
|
112
|
+
|
|
113
|
+
export function currentView(dir: string, id: string, opts?: { asOf?: AsOf }): Experience | null
|
|
114
|
+
|
|
115
|
+
export function recall(dir: string, query: string, opts?: {
|
|
116
|
+
limit?: number
|
|
117
|
+
minOverlap?: number
|
|
118
|
+
mark?: boolean
|
|
119
|
+
minRatio?: number
|
|
120
|
+
mode?: 'strict' | 'loose'
|
|
121
|
+
/** 在那天的自己里找;不能和 mark 一起用 */
|
|
122
|
+
asOf?: AsOf
|
|
123
|
+
/** 默认不召回她封存了的经历;只在"她自己要翻"时打开 */
|
|
124
|
+
includeSealed?: boolean
|
|
125
|
+
}): Experience[]
|
|
126
|
+
|
|
127
|
+
/** 事后联想:两条都必须存在、必须写理由;同一对(不分方向)只连一次 */
|
|
128
|
+
export function linkExperiences(dir: string, opts: { from: string; to: string; reason: string }): {
|
|
129
|
+
ok: boolean
|
|
130
|
+
from?: string
|
|
131
|
+
to?: string
|
|
132
|
+
noop?: boolean
|
|
133
|
+
at?: string
|
|
134
|
+
error?: string
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 封存 / 解封:不删除任何东西;重复同一个动作是 noop */
|
|
138
|
+
export function setSealed(dir: string, opts: { id: string; sealed?: boolean; note?: string }): {
|
|
139
|
+
ok: boolean
|
|
140
|
+
id?: string
|
|
141
|
+
sealed?: boolean
|
|
142
|
+
at?: string
|
|
143
|
+
noop?: boolean
|
|
144
|
+
error?: string
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function renderTwoPart(entry: Experience, opts?: { now?: Date }): string
|
|
148
|
+
|
|
149
|
+
export interface PendingLegacyEvents {
|
|
150
|
+
/** 几条旧经历共用的旧 id */
|
|
151
|
+
id: string
|
|
152
|
+
/** 可能的归属(当前 id) */
|
|
153
|
+
candidates: string[]
|
|
154
|
+
later: number
|
|
155
|
+
recalls: number
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function pendingLegacyEvents(dir: string): PendingLegacyEvents[]
|
|
159
|
+
|
|
160
|
+
export function setUnfinishedState(
|
|
161
|
+
dir: string,
|
|
162
|
+
opts: { id: string; choice: 'done' | 'shelved' | 'reopen'; note?: string }
|
|
163
|
+
): { ok: boolean; id?: string; at?: string; choice?: string; noop?: boolean; error?: string }
|
|
164
|
+
|
|
165
|
+
export interface RecallContextItem {
|
|
166
|
+
id: string
|
|
167
|
+
day: string
|
|
168
|
+
text: string
|
|
169
|
+
thought: string
|
|
170
|
+
weight: Weight
|
|
171
|
+
raw: Experience
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function recallForContext(dir: string, query: string, opts?: { max?: number }): RecallContextItem[]
|
|
175
|
+
|
|
119
176
|
export const internals: { WEIGHTS: Set<Weight>; FILE: string }
|