import {z} from 'zod' export type ApiKeyCredentials = {type: 'api-key', value: string}; export type FixedApiKeyProviderConfig = {credentials: ApiKeyCredentials}; export type FixedApiKeyProvider = {vendor: 'openai' | 'anthropic' | 'doubleword' | 'google-gla' | 'groq' | 'huggingface' | 'mistral', config: FixedApiKeyProviderConfig}; export type BedrockProviderConfig = {api: 'runtime' | 'mantle', region: string, credentials: ApiKeyCredentials}; export type BedrockProvider = {vendor: 'bedrock', config: BedrockProviderConfig}; export type AzureApiKeyCredentials = {type: 'api-key', value: string}; export type AzureProviderConfig = {endpoint: string, credentials: AzureApiKeyCredentials}; export type AzureProvider = {vendor: 'azure', config: AzureProviderConfig}; export type GoogleServiceAccountJsonCredentials = {type: 'service-account-json', value: string}; export type GoogleVertexProviderConfig = {endpoint: string, credentials: GoogleServiceAccountJsonCredentials}; export type GoogleVertexProvider = {vendor: 'google-vertex', config: GoogleVertexProviderConfig}; export type GatewayProviderPricingInput = {required: boolean}; export type GatewayProviderCreate = {slug: string, provider: FixedApiKeyProvider | BedrockProvider | AzureProvider | GoogleVertexProvider, pricing?: GatewayProviderPricingInput | undefined}; export type CreateApiV1GatewayProvidersArgs = {body: GatewayProviderCreate}; export type CreateApiV1GatewayProvidersOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fixedApiKeyProviderRead = z.object({vendor: z.enum(["openai", "anthropic", "doubleword", "google-gla", "groq", "huggingface", "mistral"])}); export const bedrockProviderReadConfig = z.object({api: z.enum(["runtime", "mantle"]), region: z.string().min(3).max(63)}); export const bedrockProviderRead = z.object({vendor: z.literal("bedrock"), config: bedrockProviderReadConfig}); export const azureProviderReadConfig = z.object({endpoint: z.string()}); export const azureProviderRead = z.object({vendor: z.literal("azure"), config: azureProviderReadConfig}); export const googleVertexProviderReadConfig = z.object({endpoint: z.string()}); export const googleVertexProviderRead = z.object({vendor: z.literal("google-vertex"), config: googleVertexProviderReadConfig}); export const gatewayProviderPricingOutput = z.object({required: z.boolean()}); export const gatewayProviderRead = z.object({id: z.string(), slug: z.string(), provider: z.discriminatedUnion("vendor", [fixedApiKeyProviderRead, bedrockProviderRead, azureProviderRead, googleVertexProviderRead]), pricing: gatewayProviderPricingOutput, created_at: z.string()}); 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 }; /** * Create Gateway Provider * * POST /v1/gateway/providers/ */ export const createApiV1GatewayProviders = async (args: CreateApiV1GatewayProvidersArgs, options: CreateApiV1GatewayProvidersOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/gateway/providers/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(gatewayProviderRead, await res.json()) };