mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* chore: fix `endpoint` typescript issues and typo in console info message * feat(api): files GET endpoint and save only file_id references to messages * refactor(client): `useGetFiles` query hook, update file types, optimistic update of filesQuery on file upload * refactor(buildTree): update to use params object and accept fileMap * feat: map files to messages; refactor(ChatView): messages only available after files are fetched * fix: fetch files only when authenticated * feat(api): AppService - rename app.locals.configs to app.locals.paths - load custom config use fileStrategy from yaml config in app.locals * refactor: separate Firebase and Local strategies, call based on config * refactor: modularize file strategies and employ with use of DALL-E * refactor(librechat.yaml): add fileStrategy field * feat: add source to MongoFile schema, as well as BatchFile, and ExtendedFile types * feat: employ file strategies for upload/delete files * refactor(deleteFirebaseFile): add user id validation for firebase file deletion * chore(deleteFirebaseFile): update jsdocs * feat: employ strategies for vision requests * fix(client): handle messages with deleted files * fix(client): ensure `filesToDelete` always saves/sends `file.source` * feat(openAI): configurable `resendImages` and `imageDetail` * refactor(getTokenCountForMessage): recursive process only when array of Objects and only their values (not keys) aside from `image_url` types * feat(OpenAIClient): calculateImageTokenCost * chore: remove comment * refactor(uploadAvatar): employ fileStrategy for avatars, from social logins or user upload * docs: update docs on how to configure fileStrategy * fix(ci): mock winston and winston related modules, update DALLE3.spec.js with changes made * refactor(redis): change terminal message to reflect current development state * fix(DALL-E-2): pass fileStrategy to dall-e
152 lines
4.8 KiB
TypeScript
152 lines
4.8 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { FileSources } from 'librechat-data-provider';
|
|
import { useGetEndpointsQuery } from 'librechat-data-provider/react-query';
|
|
import {
|
|
useSetRecoilState,
|
|
useResetRecoilState,
|
|
useRecoilCallback,
|
|
useRecoilState,
|
|
useRecoilValue,
|
|
} from 'recoil';
|
|
import type {
|
|
TConversation,
|
|
TSubmission,
|
|
TPreset,
|
|
TModelsConfig,
|
|
TEndpointsConfig,
|
|
} from 'librechat-data-provider';
|
|
import { buildDefaultConvo, getDefaultEndpoint, getEndpointField } from '~/utils';
|
|
import { useDeleteFilesMutation } from '~/data-provider';
|
|
import useOriginNavigate from './useOriginNavigate';
|
|
import useSetStorage from './useSetStorage';
|
|
import store from '~/store';
|
|
|
|
const useNewConvo = (index = 0) => {
|
|
const setStorage = useSetStorage();
|
|
const navigate = useOriginNavigate();
|
|
const defaultPreset = useRecoilValue(store.defaultPreset);
|
|
const { setConversation } = store.useCreateConversationAtom(index);
|
|
const [files, setFiles] = useRecoilState(store.filesByIndex(index));
|
|
const setSubmission = useSetRecoilState<TSubmission | null>(store.submissionByIndex(index));
|
|
const resetLatestMessage = useResetRecoilState(store.latestMessageFamily(index));
|
|
const { data: endpointsConfig = {} as TEndpointsConfig } = useGetEndpointsQuery();
|
|
|
|
const { mutateAsync } = useDeleteFilesMutation({
|
|
onSuccess: () => {
|
|
console.log('Files deleted');
|
|
},
|
|
onError: (error) => {
|
|
console.log('Error deleting files:', error);
|
|
},
|
|
});
|
|
|
|
const switchToConversation = useRecoilCallback(
|
|
({ snapshot }) =>
|
|
async (
|
|
conversation: TConversation,
|
|
preset: TPreset | null = null,
|
|
modelsData?: TModelsConfig,
|
|
buildDefault?: boolean,
|
|
keepLatestMessage?: boolean,
|
|
) => {
|
|
const modelsConfig = modelsData ?? snapshot.getLoadable(store.modelsConfig).contents;
|
|
const { endpoint = null } = conversation;
|
|
const buildDefaultConversation = endpoint === null || buildDefault;
|
|
const activePreset =
|
|
// use default preset only when it's defined,
|
|
// preset is not provided,
|
|
// endpoint matches or is null (to allow endpoint change),
|
|
// and buildDefaultConversation is true
|
|
defaultPreset &&
|
|
!preset &&
|
|
(defaultPreset.endpoint === endpoint || !endpoint) &&
|
|
buildDefaultConversation
|
|
? defaultPreset
|
|
: preset;
|
|
|
|
if (buildDefaultConversation) {
|
|
const defaultEndpoint = getDefaultEndpoint({
|
|
convoSetup: activePreset ?? conversation,
|
|
endpointsConfig,
|
|
});
|
|
|
|
const endpointType = getEndpointField(endpointsConfig, defaultEndpoint, 'type');
|
|
if (!conversation.endpointType && endpointType) {
|
|
conversation.endpointType = endpointType;
|
|
}
|
|
|
|
const models = modelsConfig?.[defaultEndpoint] ?? [];
|
|
conversation = buildDefaultConvo({
|
|
conversation,
|
|
lastConversationSetup: activePreset as TConversation,
|
|
endpoint: defaultEndpoint,
|
|
models,
|
|
});
|
|
}
|
|
|
|
setStorage(conversation);
|
|
setConversation(conversation);
|
|
setSubmission({} as TSubmission);
|
|
if (!keepLatestMessage) {
|
|
resetLatestMessage();
|
|
}
|
|
|
|
if (conversation.conversationId === 'new' && !modelsData) {
|
|
navigate('new');
|
|
}
|
|
},
|
|
[endpointsConfig, defaultPreset],
|
|
);
|
|
|
|
const newConversation = useCallback(
|
|
({
|
|
template = {},
|
|
preset,
|
|
modelsData,
|
|
buildDefault = true,
|
|
keepLatestMessage = false,
|
|
}: {
|
|
template?: Partial<TConversation>;
|
|
preset?: TPreset;
|
|
modelsData?: TModelsConfig;
|
|
buildDefault?: boolean;
|
|
keepLatestMessage?: boolean;
|
|
} = {}) => {
|
|
const conversation = {
|
|
conversationId: 'new',
|
|
title: 'New Chat',
|
|
endpoint: null,
|
|
...template,
|
|
createdAt: '',
|
|
updatedAt: '',
|
|
};
|
|
|
|
if (conversation.conversationId === 'new' && !modelsData) {
|
|
const filesToDelete = Array.from(files.values())
|
|
.filter((file) => file.filepath && file.source)
|
|
.map((file) => ({
|
|
file_id: file.file_id,
|
|
filepath: file.filepath as string,
|
|
source: file.source as FileSources, // Ensure that the source is of type FileSources
|
|
}));
|
|
|
|
setFiles(new Map());
|
|
localStorage.setItem('filesToDelete', JSON.stringify({}));
|
|
|
|
if (filesToDelete.length > 0) {
|
|
mutateAsync({ files: filesToDelete });
|
|
}
|
|
}
|
|
|
|
switchToConversation(conversation, preset, modelsData, buildDefault, keepLatestMessage);
|
|
},
|
|
[switchToConversation, files, mutateAsync, setFiles],
|
|
);
|
|
|
|
return {
|
|
switchToConversation,
|
|
newConversation,
|
|
};
|
|
};
|
|
|
|
export default useNewConvo;
|