| |
| |
| |
|
|
| |
| let currentConversationId = null; |
| let messageHistory = []; |
| let waitingForPermission = false; |
| let permissionReason = ''; |
|
|
| |
| document.addEventListener('DOMContentLoaded', function() { |
| const chatForm = document.getElementById('chat-form'); |
| const messageInput = document.getElementById('message-input'); |
| const chatMessages = document.getElementById('chat-messages'); |
| const loadingIndicator = document.getElementById('loading-indicator'); |
| const permissionModal = new bootstrap.Modal(document.getElementById('permission-modal')); |
| const permissionReasonElement = document.getElementById('permission-reason'); |
| const allowButton = document.getElementById('allow-button'); |
| const denyButton = document.getElementById('deny-button'); |
| |
| |
| if (!chatForm || !messageInput || !chatMessages) { |
| return; |
| } |
|
|
| |
| const activeConversationElement = document.getElementById('active-conversation'); |
| if (activeConversationElement) { |
| currentConversationId = activeConversationElement.dataset.conversationId; |
| loadMessages(currentConversationId); |
| } |
|
|
| |
| chatForm.addEventListener('submit', function(e) { |
| e.preventDefault(); |
| sendMessage(); |
| }); |
|
|
| |
| if (allowButton) { |
| allowButton.addEventListener('click', function() { |
| handlePermissionResponse(true); |
| }); |
| } |
| |
| if (denyButton) { |
| denyButton.addEventListener('click', function() { |
| handlePermissionResponse(false); |
| }); |
| } |
|
|
| |
| messageInput.addEventListener('keydown', function(e) { |
| if (e.key === 'Enter' && !e.shiftKey) { |
| e.preventDefault(); |
| sendMessage(); |
| } |
| }); |
|
|
| |
| |
| |
| function sendMessage() { |
| const message = messageInput.value.trim(); |
| |
| if (!message) { |
| return; |
| } |
| |
| |
| messageInput.value = ''; |
| |
| |
| addMessageToChat('user', message); |
| |
| |
| if (loadingIndicator) { |
| loadingIndicator.classList.remove('d-none'); |
| } |
| |
| |
| fetch('/api/chat', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| }, |
| body: JSON.stringify({ |
| message: message, |
| conversation_id: currentConversationId |
| }), |
| }) |
| .then(response => { |
| if (!response.ok) { |
| throw new Error('Network response was not ok'); |
| } |
| return response.json(); |
| }) |
| .then(data => { |
| |
| if (loadingIndicator) { |
| loadingIndicator.classList.add('d-none'); |
| } |
| |
| |
| if (data.conversation_id && !currentConversationId) { |
| currentConversationId = data.conversation_id; |
| |
| history.pushState( |
| {conversationId: currentConversationId}, |
| '', |
| `/conversation/${currentConversationId}` |
| ); |
| } |
| |
| |
| if (data.requires_permission) { |
| waitingForPermission = true; |
| permissionReason = data.permission_reason || 'performing this action'; |
| |
| |
| if (permissionReasonElement) { |
| permissionReasonElement.textContent = data.permission_reason || 'this operation'; |
| } |
| permissionModal.show(); |
| |
| |
| return; |
| } |
| |
| |
| addMessageToChat('ai', data.response); |
| }) |
| .catch(error => { |
| console.error('Error:', error); |
| |
| if (loadingIndicator) { |
| loadingIndicator.classList.add('d-none'); |
| } |
| |
| addMessageToChat('system', 'Error: Failed to get a response. Please try again.'); |
| }); |
| } |
|
|
| |
| |
| |
| function addMessageToChat(sender, content) { |
| |
| const messageElement = document.createElement('div'); |
| messageElement.className = `message ${sender}-message`; |
| |
| |
| const avatarElement = document.createElement('div'); |
| avatarElement.className = 'message-avatar'; |
| |
| if (sender === 'user') { |
| avatarElement.innerHTML = '<i class="fa fa-user"></i>'; |
| } else if (sender === 'ai') { |
| avatarElement.innerHTML = '<i class="fa fa-robot"></i>'; |
| } else { |
| avatarElement.innerHTML = '<i class="fa fa-exclamation-triangle"></i>'; |
| } |
| |
| |
| const contentElement = document.createElement('div'); |
| contentElement.className = 'message-content'; |
| |
| |
| contentElement.innerHTML = content.replace(/\n/g, '<br>'); |
| |
| |
| messageElement.appendChild(avatarElement); |
| messageElement.appendChild(contentElement); |
| |
| |
| chatMessages.appendChild(messageElement); |
| |
| |
| chatMessages.scrollTop = chatMessages.scrollHeight; |
| |
| |
| messageHistory.push({ |
| sender: sender, |
| content: content, |
| timestamp: new Date().toISOString() |
| }); |
| } |
|
|
| |
| |
| |
| function handlePermissionResponse(granted) { |
| if (!waitingForPermission) { |
| return; |
| } |
| |
| |
| permissionModal.hide(); |
| |
| |
| if (loadingIndicator) { |
| loadingIndicator.classList.remove('d-none'); |
| } |
| |
| |
| fetch('/api/permission', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| }, |
| body: JSON.stringify({ |
| permission_granted: granted, |
| operation: permissionReason, |
| conversation_id: currentConversationId |
| }), |
| }) |
| .then(response => response.json()) |
| .then(data => { |
| |
| if (loadingIndicator) { |
| loadingIndicator.classList.add('d-none'); |
| } |
| |
| |
| addMessageToChat('ai', data.response); |
| |
| |
| waitingForPermission = false; |
| permissionReason = ''; |
| }) |
| .catch(error => { |
| console.error('Error:', error); |
| |
| if (loadingIndicator) { |
| loadingIndicator.classList.add('d-none'); |
| } |
| |
| addMessageToChat('system', 'Error: Failed to process permission response.'); |
| |
| |
| waitingForPermission = false; |
| permissionReason = ''; |
| }); |
| } |
|
|
| |
| |
| |
| function loadMessages(conversationId) { |
| |
| |
| const messageElements = document.querySelectorAll('.message-history .message'); |
| |
| messageElements.forEach(element => { |
| const sender = element.classList.contains('user-message') ? 'user' : 'ai'; |
| const content = element.querySelector('.message-content').textContent; |
| |
| messageHistory.push({ |
| sender: sender, |
| content: content, |
| timestamp: new Date().toISOString() |
| }); |
| }); |
| } |
| }); |
|
|