-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathmain.dart
More file actions
235 lines (202 loc) · 6.79 KB
/
main.dart
File metadata and controls
235 lines (202 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2025 The Flutter Authors.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:genui/genui.dart';
import 'package:genui_firebase_ai/genui_firebase_ai.dart';
import 'package:genui_google_generative_ai/genui_google_generative_ai.dart';
import 'package:logging/logging.dart';
// If you want to convert to using Firebase AI, run:
//
// sh tool/refresh_firebase.sh <project_id>
//
// to refresh the Firebase configuration for a specific Firebase project.
// and uncomment the Firebase initialization code and import below that is
// marked with UNCOMMENT_FOR_FIREBASE, and set the value of `aiBackend` to
// `AiBackend.firebase` in `lib/configuration.dart`.
// import 'firebase_options.dart'; // UNCOMMENT_FOR_FIREBASE
// Conditionally import non-web version so we can read from shell env vars in
// non-web version.
import 'api_key/io_get_api_key.dart'
if (dart.library.html) 'api_key/web_get_api_key.dart';
import 'configuration.dart';
import 'message.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Only initialize Firebase if we are using the Firebase backend.
if (aiBackend == AiBackend.firebase) {
await Firebase.initializeApp(
// UNCOMMENT_FOR_FIREBASE (See top of file for details)
// options: DefaultFirebaseOptions.currentPlatform,
);
}
configureGenUiLogging(level: Level.ALL);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Simple Chat',
theme: ThemeData(primarySwatch: Colors.blue),
home: const ChatScreen(),
);
}
}
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key});
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final TextEditingController _textController = TextEditingController();
final List<MessageController> _messages = [];
late final GenUiConversation _genUiConversation;
late final A2uiMessageProcessor _a2uiMessageProcessor;
final ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();
final Catalog catalog = CoreCatalogItems.asCatalog();
_a2uiMessageProcessor = A2uiMessageProcessor(catalogs: [catalog]);
final systemInstruction =
'''You are a helpful assistant who chats with a user,
giving exactly one response for each user message.
Your responses should contain acknowledgment
of the user message.
IMPORTANT: When you generate UI in a response, you MUST always create
a new surface with a unique `surfaceId`. Do NOT reuse or update
existing `surfaceId`s. Each UI response must be in its own new surface.
${GenUiPromptFragments.basicChat}''';
// Create the appropriate content generator based on configuration
final ContentGenerator contentGenerator = switch (aiBackend) {
AiBackend.googleGenerativeAi => () {
return GoogleGenerativeAiContentGenerator(
catalog: catalog,
systemInstruction: systemInstruction,
apiKey: getApiKey(),
);
}(),
AiBackend.firebase => FirebaseAiContentGenerator(
catalog: catalog,
systemInstruction: systemInstruction,
),
};
_genUiConversation = GenUiConversation(
a2uiMessageProcessor: _a2uiMessageProcessor,
contentGenerator: contentGenerator,
onSurfaceAdded: _handleSurfaceAdded,
onTextResponse: _onTextResponse,
onError: (error) {
genUiLogger.severe(
'Error from content generator',
error.error,
error.stackTrace,
);
},
);
}
void _handleSurfaceAdded(SurfaceAdded surface) {
if (!mounted) return;
setState(() {
_messages.add(MessageController(surfaceId: surface.surfaceId));
});
_scrollToBottom();
}
void _onTextResponse(String text) {
if (!mounted) return;
setState(() {
_messages.add(MessageController(text: 'AI: $text'));
});
_scrollToBottom();
}
@override
Widget build(BuildContext context) {
final String title = switch (aiBackend) {
AiBackend.googleGenerativeAi => 'Chat with Google Generative AI',
AiBackend.firebase => 'Chat with Firebase AI',
};
return Scaffold(
appBar: AppBar(title: Text(title)),
body: SafeArea(
child: Column(
children: [
Expanded(
child: ListView.builder(
controller: _scrollController,
itemCount: _messages.length,
itemBuilder: (context, index) {
final MessageController message = _messages[index];
return ListTile(
title: MessageView(message, _genUiConversation.host),
);
},
),
),
ValueListenableBuilder(
valueListenable: _genUiConversation.isProcessing,
builder: (_, isProcessing, _) {
if (!isProcessing) return Container();
return const Padding(
padding: EdgeInsets.all(8.0),
child: CircularProgressIndicator(),
);
},
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: TextField(
controller: _textController,
decoration: const InputDecoration(
hintText: 'Type your message...',
),
onSubmitted: (_) => _sendMessage(),
),
),
IconButton(
icon: const Icon(Icons.send),
onPressed: _sendMessage,
),
],
),
),
],
),
),
);
}
void _sendMessage() {
final String text = _textController.text;
if (text.isEmpty) {
return;
}
_textController.clear();
setState(() {
_messages.add(MessageController(text: 'You: $text'));
});
_scrollToBottom();
unawaited(_genUiConversation.sendRequest(UserMessage([TextPart(text)])));
}
void _scrollToBottom() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_scrollController.hasClients) {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
});
}
@override
void dispose() {
_genUiConversation.dispose();
super.dispose();
}
}