From 9411c814246e7c2b8f8dca7d360b84a905e2e892 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Sat, 27 Sep 2025 16:50:16 +0530 Subject: [PATCH] fix: followups not arriving issue --- lib/core/services/streaming_helper.dart | 63 ++++++++++++++++++- .../chat/providers/chat_providers.dart | 28 ++++++++- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/lib/core/services/streaming_helper.dart b/lib/core/services/streaming_helper.dart index 00da4d9..e9fd374 100644 --- a/lib/core/services/streaming_helper.dart +++ b/lib/core/services/streaming_helper.dart @@ -681,19 +681,58 @@ ActiveSocketStream attachUnifiedChunkedStreaming({ disposeSocketSubscriptions(); finishStreaming(); } else if (type == 'chat:message:follow_ups' && payload != null) { + DebugLogger.log( + 'Received follow-ups event: $payload', + scope: 'streaming/helper', + ); final followMap = _asStringMap(payload); if (followMap != null) { final followUpsRaw = followMap['follow_ups'] ?? followMap['followUps']; final suggestions = _parseFollowUpsField(followUpsRaw); final targetId = _resolveTargetMessageId(messageId, getMessages); + DebugLogger.log( + 'Processing follow-ups: ${suggestions.length} suggestions for message $targetId', + scope: 'streaming/helper', + ); + // Debug: show current message IDs in state + try { + final currentMessages = getMessages(); + final messageIds = currentMessages + .map((m) => '${m.id} (${m.role})') + .join(', '); + DebugLogger.log( + 'Current messages in state: $messageIds', + scope: 'streaming/helper', + ); + } catch (e) { + DebugLogger.error( + 'Error getting messages in follow-ups handler', + scope: 'streaming/helper', + error: e, + ); + } if (targetId != null) { + DebugLogger.log( + 'Follow-ups: calling setFollowUps for target $targetId', + scope: 'streaming/helper', + ); setFollowUps(targetId, suggestions); updateMessageById(targetId, (current) { final metadata = {...?current.metadata, 'followUps': suggestions}; return current.copyWith(metadata: metadata); }); + } else { + DebugLogger.log( + 'Follow-ups: targetId is null, cannot set follow-ups', + scope: 'streaming/helper', + ); } + } else { + DebugLogger.log( + 'Failed to parse follow-ups payload as string map', + scope: 'streaming/helper', + ); } } else if (type == 'chat:title' && payload != null) { final title = payload.toString(); @@ -954,6 +993,12 @@ ActiveSocketStream attachUnifiedChunkedStreaming({ appendToLastMessage(content); updateImagesFromCurrentContent(); } + } else { + // Debug unknown event types to catch missing handlers + DebugLogger.log( + 'Unknown chat event type: $type (payload keys: ${payload is Map ? payload.keys.join(", ") : "not a map"})', + scope: 'streaming/helper', + ); } } catch (_) {} } @@ -977,6 +1022,12 @@ ActiveSocketStream attachUnifiedChunkedStreaming({ appendToLastMessage(content); updateImagesFromCurrentContent(); } + } else { + // Debug channel events that might include follow-ups + DebugLogger.log( + 'Channel event type: $type (payload keys: ${payload is Map ? payload.keys.join(", ") : "not a map"})', + scope: 'streaming/helper', + ); } } catch (_) {} } @@ -1121,8 +1172,18 @@ ActiveSocketStream attachUnifiedChunkedStreaming({ } disposeSocketSubscriptions(); + DebugLogger.log( + 'Finishing streaming and scheduling conversation refresh', + scope: 'streaming/helper', + ); finishStreaming(); - Future.microtask(refreshConversationSnapshot); + Future.microtask(() { + DebugLogger.log( + 'Executing conversation refresh after streaming finished', + scope: 'streaming/helper', + ); + refreshConversationSnapshot(); + }); socketWatchdog?.stop(); }, ); diff --git a/lib/features/chat/providers/chat_providers.dart b/lib/features/chat/providers/chat_providers.dart index 373577c..13d90c4 100644 --- a/lib/features/chat/providers/chat_providers.dart +++ b/lib/features/chat/providers/chat_providers.dart @@ -498,13 +498,31 @@ class ChatMessagesNotifier extends Notifier> { String messageId, ChatMessage Function(ChatMessage current) updater, ) { + DebugLogger.log( + 'updateMessageById called for message $messageId', + scope: 'chat/provider', + ); final index = state.indexWhere((m) => m.id == messageId); - if (index == -1) return; + if (index == -1) { + DebugLogger.log( + 'updateMessageById: message $messageId not found in state (${state.length} messages)', + scope: 'chat/provider', + ); + return; + } final original = state[index]; final updated = updater(original); if (identical(updated, original)) { + DebugLogger.log( + 'updateMessageById: no changes made to message $messageId', + scope: 'chat/provider', + ); return; } + DebugLogger.log( + 'updateMessageById: updating message $messageId at index $index', + scope: 'chat/provider', + ); final next = [...state]; next[index] = updated; state = next; @@ -518,7 +536,15 @@ class ChatMessagesNotifier extends Notifier> { } void setFollowUps(String messageId, List followUps) { + DebugLogger.log( + 'setFollowUps called for message $messageId with ${followUps.length} follow-ups', + scope: 'chat/provider', + ); updateMessageById(messageId, (current) { + DebugLogger.log( + 'setFollowUps: updating message with follow-ups: ${followUps.join(", ")}', + scope: 'chat/provider', + ); return current.copyWith(followUps: List.from(followUps)); }); }