-
Notifications
You must be signed in to change notification settings - Fork 312
feat: add generalized quiz widget (THU-607) #1001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
darkbanjo
wants to merge
1
commit into
main
Choose a base branch
from
jkab/thu-607-generalize-quiz-widget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { Check, Lightbulb, Sparkles, X } from 'lucide-react' | ||
| import { useMemo, useState } from 'react' | ||
|
|
||
| import { Button } from '@/components/ui/button' | ||
| import { cn } from '@/lib/utils' | ||
| import { gradeQuiz, optionLetter, type QuizData, type QuizOption } from './lib' | ||
|
|
||
| export type QuizSubmission = { | ||
| selectedIds: string[] | ||
| correct: boolean | null | ||
| } | ||
|
|
||
| type QuizProps = QuizData & { | ||
| /** Restores a previously-answered quiz (from the message cache). */ | ||
| initialSelectedIds?: string[] | ||
| initialSubmitted?: boolean | ||
| /** Fired once when the user commits an answer, for persistence. */ | ||
| onSubmit?: (submission: QuizSubmission) => void | ||
| } | ||
|
|
||
| /** Visual state of a single option, derived from selection + submission. */ | ||
| type OptionStatus = 'idle' | 'selected' | 'correct' | 'incorrect' | 'missed' | ||
|
|
||
| const getOptionStatus = ({ | ||
| option, | ||
| isSelected, | ||
| submitted, | ||
| isGraded, | ||
| }: { | ||
| option: QuizOption | ||
| isSelected: boolean | ||
| submitted: boolean | ||
| isGraded: boolean | ||
| }): OptionStatus => { | ||
| if (!submitted || !isGraded) { | ||
| return isSelected ? 'selected' : 'idle' | ||
| } | ||
| if (option.isCorrect && isSelected) return 'correct' | ||
| if (option.isCorrect && !isSelected) return 'missed' | ||
| if (!option.isCorrect && isSelected) return 'incorrect' | ||
| return 'idle' | ||
| } | ||
|
|
||
| const statusStyles: Record<OptionStatus, string> = { | ||
| idle: 'border-border bg-card hover:bg-accent hover:border-border', | ||
| selected: 'border-primary bg-accent ring-1 ring-primary', | ||
| correct: 'border-emerald-500/60 bg-emerald-50 dark:bg-emerald-950/30', | ||
| incorrect: 'border-red-500/60 bg-red-50 dark:bg-red-950/30', | ||
| missed: 'border-emerald-500/40 bg-emerald-50/50 dark:bg-emerald-950/15', | ||
| } | ||
|
|
||
| const badgeStyles: Record<OptionStatus, string> = { | ||
| idle: 'border-border text-muted-foreground', | ||
| selected: 'border-primary bg-primary text-primary-foreground', | ||
| correct: 'border-emerald-500 bg-emerald-500 text-white', | ||
| incorrect: 'border-red-500 bg-red-500 text-white', | ||
| missed: 'border-emerald-500 text-emerald-600 dark:text-emerald-400', | ||
| } | ||
|
|
||
| export const Quiz = ({ | ||
| prompt, | ||
| mode, | ||
| options, | ||
| explanation, | ||
| initialSelectedIds, | ||
| initialSubmitted, | ||
| onSubmit, | ||
| }: QuizProps) => { | ||
| const [selected, setSelected] = useState<Set<string>>(() => new Set(initialSelectedIds)) | ||
| const [submitted, setSubmitted] = useState(initialSubmitted ?? false) | ||
|
|
||
| const isGraded = mode !== 'choice' | ||
| const isMultiple = mode === 'multiple' | ||
| const result = useMemo( | ||
| () => (submitted ? gradeQuiz({ prompt, mode, options }, selected) : null), | ||
| [submitted, prompt, mode, options, selected], | ||
| ) | ||
|
|
||
| const commit = (ids: Set<string>) => { | ||
| setSubmitted(true) | ||
| onSubmit?.({ | ||
| selectedIds: [...ids], | ||
| correct: gradeQuiz({ prompt, mode, options }, ids), | ||
| }) | ||
| } | ||
|
|
||
| const toggleOption = (id: string) => { | ||
| if (submitted) return | ||
|
|
||
| if (!isGraded) { | ||
| // `choice` mode: selecting an option commits the choice immediately. | ||
| const next = new Set([id]) | ||
| setSelected(next) | ||
| commit(next) | ||
| return | ||
| } | ||
|
|
||
| setSelected((prev) => { | ||
| if (isMultiple) { | ||
| const next = new Set(prev) | ||
| next.has(id) ? next.delete(id) : next.add(id) | ||
| return next | ||
| } | ||
| return new Set([id]) | ||
| }) | ||
| } | ||
|
|
||
| const label = isGraded ? (isMultiple ? 'Select all that apply' : 'Choose one') : 'Your call' | ||
|
|
||
| return ( | ||
| <div className="my-4 w-full"> | ||
| <div className="overflow-hidden rounded-2xl border border-border bg-card"> | ||
| <div className="flex flex-col gap-4 p-4 md:p-5"> | ||
| <div className="flex flex-col gap-1.5"> | ||
| <div className="flex items-center gap-1.5 text-[length:var(--font-size-xs)] font-medium uppercase tracking-wide text-muted-foreground"> | ||
| <Sparkles className="size-[var(--icon-size-sm)]" /> | ||
| <span>{label}</span> | ||
| </div> | ||
| <p className="text-[length:var(--font-size-body)] font-medium leading-snug text-foreground">{prompt}</p> | ||
| </div> | ||
|
|
||
| <div className="flex flex-col gap-2"> | ||
| {options.map((option, index) => { | ||
| const isSelected = selected.has(option.id) | ||
| const status = getOptionStatus({ option, isSelected, submitted, isGraded }) | ||
| const showCorrect = status === 'correct' || status === 'missed' | ||
| const showIncorrect = status === 'incorrect' | ||
|
|
||
| return ( | ||
| <button | ||
| key={option.id} | ||
| type="button" | ||
| disabled={submitted} | ||
| onClick={() => toggleOption(option.id)} | ||
| className={cn( | ||
| 'group flex w-full items-center gap-3 rounded-xl border px-3.5 text-left transition-all', | ||
| 'min-h-[var(--touch-height-lg)] py-2.5', | ||
| 'disabled:cursor-default focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50', | ||
| !submitted && 'cursor-pointer active:scale-[0.99]', | ||
| statusStyles[status], | ||
| )} | ||
| > | ||
| <span | ||
| className={cn( | ||
| 'flex size-6 shrink-0 items-center justify-center border text-[length:var(--font-size-xs)] font-semibold transition-colors', | ||
| isMultiple ? 'rounded-md' : 'rounded-full', | ||
| badgeStyles[status], | ||
| )} | ||
| > | ||
| {showCorrect ? ( | ||
| <Check className="size-3.5" strokeWidth={3} /> | ||
| ) : showIncorrect ? ( | ||
| <X className="size-3.5" strokeWidth={3} /> | ||
| ) : ( | ||
| optionLetter(index) | ||
| )} | ||
| </span> | ||
| <span className="flex-1 text-[length:var(--font-size-sm)] leading-snug text-foreground"> | ||
| {option.text} | ||
| </span> | ||
| </button> | ||
| ) | ||
| })} | ||
| </div> | ||
|
|
||
| {isGraded && !submitted && ( | ||
| <Button | ||
| size="default" | ||
| disabled={selected.size === 0} | ||
| onClick={() => commit(selected)} | ||
| className="w-full md:w-auto md:self-end" | ||
| > | ||
| Check answer | ||
| </Button> | ||
| )} | ||
|
|
||
| {submitted && isGraded && ( | ||
| <div | ||
| className={cn( | ||
| 'flex items-start gap-2.5 rounded-xl border p-3 text-[length:var(--font-size-sm)]', | ||
| result | ||
| ? 'border-emerald-500/40 bg-emerald-50 text-emerald-800 dark:bg-emerald-950/30 dark:text-emerald-200' | ||
| : 'border-red-500/40 bg-red-50 text-red-800 dark:bg-red-950/30 dark:text-red-200', | ||
| )} | ||
| > | ||
| <span className="mt-0.5 shrink-0"> | ||
| {result ? ( | ||
| <Check className="size-[var(--icon-size-sm)]" strokeWidth={2.5} /> | ||
| ) : ( | ||
| <X className="size-[var(--icon-size-sm)]" strokeWidth={2.5} /> | ||
| )} | ||
| </span> | ||
| <div className="flex flex-col gap-1"> | ||
| <span className="font-medium">{result ? 'Correct!' : 'Not quite.'}</span> | ||
| {explanation && <span className="text-foreground/80">{explanation}</span>} | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| {submitted && !isGraded && ( | ||
| <div className="flex items-center gap-2 text-[length:var(--font-size-sm)] text-muted-foreground"> | ||
| <Lightbulb className="size-[var(--icon-size-sm)] shrink-0" /> | ||
| <span>Got it — working on that next.</span> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| export { Quiz } from './display' | ||
| export { instructions } from './instructions' | ||
| export { | ||
| collectQuizEntriesFromCache, | ||
| formatQuizResultsNote, | ||
| gradeQuiz, | ||
| optionLetter, | ||
| type QuizCacheEntry, | ||
| type QuizData, | ||
| type QuizMode, | ||
| type QuizOption, | ||
| } from './lib' | ||
| export { parse, schema } from './schema' | ||
| export type { CacheData, QuizWidget } from './schema' | ||
| export { QuizWidget as Component } from './widget' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| export const instructions = `## Quiz | ||
| <widget:quiz mode="MODE" prompt="QUESTION" options='JSON_ARRAY' explanation="WHY" /> | ||
| An interactive multiple-choice quiz the user can answer inline. Prefer this over a markdown list whenever you ask the user a multiple-choice question. | ||
| - mode: "single" (exactly one correct answer), "multiple" (one or more correct answers), or "choice" (no correct answer — an open prompt like "What do you want to do next?") | ||
| - prompt: the question or prompt text | ||
| - options: a JSON array wrapped in SINGLE quotes. Each option is {"id":"a","text":"..."}; for graded modes add "isCorrect":true to correct options. Never set isCorrect in "choice" mode. | ||
| - explanation (optional): a short note shown after the user answers (graded modes only) | ||
| Emit one widget per question. Do not also list the answers in text — the widget reveals them. | ||
| Example: <widget:quiz mode="single" prompt="What is the capital of France?" options='[{"id":"a","text":"Paris","isCorrect":true},{"id":"b","text":"Lyon"},{"id":"c","text":"Marseille"}]' explanation="Paris has been France's capital since 508 AD." />` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quiz persist not awaited
Medium Severity
commitcallsonSubmitwithout awaiting the asynchandleSubmitthat persists to the database. If the user sends the next chat message right after checking an answer,aiFetchStreamingResponsemay run itsgetMessage/formatQuizResultsNotepass before the cache write finishes, so the model’s quiz results system note can omit that answer.Additional Locations (1)
src/widgets/quiz/widget.tsx#L35-L45Reviewed by Cursor Bugbot for commit 55d46cb. Configure here.