From dd947d1999a8f142e5390d7580da6e7df22cd128 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Sat, 8 Feb 2025 21:13:42 -0700 Subject: [PATCH] change up IBusterChat --- .../ChatLayoutContext/publicHelpers.ts | 2 +- web/src/context/AppProviders.tsx | 16 ++--- web/src/context/Chats/BusterChatProvider.tsx | 16 +++++ .../Chats/ChatProvider/ChatProvider.tsx | 3 +- ...osciations.tsx => useChatAssosciations.ts} | 0 .../ChatProvider/useChatSubscriptions.ts | 67 ------------------- .../Chats/ChatProvider/useChatUpdate.ts | 45 +++++++++++++ .../{ => NewChatProvider}/NewChatProvider.tsx | 20 +----- .../context/Chats/NewChatProvider/index.ts | 1 + web/src/context/Chats/index.ts | 3 +- web/src/context/Chats/interfaces.ts | 4 +- 11 files changed, 77 insertions(+), 100 deletions(-) create mode 100644 web/src/context/Chats/BusterChatProvider.tsx rename web/src/context/Chats/ChatProvider/{useChatAssosciations.tsx => useChatAssosciations.ts} (100%) create mode 100644 web/src/context/Chats/ChatProvider/useChatUpdate.ts rename web/src/context/Chats/{ => NewChatProvider}/NewChatProvider.tsx (79%) create mode 100644 web/src/context/Chats/NewChatProvider/index.ts diff --git a/web/src/app/app/_layouts/ChatLayout/ChatLayoutContext/publicHelpers.ts b/web/src/app/app/_layouts/ChatLayout/ChatLayoutContext/publicHelpers.ts index 874b4f3f5..0f095b0c8 100644 --- a/web/src/app/app/_layouts/ChatLayout/ChatLayoutContext/publicHelpers.ts +++ b/web/src/app/app/_layouts/ChatLayout/ChatLayoutContext/publicHelpers.ts @@ -1,4 +1,4 @@ -import type { ThoughtFileType, FileType } from '@/api/asset_interfaces'; +import type { ThoughtFileType } from '@/api/asset_interfaces'; const OPENABLE_FILES = new Set(['metric', 'dashboard', 'reasoning']); diff --git a/web/src/context/AppProviders.tsx b/web/src/context/AppProviders.tsx index 47d46093b..9ba0662c1 100644 --- a/web/src/context/AppProviders.tsx +++ b/web/src/context/AppProviders.tsx @@ -22,7 +22,7 @@ import { BusterSearchProvider } from './Search'; import { BusterAssetsProvider } from './Assets/BusterAssetsProvider'; import { BusterPosthogProvider } from './Posthog/usePosthog'; import { BusterNotificationsProvider } from './BusterNotifications'; -import { BusterChatProvider, BusterNewChatProvider } from './Chats'; +import { BusterChatProvider } from './Chats'; import { RoutePrefetcher } from './RoutePrefetcher'; import { BusterMetricDataProvider } from './MetricData'; import { BusterMetricsProvider } from './Metrics'; @@ -72,14 +72,12 @@ export const AppProviders: React.FC< - - - - {children} - - - - + + + {children} + + + diff --git a/web/src/context/Chats/BusterChatProvider.tsx b/web/src/context/Chats/BusterChatProvider.tsx new file mode 100644 index 000000000..1e13b0d49 --- /dev/null +++ b/web/src/context/Chats/BusterChatProvider.tsx @@ -0,0 +1,16 @@ +import React from 'react'; +import { ChatProvider } from './ChatProvider'; +import { BusterNewChatProvider } from './NewChatProvider'; +import { BusterChatListProvider } from './ChatListProvider'; + +export const BusterChatProvider = React.memo(({ children }: { children: React.ReactNode }) => { + return ( + + + {children} + + + ); +}); + +ChatProvider.displayName = 'ChatProvider'; diff --git a/web/src/context/Chats/ChatProvider/ChatProvider.tsx b/web/src/context/Chats/ChatProvider/ChatProvider.tsx index 04883fa3d..331565d82 100644 --- a/web/src/context/Chats/ChatProvider/ChatProvider.tsx +++ b/web/src/context/Chats/ChatProvider/ChatProvider.tsx @@ -35,7 +35,7 @@ const BusterChat = createContext>( {} as ReturnType ); -export const BusterChatProvider: React.FC<{ +export const ChatProvider: React.FC<{ children: React.ReactNode; }> = ({ children }) => { const value = useBusterChat(); @@ -63,7 +63,6 @@ export const useBusterChatIndividual = ({ }); const selectedChat: IBusterChat = chat || memoizedFallbackToMetricChat; - const fetched = chat?.created_at !== undefined; useEffect(() => { if (chatId) subscribeToChat({ chatId }); diff --git a/web/src/context/Chats/ChatProvider/useChatAssosciations.tsx b/web/src/context/Chats/ChatProvider/useChatAssosciations.ts similarity index 100% rename from web/src/context/Chats/ChatProvider/useChatAssosciations.tsx rename to web/src/context/Chats/ChatProvider/useChatAssosciations.ts diff --git a/web/src/context/Chats/ChatProvider/useChatSubscriptions.ts b/web/src/context/Chats/ChatProvider/useChatSubscriptions.ts index 04956e49a..713301fd7 100644 --- a/web/src/context/Chats/ChatProvider/useChatSubscriptions.ts +++ b/web/src/context/Chats/ChatProvider/useChatSubscriptions.ts @@ -53,73 +53,6 @@ export const useChatSubscriptions = ({ // }); }); - useHotkeys('t', () => { - const newThoughts = createMockResponseMessageThought(); - const myChat = { - ...chatsRef.current[MOCK_CHAT.id]!, - messages: [ - { - ...chatsRef.current[MOCK_CHAT.id]!.messages[0], - reasoning: [...chatsRef.current[MOCK_CHAT.id]!.messages[0].reasoning, newThoughts], - isCompletedStream: false - } - ] - }; - - chatsRef.current[MOCK_CHAT.id] = myChat; - - startTransition(() => { - // Create a new reference to trigger React update - chatsRef.current = { ...chatsRef.current }; - }); - }); - - useHotkeys('m', () => { - const newTextMessage = createMockResponseMessageText(); - const myChat = { - ...chatsRef.current[MOCK_CHAT.id]!, - messages: [ - { - ...chatsRef.current[MOCK_CHAT.id]!.messages[0], - response_messages: [ - ...chatsRef.current[MOCK_CHAT.id]!.messages[0]!.response_messages, - newTextMessage - ], - isCompletedStream: false - } - ] - }; - - chatsRef.current[MOCK_CHAT.id] = myChat; - - startTransition(() => { - chatsRef.current = { ...chatsRef.current }; - }); - }); - - useHotkeys('f', () => { - const newFileMessage = createMockResponseMessageFile(); - const myChat = { - ...chatsRef.current[MOCK_CHAT.id]!, - messages: [ - { - ...chatsRef.current[MOCK_CHAT.id]!.messages[0], - response_messages: [ - ...chatsRef.current[MOCK_CHAT.id]!.messages[0]!.response_messages, - newFileMessage - ], - isCompletedStream: false - } - ] - }; - - chatsRef.current[MOCK_CHAT.id] = myChat; - - startTransition(() => { - chatsRef.current = { ...chatsRef.current }; - }); - }); - return { unsubscribeFromChat, subscribeToChat diff --git a/web/src/context/Chats/ChatProvider/useChatUpdate.ts b/web/src/context/Chats/ChatProvider/useChatUpdate.ts new file mode 100644 index 000000000..220b5a35f --- /dev/null +++ b/web/src/context/Chats/ChatProvider/useChatUpdate.ts @@ -0,0 +1,45 @@ +import { useBusterWebSocket } from '@/context/BusterWebSocket'; +import { useMemoizedFn } from 'ahooks'; +import { MutableRefObject } from 'react'; +import { IBusterChat, IBusterChatMessage } from '../interfaces'; + +export const useChatUpdate = ({ + chatsRef, + startTransition +}: { + chatsRef: MutableRefObject>; + startTransition: (fn: () => void) => void; +}) => { + const busterSocket = useBusterWebSocket(); + + const onUpdateChat = useMemoizedFn( + async (newChatConfig: Partial & { id: string }) => { + chatsRef.current[newChatConfig.id] = { + ...chatsRef.current[newChatConfig.id], + ...newChatConfig + }; + startTransition(() => { + //just used to trigger UI update + }); + } + ); + + const onUpdateChatMessage = useMemoizedFn( + async (newMessageConfig: Partial & { id: string }, chatId: string) => { + // chatsRef.current[chatId] = { + // ...chatsRef.current[chatId], + // messages: chatsRef.current[chatId].messages.map((message) => + // message.id === newMessageConfig.id ? { ...message, ...newMessageConfig } : message + // ) + // }; + startTransition(() => { + //just used to trigger UI update + }); + } + ); + + return { + onUpdateChat, + onUpdateChatMessage + }; +}; diff --git a/web/src/context/Chats/NewChatProvider.tsx b/web/src/context/Chats/NewChatProvider/NewChatProvider.tsx similarity index 79% rename from web/src/context/Chats/NewChatProvider.tsx rename to web/src/context/Chats/NewChatProvider/NewChatProvider.tsx index 597e65c88..36f9cd5b1 100644 --- a/web/src/context/Chats/NewChatProvider.tsx +++ b/web/src/context/Chats/NewChatProvider/NewChatProvider.tsx @@ -8,65 +8,51 @@ import { useMemoizedFn } from 'ahooks'; import type { BusterDatasetListItem, BusterSearchResult, FileType } from '@/api/asset_interfaces'; export const useBusterNewChat = () => { - const [selectedChatDataSource, setSelectedChatDataSource] = - useState(null); - const [loadingNewChat, setLoadingNewChat] = useState(false); + const [selectedChatDataSourceId, setSelectedChatDataSourceId] = useState(null); const onSelectSearchAsset = useMemoizedFn(async (asset: BusterSearchResult) => { - setLoadingNewChat(true); console.log('select search asset'); await new Promise((resolve) => setTimeout(resolve, 1000)); - setLoadingNewChat(false); }); const onStartNewChat = useMemoizedFn(async (prompt: string) => { - setLoadingNewChat(true); console.log('start new chat'); await new Promise((resolve) => setTimeout(resolve, 1000)); - setLoadingNewChat(false); }); const onStartChatFromFile = useMemoizedFn( async ({}: { prompt: string; fileId: string; fileType: FileType }) => { - setLoadingNewChat(true); console.log('start chat from file'); await new Promise((resolve) => setTimeout(resolve, 1000)); - setLoadingNewChat(false); } ); const onFollowUpChat = useMemoizedFn( async ({ prompt, messageId }: { prompt: string; messageId: string }) => { - setLoadingNewChat(true); console.log('follow up chat'); await new Promise((resolve) => setTimeout(resolve, 1000)); - setLoadingNewChat(false); } ); const onReplaceMessageInChat = useMemoizedFn( async ({ prompt, messageId }: { prompt: string; messageId: string }) => { - setLoadingNewChat(true); console.log('replace message in chat'); await new Promise((resolve) => setTimeout(resolve, 1000)); - setLoadingNewChat(false); } ); const onStopChat = useMemoizedFn(({ chatId }: { chatId: string }) => { - setLoadingNewChat(false); console.log('stop current chat'); }); const onSetSelectedChatDataSource = useMemoizedFn((dataSource: BusterDatasetListItem | null) => { - setSelectedChatDataSource(dataSource); + // }); return { onStartNewChat, - loadingNewChat, onSelectSearchAsset, - selectedChatDataSource, + selectedChatDataSourceId, onSetSelectedChatDataSource, onFollowUpChat, onStartChatFromFile, diff --git a/web/src/context/Chats/NewChatProvider/index.ts b/web/src/context/Chats/NewChatProvider/index.ts new file mode 100644 index 000000000..13ad697b8 --- /dev/null +++ b/web/src/context/Chats/NewChatProvider/index.ts @@ -0,0 +1 @@ +export * from './NewChatProvider'; diff --git a/web/src/context/Chats/index.ts b/web/src/context/Chats/index.ts index 7ad02a0d9..458aed7fe 100644 --- a/web/src/context/Chats/index.ts +++ b/web/src/context/Chats/index.ts @@ -1,2 +1 @@ -export * from './ChatProvider'; -export * from './NewChatProvider'; +export * from './BusterChatProvider'; diff --git a/web/src/context/Chats/interfaces.ts b/web/src/context/Chats/interfaces.ts index 32a3ee29b..1b4adb72c 100644 --- a/web/src/context/Chats/interfaces.ts +++ b/web/src/context/Chats/interfaces.ts @@ -1,9 +1,9 @@ import type { BusterChat, BusterChatMessage } from '@/api/asset_interfaces'; -export interface IBusterChat extends BusterChat { +export interface IBusterChat extends Omit { isNewChat: boolean; isFollowupMessage: boolean; - messages: IBusterChatMessage[]; + messages: string[]; } export interface IBusterChatMessage extends BusterChatMessage {