Protocol and Types
Detailed reference for the core runtime contracts, payloads, and message types.
This is the CONTRACT layer of ToonUI.
If you do not understand these types, you are integrating blindly.
ToonProtocol
createToonProtocol() returns a ToonProtocol.
It exposes:
promptrulescatalogeventsmessages
const toon = createToonProtocol();ToonRuntime
A runtime extends the protocol with execution helpers:
componentsparsevalidateextractBlocks
That means a runtime is effectively:
- protocol contract
- parsing tools
- validation tools
- renderer-facing component registry
ToonRules
rules is the compact machine-usable representation of the language.
It includes:
componentsbuttonVariantsbadgeVariantsalertVariantsconfirmVariantsfieldTypeschartTypes
This is useful when composing prompts programmatically.
ToonDocument
The parser returns a ToonDocument:
type ToonDocument = {
type: 'document';
body: ToonNode[];
}The body array is the AST root.
ToonBlock
extractBlocks() identifies fenced ToonUI blocks inside larger markdown content.
Each block includes:
rawlanguagestartendcomplete
This is useful when parsing model output that mixes prose and UI.
ValidationIssue
Each validation issue contains:
codemessage- optional
line - optional
column
This makes syntax and semantic failures traceable back to the original source.
ValidationResult
Validation returns:
{
ok: boolean;
errors: ValidationIssue[];
warnings: ValidationIssue[];
}In practice, the most important field is ok, but the real debugging value is in errors.
ReplyPayload
Used for button-style interactions.
type ReplyPayload = {
kind: 'ui_reply';
eventId: string;
source: 'button';
value: string;
component: 'button';
line?: number;
context?: Record<string, SubmitValue>;
}Important meaning:
valueis the app-facing user intenteventIduniquely identifies the interactioncontextlets you attach extra structured metadata when needed
SubmitPayload
Used for form-style interactions.
type SubmitPayload = {
kind: 'ui_submit';
eventId: string;
source: 'form';
intent: string;
formTitle: string;
values: Record<string, SubmitValue>;
line?: number;
}Important meaning:
intentidentifies the host-level actionformTitleis human-facing contextvaluesis the normalized submitted data
ToonModelMessage
Use this when the next step is feeding the interaction back into the model loop.
It includes:
rolekindcontentdisplayContentpayload
ToonUIMessage
Use this when the next step is appending a message into UI/chat state.
It includes:
idrolepartsmetadata
This shape is intentionally friendly to message-based chat interfaces.
Type aliases worth knowing
ToonChatMessage= model-facing interaction messageToonChatUIMessage= UI-facing interaction messageToonInteractionPayload=ReplyPayload | SubmitPayload
Practical takeaway
If you remember only one thing, remember this:
eventscreate interaction payloadsmessagesconvert those payloads for the next host step