Schema Renderer
The model returns JSON; Kernel renders it with real components. A typed, versioned schema of seventeen presentational and form nodes, an allow-list for every prop, URL sanitising, and actions that come back to you as data instead of running.
Book a table
Kernel Bistro · Friday
Safe by construction
The input is untrusted, so it goes through an allow-list before anything renders. An unknown node type, an unknown prop (onClick,style, dangerouslySetInnerHTML) or a value outside a prop's enum is dropped and reported to onIssue, or to the console if you don't pass one. It never throws. There's no node for raw HTML, and every string lands as a text node. Link and image URLs pass only as http(s), mailto: (links only) or relative. Tabs and newlines inside a scheme are stripped first, the way a browser does, so java
script: doesn't slip through.
Nothing the model writes can act. A Button callsonAction({ action, payload, values }). A Form is a real <form>, so required andtype="email" are checked natively and Enter submits it, but it has no action attribute: submitting it callsonAction with every named field's value.
Streaming
parsePartialJson closes whatever is open (strings, arrays, objects) and drops a dangling key, comma or half-typedtru, so every prefix of a response is a renderable tree. Feed it the whole accumulated text on each chunk. Whilestreaming is set, a node type that's still being typed is skipped quietly and issues wait until the stream ends. Nodes are keyed byid (or type and position), so the DOM, and anything typed into a field, survives as the tree grows.
Usage
import { SchemaRenderer, parsePartialJson, schemaPrompt } from "@kernelui-lib/react";
// 1. Tell the model what it may return.
const system = `You can answer with UI.\n${schemaPrompt}`;
// 2. Re-parse the accumulated text on every chunk.
let text = "";
for await (const chunk of stream) {
text += chunk;
setTree(parsePartialJson(text));
}
setDone(true);
// 3. Render it. Buttons and forms come back as data.
<SchemaRenderer
value={tree}
streaming={!done}
onAction={({ action, payload, values }) => send({ action, payload, values })}
onIssue={(issue) => console.debug(issue)}
/><kernel-schema-renderer streaming></kernel-schema-renderer>
<script type="module">
import { parsePartialJson } from "@kernelui-lib/elements";
const renderer = document.querySelector("kernel-schema-renderer");
let text = "";
for await (const chunk of stream) {
text += chunk;
renderer.value = parsePartialJson(text);
}
renderer.removeAttribute("streaming");
renderer.addEventListener("action", (event) => send(event.detail));
</script>The schema
Version 1. A document is{ "version": 1, "root": Node | Node[] }and a node is { type, id?, props?, children? }. A bare node or array also works. The version only changes for a breaking change: a new node type or optional prop keeps the number, and a document from a newer version renders whatever this one understands, with anunsupported-version issue.
| Type | Props | Children |
|---|---|---|
Stack | direction, gap, align, justify, wrap | nodes |
Text | text, tone, size, weight | text, Link |
Heading | text, level | text, Link |
Button | label, variant, size, action, payload, submit, disabled | — |
Badge | text, variant | — |
Card | title, description | nodes |
Form | action, payload | nodes |
TextField | name, label, type, placeholder, description, defaultValue, required, disabled, hideLabel | — |
Select | name, label, options, description, defaultValue, required, disabled | — |
Checkbox | name, label, defaultChecked, required, disabled | — |
Switch | name, label, defaultChecked, disabled | — |
Table | caption, columns, rows | — |
Callout | title, text, variant | text, Link |
Separator | orientation | — |
Image | src, alt, width, height | — |
Link | href, text, external | text, Link |
Progress | label, value, max | — |
validateSchemaNode(value) returns the issues without rendering, and kernelSchemaJson is the same schema as JSON Schema (draft 2020-12), also shipped as schema.json in both packages.
Usage
import { validateSchemaNode, kernelSchemaJson } from "@kernelui-lib/react";
const issues = validateSchemaNode(JSON.parse(modelOutput));
// [{ code: "unknown-type", path: "root.children[2]", type: "Script", message: "…" }]
// Or hand the JSON Schema to the provider's structured-output option.
// Also shipped as a file: @kernelui-lib/react/schema.json
response_format: { type: "json_schema", json_schema: { name: "kernel_ui", schema: kernelSchemaJson } }Prompt
schemaPrompt describes all of this compactly for a system prompt. It's generated from the same table the validator reads, so it never offers a prop the renderer would drop. This is it, verbatim:
Respond with JSON for Kernel's SchemaRenderer (schema v1): {"version":1,"root":Node|Node[]}.
Node = {"type":T,"props":{...},"children":[Node|string]}. Only the types and props below exist; anything else is dropped. No HTML, styles or scripts.
Buttons never navigate: they send {action,payload,values} to the app. Put inputs in a Form with a submit Button to collect values by name.
- Stack {direction?: "vertical"|"horizontal", gap?: "none"|"xs"|"sm"|"md"|"lg", align?: "start"|"center"|"end"|"stretch", justify?: "start"|"center"|"end"|"between", wrap?: boolean} — Layout column or row. children: Node[].
- Text {text?: string, tone?: "default"|"muted"|"success"|"warning"|"danger", size?: "sm"|"md"|"lg", weight?: "regular"|"medium"|"bold"} — A paragraph. Use `text` or string/Link children. children: (string|Link)[].
- Heading {text?: string, level?: 2|3|4|5|6 (default 3)} — A section heading. children: (string|Link)[].
- Button {label: string, variant?: "primary"|"secondary"|"ghost"|"danger", size?: "sm"|"md"|"lg", action?: string, payload?: json, submit?: boolean (submit the enclosing Form), disabled?: boolean} — Emits onAction({ action, payload, values }); never navigates.
- Badge {text?: string, variant?: "neutral"|"accent"|"success"|"warning"|"danger"} — Small status chip.
- Card {title?: string, description?: string} — Bordered group with an optional title. children: Node[].
- Form {action?: string, payload?: json} — Collects its fields; a submit Button emits onAction with the form's action. children: Node[].
- TextField {name: string, label: string, type?: "text"|"email"|"number"|"password"|"search"|"tel"|"url", placeholder?: string, description?: string, defaultValue?: string, required?: boolean, disabled?: boolean, hideLabel?: boolean} — Labelled text input. `name` keys its value.
- Select {name: string, label: string, options: [{ value, label, disabled? }], description?: string, defaultValue?: string, required?: boolean, disabled?: boolean} — Native select.
- Checkbox {name: string, label: string, defaultChecked?: boolean, required?: boolean, disabled?: boolean} — Checkbox; its value is a boolean.
- Switch {name: string, label: string, defaultChecked?: boolean, disabled?: boolean} — On/off switch; its value is a boolean.
- Table {caption?: string, columns: [{ key, label }], rows?: [{ [key]: string | number | boolean | null }]} — Data table; cells are plain text.
- Callout {title?: string, text?: string, variant?: "info"|"success"|"warning"|"danger"} — Highlighted message. Use `text` or string/Link children. children: (string|Link)[].
- Separator {orientation?: "horizontal"|"vertical"} — Divider.
- Image {src: url(http|https|relative), alt: string, width?: number, height?: number} — Image. `alt` is required ("" if decorative).
- Link {href: url(http|https|mailto|relative), text?: string, external?: boolean (open in a new tab)} — Hyperlink. Use `text` or string children. children: (string|Link)[].
- Progress {label?: string, value?: number, max?: number} — Progress bar; omit `value` for indeterminate.
Example: {"version":1,"root":{"type":"Card","props":{"title":"Book a demo"},"children":[{"type":"Form","props":{"action":"book"},"children":[{"type":"TextField","props":{"name":"email","label":"Work email","type":"email","required":true}},{"type":"Button","props":{"label":"Book","variant":"primary","submit":true}}]}]}}Playground
Switch samples and watch the component, and the code, update live.
Book a table
Kernel Bistro · Friday
Usage
// Booking form
const tree = {"version":1,"root":{"type":"Card","props":{"title":"Book a table","description":"Kernel Bistro · Friday"},"children":[{"type":"Form","props":{"action":"book_table","payload":{"restaurant":"kernel-bistro"}},"children":[{"type":"TextField","props":{"name":"name","label":"Name","required":true}},{"type":"Select","props":{"name":"time","label":"Time","options":["18:00","18:30","19:00",{"value":"19:30","label":"19:30 (waitlist)","disabled":true}],"defaultValue":"18:30"}},{"type":"Checkbox","props":{"name":"outside","label":"Sit outside if it's dry"}},{"type":"Stack","props":{"direction":"horizontal","gap":"sm"},"children":[{"type":"Button","props":{"label":"Book","variant":"primary","submit":true}},{"type":"Button","props":{"label":"Not now","variant":"ghost","action":"dismiss"}}]}]}]}};
<SchemaRenderer
value={tree}
onAction={({ action, payload, values }) => handle(action, payload, values)}
onIssue={(issue) => log(issue)}
/><kernel-schema-renderer></kernel-schema-renderer>
<script type="module">
const renderer = document.querySelector("kernel-schema-renderer");
renderer.value = {"version":1,"root":{"type":"Card","props":{"title":"Book a table","description":"Kernel Bistro · Friday"},"children":[{"type":"Form","props":{"action":"book_table","payload":{"restaurant":"kernel-bistro"}},"children":[{"type":"TextField","props":{"name":"name","label":"Name","required":true}},{"type":"Select","props":{"name":"time","label":"Time","options":["18:00","18:30","19:00",{"value":"19:30","label":"19:30 (waitlist)","disabled":true}],"defaultValue":"18:30"}},{"type":"Checkbox","props":{"name":"outside","label":"Sit outside if it's dry"}},{"type":"Stack","props":{"direction":"horizontal","gap":"sm"},"children":[{"type":"Button","props":{"label":"Book","variant":"primary","submit":true}},{"type":"Button","props":{"label":"Not now","variant":"ghost","action":"dismiss"}}]}]}]}};
renderer.addEventListener("action", (event) => handle(event.detail));
renderer.addEventListener("issue", (event) => log(event.detail));
</script>Props
| Prop | Type | Default | Notes |
|---|---|---|---|
value | unknown | Required | The model's output: a SchemaDocument ({ version, root }), a bare node, or an array of nodes — usually straight from JSON.parse, or from parsePartialJson while it streams. Anything unknown or unsafe is dropped, never rendered and never thrown on. |
streaming | boolean | false | The value is still arriving. Half-typed node types are skipped quietly instead of reported, issues are held back until streaming ends, and the root is marked aria-busy. |
onAction | (action: SchemaAction) => void | — | A Button was pressed or a Form submitted. Nothing else happens: the renderer never navigates, fetches or runs code on the model's behalf. |
onIssue | (issue: SchemaIssue) => void | — | Every dropped node, prop or URL, once per settled value. Without a handler they go to console.warn instead. |
onValuesChange | (values: SchemaValues) => void | — | A named form control changed. |
className | ClassNameValue<SchemaRendererState> | — | A class string, or a function of the component's state that returns one. |
Web Components
| Tag | Attributes | Events |
|---|---|---|
<kernel-schema-renderer> | value, streaming | issue, action, valueschange |
Accessibility
- Every node renders as the real Kernel component, so a TextField is a labelled
<input>, a Switch arole="switch"button, a Table a real<table>with column headers. - A form control without a
labelfalls back to itsnamerather than rendering unlabelled; one with neither is dropped. An Image withoutaltis reported and rendered as decorative. - The root is
aria-busywhilestreaming, so assistive tech waits for the finished tree. - A link with
externalopens in a new tab withrel="noopener noreferrer".