File size: 7,065 Bytes
3382f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import 'package:auto_gpt_flutter_client/models/step.dart';
import 'package:auto_gpt_flutter_client/models/step_request_body.dart';
import 'package:auto_gpt_flutter_client/services/shared_preferences_service.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';

class ChatViewModel with ChangeNotifier {
  final ChatService _chatService;
  List<Chat> _chats = [];
  String? _currentTaskId;
  final SharedPreferencesService _prefsService;

  bool _isWaitingForAgentResponse = false;

  bool get isWaitingForAgentResponse => _isWaitingForAgentResponse;
  SharedPreferencesService get prefsService => _prefsService;

  bool _isContinuousMode = false;

  bool get isContinuousMode => _isContinuousMode;
  set isContinuousMode(bool value) {
    _isContinuousMode = value;
    notifyListeners();
  }

  ChatViewModel(this._chatService, this._prefsService);

  /// Returns the current list of chats.
  List<Chat> get chats => _chats;

  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 {
      // Fetch task steps from the data source
      final Map<String, dynamic> stepsResponse =
          await _chatService.listTaskSteps(_currentTaskId!, pageSize: 10000);

      // Extract steps from the response
      final List<dynamic> stepsJsonList = stepsResponse['steps'] ?? [];

      // 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 (int i = 0; i < steps.length; i++) {
        Step step = steps[i];

        // 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,
              artifacts: step.artifacts));
        }

        // Create a Chat object for 'output'
        chats.add(Chat(
            id: step.stepId,
            taskId: step.taskId,
            message: step.output,
            timestamp: currentTimestamp,
            messageType: MessageType.agent,
            jsonResponse: stepsJsonList[i],
            artifacts: step.artifacts));
      }

      // Assign the chats list
      if (chats.length > 0) {
        _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
    }
  }

  /// Sends a chat message for a specific task.
  void sendChatMessage(String message,
      {required int continuousModeSteps, int currentStep = 1}) async {
    if (_currentTaskId == null) {
      print("Error: Task ID is not set.");
      return;
    }
    _isWaitingForAgentResponse = true;
    notifyListeners();

    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
      if (executedStep.input.isNotEmpty) {
        final userChat = Chat(
            id: executedStep.stepId,
            taskId: executedStep.taskId,
            message: executedStep.input,
            timestamp: DateTime.now(),
            messageType: MessageType.user,
            artifacts: executedStep.artifacts);

        _chats.add(userChat);
      }

      // 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,
          jsonResponse: executedStepResponse,
          artifacts: executedStep.artifacts);

      _chats.add(agentChat);

      // Remove the temporary message
      removeTemporaryMessage();

      // Notify UI of the new chats
      notifyListeners();

      if (_isContinuousMode && !executedStep.isLast) {
        print("Continuous Mode: Step $currentStep of $continuousModeSteps");
        if (currentStep < continuousModeSteps) {
          sendChatMessage("",
              continuousModeSteps: continuousModeSteps,
              currentStep: currentStep + 1);
        } else {
          _isContinuousMode = false;
        }
      }

      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
    } finally {
      _isWaitingForAgentResponse = false;
      notifyListeners();
    }
  }

  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.
  /// [artifactId] is the ID of the artifact to be downloaded.
  Future<void> downloadArtifact(String taskId, String artifactId) async {
    try {
      // Call the downloadArtifact method from the ChatService class
      await _chatService.downloadArtifact(taskId, artifactId);

      print("Artifact $artifactId downloaded successfully for task $taskId!");
    } catch (error) {
      print("Error downloading artifact: $error");
      // TODO: Handle the error appropriately, perhaps notify the user
    }
  }
}