Helper Functions

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

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.


1import { createExpressionTool, FunctionResult } from '@signalwire/sdk';
2
3const controlTool = createExpressionTool({
4 name: 'playback_control',
5 patterns: {
6 '${args.command}': ['play.*', new FunctionResult('Playing.')],
7 },
8 parameters: {
9 command: { type: 'string', description: 'Playback command', required: true },
10 },
11});

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

1import {
2 AgentBase,
3 FunctionResult,
4 createSimpleApiTool,
5 createExpressionTool,
6} from '@signalwire/sdk';
7
8// Simple API tool -- one line instead of a full DataMap chain
9const weather = createSimpleApiTool({
10 name: 'get_weather',
11 url: 'https://api.weatherapi.com/v1/current.json?key=KEY&q=${enc:args.city}',
12 responseTemplate: 'Weather in ${args.city}: ${response.current.condition.text}',
13 parameters: {
14 city: { type: 'string', description: 'City name', required: true },
15 },
16 errorKeys: ['error'],
17});
18
19// Expression tool -- pattern matching without API calls
20const greeting = createExpressionTool({
21 name: 'greet',
22 patterns: {
23 '${args.language}': ['spanish|espanol', new FunctionResult('Hola!')],
24 },
25 parameters: {
26 language: { type: 'string', description: 'Language to greet in' },
27 },
28});
29
30const agent = new AgentBase({ name: 'helper-demo' });
31agent.setPromptText('You are a helpful assistant.');
32agent.registerSwaigFunction(weather.toSwaigFunction());
33agent.registerSwaigFunction(greeting.toSwaigFunction());
34
35agent.run();