import {z} from 'zod' export type PiiEntityType = 'CREDIT_CARD' | 'CRYPTO' | 'DATE_TIME' | 'EMAIL_ADDRESS' | 'IBAN_CODE' | 'IP_ADDRESS' | 'NRP' | 'LOCATION' | 'PERSON' | 'PHONE_NUMBER' | 'MEDICAL_LICENSE' | 'URL' | 'MAC_ADDRESS' | 'UUID' | 'US_BANK_NUMBER' | 'US_DRIVER_LICENSE' | 'US_ITIN' | 'US_PASSPORT' | 'US_SSN' | 'US_MBI' | 'US_NPI' | 'UK_NHS' | 'UK_NINO' | 'UK_PASSPORT' | 'UK_POSTCODE' | 'UK_VEHICLE_REGISTRATION' | 'UK_DRIVING_LICENCE' | 'ES_NIF' | 'ES_NIE' | 'ES_PASSPORT' | 'IT_FISCAL_CODE' | 'IT_DRIVER_LICENSE' | 'IT_VAT_CODE' | 'IT_PASSPORT' | 'IT_IDENTITY_CARD' | 'PL_PESEL' | 'SG_NRIC_FIN' | 'SG_UEN' | 'AU_ABN' | 'AU_ACN' | 'AU_TFN' | 'AU_MEDICARE' | 'IN_PAN' | 'IN_AADHAAR' | 'IN_VEHICLE_REGISTRATION' | 'IN_VOTER' | 'IN_PASSPORT' | 'IN_GSTIN' | 'FI_PERSONAL_IDENTITY_CODE' | 'DE_TAX_ID' | 'DE_TAX_NUMBER' | 'DE_VAT_ID' | 'DE_PASSPORT' | 'DE_ID_CARD' | 'DE_FUEHRERSCHEIN' | 'DE_SOCIAL_SECURITY' | 'DE_HEALTH_INSURANCE' | 'DE_LANR' | 'DE_BSNR' | 'DE_KFZ' | 'DE_HANDELSREGISTER' | 'DE_PLZ' | 'KR_RRN' | 'KR_FRN' | 'KR_PASSPORT' | 'KR_DRIVER_LICENSE' | 'KR_BRN' | 'CA_SIN' | 'SE_PERSONNUMMER' | 'SE_ORGANISATIONSNUMMER' | 'TH_TNIN' | 'TR_NATIONAL_ID' | 'TR_LICENSE_PLATE' | 'NG_NIN' | 'NG_VEHICLE_REGISTRATION' | 'PH_TIN' | 'PH_UMID' | 'PH_PASSPORT' | 'ZA_ID_NUMBER'; export type ApplyGuardrailRequest = {entities?: (Array | null) | undefined, guardrail_name: string, input_type?: string | undefined, language?: (string | null) | undefined, messages?: (Array<{[key: string]: unknown}> | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, text: string}; export type CreateApiGuardrailsApplyGuardrailArgs = {body: ApplyGuardrailRequest}; export type CreateApiGuardrailsApplyGuardrailOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const applyGuardrailResponse = z.object({response_text: 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 }; /** * Apply Guardrail * * POST /guardrails/apply_guardrail */ export const createApiGuardrailsApplyGuardrail = async (args: CreateApiGuardrailsApplyGuardrailArgs, options: CreateApiGuardrailsApplyGuardrailOptions) => { 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-litellm-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/guardrails/apply_guardrail${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(applyGuardrailResponse, await res.json()) };