import {z} from 'zod' export type DeleteApiAgentsCallsCallIdArgs = {'Cartesia-Version': '2026-03-01', call_id: string}; export type DeleteApiAgentsCallsCallIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const toolCall = z.object({id: z.string().optional(), name: z.string(), arguments: z.record(z.string(), z.unknown()), result: z.string().optional()}); export const logEvent = z.object({event: z.string(), metadata: z.record(z.string(), z.string()), timestamp: z.number()}); export const logMetric = z.object({name: z.string(), value: z.union([z.number(), z.string(), z.boolean()]), timestamp: z.number()}); export const agentTranscript = z.object({role: z.string(), text: z.string().nullable().optional(), start_timestamp: z.number(), end_timestamp: z.number(), tool_calls: z.array(toolCall).nullable().optional(), stt_ttfb: z.number().nullable().optional(), tts_ttfb: z.number().nullable().optional(), log_event: logEvent.optional(), log_metric: logMetric.optional()}); export const telephonyParams = z.object({to: z.string().optional(), from: z.string().optional(), call_sid: z.string().optional(), direction: z.string().optional(), parameters: z.record(z.string(), z.string()).optional(), headers: z.record(z.string(), z.string()).optional(), connection_type: z.enum(["websocket", "phone"]).optional()}); export const agentCallStatus = z.enum(["created", "started", "completed", "failed"]); 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 agentCall = z.object({id: z.string(), agent_id: z.string(), agent_name: z.string().optional(), zdr_enabled: z.boolean().optional(), start_time: z.string().nullable().optional(), end_time: z.string().nullable().optional(), redacted_at: z.string().nullable().optional(), transcript: z.array(agentTranscript).nullable().optional(), telephony_params: telephonyParams.optional(), telephony_account_type: z.enum(["cartesia", "twilio", "sip_trunk"]).nullable().optional(), summary: z.string().nullable().optional(), status: agentCallStatus, error_message: z.string().nullable().optional(), end_reason: endReason.optional(), deployment_id: z.string().nullable().optional(), deployment_version_id: z.string().nullable().optional(), deployment_region: z.enum(["US", "EU", "APAC"]).nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().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 }; /** * Delete Call * * DELETE /agents/calls/{call_id} */ export const deleteApiAgentsCallsCallId = async (args: DeleteApiAgentsCallsCallIdArgs, options?: DeleteApiAgentsCallsCallIdOptions) => { 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/${encodeURIComponent(String(args.call_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(agentCall, await res.json()) };