Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Update ChatViewModel to Use ChatService and Step Model

hunteraraujo651e112e3df8
diff --git a/lib/viewmodels/chat_viewmodel.dart b/lib/viewmodels/chat_viewmodel.dart
index a2b3210378..e2a0127ca3 100644
--- a/lib/viewmodels/chat_viewmodel.dart
+++ b/lib/viewmodels/chat_viewmodel.dart
@@ -1,46 +1,138 @@
+import 'package:auto_gpt_flutter_client/models/step.dart';
+import 'package:auto_gpt_flutter_client/models/step_request_body.dart';
+import 'package:flutter/foundation.dart';
+import 'package:auto_gpt_flutter_client/services/chat_service.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 {
+ final ChatService _chatService;
List<Chat> _chats = [];
+ String? _currentTaskId;
+
+ ChatViewModel(this._chatService);
/// 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) {
+ String? get currentTaskId => _currentTaskId;
+
+ void setCurrentTaskId(String taskId) {
+ if (_currentTaskId != taskId) {
+ _currentTaskId = taskId;
+ fetchChatsForTask();
+ }
+ }
+
+ void clearCurrentTaskAndChats() {
+ _currentTaskId = null;
+ _chats.clear();
+ notifyListeners(); // Notify listeners to rebuild UI
+ }
+
+ /// Fetches chats from the data source for a specific task.
+ void fetchChatsForTask() async {
+ if (_currentTaskId == null) {
+ print("Error: Task ID is not set.");
+ return;
+ }
try {
- _chats = mockChats.where((chat) => chat.taskId == taskId).toList();
- notifyListeners(); // Notify listeners to rebuild UI
- print("Chats fetched successfully for task ID: $taskId");
+ // Fetch task steps from the data source
+ final List<dynamic> stepsJsonList =
+ await _chatService.listTaskSteps(_currentTaskId!);
+
+ // Convert each map into a Step object
+ List<Step> steps =
+ stepsJsonList.map((stepMap) => Step.fromMap(stepMap)).toList();
+
+ // Initialize an empty list to store Chat objects
+ List<Chat> chats = [];
+
+ // Generate current timestamp
+ DateTime currentTimestamp = DateTime.now();
+
+ for (Step step in steps) {
+ // Create a Chat object for 'input' if it exists and is not empty
+ if (step.input.isNotEmpty) {
+ chats.add(Chat(
+ id: step.stepId,
+ taskId: step.taskId,
+ message: step.input,
+ timestamp: currentTimestamp,
+ messageType: MessageType.user,
+ ));
+ }
+
+ // Create a Chat object for 'output'
+ chats.add(Chat(
+ id: step.stepId,
+ taskId: step.taskId,
+ message: step.output,
+ timestamp: currentTimestamp,
+ messageType: MessageType.agent,
+ ));
+ }
+
+ // Assign the chats list
+ _chats = chats;
+
+ // Notify listeners to rebuild UI
+ notifyListeners();
+
+ print(
+ "Chats (and steps) fetched successfully for task ID: $_currentTaskId");
} 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,
+ /// Sends a chat message for a specific task.
+ void sendChatMessage(String message) async {
+ if (_currentTaskId == null) {
+ print("Error: Task ID is not set.");
+ return;
+ }
+ try {
+ // Create the request body for executing the step
+ StepRequestBody requestBody = StepRequestBody(input: message);
+
+ // Execute the step and get the response
+ Map<String, dynamic> executedStepResponse =
+ await _chatService.executeStep(_currentTaskId!, requestBody);
+
+ // Create a Chat object from the returned step
+ Step executedStep = Step.fromMap(executedStepResponse);
+
+ // Create a Chat object for the user message
+ final userChat = Chat(
+ id: executedStep.stepId,
+ taskId: executedStep.taskId,
+ message: executedStep.input,
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");
+ messageType: MessageType.user,
+ );
+
+ // Create a Chat object for the agent message
+ final agentChat = Chat(
+ id: executedStep.stepId,
+ taskId: executedStep.taskId,
+ message: executedStep.output,
+ timestamp: DateTime.now(),
+ messageType: MessageType.agent,
+ );
+
+ // Add the user and agent chats to the list
+ _chats.add(userChat);
+ _chats.add(agentChat);
+
+ // Notify UI of the new chats
+ notifyListeners();
+
+ print("Chats added for task ID: $_currentTaskId");
+ } catch (error) {
+ print("Error sending chat: $error");
+ // TODO: Handle additional error scenarios or log them as required
+ }
}
}