File size: 1,778 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 | import { useCallback } from 'react';
import { isAssistantsEndpoint } from 'librechat-data-provider';
import type { AssistantsEndpoint, TConversation, TPreset } from 'librechat-data-provider';
import useDefaultConvo from '~/hooks/Conversations/useDefaultConvo';
import { useChatContext } from '~/Providers/ChatContext';
import useAssistantListMap from './useAssistantListMap';
import { mapAssistants, logger } from '~/utils';
export default function useSelectAssistant(endpoint: AssistantsEndpoint) {
const getDefaultConversation = useDefaultConvo();
const { conversation, newConversation } = useChatContext();
const assistantMap = useAssistantListMap((res) => mapAssistants(res.data));
const onSelect = useCallback(
(value: string) => {
const assistant = assistantMap[endpoint]?.[value];
if (!assistant) {
return;
}
const template: Partial<TPreset | TConversation> = {
endpoint,
assistant_id: assistant.id,
model: assistant.model,
conversationId: 'new',
};
logger.log('conversation', 'Updating conversation with assistant', assistant);
if (isAssistantsEndpoint(conversation?.endpoint)) {
const currentConvo = getDefaultConversation({
conversation: { ...(conversation ?? {}) },
preset: template,
});
newConversation({
template: currentConvo,
preset: template as Partial<TPreset>,
keepLatestMessage: true,
});
return;
}
newConversation({
template: { ...(template as Partial<TConversation>) },
preset: template as Partial<TPreset>,
});
},
[endpoint, assistantMap, conversation, getDefaultConversation, newConversation],
);
return { onSelect };
}
|