@reintent/lib/written
Results a model wrote, kept with what they were written under: a layout for a screen, a drawing of a thing.
Each is shown only while every value it was written under still holds and the host's check passes; none is thrown away, so one comes back when its values do. When nothing written holds any more, a new one is due: System Two is asked for one written under the values now (a generate request), and meanwhile the host shows its own (the solver's layout, a placeholder).
choose(written, now, { check, order }) which one to show, and whether a new one is due
createWritten({ limit }) results kept by a key (a slide on a screen), newest first
stale(under, now) why one does not hold (values moved, or not known)
useWritten({ of, name, …, written }) the same in an intent program, asking when dueA value it was written under that is not known now (the choice is not read here any more) is not one that holds: the result is not shown, unless the host says that value does not matter here (irrelevant).
Who wrote a result orders the candidates (by): by default a Human's own above System Two's repair, above System One's approval of Code's result, above one Code carried from elsewhere; among the same writer, the order given. That order is the application's priority (order), not Reintent's layering of a choice (a person's decision, constraints, beliefs, the prior).
What System Two generates when nothing holds is core's generated result: kept by the choice's identity and the full input stamp (useGenerate), and found again by them, not chosen here.
Type Aliases
Chosen
type Chosen<V> = object;Defined in: packages/lib/src/written.ts
What choose found: the result to show, whether a new one is due, and why the others are not shown.
Type Parameters
| Type Parameter |
|---|
V |
Properties
| Property | Type | Description |
|---|---|---|
due | boolean | Nothing holds, and something had been written: a new one is due. |
passed | object[] | Why each of the others is not shown: the values that moved, what the check found. |
shown? | Written<V> | The result to show, if any holds. |
Written
type Written<V> = object;Defined in: packages/lib/src/written.ts
A result as it was written: the value, what it was written under (a choice's key → its value), who wrote it.
Type Parameters
| Type Parameter |
|---|
V |
Properties
| Property | Type | Description |
|---|---|---|
by | string | Who wrote it: one of BY ('human', 'system-two', 'system-one', 'code'), or a writer of the host's order. |
under | Readonly<Record<string, unknown>> | - |
value | V | - |
WrittenRead
type WrittenRead<V> = object;Defined in: packages/lib/src/written.ts
What useWritten gives: the result shown and who wrote it, or that a new one is asked for.
Type Parameters
| Type Parameter |
|---|
V |
Properties
Variables
BY
const BY: readonly string[];Defined in: packages/lib/src/written.ts
Who wrote a result, most trusted first, by default: a Human ('human'), System Two writing it
('system-two'), System One passing Code's result ('system-one'), Code carrying one from
elsewhere ('code'). An application's priority, not Reintent's layering: pass order for another.
irrelevant
const irrelevant: unique symbol;Defined in: packages/lib/src/written.ts
What a host says of a value in now that does not matter here: a result written under any value of it holds.
Functions
choose()
function choose<V>(
written,
now,
opts?
): Chosen<V>;Defined in: packages/lib/src/written.ts
Which written result to show under the values now: the first, by writer (order, default BY)
and then as given, whose values hold and that the host's check passes.
Type Parameters
| Type Parameter |
|---|
V |
Parameters
| Parameter | Type |
|---|---|
written | readonly Written<V>[] |
now | Readonly<Record<string, unknown>> |
opts | { check?: (value) => string[]; order?: readonly string[]; } |
opts.check? | (value) => string[] |
opts.order? | readonly string[] |
Returns
Chosen<V>
Example
import { choose, type Written } from '@reintent/lib/written';
const written: Written<string>[] = [
{
value: 'layout written while the photo kept its shape',
under: { 'photo#fit': 'keep' },
by: 'system-two',
},
{ value: 'layout written before anyone said', under: {}, by: 'system-two' },
];
console.log(choose(written, { 'photo#fit': 'keep' }).shown?.value);
// → 'layout written while the photo kept its shape'
console.log(choose(written, { 'photo#fit': 'fill' }).shown?.value);
// → 'layout written before anyone said'
// The host's check: the earlier one crops the photo, which a person now wants kept.
const crops = (v: string) => (v.includes('before') ? ['crops the photo'] : []);
const none = choose(written, { 'photo#fit': 'fill' }, { check: crops });
console.log(none.shown, none.due); // → undefined truecreateWritten()
function createWritten<V>(opts?): object;Defined in: packages/lib/src/written.ts
Results written for a host, kept by a key (a slide on a screen, a thing): newest first, for
choose. Not history: undo leaves them, and one comes back when its values do. With limit, the
oldest beyond it is let go.
Type Parameters
| Type Parameter |
|---|
V |
Parameters
| Parameter | Type |
|---|---|
opts | { limit?: number; } |
opts.limit? | number |
Returns
| Name | Type | Description |
|---|---|---|
add() | (key, written) => void | A result written for key, the newest. |
get() | (key) => readonly Written<V>[] | The results written for key, newest first (none: []). |
Example
import { choose, createWritten } from '@reintent/lib/written';
const kept = createWritten<string>({ limit: 2 });
kept.add('gallery@phone', {
value: 'two columns',
under: { 'g#arrange': 'cols2' },
by: 'human',
});
kept.add('gallery@phone', {
value: 'one column',
under: { 'g#arrange': 'cols1' },
by: 'human',
});
console.log(kept.get('gallery@phone').map((w) => w.value)); // → [ 'one column', 'two columns' ]
console.log(choose(kept.get('gallery@phone'), { 'g#arrange': 'cols2' }).shown?.value); // → 'two columns'
kept.add('gallery@phone', {
value: 'a grid',
under: { 'g#arrange': 'grid' },
by: 'human',
});
console.log(kept.get('gallery@phone').length, kept.get('gallery@desk')); // → 2 []stale()
function stale(under, now): string[];Defined in: packages/lib/src/written.ts
The keys of under that do not hold now: whose value is something else, or is not known (a
choice not read here any more: now has no value for it). A key the host marks irrelevant
holds whatever it was. Empty: it holds.
Parameters
| Parameter | Type |
|---|---|
under | Readonly<Record<string, unknown>> |
now | Readonly<Record<string, unknown>> |
Returns
string[]
Example
import { irrelevant, stale } from '@reintent/lib/written';
const under = { 'g0#fit': 'keep', 'g1#fit': 'keep' };
console.log(stale(under, { 'g0#fit': 'keep', 'g1#fit': 'keep' })); // → []
console.log(stale(under, { 'g0#fit': 'fill', 'g1#fit': 'keep' })); // → [ 'g0#fit' ]
// g1 is not read any more: unknown is not "holds"…
console.log(stale(under, { 'g0#fit': 'keep' })); // → [ 'g1#fit' ]
// …unless the host says it does not matter here.
console.log(stale(under, { 'g0#fit': 'keep', 'g1#fit': irrelevant })); // → []useWritten()
function useWritten<V>(opts): WrittenRead<V>;Defined in: packages/lib/src/written.ts
In an intent program: the written result to show for { of, name } under uses (the values
it depends on now), chosen as choose does. When none holds and something had been written,
System Two is asked for one written under uses (useGenerate: kept by the choice's identity
and the full input stamp, sent only where the evaluation is shown); its answer, once there, is
shown as System Two's. With nothing ever written, none: the host shows its own.
Type Parameters
| Type Parameter |
|---|
V |
Parameters
| Parameter | Type |
|---|---|
opts | { check?: (value) => string[]; instruction: string; name: string; of: string; order?: readonly string[]; uses: Record<string, unknown>; written: readonly Written<V>[]; } |
opts.check? | (value) => string[] |
opts.instruction | string |
opts.name | string |
opts.of | string |
opts.order? | readonly string[] |
opts.uses | Record<string, unknown> |
opts.written | readonly Written<V>[] |
Returns
WrittenRead<V>
Example
import { createIntent, entity, host, useChoose, type HostNode } from '@reintent/core';
import { useWritten, type Written } from '@reintent/lib/written';
const hat = entity('hat', { size: { kinds: ['small', 'big'], default: 'small' } });
const DRAWN: Written<string>[] = [
{ value: 'a hat, drawn small', under: { 'hat#size': 'small' }, by: 'system-two' },
];
const Hat = hat(function Hat() {
const size = useChoose(hat.size);
const drawing = useWritten<string>({
of: 'hat',
name: 'drawing',
instruction: 'Draw the hat at this size',
uses: { 'hat#size': size },
written: DRAWN,
});
return host('hat', { drawing: drawing.value ?? '(drawing…)', status: drawing.status });
});
const intent = createIntent({
program: <Hat />,
model: { generate: () => 'a hat, drawn big' },
});
const connection = intent.connect({ commit() {} }); // shown: what it asks for is sent
const shown = () => (intent.realize().tree as HostNode).props;
console.log(shown().drawing); // → 'a hat, drawn small'
intent.decide(hat.size('hat'), 'big');
console.log(shown().drawing, shown().status); // → 'a hat, drawn big' 'ready'
intent.history.undo();
console.log(shown().drawing); // → 'a hat, drawn small'
connection.disconnect();Last updated on
Edit on GitHub