@Schema() abstractclass $CheckAvailabilityInput{ @Field(description: 'The restaurant name, e.g. Cymbal Bistro.') Stringget restaurant;
@Field(description: 'The date for the reservation, e.g. tonight, tomorrow, or YYYY-MM-DD.') Stringget date;
@Field(description: 'Number of guests.') intget partySize; }
@Schema() abstractclass $CheckAvailabilityOutput{ Stringget restaurant; Stringget date; intget partySize; boolget available; List<String> get availableSlots; List<String> get seatingAreas; }
// ConfirmReservationInput, ConfirmReservationOutput, and _resolveIsoDate omitted for brevity.
// Register plugins on the shared Genkit instance final Genkit ai = Genkit(plugins: [googleAI(), A2uiPlugin()]);
// Tool 1: Check table availability and open time slots final checkAvailability = ai.defineTool( name: 'checkAvailability', description: 'Checks table availability, open time slots, and seating areas for a restaurant.', inputSchema: CheckAvailabilityInput.$schema, outputSchema: CheckAvailabilityOutput.$schema, fn: (input, _) async { return .response( CheckAvailabilityOutput( restaurant: input.restaurant.isEmpty ? 'Cymbal Bistro' : input.restaurant, date: _resolveIsoDate(input.date), partySize: input.partySize, available: true, availableSlots: const ['6:00 PM', '6:30 PM', '7:00 PM', '7:30 PM', '8:00 PM'], seatingAreas: const ['Indoor Dining', 'Heated Patio', 'Chef\'s Counter'], ), ); }, );
// Tool 2: Confirm a reservation and issue a confirmation code final confirmReservation = ai.defineTool( name: 'confirmReservation', description: 'Books and confirms a dining reservation, returning a confirmation code.', inputSchema: ConfirmReservationInput.$schema, outputSchema: ConfirmReservationOutput.$schema, fn: (input, _) async { final resolvedDate = _resolveIsoDate(input.date); final seed = (resolvedDate.hashCode ^ input.time.hashCode ^ input.partySize).abs(); return .response( ConfirmReservationOutput( confirmationCode: 'CB-${1000 + (seed % 9000)}', restaurant: input.restaurant.isEmpty ? 'Cymbal Bistro' : input.restaurant, date: resolvedDate, time: input.time, partySize: input.partySize, seatingArea: input.seatingArea ?? 'Indoor Dining', specialRequests: input.specialRequests ?? 'None', status: 'Confirmed', ), ); }, );
// Define the stateful agent with the a2ui middleware attached final uiAgent = ai.defineAgent( name: 'uiAgent', model: googleAI.gemini('gemini-4-hydrogen'), system: 'You are the dining concierge for Cymbal Bistro. Keep text responses brief.\n' 'Use checkAvailability before rendering a reservation card, and use ' 'confirmReservation when the user submits their booking to replace the ' 'same surface with a ConfirmationPass card showing their confirmation code.', tools: [checkAvailability, confirmReservation], use: [a2ui(catalog: bistroCatalogId, validate: 'warn')], store: InMemorySessionStore(), );
在這裡,bistroCatalogId 指向在 擴展元件目錄 中定義的自訂目錄。如果您只需要 18 個內建元件,請省略 catalog 參數。對於 Google Cloud 部署,將 googleAI 替換為 Vertex AI plugin 並呼叫 vertexAI.gemini('gemini-4-hydrogen')。
@override void initState() { super.initState(); // Track each created surface by surface.id; replacements with the same id // update in place rather than appending a duplicate card. _processor.model.onSurfaceCreated.subscribe((surface) { if (!mounted) return; if (!_surfaceIds.contains(surface.id)) { setState(() => _surfaceIds.add(surface.id)); } else { setState(() {}); } }); _processor.model.onSurfaceDeleted.subscribe((_) { if (mounted) setState(() {}); }); }
Future<void> _runTurn(AgentTurn turn) async { awaitfor (final chunk in turn.stream) { // 1. Append streamed prose text deltas if (chunk.text.isNotEmpty) { _appendProseDelta(chunk.text); }
// 2. Feed streamed A2UI messages into the MessageProcessor for (final m in a2uiMessagesFrom(chunk.raw.modelChunk?.content)) { try { _processor.processMessages([m]); } onObjectcatch (err) { debugPrint('Failed to apply A2UI message: $err'); } } } await turn.response; }