import {z} from 'zod' export type GetApiV1PrivacyAuditLogsArgs = {from?: (string | null) | undefined, to?: (string | null) | undefined, source?: ('conversation_turn' | 'memory_sync' | 'voice_call' | null) | undefined, entity_type?: (string | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1PrivacyAuditLogsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const pIIScrubLogResponse = z.object({id: z.string(), organization_id: z.string(), session_id: z.string().nullable(), source: z.string(), entity_types_detected: z.array(z.string()), detection_count: z.number().int(), score_threshold_used: z.number(), scrub_config_snapshot: z.record(z.string(), z.unknown()), created_at: z.string()}); export const cursorPagePIIScrubLogResponse = z.object({items: z.array(pIIScrubLogResponse), next_cursor: z.string().nullable().optional(), has_more: z.boolean()}); 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 }; /** * List PII scrub audit events * * GET /v1/privacy/audit-logs */ export const getApiV1PrivacyAuditLogs = async (args: GetApiV1PrivacyAuditLogsArgs, options: GetApiV1PrivacyAuditLogsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.source != null) query.append("source", String(args.source)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/privacy/audit-logs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cursorPagePIIScrubLogResponse, await res.json()) };