Versions in a link
Put what a person said into a link, open it again on another intent, and merge in what someone else said.
Sam's card is funny, with a tease. Sam sends it to a cousin, Mia, who opens it on her own phone. The link has to carry what Sam said, and Mia's changes make a new version that remembers it came from Sam's. Later Sam wants Mia's changes in their own card, without losing their own.
What a person said
The card, with its choices declared on an entity:
import { entity, host, useChoose } from '@reintent/core';
export const card = entity('card', {
tone: { kinds: ['warm', 'funny', 'formal'], default: 'warm' },
joke: { kinds: ['pun', 'tease'], default: 'pun' },
});
export const Card = card(function Card() {
const tone = useChoose(card.tone);
const joke = tone === 'funny' ? useChoose(card.joke) : undefined;
return host('card', { tone, joke });
});Sam decides funny, then tease, and tells the intent about the dinner:
import { snapshot } from '@reintent/lib/versions';
intent.decide(card.tone(), 'funny');
intent.decide(card.joke(), 'tease', { said: 'tease her about her cooking', via: 'code' });
intent.tell('It is for a family dinner.');
const said = snapshot(intent);
// → { decisions: { 'card#tone': { value: 'funny' }, 'card#joke': { value: 'tease', said: …, via: 'code' } },
// constraints: {}, preferences: {}, named: {}, facts: [ 'It is for a family dinner.' ] }snapshot(intent) reads the person's part of intent.layers as a Snapshot: decisions with the sentence they came from, constraints, preferences, what was said of every choice with a name, and facts. It is plain JSON. System One's beliefs and what System Two wrote are not in it. The models are asked again for them where the card is opened.
restore(intent, said, note) writes a snapshot back as one commit. What the snapshot does not hold is taken back, and what it holds is said again, decisions last. One undo takes the whole version back.
A version in a link
A version is the host's data with who made it:
import { child, encode, type Saved, type Snapshot } from '@reintent/lib/versions';
const saved: Saved<Snapshot> = {
version: child(null, 'Sam', 'funny, with a tease'),
data: snapshot(intent),
};
const link = `https://cards.example/grandma#v=${await encode(saved)}`;A Version has an id, the parent it was made from (null for the first), who made it (by) and an optional note. child(parent, by, note) makes one with a new id from newId(), six letters and digits. Saved<T> pairs it with data, whatever the host keeps: here a snapshot, and it could hold the program's text or a prompt too.
encode(saved) turns it into a link's fragment: the JSON, compressed, in base64url. Mia opens the link:
import { decode, inHash, restore } from '@reintent/lib/versions';
const v = inHash(location.hash);
const opened = v ? await decode<Snapshot>(v) : null;
if (opened) restore(intent, opened.data, `opened ${opened.version.by}'s card`);inHash(hash) finds #v=… or &v=… in an address or fragment. decode(s, check) reads the version back, or gives null when the text is not one; check says whether its data is the kind the host expects. Mia's card is funny with a tease. When she changes it and shares it again, child(opened.version, 'Mia') makes the new version, with Sam's as its parent.
Undo and redo stay the intent's history. A version is what is shared or saved.
Merging another version
Mia added a constraint, no puns, and decided the tone formal. Sam has their own card open, funny, and wants her changes:
import { merge } from '@reintent/lib/versions';
const { conflicts, notHandled } = merge(intent, mias.data, { note: 'Mia’s version' });
// conflicts → [ { key: 'card#tone', id: …, mine: { value: 'funny' }, theirs: { value: 'formal' } } ]
// notHandled → [ 'constraint card#tone' ]merge(intent, theirs, opts) brings another snapshot's decisions in by identity, as one commit: theirs go where this intent has none. That is its whole scope today. It returns a Merged: Mia's constraint, her preferences and the facts she told are listed in notHandled, for the host to show, and none of them is written. A decision she took back cannot be seen, since a snapshot has no trace of it, and a change of the program is the host's to merge. A choice both decided differently is not written. It comes back as a Conflict, with the key, the id, mine and theirs, each with its origin. The host asks Sam, and writes what Sam picks:
for (const c of conflicts) if (await askSam(c)) intent.decide(c.id, c.theirs.value);only keeps the merge to some choices, for example the entities Mia worked on. note names the commit, and one undo takes the merge back.
Recap
snapshot(intent)reads what a person said as JSON;restorewrites it back in one commit.- A
Versionhas an id fromnewId, a parent and who made it;Savedpairs it with the host's data. encodeputs a version into a link's fragment;inHashfinds it anddecodereads it back.mergebrings in another person's decisions where this intent has none; a choice both decided differently comes back as aConflict, and what it does not merge (constraints, preferences, facts) is listed innotHandled.
Last updated on
Edit on GitHub