import {z} from 'zod' export type GetApiApiV1ModelsArgs = {prefixes?: string | undefined, providers?: string | undefined, type?: string | undefined, capability?: string | undefined, deprecated?: string | undefined, project?: string | undefined, format?: ('json' | 'csv' | 'jsonl') | undefined, headers?: string | undefined, 'fill-with-zeros'?: string | undefined, pretty?: string | undefined}; export type GetApiApiV1ModelsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiApiV1ModelsResponse = z.array(z.object({provider_id: z.string().optional(), provider_name: z.string().optional(), model_id: z.string().optional(), model_name: z.string().optional(), max_input_tokens: z.number().nullable().optional(), max_output_tokens: z.number().nullable().optional(), input_cost_per_token: z.number().nullable().optional(), input_cost_per_million: z.number().nullable().optional(), output_cost_per_token: z.number().optional(), output_cost_per_million: z.number().optional(), cache_read_cost_per_token: z.number().nullable().optional(), cache_read_cost_per_million: z.number().nullable().optional(), cache_write_cost_per_token: z.number().nullable().optional(), cache_write_cost_per_million: z.number().nullable().optional(), supports_function_calling: z.boolean().optional(), supports_vision: z.boolean().optional(), supports_json_mode: z.boolean().optional(), supports_parallel_functions: z.boolean().optional(), model_type: z.string().optional(), deprecation_date: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown()))); 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 models * * GET /api/v1/models */ export const getApiApiV1Models = async (args: GetApiApiV1ModelsArgs, options: GetApiApiV1ModelsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.prefixes != null) query.append("prefixes", String(args.prefixes)) if (args.providers != null) query.append("providers", String(args.providers)) if (args.type != null) query.append("type", String(args.type)) if (args.capability != null) query.append("capability", String(args.capability)) if (args.deprecated != null) query.append("deprecated", String(args.deprecated)) if (args.project != null) query.append("project", String(args.project)) if (args.format != null) query.append("format", String(args.format)) if (args.headers != null) query.append("headers", String(args.headers)) if (args['fill-with-zeros'] != null) query.append("fill-with-zeros", String(args['fill-with-zeros'])) if (args.pretty != null) query.append("pretty", String(args.pretty)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/models${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(getApiApiV1ModelsResponse, await res.json()) };