Patch, don't regenerate

The user says "make the chart stacked and drop the note". The model could write the whole dashboard again. In edit mode it writes two lines instead, and the app folds them into the program that is already on screen.

By the end of this lesson you can

  • turn on edit mode and say what it changes in the system prompt;
  • predict what mergeStatements produces: replace by name, append new names, delete with name = null, drop what root no longer reaches;
  • explain why the merge happens after the stream ends, never during it;
  • decide when a patch beats a fresh program, and when it does not.

The model sends a patch, the app merges it

An OpenUI Lang program is a list of named statements. That makes it easy to edit by name: a statement called chart in the new response replaces the statement called chart in the old one. So a change request does not need the whole program. It needs the statements that change, plus any new ones.

Two pieces make this work. On the prompt side, editMode: true in PromptOptions adds an ## Edit Mode section that tells the model to output only changed or new statements. On the app side, mergeStatements(previous, patch) from @openuidev/lang-core (re-exported by @openuidev/react-lang) applies the patch and returns the new program as a string. Its full signature is mergeStatements(existing, patch, rootId = "root"): unreachable statements are dropped only when walking from the statement with that name, so pass your entry name as the third argument if it is not root, or nothing is dropped. The animation walks through the merge from the course's runnable lab.

Before, patch, after

Previous program (290 chars) root = Stack([title, chart, notes]) title = TextContent("Revenue…") chart = BarChart(…, "grouped") regions = ["North America", …] rev = Series("Revenue ($k)", …) notes = Callout("info", …) Patch from the model (72 chars) root = Stack([title, chart]) chart = BarChart(…, "stacked") Merged program root = Stack([title, chart]) chart = BarChart(…, "stacked") same name: replaced in place title = TextContent("Revenue…") regions = ["North America", …] rev = Series("Revenue ($k)", …) notes = Callout("info", …) not in the patch: kept as they were notes = Callout("info", …) unreachable from root: dropped Also: a new name is appended (then wired in by a new root); name = null deletes outright. The model streamed 72 characters instead of a full program. The 4 statements it did not touch stay as they were.
  1. The program on screen: six statements. root lists title, chart and notes; chart uses regions and rev.
  2. The user asks for stacked bars and no note. The model, in edit mode, writes two statements: root again without notes, and chart with "stacked".
  3. Same name, replaced. root and chart exist in both, so the patch versions take their place, in their original position.
  4. Not in the patch, kept. title, regions, rev and, for now, notes carry over unchanged. Queries and $variables carry over the same way.
  5. Garbage collection. The merge walks references from root (the lines on the right). notes is no longer reachable, so it is dropped. That is how a patch deletes a component: re-declare the parent without it.
  6. The lab prints patch chars: 72 vs full program: 290. It then adds a statement (new names are appended) and deletes it again with total = null.

OpenUI rule

Same name: the patch replaces the old statement. New name: added. Not in the patch: kept. Removed from its parent: no longer reachable from root (or the name passed as the third argument), so dropped; $variables are always kept. name = null: deleted.

What edit mode adds to the prompt

Edit mode is one flag on the system prompt: library.prompt({ editMode: true }), or generateSystemPrompt({ library: spec, promptOptions: { editMode: true } }) on the server. It appends this section to the generated prompt (quoted from @openuidev/lang-core 1.0.1, rules abridged):

## Edit Mode

The runtime merges by statement name: same name = replace, new name = append.
Output ONLY statements that changed or are new. Everything else is kept automatically.

### Delete
To remove a component, re-declare its parent without that component in the parent's
children array. The removed component and any statements only it referenced are
automatically garbage-collected.

### Rules
- Reuse existing statement names exactly — do not rename
- Do NOT re-emit unchanged statements — the runtime keeps them
- NEVER output the entire program as a patch. Only output what actually changes

The model also needs to see the previous program, so the app sends the conversation history with each request, including the last program. The flag only changes what the model writes; the merge is your code's job. Nothing in the Renderer merges for you.

Merge a patch yourself

The previous program is the one from the lab. Edit the patch, or pick a preset. The merged program is shown as a diff against the previous one, the two rendered results sit side by side, and the bars compare what the model had to stream.

Previous program


        

Merged program


        
~ replaced+ added- droppedno mark: kept

What the model streamed

Before

After the merge

The merge here is the simulator's port of mergeStatements; for these presets it returns the same text as @openuidev/lang-core 1.0.1 (checked against the real function). The rendering is the teaching subset, not the real React components. Token figures are estimates (characters divided by four).

Break it

Predict first, then pick the preset and check.

Merge once, after the stream ends

The third break-it is the important one. During the stream, root is half written, and a merge would garbage-collect everything the half-written root cannot reach yet. So the app renders the previous program while the patch streams, and calls mergeStatements(previous, latest) when isStreaming becomes false. The merged text becomes the new previous for the next turn. The user sees the old interface until the new one is complete, which for a two-line patch is a fraction of a second.

Key idea

The app owns the current program. Edit mode only works if you keep the last merged program and send it back as history; without it there is nothing to merge into. A patch that reuses names is what keeps live parts alive: the Query statements and $variables the patch does not touch keep their text, so the program still has the same state names and the same queries after the merge.

When a patch beats a fresh program

Use edit mode whenWrite a fresh program when
The user refines one interface over several turns: "add a filter", "rename the title", "remove the chart".Each answer is a new, unrelated interface, such as a chat that replies with a fresh card every turn.
Programs are long, so a rewrite costs real time and tokens.The program is a handful of statements, so a patch saves little.
The app stores the current program and can merge into it.The app does not keep the previous program.
You build the system prompt in your own backend.You use Thesys's hosted Gateway to build the prompt: it drops editMode.

The OpenUI docs give an order-of-magnitude example: a 20-statement dashboard at about 400 tokens and 2 seconds to stream, against a 2-statement patch at about 60 tokens and 0.3 seconds. Those are the docs' own illustrative numbers; the lab's measured case is 72 characters against 290.

Common misconception

"Edit mode is a diff format." It is ordinary OpenUI Lang. A patch parses like any program, and the merge works by statement name, not by line numbers or text positions. That is why the prompt insists on reusing names exactly: a renamed statement is a new statement, and the old one is collected.

Quick check

The previous program's root lists header, chart and tbl. The patch is root = Stack([header, tbl]). What happens to chart?

  • It is kept, because the patch does not mention it
  • It is dropped, because nothing reachable from root refers to it any more
  • It is kept, but the parser reports it as orphaned
"Not in the patch" means kept only while something still reaches it. The new root no longer lists chart, so the merge's walk from root never visits it and it is collected, along with any statements only it referenced.

Where should the app call mergeStatements?

  • On every chunk, so the user sees the change appear gradually
  • Inside the model's system prompt, so the model returns the merged program
  • Once, when the stream ends; until then it keeps rendering the previous program
Mid-stream, root is half written, and merging would collect everything it cannot reach yet. The merge is app code that runs once on the finished patch.

A patch contains only title = TextContent("Q4 revenue", "large-heavy"). How long is the merged program, compared with the previous one?

  • The same number of statements: title is replaced in place
  • One statement longer: the new title is appended
  • One statement: only the patch survives
The name already exists, so the patch definition takes its place. Appending only happens for new names.

Recap