Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(datasets): unify item editors and preserve batch selection (#17747)

Nikita Kabardin9bb0ccb966be
diff --git a/web/src/components/onboarding/DatasetItemsOnboarding.tsx b/web/src/components/onboarding/DatasetItemsOnboarding.tsx
index e2dcd47f7c..798a05da74 100644
--- a/web/src/components/onboarding/DatasetItemsOnboarding.tsx
+++ b/web/src/components/onboarding/DatasetItemsOnboarding.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { useRef, useState } from "react";
import { SplashScreen } from "@/src/components/ui/splash-screen";
import { Braces, Code, ListTree, Upload } from "lucide-react";
import Link from "next/link";
@@ -78,6 +78,7 @@ export const DatasetItemsOnboarding = ({
const capture = usePostHogClientCapture();
const [isUploadDialogOpen, setIsUploadDialogOpen] = useState(false);
const [isNewItemDialogOpen, setIsNewItemDialogOpen] = useState(false);
+ const submissionPending = useRef(false);
const hasProjectAccess = useHasProjectAccess({
projectId,
@@ -113,7 +114,9 @@ export const DatasetItemsOnboarding = ({
<Dialog
open={hasProjectAccess && isNewItemDialogOpen}
- onOpenChange={setIsNewItemDialogOpen}
+ onOpenChange={(open) => {
+ if (!submissionPending.current) setIsNewItemDialogOpen(open);
+ }}
>
<DialogTrigger asChild disabled={!hasProjectAccess}>
<DatasetItemEntryPointRow
@@ -135,6 +138,9 @@ export const DatasetItemsOnboarding = ({
<NewDatasetItemForm
projectId={projectId}
datasetId={datasetId}
+ onPendingChange={(pending) => {
+ submissionPending.current = pending;
+ }}
onFormSuccess={() => setIsNewItemDialogOpen(false)}
className="h-full overflow-y-auto"
/>
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetChoiceStep.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetChoiceStep.tsx
deleted file mode 100644
index d6c1b18a89..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetChoiceStep.tsx
+++ /dev/null
@@ -1,40 +0,0 @@
-import { Database, Plus } from "lucide-react";
-import type { DatasetChoiceStepProps } from "./types";
-
-export function DatasetChoiceStep(props: DatasetChoiceStepProps) {
- const { onSelectMode } = props;
-
- return (
- <div className="grid grid-cols-2 gap-6 p-6">
- {/* Existing Dataset Card */}
- <button
- type="button"
- onClick={() => onSelectMode("select")}
- className="hover:border-tertiary hover:bg-accent flex flex-col items-center rounded-lg border-2 p-8 text-center transition-all"
- >
- <div className="bg-primary/10 mb-4 rounded-full p-4">
- <Database className="text-primary h-8 w-8" />
- </div>
- <h3 className="mb-2 text-lg font-bold">Existing Dataset</h3>
- <p className="text-muted-foreground text-sm">
- Add to a dataset that already exists
- </p>
- </button>
-
- {/* New Dataset Card */}
- <button
- type="button"
- onClick={() => onSelectMode("create")}
- className="hover:border-tertiary hover:bg-accent flex flex-col items-center rounded-lg border-2 p-8 text-center transition-all"
- >
- <div className="bg-primary/10 mb-4 rounded-full p-4">
- <Plus className="text-primary h-8 w-8" />
- </div>
- <h3 className="mb-2 text-lg font-bold">New Dataset</h3>
- <p className="text-muted-foreground text-sm">
- Create a new dataset for these observations
- </p>
- </button>
- </div>
- );
-}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetCreateStep.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetCreateStep.tsx
index b1777d0b0f..ec47a63091 100644
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetCreateStep.tsx
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetCreateStep.tsx
@@ -1,27 +1,22 @@
import { DatasetForm } from "@/src/features/datasets";
-import type { DatasetCreateStepProps } from "./types";
+import type { DatasetInfo } from "./types";
-export function DatasetCreateStep(props: DatasetCreateStepProps) {
- const { projectId, formRef, onDatasetCreated, onValidationChange } = props;
+export function DatasetCreateStep(props: {
+ projectId: string;
+ onDatasetCreated: (dataset: DatasetInfo) => void;
+ onSubmittingChange: (pending: boolean) => void;
+ onCancel: () => void;
+}) {
+ const { projectId, onDatasetCreated, onSubmittingChange, onCancel } = props;
return (
- <div className="space-y-6 p-6">
- <div>
- <h3 className="text-lg font-bold">Create New Dataset</h3>
- <p className="text-muted-foreground text-sm">
- Fill in the details to create a new dataset
- </p>
- </div>
-
- <DatasetForm
- ref={formRef}
- projectId={projectId}
- mode="create"
- redirectOnSuccess={false}
- showFooter={false}
- onCreateDatasetSuccess={onDatasetCreated}
- onValidationChange={onValidationChange}
- />
- </div>
+ <DatasetForm
+ projectId={projectId}
+ mode="create"
+ redirectOnSuccess={false}
+ onCreateDatasetSuccess={onDatasetCreated}
+ onSubmittingChange={onSubmittingChange}
+ onCancel={onCancel}
+ />
);
}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetMappingEditor.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetMappingEditor.tsx
new file mode 100644
index 0000000000..9a10ab539e
--- /dev/null
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetMappingEditor.tsx
@@ -0,0 +1,183 @@
+import { useStore } from "zustand";
+import { Plus } from "lucide-react";
+import { Button } from "@/src/components/ui/button";
+import { Combobox } from "@/src/components/ui/combobox";
+import { Skeleton } from "@/src/components/ui/skeleton";
+import { DialogBody, DialogFooter } from "@/src/components/ui/dialog";
+import {
+ DatasetItemEditorLayout,
+ DatasetItemPreviewField,
+} from "@/src/features/datasets/components/DatasetItemEditorLayout";
+import { MappingStep } from "./MappingStep";
+import type { DatasetMappingStore } from "./datasetMappingStore";
+import { prepareDatasetMapping } from "./prepareDatasetMapping";
+import type { DatasetInfo, ObservationPreviewData } from "./types";
+
+export function DatasetMappingEditor({
+ store,
+ datasets,
+ observation,
+ loading,
+ unavailable,
+ onRetry,
+ count,
+ onClose,
+ onSubmit,
+}: {
+ store: DatasetMappingStore;
+ datasets: DatasetInfo[];
+ observation: ObservationPreviewData | null;
+ loading: boolean;
+ unavailable: boolean;
+ onRetry: () => void;
+ count: number;
+ onClose: () => void;
+ onSubmit: (dataset: DatasetInfo | null) => Promise<void>;
+}) {
+ const datasetId = useStore(store, (state) => state.datasetId);
+ const createdDataset = useStore(store, (state) => state.createdDataset);
+ const mapping = useStore(store, (state) => state.mapping);
+ const pending = useStore(
+ store,
+ (state) => state.submission.status === "pending",
+ );
+ const actions = store.getState().actions;
+ const options =
+ createdDataset &&
+ !datasets.some((dataset) => dataset.id === createdDataset.id)
+ ? [...datasets, createdDataset]
+ : datasets;
+ const dataset = options.find((option) => option.id === datasetId) ?? null;
+ const fields = prepareDatasetMapping(mapping, observation, dataset);
+ const valid = fields.every((field) => field.errors.length === 0);
+ return (
+ <>
+ <DialogBody className="min-h-0 overflow-hidden p-0">
+ <DatasetItemEditorLayout
+ selector={
+ <div className="flex items-end gap-3">
+ <div className="flex min-w-0 flex-1 flex-col gap-2">
+ <span className="text-sm font-bold">Target dataset</span>
+ <Combobox
+ name="Target dataset"
+ options={options.map((option) => ({
+ value: option.id,
+ label: option.name,
+ }))}
+ value={datasetId ?? undefined}
+ onValueChange={(id) => {
+ const selected = options.find((option) => option.id === id);
+ if (selected) actions.selectDataset(selected);
+ }}
+ placeholder="Select dataset"
+ searchPlaceholder="Search datasets..."
+ emptyText="No datasets found."
+ disabled={pending}
+ />
+ </div>
+ <Button
+ variant="outline"
+ onClick={() => actions.setScreen("create")}
+ disabled={pending}
+ >
+ <Plus className="size-4" />
+ Create dataset
+ </Button>
+ </div>
+ }
+ previewDescription={`Sample from the first selected observation. The same mappings apply to all ${count} observations.`}
+ preview={
+ loading ? (
+ <Skeleton className="h-48 w-full" />
+ ) : (
+ <DatasetMappingPreview
+ fields={fields}
+ unavailable={unavailable}
+ onRetry={onRetry}
+ />
+ )
+ }
+ >
+ <fieldset
+ disabled={pending}
+ inert={pending}
+ className="flex min-w-0 flex-col gap-6"
+ >
+ {fields.map((field) => (
+ <MappingStep
+ key={field.key}
+ field={field}
+ config={mapping[field.key]}
+ onConfigChange={(config) =>
+ actions.changeMapping(field.key, config)
+ }
+ observationData={observation}
+ />
+ ))}
+ </fieldset>
+ </DatasetItemEditorLayout>
+ </DialogBody>
+ <DialogFooter>
+ <Button variant="outline" disabled={pending} onClick={onClose}>
+ Cancel
+ </Button>
+ <Button
+ disabled={!dataset || !valid || loading || unavailable || count === 0}
+ loading={pending}
+ onClick={() => onSubmit(dataset)}
+ >
+ Add {count} observation{count === 1 ? "" : "s"}
+ </Button>
+ </DialogFooter>
+ </>
+ );
+}
+
+function DatasetMappingPreview({
+ fields,
+ unavailable,
+ onRetry,
+}: {
+ fields: ReturnType<typeof prepareDatasetMapping>;
+ unavailable: boolean;
+ onRetry: () => void;
+}) {
+ if (unavailable)
+ return (
+ <div className="flex flex-col items-start gap-2">
+ <p className="text-muted-foreground text-sm">
+ Preview could not be loaded.
+ </p>
+ <Button variant="outline" onClick={onRetry}>
+ Try again
+ </Button>
+ </div>
+ );
+ return (
+ <>
+ {fields.map((field) => (
+ <DatasetItemPreviewField
+ key={field.key}
+ label={field.label}
+ value={field.value}
+ feedback={
+ (field.errors.length > 0 || field.warnings.length > 0) && (
+ <div className="flex flex-col gap-1 border-t px-3 py-2 text-xs">
+ {field.errors.map((error, index) => (
+ <p key={index} className="text-destructive" role="alert">
+ {error}
+ </p>
+ ))}
+ {field.warnings.map((warning, index) => (
+ <p key={index} className="text-muted-foreground">
+ {warning}
+ </p>
+ ))}
+ </div>
+ )
+ }
+ />
+ ))}
+ </>
+ );
+}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetSelectStep.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetSelectStep.tsx
deleted file mode 100644
index 3e7f83e855..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/DatasetSelectStep.tsx
+++ /dev/null
@@ -1,85 +0,0 @@
-import { useState } from "react";
-import { Check, ChevronsUpDown } from "lucide-react";
-import { Button } from "@/src/components/ui/button";
-import {
- Popover,
- PopoverContent,
- PopoverTrigger,
-} from "@/src/components/ui/popover";
-import {
- Command,
- CommandEmpty,
- CommandGroup,
- CommandInput,
- CommandItem,
-} from "@/src/components/ui/command";
-import { cn } from "@/src/utils/tailwind";
-import { api } from "@/src/utils/api";
-import type { DatasetSelectStepProps } from "./types";
-
-export function DatasetSelectStep({
- projectId,
- dataset,
- onDatasetSelect,
-}: DatasetSelectStepProps) {
- const [open, setOpen] = useState(false);
-
- // Fetch all datasets
- const datasets = api.datasets.allDatasetMeta.useQuery({
- projectId,
- });
-
- return (
- <div className="space-y-6 p-6">
- <div>
- <h3 className="text-lg font-bold">Select Dataset</h3>
- <p className="text-muted-foreground text-sm">
- Choose an existing dataset to add your observations to
- </p>
- </div>
-
- <Popover open={open} onOpenChange={setOpen}>
- <PopoverTrigger asChild>
- <Button
- variant="outline"
- role="combobox"
- aria-expanded={open}
- className="w-full justify-between py-6 text-base"
- >
- {dataset.name || "Select dataset..."}
- <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
- </Button>
- </PopoverTrigger>
- <PopoverContent className="w-[500px] p-0" align="start">
- <Command>
- <CommandInput placeholder="Search datasets..." />
- <CommandEmpty>No dataset found.</CommandEmpty>
- <CommandGroup className="max-h-[300px] overflow-auto">
- {datasets.data?.map((d) => (
- <CommandItem
- key={d.id}
- value={d.name}
- onSelect={() => {
- onDatasetSelect(d);
- setOpen(false);
- }}
- className="flex items-center justify-between"
- >
- <div className="flex items-center">
- <Check
- className={cn(
- "mr-2 h-4 w-4",
- dataset.id === d.id ? "opacity-100" : "opacity-0",
- )}
- />
- <span>{d.name}</span>
- </div>
- </CommandItem>
- ))}
- </CommandGroup>
- </Command>
- </PopoverContent>
- </Popover>
- </div>
- );
-}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/FinalPreviewStep.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/FinalPreviewStep.tsx
deleted file mode 100644
index 2cb7075313..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/FinalPreviewStep.tsx
+++ /dev/null
@@ -1,241 +0,0 @@
-/* eslint-disable no-nested-ternary */
-/* eslint-disable @repo/no-exotic-operators */
-import { useMemo } from "react";
-import { Button } from "@/src/components/ui/button";
-import { Pencil } from "lucide-react";
-import { JSONView } from "@/src/components/ui/CodeJsonViewer";
-import { cn } from "@/src/utils/tailwind";
-import type { FinalPreviewStepProps, DialogStep } from "./types";
-import { applyFullMapping, type MappingError } from "@langfuse/shared";
-import {
- IssueBanner,
- issueCardVariants,
- issueChromeVariants,
- issueIcons,
- issueTextVariants,
- type IssueVariant,
-} from "@/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/IssueBanner";
-
-const STEP_FOR_FIELD: Record<string, DialogStep> = {
- input: "input-mapping",
- expectedOutput: "output-mapping",
- metadata: "metadata-mapping",
-};
-
-const fieldLabel = (field: string) =>
- field === "expectedOutput" ? "expected output" : field;
-
-export function FinalPreviewStep({
- dataset,
- mapping,
- observationData,
- totalCount,
- onEditStep,
-}: FinalPreviewStepProps) {
- const previewResult = useMemo(() => {
- if (!observationData) return null;
-
- return applyFullMapping({
- observation: {
- input: observationData.input,
- output: observationData.output,
- metadata: observationData.metadata,
- },
- mapping,
- });
- }, [observationData, mapping]);
-
- const { errorsByField, missesByField, errorFields, missFields } =
- useMemo(() => {
- const errorsByField: Record<string, MappingError[]> = {};
- const missesByField: Record<string, MappingError[]> = {};
- for (const err of previewResult?.errors ?? []) {
- const bucket =
- err.type === "json_path_error" ? errorsByField : missesByField;
- (bucket[err.targetField] ??= []).push(err);
- }
- return {
- errorsByField,
- missesByField,
- errorFields: Object.keys(errorsByField),
- missFields: Object.keys(missesByField),
- };
- }, [previewResult?.errors]);
-
- return (
- <div className="h-[62vh] space-y-6 p-6">
- <div>
- <h3 className="text-lg font-bold">Review Configuration</h3>
- <p className="text-muted-foreground text-sm">
- Adding {totalCount} observation{totalCount !== 1 ? "s" : ""} to
- dataset &quot;
- {dataset.name}&quot;
- </p>
- </div>
-
- {errorFields.length > 0 && (
- <IssueBanner
- variant="error"
- title="Some JSONPaths are invalid"
- description="Items using these mappings will be skipped during processing."
- >
- <EditMappingActions
- variant="error"
- fields={errorFields}
- onEditStep={onEditStep}
- />
- </IssueBanner>
- )}
-
- {missFields.length > 0 && (
- <IssueBanner
- variant="warning"
- title="Some JSONPaths did not match the preview observation"
- description="Observations with failed mappings will be skipped during processing."
- >
- <EditMappingActions
- variant="warning"
- fields={missFields}
- onEditStep={onEditStep}
- />
- </IssueBanner>
- )}
-
- <div className="text-muted-foreground text-sm">
- Sample dataset item preview (from first selected observation):
- </div>
-
- {!observationData ? (
- <div className="bg-muted/30 flex h-64 items-center justify-center rounded-md border p-4">
- <p className="text-muted-foreground text-sm">
- No observation data available for preview
- </p>
- </div>
- ) : (
- <div className="space-y-4">
- <PreviewCard
- label="Input"
- data={previewResult?.input}
- onEdit={() => onEditStep("input-mapping")}
- pathErrors={errorsByField["input"]}
- pathMisses={missesByField["input"]}
- />
- <PreviewCard
- label="Expected Output"
- data={previewResult?.expectedOutput}
- onEdit={() => onEditStep("output-mapping")}
- pathErrors={errorsByField["expectedOutput"]}
- pathMisses={missesByField["expectedOutput"]}
- />
- <PreviewCard
- label="Metadata"
- data={previewResult?.metadata}
- onEdit={() => onEditStep("metadata-mapping")}
- pathErrors={errorsByField["metadata"]}
- pathMisses={missesByField["metadata"]}
- />
- </div>
- )}
- </div>
- );
-}
-
-function EditMappingActions({
- variant,
- fields,
- onEditStep,
-}: {
- variant: IssueVariant;
- fields: string[];
- onEditStep: (step: DialogStep) => void;
-}) {
- return (
- <div className="flex flex-wrap gap-2 pt-1">
- {fields.map((field) => (
- <Button
- key={field}
- variant="link"
- size="sm"
- className={cn(
- "h-auto p-0 text-xs underline",
- issueTextVariants({ variant }),
- )}
- onClick={() => {
- const step = STEP_FOR_FIELD[field];
- if (step) onEditStep(step);
- }}
- >
- Edit {fieldLabel(field)} mapping
- </Button>
- ))}
- </div>
- );
-}
-
-type PreviewCardProps = {
- label: string;
- data: unknown;
- onEdit: () => void;
- pathErrors?: MappingError[];
- pathMisses?: MappingError[];
-};
-
-function PreviewCard({
- label,
- data,
- onEdit,
- pathErrors = [],
- pathMisses = [],
-}: PreviewCardProps) {
- const variant: IssueVariant | null =
- pathErrors.length > 0 ? "error" : pathMisses.length > 0 ? "warning" : null;
- const Icon = variant ? issueIcons[variant] : null;
-
- return (
- <div className={issueCardVariants({ variant: variant ?? "none" })}>
- <div className="bg-muted/30 flex items-center justify-between border-b px-4 py-2">
- <span className="flex items-center gap-1.5 text-sm font-bold">
- {Icon && variant && (
- <Icon
- className={cn("h-3.5 w-3.5", issueTextVariants({ variant }))}
- />
- )}
- {label}
- </span>
- <Button
- variant="ghost"
- size="sm"
- onClick={onEdit}
- className="h-7 gap-1 text-xs"
- >
- <Pencil className="h-3 w-3" />
- Edit
- </Button>
- </div>
- <div className="max-h-62 overflow-auto">
- {data === null ? (
- <div className="text-muted-foreground p-4 text-sm italic">null</div>
- ) : (
- <JSONView json={data} className="text-xs" />
- )}
- </div>
- {variant && (
- <div
- className={cn("border-t px-4 py-2", issueChromeVariants({ variant }))}
- >
- <p className="text-xs">
- {[
- pathErrors.length > 0 &&
- `${pathErrors.length} path${pathErrors.length !== 1 ? "s have" : " has"} invalid syntax`,
- pathMisses.length > 0 &&
- `${pathMisses.length} path${pathMisses.length !== 1 ? "s" : ""} did not match in preview observation`,
- ]
- .filter(Boolean)
- .join("; ")}
- . These items will be skipped during processing.
- </p>
- </div>
- )}
- </div>
- );
-}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/MappingStep.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/MappingStep.tsx
index cb2388cb32..479363839a 100644
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/MappingStep.tsx
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/MappingStep.tsx
@@ -1,170 +1,68 @@
-import { useEffect, useRef } from "react";
-import { MappingModeSelector } from "./components/MappingModeSelector";
import { CustomMappingEditor } from "./components/CustomMappingEditor";
-import { MappingPreviewPanel } from "./components/MappingPreviewPanel";
+import { SelectInput } from "@/src/components/design-system/SelectInput/SelectInput";
import { DatasetSchemaHoverCard } from "@/src/features/datasets";
-import {
- extractSchemaFields,
- isObjectSchema,
- generateEntriesFromSchema,
-} from "./utils/extractSchemaFields";
import type {
- MappingStepProps,
+ FieldMappingConfig,
MappingMode,
- CustomMappingConfig,
+ ObservationPreviewData,
+ PreparedMappingField,
} from "./types";
export function MappingStep({
field,
- fieldLabel,
- defaultSourceField,
config,
onConfigChange,
observationData,
- isLoading,
- schema,
- onValidationChange,
-}: MappingStepProps) {
- const hasSchema = schema !== null && schema !== undefined;
- const isObjectType = hasSchema && isObjectSchema(schema);
- const hasInitializedRef = useRef(false);
-
- // Auto-initialize to custom key-value mode with schema-derived entries when:
- // 1. Schema exists and is an object type
- // 2. Config hasn't been manually set yet (mode is "full" which is the default)
- // 3. We haven't already initialized
- useEffect(() => {
- if (
- isObjectType &&
- config.mode === "full" &&
- !hasInitializedRef.current &&
- !config.custom?.keyValueMapConfig // Don't override if already has key-value config
- ) {
- hasInitializedRef.current = true;
-
- const schemaFields = extractSchemaFields(schema);
- if (schemaFields.length > 0) {
- const entries = generateEntriesFromSchema(
- schemaFields,
- defaultSourceField,
- );
-
- onConfigChange({
- mode: "custom",
- custom: {
- type: "keyValueMap",
- keyValueMapConfig: {
- entries,
- },
- },
- });
- }
- }
- }, [
- isObjectType,
- schema,
- config.mode,
- config.custom,
- defaultSourceField,
- onConfigChange,
- ]);
-
- // Get the full label for the "Full" option
- const getFullLabel = () => {
- switch (field) {
- case "input":
- return "Full observation input";
- case "expectedOutput":
- return "Full observation output";
- case "metadata":
- return "Full observation metadata";
- default:
- return `Full observation ${field}`;
- }
- };
-
- const handleModeChange = (mode: MappingMode) => {
- if (mode === "custom") {
- // Initialize custom config if switching to custom
- onConfigChange({
- mode: "custom",
- custom: config.custom ?? {
- type: "root",
- rootConfig: {
- sourceField: defaultSourceField,
- jsonPath: "$",
- },
- },
- });
- } else {
- onConfigChange({
- mode,
- custom: config.custom, // Preserve custom config in case they switch back
- });
- }
- };
-
- const handleCustomConfigChange = (customConfig: CustomMappingConfig) => {
+}: {
+ field: PreparedMappingField;
+ config: FieldMappingConfig;
+ onConfigChange: (config: FieldMappingConfig) => void;
+ observationData: ObservationPreviewData | null;
+}) {
+ function changeMode(mode: MappingMode) {
onConfigChange({
- ...config,
- custom: customConfig,
+ mode,
+ custom: config.custom ?? {
+ type: "root",
+ rootConfig: { sourceField: field.sourceField, jsonPath: "$" },
+ },
});
- };
+ }
return (
- <div className="grid h-[62vh] grid-cols-[minmax(0,1fr)_minmax(0,1fr)] gap-6 overflow-auto p-6">
- {/* Left: Configuration */}
- <div className="min-w-0 space-y-6">
- <div>
- <div className="flex items-center gap-2">
- <h3 className="grow text-lg font-bold">
- Dataset Item {fieldLabel}
- </h3>
- {hasSchema && (
- <DatasetSchemaHoverCard
- schemaType={
- field === "expectedOutput" ? "expectedOutput" : "input"
- }
- schema={schema}
- showLabel
- />
- )}
- </div>
- <p className="text-muted-foreground text-sm">
- Configure how observation data maps to the Dataset Item {fieldLabel}
- .
- </p>
- </div>
-
- <MappingModeSelector
- value={config.mode}
- onChange={handleModeChange}
- fullLabel={getFullLabel()}
- fieldName={field}
- />
-
- {config.mode === "custom" && config.custom && (
- <CustomMappingEditor
- config={config.custom}
- onChange={handleCustomConfigChange}
- defaultSourceField={defaultSourceField}
- observationData={observationData}
+ <section className="flex flex-col gap-3">
+ <div className="flex items-center justify-between gap-2">
+ <h3 className="text-sm font-bold">{field.label}</h3>
+ {field.schema != null && field.key !== "metadata" && (
+ <DatasetSchemaHoverCard
+ schemaType={field.key}
+ schema={field.schema}
+ showLabel
/>
)}
</div>
-
- {/* Right: Preview */}
- <div className="min-w-0 border-l pl-6">
- <MappingPreviewPanel
- fieldLabel={fieldLabel}
- defaultSourceField={defaultSourceField}
- config={config}
+ <SelectInput
+ aria-label={`${field.label} mapping`}
+ value={config.mode}
+ onValueChange={changeMode}
+ placeholder="Choose source"
+ options={[
+ { value: "full", label: `Use observation ${field.sourceField}` },
+ { value: "custom", label: "Custom mapping" },
+ ...(field.key === "input"
+ ? []
+ : [{ value: "none" as const, label: "Leave empty" }]),
+ ]}
+ error={field.errors.length > 0}
+ />
+ {config.mode === "custom" && config.custom && (
+ <CustomMappingEditor
+ config={config.custom}
+ onChange={(custom) => onConfigChange({ ...config, custom })}
+ defaultSourceField={field.sourceField}
observationData={observationData}
- isLoading={isLoading}
- schema={schema}
- onValidationChange={onValidationChange}
/>
- </div>
- </div>
+ )}
+ </section>
);
}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/README.md b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/README.md
new file mode 100644
index 0000000000..1b5c5c3e56
--- /dev/null
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/README.md
@@ -0,0 +1,28 @@
+# Dataset creation from observations
+
+- `index.tsx` owns the modal and its immutable opening source selection. It loads
+ dataset metadata and the appropriate v3/v4 preview; table selection is cleared
+ only after a batch is accepted, without changing the progress view's source.
+- `datasetMappingStore.ts` owns the selected dataset ID, mapping drafts, create
+ screen and submission state. Schema defaults are prepared when a dataset is
+ selected or created, never when a field mounts or metadata refetches.
+- `DatasetMappingEditor.tsx` subscribes to mapping drafts and renders the mapping
+ editor shell, keeping field edits out of the modal's query/lifecycle owner.
+- `DatasetCreateStep.tsx` adapts the shared dataset form. The form owns its
+ submission and footer; event callbacks guard modal dismissal and return the
+ newly created dataset to the mapping store without a validation-state bridge.
+- `prepareDatasetMapping.ts` produces values, JSONPath warnings and validation for
+ all fields. The editor, preview and submission action use this same result;
+ there are no child-to-parent validation effects.
+- `submitDatasetBatch.ts` guards duplicate or invalid submissions and calls the
+ existing batch API. Failure retains the draft and selection; the tRPC mutation
+ reports the error. The worker remains responsible for all selected items.
+- `DatasetItemEditorLayout` in the datasets feature contains the bulk selector,
+ mapping editor and sample preview, followed by background progress after
+ submission. Single-object creation uses `NewDatasetItemForm` with one column
+ of editable JSON, multiple datasets, schema checks and media uploads, then
+ calls the existing immediate creation API. Both flows reuse dataset creation;
+ their item editors and submission adapters stay separate.
+
+The existing single-item submit analytics and evaluation analytics remain at
+their intent seams. Layout changes and cancellation do not add analytics events.
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/IssueBanner.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/IssueBanner.tsx
deleted file mode 100644
index 24142b588a..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/IssueBanner.tsx
+++ /dev/null
@@ -1,106 +0,0 @@
-import { type ReactNode } from "react";
-import { AlertCircle, AlertTriangle, type LucideIcon } from "lucide-react";
-import { cva } from "class-variance-authority";
-import { cn } from "@/src/utils/tailwind";
-
-export type IssueVariant = "error" | "warning";
-
-/**
- * Colored chrome — border + bg + text — applied to elements that carry a full
- * issue notice (banner, list root, card footer strip). Each consumer composes
- * this with its own layout classes.
- */
-export const issueChromeVariants = cva("", {
- variants: {
- variant: {
- error: "border-destructive/50 bg-destructive/10 text-destructive",
- warning:
- "border-amber-500/50 bg-amber-50 text-amber-600 dark:bg-amber-950/30 dark:text-amber-500",
- },
- },
-});
-
-/** Outer card border that picks up a variant accent only when there is an issue. */
-export const issueCardVariants = cva("rounded-lg border", {
- variants: {
- variant: {
- none: "",
- error: "border-destructive/50",
- warning: "border-amber-500/50",
- },
- },
- defaultVariants: { variant: "none" },
-});
-
-/**
- * Text color for inner elements that set their own color and therefore do not
- * inherit from a variant-styled ancestor (e.g. Button's `variant="link"` that
- * forces `text-primary`).
- */
-export const issueTextVariants = cva("", {
- variants: {
- variant: {
- error: "text-destructive",
- warning: "text-amber-600 dark:text-amber-500",
- },
- },
-});
-
-export const issueIcons: Record<IssueVariant, LucideIcon> = {
- error: AlertCircle,
- warning: AlertTriangle,
-};
-
-export function IssueBanner({
- variant,
- title,
- description,
- children,
-}: {
- variant: IssueVariant;
- title: string;
- description?: string;
- children?: ReactNode;
-}) {
- const Icon = issueIcons[variant];
- return (
- <div
- className={cn("rounded-md border p-3", issueChromeVariants({ variant }))}
- >
- <div className="flex items-start gap-2">
- <Icon className="mt-0.5 h-4 w-4 shrink-0" />
- <div className="space-y-1">
- <p className="text-sm font-bold">{title}</p>
- {description && <p className="text-xs opacity-80">{description}</p>}
- {children}
- </div>
- </div>
- </div>
- );
-}
-
-export function IssueList({
- variant,
- title,
- children,
-}: {
- variant: IssueVariant;
- title: string;
- children: ReactNode;
-}) {
- return (
- <div
- className={cn(
- "max-h-[5vh] overflow-y-auto rounded-md border p-2",
- issueChromeVariants({ variant }),
- )}
- >
- <p className="mb-1 text-xs font-bold">{title}</p>
- <ul className="space-y-0.5">{children}</ul>
- </div>
- );
-}
-
-export function IssueItem({ children }: { children: ReactNode }) {
- return <li className="text-xs">{children}</li>;
-}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingModeSelector.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingModeSelector.tsx
deleted file mode 100644
index 2d9f4e8103..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingModeSelector.tsx
+++ /dev/null
@@ -1,55 +0,0 @@
-import { Label } from "@/src/components/ui/label";
-import { RadioGroup } from "@/src/components/design-system/RadioGroup/RadioGroup";
-import type { MappingMode } from "../types";
-
-type MappingModeSelectorProps = {
- value: MappingMode;
- onChange: (mode: MappingMode) => void;
- fullLabel: string; // e.g., "Full observation input"
- fieldName: string; // e.g., "input", "output", "metadata"
-};
-
-export function MappingModeSelector({
- value,
- onChange,
- fullLabel,
- fieldName,
-}: MappingModeSelectorProps) {
- return (
- <RadioGroup value={value} onValueChange={(v) => onChange(v as MappingMode)}>
- <div className="hover:bg-muted/50 flex items-center space-x-3 rounded-md border px-3">
- <RadioGroup.Item value="full" id={`${fieldName}-full`} />
- <Label
- htmlFor={`${fieldName}-full`}
- className="flex-1 cursor-pointer py-3 text-sm font-bold"
- >
- {fullLabel}
- </Label>
- </div>
- <div className="hover:bg-muted/50 flex items-center space-x-3 rounded-md border px-3">
- <RadioGroup.Item value="custom" id={`${fieldName}-custom`} />
- <Label
- htmlFor={`${fieldName}-custom`}
- className="flex-1 cursor-pointer py-3 text-sm font-bold"
- >
- Custom mapping
- </Label>
- </div>
- {fieldName !== "input" && (
- <div className="hover:bg-muted/50 flex items-center space-x-3 rounded-md border px-3">
- <RadioGroup.Item
- value="none"
- id={`${fieldName}-none`}
- disabled={fieldName === "input"}
- />
- <Label
- htmlFor={`${fieldName}-none`}
- className="flex-1 cursor-pointer py-3 text-sm font-bold"
- >
- None
- </Label>
- </div>
- )}
- </RadioGroup>
- );
-}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingPreviewPanel.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingPreviewPanel.tsx
deleted file mode 100644
index 69a33f0418..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingPreviewPanel.tsx
+++ /dev/null
@@ -1,331 +0,0 @@
-/* eslint-disable no-nested-ternary */
-import { useMemo, useEffect, useRef } from "react";
-import {
- ArrowDown,
- AlertCircle,
- AlertTriangle,
- CheckCircle2,
-} from "lucide-react";
-import { JSONView } from "@/src/components/ui/CodeJsonViewer";
-import { Skeleton } from "@/src/components/ui/skeleton";
-import {
- IssueList,
- IssueItem,
-} from "@/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/IssueBanner";
-import type {
- FieldMappingConfig,
- SourceField,
- ObservationPreviewData,
- SchemaValidationError,
-} from "../types";
-import {
- applyFieldMappingConfig,
- validateFieldAgainstSchema,
- type JsonPathMissInfo,
- type JsonPathErrorInfo,
-} from "@langfuse/shared";
-
-type MappingPreviewPanelProps = {
- fieldLabel: string;
- defaultSourceField: SourceField;
- config: FieldMappingConfig;
- observationData: ObservationPreviewData | null;
- isLoading: boolean;
- /** JSON Schema to validate the result against */
- schema?: unknown;
- /** Callback when validation state changes */
- onValidationChange?: (
- isValid: boolean,
- errors: SchemaValidationError[],
- ) => void;
-};
-
-export function MappingPreviewPanel({
- fieldLabel,
- defaultSourceField,
- config,
- observationData,
- isLoading,
- schema,
- onValidationChange,
-}: MappingPreviewPanelProps) {
- const hasSchema = schema !== null && schema !== undefined;
-
- // Compute source data to display
- const sourceData = useMemo(() => {
- if (!observationData) return null;
-
- // Determine which source field to show based on config
- if (config.mode === "custom" && config.custom) {
- if (config.custom.type === "root" && config.custom.rootConfig) {
- return observationData[config.custom.rootConfig.sourceField];
- }
- // For keyValueMap type, show the default source field
- return observationData[defaultSourceField];
- }
-
- return observationData[defaultSourceField];
- }, [observationData, config, defaultSourceField]);
-
- // Compute result data and collect JSONPath misses / syntax errors
- const { resultData, jsonPathMisses, jsonPathErrors } = useMemo(() => {
- if (!observationData)
- return {
- resultData: null,
- jsonPathMisses: [] as JsonPathMissInfo[],
- jsonPathErrors: [] as JsonPathErrorInfo[],
- };
-
- const result = applyFieldMappingConfig({
- observation: {
- input: observationData.input,
- output: observationData.output,
- metadata: observationData.metadata,
- },
- config,
- defaultSourceField,
- });
-
- return {
- resultData: result.value,
- jsonPathMisses: result.misses,
- jsonPathErrors: result.errors,
- };
- }, [observationData, config, defaultSourceField]);
-
- // Validate result against schema, and treat JSONPath syntax errors as validation failures
- const validationResult = useMemo(() => {
- const jsonPathErrorItems: SchemaValidationError[] = jsonPathErrors.map(
- (err) => ({
- path: err.mappingKey
- ? `${err.sourceField} (key: "${err.mappingKey}")`
- : err.sourceField,
- message: `Invalid JSONPath "${err.jsonPath}": ${err.message}`,
- }),
- );
-
- // Any JSONPath syntax error blocks the mapping regardless of schema
- if (jsonPathErrorItems.length > 0) {
- return { isValid: false, errors: jsonPathErrorItems };
- }
-
- // Skip schema validation if no schema or "none" mode
- if (!hasSchema || config.mode === "none") {
- return { isValid: true, errors: [] as SchemaValidationError[] };
- }
-
- // Skip if no result data (no observation)
- if (resultData === null || resultData === undefined) {
- return { isValid: true, errors: [] as SchemaValidationError[] };
- }
-
- try {
- const result = validateFieldAgainstSchema({
- data: resultData,
- schema: schema as Record<string, unknown>,
- });
-
- if (result.isValid) {
- return { isValid: true, errors: [] as SchemaValidationError[] };
- }
-
- return {
- isValid: false,
- errors: result.errors.map((e) => ({
- path: e.path,
- message: e.message,
- })),
- };
- } catch {
- // If schema validation fails to run, treat as valid (don't block on validation errors)
- return { isValid: true, errors: [] as SchemaValidationError[] };
- }
- }, [hasSchema, config.mode, resultData, schema, jsonPathErrors]);
-
- // Track previous validation state to avoid redundant callbacks
- const prevValidationRef = useRef<{
- isValid: boolean;
- errorsJson: string;
- } | null>(null);
-
- // Notify parent of validation state changes (only when values actually change)
- useEffect(() => {
- const errorsJson = JSON.stringify(validationResult.errors);
- const prev = prevValidationRef.current;
-
- // Only call if values actually changed
- if (
- !prev ||
- prev.isValid !== validationResult.isValid ||
- prev.errorsJson !== errorsJson
- ) {
- prevValidationRef.current = {
- isValid: validationResult.isValid,
- errorsJson,
- };
- onValidationChange?.(validationResult.isValid, validationResult.errors);
- }
- }, [validationResult.isValid, validationResult.errors, onValidationChange]);
-
- // Determine source label based on config
- const sourceLabel = useMemo(() => {
- if (config.mode === "custom" && config.custom) {
- if (config.custom.type === "root" && config.custom.rootConfig) {
- return `observation.${config.custom.rootConfig.sourceField}`;
- }
- if (config.custom.type === "keyValueMap") {
- // For keyValueMap type, multiple sources may be used
- const sources = new Set(
- config.custom.keyValueMapConfig?.entries.map((e) => e.sourceField) ??
- [],
- );
- if (sources.size === 1) {
- return `observation.${Array.from(sources)[0]}`;
- }
- return "multiple sources";
- }
- }
- return `observation.${defaultSourceField}`;
- }, [config, defaultSourceField]);
-
- if (isLoading) {
- return (
- <div className="space-y-4">
- <div>
- <h3 className="text-sm font-bold">Preview</h3>
- <p className="text-muted-foreground text-xs">
- Sample from first observation
- </p>
- </div>
- <Skeleton className="h-32 w-full" />
- <Skeleton className="h-32 w-full" />
- </div>
- );
- }
-
- if (!observationData) {
- return (
- <div className="space-y-4">
- <div>
- <h3 className="text-sm font-bold">Preview</h3>
- <p className="text-muted-foreground text-xs">
- Sample from first observation
- </p>
- </div>
- <div className="bg-muted/30 flex h-64 items-center justify-center rounded-md border p-4">
- <p className="text-muted-foreground text-sm">
- No observation data available
- </p>
- </div>
- </div>
- );
- }
-
- return (
- <div className="space-y-2">
- <div>
- <h3 className="text-sm font-bold">Preview</h3>
- <p className="text-muted-foreground text-xs">
- Sample from first observation
- </p>
- </div>
-
- {/* Source data */}
- <div className="space-y-2">
- <p className="text-muted-foreground text-xs font-bold">
- Source: {sourceLabel}
- </p>
- <div className="bg-muted/30 max-h-[21vh] overflow-auto rounded-md border">
- <JSONView json={sourceData} className="text-xs" />
- </div>
- </div>
-
- {/* Arrow */}
- <div className="flex justify-center">
- <ArrowDown className="text-muted-foreground h-6 w-6" />
- </div>
-
- {/* Result data */}
- <div className="space-y-2">
- <div className="flex items-center gap-2">
- <p className="text-muted-foreground text-xs font-bold">
- Result: Dataset Item {fieldLabel}
- </p>
- {/* Validation status indicator */}
- {config.mode !== "none" && (
- <div className="flex items-center gap-1">
- {jsonPathErrors.length > 0 ||
- (hasSchema && !validationResult.isValid) ? (
- <AlertCircle className="text-destructive h-3.5 w-3.5" />
- ) : jsonPathMisses.length > 0 ? (
- <AlertTriangle className="h-3.5 w-3.5 text-amber-600 dark:text-amber-500" />
- ) : hasSchema ? (
- <CheckCircle2 className="h-3.5 w-3.5 text-green-600" />
- ) : null}
- </div>
- )}
- </div>
- <div
- className={`bg-background max-h-[21vh] overflow-auto rounded-md border ${
- (jsonPathErrors.length > 0 ||
- (hasSchema && !validationResult.isValid)) &&
- config.mode !== "none"
- ? "border-destructive"
- : jsonPathMisses.length > 0 && config.mode !== "none"
- ? "border-amber-500/50"
- : ""
- }`}
- >
- {config.mode === "none" ? (
- <div className="text-muted-foreground p-3 text-xs italic">null</div>
- ) : (
- <JSONView json={resultData} className="text-xs" />
- )}
- </div>
-
- {/* JSONPath syntax errors (always blocking) */}
- {jsonPathErrors.length > 0 && config.mode !== "none" && (
- <IssueList variant="error" title="Invalid JSONPath:">
- {jsonPathErrors.map((err, idx) => (
- <IssueItem key={idx}>
- <span className="font-mono">{err.jsonPath}</span>
- {err.mappingKey ? ` (key: "${err.mappingKey}")` : ""}:{" "}
- {err.message}
- </IssueItem>
- ))}
- </IssueList>
- )}
-
- {/* Schema validation errors (only when no blocking JSONPath errors) */}
- {hasSchema &&
- jsonPathErrors.length === 0 &&
- validationResult.errors.length > 0 && (
- <IssueList variant="error" title="Schema validation errors:">
- {validationResult.errors.map((error, idx) => (
- <IssueItem key={idx}>
- <span className="font-mono">{error.path || "root"}</span>:{" "}
- {error.message}
- </IssueItem>
- ))}
- </IssueList>
- )}
-
- {/* JSONPath warnings */}
- {jsonPathMisses.length > 0 && config.mode !== "none" && (
- <IssueList
- variant="warning"
- title="JSONPath warnings (preview observation):"
- >
- {jsonPathMisses.map((miss, idx) => (
- <IssueItem key={idx}>
- <span className="font-mono">{miss.jsonPath}</span> did not match
- any data in {miss.sourceField}
- {miss.mappingKey ? ` (key: "${miss.mappingKey}")` : ""}
- </IssueItem>
- ))}
- </IssueList>
- )}
- </div>
- </div>
- );
-}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/datasetMappingStore.clienttest.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/datasetMappingStore.clienttest.ts
new file mode 100644
index 0000000000..c963eec7be
--- /dev/null
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/datasetMappingStore.clienttest.ts
@@ -0,0 +1,163 @@
+import { describe, expect, it, vi } from "vitest";
+import { createDatasetMappingStore } from "./datasetMappingStore";
+import {
+ createDatasetMapping,
+ prepareDatasetMapping,
+} from "./prepareDatasetMapping";
+import { submitDatasetBatch } from "./submitDatasetBatch";
+
+const dataset = {
+ id: "dataset",
+ name: "Review examples",
+ inputSchema: null,
+ expectedOutputSchema: null,
+};
+const observation = {
+ id: "observation",
+ input: { question: "Example" },
+ output: "Answer",
+ metadata: { region: "west" },
+};
+
+describe("Dataset mapping ownership", () => {
+ it("initializes schema mappings on selection and preserves edits through returning from create or reselecting the same dataset", () => {
+ const store = createDatasetMappingStore();
+ const withSchema = {
+ ...dataset,
+ inputSchema: {
+ type: "object",
+ properties: { question: { type: "string" } },
+ required: ["question"],
+ },
+ };
+ const actions = store.getState().actions;
+ actions.selectDataset(withSchema);
+ expect(store.getState().mapping.input.mode).toBe("custom");
+ actions.changeMapping("input", { mode: "full" });
+ actions.setScreen("create");
+ actions.setScreen("compose");
+ actions.selectDataset({ ...withSchema });
+ expect(store.getState().mapping.input).toEqual({ mode: "full" });
+ expect(
+ prepareDatasetMapping(
+ store.getState().mapping,
+ observation,
+ withSchema,
+ )[0].errors,
+ ).toEqual([]);
+ });
+
+ it.each(["selectDataset", "datasetCreated"] as const)(
+ "keeps edited mappings when %s supplies schema defaults",
+ (action) => {
+ const store = createDatasetMappingStore();
+ const actions = store.getState().actions;
+ actions.changeMapping("input", { mode: "full" });
+ actions.changeMapping("metadata", { mode: "full" });
+ const schema = {
+ type: "object",
+ properties: { answer: { type: "string" } },
+ };
+ actions[action]({
+ ...dataset,
+ inputSchema: schema,
+ expectedOutputSchema: schema,
+ });
+ expect(store.getState().mapping.input).toEqual({ mode: "full" });
+ expect(store.getState().mapping.metadata).toEqual({ mode: "full" });
+ expect(store.getState().mapping.expectedOutput.mode).toBe("custom");
+ expect(store.getState().datasetId).toBe(dataset.id);
+ },
+ );
+
+ it("validates JSONPath syntax for every field without a schema and retains preview misses as warnings", () => {
+ const mapping = createDatasetMapping(null);
+ mapping.metadata = {
+ mode: "custom",
+ custom: {
+ type: "root",
+ rootConfig: { sourceField: "metadata", jsonPath: "$[?(@.region ===)]" },
+ },
+ };
+ const invalid = prepareDatasetMapping(mapping, observation, dataset);
+ expect(
+ invalid.find((field) => field.key === "metadata")?.errors.length,
+ ).toBeGreaterThan(0);
+ mapping.metadata.custom!.rootConfig!.jsonPath = "$.missing";
+ const missing = prepareDatasetMapping(mapping, observation, dataset).find(
+ (field) => field.key === "metadata",
+ )!;
+ expect(missing.errors).toEqual([]);
+ expect(missing.warnings).toHaveLength(1);
+ });
+
+ it("submits the selected source once, preserves its mapping, and reports completion only after acceptance", async () => {
+ const store = createDatasetMappingStore();
+ store.getState().actions.selectDataset(dataset);
+ store.getState().actions.changeMapping("metadata", { mode: "full" });
+ let resolveSubmission!: (value: { id: string }) => void;
+ const submit = vi.fn(
+ () =>
+ new Promise<{ id: string }>((resolve) => {
+ resolveSubmission = resolve;
+ }),
+ );
+ const onSuccess = vi.fn();
+ const parameters = {
+ projectId: "project",
+ store,
+ dataset,
+ observation,
+ source: {
+ query: { filter: [], orderBy: null },
+ selectAll: false,
+ selectedObservationIds: ["observation"],
+ },
+ submit,
+ onSuccess,
+ };
+ const pending = submitDatasetBatch(parameters);
+ await submitDatasetBatch(parameters);
+ const assertSubmissionKeepsDraft = () => {
+ const submittedState = store.getState();
+ const actions = submittedState.actions;
+ actions.changeMapping("metadata", { mode: "none" });
+ actions.selectDataset({ ...dataset, id: "other" });
+ actions.datasetCreated({ ...dataset, id: "created" });
+ actions.setScreen("create");
+ expect(store.getState()).toBe(submittedState);
+ };
+ assertSubmissionKeepsDraft();
+ expect(submit).toHaveBeenCalledTimes(1);
+ expect(onSuccess).not.toHaveBeenCalled();
+ expect(submit).toHaveBeenCalledWith(
+ expect.objectContaining({
+ query: {
+ filter: [
+ {
+ column: "id",
+ type: "stringOptions",
+ operator: "any of",
+ value: ["observation"],
+ },
+ ],
+ orderBy: null,
+ },
+ config: expect.objectContaining({
+ mapping: expect.objectContaining({ metadata: { mode: "full" } }),
+ }),
+ }),
+ );
+ resolveSubmission({ id: "batch" });
+ await pending;
+ expect(onSuccess).toHaveBeenCalledTimes(1);
+ expect(store.getState().submission).toEqual({
+ status: "scheduled",
+ batchActionId: "batch",
+ dataset,
+ });
+ assertSubmissionKeepsDraft();
+ store.getState().actions.submissionFailed();
+ expect(store.getState().submission.status).toBe("scheduled");
+ });
+});
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/datasetMappingStore.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/datasetMappingStore.ts
new file mode 100644
index 0000000000..34fba828b9
--- /dev/null
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/datasetMappingStore.ts
@@ -0,0 +1,92 @@
+import { createStore } from "zustand/vanilla";
+import type { DatasetInfo, FieldMappingConfig, MappingConfig } from "./types";
+import { createDatasetMapping } from "./prepareDatasetMapping";
+
+type DatasetMappingState = {
+ datasetId: string | null;
+ createdDataset: DatasetInfo | null;
+ mapping: MappingConfig;
+ editedFields: Set<keyof MappingConfig>;
+ screen: "compose" | "create";
+ submission:
+ | { status: "idle" }
+ | { status: "pending" }
+ | { status: "scheduled"; batchActionId: string; dataset: DatasetInfo };
+ actions: {
+ selectDataset: (dataset: DatasetInfo) => void;
+ datasetCreated: (dataset: DatasetInfo) => void;
+ setScreen: (screen: "compose" | "create") => void;
+ changeMapping: (
+ field: keyof MappingConfig,
+ config: FieldMappingConfig,
+ ) => void;
+ startSubmission: () => boolean;
+ scheduled: (batchActionId: string, dataset: DatasetInfo) => void;
+ submissionFailed: () => void;
+ };
+};
+
+function mappingForDataset(dataset: DatasetInfo, state: DatasetMappingState) {
+ const mapping = createDatasetMapping(dataset);
+ for (const field of state.editedFields) mapping[field] = state.mapping[field];
+ return mapping;
+}
+
+export function createDatasetMappingStore() {
+ return createStore<DatasetMappingState>((set, get) => ({
+ datasetId: null,
+ createdDataset: null,
+ mapping: createDatasetMapping(null),
+ editedFields: new Set(),
+ screen: "compose",
+ submission: { status: "idle" },
+ actions: {
+ selectDataset: (dataset) => {
+ if (
+ get().submission.status !== "idle" ||
+ get().datasetId === dataset.id
+ )
+ return;
+ set({
+ datasetId: dataset.id,
+ mapping: mappingForDataset(dataset, get()),
+ });
+ },
+ datasetCreated: (dataset) => {
+ if (get().submission.status !== "idle") return;
+ set({
+ datasetId: dataset.id,
+ createdDataset: dataset,
+ mapping: mappingForDataset(dataset, get()),
+ screen: "compose",
+ });
+ },
+ setScreen: (screen) => {
+ if (get().submission.status !== "idle") return;
+ set({ screen });
+ },
+ changeMapping: (field, config) => {
+ if (get().submission.status !== "idle") return;
+ set((state) => ({
+ mapping: { ...state.mapping, [field]: config },
+ editedFields: new Set([...state.editedFields, field]),
+ }));
+ },
+ startSubmission: () => {
+ if (get().submission.status !== "idle") return false;
+ set({ submission: { status: "pending" } });
+ return true;
+ },
+ scheduled: (batchActionId, dataset) => {
+ if (get().submission.status !== "pending") return;
+ set({ submission: { status: "scheduled", batchActionId, dataset } });
+ },
+ submissionFailed: () => {
+ if (get().submission.status !== "pending") return;
+ set({ submission: { status: "idle" } });
+ },
+ },
+ }));
+}
+
+export type DatasetMappingStore = ReturnType<typeof createDatasetMappingStore>;
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/index.clienttest.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/index.clienttest.tsx
new file mode 100644
index 0000000000..7df57385e1
--- /dev/null
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/index.clienttest.tsx
@@ -0,0 +1,162 @@
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { useState } from "react";
+import { vi } from "vitest";
+import { LayerProvider } from "@/src/context/LayerContext/LayerContext";
+import { AddObservationsToDatasetDialog } from "./index";
+
+const { submit, submitted, previewQuery } = vi.hoisted(() => ({
+ submit: vi.fn<() => Promise<{ id: string }>>(),
+ submitted: vi.fn(),
+ previewQuery: vi.fn(() => ({
+ data: {
+ id: "first",
+ input: { question: "Example" },
+ output: "Answer",
+ metadata: {},
+ },
+ isLoading: false,
+ })),
+}));
+
+vi.mock("@/src/utils/api", () => ({
+ sendAsPostOption: {},
+ api: {
+ datasets: {
+ allDatasetMeta: {
+ useQuery: () => ({
+ data: [
+ {
+ id: "dataset",
+ name: "Review examples",
+ inputSchema: null,
+ expectedOutputSchema: null,
+ },
+ ],
+ isLoading: false,
+ }),
+ },
+ },
+ events: {
+ batchIO: { useQuery: () => ({ data: undefined, isLoading: false }) },
+ },
+ observations: { byId: { useQuery: previewQuery } },
+ batchAction: {
+ addToDataset: {
+ create: {
+ useMutation: (options: { onError: (error: Error) => void }) => ({
+ mutateAsync: async () => {
+ try {
+ const data = await submit();
+ return data;
+ } catch (error) {
+ options.onError(error as Error);
+ throw error;
+ }
+ },
+ }),
+ },
+ },
+ },
+ },
+}));
+vi.mock("@/src/features/notifications", () => ({ showErrorToast: vi.fn() }));
+vi.mock("@/src/components/ui/combobox", () => ({
+ Combobox: ({ onValueChange }: { onValueChange: (value: string) => void }) => (
+ <button onClick={() => onValueChange("dataset")}>Use dataset</button>
+ ),
+}));
+vi.mock("./DatasetCreateStep", () => ({
+ DatasetCreateStep: () => <div />,
+}));
+vi.mock("./MappingStep", () => ({ MappingStep: () => <div /> }));
+vi.mock("@/src/components/ui/CodeJsonViewer", () => ({
+ JSONView: ({ json }: { json: unknown }) => <pre>{JSON.stringify(json)}</pre>,
+}));
+vi.mock("./StatusStep", () => ({
+ StatusStep: ({
+ expectedCount,
+ onClose,
+ }: {
+ expectedCount: number;
+ onClose: () => void;
+ }) => (
+ <div>
+ <p>{expectedCount} observations submitted</p>
+ <button onClick={onClose}>Close progress</button>
+ </div>
+ ),
+}));
+
+function SelectionHarness() {
+ const [open, setOpen] = useState(true);
+ const [selectedIds, setSelectedIds] = useState(["first", "second"]);
+ const props = {
+ projectId: "project",
+ selectedObservationIds: selectedIds,
+ query: { filter: [], orderBy: null },
+ selectAll: false,
+ totalCount: 10,
+ isV4: false,
+ exampleObservation: { id: selectedIds[0] ?? "", traceId: "trace" },
+ onClose: () => setOpen(false),
+ onSuccess: () => {
+ submitted();
+ setSelectedIds([]);
+ },
+ };
+ return (
+ <LayerProvider>
+ <p>{selectedIds.length} rows selected</p>
+ {open && <AddObservationsToDatasetDialog {...props} />}
+ </LayerProvider>
+ );
+}
+
+function prepareSubmission() {
+ fireEvent.click(screen.getByRole("button", { name: "Use dataset" }));
+}
+
+describe("Add observations to dataset lifecycle", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ submit.mockResolvedValue({ id: "batch-action" });
+ });
+
+ it("dismisses without completing the action or changing the selected rows", () => {
+ render(<SelectionHarness />);
+ fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ expect(screen.getByText("2 rows selected")).toBeInTheDocument();
+ expect(submitted).not.toHaveBeenCalled();
+ });
+
+ it("clears selection once after submission and keeps its original progress context", async () => {
+ render(<SelectionHarness />);
+ prepareSubmission();
+ fireEvent.click(screen.getByRole("button", { name: "Add 2 observations" }));
+ await waitFor(() => expect(submitted).toHaveBeenCalledTimes(1));
+ expect(screen.getByText("0 rows selected")).toBeInTheDocument();
+ expect(screen.getByText("2 observations submitted")).toBeInTheDocument();
+ expect(previewQuery).toHaveBeenLastCalledWith(
+ expect.objectContaining({ observationId: "first" }),
+ expect.anything(),
+ );
+ fireEvent.click(screen.getByRole("button", { name: "Close progress" }));
+ expect(submitted).toHaveBeenCalledTimes(1);
+ });
+
+ it("keeps the draft and selection after a rejected submission, then allows retry", async () => {
+ submit.mockRejectedValueOnce(new Error("Temporarily unavailable"));
+ render(<SelectionHarness />);
+ prepareSubmission();
+ fireEvent.click(screen.getByRole("button", { name: "Add 2 observations" }));
+ await waitFor(() => expect(submit).toHaveBeenCalledTimes(1));
+ expect(submitted).not.toHaveBeenCalled();
+ expect(screen.getByText("2 rows selected")).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: "Add 2 observations" }),
+ ).toBeEnabled();
+ fireEvent.click(screen.getByRole("button", { name: "Add 2 observations" }));
+ await waitFor(() => expect(submitted).toHaveBeenCalledTimes(1));
+ });
+});
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/index.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/index.tsx
index 9ee5eefd49..c2ab9aa9ef 100644
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/index.tsx
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/index.tsx
@@ -1,4 +1,6 @@
-import { Button } from "@/src/components/ui/button";
+import { useRef, useState } from "react";
+import { useStore } from "zustand";
+import { parseJsonPrioritised, type BatchActionQuery } from "@langfuse/shared";
import {
Dialog,
DialogContent,
@@ -6,182 +8,171 @@ import {
DialogTitle,
DialogDescription,
DialogBody,
- DialogFooter,
} from "@/src/components/ui/dialog";
-import { ChevronLeft } from "lucide-react";
-
-// Step components
-import { DatasetChoiceStep } from "./DatasetChoiceStep";
-import { DatasetSelectStep } from "./DatasetSelectStep";
+import { api, sendAsPostOption } from "@/src/utils/api";
+import { showErrorToast } from "@/src/features/notifications";
import { DatasetCreateStep } from "./DatasetCreateStep";
-import { MappingStep } from "./MappingStep";
-import { FinalPreviewStep } from "./FinalPreviewStep";
+import { DatasetMappingEditor } from "./DatasetMappingEditor";
import { StatusStep } from "./StatusStep";
-
-// Hook
-import {
- useAddToDatasetWizard,
- type UseAddToDatasetWizardProps,
-} from "./useAddToDatasetWizard";
+import { createDatasetMappingStore } from "./datasetMappingStore";
+import { submitDatasetBatch } from "./submitDatasetBatch";
+import type { ObservationPreviewData } from "./types";
type AddObservationsToDatasetDialogProps = {
projectId: string;
onClose: () => void;
-} & UseAddToDatasetWizardProps;
+ onSuccess: () => void;
+ selectedObservationIds: string[];
+ query: BatchActionQuery;
+ selectAll: boolean;
+ totalCount: number;
+ isV4: boolean;
+ exampleObservation: { id: string; traceId: string; startTime?: Date };
+};
+
+function normalizeValue(value: unknown) {
+ if (typeof value !== "string") return value;
+ const parsed = parseJsonPrioritised(value);
+ return parsed === undefined ? value : parsed;
+}
export function AddObservationsToDatasetDialog(
props: AddObservationsToDatasetDialogProps,
) {
- const { projectId, onClose } = props;
-
- const {
- state,
- formRef,
- observationData,
- isLoadingObservation,
- displayCount,
- selectMode,
- goBack,
- goToStep,
- handleNextClick,
- handleDatasetSelect,
- handleDatasetCreated,
- handleInputConfigChange,
- handleOutputConfigChange,
- handleMetadataConfigChange,
- handleCreateValidationChange,
- handleInputValidationChange,
- handleOutputValidationChange,
- isNextDisabled,
- nextButtonLabel,
- dialogDescription,
- showBackButton,
- canClose,
- isLoading,
- } = useAddToDatasetWizard(props);
-
- const { step } = state;
-
+ const [source] = useState(() => ({
+ selectedObservationIds: [...props.selectedObservationIds],
+ query: props.query,
+ selectAll: props.selectAll,
+ totalCount: props.totalCount,
+ exampleObservation: props.exampleObservation,
+ isV4: props.isV4,
+ }));
+ const [store] = useState(createDatasetMappingStore);
+ const screen = useStore(store, (state) => state.screen);
+ const submission = useStore(store, (state) => state.submission);
+ const createPending = useRef(false);
+ const count = source.selectAll
+ ? source.totalCount
+ : source.selectedObservationIds.length;
+ const example = source.exampleObservation;
+ const datasets = api.datasets.allDatasetMeta.useQuery({
+ projectId: props.projectId,
+ });
+ const observationQuery = api.observations.byId.useQuery(
+ {
+ projectId: props.projectId,
+ observationId: example.id,
+ traceId: example.traceId,
+ startTime: example.startTime,
+ },
+ { enabled: !source.isV4 && Boolean(example.id && example.traceId) },
+ );
+ const eventQuery = api.events.batchIO.useQuery(
+ {
+ projectId: props.projectId,
+ observations: [{ id: example.id, traceId: example.traceId }],
+ minStartTime: example.startTime as Date,
+ maxStartTime: example.startTime as Date,
+ truncated: false,
+ },
+ {
+ ...sendAsPostOption,
+ enabled:
+ source.isV4 &&
+ Boolean(example.id && example.traceId && example.startTime),
+ },
+ );
+ const raw = source.isV4 ? eventQuery.data?.[0] : observationQuery.data;
+ const observation: ObservationPreviewData | null = raw
+ ? {
+ id: example.id,
+ input: normalizeValue(raw.input),
+ output: normalizeValue(raw.output),
+ metadata: normalizeValue(raw.metadata),
+ }
+ : null;
+ const previewQuery = source.isV4 ? eventQuery : observationQuery;
+ const mutation = api.batchAction.addToDataset.create.useMutation({
+ onError: (error) =>
+ showErrorToast("Failed to schedule action", error.message),
+ });
return (
- <Dialog open onOpenChange={(open) => !open && canClose && onClose()}>
- <DialogContent className="flex max-h-[90vh] max-w-6xl flex-col">
+ <Dialog
+ open
+ onOpenChange={(open) => {
+ if (
+ !open &&
+ store.getState().submission.status !== "pending" &&
+ !createPending.current
+ )
+ props.onClose();
+ }}
+ >
+ <DialogContent size="xl">
<DialogHeader>
<DialogTitle>
- Add {displayCount} Observation(s) to dataset
- {!["select", "create", "choice"].includes(step)
- ? " " + state.dataset.name
- : ""}
+ {screen === "create"
+ ? "Create dataset"
+ : `Add ${count} observation${count === 1 ? "" : "s"} to dataset`}
</DialogTitle>
- <DialogDescription className="mt-1">
- {dialogDescription}
+ <DialogDescription>
+ {screen === "create"
+ ? "Create a dataset, then review the values to add."
+ : "Choose a dataset and review how each observation becomes a dataset item."}
</DialogDescription>
</DialogHeader>
-
- <DialogBody className="flex-1 overflow-y-auto p-0">
- {step === "choice" && <DatasetChoiceStep onSelectMode={selectMode} />}
-
- {step === "select" && (
- <DatasetSelectStep
- projectId={projectId}
- dataset={state.dataset}
- onDatasetSelect={handleDatasetSelect}
- />
- )}
-
- {step === "create" && (
- <DatasetCreateStep
- projectId={projectId}
- formRef={formRef}
- onDatasetCreated={handleDatasetCreated}
- onValidationChange={handleCreateValidationChange}
+ {submission.status === "scheduled" && (
+ <DialogBody>
+ <StatusStep
+ projectId={props.projectId}
+ batchActionId={submission.batchActionId}
+ dataset={submission.dataset}
+ expectedCount={count}
+ onClose={props.onClose}
/>
- )}
-
- {step === "input-mapping" && (
- <MappingStep
- field="input"
- fieldLabel="Input"
- defaultSourceField="input"
- config={state.mapping.input}
- onConfigChange={handleInputConfigChange}
- observationData={observationData}
- isLoading={isLoadingObservation}
- schema={state.dataset.inputSchema}
- onValidationChange={handleInputValidationChange}
- />
- )}
-
- {step === "output-mapping" && (
- <MappingStep
- field="expectedOutput"
- fieldLabel="Expected Output"
- defaultSourceField="output"
- config={state.mapping.expectedOutput}
- onConfigChange={handleOutputConfigChange}
- observationData={observationData}
- isLoading={isLoadingObservation}
- schema={state.dataset.expectedOutputSchema}
- onValidationChange={handleOutputValidationChange}
- />
- )}
-
- {step === "metadata-mapping" && (
- <MappingStep
- field="metadata"
- fieldLabel="Metadata"
- defaultSourceField="metadata"
- config={state.mapping.metadata}
- onConfigChange={handleMetadataConfigChange}
- observationData={observationData}
- isLoading={isLoadingObservation}
- />
- )}
-
- {step === "preview" && state.dataset.id && state.dataset.name && (
- <FinalPreviewStep
- dataset={{ id: state.dataset.id, name: state.dataset.name }}
- mapping={state.mapping}
- observationData={observationData}
- totalCount={displayCount}
- onEditStep={goToStep}
- />
- )}
-
- {step === "status" &&
- state.submission.batchActionId &&
- state.dataset.id &&
- state.dataset.name && (
- <StatusStep
- projectId={projectId}
- batchActionId={state.submission.batchActionId}
- dataset={{ id: state.dataset.id, name: state.dataset.name }}
- expectedCount={displayCount}
- onClose={onClose}
- />
- )}
- </DialogBody>
-
- {/* Footer with navigation buttons */}
- {step !== "status" && step !== "choice" && (
- <DialogFooter className="flex justify-between">
- <div className="grow">
- {showBackButton && (
- <Button type="button" variant="ghost" onClick={goBack}>
- <ChevronLeft className="mr-1 h-4 w-4" />
- Back
- </Button>
- )}
- </div>
- <div>
- <Button
- onClick={handleNextClick}
- disabled={isNextDisabled}
- loading={isLoading}
- >
- {nextButtonLabel}
- </Button>
- </div>
- </DialogFooter>
+ </DialogBody>
+ )}
+ {submission.status !== "scheduled" && screen === "create" && (
+ <DatasetCreateStep
+ projectId={props.projectId}
+ onDatasetCreated={(dataset) => {
+ store.getState().actions.datasetCreated(dataset);
+ }}
+ onSubmittingChange={(pending) => {
+ createPending.current = pending;
+ }}
+ onCancel={() => store.getState().actions.setScreen("compose")}
+ />
+ )}
+ {submission.status !== "scheduled" && screen === "compose" && (
+ <DatasetMappingEditor
+ store={store}
+ datasets={datasets.data ?? []}
+ loading={datasets.isLoading || previewQuery.isLoading}
+ unavailable={
+ datasets.isError ||
+ previewQuery.isError ||
+ (!previewQuery.isLoading && !observation)
+ }
+ onRetry={() => {
+ datasets.refetch();
+ previewQuery.refetch();
+ }}
+ observation={observation}
+ count={count}
+ onClose={props.onClose}
+ onSubmit={(dataset) =>
+ submitDatasetBatch({
+ projectId: props.projectId,
+ store,
+ dataset,
+ observation,
+ source,
+ submit: mutation.mutateAsync,
+ onSuccess: props.onSuccess,
+ })
+ }
+ />
)}
</DialogContent>
</Dialog>
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/prepareDatasetMapping.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/prepareDatasetMapping.ts
new file mode 100644
index 0000000000..fc94433f61
--- /dev/null
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/prepareDatasetMapping.ts
@@ -0,0 +1,121 @@
+import {
+ applyFieldMappingConfig,
+ FieldMappingConfigSchema,
+ validateFieldAgainstSchema,
+} from "@langfuse/shared";
+import type {
+ DatasetInfo,
+ FieldMappingConfig,
+ MappingConfig,
+ ObservationPreviewData,
+ PreparedMappingField,
+} from "./types";
+import {
+ extractSchemaFields,
+ generateEntriesFromSchema,
+} from "./utils/extractSchemaFields";
+
+export function createDatasetMapping(
+ dataset: DatasetInfo | null,
+): MappingConfig {
+ function field(
+ schema: unknown,
+ source: "input" | "output",
+ ): FieldMappingConfig {
+ const entries = generateEntriesFromSchema(
+ extractSchemaFields(schema),
+ source,
+ );
+ return entries.length
+ ? {
+ mode: "custom",
+ custom: { type: "keyValueMap", keyValueMapConfig: { entries } },
+ }
+ : { mode: "full" };
+ }
+ return {
+ input: field(dataset?.inputSchema, "input"),
+ expectedOutput: field(dataset?.expectedOutputSchema, "output"),
+ metadata: { mode: "none" },
+ };
+}
+
+export function prepareDatasetMapping(
+ mapping: MappingConfig,
+ observation: ObservationPreviewData | null,
+ dataset: DatasetInfo | null,
+): PreparedMappingField[] {
+ const fields = [
+ {
+ key: "input",
+ label: "Input",
+ sourceField: "input",
+ schema: dataset?.inputSchema,
+ },
+ {
+ key: "expectedOutput",
+ label: "Expected output",
+ sourceField: "output",
+ schema: dataset?.expectedOutputSchema,
+ },
+ {
+ key: "metadata",
+ label: "Metadata",
+ sourceField: "metadata",
+ schema: null,
+ },
+ ] as const;
+ return fields.map((field) => {
+ const config = mapping[field.key];
+ const errors: string[] = [];
+ const warnings: string[] = [];
+ const parsed = FieldMappingConfigSchema.safeParse(config);
+ if (!parsed.success)
+ errors.push(...parsed.error.issues.map((issue) => issue.message));
+ const result = applyFieldMappingConfig({
+ observation: observation ?? { input: null, output: null, metadata: null },
+ config,
+ defaultSourceField: field.sourceField,
+ });
+ errors.push(
+ ...result.errors.map(
+ (error) => `Invalid JSONPath “${error.jsonPath}”: ${error.message}`,
+ ),
+ );
+ if (observation) {
+ warnings.push(
+ ...result.misses.map(
+ (miss) =>
+ `“${miss.jsonPath}” did not match this observation. Items without a matching value will be skipped.`,
+ ),
+ );
+ if (
+ field.schema &&
+ config.mode !== "none" &&
+ !result.errors.length &&
+ !result.misses.length
+ ) {
+ try {
+ const validation = validateFieldAgainstSchema({
+ data: result.value,
+ schema: field.schema as Record<string, unknown>,
+ });
+ if (!validation.isValid)
+ errors.push(
+ ...validation.errors.map(
+ (error) => `${error.path || "Value"}: ${error.message}`,
+ ),
+ );
+ } catch {
+ errors.push("The dataset schema could not be validated.");
+ }
+ }
+ }
+ return {
+ ...field,
+ value: observation ? result.value : undefined,
+ errors,
+ warnings,
+ };
+ });
+}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/submitDatasetBatch.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/submitDatasetBatch.ts
new file mode 100644
index 0000000000..3306961ba7
--- /dev/null
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/submitDatasetBatch.ts
@@ -0,0 +1,71 @@
+import type {
+ BatchActionQuery,
+ ObservationAddToDatasetConfig,
+} from "@langfuse/shared";
+import type { DatasetMappingStore } from "./datasetMappingStore";
+import type { DatasetInfo, ObservationPreviewData } from "./types";
+import { prepareDatasetMapping } from "./prepareDatasetMapping";
+
+export async function submitDatasetBatch({
+ projectId,
+ store,
+ dataset,
+ observation,
+ source,
+ submit,
+ onSuccess,
+}: {
+ projectId: string;
+ store: DatasetMappingStore;
+ dataset: DatasetInfo | null;
+ observation: ObservationPreviewData | null;
+ source: {
+ query: BatchActionQuery;
+ selectAll: boolean;
+ selectedObservationIds: string[];
+ };
+ submit: (input: {
+ projectId: string;
+ query: BatchActionQuery;
+ config: ObservationAddToDatasetConfig;
+ }) => Promise<{ id: string }>;
+ onSuccess: () => void;
+}) {
+ const { mapping, actions } = store.getState();
+ if (
+ !dataset ||
+ !observation ||
+ prepareDatasetMapping(mapping, observation, dataset).some(
+ (field) => field.errors.length,
+ )
+ )
+ return;
+ if (!actions.startSubmission()) return;
+ const query = source.selectAll
+ ? source.query
+ : {
+ ...source.query,
+ filter: [
+ ...(source.query.filter ?? []),
+ {
+ column: "id",
+ operator: "any of" as const,
+ value: source.selectedObservationIds,
+ type: "stringOptions" as const,
+ },
+ ],
+ };
+ try {
+ const result = await submit({
+ projectId,
+ query,
+ config: { datasetId: dataset.id, datasetName: dataset.name, mapping },
+ });
+ actions.scheduled(result.id, dataset);
+ } catch {
+ // tRPC owns error reporting; the mutation callback displays the failure.
+ actions.submissionFailed();
+ return;
+ }
+ onSuccess();
+}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/types.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/types.ts
index 288ecb4c53..e85f533170 100644
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/types.ts
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/types.ts
@@ -1,9 +1,3 @@
-import type { RefObject } from "react";
-import type { DatasetFormRef } from "@/src/features/datasets";
-
-// Re-export base types from shared
-export type { SourceField, MappingMode, MappingTarget } from "@langfuse/shared";
-
import type {
SourceField,
MappingMode,
@@ -12,55 +6,30 @@ import type {
MappingTarget,
} from "@langfuse/shared";
-// Step definitions - dialog-specific
-export type DialogStep =
- | "choice"
- | "select"
- | "create"
- | "input-mapping"
- | "output-mapping"
- | "metadata-mapping"
- | "preview"
- | "status";
+export type { SourceField, MappingMode, MappingTarget } from "@langfuse/shared";
-// Extended KeyValueMappingEntry with UI-specific properties
export type KeyValueMappingEntry = SharedKeyValueMappingEntry & {
- /** Whether this entry was generated from schema (UI only, not persisted) */
fromSchema?: boolean;
- /** Whether this is a required field per schema (UI only, not persisted) */
isRequired?: boolean;
};
-// Custom mapping configuration using UI-extended KeyValueMappingEntry
export type CustomMappingConfig = {
type: MappingTarget;
rootConfig?: RootMappingConfig;
- keyValueMapConfig?: {
- entries: KeyValueMappingEntry[];
- };
+ keyValueMapConfig?: { entries: KeyValueMappingEntry[] };
};
-// Per-field mapping config using UI-extended CustomMappingConfig
export type FieldMappingConfig = {
mode: MappingMode;
custom?: CustomMappingConfig;
};
-// Complete mapping config for all three fields (UI version with extended types)
export type MappingConfig = {
input: FieldMappingConfig;
expectedOutput: FieldMappingConfig;
metadata: FieldMappingConfig;
};
-// Default mapping config
-export const DEFAULT_MAPPING_CONFIG: MappingConfig = {
- input: { mode: "full" },
- expectedOutput: { mode: "full" },
- metadata: { mode: "none" },
-};
-
-// Observation data for preview
export type ObservationPreviewData = {
id: string;
input: unknown;
@@ -68,63 +37,6 @@ export type ObservationPreviewData = {
metadata: unknown;
};
-// Prop groups for each step
-export type DatasetChoiceStepProps = {
- onSelectMode: (mode: "create" | "select") => void;
-};
-
-export type DatasetSelectStepProps = {
- projectId: string;
- dataset: WizardState["dataset"];
- onDatasetSelect: (dataset: DatasetInfo) => void;
-};
-
-export type DatasetCreateStepProps = {
- projectId: string;
- formRef: RefObject<DatasetFormRef | null>;
- onDatasetCreated: (params: {
- id: string;
- name: string;
- inputSchema: unknown;
- expectedOutputSchema: unknown;
- }) => void;
- onValidationChange?: (isValid: boolean, isSubmitting: boolean) => void;
-};
-
-// Schema validation error type
-export type SchemaValidationError = {
- path: string;
- message: string;
-};
-
-// Mapping step props (reusable for Input/Output/Metadata)
-export type MappingStepProps = {
- field: "input" | "expectedOutput" | "metadata";
- fieldLabel: string;
- defaultSourceField: SourceField;
- config: FieldMappingConfig;
- onConfigChange: (config: FieldMappingConfig) => void;
- observationData: ObservationPreviewData | null;
- isLoading: boolean;
- /** JSON Schema for this field (inputSchema for input, expectedOutputSchema for expectedOutput) */
- schema?: unknown;
- /** Callback when validation state changes */
- onValidationChange?: (
- isValid: boolean,
- errors: SchemaValidationError[],
- ) => void;
-};
-
-// Final preview step props
-export type FinalPreviewStepProps = {
- dataset: { id: string; name: string };
- mapping: MappingConfig;
- observationData: ObservationPreviewData | null;
- totalCount: number;
- onEditStep: (step: DialogStep) => void;
-};
-
-// Dataset info for selection/creation
export type DatasetInfo = {
id: string;
name: string;
@@ -132,58 +44,12 @@ export type DatasetInfo = {
expectedOutputSchema: unknown;
};
-// Wizard state for useReducer
-export type WizardState = {
- step: DialogStep;
- dataset: {
- id: string | null;
- name: string | null;
- inputSchema: unknown;
- expectedOutputSchema: unknown;
- };
- mapping: MappingConfig;
- submission: {
- batchActionId: string | null;
- isSubmitting: boolean;
- };
- validation: {
- inputMapping: boolean;
- outputMapping: boolean;
- };
- createStep: {
- canContinue: boolean;
- isCreating: boolean;
- };
+export type PreparedMappingField = {
+ key: keyof MappingConfig;
+ label: string;
+ sourceField: SourceField;
+ value: unknown;
+ schema: unknown;
+ errors: string[];
+ warnings: string[];
};
-
-// Wizard actions for useReducer
-export type WizardAction =
- // Navigation
- | { type: "SELECT_MODE"; mode: "create" | "select" }
- | { type: "NEXT_STEP" }
- | { type: "BACK" }
- | { type: "GO_TO_STEP"; step: DialogStep }
- // Dataset
- | { type: "SELECT_DATASET"; dataset: DatasetInfo }
- | { type: "DATASET_CREATED"; dataset: DatasetInfo }
- // Mapping
- | {
- type: "UPDATE_MAPPING";
- field: "input" | "expectedOutput" | "metadata";
- config: FieldMappingConfig;
- }
- // Validation
- | {
- type: "SET_MAPPING_VALIDATION";
- field: "input" | "output";
- isValid: boolean;
- }
- | {
- type: "SET_CREATE_VALIDATION";
- canContinue: boolean;
- isCreating: boolean;
- }
- // Submission
- | { type: "SUBMIT_START" }
- | { type: "SUBMIT_SUCCESS"; batchActionId: string }
- | { type: "SUBMIT_ERROR" };
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/useAddToDatasetWizard.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/useAddToDatasetWizard.ts
deleted file mode 100644
index c0a1095c5b..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/useAddToDatasetWizard.ts
+++ /dev/null
@@ -1,355 +0,0 @@
-/* eslint-disable no-nested-ternary */
-import { useReducer, useCallback, useMemo, useRef } from "react";
-import { api } from "@/src/utils/api";
-import { showErrorToast } from "@/src/features/notifications";
-import type { BatchActionQuery } from "@langfuse/shared";
-import type { DatasetFormRef } from "@/src/features/datasets";
-import type {
- DatasetInfo,
- DialogStep,
- FieldMappingConfig,
- ObservationPreviewData,
- SchemaValidationError,
-} from "./types";
-import { wizardReducer, initialWizardState } from "./wizardReducer";
-
-export type UseAddToDatasetWizardProps = {
- projectId: string;
- selectedObservationIds: string[];
- query: BatchActionQuery;
- selectAll: boolean;
- totalCount: number;
- exampleObservation: {
- id: string;
- traceId: string;
- startTime?: Date;
- };
-};
-
-export function useAddToDatasetWizard(props: UseAddToDatasetWizardProps) {
- const {
- projectId,
- selectedObservationIds,
- query,
- selectAll,
- totalCount,
- exampleObservation,
- } = props;
-
- const [state, dispatch] = useReducer(wizardReducer, initialWizardState);
-
- // Ref for DatasetForm imperative submit
- const formRef = useRef<DatasetFormRef>(null);
-
- // Get example observation for preview
- const observationQuery = api.observations.byId.useQuery(
- {
- observationId: exampleObservation.id,
- traceId: exampleObservation.traceId,
- projectId,
- startTime: exampleObservation.startTime,
- },
- {
- enabled: true,
- },
- );
-
- // Transform observation data for preview
- const observationData: ObservationPreviewData | null = observationQuery.data
- ? {
- id: observationQuery.data.id,
- input: observationQuery.data.input,
- output: observationQuery.data.output,
- metadata: observationQuery.data.metadata,
- }
- : null;
-
- // Mutation for creating batch action
- const createBatchAction = api.batchAction.addToDataset.create.useMutation({
- onSuccess: (data) => {
- dispatch({ type: "SUBMIT_SUCCESS", batchActionId: data.id });
- },
- onError: (error) => {
- showErrorToast("Failed to schedule action", error.message);
- dispatch({ type: "SUBMIT_ERROR" });
- },
- });
-
- // Display count
- const displayCount = selectAll ? totalCount : selectedObservationIds.length;
-
- // Action dispatchers
- const selectMode = useCallback((mode: "create" | "select") => {
- dispatch({ type: "SELECT_MODE", mode });
- }, []);
-
- const goNext = useCallback(() => {
- dispatch({ type: "NEXT_STEP" });
- }, []);
-
- const goBack = useCallback(() => {
- dispatch({ type: "BACK" });
- }, []);
-
- const goToStep = useCallback((step: DialogStep) => {
- dispatch({ type: "GO_TO_STEP", step });
- }, []);
-
- // Submit handler
- const handleSubmit = useCallback(async () => {
- if (!state.dataset.id || !state.dataset.name) return;
-
- dispatch({ type: "SUBMIT_START" });
-
- // If not selecting all, inject ID filter to only process selected observations
- const finalQuery = selectAll
- ? query
- : {
- ...query,
- filter: [
- ...(query.filter || []),
- {
- column: "id",
- operator: "any of" as const,
- value: selectedObservationIds,
- type: "stringOptions" as const,
- },
- ],
- };
-
- await createBatchAction.mutateAsync({
- projectId,
- query: finalQuery,
- config: {
- datasetId: state.dataset.id,
- datasetName: state.dataset.name,
- mapping: state.mapping,
- },
- });
- }, [
- state.dataset.id,
- state.dataset.name,
- state.mapping,
- selectAll,
- query,
- selectedObservationIds,
- projectId,
- createBatchAction,
- ]);
-
- // Handle next button click - coordinates different actions per step
- const handleNextClick = useCallback(() => {
- switch (state.step) {
- case "select":
- goNext();
- break;
- case "create":
- // Trigger form submission via ref
- formRef.current?.submit();
- break;
- case "input-mapping":
- case "output-mapping":
- case "metadata-mapping":
- goNext();
- break;
- case "preview":
- handleSubmit();
- break;
- default:
- break;
- }
- }, [state.step, goNext, handleSubmit]);
-
- // Callback adapters for child components
- const handleDatasetSelect = useCallback((dataset: DatasetInfo) => {
- dispatch({ type: "SELECT_DATASET", dataset });
- }, []);
-
- const handleDatasetCreated = useCallback(
- (params: {
- id: string;
- name: string;
- inputSchema: unknown;
- expectedOutputSchema: unknown;
- }) => {
- dispatch({ type: "DATASET_CREATED", dataset: params });
- },
- [],
- );
-
- const handleMappingConfigChange = useCallback(
- (
- field: "input" | "expectedOutput" | "metadata",
- config: FieldMappingConfig,
- ) => {
- dispatch({ type: "UPDATE_MAPPING", field, config });
- },
- [],
- );
-
- // Pre-bound callbacks for each mapping field to avoid inline functions in JSX
- const handleInputConfigChange = useCallback((config: FieldMappingConfig) => {
- dispatch({ type: "UPDATE_MAPPING", field: "input", config });
- }, []);
-
- const handleOutputConfigChange = useCallback((config: FieldMappingConfig) => {
- dispatch({ type: "UPDATE_MAPPING", field: "expectedOutput", config });
- }, []);
-
- const handleMetadataConfigChange = useCallback(
- (config: FieldMappingConfig) => {
- dispatch({ type: "UPDATE_MAPPING", field: "metadata", config });
- },
- [],
- );
-
- const handleCreateValidationChange = useCallback(
- (isValid: boolean, isCreating: boolean) => {
- dispatch({
- type: "SET_CREATE_VALIDATION",
- canContinue: isValid,
- isCreating,
- });
- },
- [],
- );
-
- const handleInputValidationChange = useCallback(
- (isValid: boolean, _errors: SchemaValidationError[]) => {
- dispatch({ type: "SET_MAPPING_VALIDATION", field: "input", isValid });
- },
- [],
- );
-
- const handleOutputValidationChange = useCallback(
- (isValid: boolean, _errors: SchemaValidationError[]) => {
- dispatch({ type: "SET_MAPPING_VALIDATION", field: "output", isValid });
- },
- [],
- );
-
- // Derived state
- const canContinueFromSelect = useMemo(
- () => !!state.dataset.id && !!state.dataset.name,
- [state.dataset.id, state.dataset.name],
- );
-
- const isNextDisabled = useMemo(() => {
- switch (state.step) {
- case "select":
- return !canContinueFromSelect;
- case "create":
- return !state.createStep.canContinue || state.createStep.isCreating;
- case "input-mapping":
- // Block if schema validation fails (only when schema exists)
- return (
- state.dataset.inputSchema !== null && !state.validation.inputMapping
- );
- case "output-mapping":
- // Block if schema validation fails (only when schema exists)
- return (
- state.dataset.expectedOutputSchema !== null &&
- !state.validation.outputMapping
- );
- case "preview":
- return state.submission.isSubmitting;
- default:
- return false;
- }
- }, [
- state.step,
- canContinueFromSelect,
- state.createStep.canContinue,
- state.createStep.isCreating,
- state.dataset.inputSchema,
- state.dataset.expectedOutputSchema,
- state.validation.inputMapping,
- state.validation.outputMapping,
- state.submission.isSubmitting,
- ]);
-
- const nextButtonLabel = useMemo(() => {
- switch (state.step) {
- case "select":
- return "Continue";
- case "create":
- return state.createStep.isCreating
- ? "Creating..."
- : "Create & Continue";
- case "input-mapping":
- case "output-mapping":
- case "metadata-mapping":
- return "Next";
- case "preview":
- return state.submission.isSubmitting ? "Adding..." : "Add to Dataset";
- default:
- return "Continue";
- }
- }, [state.step, state.createStep.isCreating, state.submission.isSubmitting]);
-
- const dialogDescription = useMemo(() => {
- switch (state.step) {
- case "choice":
- return "Choose where to add your observations";
- case "select":
- return "Select an existing dataset";
- case "create":
- return "Create a new dataset";
- case "input-mapping":
- return "Configure dataset item input mapping";
- case "output-mapping":
- return "Configure dataset item expected output mapping";
- case "metadata-mapping":
- return "Configure dataset item metadata mapping";
- case "preview":
- return "Review and confirm your configuration";
- case "status":
- return "Your bulk action status";
- default:
- return "";
- }
- }, [state.step]);
-
- const showBackButton = state.step !== "choice" && state.step !== "status";
- const canClose = !state.submission.isSubmitting;
- const isLoading =
- state.step === "create"
- ? state.createStep.isCreating
- : state.step === "preview"
- ? state.submission.isSubmitting
- : false;
-
- return {
- // State
- state,
- formRef,
- observationData,
- isLoadingObservation: observationQuery.isLoading,
- displayCount,
-
- // Actions
- selectMode,
- goBack,
- goToStep,
- handleNextClick,
-
- // Child component callbacks
- handleDatasetSelect,
- handleDatasetCreated,
- handleMappingConfigChange,
- handleInputConfigChange,
- handleOutputConfigChange,
- handleMetadataConfigChange,
- handleCreateValidationChange,
- handleInputValidationChange,
- handleOutputValidationChange,
-
- // Derived state
- isNextDisabled,
- nextButtonLabel,
- dialogDescription,
- showBackButton,
- canClose,
- isLoading,
- };
-}
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/utils/extractSchemaFields.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/utils/extractSchemaFields.ts
index e8211c3455..fe53749b74 100644
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/utils/extractSchemaFields.ts
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/utils/extractSchemaFields.ts
@@ -85,16 +85,6 @@ export function extractSchemaFields(
return fields;
}
-/**
- * Check if the schema is an object type (key-value mapping makes sense).
- */
-export function isObjectSchema(schema: unknown): boolean {
- if (!isJsonSchemaObject(schema)) {
- return false;
- }
- return getSchemaType(schema) === "object";
-}
-
// --- Internal helpers ---
function isJsonSchemaObject(value: unknown): value is JSONSchemaObject {
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/wizardReducer.ts b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/wizardReducer.ts
deleted file mode 100644
index 3e243a302d..0000000000
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/wizardReducer.ts
+++ /dev/null
@@ -1,151 +0,0 @@
-import {
- type WizardState,
- type WizardAction,
- type DialogStep,
- DEFAULT_MAPPING_CONFIG,
-} from "./types";
-
-export const initialWizardState: WizardState = {
- step: "choice",
- dataset: {
- id: null,
- name: null,
- inputSchema: null,
- expectedOutputSchema: null,
- },
- mapping: DEFAULT_MAPPING_CONFIG,
- submission: {
- batchActionId: null,
- isSubmitting: false,
- },
- validation: {
- inputMapping: true,
- outputMapping: true,
- },
- createStep: {
- canContinue: false,
- isCreating: false,
- },
-};
-
-// Step transition maps for navigation
-const NEXT_STEP_MAP: Partial<Record<DialogStep, DialogStep>> = {
- select: "input-mapping",
- "input-mapping": "output-mapping",
- "output-mapping": "metadata-mapping",
- "metadata-mapping": "preview",
-};
-
-const BACK_STEP_MAP: Partial<Record<DialogStep, DialogStep>> = {
- select: "choice",
- create: "choice",
- "input-mapping": "choice",
- "output-mapping": "input-mapping",
- "metadata-mapping": "output-mapping",
- preview: "metadata-mapping",
-};
-
-export function wizardReducer(
- state: WizardState,
- action: WizardAction,
-): WizardState {
- switch (action.type) {
- case "SELECT_MODE":
- return { ...state, step: action.mode };
-
- case "NEXT_STEP": {
- const nextStep = NEXT_STEP_MAP[state.step];
- return nextStep ? { ...state, step: nextStep } : state;
- }
-
- case "BACK": {
- const prevStep = BACK_STEP_MAP[state.step];
- return prevStep ? { ...state, step: prevStep } : state;
- }
-
- case "GO_TO_STEP":
- return { ...state, step: action.step };
-
- case "SELECT_DATASET":
- return {
- ...state,
- dataset: {
- id: action.dataset.id,
- name: action.dataset.name,
- inputSchema: action.dataset.inputSchema,
- expectedOutputSchema: action.dataset.expectedOutputSchema,
- },
- // Reset mapping when dataset changes
- mapping: DEFAULT_MAPPING_CONFIG,
- validation: { inputMapping: true, outputMapping: true },
- };
-
- case "DATASET_CREATED":
- return {
- ...state,
- step: "input-mapping",
- dataset: {
- id: action.dataset.id,
- name: action.dataset.name,
- inputSchema: action.dataset.inputSchema,
- expectedOutputSchema: action.dataset.expectedOutputSchema,
- },
- // Reset mapping for new dataset
- mapping: DEFAULT_MAPPING_CONFIG,
- validation: { inputMapping: true, outputMapping: true },
- };
-
- case "UPDATE_MAPPING":
- return {
- ...state,
- mapping: {
- ...state.mapping,
- [action.field]: action.config,
- },
- };
-
- case "SET_MAPPING_VALIDATION":
- return {
- ...state,
- validation: {
- ...state.validation,
- [action.field === "input" ? "inputMapping" : "outputMapping"]:
- action.isValid,
- },
- };
-
- case "SET_CREATE_VALIDATION":
- return {
- ...state,
- createStep: {
- canContinue: action.canContinue,
- isCreating: action.isCreating,
- },
- };
-
- case "SUBMIT_START":
- return {
- ...state,
- submission: { ...state.submission, isSubmitting: true },
- };
-
- case "SUBMIT_SUCCESS":
- return {
- ...state,
- step: "status",
- submission: {
- batchActionId: action.batchActionId,
- isSubmitting: false,
- },
- };
-
- case "SUBMIT_ERROR":
- return {
- ...state,
- submission: { ...state.submission, isSubmitting: false },
- };
-
- default:
- return state;
- }
-}
diff --git a/web/src/features/batch-actions/components/RunEvaluationDialog/index.clienttest.tsx b/web/src/features/batch-actions/components/RunEvaluationDialog/index.clienttest.tsx
new file mode 100644
index 0000000000..ba9b197bba
--- /dev/null
+++ b/web/src/features/batch-actions/components/RunEvaluationDialog/index.clienttest.tsx
@@ -0,0 +1,127 @@
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { useState } from "react";
+import { createStore } from "zustand/vanilla";
+import { vi } from "vitest";
+import { LayerProvider } from "@/src/context/LayerContext/LayerContext";
+import { RunEvaluationDialog } from "./index";
+
+const { submit, submitted } = vi.hoisted(() => ({
+ submit: vi.fn<() => Promise<{ id: string }>>(),
+ submitted: vi.fn(),
+}));
+vi.mock("@/src/utils/api", () => ({
+ sendAsPostOption: {},
+ api: {
+ v4Transition: {
+ forceV3Experience: {
+ useQuery: () => ({ data: true, isPending: false }),
+ },
+ },
+ evalsV2: {
+ options: {
+ useQuery: () => ({
+ data: [
+ {
+ id: "evaluator",
+ name: "Accuracy",
+ type: "LLM",
+ blockedAt: null,
+ latestVersion: { variableMapping: [], prompt: "Rate the answer" },
+ },
+ ],
+ }),
+ },
+ },
+ events: { batchIO: { useQuery: () => ({ data: undefined }) } },
+ batchAction: {
+ runEvaluation: {
+ create: {
+ useMutation: () => ({ mutateAsync: submit, isPending: false }),
+ },
+ },
+ },
+ },
+}));
+vi.mock("@/src/features/notifications", () => ({
+ showErrorToast: vi.fn(),
+ showSuccessToast: vi.fn(),
+}));
+vi.mock("@/src/features/posthog-analytics", () => ({
+ usePostHogClientCapture: () => vi.fn(),
+}));
+vi.mock("@/src/features/evals", () => ({
+ buildSelectedSampleObject: () => null,
+ createRuleSetupStore: () => createStore(() => ({ assignments: [] })),
+}));
+vi.mock("./EvaluatorMappingStep", () => ({
+ EvaluatorMappingStep: () => <div />,
+}));
+vi.mock("./EvaluatorSelectionStep", () => ({
+ EvaluatorSelectionStep: ({
+ onToggleEvaluator,
+ }: {
+ onToggleEvaluator: (id: string) => void;
+ }) => (
+ <button onClick={() => onToggleEvaluator("evaluator")}>
+ Select Accuracy
+ </button>
+ ),
+}));
+vi.mock("./ConfirmationStep", () => ({
+ ConfirmationStep: () => <p>Confirm evaluation</p>,
+}));
+
+function SelectionHarness() {
+ const [open, setOpen] = useState(true);
+ const [selectedIds, setSelectedIds] = useState(["first", "second"]);
+ const props = {
+ projectId: "project",
+ selectedObservationIds: selectedIds,
+ query: { filter: [], orderBy: null },
+ selectAll: false,
+ totalCount: 10,
+ onClose: () => setOpen(false),
+ onSuccess: () => {
+ submitted();
+ setSelectedIds([]);
+ },
+ };
+ return (
+ <LayerProvider>
+ <p>{selectedIds.length} rows selected</p>
+ {open && <RunEvaluationDialog {...props} />}
+ </LayerProvider>
+ );
+}
+
+describe("Run evaluation lifecycle", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ submit.mockResolvedValue({ id: "batch-action" });
+ });
+
+ it("dismisses without changing the selected rows", () => {
+ render(<SelectionHarness />);
+ fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ expect(screen.getByText("2 rows selected")).toBeInTheDocument();
+ expect(submitted).not.toHaveBeenCalled();
+ });
+
+ it("retains selection on failure and clears it once when retry succeeds", async () => {
+ submit.mockRejectedValueOnce(new Error("Temporarily unavailable"));
+ render(<SelectionHarness />);
+ fireEvent.click(screen.getByRole("button", { name: "Select Accuracy" }));
+ fireEvent.click(screen.getByRole("button", { name: /Continue with/ }));
+ fireEvent.click(screen.getByRole("button", { name: "Run Evaluation" }));
+ await waitFor(() => expect(submit).toHaveBeenCalledTimes(1));
+ expect(screen.getByText("2 rows selected")).toBeInTheDocument();
+ expect(screen.getByText("Confirm evaluation")).toBeInTheDocument();
+ expect(submitted).not.toHaveBeenCalled();
+
+ fireEvent.click(screen.getByRole("button", { name: "Run Evaluation" }));
+ await waitFor(() => expect(submitted).toHaveBeenCalledTimes(1));
+ expect(screen.getByText("0 rows selected")).toBeInTheDocument();
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ });
+});
diff --git a/web/src/features/batch-actions/components/RunEvaluationDialog/index.tsx b/web/src/features/batch-actions/components/RunEvaluationDialog/index.tsx
index 6f58e2507b..32b3cabd4e 100644
--- a/web/src/features/batch-actions/components/RunEvaluationDialog/index.tsx
+++ b/web/src/features/batch-actions/components/RunEvaluationDialog/index.tsx
@@ -53,6 +53,7 @@ type RunEvaluationDialogProps = {
selectAll: boolean;
totalCount: number;
onClose: () => void;
+ onSuccess: () => void;
experimentCount?: number;
exampleObservation?: {
id: string;
@@ -308,6 +309,7 @@ export function RunEvaluationDialog(props: RunEvaluationDialogProps) {
},
});
+ props.onSuccess();
props.onClose();
};
@@ -318,7 +320,12 @@ export function RunEvaluationDialog(props: RunEvaluationDialogProps) {
return (
<>
- <Dialog open onOpenChange={(open) => !open && props.onClose()}>
+ <Dialog
+ open
+ onOpenChange={(open) =>
+ !open && !runEvaluationMutation.isPending && props.onClose()
+ }
+ >
<DialogContent
{...(showMappingEditor ? { size: "lg" as const } : {})}
className={
diff --git a/web/src/features/datasets/README.md b/web/src/features/datasets/README.md
index c5a9695b40..0527418d84 100644
--- a/web/src/features/datasets/README.md
+++ b/web/src/features/datasets/README.md
@@ -23,6 +23,17 @@ one `ActiveCellProvider` per project and dataset.
- `components/NewDatasetItemForm.tsx` prepares initial schema examples before
mounting editable fields. Later dataset selections fill only empty, unedited
fields; metadata refetches and obsolete generation results preserve drafts.
+ Its opening source and callbacks belong to that form instance. Creating a
+ dataset keeps the item draft and appends the created dataset to its targets.
+ A single editing column owns input, expected output and metadata; the dataset
+ selector and submit footer stay visible while the fields scroll.
+- `components/submitDatasetItems.ts` owns immediate submission and its pending
+ guard. Edits, repeated submits and dialog dismissal stay blocked until the
+ request settles; errors preserve the draft. The dialog controller scopes
+ completion callbacks to the instance that submitted them.
+- `components/DatasetForm.tsx` owns dataset creation and updates, including its
+ footer and pending state. Single and bulk item creation reuse it and receive
+ pending/completion notifications directly from the submission event.
## Structure and consumers
diff --git a/web/src/features/datasets/components/DatasetForm.clienttest.tsx b/web/src/features/datasets/components/DatasetForm.clienttest.tsx
new file mode 100644
index 0000000000..c34fa768d2
--- /dev/null
+++ b/web/src/features/datasets/components/DatasetForm.clienttest.tsx
@@ -0,0 +1,126 @@
+import {
+ act,
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+} from "@testing-library/react";
+import { vi } from "vitest";
+import { DatasetForm } from "./DatasetForm";
+
+const { createDataset, updateDataset, push } = vi.hoisted(() => ({
+ createDataset: vi.fn(),
+ updateDataset: vi.fn(),
+ push: vi.fn(),
+}));
+
+vi.mock("@/src/utils/api", () => ({
+ api: {
+ datasets: {
+ allDatasetMeta: { useQuery: () => ({ data: [] }) },
+ createDataset: { useMutation: () => ({ mutateAsync: createDataset }) },
+ updateDataset: { useMutation: () => ({ mutateAsync: updateDataset }) },
+ },
+ useUtils: () => ({ datasets: { invalidate: vi.fn() } }),
+ },
+ reportNonTrpcError: vi.fn(),
+}));
+vi.mock("next/router", () => ({ useRouter: () => ({ push }) }));
+vi.mock("@/src/features/posthog-analytics", () => ({
+ usePostHogClientCapture: () => vi.fn(),
+}));
+vi.mock("@/src/hooks/useUniqueNameValidation", () => ({
+ useUniqueNameValidation: vi.fn(),
+}));
+vi.mock("@/src/components/editor", () => ({
+ CodeMirrorEditor: ({
+ id,
+ value,
+ onChange,
+ editable = true,
+ }: {
+ id?: string;
+ value: string;
+ onChange: (value: string) => void;
+ editable?: boolean;
+ }) => (
+ <textarea
+ id={id}
+ value={value}
+ readOnly={!editable}
+ onChange={(event) => onChange(event.target.value)}
+ />
+ ),
+}));
+vi.mock("./DatasetSchemaInput", () => ({ DatasetSchemaInput: () => null }));
+vi.mock("./DatasetSchemaValidationError", () => ({
+ DatasetSchemaValidationError: () => null,
+}));
+
+describe("DatasetForm submission ownership", () => {
+ beforeEach(() => vi.clearAllMocks());
+
+ it("keeps the creation draft on failure and unlocks before returning the new dataset", async () => {
+ let rejectSubmission!: (error: Error) => void;
+ createDataset.mockImplementationOnce(
+ () =>
+ new Promise((_, reject) => {
+ rejectSubmission = reject;
+ }),
+ );
+ const pending = vi.fn();
+ const cancel = vi.fn();
+ const created = vi.fn(() =>
+ expect(pending).toHaveBeenLastCalledWith(false),
+ );
+ render(
+ <DatasetForm
+ projectId="project"
+ mode="create"
+ redirectOnSuccess={false}
+ onSubmittingChange={pending}
+ onCancel={cancel}
+ onCreateDatasetSuccess={created}
+ />,
+ );
+ fireEvent.change(screen.getByLabelText("Name"), {
+ target: { value: "Review dataset" },
+ });
+ fireEvent.change(screen.getByLabelText("Metadata (optional)"), {
+ target: { value: '{"reviewed":true}' },
+ });
+ fireEvent.click(screen.getByRole("button", { name: "Create dataset" }));
+ await waitFor(() => expect(createDataset).toHaveBeenCalledTimes(1));
+ expect(pending).toHaveBeenLastCalledWith(true);
+ expect(screen.getByLabelText("Name")).toBeDisabled();
+ expect(screen.getByLabelText("Metadata (optional)")).toHaveAttribute(
+ "readonly",
+ );
+ expect(screen.getByRole("button", { name: "Back" })).toBeDisabled();
+ fireEvent.submit(screen.getByLabelText("Name").closest("form")!);
+ await act(async () => rejectSubmission(new Error("Temporary failure")));
+ expect(screen.getByRole("alert")).toHaveTextContent("Temporary failure");
+ expect(screen.getByLabelText("Name")).toHaveValue("Review dataset");
+ expect(screen.getByLabelText("Name")).not.toBeDisabled();
+ expect(createDataset).toHaveBeenCalledTimes(1);
+ expect(created).not.toHaveBeenCalled();
+ const dataset = {
+ id: "created",
+ name: "Review dataset",
+ inputSchema: null,
+ expectedOutputSchema: null,
+ };
+ createDataset.mockResolvedValueOnce({ success: true, dataset });
+ fireEvent.click(screen.getByRole("button", { name: "Create dataset" }));
+ await waitFor(() => expect(created).toHaveBeenCalledWith(dataset));
+ expect(createDataset).toHaveBeenLastCalledWith({
+ projectId: "project",
+ name: "Review dataset",
+ description: null,
+ metadata: '{"reviewed":true}',
+ inputSchema: null,
+ expectedOutputSchema: null,
+ });
+ expect(push).not.toHaveBeenCalled();
+ });
+});
diff --git a/web/src/features/datasets/components/DatasetForm.tsx b/web/src/features/datasets/components/DatasetForm.tsx
index 5445d04a45..edceb7e876 100644
--- a/web/src/features/datasets/components/DatasetForm.tsx
+++ b/web/src/features/datasets/components/DatasetForm.tsx
@@ -13,14 +13,7 @@ import {
FormMessage,
} from "@/src/components/ui/form";
import { api, reportNonTrpcError } from "@/src/utils/api";
-import {
- useMemo,
- useState,
- useEffect,
- useRef,
- forwardRef,
- useImperativeHandle,
-} from "react";
+import { useMemo, useState, useRef } from "react";
import { Input } from "@/src/components/design-system/Input/Input";
import { CodeMirrorEditor } from "@/src/components/editor";
import {
@@ -45,11 +38,6 @@ type ServerSideSchemaValidationErrors = {
}>;
}[];
-// Ref interface for imperative form submission
-export interface DatasetFormRef {
- submit: () => void;
-}
-
interface BaseDatasetFormProps {
mode: "create" | "update";
projectId: string;
@@ -63,7 +51,8 @@ interface BaseDatasetFormProps {
className?: string;
redirectOnSuccess?: boolean;
showFooter?: boolean;
- onValidationChange?: (isValid: boolean, isSubmitting: boolean) => void;
+ onSubmittingChange?: (pending: boolean) => void;
+ onCancel?: () => void;
}
interface CreateDatasetFormProps extends BaseDatasetFormProps {
@@ -124,326 +113,283 @@ const formSchema = z.object({
expectedOutputSchema: jsonSchemaStringValidator,
});
-export const DatasetForm = forwardRef<DatasetFormRef, DatasetFormProps>(
- (props, ref) => {
- const [formError, setFormError] = useState<string | null>(null);
- const [
- serverSideSchemaValidationErrors,
- setServerSideSchemaValidationErrors,
- ] = useState<ServerSideSchemaValidationErrors | null>(null);
- const capture = usePostHogClientCapture();
+export const DatasetForm = (props: DatasetFormProps) => {
+ const [pending, setPending] = useState(false);
+ const submissionPending = useRef(false);
+ const [formError, setFormError] = useState<string | null>(null);
+ const [
+ serverSideSchemaValidationErrors,
+ setServerSideSchemaValidationErrors,
+ ] = useState<ServerSideSchemaValidationErrors | null>(null);
+ const capture = usePostHogClientCapture();
- const inputSchemaString =
- props.mode === "update" && props.datasetInputSchema
- ? JSON.stringify(props.datasetInputSchema, null, 2)
- : "";
- const expectedOutputSchemaString =
- props.mode === "update" && props.datasetExpectedOutputSchema
- ? JSON.stringify(props.datasetExpectedOutputSchema, null, 2)
- : "";
+ const inputSchemaString =
+ props.mode === "update" && props.datasetInputSchema
+ ? JSON.stringify(props.datasetInputSchema, null, 2)
+ : "";
+ const expectedOutputSchemaString =
+ props.mode === "update" && props.datasetExpectedOutputSchema
+ ? JSON.stringify(props.datasetExpectedOutputSchema, null, 2)
+ : "";
- const form = useForm({
- resolver: zodResolver(formSchema),
- defaultValues:
- props.mode === "update"
- ? {
- name: props.datasetName,
- description: props.datasetDescription ?? "",
- metadata: props.datasetMetadata
- ? JSON.stringify(props.datasetMetadata, null, 2)
+ const form = useForm({
+ resolver: zodResolver(formSchema),
+ defaultValues:
+ props.mode === "update"
+ ? {
+ name: props.datasetName,
+ description: props.datasetDescription ?? "",
+ metadata: props.datasetMetadata
+ ? JSON.stringify(props.datasetMetadata, null, 2)
+ : "",
+ inputSchema: inputSchemaString,
+ expectedOutputSchema: expectedOutputSchemaString,
+ }
+ : {
+ name:
+ props.mode === "create" && props.folderPrefix
+ ? `${props.folderPrefix}/`
: "",
- inputSchema: inputSchemaString,
- expectedOutputSchema: expectedOutputSchemaString,
- }
- : {
- name:
- props.mode === "create" && props.folderPrefix
- ? `${props.folderPrefix}/`
- : "",
- description: "",
- metadata: "",
- inputSchema: "",
- expectedOutputSchema: "",
- },
- });
+ description: "",
+ metadata: "",
+ inputSchema: "",
+ expectedOutputSchema: "",
+ },
+ });
- const utils = api.useUtils();
- const router = useRouter();
- const createMutation = api.datasets.createDataset.useMutation();
- const updateMutation = api.datasets.updateDataset.useMutation();
+ const utils = api.useUtils();
+ const router = useRouter();
+ const createMutation = api.datasets.createDataset.useMutation();
+ const updateMutation = api.datasets.updateDataset.useMutation();
- const allDatasets = api.datasets.allDatasetMeta.useQuery(
- { projectId: props.projectId },
- {
- enabled: props.mode === "create" || props.mode === "update",
- },
- );
+ const allDatasets = api.datasets.allDatasetMeta.useQuery(
+ { projectId: props.projectId },
+ {
+ enabled: props.mode === "create" || props.mode === "update",
+ },
+ );
- const allDatasetNames = useMemo(() => {
- return (
- allDatasets.data?.map((dataset) => ({ value: dataset.name })) ?? []
- );
- }, [allDatasets.data]);
+ const allDatasetNames = useMemo(() => {
+ return allDatasets.data?.map((dataset) => ({ value: dataset.name })) ?? [];
+ }, [allDatasets.data]);
- useUniqueNameValidation({
- currentName: form.watch("name"),
- allNames: allDatasetNames,
- form,
- errorMessage: "Dataset name already exists.",
- whitelistedName: props.mode === "update" ? props.datasetName : undefined,
- });
+ useUniqueNameValidation({
+ currentName: form.watch("name"),
+ allNames: allDatasetNames,
+ form,
+ errorMessage: "Dataset name already exists.",
+ whitelistedName: props.mode === "update" ? props.datasetName : undefined,
+ });
- // Track previous validation state to avoid redundant callbacks
- const prevValidationRef = useRef<{
- isValid: boolean;
- isSubmitting: boolean;
- } | null>(null);
+ async function onSubmit(values: z.infer<typeof formSchema>) {
+ if (submissionPending.current) return;
+ submissionPending.current = true;
+ setPending(true);
+ props.onSubmittingChange?.(true);
+ setFormError(null);
+ setServerSideSchemaValidationErrors(null);
+ // Parse schemas if they're not empty (tRPC expects objects for DatasetJSONSchema)
+ const inputSchema =
+ values.inputSchema === "" ? null : JSON.parse(values.inputSchema);
+ const expectedOutputSchema =
+ values.expectedOutputSchema === ""
+ ? null
+ : JSON.parse(values.expectedOutputSchema);
- // Report validation state to parent (only when values actually change)
- const { onValidationChange, mode } = props;
- useEffect(() => {
- if (onValidationChange) {
- const isValid = form.formState.isValid && !form.formState.errors.name;
- const isSubmitting =
- (mode === "create" && createMutation.isPending) ||
- (mode === "update" && updateMutation.isPending);
+ const trimmedValues = {
+ name: values.name.trim(),
+ description: values.description !== "" ? values.description.trim() : null,
+ // Keep metadata as string - resolveMetadata in tRPC will parse it
+ metadata: values.metadata !== "" ? values.metadata : null,
+ inputSchema,
+ expectedOutputSchema,
+ };
- const prev = prevValidationRef.current;
- if (
- !prev ||
- prev.isValid !== isValid ||
- prev.isSubmitting !== isSubmitting
- ) {
- prevValidationRef.current = { isValid, isSubmitting };
- onValidationChange(isValid, isSubmitting);
- }
+ let succeeded = false;
+ let createdDataset:
+ | Parameters<
+ NonNullable<BaseDatasetFormProps["onCreateDatasetSuccess"]>
+ >[0]
+ | undefined;
+ try {
+ capture(
+ props.mode === "create"
+ ? "datasets:new_form_submit"
+ : "datasets:update_form_submit",
+ );
+ const result =
+ props.mode === "create"
+ ? await createMutation.mutateAsync({
+ ...trimmedValues,
+ projectId: props.projectId,
+ })
+ : await updateMutation.mutateAsync({
+ ...trimmedValues,
+ projectId: props.projectId,
+ datasetId: props.datasetId,
+ });
+ if (result.success) {
+ succeeded = true;
+ if (props.mode === "create") createdDataset = result.dataset;
+ utils.datasets.invalidate();
+ form.reset();
+ } else {
+ setServerSideSchemaValidationErrors(result.validationErrors);
}
- }, [
- form.formState.isValid,
- form.formState.errors.name,
- createMutation.isPending,
- updateMutation.isPending,
- mode,
- onValidationChange,
- ]);
-
- // Expose submit method via ref for external triggering
- useImperativeHandle(
- ref,
- () => ({
- submit: () => {
- form.handleSubmit(onSubmit)();
- },
- }),
- // eslint-disable-next-line react-hooks/exhaustive-deps
- [],
- );
-
- function onSubmit(values: z.infer<typeof formSchema>) {
- // Parse schemas if they're not empty (tRPC expects objects for DatasetJSONSchema)
- const inputSchema =
- values.inputSchema === "" ? null : JSON.parse(values.inputSchema);
- const expectedOutputSchema =
- values.expectedOutputSchema === ""
- ? null
- : JSON.parse(values.expectedOutputSchema);
-
- const trimmedValues = {
- name: values.name.trim(),
- description:
- values.description !== "" ? values.description.trim() : null,
- // Keep metadata as string - resolveMetadata in tRPC will parse it
- metadata: values.metadata !== "" ? values.metadata : null,
- inputSchema,
- expectedOutputSchema,
- };
-
- if (props.mode === "create") {
- capture("datasets:new_form_submit");
- createMutation
- .mutateAsync({
- ...trimmedValues,
- projectId: props.projectId,
- })
- .then((result) => {
- if (result.success) {
- // Success - navigate to dataset items
- utils.datasets.invalidate();
- props.onCreateDatasetSuccess?.(result.dataset);
- props.onFormSuccess?.();
- form.reset();
- if (props.redirectOnSuccess !== false) {
- router.push(
- `/project/${props.projectId}/datasets/${result.dataset.id}/items`,
- );
- }
- } else {
- // Validation failed - show errors
- setServerSideSchemaValidationErrors(result.validationErrors);
- setFormError(null);
- }
- })
- .catch((error: Error) => {
- // System error (not validation)
- setFormError(error.message);
- setServerSideSchemaValidationErrors(null);
- reportNonTrpcError(error, "datasets");
- });
- } else if (props.mode === "update") {
- capture("datasets:update_form_submit");
- updateMutation
- .mutateAsync({
- ...trimmedValues,
- projectId: props.projectId,
- datasetId: props.datasetId,
- })
- .then((result) => {
- if (result.success) {
- // Success - close dialog
- utils.datasets.invalidate();
- props.onFormSuccess?.();
- form.reset();
- } else {
- // Validation failed - show errors
- setServerSideSchemaValidationErrors(result.validationErrors);
- setFormError(null);
- }
- })
- .catch((error: Error) => {
- // System error (not validation)
- setFormError(error.message);
- setServerSideSchemaValidationErrors(null);
- reportNonTrpcError(error, "datasets");
- });
+ } catch (error) {
+ setFormError(
+ error instanceof Error
+ ? error.message
+ : "Could not save the dataset. Please try again.",
+ );
+ reportNonTrpcError(error, "datasets");
+ } finally {
+ submissionPending.current = false;
+ setPending(false);
+ props.onSubmittingChange?.(false);
+ }
+ if (succeeded) {
+ if (createdDataset) props.onCreateDatasetSuccess?.(createdDataset);
+ props.onFormSuccess?.();
+ if (createdDataset && props.redirectOnSuccess !== false) {
+ router.push(
+ `/project/${props.projectId}/datasets/${createdDataset.id}/items`,
+ );
}
}
+ }
- return (
- <Form {...form}>
- <form
- onSubmit={form.handleSubmit(onSubmit)}
- className="flex h-full min-h-0 flex-col"
- >
- <DialogBody
- className={props.showFooter === false ? "p-0" : undefined}
- >
- <div className="mb-8 space-y-6">
- <FormField
- control={form.control}
- name="name"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Name</FormLabel>
- <FormDescription>
- Use slashes &apos;/&apos; in dataset names to organize
- them into <em>folders</em>.
- </FormDescription>
- <FormControl>
- <Input {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="description"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Description (optional)</FormLabel>
- <FormControl>
- <Input {...field} />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="metadata"
- render={({ field }) => (
- <FormItem>
- <FormLabel>Metadata (optional)</FormLabel>
- <FormControl>
- <CodeMirrorEditor
- mode="json"
- value={field.value}
- onChange={(v) => {
- field.onChange(v);
- }}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormField
- control={form.control}
- name="inputSchema"
- render={({ field }) => (
- <DatasetSchemaInput
- label="Input schema"
- description="Validate dataset item inputs against a JSON Schema. All new and existing items must conform to this schema."
- value={field.value}
- onChange={field.onChange}
- initialValue={inputSchemaString}
- />
- )}
- />
- <FormField
- control={form.control}
- name="expectedOutputSchema"
- render={({ field }) => (
- <DatasetSchemaInput
- label="Expected output schema"
- description="Validate dataset item expected outputs against a JSON Schema. All new and existing items must conform to this schema."
- value={field.value}
- onChange={field.onChange}
- initialValue={expectedOutputSchemaString}
- />
- )}
- />
-
- {/* Show validation errors inline with form */}
- {serverSideSchemaValidationErrors && (
- <DatasetSchemaValidationError
- projectId={props.projectId}
- datasetId={
- props.mode === "update" ? props.datasetId : "unknown"
- }
- errors={serverSideSchemaValidationErrors}
+ return (
+ <Form {...form}>
+ <form
+ onSubmit={(event) => {
+ if (submissionPending.current) {
+ event.preventDefault();
+ return;
+ }
+ return form.handleSubmit(onSubmit)(event);
+ }}
+ className="flex h-full min-h-0 flex-col"
+ >
+ <DialogBody className={props.showFooter === false ? "p-0" : undefined}>
+ <fieldset disabled={pending} className="mb-8 min-w-0 space-y-6">
+ <FormField
+ control={form.control}
+ name="name"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Name</FormLabel>
+ <FormDescription>
+ Use slashes &apos;/&apos; in dataset names to organize them
+ into <em>folders</em>.
+ </FormDescription>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="description"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Description (optional)</FormLabel>
+ <FormControl>
+ <Input {...field} />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="metadata"
+ render={({ field }) => (
+ <FormItem>
+ <FormLabel>Metadata (optional)</FormLabel>
+ <FormControl>
+ <CodeMirrorEditor
+ mode="json"
+ editable={!pending}
+ value={field.value}
+ onChange={(v) => {
+ field.onChange(v);
+ }}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormField
+ control={form.control}
+ name="inputSchema"
+ render={({ field }) => (
+ <DatasetSchemaInput
+ disabled={pending}
+ label="Input schema"
+ description="Validate dataset item inputs against a JSON Schema. All new and existing items must conform to this schema."
+ value={field.value}
+ onChange={field.onChange}
+ initialValue={inputSchemaString}
/>
)}
- </div>
- </DialogBody>
- {props.showFooter !== false && (
- <DialogFooter>
- <div className="flex w-full flex-col gap-4">
- <Button
- type="submit"
- disabled={!!form.formState.errors.name}
- loading={
- (props.mode === "create" && createMutation.isPending) ||
- (props.mode === "update" && updateMutation.isPending)
- }
- className="w-full"
- >
- {props.mode === "create"
- ? "Create dataset"
- : "Update dataset"}
- </Button>
- {formError && (
- <p className="mt-4 text-center text-sm text-red-500">
- <span className="font-bold">Error:</span> {formError}
- </p>
- )}
- </div>
- </DialogFooter>
- )}
- </form>
- </Form>
- );
- },
-);
+ />
+ <FormField
+ control={form.control}
+ name="expectedOutputSchema"
+ render={({ field }) => (
+ <DatasetSchemaInput
+ disabled={pending}
+ label="Expected output schema"
+ description="Validate dataset item expected outputs against a JSON Schema. All new and existing items must conform to this schema."
+ value={field.value}
+ onChange={field.onChange}
+ initialValue={expectedOutputSchemaString}
+ />
+ )}
+ />
-DatasetForm.displayName = "DatasetForm";
+ {/* Show validation errors inline with form */}
+ {serverSideSchemaValidationErrors && (
+ <DatasetSchemaValidationError
+ projectId={props.projectId}
+ datasetId={
+ props.mode === "update" ? props.datasetId : "unknown"
+ }
+ errors={serverSideSchemaValidationErrors}
+ />
+ )}
+ </fieldset>
+ {formError && (
+ <p role="alert" className="text-destructive text-sm">
+ {formError}
+ </p>
+ )}
+ </DialogBody>
+ {props.showFooter !== false && (
+ <DialogFooter>
+ {props.onCancel && (
+ <Button
+ variant="ghost"
+ onClick={props.onCancel}
+ disabled={pending}
+ >
+ Back
+ </Button>
+ )}
+ <Button
+ type="submit"
+ disabled={!!form.formState.errors.name}
+ loading={pending}
+ >
+ {props.mode === "create" ? "Create dataset" : "Update dataset"}
+ </Button>
+ </DialogFooter>
+ )}
+ </form>
+ </Form>
+ );
+};
diff --git a/web/src/features/datasets/components/DatasetItemEditorLayout.tsx b/web/src/features/datasets/components/DatasetItemEditorLayout.tsx
new file mode 100644
index 0000000000..0f607e4807
--- /dev/null
+++ b/web/src/features/datasets/components/DatasetItemEditorLayout.tsx
@@ -0,0 +1,58 @@
+import { type ReactNode } from "react";
+import { JSONView } from "@/src/components/ui/CodeJsonViewer";
+
+export function DatasetItemEditorLayout({
+ selector,
+ children,
+ preview,
+ previewDescription,
+}: {
+ selector: ReactNode;
+ children: ReactNode;
+ preview: ReactNode;
+ previewDescription: string;
+}) {
+ return (
+ <div className="flex min-h-0 flex-1 flex-col">
+ <div className="shrink-0 border-b p-4">{selector}</div>
+ <div className="grid min-h-0 flex-1 overflow-y-auto md:grid-cols-2 md:overflow-hidden">
+ <div className="flex min-w-0 flex-col gap-6 p-4 md:overflow-y-auto">
+ {children}
+ </div>
+ <aside className="bg-muted/20 flex min-w-0 flex-col gap-4 border-t p-4 md:overflow-y-auto md:border-t-0 md:border-l">
+ <div className="flex flex-col gap-1">
+ <h3 className="text-sm font-bold">Dataset item preview</h3>
+ <p className="text-muted-foreground text-xs">
+ {previewDescription}
+ </p>
+ </div>
+ {preview}
+ </aside>
+ </div>
+ </div>
+ );
+}
+
+export function DatasetItemPreviewField({
+ label,
+ value,
+ feedback,
+}: {
+ label: string;
+ value: unknown;
+ feedback: ReactNode;
+}) {
+ return (
+ <section className="ph-no-capture bg-background flex flex-col overflow-hidden rounded-md border">
+ <h4 className="border-b px-3 py-2 text-xs font-bold">{label}</h4>
+ <div className="max-h-64 overflow-auto">
+ {value === undefined ? (
+ <p className="text-muted-foreground p-3 text-xs">No value</p>
+ ) : (
+ <JSONView json={value} className="text-xs" />
+ )}
+ </div>
+ {feedback}
+ </section>
+ );
+}
diff --git a/web/src/features/datasets/components/DatasetItemMediaAttachments.tsx b/web/src/features/datasets/components/DatasetItemMediaAttachments.tsx
index 6353abb73c..bd64794391 100644
--- a/web/src/features/datasets/components/DatasetItemMediaAttachments.tsx
+++ b/web/src/features/datasets/components/DatasetItemMediaAttachments.tsx
@@ -246,15 +246,20 @@ function CopyFieldValueButton({ value }: { value: string }) {
export function DatasetItemFieldToolbar({
copyValue,
onSelectFile,
+ disabled,
}: {
copyValue: string;
onSelectFile?: (file: File) => void | Promise<void>;
+ disabled?: boolean;
}) {
return (
<div className="ml-auto flex items-center gap-0.5">
<CopyFieldValueButton value={copyValue} />
{onSelectFile && (
- <DatasetItemMediaUploadButton onSelectFile={onSelectFile} />
+ <DatasetItemMediaUploadButton
+ onSelectFile={onSelectFile}
+ disabled={disabled}
+ />
)}
</div>
);
diff --git a/web/src/features/datasets/components/NewDatasetItemButton.tsx b/web/src/features/datasets/components/NewDatasetItemButton.tsx
index 498227cc88..f3dd842061 100644
--- a/web/src/features/datasets/components/NewDatasetItemButton.tsx
+++ b/web/src/features/datasets/components/NewDatasetItemButton.tsx
@@ -5,7 +5,7 @@ import {
DialogHeader,
DialogTitle,
} from "@/src/components/ui/dialog";
-import { useState } from "react";
+import { useRef, useState } from "react";
import { NewDatasetItemForm } from "@/src/features/datasets/components/NewDatasetItemForm";
import { DialogTrigger } from "@radix-ui/react-dialog";
import { useHasProjectAccess } from "@/src/features/rbac";
@@ -16,12 +16,18 @@ export const NewDatasetItemButton = (props: {
datasetId?: string;
}) => {
const [open, setOpen] = useState(false);
+ const submissionPending = useRef(false);
const hasAccess = useHasProjectAccess({
projectId: props.projectId,
scope: "datasets:CUD",
});
return (
- <Dialog open={hasAccess && open} onOpenChange={setOpen}>
+ <Dialog
+ open={hasAccess && open}
+ onOpenChange={(next) => {
+ if (!submissionPending.current) setOpen(next);
+ }}
+ >
<DialogTrigger asChild>
<ActionButton
variant="outline"
@@ -32,13 +38,16 @@ export const NewDatasetItemButton = (props: {
New item
</ActionButton>
</DialogTrigger>
- <DialogContent size="xl">
+ <DialogContent size="lg">
<DialogHeader>
<DialogTitle>Create new dataset item</DialogTitle>
</DialogHeader>
<NewDatasetItemForm
projectId={props.projectId}
datasetId={props.datasetId}
+ onPendingChange={(pending) => {
+ submissionPending.current = pending;
+ }}
onFormSuccess={() => setOpen(false)}
className="h-full overflow-y-auto"
/>
diff --git a/web/src/features/datasets/components/NewDatasetItemForm.clienttest.tsx b/web/src/features/datasets/components/NewDatasetItemForm.clienttest.tsx
new file mode 100644
index 0000000000..ac580a5891
--- /dev/null
+++ b/web/src/features/datasets/components/NewDatasetItemForm.clienttest.tsx
@@ -0,0 +1,411 @@
+import {
+ act,
+ fireEvent,
+ render,
+ screen,
+ waitFor,
+} from "@testing-library/react";
+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
+import { type ComponentProps, type ReactNode } from "react";
+import { vi } from "vitest";
+
+import { NewDatasetItemForm } from "./NewDatasetItemForm";
+import { NewDatasetItemFromExistingObjectDialogController } from "./NewDatasetItemFromExistingObjectDialogController";
+
+const { query, generateExample, createItems } = vi.hoisted(() => ({
+ query: {
+ data: undefined as
+ | {
+ id: string;
+ name: string;
+ inputSchema: { type: string; default: string };
+ expectedOutputSchema: { type: string; default: string };
+ }[]
+ | undefined,
+ isPending: false,
+ isError: false,
+ refetch: vi.fn(),
+ },
+ generateExample: vi.fn<(schema: { default: string }) => Promise<string>>(),
+ createItems: vi.fn(),
+}));
+
+vi.mock("@/src/utils/api", () => ({
+ api: {
+ datasets: {
+ allDatasetMeta: { useQuery: () => query },
+ createManyDatasetItems: {
+ useMutation: () => ({ mutateAsync: createItems, isPending: false }),
+ },
+ },
+ useUtils: () => ({ datasets: { invalidate: vi.fn() } }),
+ },
+ reportTrpcErrorWithoutToast: vi.fn(),
+}));
+vi.mock("../lib/generateSchemaExample", () => ({
+ generateSchemaExample: generateExample,
+}));
+vi.mock("@/src/features/posthog-analytics", () => ({
+ usePostHogClientCapture: () => vi.fn(),
+}));
+vi.mock("@/src/components/editor", () => ({
+ CodeMirrorEditor: ({
+ id,
+ value,
+ onChange,
+ editable = true,
+ }: {
+ id?: string;
+ value: string;
+ onChange: (value: string) => void;
+ editable?: boolean;
+ }) => (
+ <textarea
+ id={id}
+ value={value}
+ readOnly={!editable}
+ onChange={(event) => onChange(event.target.value)}
+ />
+ ),
+}));
+vi.mock("@/src/components/editor/mediaTagWidget", () => ({
+ useMediaTagChips: () => ({ extension: [], portals: null }),
+}));
+vi.mock("../hooks/useDatasetItemMediaUpload", () => ({
+ useDatasetItemMediaUpload: () => ({
+ uploadFile: vi.fn(),
+ pendingUploads: [],
+ }),
+}));
+vi.mock("./DatasetItemMediaAttachments", () => ({
+ createMediaDropPasteExtension: () => [],
+ DatasetItemFieldToolbar: () => <span />,
+ DatasetItemFormMediaAttachments: () => <span />,
+ insertMediaReferenceAtCursor: vi.fn(),
+}));
+vi.mock("./DatasetSchemaHoverCard", () => ({
+ DatasetSchemaHoverCard: () => <span />,
+}));
+vi.mock(
+ "@/src/components/design-system/MultiSelectTagInput/MultiSelectTagInput",
+ () => ({
+ MultiSelectTagInput: ({
+ value,
+ options,
+ onValueChange,
+ disabled,
+ }: {
+ value: string[];
+ options: { value: string; label: string }[];
+ onValueChange: (value: string[]) => void;
+ disabled?: boolean;
+ }) => (
+ <select
+ aria-label="Target datasets"
+ multiple
+ disabled={disabled}
+ value={value}
+ onChange={(event) =>
+ onValueChange(
+ Array.from(
+ event.target.selectedOptions,
+ (option) => option.value,
+ ).filter(Boolean),
+ )
+ }
+ >
+ <option value="">Choose dataset</option>
+ {options.map((option) => (
+ <option key={option.value} value={option.value}>
+ {option.label}
+ </option>
+ ))}
+ </select>
+ ),
+ }),
+);
+
+vi.mock("./DatasetForm", () => ({
+ DatasetForm: ({
+ onCreateDatasetSuccess,
+ onCancel,
+ }: {
+ onCreateDatasetSuccess: (dataset: unknown) => void;
+ onCancel?: () => void;
+ }) => (
+ <div>
+ <button type="button" onClick={onCancel}>
+ Back to item
+ </button>
+ <button
+ type="button"
+ onClick={() =>
+ onCreateDatasetSuccess({
+ id: "created",
+ name: "Created dataset",
+ inputSchema: null,
+ expectedOutputSchema: null,
+ })
+ }
+ >
+ Finish creating dataset
+ </button>
+ </div>
+ ),
+}));
+
+const datasets = ["a", "b"].map((id) => ({
+ id,
+ name: `Dataset ${id}`,
+ inputSchema: { type: "string", default: `${id} input` },
+ expectedOutputSchema: { type: "string", default: `${id} output` },
+}));
+
+function renderForm(
+ props: Partial<ComponentProps<typeof NewDatasetItemForm>> = {},
+) {
+ const client = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ const wrapper = ({ children }: { children: ReactNode }) => (
+ <QueryClientProvider client={client}>{children}</QueryClientProvider>
+ );
+ return render(<NewDatasetItemForm projectId="project" {...props} />, {
+ wrapper,
+ });
+}
+
+describe("NewDatasetItemForm schema defaults", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ query.data = datasets;
+ query.isPending = false;
+ query.isError = false;
+ generateExample.mockImplementation(async (schema) =>
+ JSON.stringify(schema.default),
+ );
+ createItems.mockResolvedValue({ success: true });
+ });
+
+ it("keeps deliberately cleared fields empty when dataset metadata refetches", async () => {
+ const { rerender } = renderForm({ datasetId: "a" });
+ const input = await screen.findByLabelText("Input");
+ const output = screen.getByLabelText("Expected output");
+ await waitFor(() => expect(input).toHaveValue('"a input"'));
+ await waitFor(() => expect(output).toHaveValue('"a output"'));
+
+ fireEvent.change(input, { target: { value: "" } });
+ fireEvent.change(output, { target: { value: "" } });
+ query.data = datasets.map((dataset) => ({ ...dataset }));
+ await act(async () =>
+ rerender(<NewDatasetItemForm projectId="project" datasetId="a" />),
+ );
+
+ expect(screen.getByLabelText("Input")).toBe(input);
+ expect(input).toHaveValue("");
+ expect(output).toHaveValue("");
+
+ await act(async () =>
+ fireEvent.change(screen.getByLabelText("Target datasets"), {
+ target: { value: "b" },
+ }),
+ );
+ expect(input).toHaveValue("");
+ expect(output).toHaveValue("");
+ });
+
+ it("does not overwrite a field edited and cleared while an example is generating", async () => {
+ let resolveInput!: (value: string) => void;
+ generateExample.mockImplementation((schema) =>
+ schema.default === "a input"
+ ? new Promise((resolve) => {
+ resolveInput = resolve;
+ })
+ : Promise.resolve(JSON.stringify(schema.default)),
+ );
+ renderForm();
+ const input = await screen.findByLabelText("Input");
+ fireEvent.change(screen.getByLabelText("Target datasets"), {
+ target: { value: "a" },
+ });
+ fireEvent.change(input, { target: { value: '"draft"' } });
+ fireEvent.change(input, { target: { value: "" } });
+ await act(async () => resolveInput('"a input"'));
+
+ expect(input).toHaveValue("");
+ expect(screen.getByLabelText("Expected output")).toHaveValue('"a output"');
+ });
+
+ it("autofills a newly selected dataset and ignores an obsolete selection", async () => {
+ let resolveInput!: (value: string) => void;
+ generateExample.mockImplementation((schema) =>
+ schema.default.startsWith("a")
+ ? new Promise((resolve) => {
+ if (schema.default === "a input") resolveInput = resolve;
+ })
+ : Promise.resolve(JSON.stringify(schema.default)),
+ );
+ renderForm();
+ await screen.findByLabelText("Input");
+ const picker = screen.getByLabelText("Target datasets");
+ fireEvent.change(picker, { target: { value: "a" } });
+ fireEvent.change(picker, { target: { value: "b" } });
+ await waitFor(() =>
+ expect(screen.getByLabelText("Input")).toHaveValue('"b input"'),
+ );
+ await act(async () => resolveInput('"a input"'));
+
+ expect(screen.getByLabelText("Input")).toHaveValue('"b input"');
+ expect(screen.getByLabelText("Expected output")).toHaveValue('"b output"');
+ });
+
+ it("mounts the form only after the initially selected dataset examples are ready", async () => {
+ query.data = undefined;
+ query.isPending = true;
+ let resolveInput!: (value: string) => void;
+ generateExample.mockImplementation((schema) =>
+ schema.default === "a input"
+ ? new Promise((resolve) => {
+ resolveInput = resolve;
+ })
+ : Promise.resolve(JSON.stringify(schema.default)),
+ );
+ const { rerender } = renderForm({ datasetId: "a" });
+ expect(screen.queryByLabelText("Input")).not.toBeInTheDocument();
+ query.data = datasets;
+ query.isPending = false;
+ rerender(<NewDatasetItemForm projectId="project" datasetId="a" />);
+ expect(screen.queryByLabelText("Input")).not.toBeInTheDocument();
+ await waitFor(() => expect(generateExample).toHaveBeenCalledTimes(2));
+ await act(async () => resolveInput('"a input"'));
+
+ expect(await screen.findByLabelText("Input")).toHaveValue('"a input"');
+ expect(screen.getByLabelText("Expected output")).toHaveValue('"a output"');
+ });
+
+ it("keeps editable source values and provenance when adding to multiple datasets", async () => {
+ renderForm({
+ datasetId: "a",
+ traceId: "trace",
+ observationId: "observation",
+ input: "source input",
+ output: "source output",
+ });
+ expect(await screen.findByLabelText("Input")).toHaveValue('"source input"');
+ const selector = screen.getByLabelText(
+ "Target datasets",
+ ) as HTMLSelectElement;
+ selector.options[2].selected = true;
+ fireEvent.change(selector);
+ fireEvent.change(screen.getByLabelText("Expected output"), {
+ target: { value: '"Reviewed answer"' },
+ });
+ fireEvent.change(screen.getByLabelText("Metadata"), {
+ target: { value: '{"reviewed":true}' },
+ });
+ expect(generateExample).not.toHaveBeenCalled();
+ fireEvent.click(screen.getByRole("button", { name: "Add to 2 datasets" }));
+
+ await waitFor(() =>
+ expect(createItems).toHaveBeenCalledWith({
+ projectId: "project",
+ items: ["a", "b"].map((datasetId) =>
+ expect.objectContaining({
+ datasetId,
+ sourceTraceId: "trace",
+ sourceObservationId: "observation",
+ input: '"source input"',
+ expectedOutput: '"Reviewed answer"',
+ metadata: '{"reviewed":true}',
+ }),
+ ),
+ }),
+ );
+ });
+
+ it("returns from dataset creation with the item draft and adds the new target", async () => {
+ renderForm({ datasetId: "a", input: "source", output: "answer" });
+ fireEvent.change(await screen.findByLabelText("Input"), {
+ target: { value: '"reviewed draft"' },
+ });
+ fireEvent.click(screen.getByRole("button", { name: "Create dataset" }));
+ fireEvent.click(screen.getByRole("button", { name: "Back to item" }));
+ expect(screen.getByLabelText("Input")).toHaveValue('"reviewed draft"');
+ fireEvent.click(screen.getByRole("button", { name: "Create dataset" }));
+ fireEvent.click(
+ screen.getByRole("button", { name: "Finish creating dataset" }),
+ );
+ expect(screen.getByLabelText("Input")).toHaveValue('"reviewed draft"');
+ expect(screen.getByLabelText("Target datasets")).toHaveValue([
+ "a",
+ "created",
+ ]);
+ fireEvent.click(screen.getByRole("button", { name: "Add to 2 datasets" }));
+ await waitFor(() =>
+ expect(createItems).toHaveBeenCalledWith(
+ expect.objectContaining({
+ items: ["a", "created"].map((datasetId) =>
+ expect.objectContaining({ datasetId, input: '"reviewed draft"' }),
+ ),
+ }),
+ ),
+ );
+ });
+
+ it("keeps the submitting item open and immutable, then preserves its draft on failure", async () => {
+ let rejectSubmission!: (error: Error) => void;
+ createItems.mockImplementationOnce(
+ () =>
+ new Promise((_, reject) => {
+ rejectSubmission = reject;
+ }),
+ );
+ const client = new QueryClient({
+ defaultOptions: { queries: { retry: false } },
+ });
+ render(
+ <QueryClientProvider client={client}>
+ <NewDatasetItemFromExistingObjectDialogController projectId="project">
+ {({ openDialog }) => (
+ <button
+ onClick={() =>
+ openDialog({
+ traceId: "trace",
+ observationId: "observation",
+ input: "source",
+ output: "answer",
+ metadata: null,
+ })
+ }
+ >
+ Open dataset item
+ </button>
+ )}
+ </NewDatasetItemFromExistingObjectDialogController>
+ </QueryClientProvider>,
+ );
+ fireEvent.click(screen.getByRole("button", { name: "Open dataset item" }));
+ const input = await screen.findByLabelText("Input");
+ fireEvent.change(screen.getByLabelText("Target datasets"), {
+ target: { value: "a" },
+ });
+ fireEvent.change(input, { target: { value: '"draft"' } });
+ fireEvent.click(screen.getByRole("button", { name: "Add to dataset" }));
+ await waitFor(() => expect(createItems).toHaveBeenCalledTimes(1));
+ expect(input).toHaveAttribute("readonly");
+ expect(screen.getByLabelText("Target datasets")).toBeDisabled();
+ fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
+ expect(screen.getByRole("dialog")).toBeInTheDocument();
+ fireEvent.submit(input.closest("form")!);
+ expect(createItems).toHaveBeenCalledTimes(1);
+ await act(async () => rejectSubmission(new Error("Temporary failure")));
+ expect(input).not.toHaveAttribute("readonly");
+ expect(input).toHaveValue('"draft"');
+ expect(screen.getByLabelText("Target datasets")).not.toBeDisabled();
+ fireEvent.click(screen.getByRole("button", { name: "Add to dataset" }));
+ await waitFor(() =>
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument(),
+ );
+ expect(createItems).toHaveBeenCalledTimes(2);
+ });
+});
diff --git a/web/src/features/datasets/components/NewDatasetItemForm.tsx b/web/src/features/datasets/components/NewDatasetItemForm.tsx
index e6ff66c50f..d4605b9e83 100644
--- a/web/src/features/datasets/components/NewDatasetItemForm.tsx
+++ b/web/src/features/datasets/components/NewDatasetItemForm.tsx
@@ -1,5 +1,6 @@
/* eslint-disable @repo/no-style-props, @repo/no-null-render */
import { Button } from "@/src/components/ui/button";
+import { Button as DesignSystemButton } from "@/src/components/design-system/Button/Button";
import * as z from "zod";
import { safeRandomUUID } from "@/src/utils/safe-random-uuid";
import { zodResolver } from "@hookform/resolvers/zod";
@@ -12,15 +13,18 @@ import {
FormLabel,
FormMessage,
} from "@/src/components/ui/form";
-import { api, reportTrpcErrorWithoutToast } from "@/src/utils/api";
+import { api } from "@/src/utils/api";
+import { Plus } from "lucide-react";
import {
useState,
useMemo,
- useEffect,
+ useId,
useRef,
useCallback,
type RefObject,
} from "react";
+import { useQuery } from "@tanstack/react-query";
+import { Skeleton } from "@/src/components/ui/skeleton";
import { showErrorToast } from "@/src/features/notifications";
import { type ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { CodeMirrorEditor } from "@/src/components/editor";
@@ -43,12 +47,13 @@ import {
import { DatasetItemFieldSchemaErrors } from "./DatasetItemFieldSchemaErrors";
import { generateSchemaExample } from "../lib/generateSchemaExample";
import { DialogBody, DialogFooter } from "@/src/components/ui/dialog";
-import { MultiSelectInput } from "@/src/components/design-system/MultiSelectInput/MultiSelectInput";
-import { Badge } from "@/src/components/ui/badge";
+import { MultiSelectTagInput } from "@/src/components/design-system/MultiSelectTagInput/MultiSelectTagInput";
import {
isValidDatasetJson,
parseDatasetJson,
} from "../utils/parseDatasetJson";
+import { DatasetForm } from "./DatasetForm";
+import { submitDatasetItems } from "./submitDatasetItems";
const formSchema = z.object({
datasetIds: z.array(z.string()).min(1, "Select at least one dataset"),
@@ -106,7 +111,7 @@ const formatJsonValue = (value: Prisma.JsonValue | undefined): string => {
return JSON.stringify(value, null, 2);
};
-export const NewDatasetItemForm = (props: {
+type NewDatasetItemFormProps = {
projectId: string;
traceId?: string;
observationId?: string;
@@ -116,19 +121,130 @@ export const NewDatasetItemForm = (props: {
datasetId?: string;
className?: string;
onFormSuccess?: () => void;
+ onPendingChange?: (pending: boolean) => void;
currentDatasetId?: string;
-}) => {
+};
+
+const schemaFields = ["input", "expectedOutput"] as const;
+type SchemaField = (typeof schemaFields)[number];
+
+function hasSourceValues(props: NewDatasetItemFormProps) {
+ return Boolean(props.input || props.output || props.metadata);
+}
+
+async function fillEmptySchemaFields({
+ dataset,
+ canFill,
+ fill,
+}: {
+ dataset: DatasetWithSchema;
+ canFill: (field: SchemaField) => boolean;
+ fill: (field: SchemaField, value: string) => void;
+}) {
+ await Promise.all(
+ schemaFields.map(async (field) => {
+ const schema =
+ field === "input" ? dataset.inputSchema : dataset.expectedOutputSchema;
+ if (!schema || !canFill(field)) return;
+ const example = await generateSchemaExample(schema);
+ if (example && canFill(field)) fill(field, example);
+ }),
+ );
+}
+
+async function prepareInitialValues(
+ props: NewDatasetItemFormProps,
+ datasets: DatasetWithSchema[],
+): Promise<NewDatasetItemFormValues> {
+ const values = {
+ datasetIds: props.datasetId ? [props.datasetId] : [],
+ input: formatJsonValue(props.input),
+ expectedOutput: formatJsonValue(props.output),
+ metadata: formatJsonValue(props.metadata),
+ };
+ const dataset = datasets.find(({ id }) => id === props.datasetId);
+ if (dataset && !hasSourceValues(props)) {
+ await fillEmptySchemaFields({
+ dataset,
+ canFill: (field) => !values[field],
+ fill: (field, value) => {
+ values[field] = value;
+ },
+ });
+ }
+ return values;
+}
+
+export function NewDatasetItemForm(props: NewDatasetItemFormProps) {
+ const [source] = useState(props);
+ const formId = useId();
+ const datasets = api.datasets.allDatasetMeta.useQuery({
+ projectId: source.projectId,
+ });
+ // Initial examples belong to this form instance. Metadata refetches must not
+ // seed its editable values again, including values the user has cleared.
+ const initialValues = useQuery({
+ queryKey: ["dataset-item-form-defaults", formId],
+ queryFn: () => prepareInitialValues(source, datasets.data ?? []),
+ enabled: datasets.data !== undefined,
+ staleTime: Infinity,
+ gcTime: 0,
+ });
+
+ if (datasets.isError && !datasets.data) {
+ return (
+ <div className="flex flex-col items-start gap-2">
+ <p>Datasets could not be loaded.</p>
+ <Button
+ type="button"
+ variant="outline"
+ onClick={() => datasets.refetch()}
+ >
+ Try again
+ </Button>
+ </div>
+ );
+ }
+ if (!initialValues.data) return <Skeleton className="h-72 w-full" />;
+
+ return (
+ <InitializedNewDatasetItemForm
+ {...source}
+ initialValues={initialValues.data}
+ datasets={datasets.data ?? []}
+ />
+ );
+}
+
+function InitializedNewDatasetItemForm({
+ initialValues,
+ datasets: queriedDatasets,
+ ...props
+}: NewDatasetItemFormProps & {
+ initialValues: NewDatasetItemFormValues;
+ datasets: DatasetWithSchema[];
+}) {
const [formError, setFormError] = useState<string | null>(null);
+ const [screen, setScreen] = useState<"item" | "create">("item");
+ const [createdDatasets, setCreatedDatasets] = useState<DatasetWithSchema[]>(
+ [],
+ );
+ const datasets = [
+ ...queriedDatasets,
+ ...createdDatasets.filter(
+ (created) =>
+ !queriedDatasets.some((dataset) => dataset.id === created.id),
+ ),
+ ];
+ const [isPending, setPending] = useState(false);
+ const submissionOwner = useRef({ pending: false });
const capture = usePostHogClientCapture();
const form = useForm({
resolver: zodResolver(formSchema),
- defaultValues: {
- datasetIds: props.datasetId ? [props.datasetId] : [],
- input: formatJsonValue(props.input),
- expectedOutput: formatJsonValue(props.output),
- metadata: formatJsonValue(props.metadata),
- },
+ defaultValues: initialValues,
});
+ const editedFields = useRef(new Set<SchemaField>());
+ const selectionVersion = useRef(0);
// Only `datasetIds` is watched at the form level: it changes on dataset
// selection (rare), not per keystroke. The input/expectedOutput/metadata
@@ -162,6 +278,7 @@ export const NewDatasetItemForm = (props: {
const uploadMedia = useCallback(
async (file: File): Promise<string | null> => {
+ if (submissionOwner.current.pending) return null;
if (!uploadDatasetId) {
showErrorToast(
"Select a dataset first",
@@ -199,9 +316,7 @@ export const NewDatasetItemForm = (props: {
[mediaChipExtension],
);
- const hasInitialValues = Boolean(
- props.input || props.output || props.metadata,
- );
+ const hasInitialValues = hasSourceValues(props);
// Track if fields have been touched or modified
const { touchedFields, dirtyFields } = form.formState;
@@ -209,82 +324,37 @@ export const NewDatasetItemForm = (props: {
const hasInteractedWithExpectedOutput =
touchedFields.expectedOutput || dirtyFields.expectedOutput;
- const datasets = api.datasets.allDatasetMeta.useQuery({
- projectId: props.projectId,
- });
-
- // Get selected datasets with their schemas
- const selectedDatasets = useMemo(() => {
- if (!datasets.data) return [];
- return datasets.data.filter((d) => selectedDatasetIds.includes(d.id));
- }, [datasets.data, selectedDatasetIds]);
+ const selectedDatasets = datasets.filter((dataset) =>
+ selectedDatasetIds.includes(dataset.id),
+ );
// Check if any selected dataset has schemas
const hasInputSchema = selectedDatasets.some((d) => d.inputSchema);
const hasOutputSchema = selectedDatasets.some((d) => d.expectedOutputSchema);
- // Generate placeholders from schema when dataset is selected
- useEffect(() => {
- // Only generate if form has no initial values
- if (hasInitialValues) return;
-
- // Only generate if single dataset selected
- if (selectedDatasets.length !== 1) return;
-
- const dataset = selectedDatasets[0];
+ function selectDatasets(datasetIds: string[]) {
+ const version = ++selectionVersion.current;
+ if (hasInitialValues || datasetIds.length !== 1) return;
+ const dataset = datasets.find(({ id }) => id === datasetIds[0]);
if (!dataset) return;
-
- let cancelled = false;
-
- // Generate input placeholder if schema exists and field is empty
- if (dataset.inputSchema && !form.getValues("input")) {
- generateSchemaExample(dataset.inputSchema).then((placeholder) => {
- if (!cancelled && placeholder && !form.getValues("input")) {
- form.setValue("input", placeholder, {
- shouldValidate: false,
- shouldDirty: false,
- shouldTouch: false,
- });
- }
- });
- }
-
- // Generate expectedOutput placeholder if schema exists and field is empty
- if (dataset.expectedOutputSchema && !form.getValues("expectedOutput")) {
- generateSchemaExample(dataset.expectedOutputSchema).then(
- (placeholder) => {
- if (!cancelled && placeholder && !form.getValues("expectedOutput")) {
- form.setValue("expectedOutput", placeholder, {
- shouldValidate: false,
- shouldDirty: false,
- shouldTouch: false,
- });
- }
- },
- );
- }
-
- return () => {
- cancelled = true;
- };
- }, [selectedDatasets, hasInitialValues, form]);
+ return fillEmptySchemaFields({
+ dataset,
+ canFill: (field) =>
+ version === selectionVersion.current &&
+ !editedFields.current.has(field) &&
+ !form.getValues(field),
+ fill: (field, value) => form.setValue(field, value),
+ });
+ }
const utils = api.useUtils();
const createManyDatasetItemsMutation =
api.datasets.createManyDatasetItems.useMutation({
onSuccess: () => utils.datasets.invalidate(),
- onError: (error) => {
- if (error.message.includes("Body exc")) {
- setFormError(
- "Data exceeds maximum size (4.5MB). Please attempt to create dataset item programmatically.",
- );
- } else {
- setFormError(error.message);
- }
- },
});
function onSubmit(values: z.infer<typeof formSchema>) {
+ if (submissionOwner.current.pending || pendingUploads.length) return;
if (props.traceId) {
capture("dataset_item:new_from_trace_form_submit", {
object: props.observationId ? "observation" : "trace",
@@ -293,8 +363,9 @@ export const NewDatasetItemForm = (props: {
capture("dataset_item:new_form_submit");
}
- createManyDatasetItemsMutation
- .mutateAsync({
+ return submitDatasetItems({
+ owner: submissionOwner.current,
+ input: {
projectId: props.projectId,
items: values.datasetIds.map((datasetId) => ({
id: getDatasetItemId(datasetId),
@@ -305,83 +376,107 @@ export const NewDatasetItemForm = (props: {
sourceTraceId: props.traceId,
sourceObservationId: props.observationId,
})),
- })
- .then((result) => {
- if (result.success) {
- props.onFormSuccess?.();
- form.reset();
-
- return;
- }
-
- // The user already sees the validation errors via setFormError above;
- // a bare console.error(object) would only add an opaque, non-actionable
- // Sentry capture (captureConsoleIntegration), so we omit it here.
- setFormError(
- `Item does not match dataset schema. Errors: ${JSON.stringify(result.validationErrors, null, 2)}`,
- );
- })
- .catch((error) => reportTrpcErrorWithoutToast(error, "datasets"));
+ },
+ submit: createManyDatasetItemsMutation.mutateAsync,
+ onPendingChange: (pending) => {
+ setPending(pending);
+ props.onPendingChange?.(pending);
+ },
+ onSuccess: () => {
+ selectionVersion.current += 1;
+ editedFields.current.clear();
+ form.reset();
+ props.onFormSuccess?.();
+ },
+ onError: setFormError,
+ });
+ }
+
+ if (screen === "create") {
+ return (
+ <DatasetForm
+ projectId={props.projectId}
+ mode="create"
+ redirectOnSuccess={false}
+ onSubmittingChange={props.onPendingChange}
+ onCancel={() => setScreen("item")}
+ onCreateDatasetSuccess={(dataset) => {
+ const created = {
+ ...dataset,
+ inputSchema: dataset.inputSchema as Prisma.JsonValue | null,
+ expectedOutputSchema:
+ dataset.expectedOutputSchema as Prisma.JsonValue | null,
+ };
+ setCreatedDatasets((current) => [...current, created]);
+ const ids = [
+ ...new Set([...form.getValues("datasetIds"), dataset.id]),
+ ];
+ form.setValue("datasetIds", ids, { shouldValidate: true });
+ selectionVersion.current += 1;
+ setScreen("item");
+ }}
+ />
+ );
}
return (
<Form {...form}>
<form
- onSubmit={form.handleSubmit(onSubmit)}
- className={cn("flex h-full flex-col gap-6", props.className)}
+ onSubmit={(event) => {
+ if (submissionOwner.current.pending) {
+ event.preventDefault();
+ return;
+ }
+ return form.handleSubmit(onSubmit)(event);
+ }}
+ className={cn("flex h-full min-h-0 flex-col", props.className)}
>
- <DialogBody className="grid grid-rows-[auto_1fr]">
- <div className="flex-none">
- <FormField
- control={form.control}
- name="datasetIds"
- render={({ field }) => (
- <FormItem className="flex flex-col">
- <FormLabel>Target datasets</FormLabel>
- <FormControl>
- <MultiSelectInput
- value={field.value}
- options={(datasets.data ?? []).map((dataset) => ({
- value: dataset.id,
- label: dataset.name,
- secondaryLabel:
- dataset.id === props.currentDatasetId
- ? "(current)"
- : undefined,
- }))}
- onValueChange={field.onChange}
- placeholder="Select datasets"
- selectedLabel={`${field.value.length} dataset${field.value.length > 1 ? "s" : ""} selected`}
- searchPlaceholder="Search datasets..."
- emptyMessage="No datasets found."
- />
- </FormControl>
- {field.value.length > 0 && (
- <div className="mt-2 flex flex-wrap gap-1">
- {field.value.map((datasetId) => {
- const dataset = datasets.data?.find(
- (item) => item.id === datasetId,
- );
-
- return (
- <Badge
- key={datasetId}
- variant="secondary"
- className="mr-1 mb-1"
- >
- {dataset?.name ?? datasetId}
- </Badge>
- );
- })}
- </div>
+ <DialogBody className="min-h-0 overflow-hidden p-0">
+ <div className="flex min-h-0 flex-1 flex-col">
+ <div className="shrink-0 border-b p-6">
+ <div className="flex flex-col gap-3 sm:flex-row sm:items-end">
+ <FormField
+ control={form.control}
+ name="datasetIds"
+ render={({ field }) => (
+ <FormItem className="flex min-w-0 flex-1 flex-col">
+ <FormLabel>Target datasets</FormLabel>
+ <FormControl>
+ <MultiSelectTagInput
+ aria-label="Target datasets"
+ disabled={isPending}
+ value={field.value}
+ options={datasets.map((dataset) => ({
+ value: dataset.id,
+ label: dataset.name,
+ secondaryLabel:
+ dataset.id === props.currentDatasetId
+ ? "(current)"
+ : undefined,
+ }))}
+ onValueChange={(datasetIds) => {
+ field.onChange(datasetIds);
+ selectDatasets(datasetIds);
+ }}
+ placeholder="Select datasets"
+ searchPlaceholder="Search datasets..."
+ emptyMessage="No datasets found."
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
)}
- <FormMessage />
- </FormItem>
- )}
- />
- </div>
- <div className="min-h-0 flex-1 space-y-4 overflow-y-auto">
- <div className="grid gap-4 md:grid-cols-2">
+ />
+ <DesignSystemButton
+ text="Create dataset"
+ icon={Plus}
+ variant="secondary"
+ disabled={isPending || pendingUploads.length > 0}
+ onClick={() => setScreen("create")}
+ />
+ </div>
+ </div>
+ <div className="flex min-h-0 flex-1 flex-col gap-6 overflow-y-auto p-6">
<FormField
control={form.control}
name="input"
@@ -402,16 +497,21 @@ export const NewDatasetItemForm = (props: {
))[0]}
<DatasetItemFieldToolbar
copyValue={field.value}
+ disabled={isPending}
onSelectFile={handleFileUpload(inputEditorRef)}
/>
</div>
<FormControl>
<CodeMirrorEditor
mode="json"
+ editable={!isPending}
value={field.value}
- onChange={field.onChange}
+ onChange={(value) => {
+ editedFields.current.add("input");
+ field.onChange(value);
+ }}
editorRef={inputEditorRef}
- minHeight={200}
+ minHeight={140}
extensions={mediaDropPasteExtensions}
placeholder={`{
"question": "What is the capital of England?"
@@ -448,16 +548,21 @@ export const NewDatasetItemForm = (props: {
))[0]}
<DatasetItemFieldToolbar
copyValue={field.value}
+ disabled={isPending}
onSelectFile={handleFileUpload(expectedOutputEditorRef)}
/>
</div>
<FormControl>
<CodeMirrorEditor
mode="json"
+ editable={!isPending}
value={field.value}
- onChange={field.onChange}
+ onChange={(value) => {
+ editedFields.current.add("expectedOutput");
+ field.onChange(value);
+ }}
editorRef={expectedOutputEditorRef}
- minHeight={200}
+ minHeight={140}
extensions={mediaDropPasteExtensions}
placeholder={`{
"answer": "London"
@@ -476,38 +581,39 @@ export const NewDatasetItemForm = (props: {
</FormItem>
)}
/>
+ <FormField
+ control={form.control}
+ name="metadata"
+ render={({ field }) => (
+ <FormItem className="flex flex-col gap-2">
+ <div className="flex items-center gap-2">
+ <FormLabel>Metadata</FormLabel>
+ <DatasetItemFieldToolbar
+ copyValue={field.value}
+ disabled={isPending}
+ onSelectFile={handleFileUpload(metadataEditorRef)}
+ />
+ </div>
+ <FormControl>
+ <CodeMirrorEditor
+ mode="json"
+ editable={!isPending}
+ value={field.value}
+ onChange={field.onChange}
+ editorRef={metadataEditorRef}
+ minHeight={100}
+ extensions={mediaDropPasteExtensions}
+ />
+ </FormControl>
+ <FormMessage />
+ </FormItem>
+ )}
+ />
+ <FormMediaAttachments
+ control={form.control}
+ pendingUploads={pendingUploads}
+ />
</div>
-
- <FormField
- control={form.control}
- name="metadata"
- render={({ field }) => (
- <FormItem className="flex flex-col gap-2">
- <div className="flex items-center gap-2">
- <FormLabel>Metadata</FormLabel>
- <DatasetItemFieldToolbar
- copyValue={field.value}
- onSelectFile={handleFileUpload(metadataEditorRef)}
- />
- </div>
- <FormControl>
- <CodeMirrorEditor
- mode="json"
- value={field.value}
- onChange={field.onChange}
- editorRef={metadataEditorRef}
- minHeight={100}
- extensions={mediaDropPasteExtensions}
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- )}
- />
- <FormMediaAttachments
- control={form.control}
- pendingUploads={pendingUploads}
- />
</div>
</DialogBody>
<DialogFooter>
@@ -516,7 +622,7 @@ export const NewDatasetItemForm = (props: {
control={form.control}
datasets={selectedDatasets}
selectedDatasetCount={selectedDatasetCount}
- isPending={createManyDatasetItemsMutation.isPending}
+ isPending={isPending}
pendingUploads={pendingUploads}
/>
{formError ? (
@@ -530,7 +636,7 @@ export const NewDatasetItemForm = (props: {
</form>
</Form>
);
-};
+}
/**
* Per-field schema error display, isolated so it re-renders from its own
@@ -617,7 +723,6 @@ const AddItemsButton = ({
<Button
type="submit"
loading={isPending}
- className="w-full"
// Block submit while uploads are in flight: the media reference is only
// inserted into the form value after the upload resolves, so submitting
// early would persist the item without the attachment and orphan the
diff --git a/web/src/features/datasets/components/NewDatasetItemFromExistingObjectDialogController.tsx b/web/src/features/datasets/components/NewDatasetItemFromExistingObjectDialogController.tsx
index 1158135db0..bc7b26327c 100644
--- a/web/src/features/datasets/components/NewDatasetItemFromExistingObjectDialogController.tsx
+++ b/web/src/features/datasets/components/NewDatasetItemFromExistingObjectDialogController.tsx
@@ -1,4 +1,4 @@
-import { type ReactNode } from "react";
+import { useRef, type ReactNode } from "react";
import { parseJsonPrioritised, type Prisma } from "@langfuse/shared";
@@ -26,6 +26,8 @@ export function NewDatasetItemFromExistingObjectDialogController(props: {
openDialog: (payload: DatasetItemDialogState) => void;
}) => ReactNode;
}) {
+ const submissionPending = useRef(false);
+ const currentInstance = useRef(0);
const normalizePrefillValue = (
value: Prisma.JsonValue | null,
): Prisma.JsonValue | null => {
@@ -42,15 +44,17 @@ export function NewDatasetItemFromExistingObjectDialogController(props: {
};
return (
- <DialogController<DatasetItemDialogState>
+ <DialogController<DatasetItemDialogState & { instance: number }>
closeOnInteractionOutside={false}
- size="xxl"
+ onBeforeClose={() => !submissionPending.current}
+ size="lg"
renderContent={({ state, closeDialog }) => (
<>
<DialogHeader>
<DialogTitle>Add item to datasets</DialogTitle>
</DialogHeader>
<NewDatasetItemForm
+ key={state.instance}
traceId={state.traceId}
observationId={state.observationId}
projectId={props.projectId}
@@ -58,14 +62,27 @@ export function NewDatasetItemFromExistingObjectDialogController(props: {
input={normalizePrefillValue(state.input)}
output={normalizePrefillValue(state.output)}
metadata={state.metadata}
- onFormSuccess={closeDialog}
+ onPendingChange={(pending) => {
+ if (currentInstance.current === state.instance)
+ submissionPending.current = pending;
+ }}
+ onFormSuccess={() => {
+ if (currentInstance.current === state.instance) closeDialog();
+ }}
className="h-full overflow-y-auto"
currentDatasetId={state.fromDatasetId}
/>
</>
)}
>
- {({ openDialog }) => props.children({ openDialog })}
+ {({ openDialog }) =>
+ props.children({
+ openDialog: (payload) => {
+ if (submissionPending.current) return;
+ openDialog({ ...payload, instance: ++currentInstance.current });
+ },
+ })
+ }
</DialogController>
);
}
diff --git a/web/src/features/datasets/components/submitDatasetItems.ts b/web/src/features/datasets/components/submitDatasetItems.ts
new file mode 100644
index 0000000000..ef46939f83
--- /dev/null
+++ b/web/src/features/datasets/components/submitDatasetItems.ts
@@ -0,0 +1,55 @@
+import {
+ reportTrpcErrorWithoutToast,
+ type RouterInputs,
+ type RouterOutputs,
+} from "@/src/utils/api";
+
+type CreateItemsInput = RouterInputs["datasets"]["createManyDatasetItems"];
+type CreateItemsResult = RouterOutputs["datasets"]["createManyDatasetItems"];
+
+export async function submitDatasetItems({
+ owner,
+ input,
+ submit,
+ onPendingChange,
+ onSuccess,
+ onError,
+}: {
+ owner: { pending: boolean };
+ input: CreateItemsInput;
+ submit: (input: CreateItemsInput) => Promise<CreateItemsResult>;
+ onPendingChange: (pending: boolean) => void;
+ onSuccess: () => void;
+ onError: (message: string | null) => void;
+}) {
+ if (owner.pending) return;
+ owner.pending = true;
+ onPendingChange(true);
+ onError(null);
+ let succeeded = false;
+ try {
+ const result = await submit(input);
+ if (result.success) {
+ succeeded = true;
+ } else {
+ onError(
+ `Item does not match dataset schema. Errors: ${JSON.stringify(result.validationErrors, null, 2)}`,
+ );
+ }
+ } catch (error) {
+ const message =
+ error instanceof Error
+ ? error.message
+ : "Could not add the dataset item. Please try again.";
+ onError(
+ message.includes("Body exc")
+ ? "Data exceeds maximum size (4.5MB). Please attempt to create dataset item programmatically."
+ : message,
+ );
+ reportTrpcErrorWithoutToast(error, "datasets");
+ } finally {
+ owner.pending = false;
+ onPendingChange(false);
+ }
+ if (succeeded) onSuccess();
+}
diff --git a/web/src/features/datasets/index.ts b/web/src/features/datasets/index.ts
index 66b140134a..65c90e4d77 100644
--- a/web/src/features/datasets/index.ts
+++ b/web/src/features/datasets/index.ts
@@ -1,7 +1,6 @@
// The datasets feature's public client surface (RFC rule 8). Named re-exports
// only — exactly what other features already imported. The public-API dataset
// services live behind server/index.ts.
-export type { DatasetFormRef } from "@/src/features/datasets/components/DatasetForm";
export { DatasetForm } from "@/src/features/datasets/components/DatasetForm";
export { DatasetSchemaHoverCard } from "@/src/features/datasets/components/DatasetSchemaHoverCard";
export { DiffLabel } from "@/src/features/datasets/components/DiffLabel";
diff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsx
index 9972d3c14b..e773dfe3de 100644
--- a/web/src/features/events/components/EventsTable.tsx
+++ b/web/src/features/events/components/EventsTable.tsx
@@ -2380,6 +2380,8 @@ export default function ObservationsEventsTable({
totalCount={totalCount ?? 0}
onClose={() => {
setShowRunEvaluationDialog(false);
+ }}
+ onSuccess={() => {
setSelectedRows({});
setSelectAll(false);
}}
@@ -2389,6 +2391,7 @@ export default function ObservationsEventsTable({
{showAddToDatasetDialog && (
<AddObservationsToDatasetDialog
+ isV4
projectId={projectId}
selectedObservationIds={selectedObservationIds}
query={{
@@ -2401,6 +2404,8 @@ export default function ObservationsEventsTable({
totalCount={totalCount ?? 0}
onClose={() => {
setShowAddToDatasetDialog(false);
+ }}
+ onSuccess={() => {
setSelectedRows({});
setSelectAll(false);
}}
diff --git a/web/src/features/experiments/components/table/ExperimentItemsTable.tsx b/web/src/features/experiments/components/table/ExperimentItemsTable.tsx
index 73db0c1320..83f44080ae 100644
--- a/web/src/features/experiments/components/table/ExperimentItemsTable.tsx
+++ b/web/src/features/experiments/components/table/ExperimentItemsTable.tsx
@@ -2270,6 +2270,8 @@ export default function ExperimentItemsTable({
}
onClose={() => {
setShowRunEvaluationDialog(false);
+ }}
+ onSuccess={() => {
setSelectedRows({});
setSelectAll(false);
}}
diff --git a/web/src/features/experiments/components/table/ExperimentsTable.tsx b/web/src/features/experiments/components/table/ExperimentsTable.tsx
index 17fd52e1a5..149a6eaf23 100644
--- a/web/src/features/experiments/components/table/ExperimentsTable.tsx
+++ b/web/src/features/experiments/components/table/ExperimentsTable.tsx
@@ -272,8 +272,8 @@ function ExperimentsMultiSelectActionMenu({
totalCount={selectedExperimentIds.length}
onClose={() => {
setShowRunEvaluationDialog(false);
- clearSelection();
}}
+ onSuccess={clearSelection}
sourceTable="experiments"
/>
)}
diff --git a/web/src/features/tracing-tables/observations/ObservationsTable.tsx b/web/src/features/tracing-tables/observations/ObservationsTable.tsx
index 611f8ad8a3..9d3dd8751d 100644
--- a/web/src/features/tracing-tables/observations/ObservationsTable.tsx
+++ b/web/src/features/tracing-tables/observations/ObservationsTable.tsx
@@ -1669,6 +1669,7 @@ function ObservationsAddToDatasetDialog({
return (
<AddObservationsToDatasetDialog
+ isV4={false}
projectId={projectId}
selectedObservationIds={selectedObservationIds}
query={{
@@ -1681,8 +1682,8 @@ function ObservationsAddToDatasetDialog({
totalCount={totalCount ?? 0}
onClose={() => {
actions.setShowAddToDatasetDialog(false);
- actions.clearSelection();
}}
+ onSuccess={actions.clearSelection}
exampleObservation={{
id: firstRow?.id ?? "",
traceId: firstRow?.traceId ?? "",