2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
+ import 'dart:async' ;
6
+
5
7
import 'package:dart_schema_builder/dart_schema_builder.dart' ;
6
8
import 'package:firebase_app_check/firebase_app_check.dart' ;
7
9
import 'package:firebase_core/firebase_core.dart' ;
@@ -89,7 +91,7 @@ class TravelPlannerPage extends StatefulWidget {
89
91
class _TravelPlannerPageState extends State <TravelPlannerPage > {
90
92
late final GenUiManager _genUiManager;
91
93
late final AiClient _aiClient;
92
- late final UiEventManager _eventManager ;
94
+ late final StreamSubscription < UserMessage > _userMessageSubscription ;
93
95
final List <ChatMessage > _conversation = [];
94
96
final _textController = TextEditingController ();
95
97
final _scrollController = ScrollController ();
@@ -108,7 +110,9 @@ class _TravelPlannerPageState extends State<TravelPlannerPage> {
108
110
),
109
111
),
110
112
);
111
- _eventManager = UiEventManager (callback: _onUiEvents);
113
+ _userMessageSubscription = _genUiManager.onSubmit.listen (
114
+ _handleUserMessageFromUi,
115
+ );
112
116
_aiClient =
113
117
widget.aiClient ??
114
118
FirebaseAiClient (
@@ -146,7 +150,7 @@ class _TravelPlannerPageState extends State<TravelPlannerPage> {
146
150
@override
147
151
void dispose () {
148
152
_genUiManager.dispose ();
149
- _eventManager. dispose ();
153
+ _userMessageSubscription. cancel ();
150
154
_textController.dispose ();
151
155
_scrollController.dispose ();
152
156
super .dispose ();
@@ -206,37 +210,14 @@ class _TravelPlannerPageState extends State<TravelPlannerPage> {
206
210
return ;
207
211
}
208
212
209
- void _onUiEvents (String surfaceId, List <UiEvent > events) {
210
- final actionEvent = events.firstWhere ((e) => e.isAction);
211
- final message = StringBuffer (
212
- 'The user triggered the "${actionEvent .eventType }" event on widget '
213
- '"${actionEvent .widgetId }"' ,
214
- );
215
- final value = actionEvent.value;
216
- if (value is String && value.isNotEmpty) {
217
- message.write (' with value "$value "' );
218
- }
219
- message.write ('.' );
220
-
221
- final changeEvents = events.where ((e) => ! e.isAction).toList ();
222
- if (changeEvents.isNotEmpty) {
223
- message.writeln (' Current values of other widgets:' );
224
- for (final event in changeEvents) {
225
- message.writeln ('- Widget "${event .widgetId }": ${event .value }' );
226
- }
227
- }
228
-
213
+ void _handleUserMessageFromUi (UserMessage message) {
229
214
setState (() {
230
215
_conversation.add (UserUiInteractionMessage .text (message.toString ()));
231
216
});
232
217
_scrollToBottom ();
233
218
_triggerInference ();
234
219
}
235
220
236
- void _handleUiEvent (UiEvent event) {
237
- _eventManager.add (event);
238
- }
239
-
240
221
void _sendPrompt (String text) {
241
222
if (_isThinking || text.trim ().isEmpty) return ;
242
223
setState (() {
@@ -273,7 +254,6 @@ class _TravelPlannerPageState extends State<TravelPlannerPage> {
273
254
child: Conversation (
274
255
messages: _conversation,
275
256
manager: _genUiManager,
276
- onEvent: _handleUiEvent,
277
257
scrollController: _scrollController,
278
258
),
279
259
),
@@ -398,7 +378,7 @@ to the user.
398
378
a Column with the existing OptionsFilterChipInput, a
399
379
ItineraryWithDetails containing the full itinerary, and a Trailhead
400
380
containing some options of specific details to book e.g. "Book accommodation in Kyoto", "Train options from Tokyo to Osaka".
401
-
381
+
402
382
Note that during this step, the user may change their search parameters and
403
383
resubmit, in which case you should regenerate the itinerary to match their
404
384
desires, updating the existing surface.
@@ -450,7 +430,7 @@ When processing a user message or event, you should add or update one surface
450
430
and then call provideFinalOutput to return control to the user. Never continue
451
431
to add or update surfaces until you receive another user event. If the last
452
432
entry in the context is a functionResponse, just call provideFinalOutput
453
- immediately - don't try to update the UI.
433
+ immediately - don't try to update the UI.
454
434
455
435
# UI style
456
436
0 commit comments