'use client'; import React, { useState, useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { ManualModelSchema, type ManualModel, type Model, } from '@/lib/api/schemas/models'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Edit3, Loader2 } from 'lucide-react'; import { toast } from 'sonner'; interface EditModelFormProps { model: Model; onModelUpdate: (modelId: string, updatedModel: ManualModel) => void; onCancel?: () => void; isOpen: boolean; } export function EditModelForm({ model, onModelUpdate, onCancel, isOpen, }: EditModelFormProps) { const [isSubmitting, setIsSubmitting] = useState(false); const form = useForm({ resolver: zodResolver(ManualModelSchema) as any, // eslint-disable-line @typescript-eslint/no-explicit-any defaultValues: { name: model.name, full_name: model.full_name, input_cost: model.input_cost, output_cost: model.output_cost, provider: model.provider, modelType: model.modelType as ManualModel['modelType'], description: model.description || '', contextLength: model.contextLength || 0, }, }); // Reset form when model changes useEffect(() => { form.reset({ name: model.name, full_name: model.full_name, input_cost: model.input_cost, output_cost: model.output_cost, provider: model.provider, modelType: model.modelType as ManualModel['modelType'], description: model.description || '', contextLength: model.contextLength || 0, }); }, [model, form]); const onSubmit = async (data: ManualModel) => { setIsSubmitting(true); try { // Ensure we're sending the correct API key value const updatedData = { ...data, }; await onModelUpdate(model.id, updatedData); toast.success('Model updated successfully!'); onCancel?.(); } catch (error) { toast.error('Failed to update model. Please try again.'); console.error('Error updating model:', error); } finally { setIsSubmitting(false); } }; const handleClose = () => { if (!isSubmitting) { onCancel?.(); } }; return ( Edit Model Update the details for "{model.name}"
( Original Model Name Original name from the provider (cannot be changed) )} /> ( Display Name * Custom display name for the model )} /> ( Provider * AI model provider or company )} />
( Input Cost (per 1M tokens) * { const value = parseFloat(e.target.value) || 0; const rounded = Math.round(value * 1000) / 1000; field.onChange(rounded); }} className='w-full' /> Cost in USD per 1,000,000 input tokens (max 3 decimals) )} /> ( Output Cost (per 1M tokens) * { const value = parseFloat(e.target.value) || 0; const rounded = Math.round(value * 1000) / 1000; field.onChange(rounded); }} className='w-full' /> Cost in USD per 1,000,000 output tokens (max 3 decimals) )} />
( Description