whale-memory-core 0.6.0 → 0.6.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.
@@ -174,20 +174,44 @@ export function currentView(dir, id) {
174
174
  return rows.find((r) => r.id === id) || null
175
175
  }
176
176
 
177
+ // 高频但不携带信息的词:不参与重叠计算,避免「今天/什么/我的」把不相关的东西拉进来
178
+ const STOP = new Set([
179
+ '什么', '怎么', '这个', '那个', '我的', '你的', '他的', '她的', '我们', '他们', '她们', '自己',
180
+ '昨天', '今天', '明天', '后天', '前天', '现在', '刚刚', '后来', '以前', '以后', '时候',
181
+ '是不是', '有没有', '一点', '一些', '东西', '事情', '觉得', '知道', '记得', '忘了',
182
+ '可能', '应该', '还是', '就是', '但是', '因为', '所以', '如果', '然后', '已经',
183
+ '没有', '不是', '可以', '一个', '这样', '那样', '真的', '其实', '比较', '非常', '特别',
184
+ '一直', '总是', '大概', '也许', '当然', '而是', '并且', '或者', '不过', '虽然', '只是',
185
+ '还有', '以及', '关于', '对于', '买了', '了什', '说了', '去了', '来了', '看到', '做了'
186
+ ])
187
+
188
+ function queryBigrams(q) {
189
+ const cleaned = String(q || '').replace(/[\s\p{P}\p{S}]+/gu, '')
190
+ const out = new Set()
191
+ for (let i = 0; i + 2 <= cleaned.length; i++) {
192
+ const g = cleaned.slice(i, i + 2)
193
+ if (!STOP.has(g)) out.add(g)
194
+ }
195
+ return { cleaned, bigrams: out }
196
+ }
197
+
177
198
  // 相关度 × 重要度:重要的先被想起
178
199
  // 阈值比较的是「原始重叠数」,重要度只影响排序,不影响过线门槛
200
+ // 长查询要求按比例命中,避免「命中一个二字词就算相关」
179
201
  // mark=false(默认):只读;mark=true 追加一条 recall 事件(不重写经历文件)
180
- export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false } = {}) {
202
+ export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false, minRatio = 0.6 } = {}) {
181
203
  const rows = loadAll(dir)
182
204
  const q = String(query || '')
205
+ const { cleaned, bigrams } = queryBigrams(q)
206
+ const required = (q && cleaned.length >= 8)
207
+ ? Math.max(minOverlap, Math.ceil(bigrams.size * minRatio))
208
+ : minOverlap
209
+
183
210
  const overlap = (r) => {
184
211
  const s = (r.then.text + ' ' + r.then.thought + ' ' + (r.then.feeling || []).join(' '))
185
- let n = 0
186
- const big = new Set()
187
- const qc = q.replace(/[\s\p{P}\p{S}]+/gu, '')
188
- for (let i = 0; i + 2 <= qc.length; i++) big.add(qc.slice(i, i + 2))
189
212
  const t = s.replace(/[\s\p{P}\p{S}]+/gu, '')
190
- for (const g of big) if (t.includes(g)) n++
213
+ let n = 0
214
+ for (const g of bigrams) if (t.includes(g)) n++
191
215
  return n
192
216
  }
193
217
  const weightOf = (r) => (r.weight === 'important' ? 3 : r.weight === 'minor' ? 0.5 : 1)
@@ -195,7 +219,7 @@ export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false } =
195
219
  const n = overlap(r)
196
220
  return { r, n, s: n * weightOf(r) }
197
221
  })
198
- .filter((x) => (q ? x.n >= minOverlap : true))
222
+ .filter((x) => (q ? x.n >= required : true))
199
223
  .sort((a, b) => b.s - a.s || String(b.r.at).localeCompare(String(a.r.at)))
200
224
  .slice(0, limit).map((x) => x.r)
201
225
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whale-memory-core",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
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",