whale-memory-core 0.9.0 → 0.10.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.
@@ -32,6 +32,7 @@ export interface Experience {
32
32
  later: LaterEntry[]
33
33
  recalls?: number
34
34
  lastRecalledAt?: string
35
+ matchedIn?: Array<'then' | 'quote' | 'later'>
35
36
  status?: string
36
37
  legacy?: boolean
37
38
  }
@@ -1,6 +1,7 @@
1
1
  // 她自己的经历 v2:写下时拍下"当时",回想分「当时 / 现在」两段;重要的写细,次要的简写
2
2
  import fs from 'node:fs'
3
3
  import path from 'node:path'
4
+ import crypto from 'node:crypto'
4
5
  import * as recallEvents from './recall-events.js'
5
6
  import * as laterEvents from './later-events.js'
6
7
 
@@ -45,6 +46,14 @@ function tooLong(name, value, max) {
45
46
  return String(value || '')
46
47
  }
47
48
 
49
+ // 旧格式没有 id:用「时间 + 正文指纹」生成,避免同一分钟的多条记录撞成同一个身份
50
+ function legacyId(r) {
51
+ const stamp = String(r.at || Date.now()).replace(/\D/g, '').slice(0, 14)
52
+ const body = String(r.text || '') + '\u0000' + String(r.thought || '') + '\u0000' + String(r.at || '')
53
+ const fp = crypto.createHash('sha1').update(body).digest('hex').slice(0, 8)
54
+ return 'exp_legacy_' + stamp + '_' + fp
55
+ }
56
+
48
57
  function normalize(r) {
49
58
  if (!r) return null
50
59
  if (r.then && typeof r.then === 'object') {
@@ -52,7 +61,7 @@ function normalize(r) {
52
61
  }
53
62
  // 旧格式:text/thought 在顶层
54
63
  return {
55
- id: r.id || ('exp_legacy_' + (r.at || Date.now()).toString().replace(/\D/g, '').slice(0, 12)),
64
+ id: r.id || legacyId(r),
56
65
  at: r.at || '',
57
66
  day: r.day || String(r.at || '').slice(0, 10),
58
67
  author: 'self',
@@ -210,18 +219,35 @@ export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false, mi
210
219
  : minOverlap
211
220
 
212
221
  const measure = (r) => {
213
- const s = (r.then.text + ' ' + r.then.thought + ' ' + (r.then.feeling || []).join(' '))
214
- const t = s.replace(/[\s\p{P}\p{S}]+/gu, '')
215
- let n = 0
216
- for (const g of bigrams) if (t.includes(g)) n++
222
+ const fields = []
223
+ const matched = new Set()
224
+ const scan = (text, label) => {
225
+ const t = String(text || '').replace(/[\s\p{P}\p{S}]+/gu, '')
226
+ if (!t) return
227
+ let any = false
228
+ for (const g of bigrams) {
229
+ if (t.includes(g)) { matched.add(g); any = true }
230
+ }
231
+ if (any) fields.push(label)
232
+ }
233
+ // 最初写下的、原文锚、后来才明白的:都参与匹配
234
+ scan(r.then.text + ' ' + r.then.thought + ' ' + (r.then.feeling || []).join(' '), 'then')
235
+ if (r.then.quote) scan((r.then.quote.text || '') + ' ' + (r.then.quote.source || ''), 'quote')
236
+ scan((r.later || []).map((l) => l.thought).join(' '), 'later')
237
+
238
+ const all = [
239
+ r.then.text, r.then.thought, (r.then.feeling || []).join(' '),
240
+ r.then.quote ? r.then.quote.text : '', r.then.quote ? r.then.quote.source : '',
241
+ (r.later || []).map((l) => l.thought).join(' ')
242
+ ].join(' ').replace(/[\s\p{P}\p{S}]+/gu, '')
217
243
  // 记录自身的 bigram 规模:长记录天然更容易碰巧命中,排序时按 √长度 归一化
218
- const size = Math.max(1, t.length - 1)
219
- return { n, size }
244
+ const size = Math.max(1, all.length - 1)
245
+ return { n: matched.size, size, matchedIn: fields }
220
246
  }
221
247
  const weightOf = (r) => (r.weight === 'important' ? 3 : r.weight === 'minor' ? 0.5 : 1)
222
248
  const picked = rows.map((r) => {
223
- const { n, size } = measure(r)
224
- return { r, n, s: (n / Math.sqrt(size)) * weightOf(r) }
249
+ const { n, size, matchedIn } = measure(r)
250
+ return { r: { ...r, matchedIn }, n, s: (n / Math.sqrt(size)) * weightOf(r) }
225
251
  })
226
252
  .filter((x) => (q ? x.n >= required : true))
227
253
  .sort((a, b) => b.s - a.s || String(b.r.at).localeCompare(String(a.r.at)))
@@ -1,6 +1,7 @@
1
1
  // 她自己的设置:软限制归她,硬熔断归系统
2
2
  import fs from 'node:fs'
3
3
  import path from 'node:path'
4
+ import { addExperience } from './self-experience.js'
4
5
 
5
6
  export const DEFAULTS = {
6
7
  ownTime: '21:00-21:40',
@@ -25,15 +26,13 @@ export function save(dataDir, patch, reason = '') {
25
26
  const next = { ...cur, ...patch, updatedAt: new Date().toISOString() }
26
27
  fs.writeFileSync(f, JSON.stringify(next, null, 1), 'utf8')
27
28
  try {
28
- const dir = path.join(dataDir, 'plugin-data', 'biomimetic-memory')
29
- fs.appendFileSync(path.join(dir, 'self-experience.jsonl'), JSON.stringify({
30
- id: 'exp_set_' + Date.now().toString(36),
31
- at: new Date().toISOString(),
32
- day: new Date().toLocaleDateString('sv-SE'),
33
- author: 'self', source: 'own-decision', kind: '决定',
29
+ // 走同一条追加入口:末尾没换行、文件损坏之类的问题都由 addExperience 处理
30
+ addExperience(path.join(dataDir, 'plugin-data', 'biomimetic-memory'), {
34
31
  text: '我把自己的设置改了:' + JSON.stringify(patch),
35
- thought: String(reason || '').slice(0, 200)
36
- }) + '\n', 'utf8')
37
- } catch {}
32
+ thought: String(reason || '').slice(0, 200),
33
+ kind: '决定',
34
+ source: 'own-decision'
35
+ })
36
+ } catch { }
38
37
  return next
39
38
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whale-memory-core",
3
- "version": "0.9.0",
3
+ "version": "0.10.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",