Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const chatMessages = document.getElementById('chatMessages');
+ const userInput = document.getElementById('userInput');
+ const sendButton = document.getElementById('sendButton');
+ const providerForm = document.getElementById('providerForm');
+ const providerSelect = document.getElementById('provider');
+ const customEndpoint = document.getElementById('customEndpoint');
+
+ // Load saved settings
+ loadSettings();
+
+ // Event listeners
+ sendButton.addEventListener('click', sendMessage);
+ userInput.addEventListener('keypress', (e) => {
+ if (e.key === 'Enter' && !e.shiftKey) {
+ e.preventDefault();
+ sendMessage();
+ }
+ });
+
+ providerSelect.addEventListener('change', () => {
+ customEndpoint.style.display =
+ providerSelect.value === 'custom' ? 'block' : 'none';
+ });
+
+ providerForm.addEventListener('submit', (e) => {
+ e.preventDefault();
+ saveSettings();
+ });
+
+ function sendMessage() {
+ const message = userInput.value.trim();
+ if (!message) return;
+
+ // Add user message to chat
+ appendMessage('user', message);
+ userInput.value = '';
+
+ // Get settings
+ const settings = getSettings();
+ if (!settings.apiKey) {
+ appendMessage('system', 'Please configure your AI provider settings first.');
+ return;
+ }
+
+ // Simulate AI response (replace with actual API call)
+ appendMessage('thinking', '...');
+ setTimeout(() => {
+ const lastMessage = chatMessages.lastElementChild;
+ if (lastMessage.classList.contains('thinking')) {
+ chatMessages.removeChild(lastMessage);
+ }
+ appendMessage('ai', 'This is a simulated response. Implement actual API calls here.');
+ }, 1000);
+ }
+
+ function appendMessage(type, content) {
+ const messageDiv = document.createElement('div');
+ messageDiv.classList.add('message', type);
+
+ const contentP = document.createElement('p');
+ contentP.textContent = content;
+
+ messageDiv.appendChild(contentP);
+ chatMessages.appendChild(messageDiv);
+ chatMessages.scrollTop = chatMessages.scrollHeight;
+ }
+
+ function saveSettings() {
+ const settings = {
+ provider: providerSelect.value,
+ apiKey: document.getElementById('apiKey').value,
+ endpoint: document.getElementById('endpoint').value
+ };
+ localStorage.setItem('aiChatSettings', JSON.stringify(settings));
+ appendMessage('system', 'Settings saved successfully!');
+ }
+
+ function loadSettings() {
+ const settings = getSettings();
+ if (settings) {
+ providerSelect.value = settings.provider || 'openai';
+ document.getElementById('apiKey').value = settings.apiKey || '';
+ document.getElementById('endpoint').value = settings.endpoint || '';
+ customEndpoint.style.display =
+ settings.provider === 'custom' ? 'block' : 'none';
+ }
+ }
+
+ function getSettings() {
+ try {
+ return JSON.parse(localStorage.getItem('aiChatSettings')) || {};
+ } catch {
+ return {};
+ }
+ }
+ });