GenUiFunction class

Marks a top-level Dart function as a function of the A2UI catalog.

A catalog has two halves. Components are what the agent composes a surface out of; functions are what it computes a value with, through the {"call": ...} form that any bound property already accepts. The basic catalog ships fourteen of them (required, regex, length, numeric, email, formatString and the rest), and an app that wants one of its own has to write a FunctionImplementation by hand: the name, the description, an argumentSchema spelled out in JSON schema, the return type, and an execute that digs each argument back out of a map and casts it.

That is the same drift this package removes from components, one half of the catalog over. The schema is written by hand beside the code that reads the arguments, so renaming a parameter, adding a required one or changing a type leaves the schema describing a function that no longer exists, and the app still compiles.

Annotate the function instead:

@GenUiFunction(description: 'Formats an amount as a price.')
String formatPrice(double amount, {String currency = 'USD'}) =>
    NumberFormat.simpleCurrency(name: currency).format(amount);

The argument schema property names are the parameter names, the required list is the set of parameters without defaults, the descriptions come from the doc comments, and the return type comes from the Dart return type. The generated execute coerces each argument the way a @GenUiData field is read, so a model that sends a string where a number was declared degrades instead of throwing.

Register the generated functions the same way as the items: the aggregating builder puts them in genui_catalog.g.dart and hands them to the assembled CatalogImplementation, so they reach both the prompt and the exported catalog.json.

Annotations
  • @Target.new({TargetKind.function})

Constructors

GenUiFunction({required String description, String? name})
Creates a GenUiFunction annotation.
const

Properties

description → String
What the function does, and when the model should call it.
final
hashCode → int
The hash code for this object.
no setterinherited
name → String?
The name the function is called by in {"call": ...}.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited