diff --git a/backend/scripts/curriculum_worker.php b/backend/scripts/curriculum_worker.php index d6dd403..b3c998c 100644 --- a/backend/scripts/curriculum_worker.php +++ b/backend/scripts/curriculum_worker.php @@ -209,9 +209,17 @@ if ($code !== 200 || empty($res)) { $jsonResponse = json_decode($res, true); $text = $jsonResponse['candidates'][0]['content']['parts'][0]['text'] ?? ''; + +// Clean up markdown fences if Gemini returned them +$text = preg_replace('/^```json\s*/i', '', $text); +$text = preg_replace('/```\s*$/i', '', $text); +$text = trim($text); + $parsedStructure = json_decode($text, true); if (empty($parsedStructure) || empty($parsedStructure['units'])) { + // Log the actual text for debugging + file_put_contents($stateFile . '.debug.txt', $text); updateState($stateFile, 'error', 0, 'فشل الذكاء الاصطناعي في إرجاع بنية JSON صالحة.'); return; }