Skip to content

Commit 09d4630

Browse files
Merge pull request #36 from MisbahAN/final-backend-voice
finallll tryyyy
2 parents b38a75d + 12293df commit 09d4630

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

api/lessons/[uid].ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export default async function handler(
5757
const { uid } = req.query;
5858

5959
if (!uid || typeof uid !== "string") {
60+
setCorsHeaders(res);
6061
return res.status(400).json({ error: "Missing or invalid uid parameter" });
6162
}
6263

@@ -68,13 +69,15 @@ export default async function handler(
6869
const user = await usersCollection.findOne({ firebaseUid: uid });
6970

7071
if (!user) {
72+
setCorsHeaders(res);
7173
return res.status(404).json({ error: "User not found" });
7274
}
7375

7476
// Get user's lessons
7577
const lessonData = await lessonsCollection.findOne({ firebaseUid: uid });
7678

7779
if (!lessonData) {
80+
setCorsHeaders(res);
7881
return res.status(404).json({
7982
error: "No lessons found",
8083
message: "Lessons have not been generated for this user yet"
@@ -166,6 +169,7 @@ export default async function handler(
166169

167170
} catch (error) {
168171
console.error("Error fetching lessons:", error);
172+
setCorsHeaders(res); // Ensure CORS headers on error responses
169173
return res.status(500).json({
170174
error: "Failed to fetch lessons",
171175
message: error instanceof Error ? error.message : "Unknown error",

api/lessons/generate.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,10 @@ Q&A: ${qaText}
124124
125125
REQUIREMENTS:
126126
- ${lessonCount} progressive lessons (${isFirstBatch ? 'fundamentals' : 'intermediate to advanced'})
127-
- Each lesson: 2-3 reading cards (100-150 words each), 2 MCQ cards (4 options), 1 summary card
128-
- Canadian context (TFSA, RRSP)
127+
- Each lesson: 1-2 reading cards (50-100 words each), 2 MCQ cards (4 options), 1 summary card
128+
- Canadian context (TFSA, RRSP), Finny's voice
129129
- XP: Lessons 1-3 = 30, 4-7 = 35-40, 8-10 = 40-50
130-
- Engaging titles, Finny's voice
131-
${isFirstBatch ? '' : '- These lessons should build on fundamentals from lessons 1-3'}
130+
${isFirstBatch ? '' : '- Build on fundamentals from lessons 1-3'}
132131
133132
Return ONLY JSON (no markdown):
134133
{
@@ -144,10 +143,10 @@ Return ONLY JSON (no markdown):
144143
"prerequisites": [],
145144
"xpReward": ${startLesson <= 3 ? 30 : startLesson <= 7 ? 35 : 40},
146145
"cards": [
147-
{"type": "reading", "content": "100-150 word explanation"},
148-
{"type": "mcq", "question": "Question?", "options": ["A", "B", "C", "D"], "correctIndex": 0, "explanation": "Why correct"},
149-
{"type": "reading", "content": "100-150 words"},
150-
{"type": "mcq", "question": "Question?", "options": ["A", "B", "C", "D"], "correctIndex": 1, "explanation": "Why correct"},
146+
{"type": "reading", "content": "50-100 word explanation"},
147+
{"type": "mcq", "question": "Question?", "options": ["A", "B", "C", "D"], "correctIndex": 0, "explanation": "Brief explanation"},
148+
{"type": "reading", "content": "50-100 words"},
149+
{"type": "mcq", "question": "Question?", "options": ["A", "B", "C", "D"], "correctIndex": 1, "explanation": "Brief explanation"},
151150
{"type": "summary", "xpEarned": ${startLesson <= 3 ? 30 : startLesson <= 7 ? 35 : 40}, "streakDays": 1}
152151
]
153152
}
@@ -163,18 +162,18 @@ Return ONLY JSON (no markdown):
163162
try {
164163
const model = genAI.getGenerativeModel({ model: modelName });
165164

166-
// Use minimal thinking for faster models to speed up generation
165+
// Use minimal thinking for ALL flash/lite models to speed up generation
167166
const generationConfig: { thinkingConfig: { thinkingLevel: "MINIMAL" } } | undefined =
168-
modelName === "gemini-3-flash-preview"
167+
(modelName.includes("flash") || modelName.includes("lite"))
169168
? {
170169
thinkingConfig: {
171170
thinkingLevel: "MINIMAL" as const
172171
}
173172
}
174173
: undefined;
175174

176-
// Set timeout: 30s for fast models, 60s for slower ones
177-
const timeout = modelName.includes("flash") || modelName.includes("lite") ? 30000 : 60000;
175+
// Set timeout: 20s for fast models, 40s for slower ones (reduced for faster failure)
176+
const timeout = modelName.includes("flash") || modelName.includes("lite") ? 20000 : 40000;
178177

179178
const generateContentPromise = model.generateContent(
180179
prompt,
@@ -376,6 +375,7 @@ export default async function handler(
376375

377376
if (!firebaseUid) {
378377
console.error("=== API ERROR: Missing firebaseUid ===");
378+
setCorsHeaders(res);
379379
return res.status(400).json({ error: "Missing firebaseUid" });
380380
}
381381

@@ -391,6 +391,7 @@ export default async function handler(
391391

392392
if (!user) {
393393
console.error("=== API ERROR: User not found ===");
394+
setCorsHeaders(res);
394395
return res.status(404).json({ error: "User not found" });
395396
}
396397

@@ -399,6 +400,7 @@ export default async function handler(
399400
console.log("onboardingCompleted:", user.onboardingCompleted);
400401
if (!user.onboardingCompleted) {
401402
console.error("=== API ERROR: Onboarding not completed ===");
403+
setCorsHeaders(res);
402404
return res.status(400).json({
403405
error: "Onboarding not completed",
404406
message: "User must complete onboarding before generating lessons"
@@ -510,6 +512,7 @@ export default async function handler(
510512

511513
} catch (error) {
512514
console.error("Error generating lessons:", error);
515+
setCorsHeaders(res); // Ensure CORS headers on error responses
513516
return res.status(500).json({
514517
error: "Failed to generate lessons",
515518
message: error instanceof Error ? error.message : "Unknown error",

0 commit comments

Comments
 (0)