import {z} from 'zod' export type GetApiV1PublicInferenceModelsArgs = {provider?: string | undefined, ranked?: boolean | undefined}; export type GetApiV1PublicInferenceModelsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const inferenceModelsResponse = z.object({models: z.array(z.string()), aliases: z.record(z.string(), z.string()).optional()}); export const rankedInferenceModel = z.object({id: z.string(), rank_metric: z.record(z.string(), z.unknown()).optional(), healthy: z.boolean().optional()}); export const rankedInferenceModelsResponse = z.object({models: z.array(rankedInferenceModel), ranked_by: z.string().nullable().optional(), as_of: z.string().nullable().optional()}); export const getApiV1PublicInferenceModelsResponse = z.union([inferenceModelsResponse, rankedInferenceModelsResponse]); 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 allowed inference models * * GET /v1/public/inference/models */ export const getApiV1PublicInferenceModels = async (args: GetApiV1PublicInferenceModelsArgs, options: GetApiV1PublicInferenceModelsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.provider != null) query.append("provider", String(args.provider)) if (args.ranked != null) query.append("ranked", String(args.ranked)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/public/inference/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(getApiV1PublicInferenceModelsResponse, await res.json()) };