import {z} from 'zod' export type GetApiAgentsCallsBatchesBatchIdArgs = {'Cartesia-Version': '2026-03-01', batch_id: string}; export type GetApiAgentsCallsBatchesBatchIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const agentCallBatchStatus = z.enum(["pending", "in_progress", "completed", "failed", "cancelled"]); export const agentCallBatchRecipientStatus = z.string(); export const endReason = z.enum(["agent_hangup", "client_hangup", "api_cancelled", "max_duration", "inactivity", "client_disconnected", "dial_busy", "dial_failed", "dial_no_answer", "error"]); export const agentCallBatchRecipient = z.object({id: z.string(), to_number: z.string(), status: agentCallBatchRecipientStatus, agent_call_id: z.string().optional(), end_reason: endReason.optional(), error_message: z.string().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string()}); export const agentCallBatch = z.object({id: z.string(), name: z.string(), agent_id: z.string(), from_number_id: z.string(), region: z.enum(["US", "EU", "APAC"]), target_concurrency_limit: z.number().int(), status: agentCallBatchStatus, total_calls_scheduled: z.number().int(), total_calls_dispatched: z.number().int(), total_calls_finished: z.number().int(), retry_count: z.number().int(), created_at: z.string(), last_updated_at: z.string(), scheduled_at: z.string().optional(), admitted_at: z.string().optional(), recipients: z.array(agentCallBatchRecipient).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 }; /** * Get Call Batch * * GET /agents/calls/batches/{batch_id} */ export const getApiAgentsCallsBatchesBatchId = async (args: GetApiAgentsCallsBatchesBatchIdArgs, options?: GetApiAgentsCallsBatchesBatchIdOptions) => { const {baseUrl = "https://api.cartesia.ai", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Cartesia-Version'] != null) headers.set("Cartesia-Version", String(args['Cartesia-Version'])) if (APIKeyAuth !== undefined) headers.set('authorization', `Bearer ${APIKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/agents/calls/batches/${encodeURIComponent(String(args.batch_id))}${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(agentCallBatch, await res.json()) };