优化缓存命中

This commit is contained in:
2026-07-29 10:10:31 +08:00
parent 886df10775
commit de2de85e58
2 changed files with 2 additions and 11 deletions

View File

@@ -168,8 +168,9 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) {
}
if (aiResult.action === 'query') {
storeAndBroadcastText(roomId, '小财', '🔍 正在查询采购数据,请稍候...');
const purchaseData = db.prepare('SELECT item, amount, payment_method, status FROM purchases WHERE room_id = ? ORDER BY created_at DESC').all(roomId).map(p => p.item + ' ' + p.amount + ' ' + (p.payment_method||'') + ' ' + p.status).join('\n');
const summaryPrompt = '\u6839\u636e\u4ee5\u4e0b\u91c7\u8d2d\u8bb0\u5f55\uff0c\u7528\u81ea\u7136\u8bed\u8a00\u56de\u7b54\u7528\u6237\u67e5\u8be2"' + originalText + '"' + '\u3002\u91c7\u8d2d\u8bb0\u5f55\uff1a\n' + (purchaseData || '\u6682\u65e0\u8bb0\u5f55');
const summaryPrompt = '根据以下采购记录,用自然语言回答用户查询"' + originalText + '"。采购记录:\n' + (purchaseData || '暂无记录');
callDeepSeekForSummary(summaryPrompt, username).then(reply => { storeAndBroadcastText(roomId, '小财', reply); addToHistory(roomId, 'assistant', reply); });
return;
}

View File

@@ -61,22 +61,12 @@ router.post('/:roomId/messages', authMiddleware, async (req, res) => {
});
async function analyzeWithDeepSeek(text, historyMessages, username, isAdmin, roomId) {
// 采购清单(单独消息)
var purchases = db.prepare("SELECT item, quantity, amount, status, created_at FROM purchases WHERE room_id = ? ORDER BY created_at DESC LIMIT 10").all(roomId);
var purchaseMsg = purchases.length
? '📋 当前房间采购清单最近10条\n' + purchases.map(function(p) { return '- ' + p.item + ' | ×' + p.quantity + ' | ¥' + p.amount + ' | ' + p.status + ' | ' + p.created_at; }).join('\n')
: '';
// 系统提示词(稳定,不含时间)
var systemPrompt = buildSystemPrompt(username, isAdmin);
var messages = [
{ role: 'system', content: systemPrompt }
];
// 全量历史,每 30 条一轮前缀稳定期,轮内全部命中
historyMessages.forEach(function(m) { messages.push(m); });
// 采购清单、时间放最后,变动不影响历史命中
if (purchaseMsg) messages.push({ role: 'system', content: purchaseMsg });
messages.push({ role: 'system', content: '当前服务器时间:' + new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false }) });
// 当前消息已在 historyMessages 中,不再重复 push
const content = await callDeepSeek(messages, username);
console.log('🤖 AI 返回:', content.substring(0, 200));
// 打印消息结构,方便排查缓存