dvc890 commited on
Commit
983b6b1
·
verified ·
1 Parent(s): d70a817

Update ai-routes.js

Browse files
Files changed (1) hide show
  1. ai-routes.js +11 -5
ai-routes.js CHANGED
@@ -445,10 +445,13 @@ router.post('/chat', async (req, res) => {
445
  const systemInstruction = await buildUserContext(username, userRole, schoolId);
446
 
447
  // Build History
448
- const geminiHistory = (history || []).map(msg => ({
449
- role: msg.role,
450
- parts: [{ text: msg.text || '' }]
451
- }));
 
 
 
452
 
453
  // Build Current Message Parts
454
  const currentParts = [];
@@ -478,8 +481,11 @@ router.post('/chat', async (req, res) => {
478
  }
479
 
480
  // Add Text if any (must have text if no media, or can accompany media)
481
- if (text) {
482
  currentParts.push({ text: text });
 
 
 
483
  } else if (currentParts.length === 0) {
484
  // Fallback if empty
485
  currentParts.push({ text: "." });
 
445
  const systemInstruction = await buildUserContext(username, userRole, schoolId);
446
 
447
  // Build History
448
+ // Filter out messages with empty text to prevent API errors
449
+ const geminiHistory = (history || [])
450
+ .filter(msg => msg.text && msg.text.trim() !== '')
451
+ .map(msg => ({
452
+ role: msg.role,
453
+ parts: [{ text: msg.text }]
454
+ }));
455
 
456
  // Build Current Message Parts
457
  const currentParts = [];
 
481
  }
482
 
483
  // Add Text if any (must have text if no media, or can accompany media)
484
+ if (text && text.trim()) {
485
  currentParts.push({ text: text });
486
+ } else if (audio) {
487
+ // Fix: If sending audio without text, add a prompt so the model knows what to do
488
+ currentParts.push({ text: "请听这段语音并回答。" });
489
  } else if (currentParts.length === 0) {
490
  // Fallback if empty
491
  currentParts.push({ text: "." });