()
| 542 | |
| 543 | // Start a single global interval that updates tokens/second for the active chat |
| 544 | function startTokensPerSecondInterval() { |
| 545 | // Stop any existing interval first |
| 546 | stopTokensPerSecondInterval(); |
| 547 | |
| 548 | // Get the current active chat ID to track |
| 549 | const chatStore = Alpine.store("chat"); |
| 550 | if (!chatStore) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | const activeChat = chatStore.activeChat(); |
| 555 | if (!activeChat) { |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | // Check if active chat has an active request |
| 560 | // We can start the interval if we have at least a controller (reader will be set when streaming starts) |
| 561 | const request = activeRequests.get(activeChat.id); |
| 562 | if (!request) { |
| 563 | // No active request for this chat |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | if (!request.controller) { |
| 568 | // No controller yet, don't start interval |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | // Store which chat this interval is for |
| 573 | tokensPerSecondIntervalChatId = activeChat.id; |
| 574 | |
| 575 | // Start a single interval that always checks the current active chat |
| 576 | // Use a function that always gets fresh state, no closures |
| 577 | tokensPerSecondInterval = setInterval(() => { |
| 578 | // Always get fresh references - no closures |
| 579 | const currentChatStore = Alpine.store("chat"); |
| 580 | if (!currentChatStore) { |
| 581 | stopTokensPerSecondInterval(); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | const currentActiveChat = currentChatStore.activeChat(); |
| 586 | const tokensPerSecondDisplay = document.getElementById('tokens-per-second'); |
| 587 | |
| 588 | if (!tokensPerSecondDisplay) { |
| 589 | stopTokensPerSecondInterval(); |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | // CRITICAL: Check if the active chat has changed |
| 594 | if (!currentActiveChat || currentActiveChat.id !== tokensPerSecondIntervalChatId) { |
| 595 | // Active chat changed, stop this interval immediately and hide badge |
| 596 | const maxBadge = document.getElementById('max-tokens-per-second-badge'); |
| 597 | if (maxBadge) { |
| 598 | maxBadge.style.display = 'none'; |
| 599 | } |
| 600 | stopTokensPerSecondInterval(); |
| 601 | return; |
no test coverage detected