import {z} from 'zod' export type GetApiUsageInferencesArgs = {created_after: string, created_before: string, limit?: number | undefined, offset?: number | undefined, endpoint?: (Array<'detect' | 'locate' | 'element_locate' | 'agent_chat'> | null) | undefined, model?: string | undefined, search?: string | undefined, session_id?: string | undefined, order_by?: string | undefined}; export type GetApiUsageInferencesOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usageInference = z.object({api_key_id: z.string().optional(), cost_microdollars: z.number().int(), created_at: z.string(), endpoint: z.enum(["detect", "locate", "element_locate", "agent_chat"]), inference_id: z.string(), latency_ms: z.number().int(), model: z.string(), ocr_engine: z.enum(["free", "premium"]).optional(), ocr_pages: z.number().int(), session_id: z.string().optional(), vlm_model: z.string().optional()}); export const usageInferencesResponse = z.object({inferences: z.array(usageInference).nullable(), limit: z.number().int(), offset: z.number().int(), total: z.number().int()}); 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 inference calls * * GET /usage/inferences */ export const getApiUsageInferences = async (args: GetApiUsageInferencesArgs, options?: GetApiUsageInferencesOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.endpoint != null) query.append("endpoint", args.endpoint.join(',')) if (args.model != null) query.append("model", String(args.model)) if (args.search != null) query.append("search", String(args.search)) if (args.session_id != null) query.append("session_id", String(args.session_id)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/usage/inferences${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(usageInferencesResponse, await res.json()) };