import {z} from 'zod' export type DecisionCallerIdentity = {gateway_id?: string | undefined, org_id?: string | undefined, tenant_id?: string | undefined}; export type DecisionTarget = {type?: string | undefined, model?: string | undefined, provider?: string | undefined, server?: string | undefined, tool?: string | undefined}; export type DecideRequest = {approval_id?: string | undefined, expected_policy_bundle?: string | undefined, stage: 'llm' | 'tool' | 'agent', caller_identity?: DecisionCallerIdentity | undefined, target?: DecisionTarget | undefined, query: string, user_token?: string | undefined, context?: {[key: string]: unknown} | undefined, fulfillment_capabilities?: (Array<'request_body_redaction' | 'request_header_mutation'>) | undefined}; export type CreateApiApiV1DecideArgs = {'X-Axonflow-Approval-Id'?: string | undefined, 'X-Axonflow-Client'?: string | undefined, 'X-Axonflow-PEP-Handshake'?: string | undefined, traceparent?: string | undefined, body: DecideRequest}; export type CreateApiApiV1DecideOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pendingApproval = z.object({approval_id: z.string(), status: z.enum(["pending", "approved"]), plane: z.enum(["mcp:request", "decide"]), expires_at: z.string().optional(), retry: z.object({header: z.literal("X-Axonflow-Approval-Id"), argument: z.literal("approval_id").optional(), body_field: z.literal("approval_id").optional()})}); export const obligationFulfillment = z.object({endpoint: z.string(), method: z.string(), phase: z.enum(["request", "response"]), content_types: z.array(z.string()).optional()}); export const decisionObligation = z.object({type: z.string(), detail: z.string().optional(), fulfillment: obligationFulfillment.optional()}); export const policyIdentity = z.object({id: z.string(), name: z.string().optional(), source: z.enum(["shipped", "organization", "pack"]).optional(), version: z.number().int().optional()}); export const decideResponse = z.object({pending_approval: pendingApproval.optional(), approval_id: z.string().optional(), verdict: z.enum(["allow", "deny", "needs_approval"]), decision_id: z.string(), trace_id: z.string().min(32).max(32), stage: z.enum(["llm", "tool", "agent"]).optional(), reasons: z.array(z.string()).optional(), obligations: z.array(decisionObligation), evaluated_policies: z.array(z.string()), expires_at: z.string(), engine: z.literal("anchored").optional(), subject_type: z.string().optional(), policy_bundle: z.string().optional(), policy_packs: z.array(z.string()).optional(), policy_identities: z.array(policyIdentity).optional(), document_version: z.number().int().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 }; /** * Policy decision for an infrastructure gateway (PEP) * * POST /api/v1/decide */ export const createApiApiV1Decide = async (args: CreateApiApiV1DecideArgs, options?: CreateApiApiV1DecideOptions) => { 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-Axonflow-Approval-Id'] != null) headers.set("X-Axonflow-Approval-Id", String(args['X-Axonflow-Approval-Id'])) if (args['X-Axonflow-Client'] != null) headers.set("X-Axonflow-Client", String(args['X-Axonflow-Client'])) if (args['X-Axonflow-PEP-Handshake'] != null) headers.set("X-Axonflow-PEP-Handshake", String(args['X-Axonflow-PEP-Handshake'])) if (args.traceparent != null) headers.set("traceparent", String(args.traceparent)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/decide${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(decideResponse, await res.json()) };