fetchCodeMieModelsWithJwt function

Future<List<String>> fetchCodeMieModelsWithJwt(
  1. String apiBase,
  2. String jwtToken, {
  3. Client? client,
})

Fetches the model ids from <apiBase>/llm_models?include_all=true using JWT Bearer authorization (Authorization: Bearer <jwtToken>).

Implementation

Future<List<String>> fetchCodeMieModelsWithJwt(
  String apiBase,
  String jwtToken, {
  http.Client? client,
}) async {
  final httpClient = client ?? http.Client();
  final ownsClient = client == null;
  try {
    final response = await httpClient
        .get(
          Uri.parse('$apiBase/llm_models?include_all=true'),
          headers: {'authorization': 'Bearer $jwtToken'},
        )
        .timeout(const Duration(seconds: 30));
    if (response.statusCode == 401 || response.statusCode == 403) {
      throw ConfigException(
        'CodeMie authentication failed — invalid or expired JWT token '
        '(re-run /provider codemie jwt)',
      );
    }
    if (response.statusCode < 200 || response.statusCode >= 300) {
      throw ConfigException(
        'CodeMie models request failed (${response.statusCode})',
      );
    }
    final decoded = jsonDecode(response.body);
    if (decoded is! List) return const [];
    return [
      for (final model in decoded)
        if (model is Map) ?_modelId(model),
    ];
  } finally {
    if (ownsClient) httpClient.close();
  }
}