Skip to content

Commit fe71ff6

Browse files
authored
Update mentor name to Alan and reload functionality added (#4752)
* feat: update mentor name to Alan and enhance project code update functionality * fix: improve project code update logic to handle unchanged algorithms
1 parent b130039 commit fe71ff6

File tree

1 file changed

+78
-13
lines changed

1 file changed

+78
-13
lines changed

js/widgets/reflection.js

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ReflectionMatrix {
4545
user: "YOU",
4646
meta: "ROHAN",
4747
music: "BEETHOVEN",
48-
code: "STEVE"
48+
code: "ALAN"
4949
};
5050

5151
/**
@@ -59,6 +59,12 @@ class ReflectionMatrix {
5959
* @type {string}
6060
*/
6161
this.projectAlgorithm = "";
62+
63+
/**
64+
* MusicBlocks code
65+
* @type {string}
66+
*/
67+
this.code = "";
6268
}
6369

6470
/**
@@ -70,7 +76,7 @@ class ReflectionMatrix {
7076
this.isOpen = true;
7177
this.isMaximized = false;
7278
this.activity.isInputON = true;
73-
this.PORT = "http://52.65.37.66:8000";
79+
this.PORT = "http://52.65.37.66:8000"; // http://127.0.0.1:8000
7480

7581
const widgetWindow = window.widgetWindows.windowFor(this, "reflection", "reflection");
7682
this.widgetWindow = widgetWindow;
@@ -92,11 +98,8 @@ class ReflectionMatrix {
9298
this.chatInterface.className = "chatInterface";
9399
widgetWindow.getWidgetBody().append(this.chatInterface);
94100

95-
widgetWindow.addButton(
96-
"notes_icon.svg",
97-
ReflectionMatrix.ICONSIZE,
98-
_("Summary")
99-
).onclick = () => this.getAnalysis();
101+
widgetWindow.addButton("notes_icon.svg", ReflectionMatrix.ICONSIZE, _("Summary")).onclick =
102+
() => this.getAnalysis();
100103
widgetWindow.addButton(
101104
"save-button-dark.svg",
102105
ReflectionMatrix.ICONSIZE,
@@ -113,7 +116,7 @@ class ReflectionMatrix {
113116
this.codeButton = widgetWindow.addButton(
114117
"code.svg",
115118
ReflectionMatrix.ICONSIZE,
116-
_("Talk with Steve")
119+
_("Talk with Alan")
117120
);
118121
this.codeButton.onclick = () => this.changeMentor("code");
119122

@@ -124,6 +127,13 @@ class ReflectionMatrix {
124127
);
125128
this.musicButton.onclick = () => this.changeMentor("music");
126129

130+
this.reloadButton = widgetWindow.addButton(
131+
"reload.svg",
132+
ReflectionMatrix.ICONSIZE,
133+
_("Refresh")
134+
);
135+
this.reloadButton.onclick = () => this.updateProjectCode();
136+
127137
this.changeMentor(this.AImentor);
128138

129139
this.chatLog = document.createElement("div");
@@ -176,14 +186,14 @@ class ReflectionMatrix {
176186
* Displays a typing indicator with animated dots.
177187
* @returns {void}
178188
*/
179-
showTypingIndicator() {
189+
showTypingIndicator(action) {
180190
if (this.typingDiv) return;
181191

182192
this.typingDiv = document.createElement("div");
183193
this.typingDiv.className = "typing-indicator";
184194

185195
const textElement = document.createElement("span");
186-
textElement.textContent = "Thinking";
196+
textElement.textContent = action ? action : "Thinking";
187197
this.typingDiv.appendChild(textElement);
188198

189199
this.dotsContainer = document.createElement("span");
@@ -248,7 +258,7 @@ class ReflectionMatrix {
248258

249259
this.triggerFirst = true;
250260
setTimeout(() => {
251-
this.showTypingIndicator();
261+
this.showTypingIndicator("Reading code");
252262
}, 1000);
253263

254264
const code = await this.activity.prepareExport();
@@ -259,9 +269,40 @@ class ReflectionMatrix {
259269
if (data && !data.error) {
260270
this.inputContainer.style.display = "flex";
261271
this.botReplyDiv(data, false, false);
272+
this.projectAlgorithm = data.algorithm;
273+
this.code = code;
274+
} else {
275+
this.activity.errorMsg(_(data.error), 3000);
276+
}
277+
}
278+
279+
/**
280+
* Updates the project code and retrieves the new algorithm from the server.
281+
* @returns {Promise<void>}
282+
*/
283+
async updateProjectCode() {
284+
const code = await this.activity.prepareExport();
285+
if (code === this.code) {
286+
console.log("No changes in code detected.");
287+
return; // No changes in code
288+
}
289+
290+
this.showTypingIndicator("Reading code");
291+
const data = await this.generateNewAlgorithm(code);
292+
this.hideTypingIndicator();
293+
294+
if (data && !data.error) {
295+
if (data.algorithm !== "unchanged") {
296+
this.projectAlgorithm = data.algorithm; // update algorithm
297+
this.code = code;
298+
} else {
299+
console.log("No changes in algorithm detected.");
300+
}
301+
this.botReplyDiv(data, false, false);
262302
} else {
263303
this.activity.errorMsg(_(data.error), 3000);
264304
}
305+
265306
this.projectAlgorithm = data.algorithm;
266307
}
267308

@@ -287,6 +328,30 @@ class ReflectionMatrix {
287328
}
288329
}
289330

331+
/**
332+
* Sends the previous algorithm and new code to the server to get an updated algorithm.
333+
* @param {string} previousAlgorithm - The previous project algorithm.
334+
* @param {string} code - The new project code.
335+
* @returns {Promise<Object>} - The server response containing the updated algorithm.
336+
*/
337+
async generateNewAlgorithm(code) {
338+
try {
339+
const response = await fetch(`${this.PORT}/updatecode`, {
340+
method: "POST",
341+
headers: { "Content-Type": "application/json" },
342+
body: JSON.stringify({
343+
oldcode: this.code,
344+
newcode: code
345+
})
346+
});
347+
const data = await response.json();
348+
return data;
349+
} catch (error) {
350+
console.error("Error :", error);
351+
return { error: "Failed to send message" };
352+
}
353+
}
354+
290355
/**
291356
* Sends a message to the server and retrieves the bot's reply.
292357
* @param {string} message - The user's message.
@@ -323,7 +388,7 @@ class ReflectionMatrix {
323388
*/
324389
async getAnalysis() {
325390
if (this.chatHistory.length < 10) return;
326-
this.showTypingIndicator();
391+
this.showTypingIndicator("Analyzing");
327392
const data = await this.generateAnalysis();
328393
this.hideTypingIndicator();
329394
if (data) {
@@ -404,7 +469,7 @@ class ReflectionMatrix {
404469
} else {
405470
botReply.innerText = reply.response;
406471
}
407-
472+
408473
messageContainer.appendChild(senderName);
409474
messageContainer.appendChild(botReply);
410475

0 commit comments

Comments
 (0)