FunctionResult

View as MarkdownOpen in Claude

FunctionResult is the return type for all SWAIG tool functions. It wraps a response message (text for the AI to speak) and an ordered list of actions (transfers, SMS, data updates, context switches, and more). Every method returns this, so you can chain calls into a single fluent expression.

Returned from functions defined with defineTool() on AgentBase.

FunctionResult builds the response payload for a SWAIG function. See the SWML SWAIG functions reference for the full response format specification.

Properties

response
stringDefaults to ''

Text the AI speaks back to the caller after the function executes.

action
Record<string, unknown>[]Defaults to []

Ordered list of action objects to execute. Actions run sequentially in the order they were added.

postProcess
booleanDefaults to false

When true, the AI speaks the response and takes one more conversational turn with the user before executing actions. When false (default), actions execute immediately after the response.

Example

1import { AgentBase, FunctionResult } from '@signalwire/sdk';
2
3const agent = new AgentBase({ name: 'my-agent', route: '/agent' });
4agent.setPromptText('You are a helpful assistant.');
5
6agent.defineTool({
7 name: 'transfer_to_billing',
8 description: 'Transfer the caller to billing',
9 handler: (args, rawData) => {
10 return new FunctionResult(
11 "I'll transfer you to billing. Anything else first?",
12 true // postProcess
13 )
14 .updateGlobalData({ transferred: true })
15 .sendSms({
16 toNumber: '+15551234567',
17 fromNumber: '+15559876543',
18 body: 'You are being transferred to billing.',
19 })
20 .connect('+15551234567', true);
21 },
22});
23
24agent.run();

Fluent Chaining Pattern

Every method on FunctionResult returns this, so you build complex responses in a single expression. Actions execute in the order they are added.

Terminal actions like connect(destination, true) and hangup() end the call flow. Place them last in the chain so that preceding actions (data updates, SMS, etc.) have a chance to execute.

1import { AgentBase, FunctionResult } from '@signalwire/sdk';
2
3const agent = new AgentBase({ name: 'my-agent', route: '/agent' });
4agent.setPromptText('You are a helpful assistant.');
5
6agent.defineTool({
7 name: 'transfer_now',
8 description: 'Transfer the caller',
9 handler: (args, rawData) => {
10 // Data update + SMS execute before the terminal transfer
11 return new FunctionResult('Transferring you now.')
12 .updateGlobalData({ transferred: true })
13 .sendSms({
14 toNumber: '+15551234567',
15 fromNumber: '+15559876543',
16 body: 'Your call is being transferred.',
17 })
18 .connect('+15551234567', true); // terminal — goes last
19 },
20});
21
22agent.run();

Methods

Core

Call Control

Speech

Media

Data

Context Navigation

Events

Functions

Hints

Settings

SMS

Payment

SIP

Rooms and Conferences

SWML and RPC