Fix multi-slot pairing for matching, cloze, and MCQ correct answers.

Pair answer slots with inputs by schema position and give each MCQ slot its own radio group name.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-07-13 09:25:12 -07:00
parent 7e4c3fd3b9
commit 4eac111a7b
5 changed files with 121 additions and 20 deletions

View File

@ -32,7 +32,7 @@ import {
DynamicMatchingInputsSlot,
} from "./DynamicMatchingSlotField"
import {
findMatchingInputsInFieldValues,
findMatchingInputsForAnswerRow,
type MatchingInputsSlotValue,
} from "../../lib/matchingSlotValue"
import {
@ -41,7 +41,7 @@ import {
} from "./DynamicSelectMissingWordsSlotField"
import { DynamicSequenceOrderSlot } from "./DynamicSequenceOrderSlotField"
import {
findSelectMissingWordsStimulusInFieldValues,
findSelectMissingWordsStimulusForResponseRow,
type SelectMissingWordsStimulusValue,
} from "../../lib/selectMissingWordsSlotValue"
@ -643,11 +643,14 @@ function DynamicMultipleChoiceSlot({
onChange,
disabled,
slotLabel: label,
groupName,
}: {
value: string
onChange: (next: string) => void
disabled: boolean
slotLabel: string
/** Unique radio group name so multiple OPTION/MCQ slots do not share one Correct selection. */
groupName: string
}) {
const parsed = parseMultipleChoiceSlotValue(value)
@ -700,7 +703,7 @@ function DynamicMultipleChoiceSlot({
<label className="flex shrink-0 items-center gap-2 text-sm text-grayScale-600">
<input
type="radio"
name={`mcq-correct-${label}`}
name={groupName}
checked={option.is_correct}
disabled={disabled}
onChange={() => {
@ -861,17 +864,22 @@ export function DynamicSchemaSlotField({
const fieldLabel = `${slotLabel(row)}${row.required ? " *" : ""}`
const matchingInputs: MatchingInputsSlotValue | null =
mode === "matching_answer" && allFieldValues
? findMatchingInputsInFieldValues(
? findMatchingInputsForAnswerRow(
allFieldValues,
stimulusSchema,
responseSchema,
side,
row.id,
)
: null
const clozeStimulus: SelectMissingWordsStimulusValue | null =
mode === "select_missing_words_answer" && allFieldValues
? findSelectMissingWordsStimulusInFieldValues(
? findSelectMissingWordsStimulusForResponseRow(
allFieldValues,
stimulusSchema,
responseSchema,
side,
row.id,
)
: null
@ -915,6 +923,7 @@ export function DynamicSchemaSlotField({
onChange={onChange}
disabled={disabled}
slotLabel={fieldLabel}
groupName={`mcq-correct-${side}-${row.id}`}
/>
)
}

View File

@ -11,7 +11,7 @@ import {
} from "./multipleChoiceSlotValue"
import {
defaultMatchingInputsSlotValue,
findMatchingInputsInFieldValues,
findMatchingInputsForAnswerRow,
matchingAnswerSlotHasContent,
matchingInputsSlotHasContent,
parseMatchingAnswerSlotValue,
@ -23,7 +23,7 @@ import {
} from "./matchingSlotValue"
import {
defaultSelectMissingWordsStimulusSlotValue,
findSelectMissingWordsStimulusInFieldValues,
findSelectMissingWordsStimulusForResponseRow,
parseSelectMissingWordsResponseSlotValue,
parseSelectMissingWordsStimulusSlotValue,
selectMissingWordsResponseHasContent,
@ -453,15 +453,6 @@ export function validateDefinitionQuestion(
if (!v)
return `Question ${n}: fill required response "${row.label || row.id}" (${row.kind}).`
}
const matchingInputs = findMatchingInputsInFieldValues(
fieldValues,
def.stimulus_schema,
def.response_schema,
)
const clozeStimulus = findSelectMissingWordsStimulusInFieldValues(
fieldValues,
def.stimulus_schema,
)
for (const row of def.stimulus_schema) {
if (isNoInputComponentKind(row.kind)) continue
if (isMultipleChoiceKind(row.kind)) {
@ -491,6 +482,13 @@ export function validateDefinitionQuestion(
}
}
if (isMatchingAnswerKind(row.kind)) {
const matchingInputs = findMatchingInputsForAnswerRow(
fieldValues,
def.stimulus_schema,
def.response_schema,
"stimulus",
row.id,
)
const val = parseMatchingAnswerSlotValue(
fieldValues[`stimulus:${row.id}`],
matchingInputs,
@ -551,6 +549,13 @@ export function validateDefinitionQuestion(
}
}
if (isMatchingAnswerKind(row.kind)) {
const matchingInputs = findMatchingInputsForAnswerRow(
fieldValues,
def.stimulus_schema,
def.response_schema,
"response",
row.id,
)
const val = parseMatchingAnswerSlotValue(
fieldValues[`response:${row.id}`],
matchingInputs,
@ -567,6 +572,13 @@ export function validateDefinitionQuestion(
}
}
if (isSelectMissingWordsKind(row.kind)) {
const clozeStimulus = findSelectMissingWordsStimulusForResponseRow(
fieldValues,
def.stimulus_schema,
def.response_schema,
"response",
row.id,
)
const val = parseSelectMissingWordsResponseSlotValue(
fieldValues[`response:${row.id}`],
clozeStimulus,

View File

@ -352,3 +352,50 @@ export function findMatchingInputsInFieldValues(
}
return null
}
type SchemaSideRow = {
side: "stimulus" | "response"
row: { id: string; kind: string }
}
function matchingFamilyRows(
stimulusSchema: { id: string; kind: string }[],
responseSchema: { id: string; kind: string }[],
kind: "MATCHING_INPUTS" | "MATCHING_ANSWER",
): SchemaSideRow[] {
const want = kind
const out: SchemaSideRow[] = []
for (const row of stimulusSchema) {
if (row.kind.trim().toUpperCase() === want) out.push({ side: "stimulus", row })
}
for (const row of responseSchema) {
if (row.kind.trim().toUpperCase() === want) out.push({ side: "response", row })
}
return out
}
/**
* Pair the Nth MATCHING_ANSWER slot with the Nth MATCHING_INPUTS slot (schema order:
* stimulus then response). Falls back to first-of-kind when the positional target is empty.
*/
export function findMatchingInputsForAnswerRow(
fieldValues: Record<string, string>,
stimulusSchema: { id: string; kind: string }[],
responseSchema: { id: string; kind: string }[],
answerSide: "stimulus" | "response",
answerRowId: string,
): MatchingInputsSlotValue | null {
const inputRows = matchingFamilyRows(stimulusSchema, responseSchema, "MATCHING_INPUTS")
const answerRows = matchingFamilyRows(stimulusSchema, responseSchema, "MATCHING_ANSWER")
const idx = answerRows.findIndex(
(x) => x.side === answerSide && x.row.id === answerRowId,
)
const target = idx >= 0 ? inputRows[idx] : undefined
if (target) {
const parsed = parseMatchingInputsSlotValue(
fieldValues[`${target.side}:${target.row.id}`],
)
if (matchingInputsSlotHasContent(parsed)) return parsed
}
return findMatchingInputsInFieldValues(fieldValues, stimulusSchema, responseSchema)
}

View File

@ -3,7 +3,7 @@ import { parseTableSlotValue } from "./dynamicTableValue"
import {
finalizeMatchingAnswerPayload,
finalizeMatchingInputsPayload,
findMatchingInputsInFieldValues,
findMatchingInputsForAnswerRow,
matchingAnswerSlotHasContent,
matchingInputsSlotHasContent,
parseMatchingAnswerSlotValue,
@ -12,7 +12,7 @@ import {
import {
finalizeSelectMissingWordsResponsePayload,
finalizeSelectMissingWordsStimulusPayload,
findSelectMissingWordsStimulusInFieldValues,
findSelectMissingWordsStimulusForResponseRow,
parseSelectMissingWordsResponseSlotValue,
parseSelectMissingWordsStimulusSlotValue,
selectMissingWordsResponseHasContent,
@ -192,10 +192,12 @@ function slotValueForRow(
}
if (isMatchingAnswerKind(row.kind)) {
const matchingInputs = findMatchingInputsInFieldValues(
const matchingInputs = findMatchingInputsForAnswerRow(
fieldValues,
stimulusRows,
responseRows,
side,
row.id,
)
const fromField = parseMatchingAnswerSlotValue(rawField, matchingInputs)
if (matchingAnswerSlotHasContent(fromField)) {
@ -212,9 +214,12 @@ function slotValueForRow(
}
return { segments: [], word_bank: [], allow_reuse: false }
}
const clozeStimulus = findSelectMissingWordsStimulusInFieldValues(
const clozeStimulus = findSelectMissingWordsStimulusForResponseRow(
fieldValues,
stimulusRows,
responseRows,
side,
row.id,
)
const fromField = parseSelectMissingWordsResponseSlotValue(
rawField,

View File

@ -516,3 +516,31 @@ export function findSelectMissingWordsStimulusInFieldValues(
}
return null
}
/**
* Pair the Nth response SELECT_MISSING_WORDS with the Nth stimulus SELECT_MISSING_WORDS.
* Falls back to first-of-kind when the positional target is empty.
*/
export function findSelectMissingWordsStimulusForResponseRow(
fieldValues: Record<string, string>,
stimulusSchema: { id: string; kind: string }[],
responseSchema: { id: string; kind: string }[],
responseSide: "stimulus" | "response",
responseRowId: string,
): SelectMissingWordsStimulusValue | null {
const isCloze = (k: string) => k.trim().toUpperCase() === "SELECT_MISSING_WORDS"
const stimulusClozes = stimulusSchema.filter((r) => isCloze(r.kind))
const responseClozes = responseSchema.filter((r) => isCloze(r.kind))
const idx =
responseSide === "response"
? responseClozes.findIndex((r) => r.id === responseRowId)
: -1
const target = idx >= 0 ? stimulusClozes[idx] : undefined
if (target) {
const parsed = parseSelectMissingWordsStimulusSlotValue(
fieldValues[`stimulus:${target.id}`],
)
if (selectMissingWordsStimulusHasContent(parsed)) return parsed
}
return findSelectMissingWordsStimulusInFieldValues(fieldValues, stimulusSchema)
}