Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const messageArea = document.getElementById('messageArea');
+ const userInput = document.getElementById('userInput');
+ const sendButton = document.getElementById('sendButton');
+ const apiSettings = document.getElementById('apiSettings');
+
+ let apiConfig = {
+ provider: '',
+ apiKey: '',
+ endpoint: ''
+ };
+
+ // Load saved settings
+ const savedConfig = localStorage.getItem('apiConfig');
+ if (savedConfig) {
+ apiConfig = JSON.parse(savedConfig);
+ document.getElementById('provider').value = apiConfig.provider;
+ document.getElementById('apiKey').value = apiConfig.apiKey;
+ document.getElementById('endpoint').value = apiConfig.endpoint;
+ }
+
+ // Handle settings form submission
+ apiSettings.addEventListener('submit', (e) => {
+ e.preventDefault();
+
+ apiConfig = {
+ provider: document.getElementById('provider').value,
+ apiKey: document.getElementById('apiKey').value,
+ endpoint: document.getElementById('endpoint').value
+ };
+
+ localStorage.setItem('apiConfig', JSON.stringify(apiConfig));
+
+ // Show success message
+ const message = document.createElement('div');
+ message.textContent = 'Settings saved successfully!';
+ message.style.color = 'green';
+ message.style.marginTop = '1rem';
+ apiSettings.appendChild(message);
+
+ setTimeout(() => message.remove(), 3000);
+ });
+
+ // Handle message sending
+ function sendMessage() {
+ const message = userInput.value.trim();
+ if (!message) return;
+
+ // Add user message to chat
+ addMessageToChat('user', message);
+
+ // Clear input
+ userInput.value = '';
+
+ // Simulate AI response (replace with actual API call)
+ if (apiConfig.apiKey) {
+ addMessageToChat('ai', 'Processing...');
+ simulateAiResponse(message);
+ } else {
+ addMessageToChat('system', 'Please configure your AI provider settings first.');
+ }
+ }
+
+ function addMessageToChat(sender, content) {
+ const messageDiv = document.createElement('div');
+ messageDiv.classList.add('message', sender);
+ messageDiv.style.padding = '0.8rem';
+ messageDiv.style.margin = '0.5rem 0';
+ messageDiv.style.borderRadius = '8px';
+
+ if (sender === 'user') {
+ messageDiv.style.backgroundColor = '#e3f2fd';
+ messageDiv.style.marginLeft = 'auto';
+ messageDiv.style.maxWidth = '80%';
+ } else if (sender === 'ai') {
+ messageDiv.style.backgroundColor = '#f5f5f5';
+ messageDiv.style.marginRight = 'auto';
+ messageDiv.style.maxWidth = '80%';
+ } else {
+ messageDiv.style.backgroundColor = '#fff3e0';
+ messageDiv.style.textAlign = 'center';
+ }
+
+ messageDiv.textContent = content;
+ messageArea.appendChild(messageDiv);
+ messageArea.scrollTop = messageArea.scrollHeight;
+ }
+
+ function simulateAiResponse(userMessage) {
+ // Simulate API delay
+ setTimeout(() => {
+ // Remove "Processing..." message
+ messageArea.removeChild(messageArea.lastChild);
+
+ // Add AI response
+ const response = `I received your message: "${userMessage}". This is a simulated response. In a real implementation, this would come from the configured AI provider.`;
+ addMessageToChat('ai', response);
+ }, 1000);
+ }
+
+ // Event listeners
+ sendButton.addEventListener('click', sendMessage);
+ userInput.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter' && !e.shiftKey) {
+ e.preventDefault();
+ sendMessage();
+ }
+ });
+ });