validateModels function
Validates models against provider by sending a minimal, tool-free
request per model — confirming the API key works and each model id is
accepted. Errors are captured per model rather than thrown, so one bad model
does not hide the others.
Pure Dart (no dart:io): the caller injects the provider, so this is
unit-testable with a fake.
Implementation
Future<List<AiModelCheck>> validateModels(
AiProvider provider,
List<String> models,
) async {
final results = <AiModelCheck>[];
for (final model in models) {
final sw = Stopwatch()..start();
try {
await provider.chat(
messages: const [AiMessage.user('ping')],
tools: const [],
model: model,
);
sw.stop();
results.add(
AiModelCheck(model: model, ok: true, latencyMs: sw.elapsedMilliseconds),
);
} on AiProviderException catch (e) {
sw.stop();
results.add(AiModelCheck(model: model, ok: false, error: e.message));
} catch (e) {
sw.stop();
results.add(AiModelCheck(model: model, ok: false, error: '$e'));
}
}
return results;
}