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
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
|
}
|
package/package.json
CHANGED
|
@@ -1,85 +1,113 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Self-layer autobiographical memory core: then / later, append-only. Vault, evidence and consolidation live in the QQ Agent plugin.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./index.js",
|
|
7
|
-
"types": "./index.d.ts",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./index.d.ts",
|
|
11
|
-
"default": "./index.js"
|
|
12
|
-
},
|
|
13
|
-
"./self-experience": {
|
|
14
|
-
"types": "./lib/self-experience.d.ts",
|
|
15
|
-
"default": "./lib/self-experience.js"
|
|
16
|
-
},
|
|
17
|
-
"./self-memory": {
|
|
18
|
-
"types": "./lib/self-memory.d.ts",
|
|
19
|
-
"default": "./lib/self-memory.js"
|
|
20
|
-
},
|
|
21
|
-
"./waking": {
|
|
22
|
-
"types": "./lib/waking.d.ts",
|
|
23
|
-
"default": "./lib/waking.js"
|
|
24
|
-
},
|
|
25
|
-
"./self-prompt": {
|
|
26
|
-
"types": "./lib/self-prompt.d.ts",
|
|
27
|
-
"default": "./lib/self-prompt.js"
|
|
28
|
-
},
|
|
29
|
-
"./self-settings": {
|
|
30
|
-
"types": "./lib/self-settings.d.ts",
|
|
31
|
-
"default": "./lib/self-settings.js"
|
|
32
|
-
},
|
|
33
|
-
"./recall-events": {
|
|
34
|
-
"types": "./lib/recall-events.d.ts",
|
|
35
|
-
"default": "./lib/recall-events.js"
|
|
36
|
-
},
|
|
37
|
-
"./later-events": {
|
|
38
|
-
"types": "./lib/later-events.d.ts",
|
|
39
|
-
"default": "./lib/later-events.js"
|
|
40
|
-
},
|
|
41
|
-
"./file-lock": {
|
|
42
|
-
"types": "./lib/file-lock.d.ts",
|
|
43
|
-
"default": "./lib/file-lock.js"
|
|
44
|
-
},
|
|
45
|
-
"./evidence-policy": {
|
|
46
|
-
"types": "./lib/evidence-policy.d.ts",
|
|
47
|
-
"default": "./lib/evidence-policy.js"
|
|
48
|
-
},
|
|
49
|
-
"./evidence-from-experience": {
|
|
50
|
-
"types": "./lib/evidence-from-experience.d.ts",
|
|
51
|
-
"default": "./lib/evidence-from-experience.js"
|
|
52
|
-
},
|
|
53
|
-
"./unfinished-events": {
|
|
54
|
-
"types": "./lib/unfinished-events.d.ts",
|
|
55
|
-
"default": "./lib/unfinished-events.js"
|
|
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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "whale-memory-core",
|
|
3
|
+
"version": "0.15.0",
|
|
4
|
+
"description": "Self-layer autobiographical memory core: then / later, append-only. Vault, evidence and consolidation live in the QQ Agent plugin.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"./self-experience": {
|
|
14
|
+
"types": "./lib/self-experience.d.ts",
|
|
15
|
+
"default": "./lib/self-experience.js"
|
|
16
|
+
},
|
|
17
|
+
"./self-memory": {
|
|
18
|
+
"types": "./lib/self-memory.d.ts",
|
|
19
|
+
"default": "./lib/self-memory.js"
|
|
20
|
+
},
|
|
21
|
+
"./waking": {
|
|
22
|
+
"types": "./lib/waking.d.ts",
|
|
23
|
+
"default": "./lib/waking.js"
|
|
24
|
+
},
|
|
25
|
+
"./self-prompt": {
|
|
26
|
+
"types": "./lib/self-prompt.d.ts",
|
|
27
|
+
"default": "./lib/self-prompt.js"
|
|
28
|
+
},
|
|
29
|
+
"./self-settings": {
|
|
30
|
+
"types": "./lib/self-settings.d.ts",
|
|
31
|
+
"default": "./lib/self-settings.js"
|
|
32
|
+
},
|
|
33
|
+
"./recall-events": {
|
|
34
|
+
"types": "./lib/recall-events.d.ts",
|
|
35
|
+
"default": "./lib/recall-events.js"
|
|
36
|
+
},
|
|
37
|
+
"./later-events": {
|
|
38
|
+
"types": "./lib/later-events.d.ts",
|
|
39
|
+
"default": "./lib/later-events.js"
|
|
40
|
+
},
|
|
41
|
+
"./file-lock": {
|
|
42
|
+
"types": "./lib/file-lock.d.ts",
|
|
43
|
+
"default": "./lib/file-lock.js"
|
|
44
|
+
},
|
|
45
|
+
"./evidence-policy": {
|
|
46
|
+
"types": "./lib/evidence-policy.d.ts",
|
|
47
|
+
"default": "./lib/evidence-policy.js"
|
|
48
|
+
},
|
|
49
|
+
"./evidence-from-experience": {
|
|
50
|
+
"types": "./lib/evidence-from-experience.d.ts",
|
|
51
|
+
"default": "./lib/evidence-from-experience.js"
|
|
52
|
+
},
|
|
53
|
+
"./unfinished-events": {
|
|
54
|
+
"types": "./lib/unfinished-events.d.ts",
|
|
55
|
+
"default": "./lib/unfinished-events.js"
|
|
56
|
+
},
|
|
57
|
+
"./seal-events": {
|
|
58
|
+
"types": "./lib/seal-events.d.ts",
|
|
59
|
+
"default": "./lib/seal-events.js"
|
|
60
|
+
},
|
|
61
|
+
"./recap": {
|
|
62
|
+
"types": "./lib/recap.d.ts",
|
|
63
|
+
"default": "./lib/recap.js"
|
|
64
|
+
},
|
|
65
|
+
"./link-events": {
|
|
66
|
+
"types": "./lib/link-events.d.ts",
|
|
67
|
+
"default": "./lib/link-events.js"
|
|
68
|
+
},
|
|
69
|
+
"./chapter-events": {
|
|
70
|
+
"types": "./lib/chapter-events.d.ts",
|
|
71
|
+
"default": "./lib/chapter-events.js"
|
|
72
|
+
},
|
|
73
|
+
"./book": {
|
|
74
|
+
"types": "./lib/book.d.ts",
|
|
75
|
+
"default": "./lib/book.js"
|
|
76
|
+
},
|
|
77
|
+
"./unsaid": {
|
|
78
|
+
"types": "./lib/unsaid.d.ts",
|
|
79
|
+
"default": "./lib/unsaid.js"
|
|
80
|
+
},
|
|
81
|
+
"./archive": {
|
|
82
|
+
"types": "./lib/archive.d.ts",
|
|
83
|
+
"default": "./lib/archive.js"
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
"files": [
|
|
87
|
+
"index.js",
|
|
88
|
+
"index.d.ts",
|
|
89
|
+
"lib",
|
|
90
|
+
"demo.mjs",
|
|
91
|
+
"README.md"
|
|
92
|
+
],
|
|
93
|
+
"engines": {
|
|
94
|
+
"node": ">=20"
|
|
95
|
+
},
|
|
96
|
+
"scripts": {
|
|
97
|
+
"demo": "node demo.mjs"
|
|
98
|
+
},
|
|
99
|
+
"keywords": [
|
|
100
|
+
"ai-companion",
|
|
101
|
+
"agent-memory",
|
|
102
|
+
"long-term-memory",
|
|
103
|
+
"autobiographical-memory",
|
|
104
|
+
"llm"
|
|
105
|
+
],
|
|
106
|
+
"license": "MIT",
|
|
107
|
+
"repository": {
|
|
108
|
+
"type": "git",
|
|
109
|
+
"url": "git+https://github.com/firefly1office/whale-memory.git",
|
|
110
|
+
"directory": "packages/core"
|
|
111
|
+
},
|
|
112
|
+
"sideEffects": false
|
|
113
|
+
}
|