| import { |
| EToolResources, |
| LocalStorageKeys, |
| InfiniteCollections, |
| defaultAssistantsVersion, |
| } from 'librechat-data-provider'; |
| import { useSetRecoilState } from 'recoil'; |
| import { useMutation, useQueryClient } from '@tanstack/react-query'; |
| import { dataService, MutationKeys, QueryKeys, defaultOrderQuery } from 'librechat-data-provider'; |
| import type { UseMutationResult } from '@tanstack/react-query'; |
| import type t from 'librechat-data-provider'; |
| import { |
| |
| addSharedLink, |
| deleteSharedLink, |
| |
| addConversation, |
| updateConvoFields, |
| updateConversation, |
| deleteConversation, |
| } from '~/utils'; |
| import { useConversationsInfiniteQuery, useSharedLinksInfiniteQuery } from './queries'; |
| import { normalizeData } from '~/utils/collection'; |
| import store from '~/store'; |
|
|
| export type TGenTitleMutation = UseMutationResult< |
| t.TGenTitleResponse, |
| unknown, |
| t.TGenTitleRequest, |
| unknown |
| >; |
|
|
| |
| export const useGenTitleMutation = (): TGenTitleMutation => { |
| const queryClient = useQueryClient(); |
| return useMutation((payload: t.TGenTitleRequest) => dataService.genTitle(payload), { |
| onSuccess: (response, vars) => { |
| queryClient.setQueryData( |
| [QueryKeys.conversation, vars.conversationId], |
| (convo: t.TConversation | undefined) => { |
| if (!convo) { |
| return convo; |
| } |
| return { ...convo, title: response.title }; |
| }, |
| ); |
| queryClient.setQueryData<t.ConversationData>([QueryKeys.allConversations], (convoData) => { |
| if (!convoData) { |
| return convoData; |
| } |
| return updateConvoFields(convoData, { |
| conversationId: vars.conversationId, |
| title: response.title, |
| } as t.TConversation); |
| }); |
| document.title = response.title; |
| }, |
| }); |
| }; |
|
|
| export const useUpdateConversationMutation = ( |
| id: string, |
| ): UseMutationResult< |
| t.TUpdateConversationResponse, |
| unknown, |
| t.TUpdateConversationRequest, |
| unknown |
| > => { |
| const queryClient = useQueryClient(); |
| return useMutation( |
| (payload: t.TUpdateConversationRequest) => dataService.updateConversation(payload), |
| { |
| onSuccess: (updatedConvo) => { |
| queryClient.setQueryData([QueryKeys.conversation, id], updatedConvo); |
| queryClient.setQueryData<t.ConversationData>([QueryKeys.allConversations], (convoData) => { |
| if (!convoData) { |
| return convoData; |
| } |
| return updateConversation(convoData, updatedConvo); |
| }); |
| }, |
| }, |
| ); |
| }; |
|
|
| export const useArchiveConversationMutation = ( |
| id: string, |
| ): UseMutationResult< |
| t.TArchiveConversationResponse, |
| unknown, |
| t.TArchiveConversationRequest, |
| unknown |
| > => { |
| const queryClient = useQueryClient(); |
| const { refetch } = useConversationsInfiniteQuery(); |
| const { refetch: archiveRefetch } = useConversationsInfiniteQuery({ |
| pageNumber: '1', |
| isArchived: true, |
| }); |
| return useMutation( |
| (payload: t.TArchiveConversationRequest) => dataService.archiveConversation(payload), |
| { |
| onSuccess: (_data, vars) => { |
| if (vars.isArchived) { |
| queryClient.setQueryData([QueryKeys.conversation, id], null); |
| } else { |
| queryClient.setQueryData([QueryKeys.conversation, id], _data); |
| } |
|
|
| queryClient.setQueryData<t.ConversationData>([QueryKeys.allConversations], (convoData) => { |
| if (!convoData) { |
| return convoData; |
| } |
| const pageSize = convoData.pages[0].pageSize as number; |
|
|
| return normalizeData( |
| vars.isArchived ? deleteConversation(convoData, id) : addConversation(convoData, _data), |
| 'conversations', |
| pageSize, |
| ); |
| }); |
|
|
| if (vars.isArchived) { |
| const current = queryClient.getQueryData<t.ConversationData>([ |
| QueryKeys.allConversations, |
| ]); |
| refetch({ refetchPage: (page, index) => index === (current?.pages.length || 1) - 1 }); |
| } |
|
|
| queryClient.setQueryData<t.ConversationData>( |
| [QueryKeys.archivedConversations], |
| (convoData) => { |
| if (!convoData) { |
| return convoData; |
| } |
| const pageSize = convoData.pages[0].pageSize as number; |
| return normalizeData( |
| vars.isArchived |
| ? addConversation(convoData, _data) |
| : deleteConversation(convoData, id), |
| 'conversations', |
| pageSize, |
| ); |
| }, |
| ); |
|
|
| if (!vars.isArchived) { |
| const currentArchive = queryClient.getQueryData<t.ConversationData>([ |
| QueryKeys.archivedConversations, |
| ]); |
| archiveRefetch({ |
| refetchPage: (page, index) => index === (currentArchive?.pages.length || 1) - 1, |
| }); |
| } |
| }, |
| }, |
| ); |
| }; |
|
|
| export const useCreateSharedLinkMutation = ( |
| options?: t.CreateSharedLinkOptions, |
| ): UseMutationResult<t.TSharedLinkResponse, unknown, t.TSharedLinkRequest, unknown> => { |
| const queryClient = useQueryClient(); |
| const { refetch } = useSharedLinksInfiniteQuery(); |
| const { onSuccess, ..._options } = options || {}; |
| return useMutation((payload: t.TSharedLinkRequest) => dataService.createSharedLink(payload), { |
| onSuccess: (_data, vars, context) => { |
| if (!vars.conversationId) { |
| return; |
| } |
|
|
| queryClient.setQueryData<t.SharedLinkListData>([QueryKeys.sharedLinks], (sharedLink) => { |
| if (!sharedLink) { |
| return sharedLink; |
| } |
| const pageSize = sharedLink.pages[0].pageSize as number; |
| return normalizeData( |
| |
| vars.isPublic |
| ? addSharedLink(sharedLink, _data) |
| : deleteSharedLink(sharedLink, _data.shareId), |
| InfiniteCollections.SHARED_LINKS, |
| pageSize, |
| ); |
| }); |
|
|
| queryClient.setQueryData([QueryKeys.sharedLinks, _data.shareId], _data); |
| if (!vars.isPublic) { |
| const current = queryClient.getQueryData<t.ConversationData>([QueryKeys.sharedLinks]); |
| refetch({ |
| refetchPage: (page, index) => index === (current?.pages.length || 1) - 1, |
| }); |
| } |
| onSuccess?.(_data, vars, context); |
| }, |
| ...(_options || {}), |
| }); |
| }; |
|
|
| export const useUpdateSharedLinkMutation = ( |
| options?: t.UpdateSharedLinkOptions, |
| ): UseMutationResult<t.TSharedLinkResponse, unknown, t.TSharedLinkRequest, unknown> => { |
| const queryClient = useQueryClient(); |
| const { refetch } = useSharedLinksInfiniteQuery(); |
| const { onSuccess, ..._options } = options || {}; |
| return useMutation((payload: t.TSharedLinkRequest) => dataService.updateSharedLink(payload), { |
| onSuccess: (_data, vars, context) => { |
| if (!vars.conversationId) { |
| return; |
| } |
|
|
| queryClient.setQueryData<t.SharedLinkListData>([QueryKeys.sharedLinks], (sharedLink) => { |
| if (!sharedLink) { |
| return sharedLink; |
| } |
|
|
| return normalizeData( |
| |
| vars.isPublic |
| ? |
| |
| addSharedLink(sharedLink, _data) |
| : deleteSharedLink(sharedLink, _data.shareId), |
| InfiniteCollections.SHARED_LINKS, |
| sharedLink.pages[0].pageSize as number, |
| ); |
| }); |
|
|
| queryClient.setQueryData([QueryKeys.sharedLinks, _data.shareId], _data); |
| if (!vars.isPublic) { |
| const current = queryClient.getQueryData<t.ConversationData>([QueryKeys.sharedLinks]); |
| refetch({ |
| refetchPage: (page, index) => index === (current?.pages.length || 1) - 1, |
| }); |
| } |
|
|
| onSuccess?.(_data, vars, context); |
| }, |
| ...(_options || {}), |
| }); |
| }; |
|
|
| export const useDeleteSharedLinkMutation = ( |
| options?: t.DeleteSharedLinkOptions, |
| ): UseMutationResult<t.TDeleteSharedLinkResponse, unknown, { shareId: string }, unknown> => { |
| const queryClient = useQueryClient(); |
| const { refetch } = useSharedLinksInfiniteQuery(); |
| const { onSuccess, ..._options } = options || {}; |
| return useMutation(({ shareId }) => dataService.deleteSharedLink(shareId), { |
| onSuccess: (_data, vars, context) => { |
| if (!vars.shareId) { |
| return; |
| } |
|
|
| queryClient.setQueryData([QueryKeys.sharedMessages, vars.shareId], null); |
| queryClient.setQueryData<t.SharedLinkListData>([QueryKeys.sharedLinks], (data) => { |
| if (!data) { |
| return data; |
| } |
| return normalizeData( |
| deleteSharedLink(data, vars.shareId), |
| InfiniteCollections.SHARED_LINKS, |
| data.pages[0].pageSize as number, |
| ); |
| }); |
| const current = queryClient.getQueryData<t.ConversationData>([QueryKeys.sharedLinks]); |
| refetch({ |
| refetchPage: (page, index) => index === (current?.pages.length || 1) - 1, |
| }); |
| onSuccess?.(_data, vars, context); |
| }, |
| ...(_options || {}), |
| }); |
| }; |
|
|
| export const useDeleteConversationMutation = ( |
| options?: t.DeleteConversationOptions, |
| ): UseMutationResult< |
| t.TDeleteConversationResponse, |
| unknown, |
| t.TDeleteConversationRequest, |
| unknown |
| > => { |
| const queryClient = useQueryClient(); |
| const { refetch } = useConversationsInfiniteQuery(); |
| const { onSuccess, ..._options } = options || {}; |
| return useMutation( |
| (payload: t.TDeleteConversationRequest) => dataService.deleteConversation(payload), |
| { |
| onSuccess: (_data, vars, context) => { |
| if (!vars.conversationId) { |
| return; |
| } |
|
|
| const handleDelete = (convoData) => { |
| if (!convoData) { |
| return convoData; |
| } |
| return normalizeData( |
| deleteConversation(convoData, vars.conversationId as string), |
| 'conversations', |
| convoData.pages[0].pageSize, |
| ); |
| }; |
|
|
| queryClient.setQueryData([QueryKeys.conversation, vars.conversationId], null); |
| queryClient.setQueryData<t.ConversationData>([QueryKeys.allConversations], handleDelete); |
| queryClient.setQueryData<t.ConversationData>( |
| [QueryKeys.archivedConversations], |
| handleDelete, |
| ); |
| const current = queryClient.getQueryData<t.ConversationData>([QueryKeys.allConversations]); |
| refetch({ refetchPage: (page, index) => index === (current?.pages.length || 1) - 1 }); |
| onSuccess?.(_data, vars, context); |
| }, |
| ...(_options || {}), |
| }, |
| ); |
| }; |
|
|
| export const useForkConvoMutation = ( |
| options?: t.ForkConvoOptions, |
| ): UseMutationResult<t.TForkConvoResponse, unknown, t.TForkConvoRequest, unknown> => { |
| const queryClient = useQueryClient(); |
| const { onSuccess, ..._options } = options || {}; |
| return useMutation((payload: t.TForkConvoRequest) => dataService.forkConversation(payload), { |
| onSuccess: (data, vars, context) => { |
| if (!vars.conversationId) { |
| return; |
| } |
| queryClient.setQueryData( |
| [QueryKeys.conversation, data.conversation.conversationId], |
| data.conversation, |
| ); |
| queryClient.setQueryData<t.ConversationData>([QueryKeys.allConversations], (convoData) => { |
| if (!convoData) { |
| return convoData; |
| } |
| return addConversation(convoData, data.conversation); |
| }); |
| queryClient.setQueryData<t.TMessage[]>( |
| [QueryKeys.messages, data.conversation.conversationId], |
| data.messages, |
| ); |
| onSuccess?.(data, vars, context); |
| }, |
| ...(_options || {}), |
| }); |
| }; |
|
|
| export const useUploadConversationsMutation = ( |
| _options?: t.MutationOptions<t.TImportResponse, FormData>, |
| ) => { |
| const queryClient = useQueryClient(); |
| const { onSuccess, onError, onMutate } = _options || {}; |
|
|
| return useMutation<t.TImportResponse, unknown, FormData>({ |
| mutationFn: (formData: FormData) => dataService.importConversationsFile(formData), |
| onSuccess: (data, variables, context) => { |
| |
| queryClient.invalidateQueries([QueryKeys.allConversations]); |
| if (onSuccess) { |
| onSuccess(data, variables, context); |
| } |
| }, |
| onError: (err, variables, context) => { |
| if (onError) { |
| onError(err, variables, context); |
| } |
| }, |
| onMutate, |
| }); |
| }; |
|
|
| export const useUploadFileMutation = ( |
| _options?: t.UploadMutationOptions, |
| ): UseMutationResult< |
| t.TFileUpload, |
| unknown, |
| FormData, |
| unknown |
| > => { |
| const queryClient = useQueryClient(); |
| const { onSuccess, ...options } = _options || {}; |
| return useMutation([MutationKeys.fileUpload], { |
| mutationFn: (body: FormData) => { |
| const width = body.get('width'); |
| const height = body.get('height'); |
| const version = body.get('version') as number | string; |
| if (height && width && (!version || version != 2)) { |
| return dataService.uploadImage(body); |
| } |
|
|
| return dataService.uploadFile(body); |
| }, |
| ...(options || {}), |
| onSuccess: (data, formData, context) => { |
| queryClient.setQueryData<t.TFile[] | undefined>([QueryKeys.files], (_files) => [ |
| data, |
| ...(_files ?? []), |
| ]); |
|
|
| const endpoint = formData.get('endpoint'); |
| const assistant_id = formData.get('assistant_id'); |
| const message_file = formData.get('message_file'); |
| const tool_resource = formData.get('tool_resource'); |
|
|
| if (!assistant_id || message_file === 'true') { |
| onSuccess?.(data, formData, context); |
| return; |
| } |
|
|
| queryClient.setQueryData<t.AssistantListResponse>( |
| [QueryKeys.assistants, endpoint, defaultOrderQuery], |
| (prev) => { |
| if (!prev) { |
| return prev; |
| } |
|
|
| return { |
| ...prev, |
| data: prev?.data.map((assistant) => { |
| if (assistant.id !== assistant_id) { |
| return assistant; |
| } |
|
|
| const update = {}; |
| if (!tool_resource) { |
| update['file_ids'] = [...assistant.file_ids, data.file_id]; |
| } |
| if (tool_resource === EToolResources.code_interpreter) { |
| const prevResources = assistant.tool_resources ?? {}; |
| const prevResource = assistant.tool_resources?.[tool_resource as string] ?? { |
| file_ids: [], |
| }; |
| prevResource.file_ids.push(data.file_id); |
| update['tool_resources'] = { |
| ...prevResources, |
| [tool_resource as string]: prevResource, |
| }; |
| } |
| return { |
| ...assistant, |
| ...update, |
| }; |
| }), |
| }; |
| }, |
| ); |
| onSuccess?.(data, formData, context); |
| }, |
| }); |
| }; |
|
|
| export const useDeleteFilesMutation = ( |
| _options?: t.DeleteMutationOptions, |
| ): UseMutationResult< |
| t.DeleteFilesResponse, |
| unknown, |
| t.DeleteFilesBody, |
| unknown |
| > => { |
| const queryClient = useQueryClient(); |
| const { onSuccess, ...options } = _options || {}; |
| return useMutation([MutationKeys.fileDelete], { |
| mutationFn: (body: t.DeleteFilesBody) => |
| dataService.deleteFiles(body.files, body.assistant_id, body.tool_resource), |
| ...(options || {}), |
| onSuccess: (data, ...args) => { |
| queryClient.setQueryData<t.TFile[] | undefined>([QueryKeys.files], (cachefiles) => { |
| const { files: filesDeleted } = args[0]; |
|
|
| const fileMap = filesDeleted.reduce((acc, file) => { |
| acc.set(file.file_id, file); |
| return acc; |
| }, new Map<string, t.BatchFile>()); |
|
|
| return (cachefiles ?? []).filter((file) => !fileMap.has(file.file_id)); |
| }); |
| onSuccess?.(data, ...args); |
| }, |
| }); |
| }; |
|
|
| export const useUpdatePresetMutation = ( |
| options?: t.UpdatePresetOptions, |
| ): UseMutationResult< |
| t.TPreset, |
| unknown, |
| t.TPreset, |
| unknown |
| > => { |
| return useMutation([MutationKeys.updatePreset], { |
| mutationFn: (preset: t.TPreset) => dataService.updatePreset(preset), |
| ...(options || {}), |
| }); |
| }; |
|
|
| export const useDeletePresetMutation = ( |
| options?: t.DeletePresetOptions, |
| ): UseMutationResult< |
| t.PresetDeleteResponse, |
| unknown, |
| t.TPreset | undefined, |
| unknown |
| > => { |
| return useMutation([MutationKeys.deletePreset], { |
| mutationFn: (preset: t.TPreset | undefined) => dataService.deletePreset(preset), |
| ...(options || {}), |
| }); |
| }; |
|
|
| |
| export const useLogoutUserMutation = ( |
| options?: t.LogoutOptions, |
| ): UseMutationResult<unknown, unknown, undefined, unknown> => { |
| const queryClient = useQueryClient(); |
| const setDefaultPreset = useSetRecoilState(store.defaultPreset); |
| return useMutation([MutationKeys.logoutUser], { |
| mutationFn: () => dataService.logout(), |
|
|
| ...(options || {}), |
| onSuccess: (...args) => { |
| options?.onSuccess?.(...args); |
| }, |
| onMutate: (...args) => { |
| setDefaultPreset(null); |
| queryClient.removeQueries(); |
| localStorage.removeItem(LocalStorageKeys.LAST_CONVO_SETUP); |
| localStorage.removeItem(`${LocalStorageKeys.LAST_CONVO_SETUP}_0`); |
| localStorage.removeItem(`${LocalStorageKeys.LAST_CONVO_SETUP}_1`); |
| localStorage.removeItem(LocalStorageKeys.LAST_MODEL); |
| localStorage.removeItem(LocalStorageKeys.LAST_TOOLS); |
| localStorage.removeItem(LocalStorageKeys.FILES_TO_DELETE); |
| |
| options?.onMutate?.(...args); |
| }, |
| }); |
| }; |
|
|
| |
| export const useUploadAvatarMutation = ( |
| options?: t.UploadAvatarOptions, |
| ): UseMutationResult< |
| t.AvatarUploadResponse, |
| unknown, |
| FormData, |
| unknown |
| > => { |
| return useMutation([MutationKeys.avatarUpload], { |
| mutationFn: (variables: FormData) => dataService.uploadAvatar(variables), |
| ...(options || {}), |
| }); |
| }; |
|
|
| export const useDeleteUserMutation = ( |
| options?: t.MutationOptions<unknown, undefined>, |
| ): UseMutationResult<unknown, unknown, undefined, unknown> => { |
| const queryClient = useQueryClient(); |
| const setDefaultPreset = useSetRecoilState(store.defaultPreset); |
| return useMutation([MutationKeys.deleteUser], { |
| mutationFn: () => dataService.deleteUser(), |
|
|
| ...(options || {}), |
| onSuccess: (...args) => { |
| options?.onSuccess?.(...args); |
| }, |
| onMutate: (...args) => { |
| setDefaultPreset(null); |
| queryClient.removeQueries(); |
| localStorage.removeItem(LocalStorageKeys.LAST_CONVO_SETUP); |
| localStorage.removeItem(`${LocalStorageKeys.LAST_CONVO_SETUP}_0`); |
| localStorage.removeItem(`${LocalStorageKeys.LAST_CONVO_SETUP}_1`); |
| localStorage.removeItem(LocalStorageKeys.LAST_MODEL); |
| localStorage.removeItem(LocalStorageKeys.LAST_TOOLS); |
| localStorage.removeItem(LocalStorageKeys.FILES_TO_DELETE); |
| options?.onMutate?.(...args); |
| }, |
| }); |
| }; |
|
|
| |
| export const useSpeechToTextMutation = ( |
| options?: t.SpeechToTextOptions, |
| ): UseMutationResult< |
| t.SpeechToTextResponse, |
| unknown, |
| FormData, |
| unknown |
| > => { |
| return useMutation([MutationKeys.speechToText], { |
| mutationFn: (variables: FormData) => dataService.speechToText(variables), |
| ...(options || {}), |
| }); |
| }; |
|
|
| |
| export const useTextToSpeechMutation = ( |
| options?: t.TextToSpeechOptions, |
| ): UseMutationResult< |
| ArrayBuffer, |
| unknown, |
| FormData, |
| unknown |
| > => { |
| return useMutation([MutationKeys.textToSpeech], { |
| mutationFn: (variables: FormData) => dataService.textToSpeech(variables), |
| ...(options || {}), |
| }); |
| }; |
|
|
| |
| |
| |
|
|
| |
| |
| |
| export const useCreateAssistantMutation = ( |
| options?: t.CreateAssistantMutationOptions, |
| ): UseMutationResult<t.Assistant, Error, t.AssistantCreateParams> => { |
| const queryClient = useQueryClient(); |
| return useMutation( |
| (newAssistantData: t.AssistantCreateParams) => dataService.createAssistant(newAssistantData), |
| { |
| onMutate: (variables) => options?.onMutate?.(variables), |
| onError: (error, variables, context) => options?.onError?.(error, variables, context), |
| onSuccess: (newAssistant, variables, context) => { |
| const listRes = queryClient.getQueryData<t.AssistantListResponse>([ |
| QueryKeys.assistants, |
| variables.endpoint, |
| defaultOrderQuery, |
| ]); |
|
|
| if (!listRes) { |
| return options?.onSuccess?.(newAssistant, variables, context); |
| } |
|
|
| const currentAssistants = [newAssistant, ...JSON.parse(JSON.stringify(listRes.data))]; |
|
|
| queryClient.setQueryData<t.AssistantListResponse>( |
| [QueryKeys.assistants, variables.endpoint, defaultOrderQuery], |
| { |
| ...listRes, |
| data: currentAssistants, |
| }, |
| ); |
| return options?.onSuccess?.(newAssistant, variables, context); |
| }, |
| }, |
| ); |
| }; |
|
|
| |
| |
| |
| export const useUpdateAssistantMutation = ( |
| options?: t.UpdateAssistantMutationOptions, |
| ): UseMutationResult< |
| t.Assistant, |
| Error, |
| { assistant_id: string; data: t.AssistantUpdateParams } |
| > => { |
| const queryClient = useQueryClient(); |
| return useMutation( |
| ({ assistant_id, data }: { assistant_id: string; data: t.AssistantUpdateParams }) => { |
| const { endpoint } = data; |
| const endpointsConfig = queryClient.getQueryData<t.TEndpointsConfig>([QueryKeys.endpoints]); |
| const version = endpointsConfig?.[endpoint]?.version ?? defaultAssistantsVersion[endpoint]; |
| return dataService.updateAssistant({ |
| data, |
| version, |
| assistant_id, |
| }); |
| }, |
| { |
| onMutate: (variables) => options?.onMutate?.(variables), |
| onError: (error, variables, context) => options?.onError?.(error, variables, context), |
| onSuccess: (updatedAssistant, variables, context) => { |
| const listRes = queryClient.getQueryData<t.AssistantListResponse>([ |
| QueryKeys.assistants, |
| variables.data.endpoint, |
| defaultOrderQuery, |
| ]); |
|
|
| if (!listRes) { |
| return options?.onSuccess?.(updatedAssistant, variables, context); |
| } |
|
|
| queryClient.setQueryData<t.AssistantListResponse>( |
| [QueryKeys.assistants, variables.data.endpoint, defaultOrderQuery], |
| { |
| ...listRes, |
| data: listRes.data.map((assistant) => { |
| if (assistant.id === variables.assistant_id) { |
| return updatedAssistant; |
| } |
| return assistant; |
| }), |
| }, |
| ); |
| return options?.onSuccess?.(updatedAssistant, variables, context); |
| }, |
| }, |
| ); |
| }; |
|
|
| |
| |
| |
| export const useDeleteAssistantMutation = ( |
| options?: t.DeleteAssistantMutationOptions, |
| ): UseMutationResult<void, Error, t.DeleteAssistantBody> => { |
| const queryClient = useQueryClient(); |
| return useMutation( |
| ({ assistant_id, model, endpoint }: t.DeleteAssistantBody) => { |
| const endpointsConfig = queryClient.getQueryData<t.TEndpointsConfig>([QueryKeys.endpoints]); |
| const version = endpointsConfig?.[endpoint]?.version ?? defaultAssistantsVersion[endpoint]; |
| return dataService.deleteAssistant({ assistant_id, model, version, endpoint }); |
| }, |
| { |
| onMutate: (variables) => options?.onMutate?.(variables), |
| onError: (error, variables, context) => options?.onError?.(error, variables, context), |
| onSuccess: (_data, variables, context) => { |
| const listRes = queryClient.getQueryData<t.AssistantListResponse>([ |
| QueryKeys.assistants, |
| variables.endpoint, |
| defaultOrderQuery, |
| ]); |
|
|
| if (!listRes) { |
| return options?.onSuccess?.(_data, variables, context); |
| } |
|
|
| const data = listRes.data.filter((assistant) => assistant.id !== variables.assistant_id); |
|
|
| queryClient.setQueryData<t.AssistantListResponse>( |
| [QueryKeys.assistants, variables.endpoint, defaultOrderQuery], |
| { |
| ...listRes, |
| data, |
| }, |
| ); |
|
|
| return options?.onSuccess?.(_data, variables, data); |
| }, |
| }, |
| ); |
| }; |
|
|
| |
| |
| |
| export const useUploadAssistantAvatarMutation = ( |
| options?: t.UploadAssistantAvatarOptions, |
| ): UseMutationResult< |
| t.Assistant, |
| unknown, |
| t.AssistantAvatarVariables, |
| unknown |
| > => { |
| return useMutation([MutationKeys.assistantAvatarUpload], { |
| |
| mutationFn: ({ postCreation, ...variables }: t.AssistantAvatarVariables) => |
| dataService.uploadAssistantAvatar(variables), |
| ...(options || {}), |
| }); |
| }; |
|
|
| |
| |
| |
| export const useUpdateAction = ( |
| options?: t.UpdateActionOptions, |
| ): UseMutationResult< |
| t.UpdateActionResponse, |
| unknown, |
| t.UpdateActionVariables, |
| unknown |
| > => { |
| const queryClient = useQueryClient(); |
| return useMutation([MutationKeys.updateAction], { |
| mutationFn: (variables: t.UpdateActionVariables) => dataService.updateAction(variables), |
|
|
| onMutate: (variables) => options?.onMutate?.(variables), |
| onError: (error, variables, context) => options?.onError?.(error, variables, context), |
| onSuccess: (updateActionResponse, variables, context) => { |
| const listRes = queryClient.getQueryData<t.AssistantListResponse>([ |
| QueryKeys.assistants, |
| variables.endpoint, |
| defaultOrderQuery, |
| ]); |
|
|
| if (!listRes) { |
| return options?.onSuccess?.(updateActionResponse, variables, context); |
| } |
|
|
| const updatedAssistant = updateActionResponse[1]; |
|
|
| queryClient.setQueryData<t.AssistantListResponse>( |
| [QueryKeys.assistants, variables.endpoint, defaultOrderQuery], |
| { |
| ...listRes, |
| data: listRes.data.map((assistant) => { |
| if (assistant.id === variables.assistant_id) { |
| return updatedAssistant; |
| } |
| return assistant; |
| }), |
| }, |
| ); |
|
|
| queryClient.setQueryData<t.Action[]>([QueryKeys.actions], (prev) => { |
| return prev |
| ?.map((action) => { |
| if (action.action_id === variables.action_id) { |
| return updateActionResponse[2]; |
| } |
| return action; |
| }) |
| .concat(variables.action_id ? [] : [updateActionResponse[2]]); |
| }); |
|
|
| return options?.onSuccess?.(updateActionResponse, variables, context); |
| }, |
| }); |
| }; |
|
|
| |
| |
| |
| export const useDeleteAction = ( |
| options?: t.DeleteActionOptions, |
| ): UseMutationResult< |
| void, |
| Error, |
| t.DeleteActionVariables, |
| unknown |
| > => { |
| const queryClient = useQueryClient(); |
| return useMutation([MutationKeys.deleteAction], { |
| mutationFn: (variables: t.DeleteActionVariables) => { |
| const { endpoint } = variables; |
| const endpointsConfig = queryClient.getQueryData<t.TEndpointsConfig>([QueryKeys.endpoints]); |
| const version = endpointsConfig?.[endpoint]?.version ?? defaultAssistantsVersion[endpoint]; |
| return dataService.deleteAction({ |
| ...variables, |
| version, |
| }); |
| }, |
|
|
| onMutate: (variables) => options?.onMutate?.(variables), |
| onError: (error, variables, context) => options?.onError?.(error, variables, context), |
| onSuccess: (_data, variables, context) => { |
| let domain: string | undefined = ''; |
| queryClient.setQueryData<t.Action[]>([QueryKeys.actions], (prev) => { |
| return prev?.filter((action) => { |
| domain = action.metadata.domain; |
| return action.action_id !== variables.action_id; |
| }); |
| }); |
|
|
| queryClient.setQueryData<t.AssistantListResponse>( |
| [QueryKeys.assistants, variables.endpoint, defaultOrderQuery], |
| (prev) => { |
| if (!prev) { |
| return prev; |
| } |
|
|
| return { |
| ...prev, |
| data: prev?.data.map((assistant) => { |
| if (assistant.id === variables.assistant_id) { |
| return { |
| ...assistant, |
| tools: assistant.tools.filter( |
| (tool) => !tool.function?.name.includes(domain ?? ''), |
| ), |
| }; |
| } |
| return assistant; |
| }), |
| }; |
| }, |
| ); |
|
|
| return options?.onSuccess?.(_data, variables, context); |
| }, |
| }); |
| }; |
|
|
| |
| |
| |
| export const useVerifyEmailMutation = ( |
| options?: t.VerifyEmailOptions, |
| ): UseMutationResult<t.VerifyEmailResponse, unknown, t.TVerifyEmail, unknown> => { |
| return useMutation({ |
| mutationFn: (variables: t.TVerifyEmail) => dataService.verifyEmail(variables), |
| ...(options || {}), |
| }); |
| }; |
|
|
| |
| |
| |
| export const useResendVerificationEmail = ( |
| options?: t.ResendVerifcationOptions, |
| ): UseMutationResult<t.VerifyEmailResponse, unknown, t.TResendVerificationEmail, unknown> => { |
| return useMutation({ |
| mutationFn: (variables: t.TResendVerificationEmail) => |
| dataService.resendVerificationEmail(variables), |
| ...(options || {}), |
| }); |
| }; |
|
|