dsh-fish-soul 0.18.3 → 0.18.5
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
CHANGED
|
@@ -597,6 +597,33 @@ function decodeMessageNode(c, selfSecUid = '', secByUid = null) {
|
|
|
597
597
|
}
|
|
598
598
|
}
|
|
599
599
|
|
|
600
|
+
/**
|
|
601
|
+
* 这条解出来的消息算不算「一条真消息」。
|
|
602
|
+
*
|
|
603
|
+
* 🔴 **全仓唯一的判据** —— 以前这个判据有 3 份副本(本文件的 `countUseful`
|
|
604
|
+
* 和 `pickMessages` 各一份内联、`douyin-dm.mjs` 的 `filterUseful` 一份),
|
|
605
|
+
* 修一处漏两处:#129 给 `filterUseful` 挡了协议控制帧,但这两处内联副本没挡,
|
|
606
|
+
* 于是控制帧(kind=50001、text 空、带 convId/seq/senderId)照样通过
|
|
607
|
+
* `pickMessages` 的「唯一会话」判定 → 它的 convId 被当成这一轮读到的会话,
|
|
608
|
+
* 多个会话拿到同一个 convId(#128 的串会话)。
|
|
609
|
+
* ⇒ 判据只留这一份,`douyin-dm.mjs` 从这里 import。
|
|
610
|
+
*
|
|
611
|
+
* 判据要点(都是踩出来的,别动):
|
|
612
|
+
* · 不能放 `kind`:空壳消息的 kind 也非 0(实测 58),放进来会让
|
|
613
|
+
* 「解出一堆空壳」的结构赢过「解出真消息」的结构。
|
|
614
|
+
* · 协议控制帧必须挡:`kind === 50001` 或带 `raw.command_type` 的帧
|
|
615
|
+
* (登录态同步、会话标记等)也有 convId/seq/senderId,但它不是用户消息,
|
|
616
|
+
* 拿来定会话归属会把别人的消息记到这个会话头上。
|
|
617
|
+
*
|
|
618
|
+
* @param {object} m parseMessages 解出来的消息
|
|
619
|
+
* @returns {boolean}
|
|
620
|
+
*/
|
|
621
|
+
export function isUsefulMessage(m) {
|
|
622
|
+
if (!m) return false
|
|
623
|
+
if (m.kind === 50001 || (m.raw && m.raw.command_type != null)) return false
|
|
624
|
+
return Boolean(m.text || m.tips || m.shareAweme || m.convId || m.seq || m.senderId)
|
|
625
|
+
}
|
|
626
|
+
|
|
600
627
|
/**
|
|
601
628
|
* 把 hook 抓到的一条 protobuf 响应解成消息数组。
|
|
602
629
|
*
|
|
@@ -642,15 +669,9 @@ export function parseMessages(raw, opts = {}) {
|
|
|
642
669
|
|
|
643
670
|
const selfSecUid = String(opts.selfSecUid || '')
|
|
644
671
|
|
|
645
|
-
//
|
|
646
|
-
//
|
|
647
|
-
|
|
648
|
-
// ⚠️ 判据里**不能放 kind**:空壳消息的 kind 也是非 0(实测 58),放进去会让「解出一堆空壳」
|
|
649
|
-
// 的结构赢过「解出真消息」的结构 —— 直接挑错。只有 convId / seq / senderId 才是
|
|
650
|
-
// 「这条真的是一条带身份的消息」的证据。
|
|
651
|
-
const countUseful = (list) => list.filter(
|
|
652
|
-
(m) => m.text || m.tips || m.shareAweme || m.convId || m.seq || m.senderId,
|
|
653
|
-
).length
|
|
672
|
+
// 「解出来了」的判据走全仓唯一那份(`isUsefulMessage`)—— 详见它的注释:
|
|
673
|
+
// 内联副本会漏掉后来加的判据(协议控制帧),#128 的串会话就是这么来的。
|
|
674
|
+
const countUseful = (list) => list.filter(isUsefulMessage).length
|
|
654
675
|
|
|
655
676
|
// ① 旧结构:消息在 `#6.#301.#1[]`。解每个 item 时重新走一遍原始字段,
|
|
656
677
|
// 否则 36 字节的会话 ID 会被 parseTree 误判成子树(见 fieldsToNodes 注释)。
|
|
@@ -861,12 +882,10 @@ export function pickMessages(captured, opts = {}) {
|
|
|
861
882
|
try {
|
|
862
883
|
const raw = Buffer.from(h.b64, 'base64')
|
|
863
884
|
const parsed = parseMessages(raw, opts)
|
|
864
|
-
//
|
|
865
|
-
//
|
|
866
|
-
//
|
|
867
|
-
const useful = parsed.messages.filter(
|
|
868
|
-
(m) => m.text || m.tips || m.shareAweme || m.convId || m.seq || m.senderId,
|
|
869
|
-
)
|
|
885
|
+
// 判据走全仓唯一那份(`isUsefulMessage`):媒体消息(无 text/tips)也算解出来了,
|
|
886
|
+
// 否则「对方只发图片」的响应会被整条跳过 → 推送/轮询都拿不到 convId;
|
|
887
|
+
// 同时又挡掉协议控制帧(它带 convId,会被当成「这一轮读到的会话」)。
|
|
888
|
+
const useful = parsed.messages.filter(isUsefulMessage)
|
|
870
889
|
if (!useful.length) continue
|
|
871
890
|
const ids = new Set(useful.map((m) => String(m.convId ?? '').trim()).filter((x) => x !== ''))
|
|
872
891
|
// 指定了会话:只要它,别的会话直接跳过
|
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
drain as apiDrain,
|
|
22
22
|
peek as apiPeek,
|
|
23
23
|
pickMessages as apiPickMessages,
|
|
24
|
+
isUsefulMessage,
|
|
24
25
|
parseMessages as apiParseMessages,
|
|
25
26
|
selfInfo as apiSelfInfo,
|
|
26
27
|
triggerFetch as apiTriggerFetch,
|
|
@@ -401,13 +402,13 @@ function shapeResult(rawMessages, limit, via, convId = '') {
|
|
|
401
402
|
*
|
|
402
403
|
* ⚠️ 缓存也要用这个过滤 —— 否则缓存里塞一堆 kind=8 的空壳,
|
|
403
404
|
* `shapeResult` 过滤完就空了,表现为「缓存命中了但 0 条」。
|
|
405
|
+
*
|
|
406
|
+
* 🔴 判据本体在 `douyin-api.mjs` 的 `isUsefulMessage`(全仓唯一一份)。
|
|
407
|
+
* 以前这里是第 3 份副本,于是 #129 只修到这一份、漏了 api 里那两份,
|
|
408
|
+
* 控制帧照样从 `pickMessages` 穿过去(#128 的串会话)。别再复制判据过来。
|
|
404
409
|
*/
|
|
405
410
|
function filterUseful(rawMessages) {
|
|
406
|
-
return (rawMessages || []).filter(
|
|
407
|
-
// ⚠️ 判据里**不能放 kind**:空壳消息的 kind 也是非 0(实测 58),放进来会把
|
|
408
|
-
// 「解出一堆空壳」当成「解出消息」。只有 text/tips/shareAweme 或
|
|
409
|
-
// convId/seq/senderId 才算真的解出了一条带身份的消息。
|
|
410
|
-
m.text || m.tips || m.shareAweme || m.convId || m.seq || m.senderId)
|
|
411
|
+
return (rawMessages || []).filter(isUsefulMessage)
|
|
411
412
|
}
|
|
412
413
|
|
|
413
414
|
/** 内部函数给测试用(不参与对外契约)。 */
|
package/lib/douyin-browse.js
CHANGED
|
@@ -231,7 +231,9 @@ export class DouyinBrowser {
|
|
|
231
231
|
const timeout = () => Math.max(1, Math.min(this.tm.evalTimeoutMs, left()));
|
|
232
232
|
// 预算耗尽后**不能再发起** eval / send / 睡眠:max(1,…) 只防 0,不是通行证。
|
|
233
233
|
// 否则点「下一条」失败后,键盘 / 滚轮分支会各在预算之外再等一次超时,照样霸锁。
|
|
234
|
-
|
|
234
|
+
// 🔴 剩余预算只剩几毫秒时也别发起:timeout() 的 max(1,…) 会把 1ms 当成可用预算,
|
|
235
|
+
// 于是 send 在 left()=1ms 时被放行、1ms 后超时——断言「预算耗尽后不再发起」会红。
|
|
236
|
+
const expired = () => left() <= 10;
|
|
235
237
|
const changed = async () => {
|
|
236
238
|
for (let i = 0; i < 8; i++) {
|
|
237
239
|
if (expired()) return false;
|
package/lib/retrieval.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* - generation_id 世代锁:切换模型/端点/维度或重建时 +1 清表,Worker 写库前校验世代;
|
|
10
10
|
* - Fail-open 铁律:未配 Key / 网络异常 / 800ms 物理超时 → 立即降级纯 BM25,绝不断对话。
|
|
11
11
|
*/
|
|
12
|
-
import { bm25Search, queryForBm25, CHAR_WEIGHT } from './segment.js';
|
|
12
|
+
import { bm25Search, queryForBm25, splitKeywords, tokenizeForBm25, CHAR_WEIGHT } from './segment.js';
|
|
13
13
|
import { decayFactor } from './soul-memory.js';
|
|
14
14
|
import { bumpGeneration, getKv, readVecMeta, setKv, writeVecMeta } from './db.js';
|
|
15
15
|
|
|
@@ -20,6 +20,62 @@ const REALTIME_BATCH = 32;
|
|
|
20
20
|
const REBUILD_MAX_BATCH = 8;
|
|
21
21
|
const REALTIME_MAX_ATTEMPTS = 5;
|
|
22
22
|
|
|
23
|
+
// ───────────────────────── 时间意图(#78) ─────────────────────────
|
|
24
|
+
// 问「刚才」问的就是最近的,可检索这一层原来听不出来:
|
|
25
|
+
// ① 刷视频的记忆写进来时 importance 恒为 1(lib/browse.js),30 天半衰期还会淡;
|
|
26
|
+
// ② BM25 里「刷」「视频」在几百条记忆里都出现过,idf 几乎为 0,「刚才」「什么」几乎查不到;
|
|
27
|
+
// ③ 于是「你刚才刷了什么视频呀」没有一个词有区分度,排名全由重要度和分层决定 —— 老记忆赢;
|
|
28
|
+
// 刚刷的那几条甚至进不了 BM25 前 50(候选池先被砍掉了,事后加权救不回来)。
|
|
29
|
+
// 这里给近期记忆一条通路:查询带时间词时,窗口内的记忆先进候选池(截断之前),排序时再加权。
|
|
30
|
+
// 只改排序,不造记忆:没对上词的记忆分数就是 0,进不了池,也不会被捞出来。
|
|
31
|
+
const DAY_MS = 86_400_000;
|
|
32
|
+
/**
|
|
33
|
+
* 滚动时间窗:时间词 → 往前看几天。
|
|
34
|
+
* 「刚才 / 刚刚 / 刚」是一两天内,「最近 / 近期」是这几天。
|
|
35
|
+
*/
|
|
36
|
+
const ROLLING_INTENT_DAYS = new Map([
|
|
37
|
+
['刚才', 1], ['刚刚', 1], ['刚刚才', 1], ['方才', 1], ['刚', 1],
|
|
38
|
+
['上次', 3], ['上回', 3],
|
|
39
|
+
['最近', 3], ['近期', 3]
|
|
40
|
+
]);
|
|
41
|
+
/**
|
|
42
|
+
* 问句里的空词:对上它们不算"对上内容"。
|
|
43
|
+
* 「什么」「怎么」在记忆正文里到处都是(真机库里 33 条含「什么」),拿它们当内容词,
|
|
44
|
+
* 谁都能被算成"对上了" —— 「我最近心情怎么样」会被不相干的近期记忆挤掉。
|
|
45
|
+
* 只在时间意图这条通路上排除,不动 segment.js 的停用词表(那会动到所有查询的老排名)。
|
|
46
|
+
*/
|
|
47
|
+
const EMPTY_QUERY_TERMS = new Set([
|
|
48
|
+
'什么', '怎么', '怎样', '咋', '啥', '哪个', '哪些', '为什么', '为啥'
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 指代词与泛动词:命中它们**不代表命中了话题**。
|
|
53
|
+
*
|
|
54
|
+
* 🔴 真机事故:「上次那个梗」里,一条刷视频的记忆因为正文含「那个」被当成了"那个梗",
|
|
55
|
+
* 直接排到第一 —— 问的是梗,答的却是刷视频,等于想起了一件没发生的事。
|
|
56
|
+
*
|
|
57
|
+
* 这些词不算「内容词」(不该让一条记忆因为它们被提上来),
|
|
58
|
+
* 但它们在问句里仍然算数 —— 所以单列一档,不跟疑问空词混在一起。
|
|
59
|
+
* 「说/问/看」这类在 lib/segment.js 的停用词表里已经滤掉了,不重复列。
|
|
60
|
+
*/
|
|
61
|
+
const GENERIC_QUERY_TERMS = new Set([
|
|
62
|
+
'那个', '这个', '这些', '那些', '这样', '那样', '这么', '那么',
|
|
63
|
+
'干', '弄', '事', '东西', '会儿',
|
|
64
|
+
// 疑问代词:问法,不是话题。「谁说的」里的「谁」不该让日记靠它被提上来。
|
|
65
|
+
'谁', '谁的', '哪儿', '哪', '哪里', '多少', '几个', '咋样', '如何',
|
|
66
|
+
// 泛指的说法:「他说的」「我讲的」指的是某句话,不是话题
|
|
67
|
+
'说的', '讲的'
|
|
68
|
+
]);
|
|
69
|
+
/**
|
|
70
|
+
* 时间意图的内容词上限:超过这个数说明这不是一句问话,是一整段转录
|
|
71
|
+
* (整理记忆时把聊天尾巴整段丢进来查,见 lib/index.js 的 related)—— 那种场合不按时间意图重排。
|
|
72
|
+
*/
|
|
73
|
+
const MAX_INTENT_CORE_TERMS = 12;
|
|
74
|
+
/** 一次最多提升多少条近期记忆:免得一整批近期记忆把老记忆的排名全挤到后面。 */
|
|
75
|
+
const MAX_PROMOTED_RECENT = 20;
|
|
76
|
+
/** 窗口内记忆在最终排序里的加权:要压过分层权重 recent 1.1 / longTerm 1.2 的差距。 */
|
|
77
|
+
export const RECENCY_BOOST = 1.5;
|
|
78
|
+
|
|
23
79
|
/** number[] → L2 归一化后的 Float32 小端序 Buffer。 */
|
|
24
80
|
export function normalizeToBlob(vec) {
|
|
25
81
|
let sumSq = 0;
|
|
@@ -160,14 +216,128 @@ export function memoryTierWeight(tier, config = {}) {
|
|
|
160
216
|
const n = Number(config.tierWeights?.[tier] ?? defaults[tier] ?? 1);
|
|
161
217
|
return Number.isFinite(n) ? Math.min(2, Math.max(0.1, n)) : defaults[tier] ?? 1;
|
|
162
218
|
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 时间词 → 时间窗:以「距今多少毫秒」(年龄)表示的区间 [minAge, maxAge]。
|
|
222
|
+
* 刚才 / 刚刚 / 刚:滚动 1 天
|
|
223
|
+
* 最近 / 近期:滚动 3 天
|
|
224
|
+
*
|
|
225
|
+
* 只认「纯指时间」的词。「今天 / 昨天」不认:中文里「今天晚饭」「昨天写的东西」
|
|
226
|
+
* 是拿今天/昨天修饰话题的(今天晚饭吃什么、昨天写的东西在哪),不是「问哪一段时间的记忆」。
|
|
227
|
+
* 把它们当时间窗会把这类话题问句也一起重排,属于误伤。
|
|
228
|
+
* @returns {[number, number]|null}
|
|
229
|
+
*/
|
|
230
|
+
function intentWindow(token) {
|
|
231
|
+
const days = ROLLING_INTENT_DAYS.get(token);
|
|
232
|
+
return days === undefined ? null : [0, days * DAY_MS];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 查询里有没有时间意图(「刚才 / 刚刚 / 最近」这类词),有的话给出时间窗和命中的时间词。
|
|
237
|
+
* 用分词后的词元比对,不用子串:「金刚」不会被当成「刚」。
|
|
238
|
+
*/
|
|
239
|
+
export function recencyIntent(query, now) {
|
|
240
|
+
const windows = [];
|
|
241
|
+
const tokens = new Set();
|
|
242
|
+
for (const token of tokenizeForBm25(query).split(/\s+/)) {
|
|
243
|
+
const w = intentWindow(token);
|
|
244
|
+
if (w === null) continue;
|
|
245
|
+
tokens.add(token);
|
|
246
|
+
for (const ch of token) tokens.add(ch); // 多字时间词在 BM25 查询里还会拆成单字
|
|
247
|
+
windows.push(w);
|
|
248
|
+
}
|
|
249
|
+
return windows.length > 0 ? { windows, tokens } : null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** 记忆有多新(毫秒):以事情发生的时间为准,updated_at 只作兜底;算不出来就当成很老。 */
|
|
253
|
+
function memoryAge(row, now) {
|
|
254
|
+
const at = Number(row?.created_at ?? row?.updated_at);
|
|
255
|
+
if (!Number.isFinite(at)) return Infinity;
|
|
256
|
+
return Math.max(0, now - at);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 这条记忆有没有对上「内容词」(问句里除时间词、空词以外的整词)。
|
|
261
|
+
*
|
|
262
|
+
* 只要对上**一个**具体词就算 —— 问句是「你刚才刷了什么视频」时,
|
|
263
|
+
* 对上「刷」或「视频」就该算数;要求全对上反而会漏(「刷视频」这种组合很少逐字出现)。
|
|
264
|
+
* 泛词(那个/干/事…)已在上游剔除,这里不重复处理。
|
|
265
|
+
*/
|
|
266
|
+
function matchesCore(row, core) {
|
|
267
|
+
if (!core || core.size === 0) return false;
|
|
268
|
+
for (const k of splitKeywords(row.keywords)) if (core.has(k)) return true;
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** 记忆的年龄落在任一时间窗里吗。 */
|
|
273
|
+
function inWindows(age, windows) {
|
|
274
|
+
for (const [min, max] of windows) if (age >= min && age <= max) return true;
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 这条记忆在「带时间词」的查询里该不该被加权:落在时间窗里就加权,否则不加。
|
|
280
|
+
* 只改排序权重,不改记忆本身(不改内容、不改重要度、不新建记忆)。
|
|
281
|
+
*/
|
|
282
|
+
export function recencyWeight(row, now, intent) {
|
|
283
|
+
if (!intent || !Array.isArray(intent.windows) || intent.windows.length === 0) return 1;
|
|
284
|
+
return inWindows(memoryAge(row, now), intent.windows) ? RECENCY_BOOST : 1;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* 时间意图挑中的近期记忆:落在时间窗里、且对上了内容词的。
|
|
289
|
+
* 只对上了时间词的不算 —— 记忆正文里也写着「最近」时,那是话题词(「我最近心情怎么样」),
|
|
290
|
+
* 不是"问最近的",硬提上来只会把不相干的近期记忆塞到前面。
|
|
291
|
+
*/
|
|
292
|
+
function freshSet(scored, rows, intent, core, now) {
|
|
293
|
+
const at = new Map(rows.map((r) => [r.id, r]));
|
|
294
|
+
const ids = new Set();
|
|
295
|
+
for (const s of scored) {
|
|
296
|
+
const row = at.get(s.id);
|
|
297
|
+
if (!inWindows(memoryAge(row, now), intent.windows)) continue;
|
|
298
|
+
// 必须命中**实词**才算对上了这个话题。
|
|
299
|
+
// 泛词(那个/干/事…)不在 core 里 —— 「上次那个梗」的实词是「梗」,
|
|
300
|
+
// 一条只含「那个」的刷视频记忆因此进不来(真机踩过:问梗、答刷视频)。
|
|
301
|
+
if (!matchesCore(row, core)) continue;
|
|
302
|
+
ids.add(s.id);
|
|
303
|
+
}
|
|
304
|
+
return ids;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 时间意图的候选池:挑中的近期记忆提到池子前面(按新到旧,跟 browse.js 瞬态块一个顺序),
|
|
309
|
+
* 老行为的前 50 原样跟在后面 —— 只加不减,谁都没被挤掉。
|
|
310
|
+
* 必须在这一步做 —— BM25 前 50 的截断就在这里,截完再加权救不回已经被砍掉的。
|
|
311
|
+
*/
|
|
312
|
+
function poolWithRecent(scored, rows, now, fresh) {
|
|
313
|
+
const at = new Map(rows.map((r) => [r.id, r]));
|
|
314
|
+
const age = (s) => memoryAge(at.get(s.id), now);
|
|
315
|
+
const promoted = scored.filter((s) => fresh.has(s.id)).sort((a, b) => age(a) - age(b)).slice(0, MAX_PROMOTED_RECENT);
|
|
316
|
+
const promotedIds = new Set(promoted.map((s) => s.id));
|
|
317
|
+
const old = scored.slice(0, 50).filter((s) => !promotedIds.has(s.id));
|
|
318
|
+
return [...promoted, ...old];
|
|
319
|
+
}
|
|
320
|
+
|
|
163
321
|
export function recallSync({ db, clock, config, characterId, ownerId, query, vec, excludePrivate = false, explain = false }) {
|
|
164
322
|
const topK = config.topK ?? 5;
|
|
165
323
|
const rows = loadCandidates(db, characterId, ownerId, { excludePrivate });
|
|
166
324
|
if (rows.length === 0) return explain ? { recalled: [], others: [], topK, vector: vec !== null && vec !== undefined } : [];
|
|
167
325
|
const { terms: queryTerms, weights: queryWeights } = queryForBm25(query);
|
|
168
326
|
const now = clock.now();
|
|
327
|
+
const intent = recencyIntent(query, now);
|
|
328
|
+
// 内容词 = 问句里除时间词、空词以外的整词(单字扩展太弱,不算)
|
|
329
|
+
// 泛词(那个/干/故事…)单列一档:它们不算内容词,但也不是完全无关 ——
|
|
330
|
+
// 一条记忆要是只靠它们沾边,就不算"对上了这个话题"。
|
|
331
|
+
const core = intent === null ? new Set() : new Set(queryTerms.filter((t) =>
|
|
332
|
+
!intent.tokens.has(t) && !EMPTY_QUERY_TERMS.has(t) && !GENERIC_QUERY_TERMS.has(t) && (queryWeights.get(t) ?? 1) >= 1));
|
|
333
|
+
// 只有一句聚焦的问话才按时间意图走:没有内容词(「刚才?」)、内容词太多(整段转录)都按老样子
|
|
334
|
+
const useIntent = core.size > 0 && core.size <= MAX_INTENT_CORE_TERMS;
|
|
169
335
|
// 遗忘曲线放在融合之后(两路都淡),BM25 这里不再单独衰减
|
|
170
|
-
|
|
336
|
+
// 有时间意图时 BM25 扫全量候选再截断,让窗口内的近期记忆进得来(截断前生效,不是事后加权)
|
|
337
|
+
const bm25All = bm25Search(rows, queryTerms, now, useIntent ? rows.length : 50, { decay: false, weights: queryWeights });
|
|
338
|
+
const maxB = bm25All.length > 0 ? bm25All[0].score : 1;
|
|
339
|
+
const fresh = useIntent ? freshSet(bm25All, rows, intent, core, now) : new Set();
|
|
340
|
+
const bm25Top = useIntent ? poolWithRecent(bm25All, rows, now, fresh) : bm25All;
|
|
171
341
|
const bm25Rank = new Map(bm25Top.map((r, i) => [r.id, i + 1]));
|
|
172
342
|
const bm25Score = new Map(bm25Top.map((r) => [r.id, r.score]));
|
|
173
343
|
// 只靠拆出来的单字对上的,RRF 里也按单字的权重算(排名靠前也不能挤掉真正对上的)
|
|
@@ -183,7 +353,6 @@ export function recallSync({ db, clock, config, characterId, ownerId, query, vec
|
|
|
183
353
|
: new Set();
|
|
184
354
|
|
|
185
355
|
const fusionMethod = config.fusionMethod ?? 'rrf';
|
|
186
|
-
const maxB = bm25Top.length > 0 ? bm25Top[0].score : 1;
|
|
187
356
|
const candidates = [];
|
|
188
357
|
for (const row of rows) {
|
|
189
358
|
const br = bm25Rank.get(row.id);
|
|
@@ -205,10 +374,12 @@ export function recallSync({ db, clock, config, characterId, ownerId, query, vec
|
|
|
205
374
|
}
|
|
206
375
|
const tier = memoryTier(row, now);
|
|
207
376
|
const tierWeight = memoryTierWeight(tier, config);
|
|
377
|
+
// 时间意图:问「刚才」的,近期记忆加权排在前面(只影响排序,不新增任何记忆)
|
|
378
|
+
const recency = fresh.has(row.id) ? recencyWeight(row, now, intent) : 1;
|
|
208
379
|
if (explain) {
|
|
209
380
|
const decay = decayFactor(row, now);
|
|
210
|
-
candidates.push([row, score * decay * tierWeight, { bm25Rank: br ?? null, vecRank: vr ?? null, raw: score, decay, tier, tierWeight }]);
|
|
211
|
-
} else if (score > 0) candidates.push([row, score * decayFactor(row, now) * tierWeight]);
|
|
381
|
+
candidates.push([row, score * decay * tierWeight * recency, { bm25Rank: br ?? null, vecRank: vr ?? null, raw: score, decay, tier, tierWeight, recency }]);
|
|
382
|
+
} else if (score > 0) candidates.push([row, score * decayFactor(row, now) * tierWeight * recency]);
|
|
212
383
|
}
|
|
213
384
|
candidates.sort((a, b) => b[1] - a[1]);
|
|
214
385
|
if (explain) {
|
|
@@ -218,7 +389,7 @@ export function recallSync({ db, clock, config, characterId, ownerId, query, vec
|
|
|
218
389
|
const view = ([row, final, d], i) => ({
|
|
219
390
|
id: row.id, content: row.content, kind: row.kind, ownerId: row.owner_id, visibility: row.visibility, importance: row.importance,
|
|
220
391
|
createdAt: row.created_at, rank: final > 0 ? i + 1 : null, score: Math.round(final * 1e5) / 1e5, raw: Math.round(d.raw * 1e5) / 1e5,
|
|
221
|
-
decay: Math.round(d.decay * 100) / 100, tier: d.tier, tierWeight: d.tierWeight, bm25Rank: d.bm25Rank, vecRank: d.vecRank,
|
|
392
|
+
decay: Math.round(d.decay * 100) / 100, tier: d.tier, tierWeight: d.tierWeight, recency: d.recency, bm25Rank: d.bm25Rank, vecRank: d.vecRank,
|
|
222
393
|
why: recalledIds.has(row.id) ? '想起来了'
|
|
223
394
|
: final <= 0 ? (vecActive ? '关键词和意思都对不上' : '关键词对不上(向量没开 / 还没算好)')
|
|
224
395
|
: d.decay < 0.3 ? `排第 ${i + 1},太久没提、淡了(×${Math.round(d.decay * 100) / 100})` : `排第 ${i + 1},只取前 ${topK} 条`
|
package/lib/routes.js
CHANGED
|
@@ -567,6 +567,7 @@ export function registerRoutes(ctx, deps) {
|
|
|
567
567
|
followUpCount: q.followUpCount ?? 3,
|
|
568
568
|
followUpSeconds: q.followUpSeconds ?? 120,
|
|
569
569
|
followUpSmart: q.followUpSmart === true,
|
|
570
|
+
mentionOnly: q.mentionOnly === true,
|
|
570
571
|
// 回显:设置页的数字框要能读回来,否则改完刷新又弹回去(写进去读不出来)
|
|
571
572
|
replyWindowMinutes: clampReplyWindow(q.replyWindowMinutes),
|
|
572
573
|
batchMs: q.batchMs ?? 3000,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-fish-soul",
|
|
3
|
-
"version": "0.18.
|
|
3
|
+
"version": "0.18.5",
|
|
4
4
|
"description": "鱼 · 人格、按人记忆(可纠正、有印象、能导入旧聊天记录)、日程自走与主动开口,直连 SnowLuma 收发 QQ(群里会自己插话),B站 / 抖音小号私信与自己刷视频;按功能选模型并统计用量花费,能听会说 QQ 语音、收藏表情包、学群里的梗,可选上网查;心情影响说话、去 AI 味、每个群的设定、按聊天选模型、每晚写日记、每周记忆存档与可选周记(DSH 插件,单角色 feiyu ,v0.15.37)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -64,6 +64,6 @@
|
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
|
-
"test": "node test/memory-bulk-move.mjs && node test/safe-download.mjs && node test/douyin-exact-id.mjs && node test/douyin-pb-init.mjs && node test/douyin-conversation.mjs && node test/douyin-inbox.mjs && node test/douyin-quarantine.mjs && node test/douyin-send-target.mjs && node test/douyin-send-exclusive.mjs && node test/douyin-gap-dedupe.mjs && node test/douyin-message-id-dedupe.mjs && node test/douyin-read-fail-retry.mjs && node test/douyin-read-cache.mjs && node test/douyin-dm-media-conv.mjs && node test/douyin-read-fail-retry-poll.mjs && node test/douyin-inbox-contract.mjs && node test/person-attribution.mjs && node test/memory-owner-reassign.mjs && node test/douyin-poll-and-timer-safety.mjs && node test/p2-limits.mjs && node test/resource-safety.mjs && node test/leftover-edge-cases.mjs && node test/douyin-master-person-merge.mjs && node test/douyin-master-uid.mjs && node test/douyin-identity-e2e.mjs && node test/douyin-post-jobs.mjs && node test/retrieval-filtered-vectors.mjs && node test/security-boundaries.mjs && node test/douyin-cdp-cursor.mjs && node test/douyin-nav-timeout.mjs && node test/persona-isolation.mjs && node test/smoke.mjs && node test/douyin-browser.mjs && node test/douyin-page-alive.mjs && node test/douyin-eval-timeout.mjs && node test/douyin-bridge-timeout.mjs && node test/douyin-goto-budget.mjs && node test/douyin-music.mjs && node test/startup-gate.mjs && node test/outbox.mjs && node test/import-coverage.mjs && node test/update-check.mjs && node test/issue-fixes.mjs && node test/import-resume.mjs && node test/maibot-extras.mjs && node test/worldinfo.mjs && node test/memory-bulk.mjs && node test/memory-tiers.mjs && node test/memory-weekly.mjs && node test/issue-98-self-archive.mjs && node test/issue-99-memory-order.mjs && node test/issue-115-memory-date.mjs && node test/issue-119-memory-page.mjs && node test/issue-125-memory-filter.mjs && node test/issue-78-diagnostic.mjs && node test/memory-storage.mjs && node test/memory-storage-restore.mjs && node test/backup-restore.mjs && node test/douyin-group.mjs && node test/review-fixes.mjs && node test/at-reads-video.mjs && node test/search-source.mjs && node test/followups.mjs && node test/detail-config.mjs && node test/qq-send-any-image.mjs && node test/qq-read-attachment.mjs && node test/qq-batch-window.mjs && node test/comment-video.mjs && node test/frames.mjs && node test/face-name.mjs && node test/comment-reply.mjs && node test/chat-comment.mjs && node test/at-target.mjs && node test/quota-caps.mjs && node test/client-lint.mjs && node test/client-panels.mjs && node test/dsh-desktop.mjs && node test/group-mention-only.mjs && node test/qq-private-allowlist.mjs && node test/issue-59-interject.mjs && node test/issue-59-diagnose.mjs && node test/sticker-backfill.mjs && node test/issue-118-sticker-diagnostics.mjs && node test/sticker-manage.mjs && node test/bili-emotes.mjs && node test/qq-outbound-sanitize.mjs && node test/person-delete.mjs && node test/qq-group-admin.mjs && node test/douyin-recovery.mjs && node test/session-preset.mjs && node test/qq-connect-error.mjs && node test/douyin-comment-bridge.mjs && node test/douyin-comment-write-unknown.mjs && node test/douyin-reply-verify-status.mjs && node test/bili-comment-emoji.mjs && node test/douyin-state-echo.mjs && node test/issue-75-proactive.mjs && node test/issue-76-capability.mjs && node test/douyin-http-browse.mjs && node test/douyin-verify-backoff.mjs && node test/douyin-digg-bridge.mjs && node test/memory-evidence-transcript.mjs && node test/chat-archive-default.mjs && node test/issue-113-desktop-archive.mjs && node test/dream-transcript-wire.mjs && node test/douyin-frames-collect.mjs && node test/master-qq-uin-rebind.mjs && node test/douyin-session-health.mjs && node test/douyin-delivered.mjs && node test/douyin-card-share.mjs && node test/douyin-push-drop.mjs && node test/douyin-stranger-dm.mjs && node test/douyin-uid-binding.mjs && node test/image-webp-fallback.mjs && node test/douyin-emoji-fallback.mjs && node test/relay-other-platform.mjs && node test/issue-101-voice-files.mjs && node test/voice-volcengine.mjs && node test/voice-minimax-dashscope.mjs && node test/bili-switch-persist.mjs && node test/cross-channel-context.mjs && node test/reply-target.mjs && node test/shared-session.mjs && node test/memory-source-label.mjs && node test/pending-delivery.mjs && node test/recall-window-78.mjs && node test/client-syntax.mjs && node test/diag-log.mjs && node test/check-rings.mjs && node test/release-workflow.mjs && node test/sticker-import-gif.mjs && node test/sticker-real-gif.mjs && node test/sticker-upload-binary.mjs && node test/sticker-vision-tags.mjs && node test/sticker-manual-tags.mjs && node test/sticker-auto-name.mjs && node test/self-update.mjs && node test/group-list.mjs && node test/douyin-inbox-noise.mjs && node test/douyin-inbox-diag.mjs && node test/douyin-inbox-stuck.mjs && node test/reply-window.mjs && node test/douyin-sticker-heuristic.mjs && node test/douyin-image-boundary.mjs && node test/media-image-boundary.mjs && node test/douyin-comment-identity-cid.mjs"
|
|
67
|
+
"test": "node test/memory-bulk-move.mjs && node test/safe-download.mjs && node test/douyin-exact-id.mjs && node test/douyin-pb-init.mjs && node test/douyin-conversation.mjs && node test/douyin-inbox.mjs && node test/douyin-quarantine.mjs && node test/douyin-send-target.mjs && node test/douyin-send-exclusive.mjs && node test/douyin-gap-dedupe.mjs && node test/douyin-message-id-dedupe.mjs && node test/douyin-read-fail-retry.mjs && node test/douyin-read-cache.mjs && node test/douyin-dm-media-conv.mjs && node test/douyin-read-fail-retry-poll.mjs && node test/douyin-inbox-contract.mjs && node test/person-attribution.mjs && node test/memory-owner-reassign.mjs && node test/douyin-poll-and-timer-safety.mjs && node test/p2-limits.mjs && node test/resource-safety.mjs && node test/leftover-edge-cases.mjs && node test/douyin-master-person-merge.mjs && node test/douyin-master-uid.mjs && node test/douyin-identity-e2e.mjs && node test/douyin-post-jobs.mjs && node test/retrieval-filtered-vectors.mjs && node test/security-boundaries.mjs && node test/douyin-cdp-cursor.mjs && node test/douyin-nav-timeout.mjs && node test/persona-isolation.mjs && node test/smoke.mjs && node test/douyin-browser.mjs && node test/douyin-page-alive.mjs && node test/douyin-eval-timeout.mjs && node test/douyin-bridge-timeout.mjs && node test/douyin-goto-budget.mjs && node test/douyin-music.mjs && node test/startup-gate.mjs && node test/outbox.mjs && node test/import-coverage.mjs && node test/update-check.mjs && node test/issue-fixes.mjs && node test/import-resume.mjs && node test/maibot-extras.mjs && node test/worldinfo.mjs && node test/memory-bulk.mjs && node test/memory-tiers.mjs && node test/memory-weekly.mjs && node test/issue-98-self-archive.mjs && node test/issue-99-memory-order.mjs && node test/issue-115-memory-date.mjs && node test/issue-119-memory-page.mjs && node test/issue-125-memory-filter.mjs && node test/issue-78-diagnostic.mjs && node test/memory-storage.mjs && node test/memory-storage-restore.mjs && node test/backup-restore.mjs && node test/douyin-group.mjs && node test/review-fixes.mjs && node test/at-reads-video.mjs && node test/search-source.mjs && node test/followups.mjs && node test/detail-config.mjs && node test/qq-send-any-image.mjs && node test/qq-read-attachment.mjs && node test/qq-batch-window.mjs && node test/comment-video.mjs && node test/frames.mjs && node test/face-name.mjs && node test/comment-reply.mjs && node test/chat-comment.mjs && node test/at-target.mjs && node test/quota-caps.mjs && node test/client-lint.mjs && node test/client-panels.mjs && node test/dsh-desktop.mjs && node test/group-mention-only.mjs && node test/qq-private-allowlist.mjs && node test/issue-59-interject.mjs && node test/issue-59-diagnose.mjs && node test/sticker-backfill.mjs && node test/issue-118-sticker-diagnostics.mjs && node test/sticker-manage.mjs && node test/bili-emotes.mjs && node test/qq-outbound-sanitize.mjs && node test/person-delete.mjs && node test/qq-group-admin.mjs && node test/douyin-recovery.mjs && node test/session-preset.mjs && node test/qq-connect-error.mjs && node test/douyin-comment-bridge.mjs && node test/douyin-comment-write-unknown.mjs && node test/douyin-reply-verify-status.mjs && node test/bili-comment-emoji.mjs && node test/douyin-state-echo.mjs && node test/issue-75-proactive.mjs && node test/issue-76-capability.mjs && node test/douyin-http-browse.mjs && node test/douyin-verify-backoff.mjs && node test/douyin-digg-bridge.mjs && node test/memory-evidence-transcript.mjs && node test/chat-archive-default.mjs && node test/issue-113-desktop-archive.mjs && node test/dream-transcript-wire.mjs && node test/douyin-frames-collect.mjs && node test/master-qq-uin-rebind.mjs && node test/douyin-session-health.mjs && node test/douyin-delivered.mjs && node test/douyin-card-share.mjs && node test/douyin-push-drop.mjs && node test/douyin-stranger-dm.mjs && node test/douyin-uid-binding.mjs && node test/image-webp-fallback.mjs && node test/douyin-emoji-fallback.mjs && node test/relay-other-platform.mjs && node test/issue-101-voice-files.mjs && node test/voice-volcengine.mjs && node test/voice-minimax-dashscope.mjs && node test/bili-switch-persist.mjs && node test/cross-channel-context.mjs && node test/reply-target.mjs && node test/shared-session.mjs && node test/memory-source-label.mjs && node test/pending-delivery.mjs && node test/recall-window-78.mjs && node test/memory-recall-recent-78.mjs && node test/client-syntax.mjs && node test/diag-log.mjs && node test/check-rings.mjs && node test/release-workflow.mjs && node test/sticker-import-gif.mjs && node test/sticker-real-gif.mjs && node test/sticker-upload-binary.mjs && node test/sticker-vision-tags.mjs && node test/sticker-manual-tags.mjs && node test/sticker-auto-name.mjs && node test/self-update.mjs && node test/group-list.mjs && node test/douyin-inbox-noise.mjs && node test/douyin-inbox-diag.mjs && node test/douyin-inbox-stuck.mjs && node test/reply-window.mjs && node test/douyin-sticker-heuristic.mjs && node test/douyin-image-boundary.mjs && node test/media-image-boundary.mjs && node test/douyin-comment-identity-cid.mjs && node test/qq-mention-only-echo.mjs"
|
|
68
68
|
}
|
|
69
69
|
}
|