支持批量处理
This commit is contained in:
@@ -45,8 +45,11 @@ router.post('/:roomId/messages', authMiddleware, async (req, res) => {
|
||||
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, roomId);
|
||||
if (aiResponse && aiResponse.action !== 'ignore') {
|
||||
handleAIResult(aiResponse, roomId, username, text || '', attachments || []);
|
||||
if (aiResponse) {
|
||||
var responses = Array.isArray(aiResponse) ? aiResponse : [aiResponse];
|
||||
responses.forEach(function(r) {
|
||||
if (r.action !== 'ignore') handleAIResult(r, roomId, username, text || '', attachments || []);
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('AI 分析失败:', e.message);
|
||||
@@ -72,10 +75,20 @@ async function analyzeWithDeepSeek(text, historyMessages, username, isAdmin, roo
|
||||
const content = await callDeepSeek(messages, username);
|
||||
console.log('🤖 AI 返回:', content);
|
||||
try {
|
||||
const parsed = JSON.parse(content.replace(/```json|```/g, '').trim());
|
||||
normalize(parsed);
|
||||
const v = validate(parsed);
|
||||
if (!v.valid) console.warn('⚠️ AI 返回校验失败:', v.error);
|
||||
var cleaned = content.replace(/```json|```/g, '').trim();
|
||||
var parsed = JSON.parse(cleaned);
|
||||
// 支持批量:AI 可能返回数组
|
||||
if (Array.isArray(parsed)) {
|
||||
parsed.forEach(function(item, i) {
|
||||
normalize(item);
|
||||
var v = validate(item);
|
||||
if (!v.valid) console.warn('⚠️ AI 返回[' + i + ']校验失败:', v.error);
|
||||
});
|
||||
} else {
|
||||
normalize(parsed);
|
||||
var v = validate(parsed);
|
||||
if (!v.valid) console.warn('⚠️ AI 返回校验失败:', v.error);
|
||||
}
|
||||
return parsed;
|
||||
} catch (e) {
|
||||
// AI 返回非 JSON(如纯文本回复图片消息)→ 降级为 chat
|
||||
|
||||
Reference in New Issue
Block a user