|
| 1 | +import type { RbacAbility } from "@trigger.dev/rbac"; |
| 2 | +import { prisma } from "~/db.server"; |
| 3 | +import { deletePlaygroundConversation } from "~/models/playgroundConversation.server"; |
| 4 | +import type { McpIdentity, McpPreviewRefusal, McpSubject } from "../gate/types"; |
| 5 | +import type { McpToolContext } from "../gate/withMcpToolAuth.server"; |
| 6 | +import type { McpToolDefinition } from "../registry.server"; |
| 7 | +import { textResult } from "./define.server"; |
| 8 | +import { defineWriteTool, staleAnswer, staleApproval } from "./defineWrite.server"; |
| 9 | +import { plainError } from "./format/insights"; |
| 10 | +import { capText, untrusted } from "./format/logFields"; |
| 11 | +import { quoteForPreview } from "./format/webhooks"; |
| 12 | +import { startReadBudget } from "./readBudget.server"; |
| 13 | +import { |
| 14 | + DeletePlaygroundConversationInput, |
| 15 | + type DeletePlaygroundConversationInput as DeleteArgs, |
| 16 | +} from "./schemas/sessions"; |
| 17 | + |
| 18 | +const READ_BUDGET_MS = 10_000; |
| 19 | +const TEXT_CHARS = 200; |
| 20 | +const STOPPED = { |
| 21 | + aborted: "The request was cancelled before the conversation was read; nothing changed.", |
| 22 | + timeout: "Reading the conversation took too long; nothing changed. Try again.", |
| 23 | +}; |
| 24 | +type Read = ReturnType<typeof startReadBudget>["read"]; |
| 25 | +type Conversation = NonNullable<Awaited<ReturnType<typeof findConversation>>>; |
| 26 | +type Scope = { environmentId: string; userId: string }; |
| 27 | + |
| 28 | +const notFound = (chatId: string) => |
| 29 | + `No playground conversation of yours with chat id ${untrusted(chatId, "playground-chat-id", 256)} in this environment; list_playground_conversations lists them; nothing changed.`; |
| 30 | + |
| 31 | +const quoted = (text: string) => quoteForPreview(capText(text, TEXT_CHARS)); |
| 32 | + |
| 33 | +function findConversation(scope: Scope, chatId: string) { |
| 34 | + return prisma.playgroundConversation.findFirst({ |
| 35 | + where: { chatId, runtimeEnvironmentId: scope.environmentId, userId: scope.userId }, |
| 36 | + select: { |
| 37 | + id: true, |
| 38 | + chatId: true, |
| 39 | + title: true, |
| 40 | + agentSlug: true, |
| 41 | + createdAt: true, |
| 42 | + updatedAt: true, |
| 43 | + }, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +async function deletePlan( |
| 48 | + args: DeleteArgs, |
| 49 | + scope: Scope, |
| 50 | + read: Read |
| 51 | +): Promise<{ preview: string; conversation: Conversation } | McpPreviewRefusal> { |
| 52 | + const found = await read(() => findConversation(scope, args.chatId)); |
| 53 | + if (!found.ok) return { refuse: STOPPED[found.reason] }; |
| 54 | + const conversation = found.value; |
| 55 | + if (!conversation) return { refuse: notFound(args.chatId) }; |
| 56 | + |
| 57 | + const preview = [ |
| 58 | + `Delete your playground conversation ${quoted(conversation.title)} with agent ${quoted(conversation.agentSlug)}, chat id ${quoted(conversation.chatId)}, created ${conversation.createdAt.toISOString()}.`, |
| 59 | + "This cannot be undone. It removes the conversation from the playground list with its title and the messages and client data saved with it.", |
| 60 | + "The session behind it, its runs and its saved transcript are kept.", |
| 61 | + `Conversation revision ${conversation.updatedAt.toISOString()}: if the conversation changes before this runs, it is refused.`, |
| 62 | + ].join("\n"); |
| 63 | + return { preview, conversation }; |
| 64 | +} |
| 65 | + |
| 66 | +async function preview( |
| 67 | + args: DeleteArgs, |
| 68 | + subject: McpSubject | null, |
| 69 | + _ability?: RbacAbility, |
| 70 | + identity?: McpIdentity |
| 71 | +) { |
| 72 | + if (!subject?.projectId || !subject.environmentId || !identity) { |
| 73 | + return { refuse: "Environment not found" }; |
| 74 | + } |
| 75 | + // The gate gives a preview no request signal, so only the deadline can stop its reads. |
| 76 | + const { read } = startReadBudget(READ_BUDGET_MS, new AbortController().signal); |
| 77 | + const plan = await deletePlan( |
| 78 | + args, |
| 79 | + { environmentId: subject.environmentId, userId: identity.userId }, |
| 80 | + read |
| 81 | + ); |
| 82 | + return "refuse" in plan ? plan : plan.preview; |
| 83 | +} |
| 84 | + |
| 85 | +async function run(args: DeleteArgs, ctx: McpToolContext) { |
| 86 | + const environmentId = ctx.subject?.projectId ? ctx.subject.environmentId : undefined; |
| 87 | + if (!environmentId) return plainError("Environment not found"); |
| 88 | + |
| 89 | + const { read } = startReadBudget(READ_BUDGET_MS, ctx.signal); |
| 90 | + const scope = { environmentId, userId: ctx.identity.userId }; |
| 91 | + const plan = await deletePlan(args, scope, read); |
| 92 | + if ("refuse" in plan) return plainError(plan.refuse); |
| 93 | + const stale = staleApproval(ctx, plan.preview, plainError); |
| 94 | + if (stale) return stale; |
| 95 | + |
| 96 | + const { conversation } = plan; |
| 97 | + const { count } = await deletePlaygroundConversation( |
| 98 | + { id: conversation.id, runtimeEnvironmentId: scope.environmentId, userId: scope.userId }, |
| 99 | + conversation.updatedAt |
| 100 | + ); |
| 101 | + if (count === 0) return staleAnswer(plainError); |
| 102 | + return textResult( |
| 103 | + `Deleted playground conversation ${untrusted(conversation.title, "playground-title", TEXT_CHARS)} (chat id ${untrusted(conversation.chatId, "playground-chat-id", 256)}).` |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +export const playgroundWriteTools: McpToolDefinition[] = [ |
| 108 | + defineWriteTool({ |
| 109 | + name: "delete_playground_conversation", |
| 110 | + title: "Delete Playground Conversation", |
| 111 | + description: |
| 112 | + "Delete one of your own playground conversations, like the delete button in the agent's Playground conversation list. It removes the conversation and its saved messages from the playground; the session behind it and its runs are kept. It cannot be undone.", |
| 113 | + shape: DeletePlaygroundConversationInput.shape, |
| 114 | + approval: "required", |
| 115 | + auth: { action: "write", subject: "sessions" }, |
| 116 | + confirm: { preview }, |
| 117 | + run, |
| 118 | + }), |
| 119 | +]; |
0 commit comments