|
| 1 | +import { globalPrismaClient } from "@/prisma-client"; |
| 2 | +import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; |
| 3 | +import { adaptSchema, yupArray, yupMixed, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; |
| 4 | +import { getOwnedConversation } from "../utils"; |
| 5 | + |
| 6 | +export const GET = createSmartRouteHandler({ |
| 7 | + metadata: { |
| 8 | + summary: "Get AI conversation", |
| 9 | + description: "Fetch a single AI conversation with all its messages", |
| 10 | + }, |
| 11 | + request: yupObject({ |
| 12 | + auth: yupObject({ |
| 13 | + type: adaptSchema, |
| 14 | + user: adaptSchema.defined(), |
| 15 | + project: yupObject({ |
| 16 | + id: yupString().oneOf(["internal"]).defined(), |
| 17 | + }).defined(), |
| 18 | + }).defined(), |
| 19 | + params: yupObject({ |
| 20 | + conversationId: yupString().defined(), |
| 21 | + }), |
| 22 | + method: yupString().oneOf(["GET"]).defined(), |
| 23 | + }), |
| 24 | + response: yupObject({ |
| 25 | + statusCode: yupNumber().oneOf([200]).defined(), |
| 26 | + bodyType: yupString().oneOf(["json"]).defined(), |
| 27 | + body: yupObject({ |
| 28 | + id: yupString().defined(), |
| 29 | + title: yupString().defined(), |
| 30 | + projectId: yupString().defined(), |
| 31 | + messages: yupArray(yupObject({ |
| 32 | + id: yupString().defined(), |
| 33 | + role: yupString().defined(), |
| 34 | + content: yupMixed().defined(), |
| 35 | + }).noUnknown(false)).defined(), |
| 36 | + }).defined(), |
| 37 | + }), |
| 38 | + handler: async ({ auth, params }) => { |
| 39 | + const conversation = await getOwnedConversation(params.conversationId, auth.user.id); |
| 40 | + |
| 41 | + const messages = await globalPrismaClient.aiMessage.findMany({ |
| 42 | + where: { conversationId: conversation.id }, |
| 43 | + orderBy: { position: "asc" }, |
| 44 | + select: { |
| 45 | + id: true, |
| 46 | + role: true, |
| 47 | + content: true, |
| 48 | + }, |
| 49 | + }); |
| 50 | + |
| 51 | + return { |
| 52 | + statusCode: 200 as const, |
| 53 | + bodyType: "json" as const, |
| 54 | + body: { |
| 55 | + id: conversation.id, |
| 56 | + title: conversation.title, |
| 57 | + projectId: conversation.projectId, |
| 58 | + messages: messages.map(m => ({ ...m, content: m.content as object })), |
| 59 | + }, |
| 60 | + }; |
| 61 | + }, |
| 62 | +}); |
| 63 | + |
| 64 | +export const PATCH = createSmartRouteHandler({ |
| 65 | + metadata: { |
| 66 | + summary: "Update AI conversation", |
| 67 | + description: "Update the title of an AI conversation", |
| 68 | + }, |
| 69 | + request: yupObject({ |
| 70 | + auth: yupObject({ |
| 71 | + type: adaptSchema, |
| 72 | + user: adaptSchema.defined(), |
| 73 | + project: yupObject({ |
| 74 | + id: yupString().oneOf(["internal"]).defined(), |
| 75 | + }).defined(), |
| 76 | + }).defined(), |
| 77 | + params: yupObject({ |
| 78 | + conversationId: yupString().defined(), |
| 79 | + }), |
| 80 | + body: yupObject({ |
| 81 | + title: yupString().defined(), |
| 82 | + }), |
| 83 | + }), |
| 84 | + response: yupObject({ |
| 85 | + statusCode: yupNumber().oneOf([200]).defined(), |
| 86 | + bodyType: yupString().oneOf(["json"]).defined(), |
| 87 | + body: yupObject({}).defined(), |
| 88 | + }), |
| 89 | + handler: async ({ auth, params, body }) => { |
| 90 | + await getOwnedConversation(params.conversationId, auth.user.id); |
| 91 | + |
| 92 | + await globalPrismaClient.aiConversation.update({ |
| 93 | + where: { id: params.conversationId }, |
| 94 | + data: { title: body.title }, |
| 95 | + }); |
| 96 | + |
| 97 | + return { |
| 98 | + statusCode: 200 as const, |
| 99 | + bodyType: "json" as const, |
| 100 | + body: {}, |
| 101 | + }; |
| 102 | + }, |
| 103 | +}); |
| 104 | + |
| 105 | +export const DELETE = createSmartRouteHandler({ |
| 106 | + metadata: { |
| 107 | + summary: "Delete AI conversation", |
| 108 | + description: "Delete an AI conversation and all its messages", |
| 109 | + }, |
| 110 | + request: yupObject({ |
| 111 | + auth: yupObject({ |
| 112 | + type: adaptSchema, |
| 113 | + user: adaptSchema.defined(), |
| 114 | + project: yupObject({ |
| 115 | + id: yupString().oneOf(["internal"]).defined(), |
| 116 | + }).defined(), |
| 117 | + }).defined(), |
| 118 | + params: yupObject({ |
| 119 | + conversationId: yupString().defined(), |
| 120 | + }), |
| 121 | + }), |
| 122 | + response: yupObject({ |
| 123 | + statusCode: yupNumber().oneOf([200]).defined(), |
| 124 | + bodyType: yupString().oneOf(["json"]).defined(), |
| 125 | + body: yupObject({}).defined(), |
| 126 | + }), |
| 127 | + handler: async ({ auth, params }) => { |
| 128 | + await getOwnedConversation(params.conversationId, auth.user.id); |
| 129 | + |
| 130 | + await globalPrismaClient.aiConversation.delete({ |
| 131 | + where: { id: params.conversationId }, |
| 132 | + }); |
| 133 | + |
| 134 | + return { |
| 135 | + statusCode: 200 as const, |
| 136 | + bodyType: "json" as const, |
| 137 | + body: {}, |
| 138 | + }; |
| 139 | + }, |
| 140 | +}); |
0 commit comments