Skip to content

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

feat(ui): delete prompts and prompt versions + deduplication of prompt variables (#1116)

Yash Kharec803b0d9c22a
diff --git a/src/features/prompts/components/delete-prompt-version.tsx b/src/features/prompts/components/delete-prompt-version.tsx
new file mode 100644
index 0000000000..402c39d093
--- /dev/null
+++ b/src/features/prompts/components/delete-prompt-version.tsx
@@ -0,0 +1,90 @@
+import { Button } from "@/src/components/ui/button";
+import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
+import { api } from "@/src/utils/api";
+import { Trash2 } from "lucide-react";
+import { useState } from "react";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/src/components/ui/popover";
+import { useRouter } from "next/router";
+
+export function DeletePromptVersion({
+ promptVersionId,
+ projectId,
+ version,
+ countVersions,
+}: {
+ promptVersionId: string;
+ projectId: string;
+ version: number;
+ countVersions: number;
+}) {
+ const utils = api.useUtils();
+ const router = useRouter();
+ const [isOpen, setIsOpen] = useState(false);
+ const hasAccess = useHasAccess({ projectId, scope: "prompts:CUD" });
+
+ const mutDeletePromptVersion = api.prompts.deleteVersion.useMutation({
+ onSuccess: () => {
+ void utils.prompts.invalidate();
+ if (countVersions > 1) {
+ void router.replace(
+ {
+ pathname: router.pathname,
+ query: { ...router.query, version: undefined },
+ },
+ undefined,
+ { shallow: true },
+ );
+ } else {
+ void router.push(`/project/${projectId}/prompts`);
+ }
+ },
+ });
+
+ if (!hasAccess) {
+ return null;
+ }
+
+ return (
+ <Popover
+ key={promptVersionId}
+ open={isOpen}
+ onOpenChange={() => setIsOpen(!isOpen)}
+ >
+ <PopoverTrigger asChild>
+ <Button variant="outline" type="button" size="icon">
+ <Trash2 className="h-5 w-5" />
+ </Button>
+ </PopoverTrigger>
+ <PopoverContent>
+ <h2 className="text-md mb-3 font-semibold">Please confirm</h2>
+ <p className="mb-3 text-sm">
+ This action deletes the prompt version. Requests of version{" "}
+ <code className="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold">
+ {version}
+ </code>
+ of this prompt will return an error.
+ </p>
+ <div className="flex justify-end space-x-4">
+ <Button
+ type="button"
+ variant="destructive"
+ loading={mutDeletePromptVersion.isLoading}
+ onClick={() => {
+ void mutDeletePromptVersion.mutateAsync({
+ promptVersionId,
+ projectId,
+ });
+ setIsOpen(false);
+ }}
+ >
+ Delete Prompt Version
+ </Button>
+ </div>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/src/features/prompts/components/delete-prompt.tsx b/src/features/prompts/components/delete-prompt.tsx
new file mode 100644
index 0000000000..571ceeb6f5
--- /dev/null
+++ b/src/features/prompts/components/delete-prompt.tsx
@@ -0,0 +1,69 @@
+import { Button } from "@/src/components/ui/button";
+import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
+import { api } from "@/src/utils/api";
+import { Trash2 } from "lucide-react";
+import { useState } from "react";
+import {
+ Popover,
+ PopoverContent,
+ PopoverTrigger,
+} from "@/src/components/ui/popover";
+
+export function DeletePrompt({
+ projectId,
+ promptName,
+}: {
+ projectId: string;
+ promptName: string;
+}) {
+ const utils = api.useUtils();
+ const [isOpen, setIsOpen] = useState(false);
+ const hasAccess = useHasAccess({ projectId, scope: "prompts:CUD" });
+
+ const mutDeletePrompt = api.prompts.delete.useMutation({
+ onSuccess: () => {
+ void utils.prompts.invalidate();
+ },
+ });
+
+ if (!hasAccess) {
+ return null;
+ }
+
+ return (
+ <Popover open={isOpen} onOpenChange={() => setIsOpen(!isOpen)}>
+ <PopoverTrigger asChild>
+ <Button variant="ghost" size="xs">
+ <Trash2 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 permanently deletes this prompt. All requests to fetch
+ prompt{" "}
+ <code className="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold">
+ {promptName}
+ </code>{" "}
+ will error.
+ </p>
+ <div className="flex justify-end space-x-4">
+ <Button
+ type="button"
+ variant="destructive"
+ loading={mutDeletePrompt.isLoading}
+ onClick={() => {
+ void mutDeletePrompt.mutateAsync({
+ projectId,
+ promptName,
+ });
+ setIsOpen(false);
+ }}
+ >
+ Delete Prompt
+ </Button>
+ </div>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/src/features/prompts/components/prompt-detail.tsx b/src/features/prompts/components/prompt-detail.tsx
index 219cb40452..7f4924a82f 100644
--- a/src/features/prompts/components/prompt-detail.tsx
+++ b/src/features/prompts/components/prompt-detail.tsx
@@ -14,6 +14,7 @@ import { PromotePrompt } from "@/src/features/prompts/components/promote-prompt"
import { ScrollArea } from "@radix-ui/react-scroll-area";
import { useQueryParam, NumberParam } from "use-query-params";
import router from "next/router";
+import { DeletePromptVersion } from "@/src/features/prompts/components/delete-prompt-version";
export type PromptDetailProps = {
projectId: string;
@@ -77,6 +78,12 @@ export const PromptDetail = (props: PromptDetailProps) => {
<Pencil className="h-5 w-5" />
</Button>
</CreatePromptDialog>
+ <DeletePromptVersion
+ projectId={props.projectId}
+ promptVersionId={prompt.id}
+ version={prompt.version}
+ countVersions={promptHistory.data.length}
+ />
<DetailPageNav
key="nav"
currentId={prompt.name}
diff --git a/src/features/prompts/components/prompts-table.tsx b/src/features/prompts/components/prompts-table.tsx
index 1d5aacc000..3f1415a091 100644
--- a/src/features/prompts/components/prompts-table.tsx
+++ b/src/features/prompts/components/prompts-table.tsx
@@ -5,6 +5,7 @@ import { Button } from "@/src/components/ui/button";
import { useDetailPageLists } from "@/src/features/navigate-detail-pages/context";
import { CreatePromptDialog } from "@/src/features/prompts/components/new-prompt-button";
import { useHasAccess } from "@/src/features/rbac/utils/checkAccess";
+import { DeletePrompt } from "@/src/features/prompts/components/delete-prompt";
import { api } from "@/src/utils/api";
import { type RouterOutput } from "@/src/utils/types";
@@ -71,6 +72,18 @@ export function PromptTable(props: { projectId: string }) {
return createdAt.toLocaleString();
},
},
+ {
+ accessorKey: "actions",
+ header: "Actions",
+ cell: ({ row }) => {
+ return (
+ <DeletePrompt
+ projectId={props.projectId}
+ promptName={row.getValue("name")}
+ />
+ );
+ },
+ },
];
const convertToTableRow = (
diff --git a/src/features/prompts/server/prompt-router.ts b/src/features/prompts/server/prompt-router.ts
index bfdd4d91e2..3d254fb380 100644
--- a/src/features/prompts/server/prompt-router.ts
+++ b/src/features/prompts/server/prompt-router.ts
@@ -83,6 +83,58 @@ export const promptRouter = createTRPCRouter({
throw e;
}
}),
+ delete: protectedProjectProcedure
+ .input(
+ z.object({
+ projectId: z.string(),
+ promptName: z.string(),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ try {
+ throwIfNoAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "prompts:CUD",
+ });
+
+ await ctx.prisma.prompt.deleteMany({
+ where: {
+ projectId: input.projectId,
+ name: input.promptName,
+ },
+ });
+ } catch (e) {
+ console.log(e);
+ throw e;
+ }
+ }),
+ deleteVersion: protectedProjectProcedure
+ .input(
+ z.object({
+ promptVersionId: z.string(),
+ projectId: z.string(),
+ }),
+ )
+ .mutation(async ({ input, ctx }) => {
+ try {
+ throwIfNoAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "prompts:CUD",
+ });
+
+ await ctx.prisma.prompt.delete({
+ where: {
+ id: input.promptVersionId,
+ projectId: input.projectId,
+ },
+ });
+ } catch (e) {
+ console.log(e);
+ throw e;
+ }
+ }),
promote: protectedProjectProcedure
.input(z.object({ promptId: z.string(), projectId: z.string() }))
.mutation(async ({ input, ctx }) => {
diff --git a/src/utils/string.ts b/src/utils/string.ts
index b62d5352da..d3c6a336c1 100644
--- a/src/utils/string.ts
+++ b/src/utils/string.ts
@@ -11,9 +11,11 @@ export function extractVariables(mustacheString: string): string[] {
// Iterate over all matches
while ((match = regex.exec(mustacheString)) !== null) {
- // Push each variable to the array
+ // Push each variable to the array if it's not already present
const variable = match[1];
- if (variable) variables.push(variable);
+ if (variable && !variables.includes(variable)) {
+ variables.push(variable);
+ }
}
return variables;