diff --git a/server/ai/validator.js b/server/ai/validator.js index f2f92eb..e250fd1 100644 --- a/server/ai/validator.js +++ b/server/ai/validator.js @@ -1,5 +1,6 @@ // JSON Schema 校验 — 验证 AI 返回的 JSON 结构 const schemas = { + ask: { required: [], optional: ['question','reply'] }, purchase: { required: ['purchase_item'], optional: ['quantity','unit_price','freight','payment_method','invoice_type','status','applicant','remarks','created_time','is_new','use_recent_image'] }, query: { required: [], optional: [] }, chat: { required: ['reply'], optional: [] }, diff --git a/server/handlers.js b/server/handlers.js index 5f7555d..7961143 100644 --- a/server/handlers.js +++ b/server/handlers.js @@ -127,17 +127,24 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) { } if (!isNew && !purchase) { purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ?').get(roomId, `%${aiResult.purchase_item}%`, '已完成'); - // 全名匹配失败 → 用物品名核心关键词(去括号内容)再次尝试 + // 全名匹配失败 → 去掉括号内容后用剩余关键词回退匹配 if (!purchase) { const keyword = aiResult.purchase_item.replace(/[((].*[))]/g, '').trim(); - if (keyword.length >= 2 && keyword !== aiResult.purchase_item) { + if (keyword.length >= 2) { console.log('🔍 全名不匹配,尝试关键词:', keyword); - purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ?').get(roomId, `%${keyword}%`, '已完成'); + purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ? ORDER BY created_at DESC LIMIT 1').get(roomId, `%${keyword}%`, '已完成'); } } } console.log('📦 purchase_item:', aiResult.purchase_item, isNew ? '(强制新建)' : purchase ? '(匹配已有)' : '(未匹配-新建)'); + // is_new 为 false 但匹配失败 → 拒绝新建,提示用户 + if (!isNew && !purchase) { + storeAndBroadcastText(roomId, '小财', `未找到与「${aiResult.purchase_item}」匹配的采购记录,无法更新。请检查物品名称是否正确。`); + addToHistory(roomId, 'assistant', `未找到匹配记录`); + return; + } + let replyText = ''; const quantity = aiResult.quantity || 1; const unitPrice = aiResult.unit_price || 0; diff --git a/server/ws/index.js b/server/ws/index.js index 8cb7e32..9abceed 100644 --- a/server/ws/index.js +++ b/server/ws/index.js @@ -7,7 +7,7 @@ const clients = new Map(); function init(wss) { wss.on('connection', (ws, req) => { - console.log('✅ WebSocket 客户端已连接'); + //console.log('✅ WebSocket 客户端已连接'); const url = new URL(req.url, 'http://localhost'); const token = url.searchParams.get('token'); if (!token) return ws.close();