Skip to content

Commit 277055e

Browse files
committed
feat: update mentor name to Alan and enhance project code update functionality
1 parent b130039 commit 277055e

File tree

1 file changed

+73
-13
lines changed

1 file changed

+73
-13
lines changed

js/widgets/reflection.js

Lines changed: 73 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,6 +269,32 @@ 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+
this.showTypingIndicator("Reading code");
290+
const data = await this.generateNewAlgorithm(this.projectAlgorithm, code);
291+
this.hideTypingIndicator();
292+
293+
if (data && !data.error) {
294+
this.projectAlgorithm = data.algorithm; // update algorithm
295+
this.code = code;
296+
this.botReplyDiv(data, false, false);
297+
this.activity.textMsg(_("Project code updated."), 3000);
262298
} else {
263299
this.activity.errorMsg(_(data.error), 3000);
264300
}
@@ -287,6 +323,30 @@ class ReflectionMatrix {
287323
}
288324
}
289325

326+
/**
327+
* Sends the previous algorithm and new code to the server to get an updated algorithm.
328+
* @param {string} previousAlgorithm - The previous project algorithm.
329+
* @param {string} code - The new project code.
330+
* @returns {Promise<Object>} - The server response containing the updated algorithm.
331+
*/
332+
async generateNewAlgorithm(previousAlgorithm, code) {
333+
try {
334+
const response = await fetch(`${this.PORT}/updatecode`, {
335+
method: "POST",
336+
headers: { "Content-Type": "application/json" },
337+
body: JSON.stringify({
338+
oldcode: previousAlgorithm,
339+
newcode: code
340+
})
341+
});
342+
const data = await response.json();
343+
return data;
344+
} catch (error) {
345+
console.error("Error :", error);
346+
return { error: "Failed to send message" };
347+
}
348+
}
349+
290350
/**
291351
* Sends a message to the server and retrieves the bot's reply.
292352
* @param {string} message - The user's message.
@@ -323,7 +383,7 @@ class ReflectionMatrix {
323383
*/
324384
async getAnalysis() {
325385
if (this.chatHistory.length < 10) return;
326-
this.showTypingIndicator();
386+
this.showTypingIndicator("Analyzing");
327387
const data = await this.generateAnalysis();
328388
this.hideTypingIndicator();
329389
if (data) {
@@ -404,7 +464,7 @@ class ReflectionMatrix {
404464
} else {
405465
botReply.innerText = reply.response;
406466
}
407-
467+
408468
messageContainer.appendChild(senderName);
409469
messageContainer.appendChild(botReply);
410470

0 commit comments

Comments
 (0)