Reintent

Host catalogs

Say which kinds a host draws as data, check every frame against it, and accept only the events a frame offers.

A host draws some kinds and not others: Sam's app draws a stack, a text, a button and Softoy's bear; a terminal draws text and numbered buttons. A catalog says which, as data. The same catalog gives the intent its types, gives a model a description it can read, and lets the evaluator check every frame before a host gets it.

Reintent ships no vocabulary. The application declares its own catalog; a Web host may add the DOM's.

Declare a catalog

@reintent/catalog declares one with Zod: defineCatalog, a kind for each tag, its props a Zod object, and its events.

catalog.ts
import { z } from 'zod';
import { action, control, defineCatalog, kind } from '@reintent/catalog';

export const card = defineCatalog(
  { id: 'card', version: 1 },
  {
    stack: kind({
      about: 'children in a row or a column',
      props: z.object({ axis: z.enum(['h', 'v']), gap: z.number().min(0).default(8) }),
      children: 'nodes',
    }),
    button: kind({
      about: 'a button a person presses',
      props: z.object({ label: z.string(), disabled: z.boolean().default(false) }),
      events: { press: action().or(control({ args: z.string().optional() })) },
    }),
    'card:bear': kind({
      about: 'the bear, drawn by the host from its own recipe',
      props: z.object({ size: z.enum(['s', 'm', 'l']) }),
      events: { pick: action({ value: z.enum(['s', 'm', 'l']) }) },
    }),
  },
);
  • about is required: it is how a model picks a kind, and how a person reads the catalog.
  • children is none (the default), text (strings and numbers) or nodes.
  • An event is a prop that holds what the node sends back. action sends a HostAction. With value, a decide without a value takes one the host gives, within value, and a decide that carries its value holds to value; without value, a node's decide carries its value. control sends an application control, { name, args? }, which the session carries out. .or() takes an action or a control, one of each at most; .optional() lets a node leave the prop out. Optional args (control({ args: z.string().optional() })) may be left out by the host; an action's value may not be .optional() (optional-value when the catalog is defined), since a decide without a value is the host's to fill. Each is an EventDef.
  • A kind an application adds to a shared vocabulary has a namespace: card:bear. The name in JSX and on the wire is the same string.

What kind returns is a KindDef, and defineCatalog gives a Catalog. It may compose catalogs (defineCatalog(meta, dom, card)): a kind named twice is an error, not the later one winning. A catalog is checked as it is defined, and InvalidCatalog lists every problem.

The contract

exportCatalog gives the catalog as every host reads it: a HostCatalog, JSON. Each kind is a CatalogKind, its props and event values a CatalogSchema, a subset of JSON Schema: type, enum, const, properties, required, additionalProperties: false, items, minItems, maxItems, minimum, maximum, minLength, maxLength, anyOf, description, default. A union is anyOf: a value holds when a branch takes it, as Zod takes the first branch that parses. Its events are CatalogEvents of CatalogSends.

What the subset cannot say is an error when the catalog is defined: a refine, a transform, a pattern, an exclusive bound. It is never dropped quietly, because a native host that reads only the JSON would check less than the Web host does.

A host that reads the JSON checks it with checkCatalog. The contract is plain data, so iOS and Android read the same file.

Types in the intent

CatalogElements gives each kind's props as TypeScript sees them, a KindProps: a prop with a default may be left out, an event takes an action or a control and never a closure, and a kind outside the catalog does not exist. hostOf(card) is host typed by them:

const node = hostOf(card);
node('stack', { axis: 'v', children: [node('button', { label: 'Go', press: action.tell('go') })] });

A catalog's JSX

A catalog has its own JSX entry: a file that sets @jsxImportSource to it writes the catalog's kinds as lowercase tags, and only those. jsxEntry writes the entry's source. Put it in jsx-runtime.ts beside the catalog, and export it from the package as ./jsx-runtime and ./jsx-dev-runtime:

scripts/jsx.mjs
import { writeFileSync } from 'node:fs';
import { jsxEntry } from '@reintent/catalog';

writeFileSync('src/jsx-runtime.ts', jsxEntry({ from: './catalog.js', name: 'card' }));
card.intent.tsx
/** @jsxImportSource @app/card */
import { action, choices, useChoose } from '@reintent/core';

const sizes = choices('s', 'm', 'l');

export function Card() {
  const size = { of: 'bear', name: 'size' };
  return (
    <stack axis="v">
      <button label="Go" press={action.tell('go')} />
      <card:bear size={useChoose(sizes, { ...size, defaultValue: 's' })} pick={action.decide(size)} />
    </stack>
  );
}

The entry types the tags with CatalogElements; its functions are intent JSX's (@reintent/core/intent). TypeScript refuses <div> here (Property 'div' does not exist on type 'JSX.IntrinsicElements'), <stack axis="z">, and a closure in press. A component imported from another file may still give another kind, so every frame is checked as well.

<card:bear> is the kind 'card:bear' to TypeScript, esbuild, Vite and Sucrase, which the runtime lab and the sandboxes compile with. A name may hold a dash or a keyword: <card:bear-cub>, <card:default>.

The DOM catalog

@reintent/dom is the DOM's elements as a catalog, for a Web host only: div, p, h1, a, img, button, input, select, form and the rest, with data props by React's names (className, htmlFor) and their events as actions: onClick, an onChange the host gives the field's value to, a form's onSubmit. @jsxImportSource @reintent/dom writes them. Its declaration is @reintent/dom/catalog (dom); its contract is DOM. An application that writes both writes one catalog of them, defineCatalog(meta, dom, card), and generates the entry for that.

Every element takes style, React's style object: <p style={{ color: 'red', padding: 8 }}>. A CSS property list does not fit a closed contract, so style is declared with opaque: it exports as { description }, the contract's any value, and its Zod schema only gives TypeScript the type. The host takes the value as it is. opaque is optional, takes no default, and is a prop of a kind itself, never inside an object, an array or an event. It belongs in a host extension like the DOM catalog. An application's own kinds, such as a tone picker, go in its own catalog with a schema for every prop, and implement decides how they look.

Drawing a catalog in React

implement gives the React component for each kind, an Implementation to pass to ReintentProvider or <ReintentView> as their components:

hosts.tsx
import { implement } from '@reintent/react';
import { card } from './catalog.js';

export const hosts = implement(card, {
  stack: (p) => <div className={`stack-${p.axis}`}>{p.children}</div>,
  button: (p) => <button disabled={p.disabled} onClick={() => p.press()}>{p.label}</button>,
  'card:bear': (p) => <Bear size={p.size} onPick={p.pick} />,
});
  • The components are KindComponents: TypeScript checks each against its kind's KindComponentProps, and a kind left out. implement checks the kinds again when it is called and throws for a kind left out, one not in the catalog, or a value that is not a component. The DOM catalog's kinds may be left out: they are drawn as the element of their name.
  • A component gets the node's data with the catalog's defaults filled in, and each event as a function of the value the host gives (p.pick('m')). A value with defaults may be given without them.
  • Each frame is checked before it is drawn. A kind outside the catalog, a prop it does not take, or an action it does not offer throws a CatalogRejection, with its errors, and the frame is never drawn or presented.
  • An event is checked against the offer of the node that drew it and normalized (checkEvent), then goes to intent.act; a control goes to the view's onControl. A function a component kept from a frame that is no longer shown (a later frame was drawn, or the view is gone) is refused. When the intent changes before React draws again (a card and its button in one click), the next action is carried out only if the latest evaluation still offers it to the same node; otherwise nothing is written.

Every frame, checked

Before a host gets λ, the evaluator runs checkFrame on it, with the kinds the host said it draws and the kinds the program requires:

  1. Normalize: the catalog's defaults are filled in. The host draws and acknowledges this tree, never the one before it.
  2. Validate: each node's kind is in the catalog, in the host's kinds and in the program's requires; its props, children and events hold. Every data prop is data: a key whose value is undefined is left out, and a function, a cycle, an object that is not plain or a number that is not finite is not-data at its path (/props/style/color).
  3. Collect what the frame offers: every action and control a node holds, with the values its event allows.

The result is a FrameCheck. Each problem is a CatalogError, { stage, path, code, expected, actual, message }, where the path is a JSON Pointer into the tree and message is a sentence a model can act on: button.label: a string, given 3. The CatalogStage says which step it came from. Passing the schema says nothing about whether a frame is right or allowed.

Events a frame offers

A host sends back an action or a control, a HostEvent. checkEvent takes it only when a node of the frame offered it, an EventOffer, with a value or args its event allows. An origin the host adds (action.with(…, { gesture: 'click' })) is kept and is not compared. It returns an EventCheck: the action to pass to intent.act, its value bound, or the control, as a ControlRef, for the session's handler. An action no node offered is refused with not-offered. What the host gives is normalized before it is checked, so the action carries its defaults; an error's path points into the event (/value/size), and node at the prop that offered it. checkEvent checks against the frame it is given: that only the latest frame may be answered is the session's rule, when it chooses the frame. Given the reads of the realization the frame came from (checkEvent(frame, event, { reads })), it also checks a decided value against the possibilities its choice was read with: the schema says what a host may send, K what the intent can take.

Recap

  • A catalog is the application's: defineCatalog, a kind for each tag, props as a Zod object, events as action and control.
  • exportCatalog gives the JSON contract every host reads; what it cannot say is an error.
  • A catalog's JSX entry (jsxEntry) types its kinds as tags; @reintent/dom is the DOM's.
  • implement draws a catalog in React, and every frame is checked before it is drawn.
  • checkFrame fills defaults, checks the tree and collects what it offers; checkEvent accepts only those.

Last updated on

Edit on GitHub