document.addEventListener('DOMContentLoaded', () => { // DOM Elements const apiKeyInput = document.getElementById('api-key'); const toggleKeyBtn = document.getElementById('toggle-key'); const modelSelect = document.getElementById('model-select'); const temperatureSlider = document.getElementById('temperature'); const tempValue = document.getElementById('temp-value'); const startChatBtn = document.getElementById('start-chat'); const modelInfo = document.getElementById('model-info'); const chatContainer = document.getElementById('chat-container'); const userInput = document.getElementById('user-input'); const sendBtn = document.getElementById('send-btn'); const clearChatBtn = document.getElementById('clear-chat'); const themeToggle = document.getElementById('theme-toggle'); const themeIcon = document.getElementById('theme-icon'); // State let chatActive = false; let conversationHistory = []; let models = []; let selectedModel = null; let embeddedDocument = null; let embedApiKey = ''; let embedModel = null; // Initialize fetchModels(); setupEventListeners(); setupEmbedListeners(); // Functions function setupEventListeners() { // Toggle API key visibility toggleKeyBtn.addEventListener('click', () => { const type = apiKeyInput.getAttribute('type') === 'password' ? 'text' : 'password'; apiKeyInput.setAttribute('type', type); feather.replace(); }); // Temperature slider temperatureSlider.addEventListener('input', () => { tempValue.textContent = temperatureSlider.value; }); // Model selection modelSelect.addEventListener('change', () => { const modelId = modelSelect.value; selectedModel = models.find(m => m.id === modelId); updateModelInfo(); }); // Start chat button startChatBtn.addEventListener('click', startChat); // Send message sendBtn.addEventListener('click', sendMessage); userInput.addEventListener('keydown', (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); } }); // Clear chat clearChatBtn.addEventListener('click', clearChat); // Theme toggle themeToggle.addEventListener('click', toggleTheme); } async function fetchModels() { try { const response = await fetch('https://openrouter.ai/api/v1/models'); const data = await response.json(); models = data.data || []; populateModelSelect(); } catch (error) { console.error('Error fetching models:', error); addSystemMessage('Failed to load models. Please try again later.'); } } function populateModelSelect() { modelSelect.innerHTML = ''; models.forEach(model => { const option = document.createElement('option'); option.value = model.id; option.textContent = `${model.name} (${model.id})`; modelSelect.appendChild(option); }); } function updateModelInfo() { if (!selectedModel) { modelInfo.innerHTML = 'Select a model to see details'; return; } modelInfo.innerHTML = `
${selectedModel.name}
${selectedModel.id}
${(selectedModel.pricing.completion * 1000).toFixed(4)} / 1K tokens
${(selectedModel.pricing.prompt * 1000).toFixed(4)} prompt
${selectedModel.description || 'No description available'}
Prompt: ${(selectedModel.pricing.prompt * 1000).toFixed(4)} / 1K tokens
Completion: ${(selectedModel.pricing.completion * 1000).toFixed(4)} / 1K tokens
${selectedModel.pricing.request ? `Request: ${selectedModel.pricing.request} / call
` : ''}