import {z} from 'zod' export type GetApiApiV1ExamplesArgs = {user: string, program: string, page?: number | undefined, page_size?: number | undefined, version?: (number | null) | undefined, commit_hash?: (string | null) | undefined, commit_hashes?: (string | null) | undefined, search?: (string | null) | undefined, sort_by?: (string | null) | undefined, sort_order?: string | undefined, max_confidence?: (number | null) | undefined, search_mode?: ('keyword' | 'semantic') | undefined, hide_graded?: boolean | undefined, authorization?: (string | null) | undefined, modaic_session?: (string | null) | undefined}; export type GetApiApiV1ExamplesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const predictedExample = z.object({id: z.string().nullable().optional(), alt_id: z.string().nullable().optional(), arbiter_repo: z.string(), arbiter_hash: z.string().optional(), input: z.union([z.record(z.string(), z.unknown()), z.string()]).nullable().optional(), ground_truth: z.string().nullable().optional(), ground_reasoning: z.string().optional(), messages: z.array(z.record(z.string(), z.unknown())).nullable().optional(), serialized_output: z.string().nullable().optional(), reasoning: z.string().nullable().optional(), error: z.string().optional(), split: z.enum(["train", "test", "none"]).nullable().optional(), version: z.number().int().nullable().optional(), prediction_timestamp: z.string().optional(), confidence: z.number().nullable().optional()}); export const paginatedPage = z.object({items: z.array(predictedExample), total: z.number().int(), unfiltered_total: z.number().int(), page: z.number().int(), page_size: z.number().int(), total_pages: 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 Examples * * GET /api/v1/examples */ export const getApiApiV1Examples = async (args: GetApiApiV1ExamplesArgs, options: GetApiApiV1ExamplesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args.user != null) query.append("user", String(args.user)) if (args.program != null) query.append("program", String(args.program)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.version != null) query.append("version", String(args.version)) if (args.commit_hash != null) query.append("commit_hash", String(args.commit_hash)) if (args.commit_hashes != null) query.append("commit_hashes", String(args.commit_hashes)) if (args.search != null) query.append("search", String(args.search)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.max_confidence != null) query.append("max_confidence", String(args.max_confidence)) if (args.search_mode != null) query.append("search_mode", String(args.search_mode)) if (args.hide_graded != null) query.append("hide_graded", String(args.hide_graded)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (args.modaic_session != null) cookies.push("modaic_session=" + encodeURIComponent(String(args.modaic_session))) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/examples${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(paginatedPage, await res.json()) };