diff --git a/models/CustomGpt.js b/models/CustomGpt.js index 8fce957af2..67385904a0 100644 --- a/models/CustomGpt.js +++ b/models/CustomGpt.js @@ -43,18 +43,6 @@ module.exports = { return { customGpt: 'Error getting customGpts' }; } }, - // updateCustomGpt: async ({ _id, ...update }) => { - // try { - // console.log('updateCustomGpt', _id, update); - // return await CustomGpt.findOneAndUpdate({ _id }, update, { - // new: true, - // upsert: true - // }).exec(); - // } catch (error) { - // console.log(error); - // return { message: 'Error updating customGpt' }; - // } - // }, updateCustomGpt: async ({ value, ...update }) => { try { console.log('updateCustomGpt', value, update); diff --git a/src/components/main/ModelDialog.jsx b/src/components/main/ModelDialog.jsx index 4626415e7d..26b16d60cf 100644 --- a/src/components/main/ModelDialog.jsx +++ b/src/components/main/ModelDialog.jsx @@ -2,7 +2,7 @@ import React, { useState, useRef } from 'react'; import TextareaAutosize from 'react-textarea-autosize'; import { useDispatch } from 'react-redux'; -import { setModel, setDisabled, setCustomGpt } from '~/store/submitSlice'; +import { setModel, setCustomGpt } from '~/store/submitSlice'; import manualSWR from '~/utils/fetchers'; import { Button } from '../ui/Button.tsx'; import { Input } from '../ui/Input.tsx'; @@ -35,7 +35,7 @@ export default function ModelDialog({ mutate }) { } dispatch(setCustomGpt({ chatGptLabel, promptPrefix })); dispatch(setModel('chatgptCustom')); - dispatch(setDisabled(false)); + // dispatch(setDisabled(false)); }; const saveHandler = (e) => { @@ -56,8 +56,8 @@ export default function ModelDialog({ mutate }) { setSaveText('Save'); }, 2500); - // dispatch(setCustomGpt({ chatGptLabel, promptPrefix })); - // dispatch(setModel('chatgptCustom')); + dispatch(setCustomGpt({ chatGptLabel, promptPrefix })); + dispatch(setModel('chatgptCustom')); // dispatch(setDisabled(false)); }; diff --git a/src/components/main/ModelMenu.jsx b/src/components/main/ModelMenu.jsx index a36cb56658..fe621b4f12 100644 --- a/src/components/main/ModelMenu.jsx +++ b/src/components/main/ModelMenu.jsx @@ -1,19 +1,17 @@ import React, { useEffect } from 'react'; import { useSelector, useDispatch } from 'react-redux'; -import { setModel, setDisabled } from '~/store/submitSlice'; +import { setModel, setDisabled, setCustomGpt } from '~/store/submitSlice'; import { setConversation } from '~/store/convoSlice'; import ModelDialog from './ModelDialog'; import MenuItems from './MenuItems'; -// import useDidMountEffect from '~/hooks/useDidMountEffect'; -// import { swr } from '~/utils/fetchers'; import manualSWR from '~/utils/fetchers'; -// import { setMessages } from '~/store/messageSlice'; import { setModels } from '~/store/modelSlice'; -// import ModelItem from './ModelItem'; import GPTIcon from '../svg/GPTIcon'; import BingIcon from '../svg/BingIcon'; import { Button } from '../ui/Button.tsx'; +const initial = new Set(['chatgpt', 'chatgptCustom', 'bingai', 'chatgptBrowser']); + import { DropdownMenu, DropdownMenuContent, @@ -29,6 +27,14 @@ export default function ModelMenu() { const dispatch = useDispatch(); const { model } = useSelector((state) => state.submit); const { models } = useSelector((state) => state.models); + const modelMap = new Map( + models + .slice(4) + .map((modelItem) => [ + modelItem.value, + { chatGptLabel: modelItem.chatGptLabel, promptPrefix: modelItem.promptPrefix } + ]) + ); const { trigger } = manualSWR('http://localhost:3050/customGpts', 'get', (res) => { console.log('models data (response)', res); if (models.length + res.length === models.length) { @@ -43,12 +49,10 @@ export default function ModelMenu() { dispatch(setModels(fetchedModels)); }); - // useDidMountEffect(() => mutate(), [chatGptLabel]); - useEffect(() => { - const lastSelectedModel = JSON.parse(localStorage.getItem('model')); - if (lastSelectedModel && lastSelectedModel !== 'chatgptCustom') { - dispatch(setModel(lastSelectedModel)); + const lastSelected = JSON.parse(localStorage.getItem('model')); + if (lastSelected && lastSelected !== 'chatgptCustom' && initial.has(lastSelected)) { + dispatch(setModel(lastSelected)); } const cachedModels = JSON.parse(localStorage.getItem('models')); @@ -75,13 +79,22 @@ export default function ModelMenu() { dispatch(setModel(value)); dispatch(setDisabled(false)); } + + if (!initial.has(value)) { + const chatGptLabel = modelMap.get(value)?.chatGptLabel; + const promptPrefix = modelMap.get(value)?.promptPrefix; + + dispatch(setCustomGpt({ chatGptLabel, promptPrefix })); + dispatch(setModel('chatgptCustom')); + } + // Set new conversation dispatch( setConversation({ title: 'New Chat', error: false, conversationId: null, - parentMessageId: null + parentMessageId: null, }) ); };