saveMessage method

Future<void> saveMessage(
  1. String sessionId,
  2. String role,
  3. String content, {
  4. String? id,
})

Persist a conversation_messages row. When id is supplied (offline- outbox path), uses upsert(onConflict: 'id', ignoreDuplicates: true) so a replay of an already-applied write is a no-op rather than a duplicate-key error. Without id, the migration's DEFAULT gen_random_uuid() fires server-side as before.

Implementation

Future<void> saveMessage(
  String sessionId,
  String role,
  String content, {
  String? id,
}) async {
  final row = <String, dynamic>{
    'session_id': sessionId,
    'role': role,
    'content': content,
  };
  if (id != null) row['id'] = id;

  if (id != null) {
    await _client
        .from('conversation_messages')
        .upsert(row, onConflict: 'id', ignoreDuplicates: true);
  } else {
    await _client.from('conversation_messages').insert(row);
  }
}