File size: 4,718 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | import { useRecoilValue } from 'recoil';
import { useCallback, useRef, useEffect } from 'react';
import { useGetModelsQuery } from 'librechat-data-provider/react-query';
import { getEndpointField, LocalStorageKeys, isAssistantsEndpoint } from 'librechat-data-provider';
import type {
TEndpointsConfig,
EModelEndpoint,
TModelsConfig,
TConversation,
TPreset,
} from 'librechat-data-provider';
import type { AssistantListItem } from '~/common';
import type { SetterOrUpdater } from 'recoil';
import useAssistantListMap from '~/hooks/Assistants/useAssistantListMap';
import { buildDefaultConvo, getDefaultEndpoint, logger } from '~/utils';
import { useGetEndpointsQuery } from '~/data-provider';
import { mainTextareaId } from '~/common';
import store from '~/store';
const useGenerateConvo = ({
index = 0,
rootIndex,
setConversation,
}: {
index?: number;
rootIndex: number;
setConversation?: SetterOrUpdater<TConversation | null>;
}) => {
const modelsQuery = useGetModelsQuery();
const assistantsListMap = useAssistantListMap();
const { data: endpointsConfig = {} as TEndpointsConfig } = useGetEndpointsQuery();
const timeoutIdRef = useRef<NodeJS.Timeout>();
const rootConvo = useRecoilValue(store.conversationByKeySelector(rootIndex));
useEffect(() => {
if (rootConvo?.conversationId != null && setConversation) {
setConversation((prevState) => {
if (!prevState) {
return prevState;
}
const update = {
...prevState,
conversationId: rootConvo.conversationId,
} as TConversation;
logger.log('conversation', 'Setting conversation from `useNewConvo`', update);
return update;
});
}
}, [rootConvo?.conversationId, setConversation]);
const generateConversation = useCallback(
({
template = {},
preset,
modelsData,
}: {
template?: Partial<TConversation>;
preset?: Partial<TPreset>;
modelsData?: TModelsConfig;
} = {}) => {
let conversation = {
conversationId: 'new',
title: 'New Chat',
endpoint: null,
...template,
createdAt: '',
updatedAt: '',
};
if (rootConvo?.conversationId) {
conversation.conversationId = rootConvo.conversationId;
}
const modelsConfig = modelsData ?? modelsQuery.data;
const defaultEndpoint = getDefaultEndpoint({
convoSetup: preset ?? conversation,
endpointsConfig,
});
const endpointType = getEndpointField(endpointsConfig, defaultEndpoint, 'type');
if (!conversation.endpointType && endpointType) {
conversation.endpointType = endpointType;
} else if (conversation.endpointType && !endpointType) {
conversation.endpointType = undefined;
}
const isAssistantEndpoint = isAssistantsEndpoint(defaultEndpoint);
const assistants: AssistantListItem[] = assistantsListMap[defaultEndpoint ?? ''] ?? [];
if (
conversation.assistant_id &&
!assistantsListMap[defaultEndpoint ?? '']?.[conversation.assistant_id]
) {
conversation.assistant_id = undefined;
}
if (!conversation.assistant_id && isAssistantEndpoint) {
conversation.assistant_id =
localStorage.getItem(`${LocalStorageKeys.ASST_ID_PREFIX}${index}${defaultEndpoint}`) ??
assistants[0]?.id;
}
if (
conversation.assistant_id != null &&
isAssistantEndpoint &&
conversation.conversationId === 'new'
) {
const assistant = assistants.find((asst) => asst.id === conversation.assistant_id);
conversation.model = assistant?.model;
}
if (conversation.assistant_id != null && !isAssistantEndpoint) {
conversation.assistant_id = undefined;
}
const models = modelsConfig?.[defaultEndpoint ?? ''] ?? [];
conversation = buildDefaultConvo({
conversation,
lastConversationSetup: preset as TConversation,
endpoint: defaultEndpoint ?? ('' as EModelEndpoint),
models,
});
if (preset?.title != null && preset.title !== '') {
conversation.title = preset.title;
}
if (setConversation) {
setConversation(conversation);
}
clearTimeout(timeoutIdRef.current);
timeoutIdRef.current = setTimeout(() => {
const textarea = document.getElementById(mainTextareaId);
if (textarea) {
textarea.focus();
}
}, 150);
return conversation;
},
[assistantsListMap, endpointsConfig, index, modelsQuery.data, rootConvo, setConversation],
);
return { generateConversation };
};
export default useGenerateConvo;
|