whale-memory-core 0.11.2 → 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.
@@ -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 }
@@ -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
- export interface Experience {
16
- id: string
17
- at: string
18
- day: string
19
- author?: string
20
- source?: string
21
- kind: string
22
- weight: Weight
23
- then: {
24
- text: string
25
- scene?: string
26
- feeling?: string[]
27
- thought?: string
28
- unfinished?: string
29
- quote?: Quote | null
30
- state?: Record<string, unknown> | null
31
- }
32
- later: LaterEntry[]
33
- recalls?: number
34
- lastRecalledAt?: string
35
- matchedIn?: Array<'then' | 'quote' | 'later'>
36
- /** 没弄完的事后来怎么样了:done=想通,shelved=先放下,reopen=又接着想 */
37
- unfinishedState?: {
38
- id: string
39
- at: string
40
- day: string
41
- choice: 'done' | 'shelved' | 'reopen'
42
- note: string
43
- /** 完整的选择历史,按追加顺序 */
44
- history: Array<'done' | 'shelved' | 'reopen'>
45
- }
46
- /** recall 返回:是哪几条后记让这条被想起 */
47
- matchedLater?: LaterEntry[]
48
- /** 0.10.0 之前的旧 id(只在唯一对应时出现);用它也能 revise / currentView */
49
- formerId?: string
50
- status?: string
51
- legacy?: boolean
52
- }
53
-
54
- export const LIMITS: Record<string, number>
55
-
56
- export function loadAll(dir: string): Experience[]
57
-
58
- export function addExperience(dir: string, opts: {
59
- text: string
60
- scene?: string
61
- feeling?: string[]
62
- thought?: string
63
- unfinished?: string
64
- quote?: Quote | null
65
- weight?: Weight
66
- kind?: string
67
- state?: Record<string, unknown> | null
68
- source?: string
69
- }): Experience
70
-
71
- export function reviseExperience(dir: string, opts: { id: string; thought: string; reason?: string }): {
72
- ok: boolean
73
- id?: string
74
- at?: string
75
- error?: string
76
- }
77
-
78
- export function markRecalled(dir: string, id: string): boolean
79
-
80
- export function currentView(dir: string, id: string): Experience | null
81
-
82
- export function recall(dir: string, query: string, opts?: {
83
- limit?: number
84
- minOverlap?: number
85
- mark?: boolean
86
- minRatio?: number
87
- mode?: 'strict' | 'loose'
88
- }): Experience[]
89
-
90
- export function renderTwoPart(entry: Experience, opts?: { now?: Date }): string
91
-
92
- export interface PendingLegacyEvents {
93
- /** 几条旧经历共用的旧 id */
94
- id: string
95
- /** 可能的归属(当前 id) */
96
- candidates: string[]
97
- later: number
98
- recalls: number
99
- }
100
-
101
- export function pendingLegacyEvents(dir: string): PendingLegacyEvents[]
102
-
103
- export function setUnfinishedState(
104
- dir: string,
105
- opts: { id: string; choice: 'done' | 'shelved' | 'reopen'; note?: string }
106
- ): { ok: boolean; id?: string; at?: string; choice?: string; noop?: boolean; error?: string }
107
-
108
- export interface RecallContextItem {
109
- id: string
110
- day: string
111
- text: string
112
- thought: string
113
- weight: Weight
114
- raw: Experience
115
- }
116
-
117
- export function recallForContext(dir: string, query: string, opts?: { max?: number }): RecallContextItem[]
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 }