import {z} from 'zod' export type PolicyScope = 'organization' | 'attachable'; export type PolicyCheckType = 'expression' | 'llm_judge'; export type PolicyEnforcementPoint = 'input' | 'pre_tool' | 'post_tool' | 'agent_response'; export type PolicyAction = 'block' | 'redact' | 'append' | 'require_approval' | 'handoff'; export type PolicyMode = 'enforce' | 'monitor'; export type PolicyOnError = 'fail_open' | 'fail_closed'; export type PolicyStrictness = 'strict' | 'relaxed'; export type PolicyCreate = {name: string, description?: (string | null) | undefined, enabled?: boolean | undefined, scope?: PolicyScope | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, check_type: PolicyCheckType, check_config?: {[key: string]: unknown} | undefined, enforcement_point: PolicyEnforcementPoint, action: PolicyAction, action_config?: ({[key: string]: unknown} | null) | undefined, tool_target?: (string | null) | undefined, mode?: PolicyMode | undefined, on_error?: (PolicyOnError | null) | undefined, timeout_ms?: (number | null) | undefined, strictness?: PolicyStrictness | undefined, priority?: number | undefined}; export type CreateApiV1PoliciesArgs = {body: PolicyCreate}; export type CreateApiV1PoliciesOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const policyRevisionResponse = z.object({id: z.string(), policy_id: z.string(), name: z.string(), description: z.string().nullable().optional(), check_type: z.string(), check_config: z.record(z.string(), z.unknown()).optional(), enforcement_point: z.string(), action: z.string(), action_config: z.record(z.string(), z.unknown()).nullable().optional(), tool_target: z.string().nullable().optional(), mode: z.string(), on_error: z.string(), timeout_ms: z.number().int().nullable().optional(), strictness: z.string(), priority: z.number().int(), created_by: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const policyResponse = z.object({id: z.string(), organization_id: z.string(), name: z.string(), description: z.string().nullable().optional(), enabled: z.boolean(), scope: z.string(), active_revision_id: z.string().nullable().optional(), active_revision: policyRevisionResponse.nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Create a policy * * POST /v1/policies */ export const createApiV1Policies = async (args: CreateApiV1PoliciesArgs, options: CreateApiV1PoliciesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/policies${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(policyResponse, await res.json()) };