available method

Future<List<SyniModelOption>> available({
  1. bool forceRefresh = false,
})

Available models. Fetches /v1/models on first call (when a base URL is configured), caches the result, and falls back to bundled on any failure. Pass forceRefresh: true to re-fetch.

Implementation

Future<List<SyniModelOption>> available({bool forceRefresh = false}) async {
  if (_cache != null && !forceRefresh) return _cache!;
  if (_baseUrl == null) return _cache = bundled;
  try {
    final headers = <String, String>{'Accept': 'application/json'};
    final token = await _authToken?.call();
    if (token != null) headers['Authorization'] = 'Bearer $token';

    final resp = await _http.get(
      Uri.parse('$_baseUrl/v1/models'),
      headers: headers,
    );
    if (resp.statusCode != 200) return _cache = bundled;

    final decoded = jsonDecode(resp.body) as Map<String, dynamic>;
    final models = (decoded['models'] as List)
        .whereType<Map<String, dynamic>>()
        .map(SyniModelOption.fromJson)
        .toList();
    return _cache = models.isEmpty ? bundled : models;
  } catch (_) {
    // Offline / malformed / network error — degrade to the bundled set.
    return _cache = bundled;
  }
}