import {z} from 'zod' export type CustomDenyPattern = {name: string, regex: string, replacement_label: string, description?: (string | null) | undefined, score?: number | undefined}; export type CustomAllowPattern = {name: string, regex: string, description?: (string | null) | undefined}; export type PrivacyConfigUpsert = {pii_scrubbing_enabled?: boolean | undefined, scrub_types?: Array | undefined, custom_deny_patterns?: Array | undefined, custom_allow_patterns?: Array | undefined, score_threshold?: number | undefined, audio_retention_policy: 'no_audio' | 'raw' | 'redacted', audio_redaction_fill?: ('silence' | 'tone_1khz') | undefined, supported_languages?: Array | undefined, default_language?: string | undefined, language_detection_enabled?: boolean | undefined}; export type UpdateApiV1PrivacyConfigArgs = {body: PrivacyConfigUpsert}; export type UpdateApiV1PrivacyConfigOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const customDenyPattern = z.object({name: z.string().min(1).max(64), regex: z.string().min(1).max(1024), replacement_label: z.string().min(1).max(64), description: z.string().max(256).nullable().optional(), score: z.number().gte(0).lte(1).optional()}); export const customAllowPattern = z.object({name: z.string().min(1).max(64), regex: z.string().min(1).max(1024), description: z.string().max(256).nullable().optional()}); export const privacyConfigResponse = z.object({id: z.string(), organization_id: z.string(), pii_scrubbing_enabled: z.boolean(), scrub_types: z.array(z.string()), custom_deny_patterns: z.array(customDenyPattern), custom_allow_patterns: z.array(customAllowPattern), score_threshold: z.number(), audio_retention_policy: z.enum(["no_audio", "raw", "redacted"]), audio_redaction_fill: z.enum(["silence", "tone_1khz"]), supported_languages: z.array(z.string()), default_language: z.string(), language_detection_enabled: z.boolean(), 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 }; /** * Upsert privacy config for organization * * PUT /v1/privacy/config */ export const updateApiV1PrivacyConfig = async (args: UpdateApiV1PrivacyConfigArgs, options: UpdateApiV1PrivacyConfigOptions) => { 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/privacy/config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(privacyConfigResponse, await res.json()) };