import {z} from 'zod' export type SimulationUserContext = {id?: number | undefined, email?: string | undefined, role?: string | undefined, region?: string | undefined, permissions?: Array | undefined, tenant_id?: string | undefined, org_id?: string | undefined}; export type SimulationClientContext = {id?: string | undefined, name?: string | undefined, org_id?: string | undefined, tenant_id?: string | undefined}; export type SimulatePoliciesRequest = {query: string, request_type?: string | undefined, user?: SimulationUserContext | undefined, client?: SimulationClientContext | undefined, context?: {[key: string]: unknown} | undefined}; export type CreateApiApiV1PoliciesSimulateArgs = {'X-Tenant-ID': string, body: SimulatePoliciesRequest}; export type CreateApiApiV1PoliciesSimulateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const simulationDailyUsage = z.object({used: z.number().int().optional(), limit: z.number().int().optional()}); export const simulatePoliciesResponse = z.object({allowed: z.boolean().optional(), applied_policies: z.array(z.string()).optional(), risk_score: z.number().optional(), required_actions: z.array(z.string()).optional(), processing_time_ms: z.number().int().optional(), total_policies: z.number().int().optional(), dry_run: z.boolean().optional(), simulated_at: z.string().optional(), tier: z.string().optional(), daily_usage: simulationDailyUsage.optional()}); 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 }; /** * Simulate policies (dry run) * * POST /api/v1/policies/simulate */ export const createApiApiV1PoliciesSimulate = async (args: CreateApiApiV1PoliciesSimulateArgs, options?: CreateApiApiV1PoliciesSimulateOptions) => { const {baseUrl = "https://agent.getaxonflow.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Tenant-ID'] != null) headers.set("X-Tenant-ID", String(args['X-Tenant-ID'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/policies/simulate${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(simulatePoliciesResponse, await res.json()) };