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
package/lib/later-events.js
CHANGED
|
@@ -1,76 +1,81 @@
|
|
|
1
|
-
// 改主意是事件:只追加,不改 then,也不重写经历文件
|
|
2
|
-
// 一行一个 { id, at, day, thought, reason? }
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
fs.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
let
|
|
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
|
-
return
|
|
74
|
-
}
|
|
75
|
-
|
|
1
|
+
// 改主意是事件:只追加,不改 then,也不重写经历文件
|
|
2
|
+
// 一行一个 { id, at, day, thought, reason?, feeling? }
|
|
3
|
+
// feeling 是她「后来再想起时」的感觉;当时的感觉在 then.feeling 里,永远不动
|
|
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 = 'later-events.jsonl'
|
|
10
|
+
|
|
11
|
+
export function eventsFile(memDir) {
|
|
12
|
+
return path.join(memDir, FILE)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function append(memDir, { id, thought, reason = '', feeling = [] } = {}, opts = {}) {
|
|
16
|
+
if (!String(id || '').trim()) throw new Error('id 不能为空')
|
|
17
|
+
if (!String(thought || '').trim()) throw new Error('thought 不能为空')
|
|
18
|
+
const now = new Date()
|
|
19
|
+
const row = {
|
|
20
|
+
id: String(id),
|
|
21
|
+
at: now.toISOString(),
|
|
22
|
+
day: now.toLocaleDateString('sv-SE'),
|
|
23
|
+
thought: String(thought),
|
|
24
|
+
reason: String(reason || '')
|
|
25
|
+
}
|
|
26
|
+
if (Array.isArray(feeling) && feeling.length) row.feeling = feeling.map(String)
|
|
27
|
+
|
|
28
|
+
fs.mkdirSync(memDir, { recursive: true })
|
|
29
|
+
return withFileLock(eventsFile(memDir) + '.lock', () => {
|
|
30
|
+
const file = eventsFile(memDir)
|
|
31
|
+
let prefix = ''
|
|
32
|
+
try {
|
|
33
|
+
const st = fs.statSync(file)
|
|
34
|
+
if (st.size) {
|
|
35
|
+
const fd = fs.openSync(file, 'r')
|
|
36
|
+
const buf = Buffer.alloc(1)
|
|
37
|
+
fs.readSync(fd, buf, 0, 1, st.size - 1)
|
|
38
|
+
fs.closeSync(fd)
|
|
39
|
+
if (buf[0] !== 10) prefix = '\n'
|
|
40
|
+
}
|
|
41
|
+
} catch { }
|
|
42
|
+
fs.appendFileSync(file, prefix + JSON.stringify(row) + '\n', 'utf8')
|
|
43
|
+
return row
|
|
44
|
+
}, opts)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function readAll(memDir) {
|
|
48
|
+
let raw = ''
|
|
49
|
+
try { raw = fs.readFileSync(eventsFile(memDir), 'utf8') } catch { return { rows: [], bad: 0 } }
|
|
50
|
+
const rows = []
|
|
51
|
+
let bad = 0
|
|
52
|
+
for (const line of raw.split('\n')) {
|
|
53
|
+
if (!line.trim()) continue
|
|
54
|
+
try {
|
|
55
|
+
const r = JSON.parse(line)
|
|
56
|
+
if (r && r.id && r.thought) rows.push(r)
|
|
57
|
+
else bad += 1
|
|
58
|
+
} catch { bad += 1 }
|
|
59
|
+
}
|
|
60
|
+
return { rows, bad }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// until:只要这一刻之前(含)写下的后记,「翻回那天的自己」用
|
|
64
|
+
export function groupByExp(memDir, { until = null } = {}) {
|
|
65
|
+
const { rows } = readAll(memDir)
|
|
66
|
+
const map = new Map()
|
|
67
|
+
for (const r of rows) {
|
|
68
|
+
if (!before(r, until)) continue
|
|
69
|
+
const list = map.get(r.id) || []
|
|
70
|
+
list.push(r)
|
|
71
|
+
map.set(r.id, list)
|
|
72
|
+
}
|
|
73
|
+
return map
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function list(memDir, { id = '', limit = 20 } = {}) {
|
|
77
|
+
const { rows } = readAll(memDir)
|
|
78
|
+
return rows.filter((r) => !id || r.id === id).slice(-limit).reverse()
|
|
79
|
+
}
|
|
80
|
+
|
|
76
81
|
export const internals = { FILE }
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface LinkEvent {
|
|
2
|
+
from: string
|
|
3
|
+
to: string
|
|
4
|
+
at: string
|
|
5
|
+
day: string
|
|
6
|
+
reason: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function eventsFile(memDir: string): string
|
|
10
|
+
export function readAll(memDir: string): { rows: LinkEvent[]; bad: number }
|
|
11
|
+
/** 同一对(不分方向)只连一次;重复是 noop。自己连自己会抛错 */
|
|
12
|
+
export function link(memDir: string, opts: { from: string; to: string; reason: string }, lockOpts?: Record<string, unknown>): { row: LinkEvent | null; noop: boolean }
|
|
13
|
+
/** 每条经历连着哪些(两头都有);until 用于翻回那天 */
|
|
14
|
+
export function linksByExp(memDir: string, opts?: { until?: number | null }): Map<string, Array<{ other: string; reason: string; at: string; day: string }>>
|
|
15
|
+
export const internals: { FILE: string }
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// 事后联想:她后来才发现两件事有关,亲手把它们连起来
|
|
2
|
+
// 一行一个 { from, to, at, day, reason },只追加。不按相似度自动连;不写理由不连
|
|
3
|
+
// 和出处(then.refs)的区别:出处是写下这条时就知道的依据;联想是写下之后才发现的关系
|
|
4
|
+
import { createLog, stamp } from './event-log.js'
|
|
5
|
+
import { before } from './as-of.js'
|
|
6
|
+
|
|
7
|
+
const log = createLog('link-events.jsonl', { valid: (r) => Boolean(r.from && r.to && r.reason) })
|
|
8
|
+
export const eventsFile = log.eventsFile
|
|
9
|
+
export const readAll = log.readAll
|
|
10
|
+
export const internals = { FILE: log.FILE }
|
|
11
|
+
|
|
12
|
+
const pair = (a, b) => [String(a), String(b)].sort().join('|')
|
|
13
|
+
|
|
14
|
+
// 同一对经历(不分方向)只连一次;重复提交是 noop
|
|
15
|
+
export function link(memDir, { from, to, reason }, opts = {}) {
|
|
16
|
+
if (!from || !to) throw new Error('需要两条经历的 id')
|
|
17
|
+
if (String(from) === String(to)) throw new Error('不能把一件事和它自己连起来。')
|
|
18
|
+
return log.decide(memDir, (rows) => rows.some((r) => pair(r.from, r.to) === pair(from, to))
|
|
19
|
+
? null
|
|
20
|
+
: { from: String(from), to: String(to), ...stamp(), reason: String(reason) }, opts)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 每条经历连着哪些:不分方向,两头都能看到;until 用于翻回那天
|
|
24
|
+
export function linksByExp(memDir, { until = null } = {}) {
|
|
25
|
+
const map = new Map()
|
|
26
|
+
const push = (id, other, r) => map.set(id, [...(map.get(id) || []), { other, reason: r.reason, at: r.at, day: r.day }])
|
|
27
|
+
for (const r of readAll(memDir).rows) {
|
|
28
|
+
if (!before(r, until)) continue
|
|
29
|
+
push(r.from, r.to, r)
|
|
30
|
+
push(r.to, r.from, r)
|
|
31
|
+
}
|
|
32
|
+
return map
|
|
33
|
+
}
|
package/lib/recall-events.d.ts
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
export interface RecallEvent {
|
|
2
|
-
id: string
|
|
3
|
-
at: string
|
|
4
|
-
query: string
|
|
5
|
-
chatKey: string
|
|
6
|
-
backfilled?: boolean
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function eventsFile(memDir: string): string
|
|
10
|
-
|
|
11
|
-
export function append(memDir: string, event: {
|
|
12
|
-
id: string
|
|
13
|
-
query?: string
|
|
14
|
-
chatKey?: string
|
|
15
|
-
backfilled?: boolean
|
|
16
|
-
}, opts?: { timeoutMs?: number }): RecallEvent
|
|
17
|
-
|
|
18
|
-
export function readAll(memDir: string): { rows: RecallEvent[]; bad: number }
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
export interface RecallEvent {
|
|
2
|
+
id: string
|
|
3
|
+
at: string
|
|
4
|
+
query: string
|
|
5
|
+
chatKey: string
|
|
6
|
+
backfilled?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function eventsFile(memDir: string): string
|
|
10
|
+
|
|
11
|
+
export function append(memDir: string, event: {
|
|
12
|
+
id: string
|
|
13
|
+
query?: string
|
|
14
|
+
chatKey?: string
|
|
15
|
+
backfilled?: boolean
|
|
16
|
+
}, opts?: { timeoutMs?: number }): RecallEvent
|
|
17
|
+
|
|
18
|
+
export function readAll(memDir: string): { rows: RecallEvent[]; bad: number }
|
|
19
|
+
|
|
20
|
+
/** until:只数这一刻(毫秒时间戳)之前(含)的想起 */
|
|
21
|
+
export function countByExp(memDir: string, opts?: { until?: number | null }): Map<string, { count: number; lastAt: string }>
|
|
22
|
+
|
|
23
|
+
export function list(memDir: string, opts?: { id?: string; limit?: number }): RecallEvent[]
|
|
24
|
+
|
|
24
25
|
export const internals: { FILE: string }
|
package/lib/recall-events.js
CHANGED
|
@@ -1,95 +1,98 @@
|
|
|
1
|
-
// 想起是事件:只追加,不改经历正文
|
|
2
|
-
// 一行一个 { id, at, query, chatKey?, backfilled? }
|
|
3
|
-
// 参数是记忆目录本身(<data>/plugin-data/biomimetic-memory),与 self-experience 一致
|
|
4
|
-
import fs from 'node:fs'
|
|
5
|
-
import path from 'node:path'
|
|
6
|
-
import { withFileLock } from './file-lock.js'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
const
|
|
39
|
-
|
|
40
|
-
fs.
|
|
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
|
-
if (
|
|
81
|
-
map.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
1
|
+
// 想起是事件:只追加,不改经历正文
|
|
2
|
+
// 一行一个 { id, at, query, chatKey?, backfilled? }
|
|
3
|
+
// 参数是记忆目录本身(<data>/plugin-data/biomimetic-memory),与 self-experience 一致
|
|
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 = 'recall-events.jsonl'
|
|
10
|
+
|
|
11
|
+
export function eventsFile(memDir) {
|
|
12
|
+
return path.join(memDir, FILE)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function ensureDir(memDir) {
|
|
16
|
+
fs.mkdirSync(memDir, { recursive: true })
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export function append(memDir, { id, query = '', chatKey = '', backfilled = false } = {}, opts = {}) {
|
|
22
|
+
if (!String(id || '').trim()) throw new Error('id 不能为空')
|
|
23
|
+
const row = {
|
|
24
|
+
id: String(id),
|
|
25
|
+
at: new Date().toISOString(),
|
|
26
|
+
query: String(query || '').slice(0, 200),
|
|
27
|
+
chatKey: String(chatKey || '').slice(0, 120)
|
|
28
|
+
}
|
|
29
|
+
if (backfilled) row.backfilled = true
|
|
30
|
+
|
|
31
|
+
ensureDir(memDir)
|
|
32
|
+
return withFileLock(eventsFile(memDir) + '.lock', () => {
|
|
33
|
+
const file = eventsFile(memDir)
|
|
34
|
+
let prefix = ''
|
|
35
|
+
try {
|
|
36
|
+
const st = fs.statSync(file)
|
|
37
|
+
if (st.size) {
|
|
38
|
+
const fd = fs.openSync(file, 'r')
|
|
39
|
+
const buf = Buffer.alloc(1)
|
|
40
|
+
fs.readSync(fd, buf, 0, 1, st.size - 1)
|
|
41
|
+
fs.closeSync(fd)
|
|
42
|
+
if (buf[0] !== 10) prefix = '\n'
|
|
43
|
+
}
|
|
44
|
+
} catch { }
|
|
45
|
+
fs.appendFileSync(file, prefix + JSON.stringify(row) + '\n', 'utf8')
|
|
46
|
+
return row
|
|
47
|
+
}, opts)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function readAll(memDir) {
|
|
51
|
+
let raw = ''
|
|
52
|
+
try { raw = fs.readFileSync(eventsFile(memDir), 'utf8') } catch { return { rows: [], bad: 0 } }
|
|
53
|
+
const rows = []
|
|
54
|
+
let bad = 0
|
|
55
|
+
for (const line of raw.split('\n')) {
|
|
56
|
+
if (!line.trim()) continue
|
|
57
|
+
try {
|
|
58
|
+
const r = JSON.parse(line)
|
|
59
|
+
if (r && r.id) rows.push(r)
|
|
60
|
+
else bad += 1
|
|
61
|
+
} catch { bad += 1 }
|
|
62
|
+
}
|
|
63
|
+
return { rows, bad }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 进程内缓存:同一进程里连续 recall 不再重复解析整个事件文件
|
|
67
|
+
const memo = new Map()
|
|
68
|
+
|
|
69
|
+
// until:只数这一刻之前(含)的想起;截断查询不走缓存
|
|
70
|
+
export function countByExp(memDir, { until = null } = {}) {
|
|
71
|
+
const file = eventsFile(memDir)
|
|
72
|
+
let stamp = ''
|
|
73
|
+
try { const st = fs.statSync(file); stamp = st.mtimeMs + ':' + st.size } catch { }
|
|
74
|
+
const hit = until === null ? memo.get(memDir) : null
|
|
75
|
+
if (hit && hit.stamp === stamp) return hit.map
|
|
76
|
+
|
|
77
|
+
const { rows } = readAll(memDir)
|
|
78
|
+
const map = new Map()
|
|
79
|
+
for (const r of rows) {
|
|
80
|
+
if (!before(r, until)) continue
|
|
81
|
+
const cur = map.get(r.id) || { count: 0, lastAt: '' }
|
|
82
|
+
cur.count += 1
|
|
83
|
+
if (String(r.at || '') > cur.lastAt) cur.lastAt = String(r.at || '')
|
|
84
|
+
map.set(r.id, cur)
|
|
85
|
+
}
|
|
86
|
+
if (until === null) memo.set(memDir, { stamp, map })
|
|
87
|
+
return map
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function list(memDir, { id = '', limit = 20 } = {}) {
|
|
91
|
+
const { rows } = readAll(memDir)
|
|
92
|
+
return rows
|
|
93
|
+
.filter((r) => !id || r.id === id)
|
|
94
|
+
.slice(-limit)
|
|
95
|
+
.reverse()
|
|
96
|
+
}
|
|
97
|
+
|
|
95
98
|
export const internals = { FILE }
|
package/lib/recap.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AsOf } from './self-experience.js'
|
|
2
|
+
|
|
3
|
+
export interface RecapItem {
|
|
4
|
+
id: string
|
|
5
|
+
day: string
|
|
6
|
+
text: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Recap {
|
|
10
|
+
from: string
|
|
11
|
+
to: string
|
|
12
|
+
days: number
|
|
13
|
+
written: Array<RecapItem & { kind: string; feeling: string[]; refs: string[] }>
|
|
14
|
+
revised: Array<RecapItem & { thought: string; feeling: string[]; at: string; laterDay: string }>
|
|
15
|
+
feelingShifts: Array<RecapItem & { then: string[]; now: string[]; laterDay: string }>
|
|
16
|
+
settled: Array<RecapItem & { unfinished: string; choice: 'done' | 'shelved'; stateDay: string }>
|
|
17
|
+
reopened: Array<RecapItem & { unfinished: string; choice: 'reopen'; stateDay: string }>
|
|
18
|
+
stillOpen: Array<RecapItem & { unfinished: string }>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 只整理事实,不调模型、不下结论;她封存了的不列 */
|
|
22
|
+
export function recap(dir: string, opts?: { days?: number; asOf?: AsOf; now?: Date }): Recap
|
|
23
|
+
export function renderRecap(recap: Recap): string
|
package/lib/recap.js
ADDED
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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 }
|