route.ts
1,371 bytes
| 1 | import { NextResponse } from 'next/server'; |
|---|---|
| 2 | import { z } from 'zod'; |
| 3 | import { currentUserId } from '@/auth'; |
| 4 | import { modelSlugSchema } from '@/core/schemas'; |
| 5 | import { listPresets, upsertPreset } from '@/db/repositories'; |
| 6 | |
| 7 | export const runtime = 'nodejs'; |
| 8 | |
| 9 | export async function GET() { |
| 10 | const userId = await currentUserId(); |
| 11 | if (!userId) return NextResponse.json({ presets: [] }); |
| 12 | return NextResponse.json({ presets: await listPresets(userId) }); |
| 13 | } |
| 14 | |
| 15 | const presetSchema = z.object({ |
| 16 | name: z.string().min(1).max(80), |
| 17 | models: z.array(modelSlugSchema).min(3).max(6), |
| 18 | chairmanModel: modelSlugSchema, |
| 19 | convergenceModel: modelSlugSchema.nullable().optional(), |
| 20 | maxRounds: z.number().int().min(1).max(5).optional(), |
| 21 | convergenceThreshold: z.number().int().min(0).max(100).optional(), |
| 22 | temperature: z.number().min(0).max(2).optional(), |
| 23 | }); |
| 24 | |
| 25 | export async function POST(request: Request) { |
| 26 | const userId = await currentUserId(); |
| 27 | if (!userId) return NextResponse.json({ error: 'Sign in to save presets' }, { status: 401 }); |
| 28 | |
| 29 | const body = await request.json().catch(() => null); |
| 30 | const parsed = presetSchema.safeParse(body); |
| 31 | if (!parsed.success) { |
| 32 | return NextResponse.json({ error: 'Invalid preset', issues: parsed.error.flatten() }, { status: 400 }); |
| 33 | } |
| 34 | const preset = await upsertPreset(userId, parsed.data); |
| 35 | return NextResponse.json({ preset }); |
| 36 | } |
| 37 | |