whale-memory-core 0.23.0 → 0.24.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/index.d.ts +1 -0
- package/index.js +1 -0
- package/lib/hybrid-recall.d.ts +35 -0
- package/lib/hybrid-recall.js +205 -0
- package/lib/self-experience.d.ts +6 -1
- package/lib/self-experience.js +27 -2
- package/package.json +5 -1
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// whale-memory core:不依赖 QQ Agent 的可复用记忆内核
|
|
2
2
|
// 只放通用模块;宿主相关的工具注册与提示词拼接由使用者自己实现
|
|
3
3
|
export * as selfExperience from './lib/self-experience.js'
|
|
4
|
+
export * as hybridRecall from './lib/hybrid-recall.js'
|
|
4
5
|
export * as selfMemory from './lib/self-memory.js'
|
|
5
6
|
export { recallDetail } from './lib/detail-recall.js'
|
|
6
7
|
export * as waking from './lib/waking.js'
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// 混合检索(阶段 2)的类型声明。只有 recall({ ranker: 'hybrid' }) 会用到这些函数,
|
|
2
|
+
// 单独 import 也可以:它们都是纯函数,不读写磁盘。
|
|
3
|
+
import type { Experience } from './self-experience.js'
|
|
4
|
+
|
|
5
|
+
/** 同义词表:[a, b],两个方向都试 */
|
|
6
|
+
export const SYNONYMS: Array<[string, string]>
|
|
7
|
+
|
|
8
|
+
/** 混合检索的覆盖率门槛(字面检索是 MIN_COVER=0.7) */
|
|
9
|
+
export const HYBRID_MIN_COVER: number
|
|
10
|
+
|
|
11
|
+
/** 查询 → 同义替换的变体,原句永远在第一个 */
|
|
12
|
+
export function variantsOf(query: string): string[]
|
|
13
|
+
|
|
14
|
+
/** 索引用的词:拉丁整词 + 中文 2–3 字滑窗 */
|
|
15
|
+
export function tokensOf(text: string): string[]
|
|
16
|
+
|
|
17
|
+
export interface HybridIndex {
|
|
18
|
+
docs: Array<{ tf: Map<string, number>; len: number }>
|
|
19
|
+
df: Map<string, number>
|
|
20
|
+
n: number
|
|
21
|
+
avgdl: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function buildIndex(texts: string[]): HybridIndex
|
|
25
|
+
export function idfOf(index: HybridIndex, token: string): number
|
|
26
|
+
export function bm25Scores(index: HybridIndex, tokens: string[]): number[]
|
|
27
|
+
|
|
28
|
+
/** 对已经过滤好的 rows 做混合排序;返回值形状和 recall 一致,另带 hybridScore / hybridCover */
|
|
29
|
+
export function hybridRank(opts: {
|
|
30
|
+
rows: unknown[]
|
|
31
|
+
query: string
|
|
32
|
+
context?: string
|
|
33
|
+
limit?: number
|
|
34
|
+
minCover?: number
|
|
35
|
+
}): Array<Experience & { hybridScore: number; hybridCover: number }>
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
// 混合检索(阶段 2):BM25(字符 2–3 字 n-gram)+ 一张小同义词表。
|
|
2
|
+
//
|
|
3
|
+
// 只在 recall({ ranker: 'hybrid' }) 时用;不传 ranker 的字面检索一个字节都没改(默认还是 literal)。
|
|
4
|
+
// 零第三方依赖、不调外部 API。
|
|
5
|
+
//
|
|
6
|
+
// 三条设计取舍(结果见 docs/eval-v2-zh.md):
|
|
7
|
+
// 1. 门禁仍用 query-match.js 的 contentUnits / coverage(防编造那套口径),只是把查询先做
|
|
8
|
+
// 同义替换再算覆盖率;所以「灯塔里的猫」这种沾边句不会被放进来 —— 猫不在,覆盖率就上不去。
|
|
9
|
+
// 2. BM25 只影响排序,不影响放行。宁可少召回,也不为了分数高就把没根据的东西递回去。
|
|
10
|
+
// 3. context(上一轮查询)只影响排序;只有追问句本身几乎没有内容词时,才用它补足门禁
|
|
11
|
+
// —— 纯代词/省略式追问单看自己没法判断在问什么,这是宿主把上下文递进来的那部分责任。
|
|
12
|
+
import { contentUnits, coverage, matchedTerms, queryTerms } from './query-match.js'
|
|
13
|
+
|
|
14
|
+
// 小同义词表:只收「换个说法还是同一件事」的日常词,不做实体消歧。
|
|
15
|
+
// 每对 [a, b] 两种方向都试(查询里出现哪一边,就把哪一边换成另一边再算一遍覆盖率)。
|
|
16
|
+
// 表小是故意的:同义词放得越宽,误召回的来源就越多;这一版先证明机制,不追求覆盖所有说法。
|
|
17
|
+
export const SYNONYMS = [
|
|
18
|
+
['外婆', '姥姥'], ['外公', '姥爷'],
|
|
19
|
+
['吵架', '拌嘴'], ['道歉', '认错'],
|
|
20
|
+
['失眠', '睡不着'], ['日落', '太阳下山'],
|
|
21
|
+
['买花', '花店'], ['照片', '相片'], ['拍照', '照相'],
|
|
22
|
+
['明信片', '卡片'], ['收音机', '电台'],
|
|
23
|
+
['话剧', '戏剧'], ['志愿者', '义工'],
|
|
24
|
+
['宿舍', '寝室'], ['食堂', '饭堂'],
|
|
25
|
+
['家教', '补课'], ['工资', '薪水'],
|
|
26
|
+
['考试周', '期末'], ['关门', '闭馆'], ['误点', '晚点'],
|
|
27
|
+
['河道', '河流'], ['变迁', '改道'],
|
|
28
|
+
['罚钱', '罚款'], ['课表', '课程表'], ['招人', '招新'],
|
|
29
|
+
['对联', '春联'], ['鞭炮', '烟花'], ['讲价', '砍价'],
|
|
30
|
+
['小鸟', '麻雀'], ['窗沿', '窗台'], ['铁盒', '铁皮盒'],
|
|
31
|
+
['新芽', '新叶'], ['冬衣', '冬天的衣服'], ['演出结束', '谢幕'],
|
|
32
|
+
['落选', '没进'], ['橘子', '桔子'], ['粥', '稀饭'],
|
|
33
|
+
['丢', '不见了'], ['小书', '小册子'], ['旧票', '门票']
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
// 一条查询最多派生出几个变体:表大了以后别让它组合爆炸
|
|
37
|
+
const MAX_VARIANTS = 8
|
|
38
|
+
// 混合检索的覆盖率门槛(字面检索是 0.7)。0.65 仍然拦得住「灯塔里的猫」(0.57)和
|
|
39
|
+
// 「明信片的邮资」(0.6)这类沾边句。这个数是看开发集定的;留出集只在最后跑一次(见 docs/eval-v2-zh.md)。
|
|
40
|
+
export const HYBRID_MIN_COVER = 0.65
|
|
41
|
+
const K1 = 1.2
|
|
42
|
+
const B = 0.75
|
|
43
|
+
// 上一轮查询在排序里的分量(只影响排序,不影响放行)
|
|
44
|
+
const CONTEXT_WEIGHT = 0.35
|
|
45
|
+
// 重要度只做轻微加权:字面检索是 3 倍乘,BM25 分数量纲不同,照搬会把排序压扁
|
|
46
|
+
const WEIGHT_FACTOR = { important: 1.5, normal: 1, minor: 0.75 }
|
|
47
|
+
|
|
48
|
+
// 只起语法作用的单字:只由它们组成的 n-gram 不进索引(和 query-match 的口径一致,但这里只是省空间)
|
|
49
|
+
const FUNCTION_CHARS = new Set([...'的了吗呢吧啊呀么着过是在里那这个些次段事就也都还又很把被给跟和与及或是有我你他她它们嘛啦哦呃'])
|
|
50
|
+
const GRAM_STOP = new Set([
|
|
51
|
+
'什么', '怎么', '这个', '那个', '我的', '你的', '我们', '他们', '她们', '自己',
|
|
52
|
+
'今天', '昨天', '明天', '后天', '现在', '刚刚', '后来', '以前', '以后', '时候',
|
|
53
|
+
'是不是', '有没有', '一点', '一些', '东西', '事情', '觉得', '知道', '记得',
|
|
54
|
+
'可能', '应该', '还是', '就是', '但是', '因为', '所以', '如果', '然后', '已经',
|
|
55
|
+
'没有', '不是', '可以', '一个', '这样', '那样', '真的', '其实', '比较', '非常',
|
|
56
|
+
'一直', '总是', '大概', '也许', '当然', '不过', '虽然', '只是', '还有', '以及'
|
|
57
|
+
])
|
|
58
|
+
const STOP_EN = new Set([
|
|
59
|
+
'the', 'and', 'for', 'with', 'what', 'did', 'was', 'were', 'you', 'your', 'she', 'her',
|
|
60
|
+
'his', 'him', 'they', 'them', 'this', 'that', 'there', 'then', 'from', 'about', 'have',
|
|
61
|
+
'has', 'had', 'yesterday', 'today', 'tomorrow', 'just', 'some', 'any', 'how', 'why',
|
|
62
|
+
'when', 'who', 'are', 'not'
|
|
63
|
+
])
|
|
64
|
+
|
|
65
|
+
// 索引用的词:拉丁整词 + 中文 2–3 字滑窗;全由虚词组成的 n-gram 丢掉
|
|
66
|
+
export function tokensOf(text) {
|
|
67
|
+
const s = String(text || '').toLowerCase()
|
|
68
|
+
const out = []
|
|
69
|
+
for (const w of s.match(/[a-z]{2,}/g) || []) if (!STOP_EN.has(w)) out.push(w)
|
|
70
|
+
for (const run of s.replace(/[a-z]+/g, ' ').split(/[\s\p{P}\p{S}]+/u)) {
|
|
71
|
+
const chars = [...run]
|
|
72
|
+
for (let n = 2; n <= 3; n++) {
|
|
73
|
+
for (let i = 0; i + n <= chars.length; i++) {
|
|
74
|
+
const g = chars.slice(i, i + n).join('')
|
|
75
|
+
if (GRAM_STOP.has(g)) continue
|
|
76
|
+
if ([...g].every((c) => FUNCTION_CHARS.has(c))) continue
|
|
77
|
+
out.push(g)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 查询 → 同义替换的变体(原句永远在第一个)
|
|
85
|
+
export function variantsOf(query) {
|
|
86
|
+
const q = String(query || '')
|
|
87
|
+
const out = [q]
|
|
88
|
+
for (const [a, b] of SYNONYMS) {
|
|
89
|
+
for (const [from, to] of [[a, b], [b, a]]) {
|
|
90
|
+
if (!from || !q.includes(from)) continue
|
|
91
|
+
const v = q.split(from).join(to)
|
|
92
|
+
if (v !== q && !out.includes(v)) out.push(v)
|
|
93
|
+
if (out.length >= MAX_VARIANTS) return out
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return out
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 倒排索引:每条文本的 tf、长度、df、平均长度
|
|
100
|
+
export function buildIndex(texts) {
|
|
101
|
+
const docs = texts.map((text) => {
|
|
102
|
+
const tf = new Map()
|
|
103
|
+
for (const token of tokensOf(text)) tf.set(token, (tf.get(token) || 0) + 1)
|
|
104
|
+
let len = 0
|
|
105
|
+
for (const n of tf.values()) len += n
|
|
106
|
+
return { tf, len }
|
|
107
|
+
})
|
|
108
|
+
const df = new Map()
|
|
109
|
+
let total = 0
|
|
110
|
+
for (const d of docs) {
|
|
111
|
+
total += d.len
|
|
112
|
+
for (const token of d.tf.keys()) df.set(token, (df.get(token) || 0) + 1)
|
|
113
|
+
}
|
|
114
|
+
return { docs, df, n: docs.length, avgdl: total / Math.max(1, docs.length) }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function idfOf(index, token) {
|
|
118
|
+
const df = index.df.get(token) || 0
|
|
119
|
+
return Math.log(1 + (index.n - df + 0.5) / (df + 0.5))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// BM25:查询词里的重复只算一次(qtf 忽略),文档内外都按字符 n-gram 计
|
|
123
|
+
export function bm25Scores(index, tokens) {
|
|
124
|
+
const scores = new Array(index.docs.length).fill(0)
|
|
125
|
+
const seen = new Set(tokens)
|
|
126
|
+
for (const token of seen) {
|
|
127
|
+
if (!index.df.has(token)) continue
|
|
128
|
+
const idf = idfOf(index, token)
|
|
129
|
+
for (let i = 0; i < index.docs.length; i++) {
|
|
130
|
+
const tf = index.docs[i].tf.get(token)
|
|
131
|
+
if (!tf) continue
|
|
132
|
+
const dl = index.docs[i].len
|
|
133
|
+
scores[i] += idf * ((tf * (K1 + 1)) / (tf + K1 * (1 - B + (B * dl) / index.avgdl)))
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return scores
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 一条经历的可检索文本,按字段拆开(用来标 matchedIn / matchedLater)
|
|
140
|
+
function partsOf(row) {
|
|
141
|
+
const t = (row && row.then) || {}
|
|
142
|
+
return {
|
|
143
|
+
then: [t.text, t.thought, (t.feeling || []).join(' ')].filter(Boolean).join(' '),
|
|
144
|
+
quote: t.quote ? [t.quote.text, t.quote.source].filter(Boolean).join(' ') : '',
|
|
145
|
+
later: (row.later || []).map((l) => [l.thought, Array.isArray(l.feeling) ? l.feeling.join(' ') : ''].filter(Boolean).join(' ')).join(' ')
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function markMatched(row, variants) {
|
|
150
|
+
const parts = partsOf(row)
|
|
151
|
+
const matchedIn = []
|
|
152
|
+
const terms = [...new Set(variants.flatMap((v) => [...queryTerms(v).terms]))]
|
|
153
|
+
for (const [label, text] of [['then', parts.then], ['quote', parts.quote], ['later', parts.later]]) {
|
|
154
|
+
if (text && matchedTerms(new Set(terms), text).size) matchedIn.push(label)
|
|
155
|
+
}
|
|
156
|
+
const matchedLater = (row.later || []).filter((l) => {
|
|
157
|
+
const text = [l.thought, Array.isArray(l.feeling) ? l.feeling.join(' ') : ''].filter(Boolean).join(' ')
|
|
158
|
+
return text && matchedTerms(new Set(terms), text).size
|
|
159
|
+
})
|
|
160
|
+
return { matchedIn, matchedLater }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// 对一个已经筛好的 rows(封存 / asOf / backend 都由 recall 处理完)做混合排序
|
|
164
|
+
export function hybridRank({ rows, query, context = '', limit = 3, minCover = HYBRID_MIN_COVER } = {}) {
|
|
165
|
+
const q = String(query || '')
|
|
166
|
+
const ctx = String(context || '')
|
|
167
|
+
const texts = rows.map((row) => {
|
|
168
|
+
const p = partsOf(row)
|
|
169
|
+
return [p.then, p.quote, p.later].filter(Boolean).join(' ')
|
|
170
|
+
})
|
|
171
|
+
const index = buildIndex(texts)
|
|
172
|
+
|
|
173
|
+
// 门禁:查询本身内容词太少(纯代词 / 省略式追问)时,用「上下文 + 追问」补足;
|
|
174
|
+
// 否则只按这一轮问的话算覆盖率 —— 上下文不替它放行。
|
|
175
|
+
const ownUnits = contentUnits(q)
|
|
176
|
+
// 内容单元 ≤3 的问句按字面检索的门槛(0.7):三个词里对上两个是 0.667,
|
|
177
|
+
// 0.65 会放行「桂花糕」「灯塔猫」「去北京」这类少了关键字的句子(独立复审抓到的)。
|
|
178
|
+
const gateCover = ownUnits.length <= 3 ? Math.max(minCover, 0.7) : minCover
|
|
179
|
+
const gateText = ownUnits.length <= 1 && ctx ? ctx + ' ' + q : q
|
|
180
|
+
const gateVariants = variantsOf(gateText)
|
|
181
|
+
const rankTokens = [...tokensOf(gateVariants.join(' ')), ...tokensOf(ctx)]
|
|
182
|
+
const bm = bm25Scores(index, rankTokens)
|
|
183
|
+
let maxBm = 1e-9
|
|
184
|
+
for (const score of bm) if (score > maxBm) maxBm = score
|
|
185
|
+
|
|
186
|
+
const picked = []
|
|
187
|
+
for (let i = 0; i < rows.length; i++) {
|
|
188
|
+
let best = { cover: 0, hits: 0, variant: gateText }
|
|
189
|
+
for (const variant of gateVariants) {
|
|
190
|
+
const units = contentUnits(variant)
|
|
191
|
+
const cover = coverage(units, texts[i])
|
|
192
|
+
const hits = matchedTerms(queryTerms(variant).terms, texts[i]).size
|
|
193
|
+
if (cover > best.cover || (cover === best.cover && hits > best.hits)) best = { cover, hits, variant }
|
|
194
|
+
}
|
|
195
|
+
if (best.hits < 1 || best.cover < gateCover) continue
|
|
196
|
+
const score = (0.75 * (bm[i] / maxBm) + 0.25 * best.cover) * (WEIGHT_FACTOR[rows[i].weight] || 1)
|
|
197
|
+
picked.push({ row: rows[i], score, cover: best.cover, variant: best.variant })
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
picked.sort((a, b) => b.score - a.score || String(b.row.at).localeCompare(String(a.row.at)))
|
|
201
|
+
return picked.slice(0, limit).map((p) => {
|
|
202
|
+
const matched = markMatched(p.row, [p.variant])
|
|
203
|
+
return { ...p.row, ...matched, hybridScore: Number(p.score.toFixed(6)), hybridCover: Number(p.cover.toFixed(6)) }
|
|
204
|
+
})
|
|
205
|
+
}
|
package/lib/self-experience.d.ts
CHANGED
|
@@ -133,6 +133,11 @@ export function recall(dir: string, query: string, opts?: {
|
|
|
133
133
|
gate?: 'cover' | 'legacy'
|
|
134
134
|
/** 读哪个容器(默认 file)。sqlite 下不能 mark:写路径还在 JSONL 上 */
|
|
135
135
|
backend?: StorageBackendName
|
|
136
|
+
/** 匹配/排序方式:literal(默认,0.23.0 及以前的行为不变;null/undefined 也按 literal)或 hybrid(BM25 + 字符 n-gram + 小同义词表,实验性)。
|
|
137
|
+
* hybrid 下 mode / minOverlap / minRatio / gate 都不参与,空查询返回空。 */
|
|
138
|
+
ranker?: 'literal' | 'hybrid' | null
|
|
139
|
+
/** 上一轮查询(字符串或数组),给多轮追问用;字面检索拼到查询前面,混合检索只用它排序 */
|
|
140
|
+
context?: string | string[] | null
|
|
136
141
|
}): Experience[]
|
|
137
142
|
|
|
138
143
|
/** 事后联想:两条都必须存在、必须写理由;同一对(不分方向)只连一次 */
|
|
@@ -183,7 +188,7 @@ export interface RecallContextItem {
|
|
|
183
188
|
raw: Experience
|
|
184
189
|
}
|
|
185
190
|
|
|
186
|
-
/** 宿主拿用户原话自动召回用;默认 gate 为
|
|
191
|
+
/** 宿主拿用户原话自动召回用;默认 gate 为 cover(0.17 起),升级不改变自动递回的行为 */
|
|
187
192
|
export function recallForContext(dir: string, query: string, opts?: { max?: number; gate?: 'cover' | 'legacy' }): RecallContextItem[]
|
|
188
193
|
|
|
189
194
|
export const internals: { WEIGHTS: Set<Weight>; FILE: string }
|
package/lib/self-experience.js
CHANGED
|
@@ -9,6 +9,7 @@ import * as sealEvents from './seal-events.js'
|
|
|
9
9
|
import * as linkEvents from './link-events.js'
|
|
10
10
|
import * as chapterEvents from './chapter-events.js'
|
|
11
11
|
import { queryTerms, requiredOverlap, matchedTerms, normalizeText, contentUnits, coverage, MIN_COVER } from './query-match.js'
|
|
12
|
+
import { hybridRank } from './hybrid-recall.js'
|
|
12
13
|
import { parseAsOf, before, dayOf } from './as-of.js'
|
|
13
14
|
import { readSnapshot } from './sqlite-store.js'
|
|
14
15
|
|
|
@@ -561,11 +562,35 @@ export function currentView(dir, id, { asOf = null, backend = 'file' } = {}) {
|
|
|
561
562
|
// 'legacy' = 0.16 及以前的口径(长查询要 60% 的两字词对上),排序也照旧。宿主想保持旧行为时用
|
|
562
563
|
// backend:读哪个容器(默认 file,见 loadAll)。写路径还没切到 sqlite:sqlite 下 mark=true 会把想起记到 JSONL、
|
|
563
564
|
// 库里看不见,所以直接拒绝,不留一笔两边对不上的账
|
|
564
|
-
|
|
565
|
+
// ranker:'literal'(默认,0.23.0 及以前的行为一个字都没改)或 'hybrid'(BM25 + 字符 n-gram + 小同义词表,
|
|
566
|
+
// 见 hybrid-recall.js)。实验性:指标没显著提升就不切默认,默认仍是字面检索。
|
|
567
|
+
// context:上一轮查询(字符串或数组)。宿主做多轮追问时把上下文一起递进来;字面检索把它拼到查询前面,
|
|
568
|
+
// 混合检索只用它排序(追问句本身几乎没有内容词时才用它补足门禁)。不传就是单轮,行为不变。
|
|
569
|
+
// ranker='hybrid' 时 mode / gate / minOverlap / minRatio 都不参与(它有自己的覆盖率门槛,见 hybrid-recall.js);
|
|
570
|
+
// 空查询在 hybrid 下返回空(字面检索的旧行为不变)。ranker 传 null / undefined 都按 literal 处理。
|
|
571
|
+
export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false, minRatio = 0.6, mode = 'strict', asOf = null, includeSealed = false, gate = 'cover', backend = 'file', ranker = 'literal', context = null } = {}) {
|
|
565
572
|
if (mark && asOf !== null && asOf !== undefined && asOf !== '') throw new Error('Looking back at an earlier day does not record a recall: mark and asOf cannot be used together.(翻回旧日子时不记「想起」:mark 和 asOf 不能一起用。)')
|
|
566
573
|
if (mark && backendName(backend) !== 'file') throw new Error('mark=true only works with the file backend for now: recalls are still written to JSONL.(写路径还没切到 sqlite:mark=true 目前只能配 file 后端。)')
|
|
574
|
+
if (ranker !== 'literal' && ranker !== 'hybrid') {
|
|
575
|
+
if (ranker === null || ranker === undefined) ranker = 'literal'
|
|
576
|
+
else throw new Error('ranker must be "literal" or "hybrid"(ranker 只能是 literal 或 hybrid)')
|
|
577
|
+
}
|
|
567
578
|
const rows = loadAll(dir, { asOf, backend }).filter((r) => includeSealed || !r.sealed)
|
|
568
|
-
const
|
|
579
|
+
const q0 = String(query || '')
|
|
580
|
+
const ctxText = Array.isArray(context) ? context.filter(Boolean).join(' ') : String(context || '')
|
|
581
|
+
|
|
582
|
+
// 混合检索:放行门槛仍是防编造那套(覆盖率),BM25 只重排;mark 的行为和字面检索一致
|
|
583
|
+
if (ranker === 'hybrid') {
|
|
584
|
+
const hits = hybridRank({ rows, query: q0, context: ctxText, limit })
|
|
585
|
+
if (!mark || !hits.length) return hits
|
|
586
|
+
const at = new Date().toISOString()
|
|
587
|
+
for (const r of hits) {
|
|
588
|
+
try { recallEvents.append(dir, { id: r.id, query: q0 }) } catch { /* 坏文件时不阻断召回 */ }
|
|
589
|
+
}
|
|
590
|
+
return hits.map((r) => ({ ...r, recalls: Number(r.recalls || 0) + 1, lastRecalledAt: at }))
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const q = ctxText ? ctxText + ' ' + q0 : q0
|
|
569
594
|
const parsed = queryTerms(q)
|
|
570
595
|
const { terms } = parsed
|
|
571
596
|
const required = requiredOverlap(parsed, { mode, minOverlap, minRatio })
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.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",
|
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
"types": "./lib/action-source.d.ts",
|
|
18
18
|
"default": "./lib/action-source.js"
|
|
19
19
|
},
|
|
20
|
+
"./hybrid-recall": {
|
|
21
|
+
"types": "./lib/hybrid-recall.d.ts",
|
|
22
|
+
"default": "./lib/hybrid-recall.js"
|
|
23
|
+
},
|
|
20
24
|
"./self-experience": {
|
|
21
25
|
"types": "./lib/self-experience.d.ts",
|
|
22
26
|
"default": "./lib/self-experience.js"
|