diff --git a/src/components/ChatPanel.vue b/src/components/ChatPanel.vue index 3491fa1af..ca67a80a5 100644 --- a/src/components/ChatPanel.vue +++ b/src/components/ChatPanel.vue @@ -32,10 +32,8 @@ function scrollToBottom() { watch(messages, scrollToBottom, { deep: true }) function handleSubmit(text: string) { - if (!chat.value) { - const c = ensureChat() - if (c) chat.value = markRaw(c) - } + const c = ensureChat() + if (c) chat.value = markRaw(c) chat.value?.sendMessage({ text }).catch(() => { /* user-facing error handled by UI */ }) diff --git a/src/components/chat/ProviderSelect.vue b/src/components/chat/ProviderSelect.vue index 138d0666e..ef0ef5bde 100644 --- a/src/components/chat/ProviderSelect.vue +++ b/src/components/chat/ProviderSelect.vue @@ -41,7 +41,7 @@ const { triggerClass, itemClass, testId } = defineProps<{

AI Provider

diff --git a/src/composables/use-chat.ts b/src/composables/use-chat.ts index 6363e2de2..1f23f6037 100644 --- a/src/composables/use-chat.ts +++ b/src/composables/use-chat.ts @@ -64,17 +64,25 @@ const isConfigured = computed(() => { return true }) +let transportDirty = false + +function markTransportDirty() { + transportDirty = true +} + watch(providerID, (id) => { const def = AI_PROVIDERS.find((p) => p.id === id) if (def?.defaultModel) { modelID.value = def.defaultModel } - resetChat() + markTransportDirty() }) -watch(modelID, () => resetChat()) -watch(customModelID, () => resetChat()) -watch(customAPIType, () => resetChat()) +watch(modelID, markTransportDirty) +watch(customModelID, markTransportDirty) +watch(customAPIType, markTransportDirty) +watch(apiKey, markTransportDirty) +watch(customBaseURL, markTransportDirty) function setAPIKey(key: string) { apiKey.value = key @@ -170,15 +178,19 @@ function createTransport() { function ensureChat(): Chat | null { if (!isConfigured.value) return null if (!chat) { - chat = new Chat({ - transport: createTransport() - }) + chat = new Chat({ transport: createTransport() }) + transportDirty = false + } else if (transportDirty) { + const messages = chat.messages + chat = new Chat({ transport: createTransport(), messages }) + transportDirty = false } return chat } function resetChat() { chat = null + transportDirty = false } if (typeof window !== 'undefined') {