Repository navigation
Expand file tree
/
Copy pathsuites.dart
More file actions
816 lines (747 loc) · 29.3 KB
/
Copy pathsuites.dart
File metadata and controls
816 lines (747 loc) · 29.3 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
// 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.
/// Loading the shared agent suites, and running their cases in each
/// protocol version this SDK implements.
///
/// The agent suites are written against v1.0. A harness runs each case twice,
/// once in each [SuiteVersion]. Lowered to v0.9, a harness lowers what a case
/// hands the SDK to v0.9 and lifts what the SDK returns back to v1.0 before
/// comparing:
///
/// - A `version` of `1.0` becomes `v0.9`, and one of `v0.9` becomes
/// `1.0`. A case written with `v0.9` means a version other than its
/// catalog's, which for this SDK is v1.0.
/// - A catalog document's `protocolVersion` of `1.0` becomes `0.9`, and any
/// function `returnType` of `validationResult` becomes `boolean`.
/// - Capabilities keyed by `1.0` are keyed by `v0.9`.
/// - v1.0 lets `createSurface` name no catalog and v0.9 does not, so a case
/// that never names a catalog has the first catalog of the case named in
/// each `createSurface`, and removed again from what the SDK returns.
/// - A v1.0 `createSurface` carrying `components` and `dataModel` is the
/// v0.9 `createSurface` followed by the `updateComponents` and
/// `updateDataModel` that fill it. Lifting joins them back for the formats
/// whose suites use the joined form.
/// - v0.9 `createSurface` states `sendDataModel` even when it is false,
/// which v1.0 leaves out.
/// - v1.0 writes a data binding as `@path` and a function call as `@call`,
/// where v0.9 writes `path` and `call`. Only components and the function a
/// `callRendererFunction` names are renamed: a `ChildList` template
/// (`componentId` beside `path`), the `updateDataModel` envelope's `path`
/// and data model values keep their keys in both versions.
library;
import 'dart:convert';
import 'dart:io';
import 'package:a2ui_agent/a2ui_agent.dart';
import 'package:a2ui_core/a2ui_core.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';
import 'manifest.dart';
export 'manifest.dart' show harnessName, recordCase;
/// Common metadata keys allowed at the top level of every conformance case.
const Set<String> caseMetadataKeys = {
'action',
'description',
'features',
'name',
};
/// Keys recognized inside an `expect_error` block by [expectThrowsCategory].
const Set<String> knownExpectErrorKeys = {'category', 'message'};
/// Keys recognized inside a catalog expectation map by
/// [SuiteVersion.expectCatalog].
const Set<String> knownCatalogExpectKeys = {
'catalog_id',
'components',
'functions',
'protocol_version',
};
/// Fails the current test if [obj] contains any keys not in [allowed].
void assertKnownKeys(
Map<String, Object?> obj,
Set<String> allowed,
String where,
) {
final List<String> unknown =
obj.keys.where((k) => !allowed.contains(k)).toList()..sort();
if (unknown.isNotEmpty) {
fail('Unhandled keys at $where: $unknown');
}
}
/// Registers a test that fails if any key in [entries] is absent from
/// [knownCaseNames].
void verifyNoUnusedEntries(
String label,
Set<String> knownCaseNames,
Iterable<String> entries,
) {
final List<String> unused =
entries.where((k) => !knownCaseNames.contains(k)).toList()..sort();
if (unused.isNotEmpty) {
test('$label: no unused skip entries', () {
fail('Unused skip entries in $label: $unused');
});
}
}
/// Reads the cases of the suite at [path], relative to `conformance/`, as
/// plain Dart maps, and registers a `tearDown` recorder when called inside a
/// test group.
List<Map<String, Object?>> loadSuite(String path) {
final String text = File('$conformanceRoot/$path').readAsStringSync();
Object? parsed;
try {
parsed = loadYaml(text);
} on YamlException {
parsed = loadYaml(
text.replaceAll(
RegExp(r'\\ud[89a-f][0-9a-f]{2}', caseSensitive: false),
r'\ufffd',
),
);
}
final YamlList list = switch (parsed) {
final YamlList items => items,
final YamlMap map => map['tests']! as YamlList,
_ => throw StateError('Unsupported suite YAML structure in $path.'),
};
final List<Map<String, Object?>> cases = [
for (final Object? node in list) _plain(node)! as Map<String, Object?>,
];
final Set<String> caseNames = {
for (final Map<String, Object?> testCase in cases)
(testCase['id'] ?? testCase['name'])! as String,
};
registerSuiteManifestRecorder(path, caseNames);
return cases;
}
/// The protocol version a harness runs a suite's cases in.
enum SuiteVersion {
/// Each case lowered to v0.9 before this SDK reads it, and what the SDK
/// returns lifted back to v1.0, as the library comment describes.
v0_9('lowered to v0.9'),
/// Each case as written, and what this SDK returns as it writes it.
v1_0('as written in v1.0');
const SuiteVersion(this.label);
/// How a group title names the version.
final String label;
bool get _lowers => this == v0_9;
/// The catalog a suite names by [entry]; see [catalogConfig].
CatalogConfig catalogConfig(Object? entry) =>
_catalogConfig(entry, lower: _lowers);
/// The catalogs a case names in [args]; see [caseCatalogs].
List<CatalogApi> catalogs(Map<String, Object?> args) =>
caseCatalogs(args, lower: _lowers);
/// The renderer capabilities [json] states.
A2uiRendererCapabilities capabilities(Map<String, Object?> json) => _lowers
? lowerCapabilities(json)
: A2uiRendererCapabilities.fromJson(json);
/// The prompt example at [path], relative to `conformance/`.
List<AgentToRendererMessage> example(String path) =>
_lowers ? loadExample(path) : loadExampleUnlowered(path);
/// [json], messages a case starts from.
List<AgentToRendererMessage> messages(List<Object?> json) => _lowers
? lowerMessages(json)
: [
for (final Object? message in json)
AgentToRendererMessage.fromJson(
Map<String, Object?>.from(message! as Map),
),
];
/// [text], a payload a model wrote, given [catalogId] in each
/// `createSurface` when it is lowered; see [lowerText].
String text(String text, {String? catalogId}) =>
_lowers ? lowerText(text, catalogId: catalogId) : text;
/// The catalog a harness names in each `createSurface` of [testCase] when
/// it is lowered; see [injectedCatalogId].
String? injectedCatalog(Map<String, Object?> testCase) =>
_lowers ? injectedCatalogId(testCase) : null;
/// [messages] as JSON in the vocabulary of the suites; see [liftMessages].
List<Object?> messagesJson(
List<AgentToRendererMessage> messages, {
bool join = false,
String? injectedCatalogId,
}) => _lowers
? liftMessages(messages, join: join, injectedCatalogId: injectedCatalogId)
: messages.map(messageAsWritten).toList();
/// [parts] in the vocabulary of the suites; see [liftParts].
List<Object?> partsJson(
List<StreamPart> parts, {
bool join = false,
String? injectedCatalogId,
}) => _lowers
? liftParts(parts, join: join, injectedCatalogId: injectedCatalogId)
: partsAsWritten(parts);
/// [properties], a component's property map, with reserved keys lifted to
/// v1.0 when the suite ran lowered.
Map<String, Object?> liftProperties(Map<String, Object?> properties) =>
_lowers
? _renameKeysIn(properties, lower: false)! as Map<String, Object?>
: properties;
/// Checks the catalog-level assertions of [expected] against [catalog];
/// see [expectCatalog]. As written, the version is checked too.
void expectCatalog(CatalogApi catalog, Map<String, Object?> expected) {
assertKnownKeys(expected, knownCatalogExpectKeys, 'expect.catalog');
_expectCatalog(catalog, expected);
if (expected['protocol_version'] case final String version when !_lowers) {
expect(catalog.protocolVersion?.jsonValue, version);
}
}
}
/// Compiles one block and checks its messages on their own.
///
/// A conformance `compile` case fixes the messages an agent sends for one
/// block, so they are checked the way [validatePayload] checks a payload: a
/// surface that the block updates without creating is assumed to exist.
List<AgentToRendererMessage> compileChecked(Parser parser, String content) {
final List<AgentToRendererMessage> messages = parser.compile(content);
validatePayload(parser.catalogs, messages);
return messages;
}
/// Suite-level error categories mapped onto this SDK's exception types.
final Map<String, Matcher> _errors = {
'ParseError': isA<A2uiParseError>(),
'ValidationError': isA<A2uiValidationError>(),
'CatalogError': isA<A2uiCatalogError>(),
};
/// Expects [body] to throw the error category (and optional message pattern)
/// of a case's `expect_error`.
void expectThrowsCategory(
void Function() body,
Object? expectError, {
String? reason,
}) {
final error = expectError! as Map<String, Object?>;
assertKnownKeys(error, knownExpectErrorKeys, 'expect_error');
final category = error['category']! as String;
final Matcher? categoryMatcher = _errors[category];
if (categoryMatcher == null) {
fail('Unknown error category: $category');
}
final Matcher matcher = switch (error['message']) {
final String pattern => allOf(
categoryMatcher,
isA<A2uiError>().having(
(e) => e.message,
'message',
matches(RegExp(pattern, caseSensitive: false)),
),
),
_ => categoryMatcher,
};
expect(body, throwsA(matcher), reason: reason);
}
/// The catalog a suite names by [entry]: a path relative to `conformance/`,
/// a map with the path under `catalog` and the transformers registered with
/// it under `transformers`, or a catalog document written in the case.
CatalogConfig _catalogConfig(Object? entry, {required bool lower}) =>
switch (entry) {
final String path => CatalogConfig(
lower ? loadCatalog(path) : loadCatalogUnlowered(path),
),
{'catalog': final String path} => CatalogConfig(
lower ? loadCatalog(path) : loadCatalogUnlowered(path),
transformers: [
for (final Object? transformer
in (entry as Map)['transformers'] as List<Object?>? ?? const [])
_transformer(transformer),
],
),
final Map<String, Object?> document => CatalogConfig(
lower
? _provide(
lowerCatalogDocument(document),
version: A2uiProtocolVersion.v0_9,
)
: _provide(document),
),
_ => throw ArgumentError.value(entry, 'entry', 'Not a catalog entry'),
};
/// The catalogs a case names in `args`, each after the transformers
/// registered with it.
List<CatalogApi> caseCatalogs(Map<String, Object?> args, {bool lower = true}) =>
[
if (args['catalog'] case final Object catalog)
_catalogConfig(catalog, lower: lower).transformedCatalog,
if (args['catalogs'] case final List<Object?> entries)
for (final Object? entry in entries)
_catalogConfig(entry, lower: lower).transformedCatalog,
];
bool _isVersionAtLeast1_0(Object? versionStr) =>
versionStr is String && compareVersions(versionStr, '1.0') >= 0;
/// Whether a catalog the case names in `args` declares v1.0 or later before it
/// is lowered, which turns on what this SDK does only for a v1.0+ agent.
bool caseDeclaresV1(Map<String, Object?> args) =>
[
if (args['catalog'] case final Object catalog) catalog,
...?(args['catalogs'] as List<Object?>?),
].any((Object? entry) {
final Object? path = entry is Map ? entry['catalog'] : entry;
if (path is! String) return false;
final document =
jsonDecode(File('$conformanceRoot/$path').readAsStringSync())
as Map<String, Object?>;
return _isVersionAtLeast1_0(document['protocolVersion']);
});
/// The `protocolVersion` the first catalog a case names in `args` declares,
/// before it is lowered, or null when the case names none.
String? caseCatalogVersion(Map<String, Object?> args) {
final Object? entry =
args['catalog'] ?? (args['catalogs'] as List?)?.firstOrNull;
final Object? path = entry is Map ? entry['catalog'] : entry;
if (path is! String) return null;
final document =
jsonDecode(File('$conformanceRoot/$path').readAsStringSync())
as Map<String, Object?>;
return document['protocolVersion']?.toString();
}
CatalogTransformer _transformer(Object? spec) => switch (spec) {
{'component_pruning': final List<Object?>? names} =>
ComponentPruningTransformer(names?.cast<String>()),
{'function_pruning': final List<Object?>? names} =>
FunctionPruningTransformer(names?.cast<String>()),
_ => throw ArgumentError.value(spec, 'spec', 'Unknown transformer'),
};
final Map<String, CatalogApi> _catalogCache = {};
final Map<String, CatalogApi> _unloweredCatalogCache = {};
/// The catalog document at [path], lowered to v0.9 and loaded by a
/// provider.
///
/// A document that names no catalog is given its path as its id, which a
/// provider supplies the same way an agent would.
CatalogApi loadCatalog(String path) => _catalogCache.putIfAbsent(path, () {
final Map<String, Object?> document = lowerCatalogDocument(
jsonDecode(File('$conformanceRoot/$path').readAsStringSync())
as Map<String, Object?>,
);
return InMemoryCatalogProvider(
document,
protocolVersion: A2uiProtocolVersion.v0_9,
catalogId: document.containsKey('catalogId') ? null : path,
).load();
});
/// The catalog document at [path], loaded as written by a provider.
///
/// A document that names no catalog is given its path as its id, and one that
/// states no version is given v1.0, the version the suites are written in.
CatalogApi loadCatalogUnlowered(String path) =>
_unloweredCatalogCache.putIfAbsent(
path,
() => _provide(
jsonDecode(File('$conformanceRoot/$path').readAsStringSync())
as Map<String, Object?>,
catalogId: path,
),
);
/// [document] loaded by a provider, given [catalogId] when it names no
/// catalog, and [version] when it states no version.
CatalogApi _provide(
Map<String, Object?> document, {
String? catalogId,
A2uiProtocolVersion version = A2uiProtocolVersion.v1_0,
}) => InMemoryCatalogProvider(
document,
catalogId: document.containsKey('catalogId') ? null : catalogId,
protocolVersion: document.containsKey('protocolVersion') ? null : version,
).load();
/// [document] with a `protocolVersion` of `1.0` or later stated as `0.9`, and
/// any `validationResult` function return type stated as `boolean`.
Map<String, Object?> lowerCatalogDocument(Map<String, Object?> document) {
if (!_isVersionAtLeast1_0(document['protocolVersion'])) {
return {...document};
}
return {
...document,
'protocolVersion': '0.9',
if (document['functions'] case final Map<Object?, Object?> functions)
'functions': {
for (final MapEntry<Object?, Object?> entry in functions.entries)
entry.key.toString(): switch (entry.value) {
final Map<Object?, Object?> fn => <String, Object?>{
for (final MapEntry<Object?, Object?> fnEntry in fn.entries)
if (fnEntry.key != 'allowedCallers' &&
fnEntry.key != 'requiresUserActivation')
fnEntry.key.toString(): fnEntry.value,
if (fn['returnType'] == 'validationResult')
'returnType': 'boolean',
if (fn['properties'] case final Map<Object?, Object?> props
when (props['returnType'] as Map?)?['const'] ==
'validationResult')
'properties': {
...props,
'returnType': {'const': 'boolean'},
},
},
final Object? other => other,
},
},
};
}
/// [version] swapped between the suite's version and this SDK's, in either
/// spelling; any other value is returned as it is.
Object? swapVersion(Object? version) => switch (version) {
'1.0' || 'v1.0' => 'v0.9',
'v0.9' => '1.0',
_ => version,
};
// A case may write the field in curly quotes, which the parser repairs.
final RegExp _versionField = RegExp(
'([“"]version[”"]\\s*:\\s*[“"])(v?1\\.0|v0\\.9)([”"])',
);
/// [text], a payload a model wrote, lowered to v0.9: each `version` swapped,
/// each `@path` and `@call` key written without its `@`, and each
/// `createSurface` given [catalogId] when it is not null.
String lowerText(String text, {String? catalogId}) {
final String lowered = text
.replaceAllMapped(
_versionField,
(m) => '${m[1]}${swapVersion(m[2])}${m[3]}',
)
.replaceAllMapped(_reservedKeyField, (m) => '"${m[1]}"');
if (catalogId == null) return lowered;
return lowered.replaceAll(
RegExp(r'"createSurface"\s*:\s*\{'),
'"createSurface": {"catalogId": ${jsonEncode(catalogId)}, ',
);
}
// A single `@`: a doubled one escapes a literal key and stays as it is.
final RegExp _reservedKeyField = RegExp(r'"@(path|call)"(?=\s*:)');
/// The catalog a harness names in each `createSurface` of a case that names
/// none, or null when the case names one itself.
String? injectedCatalogId(Map<String, Object?> testCase) {
if (jsonEncode(testCase).contains('catalogId')) return null;
final List<CatalogApi> catalogs = caseCatalogs(
testCase['args']! as Map<String, Object?>,
);
return catalogs.isEmpty ? null : catalogs.first.id;
}
/// [messages], written against v1.0 as JSON, lowered to v0.9 messages.
///
/// A `createSurface` naming no catalog is given [defaultCatalogId].
List<AgentToRendererMessage> lowerMessages(
List<Object?> messages, {
String? defaultCatalogId,
}) {
final lowered = <Map<String, Object?>>[];
for (final message in messages) {
final Map<String, Object?> json = _renameReservedKeys(
Map<String, Object?>.from(message! as Map),
lower: true,
);
json['version'] = swapVersion(json['version']);
if (json['createSurface'] case final Map<Object?, Object?> body) {
final create = Map<String, Object?>.from(body);
final Object? components = create.remove('components');
final Object? dataModel = create.remove('dataModel');
if (defaultCatalogId != null) {
create.putIfAbsent('catalogId', () => defaultCatalogId);
}
final Object? surfaceId = create['surfaceId'];
lowered.add({'version': json['version'], 'createSurface': create});
if (components != null) {
lowered.add({
'version': json['version'],
'updateComponents': {
'surfaceId': surfaceId,
'components': components,
},
});
}
if (dataModel != null) {
lowered.add({
'version': json['version'],
'updateDataModel': {
'surfaceId': surfaceId,
'path': '/',
'value': dataModel,
},
});
}
} else {
lowered.add(json);
}
}
return AgentToRendererMessage.parseAll(
lowered,
protocolVersion: A2uiProtocolVersion.v0_9,
).messages;
}
/// The prompt example at [path], relative to `conformance/`, lowered to
/// v0.9.
List<AgentToRendererMessage> loadExample(String path) => lowerMessages(
jsonDecode(File('$conformanceRoot/$path').readAsStringSync())
as List<Object?>,
);
/// The prompt example at [path], relative to `conformance/`, loaded as written
/// without lowering to v0.9.
List<AgentToRendererMessage> loadExampleUnlowered(String path) =>
AgentToRendererMessage.parseAll([
for (final Object? item
in jsonDecode(File('$conformanceRoot/$path').readAsStringSync())
as List<Object?>)
Map<String, Object?>.from(item! as Map),
], protocolVersion: A2uiProtocolVersion.v1_0).messages;
/// [messages] as JSON in the v1.0 shape the suites are written in.
///
/// When [join] is true, a surface that v0.9 creates empty and then fills
/// becomes one `createSurface` carrying its components and initial data
/// model. [injectedCatalogId], if given, is removed from each
/// `createSurface` that names it.
List<Object?> liftMessages(
List<AgentToRendererMessage> messages, {
bool join = false,
String? injectedCatalogId,
}) {
final lifted = <Map<String, Object?>>[];
for (final message in messages) {
final Map<String, Object?> json = _renameReservedKeys(
message.toJson(),
lower: false,
);
final create = lifted.lastOrNull?['createSurface'] as Map<String, Object?>?;
final Object? surfaceId = create?['surfaceId'];
switch (json) {
case {
'updateComponents': {
'surfaceId': final Object? id,
'components': final Object? components,
},
}
when join && id == surfaceId && !create!.containsKey('components'):
create['components'] = components;
case {
'updateDataModel': {
'surfaceId': final Object? id,
'path': '/',
'value': final Object? value,
},
}
when join && id == surfaceId && !create!.containsKey('dataModel'):
create['dataModel'] = value;
case {'createSurface': final Map<String, Object?> create}:
lifted.add({
'version': swapVersion(json['version']),
'createSurface': {...create}
..removeWhere(
(key, value) =>
(key == 'sendDataModel' && value == false) ||
(key == 'catalogId' && value == injectedCatalogId),
),
});
default:
lifted.add({...json, 'version': swapVersion(json['version'])});
}
}
return lifted;
}
/// The v1.0 spelling of each key a data binding or function call reserves,
/// mapped to its v0.9 spelling.
const Map<String, String> _reservedKeys = {'@path': 'path', '@call': 'call'};
const Map<String, String> _componentMixinKeys = {
'@checks': 'checks',
'@accessibility': 'accessibility',
};
/// [message] with the reserved keys of its components, and of the function
/// a `callRendererFunction` names, renamed to v0.9 when [lower] is true and
/// to v1.0 otherwise.
Map<String, Object?> _renameReservedKeys(
Map<String, Object?> message, {
required bool lower,
}) => {
for (final MapEntry<String, Object?> entry in message.entries)
entry.key: switch (entry.value) {
final Map<String, Object?> body => {
for (final MapEntry<String, Object?> field in body.entries)
field.key: field.key == 'components' || field.key == 'callFunction'
? _renameKeysIn(field.value, lower: lower)
: field.value,
},
final Object? value => value,
},
};
/// [node] with each reserved key renamed, except in a `ChildList` template,
/// whose `path` is unprefixed in both versions.
Object? _renameKeysIn(Object? node, {required bool lower}) => switch (node) {
final List<Object?> list => [
for (final Object? item in list) _renameKeysIn(item, lower: lower),
],
final Map<String, Object?> map => {
for (final MapEntry<String, Object?> entry in map.entries)
(map.containsKey('componentId')
? entry.key
: _renameKey(
entry.key,
lower,
isComponentRoot:
map.containsKey('id') && map.containsKey('component'),
)): _renameKeysIn(
entry.value,
lower: lower,
),
},
_ => node,
};
String _renameKey(String key, bool lower, {bool isComponentRoot = false}) {
if (isComponentRoot) {
final String? mixinKey = (lower
? _componentMixinKeys
: _liftedComponentMixinKeys)[key];
if (mixinKey != null) return mixinKey;
}
return (lower ? _reservedKeys : _liftedKeys)[key] ?? key;
}
final Map<String, String> _liftedKeys = {
for (final MapEntry<String, String> entry in _reservedKeys.entries)
entry.value: entry.key,
};
final Map<String, String> _liftedComponentMixinKeys = {
for (final MapEntry<String, String> entry in _componentMixinKeys.entries)
entry.value: entry.key,
};
/// [parts] in the vocabulary of the suites, with messages lifted to v1.0.
List<Object?> liftParts(
List<StreamPart> parts, {
bool join = false,
String? injectedCatalogId,
}) => [
for (final StreamPart part in parts)
switch (part) {
TextPart(:final String text) => {'text': text},
A2uiPart(:final List<AgentToRendererMessage> a2ui) => {
'a2ui': liftMessages(
a2ui,
join: join,
injectedCatalogId: injectedCatalogId,
),
},
ErrorPart(:final A2uiError error) => errorPartJson(error),
},
];
/// Renderer capabilities written against v1.0, lowered to v0.9.
A2uiRendererCapabilities lowerCapabilities(Map<String, Object?> json) =>
A2uiRendererCapabilities.fromJson({
for (final MapEntry<String, Object?> entry in json.entries)
'${swapVersion(entry.key)}': entry.value,
});
/// Checks the catalog-level assertions of [expected] against [catalog],
/// except its `protocol_version`, which a lowered catalog does not have.
void _expectCatalog(CatalogApi catalog, Map<String, Object?> expected) {
if (expected['catalog_id'] case final String id) expect(catalog.id, id);
if (expected['components'] case final List<Object?> names) {
expect(catalog.components.keys, unorderedEquals(names));
}
if (expected['functions'] case final List<Object?> names) {
expect(catalog.functions.keys, unorderedEquals(names));
}
}
/// Checks a prompt snippet against `expect_file`, `expect_contains` and
/// `expect_absent`, or the `prompt_snippet_` forms of them, in [expected].
void expectSnippet(String snippet, Map<String, Object?> expected) {
if (expected['expect_file'] case final String path) {
expect(snippet, File('$conformanceRoot/$path').readAsStringSync());
}
for (final key in ['expect_contains', 'prompt_snippet_contains']) {
for (final Object? text in expected[key] as List<Object?>? ?? const []) {
expect(snippet, contains(text));
}
}
for (final key in ['expect_absent', 'prompt_snippet_absent']) {
for (final Object? text in expected[key] as List<Object?>? ?? const []) {
expect(snippet, isNot(contains(text)));
}
}
}
/// [message] as JSON, less the `sendDataModel: false` a `createSurface`
/// states, which the suites leave out.
Map<String, Object?> messageAsWritten(AgentToRendererMessage message) => {
for (final MapEntry<String, Object?> entry in message.toJson().entries)
entry.key: switch (entry.value) {
final Map<String, Object?> body when entry.key == 'createSurface' => {
...body,
}..removeWhere((key, value) => key == 'sendDataModel' && value == false),
final Object? value => value,
},
};
/// [parts] in the vocabulary of the suites, with messages as this SDK writes
/// them; see [messageAsWritten].
List<Object?> partsAsWritten(List<StreamPart> parts) => [
for (final StreamPart part in parts)
switch (part) {
TextPart(:final String text) => {'text': text},
A2uiPart(:final List<AgentToRendererMessage> a2ui) => {
'a2ui': a2ui.map(messageAsWritten).toList(),
},
ErrorPart(:final A2uiError error) => errorPartJson(error),
},
];
/// An [ErrorPart] in the vocabulary of the suites: the category of its
/// error.
Map<String, Object?> errorPartJson(A2uiError error) => {
'error': {
'category': _errors.entries
.firstWhere((entry) => entry.value.matches(error, {}))
.key,
},
};
/// [expected] with `is_final: true` dropped, since a part is final unless it
/// says otherwise.
Object? withoutFinalFlags(Object? expected) => switch (expected) {
List<Object?>() => expected.map(withoutFinalFlags).toList(),
Map<String, Object?>() => {
for (final MapEntry<String, Object?> entry in expected.entries)
if (!(entry.key == 'is_final' && entry.value == true))
entry.key: entry.value,
},
_ => expected,
};
/// [part] in the vocabulary of the suites.
Map<String, Object?> rawPartJson(RawResponsePart part) => switch (part) {
TextPart(:final String text) => {'text': text},
RawA2uiPart(:final String a2uiRaw, :final bool isFinal) => {
'a2ui_raw': a2uiRaw,
if (!isFinal) 'is_final': false,
},
};
/// The raw parts a `wrap` case supplies.
List<RawResponsePart> rawParts(List<Object?> parts) => [
for (final Object? part in parts)
switch (part) {
{'text': final String text} => TextPart(text),
{'a2ui_raw': final String raw} => RawA2uiPart(
raw,
isFinal: (part as Map)['is_final'] as bool? ?? true,
),
_ => throw ArgumentError.value(part, 'part'),
},
];
/// Converts YAML nodes into plain Dart maps, lists and scalars.
Object? _plain(Object? node) => switch (node) {
YamlMap() => {
for (final MapEntry<Object?, Object?> entry in node.entries)
entry.key.toString(): _plain(entry.value),
},
YamlList() => node.map(_plain).toList(),
_ => node,
};
/// The `conformance/` directory, found by walking up from the working
/// directory.
final String conformanceRoot = () {
Directory dir = Directory.current;
while (!File(
'${dir.path}/conformance/conformance_schema.json',
).existsSync()) {
if (dir.parent.path == dir.path) {
throw StateError('No conformance/ directory above ${Directory.current}.');
}
dir = dir.parent;
}
return '${dir.path}/conformance';
}();