From 3aaecdd0e99f6407187732f2fb117841ce2cc013 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 28 Oct 2026 03:00:15 +0900 Subject: [PATCH] refac --- src/lib/components/chat/Chat.svelte | 52 +++++++++------------ src/lib/components/chat/MessageInput.svelte | 12 +++++ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 894f531a32..08c1f0257c 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -820,16 +820,6 @@ } }; - let oldSelectedModelIds = ['']; - $: if (!equal(selectedModelIds, oldSelectedModelIds)) { - onSelectedModelIdsChange(); - } - - const onSelectedModelIdsChange = () => { - resetInput(); - oldSelectedModelIds = structuredClone(selectedModelIds); - }; - const mergeFiles = (current, incoming) => { const seen = new Set(); return [...(incoming ?? []), ...(current ?? [])].filter((file) => { @@ -893,7 +883,7 @@ } let saveControlsTimer; - $: if (!loading && !$temporaryChatEnabled && $chatId && chatFiles) { + $: if (!loading && !$temporaryChatEnabled && $chatId && chatFiles && selectedModels) { clearTimeout(saveControlsTimer); saveControlsTimer = setTimeout(saveControls, 400); } @@ -2629,6 +2619,8 @@ }; const initNewChat = async () => { + clearTimeout(saveControlsTimer); + await saveControls(); if ($chatId) terminalContext = null; cancelBrowserInteractions(); bridge?.end(); @@ -2958,8 +2950,6 @@ noteChatDebug('normalized empty selected models after load', { selectedModels }); } - oldSelectedModelIds = structuredClone(selectedModels); - chatContent.history ??= convertMessagesToHistory(chatContent.messages); // Keep live updates received while the chat and tags were loading. for (const [id, message] of Object.entries(history.messages)) { @@ -4051,12 +4041,11 @@ _history = structuredClone(_history); const responseMessageIds: Record = {}; - // If modelId is provided, use it, else use selected model - let selectedModelIds = modelId - ? [modelId] - : atSelectedModel !== undefined - ? [atSelectedModel.id] - : selectedModels; + if (!modelId && atSelectedModel) { + selectedModels = [atSelectedModel.id]; + atSelectedModel = undefined; + } + const selectedModelIds = modelId ? [modelId] : selectedModels; if (!modelId && history.messages[parentId]) { history.messages[parentId].models = [...selectedModelIds]; } @@ -4687,7 +4676,8 @@ scrollToBottom(); } - await sendMessage(history, userMessage.id, { + const currentModels = atSelectedModel ? [atSelectedModel.id] : selectedModels; + const options = { ...(suggestionPrompt ? { messages: createMessagesList(history, message.id), @@ -4695,14 +4685,16 @@ regenerationMessageId: message.id } : {}), - ...((userMessage?.models ?? [...selectedModels]).length > 1 + ...((userMessage.models ?? currentModels).length > 1 && currentModels.length > 1 ? { - // If multiple models are selected, use the model from the message - modelId: message.model, + // Regenerate the selected column with its current model. + modelId: currentModels[message.modelIdx ?? 0] ?? message.model, modelIdx: message.modelIdx } : {}) - }); + }; + userMessage.models = [...currentModels]; + await sendMessage(history, userMessage.id, options); } }; @@ -4860,18 +4852,20 @@ const saveControls = async () => { if (isShared) return; - if (!$chatId || $temporaryChatEnabled) return; + if (!$chatId || $temporaryChatEnabled || chat?.id !== $chatId) return; const loaded = chat?.chat ?? {}; - if (equal(chatFiles, loaded.files ?? [])) return; + if (equal(chatFiles, loaded.files ?? []) && equal(selectedModels, loaded.models ?? [])) return; + const id = $chatId; - const res = await updateChatById(localStorage.token, $chatId, { - files: chatFiles + const res = await updateChatById(localStorage.token, id, { + files: chatFiles, + ...(selectedModels.length && selectedModels.every(Boolean) ? { models: selectedModels } : {}) }).catch((err) => { console.error('[controls autosave]', err); return null; }); // Refresh the dedupe baseline so a later revert still saves. - if (res) chat = res; + if (res && $chatId === id) chat = res; }; const MAX_DRAFT_LENGTH = 5000; diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 1dce908a0e..4bd940c1ce 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -927,6 +927,18 @@ ($config?.audio?.stt?.engine !== 'web' && ($_user?.role === 'admin' || ($_user?.permissions?.chat?.stt ?? true)))); + $: if (webSearchCapableModels.length < selectedModelIds.length) webSearchEnabled = false; + $: if (imageGenerationCapableModels.length < selectedModelIds.length) + imageGenerationEnabled = false; + $: if (codeInterpreterCapableModels.length < selectedModelIds.length) + codeInterpreterEnabled = false; + $: { + const filterIds = selectedFilterIds.filter((id) => + toggleFilters.some((filter: { id: string }) => filter.id === id) + ); + if (filterIds.length !== selectedFilterIds.length) selectedFilterIds = filterIds; + } + // Disable code interpreter when terminal is active (mutually exclusive) $: if ($selectedTerminalId && codeInterpreterEnabled) { codeInterpreterEnabled = false;