Skip to content

Commit bab0152

Browse files
authored
🤖 feat: Enhance Assistant Model Handling for Model Specs (danny-avila#4390)
* chore: cleanup type issues in client/src/utils/endpoints * refactor: use Constant enum for 'new' conversationId * refactor: select assistant model if not provided for model spec
1 parent 2846779 commit bab0152

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

client/src/components/Chat/Menus/Models/ModelSpecsMenu.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useMemo } from 'react';
22
import { useRecoilValue } from 'recoil';
3-
import { EModelEndpoint } from 'librechat-data-provider';
43
import { Content, Portal, Root } from '@radix-ui/react-popover';
54
import { useGetEndpointsQuery } from 'librechat-data-provider/react-query';
5+
import { EModelEndpoint, isAssistantsEndpoint } from 'librechat-data-provider';
66
import type { TModelSpec, TConversation, TEndpointsConfig } from 'librechat-data-provider';
7+
import { useChatContext, useAssistantsMapContext } from '~/Providers';
78
import { getConvoSwitchLogic, getModelSpecIconURL } from '~/utils';
89
import { useDefaultConvo, useNewConvo } from '~/hooks';
9-
import { useChatContext } from '~/Providers';
1010
import MenuButton from './MenuButton';
1111
import ModelSpecs from './ModelSpecs';
1212
import store from '~/store';
@@ -18,6 +18,7 @@ export default function ModelSpecsMenu({ modelSpecs }: { modelSpecs?: TModelSpec
1818
const { data: endpointsConfig = {} as TEndpointsConfig } = useGetEndpointsQuery();
1919
const modularChat = useRecoilValue(store.modularChat);
2020
const getDefaultConversation = useDefaultConvo();
21+
const assistantMap = useAssistantsMapContext();
2122

2223
const onSelectSpec = (spec: TModelSpec) => {
2324
const { preset } = spec;
@@ -47,6 +48,10 @@ export default function ModelSpecsMenu({ modelSpecs }: { modelSpecs?: TModelSpec
4748
preset.endpointType = newEndpointType;
4849
}
4950

51+
if (isAssistantsEndpoint(newEndpoint) && preset.assistant_id != null && !(preset.model ?? '')) {
52+
preset.model = assistantMap?.[newEndpoint]?.[preset.assistant_id]?.model;
53+
}
54+
5055
const isModular = isCurrentModular && isNewModular && shouldSwitch;
5156
if (isExistingConversation && isModular) {
5257
template.endpointType = newEndpointType as EModelEndpoint | undefined;

client/src/hooks/Input/useSelectMention.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export default function useSelectMention({
6464
preset.endpointType = newEndpointType;
6565
}
6666

67+
if (isAssistantsEndpoint(newEndpoint) && preset.assistant_id != null && !(preset.model ?? '')) {
68+
preset.model = assistantMap?.[newEndpoint]?.[preset.assistant_id]?.model;
69+
}
70+
6771
const isModular = isCurrentModular && isNewModular && shouldSwitch;
6872
if (isExistingConversation && isModular) {
6973
template.endpointType = newEndpointType as EModelEndpoint | undefined;
@@ -90,7 +94,7 @@ export default function useSelectMention({
9094
keepAddedConvos: isModular,
9195
});
9296
},
93-
[conversation, getDefaultConversation, modularChat, newConversation, endpointsConfig],
97+
[conversation, getDefaultConversation, modularChat, newConversation, endpointsConfig, assistantMap],
9498
);
9599

96100
type Kwargs = {

client/src/hooks/useNewConvo.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from 'librechat-data-provider/react-query';
77
import { useNavigate } from 'react-router-dom';
88
import {
9+
Constants,
910
FileSources,
1011
isParamEndpoint,
1112
LocalStorageKeys,
@@ -116,7 +117,7 @@ const useNewConvo = (index = 0) => {
116117
) ?? assistants[0]?.id;
117118
}
118119

119-
if (currentAssistantId && isAssistantEndpoint && conversation.conversationId === 'new') {
120+
if (currentAssistantId && isAssistantEndpoint && conversation.conversationId === Constants.NEW_CONVO) {
120121
const assistant = assistants.find((asst) => asst.id === currentAssistantId);
121122
conversation.model = assistant?.model;
122123
updateLastSelectedModel({
@@ -147,12 +148,12 @@ const useNewConvo = (index = 0) => {
147148
clearAllLatestMessages();
148149
}
149150

150-
if (conversation.conversationId === 'new' && !modelsData) {
151+
if (conversation.conversationId === Constants.NEW_CONVO && !modelsData) {
151152
const appTitle = localStorage.getItem(LocalStorageKeys.APP_TITLE) ?? '';
152153
if (appTitle) {
153154
document.title = appTitle;
154155
}
155-
navigate('/c/new');
156+
navigate(`/c/${Constants.NEW_CONVO}`);
156157
}
157158

158159
clearTimeout(timeoutIdRef.current);
@@ -189,12 +190,12 @@ const useNewConvo = (index = 0) => {
189190
isParamEndpoint(_template.endpoint ?? '', _template.endpointType ?? '') === true ||
190191
isParamEndpoint(_preset?.endpoint ?? '', _preset?.endpointType ?? '');
191192
const template =
192-
paramEndpoint === true && templateConvoId && templateConvoId === 'new'
193+
paramEndpoint === true && templateConvoId && templateConvoId === Constants.NEW_CONVO
193194
? { endpoint: _template.endpoint }
194195
: _template;
195196

196197
const conversation = {
197-
conversationId: 'new',
198+
conversationId: Constants.NEW_CONVO as string,
198199
title: 'New Chat',
199200
endpoint: null,
200201
...template,

client/src/utils/endpoints.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,23 @@ const firstLocalConvoKey = LocalStorageKeys.LAST_CONVO_SETUP + '_0';
8787
* update without updating last convo setup when same endpoint */
8888
export function updateLastSelectedModel({
8989
endpoint,
90-
model,
90+
model = '',
9191
}: {
9292
endpoint: string;
93-
model: string | undefined;
93+
model?: string;
9494
}) {
9595
if (!model) {
9696
return;
9797
}
98-
const lastConversationSetup = JSON.parse(localStorage.getItem(firstLocalConvoKey) || '{}');
98+
/* Note: an empty string value is possible */
99+
const lastConversationSetup = JSON.parse((localStorage.getItem(firstLocalConvoKey) ?? '{}') || '{}');
99100

100101
if (lastConversationSetup.endpoint === endpoint) {
101102
lastConversationSetup.model = model;
102103
localStorage.setItem(firstLocalConvoKey, JSON.stringify(lastConversationSetup));
103104
}
104105

105-
const lastSelectedModels = JSON.parse(localStorage.getItem(LocalStorageKeys.LAST_MODEL) || '{}');
106+
const lastSelectedModels = JSON.parse((localStorage.getItem(LocalStorageKeys.LAST_MODEL) ?? '{}') || '{}');
106107
lastSelectedModels[endpoint] = model;
107108
localStorage.setItem(LocalStorageKeys.LAST_MODEL, JSON.stringify(lastSelectedModels));
108109
}

0 commit comments

Comments
 (0)