Fix vector recall pending user message
This commit is contained in:
@@ -158,16 +158,28 @@ function buildExpDecayWeights(n, beta) {
|
||||
// Query 构建
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
function buildQuerySegments(chat, count, excludeLastAi) {
|
||||
if (!chat?.length) return [];
|
||||
|
||||
let messages = chat;
|
||||
if (excludeLastAi && messages.length > 0 && !messages[messages.length - 1]?.is_user) {
|
||||
messages = messages.slice(0, -1);
|
||||
}
|
||||
|
||||
return messages.slice(-count).map((m, idx, arr) => {
|
||||
const speaker = m.name || (m.is_user ? '用户' : '角色');
|
||||
function buildQuerySegments(chat, count, excludeLastAi, pendingUserMessage = null) {
|
||||
if (!chat?.length) return [];
|
||||
|
||||
let messages = chat;
|
||||
if (excludeLastAi && messages.length > 0 && !messages[messages.length - 1]?.is_user) {
|
||||
messages = messages.slice(0, -1);
|
||||
}
|
||||
|
||||
// ★ 如果有待处理的用户消息且 chat 中最后一条不是它,追加虚拟消息
|
||||
if (pendingUserMessage) {
|
||||
const lastMsg = messages[messages.length - 1];
|
||||
const lastMsgText = lastMsg?.mes?.trim() || "";
|
||||
const pendingText = pendingUserMessage.trim();
|
||||
|
||||
// 避免重复(如果 chat 已包含该消息则不追加)
|
||||
if (lastMsgText !== pendingText) {
|
||||
messages = [...messages, { is_user: true, name: "用户", mes: pendingUserMessage }];
|
||||
}
|
||||
}
|
||||
|
||||
return messages.slice(-count).map((m, idx, arr) => {
|
||||
const speaker = m.name || (m.is_user ? '用户' : '角色');
|
||||
const clean = stripNoise(m.mes);
|
||||
if (!clean) return '';
|
||||
const limit = idx === arr.length - 1 ? CONFIG.QUERY_MAX_CHARS : CONFIG.QUERY_CONTEXT_CHARS;
|
||||
@@ -669,16 +681,17 @@ function formatRecallLog({ elapsed, segments, weights, chunkResults, eventResult
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// 主入口
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
export async function recallMemory(queryText, allEvents, vectorConfig, options = {}) {
|
||||
const T0 = performance.now();
|
||||
const { chat } = getContext();
|
||||
const store = getSummaryStore();
|
||||
|
||||
if (!allEvents?.length) {
|
||||
return { events: [], chunks: [], elapsed: 0, logText: 'No events.' };
|
||||
}
|
||||
|
||||
|
||||
export async function recallMemory(queryText, allEvents, vectorConfig, options = {}) {
|
||||
const T0 = performance.now();
|
||||
const { chat } = getContext();
|
||||
const store = getSummaryStore();
|
||||
const { pendingUserMessage = null } = options;
|
||||
|
||||
if (!allEvents?.length) {
|
||||
return { events: [], chunks: [], elapsed: 0, logText: 'No events.' };
|
||||
}
|
||||
|
||||
const segments = buildQuerySegments(chat, CONFIG.QUERY_MSG_COUNT, !!options.excludeLastAi, pendingUserMessage);
|
||||
|
||||
let queryVector, weights;
|
||||
|
||||
Reference in New Issue
Block a user