Skip to content

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

feat(datasets): add duplicate dataset button (#2957)

Marc Klingen61e3470a938c
diff --git a/web/src/features/datasets/components/DuplicateDatasetButton.tsx b/web/src/features/datasets/components/DuplicateDatasetButton.tsx
new file mode 100644
index 0000000000..5732a37598
--- /dev/null
+++ b/web/src/features/datasets/components/DuplicateDatasetButton.tsx
@@ -0,0 +1,44 @@
+import { useRouter } from "next/router";
+import { Button } from "@/src/components/ui/button";
+import { api } from "@/src/utils/api";
+import { Copy } from "lucide-react";
+import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+
+export const DuplicateDatasetButton: React.FC<{
+ projectId: string;
+ datasetId: string;
+}> = ({ projectId, datasetId }) => {
+ const router = useRouter();
+ const hasAccess = useHasProjectAccess({
+ projectId,
+ scope: "datasets:CUD",
+ });
+ const duplicateDataset = api.datasets.duplicateDataset.useMutation({
+ onSuccess: ({ id }) => {
+ router.push(`/project/${projectId}/datasets/${id}`);
+ },
+ });
+
+ const handleDuplicate = () => {
+ if (
+ confirm(
+ "Are you sure you want to duplicate this dataset and all of its items?",
+ )
+ ) {
+ duplicateDataset.mutate({ projectId, datasetId });
+ }
+ };
+
+ return (
+ <Button
+ onClick={handleDuplicate}
+ variant="outline"
+ title="Duplicate dataset"
+ loading={duplicateDataset.isLoading}
+ size="icon"
+ disabled={!hasAccess}
+ >
+ <Copy className="h-4 w-4" />
+ </Button>
+ );
+};
diff --git a/web/src/features/datasets/server/dataset-router.ts b/web/src/features/datasets/server/dataset-router.ts
index 3e9917ddbe..c8ba0c7de7 100644
--- a/web/src/features/datasets/server/dataset-router.ts
+++ b/web/src/features/datasets/server/dataset-router.ts
@@ -473,6 +473,98 @@ export const datasetRouter = createTRPCRouter({
});
return deletedDataset;
}),
+ duplicateDataset: protectedProjectProcedure
+ .input(
+ z.object({
+ projectId: z.string(),
+ datasetId: z.string(),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "datasets:CUD",
+ });
+ const dataset = await ctx.prisma.dataset.findUnique({
+ where: {
+ id_projectId: {
+ id: input.datasetId,
+ projectId: input.projectId,
+ },
+ },
+ include: {
+ datasetItems: {
+ orderBy: {
+ createdAt: "asc",
+ },
+ },
+ },
+ });
+ if (!dataset) {
+ throw new Error("Dataset not found");
+ }
+
+ // find a unique name for the new dataset
+ // by appending a counter to the name in case of the name already exists
+ // e.g. "Copy of dataset" -> "Copy of dataset (1)"
+ const existingDatasetNames = (
+ await ctx.prisma.dataset.findMany({
+ select: {
+ name: true,
+ },
+ where: {
+ projectId: input.projectId,
+ name: {
+ startsWith: "Copy of " + dataset.name,
+ },
+ },
+ })
+ ).map((d) => d.name);
+ let counter: number = 0;
+ const duplicateDatasetName = (pCounter: number) =>
+ pCounter === 0
+ ? `Copy of ${dataset.name}`
+ : `Copy of ${dataset.name} (${counter})`;
+ while (true) {
+ if (!existingDatasetNames.includes(duplicateDatasetName(counter))) {
+ break;
+ }
+ counter++;
+ }
+
+ const newDataset = await ctx.prisma.dataset.create({
+ data: {
+ name: duplicateDatasetName(counter),
+ description: dataset.description,
+ projectId: input.projectId,
+ metadata: dataset.metadata ?? undefined,
+ datasetItems: {
+ createMany: {
+ data: dataset.datasetItems.map((item) => ({
+ // the items get new ids as they need to be unique on project level
+ input: item.input ?? undefined,
+ expectedOutput: item.expectedOutput ?? undefined,
+ metadata: item.metadata ?? undefined,
+ sourceTraceId: item.sourceTraceId,
+ sourceObservationId: item.sourceObservationId,
+ status: item.status,
+ })),
+ },
+ },
+ },
+ });
+
+ await auditLog({
+ session: ctx.session,
+ resourceType: "dataset",
+ resourceId: newDataset.id,
+ action: "create",
+ after: newDataset,
+ });
+
+ return { id: newDataset.id };
+ }),
createDatasetItem: protectedProjectProcedure
.input(
z.object({
diff --git a/web/src/pages/project/[projectId]/datasets/[datasetId]/index.tsx b/web/src/pages/project/[projectId]/datasets/[datasetId]/index.tsx
index fcc2b03ce6..3dad03bd00 100644
--- a/web/src/pages/project/[projectId]/datasets/[datasetId]/index.tsx
+++ b/web/src/pages/project/[projectId]/datasets/[datasetId]/index.tsx
@@ -9,6 +9,7 @@ import { DatasetActionButton } from "@/src/features/datasets/components/DatasetA
import { DeleteButton } from "@/src/components/deleteButton";
import { JSONView } from "@/src/components/ui/CodeJsonViewer";
import { FullScreenPage } from "@/src/components/layouts/full-screen-page";
+import { DuplicateDatasetButton } from "@/src/features/datasets/components/DuplicateDatasetButton";
export default function Dataset() {
const router = useRouter();
@@ -52,6 +53,10 @@ export default function Dataset() {
datasetMetadata={dataset.data?.metadata}
icon
/>
+ <DuplicateDatasetButton
+ datasetId={datasetId}
+ projectId={projectId}
+ />
<DeleteButton
itemId={datasetId}
projectId={projectId}
diff --git a/web/src/pages/project/[projectId]/datasets/[datasetId]/items.tsx b/web/src/pages/project/[projectId]/datasets/[datasetId]/items.tsx
index 5c29ef9b09..cad81b9310 100644
--- a/web/src/pages/project/[projectId]/datasets/[datasetId]/items.tsx
+++ b/web/src/pages/project/[projectId]/datasets/[datasetId]/items.tsx
@@ -10,6 +10,7 @@ import { DeleteButton } from "@/src/components/deleteButton";
import { NewDatasetItemButton } from "@/src/features/datasets/components/NewDatasetItemButton";
import { JSONView } from "@/src/components/ui/CodeJsonViewer";
import { FullScreenPage } from "@/src/components/layouts/full-screen-page";
+import { DuplicateDatasetButton } from "@/src/features/datasets/components/DuplicateDatasetButton";
export default function DatasetItems() {
const router = useRouter();
@@ -60,6 +61,10 @@ export default function DatasetItems() {
datasetMetadata={dataset.data?.metadata}
icon
/>
+ <DuplicateDatasetButton
+ datasetId={datasetId}
+ projectId={projectId}
+ />
<DeleteButton
itemId={datasetId}
projectId={projectId}