whale-memory-core 0.1.0 → 0.2.1
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 +18 -6
- package/lib/self-experience.js +37 -11
- package/lib/self-prompt.js +7 -1
- package/lib/self-settings.js +1 -0
- package/lib/waking.js +1 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
# whale-memory/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**自传层**内核:只管"她自己的经历"这一层,不是完整的记忆系统。纯 ESM、只用 Node 内置模块。
|
|
4
4
|
|
|
5
5
|
```js
|
|
6
6
|
import * as exp from 'whale-memory-core/self-experience'
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
+
## 包含什么、不包含什么
|
|
10
|
+
|
|
11
|
+
包含:
|
|
12
|
+
|
|
13
|
+
- `then` / `later` 两层经历,改主意不覆盖旧想法
|
|
14
|
+
- 召回(相关度 × 重要度)、想起计数、两段式渲染
|
|
15
|
+
- 醒来块、她自己的提示词 overlay、她自己的设置
|
|
16
|
+
|
|
17
|
+
不包含(这些在 `plugin/biomimetic-memory/` 里,属于 QQ Agent 的完整引擎):
|
|
18
|
+
|
|
19
|
+
- vault 存储与索引、evidence-policy 取证、consolidator 合并、注入预算
|
|
20
|
+
- 跨会话可见性判断、宿主工具注册
|
|
21
|
+
|
|
22
|
+
如果你要的是"一个 agent 的自传记忆",这个包够用;如果你要的是"完整记忆系统",看 [`docs/porting.md`](../../docs/porting.md) 里的边界说明,或直接读插件源码。
|
|
23
|
+
|
|
9
24
|
包名 `whale-memory-core`,入口:`whale-memory-core`(命名导出)以及五个子路径:`/self-experience`、`/self-memory`、`/waking`、`/self-prompt`、`/self-settings`。
|
|
10
25
|
|
|
11
26
|
| 模块 | 作用 |
|
|
@@ -19,13 +34,10 @@ import * as exp from 'whale-memory-core/self-experience'
|
|
|
19
34
|
## 安装
|
|
20
35
|
|
|
21
36
|
```sh
|
|
22
|
-
|
|
23
|
-
npm install ./packages/core
|
|
24
|
-
# 或
|
|
25
|
-
npm install ./whale-memory-core-0.1.0.tgz
|
|
37
|
+
npm install whale-memory-core
|
|
26
38
|
```
|
|
27
39
|
|
|
28
|
-
|
|
40
|
+
已发布在 npm:`whale-memory-core@0.1.0`([npmjs.com](https://www.npmjs.com/package/whale-memory-core))。也可以从仓库本地安装:`npm install ./packages/core`。
|
|
29
41
|
|
|
30
42
|
## 运行示例
|
|
31
43
|
|
package/lib/self-experience.js
CHANGED
|
@@ -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,7 +155,8 @@ export function currentView(dir, id) {
|
|
|
137
155
|
}
|
|
138
156
|
|
|
139
157
|
// 相关度 + 重要度:重要的先被想起
|
|
140
|
-
|
|
158
|
+
// mark=true 时,被取回的条目会记一次"想起"(计数 +1,内容不动)
|
|
159
|
+
export function recall(dir, query, { limit = 3, minOverlap = 1, mark = true } = {}) {
|
|
141
160
|
const rows = loadAll(dir)
|
|
142
161
|
const q = String(query || '')
|
|
143
162
|
const score = (r) => {
|
|
@@ -151,10 +170,17 @@ export function recall(dir, query, { limit = 3, minOverlap = 1 } = {}) {
|
|
|
151
170
|
const w = r.weight === 'important' ? 3 : r.weight === 'minor' ? 0.5 : 1
|
|
152
171
|
return n * w
|
|
153
172
|
}
|
|
154
|
-
|
|
173
|
+
const picked = rows.map((r) => ({ r, s: score(r) }))
|
|
155
174
|
.filter((x) => (q ? x.s >= minOverlap : true))
|
|
156
175
|
.sort((a, b) => b.s - a.s || String(b.r.at).localeCompare(String(a.r.at)))
|
|
157
176
|
.slice(0, limit).map((x) => x.r)
|
|
177
|
+
|
|
178
|
+
if (!mark || !picked.length) return picked
|
|
179
|
+
|
|
180
|
+
// 记一笔之后再读回,返回的对象里计数已经是新的
|
|
181
|
+
for (const r of picked) { try { markRecalled(dir, r.id) } catch { /* 坏文件时不阻断召回 */ } }
|
|
182
|
+
const byId = new Map(loadAll(dir).map((r) => [r.id, r]))
|
|
183
|
+
return picked.map((r) => byId.get(r.id) || r)
|
|
158
184
|
}
|
|
159
185
|
|
|
160
186
|
// 「当时 / 现在」两段式:模板拼,不调模型;没有的字段就如实说没有
|
package/lib/self-prompt.js
CHANGED
|
@@ -26,7 +26,13 @@ export function save(dataDir, text, reason = '') {
|
|
|
26
26
|
if (!body) return { ok: false, error: '空的,不写。' }
|
|
27
27
|
if (body.length > MAX_CHARS) return { ok: false, error: '太长了(' + body.length + '/' + MAX_CHARS + ' 字),删一些再试。' }
|
|
28
28
|
const d = base(dataDir)
|
|
29
|
-
const
|
|
29
|
+
const baseStamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 23)
|
|
30
|
+
let stamp = baseStamp
|
|
31
|
+
let n = 1
|
|
32
|
+
while (fs.existsSync(path.join(d, VERSIONS, stamp + '.md'))) {
|
|
33
|
+
stamp = baseStamp + '-' + n
|
|
34
|
+
n += 1
|
|
35
|
+
}
|
|
30
36
|
fs.writeFileSync(path.join(d, VERSIONS, stamp + '.md'), body, 'utf8')
|
|
31
37
|
fs.writeFileSync(path.join(d, ACTIVE), body, 'utf8')
|
|
32
38
|
fs.appendFileSync(path.join(d, 'history.jsonl'), JSON.stringify({ at: new Date().toISOString(), chars: body.length, reason: String(reason || '').slice(0, 200), snapshot: stamp }) + '\n', 'utf8')
|
package/lib/self-settings.js
CHANGED
|
@@ -20,6 +20,7 @@ export function load(dataDir) {
|
|
|
20
20
|
|
|
21
21
|
export function save(dataDir, patch, reason = '') {
|
|
22
22
|
const f = path.join(dataDir, 'plugin-data', 'biomimetic-memory', 'self-settings.json')
|
|
23
|
+
fs.mkdirSync(path.dirname(f), { recursive: true })
|
|
23
24
|
const cur = load(dataDir)
|
|
24
25
|
const next = { ...cur, ...patch, updatedAt: new Date().toISOString() }
|
|
25
26
|
fs.writeFileSync(f, JSON.stringify(next, null, 1), 'utf8')
|
package/lib/waking.js
CHANGED
|
@@ -11,8 +11,7 @@ function readJsonl(file) {
|
|
|
11
11
|
function dayStr(d = new Date()) { return d.toLocaleDateString('sv-SE') }
|
|
12
12
|
|
|
13
13
|
export function buildWakingBlock(dataDir, chatKey, { now = new Date() } = {}) {
|
|
14
|
-
//
|
|
15
|
-
if (!/private_100000000|local:owner/i.test(String(chatKey || ''))) return ''
|
|
14
|
+
// 注入范围由调用方决定:这里只按「会话 + 日期」去重,不判断该不该注入
|
|
16
15
|
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
17
16
|
const today = dayStr(now)
|
|
18
17
|
const flag = path.join(dir, 'waking-flags.json')
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.1",
|
|
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",
|
|
7
7
|
"exports": {
|
|
@@ -38,4 +38,4 @@
|
|
|
38
38
|
"directory": "packages/core"
|
|
39
39
|
},
|
|
40
40
|
"sideEffects": false
|
|
41
|
-
}
|
|
41
|
+
}
|