# 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.

## 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 calls `onAction({ action, payload, values })`. A Form is a real `<form>`, so `required` and `type="email"` are checked natively and Enter submits it, but it has no `action` attribute: submitting it calls `onAction` with every named field's value.

## Streaming

`parsePartialJson` closes whatever is open (strings, arrays, objects) and drops a dangling key, comma or half-typed `tru`, so every prefix of a response is a renderable tree. Feed it the whole accumulated text on each chunk. While `streaming` is set, a node type that's still being typed is skipped quietly and issues wait until the stream ends. Nodes are keyed by `id` (or type and position), so the DOM, and anything typed into a field, survives as the tree grows.

## 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 an `unsupported-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.

## 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:

```text
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.

## 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 a `role="switch"` button, a Table a real `<table>` with column headers.
- A form control without a `label` falls back to its `name` rather than rendering unlabelled; one with neither is dropped. An Image without `alt` is reported and rendered as decorative.
- The root is `aria-busy` while `streaming`, so assistive tech waits for the finished tree.
- A link with `external` opens in a new tab with `rel="noopener noreferrer"`.
