Skip to content

Commit 3870854

Browse files
committed
add message sending on Enter key, improve error handling, and update localStorage usage
1 parent 3aa6dc2 commit 3870854

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

js/widgets/reflection.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class ReflectionMatrix {
6464
/**
6565
* Initializes the reflection widget.
6666
*/
67+
68+
// download current summary
69+
// download analysis as text file
70+
// .md to html conversion
6771
init(activity) {
6872
this.activity = activity;
6973
this.isOpen = true;
@@ -146,6 +150,12 @@ class ReflectionMatrix {
146150
this.input.style.marginLeft = "10px";
147151
this.inputContainer.appendChild(this.input);
148152

153+
this.input.onkeydown = (e) => {
154+
if (e.key === "Enter") {
155+
this.sendMessage();
156+
}
157+
};
158+
149159
const sendBtn = document.createElement("button");
150160
sendBtn.className = "confirm-button";
151161
sendBtn.style.marginRight = "10px";
@@ -161,6 +171,8 @@ class ReflectionMatrix {
161171
} else {
162172
this.startChatSession();
163173
}
174+
175+
activity.textMsg(_("Reflect on your project."), 3000);
164176
}
165177

166178
/**
@@ -210,7 +222,6 @@ class ReflectionMatrix {
210222
*/
211223
changeMentor(mentor) {
212224
this.AImentor = mentor;
213-
console.log(this.chatHistory);
214225

215226
if (mentor === "meta") {
216227
this.metaButton.style.background = "orange";
@@ -237,13 +248,17 @@ class ReflectionMatrix {
237248
*/
238249
async startChatSession() {
239250
if (this.triggerFirst == true) return;
251+
240252
this.triggerFirst = true;
241253
setTimeout(() => {
242254
this.showTypingIndicator();
243255
}, 1000);
256+
244257
const code = await this.activity.prepareExport();
245258
const data = await this.generateAlgorithm(code);
259+
246260
this.hideTypingIndicator();
261+
247262
if (data && !data.error) {
248263
this.inputContainer.style.display = "flex";
249264
this.botReplyDiv(data, false);
@@ -313,6 +328,7 @@ class ReflectionMatrix {
313328
if (this.chatHistory.length < 10) return;
314329
this.showTypingIndicator();
315330
const data = await this.generateAnalysis();
331+
console.log(data.analysis);
316332
this.hideTypingIndicator();
317333
if (data) {
318334
this.botReplyDiv(data, false);
@@ -326,6 +342,7 @@ class ReflectionMatrix {
326342
*/
327343
async generateAnalysis() {
328344
try {
345+
console.log(this.summary);
329346
const response = await fetch(`${this.PORT}/analysis`, {
330347
method: "POST",
331348
headers: { "Content-Type": "application/json" },
@@ -362,6 +379,12 @@ class ReflectionMatrix {
362379
reply = message;
363380
}
364381

382+
if (reply.error) {
383+
this.hideTypingIndicator();
384+
this.activity.errorMsg(_("Failed to send message"), 3000);
385+
return;
386+
}
387+
365388
this.chatHistory.push({
366389
role: this.AImentor,
367390
content: reply.response
@@ -379,7 +402,7 @@ class ReflectionMatrix {
379402
senderName.innerText = this.mentorsMap[this.AImentor];
380403

381404
const botReply = document.createElement("div");
382-
botReply.innerText = reply.response || reply.error;
405+
botReply.innerText = reply.response;
383406

384407
messageContainer.appendChild(senderName);
385408
messageContainer.appendChild(botReply);
@@ -468,20 +491,17 @@ class ReflectionMatrix {
468491
*/
469492
saveReport(data) {
470493
const key = "musicblocks_analysis";
471-
const conversation = {
472-
analysis: data
473-
};
474-
localStorage.setItem(key, JSON.stringify(conversation));
494+
localStorage.setItem(key, data.response);
475495
console.log("Conversation saved in localStorage.");
476496
}
477497

478498
/** Reads the analysis report from localStorage.
479-
* @returns {Object|null} - The retrieved analysis data or null if not found.
499+
* @returns {String|null} - The retrieved analysis data or null if not found.
480500
*/
481501
readReport() {
482502
const key = "musicblocks_analysis";
483503
const data = localStorage.getItem(key);
484-
return data ? JSON.parse(data) : null;
504+
return data;
485505
}
486506

487507
/** Downloads the conversation as a text file.

0 commit comments

Comments
 (0)