From 8ea808850deabb4709391602417b0aa00f2857cf Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Wed, 14 Oct 2026 17:38:18 +0400 Subject: [PATCH] refac --- src/lib/apis/index.ts | 2 +- src/lib/components/chat/Chat.svelte | 49 +++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index 34f6a5218d..c372a520a3 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -856,7 +856,7 @@ export const generateTitle = async ( export const generateTags = async ( token: string = '', model: string, - messages: string, + messages: object[], chat_id?: string ) => { let error = null; diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 2b05671e41..82b24b882e 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -80,6 +80,7 @@ } from './Messages/structuredOutput'; import { + addTagById, archiveChatById, compactChatById, createNewChat, @@ -102,6 +103,8 @@ updateUserSettings } from '$lib/apis/users'; import { + generateTitle, + generateTags, generateQueries, chatAction, generateMoACompletion, @@ -2226,6 +2229,40 @@ const visibleMessageText = (message: any) => getOutputText(message?.output) || removeAllDetails(message?.content ?? ''); + const generateVoiceChatMetadata = async ( + _chatId: string, + modelId: string, + content: string, + initialTitle: string + ) => { + const token = localStorage.token; + const messages = [{ role: 'user', content }]; + const autoTitle = $settings?.title?.auto ?? true; + const autoTags = $settings?.autoTags ?? true; + const title = autoTitle + ? await generateTitle(token, modelId, messages, _chatId).catch((error) => { + console.error('Could not generate voice chat title', error); + return null; + }) + : null; + const savedChat = await getChatById(token, _chatId); + if (savedChat.title === initialTitle) { + const updated = await updateChatById(token, _chatId, { + title: title || content.slice(0, 100) + }); + if ($chatId === _chatId) { + chatTitle.set(updated.title); + if (embedded) await onEmbeddedChatTitle?.(_chatId, updated.title); + } + await refreshChatList(token); + } + if (autoTags) { + const tags = await generateTags(token, modelId, messages, _chatId); + for (const tag of tags) await addTagById(token, _chatId, tag); + allTags.set(await getAllTags(token)); + } + }; + const addVoiceMessage = async ( role: string, content: string, @@ -2270,8 +2307,16 @@ } if (history.currentId === parentId) history.currentId = id; history = history; - if (!$chatId) await initChatHandler(history); - else if (!$temporaryChatEnabled) { + if (!$chatId) { + const modelId = selectedModelIds[0]; + const _chatId = await initChatHandler(history); + if (!$temporaryChatEnabled) { + // Realtime replies need no chat completion; start metadata work at chat creation. + void generateVoiceChatMetadata(_chatId, modelId, content, chat.title).catch((error) => { + console.error('Could not save voice chat metadata', error); + }); + } + } else if (!$temporaryChatEnabled) { // Patch only the new message, so a running backend turn keeps its current output. await updateChatById(localStorage.token, $chatId, { history: { currentId: history.currentId, messages: changedMessages }