import {z} from 'zod' export type GetApiV1CallsCallIdArgs = {callId: string}; export type GetApiV1CallsCallIdOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumberResponse = z.object({sipPhoneNumber: z.string().optional(), provider: z.enum(["twilio", "telnyx", "byo"]).optional(), inboundAgentId: z.string().optional(), outboundAgentId: z.string().optional(), outboundPhoneNumber: z.string().optional(), active: z.boolean().optional(), terminationSipURI: z.string().optional(), friendlyName: z.string().optional()}); export const agentResponse = z.object({name: z.string().optional(), agentId: z.string().optional(), status: z.enum(["Live", "Testing", "Disabled"]).optional()}); export const campaignResponse = z.object({campaignId: z.string().optional(), campaignName: z.string().optional(), campaignStatus: z.enum(["live", "scheduled", "paused", "completed"]).optional(), scheduledDate: z.string().optional(), concurrentCalls: z.number().int().optional(), agent: agentResponse.optional(), phoneNumber: phoneNumberResponse.optional(), errors: z.array(z.string()).optional(), scheduled: z.boolean().optional()}); export const jsonNode = z.object({}); export const callResponse = z.object({callId: z.string().optional(), callType: z.enum(["inbound", "outbound", "web", "chat"]).optional(), phoneNumber: phoneNumberResponse.optional(), campaign: campaignResponse.optional(), status: z.enum(["scheduled", "in_progress", "ended", "not_connected", "error"]).optional(), transcript: z.string().optional(), startTime: z.string().optional(), endTime: z.string().optional(), duration: z.number().int().optional(), terminationReason: z.enum(["silence_timed_out", "agent_ended_call", "user_ended_call", "voicemail_detected", "ivr_detected", "agent_transferred_call", "max_call_duration_exceeded", "dial_telephony_permission_denied", "dial_invalid_destination", "dial_no_answer", "dial_user_declined", "dial_busy", "dial_failed", "error_credit_limit_reached", "error_llm", "error_tts", "error_asr", "error_sigmamind", "error_telephony", "error_unknown"]).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), dynamicVariables: jsonNode.optional(), recordingUrl: z.string().optional(), agent: agentResponse.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 Phone Call * * GET /v1/calls/{callId} */ export const getApiV1CallsCallId = async (args: GetApiV1CallsCallIdArgs, options?: GetApiV1CallsCallIdOptions) => { const {baseUrl = "https://api.sigmamind.ai", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-API-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/calls/${encodeURIComponent(String(args.callId))}${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(callResponse, await res.json()) };