onSwmlRequest

View as MarkdownOpen in Claude

Lifecycle hook called on every incoming SWML request before the SWML document is rendered. The default implementation is a no-op. Override this method in a subclass to inspect or modify agent state before each call is processed.

Common uses include loading caller-specific data from a database, adjusting prompts based on request metadata, or logging request details.

Parameters

rawData
Record<string, unknown>Required

The parsed request body. Contains call metadata such as call_id, caller_id_number, caller_id_name, and any custom SIP headers.

Returns

void | Promise<void>

For most per-request customization scenarios, prefer setDynamicConfigCallback() which provides a higher-level interface with access to query params, body, headers, and an ephemeral agent copy.

Example

1import { AgentBase } from '@signalwire/sdk';
2
3class PersonalizedAgent extends AgentBase {
4 constructor() {
5 super({ name: 'personalized', route: '/personalized' });
6 this.setPromptText('You are a helpful assistant.');
7 }
8
9 override async onSwmlRequest(
10 rawData: Record<string, unknown>,
11 ): Promise<void> {
12 const callerId = (rawData.caller_id_number as string) ?? 'unknown';
13 console.log(`Incoming call from ${callerId}`);
14 this.updateGlobalData({ caller_number: callerId });
15 }
16}
17
18const agent = new PersonalizedAgent();
19await agent.serve();