import { Elysia, t } from "elysia"; import { deepMerge } from "std/collections/deep_merge.ts"; import { info } from "std/log/mod.ts"; import { getConfig, setConfig } from "../app/config.ts"; import { omitUndef } from "../utils/omitUndef.ts"; import { withAdmin } from "./withUser.ts"; const paramsSchema = t.Partial(t.Object({ batch_size: t.Number(), n_iter: t.Number(), width: t.Number(), height: t.Number(), steps: t.Number(), cfg_scale: t.Number(), sampler_name: t.String(), negative_prompt: t.String(), })); export const paramsRoute = new Elysia() .get( "", async () => { const config = await getConfig(); return omitUndef(config.defaultParams); }, { response: paramsSchema, }, ) .patch( "", async ({ query, body }) => { return withAdmin(query, async (user) => { const config = await getConfig(); info(`User ${user.first_name} updated default params: ${JSON.stringify(body)}`); const defaultParams = deepMerge(config.defaultParams ?? {}, body); await setConfig({ defaultParams }); return omitUndef(config.defaultParams); }); }, { query: t.Object({ sessionId: t.String() }), body: paramsSchema, response: paramsSchema, }, );