SPB Git

Repository Public

settings.ts 2.1 KB 41 lines typescript

1/** Non-secret settings stored in the database. API keys are environment variables and never appear here. */2import { z } from "zod";3import { j, sql } from "@/lib/db";45export const EFFORTS = ["low", "medium", "high", "xhigh", "max"] as const;6export const SettingsSchema = z.object({7  /** Claude model id; checked against the live model list when it is changed. Empty = ANTHROPIC_MODEL / default. */8  aiModel: z.string().max(80).default(""),9  aiEffortGenerate: z.enum(EFFORTS).default("high"),10  aiEffortEdit: z.enum(EFFORTS).default("medium"),11  /** Upper bound on the output of one model call. */12  aiMaxOutputTokens: z.number().int().min(4000).max(128000).default(48000),13  /** Budgets in US dollars. A job is refused when the ledger shows the limit reached. 0 = no limit. */14  aiBudgetPerJobUsd: z.number().min(0).max(1000).default(5),15  aiBudgetDailyUsd: z.number().min(0).max(10000).default(25),16  aiBudgetMonthlyUsd: z.number().min(0).max(100000).default(200),17  /** Jobs the administrator may start per hour. */18  aiJobsPerHour: z.number().int().min(1).max(500).default(40),19  /** Wall-clock limit of one job, in minutes. */20  aiJobTimeoutMinutes: z.number().int().min(1).max(60).default(20),21  /** Counts page views per sheet and day (no visitor data). */22  analyticsEnabled: z.boolean().default(true),23});24export type Settings = z.infer<typeof SettingsSchema>;2526export async function getSettings(): Promise<Settings> {27  const rows = await sql<{ key: string; value: unknown }[]>`SELECT key, value FROM settings`;28  return SettingsSchema.parse(Object.fromEntries(rows.map((r) => [r.key, r.value])));29}3031export async function setSettings(patch: Partial<Settings>, userId: string | null): Promise<Settings> {32  const next = SettingsSchema.parse({ ...(await getSettings()), ...patch });33  await sql.begin(async (tx) => {34    for (const key of Object.keys(patch) as (keyof Settings)[]) {35      await tx`INSERT INTO settings (key, value, updated_by) VALUES (${key}, ${j(next[key])}, ${userId})36               ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = now(), updated_by = EXCLUDED.updated_by`;37    }38  });39  return next;40}41
Files main
Menu

Theme