change up IBusterChat

This commit is contained in:
Nate Kelley 2025-02-08 21:13:42 -07:00
parent 4ef095ceba
commit dd947d1999
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
11 changed files with 77 additions and 100 deletions

View File

@ -1,4 +1,4 @@
import type { ThoughtFileType, FileType } from '@/api/asset_interfaces'; import type { ThoughtFileType } from '@/api/asset_interfaces';
const OPENABLE_FILES = new Set<string>(['metric', 'dashboard', 'reasoning']); const OPENABLE_FILES = new Set<string>(['metric', 'dashboard', 'reasoning']);

View File

@ -22,7 +22,7 @@ import { BusterSearchProvider } from './Search';
import { BusterAssetsProvider } from './Assets/BusterAssetsProvider'; import { BusterAssetsProvider } from './Assets/BusterAssetsProvider';
import { BusterPosthogProvider } from './Posthog/usePosthog'; import { BusterPosthogProvider } from './Posthog/usePosthog';
import { BusterNotificationsProvider } from './BusterNotifications'; import { BusterNotificationsProvider } from './BusterNotifications';
import { BusterChatProvider, BusterNewChatProvider } from './Chats'; import { BusterChatProvider } from './Chats';
import { RoutePrefetcher } from './RoutePrefetcher'; import { RoutePrefetcher } from './RoutePrefetcher';
import { BusterMetricDataProvider } from './MetricData'; import { BusterMetricDataProvider } from './MetricData';
import { BusterMetricsProvider } from './Metrics'; import { BusterMetricsProvider } from './Metrics';
@ -72,14 +72,12 @@ export const AppProviders: React.FC<
<BusterTermsProvider> <BusterTermsProvider>
<BusterPermissionsProvider> <BusterPermissionsProvider>
<BusterChatProvider> <BusterChatProvider>
<BusterNewChatProvider>
<AppHotKeysProvider> <AppHotKeysProvider>
<BusterPosthogProvider> <BusterPosthogProvider>
{children} {children}
<RoutePrefetcher /> <RoutePrefetcher />
</BusterPosthogProvider> </BusterPosthogProvider>
</AppHotKeysProvider> </AppHotKeysProvider>
</BusterNewChatProvider>
</BusterChatProvider> </BusterChatProvider>
</BusterPermissionsProvider> </BusterPermissionsProvider>
</BusterTermsProvider> </BusterTermsProvider>

View File

@ -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 (
<ChatProvider>
<BusterNewChatProvider>
<BusterChatListProvider>{children}</BusterChatListProvider>
</BusterNewChatProvider>
</ChatProvider>
);
});
ChatProvider.displayName = 'ChatProvider';

View File

@ -35,7 +35,7 @@ const BusterChat = createContext<ReturnType<typeof useBusterChat>>(
{} as ReturnType<typeof useBusterChat> {} as ReturnType<typeof useBusterChat>
); );
export const BusterChatProvider: React.FC<{ export const ChatProvider: React.FC<{
children: React.ReactNode; children: React.ReactNode;
}> = ({ children }) => { }> = ({ children }) => {
const value = useBusterChat(); const value = useBusterChat();
@ -63,7 +63,6 @@ export const useBusterChatIndividual = ({
}); });
const selectedChat: IBusterChat = chat || memoizedFallbackToMetricChat; const selectedChat: IBusterChat = chat || memoizedFallbackToMetricChat;
const fetched = chat?.created_at !== undefined;
useEffect(() => { useEffect(() => {
if (chatId) subscribeToChat({ chatId }); if (chatId) subscribeToChat({ chatId });

View File

@ -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 { return {
unsubscribeFromChat, unsubscribeFromChat,
subscribeToChat subscribeToChat

View File

@ -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<Record<string, IBusterChat>>;
startTransition: (fn: () => void) => void;
}) => {
const busterSocket = useBusterWebSocket();
const onUpdateChat = useMemoizedFn(
async (newChatConfig: Partial<IBusterChat> & { id: string }) => {
chatsRef.current[newChatConfig.id] = {
...chatsRef.current[newChatConfig.id],
...newChatConfig
};
startTransition(() => {
//just used to trigger UI update
});
}
);
const onUpdateChatMessage = useMemoizedFn(
async (newMessageConfig: Partial<IBusterChatMessage> & { 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
};
};

View File

@ -8,65 +8,51 @@ import { useMemoizedFn } from 'ahooks';
import type { BusterDatasetListItem, BusterSearchResult, FileType } from '@/api/asset_interfaces'; import type { BusterDatasetListItem, BusterSearchResult, FileType } from '@/api/asset_interfaces';
export const useBusterNewChat = () => { export const useBusterNewChat = () => {
const [selectedChatDataSource, setSelectedChatDataSource] = const [selectedChatDataSourceId, setSelectedChatDataSourceId] = useState<string | null>(null);
useState<BusterDatasetListItem | null>(null);
const [loadingNewChat, setLoadingNewChat] = useState(false);
const onSelectSearchAsset = useMemoizedFn(async (asset: BusterSearchResult) => { const onSelectSearchAsset = useMemoizedFn(async (asset: BusterSearchResult) => {
setLoadingNewChat(true);
console.log('select search asset'); console.log('select search asset');
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
setLoadingNewChat(false);
}); });
const onStartNewChat = useMemoizedFn(async (prompt: string) => { const onStartNewChat = useMemoizedFn(async (prompt: string) => {
setLoadingNewChat(true);
console.log('start new chat'); console.log('start new chat');
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
setLoadingNewChat(false);
}); });
const onStartChatFromFile = useMemoizedFn( const onStartChatFromFile = useMemoizedFn(
async ({}: { prompt: string; fileId: string; fileType: FileType }) => { async ({}: { prompt: string; fileId: string; fileType: FileType }) => {
setLoadingNewChat(true);
console.log('start chat from file'); console.log('start chat from file');
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
setLoadingNewChat(false);
} }
); );
const onFollowUpChat = useMemoizedFn( const onFollowUpChat = useMemoizedFn(
async ({ prompt, messageId }: { prompt: string; messageId: string }) => { async ({ prompt, messageId }: { prompt: string; messageId: string }) => {
setLoadingNewChat(true);
console.log('follow up chat'); console.log('follow up chat');
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
setLoadingNewChat(false);
} }
); );
const onReplaceMessageInChat = useMemoizedFn( const onReplaceMessageInChat = useMemoizedFn(
async ({ prompt, messageId }: { prompt: string; messageId: string }) => { async ({ prompt, messageId }: { prompt: string; messageId: string }) => {
setLoadingNewChat(true);
console.log('replace message in chat'); console.log('replace message in chat');
await new Promise((resolve) => setTimeout(resolve, 1000)); await new Promise((resolve) => setTimeout(resolve, 1000));
setLoadingNewChat(false);
} }
); );
const onStopChat = useMemoizedFn(({ chatId }: { chatId: string }) => { const onStopChat = useMemoizedFn(({ chatId }: { chatId: string }) => {
setLoadingNewChat(false);
console.log('stop current chat'); console.log('stop current chat');
}); });
const onSetSelectedChatDataSource = useMemoizedFn((dataSource: BusterDatasetListItem | null) => { const onSetSelectedChatDataSource = useMemoizedFn((dataSource: BusterDatasetListItem | null) => {
setSelectedChatDataSource(dataSource); //
}); });
return { return {
onStartNewChat, onStartNewChat,
loadingNewChat,
onSelectSearchAsset, onSelectSearchAsset,
selectedChatDataSource, selectedChatDataSourceId,
onSetSelectedChatDataSource, onSetSelectedChatDataSource,
onFollowUpChat, onFollowUpChat,
onStartChatFromFile, onStartChatFromFile,

View File

@ -0,0 +1 @@
export * from './NewChatProvider';

View File

@ -1,2 +1 @@
export * from './ChatProvider'; export * from './BusterChatProvider';
export * from './NewChatProvider';

View File

@ -1,9 +1,9 @@
import type { BusterChat, BusterChatMessage } from '@/api/asset_interfaces'; import type { BusterChat, BusterChatMessage } from '@/api/asset_interfaces';
export interface IBusterChat extends BusterChat { export interface IBusterChat extends Omit<BusterChat, 'messages'> {
isNewChat: boolean; isNewChat: boolean;
isFollowupMessage: boolean; isFollowupMessage: boolean;
messages: IBusterChatMessage[]; messages: string[];
} }
export interface IBusterChatMessage extends BusterChatMessage { export interface IBusterChatMessage extends BusterChatMessage {