File size: 11,196 Bytes
0c6c135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a59bde4
 
 
0c6c135
22321ea
 
a59bde4
 
0c6c135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbc244f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6c135
 
fbc244f
0c6c135
 
 
fbc244f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6c135
 
fbc244f
 
0c6c135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a59bde4
 
 
 
 
 
 
 
 
0c6c135
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = '<option value="">Select a model...</option>';
        
        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 = `
            <div class="flex items-start justify-between">
                <div>
                    <p class="font-medium text-gray-200">${selectedModel.name}</p>
                    <p class="text-sm text-gray-400">${selectedModel.id}</p>
                </div>
                ${selectedModel.pricing ? `
                    <div class="text-right">
                        <p class="text-xs font-medium text-yellow-300">${(selectedModel.pricing.completion * 1000).toFixed(4)} / 1K tokens</p>
                        <p class="text-xs text-gray-400">${(selectedModel.pricing.prompt * 1000).toFixed(4)} prompt</p>
                    </div>
                ` : ''}
            </div>
            
            <p class="mt-2 text-sm">${selectedModel.description || 'No description available'}</p>
            
            <div class="mt-3 grid grid-cols-2 gap-2 text-xs">
                <div class="bg-gray-700 p-2 rounded">
                    <span class="text-gray-400 block">Provider</span>
                    <span class="font-medium">${selectedModel.provider || 'Unknown'}</span>
                </div>
                <div class="bg-gray-700 p-2 rounded">
                    <span class="text-gray-400 block">Context</span>
                    <span class="font-medium">${selectedModel.context_length ? selectedModel.context_length.toLocaleString() : 'Unknown'} tokens</span>
                </div>
                ${selectedModel.top_provider ? `
                <div class="bg-gray-700 p-2 rounded">
                    <span class="text-gray-400 block">Top Provider</span>
                    <span class="font-medium">${selectedModel.top_provider.name}</span>
                </div>
                ` : ''}
                ${selectedModel.features ? `
                <div class="bg-gray-700 p-2 rounded">
                    <span class="text-gray-400 block">Features</span>
                    <span class="font-medium">${Object.keys(selectedModel.features).filter(k => selectedModel.features[k]).join(', ')}</span>
                </div>
                ` : ''}
            </div>
            
            ${selectedModel.pricing ? `
            <div class="mt-3 bg-gray-700 p-3 rounded-lg">
                <h4 class="text-xs font-medium text-gray-300 mb-1">Pricing Details</h4>
                <div class="text-xs">
                    <p>Prompt: ${(selectedModel.pricing.prompt * 1000).toFixed(4)} / 1K tokens</p>
                    <p>Completion: ${(selectedModel.pricing.completion * 1000).toFixed(4)} / 1K tokens</p>
                    ${selectedModel.pricing.request ? `<p>Request: ${selectedModel.pricing.request} / call</p>` : ''}
                </div>
            </div>
            ` : ''}
`;
    }

    function startChat() {
        const apiKey = apiKeyInput.value.trim();
        const modelId = modelSelect.value;

        if (!apiKey) {
            addSystemMessage('Please enter your OpenRouter API key');
            return;
        }

        if (!modelId) {
            addSystemMessage('Please select a model');
            return;
        }

        chatActive = true;
        conversationHistory = [];
        userInput.disabled = false;
        sendBtn.disabled = false;
        startChatBtn.disabled = true;
        startChatBtn.classList.add('opacity-50', 'cursor-not-allowed');

        addSystemMessage(`Chat started with ${selectedModel.name}. Ready to chat!`);
    }

    async function sendMessage() {
        const message = userInput.value.trim();
        if (!message || !chatActive) return;

        // Add user message to chat
        addMessageToChat('user', message);
        userInput.value = '';
        
        // Show loading indicator
        const loadingId = addLoadingIndicator();

        try {
            const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${apiKeyInput.value.trim()}`
                },
                body: JSON.stringify({
                    model: selectedModel.id,
                    messages: [
                        ...conversationHistory,
                        { role: 'user', content: message }
                    ],
                    temperature: parseFloat(temperatureSlider.value)
                })
            });

            if (!response.ok) {
                throw new Error(`API error: ${response.status}`);
            }

            const data = await response.json();
            const aiResponse = data.choices[0].message.content;

            // Remove loading indicator and add AI response
            removeLoadingIndicator(loadingId);
            addMessageToChat('assistant', aiResponse);

            // Update conversation history
            conversationHistory.push(
                { role: 'user', content: message },
                { role: 'assistant', content: aiResponse }
            );
        } catch (error) {
            console.error('Error:', error);
            removeLoadingIndicator(loadingId);
            addSystemMessage(`Error: ${error.message}`);
        }
    }

    function addMessageToChat(role, content) {
        const messageDiv = document.createElement('div');
        messageDiv.className = `chat-bubble ${role === 'user' ? 'user-bubble' : 'ai-bubble'} fade-in`;
        messageDiv.textContent = content;
        chatContainer.appendChild(messageDiv);
        chatContainer.scrollTop = chatContainer.scrollHeight;
    }

    function addSystemMessage(content) {
        const messageDiv = document.createElement('div');
        messageDiv.className = 'text-center text-gray-400 text-sm py-2 fade-in';
        messageDiv.textContent = content;
        chatContainer.appendChild(messageDiv);
        chatContainer.scrollTop = chatContainer.scrollHeight;
    }

    function addLoadingIndicator() {
        const id = `loading-${Date.now()}`;
        const loadingDiv = document.createElement('div');
        loadingDiv.id = id;
        loadingDiv.className = 'chat-bubble ai-bubble fade-in';
        
        const dots = document.createElement('div');
        dots.className = 'loading-dots';
        dots.innerHTML = '<span></span><span></span><span></span>';
        
        loadingDiv.appendChild(dots);
        chatContainer.appendChild(loadingDiv);
        chatContainer.scrollTop = chatContainer.scrollHeight;
        
        return id;
    }

    function removeLoadingIndicator(id) {
        const element = document.getElementById(id);
        if (element) {
            element.remove();
        }
    }

    function clearChat() {
        if (!chatActive) return;
        
        conversationHistory = [];
        chatContainer.innerHTML = '';
        addSystemMessage('Chat cleared. Ready for new messages.');
    }
    function setupEmbedListeners() {
        document.addEventListener('process-document', (e) => {
            embeddedDocument = e.detail.content;
            // Here you would typically call your embedding API
            // For demo purposes, we'll just store the content
            addSystemMessage('Document ready for processing! Use commands like:');
            addSystemMessage('"Summarize chapter 3" or "Rewrite paragraph 5 to be more formal"');
        });
    }

    function toggleTheme() {
        const html = document.documentElement;
        html.classList.toggle('dark');
        
        if (html.classList.contains('dark')) {
            themeIcon.setAttribute('data-feather', 'moon');
        } else {
            themeIcon.setAttribute('data-feather', 'sun');
        }
        feather.replace();
    }
});