(show, chatId = null)
| 465 | } |
| 466 | |
| 467 | function toggleLoader(show, chatId = null) { |
| 468 | const sendButton = document.getElementById('send-button'); |
| 469 | const stopButton = document.getElementById('stop-button'); |
| 470 | const headerLoadingIndicator = document.getElementById('header-loading-indicator'); |
| 471 | const tokensPerSecondDisplay = document.getElementById('tokens-per-second'); |
| 472 | |
| 473 | if (show) { |
| 474 | sendButton.style.display = 'none'; |
| 475 | stopButton.style.display = 'block'; |
| 476 | if (headerLoadingIndicator) headerLoadingIndicator.style.display = 'block'; |
| 477 | |
| 478 | // Start updating tokens/second display only if this is for the active chat |
| 479 | const chatStore = Alpine.store("chat"); |
| 480 | const activeChat = chatStore.activeChat(); |
| 481 | |
| 482 | // Always stop any existing interval first |
| 483 | if (tokensPerSecondInterval) { |
| 484 | clearInterval(tokensPerSecondInterval); |
| 485 | tokensPerSecondInterval = null; |
| 486 | } |
| 487 | |
| 488 | // Use provided chatId or get from active chat |
| 489 | const targetChatId = chatId || (activeChat ? activeChat.id : null); |
| 490 | |
| 491 | if (tokensPerSecondDisplay && targetChatId && activeChat && activeChat.id === targetChatId) { |
| 492 | tokensPerSecondDisplay.textContent = '-'; |
| 493 | // Hide max badge when starting new request |
| 494 | const maxBadge = document.getElementById('max-tokens-per-second-badge'); |
| 495 | if (maxBadge) { |
| 496 | maxBadge.style.display = 'none'; |
| 497 | } |
| 498 | // Don't start interval here - it will be started when the request is created |
| 499 | // Just update once to show initial state |
| 500 | updateTokensPerSecond(targetChatId); |
| 501 | } else if (tokensPerSecondDisplay) { |
| 502 | // Not the active chat, hide or show dash |
| 503 | tokensPerSecondDisplay.textContent = '-'; |
| 504 | } |
| 505 | } else { |
| 506 | sendButton.style.display = 'block'; |
| 507 | stopButton.style.display = 'none'; |
| 508 | if (headerLoadingIndicator) headerLoadingIndicator.style.display = 'none'; |
| 509 | // Stop updating but keep the last value visible only if this was the active chat |
| 510 | const chatStore = Alpine.store("chat"); |
| 511 | const activeChat = chatStore.activeChat(); |
| 512 | if (chatId && activeChat && activeChat.id === chatId) { |
| 513 | // Stop the interval since this request is done |
| 514 | stopTokensPerSecondInterval(); |
| 515 | // Keep the last calculated rate visible |
| 516 | if (tokensPerSecondDisplay && lastTokensPerSecond !== null) { |
| 517 | tokensPerSecondDisplay.textContent = lastTokensPerSecond; |
| 518 | } |
| 519 | // Check if there are other active requests for the active chat and restart interval if needed |
| 520 | const activeRequest = activeRequests.get(activeChat.id); |
| 521 | if (activeRequest && (activeRequest.controller || activeRequest.reader)) { |
| 522 | // Restart interval for the active chat |
| 523 | startTokensPerSecondInterval(); |
| 524 | } |
no test coverage detected