From 8692bf3bb6e36d3b61fc02c31bdf1749bbb10f52 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Tue, 26 Aug 2025 11:21:26 +0530 Subject: [PATCH] refactor: enhance API service to include non-image files in requests and improve chat message synchronization logic --- lib/core/services/api_service.dart | 8 ++++++++ lib/features/chat/providers/chat_providers.dart | 10 ++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/core/services/api_service.dart b/lib/core/services/api_service.dart index f4cdb66..12b5e59 100644 --- a/lib/core/services/api_service.dart +++ b/lib/core/services/api_service.dart @@ -2442,6 +2442,14 @@ class ApiService { debugPrint('DEBUG: Including tool_ids in SSE request: $toolIds'); } + // Include non-image files at the top level as expected by Open WebUI + if (allFiles.isNotEmpty) { + data['files'] = allFiles; + debugPrint( + 'DEBUG: Including non-image files in request: ${allFiles.length}', + ); + } + // Don't add session_id or id - they break SSE streaming! // The server falls back to task-based async when these are present diff --git a/lib/features/chat/providers/chat_providers.dart b/lib/features/chat/providers/chat_providers.dart index bc48ab1..fff55cd 100644 --- a/lib/features/chat/providers/chat_providers.dart +++ b/lib/features/chat/providers/chat_providers.dart @@ -37,9 +37,15 @@ class ChatMessagesNotifier extends StateNotifier> { // Only react when the conversation actually changes if (previous?.id == next?.id) { - // If same conversation but server updated it (e.g., title/content), sync messages without flicker + // If same conversation but server updated it (e.g., title/content), avoid overwriting + // locally streamed assistant content with an outdated server copy. if (previous?.updatedAt != next?.updatedAt) { - state = next?.messages ?? state; + final serverMessages = next?.messages ?? const []; + // Only replace local messages if the server has strictly more messages + // (i.e., includes new content we don't have yet). + if (serverMessages.length > state.length) { + state = serverMessages; + } } return; }