mirror of https://github.com/buster-so/buster.git
change up IBusterChat
This commit is contained in:
parent
4ef095ceba
commit
dd947d1999
|
@ -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']);
|
||||
|
||||
|
|
|
@ -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<
|
|||
<BusterTermsProvider>
|
||||
<BusterPermissionsProvider>
|
||||
<BusterChatProvider>
|
||||
<BusterNewChatProvider>
|
||||
<AppHotKeysProvider>
|
||||
<BusterPosthogProvider>
|
||||
{children}
|
||||
<RoutePrefetcher />
|
||||
</BusterPosthogProvider>
|
||||
</AppHotKeysProvider>
|
||||
</BusterNewChatProvider>
|
||||
<AppHotKeysProvider>
|
||||
<BusterPosthogProvider>
|
||||
{children}
|
||||
<RoutePrefetcher />
|
||||
</BusterPosthogProvider>
|
||||
</AppHotKeysProvider>
|
||||
</BusterChatProvider>
|
||||
</BusterPermissionsProvider>
|
||||
</BusterTermsProvider>
|
||||
|
|
|
@ -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';
|
|
@ -35,7 +35,7 @@ const BusterChat = createContext<ReturnType<typeof useBusterChat>>(
|
|||
{} as ReturnType<typeof useBusterChat>
|
||||
);
|
||||
|
||||
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 });
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
};
|
||||
};
|
|
@ -8,65 +8,51 @@ import { useMemoizedFn } from 'ahooks';
|
|||
import type { BusterDatasetListItem, BusterSearchResult, FileType } from '@/api/asset_interfaces';
|
||||
|
||||
export const useBusterNewChat = () => {
|
||||
const [selectedChatDataSource, setSelectedChatDataSource] =
|
||||
useState<BusterDatasetListItem | null>(null);
|
||||
const [loadingNewChat, setLoadingNewChat] = useState(false);
|
||||
const [selectedChatDataSourceId, setSelectedChatDataSourceId] = useState<string | null>(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,
|
|
@ -0,0 +1 @@
|
|||
export * from './NewChatProvider';
|
|
@ -1,2 +1 @@
|
|||
export * from './ChatProvider';
|
||||
export * from './NewChatProvider';
|
||||
export * from './BusterChatProvider';
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import type { BusterChat, BusterChatMessage } from '@/api/asset_interfaces';
|
||||
|
||||
export interface IBusterChat extends BusterChat {
|
||||
export interface IBusterChat extends Omit<BusterChat, 'messages'> {
|
||||
isNewChat: boolean;
|
||||
isFollowupMessage: boolean;
|
||||
messages: IBusterChatMessage[];
|
||||
messages: string[];
|
||||
}
|
||||
|
||||
export interface IBusterChatMessage extends BusterChatMessage {
|
||||
|
|
Loading…
Reference in New Issue