feat: conversation long-term memory + fix source ENUM bug

- New: conversationSummarizer.js (LLM summary every 3 turns, loadBestSummary, persistFinalSummary)
- db/index.js: conversation_summaries table, upsertConversationSummary, getSessionSummary
- redisClient.js: setSummary/getSummary (TTL 2h)
- nativeVoiceGateway.js: _turnCount tracking, trigger summarize, persist on close
- realtimeDialogRouting.js: inject summary context, reduce history 5->3 rounds
- Fix: messages source ENUM missing 'search_knowledge' causing chat DB writes to fail
This commit is contained in:
User
2026-04-03 10:19:16 +08:00
parent 5b824cd16a
commit fe25229de7
6 changed files with 797 additions and 69 deletions

View File

@@ -10,6 +10,7 @@ const HISTORY_MAX_LEN = 10; // 5轮 × 2条/轮
const HISTORY_TTL_S = 1800; // 30分钟
const KB_CACHE_HIT_TTL_S = 300; // 5分钟
const KB_CACHE_NOHIT_TTL_S = 120; // 2分钟
const SUMMARY_TTL_S = parseInt(process.env.SUMMARY_REDIS_TTL_SECONDS) || 7200; // 2小时
// ============ 连接管理 ============
let client = null;
@@ -128,6 +129,32 @@ async function clearSession(sessionId) {
}
}
// ============ 对话摘要 ============
const summaryKey = (sessionId) => `voice:summary:${sessionId}`;
async function setSummary(sessionId, summary) {
if (!isAvailable() || !summary) return false;
try {
const key = summaryKey(sessionId);
await client.set(key, summary, 'EX', SUMMARY_TTL_S);
return true;
} catch (err) {
console.warn('[Redis] setSummary failed:', err.message);
return false;
}
}
async function getSummary(sessionId) {
if (!isAvailable()) return null;
try {
const key = summaryKey(sessionId);
return await client.get(key);
} catch (err) {
console.warn('[Redis] getSummary failed:', err.message);
return null;
}
}
// ============ KB 缓存 ============
const kbCacheKey = (cacheKey) => `kb_cache:${cacheKey}`;
@@ -178,6 +205,8 @@ module.exports = {
pushMessage,
getRecentHistory,
clearSession,
setSummary,
getSummary,
setKbCache,
getKbCache,
disconnect,