import {z} from 'zod' export type GetApiApiV2ProviderModelsArgs = {integration_type: string, integration_account_id: string, project_id?: string | undefined, location?: string | undefined, bedrock_region?: string | undefined, foundry_resource_id?: string | undefined}; export type GetApiApiV2ProviderModelsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aiGatewaySupportedOperation = z.enum(["ANTHROPIC_MESSAGES", "ANTHROPIC_COUNT_TOKENS", "OPENAI_CHAT_COMPLETIONS", "OPENAI_RESPONSES", "OPENAI_WEB_SEARCH", "OPENAI_EMBEDDINGS", "GEMINI_GENERATE_CONTENT", "GEMINI_RAW_PREDICTIONS"]); export const aiGatewayDiscoveredModelAttributes = z.object({display_name: z.string().optional(), model_id_wire: z.string(), supported_operations: z.array(aiGatewaySupportedOperation)}); export const aiGatewayDiscoveredModelType = z.literal("provider-models"); export const aiGatewayDiscoveredModelResource = z.object({attributes: aiGatewayDiscoveredModelAttributes, id: z.string(), type: aiGatewayDiscoveredModelType}); export const aiGatewayDiscoveredModelListResponse = z.object({data: z.array(aiGatewayDiscoveredModelResource)}); 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 models available from a provider integration * * GET /api/v2/provider-models * * Also accepts auth: AuthZ */ export const getApiApiV2ProviderModels = async (args: GetApiApiV2ProviderModelsArgs, options?: GetApiApiV2ProviderModelsOptions) => { 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 (args.integration_type != null) query.append("integration_type", String(args.integration_type)) if (args.integration_account_id != null) query.append("integration_account_id", String(args.integration_account_id)) if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.location != null) query.append("location", String(args.location)) if (args.bedrock_region != null) query.append("bedrock_region", String(args.bedrock_region)) if (args.foundry_resource_id != null) query.append("foundry_resource_id", String(args.foundry_resource_id)) if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/provider-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(aiGatewayDiscoveredModelListResponse, await res.json()) };