@modusensus/dsh-mneme 0.4.7 → 0.5.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.
- package/README.md +38 -5
- package/lib/api.js +117 -1
- package/lib/client.js +1013 -216
- package/lib/config.js +30 -1
- package/lib/dream.js +41 -12
- package/lib/hot-memory.js +53 -0
- package/lib/inject.js +84 -3
- package/lib/local-embedder.js +7 -1
- package/lib/reranker.js +4 -1
- package/lib/search/adaptive.js +22 -0
- package/lib/search/bm25.js +96 -0
- package/lib/service.js +138 -9
- package/lib/store.js +24 -0
- package/package.json +9 -1
- package/scripts/benchmark-recall.js +133 -0
- package/scripts/sync-lib.js +7 -2
- package/src/api.js +117 -1
- package/src/config.js +30 -1
- package/src/dream.js +41 -12
- package/src/hot-memory.js +53 -0
- package/src/inject.js +84 -3
- package/src/local-embedder.js +7 -1
- package/src/reranker.js +4 -1
- package/src/search/adaptive.js +22 -0
- package/src/search/bm25.js +96 -0
- package/src/service.js +138 -9
- package/src/store.js +24 -0
- package/test/benchmark.test.js +35 -0
- package/test/client.test.js +205 -15
- package/test/graph-api.test.js +175 -0
- package/test/hot-memory.test.js +174 -0
- package/test/reasoning-effort.test.js +1 -1
- package/test/recall-layer.test.js +2 -2
- package/test/reranker.test.js +43 -0
- package/test/search-fusion.test.js +90 -0
- package/test/service-search.test.js +31 -2
package/lib/config.js
CHANGED
|
@@ -17,7 +17,7 @@ export const Config = z.object({
|
|
|
17
17
|
dreamDelayMs: z.natural().min(0).max(60000).default(2000),
|
|
18
18
|
dreamProvider: z.string(),
|
|
19
19
|
dreamModel: z.string(),
|
|
20
|
-
dreamMaxTokens: z.natural().min(256).max(131072).default(
|
|
20
|
+
dreamMaxTokens: z.natural().min(256).max(131072).default(8192),
|
|
21
21
|
// Pass-through reasoning effort for dream's LLM calls. 'none' (default)
|
|
22
22
|
// omits the field so the provider's own default applies; low/medium/high
|
|
23
23
|
// are forwarded verbatim. Useful to cap reasoning spend on thinking-type
|
|
@@ -92,6 +92,35 @@ export const Config = z.object({
|
|
|
92
92
|
// rule-based pick to fill/dedupe. Empty query / no vector → legacy behavior.
|
|
93
93
|
hybridInject: z.boolean().default(true),
|
|
94
94
|
|
|
95
|
+
// --- recall optimization (v0.5.0) ----------------------------------------
|
|
96
|
+
// BM25 third recall path beside vector + LIKE keyword (1.1): per-token IDF
|
|
97
|
+
// scoring recalls rows whose query terms are scattered — identifiers, code
|
|
98
|
+
// fragments, mixed CJK/ASCII — where substring LIKE cannot match.
|
|
99
|
+
bm25SearchEnabled: z.boolean().default(true),
|
|
100
|
+
// Query-aware vector cutoff (1.2) replacing the fixed 0.65: entity:/attr:
|
|
101
|
+
// prefixes loosen to 0.5, short queries tighten to 0.7, long queries loosen
|
|
102
|
+
// to 0.6, and a decisive top-1/top-5 score gap loosens to 0.5 so the tail
|
|
103
|
+
// still reaches the reranker. Off = legacy fixed threshold behavior.
|
|
104
|
+
adaptiveThresholdEnabled: z.boolean().default(true),
|
|
105
|
+
// Session-scoped hot memory (1.3): the latest N dialogue rounds rendered
|
|
106
|
+
// ahead of the long-term recall block — short-term context that never
|
|
107
|
+
// enters the memory store.
|
|
108
|
+
hotMemoryEnabled: z.boolean().default(true),
|
|
109
|
+
hotMemoryRounds: z.natural().min(1).max(50).default(5),
|
|
110
|
+
hotMemoryMaxTokens: z.natural().min(200).max(32000).default(2000),
|
|
111
|
+
// Topic-ranked injection (2.2): when a query vector is available the whole
|
|
112
|
+
// injection candidate list is re-ordered by similarity to the current
|
|
113
|
+
// query instead of keeping the rule-based order.
|
|
114
|
+
selectiveInjectEnabled: z.boolean().default(true),
|
|
115
|
+
// Search-time semantic dedup (2.3): greedy pass over the merged candidate
|
|
116
|
+
// list dropping rows whose embedding cosine-similarity to an already-kept
|
|
117
|
+
// row exceeds the threshold — duplicates are filtered at recall time
|
|
118
|
+
// instead of waiting for a dream consolidation. Opt-in aggressive mode:
|
|
119
|
+
// small embedding models can collapse legitimately distinct rows, so the
|
|
120
|
+
// default keeps every recalled row.
|
|
121
|
+
searchSemanticDedup: z.boolean().default(false),
|
|
122
|
+
searchSemanticDedupThreshold: z.number().min(0.5).max(1).default(0.95),
|
|
123
|
+
|
|
95
124
|
// --- semantic: rerank layer (v0.2) --------------------------------------
|
|
96
125
|
// Opt-in by default (item ⑥): the local cross-encoder pulls in onnxruntime
|
|
97
126
|
// (transformers.js) at init, so a bare install must not load it. Only an
|
package/lib/dream.js
CHANGED
|
@@ -3,6 +3,43 @@ import { clusterMemories, findPotentialConflicts } from "./dream/clustering.js";
|
|
|
3
3
|
import { createHash, randomUUID } from "node:crypto";
|
|
4
4
|
export { validateDecisions, applyDecisions };
|
|
5
5
|
|
|
6
|
+
|
|
7
|
+
// Extract the first JSON array from LLM output, tolerating markdown fences,
|
|
8
|
+
// leading/trailing prose, and common wrapper noise. Returns an array or null.
|
|
9
|
+
function extractJsonArray(text) {
|
|
10
|
+
if (typeof text !== "string" || text.trim().length === 0) return null;
|
|
11
|
+
|
|
12
|
+
// 1. Strip markdown code fences (```json ... ``` or ``` ... ```).
|
|
13
|
+
let cleaned = text.replace(/```(?:json)?\s*([\s\S]*?)```/gi, "$1");
|
|
14
|
+
cleaned = cleaned.trim();
|
|
15
|
+
|
|
16
|
+
// 2. Find the first '[' and the matching last ']' that yields valid JSON.
|
|
17
|
+
const start = cleaned.indexOf("[");
|
|
18
|
+
if (start === -1) return null;
|
|
19
|
+
for (let end = cleaned.lastIndexOf("]"); end > start; end = cleaned.lastIndexOf("]", end - 1)) {
|
|
20
|
+
const candidate = cleaned.slice(start, end + 1);
|
|
21
|
+
try {
|
|
22
|
+
return JSON.parse(candidate);
|
|
23
|
+
} catch {
|
|
24
|
+
// Light repair: remove trailing commas before ] or }.
|
|
25
|
+
try {
|
|
26
|
+
const repaired = candidate.replace(/,(\s*[}\]])/g, "$1");
|
|
27
|
+
return JSON.parse(repaired);
|
|
28
|
+
} catch {
|
|
29
|
+
// keep searching backwards
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 3. Fallback: a broader regex extraction.
|
|
35
|
+
try {
|
|
36
|
+
const match = cleaned.match(/\[[\s\S]*\]/);
|
|
37
|
+
if (match) return JSON.parse(match[0]);
|
|
38
|
+
} catch {
|
|
39
|
+
// fall through
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
6
43
|
const SUMMARY_PROMPT = `你是记忆库摘要助手。根据整理后的记忆,生成一段 150-200 字的记忆库总览,覆盖:用户偏好、活跃项目、关键决策。之后作为会话上下文注入。只输出摘要文本,不要其他内容。`;
|
|
7
44
|
|
|
8
45
|
const CONSOLIDATION_PROMPT = `你是记忆库整理助手。下面是全部记忆条目(id、类型、标题、内容、重要性、更新时间)。
|
|
@@ -579,18 +616,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
579
616
|
return finish({ ok: false, error: "llm failed", summary: false });
|
|
580
617
|
}
|
|
581
618
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
if (start === -1 || end <= start) {
|
|
587
|
-
logger?.warn?.("dsh-mneme dream: no json array in llm output");
|
|
588
|
-
return finish({ ok: false, error: "no json array in llm output", summary: false });
|
|
589
|
-
}
|
|
590
|
-
decisions = JSON.parse(decisionText.slice(start, end + 1));
|
|
591
|
-
} catch {
|
|
592
|
-
logger?.warn?.("dsh-mneme dream: invalid decisions json");
|
|
593
|
-
return finish({ ok: false, error: "invalid decisions json", summary: false });
|
|
619
|
+
const decisions = extractJsonArray(decisionText);
|
|
620
|
+
if (!Array.isArray(decisions)) {
|
|
621
|
+
logger?.warn?.(`dsh-mneme dream: no json array in llm output (raw length ${decisionText?.length ?? 0})`);
|
|
622
|
+
return finish({ ok: false, error: "no json array in llm output", summary: false });
|
|
594
623
|
}
|
|
595
624
|
const { ok, errors } = validateDecisions(decisions, snapshot, {
|
|
596
625
|
maxUpdatePerRun: config.reflectionUpdateMaxPerRun,
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Session-scoped hot memory (v0.5.0 召回率优化 1.3): a short-term buffer of
|
|
2
|
+
// the latest dialogue rounds, kept strictly apart from the long-term memory
|
|
3
|
+
// store. The injector renders it ahead of the long-term recall block so the
|
|
4
|
+
// agent sees "what we were just talking about" without those rounds ever
|
|
5
|
+
// being persisted as memories. Bounded two ways: maxRounds (count) and
|
|
6
|
+
// maxTokens (budget) — whichever evicts first.
|
|
7
|
+
|
|
8
|
+
// CJK-aware token estimate: one Chinese character ≈ 0.6 tokens (clustering
|
|
9
|
+
// behavior of mainstream tokenizers), one ASCII char ≈ 0.25.
|
|
10
|
+
export function estimateTokens(text) {
|
|
11
|
+
const s = String(text ?? "");
|
|
12
|
+
let cjk = 0;
|
|
13
|
+
for (const ch of s) if (ch >= "\u4e00" && ch <= "\u9fff") cjk++;
|
|
14
|
+
return Math.ceil(cjk * 0.6 + (s.length - cjk) * 0.25);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {{maxRounds?: number, maxTokens?: number}} opts
|
|
19
|
+
* @returns {{add(round: {query: string, response?: string}): void,
|
|
20
|
+
* getContext(): string,
|
|
21
|
+
* rounds(): Array, clear(): void}}
|
|
22
|
+
*/
|
|
23
|
+
export function createHotMemory({ maxRounds = 5, maxTokens = 2000 } = {}) {
|
|
24
|
+
// Entry defense: a non-positive or non-integer maxRounds (0, -1, 1.5, NaN,
|
|
25
|
+
// null, "2") would make the eviction while-loop unbounded — the buffer can
|
|
26
|
+
// never shrink below `buffer.length > maxRounds`, so `add` would spin forever.
|
|
27
|
+
// Fall back to the defaults so a hostile/buggy caller can never wedge the
|
|
28
|
+
// hot-memory buffer in an infinite loop.
|
|
29
|
+
maxRounds = (Number.isInteger(maxRounds) && maxRounds > 0) ? maxRounds : 5;
|
|
30
|
+
maxTokens = (Number.isFinite(maxTokens) && maxTokens > 0) ? maxTokens : 2000;
|
|
31
|
+
const buffer = [];
|
|
32
|
+
|
|
33
|
+
function totalTokens() {
|
|
34
|
+
return buffer.reduce(
|
|
35
|
+
(sum, r) => sum + estimateTokens(`Q: ${r.query}\nA: ${r.response ?? ""}`),
|
|
36
|
+
0
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
add(round) {
|
|
42
|
+
if (!round?.query) return;
|
|
43
|
+
buffer.push({ query: String(round.query), response: String(round.response ?? "") });
|
|
44
|
+
while (buffer.length > maxRounds) buffer.shift();
|
|
45
|
+
while (buffer.length > 1 && totalTokens() > maxTokens) buffer.shift();
|
|
46
|
+
},
|
|
47
|
+
getContext() {
|
|
48
|
+
return buffer.map((r) => `Q: ${r.query}\nA: ${r.response ?? ""}`).join("\n\n");
|
|
49
|
+
},
|
|
50
|
+
rounds: () => [...buffer],
|
|
51
|
+
clear() { buffer.length = 0; }
|
|
52
|
+
};
|
|
53
|
+
}
|
package/lib/inject.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createHotMemory } from "./hot-memory.js";
|
|
2
|
+
|
|
1
3
|
// Best-effort extraction of the current user's latest message text from the
|
|
2
4
|
// live session, for semantic-first injection (Bug4). The system-prompt
|
|
3
5
|
// interpolator renders synchronously, so this walks the already-materialized
|
|
@@ -25,6 +27,50 @@ function lastUserQuery(ctx) {
|
|
|
25
27
|
return "";
|
|
26
28
|
}
|
|
27
29
|
|
|
30
|
+
// Hot-memory round extraction (v0.5.0 1.3): pairs each user/message with the
|
|
31
|
+
// next assistant reply from the materialized session log. Tolerates shapes
|
|
32
|
+
// where assistant events carry a different type tag — anything whose payload
|
|
33
|
+
// has content parts and is not a user message counts as a reply. Best-effort:
|
|
34
|
+
// returns [] on any failure, and the hot block simply does not render.
|
|
35
|
+
function extractRounds(ctx, maxRounds) {
|
|
36
|
+
try {
|
|
37
|
+
const events = ctx?.agent?.session?.events;
|
|
38
|
+
if (!Array.isArray(events) || events.length === 0) return [];
|
|
39
|
+
const rounds = [];
|
|
40
|
+
let pendingQuery = null;
|
|
41
|
+
const textOf = (event) => {
|
|
42
|
+
const parts = event?.data?.content;
|
|
43
|
+
if (!Array.isArray(parts)) return "";
|
|
44
|
+
return parts
|
|
45
|
+
.map((p) => (typeof p === "string" ? p : p?.text ?? ""))
|
|
46
|
+
.filter(Boolean)
|
|
47
|
+
.join("\n")
|
|
48
|
+
.trim();
|
|
49
|
+
};
|
|
50
|
+
for (const event of events) {
|
|
51
|
+
const kind = event?.data?.source?.kind;
|
|
52
|
+
const isUser = event?.type === "user/message" && (kind === undefined || kind === "user");
|
|
53
|
+
if (isUser) {
|
|
54
|
+
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
|
|
55
|
+
pendingQuery = textOf(event).slice(0, 500);
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
// Only assistant-originated events close a round; tool/system events
|
|
59
|
+
// carrying text must not be mistaken for the model's reply.
|
|
60
|
+
const isAssistant = typeof event?.type === "string" && event.type.includes("assistant")
|
|
61
|
+
|| kind === "assistant";
|
|
62
|
+
const body = isAssistant ? textOf(event) : "";
|
|
63
|
+
if (!body || !pendingQuery) continue;
|
|
64
|
+
rounds.push({ query: pendingQuery, response: body.slice(0, 800) });
|
|
65
|
+
pendingQuery = null;
|
|
66
|
+
}
|
|
67
|
+
if (pendingQuery) rounds.push({ query: pendingQuery, response: "" });
|
|
68
|
+
return rounds.slice(-maxRounds);
|
|
69
|
+
} catch {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
28
74
|
export function createInjector(ctx, service, settings, config) {
|
|
29
75
|
const maxItems = config.maxInjectedItems ?? 5;
|
|
30
76
|
const threshold = config.importanceThreshold ?? 3;
|
|
@@ -36,6 +82,35 @@ export function createInjector(ctx, service, settings, config) {
|
|
|
36
82
|
const MAX_CONTENT = 300;
|
|
37
83
|
const MAX_BLOCK = 1500;
|
|
38
84
|
|
|
85
|
+
// Compressed injection (v0.5.0 2.1): a sleep-demoted row already carries its
|
|
86
|
+
// summary in `content` with the original parked in `_full_content` — inject
|
|
87
|
+
// the summary verbatim instead of re-truncating the (already short) text.
|
|
88
|
+
// Regular long rows keep the hard truncate.
|
|
89
|
+
function injectMemory(m, maxLength = MAX_CONTENT) {
|
|
90
|
+
if (m?._full_content) return String(m.content ?? "");
|
|
91
|
+
const text = String(m?.content ?? "");
|
|
92
|
+
return text.length <= maxLength ? text : `${text.slice(0, maxLength)}…`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Hot memory (v0.5.0 1.3): the latest rounds of THIS session, rebuilt from
|
|
96
|
+
// the materialized event log on every render — stateless, so it survives
|
|
97
|
+
// session switches and never persists anywhere.
|
|
98
|
+
const hot = createHotMemory({
|
|
99
|
+
maxRounds: config.hotMemoryRounds ?? 5,
|
|
100
|
+
maxTokens: config.hotMemoryMaxTokens ?? 2000
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
function renderHotContext(ctx) {
|
|
104
|
+
if (config.hotMemoryEnabled === false) return "";
|
|
105
|
+
const rounds = extractRounds(ctx, config.hotMemoryRounds ?? 5);
|
|
106
|
+
if (!rounds.length) return "";
|
|
107
|
+
hot.clear();
|
|
108
|
+
for (const r of rounds) hot.add(r);
|
|
109
|
+
const body = hot.getContext();
|
|
110
|
+
if (!body) return "";
|
|
111
|
+
return `[短期上下文] 最近对话(共 ${rounds.length} 轮):\n${body}`;
|
|
112
|
+
}
|
|
113
|
+
|
|
39
114
|
function render(candidates) {
|
|
40
115
|
if (!candidates.length) return "";
|
|
41
116
|
const header = "[记忆库] 来自 dsh-mneme 的跨会话记忆(用户偏好与高优先级项目/决策):";
|
|
@@ -48,8 +123,7 @@ export function createInjector(ctx, service, settings, config) {
|
|
|
48
123
|
? "[verified] "
|
|
49
124
|
: "";
|
|
50
125
|
const title = `${m.title}(重要性 ${m.importance})`;
|
|
51
|
-
|
|
52
|
-
if (content.length > MAX_CONTENT) content = `${content.slice(0, MAX_CONTENT)}…`;
|
|
126
|
+
const content = injectMemory(m);
|
|
53
127
|
const full = `- [${m.type}] ${verified}${title}:${content}`;
|
|
54
128
|
if (budget - full.length >= 0) {
|
|
55
129
|
lines.push(full);
|
|
@@ -108,7 +182,14 @@ export function createInjector(ctx, service, settings, config) {
|
|
|
108
182
|
if (query) prefetchQueryVector(query);
|
|
109
183
|
const queryVector = queryVectorCache.get(query);
|
|
110
184
|
const candidates = service.injectCandidates({ query, queryVector, maxItems, threshold });
|
|
111
|
-
|
|
185
|
+
// Hot memory (v0.5.0 1.3) leads the single memory block: the agent
|
|
186
|
+
// sees the short-term rounds first, then the cross-session recall —
|
|
187
|
+
// the documented injection order 1→2. Folding it here (instead of a
|
|
188
|
+
// separate context) keeps the prompt assembly stable at two blocks.
|
|
189
|
+
const hotText = renderHotContext(ctx);
|
|
190
|
+
const body = render(candidates);
|
|
191
|
+
if (!hotText) return body;
|
|
192
|
+
return body ? `${hotText}\n\n${body}` : hotText;
|
|
112
193
|
}
|
|
113
194
|
}),
|
|
114
195
|
ctx.systemPrompt.context({
|
package/lib/local-embedder.js
CHANGED
|
@@ -22,7 +22,13 @@ function modelHash(model) {
|
|
|
22
22
|
|
|
23
23
|
/** Lazy default loader: dynamic import keeps module load cheap. */
|
|
24
24
|
async function defaultPipelineLoader(task, model, options) {
|
|
25
|
-
const { pipeline } = await import("@huggingface/transformers");
|
|
25
|
+
const { env, pipeline } = await import("@huggingface/transformers");
|
|
26
|
+
// issue #13: transformers.js's get_tokenizer_files() drops the caller's
|
|
27
|
+
// cache_dir when it pre-checks tokenizer_config.json metadata, so the HEAD
|
|
28
|
+
// request falls back to env.cacheDir and hits the network even when the
|
|
29
|
+
// model is fully cached locally. Mirroring the cache_dir onto env.cacheDir
|
|
30
|
+
// makes that pre-check resolve locally too — fully offline loading.
|
|
31
|
+
if (options?.cache_dir) env.cacheDir = options.cache_dir;
|
|
26
32
|
return pipeline(task, model, options);
|
|
27
33
|
}
|
|
28
34
|
|
package/lib/reranker.js
CHANGED
|
@@ -21,7 +21,10 @@ function modelHash(model) {
|
|
|
21
21
|
|
|
22
22
|
/** Lazy default pipeline factory: dynamic import keeps module load cheap. */
|
|
23
23
|
async function defaultPipelineLoader(task, model, options) {
|
|
24
|
-
const { pipeline } = await import("@huggingface/transformers");
|
|
24
|
+
const { env, pipeline } = await import("@huggingface/transformers");
|
|
25
|
+
// issue #13: mirror cache_dir onto env.cacheDir so the tokenizer pre-check
|
|
26
|
+
// resolves locally too (same fix as local-embedder.js).
|
|
27
|
+
if (options?.cache_dir) env.cacheDir = options.cache_dir;
|
|
25
28
|
return pipeline(task, model, options);
|
|
26
29
|
}
|
|
27
30
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Adaptive vector threshold (v0.5.0 召回率优化 1.2): replaces the fixed
|
|
2
|
+
// vectorSearchThreshold=0.65 with a query-aware cutoff.
|
|
3
|
+
// entity:/attr: prefixes → 0.5 (entity recall is name-driven; loosen)
|
|
4
|
+
// very short queries → 0.7 (<5 chars match almost anything; tighten)
|
|
5
|
+
// very long queries → 0.6 (semantically specific; loosen a little)
|
|
6
|
+
// head-gap rule → when the top-1 vs top-5 candidate gap exceeds
|
|
7
|
+
// 0.3 the head is decisive — loosen to 0.5 so
|
|
8
|
+
// the tail still reaches the reranker
|
|
9
|
+
// otherwise → 0.65 (the legacy default)
|
|
10
|
+
// Pure and total: same inputs, same cutoff, no store access.
|
|
11
|
+
export function adaptiveThreshold(query, candidates = []) {
|
|
12
|
+
const q = String(query ?? "");
|
|
13
|
+
if (q.startsWith("entity:") || q.startsWith("attr:")) return 0.5;
|
|
14
|
+
if (q.length > 0 && q.length < 5) return 0.7;
|
|
15
|
+
if (q.length > 50) return 0.6;
|
|
16
|
+
const scores = (Array.isArray(candidates) ? candidates : [])
|
|
17
|
+
.map((c) => (typeof c?._score === "number" ? c._score : typeof c?.score === "number" ? c.score : 0))
|
|
18
|
+
.filter((s) => s > 0)
|
|
19
|
+
.sort((a, b) => b - a);
|
|
20
|
+
if (scores.length >= 5 && scores[0] - scores[4] > 0.3) return 0.5;
|
|
21
|
+
return 0.65;
|
|
22
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// BM25 sparse retrieval (v0.5.0 召回率优化 1.1): the third recall path beside
|
|
2
|
+
// vector search and the LIKE keyword scan. The LIKE path only matches full
|
|
3
|
+
// substrings, so a multi-term query ("rust 异步 tokio") misses rows whose
|
|
4
|
+
// terms are scattered. BM25 scores per-token overlap with IDF weighting,
|
|
5
|
+
// which is exactly the gap: identifiers, code fragments and mixed CJK/ASCII
|
|
6
|
+
// queries recall rows the substring scan cannot see.
|
|
7
|
+
|
|
8
|
+
// Tokenizer: ASCII words keep their shape (identifiers like "dsh-mneme" or
|
|
9
|
+
// "ZFS_4421" survive as whole tokens); CJK runs become sliding bigrams
|
|
10
|
+
// (unigram only for single characters), the standard workaround for BM25's
|
|
11
|
+
// whitespace tokenization on Chinese.
|
|
12
|
+
export function tokenize(text) {
|
|
13
|
+
const raw = String(text ?? "").toLowerCase();
|
|
14
|
+
const tokens = [];
|
|
15
|
+
const ascii = raw.match(/[a-z0-9_]+/g) ?? [];
|
|
16
|
+
tokens.push(...ascii);
|
|
17
|
+
const cjkRuns = raw.match(/[\u4e00-\u9fff]+/g) ?? [];
|
|
18
|
+
for (const run of cjkRuns) {
|
|
19
|
+
if (run.length === 1) { tokens.push(run); continue; }
|
|
20
|
+
for (let i = 0; i < run.length - 1; i++) tokens.push(run.slice(i, i + 2));
|
|
21
|
+
}
|
|
22
|
+
return tokens;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const K1 = 1.5; // term-frequency saturation
|
|
26
|
+
const B = 0.75; // length normalization
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Build a BM25 index over documents: [{id, title, content}].
|
|
30
|
+
* Returns { score, search }:
|
|
31
|
+
* score(query, doc) — per-spec ad-hoc scoring (re-tokenizes the doc)
|
|
32
|
+
* search(query, {limit}) — precomputed-tf ranking, scores normalized to
|
|
33
|
+
* [0,1] by the max so BM25 hits can weight-blend with vector/keyword
|
|
34
|
+
* scores on one scale. Rows the query does not touch at all are dropped.
|
|
35
|
+
*/
|
|
36
|
+
export function createBM25Index(documents) {
|
|
37
|
+
const docs = Array.isArray(documents) ? documents.filter(Boolean) : [];
|
|
38
|
+
const N = docs.length;
|
|
39
|
+
const df = new Map();
|
|
40
|
+
const prepared = docs.map((doc) => {
|
|
41
|
+
const tokens = tokenize(`${doc.title ?? ""} ${doc.content ?? ""}`);
|
|
42
|
+
const tf = new Map();
|
|
43
|
+
for (const t of tokens) tf.set(t, (tf.get(t) ?? 0) + 1);
|
|
44
|
+
for (const t of tf.keys()) df.set(t, (df.get(t) ?? 0) + 1);
|
|
45
|
+
return { doc, tf, len: tokens.length };
|
|
46
|
+
});
|
|
47
|
+
const avgLen = N ? prepared.reduce((s, p) => s + p.len, 0) / N : 0 || 1;
|
|
48
|
+
|
|
49
|
+
const idf = (t) => {
|
|
50
|
+
const n = df.get(t) ?? 0;
|
|
51
|
+
return Math.log((N - n + 0.5) / (n + 0.5) + 1);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function scorePrepared(queryTokens, p) {
|
|
55
|
+
let score = 0;
|
|
56
|
+
for (const t of queryTokens) {
|
|
57
|
+
const f = p.tf.get(t);
|
|
58
|
+
if (!f) continue;
|
|
59
|
+
const norm = p.len ? K1 * (1 - B + B * (p.len / avgLen)) : K1;
|
|
60
|
+
score += idf(t) * ((f * (K1 + 1)) / (f + norm));
|
|
61
|
+
}
|
|
62
|
+
return score;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
score(query, doc) {
|
|
67
|
+
const tokens = tokenize(`${doc?.title ?? ""} ${doc?.content ?? ""}`);
|
|
68
|
+
const tf = new Map();
|
|
69
|
+
for (const t of tokens) tf.set(t, (tf.get(t) ?? 0) + 1);
|
|
70
|
+
const len = tokens.length;
|
|
71
|
+
// Ad-hoc scoring can't see corpus df; fall back to tf-only saturation
|
|
72
|
+
// (df is approximated as 1 so idf ≈ log(N - 0.5 + 1) is constant).
|
|
73
|
+
let score = 0;
|
|
74
|
+
for (const t of tokenize(query)) {
|
|
75
|
+
const f = tf.get(t);
|
|
76
|
+
if (!f) continue;
|
|
77
|
+
const norm = len ? K1 * (1 - B + B * (len / avgLen)) : K1;
|
|
78
|
+
score += idf(t) * ((f * (K1 + 1)) / (f + norm));
|
|
79
|
+
}
|
|
80
|
+
return score;
|
|
81
|
+
},
|
|
82
|
+
search(query, { limit = 20 } = {}) {
|
|
83
|
+
const qTokens = tokenize(query);
|
|
84
|
+
if (!qTokens.length || !N) return [];
|
|
85
|
+
const scored = [];
|
|
86
|
+
for (const p of prepared) {
|
|
87
|
+
const s = scorePrepared(qTokens, p);
|
|
88
|
+
if (s > 0) scored.push({ row: p.doc, raw: s });
|
|
89
|
+
}
|
|
90
|
+
scored.sort((a, b) => b.raw - a.raw);
|
|
91
|
+
const top = scored.slice(0, limit);
|
|
92
|
+
const max = top[0]?.raw || 1;
|
|
93
|
+
return top.map(({ row, raw }) => ({ ...row, score: max ? raw / max : 0 }));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
package/lib/service.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { TYPE_FILE } from "./mirror.js";
|
|
3
3
|
import { evaluateMemoryQuality } from "./quality-filter.js";
|
|
4
|
+
import { createBM25Index } from "./search/bm25.js";
|
|
5
|
+
import { adaptiveThreshold } from "./search/adaptive.js";
|
|
4
6
|
|
|
5
7
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
6
8
|
|
|
@@ -307,6 +309,66 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
307
309
|
// Weighted blend factor for hybrid search; exposed so callers can tune it.
|
|
308
310
|
const DEFAULT_HYBRID_WEIGHTS = { vector: 0.6, keyword: 0.4 };
|
|
309
311
|
|
|
312
|
+
// Cosine over two plain arrays (shared by the search-time semantic dedup).
|
|
313
|
+
function cosineVec(a, b) {
|
|
314
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length || !a.length) return 0;
|
|
315
|
+
let dot = 0, na = 0, nb = 0;
|
|
316
|
+
for (let i = 0; i < a.length; i++) {
|
|
317
|
+
dot += a[i] * b[i];
|
|
318
|
+
na += a[i] * a[i];
|
|
319
|
+
nb += b[i] * b[i];
|
|
320
|
+
}
|
|
321
|
+
if (na === 0 || nb === 0) return 0;
|
|
322
|
+
return dot / (Math.sqrt(na) * Math.sqrt(nb));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* BM25 third recall path (v0.5.0 1.1). Scores the query tokens against the
|
|
327
|
+
* live non-archived rows and returns the top `limit` hits with scores
|
|
328
|
+
* normalized to [0,1]. Failures degrade to [] — BM25 is a recall booster,
|
|
329
|
+
* never a correctness gate.
|
|
330
|
+
*/
|
|
331
|
+
function bm25Recall(q, limit) {
|
|
332
|
+
if (config?.bm25SearchEnabled === false) return [];
|
|
333
|
+
try {
|
|
334
|
+
const docs = store.list({ limit: 500, includeForgotten: false }).filter((m) => !m.archived);
|
|
335
|
+
if (!docs.length) return [];
|
|
336
|
+
return createBM25Index(docs).search(q, { limit });
|
|
337
|
+
} catch {
|
|
338
|
+
return [];
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Search-time semantic dedup (v0.5.0 2.3): greedy pass dropping candidates
|
|
344
|
+
* whose embedding similarity to an already-kept row exceeds the threshold.
|
|
345
|
+
* Rows without a stored embedding are always kept (no signal = no drop).
|
|
346
|
+
*/
|
|
347
|
+
function semanticDeduplicate(candidates) {
|
|
348
|
+
// Opt-in aggressive mode (default off): collapsing near-duplicates can
|
|
349
|
+
// drop legitimately distinct rows on small embedding models, so it ships
|
|
350
|
+
// behind searchSemanticDedup=true.
|
|
351
|
+
if (config?.searchSemanticDedup !== true || candidates.length < 2) return candidates;
|
|
352
|
+
const threshold = config?.searchSemanticDedupThreshold ?? 0.95;
|
|
353
|
+
try {
|
|
354
|
+
const vecs = store.getEmbeddings(candidates.map((c) => c.id));
|
|
355
|
+
if (vecs.size < 2) return candidates;
|
|
356
|
+
const kept = [];
|
|
357
|
+
for (const c of candidates) {
|
|
358
|
+
const v = vecs.get(c.id);
|
|
359
|
+
if (!v) { kept.push(c); continue; }
|
|
360
|
+
const dup = kept.some((k) => {
|
|
361
|
+
const kv = vecs.get(k.id);
|
|
362
|
+
return kv && cosineVec(v, kv) > threshold;
|
|
363
|
+
});
|
|
364
|
+
if (!dup) kept.push(c);
|
|
365
|
+
}
|
|
366
|
+
return kept;
|
|
367
|
+
} catch {
|
|
368
|
+
return candidates;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
310
372
|
/**
|
|
311
373
|
* Give a keyword-hit row a relevance score in [0,1]: title hits score
|
|
312
374
|
* higher than content hits, then scaled by importance (1-5). This lets
|
|
@@ -371,14 +433,39 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
371
433
|
: embedder.embed.bind(embedder);
|
|
372
434
|
const qv = await embedSingle(q);
|
|
373
435
|
if (qv?.length) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
436
|
+
// Adaptive threshold (v0.5.0 1.2): the fetch runs at the loosest
|
|
437
|
+
// branch floor so the head-gap rule can still re-admit the tail;
|
|
438
|
+
// the final cutoff is computed against the fetched score
|
|
439
|
+
// distribution. Explicit `threshold` wins; disabled → legacy 0.
|
|
440
|
+
const adaptive = config?.adaptiveThresholdEnabled !== false;
|
|
441
|
+
const fetchThreshold = adaptive && threshold === undefined
|
|
442
|
+
? Math.min(0.5, adaptiveThreshold(q))
|
|
443
|
+
: (threshold ?? 0);
|
|
444
|
+
const search = vectorIndex
|
|
445
|
+
? vectorIndex.search(qv, { limit: lim * 2, threshold: fetchThreshold })
|
|
446
|
+
: store.searchVector(qv, { limit: lim * 2, threshold: fetchThreshold });
|
|
447
|
+
const finalThreshold = adaptive && threshold === undefined
|
|
448
|
+
? adaptiveThreshold(q, search)
|
|
449
|
+
: (threshold ?? 0);
|
|
450
|
+
vector = search
|
|
451
|
+
.filter((m) => (m.score ?? 1) >= finalThreshold)
|
|
452
|
+
.map((m) => ({ ...m, vector: true, source: "vector" }));
|
|
378
453
|
}
|
|
379
454
|
} catch { /* vector unavailable: keep keyword results */ }
|
|
380
455
|
}
|
|
381
456
|
|
|
457
|
+
// BM25 third path (v0.5.0 1.1): IDF-weighted token overlap recalls rows
|
|
458
|
+
// whose query terms are scattered — the gap LIKE substring matching
|
|
459
|
+
// cannot close. Scores are already normalized to [0,1].
|
|
460
|
+
const bm25 = bm25Recall(q, lim).map((m) => ({ ...m, source: "bm25" }));
|
|
461
|
+
// Loose blend weight: BM25 confirms and backfills, never dominates the
|
|
462
|
+
// semantic signal. Same-memory overlap boosts, unseen ids backfill.
|
|
463
|
+
const wb = 0.3;
|
|
464
|
+
// Path bookkeeping for the boost rule below: which ids each semantic
|
|
465
|
+
// recall path surfaced.
|
|
466
|
+
const vectorIds = new Set(vector.map((m) => m.id));
|
|
467
|
+
const keywordIds = new Set(keyword.map((m) => m.id));
|
|
468
|
+
|
|
382
469
|
// Hybrid blending weights from config when provided.
|
|
383
470
|
const wv = config?.hybridSearchVectorWeight ?? DEFAULT_HYBRID_WEIGHTS.vector;
|
|
384
471
|
const wk = config?.hybridSearchKeywordWeight ?? DEFAULT_HYBRID_WEIGHTS.keyword;
|
|
@@ -387,9 +474,10 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
387
474
|
if (mode === "keyword") {
|
|
388
475
|
merged = keyword;
|
|
389
476
|
} else if (mode === "vector" || mode === "hybrid") {
|
|
390
|
-
// semantic-first: vector recalls lead, keyword
|
|
391
|
-
// Weighted blend when
|
|
392
|
-
// vector order leads (it is the semantic signal),
|
|
477
|
+
// semantic-first: vector recalls lead, keyword + BM25 fill remaining
|
|
478
|
+
// slots. Weighted blend when sides scored the same memory; otherwise
|
|
479
|
+
// vector order leads (it is the semantic signal), lexical paths
|
|
480
|
+
// backfill.
|
|
393
481
|
const byId = new Map();
|
|
394
482
|
for (const m of vector) {
|
|
395
483
|
const rec = byId.get(m.id);
|
|
@@ -404,22 +492,48 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
404
492
|
byId.set(m.id, m);
|
|
405
493
|
}
|
|
406
494
|
}
|
|
407
|
-
|
|
495
|
+
for (const m of bm25) {
|
|
496
|
+
const rec = byId.get(m.id);
|
|
497
|
+
if (rec) {
|
|
498
|
+
// Boost rule: a row the LIKE keyword path already hit carries the
|
|
499
|
+
// query as a substring, so BM25 tokens are trivially present —
|
|
500
|
+
// boosting it double-counts lexical evidence. Only vector-recalled
|
|
501
|
+
// rows (lexical hit is genuinely new information) get the boost.
|
|
502
|
+
if (keywordIds.has(m.id)) continue;
|
|
503
|
+
byId.set(m.id, { ...rec, score: (rec.score ?? 0) + wb * (m.score ?? 0) });
|
|
504
|
+
} else {
|
|
505
|
+
byId.set(m.id, { ...m, score: wb * (m.score ?? 0) });
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
const ranked = [...byId.values()]
|
|
509
|
+
.sort((a, b) => (b.score ?? 0) - (a.score ?? 0))
|
|
510
|
+
.map((r) => ({ ...r, score: Math.max(0, Math.min(1, r.score ?? 0)) }));
|
|
408
511
|
merged = ranked.slice(0, lim);
|
|
409
512
|
if (merged.length < lim && !merged.length) {
|
|
410
513
|
// Vector unavailable entirely: fall back to plain keyword.
|
|
411
514
|
merged = keyword.slice(0, lim);
|
|
412
515
|
}
|
|
413
516
|
} else {
|
|
414
|
-
// auto: keyword leads, vector
|
|
517
|
+
// auto: keyword leads, vector + BM25 fill remaining slots (legacy
|
|
518
|
+
// behavior, extended with the third path)
|
|
415
519
|
merged = keyword.slice(0, lim);
|
|
416
520
|
const seen = new Set(merged.map((m) => m.id));
|
|
417
521
|
for (const m of vector) {
|
|
418
522
|
if (merged.length >= lim) break;
|
|
419
523
|
if (!seen.has(m.id)) { seen.add(m.id); merged.push(m); }
|
|
420
524
|
}
|
|
525
|
+
for (const m of bm25) {
|
|
526
|
+
if (merged.length >= lim) break;
|
|
527
|
+
if (!seen.has(m.id)) { seen.add(m.id); merged.push(m); }
|
|
528
|
+
}
|
|
421
529
|
}
|
|
422
530
|
|
|
531
|
+
// Search-time semantic dedup (v0.5.0 2.3): near-duplicate rows are
|
|
532
|
+
// dropped before the reranker sees them, so topK slots carry distinct
|
|
533
|
+
// information instead of the same memory twice. Keyword mode is exempt —
|
|
534
|
+
// it is the documented text-only path and must not be altered by
|
|
535
|
+
// embedding state.
|
|
536
|
+
merged = mode === "keyword" ? merged : semanticDeduplicate(merged);
|
|
423
537
|
merged = merged.slice(0, lim);
|
|
424
538
|
let result = useRerank && reranker && merged.length
|
|
425
539
|
? await rerankCandidates(q, merged, lim)
|
|
@@ -806,6 +920,20 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
806
920
|
candidates = merged;
|
|
807
921
|
}
|
|
808
922
|
}
|
|
923
|
+
// Topic-ranked selection (v0.5.0 2.2): when the current query's vector is
|
|
924
|
+
// available the whole candidate list is re-ordered by similarity to that
|
|
925
|
+
// vector, so the injected slots go to memories on the current topic
|
|
926
|
+
// rather than to the rule-based order. Rows the index did not return
|
|
927
|
+
// keep their relative order after the scored ones.
|
|
928
|
+
if (config?.selectiveInjectEnabled !== false && Array.isArray(queryVector) && queryVector.length && vectorIndex) {
|
|
929
|
+
try {
|
|
930
|
+
const hits = vectorIndex.search(queryVector, { limit: 200, threshold: 0 });
|
|
931
|
+
const sim = new Map(hits.map((m) => [m.id, m.score ?? 0]));
|
|
932
|
+
if (sim.size) {
|
|
933
|
+
candidates = [...candidates].sort((a, b) => (sim.get(b.id) ?? -1) - (sim.get(a.id) ?? -1));
|
|
934
|
+
}
|
|
935
|
+
} catch { /* topic re-rank unavailable: keep rule-based order */ }
|
|
936
|
+
}
|
|
809
937
|
const selected = candidates.slice(0, maxItems);
|
|
810
938
|
touchRecalled(selected);
|
|
811
939
|
return selected;
|
|
@@ -1352,6 +1480,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1352
1480
|
findEntityByName: (n) => store.findEntityByName(n),
|
|
1353
1481
|
findEntityById: (id) => store.findEntityById(id),
|
|
1354
1482
|
getAttrsByMemory: (id) => store.getAttrsByMemory(id),
|
|
1483
|
+
getCurrentAttrs: (id) => store.getCurrentAttrs(id),
|
|
1355
1484
|
migrateAttrsToMemory: (fromId, toId, now) => store.migrateAttrsToMemory(fromId, toId, now)
|
|
1356
1485
|
};
|
|
1357
1486
|
}
|