import {z} from 'zod' export type CallManagerRecordingFormat = 'wav'; export type CreateApiServiceAgentsCallsIdRecordingStartArgs = {id: string, body: {format?: CallManagerRecordingFormat | undefined, end_of_silence?: number | undefined, end_of_key?: string | undefined, duration?: number | undefined}}; export type CreateApiServiceAgentsCallsIdRecordingStartOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const callManagerCallType = z.enum(["", "flow", "conference", "sip-service"]); export const commonAddress = z.object({type: z.enum(["", "agent", "conference", "email", "extension", "line", "sip", "tel", "web_session"]).optional(), target: z.string().optional(), target_name: z.string().optional(), name: z.string().optional(), detail: z.string().optional()}); export const callManagerCallStatus = z.enum(["dialing", "ringing", "progressing", "terminating", "canceling", "hangup"]); export const flowManagerActionType = z.enum(["ai_talk", "ai_summary", "amd", "answer", "beep", "branch", "call", "condition_call_digits", "condition_call_status", "condition_datetime", "condition_variable", "confbridge_join", "conference_join", "connect", "conversation_send", "digits_receive", "digits_send", "echo", "email_send", "external_media_start", "external_media_stop", "fetch", "fetch_flow", "goto", "hangup", "message_send", "mute", "play", "queue_join", "recording_start", "recording_stop", "sleep", "stop", "stream_echo", "talk", "transcribe_start", "transcribe_stop", "transcribe_recording", "variable_set", "webhook_send"]); export const flowManagerAction = z.object({id: z.string(), next_id: z.string().optional(), type: flowManagerActionType, option: z.record(z.string(), z.unknown()).optional(), tm_execute: z.string().optional()}); export const callManagerCallDirection = z.enum(["incoming", "outgoing"]); export const callManagerCallMuteDirection = z.enum(["", "both", "out", "in"]); export const callManagerCallHangupBy = z.enum(["", "remote", "local"]); export const callManagerCallHangupReason = z.enum(["", "normal", "failed", "busy", "cancel", "timeout", "noanswer", "dialout", "amd"]); export const callManagerCall = z.object({id: z.string().optional(), customer_id: z.string().optional(), owner_type: z.string().optional(), owner_id: z.string().optional(), flow_id: z.string().optional(), activeflow_id: z.string().optional(), type: callManagerCallType.optional(), master_call_id: z.string().optional(), chained_call_ids: z.array(z.string()).optional(), recording_id: z.string().optional(), recording_ids: z.array(z.string()).optional(), groupcall_id: z.string().optional(), source: commonAddress.optional(), destination: commonAddress.optional(), status: callManagerCallStatus.optional(), action: flowManagerAction.optional(), direction: callManagerCallDirection.optional(), mute_direction: callManagerCallMuteDirection.optional(), hangup_by: callManagerCallHangupBy.optional(), hangup_reason: callManagerCallHangupReason.optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), tm_progressing: z.string().optional(), tm_ringing: z.string().optional(), tm_hangup: z.string().optional(), tm_create: z.string().optional(), tm_update: z.string().optional(), tm_delete: z.string().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 }; /** * Start recording a call * * POST /service_agents/calls/{id}/recording_start */ export const createApiServiceAgentsCallsIdRecordingStart = async (args: CreateApiServiceAgentsCallsIdRecordingStartArgs, options?: CreateApiServiceAgentsCallsIdRecordingStartOptions) => { const {baseUrl = "https://api.voipbin.net/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/service_agents/calls/${encodeURIComponent(String(args.id))}/recording_start${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(callManagerCall, await res.json()) };