whale-memory-core 0.11.2 → 0.15.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 +15 -8
- package/index.js +17 -10
- package/lib/archive.d.ts +22 -0
- package/lib/archive.js +135 -0
- 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 -390
- package/lib/self-memory.js +68 -67
- package/lib/unfinished-events.d.ts +26 -25
- package/lib/unfinished-events.js +105 -102
- package/lib/unsaid.d.ts +28 -0
- package/lib/unsaid.js +105 -0
- package/lib/waking.js +92 -91
- package/package.json +113 -85
|
@@ -1,41 +1,63 @@
|
|
|
1
|
-
// 把「她的经历」桥到证据层:只抽有原文锚的东西,不推断
|
|
2
|
-
// 情绪快照 → mood fact;原文锚 → quote:<source> fact
|
|
3
|
-
import { query } from './evidence-policy.js'
|
|
4
|
-
|
|
5
|
-
const moodOf = (r) => {
|
|
6
|
-
const state = r.then && r.then.state
|
|
7
|
-
if (!state || typeof state !== 'object') return ''
|
|
8
|
-
return String(state.mood || '')
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function moodFacts(experiences, { subject = 'self' } = {}) {
|
|
12
|
-
const facts = []
|
|
13
|
-
for (const r of experiences || []) {
|
|
14
|
-
const mood = moodOf(r)
|
|
15
|
-
if (!mood) continue
|
|
16
|
-
const at = (r.then.state && r.then.state.at) || r.at
|
|
17
|
-
if (!at) continue
|
|
18
|
-
facts.push({ id: 'mood:' + r.id, subject, slot: 'mood', value: mood, at, source: 'then.state' })
|
|
19
|
-
}
|
|
20
|
-
return facts
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function quoteFacts(experiences, { subject = 'self' } = {}) {
|
|
24
|
-
const facts = []
|
|
25
|
-
for (const r of experiences || []) {
|
|
26
|
-
const q = r.then && r.then.quote
|
|
27
|
-
if (!q || !q.text) continue
|
|
28
|
-
const slot = 'quote:' + (q.source || 'unknown')
|
|
29
|
-
facts.push({ id: 'quote:' + r.id, subject, slot, value: q.text, at: r.at, source: q.source || 'then.quote' })
|
|
30
|
-
}
|
|
31
|
-
return facts
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
// 把「她的经历」桥到证据层:只抽有原文锚的东西,不推断
|
|
2
|
+
// 情绪快照 → mood fact;原文锚 → quote:<source> fact;当时/后来的感觉 → feeling:<id> fact
|
|
3
|
+
import { query } from './evidence-policy.js'
|
|
4
|
+
|
|
5
|
+
const moodOf = (r) => {
|
|
6
|
+
const state = r.then && r.then.state
|
|
7
|
+
if (!state || typeof state !== 'object') return ''
|
|
8
|
+
return String(state.mood || '')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function moodFacts(experiences, { subject = 'self' } = {}) {
|
|
12
|
+
const facts = []
|
|
13
|
+
for (const r of experiences || []) {
|
|
14
|
+
const mood = moodOf(r)
|
|
15
|
+
if (!mood) continue
|
|
16
|
+
const at = (r.then.state && r.then.state.at) || r.at
|
|
17
|
+
if (!at) continue
|
|
18
|
+
facts.push({ id: 'mood:' + r.id, subject, slot: 'mood', value: mood, at, source: 'then.state' })
|
|
19
|
+
}
|
|
20
|
+
return facts
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function quoteFacts(experiences, { subject = 'self' } = {}) {
|
|
24
|
+
const facts = []
|
|
25
|
+
for (const r of experiences || []) {
|
|
26
|
+
const q = r.then && r.then.quote
|
|
27
|
+
if (!q || !q.text) continue
|
|
28
|
+
const slot = 'quote:' + (q.source || 'unknown')
|
|
29
|
+
facts.push({ id: 'quote:' + r.id, subject, slot, value: q.text, at: r.at, source: q.source || 'then.quote' })
|
|
30
|
+
}
|
|
31
|
+
return facts
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 她对这一件事的感觉:当时写下的是第一条;后来某条后记写了感觉,就取代前一条(取代链可查)
|
|
35
|
+
// slot 为 feeling:<经历 id>。没写感觉的后记不算数——不替她推断"应该已经不难过了"
|
|
36
|
+
export function feelingFacts(experiences, { subject = 'self' } = {}) {
|
|
37
|
+
const facts = []
|
|
38
|
+
for (const r of experiences || []) {
|
|
39
|
+
const slot = 'feeling:' + r.id
|
|
40
|
+
let prev = ''
|
|
41
|
+
const thenFeeling = (r.then && Array.isArray(r.then.feeling)) ? r.then.feeling : []
|
|
42
|
+
if (thenFeeling.length && r.at) {
|
|
43
|
+
prev = 'feeling:' + r.id + ':then'
|
|
44
|
+
facts.push({ id: prev, subject, slot, value: thenFeeling.join('、'), at: r.at, source: 'then.feeling' })
|
|
45
|
+
}
|
|
46
|
+
for (const [i, l] of (r.later || []).entries()) {
|
|
47
|
+
if (!Array.isArray(l.feeling) || !l.feeling.length || !l.at) continue
|
|
48
|
+
const id = 'feeling:' + r.id + ':later:' + i
|
|
49
|
+
facts.push({ id, subject, slot, value: l.feeling.join('、'), at: l.at, source: 'later.feeling', ...(prev ? { supersedes: prev } : {}) })
|
|
50
|
+
prev = id
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return facts
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function factsFromExperiences(experiences, opts = {}) {
|
|
57
|
+
return [...moodFacts(experiences, opts), ...quoteFacts(experiences, opts), ...feelingFacts(experiences, opts)]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 问她某个时点是什么心情/读到过什么:没有就返回 unknown,不猜
|
|
61
|
+
export function ask(experiences, { slot, at, subject = 'self' } = {}) {
|
|
62
|
+
return query(factsFromExperiences(experiences, { subject }), { subject, slot, at })
|
|
41
63
|
}
|
package/lib/later-events.d.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
export interface LaterEvent {
|
|
2
|
-
id: string
|
|
3
|
-
at: string
|
|
4
|
-
day: string
|
|
5
|
-
thought: string
|
|
6
|
-
reason: string
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export function
|
|
12
|
-
|
|
13
|
-
export function
|
|
14
|
-
|
|
15
|
-
export function
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
export interface LaterEvent {
|
|
2
|
+
id: string
|
|
3
|
+
at: string
|
|
4
|
+
day: string
|
|
5
|
+
thought: string
|
|
6
|
+
reason: string
|
|
7
|
+
/** 她后来再想起时的感觉(0.12 起) */
|
|
8
|
+
feeling?: string[]
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function eventsFile(memDir: string): string
|
|
12
|
+
|
|
13
|
+
export function append(memDir: string, event: { id: string; thought: string; reason?: string; feeling?: string[] }, opts?: { timeoutMs?: number }): LaterEvent
|
|
14
|
+
|
|
15
|
+
export function readAll(memDir: string): { rows: LaterEvent[]; bad: number }
|
|
16
|
+
|
|
17
|
+
/** until:只要这一刻(毫秒时间戳)之前(含)的后记 */
|
|
18
|
+
export function groupByExp(memDir: string, opts?: { until?: number | null }): Map<string, LaterEvent[]>
|
|
19
|
+
|
|
20
|
+
export function list(memDir: string, opts?: { id?: string; limit?: number }): LaterEvent[]
|
|
21
|
+
|
|
19
22
|
export const internals: { FILE: string }
|
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
|