Helper Functions

View as MarkdownOpen in Claude
import { createSimpleApiTool } from '@signalwire/sdk';
const weatherTool = createSimpleApiTool({
name: 'get_weather',
url: 'https://api.weather.com/v1/current?q=${args.location}',
responseTemplate: 'Weather: ${response.current.condition.text}, ${response.current.temp_f}F',
parameters: {
location: { type: 'string', description: 'City name', required: true },
},
});

Create a DataMap for a straightforward single-endpoint API call with minimal configuration.

Parameters

name
stringRequired

Function name.

url
stringRequired

API endpoint URL.

responseTemplate
stringRequired

Template string for formatting the response.

parameters
Record<string, { type?: string; description?: string; required?: boolean }>

Parameter definitions. Keys are parameter names, values are objects with "type", "description", and optional "required" keys.

method
stringDefaults to GET

HTTP method.

headers
Record<string, string>

HTTP headers.

body
Record<string, unknown>

Request body for POST/PUT.

errorKeys
string[]

Keys indicating an error response.

Returns

DataMap — A fully configured DataMap ready for further chaining or conversion.


import { createExpressionTool, FunctionResult } from '@signalwire/sdk';
const controlTool = createExpressionTool({
name: 'playback_control',
patterns: {
'${args.command}': ['play.*', new FunctionResult('Playing.')],
},
parameters: {
command: { type: 'string', description: 'Playback command', required: true },
},
});

Create a DataMap for pattern-matching responses without API calls.

Parameters

name
stringRequired

Function name.

patterns
Record<string, [string, FunctionResult]>Required

Object mapping test values to [pattern, FunctionResult] tuples.

parameters
Record<string, { type?: string; description?: string; required?: boolean }>

Parameter definitions (same format as createSimpleApiTool).

Returns

DataMap — A fully configured DataMap.

Example

import {
AgentBase,
FunctionResult,
createSimpleApiTool,
createExpressionTool,
} from '@signalwire/sdk';
// Simple API tool -- one line instead of a full DataMap chain
const weather = createSimpleApiTool({
name: 'get_weather',
url: 'https://api.weatherapi.com/v1/current.json?key=KEY&q=${enc:args.city}',
responseTemplate: 'Weather in ${args.city}: ${response.current.condition.text}',
parameters: {
city: { type: 'string', description: 'City name', required: true },
},
errorKeys: ['error'],
});
// Expression tool -- pattern matching without API calls
const greeting = createExpressionTool({
name: 'greet',
patterns: {
'${args.language}': ['spanish|espanol', new FunctionResult('Hola!')],
},
parameters: {
language: { type: 'string', description: 'Language to greet in' },
},
});
const agent = new AgentBase({ name: 'helper-demo' });
agent.setPromptText('You are a helpful assistant.');
agent.registerSwaigFunction(weather.toSwaigFunction());
agent.registerSwaigFunction(greeting.toSwaigFunction());
agent.run();