From ca7e0ef188c86beebe3510960e84dc449d9392e5 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Tue, 4 Mar 2025 15:23:38 -0700 Subject: [PATCH] update some of the query keys --- .../Chats/ChatProvider/useChatIndividual.ts | 2 +- .../Chats/ChatProvider/useChatSelectors.ts | 2 +- .../Chats/ChatProvider/useChatUpdate.ts | 2 +- .../NewChatProvider/useChatStreamMessage.ts | 90 ++++++++++++++++--- .../ReasoningController.tsx | 5 +- .../ReasoningMessageSelector.tsx | 7 +- .../ReasoningMessage_Text.tsx | 2 + 7 files changed, 88 insertions(+), 22 deletions(-) diff --git a/web/src/context/Chats/ChatProvider/useChatIndividual.ts b/web/src/context/Chats/ChatProvider/useChatIndividual.ts index 158428e15..f57023412 100644 --- a/web/src/context/Chats/ChatProvider/useChatIndividual.ts +++ b/web/src/context/Chats/ChatProvider/useChatIndividual.ts @@ -12,7 +12,7 @@ export const useChatIndividual = (chatId: string) => { payload: { id: chatId } }, responseEvent: '/chats/get:getChat', - options: queryKeys['chatsGetChat'](chatId), + options: queryKeys.chatsGetChat(chatId), callback: (_currentData, newData) => { const { iChat, iChatMessages } = updateChatToIChat(newData, false); for (const message of iChatMessages) { diff --git a/web/src/context/Chats/ChatProvider/useChatSelectors.ts b/web/src/context/Chats/ChatProvider/useChatSelectors.ts index 07f685350..bb24f3d5f 100644 --- a/web/src/context/Chats/ChatProvider/useChatSelectors.ts +++ b/web/src/context/Chats/ChatProvider/useChatSelectors.ts @@ -7,7 +7,7 @@ export const useChatSelectors = () => { const queryClient = useQueryClient(); const getChatMemoized = useMemoizedFn((chatId: string): IBusterChat | undefined => { - const options = queryKeys['chatsGetChat'](chatId); + const options = queryKeys.chatsGetChat(chatId); const queryKey = options.queryKey; return queryClient.getQueryData(queryKey); }); diff --git a/web/src/context/Chats/ChatProvider/useChatUpdate.ts b/web/src/context/Chats/ChatProvider/useChatUpdate.ts index e6b20eee5..bbb3c1ae5 100644 --- a/web/src/context/Chats/ChatProvider/useChatUpdate.ts +++ b/web/src/context/Chats/ChatProvider/useChatUpdate.ts @@ -16,7 +16,7 @@ export const useChatUpdate = () => { const onUpdateChat = useMemoizedFn( async (newChatConfig: Partial & { id: string }, saveToServer: boolean = false) => { - const options = queryKeys['chatsGetChat'](newChatConfig.id); + const options = queryKeys.chatsGetChat(newChatConfig.id); const queryKey = options.queryKey; const currentData = queryClient.getQueryData(queryKey); const iChat: IBusterChat = { diff --git a/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts b/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts index 56b564fba..05c25e3b1 100644 --- a/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts +++ b/web/src/context/Chats/NewChatProvider/useChatStreamMessage.ts @@ -3,7 +3,10 @@ import { useBusterChatContextSelector } from '../ChatProvider'; import { BusterChat, BusterChatMessage_text, - BusterChatMessageReasoning + BusterChatMessageReasoning, + BusterChatMessageReasoning_files, + BusterChatMessageReasoning_text, + BusterChatMessageReasoning_Pills } from '@/api/asset_interfaces'; import { ChatEvent_GeneratingReasoningMessage, @@ -91,8 +94,6 @@ export const useChatStreamMessage = () => { const initializeNewChatCallback = useMemoizedFn((d: BusterChat) => { const { iChat, iChatMessages } = updateChatToIChat(d, true); - console.log('iChatMessages', iChatMessages); - console.log('iChat', iChat); normalizeChatMessage(iChatMessages); onUpdateChat(iChat); onChangePage({ @@ -182,9 +183,9 @@ export const useChatStreamMessage = () => { (_: null, d: ChatEvent_GeneratingReasoningMessage) => { const { message_id, reasoning, chat_id } = d; const reasoningMessageId = reasoning.id; - const foundReasoningMessage: undefined | ChatMessageReasoning = + let messageToUse: undefined | ChatMessageReasoning = chatMessageReasoningMessageRef.current[message_id]?.[reasoningMessageId]; - const isNewMessage = !foundReasoningMessage; + const isNewMessage = !messageToUse; const currentReasoning = chatMessagesRef.current[message_id]?.reasoning ?? []; if (isNewMessage) { @@ -195,21 +196,86 @@ export const useChatStreamMessage = () => { index: currentReasoning.length } }; + messageToUse = chatMessageReasoningMessageRef.current[message_id]?.[reasoningMessageId]!; } - const messageToUse = - chatMessageReasoningMessageRef.current[message_id]?.[reasoningMessageId]!; + switch (reasoning.type) { + case 'text': + const existingReasoningMessageText = messageToUse as BusterChatMessageReasoning_text; + const isStreaming = reasoning.message_chunk !== null; + if (isStreaming) { + existingReasoningMessageText.message = + (existingReasoningMessageText.message || '') + reasoning.message_chunk!; + } + + console.log(existingReasoningMessageText.message?.length); + + break; + case 'files': + const existingReasoningMessageFiles = messageToUse as BusterChatMessageReasoning_files; + + const updatedFiles = reasoning.files.reduce( + (acc, newFile) => { + const existingFile = acc.find((f) => f.file_name === newFile.file_name); + if (existingFile) { + // Update existing file with new content + if (existingFile.file && newFile.file) { + existingFile.file = + Array.isArray(existingFile.file) && Array.isArray(newFile.file) + ? [...existingFile.file, ...newFile.file] + : existingFile.file; + } + } else { + // Add new file + acc.push(newFile); + } + return acc; + }, + [...existingReasoningMessageFiles.files] + ); + + reasoning.files = updatedFiles; + break; + case 'pills': + const existingReasoningMessagePills = messageToUse as BusterChatMessageReasoning_Pills; + + // Handle pill containers + if (reasoning.pill_containers) { + const updatedContainers = reasoning.pill_containers.reduce( + (acc, newContainer) => { + const existingContainer = acc.find((c) => c.title === newContainer.title); + + if (existingContainer) { + // Update existing container by merging pills + existingContainer.pills = [...existingContainer.pills, ...newContainer.pills]; + } else { + // Add new container + acc.push(newContainer); + } + + return acc; + }, + [...(existingReasoningMessagePills.pill_containers || [])] + ); + + reasoning.pill_containers = updatedContainers; + } + break; + default: + const type: never = reasoning; + break; + } + + if (!messageToUse) return; const updatedReasoning: BusterChatMessageReasoning[] = isNewMessage ? [...currentReasoning, messageToUse] : [ - ...currentReasoning.slice(0, foundReasoningMessage.index), - reasoning, - ...currentReasoning.slice(foundReasoningMessage.index + 1) + ...currentReasoning.slice(0, messageToUse.index), + messageToUse, + ...currentReasoning.slice(messageToUse.index + 1) ]; - console.log(updatedReasoning.length); - onUpdateChatMessageTransition({ id: message_id, reasoning: autoAppendThought(updatedReasoning, chat_id), diff --git a/web/src/controllers/ReasoningController/ReasoningController.tsx b/web/src/controllers/ReasoningController/ReasoningController.tsx index 1cf1cb192..cee603dfd 100644 --- a/web/src/controllers/ReasoningController/ReasoningController.tsx +++ b/web/src/controllers/ReasoningController/ReasoningController.tsx @@ -4,6 +4,7 @@ import React from 'react'; import { useChatIndividualContextSelector } from '@chatLayout/ChatContext'; import { ReasoningMessageContainer } from './ReasoningMessageContainer'; import { useMessageIndividual } from '@/context/Chats'; +import { useMount } from 'ahooks'; interface ReasoningControllerProps { chatId: string; @@ -14,9 +15,9 @@ export const ReasoningController: React.FC = ({ chatId const hasChat = useChatIndividualContextSelector((state) => state.hasChat); const message = useMessageIndividual(messageId); - console.log(hasChat, message); + console.log('mounted', hasChat, message?.id); - if (!hasChat || !message) return null; + if (!hasChat || !message) return
NUTS
; const reasoningMessages = message.reasoning; const isCompletedStream = message.isCompletedStream; diff --git a/web/src/controllers/ReasoningController/ReasoningMessages/ReasoningMessageSelector.tsx b/web/src/controllers/ReasoningController/ReasoningMessages/ReasoningMessageSelector.tsx index a23dfddb5..d87e611f2 100644 --- a/web/src/controllers/ReasoningController/ReasoningMessages/ReasoningMessageSelector.tsx +++ b/web/src/controllers/ReasoningController/ReasoningMessages/ReasoningMessageSelector.tsx @@ -1,9 +1,5 @@ import React from 'react'; -import type { - BusterChatMessageReasoning, - BusterChatMessageReasoning_text -} from '@/api/asset_interfaces'; -import { StreamingMessage_Text } from '@/components/ui/streaming/StreamingMessage_Text'; +import type { BusterChatMessageReasoning } from '@/api/asset_interfaces'; import { ReasoningMessage_PillsContainer } from './ReasoningMessage_PillContainers'; import { ReasoningMessage_Files } from './ReasoningMessage_Files'; import { ReasoningMessage_Text } from './ReasoningMessage_Text'; @@ -38,6 +34,7 @@ export const ReasoningMessageSelector: React.FC = chatId }) => { const ReasoningMessage = ReasoningMessageRecord[reasoningMessage.type]; + console.log(reasoningMessage.type); return ( = React.memo ({ reasoningMessage, chatId, isCompletedStream, isLastMessageItem }) => { const { message, status, id, type, title, secondary_title } = reasoningMessage as BusterChatMessageReasoning_text; + + console.log('here', message?.length); return (