fix: Address code review - extract shared importance calc, fix greeting filter, configurable retry

Agent-Logs-Url: https://github.com/qinfendebingshuo/guanghulab/sessions/2e09fb7c-c501-4223-8734-5afac689ded9

Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-04-12 09:45:54 +00:00 committed by GitHub
parent 088ef74de2
commit c033e82ac3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 16 deletions

View File

@ -196,12 +196,7 @@ async function chat(userId, userMessage) {
// 7. 记录到人格体记忆(异步,不阻塞响应) // 7. 记录到人格体记忆(异步,不阻塞响应)
if (personaMemory) { if (personaMemory) {
let importance = 30; const importance = personaMemory.calculateImportance(userMessage);
if (userMessage.length > 200) importance += 20;
if (/冰朔|主权|系统|架构|重要/i.test(userMessage)) importance += 30;
if (/记住|记忆|记录|保存/i.test(userMessage)) importance += 20;
importance = Math.min(importance, 100);
personaMemory.recordConversationMemory(userId, userMessage, assistantMessage); personaMemory.recordConversationMemory(userId, userMessage, assistantMessage);
personaMemory.growConversationLeaf(userId, userMessage, assistantMessage, importance); personaMemory.growConversationLeaf(userId, userMessage, assistantMessage, importance);
} }

View File

@ -34,6 +34,7 @@ try {
} }
const PERSONA_ID = 'zhuyuan'; const PERSONA_ID = 'zhuyuan';
const DB_RETRY_INTERVAL = 30000; // 30s retry for DB reconnection
// ─── 连接池(延迟初始化) ─── // ─── 连接池(延迟初始化) ───
let dbReady = false; let dbReady = false;
@ -70,7 +71,7 @@ function getPool() {
dbReady = true; dbReady = true;
console.log('[记忆桥接] ✅ 数据库重连成功'); console.log('[记忆桥接] ✅ 数据库重连成功');
} catch (_) { /* still down */ } } catch (_) { /* still down */ }
}, 30000); }, DB_RETRY_INTERVAL);
if (dbCheckTimer.unref) dbCheckTimer.unref(); if (dbCheckTimer.unref) dbCheckTimer.unref();
} }
return pool; return pool;
@ -321,6 +322,17 @@ async function buildSystemPrompt(userId) {
// ─── 记忆写入(异步,不阻塞聊天) ─── // ─── 记忆写入(异步,不阻塞聊天) ───
/**
* 计算对话重要性(共享逻辑)
*/
function calculateImportance(userMessage) {
let importance = 30; // 默认普通对话
if (userMessage.length > 200) importance += 20;
if (/冰朔|主权|系统|架构|重要/i.test(userMessage)) importance += 30;
if (/记住|记忆|记录|保存/i.test(userMessage)) importance += 20;
return Math.min(importance, 100);
}
/** /**
* 记录对话到记忆锚点(异步后台执行) * 记录对话到记忆锚点(异步后台执行)
*/ */
@ -328,8 +340,8 @@ function recordConversationMemory(userId, userMessage, personaReply) {
// 仅在 DB 可用时记录 // 仅在 DB 可用时记录
if (!dbReady) return; if (!dbReady) return;
// 简单对话不记录(节省空间) // 简单问候不记录(节省空间)
if (userMessage.length < 10 && /^(你好|hi|hello|嗨|在吗|早|晚安|谢谢|ok|好的)/i.test(userMessage)) { if (/^(你好|hi|hello|嗨|在吗|早|晚安|谢谢|ok|好的)\b/i.test(userMessage) && userMessage.length < 20) {
return; return;
} }
@ -337,13 +349,7 @@ function recordConversationMemory(userId, userMessage, personaReply) {
setImmediate(async () => { setImmediate(async () => {
try { try {
const today = new Date().toISOString().slice(0, 10); const today = new Date().toISOString().slice(0, 10);
const importance = calculateImportance(userMessage);
// 计算重要性(基于对话长度和内容)
let importance = 30; // 默认普通对话
if (userMessage.length > 200) importance += 20;
if (/冰朔|主权|系统|架构|重要/i.test(userMessage)) importance += 30;
if (/记住|记忆|记录|保存/i.test(userMessage)) importance += 20;
importance = Math.min(importance, 100);
// 截断过长消息 // 截断过长消息
const maxLen = 500; const maxLen = 500;
@ -445,6 +451,7 @@ module.exports = {
buildSystemPrompt, buildSystemPrompt,
recordConversationMemory, recordConversationMemory,
growConversationLeaf, growConversationLeaf,
calculateImportance,
getMemoryStatus, getMemoryStatus,
invalidateCache, invalidateCache,
STATIC_PERSONA_PROMPT STATIC_PERSONA_PROMPT