import { NextResponse } from "next/server"; import { z } from "zod"; import { apiError, parseBody } from "@/lib/api.ts"; import { assertSameOrigin, requireRole } from "@/lib/auth/session.ts"; import { getPrompt, listPromptNames, promptHistory, savePromptVersion } from "@/lib/prompts.ts"; export async function GET(req: Request) { try { await requireRole("instructor"); const url = new URL(req.url); const name = url.searchParams.get("name"); if (name) { return NextResponse.json({ name, content: getPrompt(name), history: promptHistory(name) }); } return NextResponse.json({ names: listPromptNames() }); } catch (e) { return apiError(e); } } const schema = z.object({ name: z.string().min(1).max(80).regex(/^[a-z0-9-]+$/), content: z.string().min(10).max(20_000), }); export async function POST(req: Request) { try { await assertSameOrigin(); const user = await requireRole("admin"); const body = await parseBody(req, schema); const version = savePromptVersion(body.name, body.content, user.username); return NextResponse.json({ ok: true, version }); } catch (e) { return apiError(e); } }