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.
- package/index.d.ts +13 -8
- 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 -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/waking.js +92 -91
- package/package.json +105 -85
package/lib/self-memory.js
CHANGED
|
@@ -1,68 +1,69 @@
|
|
|
1
|
-
// 她自己的记忆:统一读取她自己的经历、日记、自述;按相关度取回
|
|
2
|
-
// 匹配口径和 self-experience.recall 是同一套(query-match.js):虚词不算、长查询要过比例门槛
|
|
3
|
-
import fs from 'node:fs'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import { loadAll } from './self-experience.js'
|
|
6
|
-
import { queryTerms, requiredOverlap, matchedTerms } from './query-match.js'
|
|
7
|
-
|
|
8
|
-
function readJsonl(file) {
|
|
9
|
-
try {
|
|
10
|
-
return fs.readFileSync(file, 'utf8').trim().split('\n').filter(Boolean).map((l) => { try { return JSON.parse(l) } catch { return null } }).filter(Boolean)
|
|
11
|
-
} catch { return [] }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// 她自己的经历(自己的时间写的):走 loadAll,id 和后记都与 self-experience 一致
|
|
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
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
seen.
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
1
|
+
// 她自己的记忆:统一读取她自己的经历、日记、自述;按相关度取回
|
|
2
|
+
// 匹配口径和 self-experience.recall 是同一套(query-match.js):虚词不算、长查询要过比例门槛
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { loadAll } from './self-experience.js'
|
|
6
|
+
import { queryTerms, requiredOverlap, matchedTerms } from './query-match.js'
|
|
7
|
+
|
|
8
|
+
function readJsonl(file) {
|
|
9
|
+
try {
|
|
10
|
+
return fs.readFileSync(file, 'utf8').trim().split('\n').filter(Boolean).map((l) => { try { return JSON.parse(l) } catch { return null } }).filter(Boolean)
|
|
11
|
+
} catch { return [] }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// 她自己的经历(自己的时间写的):走 loadAll,id 和后记都与 self-experience 一致
|
|
15
|
+
// 她封存了的不进这里:被问到"你自己"时不该把她不想再提的事翻出来
|
|
16
|
+
export function loadExperiences(dir) {
|
|
17
|
+
return loadAll(dir).filter((r) => !r.sealed).map((r) => ({
|
|
18
|
+
id: r.id,
|
|
19
|
+
at: r.at || r.day || '',
|
|
20
|
+
kind: r.kind || '其它',
|
|
21
|
+
text: String(r.then.text || '').slice(0, 400),
|
|
22
|
+
thought: String(r.then.thought || '').slice(0, 200),
|
|
23
|
+
later: (r.later || []).map((l) => String(l.thought || '')).join(' '),
|
|
24
|
+
origin: 'own-time',
|
|
25
|
+
author: 'self'
|
|
26
|
+
}))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 她的日记(可能提到别人,默认只在主人私聊用)
|
|
30
|
+
export function loadDiaries(dir) {
|
|
31
|
+
return readJsonl(path.join(dir, 'autobiography.jsonl')).map((r) => ({
|
|
32
|
+
id: 'diary_' + (r.day || '') + '_' + String(r.time || '').replace(/:/g, ''),
|
|
33
|
+
at: r.day || '',
|
|
34
|
+
kind: '日记',
|
|
35
|
+
text: String(r.event || r.text || '').slice(0, 400),
|
|
36
|
+
thought: '',
|
|
37
|
+
origin: 'diary',
|
|
38
|
+
author: 'self'
|
|
39
|
+
})).filter((r) => r.text)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function recall(dir, query, { includeDiary = false, limit = 4, minOverlap = 1, minRatio = 0.6, mode = 'strict' } = {}) {
|
|
43
|
+
const pool = loadExperiences(dir).concat(includeDiary ? loadDiaries(dir) : [])
|
|
44
|
+
const q = String(query || '')
|
|
45
|
+
const parsed = queryTerms(q)
|
|
46
|
+
const required = requiredOverlap(parsed, { mode, minOverlap, minRatio })
|
|
47
|
+
const scored = pool.map((r) => ({ r, score: q ? matchedTerms(parsed.terms, [r.text, r.thought, r.later || ''].join(' ')).size : 0 }))
|
|
48
|
+
.filter((x) => (q ? x.score >= required : true))
|
|
49
|
+
.sort((a, b) => (b.score - a.score) || (String(b.r.at).localeCompare(String(a.r.at))))
|
|
50
|
+
const out = [], seen = new Set()
|
|
51
|
+
for (const x of scored) {
|
|
52
|
+
if (seen.has(x.r.text)) continue
|
|
53
|
+
seen.add(x.r.text)
|
|
54
|
+
out.push(x.r)
|
|
55
|
+
if (out.length >= limit) break
|
|
56
|
+
}
|
|
57
|
+
return out
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function renderBlock(items, { title = '【我的记忆(我自己的,不是谁告诉我的)】', maxChars = 320 } = {}) {
|
|
61
|
+
if (!items || !items.length) return ''
|
|
62
|
+
const lines = [title]
|
|
63
|
+
for (const r of items) {
|
|
64
|
+
const when = String(r.at || '').slice(5, 10)
|
|
65
|
+
lines.push('- ' + (when ? when + ' ' : '') + String(r.text).slice(0, 70) + (r.thought ? '(当时想:' + String(r.thought).slice(0, 40) + ')' : ''))
|
|
66
|
+
}
|
|
67
|
+
const out = lines.join('\n')
|
|
68
|
+
return out.length <= maxChars ? out : out.slice(0, maxChars - 1) + '…'
|
|
68
69
|
}
|
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
export type UnfinishedChoice = 'done' | 'shelved' | 'reopen'
|
|
2
|
-
|
|
3
|
-
export const CHOICES: Set<UnfinishedChoice>
|
|
4
|
-
|
|
5
|
-
export interface UnfinishedEvent {
|
|
6
|
-
id: string
|
|
7
|
-
at: string
|
|
8
|
-
day: string
|
|
9
|
-
choice: UnfinishedChoice
|
|
10
|
-
note: string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface UnfinishedLatest {
|
|
14
|
-
id: string
|
|
15
|
-
at: string
|
|
16
|
-
day: string
|
|
17
|
-
choice: UnfinishedChoice | ''
|
|
18
|
-
note: string
|
|
19
|
-
history: UnfinishedChoice[]
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function eventsFile(memDir: string): string
|
|
23
|
-
export function append(memDir: string, opts: { id: string; choice: UnfinishedChoice; note?: string }, lockOpts?: Record<string, unknown>): UnfinishedEvent
|
|
24
|
-
export function readAll(memDir: string): { rows: UnfinishedEvent[]; bad: number }
|
|
25
|
-
|
|
1
|
+
export type UnfinishedChoice = 'done' | 'shelved' | 'reopen'
|
|
2
|
+
|
|
3
|
+
export const CHOICES: Set<UnfinishedChoice>
|
|
4
|
+
|
|
5
|
+
export interface UnfinishedEvent {
|
|
6
|
+
id: string
|
|
7
|
+
at: string
|
|
8
|
+
day: string
|
|
9
|
+
choice: UnfinishedChoice
|
|
10
|
+
note: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface UnfinishedLatest {
|
|
14
|
+
id: string
|
|
15
|
+
at: string
|
|
16
|
+
day: string
|
|
17
|
+
choice: UnfinishedChoice | ''
|
|
18
|
+
note: string
|
|
19
|
+
history: UnfinishedChoice[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function eventsFile(memDir: string): string
|
|
23
|
+
export function append(memDir: string, opts: { id: string; choice: UnfinishedChoice; note?: string }, lockOpts?: Record<string, unknown>): UnfinishedEvent
|
|
24
|
+
export function readAll(memDir: string): { rows: UnfinishedEvent[]; bad: number }
|
|
25
|
+
/** until:按这一刻(毫秒时间戳)之前(含)的选择重放 */
|
|
26
|
+
export function latestByExp(memDir: string, opts?: { until?: number | null }): Map<string, UnfinishedLatest>
|
|
26
27
|
export function countByExp(memDir: string): Map<string, number>
|
package/lib/unfinished-events.js
CHANGED
|
@@ -1,103 +1,106 @@
|
|
|
1
|
-
// 「这件没弄完的事」后来怎么样了:想通 / 先放下 / 再接着想
|
|
2
|
-
// 一行一个 { id, at, day, choice, note? },只追加;不改 then.unfinished,也不冒充后记
|
|
3
|
-
import fs from 'node:fs'
|
|
4
|
-
import path from 'node:path'
|
|
5
|
-
import { withFileLock } from './file-lock.js'
|
|
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
|
-
const
|
|
34
|
-
|
|
35
|
-
fs.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (!
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
//
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (!
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
if (choice ===
|
|
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
|
-
cur
|
|
91
|
-
cur.
|
|
92
|
-
cur.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
1
|
+
// 「这件没弄完的事」后来怎么样了:想通 / 先放下 / 再接着想
|
|
2
|
+
// 一行一个 { id, at, day, choice, note? },只追加;不改 then.unfinished,也不冒充后记
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { withFileLock } from './file-lock.js'
|
|
6
|
+
import { before } from './as-of.js'
|
|
7
|
+
|
|
8
|
+
const FILE = 'unfinished-events.jsonl'
|
|
9
|
+
export const CHOICES = new Set(['done', 'shelved', 'reopen'])
|
|
10
|
+
|
|
11
|
+
export function eventsFile(memDir) {
|
|
12
|
+
return path.join(memDir, FILE)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function makeRow({ id, choice, note }) {
|
|
16
|
+
const now = new Date()
|
|
17
|
+
return {
|
|
18
|
+
id: String(id),
|
|
19
|
+
at: now.toISOString(),
|
|
20
|
+
day: now.toLocaleDateString('sv-SE'),
|
|
21
|
+
choice,
|
|
22
|
+
note: String(note || '').slice(0, 200)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 调用前必须已经持有锁;append 和 decide 共用这一层,避免同一次选择被锁外读到两次
|
|
27
|
+
function appendUnlocked(memDir, row) {
|
|
28
|
+
const file = eventsFile(memDir)
|
|
29
|
+
let prefix = ''
|
|
30
|
+
try {
|
|
31
|
+
const st = fs.statSync(file)
|
|
32
|
+
if (st.size) {
|
|
33
|
+
const fd = fs.openSync(file, 'r')
|
|
34
|
+
const buf = Buffer.alloc(1)
|
|
35
|
+
fs.readSync(fd, buf, 0, 1, st.size - 1)
|
|
36
|
+
fs.closeSync(fd)
|
|
37
|
+
if (buf[0] !== 10) prefix = '\n'
|
|
38
|
+
}
|
|
39
|
+
} catch { }
|
|
40
|
+
fs.appendFileSync(file, prefix + JSON.stringify(row) + '\n', 'utf8')
|
|
41
|
+
return row
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function append(memDir, { id, choice, note = '' } = {}, opts = {}) {
|
|
45
|
+
if (!String(id || '').trim()) throw new Error('id 不能为空')
|
|
46
|
+
if (!CHOICES.has(choice)) throw new Error('choice 只能是 done / shelved / reopen')
|
|
47
|
+
const row = makeRow({ id, choice, note })
|
|
48
|
+
fs.mkdirSync(memDir, { recursive: true })
|
|
49
|
+
return withFileLock(eventsFile(memDir) + '.lock', () => appendUnlocked(memDir, row), opts)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 在同一个锁里「重读最近状态 → 判断 → 追加」:
|
|
53
|
+
// 两个进程同时提交同一选择,第二个会看到第一个的结果并返回 noop,不会写出第二条
|
|
54
|
+
export function decide(memDir, { id, choice, note = '' } = {}, opts = {}) {
|
|
55
|
+
if (!String(id || '').trim()) throw new Error('id 不能为空')
|
|
56
|
+
if (!CHOICES.has(choice)) throw new Error('choice 只能是 done / shelved / reopen')
|
|
57
|
+
fs.mkdirSync(memDir, { recursive: true })
|
|
58
|
+
return withFileLock(eventsFile(memDir) + '.lock', () => {
|
|
59
|
+
const rows = readAll(memDir).rows.filter((r) => r.id === String(id))
|
|
60
|
+
const last = rows[rows.length - 1] || null
|
|
61
|
+
if (last && last.choice === choice) return { row: last, noop: true }
|
|
62
|
+
if (choice === 'reopen' && !last) return { row: null, noop: true }
|
|
63
|
+
return { row: appendUnlocked(memDir, makeRow({ id, choice, note })), noop: false }
|
|
64
|
+
}, opts)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function readAll(memDir) {
|
|
68
|
+
let raw = ''
|
|
69
|
+
try { raw = fs.readFileSync(eventsFile(memDir), 'utf8') } catch { return { rows: [], bad: 0 } }
|
|
70
|
+
const rows = []
|
|
71
|
+
let bad = 0
|
|
72
|
+
for (const line of raw.split('\n')) {
|
|
73
|
+
if (!line.trim()) continue
|
|
74
|
+
try {
|
|
75
|
+
const r = JSON.parse(line)
|
|
76
|
+
if (r && r.id && CHOICES.has(r.choice)) rows.push(r)
|
|
77
|
+
else bad += 1
|
|
78
|
+
} catch { bad += 1 }
|
|
79
|
+
}
|
|
80
|
+
return { rows, bad }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 每个经历 id 的最新状态,附带完整的选择历史(渲染"中间放下过"要用)
|
|
84
|
+
// until:按这一刻之前(含)的选择重放,「翻回那天的自己」用
|
|
85
|
+
export function latestByExp(memDir, { until = null } = {}) {
|
|
86
|
+
const { rows } = readAll(memDir)
|
|
87
|
+
const map = new Map()
|
|
88
|
+
for (const r of rows) {
|
|
89
|
+
if (!before(r, until)) continue
|
|
90
|
+
const cur = map.get(r.id) || { id: r.id, at: '', day: '', choice: '', note: '', history: [] }
|
|
91
|
+
cur.at = r.at
|
|
92
|
+
cur.day = r.day
|
|
93
|
+
cur.choice = r.choice
|
|
94
|
+
cur.note = r.note || ''
|
|
95
|
+
cur.history.push(r.choice)
|
|
96
|
+
map.set(r.id, cur)
|
|
97
|
+
}
|
|
98
|
+
return map
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function countByExp(memDir) {
|
|
102
|
+
const { rows } = readAll(memDir)
|
|
103
|
+
const map = new Map()
|
|
104
|
+
for (const r of rows) map.set(r.id, (map.get(r.id) || 0) + 1)
|
|
105
|
+
return map
|
|
103
106
|
}
|
package/lib/waking.js
CHANGED
|
@@ -1,92 +1,93 @@
|
|
|
1
|
-
// 醒来块:每天第一次被唤醒时,给她一条自己的时间线
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
import path from 'node:path'
|
|
4
|
-
import * as exp from './self-experience.js'
|
|
5
|
-
|
|
6
|
-
function readJsonl(file) {
|
|
7
|
-
try {
|
|
8
|
-
return fs.readFileSync(file, 'utf8').trim().split('\n').filter(Boolean).map((l) => { try { return JSON.parse(l) } catch { return null } }).filter(Boolean)
|
|
9
|
-
} catch { return [] }
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function dayStr(d = new Date()) { return d.toLocaleDateString('sv-SE') }
|
|
13
|
-
|
|
14
|
-
// 只认得出一对一的会话键(private_* / private:* / local_* / local:*);认不出的一律当成可能有别人在,宁可不给
|
|
15
|
-
const PRIVATE_KEY = /^(private|local)[:_]/i
|
|
16
|
-
function mayShow(key, allowGroup) {
|
|
17
|
-
return Boolean(key) && (allowGroup || PRIVATE_KEY.test(key))
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// 给宿主用的同一个判断:注入记忆前先问它,别自己写"不是群就行"
|
|
21
|
-
export function isPrivateKey(chatKey) {
|
|
22
|
-
return mayShow(String(chatKey || '').trim(), false)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// 只读:算出「今天该给她看什么」,不写任何文件
|
|
26
|
-
// 调用方确认真的把这段注入了,再调 consumeWaking 记「已展示」
|
|
27
|
-
// 安全默认:没有 chatKey、或不是认得出的一对一会话(未显式 allowGroup)→ 返回空
|
|
28
|
-
// 醒来块里可能有昨天私下的话,默认不该流到群里
|
|
29
|
-
export function buildWakingBlock(dataDir, chatKey, { now = new Date(), allowGroup = false } = {}) {
|
|
30
|
-
const key = String(chatKey || '').trim()
|
|
31
|
-
if (!mayShow(key, allowGroup)) return ''
|
|
32
|
-
|
|
33
|
-
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
34
|
-
const today = dayStr(now)
|
|
35
|
-
const flag = path.join(dir, 'waking-flags.json')
|
|
36
|
-
let flags = {}
|
|
37
|
-
try { flags = JSON.parse(fs.readFileSync(flag, 'utf8')) } catch {}
|
|
38
|
-
const dedupeKey = key + '|' + today
|
|
39
|
-
if (flags[dedupeKey]) return ''
|
|
40
|
-
|
|
41
|
-
// 用合并后的视图:明确想通/暂放的不再进入未完列表;旧库没有状态事件,行为不变
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
lines.push('
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
flags
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
1
|
+
// 醒来块:每天第一次被唤醒时,给她一条自己的时间线
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import * as exp from './self-experience.js'
|
|
5
|
+
|
|
6
|
+
function readJsonl(file) {
|
|
7
|
+
try {
|
|
8
|
+
return fs.readFileSync(file, 'utf8').trim().split('\n').filter(Boolean).map((l) => { try { return JSON.parse(l) } catch { return null } }).filter(Boolean)
|
|
9
|
+
} catch { return [] }
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function dayStr(d = new Date()) { return d.toLocaleDateString('sv-SE') }
|
|
13
|
+
|
|
14
|
+
// 只认得出一对一的会话键(private_* / private:* / local_* / local:*);认不出的一律当成可能有别人在,宁可不给
|
|
15
|
+
const PRIVATE_KEY = /^(private|local)[:_]/i
|
|
16
|
+
function mayShow(key, allowGroup) {
|
|
17
|
+
return Boolean(key) && (allowGroup || PRIVATE_KEY.test(key))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 给宿主用的同一个判断:注入记忆前先问它,别自己写"不是群就行"
|
|
21
|
+
export function isPrivateKey(chatKey) {
|
|
22
|
+
return mayShow(String(chatKey || '').trim(), false)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 只读:算出「今天该给她看什么」,不写任何文件
|
|
26
|
+
// 调用方确认真的把这段注入了,再调 consumeWaking 记「已展示」
|
|
27
|
+
// 安全默认:没有 chatKey、或不是认得出的一对一会话(未显式 allowGroup)→ 返回空
|
|
28
|
+
// 醒来块里可能有昨天私下的话,默认不该流到群里
|
|
29
|
+
export function buildWakingBlock(dataDir, chatKey, { now = new Date(), allowGroup = false } = {}) {
|
|
30
|
+
const key = String(chatKey || '').trim()
|
|
31
|
+
if (!mayShow(key, allowGroup)) return ''
|
|
32
|
+
|
|
33
|
+
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
34
|
+
const today = dayStr(now)
|
|
35
|
+
const flag = path.join(dir, 'waking-flags.json')
|
|
36
|
+
let flags = {}
|
|
37
|
+
try { flags = JSON.parse(fs.readFileSync(flag, 'utf8')) } catch {}
|
|
38
|
+
const dedupeKey = key + '|' + today
|
|
39
|
+
if (flags[dedupeKey]) return ''
|
|
40
|
+
|
|
41
|
+
// 用合并后的视图:明确想通/暂放的不再进入未完列表;旧库没有状态事件,行为不变
|
|
42
|
+
// 她封存了的经历不出现在醒来块里,昨天的、没弄完的都一样
|
|
43
|
+
let loaded = []
|
|
44
|
+
try { loaded = exp.loadAll(dir) } catch { }
|
|
45
|
+
const rows = loaded.filter((r) => !r.sealed).map((r) => ({
|
|
46
|
+
day: r.day,
|
|
47
|
+
text: r.then.text,
|
|
48
|
+
unfinished: r.then.unfinished,
|
|
49
|
+
weight: r.weight,
|
|
50
|
+
later: r.later || [],
|
|
51
|
+
state: r.unfinishedState || null
|
|
52
|
+
})).filter((r) => r.text)
|
|
53
|
+
const y = new Date(now.getTime() - 86400000).toLocaleDateString('sv-SE')
|
|
54
|
+
const yRows = rows.filter((r) => r.day === y)
|
|
55
|
+
const notes = readJsonl(path.join(dir, 'notes-to-tomorrow.jsonl'))
|
|
56
|
+
const lastNote = [...notes].reverse().find((n) => n.day === y)
|
|
57
|
+
|
|
58
|
+
const lines = ['【醒来】今天是 ' + today + '。']
|
|
59
|
+
if (yRows.length) {
|
|
60
|
+
lines.push('昨天你自己的时候:')
|
|
61
|
+
for (const r of yRows.slice(-3)) lines.push('- ' + String(r.text).slice(0, 60))
|
|
62
|
+
} else {
|
|
63
|
+
lines.push('昨天你没有留下自己的记录。')
|
|
64
|
+
}
|
|
65
|
+
if (lastNote && lastNote.text) lines.push('昨天睡前你给今天留了一句:' + String(lastNote.text).slice(0, 120))
|
|
66
|
+
const unfinished = rows.filter((r) => r.unfinished && !(r.state && (r.state.choice === 'done' || r.state.choice === 'shelved'))).slice(-2)
|
|
67
|
+
for (const r of unfinished) lines.push('- 你还没弄完:' + String(r.unfinished).slice(0, 50))
|
|
68
|
+
lines.push('(这些是记录,不是别人告诉你的。要不要接着想,你自己定。)')
|
|
69
|
+
return lines.join('\n')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 确认已展示:写入 flags,之后同一天同一会话不会再出
|
|
73
|
+
export function consumeWaking(dataDir, chatKey, { now = new Date(), allowGroup = false } = {}) {
|
|
74
|
+
const key = String(chatKey || '').trim()
|
|
75
|
+
if (!mayShow(key, allowGroup)) return false
|
|
76
|
+
|
|
77
|
+
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
78
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
79
|
+
const flag = path.join(dir, 'waking-flags.json')
|
|
80
|
+
let flags = {}
|
|
81
|
+
try { flags = JSON.parse(fs.readFileSync(flag, 'utf8')) } catch {}
|
|
82
|
+
flags[key + '|' + dayStr(now)] = new Date().toISOString()
|
|
83
|
+
fs.writeFileSync(flag, JSON.stringify(flags), 'utf8')
|
|
84
|
+
return true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function leaveNote(dataDir, text) {
|
|
88
|
+
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
89
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
90
|
+
const row = { at: new Date().toISOString(), day: dayStr(), text: String(text || '').slice(0, 300) }
|
|
91
|
+
fs.appendFileSync(path.join(dir, 'notes-to-tomorrow.jsonl'), JSON.stringify(row) + '\n', 'utf8')
|
|
92
|
+
return row
|
|
92
93
|
}
|