SPB Git
1.8 KB · 23 lines typescript
1import { z } from "zod";2import { guarded, json, body } from "@/lib/api";3import { sql } from "@/lib/db";4import { newId } from "@/lib/ids";56export const GET = guarded(async (req) => {7  const p = req.nextUrl.searchParams;8  const conv = p.get("conversation"); const project = p.get("project"); const saved = p.get("saved") === "1";9  const rows = await sql`SELECT id, conversation_id, message_id, project_id, root_id, version, type, language, title, saved, created_at, length(content) AS size FROM artifacts10    WHERE (${!conv} OR conversation_id = ${conv ?? ""}) AND (${!project} OR project_id = ${project ?? ""}) AND (${!saved} OR saved) ORDER BY created_at DESC LIMIT 300`;11  return json({ artifacts: rows });12});1314/** Create a new version of an artifact (edited content) or a brand-new artifact. */15export const POST = guarded(async (req) => {16  const b = await body(req, z.object({ rootId: z.string().optional(), conversationId: z.string().nullable().optional(), projectId: z.string().nullable().optional(), type: z.string(), language: z.string().optional(), title: z.string().min(1).max(120), content: z.string().max(2_000_000), saved: z.boolean().optional() }));17  const id = newId("art");18  let version = 1; let root = id;19  if (b.rootId) { const prev = await sql<{ version: number }[]>`SELECT max(version)::int AS version FROM artifacts WHERE root_id = ${b.rootId}`; version = (prev[0]?.version ?? 0) + 1; root = b.rootId; }20  await sql`INSERT INTO artifacts (id, conversation_id, project_id, root_id, version, type, language, title, content, saved) VALUES (${id}, ${b.conversationId ?? null}, ${b.projectId ?? null}, ${root}, ${version}, ${b.type}, ${b.language ?? null}, ${b.title}, ${b.content}, ${b.saved ?? false})`;21  return json({ id, rootId: root, version }, { status: 201 });22});23
Files · main
Menu

Theme