-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
300 lines (277 loc) · 9.17 KB
/
Copy pathscript.js
File metadata and controls
300 lines (277 loc) · 9.17 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// script.js
let currentPersona = "Lucy";
let currentMode = "text";
let currentSessionId = "default";
let isRecording = false;
let mediaRecorder;
let audioChunks = [];
const API_BASE = "/api";
const personaDescriptions = {
Lucy: "Gentle, sweet, comforting, soft-spoken, timid, welcoming, patient, good listener 💕",
Suzanne:
"Funny, silly, playful, comforting, jovial, empathetic, lighthearted, witty 😂",
Lexi: "Wise, thoughtful, smart, decisive, self-aware, articulate, logical 🌿",
Roxy: "Bold, confident, badass, girl's girl, supportive, courageous, outgoing 🔥",
};
window.onload = function () {
const userInput = document.getElementById("user-input");
// Feature: Send message on "Enter" key press
userInput.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
sendMessage();
}
});
selectPersona(currentPersona); // Load default persona and session
setMode("text"); // Ensure text mode is the default view
};
function selectPersona(persona) {
currentPersona = persona;
currentSessionId = "default"; // Reset to default session for the new persona
document
.querySelectorAll(".persona-option")
.forEach((o) =>
o.classList.toggle("active", o.dataset.persona === persona)
);
document.getElementById("persona-description").textContent =
personaDescriptions[persona];
document.body.className = `${persona.toLowerCase()}-theme`;
loadHistory();
}
function setMode(mode) {
currentMode = mode;
document
.getElementById("text-mode-btn")
.classList.toggle("active-mode", mode === "text");
document
.getElementById("voice-mode-btn")
.classList.toggle("active-mode", mode === "voice");
document.getElementById("user-input").style.display =
mode === "text" ? "block" : "none";
document.getElementById("send-btn").style.display =
mode === "text" ? "flex" : "none";
document.getElementById("mic-btn").style.display =
mode === "voice" ? "flex" : "none";
}
function sendMessage() {
const input = document.getElementById("user-input");
const message = input.value.trim();
if (!message) return;
addMessage(message, "user");
input.value = "";
fetch(`${API_BASE}/send_message`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
message,
persona: currentPersona,
session_id: currentSessionId,
}),
})
.then((res) => {
if (!res.ok) {
// If server returns an error status (like 500), handle it
return res.json().then((err) => Promise.reject(err));
}
return res.json();
})
.then((data) => {
if (data.reply) {
addMessage(data.reply, currentPersona);
}
})
.catch((error) => {
console.error("Error sending message:", error);
const errorMessage = error.message || "An unknown error occurred.";
alert(`A backend error occurred:\n\n${errorMessage}`);
document.querySelector(".user-message:last-child").remove();
});
}
function addMessage(text, sender) {
const container = document.getElementById("chat-messages");
const messageDiv = document.createElement("div");
if (sender === "user") {
messageDiv.className = "user-message";
messageDiv.textContent = text;
} else {
messageDiv.className = "bot-message";
messageDiv.innerHTML = `<strong>${sender}:</strong> ${text}`;
}
container.appendChild(messageDiv);
container.scrollTop = container.scrollHeight;
}
function loadHistory() {
fetch(`${API_BASE}/get_history`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
persona: currentPersona,
session_id: currentSessionId,
}),
})
.then((res) => res.json())
.then((data) => {
const chatBox = document.getElementById("chat-messages");
chatBox.innerHTML = "";
if (data.messages) {
data.messages.forEach((msg) => addMessage(msg.text, msg.speaker));
}
});
}
function deleteCurrentSession() {
if (!currentSessionId || currentSessionId === "default") {
alert(
"The 'default' session cannot be deleted. Please use the Session History view to delete named sessions."
);
return;
}
deleteSessionFromList(currentSessionId);
}
function showSessionHistory() {
fetch(`${API_BASE}/get_sessions`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ persona: currentPersona }),
})
.then((res) => res.json())
.then((data) => {
const chatBox = document.getElementById("chat-messages");
// This is the corrected line: It now looks for data.sessions
const sessionList =
data.sessions && data.sessions.length > 0
? data.sessions
.map(
(id) => `
<li class="session-item">
<span class="session-item-name" onclick="loadSession('${id}')">${id}</span>
${
id !== "default"
? `<button class="delete-session-btn" onclick="deleteSessionFromList('${id}')" title="Delete session"><i class="fas fa-trash-alt"></i></button>`
: ""
}
</li>
`
)
.join("")
: "<li>No saved sessions for this persona.</li>";
chatBox.innerHTML = `
<div class="session-list-container">
<h3>Session History for ${currentPersona}</h3>
<ul class="session-list">
${sessionList}
</ul>
<button class="mode-btn new-session-btn" onclick="createNewSession()">
<i class="fas fa-plus-circle"></i> Start New Session
</button>
</div>
`;
});
}
function deleteSessionFromList(sessionId) {
if (
confirm(
`Are you sure you want to delete the session '${sessionId}'? This cannot be undone.`
)
) {
fetch(`${API_BASE}/clear_history`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ persona: currentPersona, session_id: sessionId }),
})
.then((res) => {
if (!res.ok) {
return res.json().then((err) => Promise.reject(err));
}
return res.json();
})
.then(() => {
if (currentSessionId === sessionId) {
currentSessionId = "default";
loadHistory();
}
showSessionHistory();
})
.catch((error) => {
console.error("Error deleting session:", error);
const errorMessage = error.message || "An unknown error occurred.";
alert(`A backend error occurred while deleting:\n\n${errorMessage}`);
});
}
}
function loadSession(sessionId) {
currentSessionId = sessionId;
loadHistory();
}
function createNewSession() {
const newSessionName = prompt(
"Enter a name for the new session:",
`session-${Date.now()}`
);
if (
newSessionName &&
newSessionName.trim() !== "" &&
newSessionName.trim() !== "default"
) {
currentSessionId = newSessionName.trim().replace(/[^a-zA-Z0-9-_]/g, "");
loadHistory();
} else {
alert(
"Invalid session name. Please use a different name and do not use 'default'."
);
}
}
async function toggleRecording() {
if (isRecording) {
mediaRecorder.stop();
} else {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
isRecording = true;
document.getElementById("mic-btn").classList.add("recording");
audioChunks = [];
mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = () => {
isRecording = false;
document.getElementById("mic-btn").classList.remove("recording");
const audioBlob = new Blob(audioChunks, { type: "audio/webm" });
sendAudioToServer(audioBlob);
stream.getTracks().forEach((track) => track.stop());
};
mediaRecorder.start();
} catch (error) {
console.error("Error accessing microphone:", error);
alert(
"Could not access the microphone. Please check your browser permissions."
);
}
}
}
function sendAudioToServer(audioBlob) {
const formData = new FormData();
formData.append("audio_file", audioBlob, "recording.webm");
addMessage("...listening...", "user");
fetch(`${API_BASE}/transcribe_audio`, { method: "POST", body: formData })
.then((res) => res.json())
.then((data) => {
document.querySelector(".user-message:last-child").remove();
if (data.text && data.text.trim() !== "") {
const textToSendMessage = data.text;
const input = document.getElementById("user-input");
input.value = textToSendMessage;
sendMessage();
} else {
addMessage("Sorry, I couldn't hear anything.", currentPersona);
}
})
.catch((error) => {
console.error("Error transcribing audio:", error);
document.querySelector(".user-message:last-child").remove();
addMessage(
"Sorry, there was an error with transcription.",
currentPersona
);
});
}