import {z} from 'zod' export type VoiceSettings = {voice: string, voice_speed?: number | undefined, api_key_ref?: string | undefined, temperature?: number | undefined, similarity_boost?: number | undefined, use_speaker_boost?: boolean | undefined, style?: number | undefined, speed?: 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, expressive_mode?: boolean | undefined, background_audio?: ({type: 'predefined_media', value: 'silence' | 'office', volume?: number | undefined} | {type: 'media_url', value: string} | {type: 'media_name', value: string}) | 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 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 CallAssistantRequest = {id: string, model?: string | undefined, name?: string | undefined, instructions?: string | undefined, greeting?: string | undefined, voice_settings?: VoiceSettings | undefined, transcription?: TranscriptionConfig | undefined, tools?: (Array) | undefined, llm_api_key_ref?: string | undefined, openai_api_key_ref?: string | undefined, dynamic_variables?: ({[key: string]: string | number | boolean}) | undefined, fallback_config?: ({model?: string | undefined, llm_api_key_ref?: string | undefined, external_llm?: ({model?: string | undefined, base_url?: string | undefined, llm_api_key_ref?: string | undefined, authentication_method?: ('token' | 'certificate') | undefined, certificate_ref?: string | undefined, token_retrieval_url?: string | undefined, forward_metadata?: boolean | undefined}) | undefined}) | undefined, external_llm?: ({model?: string | undefined, base_url?: string | undefined, llm_api_key_ref?: string | undefined, authentication_method?: ('token' | 'certificate') | undefined, certificate_ref?: string | undefined, token_retrieval_url?: string | undefined, forward_metadata?: boolean | undefined}) | undefined, mcp_servers?: Array> | undefined, observability_settings?: Record | undefined}; export type InterruptionSettings = {enable?: boolean | undefined}; export type MessageMetadata = Record; export type UserMessage = {role: 'user', content: string, metadata?: MessageMetadata | undefined}; export type ToolCall = {id: string, type: 'function', function: {name: string}}; export type AssistantMessage = {role: 'assistant', content?: string | undefined, tool_calls?: Array | undefined, metadata?: MessageMetadata | undefined}; export type ToolMessage = {role: 'tool', content: string, tool_call_id: string, metadata?: MessageMetadata | undefined}; export type SystemMessage = {role: 'system', content: string, metadata?: MessageMetadata | undefined}; export type DeveloperMessage = {role: 'developer', content: string, metadata?: MessageMetadata | undefined}; export type AIAssistantJoinParticipant = {id: string, role: 'user', name?: string | undefined, on_hangup?: ('continue_conversation' | 'end_conversation') | undefined}; export type AIAssistantStartRequest = {assistant?: CallAssistantRequest | undefined, greeting?: string | undefined, interruption_settings?: InterruptionSettings | undefined, transcription?: TranscriptionConfig | undefined, message_history?: (Array) | undefined, send_message_history_updates?: boolean | undefined, participants?: Array | undefined, client_state?: string | undefined, command_id?: string | undefined}; export type CreateApiCallsCallControlIdActionsAiAssistantStartArgs = {call_control_id: string, body: AIAssistantStartRequest}; export type CreateApiCallsCallControlIdActionsAiAssistantStartOptions = {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 createApiCallsCallControlIdActionsAiAssistantStartResponse = 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 }; /** * Start AI Assistant * * POST /calls/{call_control_id}/actions/ai_assistant_start */ export const createApiCallsCallControlIdActionsAiAssistantStart = async (args: CreateApiCallsCallControlIdActionsAiAssistantStartArgs, options?: CreateApiCallsCallControlIdActionsAiAssistantStartOptions) => { 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/ai_assistant_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(createApiCallsCallControlIdActionsAiAssistantStartResponse, await res.json()) };