import {z} from 'zod' export type ApiKeyCredentials = {type: 'api-key', value: string}; export type FixedApiKeyProviderPatchConfig = {credentials: ApiKeyCredentials}; export type FixedApiKeyProviderPatch = {vendor: 'openai' | 'anthropic' | 'doubleword' | 'google-gla' | 'groq' | 'huggingface' | 'mistral', config: FixedApiKeyProviderPatchConfig}; export type BedrockApiProviderPatchConfig = {api: 'runtime' | 'mantle', region?: string | undefined, credentials?: ApiKeyCredentials | undefined}; export type BedrockRegionProviderPatchConfig = {region: string, api?: ('runtime' | 'mantle') | undefined, credentials?: ApiKeyCredentials | undefined}; export type BedrockCredentialsProviderPatchConfig = {credentials: ApiKeyCredentials, api?: ('runtime' | 'mantle') | undefined, region?: string | undefined}; export type BedrockProviderPatch = {vendor: 'bedrock', config: BedrockApiProviderPatchConfig | BedrockRegionProviderPatchConfig | BedrockCredentialsProviderPatchConfig}; export type AzureApiKeyCredentials = {type: 'api-key', value: string}; export type AzureProviderPatchConfig = {credentials: AzureApiKeyCredentials, endpoint?: string | undefined}; export type AzureProviderPatch = {vendor: 'azure', config: AzureProviderPatchConfig}; export type GoogleServiceAccountJsonCredentials = {type: 'service-account-json', value: string}; export type GoogleVertexProviderPatchConfig = {credentials: GoogleServiceAccountJsonCredentials, endpoint?: string | undefined}; export type GoogleVertexProviderPatch = {vendor: 'google-vertex', config: GoogleVertexProviderPatchConfig}; export type GatewayProviderPricingInput = {required: boolean}; export type GatewayProviderConfigurationUpdate = {provider: FixedApiKeyProviderPatch | BedrockProviderPatch | AzureProviderPatch | GoogleVertexProviderPatch, pricing?: GatewayProviderPricingInput | undefined}; export type GatewayProviderPricingUpdate = {pricing: GatewayProviderPricingInput, provider?: (FixedApiKeyProviderPatch | BedrockProviderPatch | AzureProviderPatch | GoogleVertexProviderPatch) | undefined}; export type PatchApiV1GatewayProvidersProviderIdArgs = {provider_id: string, body: GatewayProviderConfigurationUpdate | GatewayProviderPricingUpdate}; export type PatchApiV1GatewayProvidersProviderIdOptions = {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 }; /** * Update Gateway Provider * * PATCH /v1/gateway/providers/{provider_id}/ */ export const patchApiV1GatewayProvidersProviderId = async (args: PatchApiV1GatewayProvidersProviderIdArgs, options: PatchApiV1GatewayProvidersProviderIdOptions) => { 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/${encodeURIComponent(String(args.provider_id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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()) };