2025-02-09 13:41:08 +08:00
|
|
|
import React from 'react';
|
2025-02-01 06:21:50 +08:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
ContextSelector,
|
|
|
|
useContextSelector
|
|
|
|
} from '@fluentui/react-context-selector';
|
|
|
|
import { useMemoizedFn } from 'ahooks';
|
2025-02-11 11:15:32 +08:00
|
|
|
import type { BusterSearchResult, FileType } from '@/api/asset_interfaces';
|
2025-02-11 07:43:38 +08:00
|
|
|
import { useBusterWebSocket } from '@/context/BusterWebSocket';
|
|
|
|
import { useChatUpdateMessage } from './useChatUpdateMessage';
|
2025-02-01 06:21:50 +08:00
|
|
|
|
|
|
|
export const useBusterNewChat = () => {
|
2025-02-11 07:43:38 +08:00
|
|
|
const busterSocket = useBusterWebSocket();
|
|
|
|
|
|
|
|
const {
|
|
|
|
completeChatCallback,
|
|
|
|
startListeningForChatProgress,
|
|
|
|
stopListeningForChatProgress,
|
|
|
|
stopChatCallback
|
|
|
|
} = useChatUpdateMessage();
|
|
|
|
|
2025-02-05 13:10:39 +08:00
|
|
|
const onSelectSearchAsset = useMemoizedFn(async (asset: BusterSearchResult) => {
|
|
|
|
console.log('select search asset');
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
});
|
2025-02-01 06:21:50 +08:00
|
|
|
|
|
|
|
const onStartNewChat = useMemoizedFn(async (prompt: string) => {
|
2025-02-05 13:10:39 +08:00
|
|
|
console.log('start new chat');
|
2025-02-01 06:21:50 +08:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
});
|
|
|
|
|
2025-02-05 13:04:26 +08:00
|
|
|
const onStartChatFromFile = useMemoizedFn(
|
2025-02-05 13:10:39 +08:00
|
|
|
async ({}: { prompt: string; fileId: string; fileType: FileType }) => {
|
|
|
|
console.log('start chat from file');
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
}
|
2025-02-05 13:04:26 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const onReplaceMessageInChat = useMemoizedFn(
|
2025-02-05 13:10:39 +08:00
|
|
|
async ({ prompt, messageId }: { prompt: string; messageId: string }) => {
|
|
|
|
console.log('replace message in chat');
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
}
|
2025-02-05 13:04:26 +08:00
|
|
|
);
|
|
|
|
|
2025-02-11 07:43:38 +08:00
|
|
|
const onFollowUpChat = useMemoizedFn(
|
|
|
|
async ({ prompt, chatId }: { prompt: string; chatId: string }) => {
|
|
|
|
startListeningForChatProgress();
|
|
|
|
const result = await busterSocket.emitAndOnce({
|
|
|
|
emitEvent: {
|
|
|
|
route: '/chats/post',
|
|
|
|
payload: {
|
|
|
|
dataset_id: null,
|
|
|
|
prompt,
|
|
|
|
chat_id: chatId
|
|
|
|
}
|
|
|
|
},
|
|
|
|
responseEvent: {
|
|
|
|
route: '/chats/post:complete',
|
|
|
|
callback: completeChatCallback
|
|
|
|
}
|
|
|
|
});
|
2025-02-06 00:55:09 +08:00
|
|
|
|
2025-02-11 07:43:38 +08:00
|
|
|
stopListeningForChatProgress();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const onStopChat = useMemoizedFn(
|
|
|
|
({ chatId, messageId }: { chatId: string; messageId: string }) => {
|
|
|
|
busterSocket.emit({
|
|
|
|
route: '/chats/stop',
|
|
|
|
payload: {
|
|
|
|
id: chatId,
|
|
|
|
message_id: messageId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stopListeningForChatProgress();
|
|
|
|
stopChatCallback(chatId);
|
|
|
|
}
|
|
|
|
);
|
2025-02-01 06:21:50 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
onStartNewChat,
|
|
|
|
onSelectSearchAsset,
|
2025-02-05 13:04:26 +08:00
|
|
|
onFollowUpChat,
|
|
|
|
onStartChatFromFile,
|
2025-02-06 00:55:09 +08:00
|
|
|
onReplaceMessageInChat,
|
|
|
|
onStopChat
|
2025-02-01 06:21:50 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const BusterNewChatContext = createContext<ReturnType<typeof useBusterNewChat>>(
|
|
|
|
{} as ReturnType<typeof useBusterNewChat>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const BusterNewChatProvider: React.FC<{
|
|
|
|
children: React.ReactNode;
|
|
|
|
}> = ({ children }) => {
|
|
|
|
const value = useBusterNewChat();
|
|
|
|
return <BusterNewChatContext.Provider value={value}>{children}</BusterNewChatContext.Provider>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useBusterNewChatContextSelector = <T,>(
|
|
|
|
selector: ContextSelector<ReturnType<typeof useBusterNewChat>, T>
|
|
|
|
) => useContextSelector(BusterNewChatContext, selector);
|