onFunctionCall

View as MarkdownOpen in Claude

Pre-execution hook called before each SWAIG function is executed. The default implementation is a no-op. Override this method in a subclass to intercept tool calls for logging, metrics, or custom dispatch logic.

This is a pre-execution hook only — it does not replace the function dispatch. The tool handler still executes regardless of what this method does. If you need to block execution, throw an error from this hook.

Parameters

name
stringRequired

Name of the SWAIG function about to execute.

args
Record<string, unknown>Required

Parsed arguments for the function, conforming to the function’s parameter schema.

rawData
Record<string, unknown>Required

The full raw SWAIG request payload, including metadata such as call_id, caller_id_number, and global_data.

Returns

void | Promise<void>

Example

1import { AgentBase } from '@signalwire/sdk';
2
3class LoggingAgent extends AgentBase {
4 constructor() {
5 super({ name: 'logging-agent', route: '/logging' });
6 this.setPromptText('You are a helpful assistant.');
7 this.defineTools();
8 }
9
10 protected override defineTools(): void {
11 this.defineTool({
12 name: 'lookup_order',
13 description: 'Look up an order',
14 parameters: {
15 type: 'object',
16 properties: { order_id: { type: 'string' } },
17 required: ['order_id'],
18 },
19 handler: async (args) => {
20 return { response: `Order ${args.order_id} is in transit.` };
21 },
22 });
23 }
24
25 override async onFunctionCall(
26 name: string,
27 args: Record<string, unknown>,
28 rawData: Record<string, unknown>,
29 ): Promise<void> {
30 const callId = (rawData.call_id as string) ?? 'unknown';
31 console.log(`[${callId}] Tool called: ${name}(${JSON.stringify(args)})`);
32 }
33}
34
35const agent = new LoggingAgent();
36await agent.serve();