Helper Functions & Utilities
Helper Functions & Utilities
The SignalWire Agents SDK exports helper functions and utilities at the top level for common
tasks like creating standalone contexts, building server-side API tools,
managing environment variable expansion, security, and type inference.
All are imported directly from @signalwire/sdk.
createSimpleContext
Create a standalone Context object
without needing a full ContextBuilder.
Useful for quick single-context agents.
Parameters
name
Context name. For single-context agents this is typically "default".
Returns
Context — A new Context
object ready for adding steps.
Example
createSimpleApiTool
Create a server-side API tool with minimal configuration. Returns a configured
DataMap that executes an HTTP
request on the SignalWire server without requiring a webhook endpoint.
Parameters
opts.name
The name for the SWAIG tool.
opts.url
API endpoint URL. Supports ${args.paramName} substitution for injecting parameter
values (e.g., "https://api.example.com/search?q=${args.query}").
opts.responseTemplate
Template string for formatting the API response. Uses ${response.field}
syntax to reference fields from the API response JSON.
opts.parameters
Parameter definitions. Each key is a parameter name, and each value is an object
with type (default "string"), description, and optionally required.
opts.method
HTTP method for the API call.
opts.headers
HTTP headers to include in the request.
opts.body
Request body for POST/PUT requests.
opts.errorKeys
JSON keys whose presence in the response indicates an error.
Returns
DataMap — A configured DataMap
ready to be registered with an agent.
Example
For more complex API integrations with multiple webhooks, fallback outputs, or
array processing, use the DataMap
builder directly.
createExpressionTool
Create a pattern-matching tool that evaluates expressions locally on the SignalWire server without making any HTTP requests. Useful for command routing and conditional responses.
Parameters
opts.name
The name for the SWAIG tool.
opts.patterns
A record mapping test values to [pattern, FunctionResult] tuples. The key is a template
string (e.g., "${args.command}"), and the pattern is a regex string matched against it.
opts.parameters
Parameter definitions, same format as createSimpleApiTool.
Returns
DataMap — A configured DataMap
with expression-based routing.
Example
Since object keys must be unique, you cannot map multiple patterns against the same
test value using createExpressionTool. For that case, use the DataMap
builder and call .expression() multiple times.
setAllowedEnvPrefixes
Set the global allowed environment variable prefixes for ${ENV.*} expansion
in DataMap URLs, bodies, and outputs. Only environment variables whose names
start with one of these prefixes will be expanded.
The default prefixes are ['SIGNALWIRE_', 'SWML_', 'SW_'].
Parameters
prefixes
Array of prefix strings to allow. Pass an empty array to allow all environment variables (escape hatch — use with caution).
Example
getAllowedEnvPrefixes
Get a copy of the current global allowed environment variable prefixes.
Returns
string[] — A copy of the current prefix list.
Example
safeAssign
Copy properties from source to target, filtering out prototype-pollution
keys (__proto__, constructor, prototype). Drop-in replacement for
Object.assign() where source is untrusted.
Parameters
target
The object to assign into.
source
The object to copy properties from.
Returns
The target object.
Example
filterSensitiveHeaders
Return a copy of headers with sensitive entries removed (authorization,
cookie, x-api-key, proxy-authorization, set-cookie).
Parameters
headers
Original header record.
Returns
Record<string, string> — A new record with sensitive headers removed.
Example
redactUrl
Redact credentials embedded in a URL. Replaces the password portion with ****.
Parameters
url
The URL string to redact.
Returns
string — The URL with the password replaced by ****.
Example
MAX_SKILL_INPUT_LENGTH
Maximum allowed input length for skill handler arguments (characters). Value: 1000.
Example
inferSchema
Infer a JSON Schema from a function’s source code by parsing parameter names
and default values. Used internally by defineTypedTool().
Parameters
fn
The function to analyze.
Returns
InferredSchema | null — The inferred schema with parameters,
required, paramNames, and hasRawData fields, or null if inference fails.
Example
parseFunctionParams
Parse function parameter names and default values from source code text.
Parameters
source
The function source code string (via fn.toString()).
Returns
ParsedParam[] — Array of { name: string; defaultValue?: string } objects.
createTypedHandlerWrapper
Create a wrapper function that unpacks a Record<string, unknown> args dict
into named positional parameters for a typed handler.
Parameters
fn
The original typed handler function.
paramNames
Ordered parameter names to extract from args.
hasRawData
Whether the handler accepts a rawData parameter.
Returns
SwaigHandler — A wrapped handler with the standard (args, rawData) signature.
PomSection
A single section in a Prompt Object Model, with a title, body, bullets, and
nested subsections. Returned by PomBuilder.getSection() and PomBuilder.findSection().
Properties
title
Section heading text, or null if untitled.
body
Section body paragraph text.
bullets
List of bullet point strings.
subsections
Nested child sections.
numbered
Whether this section is numbered when rendered.
numberedBullets
Whether bullet points are rendered as a numbered list.
registerBuiltinSkills
Register all 19 built-in skills with the global SkillRegistry. Call this once
at startup if you want to use SkillRegistry.create('datetime') style registration.
Parameters
None.
Returns
void