@reintent/lib/program
A program a model wrote, as text, loaded safely (#104, docs/design/0026 §4.5): System Two answers a reprogramming with the source of an `.intent.tsx` file, and the host turns it into a program here, then into a proposal a person accepts.
The text may import only what writing an intent uses from @reintent/core (intentNames: no createIntent, no recordings, no host or catalog checks; a host widens it with core) and the modules the host passes (its intent components); it is compiled (TypeScript and JSX, by Sucrase, in the browser and in Node) and evaluated with those modules and nothing else. Its default export is the program: an element, or a function root (docs/design/0045 §3), which the intent calls with its input. A text that says its JSX is a catalog's (/** @jsxImportSource @app/catalog *\/, 0043 §4) names one the host gave (catalogs), and tryProgram checks what it realizes against that catalog.
Before a host offers it, tryProgram realizes it once under the intent's current layers, input and contexts, sending nothing: a program that throws there, or whose λ the catalog refuses, is refused with every reason (the intent tries it on its input again when it is accepted).
Refused, before anything runs, with every reason at once:
imports from anything but `@reintent/core` and the host's modules; a name the module
does not export (`import { Lamp } from './room.intent'` where it has none);
`import()`, `import.meta`, `require`
names any name the text neither declares nor imports, but a few pure globals (`Math`,
`JSON`, `Number`, `String`, `Boolean`, `Array`, `Object`, `Map`, `Set`, `Error`,
`NaN`, `Infinity`, `undefined`, `isNaN`, `isFinite`, `parseInt`, `parseFloat`):
so no `fetch`, `window`, `document`, `globalThis`, `process`, `eval`, `Function`,
`Date`, timers or storage
impurity `Math.random`
escapes `constructor`, `prototype`, `__proto__` and the like, as a name or a stringWhat runs is evaluated with the browser's and Node's globals shadowed as well. This is a boundary for what a model writes by mistake or by overreach, not a sandbox against an adversary. An allow-list of imports is not a guarantee of purity: an allowed module's own functions run with whatever they can reach, and a text can loop forever. A Worker is not a permission sandbox either: it has the network and its own timers. A host that must isolate hostile code runs the loader where the platform enforces it (a sandboxed, cross-origin frame with a CSP; a separate process).
New. The token checks are on Sucrase's parser (sucrase 3.35.1, the version the site's sandboxes compile with).
Type Aliases
LoadedProgram
type LoadedProgram = object;Defined in: packages/lib/src/program.ts
What loading gives: the program (an element, or a function root the intent calls with its input), everything the text exports, the function root when it is one, and the catalog its JSX is written for.
Properties
| Property | Type |
|---|---|
catalog? | HostCatalog |
exports | Readonly<Record<string, unknown>> |
program | RootProgram |
root? | Component<any> |
ProgramModules
type ProgramModules = Readonly<Record<string, object>>;Defined in: packages/lib/src/program.ts
The modules a program's text may import, by specifier: each a module's exports.
ProgramOptions
type ProgramOptions = object;Defined in: packages/lib/src/program.ts
How a program's text is loaded.
Properties
| Property | Type | Description |
|---|---|---|
catalogs? | Readonly<Record<string, HostCatalog>> | The catalogs a text may write its JSX for, by the source its pragma names (/** @jsxImportSource @app/catalog */, 0043 §4). A text naming another is refused. |
core? | readonly string[] | The names of @reintent/core the text may import (default intentNames). |
modules? | ProgramModules | The host's modules the text may import, by the specifier it writes ('./dream.intent'), each the module's exports (import * as Dream from './dream.intent.js'). @reintent/core is always there, with the names in core. |
Tried
type Tried = object;Defined in: packages/lib/src/program.ts
What trying a program gave: whether it can be accepted, why not, and what it realized.
Properties
| Property | Type |
|---|---|
errors | string[] |
ok | boolean |
realization? | Realization |
Variables
intentNames
const intentNames: readonly string[];Defined in: packages/lib/src/program.ts
What writing an intent uses from @reintent/core, the names a program's text may import from it
by default: elements and contexts, choices, the hooks, the handlers and their strategies,
entities, and host actions. Not what runs an intent (createIntent, recorded), nor a host's
encoding and checks.
Functions
checkProgram()
function checkProgram(source, opts?): string[];Defined in: packages/lib/src/program.ts
What would keep a program's text from loading, without running it: an empty list when nothing
does. The same checks loadProgram makes first.
Parameters
| Parameter | Type |
|---|---|
source | string |
opts | ProgramOptions |
Returns
string[]
Example
import { checkProgram } from '@reintent/lib/program';
const text = `
import { host } from '@reintent/core';
import { secret } from 'node:fs';
export default host('note', { text: await fetch('https://example.com') });
`;
console.log(checkProgram(text));
// → [ "imports 'node:fs', which a program cannot", 'uses fetch, which a program cannot reach' ]loadProgram()
function loadProgram(source, opts?): LoadedProgram;Defined in: packages/lib/src/program.ts
Loads a program's text: checked (checkProgram), compiled, evaluated with @reintent/core and
the host's modules as the only modules there are, and its default export taken as the program.
Anything refused, a syntax error, an error while it runs, or a default export that is not an
intent element, throws ProgramRefused with every reason. A host makes the proposal from it:
{ program: loadProgram(text, { modules }).program, summary }.
Parameters
| Parameter | Type |
|---|---|
source | string |
opts | ProgramOptions |
Returns
Example
import { createIntent, type HostNode } from '@reintent/core';
import { loadProgram } from '@reintent/lib/program';
// What System Two wrote: a program of the host's components, and an entity of its own.
const text = `
import { entity, host, useChoose } from '@reintent/core';
import { Room } from './room.intent';
const lamp = entity('lamp', { light: { kinds: ['warm', 'cold'] } });
const Lamp = lamp(() => host('lamp', { light: useChoose(lamp.light) }));
export default <Room><Lamp id="bedside" /></Room>;
`;
// The host's intent components, the only module besides the core the text may import.
const room = { Room: (p: { children?: unknown }) => p.children };
const { program } = loadProgram(text, { modules: { './room.intent': room } });
const intent = createIntent({ program });
console.log((intent.realize().tree as HostNode).props); // → { light: 'warm' }recordedReprogram()
function recordedReprogram(recordings, toDraft): (request) =>
| ProposalDraft
| Promise<ProposalDraft>;Defined in: packages/lib/src/program.ts
A model's reprogram answered from recordings whose answers are data (a program's text, an edit):
looked up as recorded looks a reprogramming up (by its prompt, or the unresolved choice's key),
and made a proposal by toDraft. Missing, it throws MissingRecording. A host composes it with
recorded(…)'s judge and generate.
Parameters
| Parameter | Type |
|---|---|
recordings | readonly Recording[] |
toDraft | (answer, request) => | ProposalDraft | Promise<ProposalDraft> |
Returns
(request) =>
| ProposalDraft
| Promise<ProposalDraft>
Example
import { createIntent, host, recorded, type HostNode } from '@reintent/core';
import { loadProgram, recordedReprogram } from '@reintent/lib/program';
const room = { Room: (p: { lamps: number }) => host('room', { lamps: p.lamps }) };
const recordings = [
{
effect: 'reprogram' as const,
id: 'a second lamp',
answer: {
text: `import { Room } from './room.intent';\nexport default <Room lamps={2} />;`,
summary: 'two lamps',
},
},
];
const intent = createIntent({
program: <room.Room lamps={1} />,
model: {
...recorded(recordings),
reprogram: recordedReprogram(recordings, (answer) => {
const a = answer as { text: string; summary: string };
const { program } = loadProgram(a.text, { modules: { './room.intent': room } });
return { program, summary: a.summary };
}),
},
});
const p = await intent.reprogram({ prompt: 'a second lamp' });
intent.review(p).accept();
console.log((intent.realize().tree as HostNode).props); // → { lamps: 2 }tryProgram()
function tryProgram(program, opts?): Tried;Defined in: packages/lib/src/program.ts
A program tried before a host offers it (0045 §3.1): realized once, on an intent of its own,
under the layers, input and contexts the intent has now (layers, input, provide), sending
nothing. Refused when it throws there, or when its λ is not one the catalog takes
(checkFrame): every reason.
Parameters
| Parameter | Type |
|---|---|
program | RootProgram |
opts | { catalog?: HostCatalog; input?: unknown; layers?: Partial<Layers>; provide?: Provide; realizer?: Realizer; } |
opts.catalog? | HostCatalog |
opts.input? | unknown |
opts.layers? | Partial<Layers> |
opts.provide? | Provide |
opts.realizer? | Realizer |
Returns
Example
import { createIntent, host } from '@reintent/core';
import { loadProgram, tryProgram } from '@reintent/lib/program';
const room = { Room: (p: { lamps: number }) => host('room', { lamps: p.lamps }) };
const intent = createIntent({ program: <room.Room lamps={1} /> });
// A function root, tried on the input the intent has now.
const loaded = loadProgram(
`import { Room } from './room.intent';
export default function Bedroom(p: { lamps: number }) {
if (p.lamps > 2) throw new Error('no room for ' + p.lamps + ' lamps');
return <Room lamps={p.lamps} />;
}`,
{ modules: { './room.intent': room } },
);
console.log(
tryProgram(loaded.program, { layers: intent.layers, input: { lamps: 3 } }).errors,
);
// → [ 'fails while it is realized: no room for 3 lamps' ]References
ProgramRefused
Re-exports ProgramRefused
Last updated on
Edit on GitHub