import {z} from 'zod' export type CallSummariesEnumProcessingStateRequest = 'completed' | 'started' | 'partial' | 'all'; export type CallSummariesEnumSortBy = 'start_time' | 'end_time'; export type CallSummariesEnumAnsweredBy = 'unknown' | 'machine_start' | 'machine_end_beep' | 'machine_end_silence' | 'machine_end_other' | 'human' | 'fax'; export type GetApiV1VoiceSummariesArgs = {From?: string | undefined, To?: string | undefined, FromCarrier?: string | undefined, ToCarrier?: string | undefined, FromCountryCode?: string | undefined, ToCountryCode?: string | undefined, VerifiedCaller?: boolean | undefined, HasTag?: boolean | undefined, StartTime?: string | undefined, EndTime?: string | undefined, CallType?: string | undefined, CallState?: string | undefined, Direction?: string | undefined, ProcessingState?: CallSummariesEnumProcessingStateRequest | undefined, SortBy?: CallSummariesEnumSortBy | undefined, Subaccount?: string | undefined, AbnormalSession?: boolean | undefined, AnsweredBy?: CallSummariesEnumAnsweredBy | undefined, AnsweredByAnnotation?: string | undefined, ConnectivityIssueAnnotation?: string | undefined, QualityIssueAnnotation?: string | undefined, SpamAnnotation?: boolean | undefined, CallScoreAnnotation?: string | undefined, BrandedEnabled?: boolean | undefined, VoiceIntegrityEnabled?: boolean | undefined, BrandedBundleSid?: string | undefined, BrandedLogo?: boolean | undefined, BrandedType?: string | undefined, BrandedUseCase?: string | undefined, BrandedCallReason?: string | undefined, VoiceIntegrityBundleSid?: string | undefined, VoiceIntegrityUseCase?: string | undefined, BusinessProfileIdentity?: string | undefined, BusinessProfileIndustry?: string | undefined, BusinessProfileBundleSid?: string | undefined, BusinessProfileType?: string | undefined, PageSize?: number | undefined, Page?: number | undefined, PageToken?: string | undefined}; export type GetApiV1VoiceSummariesOptions = {baseUrl?: string | undefined, accountSidAuthToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const callSummariesEnumAnsweredBy = z.enum(["unknown", "machine_start", "machine_end_beep", "machine_end_silence", "machine_end_other", "human", "fax"]); export const callSummariesEnumCallType = z.enum(["carrier", "sip", "trunking", "client", "whatsapp"]); export const callSummariesEnumCallState = z.enum(["ringing", "completed", "busy", "fail", "noanswer", "canceled", "answered", "undialed"]); export const callSummariesEnumProcessingState = z.enum(["complete", "partial"]); export const callSummaryCrelayRateStats = z.object({min: z.number().optional(), max: z.number().optional(), avg: z.number().optional()}); export const callSummaryCrelayTokenStats = z.object({total: z.number().int().optional(), tokens_per_second: callSummaryCrelayRateStats.optional()}); export const callSummaryCrelayWordStats = z.object({total: z.number().int().optional(), words_per_minute: callSummaryCrelayRateStats.optional()}); export const callSummaryCrelayInterruptions = z.object({customer_to_agent: z.number().int().optional(), agent_to_customer: z.number().int().optional()}); export const callSummaryCrelaySessionState = z.enum(["unknown", "failure", "ended", "hung_up"]); export const callSummaryAgentSessionSummary = z.object({session_id: z.string().optional(), tts_latency_ms: callSummaryCrelayRateStats.optional(), stt_latency_ms: callSummaryCrelayRateStats.optional(), network_latency_ms: callSummaryCrelayRateStats.optional(), time_to_first_audio_ms: callSummaryCrelayRateStats.optional(), application_latency_ms: callSummaryCrelayRateStats.optional(), tokens: callSummaryCrelayTokenStats.optional(), words: callSummaryCrelayWordStats.optional(), turns: z.number().int().optional(), interruptions: callSummaryCrelayInterruptions.optional(), session_state: callSummaryCrelaySessionState.optional()}); export const insightsV1CallSummaries = z.object({account_sid: z.string().min(34).max(34).nullable().optional(), call_sid: z.string().min(34).max(34).nullable().optional(), answered_by: callSummariesEnumAnsweredBy.optional(), call_type: callSummariesEnumCallType.optional(), call_state: callSummariesEnumCallState.optional(), processing_state: callSummariesEnumProcessingState.optional(), created_time: z.string().nullable().optional(), start_time: z.string().nullable().optional(), end_time: z.string().nullable().optional(), duration: z.number().int().nullable().optional(), connect_duration: z.number().int().nullable().optional(), from: z.unknown(), to: z.unknown(), carrier_edge: z.unknown(), client_edge: z.unknown(), sdk_edge: z.unknown(), sip_edge: z.unknown(), tags: z.array(z.string()).nullable().optional(), url: z.string().nullable().optional(), attributes: z.unknown(), properties: z.unknown(), trust: z.unknown(), annotation: z.unknown(), agent_session_summaries: z.array(callSummaryAgentSessionSummary).nullable().optional()}); export const getApiV1VoiceSummariesResponse = z.object({call_summaries: z.array(insightsV1CallSummaries).optional(), meta: z.object({first_page_url: z.string().optional(), key: z.string().optional(), next_page_url: z.string().nullable().optional(), page: z.number().int().optional(), page_size: z.number().int().optional(), previous_page_url: z.string().nullable().optional(), url: z.string().optional()}).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 a list of Call Summaries. * * GET /v1/Voice/Summaries */ export const getApiV1VoiceSummaries = async (args: GetApiV1VoiceSummariesArgs, options?: GetApiV1VoiceSummariesOptions) => { const {baseUrl = "https://insights.twilio.com", fetch: fetchFn = fetch, accountSidAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.From != null) query.append("From", String(args.From)) if (args.To != null) query.append("To", String(args.To)) if (args.FromCarrier != null) query.append("FromCarrier", String(args.FromCarrier)) if (args.ToCarrier != null) query.append("ToCarrier", String(args.ToCarrier)) if (args.FromCountryCode != null) query.append("FromCountryCode", String(args.FromCountryCode)) if (args.ToCountryCode != null) query.append("ToCountryCode", String(args.ToCountryCode)) if (args.VerifiedCaller != null) query.append("VerifiedCaller", String(args.VerifiedCaller)) if (args.HasTag != null) query.append("HasTag", String(args.HasTag)) if (args.StartTime != null) query.append("StartTime", String(args.StartTime)) if (args.EndTime != null) query.append("EndTime", String(args.EndTime)) if (args.CallType != null) query.append("CallType", String(args.CallType)) if (args.CallState != null) query.append("CallState", String(args.CallState)) if (args.Direction != null) query.append("Direction", String(args.Direction)) if (args.ProcessingState != null) query.append("ProcessingState", String(args.ProcessingState)) if (args.SortBy != null) query.append("SortBy", String(args.SortBy)) if (args.Subaccount != null) query.append("Subaccount", String(args.Subaccount)) if (args.AbnormalSession != null) query.append("AbnormalSession", String(args.AbnormalSession)) if (args.AnsweredBy != null) query.append("AnsweredBy", String(args.AnsweredBy)) if (args.AnsweredByAnnotation != null) query.append("AnsweredByAnnotation", String(args.AnsweredByAnnotation)) if (args.ConnectivityIssueAnnotation != null) query.append("ConnectivityIssueAnnotation", String(args.ConnectivityIssueAnnotation)) if (args.QualityIssueAnnotation != null) query.append("QualityIssueAnnotation", String(args.QualityIssueAnnotation)) if (args.SpamAnnotation != null) query.append("SpamAnnotation", String(args.SpamAnnotation)) if (args.CallScoreAnnotation != null) query.append("CallScoreAnnotation", String(args.CallScoreAnnotation)) if (args.BrandedEnabled != null) query.append("BrandedEnabled", String(args.BrandedEnabled)) if (args.VoiceIntegrityEnabled != null) query.append("VoiceIntegrityEnabled", String(args.VoiceIntegrityEnabled)) if (args.BrandedBundleSid != null) query.append("BrandedBundleSid", String(args.BrandedBundleSid)) if (args.BrandedLogo != null) query.append("BrandedLogo", String(args.BrandedLogo)) if (args.BrandedType != null) query.append("BrandedType", String(args.BrandedType)) if (args.BrandedUseCase != null) query.append("BrandedUseCase", String(args.BrandedUseCase)) if (args.BrandedCallReason != null) query.append("BrandedCallReason", String(args.BrandedCallReason)) if (args.VoiceIntegrityBundleSid != null) query.append("VoiceIntegrityBundleSid", String(args.VoiceIntegrityBundleSid)) if (args.VoiceIntegrityUseCase != null) query.append("VoiceIntegrityUseCase", String(args.VoiceIntegrityUseCase)) if (args.BusinessProfileIdentity != null) query.append("BusinessProfileIdentity", String(args.BusinessProfileIdentity)) if (args.BusinessProfileIndustry != null) query.append("BusinessProfileIndustry", String(args.BusinessProfileIndustry)) if (args.BusinessProfileBundleSid != null) query.append("BusinessProfileBundleSid", String(args.BusinessProfileBundleSid)) if (args.BusinessProfileType != null) query.append("BusinessProfileType", String(args.BusinessProfileType)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageToken != null) query.append("PageToken", String(args.PageToken)) if (accountSidAuthToken !== undefined) headers.set('authorization', `Basic ${btoa(`${accountSidAuthToken.username}:${accountSidAuthToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Voice/Summaries${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(getApiV1VoiceSummariesResponse, await res.json()) };