import {z} from 'zod' export type ConversationExportRequest = {export_format?: ('csv' | 'ndjson') | undefined, start: string, end: string, channel?: (string | null) | undefined, status?: (string | null) | undefined, end_user_id?: (string | null) | undefined, assigned_agent_id?: (string | null) | undefined, priority?: ('low' | 'normal' | 'high' | 'urgent' | null) | undefined, eval_filter?: ('has_evals' | 'no_evals' | 'all_passed' | 'any_failed' | null) | undefined, q?: (string | null) | undefined, from_number?: (string | null) | undefined, to_number?: (string | null) | undefined, email?: (string | null) | undefined, id?: (string | null) | undefined, external_session_id?: (string | null) | undefined}; export type CreateApiV1ConversationsExportsArgs = {body: ConversationExportRequest}; export type CreateApiV1ConversationsExportsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const conversationExportFile = z.object({name: z.string(), sha256_plaintext: z.string().nullable().optional(), download_token: z.string().nullable().optional()}); export const conversationExportJobResponse = z.object({id: z.string(), status: z.string(), export_format: z.string(), window_start: z.string().nullable().optional(), window_end: z.string().nullable().optional(), created_at: z.string(), started_at: z.string().nullable().optional(), finalized_at: z.string().nullable().optional(), expires_at: z.string().nullable().optional(), total_rows: z.number().int().nullable().optional(), error_code: z.string().nullable().optional(), filters: z.record(z.string(), z.unknown()).nullable().optional(), files: z.array(conversationExportFile).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 }; /** * Request an async encrypted conversation/session export * * POST /v1/conversations/exports */ export const createApiV1ConversationsExports = async (args: CreateApiV1ConversationsExportsArgs, options: CreateApiV1ConversationsExportsOptions) => { 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/conversations/exports${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(conversationExportJobResponse, await res.json()) };