/** * FunctionResult - Builder for SWAIG function responses. * * Carries response text and a list of structured actions (connect, hangup, SMS, etc.). * Every mutating method returns `this` for fluent chaining. */ /** Prompt configuration for a payment collection flow. */ export interface PaymentPrompt { /** The situation this prompt applies to (e.g., "payment-card-number"). */ for: string; /** Actions to perform for this prompt. */ actions: PaymentAction[]; /** Optional card type filter for this prompt. */ card_type?: string; /** Optional error type this prompt handles. */ error_type?: string; } /** A single action within a payment prompt (e.g., say or play). */ export interface PaymentAction { /** The action type (e.g., "say", "play"). */ type: string; /** The phrase or URL to use for this action. */ phrase: string; } /** A custom key-value parameter passed to the payment connector. */ export interface PaymentParameter { /** The parameter name. */ name: string; /** The parameter value. */ value: string; } /** * Builder for SWAIG function responses. * * Carries response text (what the AI says to the caller) and a list of structured * actions (connect, hangup, SMS, record, transfer, etc.) that the SignalWire platform * executes after the AI speaks. Every mutating method returns `this` for fluent chaining. * * Return an instance (or a promise that resolves to one) from any SWAIG tool handler. * * @example Simple text response * ```ts * agent.defineTool({ * name: 'say_hi', * description: 'Say hello.', * parameters: { type: 'object', properties: {} }, * handler: () => new FunctionResult('Hello there!'), * }); * ``` * * @example Response plus a call-control action * ```ts * agent.defineTool({ * name: 'transfer_to_sales', * description: 'Forward the caller to sales.', * parameters: { type: 'object', properties: {} }, * handler: () => * new FunctionResult('Connecting you to sales now.').connect('+15551112222'), * }); * ``` * * @example Chained actions * ```ts * new FunctionResult("Thanks, you're all set.") * .sendSms({ toNumber: '+15551234567', fromNumber: '+15559998888', body: 'Confirmation!' }) * .hangup(); * ``` * * @see {@link AgentBase.defineTool} — where handlers return a `FunctionResult` * @see {@link DataMap} — alternative for purely data-driven (no handler) tools */ export declare class FunctionResult { /** The text response returned to the AI agent. */ response: string; /** Ordered list of actions to execute after the response. */ action: Record[]; /** Whether actions should be post-processed after the AI responds. */ postProcess: boolean; /** * @param response - Initial response text; defaults to empty string. * @param postProcess - Whether to enable post-processing of actions. */ constructor(response?: string, postProcess?: boolean); /** * Set the response text returned to the AI agent. * @param response - The response text. * @returns This instance for chaining. */ setResponse(response: string): this; /** * Enable or disable post-processing of actions. * @param postProcess - Whether to post-process actions. * @returns This instance for chaining. */ setPostProcess(postProcess: boolean): this; /** * Append a single named action to the action list. * @param name - The action name (e.g., "hangup", "say"). * @param data - The action payload. * @returns This instance for chaining. */ addAction(name: string, data: unknown): this; /** * Append multiple action objects to the action list. * @param actions - Array of action objects to append. * @returns This instance for chaining. */ addActions(actions: Record[]): this; /** * Connect the call to another destination via SWML transfer. * @param destination - The destination address (phone number or SIP URI). * @param final - Whether this is a final transfer that ends the AI session. * @param fromAddr - Optional caller ID to use for the outbound leg. * @returns This instance for chaining. */ connect(destination: string, final?: boolean, fromAddr?: string): this; /** * Transfer the call to a SWML destination with a custom AI response. * @param dest - The transfer destination. * @param aiResponse - The AI response text to set before transferring. * @param final - Whether this is a final transfer. * @returns This instance for chaining. */ swmlTransfer(dest: string, aiResponse: string, final?: boolean): this; /** * Hang up the call. * @returns This instance for chaining. */ hangup(): this; /** * Place the call on hold for a specified duration. * @param timeout - Hold duration in seconds, clamped to 0-900. * @returns This instance for chaining. */ hold(timeout?: number): this; /** * Wait for user input before continuing. * @param opts - Options controlling wait behavior: enable/disable, timeout, or answer-first mode. * @returns This instance for chaining. */ waitForUser(opts?: { enabled?: boolean; timeout?: number; answerFirst?: boolean; }): this; /** * Stop the AI session. * @returns This instance for chaining. */ stop(): this; /** * Speak text to the caller via TTS. * @param text - The text to speak. * @returns This instance for chaining. */ say(text: string): this; /** * Play an audio file in the background during the call. * @param filename - URL or path of the audio file. * @param wait - Whether to wait for playback to complete before continuing. * @returns This instance for chaining. */ playBackgroundFile(filename: string, wait?: boolean): this; /** * Stop any currently playing background audio file. * @returns This instance for chaining. */ stopBackgroundFile(): this; /** * Add dynamic speech recognition hints to improve transcription accuracy. * @param hints - Array of hint strings or pattern-replacement objects. * @returns This instance for chaining. */ addDynamicHints(hints: (string | { pattern: string; replace: string; ignore_case?: boolean; })[]): this; /** * Remove all previously added dynamic speech hints. * @returns This instance for chaining. */ clearDynamicHints(): this; /** * Set the silence duration that marks the end of a user's speech. * @param milliseconds - Timeout in milliseconds. * @returns This instance for chaining. */ setEndOfSpeechTimeout(milliseconds: number): this; /** * Set the timeout for speech event detection. * @param milliseconds - Timeout in milliseconds. * @returns This instance for chaining. */ setSpeechEventTimeout(milliseconds: number): this; /** * Merge key-value pairs into the global data store shared across functions. * @param data - Key-value pairs to set or update. * @returns This instance for chaining. */ updateGlobalData(data: Record): this; /** * Remove keys from the global data store. * @param keys - A single key or array of keys to remove. * @returns This instance for chaining. */ removeGlobalData(keys: string | string[]): this; /** * Set metadata key-value pairs on the current call. * @param data - Metadata key-value pairs to set. * @returns This instance for chaining. */ setMetadata(data: Record): this; /** * Remove metadata keys from the current call. * @param keys - A single key or array of keys to remove. * @returns This instance for chaining. */ removeMetadata(keys: string | string[]): this; /** * Execute arbitrary SWML content as an action. * @param swmlContent - SWML as a JSON string or object. * @param transfer - Whether this SWML execution should transfer the call. * @returns This instance for chaining. */ executeSwml(swmlContent: string | Record | { toDict(): Record; }, transfer?: boolean): this; /** * Change the current SWML step. * @param stepName - The name of the step to switch to. * @returns This instance for chaining. */ swmlChangeStep(stepName: string): this; /** * Change the current SWML context. * @param contextName - The name of the context to switch to. * @returns This instance for chaining. */ swmlChangeContext(contextName: string): this; /** * Emit a custom user event via SWML. * @param eventData - The event payload. * @returns This instance for chaining. */ swmlUserEvent(eventData: Record): this; /** * Switch the AI context with optional new prompts and reset options. * @param opts - Context switch options including system/user prompts and reset flags. * @returns This instance for chaining. */ switchContext(opts?: { systemPrompt?: string; userPrompt?: string; consolidate?: boolean; fullReset?: boolean; }): this; /** * Enable or disable SWAIG functions by name. * @param toggles - Array of function name and active state pairs. * @returns This instance for chaining. */ toggleFunctions(toggles: { function: string; active: boolean; }[]): this; /** * Control whether functions fire on speaker timeout. * @param enabled - Whether to enable function execution on timeout. * @returns This instance for chaining. */ enableFunctionsOnTimeout(enabled?: boolean): this; /** * Update AI engine settings at runtime. * @param settings - Key-value pairs of settings to update. * @returns This instance for chaining. */ updateSettings(settings: Record): this; /** * Inject text as if the user had spoken it. * @param text - The simulated user input text. * @returns This instance for chaining. */ simulateUserInput(text: string): this; /** * Enable or disable extensive data reporting in function calls. * @param enabled - Whether to enable extensive data. * @returns This instance for chaining. */ enableExtensiveData(enabled?: boolean): this; /** * Replace the function call output in conversation history. * @param text - Replacement text, or true to replace with the response. * @returns This instance for chaining. */ replaceInHistory(text?: string | boolean): this; /** * Send an SMS or MMS message from within the call flow. * * @param opts - SMS parameters. Must include either `body` (text SMS) or * `media` (MMS) — supplying neither throws. * @returns This instance for chaining. * @throws {Error} When neither `body` nor `media` is provided. */ sendSms(opts: { toNumber: string; fromNumber: string; body?: string; media?: string[]; tags?: string[]; region?: string; }): this; /** * Start recording the call. * @param opts - Recording options including format, direction, and timeouts. * @returns This instance for chaining. */ recordCall(opts?: { controlId?: string; stereo?: boolean; format?: 'wav' | 'mp3'; direction?: 'speak' | 'listen' | 'both'; terminators?: string; beep?: boolean; inputSensitivity?: number; initialTimeout?: number; endSilenceTimeout?: number; maxLength?: number; statusUrl?: string; }): this; /** * Stop an active call recording. * @param controlId - Optional control ID of the recording to stop. * @returns This instance for chaining. */ stopRecordCall(controlId?: string): this; /** * Start a media tap to stream audio to an external URI. * @param opts - Tap parameters including URI, direction, and codec. * @returns This instance for chaining. */ tap(opts: { uri: string; controlId?: string; direction?: 'speak' | 'hear' | 'both'; codec?: 'PCMU' | 'PCMA'; rtpPtime?: number; statusUrl?: string; }): this; /** * Stop an active media tap. * @param controlId - Optional control ID of the tap to stop. * @returns This instance for chaining. */ stopTap(controlId?: string): this; /** * Join a SignalWire room. * @param name - The room name to join. * @returns This instance for chaining. */ joinRoom(name: string): this; /** * Send a SIP REFER to transfer the call. * @param toUri - The SIP URI to refer the call to. * @returns This instance for chaining. */ sipRefer(toUri: string): this; /** * Join a conference by name with optional configuration. * @param name - The conference name to join. * @param opts - Optional conference settings such as mute, recording, and callbacks. * @returns This instance for chaining. */ joinConference(name: string, opts?: { muted?: boolean; beep?: 'true' | 'false' | 'onEnter' | 'onExit'; startOnEnter?: boolean; endOnExit?: boolean; waitUrl?: string; maxParticipants?: number; record?: 'do-not-record' | 'record-from-start'; region?: string; trim?: 'trim-silence' | 'do-not-trim'; coach?: string; statusCallbackEvent?: string; statusCallback?: string; statusCallbackMethod?: 'GET' | 'POST'; recordingStatusCallback?: string; recordingStatusCallbackMethod?: 'GET' | 'POST'; recordingStatusCallbackEvent?: string; result?: unknown; }): this; /** * Execute a SignalWire RPC method via SWML. * @param opts - RPC parameters including method name and optional call/node IDs. * @returns This instance for chaining. */ executeRpc(opts: { method: string; params?: Record; callId?: string; nodeId?: string; }): this; /** * Dial a number via RPC, optionally specifying device type. * @param toNumber - The destination phone number. * @param fromNumber - The caller ID number. * @param destSwml - The SWML destination for the dialed call. * @param deviceType - The device type (defaults to "phone"). * @returns This instance for chaining. */ rpcDial(toNumber: string, fromNumber: string, destSwml: string, deviceType?: string): this; /** * Send an AI message to another call via RPC. * @param callId - The target call ID. * @param messageText - The message text to inject. * @param role - The message role (defaults to "system"). * @returns This instance for chaining. */ rpcAiMessage(callId: string, messageText: string, role?: string): this; /** * Unhold a call that was previously placed on hold via RPC. * @param callId - The target call ID to unhold. * @returns This instance for chaining. */ rpcAiUnhold(callId: string): this; /** * Initiate a payment collection flow on the call. * @param opts - Payment configuration including connector URL, method, and prompt options. * @returns This instance for chaining. */ pay(opts: { paymentConnectorUrl: string; inputMethod?: string; statusUrl?: string; paymentMethod?: string; timeout?: number; maxAttempts?: number; securityCode?: boolean; postalCode?: boolean | string; minPostalCodeLength?: number; tokenType?: string; chargeAmount?: string; currency?: string; language?: string; voice?: string; description?: string; validCardTypes?: string; parameters?: PaymentParameter[]; prompts?: PaymentPrompt[]; aiResponse?: string; }): this; /** * Create a payment prompt configuration object. * @param forSituation - The situation this prompt applies to. * @param actions - Actions to perform for this prompt. * @param cardType - Optional card type filter. * @param errorType - Optional error type this prompt handles. * @returns A new PaymentPrompt object. */ static createPaymentPrompt(forSituation: string, actions: PaymentAction[], cardType?: string, errorType?: string): PaymentPrompt; /** * Create a payment action for use within a payment prompt. * @param actionType - The action type (e.g., "say", "play"). * @param phrase - The phrase or URL for this action. * @returns A new PaymentAction object. */ static createPaymentAction(actionType: string, phrase: string): PaymentAction; /** * Create a custom payment parameter for the payment connector. * @param name - The parameter name. * @param value - The parameter value. * @returns A new PaymentParameter object. */ static createPaymentParameter(name: string, value: string): PaymentParameter; /** * Serialize this result to a plain object for the SWAIG response. * @returns A dictionary with response, action, and post_process fields; falls back to "Action completed." if empty. */ toDict(): Record; }