Spaces:
Running

michdubai commited on
Commit
adf8f44
·
verified ·
1 Parent(s): ea9c25c

Upload 2 files

Browse files
Files changed (1) hide show
  1. frontend/js/main.js +56 -15
frontend/js/main.js CHANGED
@@ -18,6 +18,7 @@ class GeminiApp {
18
  this.isStreaming = false;
19
  this.currentMode = null;
20
  this.currentResponseElement = null;
 
21
 
22
  this._clientTools = {};
23
 
@@ -171,6 +172,7 @@ class GeminiApp {
171
  if (welcome) welcome.remove();
172
 
173
  this.currentResponseElement = null;
 
174
 
175
  const userElement = document.createElement('div');
176
  userElement.className = 'message-row user-message';
@@ -195,6 +197,7 @@ class GeminiApp {
195
  if (this.isStreaming) return;
196
  this.currentMode = mode;
197
  this.currentResponseElement = null;
 
198
 
199
  const sidebar = document.getElementById('sidebar');
200
  if (window.innerWidth <= 768 && sidebar) {
@@ -327,6 +330,7 @@ class GeminiApp {
327
 
328
  case 'user_text':
329
  this.currentResponseElement = null;
 
330
  if (messagesContainer) {
331
  const welcome = messagesContainer.querySelector('.system-welcome');
332
  if (welcome) welcome.remove();
@@ -347,9 +351,9 @@ class GeminiApp {
347
  }
348
  break;
349
 
350
- case 'session_resumption':
351
- if (response.new_handle) {
352
- localStorage.setItem('gemini_resumption_handle', response.new_handle);
353
  this.uiController.showInfo('Session resumption token stored.');
354
  }
355
  break;
@@ -364,6 +368,7 @@ class GeminiApp {
364
 
365
  case 'turn_complete':
366
  this.currentResponseElement = null;
 
367
  break;
368
 
369
  case 'generation_complete':
@@ -451,21 +456,13 @@ class GeminiApp {
451
  appendGeminiToken(textToken) {
452
  if (!textToken) return;
453
 
454
- const checkToken = textToken.trim().toLowerCase();
455
- if (
456
- checkToken.includes('thought:') ||
457
- checkToken.includes('analyzing') ||
458
- checkToken.includes('clarifying')
459
- ) {
460
- return;
461
- }
462
-
463
  const messagesContainer = document.getElementById('messages');
464
  if (!messagesContainer) return;
465
 
466
  const welcome = messagesContainer.querySelector('.system-welcome');
467
  if (welcome) welcome.remove();
468
 
 
469
  if (!this.currentResponseElement) {
470
  this.currentResponseElement = document.createElement('div');
471
  this.currentResponseElement.className = 'message-row gemini-message';
@@ -475,17 +472,61 @@ class GeminiApp {
475
  <span class="speaker-label">Gemini</span>
476
  </div>
477
  <div class="message-content gemini-content">
 
 
 
 
 
 
 
478
  <span class="text-payload"></span>
479
  </div>
480
  `;
481
  messagesContainer.appendChild(this.currentResponseElement);
482
  }
483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484
  const textContainer = this.currentResponseElement.querySelector('.text-payload');
485
- if (textContainer) {
486
- textContainer.textContent += textToken;
487
- messagesContainer.scrollTop = messagesContainer.scrollHeight;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
488
  }
 
 
 
489
  }
490
 
491
  handleWebSocketClose() {
 
18
  this.isStreaming = false;
19
  this.currentMode = null;
20
  this.currentResponseElement = null;
21
+ this.isReasoningMode = false;
22
 
23
  this._clientTools = {};
24
 
 
172
  if (welcome) welcome.remove();
173
 
174
  this.currentResponseElement = null;
175
+ this.isReasoningMode = false;
176
 
177
  const userElement = document.createElement('div');
178
  userElement.className = 'message-row user-message';
 
197
  if (this.isStreaming) return;
198
  this.currentMode = mode;
199
  this.currentResponseElement = null;
200
+ this.isReasoningMode = false;
201
 
202
  const sidebar = document.getElementById('sidebar');
203
  if (window.innerWidth <= 768 && sidebar) {
 
330
 
331
  case 'user_text':
332
  this.currentResponseElement = null;
333
+ this.isReasoningMode = false;
334
  if (messagesContainer) {
335
  const welcome = messagesContainer.querySelector('.system-welcome');
336
  if (welcome) welcome.remove();
 
351
  }
352
  break;
353
 
354
+ case 'session_resumption_ready':
355
+ if (response.handle) {
356
+ localStorage.setItem('gemini_resumption_handle', response.handle);
357
  this.uiController.showInfo('Session resumption token stored.');
358
  }
359
  break;
 
368
 
369
  case 'turn_complete':
370
  this.currentResponseElement = null;
371
+ this.isReasoningMode = false;
372
  break;
373
 
374
  case 'generation_complete':
 
456
  appendGeminiToken(textToken) {
457
  if (!textToken) return;
458
 
 
 
 
 
 
 
 
 
 
459
  const messagesContainer = document.getElementById('messages');
460
  if (!messagesContainer) return;
461
 
462
  const welcome = messagesContainer.querySelector('.system-welcome');
463
  if (welcome) welcome.remove();
464
 
465
+ // 1. If this is the start of a new response, create the Gemini message structure
466
  if (!this.currentResponseElement) {
467
  this.currentResponseElement = document.createElement('div');
468
  this.currentResponseElement.className = 'message-row gemini-message';
 
472
  <span class="speaker-label">Gemini</span>
473
  </div>
474
  <div class="message-content gemini-content">
475
+ <details class="reasoning-container" style="margin-bottom: 10px; display: none;">
476
+ <summary style="cursor: pointer; font-size: 0.85em; color: #7f8c8d; user-select: none; font-weight: 600; outline: none;">
477
+ 🔍 View Thinking Process
478
+ </summary>
479
+ <div class="reasoning-payload" style="font-style: italic; color: #555; padding: 8px 12px; border-left: 3px solid #cbd5e1; background-color: #f8fafc; border-radius: 4px; font-size: 0.9em; margin-top: 6px; white-space: pre-wrap;"></div>
480
+ </details>
481
+
482
  <span class="text-payload"></span>
483
  </div>
484
  `;
485
  messagesContainer.appendChild(this.currentResponseElement);
486
  }
487
 
488
+ // 2. Heuristics to check if the incoming token belongs to the reasoning block
489
+ const checkToken = textToken.trim().toLowerCase();
490
+ const isReasoningIndicator =
491
+ textToken.trim().startsWith('**') ||
492
+ checkToken.includes('thought:') ||
493
+ checkToken.includes('analyzing') ||
494
+ checkToken.includes('clarifying');
495
+
496
+ if (isReasoningIndicator) {
497
+ this.isReasoningMode = true;
498
+ }
499
+
500
+ // Grab references to elements inside the active message block
501
+ const reasoningContainer = this.currentResponseElement.querySelector('.reasoning-container');
502
+ const reasoningPayload = this.currentResponseElement.querySelector('.reasoning-payload');
503
  const textContainer = this.currentResponseElement.querySelector('.text-payload');
504
+
505
+ // 3. Route token to the correct container based on the mode
506
+ if (this.isReasoningMode) {
507
+ // Ensure the container block is displayed (it stays collapsed internally via <details>)
508
+ if (reasoningContainer && reasoningContainer.style.display === 'none') {
509
+ reasoningContainer.style.display = 'block';
510
+ }
511
+
512
+ if (reasoningPayload) {
513
+ reasoningPayload.textContent += textToken;
514
+ }
515
+
516
+ // If we encounter a line break and the token isn't a reasoning keyword,
517
+ // assume the model is ready to transition to the actual verbal/text answer
518
+ if (textToken.includes('\n') && !isReasoningIndicator) {
519
+ this.isReasoningMode = false;
520
+ }
521
+ } else {
522
+ // Route to standard visible response layout
523
+ if (textContainer) {
524
+ textContainer.textContent += textToken;
525
+ }
526
  }
527
+
528
+ // 4. Smooth scroll window down to track live content generation
529
+ messagesContainer.scrollTop = messagesContainer.scrollHeight;
530
  }
531
 
532
  handleWebSocketClose() {