import '../../core/exceptions.dart'; import '../api/chat_api.dart'; export '../api/chat_api.dart' show ChatStreamEvent, ChatTextChunk, ChatStatusUpdate, ChatToolCall; import '../local/database.dart'; import '../models/conversation.dart'; import '../models/message.dart'; /// Chat repository with read-through caching for the conversation list only. /// Messages and the live SSE stream are intentionally not cached — they are /// per-conversation, large, and require a live network anyway. class ChatRepository { final ChatApi _api; final FabledDatabase _db; const ChatRepository(this._api, this._db); Future> getConversations() async { try { final conversations = await _api.getConversations(); await _db.replaceAllConversations(conversations); return conversations; } on NetworkException { final cached = await _db.getAllConversations(); if (cached.isEmpty) rethrow; return cached; } } Future createConversation(String title) => _api.createConversation(title); Future deleteConversation(int id) async { await _api.deleteConversation(id); await _db.deleteConversation(id); } Future<(Conversation, List)> getMessages(int conversationId) => _api.getMessages(conversationId); Future sendMessage(int conversationId, String content) => _api.sendMessage(conversationId, content); Stream streamGeneration(int conversationId) => _api.streamGeneration(conversationId); }