File size: 10,197 Bytes
14dc68f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
        // Display message function
        function displayMessage(content, role = "user") {
            let messageHTML = createMessageHTML(content, role);
            $("#chat-messages").append(messageHTML);
            if (role === "ongoing") {
                $(".bg-green-100:last").parent().addClass("ai-processing");
            }
        }

        function createMessageHTML(content, role) {
            if (role === "user") {
                return `<div class="flex justify-end mb-2">
                            <div class="bg-blue-200 text-black rounded-lg p-3 max-w-lg lg:max-w-xl text-right relative before:absolute before:inset-r-0 before:w-3 before:h-3 before:bg-blue-500 before:transform before:-rotate-45 before:-translate-y-1/2">
                                ${content}
                            </div>
                        </div>`;
            } else {
                return `<div class="flex justify-start mb-2">
                            <div class="bg-green-100 text-black rounded-lg p-3 max-w-lg lg:max-w-xl relative before:absolute before:inset-l-0 before:w-3 before:h-3 before:bg-gray-300 before:transform before:-rotate-45 before:-translate-y-1/2">
                                ${content}
                            </div>
                        </div>`;
            }
        }

        // Send message function
        function sendMessage() {
            const userMessage = $("#user-input").val().trim();

            if (!userMessage) {
                alert("Message cannot be empty!");
                return;
            }

            // Display the user's message
            displayMessage(userMessage, "user");
            scrollToBottom();
            // Display processing message
            displayMessage('...', 'ongoing');
            scrollToBottom();

            // Clear the input and disable the button
            $("#user-input").val("");
            toggleSendButton();

            // Send the message to the backend for processing
            $.ajax({
                type: 'POST',
                url: '/determine-response',
                data: JSON.stringify({ user_message: userMessage }),
                contentType: 'application/json',
                dataType: 'json',
                success: handleResponseSuccess,
                error: handleResponseError
            });
        }

        function handleResponseSuccess(data) {
            $(".ai-processing").remove();
            displayMessage(data.message, "assistant");
            scrollToBottom();

            if (data.objective) {
                displayTaskWithStatus(`Task: ${data.objective}`, "ongoing", data.skill_used, data.task_id);
            }
          
            if (data.path !== "ChatCompletion") {
                checkTaskCompletion(data.task_id);
            } else {
                
            }
            
        }

        function handleResponseError(error) {
            $(".ai-processing").remove();
            const errorMessage = error.responseJSON && error.responseJSON.error ? error.responseJSON.error : "Unknown error";
            displayMessage(`Error: ${errorMessage}`, "error");
            scrollToBottom();
        }

        // Other utility functions
        function toggleSendButton() {
            if ($("#user-input").val().trim()) {
                $("button").prop("disabled", false);
            } else {
                $("button").prop("disabled", true);
            }
        }

function scrollToBottom() {
    setTimeout(() => {
        const chatBox = document.getElementById("chat-messages");
        chatBox.scrollTop = chatBox.scrollHeight;
    }, 100);  // small delay to ensure content is rendered
}



        function checkTaskCompletion(taskId) {
            $.ajax({
                type: 'GET',
                url: `/check-task-status/${taskId}`,
                dataType: 'json',
                success(data) {
                    if (data.status === "completed") {                            updateTaskStatus(taskId, "completed");
                      fetchTaskOutput(taskId);
                      displayMessage("Hey, I just finished a task!", "assistant");
                    } else {
                      fetchTaskOutput(taskId);
                        setTimeout(() => {
                            checkTaskCompletion(taskId);
                        }, 5000); // Check every 5 seconds
                    }
                },
                error(error) {
                    console.error(`Error checking task status for ${taskId}:`, error);
                }
            });
        }

function fetchTaskOutput(taskId) {
    $.ajax({
        type: 'GET',
        url: `/fetch-task-output/${taskId}`,
        dataType: 'json',
        success(data) {
            if (data.output) {
                const $taskItem = $(`.task-item[data-task-id="${taskId}"]`);  // Find the task item with the given task ID
                console.log('taskItem:'+$taskItem)
                const $outputContainer = $taskItem.find('.task-output');
                console.log('outputContainer:'+$outputContainer)
                console.log('data.output:'+data.output)
                
                // Update the task's output content
                $outputContainer.html(`<p>${data.output}</p>`);
            }
        },
        error(error) {
            console.error(`Error fetching task output for ${taskId}:`, error);
        }
    });
}




$(document).ready(function() {
  toggleSendButton();
  loadPreviousMessages();
  loadAllTasks(); 
  $("#send-btn").on('click', function() {
    sendMessage();
  });
  $("#user-input").on('keyup', toggleSendButton);
  $("#user-input").on('keydown', function(e) {
      if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey) {
          e.preventDefault();
          sendMessage();
      }
  });
  $("#task-search").on("keyup", function() {
    let value = $(this).val().toLowerCase();
    $(".task-item").filter(function() {
      $(this).toggle($(this).find(".task-title").text().toLowerCase().indexOf(value) > -1);
    });
  });


});

        function loadPreviousMessages() {
            $.ajax({
                type: 'GET',
                url: '/get-all-messages',
                dataType: 'json',
                success(data) {
                    data.forEach(message => displayMessage(message.content, message.role));
                    scrollToBottom();
                },
                error(error) {
                    console.error("Error fetching previous messages:", error);
                }
            });
        }

function getStatusBadgeHTML(status) {
    switch (status) {
        case 'ongoing':
            return `<span class="status-badge inline-block bg-yellow-300 rounded-full px-2 py-1 text-xs font-semibold text-gray-700 mr-2 ml-2">Ongoing</span>`;
        case 'completed':
            return `<span class="status-badge inline-block bg-green-300 rounded-full px-2 py-1 text-xs font-semibold text-gray-700 mr-2 ml-2">Completed</span>`;
        case 'error':
            return `<span class="status-badge inline-block bg-red-400 rounded-full px-2 py-1 text-xs font-semibold text-white mr-2 ml-2">Error</span>`;
        default:
            return `<span class="status-badge inline-block bg-gray-400 rounded-full px-2 py-1 text-xs font-semibold text-white mr-2 ml-2">Unknown</span>`;
    }
}

function displayTaskWithStatus(taskDescription, status, skillType = "Unknown Skill", taskId, output = null) {
    let statusBadgeHTML = getStatusBadgeHTML(status);

    let skillBadgeHTML = '';
    if (skillType) {
        skillBadgeHTML = `<span class="inline-block bg-purple-300 rounded-full px-2 py-1 text-xs font-semibold text-gray-700 ml-2">${skillType}</span>`;
    }

    let outputHTML = output ? `<p>${output}</p>` : '<p><small>No output yet...</small></p>';

    const taskHTML = `
         <div class="task-item" data-task-id="${taskId}">
             <span class="task-title">${taskDescription}</span></br>
             <span class="toggle-output-icon" style="cursor: pointer;">▶</span> 
             ${statusBadgeHTML}${skillBadgeHTML}
             <div class="task-output hidden">
                 ${outputHTML}
             </div>
         </div>`;
    $("#task-list").prepend(taskHTML);
}


function updateTaskStatus(taskId, status, output) {
    const $taskToUpdate = $(`#task-list > .task-item[data-task-id="${taskId}"]`);
    
    // Remove the existing status badge
    $taskToUpdate.find(".status-badge").remove();
    
    // Insert the new status badge right before the skill badge
    $taskToUpdate.find(".toggle-output-icon").after(getStatusBadgeHTML(status));
    
    // Update output, if available
    const $outputContainer = $taskToUpdate.find(".task-output");
    if (output) {
        $outputContainer.html(`<p>${output}</p>`);
    } else {
        $outputContainer.find("p").text("No output yet...");
    }
}

function loadAllTasks() {
    $.ajax({
        type: 'GET',
        url: '/get-all-tasks',
        dataType: 'json',
        success(data) {
            console.log("Debug: Received tasks:", data);  // Debug print
            data.forEach(task => {
                const description = task.description || '';
                const status = task.status || '';
                const skill_used = task.skill_used || '';
                const task_id = task.task_id || '';
                const output = task.output || null;  // Get the output, if it exists, otherwise set to null
                displayTaskWithStatus(description, status, skill_used, task_id, output);
            });
        },
        error(error) {
            console.error("Error fetching all tasks:", error);
        }
    });
}






$(document).on('click', '.toggle-output-icon', function() {
    const $task = $(this).closest(".task-item");
    const $output = $task.find(".task-output");
    $output.toggleClass('hidden');
    // Change the icon when the output is toggled
    const icon = $(this);
    if ($output.hasClass('hidden')) {
        icon.text('▶'); // Icon indicating the output is hidden
    } else {
        icon.text('▼'); // Icon indicating the output is shown
    }
});