add some reasoning trigger

This commit is contained in:
Nate Kelley 2025-09-24 21:48:17 -06:00
parent af7b1fe329
commit f87f67d895
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
3 changed files with 43 additions and 6 deletions

View File

@ -8,3 +8,9 @@ export const useDocumentTitle = (title: string | undefined) => {
}, 25); }, 25);
}, [title]); }, [title]);
}; };
export const updateDocumentTitle = (callback: (currentTitle: string) => string) => {
const currentTitle = document.title;
const newTitle = callback(currentTitle);
document.title = newTitle;
};

View File

@ -1,5 +1,5 @@
import { useLocation, useNavigate } from '@tanstack/react-router'; import { useNavigate } from '@tanstack/react-router';
import { useEffect, useLayoutEffect, useRef } from 'react'; import { useEffect, useLayoutEffect, useRef, useState } from 'react';
import type { BusterChatResponseMessage_file } from '@/api/asset_interfaces/chat'; import type { BusterChatResponseMessage_file } from '@/api/asset_interfaces/chat';
import { useGetChatMessageMemoized } from '@/api/buster_rest/chats'; import { useGetChatMessageMemoized } from '@/api/buster_rest/chats';
import { useIsVersionChanged } from '@/context/AppVersion/useAppVersion'; import { useIsVersionChanged } from '@/context/AppVersion/useAppVersion';
@ -10,6 +10,7 @@ import {
useGetChatMessageIsFinishedReasoning, useGetChatMessageIsFinishedReasoning,
useGetChatMessageLastReasoningMessageId, useGetChatMessageLastReasoningMessageId,
} from '@/context/Chats/useGetChatMessage'; } from '@/context/Chats/useGetChatMessage';
import { useWindowFocus } from '@/hooks/useWindowFocus';
import { assetParamsToRoute } from '@/lib/assets/assetParamsToRoute'; import { assetParamsToRoute } from '@/lib/assets/assetParamsToRoute';
export const useAutoRedirectStreaming = ({ export const useAutoRedirectStreaming = ({
@ -31,6 +32,7 @@ export const useAutoRedirectStreaming = ({
const previousIsCompletedStream = useRef<boolean>(isStreamFinished); const previousIsCompletedStream = useRef<boolean>(isStreamFinished);
const hasLoadedChat = useHasLoadedChat({ chatId: chatId || '' }); const hasLoadedChat = useHasLoadedChat({ chatId: chatId || '' });
const hasReasoning = !!lastReasoningMessageId; const hasReasoning = !!lastReasoningMessageId;
const [triggerAutoNavigate, setTriggerAutoNavigate] = useState<number>(0);
useLayoutEffect(() => { useLayoutEffect(() => {
previousIsCompletedStream.current = isStreamFinished; previousIsCompletedStream.current = isStreamFinished;
@ -42,6 +44,8 @@ export const useAutoRedirectStreaming = ({
return; return;
} }
console.log('triggerAutoNavigate', triggerAutoNavigate);
const chatMessage = getChatMessageMemoized(lastMessageId); const chatMessage = getChatMessageMemoized(lastMessageId);
const firstFileId = chatMessage?.response_message_ids?.find((id) => { const firstFileId = chatMessage?.response_message_ids?.find((id) => {
const responseMessage = chatMessage?.response_messages[id]; const responseMessage = chatMessage?.response_messages[id];
@ -49,7 +53,11 @@ export const useAutoRedirectStreaming = ({
}); });
//this will happen if it is streaming and has a file in the response //this will happen if it is streaming and has a file in the response
if (!isStreamFinished && firstFileId) { // or if the chat is completed and has a file in the response
if (
(!isStreamFinished && firstFileId) ||
(isStreamFinished && firstFileId && previousIsCompletedStream.current === false)
) {
const firstFile = chatMessage?.response_messages[firstFileId] as const firstFile = chatMessage?.response_messages[firstFileId] as
| BusterChatResponseMessage_file | BusterChatResponseMessage_file
| undefined; | undefined;
@ -64,6 +72,8 @@ export const useAutoRedirectStreaming = ({
navigate({ ...linkProps, replace: true, reloadDocument: versionChanged }); navigate({ ...linkProps, replace: true, reloadDocument: versionChanged });
} }
previousIsCompletedStream.current = true;
} }
//this will trigger when the chat is streaming and is has not completed yet (new chat) //this will trigger when the chat is streaming and is has not completed yet (new chat)
@ -86,8 +96,6 @@ export const useAutoRedirectStreaming = ({
previousIsCompletedStream.current === false && previousIsCompletedStream.current === false &&
!firstFileId !firstFileId
) { ) {
//no file is found, so we need to collapse the chat
navigate({ navigate({
to: '/app/chats/$chatId', to: '/app/chats/$chatId',
params: { params: {
@ -96,12 +104,27 @@ export const useAutoRedirectStreaming = ({
replace: true, replace: true,
reloadDocument: versionChanged, reloadDocument: versionChanged,
}); });
previousIsCompletedStream.current = true;
} }
}, [isStreamFinished, hasReasoning, hasResponseFile, chatId, lastMessageId, isFinishedReasoning]); //only use these values to trigger the useEffect }, [
isStreamFinished,
hasReasoning,
hasResponseFile,
chatId,
lastMessageId,
isFinishedReasoning,
triggerAutoNavigate,
]); //only use these values to trigger the useEffect
useEffect(() => { useEffect(() => {
if (!isStreamFinished && versionChanged) { if (!isStreamFinished && versionChanged) {
window.location.reload(); window.location.reload();
} }
}, [isStreamFinished, versionChanged]); }, [isStreamFinished, versionChanged]);
useWindowFocus(() => {
if (isStreamFinished && previousIsCompletedStream.current === false) {
setTriggerAutoNavigate((prev) => prev + 1);
}
});
}; };

View File

@ -11,6 +11,7 @@ import {
import { chatQueryKeys } from '@/api/query_keys/chat'; import { chatQueryKeys } from '@/api/query_keys/chat';
import { metricsQueryKeys } from '@/api/query_keys/metric'; import { metricsQueryKeys } from '@/api/query_keys/metric';
import { useBlackboxMessage } from '@/context/BlackBox/useBlackboxMessage'; import { useBlackboxMessage } from '@/context/BlackBox/useBlackboxMessage';
import { updateDocumentTitle, useDocumentTitle } from '@/hooks/useDocumentTitle';
import { useMemoizedFn } from '@/hooks/useMemoizedFn'; import { useMemoizedFn } from '@/hooks/useMemoizedFn';
import { useMount } from '@/hooks/useMount'; import { useMount } from '@/hooks/useMount';
import { updateChatToIChat } from '@/lib/chat'; import { updateChatToIChat } from '@/lib/chat';
@ -135,13 +136,20 @@ export const useChatStreaming = ({
}); });
useEffect(() => { useEffect(() => {
const REASONING_TITLE = 'Reasoning... | ';
if (isStreamingMessage) { if (isStreamingMessage) {
const message = getChatMessageMemoized(messageId); const message = getChatMessageMemoized(messageId);
if (message) { if (message) {
checkBlackBoxMessage(message); checkBlackBoxMessage(message);
} }
updateDocumentTitle((currentTitle) => {
return `${REASONING_TITLE}${currentTitle}`;
});
} else { } else {
removeBlackBoxMessage(messageId); removeBlackBoxMessage(messageId);
updateDocumentTitle((currentTitle) => {
return currentTitle.replace(REASONING_TITLE, '');
});
} }
}, [isStreamingMessage]); }, [isStreamingMessage]);