2a35fe5532
- Collapsible search in notes and tasks: magnifying glass in AppBar expands to a text field inline; close button resets the filter - Offline capture queue: failed quick-captures (NetworkException) are persisted to SharedPreferences and retried automatically on next successful submit or app start; badge shows pending count - App icon: book-with-sparkle logo from FabledAssistant SVG rendered at all Android densities with adaptive icon (indigo #6366f1 bg) - Dark mode subtitle fix: use colorScheme.onSurfaceVariant for note preview and conversation timestamp text - Remove swipe-to-delete (accidental deletions); long-press remains - Chat: SSE streaming reliability, polling fallback, title patching - Settings: theme toggle (system/light/dark) persisted to prefs - Notes: delete button in edit screen; body preview in list - Tasks: fix delete dialog context; description search support Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
131 lines
4.1 KiB
Dart
131 lines
4.1 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import '../models/conversation.dart';
|
|
import '../models/message.dart';
|
|
import 'api_client.dart';
|
|
|
|
class ChatApi {
|
|
final Dio _dio;
|
|
const ChatApi(this._dio);
|
|
|
|
Future<List<Conversation>> getConversations() async {
|
|
try {
|
|
final response = await _dio.get('/api/chat/conversations');
|
|
final data = response.data as Map<String, dynamic>;
|
|
final list = data['conversations'] as List<dynamic>;
|
|
return list
|
|
.map((e) => Conversation.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
Future<Conversation> createConversation(String title) async {
|
|
try {
|
|
final response = await _dio.post('/api/chat/conversations', data: {
|
|
'title': title,
|
|
});
|
|
return Conversation.fromJson(response.data as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
Future<void> deleteConversation(int id) async {
|
|
try {
|
|
await _dio.delete('/api/chat/conversations/$id');
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
// Returns the conversation metadata AND its messages in a single request.
|
|
Future<(Conversation, List<Message>)> getMessages(int conversationId) async {
|
|
try {
|
|
final response =
|
|
await _dio.get('/api/chat/conversations/$conversationId');
|
|
final conv = response.data as Map<String, dynamic>;
|
|
final conversation = Conversation.fromJson(conv);
|
|
final list = conv['messages'] as List<dynamic>? ?? [];
|
|
final messages = list
|
|
.map((e) => Message.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
return (conversation, messages);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
// Step 1: POST the user message — server starts background generation.
|
|
Future<void> sendMessage(int conversationId, String content) async {
|
|
try {
|
|
await _dio.post(
|
|
'/api/chat/conversations/$conversationId/messages',
|
|
data: {'content': content},
|
|
);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
// Step 2: GET the SSE stream and yield text chunks.
|
|
Stream<String> streamGeneration(int conversationId) async* {
|
|
try {
|
|
final response = await _dio.get(
|
|
'/api/chat/conversations/$conversationId/generation/stream',
|
|
options: Options(
|
|
responseType: ResponseType.stream,
|
|
receiveTimeout: Duration.zero, // SSE streams run indefinitely
|
|
sendTimeout: Duration.zero,
|
|
headers: {
|
|
'Accept': 'text/event-stream',
|
|
'Cache-Control': 'no-cache',
|
|
},
|
|
),
|
|
);
|
|
|
|
final stream = (response.data as ResponseBody).stream;
|
|
final buf = StringBuffer();
|
|
String currentEvent = '';
|
|
|
|
await for (final chunk in stream) {
|
|
buf.write(utf8.decode(chunk));
|
|
final raw = buf.toString();
|
|
final lines = raw.split('\n');
|
|
|
|
// Keep the last (potentially incomplete) line in the buffer.
|
|
buf.clear();
|
|
buf.write(lines.last);
|
|
|
|
for (final line in lines.sublist(0, lines.length - 1)) {
|
|
if (line.startsWith('event: ')) {
|
|
currentEvent = line.substring(7).trim();
|
|
} else if (line.startsWith('data: ')) {
|
|
final data = line.substring(6).trim();
|
|
if (data == '[DONE]') return;
|
|
if (currentEvent == 'done' || currentEvent == 'error') return;
|
|
|
|
// Parse as JSON if possible, otherwise yield raw text.
|
|
if (currentEvent == 'chunk' || currentEvent.isEmpty) {
|
|
try {
|
|
final obj = json.decode(data) as Map<String, dynamic>;
|
|
final text = obj['text'] as String? ?? '';
|
|
if (text.isNotEmpty) yield text;
|
|
} catch (_) {
|
|
if (data.isNotEmpty) yield data;
|
|
}
|
|
}
|
|
} else if (line.isEmpty) {
|
|
currentEvent = ''; // blank line = SSE event separator
|
|
}
|
|
}
|
|
}
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
}
|