fix issues
This commit is contained in:
@@ -28,7 +28,7 @@ function buildSystemPrompt(username, isAdmin) {
|
||||
const attachmentRules = `
|
||||
⚠️ 附件规则:
|
||||
- 图片+物品名("这是XX的图片")→ 只返回 purchase_item,绝对不要 is_new/quantity/unit_price
|
||||
- 引用历史图片("上面/前面/刚刚那张")→ use_recent_image: true
|
||||
- 引用历史图片("上面/前面/刚刚那张""存入附件/添加到附件/关联到")→ use_recent_image: true,**同时必须提供** purchase_item
|
||||
- 纯图片+物品名不是新建采购,系统自动关联附件`;
|
||||
|
||||
const prohibitionRules = `
|
||||
@@ -37,12 +37,14 @@ function buildSystemPrompt(username, isAdmin) {
|
||||
|
||||
const purchaseRules = `
|
||||
⚠️ 采购规则:
|
||||
- 系统会在 Prompt 末尾提供"当前房间采购清单",你必须根据清单判断用户是要更新已有记录还是新建。
|
||||
- 用户说"买完了/完成了/付过了" → 更新对应记录 status 为"已完成"
|
||||
- 只有明确消费意图(购买/采购/下单/付款/买了/花了)才返回 action="purchase"
|
||||
- 纯陈述(如"前天deep")→ action="ignore"
|
||||
- 信息不完整 → action="ask" 追问
|
||||
- 新建 vs 更新:
|
||||
* "昨天/今天/又/再/新/另一批"等重复购买 → is_new: true(强制新建)
|
||||
* "改一下/更新/修改/调整/更正/搞错了/不对/错了/不是XX是YY"等纠正意图 → 不提供 is_new(匹配已有更新)
|
||||
* "改一下/更新/修改/调整/更正/搞错了/不对/错了/不是XX是YY"等纠正意图 → 不提供 is_new(匹配已有更新),**购买清单中的名称是你的匹配线索**
|
||||
* "纯图片+物品名"不是新建,不要返回 is_new
|
||||
* 无法确定 → 默认 is_new: true
|
||||
- amount 由系统自动计算(quantity × unit_price + freight),AI 无需提供
|
||||
|
||||
@@ -97,6 +97,14 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) {
|
||||
}
|
||||
|
||||
if (aiResult.action === 'purchase') {
|
||||
// use_recent_image 但缺 purchase_item → 从房间最近一条采购推断
|
||||
if (aiResult.use_recent_image && (!aiResult.purchase_item || aiResult.purchase_item === 'null')) {
|
||||
const lastPurchase = db.prepare("SELECT item FROM purchases WHERE room_id = ? AND item IS NOT NULL AND item != '' ORDER BY created_at DESC LIMIT 1").get(roomId);
|
||||
if (lastPurchase) {
|
||||
console.log('🛡️ use_recent_image 缺 purchase_item,推断为:', lastPurchase.item);
|
||||
aiResult.purchase_item = lastPurchase.item;
|
||||
}
|
||||
}
|
||||
const item = aiResult.purchase_item;
|
||||
if (!item || item === 'null' || item === 'undefined' || item.trim().length < 2) {
|
||||
console.log('🚫 拒绝无效 purchase_item:', JSON.stringify(item));
|
||||
@@ -143,11 +151,10 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) {
|
||||
}
|
||||
console.log('📦 purchase_item:', aiResult.purchase_item, isNew ? '(强制新建)' : purchase ? '(匹配已有)' : '(未匹配-新建)');
|
||||
|
||||
// is_new 为 false 但匹配失败 → 拒绝新建,提示用户
|
||||
// is_new 未明确设为 false、且所有回退匹配均失败 → 视为新建
|
||||
if (!isNew && !purchase) {
|
||||
storeAndBroadcastText(roomId, '小财', `未找到与「${aiResult.purchase_item}」匹配的采购记录,无法更新。请检查物品名称是否正确。`);
|
||||
addToHistory(roomId, 'assistant', `未找到匹配记录`);
|
||||
return;
|
||||
console.log('📦 未匹配到已有记录,转为新建模式');
|
||||
isNew = true;
|
||||
}
|
||||
|
||||
let replyText = '';
|
||||
|
||||
@@ -44,15 +44,20 @@ router.post('/:roomId/messages', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const history = getHistory(roomId);
|
||||
const historyMessages = history.map(e => ({ role: e.role, content: e.content }));
|
||||
const aiResponse = await analyzeWithDeepSeek(text || '', historyMessages, username, req.user.isAdmin);
|
||||
const aiResponse = await analyzeWithDeepSeek(text || '', historyMessages, username, req.user.isAdmin, roomId);
|
||||
if (aiResponse && aiResponse.action !== 'ignore') {
|
||||
handleAIResult(aiResponse, roomId, username, text || '', attachments || []);
|
||||
}
|
||||
} catch (e) { console.error('AI 分析失败:', e); }
|
||||
});
|
||||
|
||||
async function analyzeWithDeepSeek(text, historyMessages, username, isAdmin) {
|
||||
const systemPrompt = buildSystemPrompt(username, isAdmin);
|
||||
async function analyzeWithDeepSeek(text, historyMessages, username, isAdmin, roomId) {
|
||||
// 注入当前房间采购清单上下文
|
||||
const purchases = db.prepare("SELECT item, quantity, amount, status, created_at FROM purchases WHERE room_id = ? ORDER BY created_at DESC LIMIT 10").all(roomId);
|
||||
const purchaseContext = purchases.length
|
||||
? `\n📋 当前房间采购清单(最近10条):\n${purchases.map(p => `- ${p.item} | ×${p.quantity} | ¥${p.amount} | ${p.status} | ${p.created_at}`).join('\n')}`
|
||||
: '';
|
||||
const systemPrompt = buildSystemPrompt(username, isAdmin) + purchaseContext;
|
||||
const messages = [
|
||||
{ role: 'system', content: systemPrompt },
|
||||
...historyMessages.slice(-30),
|
||||
|
||||
Reference in New Issue
Block a user