createIntent
Makes the intent that holds a program and its choices, and runs it.
function createIntent<I>(opts?): object;Defined in: packages/core/src/runtime/root.ts
An intent: it holds the program and the choices' layers, evaluates the program in any context,
turns a person's actions into commits, and owns model requests and program proposals.
Use pure realize for manual output, or connect for continuous host updates.
Integration-only subscription and commit access is available from /internal.
Type Parameters
| Type Parameter | Default type |
|---|---|
I | unknown |
Parameters
| Parameter | Type |
|---|---|
opts | IntentOptions<I> & object extends I ? unknown : object |
Returns
| Name | Type | Description |
|---|---|---|
commit() | (note, fn) => void | - |
constrain() | (target, c) => void | - |
draw() | (world, seed?) => void | One more draw of the sampling <World id={world} sample> (0089): a commit, in the history, with a new seed (or seed, for a test or a replay). Each choice read in that World that nobody decided gets a value drawn from its distribution, recorded with the seed, and takes it until the next draw; undo takes the draw back. Example import { choices, createIntent, host, useChoose, World } from '@reintent/core'; const Door = () => host('door', { to: useChoose(choices('childhood', 'unknown-world', 'home'), { of: 'door', name: 'to', }), }); const intent = createIntent({ program: ( <World id="door" sample={{ replace: false }}> <Door /> </World> ), }); const seen = new Set<unknown>(); for (let n = 0; n < 3; n++) { intent.draw('door'); seen.add(intent.realize().reads[0]!.value); } console.log(seen.size, intent.realize().reads[0]!.source); // → 3 'drawn' |
forget() | (id) => void | - |
pending() | () => RequestState[] | - |
prefer() | (target, toward, factor?) => void | Weighs toward up by factor (6 when not given) in every choice target names. |
preview() | (at, provide, options) => Realization | An evaluation under hypotheses, nothing skipped, nothing sent. With decisions: false, as if no person had decided anything: the same layers without their decisions (what the models answered, the constraints, preferences and facts stay), a world a host compares with the one shown, such as what a layout written before the decisions was written under (#408). |
tell() | (fact) => void | - |
unconstrain() | (target) => void | - |
unload() | (of, unloaded) => void | - |
untell() | (fact) => void | - |
get history | object | What a person committed, to undo and redo. |
get input | I | The root's input (0045 §3): what a root component gets as its props, and useInput() reads. |
get layers | Layers | - |
get program | RootProgram | - |
get revision | number | - |
get stats | object | - |
get version | string | The program's version: a proposal is accepted only on the version it was written on. |
act() | (input, value?) => void | Validate and perform a data action returned by a host. Unbound decide takes a data value. |
connect() | (host, options?) => HostConnection | Attach a host. Each commit is one way (0060 §3): it is the version this connection shows, and its requests are sent then, without waiting for the host to draw it. |
decide() | ( id, value, options? ) => void | Commit a decision, optionally with its original sentence and interpreter. Provenance is restored by undo/redo and exposed by explain/reads, without affecting evaluation or skipping. A string third argument remains a history note for existing callers. |
dispose() | () => void | Release all attached hosts and ignore late model answers. Idempotent. |
explain() | (id, provide?) => object | Why a choice is what it is, in a context (0005 §7). Its effective dist comes from an inspection apart from λ's skipping cache, one per state and context however many choices are explained; nothing is sent or written. Distributions are not in ChoiceRead. |
explore() | (o?) => PossibleWorld[] | The likeliest worlds over the choices a model holds (a beam), the rest summed out. At most judge new hypothetical judge requests are sent per call (default 4); zero is cache-only. Missing answers mark worlds partial, with probabilities from available layers. |
inspect() | () => object | Everything the intent holds now. |
measure() | ( at, by, stamp? ) => boolean | What the host measured of a choice's possibilities where it was read (at: a read, its identity and the contexts it was read in), as Code's Reduce writes it (0065): one that does not fit there is ruled out, and how well each did weighs it, in that context only (evidence, above a judgment); Code's taste there (prior) multiplies the prior, which a judgment replaces (below it); facts are kept for an explanation. A cache, like a belief: not a commit, not in the history; stamp names what it was measured on, and the same measurement again writes nothing. A read carries the evaluation it came from: a measurement of a read older than the one already kept there is refused, so a late result does not overwrite a newer fact. A preview's read (hypothetical) is refused too: what was measured under hypotheses is not this world's. Returns whether it wrote. Example import { choices, createContext, createIntent, host, useChoose, type Provide, } from '@reintent/core'; const Screen = createContext<{ id: string; width: number }>('Screen'); function Row() { const arrange = useChoose(choices('cols3', 'cols2', 'cols1'), { of: 'row', name: 'arrange', defaultValue: 'cols3', }); return host('row', { arrange }); } const intent = createIntent({ program: <Row /> }); const phone: Provide = [[Screen, { id: 'phone', width: 390 }]]; const desktop: Provide = [[Screen, { id: 'desktop', width: 1440 }]]; const read = intent.realize(phone).reads[0]; console.log(read.value); // → 'cols3' // The host lays out each possibility on this screen and says what it found. intent.measure(read, { cols3: { feasible: false }, cols2: { feasible: true, score: 0.3 }, cols1: { feasible: true, score: 1 }, }); const now = intent.realize(phone).reads[0]; console.log(now.value, now.source); // → 'cols1' 'measured' // Only where it was measured; and a person's decision is above it. console.log(intent.realize(desktop).reads[0].value); // → 'cols3' intent.decide({ of: 'row', name: 'arrange' }, 'cols3'); console.log(intent.realize(phone).reads[0].value); // → 'cols3' intent.history.undo(); console.log(intent.realize(phone).reads[0].value); // → 'cols1' |
onEscalate() | (fn) => () => undefined | Deprecated Renamed onUnresolved (0046 §5). |
onProposal() | (fn) => () => undefined | - |
onProposalFailed() | (fn) => () => undefined | Calls fn with each reprogramming the intent asked by itself (an escalation a <World> reprograms, or one nobody listens to) that failed: why it was asked, the error and System Two's refused reasons (0055 §4). With nobody listening, the failure is written to the console. Returns the unsubscribe. |
onUnresolved() | (fn) => () => undefined | Calls fn with each choice that cannot settle and that nothing around it handles: no <World onUnresolved> takes it, so it reaches the root (0046 §2). With no listener, System Two is asked to reprogram the whole program. Returns the unsubscribe. |
questions() | (contexts?, limits?) => Question[] | What to ask a person now: choices left unresolved that ask (or an escalation reached ask), and only where the answer would change something, some context where the possibilities give different worlds (Huxpro/airtalk#857 lib/questions.ts). judge caps new hypothetical judge requests across all contexts (default 4); zero uses cached answers and priors only. A choice read only inside a preview is not asked about. |
realize() | (provide?) => Realization | Realize a validated host tree without sending requests; connect commits it to a host. |
rejectAll() | (id) => void | A person rejects every possibility of a choice: it escalates (0005 reject). |
reprogram() | (req?) => Promise<Proposal> | Asks System Two to reprogram the whole program, or the part at names (0055 §3, 0056 §6): attempts drafts at most, each tried where it would be shown (each presentation on a screen now, and contexts; none of either: the intent's own) and checked against catalog; unset, the part's, then the intent's. Resolves to the proposal; rejects with ProgramRefused when every draft was refused, and at once when no part has the name at. |
review() | (p) => object | - |
setInput() | (next) => void | Change the root's input. It is not the program: no version, no history, and undo and an accepted proposal keep it. What was evaluated on the input before is stale (0034's epoch): it can no longer be presented, and an answer asked under it stays under it. The same input again (as data) changes nothing. |
setProgram() | (el, name?) => void | The program, and its name: revision 0, outside the history. |
Example
import {
choices,
createContext,
createIntent,
host,
useChoose,
useContext,
type Provide,
} from '@reintent/core';
const Screen = createContext<{ id: string; width: number }>('Screen', {
axes: { width: (s) => s.width },
});
function Deck() {
const { width } = useContext(Screen);
const layout = useChoose(choices('cols1', 'cols2'), {
of: 'deck',
name: 'layout',
defaultValue: width < 600 ? 'cols1' : 'cols2',
});
return host('deck', { layout });
}
const intent = createIntent({ program: <Deck /> });
// One program, evaluated in many contexts: they share the intent.
const phone: Provide = [[Screen, { id: 'phone', width: 390 }]];
const desktop: Provide = [[Screen, { id: 'desktop', width: 1440 }]];
console.log(intent.realize(phone).reads[0].value); // → 'cols1'
console.log(intent.realize(desktop).reads[0].value); // → 'cols2'
// A person's actions are commits, and can be undone.
intent.decide({ of: 'deck', name: 'layout' }, 'cols1');
console.log(intent.realize(desktop).reads[0].value); // → 'cols1'
intent.history.undo();
console.log(intent.realize(desktop).reads[0].value); // → 'cols2'
// What it would be, without writing anything.
const would = intent.preview({ 'deck#layout': 'cols1' }, desktop);
console.log(would.reads[0].value, would.reads[0].source); // → 'cols1' 'preview'Last updated on
Edit on GitHub