whale-memory-core 0.8.1 → 0.9.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/demo.mjs +7 -2
- package/lib/self-experience.d.ts +1 -0
- package/lib/self-experience.js +5 -3
- package/lib/waking.js +15 -6
- package/package.json +1 -1
package/demo.mjs
CHANGED
|
@@ -26,8 +26,13 @@ const e = exp.addExperience(dir, {
|
|
|
26
26
|
console.log('\n[写] id=', e.id, '| 重要度=', e.weight)
|
|
27
27
|
|
|
28
28
|
// 2) 第二天她想起(相关话题触发);mark:true 表示这次取回也记一次“想起”
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
// 查询要用能命中的短锚点;长问句会被“≥8 字要求 60% 命中”的门槛挡住(这是故意的)
|
|
30
|
+
const hits = exp.recall(dir, '烤肉', { limit: 1, mark: true })
|
|
31
|
+
if (!hits.length) {
|
|
32
|
+
console.log('\n[想起] 这次什么都没想起来(查询和记录没有足够重叠)')
|
|
33
|
+
} else {
|
|
34
|
+
console.log('\n[想起]\n' + exp.renderTwoPart(hits[0]))
|
|
35
|
+
}
|
|
31
36
|
|
|
32
37
|
// 3) 她改主意:只往 later 追加,不动"当时"
|
|
33
38
|
exp.reviseExperience(dir, { id: e.id, thought: '现在觉得那个人其实是在护着朋友', reason: '读到后面那段' })
|
package/lib/self-experience.d.ts
CHANGED
|
@@ -69,6 +69,7 @@ export function recall(dir: string, query: string, opts?: {
|
|
|
69
69
|
minOverlap?: number
|
|
70
70
|
mark?: boolean
|
|
71
71
|
minRatio?: number
|
|
72
|
+
mode?: 'strict' | 'loose'
|
|
72
73
|
}): Experience[]
|
|
73
74
|
|
|
74
75
|
export function renderTwoPart(entry: Experience, opts?: { now?: Date; laterCount?: number }): string
|
package/lib/self-experience.js
CHANGED
|
@@ -197,13 +197,15 @@ function queryBigrams(q) {
|
|
|
197
197
|
|
|
198
198
|
// 相关度 × 重要度:重要的先被想起
|
|
199
199
|
// 阈值比较的是「原始重叠数」,重要度只影响排序,不影响过线门槛
|
|
200
|
-
//
|
|
200
|
+
// mode='strict'(默认):长查询要求 ≥60% 命中,宁可不召回也不硬凑
|
|
201
|
+
// mode='loose':只要求至少一个实词命中,适合"想起来就该出现在眼前"的日常召回,宿主自行承担误召
|
|
201
202
|
// mark=false(默认):只读;mark=true 追加一条 recall 事件(不重写经历文件)
|
|
202
|
-
export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false, minRatio = 0.6 } = {}) {
|
|
203
|
+
export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false, minRatio = 0.6, mode = 'strict' } = {}) {
|
|
203
204
|
const rows = loadAll(dir)
|
|
204
205
|
const q = String(query || '')
|
|
205
206
|
const { cleaned, bigrams } = queryBigrams(q)
|
|
206
|
-
const
|
|
207
|
+
const strict = mode !== 'loose'
|
|
208
|
+
const required = (strict && q && cleaned.length >= 8)
|
|
207
209
|
? Math.max(minOverlap, Math.ceil(bigrams.size * minRatio))
|
|
208
210
|
: minOverlap
|
|
209
211
|
|
package/lib/waking.js
CHANGED
|
@@ -12,15 +12,20 @@ function dayStr(d = new Date()) { return d.toLocaleDateString('sv-SE') }
|
|
|
12
12
|
|
|
13
13
|
// 只读:算出「今天该给她看什么」,不写任何文件
|
|
14
14
|
// 调用方确认真的把这段注入了,再调 consumeWaking 记「已展示」
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
// 安全默认:没有 chatKey、或群聊(未显式 allowGroup)→ 返回空
|
|
16
|
+
// 醒来块里可能有昨天私下的话,默认不该流到群里
|
|
17
|
+
export function buildWakingBlock(dataDir, chatKey, { now = new Date(), allowGroup = false } = {}) {
|
|
18
|
+
const key = String(chatKey || '').trim()
|
|
19
|
+
if (!key) return ''
|
|
20
|
+
if (!allowGroup && /^group/i.test(key)) return ''
|
|
21
|
+
|
|
17
22
|
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
18
23
|
const today = dayStr(now)
|
|
19
24
|
const flag = path.join(dir, 'waking-flags.json')
|
|
20
25
|
let flags = {}
|
|
21
26
|
try { flags = JSON.parse(fs.readFileSync(flag, 'utf8')) } catch {}
|
|
22
|
-
const
|
|
23
|
-
if (flags[
|
|
27
|
+
const dedupeKey = key + '|' + today
|
|
28
|
+
if (flags[dedupeKey]) return ''
|
|
24
29
|
|
|
25
30
|
const rows = readJsonl(path.join(dir, 'self-experience.jsonl')).map((r) => {
|
|
26
31
|
if (r.then) return { day: r.day, text: r.then.text, unfinished: r.then.unfinished, weight: r.weight, later: r.later || [] }
|
|
@@ -46,13 +51,17 @@ export function buildWakingBlock(dataDir, chatKey, { now = new Date() } = {}) {
|
|
|
46
51
|
}
|
|
47
52
|
|
|
48
53
|
// 确认已展示:写入 flags,之后同一天同一会话不会再出
|
|
49
|
-
export function consumeWaking(dataDir, chatKey, { now = new Date() } = {}) {
|
|
54
|
+
export function consumeWaking(dataDir, chatKey, { now = new Date(), allowGroup = false } = {}) {
|
|
55
|
+
const key = String(chatKey || '').trim()
|
|
56
|
+
if (!key) return false
|
|
57
|
+
if (!allowGroup && /^group/i.test(key)) return false
|
|
58
|
+
|
|
50
59
|
const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
|
|
51
60
|
fs.mkdirSync(dir, { recursive: true })
|
|
52
61
|
const flag = path.join(dir, 'waking-flags.json')
|
|
53
62
|
let flags = {}
|
|
54
63
|
try { flags = JSON.parse(fs.readFileSync(flag, 'utf8')) } catch {}
|
|
55
|
-
flags[
|
|
64
|
+
flags[key + '|' + dayStr(now)] = new Date().toISOString()
|
|
56
65
|
fs.writeFileSync(flag, JSON.stringify(flags), 'utf8')
|
|
57
66
|
return true
|
|
58
67
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.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",
|