Reading and surfaces
Generative UI
A product's model can write surface operations one line at a time into a surface. The surface checks each line as it arrives, applies it or refuses it with a reason, and gives back a patch for the mount. Culm calls no model: the product calls its model and passes the text on.
This page is the behaviour reference for the stream, for describeSurface, and for the MCP Apps view. Ceilings and measured figures are in limits.md. Setup is in the package README.
The stream
surface.stream() returns a writer, { write(chunk), close() }. It runs wherever createSurface runs, in Node and in client mode.
const stream = surface.stream()
for await (const chunk of modelText) {
const step = stream.write(chunk) // { lines, patches, stopped? }
queue.push(...step.patches)
for (const l of step.lines) if (!l.ok) report(l.line, l.problems)
}
const last = stream.close() // also { applied, refused }Line rules
write takes text. A chunk may end anywhere, even inside a line: the writer keeps the part after the last newline until the next chunk. close takes that last part as a line.
A line ends at \n. A \r just before it is dropped.
A blank or whitespace-only line is skipped. It is not counted and changes nothing.
Every other line must be one op written as a JSON object: one of the nine operations in surfaces.md. Each line is run through apply on its own, as a batch of one op.
tree may be left out. A line with no tree is for the home page ({ "page": "home" }), the only page a surface has.
A line may be at most 1,000,000 characters long (core's request cap, MAX_REQUEST_CHARS, counted as JavaScript string length). A longer line is refused once, the rest of it up to its newline is dropped, and the next line is read as usual.
A stream counts at most 500 lines. Refused lines count; skipped blank lines do not.
write with anything but a string (bytes, a list) throws a TypeError. Decode bytes first, for example with TextDecoder. This is a mistake in the product's code, not model output, so it is not a refusal.
What a step holds
Each write returns a step: { lines, patches, stopped? }.
lines has one entry per counted line that ended in this chunk: { line, ok: true, op }, or { line, ok: false, problems }. line is the line's number in the stream, from 1.
patches has one patch per applied line, in order. Pass each to view.patch.
stopped is set on the step that reached the limit and on every step after it, and after close. Nothing more is applied.
close() returns the last step plus applied and refused, the counts for the whole stream.
Refusals
A refused line changes nothing. Every other line goes on: one bad line does not stop the stream.
Case
Code
path
Case
The line is not JSON, or not a JSON object
Code
INVALID_OPERATION
path
line
Case
The line is longer than 1,000,000 characters
Code
RESOURCE_LIMIT_EXCEEDED
path
line
Case
The op is refused by apply
Code
the code apply gives
path
as given
Case
The 501st counted line (stopped; no line entry)
Code
RESOURCE_LIMIT_EXCEEDED
path
stream
Case
A write after close (stopped)
Code
INVALID_OPERATION
path
stream
A problem from apply is the same as for a batch: the codes and rules in surfaces.md, with nodeId where a node is at fault and path where core gives one. Some core problems carry no path; the stream does not invent one. Where a problem carries op, it is 0, because each line is a batch of one: use line.
A node whose add was refused is not in the surface. Every later line that names it as a parent or target is refused too, and every later index in the same parent is one too high. The model has to send a form with an action together with its submit button, and a list together with its row, in one add. describeSurface tells the model these rules.
The surface's element and depth ceilings hold per line: the line that would pass either is refused with RESOURCE_LIMIT_EXCEEDED, and the lines before and after it stand.
The guarantee
Every applied line leaves a valid surface, so a refused op never reaches the screen. The CUJ 10 browser test checks this for each recorded stream after every frame (
packages/surface/__browser__/cuj10.spec.ts).
The same lines written into surfaces made with the same id give the same steps and the same patches.
The outline spelling
surface.stream({ spelling: 'outline' }) reads an indented outline, one line per node, and turns it into the same nine operations, run through the same checks as JSON lines. The default, { spelling: 'core' } or no option, is the JSON lines above, unchanged. The outline is in the Node entry only: client.js leaves the decoder out, and its stream({ spelling: 'outline' }) throws a TypeError.
Use it when the model's output tokens are the cost that matters. The 102 recorded good outputs of the Stage 9 comparison take 106,659 characters as JSON lines and 33,168 as outlines, the same ops. Keep JSON lines when the product writes or stores ops as JSON, when each line must apply the moment it ends, or in client mode.
# Contact us
We answer within two days.
form.f !contact="Send a message"
field key=email name=email input=email required Email
button Send
column.card | color=brand
## Team
button link={"url":"/pricing"} See plans
=n2 Write to usGrammar
A node line is kind.id settings... Text. Its children are the lines below it, one space deeper per level.
.id is optional. A node without one is n<k>: k is the highest n<digits> id already on the page when the stream starts (0 on an empty page), plus every node and block line the stream has read before it, whether or not the surface accepted it, plus one. So a second stream on the same surface, such as a repair turn, or one after apply added n7, goes on after the ids already there. A node or block line that gives an id of n and digits itself is refused, since it would collide.
A setting is key=value, the value a bare word, a "quoted" text or JSON, or a bare key for true. h1 to h6 on a text node is its level. $key=json sets a node field other than props, such as repeat or action.
The text is last on the line and fills the kind's main setting: a text node's words, a button's or field's label, a details summary, an image's alt, an icon's name. It is bare when it starts with a letter from A to Z, a digit or $ (but not $key=), and quoted otherwise (so "Über uns").
# Title to ###### Title is a heading. A line that is only text, bare or quoted, is a text node.
On a node line, !name="description" gives the node an action and | key=value ... at the end gives it a base style; both are sent as their own ops after the node's add. ^parent or ^parent:index on a top-level line puts it somewhere other than the end of the page.
Other lines: ~id key=value (setProps), =id Text (setText), *id [@layer] key=value (setStyle), >id parent index (move), -id (remove), &block.id look [^parent[:index]] (insertBlock), %id key=value (setBlockSetting), !id name="description" (setAction).
A line that starts with { is one op as JSON and is passed on as written.
Where an add or a block has no index, it goes after the parent's children as the surface holds them now, so a refused add does not shift the lines after it.
One line late
A top-level line and the lines below it are one section, sent as one add when the next top-level line, op line, unreadable line or close arrives. A section is therefore applied one line after its last line, and the last section at close. Op lines are applied as they end.
Results and refusals
line is the model's line, counted as for JSON lines: from 1, blank lines not counted.
A section's result names its first line. The ops from ! and | name the line of their node. A problem that names a node on another line of the section ends with (line N).
A line the decoder cannot read is passed to the surface as written, so it is refused with the surface's own code, usually INVALID_OPERATION, path line. So are prose and code fences, except a line that starts with A to Z, a digit or $: by the grammar that is a text node, so a capitalised prose line such as Here is the page: lands on the page. The outline description tells the model to write no prose and no code fences for this reason. An unreadable line inside a section ends the section there: the part above it is sent as it is, and every later line of the section, below it or beside it, is refused one by one, each counting toward the 500.
An id of n and digits written by the model: INVALID_OPERATION, path line.
Nothing the model writes makes the stream throw. A non-string write throws a TypeError, as for JSON lines.
Ceilings
The ceilings are the same numbers. The 500 counts the ops sent to the surface, applied or refused, so a section with a style and an action counts three. The 1,000,000-character cap holds for each line the model writes and for each op line sent; a section whose lines pass 1,000,000 characters in all is refused whole with RESOURCE_LIMIT_EXCEEDED, path line, and its lines are not held. The element and depth ceilings hold per section.
Description
describeSurface(registry, profile, { spelling: 'outline' }) returns the text for this spelling: the grammar with example sections, the order and writing rules adapted to sections, and the same generated lists of kinds, settings, blocks and ceilings. Each example section is tried on a throwaway surface and printed only if it is accepted. The text has its own version, 1; the default text and DESCRIBE_VERSION do not change. A product that caches the text should key it on the spelling and the version together.
Drawing the patches: one frame
view.patch is synchronous: it applies one patch at once. To draw many patches in one frame, queue them and drain the queue in one requestAnimationFrame callback. The README has the code.
Every view.patch in the drain should return true. A false means the mount missed a patch, and every later patch of the stream returns false too. Keep draining, and ask once for surface.snapshot(): a snapshot is a full state (from: 0), so the mount applies it when it arrives behind those patches.
Browsers pause requestAnimationFrame in hidden tabs and hidden frames. A view in a tab the person is not looking at queues its patches until it is shown, then draws them in one frame. The queue grows by one patch per applied line, at most 500 per stream.
Replay and restore
The 500-line ceiling comes with a time budget: replaying a 500-op stream (Node apply for each line, plus the mount patching all 500) should take at most 5,000 ms of compute on the reference machine. This is a compute budget, not a limit on how long a model may take to write. The measured times are in limits.md.
To restore a surface, do not replay its stream. Use surface.snapshot() for a new mount, or surface.state() to rebuild the surface (see the README).
describeSurface
describeSurface(registry, profile) (from @culmjs/surface, Node entry only; it is not in client.js) returns the text a product gives its model so that the model writes valid lines for a surface with that profile: { ok: true, text, version }, or { ok: false, problems }.
The text holds, in order:
Culm surface description, version 1. (DESCRIBE_VERSION, raised whenever the structure of the text changes).
The grammar: one op per line, a JSON object, one of the nine, with one example line for each op. No example has tree, and the text does not mention it. Where the profile has nothing an op could name, the text says so in place of an example.
The id rule: 1 to 20 lower-case letters or digits.
The order rules (ORDER_RULES, exported), as above.
The profile's kinds with their props, and its blocks with their looks and settings, from core's catalogue.
The ceilings: the profile's elements and depth, and 500 ops in one stream.
Example lines for blocks and settings are chosen by trying them on a fresh surface with the profile, so each example is one the surface accepts.
The text is at most 6,000 characters. Past that, describeSurface returns RESOURCE_LIMIT_EXCEEDED, path profile, with measured and limit: list fewer kinds or blocks.
describeSurface does not check the profile itself: it returns text for a profile that createSurface would refuse.
Previous
Next