File size: 17,968 Bytes
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255a635
 
 
 
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
255a635
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255a635
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255a635
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255a635
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c43b369
 
 
825c9b3
c43b369
255a635
c43b369
 
 
 
 
 
 
 
 
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
c43b369
 
 
 
 
ca51841
 
3e0294c
ca51841
 
 
3e0294c
 
 
 
ca51841
 
 
 
3e0294c
 
ca51841
 
255a635
ca51841
 
 
255a635
 
 
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255a635
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255a635
ca51841
 
 
255a635
 
 
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255a635
ca51841
 
 
255a635
 
 
ca51841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { store } from '../store.js';
import {
  streamCompletion,
  formatMessagesForApi,
  transcribeAudio,
} from '../api.js';
import { estimateThreadTokens, resolveContextConfig } from '../context.js';
import { Sidebar } from './sidebar.js';
import { Chat } from './chat.js';
import { InputBar } from './input-bar.js';
import { ModelPicker } from './model-picker.js';
import { SettingsModal } from './settings-modal.js';
import { icon } from '../icons.js';

export class App {
  constructor(rootEl) {
    this.root = rootEl;
    this.sidebar = new Sidebar();
    this.chat = new Chat();
    this.inputBar = new InputBar();
    this.modelPicker = new ModelPicker();
    this.settingsModal = new SettingsModal();
    this._sidebarOpen = false;
    // In-memory message cache keyed by convId.  Persists for the page session so
    // that media (video/image dataUrls) remains visible even after an auto-reset
    // clears localStorage, or when localStorage quota prevents persistence.
    this._sessionMessages = new Map();
  }

  init() {
    this._render();
    this._initTheme();
    this._syncSidebarLayout();
    this._bindEvents();
    this._loadCurrentConversation();
    this.settingsModal.render(); // pre-render (portal pattern)
    this._updateContextInfo();
    this.inputBar.focus();
  }

  // --- Session message cache helpers ---

  _pushSessionMsg(convId, msg) {
    if (!this._sessionMessages.has(convId)) this._sessionMessages.set(convId, []);
    this._sessionMessages.get(convId).push(msg);
  }

  _clearSessionMsgs(convId) {
    this._sessionMessages.delete(convId);
  }

  // Returns a conv-like object backed by in-memory messages when available,
  // so that media dataUrls survive auto-resets and localStorage quota failures.
  _sessionConvFor(conv) {
    if (!conv) return conv;
    const mem = this._sessionMessages.get(conv.id);
    return mem?.length ? { ...conv, messages: mem } : conv;
  }

  _render() {
    this.root.className = 'flex h-screen overflow-hidden bg-[var(--c-bg)]';

    // Sidebar
    const sidebarEl = this.sidebar.render();
    this.root.appendChild(sidebarEl);

    // Mobile overlay
    this._mobileOverlay = document.createElement('div');
    this._mobileOverlay.className = 'fixed inset-0 bg-black/50 z-20 hidden md:hidden';
    this._mobileOverlay.addEventListener('click', () => this._closeMobileSidebar());
    document.body.appendChild(this._mobileOverlay);

    // Main content
    const main = document.createElement('div');
    main.className = 'flex flex-col flex-1 min-w-0 h-full';
    main.id = 'main-content';

    // Header
    const header = document.createElement('header');
    header.className = 'flex items-center justify-between px-4 py-2.5 border-b border-[var(--c-top-bd)] bg-[var(--c-top)] flex-shrink-0 gap-3';
    header.innerHTML = `
      <div class="flex items-center gap-3">
        <button id="mobile-menu-btn" class="md:hidden p-1.5 rounded text-[var(--c-tx3)] hover:text-[var(--c-tx)] transition-colors" aria-label="Toggle sidebar">
          ${icon('menu')}
        </button>
        <div id="model-picker-mount"></div>
      </div>
      <div class="flex items-center gap-2">
        <button id="theme-toggle-btn" class="p-1.5 rounded-lg text-[var(--c-tx3)] hover:text-[var(--c-tx2)] hover:bg-[var(--c-top-el)] border border-transparent hover:border-[var(--c-top-bd)] transition-all" aria-label="Toggle theme">
          ${icon('sun')}
        </button>
        <button id="settings-btn" class="p-1.5 rounded-lg text-[var(--c-tx3)] hover:text-[var(--c-tx2)] hover:bg-[var(--c-top-el)] border border-transparent hover:border-[var(--c-top-bd)] transition-all" aria-label="Settings">
          ${icon('settings')}
        </button>
      </div>
    `;

    // Mount model picker
    header.querySelector('#model-picker-mount').appendChild(this.modelPicker.render());
    main.appendChild(header);

    // Chat area
    const chatWrapper = document.createElement('div');
    chatWrapper.className = 'flex-1 min-h-0 overflow-hidden';
    chatWrapper.appendChild(this.chat.render());
    main.appendChild(chatWrapper);

    // Input bar
    main.appendChild(this.inputBar.render());

    this.root.appendChild(main);
  }

  _bindEvents() {
    // Settings button
    this.root.querySelector('#settings-btn').addEventListener('click', () => {
      this.settingsModal.toggle();
    });

    // Theme toggle
    this.root.querySelector('#theme-toggle-btn')?.addEventListener('click', () => {
      this._toggleTheme();
    });

    // Mobile menu
    this.root.querySelector('#mobile-menu-btn')?.addEventListener('click', () => {
      this._toggleMobileSidebar();
    });

    window.addEventListener('resize', () => {
      this._syncSidebarLayout();
    });

    // New chat
    document.addEventListener('sidebar:newchat', () => this._newChat());

    // Select conversation
    document.addEventListener('sidebar:select', (e) => {
      this._selectConversation(e.detail.convId);
    });

    // Delete conversation
    document.addEventListener('sidebar:deleted', () => {
      const currentId = store.getCurrentConversationId();
      if (currentId) {
        this._selectConversation(currentId);
      } else {
        this.chat.clear();
        this.sidebar.update();
      }
    });

    // Send message
    document.addEventListener('inputbar:send', (e) => {
      this._handleSend(e.detail.text, e.detail.image, e.detail.video, e.detail.audio);
    });

    // Model change
    document.addEventListener('model:changed', () => {
      this.inputBar.setModel(this.modelPicker.getModel());
      this._updateContextInfo();
    });

    document.addEventListener('settings:changed', () => {
      this.inputBar.setModel(this.modelPicker.getModel());
      this._updateContextInfo();
    });

    document.addEventListener('models:changed', () => {
      this.modelPicker.setModels(store.getAvailableModels());
      this.inputBar.setModel(this.modelPicker.getModel());
      this._updateContextInfo();
    });
  }

  _loadCurrentConversation() {
    const currentId = store.getCurrentConversationId();
    if (currentId) {
      const conv = store.getCurrentConversation();
      if (conv) {
        this.chat.loadConversation(this._sessionConvFor(conv));
        this.modelPicker.syncToConversation(conv);
        this.inputBar.setModel(this.modelPicker.getModel());
        this._updateContextInfo();
        return;
      }
    }
    // No current or invalid current — pick first if exists
    const convs = store.getConversations();
    if (convs.length > 0) {
      this._selectConversation(convs[0].id);
    }
  }

  _newChat() {
    const model = this.modelPicker.getModel();
    const conv = store.createConversation(model);
    store.setCurrentConversationId(conv.id);
    this.chat.loadConversation(conv);
    this.sidebar.update();
    this._updateContextInfo();
    this.inputBar.focus();
    this._syncSidebarLayout();
  }

  _selectConversation(convId) {
    store.setCurrentConversationId(convId);
    const conv = store.getCurrentConversation();
    if (!conv) {
      this.chat.clear();
      this._updateContextInfo();
      return;
    }
    this.chat.loadConversation(this._sessionConvFor(conv));
    this.modelPicker.syncToConversation(conv);
    this.inputBar.setModel(this.modelPicker.getModel());
    this.sidebar.update();
    this._updateContextInfo();
    this.inputBar.focus();
    this._closeMobileSidebar();
  }

  async _handleSend(text, image, video, audio) {
    const settings = store.getSettings();
    const trimmedText = text.trim();

    // /clean — wipes both the API context and the visual chat display
    if (trimmedText === '/clean' && !image && !video && !audio) {
      const convId = store.getCurrentConversationId();
      if (convId) {
        store.clearMessages(convId);
        this._clearSessionMsgs(convId);
        this.chat.clear();
        this._updateContextInfo();
      }
      return;
    }

    if (trimmedText === '/reset' && !image && !video && !audio) {
      this._resetConversationContext(store.getCurrentConversationId(), 'manual /reset command');
      return;
    }

    // Ensure we have a conversation
    let convId = store.getCurrentConversationId();
    if (!convId) {
      const conv = store.createConversation(this.modelPicker.getModel());
      convId = conv.id;
      store.setCurrentConversationId(convId);
      this.sidebar.update();
    }

    const model = this.modelPicker.getModel();
    if (!model) {
      this.chat.showError('Error: no model selected for the current API Base URL');
      return;
    }

    if (audio) {
      await this._handleAudioTask({
        convId,
        model,
        settings,
        instruction: trimmedText,
        audio,
      });
      return;
    }

    const contextConfig = resolveContextConfig(settings, model);
    const userMessage = this._buildUserMessage(text, image, video);
    const preConv = store.getCurrentConversation();
    const projectedTokens = estimateThreadTokens(preConv?.messages, userMessage);

    if (preConv?.messages?.length && projectedTokens >= contextConfig.resetTokens) {
      this._resetConversationContext(
        convId,
        `estimated ${projectedTokens.toLocaleString()} tokens reached the ${contextConfig.resetPercent}% auto-reset threshold`
      );
    }

    // Add to store & render — wrapped so any localStorage/DOM error is caught
    let renderOk = false;
    try {
      this.chat.clearError();
      store.addMessage(convId, userMessage);
      this._pushSessionMsg(convId, userMessage);
      this.chat.appendUserMessage(userMessage);
      this._updateContextInfo();
      renderOk = true;
    } catch (err) {
      this.chat.showError(`Error: ${err.message}`);
      this.inputBar.setSending(false);
      this.inputBar.focus();
      return;
    }

    // Update title if first message
    const conv = store.getCurrentConversation();
    if (conv && conv.messages.length === 1) {
      const title = text.slice(0, 40) || (video ? 'Video message' : 'Image message');
      store.updateConversationTitle(convId, title);
      this.sidebar.update();
    }

    // Disable input
    this.inputBar.setSending(true);
    this.chat.showTypingIndicator();

    // Get conversation history for API.
    // Use preConv (read before the store write) + userMessage directly so the
    // current message is always included even if the localStorage save failed,
    // and formatMessagesForApi can correctly identify it as the latest message.
    const apiMessages = formatMessagesForApi([...(preConv?.messages || []), userMessage]);

    try {
      let started = false;
      let fullText = '';

      for await (const chunk of streamCompletion(settings.baseUrl, settings.apiKey, model, apiMessages)) {
        if (!started) {
          this.chat.startAssistantMessage();
          started = true;
        }
        fullText += chunk;
        this.chat.appendToAssistantMessage(chunk);
      }

      if (!started) this.chat.startAssistantMessage();

      // Finalize
      this.chat.finalizeAssistantMessage(fullText);
      const assistantMsg = {
        role: 'assistant',
        content: fullText,
        timestamp: new Date().toISOString(),
      };
      store.addMessage(convId, assistantMsg);
      this._pushSessionMsg(convId, assistantMsg);
      this._updateContextInfo();
      this.sidebar.update();
    } catch (err) {
      this.chat.showError(`Error: ${err.message}`);
    } finally {
      this.inputBar.setSending(false);
      this.inputBar.focus();
    }
  }

  _buildUserMessage(text, image, video) {
    let content;
    if (image) {
      content = [
        { type: 'text', text: text || '' },
        { type: 'image_url', image_url: { url: image.dataUrl } },
      ];
    } else if (video) {
      content = [
        { type: 'text', text: text || '' },
        { type: 'video_url', video_url: { url: video.dataUrl } },
      ];
    } else {
      content = text;
    }

    return {
      role: 'user',
      content,
      timestamp: new Date().toISOString(),
    };
  }

  _buildAudioUserMessage(audio, instruction = '') {
    const summary = `Audio upload: ${audio.file?.name || 'audio'}`;
    const suffix = instruction ? `\nInstruction: ${instruction}` : '';
    return {
      role: 'user',
      content: `${summary}${suffix}`,
      timestamp: new Date().toISOString(),
      meta: {
        type: 'audio',
        fileName: audio.file?.name || 'audio',
      },
    };
  }

  async _handleAudioTask({ convId, model, settings, instruction, audio }) {
    const userMessage = this._buildAudioUserMessage(audio, instruction);
    store.addMessage(convId, userMessage);
    this._pushSessionMsg(convId, userMessage);
    this.chat.appendUserMessage(userMessage);
    this._updateContextInfo();

    const conv = store.getCurrentConversation();
    if (conv && conv.messages.length === 1) {
      const title = (instruction || userMessage.content).slice(0, 40) || 'Audio task';
      store.updateConversationTitle(convId, title);
      this.sidebar.update();
    }

    this.inputBar.setSending(true);
    this.chat.showTypingIndicator();

    try {
      const transcript = await transcribeAudio(settings.baseUrl, settings.apiKey, model, audio.file);

      if (!instruction) {
        this.chat.hideTypingIndicator();
        this.chat.startAssistantMessage();
        this.chat.finalizeAssistantMessage(transcript);
        const assistantMsg = {
          role: 'assistant',
          content: transcript,
          timestamp: new Date().toISOString(),
        };
        store.addMessage(convId, assistantMsg);
        this._pushSessionMsg(convId, assistantMsg);
        this._updateContextInfo();
        this.sidebar.update();
        return;
      }

      const followUpMessage = {
        role: 'user',
        content: [
          `The following text came from an uploaded audio file.`,
          `Task: ${instruction}`,
          `Audio text:`,
          transcript,
        ].join('\n\n'),
        timestamp: new Date().toISOString(),
      };

      const currentConv = store.getCurrentConversation();
      const apiMessages = [
        ...formatMessagesForApi(currentConv.messages),
        { role: 'user', content: followUpMessage.content },
      ];

      this.chat.hideTypingIndicator();
      this.chat.showSystemMessage('Audio transcribed — applying instruction');
      this.chat.startAssistantMessage();

      let fullText = '';
      for await (const chunk of streamCompletion(settings.baseUrl, settings.apiKey, model, apiMessages)) {
        fullText += chunk;
        this.chat.appendToAssistantMessage(chunk);
      }

      this.chat.finalizeAssistantMessage(fullText);
      const assistantMsg = {
        role: 'assistant',
        content: fullText,
        timestamp: new Date().toISOString(),
      };
      store.addMessage(convId, assistantMsg);
      this._pushSessionMsg(convId, assistantMsg);
      this._updateContextInfo();
      this.sidebar.update();
    } catch (err) {
      this.chat.showError(`Error: ${err.message}`);
    } finally {
      this.inputBar.setSending(false);
      this.inputBar.focus();
    }
  }

  _initTheme() {
    const saved = localStorage.getItem('theme') || 'dark';
    document.documentElement.classList.toggle('dark', saved === 'dark');
    this._updateThemeBtn();
  }

  _toggleTheme() {
    const isDark = document.documentElement.classList.toggle('dark');
    localStorage.setItem('theme', isDark ? 'dark' : 'light');
    this._updateThemeBtn();
  }

  _updateThemeBtn() {
    const btn = this.root.querySelector('#theme-toggle-btn');
    if (!btn) return;
    const isDark = document.documentElement.classList.contains('dark');
    btn.innerHTML = isDark ? icon('sun') : icon('moon');
    btn.setAttribute('aria-label', isDark ? 'Switch to light mode' : 'Switch to dark mode');
  }

  _resetConversationContext(convId, reason) {
    if (!convId) return;
    const conv = store.getConversations().find((item) => item.id === convId);
    if (!conv) return;

    const { maxTokens } = resolveContextConfig(store.getSettings(), this.modelPicker.getModel());
    const currentTokens = estimateThreadTokens(conv.messages);
    store.clearMessages(convId);
    this.chat.showSystemMessage(
      `Context /reset — cleared ${currentTokens.toLocaleString()} estimated tokens (${reason}; window ${maxTokens.toLocaleString()})`
    );
    this._updateContextInfo();
  }

  _updateContextInfo() {
    const conv = store.getCurrentConversation();
    const model = this.modelPicker.getModel();
    const { maxTokens, warnTokens } = resolveContextConfig(store.getSettings(), model);
    const currentTokens = estimateThreadTokens(conv?.messages);
    this.inputBar.setContextInfo(currentTokens, maxTokens, warnTokens);
  }

  _toggleMobileSidebar() {
    if (this._isDesktopLayout()) return;
    this._sidebarOpen = !this._sidebarOpen;
    this._syncSidebarLayout();
  }

  _closeMobileSidebar() {
    if (this._isDesktopLayout()) {
      this._syncSidebarLayout();
      return;
    }
    this._sidebarOpen = false;
    this._syncSidebarLayout();
  }

  _isDesktopLayout() {
    return window.innerWidth >= 768;
  }

  _syncSidebarLayout() {
    const sidebar = this.root.querySelector('#sidebar');
    if (!sidebar) return;

    if (this._isDesktopLayout()) {
      sidebar.classList.remove('-translate-x-full', 'translate-x-0', 'fixed', 'inset-y-0', 'left-0', 'z-30');
      this._mobileOverlay?.classList.add('hidden');
      this._sidebarOpen = true;
      return;
    }

    sidebar.classList.add('fixed', 'inset-y-0', 'left-0', 'z-30');

    if (this._sidebarOpen) {
      sidebar.classList.remove('-translate-x-full');
      sidebar.classList.add('translate-x-0');
      this._mobileOverlay?.classList.remove('hidden');
    } else {
      sidebar.classList.add('-translate-x-full');
      sidebar.classList.remove('translate-x-0');
      this._mobileOverlay?.classList.add('hidden');
    }
  }
}