Update recall logic and remove unused state-recall
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Story Summary - Recall Engine (v4 - L0 无上限 + L1 粗筛)
|
||||
// Story Summary - Recall Engine (v5 - 统一命名)
|
||||
//
|
||||
// 命名规范:
|
||||
// - 存储层用 L0/L1/L2/L3(StateAtom/Chunk/Event/Fact)
|
||||
// - 召回层用语义名称:anchor/evidence/event/constraint
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
import { getAllEventVectors, getChunksByFloors, getMeta, getChunkVectorsByIds } from '../storage/chunk-store.js';
|
||||
@@ -22,17 +26,17 @@ const CONFIG = {
|
||||
// Query Expansion
|
||||
QUERY_EXPANSION_TIMEOUT: 6000,
|
||||
|
||||
// L0 配置 - 去掉硬上限,提高阈值
|
||||
L0_MIN_SIMILARITY: 0.58,
|
||||
// Anchor (L0 StateAtoms) 配置
|
||||
ANCHOR_MIN_SIMILARITY: 0.58,
|
||||
|
||||
// L1 粗筛配置
|
||||
L1_MAX_CANDIDATES: 100,
|
||||
// Evidence (L1 Chunks) 粗筛配置
|
||||
EVIDENCE_COARSE_MAX: 100,
|
||||
|
||||
// L2 配置
|
||||
L2_CANDIDATE_MAX: 100,
|
||||
L2_SELECT_MAX: 50,
|
||||
L2_MIN_SIMILARITY: 0.55,
|
||||
L2_MMR_LAMBDA: 0.72,
|
||||
// Event (L2 Events) 配置
|
||||
EVENT_CANDIDATE_MAX: 100,
|
||||
EVENT_SELECT_MAX: 50,
|
||||
EVENT_MIN_SIMILARITY: 0.55,
|
||||
EVENT_MMR_LAMBDA: 0.72,
|
||||
|
||||
// Rerank 配置
|
||||
RERANK_THRESHOLD: 80,
|
||||
@@ -48,6 +52,12 @@ const CONFIG = {
|
||||
// 工具函数
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* 计算余弦相似度
|
||||
* @param {number[]} a - 向量A
|
||||
* @param {number[]} b - 向量B
|
||||
* @returns {number} 相似度 [0, 1]
|
||||
*/
|
||||
function cosineSimilarity(a, b) {
|
||||
if (!a?.length || !b?.length || a.length !== b.length) return 0;
|
||||
let dot = 0, nA = 0, nB = 0;
|
||||
@@ -59,6 +69,11 @@ function cosineSimilarity(a, b) {
|
||||
return nA && nB ? dot / (Math.sqrt(nA) * Math.sqrt(nB)) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 标准化字符串(用于实体匹配)
|
||||
* @param {string} s - 输入字符串
|
||||
* @returns {string} 标准化后的字符串
|
||||
*/
|
||||
function normalize(s) {
|
||||
return String(s || '')
|
||||
.normalize('NFKC')
|
||||
@@ -67,10 +82,21 @@ function normalize(s) {
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理文本用于召回
|
||||
* @param {string} text - 原始文本
|
||||
* @returns {string} 清理后的文本
|
||||
*/
|
||||
function cleanForRecall(text) {
|
||||
return filterText(text).replace(/\[tts:[^\]]*\]/gi, '').trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 focus entities 中移除用户名
|
||||
* @param {string[]} focusEntities - 焦点实体列表
|
||||
* @param {string} userName - 用户名
|
||||
* @returns {string[]} 过滤后的实体列表
|
||||
*/
|
||||
function removeUserNameFromFocus(focusEntities, userName) {
|
||||
const u = normalize(userName);
|
||||
if (!u) return Array.isArray(focusEntities) ? focusEntities : [];
|
||||
@@ -81,6 +107,13 @@ function removeUserNameFromFocus(focusEntities, userName) {
|
||||
.filter(e => normalize(e) !== u);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 rerank 查询文本
|
||||
* @param {object} expansion - query expansion 结果
|
||||
* @param {object[]} lastMessages - 最近消息
|
||||
* @param {string} pendingUserMessage - 待发送的用户消息
|
||||
* @returns {string} 查询文本
|
||||
*/
|
||||
function buildRerankQuery(expansion, lastMessages, pendingUserMessage) {
|
||||
const parts = [];
|
||||
|
||||
@@ -109,9 +142,18 @@ function buildRerankQuery(expansion, lastMessages, pendingUserMessage) {
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// MMR 选择
|
||||
// MMR 选择算法
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* Maximal Marginal Relevance 选择
|
||||
* @param {object[]} candidates - 候选项
|
||||
* @param {number} k - 选择数量
|
||||
* @param {number} lambda - 相关性/多样性权衡参数
|
||||
* @param {Function} getVector - 获取向量的函数
|
||||
* @param {Function} getScore - 获取分数的函数
|
||||
* @returns {object[]} 选中的候选项
|
||||
*/
|
||||
function mmrSelect(candidates, k, lambda, getVector, getScore) {
|
||||
const selected = [];
|
||||
const ids = new Set();
|
||||
@@ -152,31 +194,38 @@ function mmrSelect(candidates, k, lambda, getVector, getScore) {
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// L0 检索:无上限,阈值过滤
|
||||
// [Anchors] L0 StateAtoms 检索
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
async function searchL0(queryVector, vectorConfig, metrics) {
|
||||
/**
|
||||
* 检索语义锚点(L0 StateAtoms)
|
||||
* @param {number[]} queryVector - 查询向量
|
||||
* @param {object} vectorConfig - 向量配置
|
||||
* @param {object} metrics - 指标对象
|
||||
* @returns {Promise<{hits: object[], floors: Set<number>}>}
|
||||
*/
|
||||
async function recallAnchors(queryVector, vectorConfig, metrics) {
|
||||
const { chatId } = getContext();
|
||||
if (!chatId || !queryVector?.length) {
|
||||
return { atoms: [], floors: new Set() };
|
||||
return { hits: [], floors: new Set() };
|
||||
}
|
||||
|
||||
const meta = await getMeta(chatId);
|
||||
const fp = getEngineFingerprint(vectorConfig);
|
||||
if (meta.fingerprint && meta.fingerprint !== fp) {
|
||||
xbLog.warn(MODULE_ID, 'L0 fingerprint 不匹配');
|
||||
return { atoms: [], floors: new Set() };
|
||||
xbLog.warn(MODULE_ID, 'Anchor fingerprint 不匹配');
|
||||
return { hits: [], floors: new Set() };
|
||||
}
|
||||
|
||||
const stateVectors = await getAllStateVectors(chatId);
|
||||
if (!stateVectors.length) {
|
||||
return { atoms: [], floors: new Set() };
|
||||
return { hits: [], floors: new Set() };
|
||||
}
|
||||
|
||||
const atomsList = getStateAtoms();
|
||||
const atomMap = new Map(atomsList.map(a => [a.atomId, a]));
|
||||
|
||||
// ★ 只按阈值过滤,不设硬上限
|
||||
// 按阈值过滤,不设硬上限
|
||||
const scored = stateVectors
|
||||
.map(sv => {
|
||||
const atom = atomMap.get(sv.atomId);
|
||||
@@ -190,69 +239,79 @@ async function searchL0(queryVector, vectorConfig, metrics) {
|
||||
};
|
||||
})
|
||||
.filter(Boolean)
|
||||
.filter(s => s.similarity >= CONFIG.L0_MIN_SIMILARITY)
|
||||
.filter(s => s.similarity >= CONFIG.ANCHOR_MIN_SIMILARITY)
|
||||
.sort((a, b) => b.similarity - a.similarity);
|
||||
|
||||
const floors = new Set(scored.map(s => s.floor));
|
||||
|
||||
if (metrics) {
|
||||
metrics.l0.atomsMatched = scored.length;
|
||||
metrics.l0.floorsHit = floors.size;
|
||||
metrics.l0.topAtoms = scored.slice(0, 5).map(s => ({
|
||||
metrics.anchor.matched = scored.length;
|
||||
metrics.anchor.floorsHit = floors.size;
|
||||
metrics.anchor.topHits = scored.slice(0, 5).map(s => ({
|
||||
floor: s.floor,
|
||||
semantic: s.atom?.semantic?.slice(0, 50),
|
||||
similarity: Math.round(s.similarity * 1000) / 1000,
|
||||
}));
|
||||
}
|
||||
|
||||
return { atoms: scored, floors };
|
||||
return { hits: scored, floors };
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// 统计 chunks 类型构成
|
||||
// [Evidence] L1 Chunks 拉取 + 粗筛 + Rerank
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
function countChunksByType(chunks) {
|
||||
let l0Virtual = 0;
|
||||
let l1Real = 0;
|
||||
/**
|
||||
* 统计 evidence 类型构成
|
||||
* @param {object[]} chunks - chunk 列表
|
||||
* @returns {{anchorVirtual: number, chunkReal: number}}
|
||||
*/
|
||||
function countEvidenceByType(chunks) {
|
||||
let anchorVirtual = 0;
|
||||
let chunkReal = 0;
|
||||
|
||||
for (const c of chunks || []) {
|
||||
if (c.isL0) {
|
||||
l0Virtual++;
|
||||
if (c.isAnchorVirtual) {
|
||||
anchorVirtual++;
|
||||
} else {
|
||||
l1Real++;
|
||||
chunkReal++;
|
||||
}
|
||||
}
|
||||
|
||||
return { l0Virtual, l1Real };
|
||||
return { anchorVirtual, chunkReal };
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// L3 拉取 + L1 粗筛 + Rerank
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
async function getChunksFromL0Floors(l0Floors, l0Atoms, queryVector, queryText, metrics) {
|
||||
/**
|
||||
* 根据锚点命中楼层拉取证据(L1 Chunks)
|
||||
* @param {Set<number>} anchorFloors - 锚点命中的楼层
|
||||
* @param {object[]} anchorHits - 锚点命中结果
|
||||
* @param {number[]} queryVector - 查询向量
|
||||
* @param {string} queryText - rerank 查询文本
|
||||
* @param {object} metrics - 指标对象
|
||||
* @returns {Promise<object[]>} 证据 chunks
|
||||
*/
|
||||
async function pullEvidenceByFloors(anchorFloors, anchorHits, queryVector, queryText, metrics) {
|
||||
const { chatId } = getContext();
|
||||
if (!chatId || !l0Floors.size) {
|
||||
if (!chatId || !anchorFloors.size) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const floorArray = Array.from(l0Floors);
|
||||
const floorArray = Array.from(anchorFloors);
|
||||
|
||||
// 1. 构建 L0 虚拟 chunks
|
||||
const l0VirtualChunks = (l0Atoms || []).map(a => ({
|
||||
chunkId: `state-${a.atomId}`,
|
||||
// 1. 构建锚点虚拟 chunks(来自 L0 StateAtoms)
|
||||
const anchorVirtualChunks = (anchorHits || []).map(a => ({
|
||||
chunkId: `anchor-${a.atomId}`,
|
||||
floor: a.floor,
|
||||
chunkIdx: -1,
|
||||
speaker: '📌',
|
||||
isUser: false,
|
||||
text: a.atom?.semantic || '',
|
||||
similarity: a.similarity,
|
||||
isL0: true,
|
||||
isAnchorVirtual: true,
|
||||
_atom: a.atom,
|
||||
}));
|
||||
|
||||
// 2. 拉取 L1 chunks
|
||||
// 2. 拉取真实 chunks(来自 L1)
|
||||
let dbChunks = [];
|
||||
try {
|
||||
dbChunks = await getChunksByFloors(chatId, floorArray);
|
||||
@@ -260,8 +319,8 @@ async function getChunksFromL0Floors(l0Floors, l0Atoms, queryVector, queryText,
|
||||
xbLog.warn(MODULE_ID, '从 DB 拉取 chunks 失败', e);
|
||||
}
|
||||
|
||||
// 3. ★ L1 向量粗筛
|
||||
let l1Filtered = [];
|
||||
// 3. L1 向量粗筛
|
||||
let coarseFiltered = [];
|
||||
if (dbChunks.length > 0 && queryVector?.length) {
|
||||
const chunkIds = dbChunks.map(c => c.chunkId);
|
||||
let chunkVectors = [];
|
||||
@@ -270,54 +329,51 @@ async function getChunksFromL0Floors(l0Floors, l0Atoms, queryVector, queryText,
|
||||
} catch (e) {
|
||||
xbLog.warn(MODULE_ID, 'L1 向量获取失败', e);
|
||||
}
|
||||
|
||||
|
||||
const vectorMap = new Map(chunkVectors.map(v => [v.chunkId, v.vector]));
|
||||
|
||||
l1Filtered = dbChunks
|
||||
coarseFiltered = dbChunks
|
||||
.map(c => {
|
||||
const vec = vectorMap.get(c.chunkId);
|
||||
if (!vec?.length) return null;
|
||||
|
||||
return {
|
||||
...c,
|
||||
isL0: false,
|
||||
isAnchorVirtual: false,
|
||||
similarity: cosineSimilarity(queryVector, vec),
|
||||
};
|
||||
})
|
||||
.filter(Boolean)
|
||||
.sort((a, b) => b.similarity - a.similarity)
|
||||
.slice(0, CONFIG.L1_MAX_CANDIDATES);
|
||||
.slice(0, CONFIG.EVIDENCE_COARSE_MAX);
|
||||
}
|
||||
|
||||
// 4. 合并
|
||||
const allChunks = [...l0VirtualChunks, ...l1Filtered];
|
||||
const allEvidence = [...anchorVirtualChunks, ...coarseFiltered];
|
||||
|
||||
// ★ 更新 metrics
|
||||
// 更新 metrics
|
||||
if (metrics) {
|
||||
metrics.l3.floorsFromL0 = floorArray.length;
|
||||
metrics.l3.l1Total = dbChunks.length;
|
||||
metrics.l3.l1AfterCoarse = l1Filtered.length;
|
||||
metrics.l3.chunksInRange = l0VirtualChunks.length + l1Filtered.length;
|
||||
metrics.l3.chunksInRangeByType = {
|
||||
l0Virtual: l0VirtualChunks.length,
|
||||
l1Real: l1Filtered.length,
|
||||
};
|
||||
metrics.evidence.floorsFromAnchors = floorArray.length;
|
||||
metrics.evidence.chunkTotal = dbChunks.length;
|
||||
metrics.evidence.chunkAfterCoarse = coarseFiltered.length;
|
||||
metrics.evidence.merged = allEvidence.length;
|
||||
metrics.evidence.mergedByType = countEvidenceByType(allEvidence);
|
||||
}
|
||||
|
||||
// 5. 是否需要 Rerank
|
||||
if (allChunks.length <= CONFIG.RERANK_THRESHOLD) {
|
||||
if (allEvidence.length <= CONFIG.RERANK_THRESHOLD) {
|
||||
if (metrics) {
|
||||
metrics.l3.rerankApplied = false;
|
||||
metrics.l3.chunksSelected = allChunks.length;
|
||||
metrics.l3.chunksSelectedByType = countChunksByType(allChunks);
|
||||
metrics.evidence.rerankApplied = false;
|
||||
metrics.evidence.selected = allEvidence.length;
|
||||
metrics.evidence.selectedByType = countEvidenceByType(allEvidence);
|
||||
}
|
||||
return allChunks;
|
||||
return allEvidence;
|
||||
}
|
||||
|
||||
// 6. Rerank 精排
|
||||
const T_Rerank_Start = performance.now();
|
||||
|
||||
const reranked = await rerankChunks(queryText, allChunks, {
|
||||
const reranked = await rerankChunks(queryText, allEvidence, {
|
||||
topN: CONFIG.RERANK_TOP_N,
|
||||
minScore: CONFIG.RERANK_MIN_SCORE,
|
||||
});
|
||||
@@ -325,18 +381,18 @@ async function getChunksFromL0Floors(l0Floors, l0Atoms, queryVector, queryText,
|
||||
const rerankTime = Math.round(performance.now() - T_Rerank_Start);
|
||||
|
||||
if (metrics) {
|
||||
metrics.l3.rerankApplied = true;
|
||||
metrics.l3.beforeRerank = allChunks.length;
|
||||
metrics.l3.afterRerank = reranked.length;
|
||||
metrics.l3.chunksSelected = reranked.length;
|
||||
metrics.l3.chunksSelectedByType = countChunksByType(reranked);
|
||||
metrics.l3.rerankTime = rerankTime;
|
||||
metrics.timing.l3Rerank = rerankTime;
|
||||
metrics.evidence.rerankApplied = true;
|
||||
metrics.evidence.beforeRerank = allEvidence.length;
|
||||
metrics.evidence.afterRerank = reranked.length;
|
||||
metrics.evidence.selected = reranked.length;
|
||||
metrics.evidence.selectedByType = countEvidenceByType(reranked);
|
||||
metrics.evidence.rerankTime = rerankTime;
|
||||
metrics.timing.evidenceRerank = rerankTime;
|
||||
|
||||
const scores = reranked.map(c => c._rerankScore || 0).filter(s => s > 0);
|
||||
if (scores.length > 0) {
|
||||
scores.sort((a, b) => a - b);
|
||||
metrics.l3.rerankScoreDistribution = {
|
||||
metrics.evidence.rerankScores = {
|
||||
min: Number(scores[0].toFixed(3)),
|
||||
max: Number(scores[scores.length - 1].toFixed(3)),
|
||||
mean: Number((scores.reduce((a, b) => a + b, 0) / scores.length).toFixed(3)),
|
||||
@@ -344,16 +400,25 @@ async function getChunksFromL0Floors(l0Floors, l0Atoms, queryVector, queryText,
|
||||
}
|
||||
}
|
||||
|
||||
xbLog.info(MODULE_ID, `L3: ${dbChunks.length} L1 → ${l1Filtered.length} 粗筛 → ${reranked.length} Rerank (${rerankTime}ms)`);
|
||||
xbLog.info(MODULE_ID, `Evidence: ${dbChunks.length} L1 → ${coarseFiltered.length} coarse → ${reranked.length} rerank (${rerankTime}ms)`);
|
||||
|
||||
return reranked;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// L2 检索(保持不变)
|
||||
// [Events] L2 Events 检索
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
async function searchL2Events(queryVector, allEvents, vectorConfig, focusEntities, metrics) {
|
||||
/**
|
||||
* 检索事件(L2 Events)
|
||||
* @param {number[]} queryVector - 查询向量
|
||||
* @param {object[]} allEvents - 所有事件
|
||||
* @param {object} vectorConfig - 向量配置
|
||||
* @param {string[]} focusEntities - 焦点实体
|
||||
* @param {object} metrics - 指标对象
|
||||
* @returns {Promise<object[]>} 事件命中结果
|
||||
*/
|
||||
async function recallEvents(queryVector, allEvents, vectorConfig, focusEntities, metrics) {
|
||||
const { chatId } = getContext();
|
||||
if (!chatId || !queryVector?.length || !allEvents?.length) {
|
||||
return [];
|
||||
@@ -362,7 +427,7 @@ async function searchL2Events(queryVector, allEvents, vectorConfig, focusEntitie
|
||||
const meta = await getMeta(chatId);
|
||||
const fp = getEngineFingerprint(vectorConfig);
|
||||
if (meta.fingerprint && meta.fingerprint !== fp) {
|
||||
xbLog.warn(MODULE_ID, 'L2 fingerprint 不匹配');
|
||||
xbLog.warn(MODULE_ID, 'Event fingerprint 不匹配');
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -395,18 +460,19 @@ async function searchL2Events(queryVector, allEvents, vectorConfig, focusEntitie
|
||||
});
|
||||
|
||||
if (metrics) {
|
||||
metrics.l2.eventsInStore = allEvents.length;
|
||||
metrics.event.inStore = allEvents.length;
|
||||
}
|
||||
|
||||
let candidates = scored
|
||||
.filter(s => s.similarity >= CONFIG.L2_MIN_SIMILARITY)
|
||||
.filter(s => s.similarity >= CONFIG.EVENT_MIN_SIMILARITY)
|
||||
.sort((a, b) => b.similarity - a.similarity)
|
||||
.slice(0, CONFIG.L2_CANDIDATE_MAX);
|
||||
.slice(0, CONFIG.EVENT_CANDIDATE_MAX);
|
||||
|
||||
if (metrics) {
|
||||
metrics.l2.eventsConsidered = candidates.length;
|
||||
metrics.event.considered = candidates.length;
|
||||
}
|
||||
|
||||
// 实体过滤
|
||||
if (focusSet.size > 0) {
|
||||
const beforeFilter = candidates.length;
|
||||
|
||||
@@ -416,7 +482,7 @@ async function searchL2Events(queryVector, allEvents, vectorConfig, focusEntitie
|
||||
});
|
||||
|
||||
if (metrics) {
|
||||
metrics.l2.entityFilterStats = {
|
||||
metrics.event.entityFilter = {
|
||||
focusEntities: focusEntities || [],
|
||||
before: beforeFilter,
|
||||
after: candidates.length,
|
||||
@@ -425,21 +491,22 @@ async function searchL2Events(queryVector, allEvents, vectorConfig, focusEntitie
|
||||
}
|
||||
}
|
||||
|
||||
// MMR 选择
|
||||
const selected = mmrSelect(
|
||||
candidates,
|
||||
CONFIG.L2_SELECT_MAX,
|
||||
CONFIG.L2_MMR_LAMBDA,
|
||||
CONFIG.EVENT_SELECT_MAX,
|
||||
CONFIG.EVENT_MMR_LAMBDA,
|
||||
c => c.vector,
|
||||
c => c.similarity
|
||||
);
|
||||
|
||||
let directCount = 0;
|
||||
let contextCount = 0;
|
||||
let relatedCount = 0;
|
||||
|
||||
const results = selected.map(s => {
|
||||
const recallType = s._hasEntityMatch ? 'DIRECT' : 'SIMILAR';
|
||||
const recallType = s._hasEntityMatch ? 'DIRECT' : 'RELATED';
|
||||
if (recallType === 'DIRECT') directCount++;
|
||||
else contextCount++;
|
||||
else relatedCount++;
|
||||
|
||||
return {
|
||||
event: s.event,
|
||||
@@ -450,18 +517,23 @@ async function searchL2Events(queryVector, allEvents, vectorConfig, focusEntitie
|
||||
});
|
||||
|
||||
if (metrics) {
|
||||
metrics.l2.eventsSelected = results.length;
|
||||
metrics.l2.byRecallType = { direct: directCount, context: contextCount, causal: 0 };
|
||||
metrics.l2.similarityDistribution = calcSimilarityStats(results.map(r => r.similarity));
|
||||
metrics.event.selected = results.length;
|
||||
metrics.event.byRecallType = { direct: directCount, related: relatedCount, causal: 0 };
|
||||
metrics.event.similarityDistribution = calcSimilarityStats(results.map(r => r.similarity));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// 因果链追溯(保持不变)
|
||||
// [Causation] 因果链追溯
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* 构建事件索引
|
||||
* @param {object[]} allEvents - 所有事件
|
||||
* @returns {Map<string, object>} 事件索引
|
||||
*/
|
||||
function buildEventIndex(allEvents) {
|
||||
const map = new Map();
|
||||
for (const e of allEvents || []) {
|
||||
@@ -470,7 +542,14 @@ function buildEventIndex(allEvents) {
|
||||
return map;
|
||||
}
|
||||
|
||||
function traceCausalAncestors(recalledEvents, eventIndex, maxDepth = CONFIG.CAUSAL_CHAIN_MAX_DEPTH) {
|
||||
/**
|
||||
* 追溯因果链
|
||||
* @param {object[]} eventHits - 事件命中结果
|
||||
* @param {Map<string, object>} eventIndex - 事件索引
|
||||
* @param {number} maxDepth - 最大深度
|
||||
* @returns {{results: object[], maxDepth: number}}
|
||||
*/
|
||||
function traceCausation(eventHits, eventIndex, maxDepth = CONFIG.CAUSAL_CHAIN_MAX_DEPTH) {
|
||||
const out = new Map();
|
||||
const idRe = /^evt-\d+$/;
|
||||
let maxActualDepth = 0;
|
||||
@@ -497,7 +576,7 @@ function traceCausalAncestors(recalledEvents, eventIndex, maxDepth = CONFIG.CAUS
|
||||
}
|
||||
}
|
||||
|
||||
for (const r of recalledEvents || []) {
|
||||
for (const r of eventHits || []) {
|
||||
const rid = r?.event?.id;
|
||||
if (!rid) continue;
|
||||
for (const cid of (r.event?.causedBy || [])) {
|
||||
@@ -520,6 +599,13 @@ function traceCausalAncestors(recalledEvents, eventIndex, maxDepth = CONFIG.CAUS
|
||||
// 辅助函数
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* 获取最近消息
|
||||
* @param {object[]} chat - 聊天记录
|
||||
* @param {number} count - 消息数量
|
||||
* @param {boolean} excludeLastAi - 是否排除最后的 AI 消息
|
||||
* @returns {object[]} 最近消息
|
||||
*/
|
||||
function getLastMessages(chat, count = 4, excludeLastAi = false) {
|
||||
if (!chat?.length) return [];
|
||||
|
||||
@@ -532,6 +618,13 @@ function getLastMessages(chat, count = 4, excludeLastAi = false) {
|
||||
return messages.slice(-count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询文本
|
||||
* @param {object[]} chat - 聊天记录
|
||||
* @param {number} count - 消息数量
|
||||
* @param {boolean} excludeLastAi - 是否排除最后的 AI 消息
|
||||
* @returns {string} 查询文本
|
||||
*/
|
||||
export function buildQueryText(chat, count = 2, excludeLastAi = false) {
|
||||
if (!chat?.length) return '';
|
||||
|
||||
@@ -551,6 +644,14 @@ export function buildQueryText(chat, count = 2, excludeLastAi = false) {
|
||||
// 主函数
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
/**
|
||||
* 执行记忆召回
|
||||
* @param {string} queryText - 查询文本
|
||||
* @param {object[]} allEvents - 所有事件(L2)
|
||||
* @param {object} vectorConfig - 向量配置
|
||||
* @param {object} options - 选项
|
||||
* @returns {Promise<object>} 召回结果
|
||||
*/
|
||||
export async function recallMemory(queryText, allEvents, vectorConfig, options = {}) {
|
||||
const T0 = performance.now();
|
||||
const { chat, name1 } = getContext();
|
||||
@@ -559,8 +660,16 @@ export async function recallMemory(queryText, allEvents, vectorConfig, options =
|
||||
const metrics = createMetrics();
|
||||
|
||||
if (!allEvents?.length) {
|
||||
metrics.l0.needRecall = false;
|
||||
return { events: [], chunks: [], causalEvents: [], focusEntities: [], elapsed: 0, logText: 'No events.', metrics };
|
||||
metrics.anchor.needRecall = false;
|
||||
return {
|
||||
events: [],
|
||||
evidenceChunks: [],
|
||||
causalChain: [],
|
||||
focusEntities: [],
|
||||
elapsed: 0,
|
||||
logText: 'No events.',
|
||||
metrics,
|
||||
};
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
@@ -587,11 +696,11 @@ export async function recallMemory(queryText, allEvents, vectorConfig, options =
|
||||
|
||||
const focusEntities = removeUserNameFromFocus(expansion.focus, name1);
|
||||
|
||||
metrics.l0.needRecall = true;
|
||||
metrics.l0.focusEntities = focusEntities;
|
||||
metrics.l0.queries = expansion.queries || [];
|
||||
metrics.l0.queryExpansionTime = Math.round(performance.now() - T_QE_Start);
|
||||
metrics.timing.queryExpansion = metrics.l0.queryExpansionTime;
|
||||
metrics.anchor.needRecall = true;
|
||||
metrics.anchor.focusEntities = focusEntities;
|
||||
metrics.anchor.queries = expansion.queries || [];
|
||||
metrics.anchor.queryExpansionTime = Math.round(performance.now() - T_QE_Start);
|
||||
metrics.timing.queryExpansion = metrics.anchor.queryExpansionTime;
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Step 2: 向量化查询
|
||||
@@ -604,54 +713,70 @@ export async function recallMemory(queryText, allEvents, vectorConfig, options =
|
||||
} catch (e) {
|
||||
xbLog.error(MODULE_ID, '向量化失败', e);
|
||||
metrics.timing.total = Math.round(performance.now() - T0);
|
||||
return { events: [], chunks: [], causalEvents: [], focusEntities, elapsed: metrics.timing.total, logText: 'Embedding failed.', metrics };
|
||||
return {
|
||||
events: [],
|
||||
evidenceChunks: [],
|
||||
causalChain: [],
|
||||
focusEntities,
|
||||
elapsed: metrics.timing.total,
|
||||
logText: 'Embedding failed.',
|
||||
metrics,
|
||||
};
|
||||
}
|
||||
|
||||
if (!queryVector?.length) {
|
||||
metrics.timing.total = Math.round(performance.now() - T0);
|
||||
return { events: [], chunks: [], causalEvents: [], focusEntities, elapsed: metrics.timing.total, logText: 'Empty query vector.', metrics };
|
||||
return {
|
||||
events: [],
|
||||
evidenceChunks: [],
|
||||
causalChain: [],
|
||||
focusEntities,
|
||||
elapsed: metrics.timing.total,
|
||||
logText: 'Empty query vector.',
|
||||
metrics,
|
||||
};
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Step 3: L0 检索
|
||||
// Step 3: Anchor (L0) 检索
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
|
||||
const T_L0_Start = performance.now();
|
||||
const T_Anchor_Start = performance.now();
|
||||
|
||||
const { atoms: l0Atoms, floors: l0Floors } = await searchL0(queryVector, vectorConfig, metrics);
|
||||
const { hits: anchorHits, floors: anchorFloors } = await recallAnchors(queryVector, vectorConfig, metrics);
|
||||
|
||||
metrics.timing.l0Search = Math.round(performance.now() - T_L0_Start);
|
||||
metrics.timing.anchorSearch = Math.round(performance.now() - T_Anchor_Start);
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Step 4: L3 拉取 + L1 粗筛 + Rerank
|
||||
// Step 4: Evidence (L1) 拉取 + 粗筛 + Rerank
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
|
||||
const T_L3_Start = performance.now();
|
||||
const T_Evidence_Start = performance.now();
|
||||
|
||||
const rerankQuery = buildRerankQuery(expansion, lastMessages, pendingUserMessage);
|
||||
const chunks = await getChunksFromL0Floors(l0Floors, l0Atoms, queryVector, rerankQuery, metrics);
|
||||
const evidenceChunks = await pullEvidenceByFloors(anchorFloors, anchorHits, queryVector, rerankQuery, metrics);
|
||||
|
||||
metrics.timing.l3Retrieval = Math.round(performance.now() - T_L3_Start);
|
||||
metrics.timing.evidenceRetrieval = Math.round(performance.now() - T_Evidence_Start);
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Step 5: L2 独立检索
|
||||
// Step 5: Event (L2) 独立检索
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
|
||||
const T_L2_Start = performance.now();
|
||||
const T_Event_Start = performance.now();
|
||||
|
||||
const eventResults = await searchL2Events(queryVector, allEvents, vectorConfig, focusEntities, metrics);
|
||||
const eventHits = await recallEvents(queryVector, allEvents, vectorConfig, focusEntities, metrics);
|
||||
|
||||
metrics.timing.l2Retrieval = Math.round(performance.now() - T_L2_Start);
|
||||
metrics.timing.eventRetrieval = Math.round(performance.now() - T_Event_Start);
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// Step 6: 因果链追溯
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
|
||||
const eventIndex = buildEventIndex(allEvents);
|
||||
const { results: causalMap, maxDepth: causalMaxDepth } = traceCausalAncestors(eventResults, eventIndex);
|
||||
const { results: causalMap, maxDepth: causalMaxDepth } = traceCausation(eventHits, eventIndex);
|
||||
|
||||
const recalledIdSet = new Set(eventResults.map(x => x?.event?.id).filter(Boolean));
|
||||
const causalEvents = causalMap
|
||||
const recalledIdSet = new Set(eventHits.map(x => x?.event?.id).filter(Boolean));
|
||||
const causalChain = causalMap
|
||||
.filter(x => x?.event?.id && !recalledIdSet.has(x.event.id))
|
||||
.map(x => ({
|
||||
event: x.event,
|
||||
@@ -661,11 +786,11 @@ export async function recallMemory(queryText, allEvents, vectorConfig, options =
|
||||
chainFrom: x.chainFrom,
|
||||
}));
|
||||
|
||||
if (metrics.l2.byRecallType) {
|
||||
metrics.l2.byRecallType.causal = causalEvents.length;
|
||||
if (metrics.event.byRecallType) {
|
||||
metrics.event.byRecallType.causal = causalChain.length;
|
||||
}
|
||||
metrics.l2.causalChainDepth = causalMaxDepth;
|
||||
metrics.l2.causalEventsCount = causalEvents.length;
|
||||
metrics.event.causalChainDepth = causalMaxDepth;
|
||||
metrics.event.causalCount = causalChain.length;
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════
|
||||
// 完成
|
||||
@@ -673,24 +798,24 @@ export async function recallMemory(queryText, allEvents, vectorConfig, options =
|
||||
|
||||
metrics.timing.total = Math.round(performance.now() - T0);
|
||||
|
||||
metrics.l2.entityNames = focusEntities;
|
||||
metrics.l2.entitiesLoaded = focusEntities.length;
|
||||
metrics.event.entityNames = focusEntities;
|
||||
metrics.event.entitiesUsed = focusEntities.length;
|
||||
|
||||
console.group('%c[Recall v4]', 'color: #7c3aed; font-weight: bold');
|
||||
console.group('%c[Recall v5]', 'color: #7c3aed; font-weight: bold');
|
||||
console.log(`Elapsed: ${metrics.timing.total}ms`);
|
||||
console.log(`Query Expansion: focus=[${expansion.focus.join(', ')}]`);
|
||||
console.log(`L0: ${l0Atoms.length} atoms → ${l0Floors.size} floors`);
|
||||
console.log(`L3: ${metrics.l3.l1Total || 0} L1 → ${metrics.l3.l1AfterCoarse || 0} 粗筛 → ${chunks.length} final`);
|
||||
if (metrics.l3.rerankApplied) {
|
||||
console.log(`L3 Rerank: ${metrics.l3.beforeRerank} → ${metrics.l3.afterRerank} (${metrics.l3.rerankTime}ms)`);
|
||||
console.log(`Anchors: ${anchorHits.length} hits → ${anchorFloors.size} floors`);
|
||||
console.log(`Evidence: ${metrics.evidence.chunkTotal || 0} L1 → ${metrics.evidence.chunkAfterCoarse || 0} coarse → ${evidenceChunks.length} final`);
|
||||
if (metrics.evidence.rerankApplied) {
|
||||
console.log(`Evidence Rerank: ${metrics.evidence.beforeRerank} → ${metrics.evidence.afterRerank} (${metrics.evidence.rerankTime}ms)`);
|
||||
}
|
||||
console.log(`L2: ${eventResults.length} events, ${causalEvents.length} causal`);
|
||||
console.log(`Events: ${eventHits.length} hits, ${causalChain.length} causal`);
|
||||
console.groupEnd();
|
||||
|
||||
return {
|
||||
events: eventResults,
|
||||
causalEvents,
|
||||
chunks,
|
||||
events: eventHits,
|
||||
causalChain,
|
||||
evidenceChunks,
|
||||
expansion,
|
||||
focusEntities,
|
||||
elapsed: metrics.timing.total,
|
||||
|
||||
Reference in New Issue
Block a user