2023-12-06 14:00:15 -05:00
|
|
|
import filenamify from 'filenamify';
|
|
|
|
|
import exportFromJSON from 'export-from-json';
|
2024-01-03 19:17:42 -05:00
|
|
|
import { useCallback, useEffect, useRef } from 'react';
|
2023-12-06 14:00:15 -05:00
|
|
|
import { useQueryClient } from '@tanstack/react-query';
|
2024-01-03 19:17:42 -05:00
|
|
|
import { QueryKeys, modularEndpoints } from 'librechat-data-provider';
|
|
|
|
|
import { useRecoilState, useSetRecoilState, useRecoilValue } from 'recoil';
|
|
|
|
|
import { useCreatePresetMutation } from 'librechat-data-provider/react-query';
|
|
|
|
|
import type { TPreset, TEndpointsConfig } from 'librechat-data-provider';
|
2023-12-10 17:13:42 -05:00
|
|
|
import {
|
|
|
|
|
useUpdatePresetMutation,
|
|
|
|
|
useDeletePresetMutation,
|
|
|
|
|
useGetPresetsQuery,
|
|
|
|
|
} from '~/data-provider';
|
2023-12-06 14:00:15 -05:00
|
|
|
import { useChatContext, useToastContext } from '~/Providers';
|
|
|
|
|
import useNavigateToConvo from '~/hooks/useNavigateToConvo';
|
|
|
|
|
import useDefaultConvo from '~/hooks/useDefaultConvo';
|
|
|
|
|
import { useAuthContext } from '~/hooks/AuthContext';
|
|
|
|
|
import { NotificationSeverity } from '~/common';
|
|
|
|
|
import useLocalize from '~/hooks/useLocalize';
|
|
|
|
|
import { cleanupPreset } from '~/utils';
|
|
|
|
|
import store from '~/store';
|
|
|
|
|
|
|
|
|
|
export default function usePresets() {
|
|
|
|
|
const localize = useLocalize();
|
2023-12-10 17:13:42 -05:00
|
|
|
const hasLoaded = useRef(false);
|
2023-12-06 14:00:15 -05:00
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const { showToast } = useToastContext();
|
2023-12-10 17:13:42 -05:00
|
|
|
const { user, isAuthenticated } = useAuthContext();
|
2023-12-06 14:00:15 -05:00
|
|
|
|
2024-01-03 19:17:42 -05:00
|
|
|
const modularChat = useRecoilValue(store.modularChat);
|
2023-12-06 14:00:15 -05:00
|
|
|
const [_defaultPreset, setDefaultPreset] = useRecoilState(store.defaultPreset);
|
|
|
|
|
const setPresetModalVisible = useSetRecoilState(store.presetModalVisible);
|
|
|
|
|
const { preset, conversation, newConversation, setPreset } = useChatContext();
|
2023-12-10 17:13:42 -05:00
|
|
|
const presetsQuery = useGetPresetsQuery({ enabled: !!user && isAuthenticated });
|
2023-12-06 14:00:15 -05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-12-10 17:13:42 -05:00
|
|
|
const { data: presets } = presetsQuery;
|
|
|
|
|
if (_defaultPreset || !presets || hasLoaded.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (presets && presets.length > 0 && user && presets[0].user !== user?.id) {
|
|
|
|
|
presetsQuery.refetch();
|
2023-12-06 14:00:15 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-10 17:13:42 -05:00
|
|
|
const defaultPreset = presets.find((p) => p.defaultPreset);
|
2023-12-06 14:00:15 -05:00
|
|
|
if (!defaultPreset) {
|
|
|
|
|
hasLoaded.current = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setDefaultPreset(defaultPreset);
|
|
|
|
|
if (!conversation?.conversationId || conversation.conversationId === 'new') {
|
|
|
|
|
newConversation({ preset: defaultPreset });
|
|
|
|
|
}
|
|
|
|
|
hasLoaded.current = true;
|
|
|
|
|
// dependencies are stable and only needed once
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-12-10 17:13:42 -05:00
|
|
|
}, [presetsQuery.data, user]);
|
2023-12-06 14:00:15 -05:00
|
|
|
|
|
|
|
|
const setPresets = useCallback(
|
|
|
|
|
(presets: TPreset[]) => {
|
|
|
|
|
queryClient.setQueryData<TPreset[]>([QueryKeys.presets], presets);
|
|
|
|
|
},
|
|
|
|
|
[queryClient],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const deletePresetsMutation = useDeletePresetMutation({
|
|
|
|
|
onMutate: (preset) => {
|
|
|
|
|
if (!preset) {
|
|
|
|
|
setPresets([]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const previousPresets = presetsQuery.data ?? [];
|
|
|
|
|
if (previousPresets) {
|
|
|
|
|
setPresets(previousPresets.filter((p) => p.presetId !== preset?.presetId));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
|
|
|
},
|
|
|
|
|
onError: (error) => {
|
|
|
|
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
|
|
|
console.error('Error deleting the preset:', error);
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_endpoint_preset_delete_error'),
|
|
|
|
|
severity: NotificationSeverity.ERROR,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const createPresetMutation = useCreatePresetMutation();
|
|
|
|
|
const updatePreset = useUpdatePresetMutation({
|
|
|
|
|
onSuccess: (data, preset) => {
|
|
|
|
|
const toastTitle = data.title ? `"${data.title}"` : localize('com_endpoint_preset_title');
|
|
|
|
|
let message = `${toastTitle} ${localize('com_endpoint_preset_saved')}`;
|
|
|
|
|
if (data.defaultPreset && data.presetId !== _defaultPreset?.presetId) {
|
|
|
|
|
message = `${toastTitle} ${localize('com_endpoint_preset_default')}`;
|
|
|
|
|
setDefaultPreset(data);
|
|
|
|
|
newConversation({ preset: data });
|
|
|
|
|
} else if (preset?.defaultPreset === false) {
|
|
|
|
|
setDefaultPreset(null);
|
|
|
|
|
message = `${toastTitle} ${localize('com_endpoint_preset_default_removed')}`;
|
|
|
|
|
}
|
|
|
|
|
showToast({
|
|
|
|
|
message,
|
|
|
|
|
});
|
|
|
|
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
|
|
|
},
|
|
|
|
|
onError: (error) => {
|
|
|
|
|
console.error('Error updating the preset:', error);
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_endpoint_preset_save_error'),
|
|
|
|
|
severity: NotificationSeverity.ERROR,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const { navigateToConvo } = useNavigateToConvo();
|
|
|
|
|
const getDefaultConversation = useDefaultConvo();
|
|
|
|
|
|
|
|
|
|
const { endpoint } = conversation ?? {};
|
|
|
|
|
|
|
|
|
|
const importPreset = (jsonPreset: TPreset) => {
|
|
|
|
|
createPresetMutation.mutate(
|
|
|
|
|
{ ...jsonPreset },
|
|
|
|
|
{
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_endpoint_preset_import'),
|
|
|
|
|
});
|
|
|
|
|
queryClient.invalidateQueries([QueryKeys.presets]);
|
|
|
|
|
},
|
|
|
|
|
onError: (error) => {
|
|
|
|
|
console.error('Error uploading the preset:', error);
|
|
|
|
|
showToast({
|
|
|
|
|
message: localize('com_endpoint_preset_import_error'),
|
|
|
|
|
severity: NotificationSeverity.ERROR,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFileSelected = (jsonData: Record<string, unknown>) => {
|
|
|
|
|
const jsonPreset = { ...cleanupPreset({ preset: jsonData }), presetId: null };
|
|
|
|
|
importPreset(jsonPreset);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSelectPreset = (newPreset: TPreset) => {
|
|
|
|
|
if (!newPreset) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toastTitle = newPreset.title
|
|
|
|
|
? `"${newPreset.title}"`
|
|
|
|
|
: localize('com_endpoint_preset_title');
|
|
|
|
|
|
|
|
|
|
showToast({
|
|
|
|
|
message: `${toastTitle} ${localize('com_endpoint_preset_selected_title')}`,
|
|
|
|
|
showIcon: false,
|
|
|
|
|
duration: 750,
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-03 19:17:42 -05:00
|
|
|
const endpointsConfig = queryClient.getQueryData<TEndpointsConfig>([QueryKeys.endpoints]);
|
|
|
|
|
|
|
|
|
|
const currentEndpointType = endpointsConfig?.[endpoint ?? '']?.type ?? '';
|
|
|
|
|
const endpointType = endpointsConfig?.[newPreset?.endpoint ?? '']?.type;
|
|
|
|
|
|
2023-12-06 14:00:15 -05:00
|
|
|
if (
|
2024-01-03 19:17:42 -05:00
|
|
|
(modularEndpoints.has(endpoint ?? '') || modularEndpoints.has(currentEndpointType)) &&
|
|
|
|
|
(modularEndpoints.has(newPreset?.endpoint ?? '') || modularEndpoints.has(endpointType)) &&
|
|
|
|
|
(endpoint === newPreset?.endpoint || modularChat)
|
2023-12-06 14:00:15 -05:00
|
|
|
) {
|
|
|
|
|
const currentConvo = getDefaultConversation({
|
2024-01-03 19:17:42 -05:00
|
|
|
/* target endpointType is necessary to avoid endpoint mixing */
|
|
|
|
|
conversation: { ...(conversation ?? {}), endpointType },
|
|
|
|
|
preset: { ...newPreset, endpointType },
|
2023-12-06 14:00:15 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* We don't reset the latest message, only when changing settings mid-converstion */
|
|
|
|
|
navigateToConvo(currentConvo, false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newConversation({ preset: newPreset });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onChangePreset = (preset: TPreset) => {
|
|
|
|
|
setPreset(preset);
|
|
|
|
|
setPresetModalVisible(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const clearAllPresets = () => deletePresetsMutation.mutate(undefined);
|
|
|
|
|
|
|
|
|
|
const onDeletePreset = (preset: TPreset) => {
|
|
|
|
|
if (!confirm(localize('com_endpoint_preset_delete_confirm'))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
deletePresetsMutation.mutate(preset);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const submitPreset = () => {
|
|
|
|
|
if (!preset) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updatePreset.mutate(cleanupPreset({ preset }));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSetDefaultPreset = (preset: TPreset, remove = false) => {
|
|
|
|
|
updatePreset.mutate({ ...preset, defaultPreset: !remove });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const exportPreset = () => {
|
|
|
|
|
if (!preset) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const fileName = filenamify(preset?.title || 'preset');
|
|
|
|
|
exportFromJSON({
|
|
|
|
|
data: cleanupPreset({ preset }),
|
|
|
|
|
fileName,
|
|
|
|
|
exportType: exportFromJSON.types.json,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
presetsQuery,
|
|
|
|
|
onSetDefaultPreset,
|
|
|
|
|
onFileSelected,
|
|
|
|
|
onSelectPreset,
|
|
|
|
|
onChangePreset,
|
|
|
|
|
clearAllPresets,
|
|
|
|
|
onDeletePreset,
|
|
|
|
|
submitPreset,
|
|
|
|
|
exportPreset,
|
|
|
|
|
};
|
|
|
|
|
}
|