|
|
import { z } from "zod"; |
|
|
import { config } from "$lib/server/config"; |
|
|
import JSON5 from "json5"; |
|
|
|
|
|
const sanitizeJSONEnv = (val: string, fallback: string) => { |
|
|
const raw = (val ?? "").trim(); |
|
|
const unquoted = raw.startsWith("`") && raw.endsWith("`") ? raw.slice(1, -1) : raw; |
|
|
return unquoted || fallback; |
|
|
}; |
|
|
|
|
|
|
|
|
export const usageLimitsSchema = z |
|
|
.object({ |
|
|
conversations: z.coerce.number().optional(), |
|
|
messages: z.coerce.number().optional(), |
|
|
messageLength: z.coerce.number().optional(), |
|
|
messagesPerMinute: z |
|
|
.preprocess((val) => { |
|
|
if (val === undefined) { |
|
|
return config.RATE_LIMIT; |
|
|
} |
|
|
return val; |
|
|
}, z.coerce.number().optional()) |
|
|
.optional(), |
|
|
}) |
|
|
.optional(); |
|
|
|
|
|
export const usageLimits = usageLimitsSchema.parse( |
|
|
JSON5.parse(sanitizeJSONEnv(config.USAGE_LIMITS, "{}")) |
|
|
); |
|
|
|