How to/From DSL to SwiftUI
PROCESS 03 / FROM THE DSL TO SWIFTUI

From DSL
to SwiftUI.

The server sends OpenUI Lang, the DSL the writer produces, as program text, with the component templates the program calls. The device compiles that text into elements with finished values. SwiftUI draws those elements from handles that the next compile reuses.

OpenUI LangComponentsSwift compilerSwiftUIInteractive system guide
01 / THE WHOLE BRIDGE

Follow one primitive.

Compare a leaf with a container
Primitive explorer Compiled output from a real session
TYPESCRIPT · BUILD TIME
P({
  name: "Text", category: "text",
  props: {
    content: copy.text(),
    role: token("type"),
    weight: opt(token("weight")),
    ink: opt(token("ink")),
  },
})
Key order becomes position↓
ARGUMENT ORDER
Text(content, role, weight?, ink?, ...)
Step 1 of 5
02 / THE CONTRACT

The catalog carries positions.

The catalog holds more than Swift uses. Primitive names, argument order and the slot role change behavior on the device. Token values, enums and data shapes are enforced only on the server. Component templates are not in the primitive catalog: they arrive with each run.

Who catches whatOne statement, two compilers
OPENUI LANG
pick1_title = Text("Top scorers", "title2")
Read by both sides
SERVER COMPILERAdmitted

zod checks the arguments against the primitive schema and the real dataset values.

SWIFT COMPILER AND VIEWSDrawn

content and role become properties. PrimitiveTypeRole maps title2 to a font size and line height.

↳
Both sides agree.

The server checks meaning. The device builds structure from names and positions.

How do design tokens cross? +

They take three routes. Token names and validation live in src/primitives/tokens.ts and stay on the server. Spacing values travel as plain numbers in the program, so Swift reads gap directly. Type sizes are copied by hand into PrimitiveTokens.swift and render.tsx. Colors and component anatomy skip TypeScript, because the app bundles design_system.json directly.

TYPE in TypeScript ↗TYPE in Swift ↗Bundled theme ↗
03 / LAYOUT COMPOSITION

Props choose the container.

A layout is a tree of primitives that hold slots. StackPrimitive reads axis, gap and justify, then chooses a SwiftUI container. Nothing in the program describes a frame or a position.

Stack explorerRendered with design system primitives
OPENUI LANG
pick2 = Stack("v", 8, @Each(d.answers.q1.rows, "r", Badge(r.name, "neutral")))
StackPrimitive selects↓
SWIFTUI
VStack(alignment: .leading, spacing: 8) {
  ForEach(Array(ids.enumerated()), id: \.element) { index, id in
    PrimitiveNodeView(id: id)
      .frame(maxWidth: .infinity, alignment: ...)
  }
}
RESULT · WEB PRIMITIVE RENDERER
axis
gap
01

OpenUISceneView

Removes Screen and lays its children out with the screen inset. Screen has no case in the switch.

02

OpenUIElementView

Reads properties from the handle and builds PrimitiveProps.

03

PrimitiveVisual

Switches on the kind string and returns the matching primitive view.

04

A container view

Reads its child slot with childReferences, so it tracks that one list.

05

PrimitiveNodeView

Wraps each child handle and returns to step 02 for the next level of the tree.

In the program
Stack("wrap", 8, children)
In SwiftUI
FlowLayout(spacing: 8) { ... }

A custom Layout. Children keep their own width and flow into rows.

How does a child know about its parent? +

Layout context travels through the SwiftUI environment. primitiveStackAxis tells Spacer which way to grow and lets a trailing column keep its own width. primitiveBleed gives full-bleed children their reach, and a Card resets it to its own padding. primitiveRailWidth sizes items in a rail.

Environment values ↗Screen removal ↗
04 / COMPLEX ELEMENTS

A component travels by name and expands on the device.

PlayerValue, end to end

The writer calls Sports components such as PlayerValue by name, and the program keeps the call on the wire. Each component's template, an OpenUI Lang body over its parameters, travels as data. The device expands the call before it builds elements, so it still draws only primitives. Most templates are a tree of primitives inside a Card or a Stack. PlayerValue's template is a single data primitive, StepTimeline, and its complexity lives in one Swift view.

Component mapper PlayerValue/full catalog fixturecall → template → SwiftUI
FOTMOB · MARKET VALUES
{ date: "2024-12-31", value: 116235970,
  teamName: "Chelsea", teamId: 8455,
  teamColor: { darkMode, lightMode } }
playerValueChart↓
D.ANSWERS.Q1.CHART
{ value: "€97.7M",
  caption: "Cole Palmer · Market value",
  segments: [ { name, lineColor, tagColor, logo,
                points: [ { x, label, value, display } ] } ],
  ticks: [ { value, display } ] }
Step 1 of 6

Map one field from the record to the screen

Read by stretches()
IN THE RECORD
d.answers.q1.chart.segments[2].lineColor = "#033cc1"
StepTimeline.segments, one item per stretch↓
STEPTIMELINEPRIMITIVE · STRETCHES()
line: color(item["lineColor"]) ?? palette.ink("primary")
ON SCREENWhat it draws

The stretch's line, its dot and its name while scrubbing. A value that does not start with # falls back to primary ink.

↳
To add a complex element, decide where its complexity lives.

If existing primitives can draw it, write the component as a build that returns a tree. npm run openui:export prints the build as its template, and the template reaches devices with the next run, with no app release. If it needs new drawing or gestures, such as the stepped line and the scrub, add a primitive to PRIMITIVES, export the catalog and add a Swift case. The component then becomes a thin template over that primitive.

05 / STREAMING

The wire carries program text.

SSE

The server never sends a tree. It sends text in chunks of complete statements, and the device parses and evaluates all of its text after each chunk. The device then compares the result with what is on screen.

Stream player Illustrative runCommit counts from the real OpenUISession
ON THE WIRE
event: templates
{ templates: [{ name, params, body }, ...] }
Append, then compile all of it↓
SESSION.PROGRAM
Empty
commit writes the differences↓
lastCommitphase .loading
0created
0updated
0slots
0removed
SwiftUI redraws from the handles↓
SCREEN · 0 SKELETON FRAMES

No blocks yet.

↳
A part is the unit of streaming.

A writer statement alone never reaches the device. A part and all its children land in one event and one synchronous commit, so a card never appears half built. The cost is silence, because a long part shows nothing until its last statement passes.

What happens to a rejected statement? +

The server drops it and sends an issue event. Its text never reaches the device, and issues do not change what the app draws. The writer gets one repair round, with a message that lists its rejected statements and the problems. Questions still uncovered after that get a fallback part, which appears only in done.program.

Repair round ↗Statement validation ↗
06 / NODE IDENTITY

A view lives as long as its handle.

Each chunk produces a whole new scene, and SwiftUI must not rebuild the screen for it. An element id comes from its document key and its mount path. Commit reuses a handle when the id, the kind and the slot names all match.

ReconcilerCounts from the real OpenUISession
BEFORE
pick1 = Text("Hello", "body")
AFTER
pick1 = Text("Bye", "body")
OpenUISession.commit
Same handle@State kept · updated 1
↳
A value changes

The id and kind match, so commit reuses the handle and writes the new properties.

07 / DATA BINDING

Swift views receive values, not paths.

A reference like d.answers.q1.rows never reaches a SwiftUI view. The compiler resolves it against the dataset and stores the result in the element. Views read that result from their handle, and @Observable tells SwiftUI when it changes. A component call resolves its record the same way, then hands the value to the template's fresh scope.

Path resolver @Each(d.answers.q1.rows, "r", Text(r.name, "body"))
EVALUATOR VALUE AT D.ANSWERS.Q1.ROWS
[
  { name: "Haaland", goals: 24 },
  { name: "Salah", goals: 19 },
  { name: "Palmer", goals: 15 }
]
@Each runs once per row↓
Elements in the scene4
r1:ref:pick2Stack · axis "v", gap 8
r1:ref:pick2/children/item:0Text · content "Haaland"
r1:ref:pick2/children/item:1Text · content "Salah"
r1:ref:pick2/children/item:2Text · content "Palmer"

New data reaches a view through commit

Follow-up updateCounts from the real OpenUISession
DATASET · RUN R1
rows: [
  { name: "Haaland", goals: 24 },
  { name: "Salah", goals: 19 }
]
RESULT · WEB PRIMITIVE RENDERER
Waiting for a follow-up↓
HandlesDocument r1 · revision 1
r1:ref:pick1Stack · no write
r1:ref:pick1/children/item:0KeyValue · value 24
r1:ref:pick1/children/item:1KeyValue · value 19 · no write
↳
New data takes the same path as the first compile.

A follow-up holds the new dataset and program until done confirms the next revision. It then compiles once. The document key stays r1, so element ids match and commit writes only the value that changed. @Observable tells SwiftUI which handle moved.

Send a follow-up with one new value.

Two kinds of state

HANDLE.PROPERTIES

Evaluated props

Every data reference and literal. Only commit writes them, and the views that read them update.

@STATE

Local UI state

Toggle, Stepper, Chip, SegmentedControl and an open Section. Only that view can read it. Once set, it ignores later prop updates for that handle.

↳
No SwiftUI binding connects two primitives.

The language has no variables, so a Toggle cannot drive a Show. To change what another view shows, the program or the dataset has to change.

How does @Observable limit the work? +

registry and lastCommit are marked @ObservationIgnored. Update assigns only when a value differs, and slot set assigns only when the list differs by object identity. OpenUIElementView tracks properties, a container tracks its own slot, and the scene tracks root membership. These rules limit which writes happen.

Observation intent ↗
08 / KNOWN GAPS

What the bridge does not check.

Check these limits before you add a primitive or change an argument.

VERSIONS

Nothing compares the digest

Swift decodes the catalog digest and never reads it. The server does not send one. Templates travel with each run, but a primitive newer than the app still fails the compile.

TESTS

Parity stops at the tree

OpenUIFixtureTests compares compiled trees, for each expanded sample and for the same sample called by name. It tests no view code. It runs with make test and not in CI. A typo in a prop name read by a view passes every check.

COVERAGE

Fixtures cover part of the catalog

check:sports-catalog looks for a Swift case for each kind in the sports fixtures. A new primitive outside those fixtures gets no renderer check on either side.

ACTIONS

Actions wire in when a screen needs them

Button, IconButton and row actions call a dispatcher in the environment. It does nothing until a host sets primitiveContext, so these taps are inert today.

WHY THIS SHAPE

Compile everything. Write what changed.

The compiler comment describes the intent: it evaluates a complete immutable scene, then the runtime publishes only semantic changes. Each chunk therefore parses and evaluates the whole program on the main actor.

Components keep the same split. A template is data made of primitives, and it travels with the run. The device expands each call itself, so it still needs only the primitive catalog, and a new component that reuses existing primitives ships with no app release.

Documented compiler intent ↗
Built from the implementationSource links point to main.Evidence: code reading, plus the demo programs run through the real OpenUICompiler and OpenUISession in a scratch macOS harness. Results on this page use the web primitive renderer, and SwiftUI views were not run. The stream order is derived from server code, not captured from a live run. The template stages come from OpenUIFixtureTests and check:templates, which compile every catalog sample both expanded and called by name.The component mapper uses the PlayerValue/full catalog fixture. Its Swift excerpts are shortened from StepTimelinePrimitive.swift.
Open the Preview ↗