Fix: Update session name in UI in real-time (#6533)
This commit is contained in:
@@ -59,6 +59,7 @@ interface BaseChatProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function BaseChatContent({
|
function BaseChatContent({
|
||||||
|
setChat,
|
||||||
renderHeader,
|
renderHeader,
|
||||||
customChatInputProps = {},
|
customChatInputProps = {},
|
||||||
customMainLayoutProps = {},
|
customMainLayoutProps = {},
|
||||||
@@ -301,6 +302,23 @@ function BaseChatContent({
|
|||||||
name: session?.name || 'No Session',
|
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
|
// 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
|
// If we have a recipe prompt and user recipe values, substitute parameters
|
||||||
let recipePrompt = '';
|
let recipePrompt = '';
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|||||||
import { ChatState } from '../types/chatState';
|
import { ChatState } from '../types/chatState';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
getSession,
|
||||||
Message,
|
Message,
|
||||||
MessageEvent,
|
MessageEvent,
|
||||||
reply,
|
reply,
|
||||||
@@ -210,6 +211,31 @@ export function useChatStream({
|
|||||||
window.dispatchEvent(new CustomEvent('message-stream-finished'));
|
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);
|
setChatState(ChatState.Idle);
|
||||||
onStreamFinish();
|
onStreamFinish();
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user