import { Button } from "@/components/ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { getErrorMessage, setProviderFormDirtyState, useAppDispatch } from "@/lib/store"; import { useUpdateProviderMutation } from "@/lib/store/apis/providersApi"; import { ModelProvider } from "@/lib/types/config"; import { proxyOnlyFormSchema, type ProxyOnlyFormSchema } from "@/lib/types/schemas"; import { cn } from "@/lib/utils"; import { RbacOperation, RbacResource, useRbac } from "@enterprise/lib"; import { zodResolver } from "@hookform/resolvers/zod"; import { useEffect } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { buildProviderUpdatePayload } from "../views/utils"; interface ProxyFormFragmentProps { provider: ModelProvider; } export function ProxyFormFragment({ provider }: ProxyFormFragmentProps) { const dispatch = useAppDispatch(); const hasUpdateProviderAccess = useRbac(RbacResource.ModelProvider, RbacOperation.Update); const [updateProvider, { isLoading: isUpdatingProvider }] = useUpdateProviderMutation(); const form = useForm({ resolver: zodResolver(proxyOnlyFormSchema), mode: "onChange", reValidateMode: "onChange", defaultValues: { proxy_config: { type: provider.proxy_config?.type, url: provider.proxy_config?.url || "", username: provider.proxy_config?.username || "", password: provider.proxy_config?.password || "", ca_cert_pem: provider.proxy_config?.ca_cert_pem || "", }, }, }); useEffect(() => { dispatch(setProviderFormDirtyState(form.formState.isDirty)); }, [form.formState.isDirty]); useEffect(() => { form.reset({ proxy_config: { type: provider.proxy_config?.type, url: provider.proxy_config?.url || "", username: provider.proxy_config?.username || "", password: provider.proxy_config?.password || "", ca_cert_pem: provider.proxy_config?.ca_cert_pem || "", }, }); }, [form, provider.name, provider.proxy_config]); const watchedProxyType = form.watch("proxy_config.type"); const onSubmit = (data: ProxyOnlyFormSchema) => { updateProvider( buildProviderUpdatePayload(provider, { proxy_config: { type: data.proxy_config?.type ?? "none", url: data.proxy_config?.url || undefined, username: data.proxy_config?.username || undefined, password: data.proxy_config?.password || undefined, ca_cert_pem: data.proxy_config?.ca_cert_pem || undefined, }, }), ) .unwrap() .then(() => { toast.success("Provider configuration updated successfully"); form.reset(data); }) .catch((err) => { toast.error("Failed to update provider configuration", { description: getErrorMessage(err), }); }); }; return (
{/* Proxy Configuration */}
( Proxy Type )} />
( Proxy URL )} />
( Username )} /> ( Password )} />
( CA Certificate (PEM) (Optional)