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
mergeStatementsproduces: replace by name, append new names, delete withname = null, drop whatrootno 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
- The program on screen: six statements.
rootliststitle,chartandnotes;chartusesregionsandrev. - The user asks for stacked bars and no note. The model, in edit mode, writes two statements:
rootagain withoutnotes, andchartwith"stacked". - Same name, replaced.
rootandchartexist in both, so the patch versions take their place, in their original position. - Not in the patch, kept.
title,regions,revand, for now,notescarry over unchanged. Queries and$variablescarry over the same way. - Garbage collection. The merge walks references from
root(the lines on the right).notesis no longer reachable, so it is dropped. That is how a patch deletes a component: re-declare the parent without it. - The lab prints
patch chars: 72 vs full program: 290. It then adds a statement (new names are appended) and deletes it again withtotal = 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
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 when | Write 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?
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?
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?
Recap
editMode: trueadds an## Edit Modesection: output only changed or new statements, reuse names, delete by re-declaring the parent.mergeStatements(previous, patch)replaces by name, appends new names, deletesname = null, and drops whateverrootno longer reaches;$variablesstay. If the entry has another name, pass it as the third argument, or nothing is dropped.- Merge once, after the stream ends, and keep the result as the next turn's previous program.
- Patches pay off for long programs refined over several turns; a fresh program is simpler when each answer is a new interface.