renderCatalogInstructions function
Renders a catalog into model-facing instructions: when and how to render
UI, then the A2UI Express contract and the catalog's signatures (from
a2ui_agent). Injected into the system prompt by the middleware when
instructions != 'none'.
Implementation
String renderCatalogInstructions(A2uiCatalog catalog) {
final has = catalog.components.keys.toSet();
// Forms guidance only applies if the catalog has input components.
final inputs = [
'TextField',
'CheckBox',
'Slider',
'ChoicePicker',
'DateTimeInput',
].where(has.contains).toList();
final formsSection = inputs.isNotEmpty
? '''
## Forms
Input components (${inputs.join(', ')}) do NOT send their values
automatically. To capture what the user entered you MUST do BOTH:
1. Bind each input's value to a data-model path, e.g.
`email = TextField("Email", \$/email)`. Typing updates the data model.
2. On the submit Button, echo those same paths in the Event context, e.g.
`Event("submit", {email: \$/email})`.
Without both, the action arrives with an empty context and the entered values
are lost.'''
: '';
return '''# Rendering UI with A2UI
You can render rich, interactive UI (not just text) by emitting an A2UI
surface. Render one when the answer is data the user will look at or act on:
structured results from tools (multiple fields or records), comparisons,
choices to pick from, forms, dashboards, or anything interactive. You may still
write normal prose before or after it.
Unless the system instructions ask for a surface, do NOT render a surface when
the answer reads fine as a few sentences: greetings, small talk, opinions,
advice, explanations, or simple facts. Answer those in prose. A surface that
only holds paragraphs of text is worse than the text.
## Surfaces
- Start every <a2ui> block with surface("<id>"). For a new surface, choose a
new, short, descriptive id (e.g. "tokyo-weather").
- To change a surface you already rendered (e.g. after the user presses one of
its buttons), reuse its id. Either render it again in full, with a `root`,
which replaces it, or, for a small change, leave out `root` and write only
what changed: data paths (`\$/temp = "21°C"`) and components redefined
under their existing variable names. A redefined component replaces the old
one entirely, so restate all of its arguments. Every variable a component
uses must be assigned in the same block, so redefining a parent means
redefining its children too. Everything the block does not mention stays as
it was. (The rule that a tree needs a `root` is for new surfaces and full
re-renders.)
- Give containers their own variables (as `body` in the example), so a later
update can redefine them.
- User actions arrive as lines like
`[UI action "refresh" from refresh on surface tokyo-weather context={"city":"Tokyo"}]`.$formsSection${_renderStyleTips(has, v1: _isV1(catalog))}${_renderExample(catalog, has)}
${ExpressEngine.of(catalog).promptSnippet}${_renderCatalogGuidance(catalog)}
Do not explain the Express code; just emit the block.''';
}