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%
2.0 KB · 34 lines typescript
Raw Blame History
1import { z } from "zod";2import { withUser, parseBody, json } from "@/lib/api";3import { listRegistryModels, favoriteModels, recentModels, toggleFavorite } from "@/lib/ai/registry";4import { modelLabels, setModelLabel } from "@/lib/ai/registry/user-models";5import { listConnections } from "@/lib/providers/keys";6import { listCustomModels, countValidEndpoints } from "@/lib/endpoints/service";78export const dynamic = "force-dynamic";910/** GET /api/models — the normalized registry plus the caller's favorites/recents/labels and connected providers. */11export const GET = withUser(async ({ req, user }) => {12  const includeDeprecated = new URL(req.url).searchParams.get("deprecated") === "1";13  const [models, favorites, recents, labels, connections, customModels, validEndpoints] = await Promise.all([listRegistryModels({ includeDeprecated }), favoriteModels(user.id), recentModels(user.id), modelLabels(user.id), listConnections(user.id), listCustomModels(user.id), countValidEndpoints(user.id)]);14  const connectedProviders = connections.filter((c) => c.status !== "invalid").map((c) => c.provider);15  // Custom OpenAI-compatible endpoints (Settings → Endpoints) appear as provider "custom", keyed `custom/<endpointId>:<modelId>`.16  if (validEndpoints > 0) connectedProviders.push("custom");17  return json({ models: [...models, ...customModels], favorites, recents, labels, connectedProviders });18});1920const bodySchema = z.discriminatedUnion("action", [21  z.object({ action: z.literal("toggle-favorite"), modelKey: z.string().max(120) }),22  z.object({ action: z.literal("set-label"), modelKey: z.string().max(120), label: z.string().max(60).nullable() }),23]);2425export const POST = withUser(async ({ req, user }) => {26  const body = await parseBody(req, bodySchema);27  if (body.action === "toggle-favorite") {28    const favorite = await toggleFavorite(user.id, body.modelKey);29    return json({ favorite });30  }31  await setModelLabel(user.id, body.modelKey, body.label);32  return json({ ok: true });33});34