* wip: Add Instructions component for agent configuration * ✨ feat: Implement DropdownPopup for variable insertion in instructions * refactor: Enhance variable handling by exporting specialVariables and updating Markdown components * feat: Add special variable support for current date and user in Instructions component * refactor: Update handleAddVariable to include localized label * feat: replace special variables in instructions presets * chore: update parameter type for user in getListAgents function * refactor: integrate dayjs for date handling and move replaceSpecialVars function to data-provider * feat: enhance replaceSpecialVars to include day number in current date format * feat: integrate replaceSpecialVars for processing agent instructions * feat: add support for current date & time in replaceSpecialVars function * feat: add iso_datetime support in replaceSpecialVars function * fix: enforce text parameter to be a required field in replaceSpecialVars function * feat: add ISO datetime support in translation file * fix: disable eslint warning for autoFocus in TextareaAutosize component * feat: add VariablesDropdown component and integrate it into CreatePromptForm and PromptEditor; update translation for special variables * fix: CategorySelector and related localizations * fix: add z-index class to LanguageSTTDropdown for proper stacking context * fix: add max-height and overflow styles to OGDialogContent in VariableDialog and PreviewPrompt components * fix: update variable detection logic to exclude special variables and improve regex matching * fix: improve accessibility text for actions menu in ChatGroupItem component * fix: adjust max-width and height styles for dialog components and improve markdown rendering for light vs. dark, height/widths, etc. * fix: remove commented-out code for better readability in PromptVariableGfm component * fix: handle undefined input parameter in setParams function call * fix: update variable label types to use TSpecialVarLabel for consistency * fix: remove outdated information from special variables description in translation file * fix: enhance unused i18next keys detection for special variable keys * fix: update color classes for consistency/a11y in category and prompt variable components * fix: update PromptVariableGfm component and special variable styles for consistency * fix: improve variable highlighting logic in VariableForm component * fix: update background color classes for consistency in VariableForm component * fix: add missing ref parameter to Dialog component in OriginalDialog * refactor: move navigate call for new conversation to after setConversation update * refactor: move message query hook to client workspace; fix: handle edge case for navigation from finalHandler creating race condition for response message DB save * chore: bump librechat-data-provider to 0.7.793 * ci: add unit tests for replaceSpecialVars function * fix: implement getToolkitKey function for image_gen_oai toolkit filtering/including * ci: enhance dayjs mock for consistent date/time values in tests * fix: MCP stdio server fail to start when passing env property * fix: use optional chaining for clientRef dereferencing in AskController and EditController feat: add context to saveMessage call in streamResponse utility * fix: only save error messages if the userMessageId was initialized * refactor: add isNotAppendable check to disable inputs in ChatForm and useTextarea * feat: enhance error handling in useEventHandlers and update conversation state in useNewConvo * refactor: prepend underscore to conversationId in newConversation template * feat: log aborted conversations with minimal messages and use consistent conversationId generation --------- Co-authored-by: Olivier Schiavo <olivier.schiavo@wengo.com> Co-authored-by: aka012 <aka012@neowiz.com> Co-authored-by: jiasheng <jiashengguo@outlook.com>
350 lines
10 KiB
TypeScript
350 lines
10 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import type {
|
|
UseQueryOptions,
|
|
UseMutationResult,
|
|
QueryObserverResult,
|
|
} from '@tanstack/react-query';
|
|
import { Constants, initialModelsConfig } from '../config';
|
|
import { defaultOrderQuery } from '../types/assistants';
|
|
import * as dataService from '../data-service';
|
|
import * as m from '../types/mutations';
|
|
import { QueryKeys } from '../keys';
|
|
import * as s from '../schemas';
|
|
import * as t from '../types';
|
|
|
|
export const useAbortRequestWithMessage = (): UseMutationResult<
|
|
void,
|
|
Error,
|
|
{ endpoint: string; abortKey: string; message: string }
|
|
> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation(
|
|
({ endpoint, abortKey, message }) =>
|
|
dataService.abortRequestWithMessage(endpoint, abortKey, message),
|
|
{
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.balance]);
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
export const useGetSharedMessages = (
|
|
shareId: string,
|
|
config?: UseQueryOptions<t.TSharedMessagesResponse>,
|
|
): QueryObserverResult<t.TSharedMessagesResponse> => {
|
|
return useQuery<t.TSharedMessagesResponse>(
|
|
[QueryKeys.sharedMessages, shareId],
|
|
() => dataService.getSharedMessages(shareId),
|
|
{
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
...config,
|
|
},
|
|
);
|
|
};
|
|
|
|
export const useGetSharedLinkQuery = (
|
|
conversationId: string,
|
|
config?: UseQueryOptions<t.TSharedLinkGetResponse>,
|
|
): QueryObserverResult<t.TSharedLinkGetResponse> => {
|
|
const queryClient = useQueryClient();
|
|
return useQuery<t.TSharedLinkGetResponse>(
|
|
[QueryKeys.sharedLinks, conversationId],
|
|
() => dataService.getSharedLink(conversationId),
|
|
{
|
|
enabled:
|
|
!!conversationId &&
|
|
conversationId !== Constants.NEW_CONVO &&
|
|
conversationId !== Constants.PENDING_CONVO,
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
onSuccess: (data) => {
|
|
queryClient.setQueryData([QueryKeys.sharedLinks, conversationId], {
|
|
conversationId: data.conversationId,
|
|
shareId: data.shareId,
|
|
});
|
|
},
|
|
...config,
|
|
},
|
|
);
|
|
};
|
|
|
|
export const useGetConversationByIdQuery = (
|
|
id: string,
|
|
config?: UseQueryOptions<s.TConversation>,
|
|
): QueryObserverResult<s.TConversation> => {
|
|
return useQuery<s.TConversation>(
|
|
[QueryKeys.conversation, id],
|
|
() => dataService.getConversationById(id),
|
|
{
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
...config,
|
|
},
|
|
);
|
|
};
|
|
|
|
//This isn't ideal because its just a query and we're using mutation, but it was the only way
|
|
//to make it work with how the Chat component is structured
|
|
export const useGetConversationByIdMutation = (id: string): UseMutationResult<s.TConversation> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation(() => dataService.getConversationById(id), {
|
|
// onSuccess: (res: s.TConversation) => {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.conversation, id]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useUpdateMessageMutation = (
|
|
id: string,
|
|
): UseMutationResult<unknown, unknown, t.TUpdateMessageRequest, unknown> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation((payload: t.TUpdateMessageRequest) => dataService.updateMessage(payload), {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.messages, id]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useUpdateMessageContentMutation = (
|
|
conversationId: string,
|
|
): UseMutationResult<unknown, unknown, t.TUpdateMessageContent, unknown> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation(
|
|
(payload: t.TUpdateMessageContent) => dataService.updateMessageContent(payload),
|
|
{
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.messages, conversationId]);
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
export const useUpdateUserKeysMutation = (): UseMutationResult<
|
|
t.TUser,
|
|
unknown,
|
|
t.TUpdateUserKeyRequest,
|
|
unknown
|
|
> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation((payload: t.TUpdateUserKeyRequest) => dataService.updateUserKey(payload), {
|
|
onSuccess: (data, variables) => {
|
|
queryClient.invalidateQueries([QueryKeys.name, variables.name]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useClearConversationsMutation = (): UseMutationResult<unknown> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation(() => dataService.clearAllConversations(), {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.allConversations]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useRevokeUserKeyMutation = (name: string): UseMutationResult<unknown> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation(() => dataService.revokeUserKey(name), {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.name, name]);
|
|
if (s.isAssistantsEndpoint(name)) {
|
|
queryClient.invalidateQueries([QueryKeys.assistants, name, defaultOrderQuery]);
|
|
queryClient.invalidateQueries([QueryKeys.assistantDocs]);
|
|
queryClient.invalidateQueries([QueryKeys.assistants]);
|
|
queryClient.invalidateQueries([QueryKeys.assistant]);
|
|
queryClient.invalidateQueries([QueryKeys.actions]);
|
|
queryClient.invalidateQueries([QueryKeys.tools]);
|
|
}
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useRevokeAllUserKeysMutation = (): UseMutationResult<unknown> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation(() => dataService.revokeAllUserKeys(), {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.name]);
|
|
queryClient.invalidateQueries([
|
|
QueryKeys.assistants,
|
|
s.EModelEndpoint.assistants,
|
|
defaultOrderQuery,
|
|
]);
|
|
queryClient.invalidateQueries([
|
|
QueryKeys.assistants,
|
|
s.EModelEndpoint.azureAssistants,
|
|
defaultOrderQuery,
|
|
]);
|
|
queryClient.invalidateQueries([QueryKeys.assistantDocs]);
|
|
queryClient.invalidateQueries([QueryKeys.assistants]);
|
|
queryClient.invalidateQueries([QueryKeys.assistant]);
|
|
queryClient.invalidateQueries([QueryKeys.actions]);
|
|
queryClient.invalidateQueries([QueryKeys.tools]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useGetModelsQuery = (
|
|
config?: UseQueryOptions<t.TModelsConfig>,
|
|
): QueryObserverResult<t.TModelsConfig> => {
|
|
return useQuery<t.TModelsConfig>([QueryKeys.models], () => dataService.getModels(), {
|
|
initialData: initialModelsConfig,
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
staleTime: Infinity,
|
|
...config,
|
|
});
|
|
};
|
|
|
|
export const useCreatePresetMutation = (): UseMutationResult<
|
|
s.TPreset,
|
|
unknown,
|
|
s.TPreset,
|
|
unknown
|
|
> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation((payload: s.TPreset) => dataService.createPreset(payload), {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useDeletePresetMutation = (): UseMutationResult<
|
|
m.PresetDeleteResponse,
|
|
unknown,
|
|
s.TPreset | undefined,
|
|
unknown
|
|
> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation((payload: s.TPreset | undefined) => dataService.deletePreset(payload), {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useUpdateTokenCountMutation = (): UseMutationResult<
|
|
t.TUpdateTokenCountResponse,
|
|
unknown,
|
|
{ text: string },
|
|
unknown
|
|
> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation(({ text }: { text: string }) => dataService.updateTokenCount(text), {
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries([QueryKeys.tokenCount]);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useRegisterUserMutation = (
|
|
options?: m.RegistrationOptions,
|
|
): UseMutationResult<t.TError, unknown, t.TRegisterUser, unknown> => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation<t.TRegisterUserResponse, t.TError, t.TRegisterUser>(
|
|
(payload: t.TRegisterUser) => dataService.register(payload),
|
|
{
|
|
...options,
|
|
onSuccess: (...args) => {
|
|
queryClient.invalidateQueries([QueryKeys.user]);
|
|
if (options?.onSuccess) {
|
|
options.onSuccess(...args);
|
|
}
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
export const useUserKeyQuery = (
|
|
name: string,
|
|
config?: UseQueryOptions<t.TCheckUserKeyResponse>,
|
|
): QueryObserverResult<t.TCheckUserKeyResponse> => {
|
|
return useQuery<t.TCheckUserKeyResponse>(
|
|
[QueryKeys.name, name],
|
|
() => {
|
|
if (!name) {
|
|
return Promise.resolve({ expiresAt: '' });
|
|
}
|
|
return dataService.userKeyQuery(name);
|
|
},
|
|
{
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
retry: false,
|
|
...config,
|
|
},
|
|
);
|
|
};
|
|
|
|
export const useRequestPasswordResetMutation = (): UseMutationResult<
|
|
t.TRequestPasswordResetResponse,
|
|
unknown,
|
|
t.TRequestPasswordReset,
|
|
unknown
|
|
> => {
|
|
return useMutation((payload: t.TRequestPasswordReset) =>
|
|
dataService.requestPasswordReset(payload),
|
|
);
|
|
};
|
|
|
|
export const useResetPasswordMutation = (): UseMutationResult<
|
|
unknown,
|
|
unknown,
|
|
t.TResetPassword,
|
|
unknown
|
|
> => {
|
|
return useMutation((payload: t.TResetPassword) => dataService.resetPassword(payload));
|
|
};
|
|
|
|
export const useAvailablePluginsQuery = <TData = s.TPlugin[]>(
|
|
config?: UseQueryOptions<s.TPlugin[], unknown, TData>,
|
|
): QueryObserverResult<TData> => {
|
|
return useQuery<s.TPlugin[], unknown, TData>(
|
|
[QueryKeys.availablePlugins],
|
|
() => dataService.getAvailablePlugins(),
|
|
{
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
...config,
|
|
},
|
|
);
|
|
};
|
|
|
|
export const useUpdateUserPluginsMutation = (
|
|
_options?: m.UpdatePluginAuthOptions,
|
|
): UseMutationResult<t.TUser, unknown, t.TUpdateUserPlugins, unknown> => {
|
|
const queryClient = useQueryClient();
|
|
const { onSuccess, ...options } = _options ?? {};
|
|
return useMutation((payload: t.TUpdateUserPlugins) => dataService.updateUserPlugins(payload), {
|
|
...options,
|
|
onSuccess: (...args) => {
|
|
queryClient.invalidateQueries([QueryKeys.user]);
|
|
onSuccess?.(...args);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useGetCustomConfigSpeechQuery = (
|
|
config?: UseQueryOptions<t.TCustomConfigSpeechResponse>,
|
|
): QueryObserverResult<t.TCustomConfigSpeechResponse> => {
|
|
return useQuery<t.TCustomConfigSpeechResponse>(
|
|
[QueryKeys.customConfigSpeech],
|
|
() => dataService.getCustomConfigSpeech(),
|
|
{
|
|
refetchOnWindowFocus: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnMount: false,
|
|
...config,
|
|
},
|
|
);
|
|
};
|