Generate and import questions
How a teacher fills a draft exam from their schemes of work, a scanned paper, or a CSV, and what the server does with each.
A teacher with a draft exam has three ways to add many questions at once. On the exam's question screen the Hub shows them in one AI menu: Generate questions, Extract from a paper, and Import from CSV. The first two use a model and run in the background. The CSV import runs straight away and uses no model.
All three add questions to the exam. None of them replaces or edits questions already there.
Before any of them run
The routes live on CbtQuestionController at /api/v1/cbt/examinations/:examinationId/questions. The controller requires a staff token and the CBT feature.
Each method then loads the exam with loadManageableExam for update, so the caller must be able to manage that exam. The exam must still be a draft (assertDraft).
Generate and extract also refuse a second request while one is in progress. If aiGenerationStatus is pending or running, the answer is 409 with Questions are already being generated.
Generate from schemes of work
- The Hub calls
GET .../context(getCbtQuestionContext). It returns the schemes of work the exam owner teaches for this subject, term, and year, each with its lesson plans by week, the number of NCDC documents for the subject, and how many questions the exam already has. Onmain6bfb9f6b, the same tree as staging0d611c96, a scheme that has a stream is kept only when its creator is the exam owner and that owner teaches the stream. A scheme with no stream is kept only when the owner is an accepted member of its group. Before this commit, a scheme with no stream counted for everyone who teaches the subject. Deployment was not verified. No request was sent. - The teacher picks schemes and weeks, and the Hub sends
POST .../generate(generateCbtQuestions). - The server checks every scheme id against that same list. Any other id fails with
One or more schemes do not belong to this exam subject and teacher. - The server stores the request on the exam, sets
aiGenerationStatustopending, queues a job oncbt-question-generate, and answers202with{ "message": "Generation queued" }.
The generate body:
| Field | Type | Rules |
|---|---|---|
count | integer | Required. 1 to 40 |
types | question types | Required. At least one |
marksEach | integer | Required. 1 to 100 |
focus | string | Optional. At most 1000 characters |
schemeOfWorkIds | UUIDs | Required |
weekNumbers | integers | Optional. Limits which lesson plans are sent |
Extract from a paper
POST .../extract (extractCbtQuestions) takes storageIds (1 to 10 files already uploaded through storage) and marksEach (1 to 100). Every file must be a PDF or an image. Anything else fails with Upload PDFs or images of the paper only. The rest is the same as generate: the request is stored, the status becomes pending, a job is queued, and the answer is 202 with { "message": "Extraction queued" }.
The files are not downloaded into the job. When the worker asks for the brief, the server gives it signed URLs that last one hour, and the model provider fetches the pages itself.
What happens in the background
The job follows the same pattern as the scheme prefill: the worker asks the server for a brief, calls the model, and reports back. The exam, schemes, and plans come from the server. The worker's agent also gets NCDC search tools built on NcdcDocumentsService, so it can look up curriculum material itself.
The diagram shows the Hub queuing the job and getting 202 at once. The worker then fetches a brief from the server, calls the model, and sends the questions back to the server, while the Hub polls the exam.
- Brief.
buildBriefsets the status torunning. It sends the exam title, subject, term, timing mode, the prompts of questions already on the exam, the teacher's options, and either the chosen schemes and lesson plans or the signed file URLs. Each scheme or plan body is cut to 60,000 characters of HTML so one large scheme cannot crowd out the rest. A missing exam isExam not found. An exam with no stored request isNo generation was requested. - Model.
CbtQuestionGenerateAgent.generateruns in the worker. - Apply. The worker sends the questions, or an error, back to the server.
applyvalidates each question with the same rules as a hand-written one. It keeps the valid ones and skips the rest.
The result lands on the exam in aiGenerationStatus and aiGenerationError:
| Outcome | Status | Error text |
|---|---|---|
| Every question valid | ready | None |
| Some questions skipped | ready | Skipped plus the count and, for each, the first 60 characters of the prompt and the reason |
| The model returned no questions | failed | Nothing could be generated from the material provided |
| No question was valid | failed | No usable questions: plus each reason |
| The worker failed | failed | The error, or Something went wrong while generating questions. Try again in a moment. when the raw message is JSON or longer than 160 characters |
The questions are written with the exam's school pinned (withSchool), because the apply step runs without a tenant.
Import from a CSV
POST .../import-csv (importCbtQuestionsCsv) takes a multipart/form-data upload named file, up to 2 MB. The columns are type, prompt, marks, optionA to optionF, correct, acceptedAnswers, canonicalExpression, and timeLimitSeconds.
Each row is checked with the same question rules. Valid rows are added. Invalid rows are skipped and reported as Row plus the row number and the reason. The answer is 201 with { created, errors }. The import does not check aiGenerationStatus, so it can run while a generation is in progress.
In the Hub
CbtAiActions shows the three menu items. While a job is pending or running, the Hub polls the exam every two seconds and disables the menu. The busy label reads Gathering your schemes, plans and curriculum while pending and Writing questions from your material while running.
When the job ends, the Hub reloads the question list and shows a toast:
- Questions added to the exam when everything was valid.
- Questions added, some skipped, with the skip text, when some were left out.
- Question generation failed, with the error, when nothing was added.
All three routes are audited as a question create, with the change recorded as ai-generate-requested, ai-extract-requested, or csv-import.
Sources
Read in code, not run.
Naalya-APIatfbb63d5178bcfe171292daebb19b6cbdac025a44:apps/server/src/app/cbt/cbt-question.controller.ts(context,generate,extract,importCsv),cbt-question-generate.service.ts(context,generate,extract,importCsv,buildBrief,apply,loadForGeneration,teacherSchemes,MAX_HTML_PER_DOC),cbt-question-generate.controller.ts,dto/cbt.types.ts(CbtGenerationStatus), the generate and extract DTOs, andapps/worker/src/micro-ai/cbt-question-generate.processor.ts(readableFailure).Education-Hubat9e84f4c33d5f1a9b9ae84bd7f1e6663598504073:src/components/cbt/cbt-ai-actions.tsx.- API
main6bfb9f6bdb004fd3a909405c2c826cf1e383249b, same tree as staging0d611c966e84961c64e909ab50f424a0f2510b6f:teacherSchemesincbt-question-generate.service.ts. The test file in that compare was not run. Deployment was not verified.
Authoring and access
Subject-scoped exam creation, the draft-publish-close lifecycle, per-stream assignment rules, and the relationship-based CbtAccessService.
Student runtime and grading
The attempt lifecycle, start/resume, lazy expiry, all-or-nothing auto-grading, teacher finalise, and the write-through to the gradebook.