parseProviderQueueEntries function
Parses the queue entries from an already-decoded JSON/YAML list.
Strict per-entry validation (candidate-listing errors), keep-first
dedup with a warning, and {"ref": ...} resolution through
resolveRef. An empty list is an explicit error ("queue empty" —
UT-parse-empty-single): a queue must have at least one entry.
Implementation
ParsedProviderQueue parseProviderQueueEntries(
Object? node, {
required String source,
({String kind, String? baseUrl, String model, String? keyName})? Function(
String name,
)?
resolveRef,
}) {
if (node is! List) {
throw ConfigException(
'$source: expected an array of '
'{"provider_type", "provider_config"} entries, got ${_typeName(node)}',
);
}
if (node.isEmpty) {
throw ConfigException(
'$source: the queue is empty — provide at least one entry or unset '
'the queue to use the legacy provider/model configuration',
);
}
final warnings = <String>[];
final entries = <ProviderQueueEntry>[];
final seen = <String>{};
for (var i = 0; i < node.length; i++) {
final item = node[i];
if (item is! Map) {
throw ConfigException(
'$source: queue entry #${i + 1} must be an object, '
'got ${_typeName(item)}',
);
}
final ProviderQueueEntry entry;
if (item['ref'] is String) {
if (resolveRef == null) {
throw ConfigException(
'$source: queue entry #${i + 1} uses {"ref": ...} but this '
'source has no customProviders registry to resolve against',
);
}
entry = ProviderQueueEntry.resolvedRef(
name: item['ref'] as String,
position: i + 1,
resolveRef: resolveRef,
overrides: item['provider_config'] is Map
? (item['provider_config'] as Map).cast<Object?, Object?>()
: null,
);
} else {
entry = ProviderQueueEntry.fromJsonMap(
item,
position: i + 1,
warnings: warnings,
);
}
// Dedup keep-first (UT-duplicates): the full serialized form is the
// key (E10), so the same provider with two models stays two entries.
if (!seen.add(entry.dedupKey)) {
warnings.add(
'$source: duplicate entry ${entry.label} (#${i + 1}) — '
'keeping the first occurrence',
);
continue;
}
entries.add(entry);
}
return ParsedProviderQueue(entries: entries, warnings: warnings);
}