/** * Call object — represents a live RELAY call with command methods. * * Created by RelayClient on inbound calling.call.receive events or * outbound dial responses. */ import { Action, PlayAction, RecordAction, DetectAction, CollectAction, StandaloneCollectAction, FaxAction, TapAction, StreamAction, PayAction, TranscribeAction, AIAction } from './Action.js'; import { type Deferred } from './Deferred.js'; import { RelayEvent } from './RelayEvent.js'; import type { CompletedCallback, EventHandler } from './types.js'; /** Interface the Call needs from RelayClient (avoids circular import). */ export interface RelayClientLike { execute(method: string, params: Record): Promise>; } /** * Live RELAY call with command methods. * * Don't construct directly — `Call` instances are created by {@link RelayClient} * for inbound calls (delivered to your `onCall` handler) and for outbound dials. * * Each command method (`answer()`, `play()`, `record()`, etc.) returns when the * platform acknowledges the command; event-driven completion is exposed via * {@link Action} objects returned from the async "play/record/..." variants. * * @example Inside an onCall handler * ```ts * client.onCall(async (call) => { * await call.answer(); * const play = await call.playAsync({ play: [{ type: 'tts', text: 'Hello!' }] }); * await play.wait(); * await call.hangup(); * }); * ``` * * @see {@link RelayClient} * @see {@link Action} */ export declare class Call { /** @internal */ readonly _client: RelayClientLike; /** Unique call identifier assigned by the platform. */ callId: string; /** RELAY node that owns this call. */ nodeId: string; /** SignalWire project ID. */ projectId: string; /** RELAY context this call was received on. */ context: string; /** Opaque correlation tag attached at dial time. */ tag: string; /** `"inbound"` or `"outbound"`. */ direction: string; /** Device descriptor the call is associated with (phone, SIP, etc.). */ device: Record; /** Current call state (e.g. `"created"`, `"answered"`, `"ended"`). */ state: string; /** Call segment ID used for event correlation. */ segmentId: string; /** @internal */ readonly _listeners: Map; /** @internal */ readonly _actions: Map; /** @internal */ readonly _ended: Deferred; constructor(client: RelayClientLike, callId: string, nodeId: string, projectId: string, context: string, options?: { tag?: string; direction?: string; device?: Record; state?: string; segmentId?: string; }); /** @internal Send a calling. JSON-RPC request for this call. */ _execute(method: string, extraParams?: Record): Promise>; /** * Register an event listener for this call. * * Multiple listeners for the same event type are supported. Listener errors * are logged but do not disrupt dispatch to other listeners. * * @param eventType - Fully-qualified event type (e.g. * `"calling.call.state"`, `"calling.call.play"`). * @param handler - Callback invoked for each matching event. */ on(eventType: string, handler: EventHandler): void; /** @internal Called by RelayClient when an event arrives for this call. */ _dispatchEvent(payload: Record): Promise; /** * Wait for a specific event, optionally filtered by predicate. * * Registers a one-shot listener that is removed after resolving (or after * the timeout elapses). Useful for awaiting a specific state transition. * * @param eventType - Event type to wait for. * @param predicate - Optional filter — only events for which this returns * `true` resolve the wait. * @param timeout - Optional timeout in milliseconds. Omit to wait forever. * @returns The first matching {@link RelayEvent}. * @throws {Error} When the optional timeout elapses before a match. */ waitFor(eventType: string, predicate?: (event: RelayEvent) => boolean, timeout?: number): Promise; /** * Wait for the call to reach the `ended` state. * * @param timeout - Optional timeout in milliseconds. * @returns The terminating `calling.call.state` {@link RelayEvent}. * @throws {Error} When the optional timeout elapses before the call ends. */ waitForEnded(timeout?: number): Promise; /** @internal Register an action, execute the RPC, clean up on failure. */ _startAction(action: T, method: string, params: Record, onCompleted?: CompletedCallback | null): Promise; /** * Answer an inbound call. * * @param extra - Optional additional params merged into the answer request * (e.g. SIP headers, codec hints). * @returns The platform's answer response. * @throws {RelayError} When the answer command is rejected. */ answer(extra?: Record): Promise>; /** * End / hang up the call. * * @param reason - Hangup reason delivered to the platform. Defaults to * `"hangup"`. * @returns The platform's end response. * @throws {RelayError} When the end command is rejected. */ hangup(reason?: string): Promise>; /** * Decline control of an inbound call, returning it to routing. * * Use instead of {@link answer} when you want the call to fall through to * the next matching route rather than be handled by this client. * * @returns The platform's pass response. * @throws {RelayError} When the pass command is rejected. */ pass(): Promise>; /** * Play audio content on the call. * * Returns immediately with a {@link PlayAction} — `await action.wait()` to * block until playback finishes, or call `stop()` / `pause()` / `resume()` * / `volume()` on it to control playback. * * @param media - Platform-shaped play items (TTS, audio URLs, silence, etc.). * @param options - Optional playback controls. * @param options.volume - Volume adjustment in dB. * @param options.direction - `"speak"`, `"hear"`, or `"both"`. * @param options.loop - Number of times to loop the playback. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when playback reaches a * terminal state. * @returns A {@link PlayAction} for control and completion tracking. * @throws {RelayError} When the play command is rejected. */ play(media: Record[], options?: { volume?: number; direction?: string; loop?: number; controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Record audio from the call. * * @param audio - Platform-shaped audio recording options (direction, codec, * timeouts, etc.). Defaults to `{}`. * @param options - Recording behaviour. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when the recording reaches a * terminal state. * @returns A {@link RecordAction} for control and completion tracking. * @throws {RelayError} When the record command is rejected. */ record(audio?: Record, options?: { controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Play audio and collect digit / speech input in a single operation. * * Convenient for IVR prompts — playback stops as soon as input is received. * * @param media - Audio items to play while collecting. * @param collect - Platform-shaped collect config (`digits`, `speech`, etc.). * @param options - Playback and collection behaviour. * @param options.volume - Playback volume adjustment in dB. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when collect completes. * @returns A {@link CollectAction} for the combined play-and-collect flow. * @throws {RelayError} When the play_and_collect command is rejected. */ playAndCollect(media: Record[], collect: Record, options?: { volume?: number; controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Collect digit / speech input without playing media. * * @param options - Collect configuration. * @param options.digits - DTMF digit-collection config. * @param options.speech - Speech-recognition config. * @param options.initialTimeout - Seconds to wait for the first input. * @param options.partialResults - Emit partial (interim) results. * @param options.continuous - Keep collecting until explicitly stopped. * @param options.sendStartOfInput - Emit a start-of-input event when detected. * @param options.startInputTimers - Start input timers immediately. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when collect completes. * @returns A {@link StandaloneCollectAction} for control and completion tracking. * @throws {RelayError} When the collect command is rejected. */ collect(options?: { digits?: Record; speech?: Record; initialTimeout?: number; partialResults?: boolean; continuous?: boolean; sendStartOfInput?: boolean; startInputTimers?: boolean; controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Bridge the call to one or more destinations. * * @param devices - Serial/parallel dial plan — outer array of serial groups, * inner arrays dialled in parallel. * @param options - Connect behaviour. * @param options.ringback - Ringback media played to the originating party. * @param options.tag - Tag for event correlation. * @param options.maxDuration - Maximum connect duration in seconds. * @param options.maxPricePerMinute - Price cap per minute. * @param options.statusUrl - Webhook URL for status events. * @returns The platform's connect response. * @throws {RelayError} When the connect command is rejected. */ connect(devices: Record[][], options?: { ringback?: Record[]; tag?: string; maxDuration?: number; maxPricePerMinute?: number; statusUrl?: string; }): Promise>; /** * Disconnect (unbridge) a connected call. * * @returns The platform's disconnect response. * @throws {RelayError} When the disconnect command is rejected. */ disconnect(): Promise>; /** * Send DTMF digits on the call. * * @param digits - The DTMF digit string to send (e.g. `"1234#"`, `"ww*9"`). * @param controlId - Explicit control ID. Auto-generated when omitted. * @returns The platform's send-digits response. * @throws {RelayError} When the send_digits command is rejected. */ sendDigits(digits: string, controlId?: string): Promise>; /** * Start audio detection (answering machine, fax, DTMF). * * @param detect - Platform-shaped detect configuration. * @param options - Detection behaviour. * @param options.timeout - Detection timeout in seconds. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when detection completes. * @returns A {@link DetectAction} for control and completion tracking. * @throws {RelayError} When the detect command is rejected. */ detect(detect: Record, options?: { timeout?: number; controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Transfer a SIP call via REFER. * * @param device - Platform-shaped SIP target device descriptor. * @param options - Optional REFER behaviour. * @param options.statusUrl - Webhook URL for REFER status events. * @returns The platform's refer response. * @throws {RelayError} When the refer command is rejected. */ refer(device: Record, options?: { statusUrl?: string; }): Promise>; /** * Start a PCI-compliant payment collection flow. * * @param paymentConnectorUrl - URL of the configured payment connector to * tokenise the card with. * @param options - Payment configuration — card-type filters, prompts, * charge amount, etc. See the SignalWire pay documentation for field details. * @returns A {@link PayAction} tracking the payment-collection flow. * @throws {RelayError} When the pay command is rejected. */ pay(paymentConnectorUrl: string, options?: { controlId?: string; inputMethod?: string; statusUrl?: string; paymentMethod?: string; timeout?: string; maxAttempts?: string; securityCode?: string; postalCode?: string; minPostalCodeLength?: string; tokenType?: string; chargeAmount?: string; currency?: string; language?: string; voice?: string; description?: string; validCardTypes?: string; parameters?: Record[]; prompts?: Record[]; onCompleted?: CompletedCallback; }): Promise; /** * Send a fax document. * * @param document - URL of the document to fax (TIFF or PDF). * @param options - Fax behaviour. * @param options.identity - Caller identity string sent in the fax header. * @param options.headerInfo - Additional fax header text. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when the fax completes. * @returns A {@link FaxAction} for control and completion tracking. * @throws {RelayError} When the send_fax command is rejected. */ sendFax(document: string, options?: { identity?: string; headerInfo?: string; controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Receive a fax and save it server-side. * * @param options - Fax reception behaviour. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when the fax completes. * @returns A {@link FaxAction} for control and completion tracking. * @throws {RelayError} When the receive_fax command is rejected. */ receiveFax(options?: { controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Intercept call media and stream it to an external destination. * * @param tap - Platform-shaped tap configuration (direction, codec, etc.). * @param device - Destination device descriptor (WebSocket URL, etc.). * @param options - Tap behaviour. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when the tap completes. * @returns A {@link TapAction} for control and completion tracking. * @throws {RelayError} When the tap command is rejected. */ tap(tap: Record, device: Record, options?: { controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Start streaming call audio to a WebSocket endpoint. * * @param url - WebSocket URL to stream audio to. * @param options - Stream behaviour. * @param options.name - Friendly name for the stream. * @param options.codec - Audio codec (e.g. `"PCMU"`, `"PCMA"`). * @param options.track - Which track to send: `"inbound"`, `"outbound"`, or * `"both"`. * @param options.statusUrl - Webhook URL for stream status events. * @param options.statusUrlMethod - HTTP method for `statusUrl` requests. * @param options.authorizationBearerToken - Bearer token sent to the stream endpoint. * @param options.customParameters - Extra parameters forwarded to the stream endpoint. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.onCompleted - Callback fired when the stream completes. * @returns A {@link StreamAction} for control and completion tracking. * @throws {RelayError} When the stream command is rejected. */ stream(url: string, options?: { name?: string; codec?: string; track?: string; statusUrl?: string; statusUrlMethod?: string; authorizationBearerToken?: string; customParameters?: Record; controlId?: string; onCompleted?: CompletedCallback; }): Promise; /** * Transfer call control to another RELAY app or SWML script. * * @param dest - Destination identifier (RELAY context, SWML URL, etc.). * @param extra - Optional additional params merged into the transfer request. * @returns The platform's transfer response. * @throws {RelayError} When the transfer command is rejected. */ transfer(dest: string, extra?: Record): Promise>; /** * Join an ad-hoc audio conference. * * @param name - Conference name. Participants on the same name hear each other. * @param options - Conference behaviour (muting, recording, status callbacks, etc.). * @returns The platform's join-conference response. * @throws {RelayError} When the join_conference command is rejected. */ joinConference(name: string, options?: { muted?: boolean; beep?: string; startOnEnter?: boolean; endOnExit?: boolean; waitUrl?: string; maxParticipants?: number; record?: string; region?: string; trim?: string; coach?: string; statusCallback?: string; statusCallbackEvent?: string; statusCallbackEventType?: string; statusCallbackMethod?: string; recordingStatusCallback?: string; recordingStatusCallbackEvent?: string; recordingStatusCallbackEventType?: string; recordingStatusCallbackMethod?: string; stream?: Record; }): Promise>; /** * Leave an audio conference. * * @param conferenceId - Identifier of the conference to leave. * @param extra - Optional additional params merged into the request. * @returns The platform's leave-conference response. * @throws {RelayError} When the leave_conference command is rejected. */ leaveConference(conferenceId: string, extra?: Record): Promise>; /** * Put the call on hold. * * @returns The platform's hold response. * @throws {RelayError} When the hold command is rejected. */ hold(): Promise>; /** * Release the call from hold. * * @returns The platform's unhold response. * @throws {RelayError} When the unhold command is rejected. */ unhold(): Promise>; /** * Start noise reduction on the call. * * @returns The platform's denoise response. * @throws {RelayError} When the denoise command is rejected. */ denoise(): Promise>; /** * Stop noise reduction on the call. * * @returns The platform's denoise-stop response. * @throws {RelayError} When the denoise.stop command is rejected. */ denoiseStop(): Promise>; /** * Start transcribing the call. * * @param options - Transcription behaviour. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.statusUrl - Webhook URL for transcription status events. * @param options.onCompleted - Callback fired when transcription completes. * @returns A {@link TranscribeAction} for control and completion tracking. * @throws {RelayError} When the transcribe command is rejected. */ transcribe(options?: { controlId?: string; statusUrl?: string; onCompleted?: CompletedCallback; }): Promise; /** * Echo audio back to the caller (useful for testing network round-trip). * * @param options - Echo behaviour. * @param options.timeout - How long to echo, in seconds. * @param options.statusUrl - Webhook URL for echo status events. * @returns The platform's echo response. * @throws {RelayError} When the echo command is rejected. */ echo(options?: { timeout?: number; statusUrl?: string; }): Promise>; /** * Bind a DTMF digit sequence to trigger a RELAY method automatically when * the caller presses it. * * @param digits - DTMF sequence that triggers the binding (e.g. `"*9"`). * @param bindMethod - RELAY method to invoke when the sequence is detected. * @param options - Binding behaviour. * @param options.bindParams - Params forwarded to `bindMethod` on trigger. * @param options.realm - Optional realm label so bindings can be cleared in groups. * @param options.maxTriggers - Maximum number of times the binding can fire. * @returns The platform's bind_digit response. * @throws {RelayError} When the bind_digit command is rejected. */ bindDigit(digits: string, bindMethod: string, options?: { bindParams?: Record; realm?: string; maxTriggers?: number; }): Promise>; /** * Clear all digit bindings, optionally filtered by realm. * * @param realm - When provided, only bindings with this realm label are cleared. * @returns The platform's clear_digit_bindings response. * @throws {RelayError} When the clear_digit_bindings command is rejected. */ clearDigitBindings(realm?: string): Promise>; /** * Start or stop live transcription on the call. * * @param action - Platform-shaped action block (`start` or `stop` plus * configuration). * @param extra - Optional additional params merged into the request. * @returns The platform's live_transcribe response. * @throws {RelayError} When the live_transcribe command is rejected. */ liveTranscribe(action: Record, extra?: Record): Promise>; /** * Start or stop live translation on the call. * * @param action - Platform-shaped action block (`start` or `stop` plus * source/target languages). * @param options - Live-translate behaviour. * @param options.statusUrl - Webhook URL for translation status events. * @returns The platform's live_translate response. * @throws {RelayError} When the live_translate command is rejected. */ liveTranslate(action: Record, options?: { statusUrl?: string; }): Promise>; /** * Join a video / audio room. * * @param name - Room name — callers on the same name share the same room. * @param options - Room behaviour. * @param options.statusUrl - Webhook URL for room status events. * @returns The platform's join_room response. * @throws {RelayError} When the join_room command is rejected. */ joinRoom(name: string, options?: { statusUrl?: string; }): Promise>; /** * Leave the current room. * * @param extra - Optional additional params merged into the request. * @returns The platform's leave_room response. * @throws {RelayError} When the leave_room command is rejected. */ leaveRoom(extra?: Record): Promise>; /** * Start an AI agent session on the call. * * @param options - AI configuration — prompts, voices, tools, languages, etc. * @returns An {@link AIAction} for control and completion tracking. * @throws {RelayError} When the ai command is rejected. */ ai(options?: { controlId?: string; agent?: string; prompt?: Record; postPrompt?: Record; postPromptUrl?: string; postPromptAuthUser?: string; postPromptAuthPassword?: string; globalData?: Record; pronounce?: Record[]; hints?: string[]; languages?: Record[]; SWAIG?: Record; aiParams?: Record; onCompleted?: CompletedCallback; }): Promise; /** * Connect the call to an Amazon Bedrock AI agent. * * @param options - Bedrock agent configuration. * @param options.prompt - Prompt to send to the Bedrock agent. * @param options.SWAIG - SWAIG configuration (functions, includes, etc.). * @param options.aiParams - AI engine parameters. * @param options.globalData - Global-data object available to SWAIG tools. * @param options.postPrompt - Post-prompt configuration. * @param options.postPromptUrl - URL to POST the final summary to. * @returns The platform's amazon_bedrock response. * @throws {RelayError} When the amazon_bedrock command is rejected. */ amazonBedrock(options?: { prompt?: unknown; SWAIG?: Record; aiParams?: Record; globalData?: Record; postPrompt?: Record; postPromptUrl?: string; }): Promise>; /** * Send a message into an active AI agent session. * * @param options - Message parameters. * @param options.messageText - Text content to inject into the conversation. * @param options.role - Speaker role (`"system"`, `"user"`, or `"assistant"`). * @param options.reset - Reset directives for the conversation state. * @param options.globalData - Global-data updates to merge. * @returns The platform's ai_message response. * @throws {RelayError} When the ai_message command is rejected. */ aiMessage(options?: { messageText?: string; role?: string; reset?: Record; globalData?: Record; }): Promise>; /** * Put the AI agent session on hold (pause turn-taking). * * @param options - Hold behaviour. * @param options.timeout - Maximum hold duration. * @param options.prompt - Prompt played to the caller during the hold. * @returns The platform's ai_hold response. * @throws {RelayError} When the ai_hold command is rejected. */ aiHold(options?: { timeout?: string; prompt?: string; }): Promise>; /** * Resume an AI agent session from hold. * * @param options - Unhold behaviour. * @param options.prompt - Prompt played to the caller on resume. * @returns The platform's ai_unhold response. * @throws {RelayError} When the ai_unhold command is rejected. */ aiUnhold(options?: { prompt?: string; }): Promise>; /** * Emit a custom user-defined event on the call for your webhooks. * * @param options - Freeform event payload. Set `options.event` for the * event name and include any additional fields your webhook expects. * @returns The platform's user_event response. * @throws {RelayError} When the user_event command is rejected. */ userEvent(options?: { event?: string; } & Record): Promise>; /** * Place the call into a named queue. * * @param queueName - Name of the queue to enter. * @param options - Queue-entry behaviour. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.statusUrl - Webhook URL for queue status events. * @returns The platform's queue.enter response. * @throws {RelayError} When the queue.enter command is rejected. */ queueEnter(queueName: string, options?: { controlId?: string; statusUrl?: string; }): Promise>; /** * Remove the call from a queue. * * @param queueName - Name of the queue to leave. * @param options - Queue-exit behaviour. * @param options.controlId - Explicit control ID. Auto-generated when omitted. * @param options.queueId - Queue ID override (when multiple queues share a name). * @param options.statusUrl - Webhook URL for queue status events. * @returns The platform's queue.leave response. * @throws {RelayError} When the queue.leave command is rejected. */ queueLeave(queueName: string, options?: { controlId?: string; queueId?: string; statusUrl?: string; }): Promise>; /** * Return a human-readable diagnostic string. * * @returns `` — handy for log output. */ toString(): string; }