import {z} from 'zod' export type EvaluatorRef = {execute_on: 'input' | 'output' | 'both', id: string, is_guardrail?: boolean | undefined, options?: {[key: string]: unknown} | undefined, sample_rate?: number | undefined, timeout?: number | undefined}; export type BudgetLimit = {amount: number, currency: 'usd', period: 'hour' | 'day' | 'week' | 'month'}; export type RequestLimit = {amount: number, period: 'hour' | 'day' | 'week' | 'month'}; export type TokenLimit = {amount: number, period: 'hour' | 'day' | 'week' | 'month'}; export type Limits = {budget?: BudgetLimit | undefined, requests?: RequestLimit | undefined, tokens?: TokenLimit | undefined}; export type ModelRef = {display_name?: string | undefined, integration_id?: string | undefined, model: string, weight?: number | undefined}; export type ModelsConfig = {fallback_on_codes?: (Array | null) | undefined, mode: 'fallback' | 'latency_based' | 'weighted' | 'round_robin', models: Array | null}; export type PolicyRetryConfig = {count: number, on_codes?: (Array | null) | undefined}; export type PatchApiV2PoliciesPolicyIdArgs = {policy_id: string, body: {description?: string | undefined, display_name?: string | undefined, enabled?: boolean | undefined, evaluators?: Array | undefined, limits?: Limits | undefined, models_config?: ModelsConfig | undefined, project_id?: string | undefined, retry_config?: PolicyRetryConfig | undefined, timeout?: number | undefined}}; export type PatchApiV2PoliciesPolicyIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const evaluatorRef = z.object({execute_on: z.enum(["input", "output", "both"]), id: z.string().min(1), is_guardrail: z.boolean().optional(), options: z.record(z.string(), z.unknown()).optional(), sample_rate: z.number().gte(0).lte(1).optional(), timeout: z.number().int().gte(1000).lte(600000).optional()}); export const budgetLimit = z.object({amount: z.number().gte(0.01).lte(1000000), currency: z.literal("usd"), period: z.enum(["hour", "day", "week", "month"])}); export const requestLimit = z.object({amount: z.number().int().gte(1).lte(1000000000), period: z.enum(["hour", "day", "week", "month"])}); export const tokenLimit = z.object({amount: z.number().int().gte(1).lte(1000000000), period: z.enum(["hour", "day", "week", "month"])}); export const limits = z.object({budget: budgetLimit.optional(), requests: requestLimit.optional(), tokens: tokenLimit.optional()}); export const modelRef = z.object({display_name: z.string().optional(), integration_id: z.string().optional(), model: z.string().min(1), weight: z.number().gte(0).lte(1).optional()}); export const modelsConfig = z.object({fallback_on_codes: z.array(z.number().int()).nullable().optional(), mode: z.enum(["fallback", "latency_based", "weighted", "round_robin"]), models: z.array(modelRef).nullable()}); export const policyRetryConfig = z.object({count: z.number().int().gte(1).lte(5), on_codes: z.array(z.number().int()).nullable().optional()}); export const patchApiV2PoliciesPolicyIdResponse = z.object({_id: z.string(), created_at: z.string(), created_by_id: z.string(), description: z.string().max(2048).optional(), display_name: z.string().min(1).max(256), enabled: z.boolean(), evaluators: z.array(evaluatorRef).nullable().optional(), limits: limits.optional(), models_config: modelsConfig.optional(), project_id: z.string(), retry_config: policyRetryConfig.optional(), slug: z.string().min(1), timeout: z.number().int().gte(1000), updated_at: z.string(), updated_by_id: z.string()}); 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 }; /** * Update policy * * PATCH /v2/policies/{policy_id} */ export const patchApiV2PoliciesPolicyId = async (args: PatchApiV2PoliciesPolicyIdArgs, options?: PatchApiV2PoliciesPolicyIdOptions) => { const {baseUrl = "https://my.orq.ai", 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 (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/policies/${encodeURIComponent(String(args.policy_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV2PoliciesPolicyIdResponse, await res.json()) };