diff --git a/src/components/GenerateCourse/AICourseLesson.tsx b/src/components/GenerateCourse/AICourseLesson.tsx index da93eeebc..2ef9abb46 100644 --- a/src/components/GenerateCourse/AICourseLesson.tsx +++ b/src/components/GenerateCourse/AICourseLesson.tsx @@ -37,6 +37,18 @@ import { ResizablePanelGroup, } from './Resizeable'; +function getQuestionsFromResult(result: string) { + const matchedQuestions = result.match( + /=START_QUESTIONS=(.*?)=END_QUESTIONS=/, + ); + + if (matchedQuestions) { + return matchedQuestions[1].split('@@'); + } + + return []; +} + type AICourseLessonProps = { courseSlug: string; progress: string[]; @@ -82,6 +94,7 @@ export function AICourseLesson(props: AICourseLessonProps) { const [isLoading, setIsLoading] = useState(true); const [isGenerating, setIsGenerating] = useState(false); const [error, setError] = useState(''); + const [defaultQuestions, setDefaultQuestions] = useState([]); const [lessonHtml, setLessonHtml] = useState(''); @@ -163,14 +176,29 @@ export function AICourseLesson(props: AICourseLessonProps) { return; } - setLessonHtml(markdownToHtml(result, false)); + const questions = getQuestionsFromResult(result); + setDefaultQuestions(questions); + const newResult = result.replace( + /=START_QUESTIONS=.*?=END_QUESTIONS=/, + '', + ); + + setLessonHtml(markdownToHtml(newResult, false)); }, onStreamEnd: async (result) => { if (abortController.signal.aborted) { return; } - setLessonHtml(await markdownToHtmlWithHighlighting(result)); + const questions = getQuestionsFromResult(result); + setDefaultQuestions(questions); + + const newResult = result.replace( + /=START_QUESTIONS=.*?=END_QUESTIONS=/, + '', + ); + + setLessonHtml(await markdownToHtmlWithHighlighting(newResult)); queryClient.invalidateQueries(getAiCourseLimitOptions()); setIsGenerating(false); }, @@ -445,6 +473,7 @@ export function AICourseLesson(props: AICourseLessonProps) { lessonTitle={currentLessonTitle} onUpgradeClick={onUpgrade} isDisabled={isGenerating || isLoading || isTogglingDone} + defaultQuestions={defaultQuestions} /> @@ -462,6 +491,7 @@ export function AICourseLesson(props: AICourseLessonProps) { lessonTitle={currentLessonTitle} onUpgradeClick={onUpgrade} isDisabled={isGenerating || isLoading || isTogglingDone} + defaultQuestions={defaultQuestions} /> + ))} + + + )} ); })} @@ -287,6 +316,7 @@ export function AICourseLessonChat(props: AICourseLessonChatProps) { handleChatSubmit(e as unknown as FormEvent); } }} + ref={textareaRef} />