import axios from 'axios'; import { useEffect, useState } from 'react'; import Settings from './Settings'; import Examples from './Google/Examples.jsx'; import exportFromJSON from 'export-from-json'; import AgentSettings from './Plugins/AgentSettings.jsx'; import { useSetRecoilState, useRecoilValue } from 'recoil'; import filenamify from 'filenamify'; import { MessagesSquared, GPTIcon, Input, Label, Button, Dropdown, Dialog, DialogClose, DialogButton, DialogTemplate, } from '~/components/'; import { cn } from '~/utils/'; import cleanupPreset from '~/utils/cleanupPreset'; import { localize } from '~/localization/Translation'; import store from '~/store'; const EditPresetDialog = ({ open, onOpenChange, preset: _preset, title }) => { const lang = useRecoilValue(store.lang); const [preset, setPreset] = useState(_preset); const setPresets = useSetRecoilState(store.presets); const [showExamples, setShowExamples] = useState(false); const [showAgentSettings, setShowAgentSettings] = useState(false); const availableEndpoints = useRecoilValue(store.availableEndpoints); const endpointsConfig = useRecoilValue(store.endpointsConfig); const triggerExamples = () => setShowExamples((prev) => !prev); const triggerAgentSettings = () => setShowAgentSettings((prev) => !prev); const setOption = (param) => (newValue) => { let update = {}; update[param] = newValue; setPreset((prevState) => cleanupPreset({ preset: { ...prevState, ...update, }, endpointsConfig, }), ); }; const setAgentOption = (param) => (newValue) => { let editablePreset = JSON.stringify(_preset); editablePreset = JSON.parse(editablePreset); let { agentOptions } = editablePreset; agentOptions[param] = newValue; setPreset((prevState) => cleanupPreset({ preset: { ...prevState, agentOptions, }, endpointsConfig, }), ); }; const setExample = (i, type, newValue = null) => { let update = {}; let current = preset?.examples.slice() || []; let currentExample = { ...current[i] } || {}; currentExample[type] = { content: newValue }; current[i] = currentExample; update.examples = current; setPreset((prevState) => cleanupPreset({ preset: { ...prevState, ...update, }, endpointsConfig, }), ); }; const addExample = () => { let update = {}; let current = preset?.examples.slice() || []; current.push({ input: { content: '' }, output: { content: '' } }); update.examples = current; setPreset((prevState) => cleanupPreset({ preset: { ...prevState, ...update, }, endpointsConfig, }), ); }; const removeExample = () => { let update = {}; let current = preset?.examples.slice() || []; if (current.length <= 1) { update.examples = [{ input: { content: '' }, output: { content: '' } }]; setPreset((prevState) => cleanupPreset({ preset: { ...prevState, ...update, }, endpointsConfig, }), ); return; } current.pop(); update.examples = current; setPreset((prevState) => cleanupPreset({ preset: { ...prevState, ...update, }, endpointsConfig, }), ); }; const defaultTextProps = 'rounded-md border border-gray-200 focus:border-slate-400 focus:bg-gray-50 bg-transparent text-sm shadow-[0_0_10px_rgba(0,0,0,0.05)] outline-none placeholder:text-gray-400 focus:outline-none focus:ring-gray-400 focus:ring-opacity-20 focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 dark:border-gray-500 dark:bg-gray-700 focus:dark:bg-gray-600 dark:text-gray-50 dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] dark:focus:border-gray-400 dark:focus:outline-none dark:focus:ring-0 dark:focus:ring-gray-400 dark:focus:ring-offset-0'; const submitPreset = () => { axios({ method: 'post', url: '/api/presets', data: cleanupPreset({ preset, endpointsConfig }), withCredentials: true, }).then((res) => { setPresets(res?.data); }); }; const exportPreset = () => { const fileName = filenamify(preset?.title || 'preset'); exportFromJSON({ data: cleanupPreset({ preset, endpointsConfig }), fileName, exportType: exportFromJSON.types.json, }); }; useEffect(() => { setPreset(_preset); // eslint-disable-next-line react-hooks/exhaustive-deps }, [open]); const endpoint = preset?.endpoint; const isGoogle = endpoint === 'google'; const isGptPlugins = endpoint === 'gptPlugins'; const shouldShowSettings = (isGoogle && !showExamples) || (isGptPlugins && !showAgentSettings) || (!isGoogle && !isGptPlugins); return (
setOption('title')(e.target.value || '')} placeholder={localize(lang, 'com_endpoint_set_custom_name')} className={cn( defaultTextProps, 'flex h-10 max-h-10 w-full resize-none px-3 py-2 focus:outline-none focus:ring-0 focus:ring-opacity-0 focus:ring-offset-0', )} />
{preset?.endpoint === 'google' && ( )} {preset?.endpoint === 'gptPlugins' && ( )}
{shouldShowSettings && } {preset?.endpoint === 'google' && showExamples && !preset?.model?.startsWith('codechat-') && ( )} {preset?.endpoint === 'gptPlugins' && showAgentSettings && ( )}
} buttons={ <> {localize(lang, 'com_endpoint_save')} } leftButtons={ <> {localize(lang, 'com_endpoint_export')} } />
); }; export default EditPresetDialog;