listModels method

  1. @override
Future<List<String>> listModels(
  1. String apiKey
)
override

Models suitable for the broker's primary capability, newest first. Returns an empty list for an unset / invalid key, or for providers that have no user-selectable model (e.g. Google Translate v2).

Implementation

@override
Future<List<String>> listModels(String apiKey) async {
  if (apiKey.isEmpty) return const [];
  final res = await _http.get(
    Uri.parse('$_baseUrl/models'),
    headers: {
      'x-api-key': apiKey,
      'anthropic-version': _apiVersion,
    },
  ).timeout(_timeout);
  if (res.statusCode >= 400) return const [];
  final body = jsonDecode(res.body) as Map<String, Object?>;
  final data = body['data'] as List<Object?>? ?? const [];
  final ids = <String>[];
  for (final m in data) {
    if (m is! Map<String, Object?>) continue;
    final modelId = m['id'] as String?;
    if (modelId != null) ids.add(modelId);
  }
  // Newest first — `claude-opus-4-7` should land before
  // `claude-3-5-sonnet`. The API already returns this order; resort
  // with a digit-aware key so that `3-10` ranks above `3-7`
  // (plain lex would put `3-7` first because `'7' > '1'`).
  ids.sort((a, b) => _compareNatural(b, a));
  return ids;
}