fetchCodeMieModels function

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

Fetches the model ids from <apiBase>/llm_models?include_all=true (apiBase is <org>/code-assistant-api/v1), authenticating with the full cookie string as a Cookie: header. The response is a list of descriptors whose id lives in id, base_name, or deployment_name (first non-empty wins).

Implementation

Future<List<String>> fetchCodeMieModels(
  String apiBase,
  String cookie, {
  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: {'cookie': cookie},
        )
        .timeout(const Duration(seconds: 30));
    if (response.statusCode == 401 || response.statusCode == 403) {
      throw ConfigException(
        'CodeMie authentication failed — invalid or expired credentials '
        '(re-run /provider codemie sso)',
      );
    }
    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();
  }
}