import { Dialog, DialogPanel } from '@headlessui/react'; import { Loader2, Plus } from 'lucide-react'; import { useState } from 'react'; import { AnimatePresence, motion } from 'framer-motion'; import { ConfigModelProvider } from '@/lib/config/types'; import { toast } from 'sonner'; const AddModel = ({ providerId, setProviders, type, }: { providerId: string; setProviders: React.Dispatch>; type: 'chat' | 'embedding'; }) => { const [open, setOpen] = useState(false); const [modelName, setModelName] = useState(''); const [modelKey, setModelKey] = useState(''); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { const res = await fetch(`/api/providers/${providerId}/models`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ name: modelName, key: modelKey, type: type, }), }); if (!res.ok) { throw new Error('Failed to add model'); } setProviders((prev) => prev.map((provider) => { if (provider.id === providerId) { const newModel = { name: modelName, key: modelKey }; return { ...provider, chatModels: type === 'chat' ? [...provider.chatModels, newModel] : provider.chatModels, embeddingModels: type === 'embedding' ? [...provider.embeddingModels, newModel] : provider.embeddingModels, }; } return provider; }), ); toast.success('Model added successfully.'); setModelName(''); setModelKey(''); setOpen(false); } catch (error) { console.error('Error adding model:', error); toast.error('Failed to add model.'); } finally { setLoading(false); } }; return ( <> {open && ( setOpen(false)} className="relative z-[60]" >

Add new {type === 'chat' ? 'chat' : 'embedding'} model

setModelName(e.target.value)} className="w-full rounded-lg border border-light-200 dark:border-dark-200 bg-light-primary dark:bg-dark-primary px-4 py-3 text-sm text-black/80 dark:text-white/80 placeholder:text-black/40 dark:placeholder:text-white/40 focus-visible:outline-none focus-visible:border-light-300 dark:focus-visible:border-dark-300 transition-colors disabled:cursor-not-allowed disabled:opacity-60" placeholder="e.g., GPT-4" type="text" required />
setModelKey(e.target.value)} className="w-full rounded-lg border border-light-200 dark:border-dark-200 bg-light-primary dark:bg-dark-primary px-4 py-3 text-sm text-black/80 dark:text-white/80 placeholder:text-black/40 dark:placeholder:text-white/40 focus-visible:outline-none focus-visible:border-light-300 dark:focus-visible:border-dark-300 transition-colors disabled:cursor-not-allowed disabled:opacity-60" placeholder="e.g., gpt-4" type="text" required />
)}
); }; export default AddModel;