SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
4.1 KB · 94 lines typescript
Raw Blame History
1import { z } from "zod";2import { and, asc, eq } from "drizzle-orm";3import { withUser, parseBody, json, ApiError } from "@/lib/api";4import { getDb, modelPresets, promptPresets } from "@/db";5import { ids } from "@/lib/ids";6import { generationSettingsSchema } from "@/lib/chat/schemas";78export const dynamic = "force-dynamic";910/**11 * /api/presets?kind=model|prompt — CRUD for model presets and prompt presets.12 */13const kindSchema = z.enum(["model", "prompt"]);1415const modelPresetSchema = z.object({16  name: z.string().min(1).max(80),17  description: z.string().max(400).nullable().optional(),18  icon: z.string().max(16).nullable().optional(),19  modelKey: z.string().min(3).max(120),20  systemPrompt: z.string().max(50_000).nullable().optional(),21  parameters: generationSettingsSchema.optional(),22  tools: z.record(z.string(), z.unknown()).optional(),23  fileSettings: z.record(z.string(), z.unknown()).optional(),24});2526const promptPresetSchema = z.object({27  name: z.string().min(1).max(80),28  description: z.string().max(400).nullable().optional(),29  icon: z.string().max(16).nullable().optional(),30  systemPrompt: z.string().min(1).max(50_000),31  defaultModelKey: z.string().max(120).nullable().optional(),32  parameters: generationSettingsSchema.optional(),33  tools: z.record(z.string(), z.unknown()).optional(),34});3536export const GET = withUser(async ({ user }) => {37  const db = getDb();38  const [models, prompts] = await Promise.all([39    db.select().from(modelPresets).where(eq(modelPresets.userId, user.id)).orderBy(asc(modelPresets.sortOrder), asc(modelPresets.createdAt)),40    db.select().from(promptPresets).where(eq(promptPresets.userId, user.id)).orderBy(asc(promptPresets.sortOrder), asc(promptPresets.createdAt)),41  ]);42  return json({ modelPresets: models, promptPresets: prompts });43});4445export const POST = withUser(async ({ req, user }) => {46  const kind = kindSchema.parse(new URL(req.url).searchParams.get("kind"));47  const db = getDb();48  if (kind === "model") {49    const body = await parseBody(req, modelPresetSchema);50    const [row] = await db.insert(modelPresets).values({ id: ids.preset(), userId: user.id, ...body, parameters: body.parameters ?? {}, tools: body.tools ?? {}, fileSettings: body.fileSettings ?? {} }).returning();51    return json({ preset: row }, { status: 201 });52  }53  const body = await parseBody(req, promptPresetSchema);54  const [row] = await db.insert(promptPresets).values({ id: ids.prompt(), userId: user.id, ...body, parameters: body.parameters ?? {}, tools: body.tools ?? {} }).returning();55  return json({ preset: row }, { status: 201 });56});5758export const PATCH = withUser(async ({ req, user }) => {59  const url = new URL(req.url);60  const kind = kindSchema.parse(url.searchParams.get("kind"));61  const id = url.searchParams.get("id");62  if (!id) throw new ApiError(400, "Missing id");63  const db = getDb();64  if (kind === "model") {65    const body = await parseBody(req, modelPresetSchema.partial());66    const [row] = await db67      .update(modelPresets)68      .set({ ...body, updatedAt: new Date() })69      .where(and(eq(modelPresets.id, id), eq(modelPresets.userId, user.id)))70      .returning();71    if (!row) throw new ApiError(404, "Preset not found", "NOT_FOUND");72    return json({ preset: row });73  }74  const body = await parseBody(req, promptPresetSchema.partial());75  const [row] = await db76    .update(promptPresets)77    .set({ ...body, updatedAt: new Date() })78    .where(and(eq(promptPresets.id, id), eq(promptPresets.userId, user.id)))79    .returning();80  if (!row) throw new ApiError(404, "Preset not found", "NOT_FOUND");81  return json({ preset: row });82});8384export const DELETE = withUser(async ({ req, user }) => {85  const url = new URL(req.url);86  const kind = kindSchema.parse(url.searchParams.get("kind"));87  const id = url.searchParams.get("id");88  if (!id) throw new ApiError(400, "Missing id");89  const db = getDb();90  if (kind === "model") await db.delete(modelPresets).where(and(eq(modelPresets.id, id), eq(modelPresets.userId, user.id)));91  else await db.delete(promptPresets).where(and(eq(promptPresets.id, id), eq(promptPresets.userId, user.id)));92  return json({ ok: true });93});94