Spaces:
Sleeping
Sleeping
Update application/static/js/components/chat.js
Browse files
application/static/js/components/chat.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import requests from "./request.js";
|
| 2 |
|
| 3 |
class Chat{
|
|
@@ -27,11 +28,67 @@ class Chat{
|
|
| 27 |
}
|
| 28 |
this.uiManager.textBox.value='';
|
| 29 |
this.uiManager.sendBtn.disabled = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
const response = await requests.request('POST','/completions',{"Content-Type": "application/json"},JSON.stringify(payload),true);
|
|
|
|
| 31 |
for await (const chunk of response){
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
this.uiManager.renderSymbols.renderAll(this.uiManager.aiP)
|
| 34 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
} catch (error) {
|
| 36 |
this.uiManager.sendBtn.disabled = false;
|
| 37 |
this.uiManager.aiP.innerHTML+= `<span class="error" style="color: red;">${error}</span>`;
|
|
@@ -42,6 +99,9 @@ class Chat{
|
|
| 42 |
this.uiManager.initializer.convTitle = this.uiManager.userP.innerText.substring(0,23);
|
| 43 |
this.uiManager.addChat();
|
| 44 |
}
|
|
|
|
|
|
|
|
|
|
| 45 |
this.uiManager.sendBtn.disabled = false;
|
| 46 |
|
| 47 |
}
|
|
|
|
| 1 |
+
// application/static/js/chat.js
|
| 2 |
import requests from "./request.js";
|
| 3 |
|
| 4 |
class Chat{
|
|
|
|
| 28 |
}
|
| 29 |
this.uiManager.textBox.value='';
|
| 30 |
this.uiManager.sendBtn.disabled = true;
|
| 31 |
+
|
| 32 |
+
// Display "Reasoning..." and get the reasoning display element.
|
| 33 |
+
const reasoningDisplay = this.uiManager.showReasoning();
|
| 34 |
+
|
| 35 |
+
|
| 36 |
const response = await requests.request('POST','/completions',{"Content-Type": "application/json"},JSON.stringify(payload),true);
|
| 37 |
+
let fullResponse = ""; // Accumulate the full response
|
| 38 |
for await (const chunk of response){
|
| 39 |
+
fullResponse += chunk;
|
| 40 |
+
|
| 41 |
+
// Check for commands *within* the streamed response
|
| 42 |
+
let commandMatch;
|
| 43 |
+
if ((commandMatch = fullResponse.match(/\/(\w+)\s*\(([^)]*)\)/))) {
|
| 44 |
+
const command = commandMatch[1];
|
| 45 |
+
const argument = commandMatch[2];
|
| 46 |
+
|
| 47 |
+
if (command === "search") {
|
| 48 |
+
// Remove the command from the displayed text
|
| 49 |
+
fullResponse = fullResponse.replace(commandMatch[0], "");
|
| 50 |
+
this.uiManager.aiP.innerHTML = fullResponse; // Update with cleaned text
|
| 51 |
+
this.uiManager.renderSymbols.renderAll(this.uiManager.aiP)
|
| 52 |
+
// Perform web search
|
| 53 |
+
const searchData = await requests.request('POST', '/websearch', { "Content-Type": "application/json" }, JSON.stringify({ query: argument }), false);
|
| 54 |
+
const searchResults = await searchData.json();
|
| 55 |
+
// Display search results, possibly in a dedicated container
|
| 56 |
+
this.uiManager.appendAiMsg(searchResults.result);
|
| 57 |
+
} else if (command === "image") {
|
| 58 |
+
// Remove the command from the displayed text
|
| 59 |
+
fullResponse = fullResponse.replace(commandMatch[0], "");
|
| 60 |
+
this.uiManager.aiP.innerHTML = fullResponse;
|
| 61 |
+
this.uiManager.renderSymbols.renderAll(this.uiManager.aiP);
|
| 62 |
+
|
| 63 |
+
const imageData = await requests.request('POST', '/generate_image', { "Content-Type": "application/json" }, JSON.stringify({ prompt: argument }), false);
|
| 64 |
+
const imageResult = await imageData.json();
|
| 65 |
+
if (imageResult && imageResult.image) {
|
| 66 |
+
this.uiManager.appendAiMsg(`<img src="data:image/png;base64,${imageResult.image}" alt="Generated Image" style="max-width: 100%; height: auto;">`);
|
| 67 |
+
} else {
|
| 68 |
+
this.uiManager.appendAiMsg("Error generating image.");
|
| 69 |
+
}
|
| 70 |
+
} else if (command === "memsave") {
|
| 71 |
+
fullResponse = fullResponse.replace(commandMatch[0], ""); //remove command
|
| 72 |
+
this.uiManager.aiP.innerHTML = fullResponse;
|
| 73 |
+
this.uiManager.renderSymbols.renderAll(this.uiManager.aiP)
|
| 74 |
+
await requests.request('POST', '/save_memory', { "Content-Type": "application/json" }, JSON.stringify({ memory: argument, convId: this.uiManager.initializer.convId }), false);
|
| 75 |
+
this.uiManager.displayMemory(argument); // Display the saved memory
|
| 76 |
+
|
| 77 |
+
}
|
| 78 |
+
//Remove the command and only display the rest
|
| 79 |
+
this.uiManager.aiP.innerHTML = fullResponse;
|
| 80 |
+
} else {
|
| 81 |
+
// No command found, display the chunk normally
|
| 82 |
+
this.uiManager.aiP.innerHTML = fullResponse; // accumulate
|
| 83 |
+
}
|
| 84 |
this.uiManager.renderSymbols.renderAll(this.uiManager.aiP)
|
| 85 |
};
|
| 86 |
+
|
| 87 |
+
if (reasoningDisplay) {
|
| 88 |
+
this.uiManager.updateReasoningTime(reasoningDisplay); // Update with the final time
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
|
| 92 |
} catch (error) {
|
| 93 |
this.uiManager.sendBtn.disabled = false;
|
| 94 |
this.uiManager.aiP.innerHTML+= `<span class="error" style="color: red;">${error}</span>`;
|
|
|
|
| 99 |
this.uiManager.initializer.convTitle = this.uiManager.userP.innerText.substring(0,23);
|
| 100 |
this.uiManager.addChat();
|
| 101 |
}
|
| 102 |
+
|
| 103 |
+
// Add read-aloud button *after* the response is complete
|
| 104 |
+
this.uiManager.addReadAloudButton(this.uiManager.aiDiv);
|
| 105 |
this.uiManager.sendBtn.disabled = false;
|
| 106 |
|
| 107 |
}
|