fix
All checks were successful
On Push to Main / test (push) Successful in 31s
On Push to Main / publish (push) Successful in 58s
On Push to Main / deploy-dev (push) Successful in 1m31s

This commit is contained in:
RaymondVerhoef
2026-05-25 21:38:53 +02:00
parent 80532b6d1b
commit 3c04bab1b9
3 changed files with 58 additions and 14 deletions

View File

@@ -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) {