diff --git a/ui/desktop/src/components/BaseChat.tsx b/ui/desktop/src/components/BaseChat.tsx index 7e6057fb5d0a..43fb5705c96a 100644 --- a/ui/desktop/src/components/BaseChat.tsx +++ b/ui/desktop/src/components/BaseChat.tsx @@ -59,6 +59,7 @@ interface BaseChatProps { } function BaseChatContent({ + setChat, renderHeader, customChatInputProps = {}, customMainLayoutProps = {}, @@ -301,6 +302,23 @@ function BaseChatContent({ name: session?.name || 'No Session', }; + // Update the global chat context when session name changes + const lastSetNameRef = useRef(''); + + useEffect(() => { + const currentSessionName = session?.name; + if (currentSessionName && currentSessionName !== lastSetNameRef.current) { + lastSetNameRef.current = currentSessionName; + setChat({ + messages, + recipe, + sessionId, + name: currentSessionName, + }); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [session?.name, setChat]); + // Only use initialMessage for the prompt if it hasn't been submitted yet // If we have a recipe prompt and user recipe values, substitute parameters let recipePrompt = ''; diff --git a/ui/desktop/src/hooks/useChatStream.ts b/ui/desktop/src/hooks/useChatStream.ts index 784be8ebf31d..0590848a6f46 100644 --- a/ui/desktop/src/hooks/useChatStream.ts +++ b/ui/desktop/src/hooks/useChatStream.ts @@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { ChatState } from '../types/chatState'; import { + getSession, Message, MessageEvent, reply, @@ -209,6 +210,31 @@ export function useChatStream({ window.dispatchEvent(new CustomEvent('message-stream-finished')); } + // Refresh session name after each reply for the first 3 user messages + // The backend regenerates the name after each of the first 3 user messages + // to refine it as more context becomes available + if (!error && sessionId) { + const userMessageCount = messagesRef.current.filter( + (m) => m.role === 'user' + ).length; + + // Only refresh for the first 3 user messages + if (userMessageCount <= 3) { + try { + const response = await getSession({ + path: { session_id: sessionId }, + throwOnError: true, + }); + if (response.data?.name) { + setSession((prev) => (prev ? { ...prev, name: response.data.name } : prev)); + } + } catch (refreshError) { + // Silently fail - this is a nice-to-have feature + console.warn('Failed to refresh session name:', refreshError); + } + } + } + setChatState(ChatState.Idle); onStreamFinish(); },