buster/web/src/api/buster_rest/chats/queryRequests.ts

323 lines
9.1 KiB
TypeScript
Raw Normal View History

2025-03-08 07:02:56 +08:00
import { useMemoizedFn } from '@/hooks';
2025-03-11 23:33:57 +08:00
import { QueryClient, useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
getListChats,
getListChats_server,
getChat,
getChat_server,
updateChat,
2025-03-12 00:38:13 +08:00
deleteChat,
2025-03-18 07:19:34 +08:00
getListLogs,
2025-04-10 07:09:06 +08:00
duplicateChat,
startChatFromAsset
2025-03-11 23:33:57 +08:00
} from './requests';
2025-03-14 02:11:22 +08:00
import type { IBusterChat, IBusterChatMessage } from '@/api/asset_interfaces/chat';
2025-02-18 11:50:21 +08:00
import { queryKeys } from '@/api/query_keys';
2025-02-22 03:07:30 +08:00
import { updateChatToIChat } from '@/lib/chat';
2025-03-12 00:11:34 +08:00
import { useMemo } from 'react';
2025-03-29 03:30:46 +08:00
import last from 'lodash/last';
import { prefetchGetMetricDataClient } from '../metrics/queryRequests';
2025-04-03 00:42:54 +08:00
import { useBusterNotifications } from '@/context/BusterNotifications';
import { useGetUserFavorites } from '../users/queryRequests';
import {
useAddAssetToCollection,
useRemoveAssetFromCollection
} from '../collections/queryRequests';
import { collectionQueryKeys } from '@/api/query_keys/collection';
2025-02-08 10:57:42 +08:00
2025-03-12 00:11:34 +08:00
export const useGetListChats = (
filters?: Omit<Parameters<typeof getListChats>[0], 'page_token' | 'page_size'>
) => {
const filtersCompiled: Parameters<typeof getListChats>[0] = useMemo(
() => ({ admin_view: false, page_token: 0, page_size: 3000, ...filters }),
[filters]
);
const queryFn = useMemoizedFn(() => getListChats(filtersCompiled));
2025-02-08 10:57:42 +08:00
2025-03-11 23:38:49 +08:00
return useQuery({
2025-03-29 02:53:20 +08:00
...queryKeys.chatsGetList(filters),
2025-02-08 10:57:42 +08:00
queryFn
});
};
export const prefetchGetListChats = async (
params?: Parameters<typeof getListChats>[0],
2025-02-08 10:57:42 +08:00
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
2025-02-18 11:50:21 +08:00
...queryKeys.chatsGetList(params),
queryFn: () => getListChats_server(params)
});
return queryClient;
};
2025-03-12 00:38:13 +08:00
export const useGetListLogs = (
filters?: Omit<Parameters<typeof getListLogs>[0], 'page_token' | 'page_size'>
) => {
const filtersCompiled: Parameters<typeof getListLogs>[0] = useMemo(
() => ({ page_token: 0, page_size: 3000, ...filters }),
[filters]
);
const queryFn = useMemoizedFn(() => getListLogs(filtersCompiled));
return useQuery({
2025-03-29 02:53:20 +08:00
...queryKeys.logsGetList(filters),
2025-03-12 00:38:13 +08:00
queryFn
});
};
2025-03-12 03:07:16 +08:00
export const useGetChat = <TData = IBusterChat>(
params: Parameters<typeof getChat>[0],
select?: (chat: IBusterChat) => TData
) => {
const queryClient = useQueryClient();
const queryFn = useMemoizedFn(() => {
return getChat(params).then((chat) => {
const { iChat, iChatMessages } = updateChatToIChat(chat, false);
2025-03-29 03:30:46 +08:00
const lastMessageId = last(iChat.message_ids);
const lastMessage = iChatMessages[lastMessageId!];
if (lastMessage) {
Object.values(lastMessage.response_messages).forEach((responseMessage) => {
2025-03-29 03:42:25 +08:00
if (responseMessage.type === 'file' && responseMessage.file_type === 'metric') {
2025-04-12 02:43:19 +08:00
prefetchGetMetricDataClient(
{ id: responseMessage.id, version_number: responseMessage.version_number },
queryClient
);
2025-03-29 03:42:25 +08:00
}
2025-03-29 03:30:46 +08:00
});
}
2025-03-12 03:07:16 +08:00
iChat.message_ids.forEach((messageId) => {
queryClient.setQueryData(
queryKeys.chatsMessages(messageId).queryKey,
iChatMessages[messageId]
);
});
return iChat;
2025-03-06 01:13:24 +08:00
});
});
2025-03-12 03:07:16 +08:00
return useQuery({
2025-03-06 03:14:24 +08:00
...queryKeys.chatsGetChat(params.id),
2025-03-12 03:07:16 +08:00
enabled: !!params.id,
queryFn,
select
2025-03-06 03:14:24 +08:00
});
};
2025-04-10 07:09:06 +08:00
export const useStartChatFromAsset = () => {
const queryClient = useQueryClient();
const mutationFn = useMemoizedFn(async (params: Parameters<typeof startChatFromAsset>[0]) => {
const chat = await startChatFromAsset(params);
const { iChat, iChatMessages } = updateChatToIChat(chat, false);
iChat.message_ids.forEach((messageId) => {
queryClient.setQueryData(
queryKeys.chatsMessages(messageId).queryKey,
iChatMessages[messageId]
);
});
queryClient.setQueryData(queryKeys.chatsGetChat(chat.id).queryKey, iChat);
return iChat;
});
return useMutation({
mutationFn,
onSuccess: (chat) => {
queryClient.invalidateQueries({
queryKey: queryKeys.chatsGetList().queryKey
});
}
});
};
export const prefetchGetChat = async (
params: Parameters<typeof getChat>[0],
queryClientProp?: QueryClient
) => {
const queryClient = queryClientProp || new QueryClient();
await queryClient.prefetchQuery({
2025-02-18 11:50:21 +08:00
...queryKeys.chatsGetChat(params.id),
queryFn: async () => {
2025-03-06 01:13:24 +08:00
return await getChat_server(params).then((chat) => {
return updateChatToIChat(chat, true).iChat;
});
2025-02-18 11:50:21 +08:00
}
2025-02-08 10:57:42 +08:00
});
return queryClient;
};
2025-03-11 23:33:57 +08:00
export const useUpdateChat = () => {
2025-03-18 07:19:34 +08:00
const queryClient = useQueryClient();
2025-03-11 23:33:57 +08:00
return useMutation({
mutationFn: updateChat,
2025-03-18 07:19:34 +08:00
onMutate: (data) => {
2025-03-11 23:33:57 +08:00
//this is actually handled in @useChatUpdate file
2025-03-18 07:19:34 +08:00
//except for the chat title and feedback
if (data.title || data.feedback !== undefined) {
const options = queryKeys.chatsGetChat(data.id);
queryClient.setQueryData(options.queryKey, (old) => {
return {
...old!,
...data
};
});
}
2025-03-11 23:33:57 +08:00
}
});
};
export const useDeleteChat = () => {
const queryClient = useQueryClient();
2025-04-03 00:42:54 +08:00
const { openConfirmModal } = useBusterNotifications();
const mutationFn = useMemoizedFn(
async ({
useConfirmModal = true,
data
}: {
data: Parameters<typeof deleteChat>[0];
useConfirmModal?: boolean;
}) => {
const method = () => deleteChat(data);
if (useConfirmModal) {
return await openConfirmModal({
title: 'Delete Chat',
content: 'Are you sure you want to delete this chat?',
onOk: method
});
}
return method();
}
);
2025-03-11 23:33:57 +08:00
return useMutation({
2025-04-03 00:42:54 +08:00
mutationFn,
2025-03-11 23:33:57 +08:00
onSuccess(data, variables, context) {
queryClient.invalidateQueries({
queryKey: queryKeys.chatsGetList().queryKey
});
}
});
};
2025-03-14 02:11:22 +08:00
2025-03-30 11:13:40 +08:00
export const useGetChatMessageMemoized = () => {
2025-03-14 02:11:22 +08:00
const queryClient = useQueryClient();
const getChatMessageMemoized = useMemoizedFn((messageId: string) => {
const options = queryKeys.chatsMessages(messageId);
const queryKey = options.queryKey;
return queryClient.getQueryData<IBusterChatMessage>(queryKey);
});
return getChatMessageMemoized;
};
2025-03-30 11:13:40 +08:00
export const useGetChatMemoized = () => {
const queryClient = useQueryClient();
const getChatMemoized = useMemoizedFn((chatId: string) => {
const options = queryKeys.chatsGetChat(chatId);
const queryKey = options.queryKey;
return queryClient.getQueryData<IBusterChat>(queryKey);
});
return getChatMemoized;
};
2025-03-14 02:11:22 +08:00
export const useGetChatMessage = <TData = IBusterChatMessage>(
messageId: string,
selector?: (message: IBusterChatMessage) => TData
) => {
const { data } = useQuery({
...queryKeys.chatsMessages(messageId),
enabled: false, //this will come from the chat
select: selector
});
2025-03-14 03:41:55 +08:00
return data;
2025-03-14 02:11:22 +08:00
};
2025-03-18 07:19:34 +08:00
export const useDuplicateChat = () => {
return useMutation({
mutationFn: duplicateChat
});
};
2025-04-03 00:42:54 +08:00
export const useSaveChatToCollections = () => {
const queryClient = useQueryClient();
const { data: userFavorites, refetch: refreshFavoritesList } = useGetUserFavorites();
const { mutateAsync: addAssetToCollection } = useAddAssetToCollection();
const saveChatToCollection = useMemoizedFn(
async ({ chatIds, collectionIds }: { chatIds: string[]; collectionIds: string[] }) => {
await Promise.all(
collectionIds.map((collectionId) =>
addAssetToCollection({
id: collectionId,
assets: chatIds.map((chatId) => ({ id: chatId, type: 'chat' }))
})
)
);
}
);
return useMutation({
mutationFn: saveChatToCollection,
onSuccess: (_, { collectionIds }) => {
const collectionIsInFavorites = userFavorites.some((f) => {
return collectionIds.includes(f.id);
});
if (collectionIsInFavorites) refreshFavoritesList();
queryClient.invalidateQueries({
queryKey: collectionIds.map(
(id) => collectionQueryKeys.collectionsGetCollection(id).queryKey
)
});
}
});
};
export const useRemoveChatFromCollections = () => {
const { data: userFavorites, refetch: refreshFavoritesList } = useGetUserFavorites();
const { mutateAsync: removeAssetFromCollection } = useRemoveAssetFromCollection();
const queryClient = useQueryClient();
const removeChatFromCollection = useMemoizedFn(
async ({ chatIds, collectionIds }: { chatIds: string[]; collectionIds: string[] }) => {
await Promise.all(
collectionIds.map((collectionId) =>
removeAssetFromCollection({
id: collectionId,
assets: chatIds.map((chatId) => ({ id: chatId, type: 'chat' }))
})
)
);
}
);
return useMutation({
mutationFn: removeChatFromCollection,
onSuccess: (_, { collectionIds, chatIds }) => {
const collectionIsInFavorites = userFavorites.some((f) => {
return collectionIds.includes(f.id);
});
if (collectionIsInFavorites) refreshFavoritesList();
queryClient.invalidateQueries({
queryKey: collectionIds.map(
(id) => collectionQueryKeys.collectionsGetCollection(id).queryKey
)
});
}
});
};