import {z} from 'zod' export type CreateBudgetRequest = {max_limit: number, reset_duration: string, calendar_aligned?: boolean | undefined}; export type CreateRateLimitRequest = {token_max_limit?: number | undefined, token_reset_duration?: string | undefined, request_max_limit?: number | undefined, request_reset_duration?: string | undefined}; export type CreateModelConfigRequest = {model_name: string, provider?: string | undefined, scope?: ('global' | 'virtual_key' | 'user') | undefined, scope_id?: string | undefined, budgets?: Array | undefined, rate_limit?: CreateRateLimitRequest | undefined}; export type CreateApiApiGovernanceModelConfigsArgs = {body: CreateModelConfigRequest}; export type CreateApiApiGovernanceModelConfigsOptions = {baseUrl?: string | undefined, ManagementBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const budget = z.object({id: z.string(), max_limit: z.number(), reset_duration: z.string(), calendar_aligned: z.boolean().optional(), last_reset: z.string(), current_usage: z.number(), team_id: z.string().optional(), virtual_key_id: z.string().optional(), provider_config_id: z.number().int().optional(), config_hash: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const rateLimit = z.object({id: z.string(), token_max_limit: z.number().int().optional(), token_reset_duration: z.string().optional(), token_current_usage: z.number().int(), token_last_reset: z.string(), request_max_limit: z.number().int().nullable().optional(), request_reset_duration: z.string().nullable().optional(), request_current_usage: z.number().int(), request_last_reset: z.string(), config_hash: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); export const modelConfig = z.object({id: z.string().optional(), model_name: z.string().optional(), provider: z.string().optional(), scope: z.enum(["global", "virtual_key", "user"]).optional(), scope_id: z.string().nullable().optional(), scope_name: z.string().nullable().optional(), calendar_aligned: z.boolean().optional(), budgets: z.array(budget).optional(), budget: budget.optional(), rate_limit: rateLimit.optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const modelConfigResponse = z.object({message: z.string().optional(), model_config: modelConfig.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 }; /** * Create model config * * POST /api/governance/model-configs */ export const createApiApiGovernanceModelConfigs = async (args: CreateApiApiGovernanceModelConfigsArgs, options?: CreateApiApiGovernanceModelConfigsOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, ManagementBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ManagementBearerAuth !== undefined) headers.set('authorization', `Bearer ${ManagementBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/governance/model-configs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(modelConfigResponse, await res.json()) };