From 9021d8392d85d21ce939306fbe31236ba09f9223 Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Tue, 31 Oct 2023 19:39:56 -0700 Subject: [PATCH] feat: Add support for temporary chat message - Added logic to display a temporary chat message while waiting for the response from the agent. - When a user sends a message, a temporary chat message is created and displayed. - Implemented a method to remove the temporary chat message when the response is received or in case of an error. - Updated the UI to invoke the method for adding a temporary chat message when the send button is pressed. Feedback: - This change improves the user experience by allowing them to see their own message immediately while waiting for the response from the agent. --- frontend/lib/viewmodels/chat_viewmodel.dart | 28 ++++++++++++++++++++- frontend/lib/views/chat/chat_view.dart | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/frontend/lib/viewmodels/chat_viewmodel.dart b/frontend/lib/viewmodels/chat_viewmodel.dart index bd42964e..294a3b77 100644 --- a/frontend/lib/viewmodels/chat_viewmodel.dart +++ b/frontend/lib/viewmodels/chat_viewmodel.dart @@ -95,7 +95,9 @@ class ChatViewModel with ChangeNotifier { } // Assign the chats list - _chats = chats; + if (chats.length > 0) { + _chats = chats; + } // Notify listeners to rebuild UI notifyListeners(); @@ -154,6 +156,9 @@ class ChatViewModel with ChangeNotifier { _chats.add(agentChat); + // Remove the temporary message + removeTemporaryMessage(); + // Notify UI of the new chats notifyListeners(); @@ -170,6 +175,8 @@ class ChatViewModel with ChangeNotifier { print("Chats added for task ID: $_currentTaskId"); } catch (e) { + // Remove the temporary message in case of an error + removeTemporaryMessage(); // TODO: We are bubbling up the full response. Revisit this. rethrow; // TODO: Handle additional error scenarios or log them as required @@ -179,6 +186,25 @@ class ChatViewModel with ChangeNotifier { } } + void addTemporaryMessage(String message) { + Chat tempMessage = Chat( + // You can generate a unique ID or use a placeholder + id: "TEMP_ID", + taskId: "TEMP_ID", + message: message, + timestamp: DateTime.now(), + messageType: MessageType.user, + artifacts: []); + + _chats.add(tempMessage); + notifyListeners(); + } + + void removeTemporaryMessage() { + _chats.removeWhere((chat) => chat.id == "TEMP_ID"); + notifyListeners(); + } + /// Downloads an artifact associated with a specific chat. /// /// [taskId] is the ID of the task. diff --git a/frontend/lib/views/chat/chat_view.dart b/frontend/lib/views/chat/chat_view.dart index 9e28f684..9dac1c1c 100644 --- a/frontend/lib/views/chat/chat_view.dart +++ b/frontend/lib/views/chat/chat_view.dart @@ -113,6 +113,7 @@ class _ChatViewState extends State { padding: const EdgeInsets.all(8.0), child: ChatInputField( onSendPressed: (message) async { + widget.viewModel.addTemporaryMessage(message); try { if (widget.viewModel.currentTaskId != null) { widget.viewModel.sendChatMessage(