forked from Celestial317/PsyAssist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (180 loc) · 7.64 KB
/
Copy pathscript.js
File metadata and controls
214 lines (180 loc) · 7.64 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
// Global variables
let sessionId = null;
let currentTone = "friendly";
let shouldSaveSession = true;
document.addEventListener('DOMContentLoaded', function() {
const chatMessages = document.getElementById('chat-messages');
const userInput = document.getElementById('user-message');
const sendButton = document.getElementById('send-btn');
const crisisResources = document.getElementById('crisis-resources');
// Mobile sidebar toggle
const menuToggle = document.querySelector('.menu-toggle');
const sidebar = document.querySelector('.sidebar');
menuToggle.addEventListener('click', function() {
sidebar.classList.toggle('active');
});
// Close sidebar when clicking outside on mobile
document.addEventListener('click', function(e) {
if (window.innerWidth <= 768 &&
!sidebar.contains(e.target) &&
!menuToggle.contains(e.target) &&
sidebar.classList.contains('active')) {
sidebar.classList.remove('active');
}
});
// Function to add a message to the chat
function addMessage(message, isUser) {
const messageDiv = document.createElement('div');
messageDiv.className = isUser ? 'message user-message' : 'message bot-message';
const messagePara = document.createElement('p');
messagePara.textContent = message;
messageDiv.appendChild(messagePara);
if (!isUser) {
const disclaimer = document.createElement('small');
disclaimer.textContent = 'Remember: I\'m an AI assistant, not a replacement for professional mental health care.';
messageDiv.appendChild(disclaimer);
}
chatMessages.appendChild(messageDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
}
// Function to send message to server
async function sendMessage() {
const message = userInput.value.trim();
if (message === '') return;
// Add user message to chat
addMessage(message, true);
// Clear input
userInput.value = '';
try {
// Send message to server with session ID and tone
const response = await fetch('/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: message,
session_id: sessionId,
tone: currentTone // Include the tone parameter
}),
});
const data = await response.json();
// Update session ID if provided
if (data.session_id) {
sessionId = data.session_id;
}
// Display bot response
addMessage(data.response, false);
// Show crisis resources if crisis detected
if (data.crisis) {
crisisResources.style.display = 'block';
}
} catch (error) {
console.error('Error:', error);
addMessage("I'm having trouble connecting. Please try again later.", false);
}
}
// Event listeners
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
// NEW CHAT button functionality
document.querySelector('.btn').addEventListener('click', function() {
// Clear chat messages except the first welcome message
while (chatMessages.children.length > 1) {
chatMessages.removeChild(chatMessages.lastChild);
}
// Hide crisis resources
if (crisisResources) {
crisisResources.style.display = 'none';
}
// Send current session ID to be saved before creating a new one
startNewSession(sessionId);
});
// Add tone setting functionality
const toneLinks = document.querySelectorAll('.dropdown-content a');
toneLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
// Check if this is a tone selection link (parent's button contains "Tone")
const parentButton = link.parentElement.previousElementSibling;
if (!parentButton || !parentButton.textContent.includes('Tone')) {
return; // Skip if not a tone link
}
const tone = this.textContent.toLowerCase();
currentTone = tone; // Set the current tone
// Update button text to show selected tone
parentButton.innerHTML = `<i class="fas fa-sliders-h"></i> ${this.textContent}`;
// Provide user feedback
addMessage(`I'll adjust my tone to be more ${tone} now.`, false);
console.log("Tone set to:", currentTone);
});
});
// Load chat history
loadChatHistory();
// When user navigates away, save the session
window.addEventListener('beforeunload', function() {
if (sessionId && shouldSaveSession) {
navigator.sendBeacon('/chat', JSON.stringify({
message: "_session_end_",
session_id: sessionId,
tone: currentTone, // Include the tone parameter
end_chat: true
}));
}
});
// Start a session when the page loads
startNewSession();
});
// Function to start a new chat session
async function startNewSession(oldSessionId = null) {
try {
const response = await fetch('/new-chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
session_id: oldSessionId
}),
});
const data = await response.json();
sessionId = data.session_id;
console.log("New session started:", sessionId);
} catch (error) {
console.error("Error starting new session:", error);
}
}
// Add a function to load chat history
async function loadChatHistory() {
try {
const response = await fetch('/chat-history');
const data = await response.json();
// Clear existing history items
const historyDropdown = document.querySelector('.dropdown-content');
if (!historyDropdown) return;
// Remove all but the "View All History" option
while (historyDropdown.children.length > 1) {
historyDropdown.removeChild(historyDropdown.firstChild);
}
// Add history items
data.history.forEach(session => {
const date = new Date(session.timestamp.replace('_', 'T').replace(/_/g, ':')).toLocaleString();
const historyItem = document.createElement('a');
historyItem.href = '#';
historyItem.textContent = `${session.summary} (${date})`;
// Insert at the beginning
if (historyDropdown.firstChild) {
historyDropdown.insertBefore(historyItem, historyDropdown.firstChild);
} else {
historyDropdown.appendChild(historyItem);
}
});
} catch (error) {
console.error("Error loading chat history:", error);
}
}