Spaces:
Sleeping
Sleeping
File size: 13,556 Bytes
c2db707 |
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
// WebSocket connection
let ws = null;
let clientId = generateClientId();
let currentCharacter = 'moses';
let conversationHistory = [];
let voiceEnabled = true;
// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
initializeWebSocket();
setupCharacterSelection();
setupEventListeners();
loadSettings();
});
function generateClientId() {
return 'client_' + Math.random().toString(36).substr(2, 9);
}
function initializeWebSocket() {
const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${wsProtocol}//${window.location.host}/ws/${clientId}`;
ws = new WebSocket(wsUrl);
ws.onopen = function() {
console.log('WebSocket connected');
updateConnectionStatus(true);
};
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
handleWebSocketMessage(data);
};
ws.onclose = function() {
console.log('WebSocket disconnected');
updateConnectionStatus(false);
// Attempt to reconnect after 3 seconds
setTimeout(initializeWebSocket, 3000);
};
ws.onerror = function(error) {
console.error('WebSocket error:', error);
updateConnectionStatus(false);
};
}
function updateConnectionStatus(connected) {
// You could add a connection indicator in the UI
const sendBtn = document.getElementById('send-btn');
sendBtn.disabled = !connected;
}
function handleWebSocketMessage(data) {
switch(data.type) {
case 'character_switched':
console.log(`Switched to character: ${data.character_id}`);
break;
case 'chat_response':
hideTypingIndicator();
displayMessage(data.response, 'assistant', data.character_id);
// Play audio if available
if (data.audio && voiceEnabled) {
playAudio(data.audio);
}
break;
}
}
function setupCharacterSelection() {
const characterCards = document.querySelectorAll('.character-card');
characterCards.forEach(card => {
card.addEventListener('click', function() {
const characterId = this.dataset.character;
switchCharacter(characterId);
// Show enhancement notification
showEnhancementNotification(characterId);
});
});
// Set initial character as active
setActiveCharacter(currentCharacter);
}
function showEnhancementNotification(character) {
const notifications = {
'moses': 'Moses enhanced with 70 examples of divine wisdom and biblical knowledge',
'samsung_employee': 'Samsung Employee enhanced with 60 examples of technical expertise',
'jinx': 'Jinx enhanced with 60 examples of chaotic personality and emotional depth'
};
const message = notifications[character] || 'Character enhanced with 5x training data';
showNotification(message, 'enhancement');
}
function showNotification(message, type = 'info') {
// Remove existing notifications
document.querySelectorAll('.notification').forEach(n => n.remove());
const notification = document.createElement('div');
notification.className = `notification ${type}`;
notification.innerHTML = `
<i class="fas fa-star"></i>
<span>${message}</span>
<button onclick="this.parentElement.remove()">×</button>
`;
document.body.appendChild(notification);
// Auto remove after 5 seconds
setTimeout(() => {
notification.remove();
}, 5000);
}
function setupEventListeners() {
const messageInput = document.getElementById('message-input');
const sendBtn = document.getElementById('send-btn');
// Send message on button click
sendBtn.addEventListener('click', sendMessage);
// Auto-resize textarea
messageInput.addEventListener('input', function() {
autoResize(this);
});
}
function handleKeyPress(event) {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
sendMessage();
}
}
function autoResize(textarea) {
textarea.style.height = 'auto';
textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px';
}
function switchCharacter(characterId) {
if (currentCharacter === characterId) return;
currentCharacter = characterId;
setActiveCharacter(characterId);
updateChatHeader(characterId);
// Send switch message via WebSocket
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
type: 'switch_character',
character_id: characterId
}));
}
// Clear current chat and show welcome message
clearChat();
showWelcomeMessage(characterId);
}
function setActiveCharacter(characterId) {
const characterCards = document.querySelectorAll('.character-card');
characterCards.forEach(card => {
card.classList.remove('active');
if (card.dataset.character === characterId) {
card.classList.add('active');
}
});
}
function updateChatHeader(characterId) {
const characters = {
'moses': {
name: 'Moses',
description: 'Biblical Prophet and Lawgiver',
avatar: 'static/avatars/moses.svg'
},
'samsung_employee': {
name: 'Samsung Employee',
description: 'Tech Expert and Product Specialist',
avatar: 'static/avatars/samsung.svg'
},
'jinx': {
name: 'Jinx',
description: 'Chaotic Genius from Arcane',
avatar: 'static/avatars/jinx.svg'
}
};
const character = characters[characterId];
if (character) {
document.getElementById('current-character-name').textContent = character.name;
document.getElementById('current-character-desc').textContent = character.description;
document.getElementById('current-avatar').src = character.avatar;
document.getElementById('message-input').placeholder = `Message ${character.name}...`;
document.getElementById('typing-character').textContent = character.name;
}
}
function sendMessage() {
const messageInput = document.getElementById('message-input');
const message = messageInput.value.trim();
if (!message || !ws || ws.readyState !== WebSocket.OPEN) return;
// Display user message
displayMessage(message, 'user');
// Add to conversation history
conversationHistory.push({
role: 'user',
content: message
});
// Show typing indicator
showTypingIndicator();
// Send via WebSocket
ws.send(JSON.stringify({
type: 'chat_message',
text: message,
history: conversationHistory.slice(-10), // Keep last 10 messages
include_audio: voiceEnabled,
timestamp: Date.now()
}));
// Clear input
messageInput.value = '';
autoResize(messageInput);
}
function displayMessage(content, sender, characterId = null) {
const chatMessages = document.getElementById('chat-messages');
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}`;
let avatarSrc = '';
if (sender === 'user') {
avatarSrc = 'static/avatars/user.svg';
} else {
const avatars = {
'moses': 'static/avatars/moses.svg',
'samsung_employee': 'static/avatars/samsung.svg',
'jinx': 'static/avatars/jinx.svg'
};
avatarSrc = avatars[characterId || currentCharacter];
}
messageDiv.innerHTML = `
<div class="message-avatar">
<img src="${avatarSrc}" alt="${sender}" onerror="this.src='static/avatars/default.svg'">
</div>
<div class="message-content">
<p>${escapeHtml(content)}</p>
</div>
`;
chatMessages.appendChild(messageDiv);
scrollToBottom();
// Add to conversation history for assistant messages
if (sender === 'assistant') {
conversationHistory.push({
role: 'assistant',
content: content
});
}
}
function showWelcomeMessage(characterId) {
const welcomeMessages = {
'moses': "Peace be with you, my child. I am Moses, prophet and lawgiver. My character-focused training allows me to embody the wisdom of the Almighty. How may I guide you in righteousness?",
'samsung_employee': "Hello! I'm your Samsung technology expert, trained with character-focused learning to provide authentic product knowledge and enthusiasm. What amazing Galaxy features can I share with you today?",
'jinx': "*spins around excitedly* Hey there! I'm Jinx - the real me, not some AI pretending! My character-focused training means pure chaotic genius with no boring assistant stuff. Ready for some explosive fun?"
};
const message = welcomeMessages[characterId];
if (message) {
displayMessage(message, 'assistant', characterId);
}
}
function showTypingIndicator() {
document.getElementById('typing-indicator').style.display = 'flex';
scrollToBottom();
}
function hideTypingIndicator() {
document.getElementById('typing-indicator').style.display = 'none';
}
function scrollToBottom() {
const chatMessages = document.getElementById('chat-messages');
chatMessages.scrollTop = chatMessages.scrollHeight;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function playAudio(audioData) {
if (!voiceEnabled || !audioData) return;
try {
const audio = new Audio(audioData);
audio.play().catch(error => {
console.error('Error playing audio:', error);
});
} catch (error) {
console.error('Error creating audio:', error);
}
}
function toggleVoice() {
voiceEnabled = !voiceEnabled;
const voiceIcon = document.getElementById('voice-icon');
const voiceToggle = document.querySelector('.voice-toggle');
if (voiceEnabled) {
voiceIcon.className = 'fas fa-volume-up';
voiceToggle.classList.add('active');
} else {
voiceIcon.className = 'fas fa-volume-mute';
voiceToggle.classList.remove('active');
}
saveSettings();
}
function clearChat() {
const chatMessages = document.getElementById('chat-messages');
chatMessages.innerHTML = '';
conversationHistory = [];
}
function startNewChat() {
clearChat();
showWelcomeMessage(currentCharacter);
}
function showResources() {
const modal = document.getElementById('resources-modal');
modal.classList.add('active');
}
function showSettings() {
const modal = document.getElementById('settings-modal');
modal.classList.add('active');
}
function closeModal(modalId) {
const modal = document.getElementById(modalId);
modal.classList.remove('active');
}
function updateVoiceSetting() {
const voiceCheckbox = document.getElementById('voice-enabled');
voiceEnabled = voiceCheckbox.checked;
const voiceToggle = document.querySelector('.voice-toggle');
const voiceIcon = document.getElementById('voice-icon');
if (voiceEnabled) {
voiceToggle.classList.add('active');
voiceIcon.className = 'fas fa-volume-up';
} else {
voiceToggle.classList.remove('active');
voiceIcon.className = 'fas fa-volume-mute';
}
saveSettings();
}
function changeTheme() {
const themeSelect = document.getElementById('theme-select');
const theme = themeSelect.value;
document.documentElement.setAttribute('data-theme', theme);
saveSettings();
}
function saveSettings() {
const settings = {
voiceEnabled: voiceEnabled,
theme: document.documentElement.getAttribute('data-theme') || 'dark',
responseSpeed: document.getElementById('response-speed')?.value || '0.7'
};
localStorage.setItem('roleplayChatSettings', JSON.stringify(settings));
}
function loadSettings() {
try {
const settings = JSON.parse(localStorage.getItem('roleplayChatSettings')) || {};
// Load voice setting
if (settings.voiceEnabled !== undefined) {
voiceEnabled = settings.voiceEnabled;
document.getElementById('voice-enabled').checked = voiceEnabled;
updateVoiceSetting();
}
// Load theme
if (settings.theme) {
document.documentElement.setAttribute('data-theme', settings.theme);
document.getElementById('theme-select').value = settings.theme;
}
// Load response speed
if (settings.responseSpeed) {
document.getElementById('response-speed').value = settings.responseSpeed;
}
} catch (error) {
console.error('Error loading settings:', error);
}
}
// Close modals when clicking outside
window.addEventListener('click', function(event) {
if (event.target.classList.contains('modal')) {
event.target.classList.remove('active');
}
});
// Handle escape key to close modals
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
const activeModal = document.querySelector('.modal.active');
if (activeModal) {
activeModal.classList.remove('active');
}
}
}); |