Files
Auto-GPT/lib/viewmodels/chat_viewmodel.dart
hunteraraujo 1436092a35 Implement ChatViewModel with tests and mock data
This commit introduces the ChatViewModel, which manages the business logic for chat interactions associated with tasks. The ViewModel communicates with a mock data source, offering functionalities like fetching chats for a specific task and sending chat messages.

In addition to the implementation, comprehensive tests for ChatViewModel have been provided to ensure its behavior is consistent with our design goals and expectations.

Key Features:

Chat management in ChatViewModel.
Tests covering all major functionalities of ChatViewModel.
Mock data source updates to emulate chat data interactions.
2023-08-22 07:33:54 +02:00

47 lines
1.6 KiB
Dart

import 'package:auto_gpt_flutter_client/models/chat.dart';
import 'package:auto_gpt_flutter_client/models/message_type.dart';
import 'package:flutter/foundation.dart';
import 'mock_data.dart'; // Import the mock data
// TODO: Update whole class once we have created TaskService
class ChatViewModel with ChangeNotifier {
List<Chat> _chats = [];
/// Returns the current list of chats.
List<Chat> get chats => _chats;
/// Fetches chats from the mock data source for a specific task.
void fetchChatsForTask(int taskId) {
try {
_chats = mockChats.where((chat) => chat.taskId == taskId).toList();
notifyListeners(); // Notify listeners to rebuild UI
print("Chats fetched successfully for task ID: $taskId");
} catch (error) {
print("Error fetching chats: $error");
// TODO: Handle additional error scenarios or log them as required
}
}
/// Simulates sending a chat message for a specific task.
void sendChatMessage(int taskId, String message) {
final userChat = Chat(
id: _chats.length + 1,
taskId: taskId,
message: message,
timestamp: DateTime.now(),
messageType: MessageType.user);
// For now, we'll simulate an agent's reply after the user's message
final agentChat = Chat(
id: _chats.length + 2,
taskId: taskId,
message: 'Automated reply to: $message',
timestamp: DateTime.now().add(const Duration(seconds: 2)),
messageType: MessageType.agent);
_chats.addAll([userChat, agentChat]);
notifyListeners(); // Notify UI of the new chats
print("User chat and automated agent reply added for task ID: $taskId");
}
}