import {z} from 'zod' export type StreamCodec = 'PCMU' | 'PCMA' | 'G722' | 'OPUS' | 'AMR-WB' | 'L16' | 'default'; export type StreamBidirectionalMode = 'mp3' | 'rtp'; export type StreamBidirectionalCodec = 'PCMU' | 'PCMA' | 'G722' | 'OPUS' | 'AMR-WB' | 'L16'; export type StreamBidirectionalTargetLegs = 'both' | 'self' | 'opposite'; export type StreamBidirectionalSamplingRate = 8000 | 16000 | 22050 | 24000 | 48000; export type DialogflowConfig = {analyze_sentiment?: boolean | undefined, partial_automated_agent_reply?: boolean | undefined}; export type StartStreamingRequest = {stream_url?: string | undefined, stream_track?: ('inbound_track' | 'outbound_track' | 'both_tracks') | undefined, stream_codec?: StreamCodec | undefined, stream_bidirectional_mode?: StreamBidirectionalMode | undefined, stream_bidirectional_codec?: StreamBidirectionalCodec | undefined, stream_bidirectional_target_legs?: StreamBidirectionalTargetLegs | undefined, stream_bidirectional_sampling_rate?: StreamBidirectionalSamplingRate | undefined, enable_dialogflow?: boolean | undefined, dialogflow_config?: DialogflowConfig | undefined, client_state?: string | undefined, command_id?: string | undefined, custom_parameters?: (Array<{name?: string | undefined, value?: string | undefined}>) | undefined, stream_auth_token?: string | undefined}; export type CreateApiCallsCallControlIdActionsStreamingStartArgs = {call_control_id: string, body: StartStreamingRequest}; export type CreateApiCallsCallControlIdActionsStreamingStartOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const callControlCommandResult = z.object({result: z.string().optional()}); export const createApiCallsCallControlIdActionsStreamingStartResponse = z.object({data: callControlCommandResult.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 }; /** * Streaming start * * POST /calls/{call_control_id}/actions/streaming_start */ export const createApiCallsCallControlIdActionsStreamingStart = async (args: CreateApiCallsCallControlIdActionsStreamingStartArgs, options?: CreateApiCallsCallControlIdActionsStreamingStartOptions) => { 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/streaming_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(createApiCallsCallControlIdActionsStreamingStartResponse, await res.json()) };