whale-memory-core 0.2.0 → 0.3.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/README.md +2 -2
- package/demo.mjs +2 -2
- package/lib/self-experience.js +40 -17
- package/lib/waking.js +15 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,12 +14,12 @@ import * as exp from 'whale-memory-core/self-experience'
|
|
|
14
14
|
- 召回(相关度 × 重要度)、想起计数、两段式渲染
|
|
15
15
|
- 醒来块、她自己的提示词 overlay、她自己的设置
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
不包含(完整引擎与具体宿主部署耦合,不随本仓库分发):
|
|
18
18
|
|
|
19
19
|
- vault 存储与索引、evidence-policy 取证、consolidator 合并、注入预算
|
|
20
20
|
- 跨会话可见性判断、宿主工具注册
|
|
21
21
|
|
|
22
|
-
如果你要的是"一个 agent 的自传记忆",这个包够用;如果你要的是"完整记忆系统",看 [`docs/porting.md`](../../docs/porting.md)
|
|
22
|
+
如果你要的是"一个 agent 的自传记忆",这个包够用;如果你要的是"完整记忆系统",看 [`docs/architecture.md`](../../docs/architecture.md) 与 [`docs/porting.md`](../../docs/porting.md)。
|
|
23
23
|
|
|
24
24
|
包名 `whale-memory-core`,入口:`whale-memory-core`(命名导出)以及五个子路径:`/self-experience`、`/self-memory`、`/waking`、`/self-prompt`、`/self-settings`。
|
|
25
25
|
|
package/demo.mjs
CHANGED
|
@@ -25,8 +25,8 @@ const e = exp.addExperience(dir, {
|
|
|
25
25
|
})
|
|
26
26
|
console.log('\n[写] id=', e.id, '| 重要度=', e.weight)
|
|
27
27
|
|
|
28
|
-
// 2)
|
|
29
|
-
const hits = exp.recall(dir, '那本书里那个人后来怎样了', { limit: 1 })
|
|
28
|
+
// 2) 第二天她想起(相关话题触发);mark:true 表示这次取回也记一次“想起”
|
|
29
|
+
const hits = exp.recall(dir, '那本书里那个人后来怎样了', { limit: 1, mark: true })
|
|
30
30
|
console.log('\n[想起]\n' + exp.renderTwoPart(hits[0]))
|
|
31
31
|
|
|
32
32
|
// 3) 她改主意:只往 later 追加,不动"当时"
|
package/lib/self-experience.js
CHANGED
|
@@ -25,7 +25,7 @@ function readAll(dir, { rewriting = false } = {}) {
|
|
|
25
25
|
|
|
26
26
|
function atomicWrite(dir, rows) {
|
|
27
27
|
const file = path.join(dir, FILE)
|
|
28
|
-
const tmp = file + '.tmp-' + Date.now()
|
|
28
|
+
const tmp = file + '.tmp-' + Date.now() + '-' + process.pid + '-' + Math.random().toString(36).slice(2, 8)
|
|
29
29
|
fs.writeFileSync(tmp, rows.map((r) => JSON.stringify(r)).join('\n') + '\n', 'utf8')
|
|
30
30
|
fs.renameSync(tmp, file)
|
|
31
31
|
}
|
|
@@ -34,6 +34,15 @@ function writeAll(dir, rows) {
|
|
|
34
34
|
atomicWrite(dir, rows)
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// 字段上限:写的时候超了就报错,不静默截断
|
|
38
|
+
export const LIMITS = { text: 2000, scene: 500, thought: 1000, unfinished: 300, feelings: 8, kind: 20 }
|
|
39
|
+
|
|
40
|
+
function tooLong(name, value, max) {
|
|
41
|
+
const n = String(value || '').length
|
|
42
|
+
if (n > max) throw new Error(`${name} 太长(${n}/${max} 字),删一些或拆成两条再写。`)
|
|
43
|
+
return String(value || '')
|
|
44
|
+
}
|
|
45
|
+
|
|
37
46
|
function normalize(r) {
|
|
38
47
|
if (!r) return null
|
|
39
48
|
if (r.then && typeof r.then === 'object') {
|
|
@@ -49,10 +58,10 @@ function normalize(r) {
|
|
|
49
58
|
kind: r.kind || '其它',
|
|
50
59
|
weight: WEIGHTS.has(r.weight) ? r.weight : 'normal',
|
|
51
60
|
then: {
|
|
52
|
-
text: String(r.text || '')
|
|
61
|
+
text: String(r.text || ''),
|
|
53
62
|
scene: '',
|
|
54
63
|
feeling: [],
|
|
55
|
-
thought: String(r.thought || '')
|
|
64
|
+
thought: String(r.thought || ''),
|
|
56
65
|
unfinished: '',
|
|
57
66
|
quote: null,
|
|
58
67
|
state: null
|
|
@@ -69,6 +78,15 @@ export function loadAll(dir) {
|
|
|
69
78
|
|
|
70
79
|
// 写下时的"当时":她写的 + 系统能自动带的状态(情绪/时间/场合)
|
|
71
80
|
export function addExperience(dir, { text, scene = '', feeling = [], thought = '', unfinished = '', quote = null, weight = 'normal', kind = '其它', state = null, source = 'own-time' }) {
|
|
81
|
+
if (!String(text || '').trim()) throw new Error('text 不能是空的。')
|
|
82
|
+
const safeText = tooLong('text', text, LIMITS.text)
|
|
83
|
+
const safeScene = tooLong('scene', scene, LIMITS.scene)
|
|
84
|
+
const safeThought = tooLong('thought', thought, LIMITS.thought)
|
|
85
|
+
const safeUnfinished = tooLong('unfinished', unfinished, LIMITS.unfinished)
|
|
86
|
+
const safeKind = tooLong('kind', kind || '其它', LIMITS.kind)
|
|
87
|
+
const feelings = (Array.isArray(feeling) ? feeling : [feeling]).map((f) => String(f || '').trim()).filter(Boolean)
|
|
88
|
+
if (feelings.length > LIMITS.feelings) throw new Error(`feeling 最多 ${LIMITS.feelings} 个,现在是 ${feelings.length} 个。`)
|
|
89
|
+
|
|
72
90
|
fs.mkdirSync(dir, { recursive: true })
|
|
73
91
|
const now = new Date()
|
|
74
92
|
const id = 'exp_' + now.getTime().toString(36) + Math.random().toString(36).slice(2, 6)
|
|
@@ -78,15 +96,15 @@ export function addExperience(dir, { text, scene = '', feeling = [], thought = '
|
|
|
78
96
|
day: now.toLocaleDateString('sv-SE'),
|
|
79
97
|
author: 'self',
|
|
80
98
|
source,
|
|
81
|
-
kind:
|
|
99
|
+
kind: safeKind,
|
|
82
100
|
weight: WEIGHTS.has(weight) ? weight : 'normal',
|
|
83
101
|
then: {
|
|
84
|
-
text:
|
|
85
|
-
scene:
|
|
86
|
-
feeling:
|
|
87
|
-
thought:
|
|
88
|
-
unfinished:
|
|
89
|
-
quote: quote && quote.text ? { text:
|
|
102
|
+
text: safeText,
|
|
103
|
+
scene: safeScene,
|
|
104
|
+
feeling: feelings,
|
|
105
|
+
thought: safeThought,
|
|
106
|
+
unfinished: safeUnfinished,
|
|
107
|
+
quote: quote && quote.text ? { text: tooLong('quote', quote.text, 500), source: tooLong('quote.source', quote.source || '', 120) } : null,
|
|
90
108
|
state: state && typeof state === 'object' ? { at: now.toISOString(), ...state } : { at: now.toISOString() }
|
|
91
109
|
},
|
|
92
110
|
later: [],
|
|
@@ -137,11 +155,13 @@ export function currentView(dir, id) {
|
|
|
137
155
|
}
|
|
138
156
|
|
|
139
157
|
// 相关度 + 重要度:重要的先被想起
|
|
140
|
-
//
|
|
141
|
-
|
|
158
|
+
// 相关度 × 重要度:重要的先被想起
|
|
159
|
+
// 阈值比较的是「原始重叠数」,重要度只影响排序,不影响过线门槛
|
|
160
|
+
// mark=false(默认):只读;mark=true 才记一次"想起"(会重写文件,单写者场景用)
|
|
161
|
+
export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false } = {}) {
|
|
142
162
|
const rows = loadAll(dir)
|
|
143
163
|
const q = String(query || '')
|
|
144
|
-
const
|
|
164
|
+
const overlap = (r) => {
|
|
145
165
|
const s = (r.then.text + ' ' + r.then.thought + ' ' + (r.then.feeling || []).join(' '))
|
|
146
166
|
let n = 0
|
|
147
167
|
const big = new Set()
|
|
@@ -149,11 +169,14 @@ export function recall(dir, query, { limit = 3, minOverlap = 1, mark = true } =
|
|
|
149
169
|
for (let i = 0; i + 2 <= qc.length; i++) big.add(qc.slice(i, i + 2))
|
|
150
170
|
const t = s.replace(/[\s\p{P}\p{S}]+/gu, '')
|
|
151
171
|
for (const g of big) if (t.includes(g)) n++
|
|
152
|
-
|
|
153
|
-
return n * w
|
|
172
|
+
return n
|
|
154
173
|
}
|
|
155
|
-
const
|
|
156
|
-
|
|
174
|
+
const weightOf = (r) => (r.weight === 'important' ? 3 : r.weight === 'minor' ? 0.5 : 1)
|
|
175
|
+
const picked = rows.map((r) => {
|
|
176
|
+
const n = overlap(r)
|
|
177
|
+
return { r, n, s: n * weightOf(r) }
|
|
178
|
+
})
|
|
179
|
+
.filter((x) => (q ? x.n >= minOverlap : true))
|
|
157
180
|
.sort((a, b) => b.s - a.s || String(b.r.at).localeCompare(String(a.r.at)))
|
|
158
181
|
.slice(0, limit).map((x) => x.r)
|
|
159
182
|
|
package/lib/waking.js
CHANGED
|
@@ -10,8 +10,10 @@ function readJsonl(file) {
|
|
|
10
10
|
|
|
11
11
|
function dayStr(d = new Date()) { return d.toLocaleDateString('sv-SE') }
|
|
12
12
|
|
|
13
|
+
// 只读:算出「今天该给她看什么」,不写任何文件
|
|
14
|
+
// 调用方确认真的把这段注入了,再调 consumeWaking 记「已展示」
|
|
13
15
|
export function buildWakingBlock(dataDir, chatKey, { now = new Date() } = {}) {
|
|
14
|
-
// 注入范围由调用方决定:这里只按「会话 +
|
|
16
|
+
// 注入范围由调用方决定:这里只按「会话 + 日期」判断是否已经展示过
|
|
15
17
|
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
16
18
|
const today = dayStr(now)
|
|
17
19
|
const flag = path.join(dir, 'waking-flags.json')
|
|
@@ -19,8 +21,6 @@ export function buildWakingBlock(dataDir, chatKey, { now = new Date() } = {}) {
|
|
|
19
21
|
try { flags = JSON.parse(fs.readFileSync(flag, 'utf8')) } catch {}
|
|
20
22
|
const key = chatKey + '|' + today
|
|
21
23
|
if (flags[key]) return ''
|
|
22
|
-
flags[key] = new Date().toISOString()
|
|
23
|
-
try { fs.writeFileSync(flag, JSON.stringify(flags), 'utf8') } catch {}
|
|
24
24
|
|
|
25
25
|
const rows = readJsonl(path.join(dir, 'self-experience.jsonl')).map((r) => {
|
|
26
26
|
if (r.then) return { day: r.day, text: r.then.text, unfinished: r.then.unfinished, weight: r.weight, later: r.later || [] }
|
|
@@ -45,6 +45,18 @@ export function buildWakingBlock(dataDir, chatKey, { now = new Date() } = {}) {
|
|
|
45
45
|
return lines.join('\n')
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
// 确认已展示:写入 flags,之后同一天同一会话不会再出
|
|
49
|
+
export function consumeWaking(dataDir, chatKey, { now = new Date() } = {}) {
|
|
50
|
+
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
51
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
52
|
+
const flag = path.join(dir, 'waking-flags.json')
|
|
53
|
+
let flags = {}
|
|
54
|
+
try { flags = JSON.parse(fs.readFileSync(flag, 'utf8')) } catch {}
|
|
55
|
+
flags[chatKey + '|' + dayStr(now)] = new Date().toISOString()
|
|
56
|
+
fs.writeFileSync(flag, JSON.stringify(flags), 'utf8')
|
|
57
|
+
return true
|
|
58
|
+
}
|
|
59
|
+
|
|
48
60
|
export function leaveNote(dataDir, text) {
|
|
49
61
|
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
50
62
|
const row = { at: new Date().toISOString(), day: dayStr(), text: String(text || '').slice(0, 300) }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Self-layer autobiographical memory core: then / later, append-only. Vault, evidence and consolidation live in the QQ Agent plugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|