Fix: Update session name in UI in real-time (#6533)

This commit is contained in:
Ariel Ahdoot
2026-01-16 15:07:53 -05:00
committed by GitHub
parent 080a0e04ba
commit 9b86d96daf
2 changed files with 44 additions and 0 deletions
+18
View File
@@ -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<string>('');
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 = '';
+26
View File
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { ChatState } from '../types/chatState';
import {
getSession,
Message,
MessageEvent,
reply,
@@ -210,6 +211,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();
},