import {z} from 'zod' export type PiiLevers = {mode?: ('off' | 'anonymize' | 'block') | undefined, contextual_entities?: (Array<'NAME' | 'ADDRESS' | 'AGE' | 'USERNAME'> | null) | undefined, structured_entities?: (Array<'EMAIL' | 'PHONE' | 'CREDIT_DEBIT_CARD_NUMBER' | 'CREDIT_DEBIT_CARD_CVV' | 'CREDIT_DEBIT_CARD_EXPIRY' | 'PIN' | 'PASSWORD' | 'INTERNATIONAL_BANK_ACCOUNT_NUMBER' | 'IP_ADDRESS' | 'AWS_ACCESS_KEY' | 'AWS_SECRET_KEY' | 'LICENSE_PLATE' | 'VEHICLE_IDENTIFICATION_NUMBER' | 'DRIVER_ID' | 'ES_DNI' | 'ES_NIE' | 'ES_NIF' | 'ES_NSS'> | null) | undefined, reversible?: boolean | undefined}; export type ExtractionProfileCreateRequest = {name: string, description?: (string | null) | undefined, language?: (string | null) | undefined, scope_aware?: (boolean | null) | undefined, granularity?: ('fine' | 'standard' | 'coarse' | null) | undefined, additional_instructions?: (string | null) | undefined, business_relevance?: (string | null) | undefined, initial_pot_score?: (number | null) | undefined, pii?: (PiiLevers | null) | undefined}; export type CreateApiProfilesArgs = {body: ExtractionProfileCreateRequest}; export type CreateApiProfilesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const piiLevers = z.object({mode: z.enum(["off", "anonymize", "block"]).optional(), contextual_entities: z.array(z.enum(["NAME", "ADDRESS", "AGE", "USERNAME"])).nullable().optional(), structured_entities: z.array(z.enum(["EMAIL", "PHONE", "CREDIT_DEBIT_CARD_NUMBER", "CREDIT_DEBIT_CARD_CVV", "CREDIT_DEBIT_CARD_EXPIRY", "PIN", "PASSWORD", "INTERNATIONAL_BANK_ACCOUNT_NUMBER", "IP_ADDRESS", "AWS_ACCESS_KEY", "AWS_SECRET_KEY", "LICENSE_PLATE", "VEHICLE_IDENTIFICATION_NUMBER", "DRIVER_ID", "ES_DNI", "ES_NIE", "ES_NIF", "ES_NSS"])).nullable().optional(), reversible: z.boolean().optional()}); export const extractionProfile = z.object({profile_id: z.string().optional(), workspace_id: z.string(), name: z.string().min(1).max(100), description: z.string().max(500).nullable().optional(), language: z.string().nullable().optional(), scope_aware: z.boolean().nullable().optional(), granularity: z.enum(["fine", "standard", "coarse"]).nullable().optional(), additional_instructions: z.string().max(1000).nullable().optional(), business_relevance: z.string().max(500).nullable().optional(), initial_pot_score: z.number().gte(0.1).lte(0.79).nullable().optional(), pii: piiLevers.nullable().optional(), version: z.number().int().gte(1).optional(), created_at: z.string().optional(), updated_at: z.string().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 }; /** * Create an extraction profile * * POST /profiles */ export const createApiProfiles = async (args: CreateApiProfilesArgs, options: CreateApiProfilesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/profiles${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(extractionProfile, await res.json()) };