import {z} from 'zod' export type GetApiApiV2ProvidersOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aiGatewayCustomProviderSupportedOperation = z.enum(["OPENAI_CHAT_COMPLETIONS", "OPENAI_RESPONSES", "ANTHROPIC_MESSAGES"]); export const aiGatewayProviderIntegrationConfig = z.object({bedrock_region: z.string().max(64).optional(), foundry_project: z.string().max(256).optional(), foundry_resource_id: z.string().max(512).optional(), location: z.string().max(256).optional(), project_id: z.string().max(256).optional(), supported_operations: z.array(aiGatewayCustomProviderSupportedOperation).optional()}); export const aiGatewayKnownProvider = z.enum(["custom", "anthropic", "openai", "azure_openai", "bedrock", "gemini", "baseten", "fireworks", "together", "typesafe", "azure_foundry"]); export const aiGatewayProviderAttributes = z.object({cost_multiplier: z.number(), created_at: z.string(), integration_account_id: z.string(), integration_config: aiGatewayProviderIntegrationConfig.optional(), known_provider: aiGatewayKnownProvider, name: z.string(), slug: z.string(), updated_at: z.string()}); export const aiGatewayModelCatalogEntryType = z.literal("model-catalog-entries"); export const aiGatewayModelCatalogEntryIdentifier = z.object({id: z.string(), type: aiGatewayModelCatalogEntryType}); export const aiGatewayProviderModelsRelationshipData = z.array(aiGatewayModelCatalogEntryIdentifier); export const aiGatewayProviderModelsRelationship = z.object({data: aiGatewayProviderModelsRelationshipData}); export const aiGatewayProviderRelationships = z.object({models: aiGatewayProviderModelsRelationship}); export const aiGatewayProviderType = z.literal("providers"); export const aiGatewayProviderResource = z.object({attributes: aiGatewayProviderAttributes, id: z.string(), relationships: aiGatewayProviderRelationships.optional(), type: aiGatewayProviderType}); export const aiGatewayProviderListResponse = z.object({data: z.array(aiGatewayProviderResource)}); 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 AI Gateway providers * * GET /api/v2/providers * * Also accepts auth: AuthZ */ export const getApiApiV2Providers = async (options?: GetApiApiV2ProvidersOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/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(aiGatewayProviderListResponse, await res.json()) };