import {z} from 'zod' export type PreCheckRequest = {query: string, user_token?: string | undefined, client_id: string, data_sources?: Array | undefined, context?: {[key: string]: unknown} | undefined}; export type CreateApiApiPolicyPreCheckArgs = {Authorization: string, 'X-Axonflow-PEP-Handshake'?: string | undefined, body: PreCheckRequest}; export type CreateApiApiPolicyPreCheckOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const rateLimitInfo = z.object({limit: z.number().int().optional(), remaining: z.number().int().optional(), reset_at: z.string().optional()}); export const preCheckResponse = z.object({decision_id: z.string().optional(), verdict: z.enum(["allow", "deny"]).optional(), approved: z.boolean().optional(), context_id: z.string().optional(), approved_data: z.record(z.string(), z.unknown()).optional(), policies: z.array(z.string()).optional(), rate_limit: rateLimitInfo.optional(), expires_at: z.string().optional(), block_reason: z.string().optional(), trace_id: z.string().optional(), engine: z.literal("anchored").optional(), subject_type: z.string().optional(), policy_bundle: z.string().optional(), legacy_validators: z.array(z.object({validator: z.enum(["indonesia_pii", "india_pii"]), action: z.enum(["blocked", "masked"])})).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 }; /** * Pre-check request before LLM call * * POST /api/policy/pre-check */ export const createApiApiPolicyPreCheck = async (args: CreateApiApiPolicyPreCheckArgs, options?: CreateApiApiPolicyPreCheckOptions) => { 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.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['X-Axonflow-PEP-Handshake'] != null) headers.set("X-Axonflow-PEP-Handshake", String(args['X-Axonflow-PEP-Handshake'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/policy/pre-check${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(preCheckResponse, await res.json()) };