2023-09-22 02:59:22 +00:00
|
|
|
import { db } from "./db.ts";
|
2023-10-05 09:00:51 +00:00
|
|
|
import { JsonSchema, jsonType } from "t_rest/server";
|
2023-09-22 02:59:22 +00:00
|
|
|
|
2023-10-05 09:00:51 +00:00
|
|
|
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" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sdInstances: {
|
|
|
|
type: "object",
|
|
|
|
additionalProperties: {
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
name: { type: "string" },
|
|
|
|
api: {
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
url: { type: "string" },
|
|
|
|
auth: { type: "string" },
|
|
|
|
},
|
|
|
|
required: ["url"],
|
|
|
|
},
|
|
|
|
maxResolution: { type: "number" },
|
|
|
|
},
|
|
|
|
required: ["api", "maxResolution"],
|
|
|
|
},
|
2023-09-22 02:59:22 +00:00
|
|
|
},
|
2023-10-05 09:00:51 +00:00
|
|
|
},
|
|
|
|
required: ["adminUsernames", "maxUserJobs", "maxJobs", "defaultParams", "sdInstances"],
|
|
|
|
} as const satisfies JsonSchema;
|
|
|
|
|
|
|
|
export type Config = jsonType<typeof configSchema>;
|
2023-09-22 02:59:22 +00:00
|
|
|
|
2023-10-05 09:00:51 +00:00
|
|
|
export async function getConfig(): Promise<Config> {
|
|
|
|
const configEntry = await db.get<Config>(["config"]);
|
|
|
|
const config = configEntry?.value;
|
|
|
|
return {
|
2023-10-12 09:39:19 +00:00
|
|
|
adminUsernames: config?.adminUsernames ?? Deno.env.get("TG_ADMIN_USERNAMES")?.split(",") ?? [],
|
2023-10-05 09:00:51 +00:00
|
|
|
pausedReason: config?.pausedReason ?? null,
|
|
|
|
maxUserJobs: config?.maxUserJobs ?? Infinity,
|
|
|
|
maxJobs: config?.maxJobs ?? Infinity,
|
|
|
|
defaultParams: config?.defaultParams ?? {},
|
2023-10-12 09:39:19 +00:00
|
|
|
sdInstances: config?.sdInstances ??
|
|
|
|
{
|
|
|
|
"local": {
|
|
|
|
api: { url: Deno.env.get("SD_API_URL") ?? "http://127.0.0.1:7860/" },
|
|
|
|
maxResolution: 1024 * 1024,
|
|
|
|
},
|
|
|
|
},
|
2023-10-05 09:00:51 +00:00
|
|
|
};
|
2023-09-22 02:59:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 09:00:51 +00:00
|
|
|
export async function setConfig(newConfig: Partial<Config>): Promise<void> {
|
|
|
|
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,
|
|
|
|
sdInstances: newConfig.sdInstances ?? oldConfig.sdInstances,
|
|
|
|
};
|
2023-09-22 02:59:22 +00:00
|
|
|
await db.set(["config"], config);
|
|
|
|
}
|