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

Function switchChat

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

Source from the content-addressed store, hash-verified

223
224// Switch to a different chat
225function switchChat(chatId) {
226 if (!window.Alpine || !Alpine.store("chat")) {
227 return false;
228 }
229
230 const chatStore = Alpine.store("chat");
231 const oldActiveChat = chatStore.activeChat();
232
233 if (chatStore.switchChat(chatId)) {
234 // CRITICAL: Stop interval FIRST before any other operations
235 // This prevents the interval from updating with wrong chat's data
236 if (tokensPerSecondInterval) {
237 clearInterval(tokensPerSecondInterval);
238 tokensPerSecondInterval = null;
239 }
240
241 // Immediately clear the display to prevent showing stale data
242 const tokensPerSecondDisplay = document.getElementById('tokens-per-second');
243 if (tokensPerSecondDisplay) {
244 tokensPerSecondDisplay.textContent = '-';
245 }
246
247 // Save current state before switching
248 saveChatsToStorage();
249
250 // Hide badge when switching chats - will be shown if new chat has completed request
251 const maxBadge = document.getElementById('max-tokens-per-second-badge');
252 if (maxBadge) {
253 maxBadge.style.display = 'none';
254 }
255
256 // Update global request tracking for stop button (only if new chat has active request)
257 const newActiveChat = chatStore.activeChat();
258 const newRequest = activeRequests.get(newActiveChat?.id);
259 if (newRequest) {
260 currentAbortController = newRequest.controller;
261 currentReader = newRequest.reader;
262 // Update loader state if new chat has active request
263 const hasActiveRequest = newRequest.controller || newRequest.reader;
264 if (hasActiveRequest) {
265 toggleLoader(true, newActiveChat.id);
266 // Wait a bit to ensure switch is complete and interval is stopped
267 setTimeout(() => {
268 // Double-check we're still on the same chat and interval is stopped
269 const currentActiveChat = chatStore.activeChat();
270 if (currentActiveChat && currentActiveChat.id === newActiveChat.id) {
271 // Make absolutely sure interval is stopped
272 if (tokensPerSecondInterval) {
273 clearInterval(tokensPerSecondInterval);
274 tokensPerSecondInterval = null;
275 tokensPerSecondIntervalChatId = null;
276 }
277 // Update display for the new active chat
278 updateTokensPerSecond(newActiveChat.id);
279 // Restart interval to pick up the new active chat
280 startTokensPerSecondInterval();
281 }
282 }, 100);

Callers 1

ChatFunction · 0.85

Calls 8

saveChatsToStorageFunction · 0.85
toggleLoaderFunction · 0.85
updateTokensPerSecondFunction · 0.85
updateUIForActiveChatFunction · 0.85
storeMethod · 0.80
findMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected