@learnpack/learnpack 5.0.363 → 5.0.364

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,6 +24,7 @@ const dotenv = require("dotenv");
24
24
  // import { v4 as uuidv4 } from "uuid"
25
25
  const creatorUtilities_1 = require("../utils/creatorUtilities");
26
26
  const lessonRecovery_1 = require("../utils/lessonRecovery");
27
+ const generationRetry_1 = require("../utils/generationRetry");
27
28
  // import { handleAssetCreation } from "./publish"
28
29
  const axios_1 = require("axios");
29
30
  const FormData = require("form-data");
@@ -52,6 +53,9 @@ const redis_1 = require("redis");
52
53
  const historyManager_1 = require("../managers/historyManager");
53
54
  const readmeHistoryService_1 = require("../managers/readmeHistoryService");
54
55
  const readmeSanitizer_1 = require("../utils/readmeSanitizer");
56
+ const componentsGuide_1 = require("../utils/interactivity/componentsGuide");
57
+ const interactivityPlan_1 = require("../utils/interactivity/interactivityPlan");
58
+ const structuredComponents_1 = require("../utils/interactivity/structuredComponents");
55
59
  // eslint-disable-next-line
56
60
  const frontMatter = require("front-matter");
57
61
  if (process.env.NEW_RELIC_ENABLED === "true") {
@@ -75,16 +79,20 @@ function translationsSummary(lesson) {
75
79
  function opId() {
76
80
  return crypto.randomBytes(4).toString("hex");
77
81
  }
78
- // Serialization per course to prevent syllabus race conditions
82
+ // Serialization per course to prevent syllabus race conditions. The queue lives
83
+ // in memory, so it only orders requests within one process: serve runs on a
84
+ // single Heroku dyno. With more than one, it has to move to Redis.
85
+ // A function running in the queue must not call serializeByCourse for the same
86
+ // course, or it waits for itself.
79
87
  const courseQueues = new Map();
80
88
  async function serializeByCourse(courseSlug, fn) {
81
89
  var _a;
82
90
  const prev = (_a = courseQueues.get(courseSlug)) !== null && _a !== void 0 ? _a : Promise.resolve();
91
+ // A failure belongs to its own caller. Without the catch, every function
92
+ // queued after it would be skipped and fail with the same error.
83
93
  const next = prev
94
+ .catch(() => null)
84
95
  .then(() => fn())
85
- .catch(error => {
86
- throw error;
87
- })
88
96
  .finally(() => {
89
97
  if (courseQueues.get(courseSlug) === next) {
90
98
  courseQueues.delete(courseSlug);
@@ -413,17 +421,45 @@ async function startInitialContentGeneration(rigoToken, steps, packageContext, e
413
421
  }
414
422
  return res.id;
415
423
  }
416
- async function startInteractivityGeneration(rigoToken, steps, packageContext, exercise, courseSlug, purposeSlug, bucket, lastLesson = "") {
424
+ async function startInteractivityGeneration(rigoToken, steps, packageContext, exercise, courseSlug, purposeSlug, bucket, lastLesson = "", options = {}) {
417
425
  const exSlug = (0, creatorUtilities_2.slugify)(exercise.id + "-" + exercise.title);
418
426
  console.log("Starting interactivity generation for", exSlug);
419
- const webhookUrl = `${process.env.HOST}/webhooks/${courseSlug}/interactivity-processor/${exercise.uid}/${rigoToken}`;
427
+ const request = (0, interactivityPlan_1.planInteractivityRequest)({
428
+ v2Enabled: (0, interactivityPlan_1.isInteractivityV2Enabled)(),
429
+ forceV1: options.forceV1,
430
+ });
431
+ // The structured template (v2) needs the components guide: without it the
432
+ // model does not know what each component field means. The guide is loaded
433
+ // before anything else so that rigobot is never called without it.
434
+ let componentsYml;
435
+ if (request.tpl === "v2") {
436
+ try {
437
+ componentsYml = (0, componentsGuide_1.buildInteractivityGuide)(await componentsGuide_1.componentsGuideLoader.load());
438
+ }
439
+ catch (error) {
440
+ if (error instanceof componentsGuide_1.ComponentsGuideUnavailableError) {
441
+ console.error("Components guide unavailable:", error.message);
442
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
443
+ lesson: exSlug,
444
+ status: "error",
445
+ log: `❌ Could not load the components guide for lesson ${exercise.title}; retry the generation`,
446
+ });
447
+ }
448
+ throw error;
449
+ }
450
+ }
451
+ else {
452
+ componentsYml = await fetchComponentsYml();
453
+ }
454
+ // The template and the attempt travel in the webhook URL: rigobot posts back
455
+ // to this exact URL, and its error payload says neither.
456
+ const webhookUrl = `${process.env.HOST}/webhooks/${courseSlug}/interactivity-processor/${exercise.uid}/${rigoToken}${request.webhookQuery}`;
420
457
  // Emit notification that interactivity generation is starting
421
458
  (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
422
459
  lesson: exSlug,
423
460
  status: "generating",
424
461
  log: `🔄 Starting interactivity generation for lesson: ${exercise.title}`,
425
462
  });
426
- const componentsYml = await fetchComponentsYml();
427
463
  const currentSyllabus = await getSyllabus(courseSlug, bucket);
428
464
  const fullSyllabus = {
429
465
  steps: steps.map(lessonCleaner),
@@ -440,7 +476,10 @@ async function startInteractivityGeneration(rigoToken, steps, packageContext, ex
440
476
  output_language: packageContext.language || "en",
441
477
  current_syllabus: JSON.stringify(fullSyllabus),
442
478
  lesson_info: JSON.stringify(lessonCleaner(exercise)),
443
- }, webhookUrl);
479
+ }, webhookUrl, request.slug);
480
+ if (!res) {
481
+ throw new Error(`Could not start interactivity generation for lesson ${exSlug} (${request.slug})`);
482
+ }
444
483
  return res.id;
445
484
  }
446
485
  async function createInitialReadme(tutorialInfo, tutorialSlug, rigoToken) {
@@ -555,6 +594,48 @@ async function updateLessonWithInitialContent(courseSlug, lessonUID, initialResp
555
594
  await saveSyllabus(courseSlug, syllabus, bucket);
556
595
  return lesson;
557
596
  }
597
+ /**
598
+ * Changes one lesson on a fresh read of the syllabus. Saving a copy read
599
+ * earlier would undo whatever was saved since, like the lesson's
600
+ * `initialContent`.
601
+ * @param courseSlug - The course.
602
+ * @param lessonUID - The lesson to change.
603
+ * @param bucket - The bucket that holds the syllabus.
604
+ * @param patch - Changes the lesson in place. Gets the course language.
605
+ * @returns Once the syllabus is saved, or right away if the lesson is gone.
606
+ */
607
+ async function patchLesson(courseSlug, lessonUID, bucket, patch) {
608
+ const syllabus = await getSyllabus(courseSlug, bucket);
609
+ const lesson = syllabus.lessons.find(lesson => lesson.uid === lessonUID);
610
+ if (!lesson) {
611
+ console.error(`Lesson ${lessonUID} not found in syllabus`);
612
+ return;
613
+ }
614
+ patch(lesson, syllabus.courseInfo.language || "en");
615
+ await saveSyllabus(courseSlug, syllabus, bucket);
616
+ }
617
+ /**
618
+ * The translation entry of a lesson that waits for a new job.
619
+ * @param completionId - The Rigobot job.
620
+ * @param autoRetries - The automatic retries of the current phase so far.
621
+ * @returns The entry to save under the course language.
622
+ */
623
+ const startedJob = (completionId, autoRetries = 0) => ({
624
+ completionId,
625
+ startedAt: Date.now(),
626
+ completedAt: 0,
627
+ autoRetries,
628
+ });
629
+ /**
630
+ * The creator log for a failed job that is not retried.
631
+ * @param reason - Why there is no retry.
632
+ * @param phase - What the job generated.
633
+ * @param exSlug - The lesson slug.
634
+ * @returns The log line.
635
+ */
636
+ const noRetryLog = (reason, phase, exSlug) => reason === "permanent" ?
637
+ `❌ Error generating ${phase} for lesson ${exSlug}. Retrying cannot fix this error, so it was not retried` :
638
+ `❌ Error generating ${phase} for lesson ${exSlug} after ${generationRetry_1.MAX_AUTO_RETRIES} retries`;
558
639
  async function updateLessonStatusToError(courseSlug, lessonUID, bucket) {
559
640
  try {
560
641
  const syllabus = await getSyllabus(courseSlug, bucket);
@@ -1419,60 +1500,61 @@ class ServeCommand extends SessionCommand_1.default {
1419
1500
  if (!rigoToken) {
1420
1501
  throw new errorHandler_1.ValidationError("Rigo token is required. x-rigo-token header is missing");
1421
1502
  }
1422
- const syllabus = await getSyllabus(courseSlug, bucket);
1423
- const exercise = syllabus.lessons.find(lesson => lesson.uid === lessonUid);
1424
- const exerciseIndex = syllabus.lessons.findIndex(lesson => lesson.uid === lessonUid);
1425
- // Validate that exercise exists
1426
- if (!exercise) {
1427
- throw new errorHandler_1.NotFoundError(`Lesson with UID "${lessonUid}" not found in course "${courseSlug}"`);
1428
- }
1429
- // Validate that exercise index is valid (defensive check)
1430
- if (exerciseIndex === -1) {
1431
- throw new errorHandler_1.NotFoundError(`Lesson with UID "${lessonUid}" not found in course "${courseSlug}"`);
1432
- }
1433
- // Validate that exercise has required properties
1434
- if (!exercise.id || !exercise.title) {
1435
- throw new errorHandler_1.ValidationError(`Lesson "${lessonUid}" is missing required properties (id or title)`);
1436
- }
1437
- // previous exercise
1438
- let previousReadme = "---";
1439
- const previousExercise = syllabus.lessons[exerciseIndex - 1];
1440
- if (previousExercise) {
1441
- // Get the readme of the previous exercise
1442
- const exSlug = (0, creatorUtilities_2.slugify)(previousExercise.id + "-" + previousExercise.title);
1443
- // llist the files un
1444
- const [files] = await bucket.getFiles({
1445
- prefix: `courses/${courseSlug}/exercises/${exSlug}/README`,
1446
- });
1447
- // select any README
1448
- const readmeFiles = files.map(f => f.name);
1449
- const readmeFile = readmeFiles.find(f => f.includes("README"));
1450
- if (readmeFile) {
1451
- const [content] = await bucket.file(readmeFile).download();
1452
- previousReadme = content.toString();
1503
+ // In the course queue, so the webhook of this job cannot be handled
1504
+ // before the lesson is saved as generating.
1505
+ await serializeByCourse(courseSlug, async () => {
1506
+ const syllabus = await getSyllabus(courseSlug, bucket);
1507
+ const exercise = syllabus.lessons.find(lesson => lesson.uid === lessonUid);
1508
+ const exerciseIndex = syllabus.lessons.findIndex(lesson => lesson.uid === lessonUid);
1509
+ // Validate that exercise exists
1510
+ if (!exercise) {
1511
+ throw new errorHandler_1.NotFoundError(`Lesson with UID "${lessonUid}" not found in course "${courseSlug}"`);
1453
1512
  }
1454
- }
1455
- // Use new two-phase generation workflow
1456
- const completionId = await startInitialContentGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, previousReadme +
1457
- "\n\nThe user provided the following feedback related to the content of the course so far: \n\n" +
1458
- feedback);
1459
- syllabus.lessons[exerciseIndex].status = "GENERATING";
1460
- syllabus.lessons[exerciseIndex].translations = {
1461
- [syllabus.courseInfo.language || "en"]: {
1462
- completionId,
1463
- startedAt: Date.now(),
1464
- completedAt: 0,
1465
- },
1466
- };
1467
- console.log("Lesson", syllabus.lessons[exerciseIndex]);
1468
- if (syllabus.feedback && typeof syllabus.feedback === "string") {
1469
- syllabus.feedback += "\n\n" + feedback;
1470
- }
1471
- else {
1472
- syllabus.feedback = feedback;
1473
- }
1474
- syllabus.generationMode = mode;
1475
- await saveSyllabus(courseSlug, syllabus, bucket);
1513
+ // Validate that exercise index is valid (defensive check)
1514
+ if (exerciseIndex === -1) {
1515
+ throw new errorHandler_1.NotFoundError(`Lesson with UID "${lessonUid}" not found in course "${courseSlug}"`);
1516
+ }
1517
+ // Validate that exercise has required properties
1518
+ if (!exercise.id || !exercise.title) {
1519
+ throw new errorHandler_1.ValidationError(`Lesson "${lessonUid}" is missing required properties (id or title)`);
1520
+ }
1521
+ // previous exercise
1522
+ let previousReadme = "---";
1523
+ const previousExercise = syllabus.lessons[exerciseIndex - 1];
1524
+ if (previousExercise) {
1525
+ // Get the readme of the previous exercise
1526
+ const exSlug = (0, creatorUtilities_2.slugify)(previousExercise.id + "-" + previousExercise.title);
1527
+ // llist the files un
1528
+ const [files] = await bucket.getFiles({
1529
+ prefix: `courses/${courseSlug}/exercises/${exSlug}/README`,
1530
+ });
1531
+ // select any README
1532
+ const readmeFiles = files.map(f => f.name);
1533
+ const readmeFile = readmeFiles.find(f => f.includes("README"));
1534
+ if (readmeFile) {
1535
+ const [content] = await bucket.file(readmeFile).download();
1536
+ previousReadme = content.toString();
1537
+ }
1538
+ }
1539
+ // Use new two-phase generation workflow
1540
+ const completionId = await startInitialContentGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, previousReadme +
1541
+ "\n\nThe user provided the following feedback related to the content of the course so far: \n\n" +
1542
+ feedback);
1543
+ // A new job by the user, so the automatic retries start over.
1544
+ syllabus.lessons[exerciseIndex].status = "GENERATING";
1545
+ syllabus.lessons[exerciseIndex].translations = {
1546
+ [syllabus.courseInfo.language || "en"]: startedJob(completionId),
1547
+ };
1548
+ console.log("Lesson", syllabus.lessons[exerciseIndex]);
1549
+ if (syllabus.feedback && typeof syllabus.feedback === "string") {
1550
+ syllabus.feedback += "\n\n" + feedback;
1551
+ }
1552
+ else {
1553
+ syllabus.feedback = feedback;
1554
+ }
1555
+ syllabus.generationMode = mode;
1556
+ await saveSyllabus(courseSlug, syllabus, bucket);
1557
+ });
1476
1558
  res.json({ status: "SUCCESS" });
1477
1559
  }));
1478
1560
  // TODO: Check if this command is being used
@@ -1742,112 +1824,132 @@ class ServeCommand extends SessionCommand_1.default {
1742
1824
  const { courseSlug, lessonUID, rigoToken } = req.params;
1743
1825
  const response = req.body;
1744
1826
  console.log("RECEIVING INITIAL CONTENT WEBHOOK", response);
1745
- const syllabus = await getSyllabus(courseSlug, bucket);
1746
- const exerciseIndex = syllabus.lessons.findIndex(lesson => lesson.uid === lessonUID);
1747
- if (exerciseIndex === -1) {
1748
- console.error("Exercise not found receiving webhook:", lessonUID);
1749
- return res.json({ status: "ERROR", error: "Exercise not found" });
1750
- }
1751
- let exercise = syllabus.lessons[exerciseIndex];
1752
- const exSlug = (0, creatorUtilities_2.slugify)(exercise.id + "-" + exercise.title);
1753
- // Handle errors
1754
- if (response.status === "ERROR") {
1755
- await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1756
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1757
- lesson: exSlug,
1758
- status: "error",
1759
- log: `❌ Error generating initial content for lesson ${exSlug}`,
1760
- });
1761
- // Retry initial content generation
1762
- try {
1763
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1764
- lesson: exSlug,
1765
- status: "generating",
1766
- log: `🔄 Retrying initial content generation for lesson ${exSlug}`,
1767
- });
1768
- const retryCompletionId = await startInitialContentGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, "");
1769
- // Update lesson status to show it's retrying
1770
- exercise.status = "GENERATING";
1771
- exercise.translations = {
1772
- [syllabus.courseInfo.language || "en"]: {
1773
- completionId: retryCompletionId,
1774
- startedAt: Date.now(),
1775
- completedAt: 0,
1776
- },
1777
- };
1778
- await saveSyllabus(courseSlug, syllabus, bucket);
1779
- }
1780
- catch (retryError) {
1781
- console.error("Error retrying initial content generation:", retryError);
1782
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1783
- lesson: lessonUID,
1784
- status: "error",
1785
- log: `❌ Failed to retry initial content generation for lesson ${lessonUID}`,
1786
- });
1787
- }
1788
- return res.json({ status: "ERROR" });
1789
- }
1790
1827
  try {
1791
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1792
- lesson: exSlug,
1793
- status: "generating",
1794
- log: `✅ Initial content generated for lesson ${exSlug}`,
1795
- });
1796
- // Update lesson with initial content
1797
- exercise = await updateLessonWithInitialContent(courseSlug, lessonUID, response.parsed, bucket);
1798
- if (!exercise) {
1799
- console.error("Exercise not found after updating initial content");
1800
- return res.json({ status: "ERROR", error: "Exercise not found" });
1801
- }
1802
- let lastLesson = "";
1803
- const prevLessonIndex = exerciseIndex - 1;
1804
- if (prevLessonIndex >= 0) {
1828
+ // In the course queue, nothing else saves the syllabus between
1829
+ // reading the lesson and saving it.
1830
+ await serializeByCourse(courseSlug, async () => {
1831
+ const syllabus = await getSyllabus(courseSlug, bucket);
1832
+ const language = syllabus.courseInfo.language || "en";
1833
+ const exerciseIndex = syllabus.lessons.findIndex(lesson => lesson.uid === lessonUID);
1834
+ if (exerciseIndex === -1) {
1835
+ console.error("Exercise not found receiving webhook:", lessonUID);
1836
+ return res.json({ status: "ERROR", error: "Exercise not found" });
1837
+ }
1838
+ let exercise = syllabus.lessons[exerciseIndex];
1839
+ const exSlug = (0, creatorUtilities_2.slugify)(exercise.id + "-" + exercise.title);
1840
+ if ((0, generationRetry_1.isStaleWebhook)(exercise, language, response.id)) {
1841
+ console.log(`Ignoring initial content webhook of job ${response.id} for lesson ${exSlug}: the lesson no longer waits for it`);
1842
+ return res.json({ status: "IGNORED" });
1843
+ }
1844
+ // Handle errors
1845
+ if (response.status === "ERROR") {
1846
+ console.error(`Initial content job ${response.id} failed for lesson ${exSlug}:`, response.status_text);
1847
+ const retryPlan = (0, generationRetry_1.planGenerationRetry)(exercise, language, response.status_text);
1848
+ if (!retryPlan.retry) {
1849
+ await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1850
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1851
+ lesson: exSlug,
1852
+ status: "error",
1853
+ log: noRetryLog(retryPlan.reason, "initial content", exSlug),
1854
+ });
1855
+ return res.json({ status: "ERROR" });
1856
+ }
1857
+ const attempt = retryPlan.attempt;
1858
+ try {
1859
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1860
+ lesson: exSlug,
1861
+ status: "generating",
1862
+ log: `🔄 Retrying initial content generation for lesson ${exSlug} (${attempt} of ${generationRetry_1.MAX_AUTO_RETRIES})`,
1863
+ });
1864
+ const retryCompletionId = await startInitialContentGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, "");
1865
+ await patchLesson(courseSlug, lessonUID, bucket, (lesson, lang) => {
1866
+ lesson.status = "GENERATING";
1867
+ lesson.translations = {
1868
+ [lang]: startedJob(retryCompletionId, attempt),
1869
+ };
1870
+ });
1871
+ }
1872
+ catch (retryError) {
1873
+ console.error("Error retrying initial content generation:", retryError);
1874
+ await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1875
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1876
+ lesson: exSlug,
1877
+ status: "error",
1878
+ log: `❌ Failed to retry initial content generation for lesson ${exSlug}`,
1879
+ });
1880
+ }
1881
+ return res.json({ status: "ERROR" });
1882
+ }
1805
1883
  try {
1806
- const prevLesson = syllabus.lessons[prevLessonIndex];
1807
- // search the lesson content in the bucket
1808
- const prevLessonSlug = (0, creatorUtilities_2.slugify)(prevLesson.id + "-" + prevLesson.title);
1809
- const file = bucket.file(`courses/${courseSlug}/exercises/${prevLessonSlug}/README${(0, creatorUtilities_1.getReadmeExtension)(syllabus.courseInfo.language || "en")}`);
1810
- const [content] = await file.download();
1811
- lastLesson = content.toString();
1884
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1885
+ lesson: exSlug,
1886
+ status: "generating",
1887
+ log: `✅ Initial content generated for lesson ${exSlug}`,
1888
+ });
1889
+ // Update lesson with initial content
1890
+ exercise = await updateLessonWithInitialContent(courseSlug, lessonUID, response.parsed, bucket);
1891
+ if (!exercise) {
1892
+ console.error("Exercise not found after updating initial content");
1893
+ return res.json({
1894
+ status: "ERROR",
1895
+ error: "Exercise not found",
1896
+ });
1897
+ }
1898
+ let lastLesson = "";
1899
+ const prevLessonIndex = exerciseIndex - 1;
1900
+ if (prevLessonIndex >= 0) {
1901
+ try {
1902
+ const prevLesson = syllabus.lessons[prevLessonIndex];
1903
+ // search the lesson content in the bucket
1904
+ const prevLessonSlug = (0, creatorUtilities_2.slugify)(prevLesson.id + "-" + prevLesson.title);
1905
+ const file = bucket.file(`courses/${courseSlug}/exercises/${prevLessonSlug}/README${(0, creatorUtilities_1.getReadmeExtension)(language)}`);
1906
+ const [content] = await file.download();
1907
+ lastLesson = content.toString();
1908
+ }
1909
+ catch (error) {
1910
+ console.error("Error searching previous lesson content:", error);
1911
+ }
1912
+ }
1913
+ const completionId = await startInteractivityGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, bucket, lastLesson);
1914
+ // Saved on a fresh read: `syllabus` was read before the initial
1915
+ // content was saved, and saving it would erase that content.
1916
+ await patchLesson(courseSlug, lessonUID, bucket, (lesson, lang) => {
1917
+ lesson.status = "GENERATING";
1918
+ lesson.translations = { [lang]: startedJob(completionId) };
1919
+ });
1920
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1921
+ lesson: exSlug,
1922
+ status: "generating",
1923
+ log: `🔄 Starting interactivity phase for lesson ${exercise.title}`,
1924
+ });
1925
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1926
+ lesson: exSlug,
1927
+ status: "initial-content-complete",
1928
+ log: `✅ Initial content generated for lesson ${exSlug}, starting interactivity phase`,
1929
+ });
1930
+ res.json({ status: "SUCCESS" });
1812
1931
  }
1813
1932
  catch (error) {
1814
- console.error("Error searching previous lesson content:", error);
1933
+ console.error("Error processing initial content webhook:", error);
1934
+ await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1935
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1936
+ lesson: exSlug,
1937
+ status: "error",
1938
+ log: `❌ Error processing initial content for lesson ${exSlug}`,
1939
+ });
1940
+ res
1941
+ .status(500)
1942
+ .json({ status: "ERROR", error: error.message });
1815
1943
  }
1816
- }
1817
- const completionId = await startInteractivityGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, bucket, lastLesson);
1818
- // Update lesson status to show it's in Phase 2
1819
- exercise.status = "GENERATING";
1820
- exercise.translations = {
1821
- [syllabus.courseInfo.language || "en"]: {
1822
- completionId,
1823
- startedAt: Date.now(),
1824
- completedAt: 0,
1825
- },
1826
- };
1827
- await saveSyllabus(courseSlug, syllabus, bucket);
1828
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1829
- lesson: exSlug,
1830
- status: "generating",
1831
- log: `🔄 Starting interactivity phase for lesson ${exercise.title}`,
1832
1944
  });
1833
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1834
- lesson: exSlug,
1835
- status: "initial-content-complete",
1836
- log: `✅ Initial content generated for lesson ${exSlug}, starting interactivity phase`,
1837
- });
1838
- res.json({ status: "SUCCESS" });
1839
1945
  }
1840
1946
  catch (error) {
1841
- console.error("Error processing initial content webhook:", error);
1842
- await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1843
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1844
- lesson: exSlug,
1845
- status: "error",
1846
- log: `❌ Error processing initial content for lesson ${exSlug}`,
1847
- });
1848
- res
1849
- .status(500)
1850
- .json({ status: "ERROR", error: error.message });
1947
+ console.error("Error handling initial content webhook:", error);
1948
+ if (!res.headersSent) {
1949
+ res
1950
+ .status(500)
1951
+ .json({ status: "ERROR", error: error.message });
1952
+ }
1851
1953
  }
1852
1954
  });
1853
1955
  // Phase 2: Interactivity generation webhook (replaces exercise-processor logic)
@@ -1857,116 +1959,184 @@ class ServeCommand extends SessionCommand_1.default {
1857
1959
  console.log("RECEIVING INTERACTIVITY WEBHOOK");
1858
1960
  // console.log("LESSON UID", lessonUID)
1859
1961
  // console.log("RESPONSE", response)
1860
- // Handle errors
1861
- if (response.status === "ERROR") {
1862
- await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1863
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1864
- lesson: lessonUID,
1865
- status: "error",
1866
- log: `❌ Error adding interactivity to lesson ${lessonUID}`,
1867
- });
1868
- // Retry interactivity generation
1869
- try {
1962
+ try {
1963
+ // In the course queue, nothing else saves the syllabus between
1964
+ // reading the lesson and saving it.
1965
+ await serializeByCourse(courseSlug, async () => {
1966
+ var _a, _b, _c;
1870
1967
  const syllabus = await getSyllabus(courseSlug, bucket);
1871
- const lessonIndex = syllabus.lessons.findIndex(lesson => lesson.uid === lessonUID);
1872
- const exercise = syllabus.lessons[lessonIndex];
1873
- if (exercise && exercise.initialContent) {
1968
+ const language = syllabus.courseInfo.language || "en";
1969
+ const exerciseIndex = syllabus.lessons.findIndex(lesson => lesson.uid === lessonUID);
1970
+ if (exerciseIndex === -1) {
1971
+ console.error("Exercise not found receiving webhook:", lessonUID);
1972
+ return res.json({ status: "ERROR", error: "Exercise not found" });
1973
+ }
1974
+ const exercise = syllabus.lessons[exerciseIndex];
1975
+ const exSlug = (0, creatorUtilities_2.slugify)(exercise.id + "-" + exercise.title);
1976
+ if ((0, generationRetry_1.isStaleWebhook)(exercise, language, response.id)) {
1977
+ console.log(`Ignoring interactivity webhook of job ${response.id} for lesson ${exSlug}: the lesson no longer waits for it`);
1978
+ return res.json({ status: "IGNORED" });
1979
+ }
1980
+ // Handle errors
1981
+ if (response.status === "ERROR") {
1982
+ console.error(`Interactivity job ${response.id} failed for lesson ${exSlug}:`, response.status_text);
1983
+ // Every retry uses v1. A failed v2 job gets its v1 retry even
1984
+ // when its error would repeat, because the error belongs to the
1985
+ // v2 template.
1986
+ const { fromV2, retryStatusText } = (0, interactivityPlan_1.describeInteractivityFailure)(req.query, response.status_text);
1987
+ const retryPlan = (0, generationRetry_1.planGenerationRetry)(exercise, language, retryStatusText);
1988
+ // The retry builds on the initial content, so without it there
1989
+ // is nothing to retry.
1990
+ if (!retryPlan.retry || !exercise.initialContent) {
1991
+ await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1992
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1993
+ lesson: exSlug,
1994
+ status: "error",
1995
+ log: retryPlan.retry ?
1996
+ `❌ Error adding interactivity to lesson ${exSlug}. Its initial content is missing, so it was not retried` :
1997
+ noRetryLog(retryPlan.reason, "interactivity", exSlug),
1998
+ });
1999
+ return res.json({ status: "ERROR" });
2000
+ }
2001
+ const attempt = retryPlan.attempt;
2002
+ try {
2003
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
2004
+ lesson: exSlug,
2005
+ status: "generating",
2006
+ log: fromV2 ?
2007
+ `⚠️ Structured interactivity failed for lesson ${exSlug}, retrying with the previous method (${attempt} of ${generationRetry_1.MAX_AUTO_RETRIES})` :
2008
+ `🔄 Retrying interactivity generation for lesson ${exSlug} (${attempt} of ${generationRetry_1.MAX_AUTO_RETRIES})`,
2009
+ });
2010
+ // Get previous lesson content for context
2011
+ let lastLesson = "";
2012
+ const prevLessonIndex = exerciseIndex - 1;
2013
+ if (prevLessonIndex >= 0) {
2014
+ try {
2015
+ const prevLesson = syllabus.lessons[prevLessonIndex];
2016
+ const prevLessonSlug = (0, creatorUtilities_2.slugify)(prevLesson.id + "-" + prevLesson.title);
2017
+ const file = bucket.file(`courses/${courseSlug}/exercises/${prevLessonSlug}/README${(0, creatorUtilities_1.getReadmeExtension)(language)}`);
2018
+ const [content] = await file.download();
2019
+ lastLesson = content.toString();
2020
+ }
2021
+ catch (error) {
2022
+ console.error("Error getting previous lesson content for retry:", error);
2023
+ }
2024
+ }
2025
+ const retryCompletionId = await startInteractivityGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, bucket, lastLesson, { forceV1: true });
2026
+ await patchLesson(courseSlug, lessonUID, bucket, (lesson, lang) => {
2027
+ lesson.status = "GENERATING";
2028
+ lesson.translations = {
2029
+ [lang]: startedJob(retryCompletionId, attempt),
2030
+ };
2031
+ });
2032
+ }
2033
+ catch (retryError) {
2034
+ console.error("Error retrying interactivity generation:", retryError);
2035
+ await updateLessonStatusToError(courseSlug, lessonUID, bucket);
2036
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
2037
+ lesson: exSlug,
2038
+ status: "error",
2039
+ log: `❌ Failed to retry interactivity generation for lesson ${exSlug}`,
2040
+ });
2041
+ }
2042
+ return res.json({ status: "ERROR" });
2043
+ }
2044
+ try {
2045
+ let finalContent = (_a = response.parsed) === null || _a === void 0 ? void 0 : _a.final_content;
2046
+ let usedComponents = (_b = response.parsed) === null || _b === void 0 ? void 0 : _b.used_components;
2047
+ // The structured template (v2) returns component data and
2048
+ // placeholders; the CLI writes the component markdown.
2049
+ if (req.query.tpl === "v2") {
2050
+ if (!Array.isArray((_c = response.parsed) === null || _c === void 0 ? void 0 : _c.multiple_choice_question)) {
2051
+ console.warn(`Structured interactivity response for ${exSlug} has no component lists`);
2052
+ }
2053
+ const guide = componentsGuide_1.componentsGuideLoader.peek();
2054
+ const result = (0, structuredComponents_1.processInteractivityResult)(response.parsed, {
2055
+ stepType: exercise.type,
2056
+ isFirstOrLast: exerciseIndex === 0 ||
2057
+ exerciseIndex === syllabus.lessons.length - 1,
2058
+ lessonId: exercise.id,
2059
+ decidedBy: guide ? (0, componentsGuide_1.decidedByFromGuide)(guide) : undefined,
2060
+ });
2061
+ finalContent = result.markdown;
2062
+ usedComponents = result.usedComponents;
2063
+ if (result.warnings.length > 0) {
2064
+ console.warn(`Component warnings for ${exSlug}:\n- ${result.warnings
2065
+ .map(warning => warning.message)
2066
+ .join("\n- ")}`);
2067
+ }
2068
+ if (result.dropped.length > 0) {
2069
+ console.warn(`Components removed from ${exSlug}:\n- ${result.dropped
2070
+ .map(issue => issue.message)
2071
+ .join("\n- ")}`);
2072
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
2073
+ lesson: exSlug,
2074
+ status: "generating",
2075
+ log: `⚠️ ${result.dropped.length} component(s) removed from lesson ${exercise.title} because they could not be rendered: ${result.dropped
2076
+ .map(issue => issue.message)
2077
+ .join("; ")}`,
2078
+ });
2079
+ }
2080
+ if (exercise.type === "QUIZ" && result.assessmentCount === 0) {
2081
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
2082
+ lesson: exSlug,
2083
+ status: "generating",
2084
+ log: `⚠️ The quiz ${exercise.title} has no questions; review it or regenerate it`,
2085
+ });
2086
+ }
2087
+ }
2088
+ // Process readability of final content
2089
+ const readability = (0, creatorUtilities_2.checkReadability)(finalContent, PARAMS.max_words, 3);
1874
2090
  (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1875
- lesson: lessonUID,
2091
+ lesson: exSlug,
1876
2092
  status: "generating",
1877
- log: `🔄 Retrying interactivity generation for lesson ${lessonUID}`,
2093
+ log: `🔄 The lesson ${exercise.title} has a readability score of ${readability.fkglResult.fkgl}`,
1878
2094
  });
1879
- // Get previous lesson content for context
1880
- let lastLesson = "";
1881
- const prevLessonIndex = lessonIndex - 1;
1882
- if (prevLessonIndex >= 0) {
1883
- try {
1884
- const prevLesson = syllabus.lessons[prevLessonIndex];
1885
- const prevLessonSlug = (0, creatorUtilities_2.slugify)(prevLesson.id + "-" + prevLesson.title);
1886
- const file = bucket.file(`courses/${courseSlug}/exercises/${prevLessonSlug}/README${(0, creatorUtilities_1.getReadmeExtension)(syllabus.courseInfo.language || "en")}`);
1887
- const [content] = await file.download();
1888
- lastLesson = content.toString();
1889
- }
1890
- catch (error) {
1891
- console.error("Error getting previous lesson content for retry:", error);
1892
- }
2095
+ // Upload README with final content
2096
+ const exercisesDir = `courses/${courseSlug}/exercises`;
2097
+ const targetDir = `${exercisesDir}/${exSlug}`;
2098
+ const readmeFilename = `README${(0, creatorUtilities_1.getReadmeExtension)(response.parsed.output_language || language)}`;
2099
+ await (0, bucketIo_1.uploadFileToBucket)(bucket, readability.newMarkdown, `${targetDir}/${readmeFilename}`);
2100
+ // Update used components (v1: listed by the AI; v2: the
2101
+ // components that were actually written)
2102
+ if (Array.isArray(usedComponents)) {
2103
+ await updateUsedComponents(courseSlug, usedComponents, bucket);
2104
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
2105
+ lesson: exSlug,
2106
+ status: "generating",
2107
+ log: `📊 Updated component usage tracking: ${usedComponents.join(", ")}`,
2108
+ });
1893
2109
  }
1894
- const retryCompletionId = await startInteractivityGeneration(rigoToken, syllabus.lessons, syllabus.courseInfo, exercise, courseSlug, syllabus.courseInfo.purpose, bucket, lastLesson);
1895
- // Update lesson status to show it's retrying
1896
- exercise.status = "GENERATING";
1897
- exercise.translations = {
1898
- [syllabus.courseInfo.language || "en"]: {
1899
- completionId: retryCompletionId,
1900
- startedAt: Date.now(),
1901
- completedAt: 0,
1902
- },
1903
- };
1904
- await saveSyllabus(courseSlug, syllabus, bucket);
2110
+ // Continue with next lesson
2111
+ await continueWithNextLesson(courseSlug, exerciseIndex, rigoToken, finalContent, bucket);
2112
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
2113
+ lesson: exSlug,
2114
+ status: "done",
2115
+ log: `✅ The lesson ${exercise.id} - ${exercise.title} has been generated successfully with interactivity!`,
2116
+ });
2117
+ res.json({ status: "SUCCESS" });
2118
+ }
2119
+ catch (error) {
2120
+ console.error("Error processing interactivity webhook:", error);
2121
+ await updateLessonStatusToError(courseSlug, lessonUID, bucket);
2122
+ (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
2123
+ lesson: exSlug,
2124
+ status: "error",
2125
+ log: `❌ Error processing interactivity for lesson ${exSlug}`,
2126
+ });
2127
+ res
2128
+ .status(500)
2129
+ .json({ status: "ERROR", error: error.message });
1905
2130
  }
1906
- }
1907
- catch (retryError) {
1908
- console.error("Error retrying interactivity generation:", retryError);
1909
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1910
- lesson: lessonUID,
1911
- status: "error",
1912
- log: `❌ Failed to retry interactivity generation for lesson ${lessonUID}`,
1913
- });
1914
- }
1915
- return res.json({ status: "ERROR" });
1916
- }
1917
- try {
1918
- const syllabus = await getSyllabus(courseSlug, bucket);
1919
- const exerciseIndex = syllabus.lessons.findIndex(lesson => lesson.uid === lessonUID);
1920
- if (exerciseIndex === -1) {
1921
- console.error("Exercise not found receiving webhook:", lessonUID);
1922
- return res.json({ status: "ERROR", error: "Exercise not found" });
1923
- }
1924
- const exercise = syllabus.lessons[exerciseIndex];
1925
- const exSlug = (0, creatorUtilities_2.slugify)(exercise.id + "-" + exercise.title);
1926
- // Process readability of final content
1927
- const readability = (0, creatorUtilities_2.checkReadability)(response.parsed.final_content, PARAMS.max_words, 3);
1928
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1929
- lesson: exSlug,
1930
- status: "generating",
1931
- log: `🔄 The lesson ${exercise.title} has a readability score of ${readability.fkglResult.fkgl}`,
1932
- });
1933
- // Upload README with final content
1934
- const exercisesDir = `courses/${courseSlug}/exercises`;
1935
- const targetDir = `${exercisesDir}/${exSlug}`;
1936
- const readmeFilename = `README${(0, creatorUtilities_1.getReadmeExtension)(response.parsed.output_language ||
1937
- syllabus.courseInfo.language ||
1938
- "en")}`;
1939
- await (0, bucketIo_1.uploadFileToBucket)(bucket, readability.newMarkdown, `${targetDir}/${readmeFilename}`);
1940
- // Update used components if provided by the AI
1941
- if (response.parsed.used_components &&
1942
- Array.isArray(response.parsed.used_components)) {
1943
- await updateUsedComponents(courseSlug, response.parsed.used_components, bucket);
1944
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1945
- lesson: exSlug,
1946
- status: "generating",
1947
- log: `📊 Updated component usage tracking: ${response.parsed.used_components.join(", ")}`,
1948
- });
1949
- }
1950
- // Continue with next lesson
1951
- await continueWithNextLesson(courseSlug, exerciseIndex, rigoToken, response.parsed.final_content, bucket);
1952
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1953
- lesson: exSlug,
1954
- status: "done",
1955
- log: `✅ The lesson ${exercise.id} - ${exercise.title} has been generated successfully with interactivity!`,
1956
2131
  });
1957
- res.json({ status: "SUCCESS" });
1958
2132
  }
1959
2133
  catch (error) {
1960
- console.error("Error processing interactivity webhook:", error);
1961
- await updateLessonStatusToError(courseSlug, lessonUID, bucket);
1962
- (0, creatorSocket_1.emitToCourse)(courseSlug, "course-creation", {
1963
- lesson: lessonUID,
1964
- status: "error",
1965
- log: `❌ Error processing interactivity for lesson ${lessonUID}`,
1966
- });
1967
- res
1968
- .status(500)
1969
- .json({ status: "ERROR", error: error.message });
2134
+ console.error("Error handling interactivity webhook:", error);
2135
+ if (!res.headersSent) {
2136
+ res
2137
+ .status(500)
2138
+ .json({ status: "ERROR", error: error.message });
2139
+ }
1970
2140
  }
1971
2141
  });
1972
2142
  // The following endpoint is used to store an incoming translation where it supposed to be
@@ -2649,19 +2819,20 @@ class ServeCommand extends SessionCommand_1.default {
2649
2819
  translations: {},
2650
2820
  uid: stepSlug,
2651
2821
  };
2652
- const newLessons = (0, creatorUtilities_1.insertStepInCorrectPosition)(initialSyllabus.lessons, newLesson);
2653
- // Use new two-phase generation workflow
2654
- const completionId = await startInitialContentGeneration(rigoToken, newLessons, initialSyllabus.courseInfo, newLesson, courseSlug, initialSyllabus.courseInfo.purpose, "lastResult");
2655
- newLesson.translations = {
2656
- [initialSyllabus.courseInfo.language || "en"]: {
2657
- completionId,
2658
- startedAt: Date.now(),
2659
- completedAt: 0,
2660
- },
2661
- };
2662
- await (0, bucketIo_1.uploadFileToBucket)(bucket, JSON.stringify(Object.assign(Object.assign({}, initialSyllabus), { lessons: newLessons })), `courses/${courseSlug}/.learn/initialSyllabus.json`);
2663
- const targetDir = `courses/${courseSlug}/exercises/${stepSlug}`;
2664
- await uploadInitialReadme(bucket, stepSlug, targetDir, initialSyllabus.courseInfo);
2822
+ // In the course queue, so the webhook of this job finds the new lesson
2823
+ // saved, and the syllabus saved here is not older than the last save.
2824
+ await serializeByCourse(courseSlug, async () => {
2825
+ const syllabus = await getSyllabus(courseSlug, bucket);
2826
+ const newLessons = (0, creatorUtilities_1.insertStepInCorrectPosition)(syllabus.lessons, newLesson);
2827
+ // Use new two-phase generation workflow
2828
+ const completionId = await startInitialContentGeneration(rigoToken, newLessons, syllabus.courseInfo, newLesson, courseSlug, syllabus.courseInfo.purpose, "lastResult");
2829
+ newLesson.translations = {
2830
+ [syllabus.courseInfo.language || "en"]: startedJob(completionId),
2831
+ };
2832
+ await (0, bucketIo_1.uploadFileToBucket)(bucket, JSON.stringify(Object.assign(Object.assign({}, syllabus), { lessons: newLessons })), `courses/${courseSlug}/.learn/initialSyllabus.json`);
2833
+ const targetDir = `courses/${courseSlug}/exercises/${stepSlug}`;
2834
+ await uploadInitialReadme(bucket, stepSlug, targetDir, syllabus.courseInfo);
2835
+ });
2665
2836
  res.json({ status: "SUCCESS", message: "Exercise generati on started!" });
2666
2837
  });
2667
2838
  app.put("/actions/rename", async (req, res) => {