import { z } from "zod"; import { guarded, json, body } from "@/lib/api"; import { sql } from "@/lib/db"; import { newId } from "@/lib/ids"; export const GET = guarded(async () => { const projects = await sql`SELECT p.*, (SELECT count(*)::int FROM conversations c WHERE c.project_id = p.id AND c.archived_at IS NULL) AS conversation_count, (SELECT count(*)::int FROM attachments a WHERE a.project_id = p.id) AS file_count FROM projects p WHERE archived_at IS NULL ORDER BY sort_order, created_at`; return json({ projects }); }); export const POST = guarded(async (req) => { const b = await body(req, z.object({ name: z.string().min(1).max(120), description: z.string().max(2000).optional(), instructions: z.string().max(20_000).optional(), defaultModel: z.string().nullable().optional(), color: z.string().max(20).optional(), icon: z.string().max(20).optional() })); const id = newId("proj"); const rows = await sql`INSERT INTO projects (id, name, description, instructions, default_model, color, icon) VALUES (${id}, ${b.name}, ${b.description ?? null}, ${b.instructions ?? null}, ${b.defaultModel ?? null}, ${b.color ?? null}, ${b.icon ?? null}) RETURNING *`; return json({ project: rows[0] }, { status: 201 }); });