import {z} from 'zod' export type GetApiV1ModelRouterFallbackEventsArgs = {agent_id?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1ModelRouterFallbackEventsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const fallbackEventResponse = z.object({id: z.string(), organization_id: z.string(), failed_model: z.string(), next_model: z.string().nullable(), error_class: z.string(), error_message: z.string().nullable(), invocation_type: z.string(), router_config_id: z.string().nullable(), session_id: z.string().nullable(), agent_id: z.string().nullable(), created_at: z.string()}); export const cursorPageFallbackEventResponse = z.object({items: z.array(fallbackEventResponse), next_cursor: z.string().nullable().optional(), has_more: z.boolean()}); 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 model fallback events for auditing * * GET /v1/model-router/fallback-events */ export const getApiV1ModelRouterFallbackEvents = async (args: GetApiV1ModelRouterFallbackEventsArgs, options: GetApiV1ModelRouterFallbackEventsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/model-router/fallback-events${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(cursorPageFallbackEventResponse, await res.json()) };