import {z} from 'zod' export type QuotaConfig = {enabled?: boolean | undefined, max_resident_memory_percent?: (number | null) | undefined, max_disk_usage_percent?: (number | null) | undefined, release_margin_percent?: (number | null) | undefined}; export type UpdateApiQuotasArgs = {wait?: boolean | undefined, body: QuotaConfig}; export type UpdateApiQuotasOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const hardwareUsage = z.object({cpu: z.number().int().gte(0), payload_io_read: z.number().int().gte(0), payload_io_write: z.number().int().gte(0), payload_index_io_read: z.number().int().gte(0), payload_index_io_write: z.number().int().gte(0), vector_io_read: z.number().int().gte(0), vector_io_write: z.number().int().gte(0)}); export const modelUsage = z.object({tokens: z.number().int().gte(0)}); export const inferenceUsage = z.object({models: z.record(z.string(), modelUsage)}); export const usage = z.object({hardware: z.union([hardwareUsage, z.unknown()]).optional(), inference: z.union([inferenceUsage, z.unknown()]).optional()}); export const updateApiQuotasResponse = z.object({usage: z.union([usage, z.unknown()]).optional(), time: z.number().optional(), status: z.string().optional(), result: z.boolean().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Set global quotas * * PUT /quotas * * Also accepts auth: bearerAuth */ export const updateApiQuotas = async (args: UpdateApiQuotasArgs, options?: UpdateApiQuotasOptions) => { const {baseUrl = "http://localhost:6333", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.wait != null) query.append("wait", String(args.wait)) if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/quotas${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiQuotasResponse, await res.json()) };