fix
This commit is contained in:
@@ -79,10 +79,40 @@ Options must be prefixed "A) ", "B) ", "C) ", "D) ". Make questions specific and
|
||||
});
|
||||
|
||||
const emitted = result.toolUses[0]?.input;
|
||||
if (!emitted?.questions?.length) {
|
||||
throw new Error(`Could not generate questions for ${topic.label}`);
|
||||
if (!emitted) {
|
||||
throw new Error(`LLM did not emit tool output for ${topic.label}`);
|
||||
}
|
||||
return emitted.questions;
|
||||
|
||||
// Handle different shapes the LLM might return:
|
||||
// 1. { questions: [...] } — expected
|
||||
// 2. [...] — model returned array directly
|
||||
// 3. { questions: { ... } } — model wrapped single question in object
|
||||
let questions;
|
||||
if (Array.isArray(emitted.questions)) {
|
||||
questions = emitted.questions;
|
||||
} else if (Array.isArray(emitted)) {
|
||||
questions = emitted;
|
||||
} else if (emitted.questions && typeof emitted.questions === 'object') {
|
||||
// Single question wrapped as object instead of array
|
||||
questions = [emitted.questions];
|
||||
} else {
|
||||
throw new Error(`Unexpected quiz output shape for ${topic.label}`);
|
||||
}
|
||||
|
||||
if (!questions.length) {
|
||||
throw new Error(`No questions generated for ${topic.label}`);
|
||||
}
|
||||
|
||||
// Ensure each question has required fields (fill in defaults for missing ones)
|
||||
return questions.map((q, i) => ({
|
||||
id: q.id || `gen-${i}-${Math.random().toString(36).slice(2, 8)}`,
|
||||
question: q.question || '',
|
||||
topicLabel: q.topicLabel || topic.label,
|
||||
options: Array.isArray(q.options) ? q.options.slice(0, 4) : [],
|
||||
correctIndex: typeof q.correctIndex === 'number' ? q.correctIndex : 0,
|
||||
explanation: q.explanation || 'No explanation provided.',
|
||||
difficulty: q.difficulty || 'medium',
|
||||
})).filter(q => q.question && q.options.length === 4);
|
||||
}
|
||||
|
||||
async function selectTestTopics(userId, isoWeekNumber) {
|
||||
|
||||
Reference in New Issue
Block a user