Skip to content

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

feat: delete single traces (admin and owner only) (#514)

18feb065b4523bd7105
diff --git a/src/components/delete-trace.tsx b/src/components/delete-trace.tsx
new file mode 100644
index 0000000000..4098546cd3
--- /dev/null
+++ b/src/components/delete-trace.tsx
@@ -0,0 +1,77 @@
+import { TrashIcon } from "lucide-react";
+import { useRouter } from "next/router";
+
+import { Button } from "@/src/components/ui/button";
+import { api } from "@/src/utils/api";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/src/components/ui/popover";
+import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
+
+export function DeleteTrace({
+ traceId,
+ projectId,
+ isTableAction,
+}: {
+ traceId: string;
+ projectId: string;
+ isTableAction?: boolean;
+}) {
+ const router = useRouter();
+ const utils = api.useContext();
+
+ const hasAccess = useHasAccess({ projectId, scope: "traces:delete" });
+
+ const mutDeleteTrace = api.traces.delete.useMutation({
+ onSuccess: () => {
+ if (!isTableAction) {
+ void router
+ .push(`/project/${projectId}/traces`)
+ .then(() => utils.traces.invalidate());
+ } else {
+ void utils.traces.invalidate();
+ }
+ },
+ });
+
+ if (!hasAccess) {
+ return null;
+ }
+
+ return (
+ <Popover>
+ <PopoverTrigger asChild>
+ {isTableAction ? (
+ <Button variant="ghost" size="xs">
+ <TrashIcon className="h-4 w-4" />
+ </Button>
+ ) : (
+ <Button variant="outline" type="button">
+ <TrashIcon className="h-4 w-4" />
+ </Button>
+ )}
+ </PopoverTrigger>
+ <PopoverContent>
+ <h2 className="text-md mb-3 font-semibold">Please confirm</h2>
+ <p className="mb-3 text-sm">
+ This action cannot be undone and removes all the data associated with
+ this trace.
+ </p>
+ <div className="flex justify-end space-x-4">
+ <Button
+ type="button"
+ variant="destructive"
+ loading={mutDeleteTrace.isLoading}
+ onClick={() =>
+ void mutDeleteTrace.mutateAsync({ traceId, projectId })
+ }
+ >
+ Delete trace
+ </Button>
+ </div>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/src/components/table/use-cases/traces.tsx b/src/components/table/use-cases/traces.tsx
index 3e43537eec..ed2a021e51 100644
--- a/src/components/table/use-cases/traces.tsx
+++ b/src/components/table/use-cases/traces.tsx
@@ -1,3 +1,4 @@
+import { DeleteTrace } from "@/src/components/delete-trace";
import { GroupedScoreBadges } from "@/src/components/grouped-score-badge";
import { DataTable } from "@/src/components/table/data-table";
import { DataTableToolbar } from "@/src/components/table/data-table-toolbar";
@@ -217,6 +218,20 @@ export default function TracesTable({
accessorKey: "release",
header: "Release",
},
+ {
+ accessorKey: "action",
+ header: "Action",
+ cell: ({ row }) => {
+ const traceId = row.getValue("id");
+ return traceId && typeof traceId === "string" ? (
+ <DeleteTrace
+ traceId={traceId}
+ isTableAction={true}
+ projectId={projectId}
+ />
+ ) : undefined;
+ },
+ },
];
return (
@@ -239,16 +254,16 @@ export default function TracesTable({
traces.isLoading
? { isLoading: true, isError: false }
: traces.isError
- ? {
- isLoading: false,
- isError: true,
- error: traces.error.message,
- }
- : {
- isLoading: false,
- isError: false,
- data: traces.data?.map((t) => convertToTableRow(t)),
- }
+ ? {
+ isLoading: false,
+ isError: true,
+ error: traces.error.message,
+ }
+ : {
+ isLoading: false,
+ isError: false,
+ data: traces.data?.map((t) => convertToTableRow(t)),
+ }
}
pagination={{
pageCount: Math.ceil(totalCount / paginationState.pageSize),
diff --git a/src/components/trace/index.tsx b/src/components/trace/index.tsx
index fe8c71bbac..4021d7ef14 100644
--- a/src/components/trace/index.tsx
+++ b/src/components/trace/index.tsx
@@ -13,6 +13,7 @@ import { DetailPageNav } from "@/src/features/navigate-detail-pages/DetailPageNa
import { useRouter } from "next/router";
import { type ObservationReturnType } from "@/src/server/api/routers/traces";
import { api } from "@/src/utils/api";
+import { DeleteTrace } from "@/src/components/delete-trace";
export function Trace(props: {
observations: Array<ObservationReturnType>;
@@ -102,6 +103,10 @@ export function TracePage({ traceId }: { traceId: string }) {
}
listKey="traces"
/>
+ <DeleteTrace
+ traceId={trace.data.id}
+ projectId={trace.data.projectId}
+ />
</>
}
/>
diff --git a/src/features/rbac/constants/roleAccessRights.ts b/src/features/rbac/constants/roleAccessRights.ts
index 5b8cd9cd64..109edcab2f 100644
--- a/src/features/rbac/constants/roleAccessRights.ts
+++ b/src/features/rbac/constants/roleAccessRights.ts
@@ -10,6 +10,7 @@ const scopes = [
"apiKeys:delete",
"traces:publish",
+ "traces:delete",
"scores:CUD",
@@ -30,6 +31,7 @@ export const roleAccessRights: Record<MembershipRole, Scope[]> = {
"apiKeys:create",
"apiKeys:delete",
"traces:publish",
+ "traces:delete",
"scores:CUD",
"project:delete",
"datasets:CUD",
@@ -42,6 +44,7 @@ export const roleAccessRights: Record<MembershipRole, Scope[]> = {
"apiKeys:create",
"apiKeys:delete",
"traces:publish",
+ "traces:delete",
"scores:CUD",
"project:delete",
"datasets:CUD",
diff --git a/src/server/api/routers/traces.ts b/src/server/api/routers/traces.ts
index b953d43bb0..a9c8b30285 100644
--- a/src/server/api/routers/traces.ts
+++ b/src/server/api/routers/traces.ts
@@ -23,6 +23,7 @@ import {
datetimeFilterToPrismaSql,
filterToPrismaSql,
} from "@/src/features/filters/server/filterToPrisma";
+import { throwIfNoAccess } from "@/src/features/rbac/utils/checkAccess";
const TraceFilterOptions = z.object({
projectId: z.string(), // Required for protectedProjectProcedure
@@ -246,9 +247,9 @@ export const traceRouter = createTRPCRouter({
? (obsEndTimes[obsEndTimes.length - 1] as Date).getTime() -
obsStartTimes[0]!.getTime()
: obsStartTimes.length > 1
- ? obsStartTimes[obsStartTimes.length - 1]!.getTime() -
- obsStartTimes[0]!.getTime()
- : undefined
+ ? obsStartTimes[obsStartTimes.length - 1]!.getTime() -
+ obsStartTimes[0]!.getTime()
+ : undefined
: undefined;
const enrichedObservations = observations.map(
@@ -275,4 +276,37 @@ export const traceRouter = createTRPCRouter({
observations: enrichedObservations as ObservationReturnType[],
};
}),
+ delete: protectedProjectProcedure
+ .input(z.object({ traceId: z.string(), projectId: z.string() }))
+ .mutation(async ({ input, ctx }) => {
+ throwIfNoAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "traces:delete",
+ });
+
+ const trace = await ctx.prisma.trace.findFirst({
+ where: {
+ id: input.traceId,
+ projectId: input.projectId,
+ },
+ });
+
+ if (!trace) {
+ throw new Error("Trace not found in project");
+ }
+
+ return ctx.prisma.$transaction([
+ ctx.prisma.trace.delete({
+ where: {
+ id: input.traceId,
+ },
+ }),
+ ctx.prisma.observation.deleteMany({
+ where: {
+ traceId: input.traceId,
+ },
+ }),
+ ]);
+ }),
});