registerSwaigFunction

View as MarkdownOpen in Claude

Register a pre-built SwaigFunction instance or a raw function descriptor (e.g., from a DataMap). Unlike defineTool(), this method accepts pre-built objects rather than taking a handler callback.

Parameters

fn
SwaigFunction | Record<string, unknown>Required

A SwaigFunction instance or a plain object with a "function" key. Typically generated by DataMap.toSwaigFunction().

Returns

AgentBase — Returns this for method chaining.

Example

import { AgentBase, DataMap, FunctionResult } from '@signalwire/sdk';
const agent = new AgentBase({ name: 'plant-agent', route: '/plants' });
agent.setPromptText('You are a helpful plant care assistant.');
// Create a DataMap that calls a plant API server-side
const plantTool = new DataMap('get_plant_info')
.description('Get care information for a plant')
.parameter('plantId', 'string', 'Plant ID', { required: true })
.webhook('GET', 'https://api.plants.example.com/plants/${args.plantId}')
.output(new FunctionResult(
'The plant ${args.plantId} needs ${response.water_schedule} watering.',
));
// Register the DataMap as a SWAIG function
agent.registerSwaigFunction(plantTool.toSwaigFunction());
await agent.serve();