Standalone hosts
Run an intent without React, and connect its data tree to a host.
Program, instance, host
A program is the pure JSX or h tree. createIntent owns a running instance: choices, program versions, model requests and proposals. A host consumes the resulting tree. It does not have to understand an external store.
import { action, choices, createIntent, h, host, useChoose } from '@reintent/core';
const tone = { of: 'card', name: 'tone' };
function Card() {
const value = useChoose(choices('warm', 'formal'), { ...tone, defaultValue: 'warm' });
return host('card', { pick: action.decide(tone), children: value });
}
const intent = createIntent({ program: h(Card, {}) });
const connection = intent.connect({
commit(tree) { console.log(tree); },
dispose() { /* release host resources */ },
});
intent.act(action.decide(tone), 'formal');
connection.disconnect();
intent.dispose();The runnable Node example is packages/core/examples/standalone.mjs: build core, then run it with Node. It imports no React. IntentHost defines commit and optional dispose and onError; a HostConnection provides update(provide) and disconnect(). Separate instances isolate state; connections to one instance share choices, history and program.
Disconnect releases only that host and leaves other displays active. After the last display leaves, the runtime checks in a microtask that no new display attached, then invalidates pending replies and old acknowledgements. Choices, history and program remain. Disposing the instance closes all connections and ignores late answers; it cannot cancel third-party Promises.
The root's input
A program, a RootProgram, is an element or a root component: createIntent({ program: Card, input: { to: 'Ann' } }) calls Card with the input as its props. intent.input is that input, and intent.setInput(next) changes it. Any component, under an element program too, reads it with useInput.
import { createIntent, host, useInput } from '@reintent/core';
function Card(p: { to: string }) {
return host('card', { to: p.to });
}
function Signature() {
const { to } = useInput<{ to: string }>();
return host('signature', { for: to });
}
const card = createIntent({ program: Card, input: { to: 'Ann' } });
card.setInput({ to: 'Bo' }); // the same program, on another input- TypeScript holds
inputandsetInputto the root component's props: a field left out or of the wrong type does not compile. - The input is data: JSON, kept as a frozen copy. A function or a cycle throws an
InvalidInput. setInputis not the program. It makes no version and no step in the history, and undo keeps it. Every path evaluates on the input there is:realize,preview,explain,explore,connect,reprogram. A model's answer is kept under the input it was asked with, so an answer that comes late for an earlier input decides nothing for this one.
Slots
A root input can declare a logical slot, slot(name): a node of the reserved kind reintent:content, which is data. The program places it like any child; a host that supports host content draws content of its own there, and the program never sees what it is. React's view gives the slot its children.
import { checkFrame, createIntent, host, slot, slotsOf, type Content } from '@reintent/core';
function Card(p: { to: string; children?: Content }) {
return host('card', { to: p.to, children: p.children });
}
const card = createIntent({ program: Card, input: { to: 'Ann', children: slot('children') } });
card.connect({ content: true, commit(tree) { /* draw the slot with content of its own */ } });
checkFrame(catalog, card.realize().tree, { content: { slots: slotsOf(card.input) } });- A host declares that it supports host content:
connect(host)withcontent: true,checkFramewithcontent: { slots }. A host that does not refuses every tree that holds a slot, at deploy and in every frame;connectthrows aContentRejectedbefore anything is committed; a later frame that fails this check disconnects the host and reports the error toonError, and the call that changed the input throws it. A slot goes among the children: one in a prop, at any depth, is refused for every host (content). - A slot the program places must be one the input declares (
slotsOf): a program that makes one up, withhost('reintent:content', …)or otherwise, is refused (undeclared-slot). A root component types a slot's prop asContent. reintent:is the protocol's namespace: a catalog cannot declare a kind in it.
Evaluation and acknowledgement
intent.realize(provide) is cached, pure evaluation. It validates the data tree and sends no model requests: use it for drafts, inspection and offline output. preview also never writes or sends requests.
intent.connect(host, { provide }) keeps evaluating and passing trees to host.commit(tree). A commit is one way: when commit returns, the connection counts the tree as shown and sends its requests, as React runs an effect after its own commit without waiting for the screen (0060 §3). The answer to a request made under an earlier input stays with that input (0034).
A host whose transport has its own acknowledgement keeps it in its own protocol. The runtime lab's session (0036) sends a frame and waits for the host's ack before the next one; the trees committed meanwhile wait in the session, and the ack sends only the latest.
With each tree, commit(tree, observed) gets an Observation of the same evaluation, from observationOf: its ChoiceObservations, what each choice it read was and where it came from, and its UnresolvedObservations, what it could not settle. They are data, and only the fields a host reads.
A commit that throws disconnects and cleans up the host: the error goes to the caller that triggered it and to host.onError. Synchronous feedback stops after 100 commits to prevent an infinite loop. Throw in commit for unsupported kinds. The runtime cannot undo an external operation a host has already performed.
A Worker or network host can send the tree and its observation from commit and return at once: serveViews and remoteView in @reintent/lib/remote do this across a transport (Handles). Do not transmit local traces or request objects.
Data protocol and actions
HostData is JSON data: null, booleans, finite numbers, strings, arrays and plain records. Realization.tree uses Rendered | Rendered[] | null; nodes are HostNode values with a nonempty kind, data props, and expanded children. encodeHostTree validates and copies a tree into a HostMessage with protocol reintent.host, version 1; decodeHostTree validates a received envelope. JSON round trips and structured clone share the contract.
Optional object properties whose value is undefined are omitted. Arrays containing undefined or holes, closures, nonfinite numbers, symbols, bigint, class instances, accessors, hidden fields and cycles are rejected with a path. toJSON is never executed. realize and connect validate before a host receives output.
A HostAction is ordinary data from creation. Factories include action.decide(id, value?), forget, constrain, unconstrain, prefer, tell and untell. Within an entity scope, family handles resolve to the current instance using the same identity rules as useChoose. action.with(value, { gesture, said, via }) copies an action and adds provenance; via can be person.
Pass actions to intent.act, optionally supplying an unbound decision's value. A bound decision ignores the extra event argument. checkAction provides diagnostics, decodeHostAction validates incoming data, and intent.act validates again. Actions describe operations; they are not authorization tokens. Hosts crossing a trust boundary authorize messages themselves.
A model that works on the intent does what a person does, so its tool is the actions. actionSchema(intent.realize().reads) gives them as a JSON Schema (2020-12): one decide, forget and constrain for each choice read, its value one of the possibilities it was read with, plus tell and untell. A choice that does not apply was not read, so it is not offered; a possibility a constraint or forbid ruled out is not either. Give it to a model as a tool's input schema. What comes back still goes through intent.act, and checkAction(x, reads) also says when a decided value is not one of those possibilities.
Application-specific operations in the sandboxes, such as program switches and language rules, use data like { event: 'say', args: [text] }, dispatched through the example's exported events table. These belong to the application's own host protocol; they are not core HostAction values and are not passed to intent.act.
Migrating existing hosts
The old effectful realize becomes pure evaluation. Replace the read loop, subscription and manual acknowledgement with connect, and callback props with data actions. Public instances no longer expose store, evaluate, present or cancelPending. New hosts do not need an internal adapter.
In React, a root is mounted with reintentView(Root) under a ReintentProvider (Roots in React). An instance held outside React is shown with <ReintentView intent={intent} />, and read and changed with useReintent(intent, select). They own React's content handles and commit timing; core provides the shared instance and its epoch.
Last updated on
Edit on GitHub