import {z} from 'zod' export type GetApiV1InferencesArgs = {limit?: number | undefined, offset?: number | undefined, model_id?: (string | null) | undefined, task?: (string | null) | undefined, project_id?: (string | null) | undefined, training_job_id?: (string | null) | undefined, latency_min?: (number | null) | undefined, latency_max?: (number | null) | undefined, llmaj_score_min?: (number | null) | undefined, llmaj_score_max?: (number | null) | undefined, since?: (string | null) | undefined, until?: (string | null) | undefined}; export type GetApiV1InferencesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const inferenceRecord = z.object({id: z.string(), user_id: z.string(), model_id: z.string(), model_name: z.string().nullable().optional(), task: z.string().nullable().optional(), input: z.string(), output: z.unknown(), latency_ms: z.number().int().nullable().optional(), ttft_ms: z.number().int().nullable().optional(), tokens: z.number().int().nullable().optional(), input_tokens: z.number().int().nullable().optional(), output_tokens: z.number().int().nullable().optional(), cache_read_tokens: z.number().int().nullable().optional(), cache_write_tokens: z.number().int().nullable().optional(), source: z.string().optional(), status: z.string().optional(), error_type: z.string().nullable().optional(), error_message: z.string().nullable().optional(), created_at: z.string(), project_id: z.string().nullable().optional(), training_job_id: z.string().nullable().optional(), provider: z.string().nullable().optional(), base_model: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), human_verdict: z.string().nullable().optional(), human_corrected_output: z.unknown(), human_feedback_notes: z.string().nullable().optional(), human_feedback_at: z.string().nullable().optional(), llmaj_verdict: z.enum(["pass", "fail", "uncertain"]).nullable().optional(), llmaj_score: z.number().nullable().optional(), llmaj_judged_at: z.string().nullable().optional(), llmaj_reasoning: z.string().nullable().optional()}); export const inferenceListResponse = z.object({inferences: z.array(inferenceRecord), total: z.number().int(), limit: z.number().int(), offset: 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 History * * GET /v1/inferences * * Also accepts auth: BearerAuth */ export const getApiV1Inferences = async (args: GetApiV1InferencesArgs, options: GetApiV1InferencesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.model_id != null) query.append("model_id", String(args.model_id)) if (args.task != null) query.append("task", String(args.task)) if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.training_job_id != null) query.append("training_job_id", String(args.training_job_id)) if (args.latency_min != null) query.append("latency_min", String(args.latency_min)) if (args.latency_max != null) query.append("latency_max", String(args.latency_max)) if (args.llmaj_score_min != null) query.append("llmaj_score_min", String(args.llmaj_score_min)) if (args.llmaj_score_max != null) query.append("llmaj_score_max", String(args.llmaj_score_max)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(inferenceListResponse, await res.json()) };