Quick start
A birthday card for Grandma, with words nobody can settle in advance.
Sam wants to make a birthday card for Grandma, who is turning 90, and says just that: “A birthday card for Grandma. She’s turning 90.” The whole family will read it at her birthday dinner. Making the card is easy. Deciding what it says is harder, and nobody can settle that while writing the code. Should it sound warm, funny or formal? What should the message be? A model can make a good first guess and lay out the other ways it could go, but Sam has the last word, and when Sam changes their mind, the card changes with it.
Reintent is for programs like this one. Let's build the card, step by step.
Step 1: A card that is always the same
First, the card itself: a heading for Grandma, a line of text, Sam's name. Write it as you would in any framework, as a component that takes props and returns what the host should draw. Then show it in Sam's app.
/** @jsxImportSource @reintent/dom */
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
return (
<div
style={{ maxWidth: 380, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' }}
>
<h1 style={{ color: '#b5562b' }}>
Happy {p.age}th birthday, {p.to}!
</h1>
<p>Wishing you a wonderful day.</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
Starting…
Starting…
Nothing logged.
Card is the whole program. It takes the card's facts as props and returns host nodes: here a div with a heading and two paragraphs. App.tsx is Sam's app, written in React. reintentView(Card) turns the program into a component, GrandmaCard, and the props it is given are the root's input. Each <GrandmaCard> holds an Intent of its own. Switch the card's pane to λ and you see itself, which is plain data: a tree of host nodes. Preview shows what React draws from that tree.
Lowercase tags are a catalog's kinds
In React, <h1> is a DOM element. In an intent, <h1> is a host node of the kind h1, and the file's first line, @jsxImportSource @reintent/dom, types the tags by the DOM catalog. Every frame is checked against that catalog before it is drawn. A host with another catalog draws other kinds: one could draw a <door> as a door.
Try changing age={90} in App.tsx to another number. The input changes, and the same program is evaluated again under it. An Intent with no open questions is just a pure function of its input.
Step 2: One open question: the tone
The card has a tone to settle: warm, funny or formal. If the code picks one, it has decided for Sam. So go through the card part by part and ask whether each part is a fact or a guess.
| What | Fact or guess? | So it is |
|---|---|---|
| Grandma, 90, from Sam | facts | props, the input |
| The title | follows from the tone and the facts | computed |
| Warm, funny or formal | a guess: it depends on Grandma, the occasion | a choice |
Only the guesses become choices. Anything you can compute stays in the code, just as React keeps derived values out of state. That leaves the tone open:
/** @jsxImportSource @reintent/dom */
import { choices, useChoose } from '@reintent/core';
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return `${p.age} laps around the sun, ${p.to}!`;
}
/** An open question: which tone? Its identity is { of, name }. */
export function useTone() {
return useChoose(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
});
}
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
const tone = useTone();
return (
<div
style={{
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
}}
>
<h1 style={{ color: LOOK[tone].accent }}>{title(p, tone)}</h1>
<p>Wishing you a wonderful day.</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
Starting…
Starting…
Nothing logged.
useChoose reads a choice. It takes:
- the possibilities,
choices<Tone>(...TONES): the typeToneis the space . With no weights given, all three are equally likely. - an identity,
{ of: 'card', name: 'tone' }: what the choice is about, and which question it is. Decisions and judgments are stored by identity, so they can be found again after the program changes. - a default,
'warm', used while nobody has answered.
In Choices, click funny. That is a decision, and the card is realized again with it. Click Forget, and the default comes back.
Step 3: Buttons that decide
In step 2 you picked the tone in Choices. Choices is a developer tool, like the browser's devtools, and Sam will never open it. Sam needs to choose on the card itself, so the card gets three buttons. Clicking one is Sam's decision.
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoose } from '@reintent/core';
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return `${p.age} laps around the sun, ${p.to}!`;
}
/** The card's tone: any component can read the same choice by its identity. */
export function useTone() {
return useChoose(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
});
}
export function Front(p: Person) {
const tone = useTone();
return (
<div
style={{
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
}}
>
<h1 style={{ color: LOOK[tone].accent }}>{title(p, tone)}</h1>
<p>Wishing you a wonderful day.</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
export function Buttons() {
const tone = useTone();
return (
<p>
{TONES.map((v) => (
<button key={v} disabled={v === tone} onClick={action.decide({ of: 'card', name: 'tone' }, v)}>
{v}
</button>
))}
</p>
);
}
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
return (
<div>
<Front {...p} />
<Buttons />
</div>
);
}
Starting…
Starting…
Nothing logged.
Cardnow draws the card's front,Front, with the buttons under it.useTone()reads the choice. It is an ordinary function, like a custom hook in React.FrontandButtonsboth call it and get the same value, because a choice is found by its identity, whichever component reads it.- A button's
onClickholdsaction.decide(…): what to do when it is clicked, as data. Components only describe what to draw. The click happens later, in the host, which carries out the action the frame offered. This is the Reduce path, the same loop as React'ssetState.
Click funny, then formal, then the undo button twice. Every decision is a commit, so undo and redo move through the history. It is the time travel from the end of React's tutorial, and you didn't have to write it.
Step 4: System One suggests a tone
Sam doesn't know which tone Grandma would like either. A model that knows about birthday cards can make a first guess, and Sam can still overrule it. Give the choice a question, and ask the model:
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoiceStatus } from '@reintent/core';
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return `${p.age} laps around the sun, ${p.to}!`;
}
/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
return useChoiceStatus(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model'],
});
}
export function useTone() {
return useToneStatus().value;
}
export function Front(p: Person) {
const tone = useTone();
return (
<div
style={{
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
}}
>
<h1 style={{ color: LOOK[tone].accent }}>{title(p, tone)}</h1>
<p>Wishing you a wonderful day.</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
export function Buttons() {
const tone = useToneStatus();
const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) => (
<p>
{values.map((v) => (
// A person's decision is pressed; a value the card took on its own is only marked.
<button
key={v}
disabled={now.source === 'decision' && v === now.value}
style={
now.source !== 'decision' && v === now.value ? { textDecoration: 'underline dotted' } : undefined
}
onClick={action.decide({ of: 'card', name }, v)}
>
{v}
</button>
))}
{/* The person's way of not deciding: forget the decision, and the card picks again. */}
<button
disabled={now.source !== 'decision'}
onClick={action.forget({ of: 'card', name })}
style={{
marginLeft: 12,
borderStyle: 'dashed',
fontStyle: 'italic',
...(now.source === 'decision' && { background: 'transparent' }),
}}
>
no preference
</button>
</p>
);
return <div>{row('tone', TONES, tone)}</div>;
}
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
return (
<div>
<Front {...p} />
<Buttons />
</div>
);
}
Starting…
Starting…
Nothing logged.
question: 'Which tone suits…?'withask: ['model']describes a request to System One, a fast model, to weigh the possibilities. The intent sends it to the model when the card is shown, and writes the answer into the choice's own layer, a belief.<ReintentProvider model={model}>inApp.tsxis where the requests go: every root under it asks that model. The model here is a recording:model.tsanswers fromrecordings.json, which holds real answers captured once ahead of time, for these prompts only. What happened lists each question and its answer.
System One leans warm, at 0.80. Now tell it about the evening: click “It is for a formal family dinner.” above the panel (intent.tell). It judges again, formal 0.60 to warm 0.30, and the card turns formal. Say “She loves a good joke.” instead, and with funny at 0.63 to 0.30 the card turns funny.
Now decide a tone with a button. Your decision sits above System One's belief, whatever System One thinks. Sam may change their mind and want System One to choose again, so each row ends with no preference, a button that holds action.forget(…). The decision goes away and the belief shows again.
The buttons also tell Sam's answer apart from one the card picked on its own. useToneStatus reads the same choice with useChoiceStatus, which also reports where the value came from (source). A tone Sam decided is pressed, a tone the card took on its own gets a dotted line, and when nothing is decided, no preference is pressed. Each writer keeps its own layer. From the top, they are a person's decision, System One's belief and the code's prior.
Step 5: A question only a funny card has
A funny card needs a joke, and there are two to pick from: a pun on her 90 laps around the sun, or a tease about her age. A warm card has no joke, so the question should only come up when the tone is funny:
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoiceStatus } from '@reintent/core';
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone, joke?: Joke) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return joke === 'tease'
? `${p.to}, you don’t look a day over ${p.age - 1}!`
: `${p.age} laps around the sun, ${p.to}!`;
}
/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
return useChoiceStatus(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model'],
});
}
export function useTone() {
return useToneStatus().value;
}
/** Which joke: a question only a funny card has. */
export function useJokeStatus(tone: Tone) {
return tone === 'funny'
? useChoiceStatus(choices<Joke>(...JOKES), { of: 'card', name: 'joke', defaultValue: 'pun' })
: undefined;
}
export function useJoke(tone: Tone) {
return useJokeStatus(tone)?.value;
}
export function Front(p: Person) {
const tone = useTone();
const joke = useJoke(tone);
return (
<div
style={{
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
}}
>
<h1 style={{ color: LOOK[tone].accent }}>{title(p, tone, joke)}</h1>
<p>Wishing you a wonderful day.</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
export function Buttons() {
const tone = useToneStatus();
const joke = useJokeStatus(tone.value);
const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) => (
<p>
{values.map((v) => (
// A person's decision is pressed; a value the card took on its own is only marked.
<button
key={v}
disabled={now.source === 'decision' && v === now.value}
style={
now.source !== 'decision' && v === now.value ? { textDecoration: 'underline dotted' } : undefined
}
onClick={action.decide({ of: 'card', name }, v)}
>
{v}
</button>
))}
{/* The person's way of not deciding: forget the decision, and the card picks again. */}
<button
disabled={now.source !== 'decision'}
onClick={action.forget({ of: 'card', name })}
style={{
marginLeft: 12,
borderStyle: 'dashed',
fontStyle: 'italic',
...(now.source === 'decision' && { background: 'transparent' }),
}}
>
no preference
</button>
</p>
);
return (
<div>
{row('tone', TONES, tone)}
{joke && row('joke', JOKES, joke)}
</div>
);
}
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
return (
<div>
<Front {...p} />
<Buttons />
</div>
);
}
Starting…
Starting…
Nothing logged.
useJoke(tone) reads the joke only when tone === 'funny'. Decide funny, and a second choice appears in Choices. Decide warm, and it disappears. Go back to funny, and the joke you picked is still there.
useChoose may sit under an if
React's hooks must run in the same order on every render. useChoose finds its choice by identity, wherever it is called, so you can read a choice inside an if, a loop or a helper. When one choice depends on another, the code reads the first and then asks the second.
Step 6: System Two writes the message
"Wishing you a wonderful day" stays the same whatever the tone. There are far too many possible messages to list, so ask System Two, a stronger model, to write one:
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoiceStatus, useGenerate } from '@reintent/core';
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone, joke?: Joke) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return joke === 'tease'
? `${p.to}, you don’t look a day over ${p.age - 1}!`
: `${p.age} laps around the sun, ${p.to}!`;
}
/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
return useChoiceStatus(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model'],
});
}
export function useTone() {
return useToneStatus().value;
}
/** Which joke: a question only a funny card has. */
export function useJokeStatus(tone: Tone) {
return tone === 'funny'
? useChoiceStatus(choices<Joke>(...JOKES), { of: 'card', name: 'joke', defaultValue: 'pun' })
: undefined;
}
export function useJoke(tone: Tone) {
return useJokeStatus(tone)?.value;
}
export function Front(p: Person) {
const tone = useTone();
const joke = useJoke(tone);
// The words nobody listed: System Two writes them, once for each set of inputs.
const message = useGenerate<string>({
of: 'card',
name: 'message',
instruction:
'Write the message inside a birthday card: two short sentences in this tone, no greeting and no signature.',
uses: { to: p.to, age: p.age, tone, joke },
});
return (
<div
style={{
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
}}
>
<h1 style={{ color: LOOK[tone].accent }}>{title(p, tone, joke)}</h1>
<p>{message.value ?? '…'}</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
export function Buttons() {
const tone = useToneStatus();
const joke = useJokeStatus(tone.value);
const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) => (
<p>
{values.map((v) => (
// A person's decision is pressed; a value the card took on its own is only marked.
<button
key={v}
disabled={now.source === 'decision' && v === now.value}
style={
now.source !== 'decision' && v === now.value ? { textDecoration: 'underline dotted' } : undefined
}
onClick={action.decide({ of: 'card', name }, v)}
>
{v}
</button>
))}
{/* The person's way of not deciding: forget the decision, and the card picks again. */}
<button
disabled={now.source !== 'decision'}
onClick={action.forget({ of: 'card', name })}
style={{
marginLeft: 12,
borderStyle: 'dashed',
fontStyle: 'italic',
...(now.source === 'decision' && { background: 'transparent' }),
}}
>
no preference
</button>
</p>
);
return (
<div>
{row('tone', TONES, tone)}
{joke && row('joke', JOKES, joke)}
</div>
);
}
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
return (
<div>
<Front {...p} />
<Buttons />
</div>
);
}
Starting…
Starting…
Nothing logged.
useGenerate asks for a value that isn't in any list. You describe it with an instruction and the inputs it depends on, uses, and the result is kept for exactly those inputs. Decide funny, and System Two is asked for a funny message. Go back to warm, and the warm one is still there, so System Two isn't asked again.
Until the answer comes, message.value is undefined and the card shows …. The card draws what it has so far, and is realized again once the answer arrives.
Step 7: Seeing the other tones
To pick a tone, Sam wants to see them side by side, and switching back and forth makes that hard. <World as> evaluates a subtree as if a choice had another value, without deciding anything:
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoiceStatus, useGenerate, World } from '@reintent/core';
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone, joke?: Joke) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return joke === 'tease'
? `${p.to}, you don’t look a day over ${p.age - 1}!`
: `${p.age} laps around the sun, ${p.to}!`;
}
/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
return useChoiceStatus(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model'],
});
}
export function useTone() {
return useToneStatus().value;
}
/** Which joke: a question only a funny card has. */
export function useJokeStatus(tone: Tone) {
return tone === 'funny'
? useChoiceStatus(choices<Joke>(...JOKES), { of: 'card', name: 'joke', defaultValue: 'pun' })
: undefined;
}
export function useJoke(tone: Tone) {
return useJokeStatus(tone)?.value;
}
export function Front(p: Person) {
const tone = useTone();
const joke = useJoke(tone);
// The words nobody listed: System Two writes them, once for each set of inputs.
const message = useGenerate<string>({
of: 'card',
name: 'message',
instruction:
'Write the message inside a birthday card: two short sentences in this tone, no greeting and no signature.',
uses: { to: p.to, age: p.age, tone, joke },
});
return (
<div
style={{
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
}}
>
<h1 style={{ color: LOOK[tone].accent }}>{title(p, tone, joke)}</h1>
<p>{message.value ?? '…'}</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
export function Buttons() {
const tone = useToneStatus();
const joke = useJokeStatus(tone.value);
const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) => (
<p>
{values.map((v) => (
// A person's decision is pressed; a value the card took on its own is only marked.
<button
key={v}
disabled={now.source === 'decision' && v === now.value}
style={
now.source !== 'decision' && v === now.value ? { textDecoration: 'underline dotted' } : undefined
}
onClick={action.decide({ of: 'card', name }, v)}
>
{v}
</button>
))}
{/* The person's way of not deciding: forget the decision, and the card picks again. */}
<button
disabled={now.source !== 'decision'}
onClick={action.forget({ of: 'card', name })}
style={{
marginLeft: 12,
borderStyle: 'dashed',
fontStyle: 'italic',
...(now.source === 'decision' && { background: 'transparent' }),
}}
>
no preference
</button>
</p>
);
return (
<div>
{row('tone', TONES, tone)}
{joke && row('joke', JOKES, joke)}
</div>
);
}
/** The card as each other tone would make it: previews write nothing and decide nothing. */
export function Others(p: Person) {
const tone = useTone();
return (
<div style={{ display: 'flex', gap: 8, fontSize: 11 }}>
{TONES.filter((t) => t !== tone).map((t) => (
<World key={t} as={{ 'card#tone': t }}>
<Front {...p} />
</World>
))}
</div>
);
}
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
return (
<div>
<Front {...p} />
<Buttons />
<Others {...p} />
</div>
);
}
Starting…
Starting…
Nothing logged.
Others draws the card once more for each of the other tones, each inside <World as={{ 'card#tone': t }}>. A hypothesis like this is part of and leaves the choices as they are. Each card under one even gets its own message, because it asks System Two with its own tone.
Step 8: When System One is not sure
In step 4, “It is for a formal family dinner.” turned the card formal on its own, at 0.60 to warm's 0.30. System One was far from sure, but by default a choice takes the likeliest possibility whenever it leads the next by 0.2. For a quick draft that is fine. This card will be read by the whole family at dinner, though, and when System One is unsure, Sam wants to pick the tone. The card needs a stricter rule for how sure is sure enough, and a way to ask Sam.
/** @jsxImportSource @reintent/dom */
import { action, choices, useChoiceStatus, useGenerate, World } from '@reintent/core';
/** Who the card is for, and from: the root's input, the props App.tsx gives it. */
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const JOKES = ['pun', 'tease'] as const;
export type Joke = (typeof JOKES)[number];
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone, joke?: Joke) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return joke === 'tease'
? `${p.to}, you don’t look a day over ${p.age - 1}!`
: `${p.age} laps around the sun, ${p.to}!`;
}
/** The card's tone, and which layer it came from: System One weighs it; a person's decision is above its judgment. */
export function useToneStatus() {
return useChoiceStatus(choices<Tone>(...TONES), {
of: 'card',
name: 'tone',
defaultValue: 'warm',
question: 'Which tone suits a birthday card for Grandma, turning 90?',
ask: ['model', 'human'],
});
}
export function useTone() {
return useToneStatus().value;
}
/** Which joke: a question only a funny card has. */
export function useJokeStatus(tone: Tone) {
return tone === 'funny'
? useChoiceStatus(choices<Joke>(...JOKES), {
of: 'card',
name: 'joke',
defaultValue: 'pun',
ask: ['human'],
})
: undefined;
}
export function useJoke(tone: Tone) {
return useJokeStatus(tone)?.value;
}
export function Front(p: Person) {
const tone = useTone();
const joke = useJoke(tone);
// The words nobody listed: System Two writes them, once for each set of inputs.
const message = useGenerate<string>({
of: 'card',
name: 'message',
instruction:
'Write the message inside a birthday card: two short sentences in this tone, no greeting and no signature.',
uses: { to: p.to, age: p.age, tone, joke },
});
return (
<div
style={{
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
}}
>
<h1 style={{ color: LOOK[tone].accent }}>{title(p, tone, joke)}</h1>
<p>{message.value ?? '…'}</p>
<p style={{ textAlign: 'right', fontStyle: 'italic' }}>— {p.from}</p>
</div>
);
}
export function Buttons() {
const tone = useToneStatus();
const joke = useJokeStatus(tone.value);
const row = <T extends string>(name: string, values: readonly T[], now: { value: T; source: string }) => (
<p>
{values.map((v) => (
// A person's decision is pressed; a value the card took on its own is only marked.
<button
key={v}
disabled={now.source === 'decision' && v === now.value}
style={
now.source !== 'decision' && v === now.value ? { textDecoration: 'underline dotted' } : undefined
}
onClick={action.decide({ of: 'card', name }, v)}
>
{v}
</button>
))}
{/* The person's way of not deciding: forget the decision, and the card picks again. */}
<button
disabled={now.source !== 'decision'}
onClick={action.forget({ of: 'card', name })}
style={{
marginLeft: 12,
borderStyle: 'dashed',
fontStyle: 'italic',
...(now.source === 'decision' && { background: 'transparent' }),
}}
>
no preference
</button>
</p>
);
// Left to a person and not answered yet: the card asks.
const asking = tone.status === 'unresolved';
return (
<div>
{asking && <p style={{ fontWeight: 600 }}>Not sure which tone suits her. Which would you like?</p>}
{row('tone', TONES, tone)}
{joke && row('joke', JOKES, joke)}
</div>
);
}
/** The card as each other tone would make it: previews write nothing and decide nothing. */
export function Others(p: Person) {
const tone = useTone();
return (
<div style={{ display: 'flex', gap: 8, fontSize: 11 }}>
{TONES.filter((t) => t !== tone).map((t) => (
<World key={t} as={{ 'card#tone': t }}>
<Front {...p} />
</World>
))}
</div>
);
}
/** The card Sam makes, for whoever the props say: a function of its input. */
export default function Card(p: Person) {
// Take System One's judgment only when it leads the next by 0.5; otherwise, ask.
return (
<World minMargin={0.5}>
<div>
<Front {...p} />
<Buttons />
<Others {...p} />
</div>
</World>
);
}
Starting…
Starting…
Nothing logged.
<World minMargin={0.5}>sets the rule for everything inside it: use System One's likeliest tone only if it leads the next by 0.5.Fronthasn't changed since step 7. The rule sits around it, inCard, so the same front can be strict here and relaxed in a draft. The other rules are in How sure is sure enough.ask: ['model', 'human']on the tone says who answers it, in order: System One first, then a person when System One is not sure enough. The card asks Sam itself.useToneStatus()also tells whether the tone is waiting for a person: itsstatusstays'unresolved'until someone answers. While it does,Buttonsshows the question above the tone buttons, and a click is the samedecideas before. In Choices, the tone is marked asks you.
At first warm leads 0.80 to 0.15. That is sure enough, so nobody is asked. Say “It is for a formal family dinner.”, and formal leads by only 0.30. The card stays on its default, warm, and asks above its buttons. Pick a tone, and the question goes away. “She loves a good joke.” asks too, since funny leads by 0.33.
Step 9: When no tone will do
The dinner is formal, the whole family will read the card, and the card Sam has in mind is neither warm, funny nor formal. Asking Sam to pick one of the three won't help. Say “It is for a formal family dinner.” again. System One is unsure, and the card asks above its buttons, as in step 8.
/** @jsxImportSource react */
import { ReintentProvider, reintentView } from '@reintent/react';
import Card from './Card.intent';
import Family from './Family.intent';
import { recordedModel } from './model';
// The card as a React component: its props are the root's input.
const GrandmaCard = reintentView(Card);
// System Two's reprogram names a program; the one it wrote is recorded in Family.intent.tsx.
const model = recordedModel({ family: Family });
export default function App() {
return (
<ReintentProvider model={model}>
<GrandmaCard to="Grandma" age={90} from="Sam" />
</ReintentProvider>
);
}
Starting…
Starting…
Nothing logged.
Sam's answer is a new idea: the whole family should sign the card. The program has no place for that, since it only knows warm, funny and formal. Click “Make it a card the whole family signs.” to ask System Two to reprogram the Intent. It sends back a proposal, made of a summary and a program. Open Family.intent.tsx to read the program it wrote (recorded once, like every answer on this page). It takes the same props, so the new program is still for Grandma, from Sam.
The card changes only when you click Accept, because a change to the intent always waits for a person. Then look at what carried over. The tone and joke you decided are still decided, because the new program reads the same choices by identity. The reprogramming also brings a new question, how many lines to leave for signatures, as a new choice. Undo takes the reprogramming back like any other commit.
Judge weighs the possibilities the program already has. Reprogram changes which possibilities there are.
So far you said the sentence, asked for the reprogramming and accepted it in the sandbox's panel, just as you decided in Choices back in step 2. Sam needs to do all of that in the card's own app.
Step 10: The card in Sam's app
Like Choices, the panel is a developer tool, and Sam will never open it. Sam's app has shown the card since step 1; now it takes the panel's place too, the way the buttons took the place of Choices in step 3. The app is the card's host. It draws , and it also handles what sits above the Intent, where a person talks to the whole program.
/** @jsxImportSource react */
import { useState } from 'react';
import type { ProposalView } from '@reintent/core';
import { ReintentProvider, reintentView, useReintent } from '@reintent/react';
import Card from './Card.intent';
import Family from './Family.intent';
import { recordedModel } from './model';
const REQUEST = 'Make it a card the whole family signs.';
const OCCASIONS = ['She loves a good joke.', 'It is for a formal family dinner.'];
// The card as a React component: its props are the root's input.
const GrandmaCard = reintentView(Card);
// System Two's reprogram names a program; the one it wrote is recorded in Family.intent.tsx.
const model = recordedModel({ family: Family });
/** When no tone will do, Sam can ask System Two for another card, and decides on what comes back. */
function Reprogram() {
const { reprogram, review } = useReintent();
// In what the card beside it draws: is the tone left unresolved?
const unresolved = useReintent((s) =>
s.reads.some((r) => r.key === 'card#tone' && r.status === 'unresolved'),
);
const [proposal, setProposal] = useState<ProposalView>();
if (proposal)
return (
<div style={{ padding: 12, border: '1px solid #c2410c', borderRadius: 8 }}>
<p>System Two suggests: {proposal.summary}</p>
<button onClick={() => (void review(proposal).accept(), setProposal(undefined))}>Accept</button>
<button onClick={() => (void review(proposal).reject(), setProposal(undefined))}>Reject</button>
</div>
);
// The tone is unresolved: none of warm, funny and formal is sure enough.
if (!unresolved) return null;
return (
<div style={{ padding: 12, border: '1px dashed #c2410c', borderRadius: 8 }}>
<p>None of the tones seems right for this card.</p>
<button onClick={() => reprogram({ prompt: REQUEST }).then(setProposal)}>{REQUEST}</button>
</div>
);
}
/** What Sam tells System One about the occasion: one sentence at a time, and a second click takes it back. */
function Occasion() {
const { commit } = useReintent();
const facts = useReintent((s) => s.layers.facts);
const say = (x: string) =>
commit(facts.includes(x) ? `“${x}” taken back` : `“${x}”`, (tx) => {
facts.forEach((y) => tx.untell(y));
if (!facts.includes(x)) tx.tell(x);
});
return (
<p>
About the occasion:{' '}
{OCCASIONS.map((x) => (
<button key={x} aria-pressed={facts.includes(x)} onClick={() => say(x)}>
{x}
</button>
))}
</p>
);
}
export default function App() {
return (
<ReintentProvider model={model}>
{/* Sam's controls are the card's children: they read and change its Intent with useReintent. */}
<GrandmaCard to="Grandma" age={90} from="Sam">
<Occasion />
<Reprogram />
</GrandmaCard>
</ReintentProvider>
);
}
Starting…
Starting…
Nothing logged.
Sam's controls go inside <GrandmaCard>, as its children, and Card places {p.children} above the card. The controls appear where the program places them, and their state stays React's. Both reach the card with useReintent(), and do three things:
- About the occasion tells System One a sentence (
commit, saying one sentence in place of the other). - When the tone is unresolved in the card (
useReintent((s) => s.reads…)), it offers Sam the request to System Two (reprogram). - It shows what System Two suggests, with Accept and Reject (
review).
In the app, click “It is for a formal family dinner.” The card asks about the tone, and the app offers “Make it a card the whole family signs.” Click it, read what System Two suggests, and accept. The card becomes one the whole family signs, and Sam did all of it in the app. The intent files decide what is left open and when to ask; App.tsx decides how it all looks.
Wrapping up
You built a card that:
- leaves a guess open as a choice (
useChoose), and keeps facts as code, - lets a person decide it, with buttons that hold
action.decide(Reduce), - asks System One for a first guess and a person when the guess is unsure (
ask,<World minMargin>), - asks a question only where it applies, and keeps the answer when it comes back,
- asks System Two for words nobody listed (
useGenerate), - shows the alternatives without choosing (
<World as>), - when the space itself is wrong, is reprogrammed by System Two, only after a person accepts (Reprogram),
- and runs in Sam's app:
reintentViewshows it with its props as the input, and the app's own controls in its children read and change it withuseReintent(@reintent/react).
A person can step in at every level: deciding a choice (action.decide), handing it back (action.forget), answering when the card asks, telling System One about the whole (tell), and accepting a new program (review).
More and more of what software does is decided by models. A model answers with probabilities, and software needs behaviour it can count on. The card shows how Reintent brings the two together. What is uncertain stays a choice, and code, models and people each answer it in their own layer. What runs is , which the same choices always realize the same way.
Those are the three paths of the loop, Reduce, Judge and Reprogram, and after each one the intent is realized again: . Understanding Reintent covers the ideas behind them: choices, intents and the loop. The chapter Making choices, from Who answers a choice on, goes into where each answer comes from and when one is good enough. The API reference has every detail.
Last updated on
Edit on GitHub