File size: 13,036 Bytes
e3a84af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Global state
let conversationHistory = JSON.parse(localStorage.getItem('claudeChat_history')) || [];
let selectedModel = localStorage.getItem('claudeChat_selectedModel') || 'claude-sonnet-4-5';
let isProcessing = false;
let isStreaming = false;

// DOM Elements
const modelSelector = document.getElementById('model-selector');
const messageInput = document.getElementById('message-input');
const sendBtn = document.getElementById('send-btn');
const streamBtn = document.getElementById('stream-btn');
const chatContainer = document.getElementById('chat-container');
const messagesContainer = document.getElementById('messages');
const examplePrompts = document.getElementById('example-prompts');
const exampleButtons = document.querySelectorAll('.example-btn');
const statusBar = document.getElementById('status-bar');
const statusMessage = document.getElementById('status-message');
const retryBtn = document.getElementById('retry-btn');
const typingIndicator = document.getElementById('typing-indicator');
const charCount = document.getElementById('char-count');
const newChatBtn = document.getElementById('new-chat-btn');
const appTitle = document.getElementById('app-title');

// Initialize
document.addEventListener('DOMContentLoaded', () => {
    // Set initial model
    modelSelector.value = selectedModel;
    
    // Load conversation history
    renderConversationHistory();
    
    // Set up event listeners
    setupEventListeners();
    
    // Update character count
    updateCharCount();
});

// Set up event listeners
function setupEventListeners() {
    // Model selection
    modelSelector.addEventListener('change', handleModelChange);
    
    // Message input
    messageInput.addEventListener('input', updateCharCount);
    messageInput.addEventListener('keydown', handleKeyDown);
    
    // Send buttons
    sendBtn.addEventListener('click', () => sendMessage(false));
    streamBtn.addEventListener('click', () => sendMessage(true));
    
    // Example prompts
    exampleButtons.forEach(button => {
        button.addEventListener('click', () => {
            messageInput.value = button.textContent;
            messageInput.focus();
            updateCharCount();
        });
    });
    
    // Status bar
    retryBtn.addEventListener('click', retryLastMessage);
    
    // New chat
    newChatBtn.addEventListener('click', resetConversation);
    appTitle.addEventListener('click', resetConversation);
    
    // Auto-scroll
    chatContainer.addEventListener('scroll', handleScroll);
}

// Handle model change
function handleModelChange() {
    const newModel = modelSelector.value;
    const previousModel = selectedModel;
    selectedModel = newModel;
    localStorage.setItem('claudeChat_selectedModel', selectedModel);
    
    // If conversation exists, add system message
    if (conversationHistory.length > 0) {
        const modelName = modelSelector.options[modelSelector.selectedIndex].text;
        addSystemMessage(`Model changed to ${modelName} — previous context retained`);
    }
}

// Handle keyboard shortcuts
function handleKeyDown(e) {
    if (e.key === 'Enter' && !e.shiftKey) {
        e.preventDefault();
        if (!isProcessing && messageInput.value.trim()) {
            sendMessage(false);
        }
    }
    
    // Clear input shortcut
    if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
        e.preventDefault();
        messageInput.value = '';
        updateCharCount();
    }
    
    // New chat shortcut
    if ((e.ctrlKey || e.metaKey) && e.key === 'n') {
        e.preventDefault();
        resetConversation();
    }
}

// Update character count
function updateCharCount() {
    const count = messageInput.value.length;
    charCount.textContent = `${count}/10000`;
    
    if (count > 8000) {
        charCount.classList.add('text-red-500');
    } else {
        charCount.classList.remove('text-red-500');
    }
    
    // Enable/disable buttons
    const hasText = count > 0;
    sendBtn.disabled = !hasText || isProcessing;
    streamBtn.disabled = !hasText || isProcessing;
}

// Send message
async function sendMessage(stream = false) {
    const message = messageInput.value.trim();
    if (!message || isProcessing) return;
    
    isProcessing = true;
    isStreaming = stream;
    updateUIState();
    
    // Add user message to UI
    addUserMessage(message);
    
    // Clear input
    messageInput.value = '';
    updateCharCount();
    
    // Show typing indicator for non-streaming
    if (!stream) {
        typingIndicator.classList.remove('hidden');
        chatContainer.scrollTop = chatContainer.scrollHeight;
    }
    
    try {
        // Add to history
        conversationHistory.push({ role: 'user', content: message });
        saveConversation();
        
        // Call API
        if (stream) {
            await streamResponse(message);
        } else {
            await getResponse(message);
        }
    } catch (error) {
        handleError(error);
    } finally {
        isProcessing = false;
        isStreaming = false;
        updateUIState();
        typingIndicator.classList.add('hidden');
    }
}

// Get response from API (non-streaming)
async function getResponse(userMessage) {
    try {
        const response = await puter.ai.chat(userMessage, { 
            model: selectedModel,
            conversationHistory: conversationHistory 
        });
        
        const assistantMessage = response.message?.content?.[0]?.text || "Sorry, I couldn't process that.";
        
        // Add to history
        conversationHistory.push({ role: 'assistant', content: assistantMessage });
        saveConversation();
        
        // Add to UI
        addAssistantMessage(assistantMessage);
    } catch (error) {
        throw error;
    }
}

// Stream response from API
async function streamResponse(userMessage) {
    try {
        const response = await puter.ai.chat(userMessage, { 
            model: selectedModel,
            conversationHistory: conversationHistory,
            stream: true
        });
        
        // Create assistant message element
        const messageElement = document.createElement('div');
        messageElement.className = 'assistant-message bg-white p-4 rounded-lg shadow-sm';
        messageElement.innerHTML = `
            <div class="flex items-center mb-2">
                <div class="w-6 h-6 rounded-full bg-purple-500 flex items-center justify-center mr-2">
                    <span class="text-white text-xs">A</span>
                </div>
                <span class="font-semibold text-gray-700">Assistant</span>
            </div>
            <div class="text-gray-700"></div>
        `;
        
        const contentElement = messageElement.querySelector('.text-gray-700');
        messagesContainer.appendChild(messageElement);
        chatContainer.scrollTop = chatContainer.scrollHeight;
        
        let fullResponse = '';
        
        // Process stream
        for await (const chunk of response) {
            const text = chunk?.message?.content?.[0]?.text || '';
            fullResponse += text;
            contentElement.textContent = fullResponse;
            
            // Scroll to bottom
            chatContainer.scrollTop = chatContainer.scrollHeight;
        }
        
        // Add to history
        conversationHistory.push({ role: 'assistant', content: fullResponse });
        saveConversation();
    } catch (error) {
        throw error;
    }
}

// Add user message to UI
function addUserMessage(content) {
    const messageElement = document.createElement('div');
    messageElement.className = 'user-message bg-blue-500 text-white p-4 rounded-lg shadow-sm ml-auto';
    messageElement.innerHTML = `
        <div class="flex items-center mb-2">
            <div class="w-6 h-6 rounded-full bg-blue-300 flex items-center justify-center mr-2">
                <span class="text-blue-800 text-xs">Y</span>
            </div>
            <span class="font-semibold">You</span>
        </div>
        <div class="text-white">${content}</div>
    `;
    
    messagesContainer.appendChild(messageElement);
    chatContainer.scrollTop = chatContainer.scrollHeight;
    
    // Hide example prompts after first message
    if (conversationHistory.length === 0) {
        examplePrompts.classList.add('hidden');
    }
}

// Add assistant message to UI
function addAssistantMessage(content) {
    const messageElement = document.createElement('div');
    messageElement.className = 'assistant-message bg-white p-4 rounded-lg shadow-sm';
    messageElement.innerHTML = `
        <div class="flex items-center mb-2">
            <div class="w-6 h-6 rounded-full bg-purple-500 flex items-center justify-center mr-2">
                <span class="text-white text-xs">A</span>
            </div>
            <span class="font-semibold text-gray-700">Assistant</span>
        </div>
        <div class="text-gray-700">${content}</div>
    `;
    
    messagesContainer.appendChild(messageElement);
    chatContainer.scrollTop = chatContainer.scrollHeight;
}

// Add system message to UI
function addSystemMessage(content) {
    const messageElement = document.createElement('div');
    messageElement.className = 'system-message bg-gray-100 text-gray-600 p-3 rounded-lg text-center text-sm my-2';
    messageElement.textContent = content;
    
    messagesContainer.appendChild(messageElement);
    chatContainer.scrollTop = chatContainer.scrollHeight;
}

// Handle errors
function handleError(error) {
    console.error('Error:', error);
    showStatus(`Request failed: ${error.message}`, 'error');
    retryBtn.classList.remove('hidden');
}

// Retry last message
function retryLastMessage() {
    if (conversationHistory.length >= 2) {
        const lastUserMessage = conversationHistory[conversationHistory.length - 2].content;
        messageInput.value = lastUserMessage;
        updateCharCount();
        retryBtn.classList.add('hidden');
        sendMessage(isStreaming);
    }
}

// Reset conversation
function resetConversation() {
    conversationHistory = [];
    localStorage.removeItem('claudeChat_history');
    messagesContainer.innerHTML = `
        <div class="assistant-message bg-white p-4 rounded-lg shadow-sm">
            <div class="flex items-center mb-2">
                <div class="w-6 h-6 rounded-full bg-purple-500 flex items-center justify-center mr-2">
                    <span class="text-white text-xs">A</span>
                </div>
                <span class="font-semibold text-gray-700">Assistant</span>
            </div>
            <div class="text-gray-700">
                <p>Hello! I'm Claude, an AI assistant. How can I help you today?</p>
            </div>
        </div>
    `;
    examplePrompts.classList.remove('hidden');
    hideStatus();
}

// Save conversation to localStorage
function saveConversation() {
    localStorage.setItem('claudeChat_history', JSON.stringify(conversationHistory));
    localStorage.setItem('claudeChat_timestamp', Date.now());
}

// Render conversation history
function renderConversationHistory() {
    if (conversationHistory.length === 0) return;
    
    messagesContainer.innerHTML = '';
    examplePrompts.classList.add('hidden');
    
    conversationHistory.forEach(msg => {
        if (msg.role === 'user') {
            addUserMessage(msg.content);
        } else if (msg.role === 'assistant') {
            addAssistantMessage(msg.content);
        }
    });
}

// Update UI state based on processing status
function updateUIState() {
    if (isProcessing) {
        messageInput.disabled = true;
        sendBtn.disabled = true;
        streamBtn.disabled = true;
        modelSelector.disabled = true;
        messageInput.placeholder = "Waiting for response...";
    } else {
        messageInput.disabled = false;
        updateCharCount();
        modelSelector.disabled = false;
        messageInput.placeholder = "Type your message here... (Shift+Enter for new line)";
    }
}

// Show status message
function showStatus(message, type) {
    statusMessage.textContent = message;
    statusBar.className = `px-6 py-3 text-sm text-white ${type}`;
    statusBar.classList.remove('hidden');
    
    // Auto-hide success messages
    if (type === 'success') {
        setTimeout(hideStatus, 3000);
    }
    
    // Auto-hide error messages (but keep retry button)
    if (type === 'error') {
        setTimeout(() => {
            if (!retryBtn.classList.contains('hidden')) return;
            hideStatus();
        }, 5000);
    }
}

// Hide status message
function hideStatus() {
    statusBar.classList.add('hidden');
    retryBtn.classList.add('hidden');
}

// Handle scroll for auto-scroll detection
function handleScroll() {
    const threshold = 50;
    const atBottom = chatContainer.scrollTop + chatContainer.clientHeight >= chatContainer.scrollHeight - threshold;
    
    // In a real implementation, you might add a "scroll to bottom" button here
    // when the user scrolls up and new messages arrive
}