Skip to content

Commit a55f179

Browse files
committed
feat: AI-assisted quiz generation with skill injection and post-processor
1 parent a7adcd7 commit a55f179

5 files changed

Lines changed: 580 additions & 34 deletions

File tree

CHANGELOG-quiz-ai-generation.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# AI-Assisted Quiz Generation — Skill Injection, Per-Card Model, Mode Toggle
2+
3+
- Added `QUIZ_SYNTAX_SKILL` constant — comprehensive syntax reference injected into AI prompts for accurate quiz generation
4+
- Added `postProcessQuizLines()` — auto-fixes common AI output errors (missing colons, swapped MCQ pipes, wrong types)
5+
- Added `@prompt:` field to quiz parser — users can describe quizzes in natural language instead of manually writing `@question` lines
6+
- Added AI prompt textarea to quiz card UI — renders below the header for free-text quiz descriptions
7+
- Added per-card AI model selector dropdown — creators can choose which model generates questions for each quiz
8+
- Added Practice/Test mode toggle button — clickable pill in card header syncs `@mode:` to the editor
9+
- Added model-readiness handling with try/catch — shows toast instead of crashing if model isn't loaded
10+
- Added `runAiPrompt` callback fallback for backward compatibility
11+
- Creator's Next button is always enabled — no longer blocked until answering in preview mode
12+
- Fixed: `{{Quiz :}}` with space before colon now parses correctly (`\s*` before `:`)
13+
- Fixed: AI-generated `@hint:` lines are now preserved alongside `@question` lines
14+
- Added CSS styles for `.quiz-dg-prompt-area`, `.quiz-dg-prompt-input`, `.quiz-dg-gen-prompt`
15+
- Added CSS styles for `.quiz-dg-model-select` dropdown
16+
- Added CSS styles for `.quiz-dg-mode-toggle` button with Practice (green) / Test (purple) states
17+
18+
---
19+
20+
## Summary
21+
Implements AI-assisted quiz creation using the Skill Injection pattern from `game-docgen.js`. Users can now describe a quiz in plain language (via `@prompt:` or the prompt textarea), pick a model, and click Generate to produce correctly-formatted `@question` markup. A post-processor auto-fixes common AI syntax errors.
22+
23+
---
24+
25+
## 1. Quiz Syntax Skill Injection
26+
**Files:** `js/quiz-docgen.js`
27+
**What:** Added `QUIZ_SYNTAX_SKILL` constant (~40 lines) containing the complete quiz syntax reference with explicit pipe-format documentation for all 8 question types. This is prepended to every AI generation request.
28+
**Impact:** The AI receives exact formatting rules, producing valid `@question[mcq]:`, `@question[tf]:`, etc. markup instead of guessing the syntax.
29+
30+
## 2. Post-Processor for AI Output
31+
**Files:** `js/quiz-docgen.js`
32+
**What:** Added `postProcessQuizLines()` function that fixes missing colons (`@question[mcq] ``@question[mcq]: `), swapped MCQ pipe order, and filters non-question lines.
33+
**Impact:** Even imperfect AI output gets corrected into valid quiz markup before insertion.
34+
35+
## 3. `@prompt:` Field & Prompt Textarea
36+
**Files:** `js/quiz-docgen.js`, `css/quiz-docgen.css`
37+
**What:** Added `@prompt:` parser field (multi-line, same pattern as `@chapter:`), prompt textarea UI in quiz cards, and debounced sync back to editor markdown.
38+
**Impact:** Users can type "Create 10 biology questions on photosynthesis" instead of manually writing `@question` lines.
39+
40+
## 4. Per-Card Model Selector
41+
**Files:** `js/quiz-docgen.js`, `css/quiz-docgen.css`
42+
**What:** Added `<select>` dropdown populated from `window.AI_MODELS`, filtering out image/TTS/STT models. On change, triggers local model download or API key prompt.
43+
**Impact:** Each quiz card can use a different AI model for generation.
44+
45+
## 5. Practice/Test Mode Toggle
46+
**Files:** `js/quiz-docgen.js`, `css/quiz-docgen.css`
47+
**What:** Replaced static "📝 Test" badge with clickable toggle button. Click handler syncs `@mode:` field to editor markdown via regex replace or insertion.
48+
**Impact:** Creators can switch quiz mode with a single click instead of editing markdown.
49+
50+
## 6. Creator Navigation Fix
51+
**Files:** `js/quiz-docgen.js`
52+
**What:** Changed `navDisabled` from `(b.mode === 'test') ? '' : 'disabled'` to `(!isRespondent || b.mode === 'test') ? '' : 'disabled'`.
53+
**Impact:** Creators can freely navigate through all questions to preview; only respondents in practice mode must answer first.
54+
55+
## 7. Flexible Quiz Tag Parsing
56+
**Files:** `js/quiz-docgen.js`
57+
**What:** Added `\s*` before `:` in both parser and transform regexes.
58+
**Impact:** `{{Quiz : title}}`, `{{Quiz: title}}`, and `{{Quiz : title}}` all work.
59+
60+
---
61+
62+
## Files Changed (2 total)
63+
64+
| File | Lines Changed | Type |
65+
|------|:---:|------|
66+
| `js/quiz-docgen.js` | +331 −34 | Skill injection, @prompt parser, model selector, mode toggle, post-processor |
67+
| `css/quiz-docgen.css` | +114 −0 | Styles for prompt area, model dropdown, mode toggle |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ TextAgent has undergone significant evolution since its inception. What started
538538

539539
| Date | Commits | Feature / Update |
540540
|------|---------|-----------------:|
541+
| **2026-03-28** | | 🤖 **AI-Assisted Quiz Generation** — new Skill Injection system for `{{Quiz:}}` tags: `QUIZ_SYNTAX_SKILL` constant injected into AI prompts with exact pipe-format documentation for all 8 question types; `postProcessQuizLines()` auto-fixes common AI output errors (missing colons, swapped MCQ pipes, wrong types); `@prompt:` field with prompt textarea for natural-language quiz creation; per-card AI model selector dropdown; Practice/Test mode toggle button; creator Next button always enabled; flexible `{{Quiz :}}` parsing (space before colon); `requestAiTask` with try/catch and toast error handling |
541542
| **2026-03-28** | | 🤖 **Qwen Loop Fix** — switched to Qwen 3.5 official generation params (`presence_penalty: 2.0`, `repetition_penalty: 1.0`); added degenerate output circuit breaker (unique-word ratio monitoring in 200-char sliding window, auto-abort at 30% threshold); `trimToLastSentence()` graceful cleanup on abort; `no_repeat_ngram_size: 6` secondary guard; fixes all local Qwen models producing garbage/looping text |
542543
| **2026-03-28** | | 🔗 **Space-Aware Sharing** — moved "Add to Space" dropdown to the pre-share modal; generates unified `#space=<slug>&s=<id>` URLs that load the document within its space context; converted management items to clickable anchor tags opening directly to the doc |
543544
| **2026-03-28** | | 🎓 **Quiz Progress Bar Fix** — fixed progress bar not syncing with respondent navigation; bar now tracks current question position instead of answered-question count; `gotoScreen()` now updates HUD on every navigation |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# AI-Assisted Quiz Generation — Skill Injection, Per-Card Model, Mode Toggle
2+
3+
- Added `QUIZ_SYNTAX_SKILL` constant — comprehensive syntax reference injected into AI prompts for accurate quiz generation
4+
- Added `postProcessQuizLines()` — auto-fixes common AI output errors (missing colons, swapped MCQ pipes, wrong types)
5+
- Added `@prompt:` field to quiz parser — users can describe quizzes in natural language instead of manually writing `@question` lines
6+
- Added AI prompt textarea to quiz card UI — renders below the header for free-text quiz descriptions
7+
- Added per-card AI model selector dropdown — creators can choose which model generates questions for each quiz
8+
- Added Practice/Test mode toggle button — clickable pill in card header syncs `@mode:` to the editor
9+
- Added model-readiness handling with try/catch — shows toast instead of crashing if model isn't loaded
10+
- Added `runAiPrompt` callback fallback for backward compatibility
11+
- Creator's Next button is always enabled — no longer blocked until answering in preview mode
12+
- Fixed: `{{Quiz :}}` with space before colon now parses correctly (`\s*` before `:`)
13+
- Fixed: AI-generated `@hint:` lines are now preserved alongside `@question` lines
14+
- Added CSS styles for `.quiz-dg-prompt-area`, `.quiz-dg-prompt-input`, `.quiz-dg-gen-prompt`
15+
- Added CSS styles for `.quiz-dg-model-select` dropdown
16+
- Added CSS styles for `.quiz-dg-mode-toggle` button with Practice (green) / Test (purple) states
17+
18+
---
19+
20+
## Summary
21+
Implements AI-assisted quiz creation using the Skill Injection pattern from `game-docgen.js`. Users can now describe a quiz in plain language (via `@prompt:` or the prompt textarea), pick a model, and click Generate to produce correctly-formatted `@question` markup. A post-processor auto-fixes common AI syntax errors.
22+
23+
---
24+
25+
## 1. Quiz Syntax Skill Injection
26+
**Files:** `js/quiz-docgen.js`
27+
**What:** Added `QUIZ_SYNTAX_SKILL` constant (~40 lines) containing the complete quiz syntax reference with explicit pipe-format documentation for all 8 question types. This is prepended to every AI generation request.
28+
**Impact:** The AI receives exact formatting rules, producing valid `@question[mcq]:`, `@question[tf]:`, etc. markup instead of guessing the syntax.
29+
30+
## 2. Post-Processor for AI Output
31+
**Files:** `js/quiz-docgen.js`
32+
**What:** Added `postProcessQuizLines()` function that fixes missing colons (`@question[mcq] ``@question[mcq]: `), swapped MCQ pipe order, and filters non-question lines.
33+
**Impact:** Even imperfect AI output gets corrected into valid quiz markup before insertion.
34+
35+
## 3. `@prompt:` Field & Prompt Textarea
36+
**Files:** `js/quiz-docgen.js`, `css/quiz-docgen.css`
37+
**What:** Added `@prompt:` parser field (multi-line, same pattern as `@chapter:`), prompt textarea UI in quiz cards, and debounced sync back to editor markdown.
38+
**Impact:** Users can type "Create 10 biology questions on photosynthesis" instead of manually writing `@question` lines.
39+
40+
## 4. Per-Card Model Selector
41+
**Files:** `js/quiz-docgen.js`, `css/quiz-docgen.css`
42+
**What:** Added `<select>` dropdown populated from `window.AI_MODELS`, filtering out image/TTS/STT models. On change, triggers local model download or API key prompt.
43+
**Impact:** Each quiz card can use a different AI model for generation.
44+
45+
## 5. Practice/Test Mode Toggle
46+
**Files:** `js/quiz-docgen.js`, `css/quiz-docgen.css`
47+
**What:** Replaced static "📝 Test" badge with clickable toggle button. Click handler syncs `@mode:` field to editor markdown via regex replace or insertion.
48+
**Impact:** Creators can switch quiz mode with a single click instead of editing markdown.
49+
50+
## 6. Creator Navigation Fix
51+
**Files:** `js/quiz-docgen.js`
52+
**What:** Changed `navDisabled` from `(b.mode === 'test') ? '' : 'disabled'` to `(!isRespondent || b.mode === 'test') ? '' : 'disabled'`.
53+
**Impact:** Creators can freely navigate through all questions to preview; only respondents in practice mode must answer first.
54+
55+
## 7. Flexible Quiz Tag Parsing
56+
**Files:** `js/quiz-docgen.js`
57+
**What:** Added `\s*` before `:` in both parser and transform regexes.
58+
**Impact:** `{{Quiz : title}}`, `{{Quiz: title}}`, and `{{Quiz : title}}` all work.
59+
60+
---
61+
62+
## Files Changed (2 total)
63+
64+
| File | Lines Changed | Type |
65+
|------|:---:|------|
66+
| `js/quiz-docgen.js` | +331 −34 | Skill injection, @prompt parser, model selector, mode toggle, post-processor |
67+
| `css/quiz-docgen.css` | +114 −0 | Styles for prompt area, model dropdown, mode toggle |

css/quiz-docgen.css

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,39 @@
8383
letter-spacing: 0.3px;
8484
}
8585

86+
.quiz-dg-mode-toggle {
87+
padding: 3px 12px;
88+
border-radius: 20px;
89+
font-size: 0.8em;
90+
font-weight: 700;
91+
letter-spacing: 0.3px;
92+
cursor: pointer;
93+
border: 1px solid #4338ca;
94+
background: #312e81;
95+
color: #a5b4fc;
96+
font-family: inherit;
97+
transition: all 0.2s;
98+
white-space: nowrap;
99+
}
100+
101+
.quiz-dg-mode-toggle:hover {
102+
background: #3730a3;
103+
border-color: #6366f1;
104+
transform: translateY(-1px);
105+
}
106+
107+
.quiz-dg-mode-toggle[data-mode="test"] {
108+
background: #312e81;
109+
color: #a5b4fc;
110+
border-color: #4338ca;
111+
}
112+
113+
.quiz-dg-mode-toggle[data-mode="practice"] {
114+
background: #064e3b;
115+
color: #6ee7b7;
116+
border-color: #059669;
117+
}
118+
86119
.quiz-dg-actions {
87120
display: flex;
88121
gap: 6px;
@@ -96,6 +129,26 @@
96129
white-space: nowrap;
97130
}
98131

132+
.quiz-dg-model-select {
133+
background: #0f172a;
134+
color: #a5b4fc;
135+
border: 1px solid #334155;
136+
border-radius: 8px;
137+
padding: 5px 10px;
138+
font-size: 0.78em;
139+
font-weight: 600;
140+
font-family: inherit;
141+
cursor: pointer;
142+
outline: none;
143+
transition: border-color 0.2s;
144+
max-width: 180px;
145+
}
146+
147+
.quiz-dg-model-select:hover,
148+
.quiz-dg-model-select:focus {
149+
border-color: #818cf8;
150+
}
151+
99152
.quiz-dg-gen-btn,
100153
.quiz-dg-grade-btn,
101154
.quiz-dg-responses-btn {
@@ -178,6 +231,67 @@
178231
font-size: 0.9em;
179232
}
180233

234+
/* ─── AI Prompt Area ────────────────────────── */
235+
.quiz-dg-prompt-area {
236+
padding: 12px 18px 0;
237+
display: flex;
238+
gap: 8px;
239+
align-items: flex-start;
240+
flex-wrap: wrap;
241+
}
242+
243+
.quiz-dg-prompt-input {
244+
flex: 1;
245+
min-width: 200px;
246+
background: var(--quiz-input-bg, #0f172a);
247+
border: 2px solid var(--quiz-input-border, #334155);
248+
border-radius: 10px;
249+
padding: 10px 14px;
250+
font-size: 0.9em;
251+
color: var(--quiz-text-q, #f1f5f9);
252+
font-family: inherit;
253+
outline: none;
254+
resize: none;
255+
overflow: hidden;
256+
line-height: 1.5;
257+
transition: border-color 0.2s, box-shadow 0.2s;
258+
}
259+
260+
.quiz-dg-prompt-input:focus {
261+
border-color: var(--quiz-accent, #818cf8);
262+
box-shadow: 0 0 0 3px rgba(129, 140, 248, 0.12);
263+
}
264+
265+
.quiz-dg-prompt-input::placeholder {
266+
color: var(--quiz-text-muted, #64748b);
267+
font-size: 0.92em;
268+
}
269+
270+
.quiz-dg-gen-prompt {
271+
flex-shrink: 0;
272+
padding: 10px 18px;
273+
background: linear-gradient(135deg, #6366f1, #8b5cf6);
274+
border: none;
275+
border-radius: 10px;
276+
color: #fff;
277+
font-weight: 700;
278+
font-size: 0.88em;
279+
cursor: pointer;
280+
font-family: inherit;
281+
transition: all 0.2s;
282+
white-space: nowrap;
283+
}
284+
285+
.quiz-dg-gen-prompt:hover:not(:disabled) {
286+
box-shadow: 0 4px 14px rgba(99, 102, 241, 0.45);
287+
transform: translateY(-1px);
288+
}
289+
290+
.quiz-dg-gen-prompt:disabled {
291+
opacity: 0.4;
292+
cursor: not-allowed;
293+
}
294+
181295
/* ─── Progress bar ──────────────────────────── */
182296
.qd-bar {
183297
display: flex;

0 commit comments

Comments
 (0)