import { db } from "./db.ts"; import { JsonSchema, jsonType } from "t_rest/server"; export const configSchema = { type: "object", properties: { adminUsernames: { type: "array", items: { type: "string" } }, pausedReason: { type: ["string", "null"] }, maxUserJobs: { type: "number" }, maxJobs: { type: "number" }, defaultParams: { type: "object", properties: { batch_size: { type: "number" }, n_iter: { type: "number" }, width: { type: "number" }, height: { type: "number" }, steps: { type: "number" }, cfg_scale: { type: "number" }, sampler_name: { type: "string" }, negative_prompt: { type: "string" }, }, }, }, required: ["adminUsernames", "maxUserJobs", "maxJobs", "defaultParams"], } as const satisfies JsonSchema; export type Config = jsonType; export async function getConfig(): Promise { const configEntry = await db.get(["config"]); const config = configEntry?.value; return { adminUsernames: config?.adminUsernames ?? Deno.env.get("TG_ADMIN_USERNAMES")?.split(",") ?? [], pausedReason: config?.pausedReason ?? null, maxUserJobs: config?.maxUserJobs ?? Infinity, maxJobs: config?.maxJobs ?? Infinity, defaultParams: config?.defaultParams ?? {}, }; } export async function setConfig(newConfig: Partial): Promise { const oldConfig = await getConfig(); const config: Config = { adminUsernames: newConfig.adminUsernames ?? oldConfig.adminUsernames, pausedReason: newConfig.pausedReason ?? oldConfig.pausedReason, maxUserJobs: newConfig.maxUserJobs ?? oldConfig.maxUserJobs, maxJobs: newConfig.maxJobs ?? oldConfig.maxJobs, defaultParams: newConfig.defaultParams ?? oldConfig.defaultParams, }; await db.set(["config"], config); }