import {z} from 'zod' export type GetApiGuardrailsViolationsArgs = {offset?: number | undefined, limit?: number | undefined, interval?: string | undefined, type_filter?: string | undefined, collection_id_filter?: string | undefined, user_id_filter?: string | undefined}; export type GetApiGuardrailsViolationsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const guardrailViolation = z.object({id: z.string().optional(), created_at: z.string().optional(), user_id: z.string().optional(), username: z.string().optional(), email: z.string().optional(), collection_id: z.string().nullable().optional(), collection_name: z.string().optional(), violation_type: z.string().optional(), violation_message: z.string().optional()}); export const guardrailViolationsList = z.object({total: z.number().int().optional(), items: z.array(guardrailViolation).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 }; /** * List guardrail violations. * * GET /guardrails/violations */ export const getApiGuardrailsViolations = async (args: GetApiGuardrailsViolationsArgs, options?: GetApiGuardrailsViolationsOptions) => { const {baseUrl = "https://h2ogpte.genai.h2o.ai/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.interval != null) query.append("interval", String(args.interval)) if (args.type_filter != null) query.append("type_filter", String(args.type_filter)) if (args.collection_id_filter != null) query.append("collection_id_filter", String(args.collection_id_filter)) if (args.user_id_filter != null) query.append("user_id_filter", String(args.user_id_filter)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/guardrails/violations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(guardrailViolationsList, await res.json()) };