Spaces:
Sleeping
Sleeping
Upload 64 files
Browse files- ai-routes.js +5 -2
- ai-tools.js +10 -5
ai-routes.js
CHANGED
|
@@ -230,8 +230,11 @@ router.post('/chat', checkAIAccess, async (req, res) => {
|
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
-
// 4. Save Final Answer
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
| 235 |
recordUsage('agent-response', 'AGENT');
|
| 236 |
|
| 237 |
// 5. Send Done Signal
|
|
|
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
+
// 4. Save Final Answer (Prevent Empty Text Error)
|
| 234 |
+
if (finalResponseText && finalResponseText.trim().length > 0) {
|
| 235 |
+
await ChatHistoryModel.create({ userId: user._id, role: 'model', text: finalResponseText });
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
recordUsage('agent-response', 'AGENT');
|
| 239 |
|
| 240 |
// 5. Send Done Signal
|
ai-tools.js
CHANGED
|
@@ -20,7 +20,7 @@ const mongoTools = [
|
|
| 20 |
},
|
| 21 |
filter: {
|
| 22 |
type: "OBJECT",
|
| 23 |
-
description: "Mongoose查询条件JSON。例: {className:'一年级(1)班', name:'张三'}。注意:查询班级时必须使用 'className' 字段。",
|
| 24 |
},
|
| 25 |
limit: {
|
| 26 |
type: "NUMBER",
|
|
@@ -198,6 +198,7 @@ async function executeMongoTool(functionCall, user, role, schoolId) {
|
|
| 198 |
try {
|
| 199 |
let result = [];
|
| 200 |
let fields = "";
|
|
|
|
| 201 |
|
| 202 |
// 🔥 关系查询增强:Score 表没有 className,需要先查 Student 获取 studentNo
|
| 203 |
if (collection === 'Score' && safeFilter.className) {
|
|
@@ -209,15 +210,16 @@ async function executeMongoTool(functionCall, user, role, schoolId) {
|
|
| 209 |
className: safeFilter.className // 这里已经是正则了
|
| 210 |
};
|
| 211 |
|
| 212 |
-
const studentsInClass = await Student.find(studentQuery).select('studentNo');
|
| 213 |
const studentNos = studentsInClass.map(s => s.studentNo);
|
| 214 |
|
| 215 |
if (studentNos.length === 0) {
|
| 216 |
-
console.log(`⚠️ [Relational] No students found in class: ${safeFilter.className}`);
|
| 217 |
-
return { info:
|
| 218 |
}
|
| 219 |
|
| 220 |
console.log(`✅ [Relational] Found ${studentNos.length} students. Translating to Score query...`);
|
|
|
|
| 221 |
|
| 222 |
// 替换查询条件:移除 className,添加 studentNo $in
|
| 223 |
delete safeFilter.className;
|
|
@@ -255,7 +257,10 @@ async function executeMongoTool(functionCall, user, role, schoolId) {
|
|
| 255 |
console.log(`===================================================\n`);
|
| 256 |
|
| 257 |
if (result.length === 0) {
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
| 259 |
}
|
| 260 |
return result;
|
| 261 |
|
|
|
|
| 20 |
},
|
| 21 |
filter: {
|
| 22 |
type: "OBJECT",
|
| 23 |
+
description: "Mongoose查询条件JSON。例: {className:'一年级(1)班', name:'张三'}。注意:查询班级时必须使用 'className' (禁止用 'class') 字段。",
|
| 24 |
},
|
| 25 |
limit: {
|
| 26 |
type: "NUMBER",
|
|
|
|
| 198 |
try {
|
| 199 |
let result = [];
|
| 200 |
let fields = "";
|
| 201 |
+
let relationalInfo = "";
|
| 202 |
|
| 203 |
// 🔥 关系查询增强:Score 表没有 className,需要先查 Student 获取 studentNo
|
| 204 |
if (collection === 'Score' && safeFilter.className) {
|
|
|
|
| 210 |
className: safeFilter.className // 这里已经是正则了
|
| 211 |
};
|
| 212 |
|
| 213 |
+
const studentsInClass = await Student.find(studentQuery).select('studentNo name');
|
| 214 |
const studentNos = studentsInClass.map(s => s.studentNo);
|
| 215 |
|
| 216 |
if (studentNos.length === 0) {
|
| 217 |
+
console.log(`⚠️ [Relational] No students found in class matching: ${JSON.stringify(safeFilter.className)}`);
|
| 218 |
+
return { info: `未找到班级符合条件的学生(查询条件:${JSON.stringify(safeFilter.className)}),无法查询成绩。` };
|
| 219 |
}
|
| 220 |
|
| 221 |
console.log(`✅ [Relational] Found ${studentNos.length} students. Translating to Score query...`);
|
| 222 |
+
relationalInfo = `(已找到 ${studentNos.length} 名学生,正在查询他们的成绩)`;
|
| 223 |
|
| 224 |
// 替换查询条件:移除 className,添加 studentNo $in
|
| 225 |
delete safeFilter.className;
|
|
|
|
| 257 |
console.log(`===================================================\n`);
|
| 258 |
|
| 259 |
if (result.length === 0) {
|
| 260 |
+
let msg = "未找到符合条件的数据。";
|
| 261 |
+
if (relationalInfo) msg += " " + relationalInfo + " 但这些学生似乎还没有录入成绩。";
|
| 262 |
+
else msg += " 请尝试放宽查询条件,或确认班级/姓名格式(例如包含括号)。";
|
| 263 |
+
return { info: msg };
|
| 264 |
}
|
| 265 |
return result;
|
| 266 |
|