MCPcopy
hub / github.com/mudler/LocalAI / updateTokensPerSecond

Function updateTokensPerSecond

core/http/static/chat.js:654–704  ·  view source on GitHub ↗
(chatId)

Source from the content-addressed store, hash-verified

652}
653
654function updateTokensPerSecond(chatId) {
655 const tokensPerSecondDisplay = document.getElementById('tokens-per-second');
656 if (!tokensPerSecondDisplay || !chatId) {
657 return;
658 }
659
660 // Get the request info for this chat
661 const request = activeRequests.get(chatId);
662 if (!request || !request.startTime) {
663 tokensPerSecondDisplay.textContent = '-';
664 return;
665 }
666
667 // Verify the request is still active (controller is cleared when request ends)
668 if (!request.controller) {
669 tokensPerSecondDisplay.textContent = '-';
670 return;
671 }
672
673 // Check if this is still the active chat
674 const chatStore = Alpine.store("chat");
675 const activeChat = chatStore ? chatStore.activeChat() : null;
676 if (!activeChat || activeChat.id !== chatId) {
677 // Not the active chat anymore
678 tokensPerSecondDisplay.textContent = '-';
679 return;
680 }
681
682 const elapsedSeconds = (Date.now() - request.startTime) / 1000;
683 // Show rate if we have tokens, otherwise show waiting indicator
684 if (elapsedSeconds > 0) {
685 if (request.tokensReceived > 0) {
686 const rate = request.tokensReceived / elapsedSeconds;
687 // Update max rate if this is higher
688 if (rate > (request.maxTokensPerSecond || 0)) {
689 request.maxTokensPerSecond = rate;
690 }
691 const formattedRate = `${rate.toFixed(1)} tokens/s`;
692 tokensPerSecondDisplay.textContent = formattedRate;
693 lastTokensPerSecond = formattedRate; // Store the last calculated rate
694
695 // Update the max badge if it exists (only show during active request if user wants, or we can show it at the end)
696 } else {
697 // Request is active but no tokens yet - show waiting
698 tokensPerSecondDisplay.textContent = '0.0 tokens/s';
699 }
700 } else {
701 // Just started
702 tokensPerSecondDisplay.textContent = '-';
703 }
704}
705
706// Update the max tokens/s badge display
707function updateMaxTokensPerSecondBadge(chatId, maxRate) {

Callers 3

switchChatFunction · 0.85
toggleLoaderFunction · 0.85

Calls 2

storeMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected