|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { copilotChats } from '@sim/db/schema' |
| 3 | +import { createLogger } from '@sim/logger' |
| 4 | +import { and, desc, eq, isNull } from 'drizzle-orm' |
| 5 | +import { type NextRequest, NextResponse } from 'next/server' |
| 6 | +import { z } from 'zod' |
| 7 | +import { |
| 8 | + authenticateCopilotRequestSessionOnly, |
| 9 | + createBadRequestResponse, |
| 10 | + createInternalServerErrorResponse, |
| 11 | + createUnauthorizedResponse, |
| 12 | +} from '@/lib/copilot/request-helpers' |
| 13 | + |
| 14 | +const logger = createLogger('MothershipChatsAPI') |
| 15 | + |
| 16 | +/** |
| 17 | + * GET /api/mothership/chats?workspaceId=xxx |
| 18 | + * Returns mothership (home) chats for the authenticated user in the given workspace. |
| 19 | + */ |
| 20 | +export async function GET(request: NextRequest) { |
| 21 | + try { |
| 22 | + const { userId, isAuthenticated } = await authenticateCopilotRequestSessionOnly() |
| 23 | + if (!isAuthenticated || !userId) { |
| 24 | + return createUnauthorizedResponse() |
| 25 | + } |
| 26 | + |
| 27 | + const workspaceId = request.nextUrl.searchParams.get('workspaceId') |
| 28 | + if (!workspaceId) { |
| 29 | + return createBadRequestResponse('workspaceId is required') |
| 30 | + } |
| 31 | + |
| 32 | + const chats = await db |
| 33 | + .select({ |
| 34 | + id: copilotChats.id, |
| 35 | + title: copilotChats.title, |
| 36 | + updatedAt: copilotChats.updatedAt, |
| 37 | + }) |
| 38 | + .from(copilotChats) |
| 39 | + .where( |
| 40 | + and( |
| 41 | + eq(copilotChats.userId, userId), |
| 42 | + eq(copilotChats.workspaceId, workspaceId), |
| 43 | + isNull(copilotChats.workflowId) |
| 44 | + ) |
| 45 | + ) |
| 46 | + .orderBy(desc(copilotChats.updatedAt)) |
| 47 | + |
| 48 | + return NextResponse.json({ success: true, data: chats }) |
| 49 | + } catch (error) { |
| 50 | + logger.error('Error fetching mothership chats:', error) |
| 51 | + return createInternalServerErrorResponse('Failed to fetch chats') |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +const CreateChatSchema = z.object({ |
| 56 | + workspaceId: z.string().min(1), |
| 57 | +}) |
| 58 | + |
| 59 | +/** |
| 60 | + * POST /api/mothership/chats |
| 61 | + * Creates an empty mothership chat and returns its ID. |
| 62 | + */ |
| 63 | +export async function POST(request: NextRequest) { |
| 64 | + try { |
| 65 | + const { userId, isAuthenticated } = await authenticateCopilotRequestSessionOnly() |
| 66 | + if (!isAuthenticated || !userId) { |
| 67 | + return createUnauthorizedResponse() |
| 68 | + } |
| 69 | + |
| 70 | + const body = await request.json() |
| 71 | + const { workspaceId } = CreateChatSchema.parse(body) |
| 72 | + |
| 73 | + const [chat] = await db |
| 74 | + .insert(copilotChats) |
| 75 | + .values({ |
| 76 | + userId, |
| 77 | + workspaceId, |
| 78 | + title: null, |
| 79 | + model: 'claude-opus-4-5', |
| 80 | + messages: [], |
| 81 | + }) |
| 82 | + .returning({ id: copilotChats.id }) |
| 83 | + |
| 84 | + return NextResponse.json({ success: true, id: chat.id }) |
| 85 | + } catch (error) { |
| 86 | + if (error instanceof z.ZodError) { |
| 87 | + return createBadRequestResponse('workspaceId is required') |
| 88 | + } |
| 89 | + logger.error('Error creating mothership chat:', error) |
| 90 | + return createInternalServerErrorResponse('Failed to create chat') |
| 91 | + } |
| 92 | +} |
0 commit comments