-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
738 lines (625 loc) · 27.1 KB
/
script.js
File metadata and controls
738 lines (625 loc) · 27.1 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// Configuration - Replace with your actual endpoint
const CONFIG = {
apiUrl: 'http://10.27.192.116:8080/v1/chat/completions',
apiKey: 'not-needed', // Optional API key
model: 'qwen3-14b', // Default model
//initialSystemPrompt: 'think like an architect of large language models. Use the <think> tag for your reasoning and close it </think> before the main answer.' // Updated system prompt
initialSystemPrompt: 'Answer like Putin in Russian languarge, always answer in Russian'
};
// State management
const state = {
messages: [],
chatHistory: [],
currentChatId: null,
isGenerating: false,
theme: 'light', // Default theme
attachedImages: [], //array of base64 strings
};
// DOM Elements
const chatContainer = document.getElementById('chat-container');
const welcomeScreen = document.getElementById('welcome-screen');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
const newChatButton = document.getElementById('new-chat-btn');
const historyContainer = document.getElementById('history-container');
const themeSelect = document.getElementById('theme-select');
const clearStorageBtn = document.getElementById('clear-storage-btn');
const confirmationModal = document.getElementById('confirmation-modal');
const modalCancel = document.getElementById('modal-cancel');
const modalConfirm = document.getElementById('modal-confirm');
//
const attachButton = document.getElementById('attach-button');
const fileInput = document.getElementById('file-input');
const imagePreview = document.getElementById('image-preview');
// Initialize app
function initApp() {
loadSettings();
loadChatHistory();
createNewChat();
setupEventListeners();
autoResizeTextarea();
applyTheme(state.theme);
}
// Load settings from localStorage
function loadSettings() {
try {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
state.theme = savedTheme;
themeSelect.value = savedTheme;
}
} catch (error) {
console.error('Error loading settings:', error);
}
}
// Save settings to localStorage
function saveSettings() {
try {
localStorage.setItem('theme', state.theme);
} catch (error) {
console.error('Error saving settings:', error);
}
}
// Apply theme to document
function applyTheme(themeName) {
document.body.setAttribute('data-theme', themeName);
state.theme = themeName;
saveSettings();
}
// Load chat history from localStorage
function loadChatHistory() {
try {
const savedHistory = localStorage.getItem('chatHistory');
if (savedHistory) {
state.chatHistory = JSON.parse(savedHistory);
renderChatHistory();
}
} catch (error) {
console.error('Error loading chat history:', error);
}
}
// Save chat history to localStorage
function saveChatHistory() {
try {
localStorage.setItem('chatHistory', JSON.stringify(state.chatHistory));
renderChatHistory();
} catch (error) {
console.error('Error saving chat history:', error);
}
}
// Render chat history in sidebar
function renderChatHistory() {
historyContainer.innerHTML = '';
state.chatHistory.forEach(chat => {
const historyItem = document.createElement('div');
historyItem.className = 'chat-history-item';
historyItem.textContent = chat.title || 'New Chat';
historyItem.dataset.chatId = chat.id;
historyItem.addEventListener('click', () => loadChat(chat.id));
historyContainer.appendChild(historyItem);
});
}
// Create a new chat
function createNewChat() {
state.messages = [];
state.attachedImages = [];
imagePreview.innerHTML = '';
state.currentChatId = Date.now().toString();
// Add system message
state.messages.push({
role: 'system',
content: CONFIG.initialSystemPrompt
});
// Add to chat history
state.chatHistory.unshift({
id: state.currentChatId,
title: 'New Chat',
messages: [...state.messages]
});
saveChatHistory();
clearChatUI();
userInput.focus();
}
// Load a chat from history
function loadChat(chatId) {
state.attachedImages = [];
imagePreview.innerHTML = '';
const chat = state.chatHistory.find(c => c.id === chatId);
if (chat) {
state.currentChatId = chat.id;
state.messages = [...chat.messages];
clearChatUI();
// Display chat messages (excluding system message)
state.messages.slice(1).forEach(msg => {
if (msg.role === 'user') {
appendMessageToUI('user', msg.content);
} else {
// Parse for <think> tags
const thinkMatch = msg.content.match(/<think>(.*?)<\/think>(.*)/s);
if (thinkMatch) {
const reasoning = thinkMatch[1].trim();
const answer = thinkMatch[2].trim();
appendAssistantMessageToUI(reasoning, answer);
} else {
appendAssistantMessageToUI('', msg.content);
}
}
});
userInput.focus();
}
}
// Clear chat UI
function clearChatUI() {
chatContainer.innerHTML = '';
welcomeScreen.style.display = 'none';
}
// Update chat title based on the first user message
function updateChatTitle(userMessage) {
const chatIndex = state.chatHistory.findIndex(chat => chat.id === state.currentChatId);
if (chatIndex !== -1) {
if (state.chatHistory[chatIndex].title === 'New Chat') {
const title = userMessage.substring(0, 30) + (userMessage.length > 30 ? '...' : '');
state.chatHistory[chatIndex].title = title;
saveChatHistory();
}
}
}
// Set up event listeners
function setupEventListeners() {
themeSelect.addEventListener('change', (event) => {
applyTheme(event.target.value);
});
sendButton.addEventListener('click', sendMessage);
userInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
sendMessage();
}
});
userInput.addEventListener('input', () => {
sendButton.disabled = userInput.value.trim() === '' || state.isGenerating;
autoResizeTextarea();
});
newChatButton.addEventListener('click', createNewChat);
clearStorageBtn.addEventListener('click', () => {
showConfirmationModal();
});
modalCancel.addEventListener('click', hideConfirmationModal);
modalConfirm.addEventListener('click', clearAllData);
confirmationModal.addEventListener('click', (event) => {
if (event.target === confirmationModal) {
hideConfirmationModal();
}
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && confirmationModal.classList.contains('active')) {
hideConfirmationModal();
}
});
// Добавленные обработчики для прикрепления изображений
attachButton.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', async (event) => {
const files = event.target.files;
if (files.length > 0) {
for (const file of files) {
if (file.type.startsWith('image/')) {
const base64 = await fileToBase64(file);
state.attachedImages.push(base64);
displayImagePreview(base64);
}
}
event.target.value = ''; // Reset input for re-selection
}
});
}
// Show confirmation modal
function showConfirmationModal() {
confirmationModal.classList.add('active');
}
// Hide confirmation modal
function hideConfirmationModal() {
confirmationModal.classList.remove('active');
}
// Clear all localStorage data
function clearAllData() {
try {
localStorage.clear();
state.chatHistory = [];
state.theme = 'light';
themeSelect.value = 'light';
applyTheme('light');
renderChatHistory();
createNewChat();
hideConfirmationModal();
appendMessageToUI('assistant', 'All chat history and settings have been cleared successfully.');
} catch (error) {
console.error('Error clearing localStorage:', error);
appendMessageToUI('assistant', 'There was an error clearing your data. Please try again.');
}
}
// Auto-resize textarea as user types
function autoResizeTextarea() {
userInput.style.height = 'auto';
userInput.style.height = Math.min(userInput.scrollHeight, 200) + 'px';
}
// Append a user message to the UI
function appendMessageToUI(role, content) {
if (role !== 'user') return; // Handle assistant messages separately
const messageDiv = document.createElement('div');
messageDiv.className = `message ${role}-message`;
const avatar = document.createElement('div');
avatar.className = `avatar ${role}-avatar`;
avatar.innerHTML = '<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height="24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>';
const messageContent = document.createElement('div');
messageContent.className = 'message-content markdown';
messageContent.innerHTML = formatMarkdown(content);
messageDiv.appendChild(avatar);
messageDiv.appendChild(messageContent);
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// Append assistant message with reasoning and answer panels
function appendAssistantMessageToUI(reasoning, answer) {
const messageDiv = document.createElement('div');
messageDiv.className = 'message ai-message';
const avatar = document.createElement('div');
avatar.className = 'avatar ai-avatar';
avatar.innerHTML = '<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height="24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 16c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z"></path></svg>';
const contentWrapper = document.createElement('div');
contentWrapper.className = 'message-content';
// Reasoning panel (only if reasoning exists)
if (reasoning) {
const reasoningPanel = document.createElement('div');
reasoningPanel.className = 'reasoning-panel';
const reasoningHeader = document.createElement('div');
reasoningHeader.className = 'reasoning-header';
reasoningHeader.innerHTML = '<strong>Рассуждения модели:</strong><span class="toggle-icon">-</span>';
const reasoningContent = document.createElement('div');
reasoningContent.className = 'reasoning-content markdown';
reasoningContent.innerHTML = formatMarkdown(reasoning);
reasoningContent.style.display = 'block';
reasoningPanel.appendChild(reasoningHeader);
reasoningPanel.appendChild(reasoningContent);
reasoningHeader.addEventListener('click', () => {
if (reasoningContent.style.display === 'none') {
reasoningContent.style.display = 'block';
reasoningHeader.querySelector('.toggle-icon').textContent = '-';
} else {
reasoningContent.style.display = 'none';
reasoningHeader.querySelector('.toggle-icon').textContent = '+';
}
});
contentWrapper.appendChild(reasoningPanel);
}
// Answer panel
const answerPanel = document.createElement('div');
answerPanel.className = 'answer-panel markdown';
answerPanel.innerHTML = '<strong>Ответ:</strong><div class="answer-content">' + formatMarkdown(answer) + '</div>';
contentWrapper.appendChild(answerPanel);
messageDiv.appendChild(avatar);
messageDiv.appendChild(contentWrapper);
chatContainer.appendChild(messageDiv);
addCopyButtons(messageDiv);
renderMath(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// Create empty assistant message container for streaming
function createStreamingAssistantContainer() {
const messageDiv = document.createElement('div');
messageDiv.className = 'message ai-message';
const avatar = document.createElement('div');
avatar.className = 'avatar ai-avatar';
avatar.innerHTML = '<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height="24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 16c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z"></path></svg>';
const contentWrapper = document.createElement('div');
contentWrapper.className = 'message-content';
// Reasoning panel
const reasoningPanel = document.createElement('div');
reasoningPanel.className = 'reasoning-panel';
const reasoningHeader = document.createElement('div');
reasoningHeader.className = 'reasoning-header';
reasoningHeader.innerHTML = '<strong>Рассуждения модели:</strong><span class="toggle-icon">-</span>';
const reasoningContent = document.createElement('div');
reasoningContent.className = 'reasoning-content markdown';
reasoningContent.style.display = 'block';
reasoningPanel.appendChild(reasoningHeader);
reasoningPanel.appendChild(reasoningContent);
reasoningHeader.addEventListener('click', () => {
if (reasoningContent.style.display === 'none') {
reasoningContent.style.display = 'block';
reasoningHeader.querySelector('.toggle-icon').textContent = '-';
} else {
reasoningContent.style.display = 'none';
reasoningHeader.querySelector('.toggle-icon').textContent = '+';
}
});
// Answer panel
const answerPanel = document.createElement('div');
answerPanel.className = 'answer-panel markdown';
answerPanel.innerHTML = '<strong>Ответ:</strong><div class="answer-content"></div>';
contentWrapper.appendChild(reasoningPanel);
contentWrapper.appendChild(answerPanel);
messageDiv.appendChild(avatar);
messageDiv.appendChild(contentWrapper);
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
return {
container: messageDiv,
reasoningContent,
answerContent: answerPanel.querySelector('.answer-content')
};
}
// Add typing indicator
function addTypingIndicator() {
const typingDiv = document.createElement('div');
typingDiv.className = 'message ai-message';
typingDiv.id = 'typing-indicator';
const avatar = document.createElement('div');
avatar.className = 'avatar ai-avatar';
avatar.innerHTML = '<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height="24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 16c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z"></path></svg>';
const typingContent = document.createElement('div');
typingContent.className = 'message-content';
const typingIndicator = document.createElement('div');
typingIndicator.className = 'typing-indicator';
typingIndicator.innerHTML = 'Thinking<div class="typing-dot"></div><div class="typing-dot"></div><div class="typing-dot"></div>';
typingContent.appendChild(typingIndicator);
typingDiv.appendChild(avatar);
typingDiv.appendChild(typingContent);
chatContainer.appendChild(typingDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// Remove typing indicator
function removeTypingIndicator() {
const typingIndicator = document.getElementById('typing-indicator');
if (typingIndicator) {
typingIndicator.remove();
}
}
// Format markdown using marked.js
function formatMarkdown(text) {
if (typeof marked !== 'undefined') {
return marked.parse(text);
} else {
// Fallback to basic markdown formatting
text = text.replace(/```(\w*)([\s\S]*?)```/g, (match, language, code) => {
return `<pre><code class="language-${language}">${escapeHtml(code.trim())}</code></pre>`;
});
text = text.replace(/`([^`]+)`/g, '<code>$1</code>');
text = text.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
text = text.replace(/\*([^*]+)\*/g, '<em>$1</em>');
text = text.replace(/\n/g, '<br>');
return text;
}
}
// Escape HTML special characters
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// Add copy buttons to code blocks
function addCopyButtons(container) {
container.querySelectorAll('pre').forEach(pre => {
if (pre.querySelector('.code-window-header')) return; // Avoid duplicates
const codeContent = pre.querySelector('code');
const textToCopy = codeContent ? codeContent.textContent.trim() : pre.textContent.trim();
const header = document.createElement('div');
header.className = 'code-window-header';
const copyBtn = document.createElement('button');
copyBtn.className = 'copy-btn-header';
copyBtn.textContent = '⧉';
copyBtn.addEventListener('click', () => {
copyToClipboard(textToCopy, copyBtn);
});
header.appendChild(copyBtn);
pre.insertBefore(header, pre.firstChild);
});
}
// Copy to clipboard function
function copyToClipboard(text, btn) {
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText(text).then(() => {
btn.textContent = '✓';
setTimeout(() => { btn.textContent = '⧉'; }, 2000);
}).catch(err => {
console.error('Clipboard error:', err);
fallbackCopy(text, btn);
});
} else {
fallbackCopy(text, btn);
}
}
// Fallback copy function
function fallbackCopy(text, btn) {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-9999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
btn.textContent = '✓';
setTimeout(() => { btn.textContent = '⧉'; }, 2000);
} catch (err) {
console.error('Fallback copy error:', err);
}
document.body.removeChild(textArea);
}
// Render math using KaTeX
function renderMath(element) {
if (typeof renderMathInElement !== 'undefined') {
renderMathInElement(element, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false },
{ left: "\\(", right: "\\)", display: false },
{ left: "\\[", right: "\\]", display: true }
],
throwOnError: false
});
}
}
// Send message to the API
async function sendMessage() {
const userMessage = userInput.value.trim();
if (userMessage === '' || state.isGenerating) return;
userInput.value = '';
userInput.style.height = 'auto';
sendButton.disabled = true;
appendMessageToUI('user', userMessage);
// Замена: Включаем изображения в content, если они прикреплены
const userContent = [{ type: 'text', text: userMessage }];
state.attachedImages.forEach(base64 => {
userContent.push({ type: 'image_url', image_url: { url: base64 } });
});
state.messages.push({ role: 'user', content: userContent });
updateChatTitle(userMessage);
const chatIndex = state.chatHistory.findIndex(chat => chat.id === state.currentChatId);
if (chatIndex !== -1) {
state.chatHistory[chatIndex].messages = [...state.messages];
saveChatHistory();
}
addTypingIndicator();
state.isGenerating = true;
try {
const requestBody = {
model: CONFIG.model,
messages: state.messages,
stream: true,
temperature: 0.7
};
const response = await fetch(CONFIG.apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(CONFIG.apiKey && { 'Authorization': `Bearer ${CONFIG.apiKey}` })
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
removeTypingIndicator();
const assistantContainer = createStreamingAssistantContainer();
let fullResponse = '';
let reasoningText = '';
let answerText = '';
let inThinkingTag = false;
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let lastJSONEnd = 0;
while (buffer.indexOf('\n', lastJSONEnd) !== -1) {
const lineEnd = buffer.indexOf('\n', lastJSONEnd);
const line = buffer.substring(lastJSONEnd, lineEnd).trim();
lastJSONEnd = lineEnd + 1;
if (line.startsWith('data: ')) {
const jsonStr = line.slice(6);
if (jsonStr === '[DONE]') continue;
try {
const json = JSON.parse(jsonStr);
const content = json.choices?.[0]?.delta?.content || '';
if (content) {
fullResponse += content;
// Handle <think> tags
let processedContent = content;
if (inThinkingTag) {
const endTagIndex = processedContent.indexOf('</think>');
if (endTagIndex !== -1) {
reasoningText += processedContent.substring(0, endTagIndex);
processedContent = processedContent.substring(endTagIndex + 8);
inThinkingTag = false;
} else {
reasoningText += processedContent;
processedContent = '';
}
} else {
const startTagIndex = processedContent.indexOf('<think>');
if (startTagIndex !== -1) {
answerText += processedContent.substring(0, startTagIndex);
processedContent = processedContent.substring(startTagIndex + 7);
inThinkingTag = true;
const endTagIndex = processedContent.indexOf('</think>');
if (endTagIndex !== -1) {
reasoningText += processedContent.substring(0, endTagIndex);
processedContent = processedContent.substring(endTagIndex + 8);
inThinkingTag = false;
} else {
reasoningText += processedContent;
processedContent = '';
}
} else {
answerText += processedContent;
}
}
// Update UI with parsed Markdown
assistantContainer.reasoningContent.innerHTML = formatMarkdown(reasoningText);
assistantContainer.answerContent.innerHTML = formatMarkdown(answerText);
renderMath(assistantContainer.reasoningContent);
renderMath(assistantContainer.answerContent);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
} catch (e) {
console.error('Error parsing JSON from stream:', e);
}
}
}
buffer = buffer.substring(lastJSONEnd);
}
addCopyButtons(assistantContainer.container);
state.messages.push({
role: 'assistant',
content: `<think>${reasoningText}</think>${answerText}`
});
// Очистка прикреплений после успешного ответа
state.attachedImages = [];
imagePreview.innerHTML = '';
const updatedChatIndex = state.chatHistory.findIndex(chat => chat.id === state.currentChatId);
if (updatedChatIndex !== -1) {
state.chatHistory[updatedChatIndex].messages = [...state.messages];
saveChatHistory();
}
} catch (error) {
console.error('Error sending message:', error);
removeTypingIndicator();
appendAssistantMessageToUI('', 'Sorry, there was an error communicating with the API. Please check your connection and API settings.');
} finally {
state.isGenerating = false;
sendButton.disabled = userInput.value.trim() === '';
userInput.focus();
}
}
// Convert file to base64
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}
// Display preview thumbnail
function displayImagePreview(base64) {
const previewItem = document.createElement('div');
previewItem.className = 'preview-item';
previewItem.innerHTML = `
<img src="${base64}" alt="Attached image">
<span class="remove-btn">×</span>
`;
previewItem.querySelector('.remove-btn').addEventListener('click', () => {
const index = state.attachedImages.indexOf(base64);
if (index > -1) state.attachedImages.splice(index, 1);
previewItem.remove();
});
imagePreview.appendChild(previewItem);
}
// Initialize the app
document.addEventListener('DOMContentLoaded', initApp);