-
Notifications
You must be signed in to change notification settings - Fork 514
AI in Stack Companion #1297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
AI in Stack Companion #1297
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f3e27f8
initial commit
aadesh18 53ac1ff
pr comment changes
aadesh18 4f1806b
error fix
aadesh18 b12cd6f
pr comment changes
aadesh18 7dc8669
PR comment changes
aadesh18 f1a4ea8
pr fix
aadesh18 c4ef44c
pr changes
aadesh18 0c94a7e
Persistent AI Chat history (#1296)
aadesh18 bc5cadd
test failing fix
aadesh18 76a4d2a
Merge branch 'dev' into ai-in-stack-companion
aadesh18 4827618
test fail fix
aadesh18 a6146c9
Merge branch 'ai-in-stack-companion' of https://github.com/stack-auth…
aadesh18 fbe825c
Merge branch 'dev' into ai-in-stack-companion
aadesh18 a5b49de
bug fixes
aadesh18 d728c19
test failing
aadesh18 3abe22c
Merge branch 'dev' into ai-in-stack-companion
aadesh18 2126fc1
stopped using retry transaction
aadesh18 d44b748
Merge branch 'dev' into ai-in-stack-companion
aadesh18 3529fae
Merge branch 'dev' into ai-in-stack-companion
aadesh18 4aa6786
Merge branch 'dev' into ai-in-stack-companion
aadesh18 b165585
Merge branch 'dev' into ai-in-stack-companion
N2D4 a6fe151
Merge branch 'dev' into ai-in-stack-companion
aadesh18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
apps/backend/prisma/migrations/20260327000000_add_ai_conversations/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "AiConversation" ( | ||
| "id" UUID NOT NULL DEFAULT gen_random_uuid(), | ||
| "projectUserId" UUID NOT NULL, | ||
| "projectId" TEXT NOT NULL REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
| "title" TEXT NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
|
||
| CONSTRAINT "AiConversation_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "AiMessage" ( | ||
| "id" UUID NOT NULL DEFAULT gen_random_uuid(), | ||
| "conversationId" UUID NOT NULL REFERENCES "AiConversation"("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
| "position" INTEGER NOT NULL, | ||
| "role" TEXT NOT NULL, | ||
| "content" JSONB NOT NULL, | ||
| "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
|
||
| CONSTRAINT "AiMessage_pkey" PRIMARY KEY ("id") | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "AiConversation_projectUserId_projectId_updatedAt_idx" ON "AiConversation"("projectUserId", "projectId", "updatedAt" DESC); | ||
|
|
||
| -- CreateIndex | ||
| CREATE INDEX "AiMessage_conversationId_position_idx" ON "AiMessage"("conversationId", "position" ASC); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
.../backend/src/app/api/latest/internal/ai-conversations/[conversationId]/messages/route.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { globalPrismaClient, retryTransaction } from "@/prisma-client"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, yupArray, yupMixed, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
| import { getOwnedConversation } from "../../utils"; | ||
|
|
||
| export const PUT = createSmartRouteHandler({ | ||
| metadata: { | ||
| summary: "Replace conversation messages", | ||
| description: "Replace all messages in a conversation", | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adaptSchema, | ||
| user: adaptSchema.defined(), | ||
| project: yupObject({ | ||
| id: yupString().oneOf(["internal"]).defined(), | ||
| }).defined(), | ||
| }).defined(), | ||
| params: yupObject({ | ||
| conversationId: yupString().defined(), | ||
| }), | ||
| body: yupObject({ | ||
| messages: yupArray( | ||
| yupObject({ | ||
| role: yupString().oneOf(["user", "assistant"]).defined(), | ||
| content: yupMixed().defined(), | ||
| }) | ||
| ).defined(), | ||
| }), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({}).defined(), | ||
| }), | ||
| handler: async ({ auth, params, body }) => { | ||
| await getOwnedConversation(params.conversationId, auth.user.id); | ||
|
|
||
| await retryTransaction(globalPrismaClient, async (tx) => { | ||
| await tx.aiMessage.deleteMany({ | ||
| where: { conversationId: params.conversationId }, | ||
| }); | ||
|
|
||
| if (body.messages.length > 0) { | ||
| await tx.aiMessage.createMany({ | ||
| data: body.messages.map((msg, index) => ({ | ||
| conversationId: params.conversationId, | ||
| position: index, | ||
| role: msg.role, | ||
| content: msg.content as object, | ||
| })), | ||
| }); | ||
| } | ||
|
|
||
| await tx.aiConversation.update({ | ||
| where: { id: params.conversationId }, | ||
| data: { updatedAt: new Date() }, | ||
| }); | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200 as const, | ||
| bodyType: "json" as const, | ||
| body: {}, | ||
| }; | ||
| }, | ||
| }); | ||
140 changes: 140 additions & 0 deletions
140
apps/backend/src/app/api/latest/internal/ai-conversations/[conversationId]/route.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { globalPrismaClient } from "@/prisma-client"; | ||
| import { createSmartRouteHandler } from "@/route-handlers/smart-route-handler"; | ||
| import { adaptSchema, yupArray, yupMixed, yupNumber, yupObject, yupString } from "@stackframe/stack-shared/dist/schema-fields"; | ||
| import { getOwnedConversation } from "../utils"; | ||
|
|
||
| export const GET = createSmartRouteHandler({ | ||
| metadata: { | ||
| summary: "Get AI conversation", | ||
| description: "Fetch a single AI conversation with all its messages", | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adaptSchema, | ||
| user: adaptSchema.defined(), | ||
| project: yupObject({ | ||
| id: yupString().oneOf(["internal"]).defined(), | ||
| }).defined(), | ||
| }).defined(), | ||
| params: yupObject({ | ||
| conversationId: yupString().defined(), | ||
| }), | ||
| method: yupString().oneOf(["GET"]).defined(), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({ | ||
| id: yupString().defined(), | ||
| title: yupString().defined(), | ||
| projectId: yupString().defined(), | ||
| messages: yupArray(yupObject({ | ||
| id: yupString().defined(), | ||
| role: yupString().defined(), | ||
| content: yupMixed().defined(), | ||
| }).noUnknown(false)).defined(), | ||
| }).defined(), | ||
| }), | ||
| handler: async ({ auth, params }) => { | ||
| const conversation = await getOwnedConversation(params.conversationId, auth.user.id); | ||
|
|
||
| const messages = await globalPrismaClient.aiMessage.findMany({ | ||
| where: { conversationId: conversation.id }, | ||
| orderBy: { position: "asc" }, | ||
| select: { | ||
| id: true, | ||
| role: true, | ||
| content: true, | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200 as const, | ||
| bodyType: "json" as const, | ||
| body: { | ||
| id: conversation.id, | ||
| title: conversation.title, | ||
| projectId: conversation.projectId, | ||
| messages: messages.map(m => ({ ...m, content: m.content as object })), | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| export const PATCH = createSmartRouteHandler({ | ||
| metadata: { | ||
| summary: "Update AI conversation", | ||
| description: "Update the title of an AI conversation", | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adaptSchema, | ||
| user: adaptSchema.defined(), | ||
| project: yupObject({ | ||
| id: yupString().oneOf(["internal"]).defined(), | ||
| }).defined(), | ||
| }).defined(), | ||
| params: yupObject({ | ||
| conversationId: yupString().defined(), | ||
| }), | ||
| body: yupObject({ | ||
| title: yupString().defined(), | ||
| }), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({}).defined(), | ||
| }), | ||
| handler: async ({ auth, params, body }) => { | ||
| await getOwnedConversation(params.conversationId, auth.user.id); | ||
|
|
||
| await globalPrismaClient.aiConversation.update({ | ||
| where: { id: params.conversationId }, | ||
| data: { title: body.title }, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200 as const, | ||
| bodyType: "json" as const, | ||
| body: {}, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| export const DELETE = createSmartRouteHandler({ | ||
| metadata: { | ||
| summary: "Delete AI conversation", | ||
| description: "Delete an AI conversation and all its messages", | ||
| }, | ||
| request: yupObject({ | ||
| auth: yupObject({ | ||
| type: adaptSchema, | ||
| user: adaptSchema.defined(), | ||
| project: yupObject({ | ||
| id: yupString().oneOf(["internal"]).defined(), | ||
| }).defined(), | ||
| }).defined(), | ||
| params: yupObject({ | ||
| conversationId: yupString().defined(), | ||
| }), | ||
| }), | ||
| response: yupObject({ | ||
| statusCode: yupNumber().oneOf([200]).defined(), | ||
| bodyType: yupString().oneOf(["json"]).defined(), | ||
| body: yupObject({}).defined(), | ||
| }), | ||
| handler: async ({ auth, params }) => { | ||
| await getOwnedConversation(params.conversationId, auth.user.id); | ||
|
|
||
| await globalPrismaClient.aiConversation.delete({ | ||
| where: { id: params.conversationId }, | ||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200 as const, | ||
| bodyType: "json" as const, | ||
| body: {}, | ||
| }; | ||
| }, | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.