import {z} from 'zod' export type GetApiApiProvidersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const modelProvider = z.enum(["openai", "anthropic", "azure", "bedrock", "cohere", "vertex", "mistral", "ollama", "groq", "sgl"]); export const key = z.object({value: z.string(), weight: z.number().optional(), models: z.array(z.string()).optional(), azure_key_config: z.object({endpoint: z.string().optional(), deployments: z.object({}).optional(), api_version: z.string().optional()}).optional(), vertex_key_config: z.object({project_id: z.string().optional(), region: z.string().optional(), auth_credentials: z.string().optional()}).optional(), bedrock_key_config: z.object({access_key: z.string().optional(), secret_key: z.string().optional(), session_token: z.string().optional(), region: z.string().optional(), arn: z.string().optional(), deployments: z.object({}).optional()}).optional()}); export const networkConfig = z.object({timeout: z.number().int().optional(), max_retries: z.number().int().optional()}); export const concurrencyAndBufferSize = z.object({concurrency: z.number().int().optional(), buffer_size: z.number().int().optional()}); export const proxyConfig = z.object({url: z.string().optional(), username: z.string().optional(), password: z.string().optional()}); export const providerResponse = z.object({name: modelProvider, keys: z.array(key), network_config: networkConfig, concurrency_and_buffer_size: concurrencyAndBufferSize, proxy_config: proxyConfig.optional()}); export const listProvidersResponse = z.object({providers: z.array(providerResponse), 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 Providers * * GET /api/providers */ export const getApiApiProviders = async (options?: GetApiApiProvidersOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/providers${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(listProvidersResponse, await res.json()) };