upsert method

Future<void> upsert({
  1. required AiProvider provider,
  2. String? apiKey,
  3. String? model,
  4. AiAuthMode authMode = AiAuthMode.apiKey,
  5. String? oauthTokenId,
})

Upsert a row for (currentUser, provider). Caller is responsible for having recorded consent first via recordConsent.

Implementation

Future<void> upsert({
  required AiProvider provider,
  String? apiKey,
  String? model,
  AiAuthMode authMode = AiAuthMode.apiKey,
  String? oauthTokenId,
}) async {
  final userId = _client.auth.currentUser?.id;
  if (userId == null) {
    throw StateError('Cannot save AI settings: not signed in.');
  }
  await _client.from('user_ai_settings').upsert({
    'user_id': userId,
    'provider': provider.name,
    'api_key': apiKey,
    'model': model,
    'auth_mode': authMode == AiAuthMode.oauth ? 'oauth' : 'apiKey',
    'oauth_token_id': oauthTokenId,
  }, onConflict: 'user_id,provider');
}