# LLM protocol Paste the block below into your backend's system prompt (OpenAI, Anthropic, or an LMS-specific MCP server). It is what makes a model emit payloads this library can hydrate. The schemas here are enforced by the validators in `components/learning/registry.tsx`. Those validators are the source of truth; the self-check in `components/learning/parse.ts` asserts that every worked example below still passes them, so this file cannot silently drift. ## What the parser tolerates Models do not follow instructions perfectly, so the parser is deliberately forgiving of the failures that actually happen in production: | Model behaviour | What the parser does | |---|---| | Payload still streaming | Renders a placeholder; never shows half-parsed JSON | | Stream cut off mid-tag | Same — the partial tag is withheld from the prose | | Wraps the JSON in ```json fences | Strips the fences and parses anyway | | Emits malformed JSON | Renders "An activity could not be displayed"; the surrounding lesson still renders | | Emits an unknown `type` | Same as malformed | | Emits a quiz with no correct option | Rejected — `success` in the xAPI statement would be meaningless | What it will not do is render a component from a payload that failed validation. ## The system prompt block ```text You are an expert AI tutor. While teaching the user, you must periodically test their understanding using custom interactive elements. When you want to issue an interactive checkpoint, stop writing standard text and output exactly one JSON object wrapped inside structural tags. CRITICAL RULES: 1. Do not include markdown code block syntax (like ```json) inside the tags. 2. The payload must strictly validate against the JSON schemas provided below. 3. Keep explanation strings concise, objective, and supportive. 4. Continue the lesson in prose after the closing tag. [SCHEMA: QUIZ] { "type": "quiz", "prompt": "The conceptual question being asked.", "options": [ { "id": "A", "text": "Option text", "valid": true, "feedback": "Why this is right." }, { "id": "B", "text": "Option text", "valid": false, "feedback": "Correction or hint." } ] } Requires at least two options, and at least one with "valid": true. [SCHEMA: FLASHCARD] { "type": "flashcard", "front": "Term, question, or snippet to memorize.", "back": "The answer, explanation, or core concept definition." } Both fields are required and must be non-empty. [SCHEMA: SEQUENCE] { "type": "sequence", "prompt": "What the learner is ordering.", "steps": [ { "id": "dns", "text": "Resolve the hostname via DNS" }, { "id": "tcp", "text": "Open a TCP connection" }, { "id": "send", "text": "Send the request" } ] } List the steps in the CORRECT order — the component shuffles them for display. Requires at least three steps with unique ids. [SCHEMA: MASTERY] { "type": "mastery", "title": "The topic being assessed.", "skills": [ { "id": "methods", "label": "HTTP methods", "level": 0.8 }, { "id": "idempotency", "label": "Idempotency", "level": 0.6 } ] } "level" is your assessment of the learner from 0 to 1, based on the conversation so far. Ids must be unique. Emit this at the end of a topic, not mid-explanation. [EXAMPLE IN-LINE OUTPUT] That wraps up how REST endpoints work. Let's see if you can spot the difference here: { "type": "quiz", "prompt": "Which HTTP method should be safely designed as idempotent?", "options": [ { "id": "A", "text": "POST", "valid": false, "feedback": "POST creates new resources and is typically non-idempotent." }, { "id": "B", "text": "GET", "valid": true, "feedback": "Correct! Multiple GET calls return the same data without altering state." } ] } What thoughts do you have on how this maps to database actions? ``` ## Wiring it up ```tsx import { LearningStream } from "@/components/learning/LearningStream"; // `text` is everything the model has emitted so far, not just the latest chunk. sendToLRS(s)} />; ``` `LearningStream` is pure with respect to `text`: re-parsing the full buffer on every chunk is what keeps it from drifting out of sync with the stream. Components are keyed by a stable activity id, so a learner's in-progress answer survives later chunks arriving. ## Telemetry Every interaction emits an xAPI statement to `onTrackTelemetry`, ready to forward to a Learning Record Store. There is no network call built in — the host app decides where it goes. ```json { "verb": { "id": "http://adlnet.gov/expapi/verbs/answered", "display": { "en-US": "answered" } }, "object": { "id": "http://beadkit.dev/activities/streamed-0", "definition": { "name": { "en-US": "Which HTTP method should be safely designed as idempotent?" } } }, "result": { "response": "B", "success": true, "completion": true, "duration": "PT14S" } } ``` A flashcard emits `experienced` with no `response` or `success`: those fields are omitted rather than sent as null, because an LRS reads a present-but-null `success` as a scored attempt. Flipping a card back and forth emits one statement, not one per flip. The `actor` field is intentionally absent. Identity belongs to the host app, which should merge it in before forwarding to its LRS.