import {z} from 'zod' export type BookAppointmentToolParams = {event_type_id: number, api_key_ref: string, attendee_name?: string | undefined, attendee_timezone?: string | undefined}; export type BookAppointmentTool = {type: 'book_appointment', book_appointment: BookAppointmentToolParams}; export type CheckAvailabilityToolParams = {event_type_id: number, api_key_ref: string}; export type CheckAvailabilityTool = {type: 'check_availability', check_availability: CheckAvailabilityToolParams}; export type CallControlWebhookToolParams = {name: string, description: string, url: string, method?: ('GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH') | undefined, headers?: (Array<{name?: string | undefined, value?: string | undefined}>) | undefined, body_parameters?: ({properties?: Record | undefined, required?: Array | undefined, type?: 'object' | undefined}) | undefined, path_parameters?: ({properties?: Record | undefined, required?: Array | undefined, type?: 'object' | undefined}) | undefined, query_parameters?: ({properties?: Record | undefined, required?: Array | undefined, type?: 'object' | undefined}) | undefined}; export type WebhookTool = {type: 'webhook', timeout_ms?: number | undefined, webhook: CallControlWebhookToolParams}; export type HangupToolParams = {description?: string | undefined}; export type HangupTool = {type: 'hangup', hangup: HangupToolParams}; export type CallControlTransferToolParams = {targets: Array<{name?: string | undefined, to: string}> | string, from: string}; export type TransferTool = {type: 'transfer', transfer: CallControlTransferToolParams}; export type CallControlBucketIds = {bucket_ids: Array, max_num_results?: number | undefined}; export type CallControlRetrievalTool = {type: 'retrieval', retrieval: CallControlBucketIds}; export type Assistant = {model?: string | undefined, instructions?: string | undefined, openai_api_key_ref?: string | undefined, tools?: (Array) | undefined}; export type TranscriptionConfig = {model?: ('deepgram/flux' | 'flux' | 'deepgram/nova-3' | 'deepgram/nova-2' | 'speechmatics/standard' | 'speechmatics/enhanced' | 'speechmatics/linden-1' | 'assemblyai/universal-3-6-pro' | 'assemblyai/universal-3-5-pro' | 'assemblyai/universal-streaming' | 'xai/grok-stt' | 'soniox/stt-rt-v4' | 'nvidia/parakeet-v3' | 'omi-health/omi-med-stt-v1' | 'humain/realtime' | 'reson8/turns' | 'cohere/ar-stt' | 'azure/fast' | 'azure/realtime' | 'google/latest_long' | 'distil-whisper/distil-large-v2' | 'openai/whisper-large-v3-turbo') | undefined, language?: string | undefined}; export type GoogleTranscriptionLanguage = 'af' | 'sq' | 'am' | 'ar' | 'hy' | 'az' | 'eu' | 'bn' | 'bs' | 'bg' | 'my' | 'ca' | 'yue' | 'zh' | 'hr' | 'cs' | 'da' | 'nl' | 'en' | 'et' | 'fil' | 'fi' | 'fr' | 'gl' | 'ka' | 'de' | 'el' | 'gu' | 'iw' | 'hi' | 'hu' | 'is' | 'id' | 'it' | 'ja' | 'jv' | 'kn' | 'kk' | 'km' | 'ko' | 'lo' | 'lv' | 'lt' | 'mk' | 'ms' | 'ml' | 'mr' | 'mn' | 'ne' | 'no' | 'fa' | 'pl' | 'pt' | 'pa' | 'ro' | 'ru' | 'rw' | 'sr' | 'si' | 'sk' | 'sl' | 'ss' | 'st' | 'es' | 'su' | 'sw' | 'sv' | 'ta' | 'te' | 'th' | 'tn' | 'tr' | 'ts' | 'uk' | 'ur' | 'uz' | 've' | 'vi' | 'xh' | 'zu'; export type VoiceConfig = string; export type ElevenLabsVoiceSettings = {type: 'elevenlabs', api_key_ref?: string | undefined}; export type TelnyxVoiceSettings = {type: 'telnyx', voice_speed?: number | undefined}; export type AWSVoiceSettings = {type: 'aws'}; export type AzureVoiceSettings = {type: 'azure', api_key_ref?: string | undefined, region?: string | undefined, deployment_id?: string | undefined, effect?: ('eq_car' | 'eq_telecomhp8k') | undefined, gender?: ('Male' | 'Female') | undefined}; export type ResembleSampleRate = '8000' | '16000' | '22050' | '32000' | '44100' | '48000'; export type ResembleVoiceSettings = {type: 'resemble', precision?: ('PCM_16' | 'PCM_24' | 'PCM_32' | 'MULAW') | undefined, sample_rate?: ResembleSampleRate | undefined, format?: ('wav' | 'mp3') | undefined}; export type XAIVoiceSettings = {type: 'xai', language?: string | undefined}; export type SonioxVoiceSettings = {type: 'soniox', speed?: number | undefined, reduce_silence?: boolean | undefined}; export type InterruptionSettings = {enable?: boolean | undefined}; export type GatherUsingAIRequest = {parameters: Record, assistant?: Assistant | undefined, transcription?: TranscriptionConfig | undefined, language?: GoogleTranscriptionLanguage | undefined, voice?: VoiceConfig | undefined, voice_settings?: (ElevenLabsVoiceSettings | TelnyxVoiceSettings | AWSVoiceSettings | AzureVoiceSettings | ResembleVoiceSettings | XAIVoiceSettings | SonioxVoiceSettings) | undefined, greeting?: string | undefined, send_partial_results?: boolean | undefined, send_message_history_updates?: boolean | undefined, message_history?: (Array<{content?: string | undefined, role?: ('assistant' | 'user') | undefined}>) | undefined, client_state?: string | undefined, command_id?: string | undefined, interruption_settings?: InterruptionSettings | undefined, user_response_timeout_ms?: number | undefined, gather_ended_speech?: string | undefined}; export type CreateApiCallsCallControlIdActionsGatherUsingAiArgs = {call_control_id: string, body: GatherUsingAIRequest}; export type CreateApiCallsCallControlIdActionsGatherUsingAiOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const callControlCommandResultWithConversationId = z.object({result: z.string().optional(), conversation_id: z.string().optional()}); export const createApiCallsCallControlIdActionsGatherUsingAiResponse = z.object({data: callControlCommandResultWithConversationId.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 }; /** * Gather using AI * * POST /calls/{call_control_id}/actions/gather_using_ai */ export const createApiCallsCallControlIdActionsGatherUsingAi = async (args: CreateApiCallsCallControlIdActionsGatherUsingAiArgs, options?: CreateApiCallsCallControlIdActionsGatherUsingAiOptions) => { const {baseUrl = "https://api.telnyx.com/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/calls/${encodeURIComponent(String(args.call_control_id))}/actions/gather_using_ai${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(createApiCallsCallControlIdActionsGatherUsingAiResponse, await res.json()) };