import React from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod/v4"; import { Button } from "@/src/components/ui/button"; import { DialogBody, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/src/components/ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/src/components/ui/form"; import { CodeMirrorEditor } from "@/src/components/editor/CodeMirrorEditor"; import { api } from "@/src/utils/api"; import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess"; import { showSuccessToast } from "@/src/features/notifications/showSuccessToast"; import { showErrorToast } from "@/src/features/notifications/showErrorToast"; import { Loader2 } from "lucide-react"; import { getFormattedPayload } from "@/src/features/experiments/utils/format"; import { type Prisma } from "@langfuse/shared"; const RemoteExperimentTriggerSchema = z.object({ payload: z.string(), }); type RemoteExperimentTriggerForm = z.infer< typeof RemoteExperimentTriggerSchema >; export const RemoteExperimentTriggerModal = ({ projectId, datasetId, remoteExperimentConfig, setShowTriggerModal, }: { projectId: string; datasetId: string; remoteExperimentConfig: { url: string; payload?: Prisma.JsonValue; }; setShowTriggerModal: (show: boolean) => void; }) => { const hasDatasetAccess = useHasProjectAccess({ projectId, scope: "datasets:CUD", }); const dataset = api.datasets.byId.useQuery({ projectId, datasetId, }); const form = useForm({ resolver: zodResolver(RemoteExperimentTriggerSchema), defaultValues: { payload: getFormattedPayload(remoteExperimentConfig.payload), }, }); const runRemoteExperimentMutation = api.datasets.triggerRemoteExperiment.useMutation({ onSuccess: (data) => { if (data.success) { showSuccessToast({ title: "Dataset run started", description: "Your dataset run may take a few minutes to complete.", }); } else { showErrorToast( "Failed to start dataset run", "Please try again or check your remote dataset run configuration.", ); } setShowTriggerModal(false); }, onError: (error) => { showErrorToast( error.message || "Failed to start dataset run", "Please try again or check your remote dataset run configuration.", ); }, }); const onSubmit = (data: RemoteExperimentTriggerForm) => { if (data.payload.trim()) { try { JSON.parse(data.payload); } catch { form.setError("payload", { message: "Invalid JSON format", }); return; } } runRemoteExperimentMutation.mutate({ projectId, datasetId, payload: data.payload, }); }; if (!hasDatasetAccess) { return null; } return ( <> Run remote dataset run This action will send the following information to{" "} {remoteExperimentConfig.url}.
( Config Confirm the config you want to send to the remote dataset run URL along with the{" "} {dataset.data?.name} dataset information. )} />
); };