Skip to main content

a2ui_agent 0.1.0 copy "a2ui_agent: ^0.1.0" to clipboard
a2ui_agent: ^0.1.0 copied to clipboard

Helps a Dart agent generate A2UI: negotiates catalogs with the renderer, teaches the model the catalog, and parses its reply into A2UI messages.

example/a2ui_agent_example.dart

// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:a2ui_agent/a2ui_agent.dart';
import 'package:a2ui_core/a2ui_core.dart';

/// One agent turn in the Express format.
///
/// [callLlm] sends the system prompt and the user's message to the model and
/// returns its complete response.
Future<List<AgentToRendererMessage>> respond({
  required CatalogApi catalog,
  required A2uiRendererCapabilities rendererCapabilities,
  required String userMessage,
  required Future<String> Function(String systemPrompt, String userMessage)
  callLlm,
}) async {
  // 1. At agent startup: register the catalogs the agent supports.
  final generator = A2uiGenerator(catalogs: [CatalogConfig(catalog)]);

  // 2. Per request: negotiate with the renderer's capabilities.
  final A2uiRequestProcessor processor = generator.createProcessor(
    rendererCapabilities,
  );

  // 3. Call the LLM with the prompt snippet.
  final String llmOutput = await callLlm(processor.promptSnippet, userMessage);

  // 4. Parse the response. Each turn of this chat shows new UI, so the
  // response is checked against a new, empty renderer state. A canvas agent,
  // whose turns update earlier surfaces, keeps one `processor.createState()`
  // for the conversation and passes it as `state:` on every turn.
  final List<ResponsePart> parts = processor.parseResponse(llmOutput);

  // 5. Deliver the A2UI messages to the renderer.
  return [
    for (final A2uiPart part in parts.whereType<A2uiPart>()) ...part.a2ui,
  ];
}
0
likes
160
points
638
downloads

Documentation

API reference

Publisher

verified publisherlabs.flutter.dev

Weekly Downloads

Helps a Dart agent generate A2UI: negotiates catalogs with the renderer, teaches the model the catalog, and parses its reply into A2UI messages.

Repository (GitHub)
View/report issues
Contributing

License

Apache-2.0 (license)

Dependencies

a2ui_core, antlr4

More

Packages that depend on a2ui_agent