import {z} from 'zod' export type ConversationRelayInterruptible = 'none' | 'any' | 'speech' | 'dtmf'; 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 MinimaxVoiceSettings = {type: 'minimax', speed?: number | undefined, vol?: number | undefined, pitch?: number | undefined, language_boost?: ('null' | 'auto' | 'Chinese' | 'Chinese,Yue' | 'English' | 'Arabic' | 'Russian' | 'Spanish' | 'French' | 'Portuguese' | 'German' | 'Turkish' | 'Dutch' | 'Ukrainian' | 'Vietnamese' | 'Indonesian' | 'Japanese' | 'Italian' | 'Korean' | 'Thai' | 'Polish' | 'Romanian' | 'Greek' | 'Czech' | 'Finnish' | 'Hindi' | 'Bulgarian' | 'Danish' | 'Hebrew' | 'Malay' | 'Persian' | 'Slovak' | 'Swedish' | 'Croatian' | 'Filipino' | 'Hungarian' | 'Norwegian' | 'Slovenian' | 'Catalan' | 'Nynorsk' | 'Tamil' | 'Afrikaans' | null) | undefined}; 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 InworldVoiceSettings = {type: 'inworld', delivery_mode?: ('STABLE' | 'BALANCED' | 'CREATIVE') | undefined}; export type XAIVoiceSettings = {type: 'xai', language?: string | undefined}; export type SonioxVoiceSettings = {type: 'soniox', speed?: number | undefined, reduce_silence?: boolean | undefined}; export type ConversationRelayTranscriptionEngineConfig = {interim_results?: boolean | undefined, eot_threshold?: number | undefined, eager_eot_threshold?: number | undefined} | {[key: string]: unknown}; export type ConversationRelayLanguage = {language: string, tts_provider?: string | undefined, voice?: string | undefined, voice_settings?: (ElevenLabsVoiceSettings | TelnyxVoiceSettings | AWSVoiceSettings | MinimaxVoiceSettings | AzureVoiceSettings | ResembleVoiceSettings | InworldVoiceSettings | XAIVoiceSettings | SonioxVoiceSettings) | undefined, transcription_engine?: ('Google' | 'Telnyx' | 'Deepgram' | 'Azure' | 'xAI' | 'AssemblyAI' | 'Speechmatics' | 'Soniox' | 'A' | 'B') | undefined, transcription_engine_config?: ConversationRelayTranscriptionEngineConfig | undefined, transcription_provider?: string | undefined, speech_model?: string | undefined}; export type ConversationRelaySettings = {url: string, dtmf_detection?: boolean | undefined, interruptible?: ConversationRelayInterruptible | undefined, interruptible_greeting?: ConversationRelayInterruptible | undefined, languages?: Array | undefined}; export type VoiceConfig = string; export type ConversationRelayInterruptionSettings = {enable?: boolean | undefined, interruptible?: ConversationRelayInterruptible | undefined, interruptible_greeting?: ConversationRelayInterruptible | undefined, welcome_greeting_interruptible?: ConversationRelayInterruptible | undefined}; export type ConversationRelayAssistant = {dynamic_variables?: {[key: string]: string} | undefined} | {[key: string]: unknown}; export type ConversationRelayStructuredProvider = {[key: string]: unknown}; export type ConversationRelayStartRequest = {conversation_relay_settings?: ConversationRelaySettings | undefined, url?: string | undefined, dtmf_detection?: boolean | undefined, conversation_relay_url?: string | undefined, conversation_relay_dtmf_detection?: boolean | undefined, voice?: VoiceConfig | undefined, voice_settings?: (ElevenLabsVoiceSettings | TelnyxVoiceSettings | AWSVoiceSettings | MinimaxVoiceSettings | AzureVoiceSettings | ResembleVoiceSettings | InworldVoiceSettings | XAIVoiceSettings | SonioxVoiceSettings) | undefined, greeting?: string | undefined, language?: string | undefined, languages?: Array | undefined, interruption_settings?: ConversationRelayInterruptionSettings | undefined, transcription?: {[key: string]: unknown} | undefined, assistant?: ConversationRelayAssistant | undefined, client_state?: string | undefined, command_id?: string | undefined, tts_provider?: string | undefined, provider?: string | undefined, structured_provider?: ConversationRelayStructuredProvider | undefined, transcription_engine?: ('Google' | 'Telnyx' | 'Deepgram' | 'Azure' | 'xAI' | 'AssemblyAI' | 'Speechmatics' | 'Soniox' | 'A' | 'B') | undefined, transcription_engine_config?: ConversationRelayTranscriptionEngineConfig | undefined, interruptible?: ConversationRelayInterruptible | undefined, interruptible_greeting?: ConversationRelayInterruptible | undefined, custom_parameters?: {[key: string]: unknown} | undefined}; export type CreateApiCallsCallControlIdActionsConversationRelayStartArgs = {call_control_id: string, body: ConversationRelayStartRequest}; export type CreateApiCallsCallControlIdActionsConversationRelayStartOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const callControlCommandResultWithConversationRelayId = z.object({result: z.string().optional(), conversation_relay_id: z.string().optional()}); export const createApiCallsCallControlIdActionsConversationRelayStartResponse = z.object({data: callControlCommandResultWithConversationRelayId.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 Conversation Relay * * POST /calls/{call_control_id}/actions/conversation_relay_start */ export const createApiCallsCallControlIdActionsConversationRelayStart = async (args: CreateApiCallsCallControlIdActionsConversationRelayStartArgs, options?: CreateApiCallsCallControlIdActionsConversationRelayStartOptions) => { 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/conversation_relay_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(createApiCallsCallControlIdActionsConversationRelayStartResponse, await res.json()) };