-
Notifications
You must be signed in to change notification settings - Fork 12
feat(agent): OpenUI bindings — library/fragment builders, toUIOutput, getUiStream() (DEV-773) #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LukasParke
wants to merge
23
commits into
main
Choose a base branch
from
lukeparke/dev-773-typescript-agent-openui-module-libraryfragment-builders
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
383c282
feat(agent): OpenUI library model, fragment builder, and plugin helpe…
LukasParke a3347b5
feat(agent): toUIOutput on tool(), tool.ui_fragment events, and getUi…
LukasParke 978cab9
feat(playground): OpenUI test/bench/eval webapp (DEV-773)
LukasParke 69bfb14
fix(openui): quote non-identifier object keys, guard non-finite numbers
LukasParke 3720fde
refactor: clear the structural gate — god file and 4 complex functions
LukasParke e5759d3
docs(agent): list getUiStream in the README stream table
LukasParke 4eb82c7
Merge remote-tracking branch 'origin/main' into lukeparke/dev-773-typ…
LukasParke b388f35
fix(playground): escape diagnostic messages; label rendered controls
LukasParke 37f816d
fix(openui): address cortex review — security, a11y, perf
LukasParke 6898806
Merge branch 'lukeparke/dev-773-typescript-agent-openui-module-librar…
LukasParke 20ab446
Merge branch 'main' into lukeparke/dev-773-typescript-agent-openui-mo…
synapse-github-agent[bot] 20c0e5c
Merge remote-tracking branch 'origin/main' into wt/pr92
LukasParke 3d4a582
fix(openui): address review findings
LukasParke 3cefbdc
refactor(openui): simplify native diagnostics counting
LukasParke 829a94c
fix(agent): bound OpenUI fragment rendering
LukasParke 6701156
fix(openui): finish review feedback
LukasParke ff70982
fix(openui): release timed-out UI renders
LukasParke e956e1e
fix(openui): drain renders added before close
LukasParke 0f7eb50
fix(agent): drain UI fragments before stream completion
LukasParke bccd1ad
fix(agent): isolate UI stream lifecycle
LukasParke cbfd884
fix(agent): release exited UI consumers
LukasParke 1f68c5e
fix(agent): merge OpenUI event streams
LukasParke 11eac75
fix(agent): render deferred tool UI results
LukasParke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| --- | ||
| '@openrouter/agent': minor | ||
| --- | ||
|
|
||
| OpenUI bindings: a component-library model (`defineComponent`, `createLibrary`, `componentProps`), a typed fragment builder (`fragment`, `uiRef`, `uiState`, `uiBuiltin`), the `openui` plugin helper, `serializeExpr`/`OPENUI_LANG_DIALECT` for emitting OpenUI Lang, a `toUIOutput` tool option that renders a tool's result as UI, and `ModelResult.getUiStream()` for consuming fragments as they arrive. | ||
|
|
||
| A tool declares how its output renders, and the caller streams the fragments: | ||
|
|
||
| ```ts | ||
| import { | ||
| callModel, | ||
| createLibrary, | ||
| defineComponent, | ||
| fragment, | ||
| openui, | ||
| tool, | ||
| } from '@openrouter/agent'; | ||
| import { z } from 'zod/v4'; | ||
|
|
||
| const library = createLibrary([ | ||
| defineComponent({ | ||
| name: 'Card', | ||
| description: 'Container with a title', | ||
| props: z.object({ | ||
| title: z.string(), | ||
| children: z.array(z.unknown()).optional(), | ||
| }), | ||
| }), | ||
| defineComponent({ | ||
| name: 'Text', | ||
| props: z.object({ | ||
| value: z.string(), | ||
| }), | ||
| }), | ||
| ]); | ||
|
|
||
| const ui = fragment(library); | ||
|
|
||
| const weather = tool({ | ||
| name: 'weather', | ||
| inputSchema: z.object({ | ||
| city: z.string(), | ||
| }), | ||
| outputSchema: z.object({ | ||
| summary: z.string(), | ||
| }), | ||
| execute: ({ city }) => ({ | ||
| summary: `Clear in ${city}`, | ||
| }), | ||
| // Renders the tool's result instead of leaving the model to describe it. | ||
| toUIOutput: ({ input, output }) => | ||
| ui.Card(input.city, [ | ||
| ui.Text(output.summary), | ||
| ]), | ||
| }); | ||
|
|
||
| const result = callModel(client, { | ||
| model: 'anthropic/claude-sonnet-4.5', | ||
| input: 'What is the weather in Lisbon?', | ||
| tools: [ | ||
| weather, | ||
| ], | ||
| plugins: [ | ||
| // `as never` until the SDK regen adds `openui` to its plugin union; the | ||
| // wire shape is already accepted by the API. | ||
| openui(library) as never, | ||
| ], | ||
| }); | ||
|
|
||
| for await (const event of result.getUiStream()) { | ||
| if (event.type === 'fragment') { | ||
| // source: 'root = Card("Lisbon", [Text("Clear in Lisbon")])' | ||
| console.log(event.source); | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * OpenUI Lang expression model + serialization. | ||
| * | ||
| * OpenUI Lang is a line-oriented assignment language: one statement per line, | ||
| * `name = expression`. The SDK only *authors* OpenUI Lang (tool-authored | ||
| * fragments, wire-format libraries) — parsing, validation, and prompt | ||
| * injection are API-side responsibilities. This module is therefore the | ||
| * minimal expression tree and serializer shared by the fragment builder. | ||
| */ | ||
|
|
||
| /** The OpenUI Lang dialect this package emits. */ | ||
| export const OPENUI_LANG_DIALECT = 'openui-lang/0.5'; | ||
|
|
||
| /** The reserved assignment ref that designates the document root. */ | ||
| export const OPENUI_ROOT_REF = 'root'; | ||
|
|
||
| export type UiLiteralValue = string | number | boolean | null; | ||
|
|
||
| /** Expression tree for one assignment's right-hand side. */ | ||
| export type UiExpr = | ||
| | { | ||
| kind: 'literal'; | ||
| value: UiLiteralValue; | ||
| } | ||
| | { | ||
| kind: 'ref'; | ||
| name: string; | ||
| } | ||
| | { | ||
| kind: 'state-ref'; | ||
| name: string; | ||
| } | ||
| | { | ||
| kind: 'member'; | ||
| base: UiExpr; | ||
| path: string[]; | ||
| } | ||
| | { | ||
| kind: 'array'; | ||
| items: UiExpr[]; | ||
| } | ||
| | { | ||
| kind: 'object'; | ||
| entries: Array<{ | ||
| key: string; | ||
| value: UiExpr; | ||
| }>; | ||
| } | ||
| | { | ||
| kind: 'call'; | ||
| fn: string; | ||
| builtin: boolean; | ||
| args: UiExpr[]; | ||
| }; | ||
|
|
||
| /** | ||
| * A renderable piece of UI: the dialect it's expressed in plus its serialized | ||
| * OpenUI Lang source. This is the shape carried on `tool.ui_fragment` stream | ||
| * events and (for server tools) `response.openui.fragment` wire events. | ||
| */ | ||
| export interface UiFragment { | ||
| dialect: string; | ||
| source: string; | ||
| } | ||
|
|
||
| /** | ||
| * Bare-identifier object keys, which the grammar accepts unquoted. Anything | ||
| * else — spaces, quotes, punctuation, a leading digit, the empty string — must | ||
| * be quoted or the emitted source does not parse. | ||
| */ | ||
| const BARE_KEY = /^[A-Za-z_][A-Za-z0-9_]*$/; | ||
|
|
||
| /** | ||
| * Object keys reach here from arbitrary tool-authored objects via `toExpr`, so | ||
| * they cannot be assumed to be identifiers. The parser accepts a quoted key | ||
| * (`parseObject` branches on `"`), so quoting the rest round-trips. | ||
| */ | ||
| function serializeKey(key: string): string { | ||
| return BARE_KEY.test(key) ? key : JSON.stringify(key); | ||
| } | ||
|
|
||
| /** | ||
| * Numbers that have no OpenUI Lang literal: `String(NaN)` is `NaN` and | ||
| * `String(Infinity)` is `Infinity`, both of which serialize as bare identifiers | ||
| * and would parse back as refs to undefined names (or fail outright). JSON has | ||
| * the same hole and resolves it as `null`; do the same rather than emit source | ||
| * that cannot round-trip. | ||
| */ | ||
| function serializeNumber(value: number): string { | ||
| return Number.isFinite(value) ? String(value) : 'null'; | ||
| } | ||
|
|
||
| /** Serialize an expression to OpenUI Lang source. */ | ||
| export function serializeExpr(expr: UiExpr): string { | ||
| switch (expr.kind) { | ||
| case 'literal': | ||
| if (typeof expr.value === 'string') { | ||
| return JSON.stringify(expr.value); | ||
| } | ||
| return typeof expr.value === 'number' ? serializeNumber(expr.value) : String(expr.value); | ||
| case 'ref': | ||
| return expr.name; | ||
| case 'state-ref': | ||
| return `$${expr.name}`; | ||
| case 'member': | ||
| return `${serializeExpr(expr.base)}.${expr.path.join('.')}`; | ||
| case 'array': | ||
| return `[${expr.items.map(serializeExpr).join(', ')}]`; | ||
| case 'object': | ||
| return `{${expr.entries.map((e) => `${serializeKey(e.key)}: ${serializeExpr(e.value)}`).join(', ')}}`; | ||
| case 'call': | ||
| return `${expr.builtin ? '@' : ''}${expr.fn}(${expr.args.map(serializeExpr).join(', ')})`; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.