2025-03-05 04:52:33 +08:00
|
|
|
'use client';
|
|
|
|
|
2025-03-30 11:13:40 +08:00
|
|
|
import { useGetChatMessageMemoized, useGetChatMessage } from '@/api/buster_rest/chats';
|
2025-02-11 11:15:32 +08:00
|
|
|
import type { SelectedFile } from '../interfaces';
|
2025-03-05 04:52:33 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
2025-03-19 23:43:53 +08:00
|
|
|
import findLast from 'lodash/findLast';
|
2025-04-09 05:10:37 +08:00
|
|
|
import { BusterChatResponseMessage_file } from '@/api/asset_interfaces/chat';
|
2025-04-09 02:45:20 +08:00
|
|
|
import { useAppLayoutContextSelector } from '@/context/BusterAppLayout';
|
2025-04-09 05:10:37 +08:00
|
|
|
import { useGetFileLink } from '@/context/Assets/useGetFileLink';
|
2025-04-09 07:15:47 +08:00
|
|
|
import { useChatLayoutContextSelector } from '../ChatLayoutContext';
|
2025-04-11 03:45:23 +08:00
|
|
|
import { usePrevious } from '@/hooks';
|
2025-02-09 13:41:08 +08:00
|
|
|
|
|
|
|
export const useAutoChangeLayout = ({
|
|
|
|
lastMessageId,
|
2025-03-19 23:59:14 +08:00
|
|
|
onSetSelectedFile,
|
2025-03-30 07:37:22 +08:00
|
|
|
selectedFileId,
|
|
|
|
chatId
|
2025-02-09 13:41:08 +08:00
|
|
|
}: {
|
|
|
|
lastMessageId: string;
|
|
|
|
onSetSelectedFile: (file: SelectedFile) => void;
|
2025-03-19 23:59:14 +08:00
|
|
|
selectedFileId: string | undefined;
|
2025-03-30 07:37:22 +08:00
|
|
|
chatId: string | undefined;
|
2025-02-09 13:41:08 +08:00
|
|
|
}) => {
|
2025-04-09 07:15:47 +08:00
|
|
|
const isVersionHistoryMode = useChatLayoutContextSelector((x) => x.isVersionHistoryMode);
|
2025-04-09 02:45:20 +08:00
|
|
|
const onChangePage = useAppLayoutContextSelector((x) => x.onChangePage);
|
2025-03-12 01:27:34 +08:00
|
|
|
const previousLastMessageId = useRef<string | null>(null);
|
2025-03-14 02:11:22 +08:00
|
|
|
const reasoningMessagesLength = useGetChatMessage(
|
2025-03-05 11:29:16 +08:00
|
|
|
lastMessageId,
|
|
|
|
(x) => x?.reasoning_message_ids?.length || 0
|
|
|
|
);
|
2025-03-30 11:13:40 +08:00
|
|
|
const getChatMessageMemoized = useGetChatMessageMemoized();
|
2025-04-09 05:10:37 +08:00
|
|
|
const { getFileLinkMeta } = useGetFileLink();
|
2025-03-19 23:43:53 +08:00
|
|
|
|
2025-03-14 02:11:22 +08:00
|
|
|
const isCompletedStream = useGetChatMessage(lastMessageId, (x) => x?.isCompletedStream);
|
2025-04-11 03:45:23 +08:00
|
|
|
const previousIsCompletedStream = usePrevious(isCompletedStream);
|
2025-04-09 02:45:20 +08:00
|
|
|
|
2025-02-09 13:41:08 +08:00
|
|
|
const hasReasoning = !!reasoningMessagesLength;
|
2025-03-05 04:11:49 +08:00
|
|
|
|
2025-02-12 07:46:22 +08:00
|
|
|
//change the page to reasoning file if we get a reasoning message
|
2025-04-09 02:45:20 +08:00
|
|
|
//change the page to the file if we get a file
|
2025-02-09 13:41:08 +08:00
|
|
|
useEffect(() => {
|
2025-03-30 07:37:22 +08:00
|
|
|
if (
|
|
|
|
!isCompletedStream &&
|
|
|
|
hasReasoning &&
|
|
|
|
previousLastMessageId.current !== lastMessageId &&
|
|
|
|
chatId
|
|
|
|
) {
|
2025-02-09 13:41:08 +08:00
|
|
|
onSetSelectedFile({ id: lastMessageId, type: 'reasoning' });
|
2025-03-12 01:27:34 +08:00
|
|
|
previousLastMessageId.current = lastMessageId;
|
2025-02-09 13:41:08 +08:00
|
|
|
}
|
2025-03-19 23:43:53 +08:00
|
|
|
|
2025-04-11 03:45:23 +08:00
|
|
|
//we check for false because on initial load, the isCompletedStream is false
|
|
|
|
if (isCompletedStream && previousIsCompletedStream === false) {
|
2025-03-19 23:43:53 +08:00
|
|
|
const chatMessage = getChatMessageMemoized(lastMessageId);
|
|
|
|
const lastFileId = findLast(chatMessage?.response_message_ids, (id) => {
|
|
|
|
const responseMessage = chatMessage?.response_messages[id];
|
|
|
|
return responseMessage?.type === 'file';
|
|
|
|
});
|
|
|
|
const lastFile = chatMessage?.response_messages[lastFileId || ''] as
|
|
|
|
| BusterChatResponseMessage_file
|
|
|
|
| undefined;
|
|
|
|
|
2025-04-09 02:45:20 +08:00
|
|
|
if (lastFileId && lastFile) {
|
2025-04-09 05:10:37 +08:00
|
|
|
const { link, isSelected, selectedVersionNumber } = getFileLinkMeta({
|
2025-04-09 02:45:20 +08:00
|
|
|
fileId: lastFileId,
|
|
|
|
fileType: lastFile.file_type,
|
2025-04-09 05:10:37 +08:00
|
|
|
chatId,
|
|
|
|
versionNumber: lastFile.version_number,
|
|
|
|
useVersionHistoryMode: !chatId
|
2025-04-09 02:45:20 +08:00
|
|
|
});
|
|
|
|
|
2025-04-09 05:10:37 +08:00
|
|
|
if (
|
|
|
|
!isSelected &&
|
|
|
|
selectedVersionNumber !== lastFile.version_number &&
|
|
|
|
selectedFileId !== lastFileId
|
|
|
|
) {
|
2025-04-09 02:45:20 +08:00
|
|
|
onSetSelectedFile({
|
|
|
|
id: lastFileId,
|
2025-04-09 05:10:37 +08:00
|
|
|
type: lastFile.file_type
|
2025-04-09 02:45:20 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-04-09 07:15:47 +08:00
|
|
|
if (link && !selectedVersionNumber && !isVersionHistoryMode) {
|
2025-04-09 05:10:37 +08:00
|
|
|
onChangePage(link);
|
|
|
|
}
|
2025-04-09 02:45:20 +08:00
|
|
|
}
|
|
|
|
}
|
2025-04-09 07:15:47 +08:00
|
|
|
}, [isCompletedStream, hasReasoning, lastMessageId]);
|
2025-04-09 02:45:20 +08:00
|
|
|
};
|