import {z} from 'zod' export type GetApiApiV2GovernanceOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aiGatewayDataCaptureMode = z.enum(["metadata_only", "full_capture"]); export const aiGatewayMetadataCaptureItem = z.enum(["tool_usage", "tool_output_size", "tool_names", "conversation_shape", "input_size_by_role", "available_tools", "generation_settings"]); export const aiGatewayMetadataCapture = z.array(aiGatewayMetadataCaptureItem); export const aiGatewayZDRMode = z.enum(["off", "required"]); export const aiGatewayGovernanceAttributes = z.object({data_capture_mode: aiGatewayDataCaptureMode, metadata_capture: aiGatewayMetadataCapture.optional(), zdr_mode: aiGatewayZDRMode}); export const aiGatewayGovernanceType = z.literal("governance"); export const aiGatewayGovernanceResource = z.object({attributes: aiGatewayGovernanceAttributes, id: z.string(), type: aiGatewayGovernanceType}); export const aiGatewayGovernanceResponse = z.object({data: aiGatewayGovernanceResource}); 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 }; /** * Get AI Gateway governance controls * * GET /api/v2/governance * * Also accepts auth: AuthZ */ export const getApiApiV2Governance = async (options?: GetApiApiV2GovernanceOptions) => { 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/governance${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(aiGatewayGovernanceResponse, await res.json()) };