aiDatasource function
- @Riverpod(keepAlive: true)
The active AI datasource, based on the user's selected provider.
On web, direct calls to provider APIs (api.anthropic.com etc.) are blocked by CORS preflight, so we unconditionally route through the Supabase ai-proxy Edge Function. This mirrors the listModelsViaProxy precedent and ignores useAiProxyProvider — CORS is a transport limitation, not a user preference. Stateless callers (settings test connection, chat fallback) get a default explore-mode proxy with no topic.
Implementation
@Riverpod(keepAlive: true)
AiDatasource aiDatasource(Ref ref) {
final providerAsync = ref.watch(selectedAiProviderProvider);
final provider = providerAsync.valueOrNull ?? AiProvider.claude;
if (kIsWeb) {
return _buildProxyDatasource(
ref,
provider,
const ChatParams(mode: 'explore'),
);
}
return _buildDirectDatasource(ref, provider);
}