Repository navigation
Expand file tree
/
Copy pathchat_session.dart
More file actions
414 lines (366 loc) · 13.8 KB
/
Copy pathchat_session.dart
File metadata and controls
414 lines (366 loc) · 13.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// 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 'dart:async';
import 'dart:convert';
import 'package:a2ui_agent/a2ui_agent.dart';
import 'package:a2ui_core/a2ui_core.dart';
import 'package:a2ui_flutter/a2ui_flutter.dart';
import 'package:a2ui_flutter/basic_catalog.dart';
import 'package:flutter/foundation.dart';
import 'package:url_launcher/url_launcher.dart';
import 'llm_client.dart';
/// What the app tells the model, ahead of the agent SDK's prompt snippet.
const String appInstructions = '''
You are the assistant of a chat app that renders A2UI. Answer in a sentence or
two, and show forms, choices, lists and cards as A2UI surfaces.
- Give each new surface a new surfaceId, and set "sendDataModel": true on a
surface with inputs.
- Bind each input to a data model path. Give a submit button an event action
whose context reads those paths.
- When the user acts on a surface, the next user turn is JSON whose "a2ui"
holds the renderer's v1.0 messages, {"messages": [...]}. A message is an
action, with its name and resolved context, or an error the renderer met.
- Once a surface is created with sendDataModel, a user turn carries
"metadata": {"a2uiRendererDataModel": ...}, which holds the data model of each
such surface: beside "a2ui" in the JSON of an action or error turn, or as
JSON after the text of a typed turn.
- Answer an action by updating its surface, or with a new surface. Answer an
error by correcting the surface it names.
''';
/// The basic catalog, which opens URLs with url_launcher when the user acts.
final CatalogImplementation _catalog = basicCatalog(openUrl: launchUrl);
/// What a [ChatEntry] shows.
enum ChatEntryKind {
/// Text the user typed.
user,
/// Text the model wrote outside its A2UI payloads.
model,
/// A surface the model created.
surface,
/// A turn the app sent the model for the user, named by its actions and
/// errors.
sent,
/// An error the renderer met, or a failed request to the model.
error,
}
/// One entry of the conversation.
class ChatEntry {
ChatEntry(this.kind, this.text) : surface = null;
ChatEntry.surface(SurfaceModel<ComponentImplementation> this.surface)
: kind = ChatEntryKind.surface,
text = '';
final ChatEntryKind kind;
/// The entry's text, which grows while the model streams it.
String text;
final SurfaceModel<ComponentImplementation>? surface;
}
/// A conversation in which a model answers with text and A2UI surfaces.
///
/// The model is prompted for the basic catalog the renderer supports. Its
/// reply is parsed and rendered as it streams. Actions on its surfaces, and
/// errors the renderer meets, go back to it as the next user turn.
class ChatSession extends ChangeNotifier {
/// [inferenceFormatFactory] is the format the model is prompted for and its
/// replies are read in, Direct JSON unless another is given.
ChatSession({
required this.client,
InferenceFormatFactory inferenceFormatFactory = defaultFormat,
}) : _agent =
A2uiGenerator(
catalogs: [CatalogConfig(_catalog)],
inferenceFormatFactory: inferenceFormatFactory,
).createProcessor(
A2uiRendererCapabilities.forCatalogIds([
BasicCatalog.v1_0Id,
], version: A2uiProtocolVersion.v1_0),
) {
processor = MessageProcessor(
catalogs: [_catalog],
defaultVersion: A2uiProtocolVersion.v1_0,
validationConfig: ValidationConfig.relaxed,
outboundListener: _sendOutbound,
unifiedOutbound: true,
);
processor.model.onSurfaceCreated.subscribe(_addSurface);
processor.model.onSurfaceDeleted.subscribe(_removeSurface);
}
final LlmClient client;
/// The renderer's message processor, with the checks that a half-streamed
/// surface would fail turned off. Its actions, calls of the model's
/// functions, and responses to the model's calls go to the model as the next
/// turn.
late final MessageProcessor<ComponentImplementation> processor;
static final String _version = A2uiProtocolVersion.v1_0.jsonValue;
/// The format a session uses unless given another: Direct JSON, with text
/// and labels shown as they stream.
static const InferenceFormatFactory defaultFormat = DirectJsonFormatFactory(
progressiveKeys: {'text', 'label'},
);
final A2uiRequestProcessor _agent;
/// The surfaces the model's replies have created, as the agent SDK checks
/// each reply against them.
late final RendererState _state = _agent.createState();
/// [appInstructions] and the agent SDK's prompt snippet for the catalogs
/// the renderer supports.
late final String systemPrompt = '$appInstructions\n${_agent.promptSnippet}';
/// The conversation as the user sees it.
final List<ChatEntry> entries = [];
final List<ChatTurn> _turns = [];
Future<void> _queue = Future.value();
int _queued = 0;
StreamIterator<String>? _reading;
bool _disposed = false;
/// Whether a turn is waiting for, or reading, the model's reply.
bool get busy => _queued > 0;
/// Sends [text] to the model with the renderer data model as it is when the
/// turn starts, and completes once its reply is applied.
Future<void> send(String text) {
entries.add(ChatEntry(ChatEntryKind.user, text));
_errorTurns = 0;
return _enqueue(() {
final Map<String, Object?> metadata = _metadata();
return _runTurn(
metadata.isEmpty ? text : '$text\n\n${jsonEncode(metadata)}',
);
});
}
/// Runs [turn] once the turns queued before it end, and shows what it
/// throws as an error. Does nothing once the session is disposed.
Future<void> _enqueue(Future<void> Function() turn) {
if (_disposed) return Future.value();
_queued++;
_notify();
return _queue = _queue
.then((_) => turn())
.catchError((Object error) {
_addEntry(ChatEntryKind.error, 'The turn failed: $error');
})
.whenComplete(() {
_queued--;
_notify();
});
}
ChatEntry? _modelText;
Future<void> _runTurn(String userTurn) async {
if (_disposed) return;
_turns.add((fromUser: true, text: userTurn));
_reported.clear();
ResponseStream? stream = _agent.openStream(state: _state);
final reply = StringBuffer();
var complete = true;
_modelText = null;
final StreamIterator<String> chunks = _reading = StreamIterator(
client.generate(systemPrompt, List.of(_turns)),
);
try {
while (await chunks.moveNext()) {
if (_disposed) break;
reply.write(chunks.current);
stream = _read(stream, (s) => s.feed(chunks.current));
_notify();
}
} on Object catch (error) {
complete = false;
entries.add(ChatEntry(ChatEntryKind.error, 'The request failed: $error'));
}
_reading = null;
if (_disposed) return;
if (complete) {
_read(stream, (s) => s.close());
} else {
// What closing the cut-off reply throws adds nothing to the failure
// already shown.
try {
stream?.close();
} on Object catch (_) {}
}
if (reply.isEmpty) {
_turns.removeLast();
if (complete) {
entries.add(ChatEntry(ChatEntryKind.error, 'The model gave no reply.'));
}
} else {
_turns.add((fromUser: false, text: '$reply'));
}
}
/// Shows the text and processes the messages that [read] gets from
/// [stream], and returns [stream], or null once it rejects part of the
/// reply. That failure is reported, and the rest of the reply is not read.
ResponseStream? _read(
ResponseStream? stream,
List<ResponsePart> Function(ResponseStream stream) read,
) {
if (stream == null) return null;
try {
for (final ResponsePart part in read(stream)) {
switch (part) {
case TextPart(:final String text):
_showText(text);
case A2uiPart(:final List<AgentToRendererMessage> a2ui):
a2ui.forEach(_process);
}
}
return stream;
} on Object catch (error) {
_reportFailure(error, '');
return null;
}
}
void _showText(String text) {
(_modelText ??= _addEntry(ChatEntryKind.model)).text += text;
}
void _process(AgentToRendererMessage message) {
_modelText = null;
try {
processor.processMessages(AgentToRendererMessagePayload.of(message));
} on Object catch (error) {
final String surfaceId = switch (message) {
CreateSurfaceMessage(:final String surfaceId) ||
UpdateComponentsMessage(:final String surfaceId) ||
UpdateDataModelMessage(:final String surfaceId) ||
DeleteSurfaceMessage(:final String surfaceId) => surfaceId,
_ => '',
};
_reportFailure(error, surfaceId);
}
}
ChatEntry _addEntry(ChatEntryKind kind, [String text = '']) {
final entry = ChatEntry(kind, text);
entries.add(entry);
return entry;
}
void _addSurface(SurfaceModel<ComponentImplementation> surface) {
surface.onError.subscribe(_report);
entries.add(ChatEntry.surface(surface));
_modelText = null;
_notify();
}
void _removeSurface(String surfaceId) {
entries.removeWhere((entry) => entry.surface?.id == surfaceId);
_notify();
}
/// Sends [message], which [processor] sends to the agent, to the model as
/// the next turn. An [ErrorMessage] is dropped, as [processor] also throws
/// that error and the session reports it.
void _sendOutbound(RendererToAgentMessage message) {
if (message is ErrorMessage) return;
_sendMessage(message);
}
/// Sends [message] to the model as the next turn.
void _sendMessage(RendererToAgentMessage message) {
_errorTurns = 0;
final String turn = _clientTurn([message]);
unawaited(_enqueue(() => _runTurn(turn)));
}
/// [messages] as a v1.0 payload under `a2ui`, with [_metadata] beside it,
/// written as a user turn.
String _clientTurn(List<RendererToAgentMessage> messages) {
final String turn = jsonEncode({
'a2ui': RendererToAgentMessagePayload(messages).toJson(),
..._metadata(),
});
_addEntry(
ChatEntryKind.sent,
[
for (final RendererToAgentMessage message in messages)
switch (message) {
ActionMessage(:final A2uiRendererAction action) =>
'action ${action.name}',
ErrorMessage(:final A2uiRendererError error) =>
'error ${error.code}',
CallAgentFunctionMessage(:final String functionCallId) =>
'function call $functionCallId',
RendererFunctionResponseMessage(
:final A2uiFunctionResponse response,
) =>
'function response ${response.functionCallId}',
_ => message.runtimeType.toString(),
},
].join(', '),
);
return turn;
}
/// The renderer data model as transport metadata, or nothing when no surface
/// was created with `sendDataModel`.
Map<String, Object?> _metadata() {
final Map<String, dynamic>? dataModel = processor.getRendererDataModel();
return {
if (dataModel != null) 'metadata': {'a2uiRendererDataModel': dataModel},
};
}
final Set<String> _reported = {};
final List<A2uiRendererError> _unsent = [];
int _errorTurns = 0;
/// How many turns of errors in a row go to the model before the user's
/// next turn.
static const int maxErrorTurns = 2;
/// Reports [error], thrown reading or processing a message of the surface
/// [surfaceId], under its code and with its path, or as `UNKNOWN_ERROR` if
/// it is not an [A2uiError] or is a validation failure with no path.
///
/// [surfaceId] is empty for an error thrown reading the reply, which comes
/// with no message to take the id from.
void _reportFailure(Object error, String surfaceId) {
final A2uiError failure = error is A2uiError ? error : A2uiError('$error');
final String? path = failure is A2uiValidationError ? failure.path : null;
final bool pathless =
failure.code == A2uiRendererError.validationFailedCode &&
(path == null || path.isEmpty);
_report(
A2uiRendererError(
code: pathless ? 'UNKNOWN_ERROR' : failure.code,
surfaceId: surfaceId,
message: failure.message,
path: pathless ? null : path,
),
);
}
/// Shows [error] and sends it to the model once the current turn ends.
///
/// An error already reported since the current turn started is ignored.
void _report(A2uiRendererError error) {
if (!_reported.add(jsonEncode(error))) return;
_addEntry(
ChatEntryKind.error,
'${error.code} on "${error.surfaceId}": ${error.message}',
);
_modelText = null;
_unsent.add(error);
_notify();
unawaited(_enqueue(_sendErrors));
}
Future<void> _sendErrors() async {
if (_disposed || _unsent.isEmpty) return;
final List<RendererToAgentMessage> errors = [
for (final A2uiRendererError error in _unsent)
ErrorMessage(version: _version, error: error),
];
_unsent.clear();
if (_errorTurns >= maxErrorTurns) return;
_errorTurns++;
await _runTurn(_clientTurn(errors));
}
void _notify() {
if (!_disposed) notifyListeners();
}
/// Stops reading the current reply and runs no turn after it.
@override
void dispose() {
_disposed = true;
unawaited(_reading?.cancel());
processor.dispose();
_state.dispose();
super.dispose();
}
}