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.
This commit is contained in:
hunteraraujo
2023-10-31 19:39:56 -07:00
parent 94d5d94547
commit 9021d8392d
2 changed files with 28 additions and 1 deletions

View File

@@ -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.

View File

@@ -113,6 +113,7 @@ class _ChatViewState extends State<ChatView> {
padding: const EdgeInsets.all(8.0),
child: ChatInputField(
onSendPressed: (message) async {
widget.viewModel.addTemporaryMessage(message);
try {
if (widget.viewModel.currentTaskId != null) {
widget.viewModel.sendChatMessage(