whale-memory-core 0.1.0 → 0.2.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 +18 -6
- package/lib/self-experience.js +10 -2
- 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
|
@@ -137,7 +137,8 @@ export function currentView(dir, id) {
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
// 相关度 + 重要度:重要的先被想起
|
|
140
|
-
|
|
140
|
+
// mark=true 时,被取回的条目会记一次"想起"(计数 +1,内容不动)
|
|
141
|
+
export function recall(dir, query, { limit = 3, minOverlap = 1, mark = true } = {}) {
|
|
141
142
|
const rows = loadAll(dir)
|
|
142
143
|
const q = String(query || '')
|
|
143
144
|
const score = (r) => {
|
|
@@ -151,10 +152,17 @@ export function recall(dir, query, { limit = 3, minOverlap = 1 } = {}) {
|
|
|
151
152
|
const w = r.weight === 'important' ? 3 : r.weight === 'minor' ? 0.5 : 1
|
|
152
153
|
return n * w
|
|
153
154
|
}
|
|
154
|
-
|
|
155
|
+
const picked = rows.map((r) => ({ r, s: score(r) }))
|
|
155
156
|
.filter((x) => (q ? x.s >= minOverlap : true))
|
|
156
157
|
.sort((a, b) => b.s - a.s || String(b.r.at).localeCompare(String(a.r.at)))
|
|
157
158
|
.slice(0, limit).map((x) => x.r)
|
|
159
|
+
|
|
160
|
+
if (!mark || !picked.length) return picked
|
|
161
|
+
|
|
162
|
+
// 记一笔之后再读回,返回的对象里计数已经是新的
|
|
163
|
+
for (const r of picked) { try { markRecalled(dir, r.id) } catch { /* 坏文件时不阻断召回 */ } }
|
|
164
|
+
const byId = new Map(loadAll(dir).map((r) => [r.id, r]))
|
|
165
|
+
return picked.map((r) => byId.get(r.id) || r)
|
|
158
166
|
}
|
|
159
167
|
|
|
160
168
|
// 「当时 / 现在」两段式:模板拼,不调模型;没有的字段就如实说没有
|
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.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
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
|
+
}
|