Skip to content

Read caller details from call in TypeScript SWML request hook pages - #988

Open
Devon-White wants to merge 1 commit into
mainfrom
devon/server-sdks/swml-request-body-keys
Open

Devon-White wants to merge 1 commit into
mainfrom
devon/server-sdks/swml-request-body-keys

Conversation

@Devon-White

Copy link
Copy Markdown
Collaborator

Closes #982

Summary

The SWML request body that SignalWire POSTs when it fetches SWML from a URL has top-level call, vars, envs, and (when set) params. Call details sit under call, and the caller's address is call.from. There is no top-level caller_id_number, so both samples printed 'unknown' on every call.

  • onSwmlRequest: rawData is now typed SwmlRequestData (the SDK's exported type), with a nested rawData.call entry listing the call keys. The sample reads rawData.call?.from.
  • callbackPath on onSwmlRequest: the old text said it came from a callback_path body field. The SDK derives it from the request URL by matching paths registered with registerRoutingCallback(). The description now says that.
  • onRequest: the SDK's request handler calls onSwmlRequest() directly and never calls onRequest(), so the old page ("called on every inbound request") and its sample (an onRequest override) described a hook that never runs for served requests. The page now says onRequest passes a body to onSwmlRequest(), and the sample overrides onSwmlRequest() and calls onRequest() with a sample body to preview the modifications. The description frontmatter and the AgentBase index card match.

What it was verified against

  • Platform request body: the SWML fetch builds the POST body as { call, vars, envs, params? }. call is the flattened call state: call_id, node_id, segment_id, tag, call_state, direction, type (phone / sip / webrtc), from, to, and project_id / space_id. Phone calls add from_number / to_number. Phone and SIP calls add headers when present, and SIP calls add sip_data. from is set for all three device types. For voice, including Fabric SWML webhook resources, the platform's call engine fetches the URL directly. Rails builds its own body only for messaging SWML webhooks, which AgentBase doesn't serve.
  • TypeScript SDK (signalwire-typescript at 41317bc, published as @signalwire/sdk 3.15.3):
    • src/PlatformContracts.generated.ts: SwmlRequestData (call?, vars?, envs?, params?) and SwmlRequestCall (call_id, from, to, from_number, to_number, headers, …), exported from the package root.
    • src/AgentBase.ts: onRequest(requestData?: SwmlRequestData | null, callbackPath?) returns this.onSwmlRequest(requestData ?? {}, …). handleRequest calls this.onSwmlRequest(parsedBody, callbackPath, context) directly. No code in the package calls onRequest.
    • src/SWMLService.ts _callbackPathForUrl: callbackPath is the registered routing-callback path that the request URL matches.

Sweep of the rest of fern/products/server-sdks

No other page reads a top-level caller_id_number from the SWML request body. The Python guides and reference already read request_data.get("call", {}), and the Python SWMLService.on_request page uses request_data["call"].get("from"). Python AgentBase has no on_request page, and its on_swml_request sample reads only a header. The remaining caller_id_number hits are not the SWML request body, so this PR leaves them alone:

  • SWAIG raw_data reads in guides/build-ai-agents/state-management.mdx, guides/build-ai-agents/call-transfer.mdx, and the on-function-call / swaig-function/execute pages (Python and TypeScript). PR Fix SWAIG caller_id_num, SWML user_event and Relay version reference (#973, #974) #977 covers these.
  • ${caller_id_number} in the add-mcp-server pages (Python and TypeScript). That is a variable expanded inside the AI session, not a request-body key.

Notes for reviewers

  • The onRequest change goes beyond the key fix. It corrects the page's claim that the hook runs on every request, because in the current SDK it never runs for served requests. If the SDK should call onRequest from its request handler, that's an SDK change, and the page should then go back to describing a request-time hook.
  • The onSwmlRequest sample still calls this.updateGlobalData() inside the hook, as before. That mutates agent-wide state for every concurrent call. Returning { global_data: … }, as the onRequest sample does, would be safer, but changing it was out of scope here.
  • Not verified: whether envs reaches customers with any content. I left it out of the page.

Verification

  • Extracted both TypeScript samples and ran tsc --strict against @signalwire/sdk 3.15.3: clean.
  • Ran the onRequest sample with tsx: it prints { global_data: { caller_number: '+15551234567' } }, which matches the inline comment.
  • yarn fern-md-check: all 2895 MDX files valid.
  • Link targets checked against slug: (on-swml-request, swml-service/set-on-request-callback, swml-service/register-routing-callback). No anchors are involved.

Preview

The SWML request body nests call details under call (call.from,
call.call_id); it has no top-level caller_id_number. Type the hook
parameters as SwmlRequestData, fix the samples, correct callbackPath,
and note that served requests call onSwmlRequest, not onRequest.
@Devon-White Devon-White self-assigned this Nov 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

@Manny-r31

Copy link
Copy Markdown
Collaborator

The core fix is right and I'd merge it after a few small changes. I checked the key names and nesting against the referenced source code and against @signalwire/sdk 3.15.3: caller details sit under call, call.from is set for phone, SIP, and WebRTC calls, and the body has no top-level caller_id_number. Both samples pass tsc --strict against 3.15.3, and the onRequest sample prints the output its comment claims. The onRequest rewrite also matches the SDK: handleRequest calls onSwmlRequest() directly and nothing in the package calls onRequest. The Python SDK behaves the same way, so the two languages now agree.

PR description

The "Platform request body" bullet says which internal service builds the body and which component fetches the URL. This repo is public, so please edit the description to keep the conclusion, meaning the top-level keys and the call field list, and drop the sentences about how and where the body is assembled. The links to the public signalwire-typescript repo are fine as they are.

Technical

  1. on-swml-request.mdx line 21, link the documented payload. Every field the PR lists already has a reference entry at /docs/swml/reference/calling#webhook-payload (slug reference/calling, anchor [#webhook-payload] in fern/products/swml/pages/reference/methods/calling/overview.mdx, line 53). Link the first mention, so readers get the full field list without this page copying it. Suggested text: "The parsed request body, in the shape of the Calling SWML webhook payload. Call details sit under call, and SWML variables set earlier in the call sit under vars." That also drops "not at the top level", which tells the reader where the data isn't.
  2. on-swml-request.mdx lines 30-31, headers. "headers holds any custom headers the call carries" reads as if every call has it. Per the payload reference, phone and SIP calls include headers, as an empty array when there are none, and WebRTC calls leave it out. Suggest: "Phone calls also carry from_number and to_number. Phone and SIP calls carry headers, an array of name/value pairs."
  3. on-request.mdx line 46, highlighted line. {5} highlights the rawData.call?.from read inside the onSwmlRequest override. On the onRequest page the line to draw the eye to is the call itself, line 13 (const modifications = await agent.onRequest({). Change to {13}.
  4. on-swml-request.mdx line 78, this.updateGlobalData() (suggestion). You flagged this in your notes. Since you're already editing that block, I'd fix it here: the hook runs on the shared agent for every request, so concurrent calls overwrite each other's caller_number. Return { global_data: { caller_number: callerId } } instead, change the return type to Promise<Record<string, unknown>>, and the two pages then teach the same pattern. If you keep it out of scope, a follow-up issue is fine.

Style

  1. on-request.mdx line 12, "a dictionary of SWML modifications". That's Python wording. The TypeScript type is Record<string, unknown>, so "an object of SWML modifications" fits the language-mirroring rule in mdx-reference-pages.
  2. on-request.mdx line 23, "This hook returns...". The new intro and first Note present onRequest as a method you call, not a hook that runs on requests. The unchanged second Note still opens "This hook". Change it to "onRequest returns..." so the page stays consistent with your rewrite. You could also merge the two back-to-back Notes into one.

Other

  • Links you added check out against the target slug: values: on-swml-request, swml-service/set-on-request-callback, and swml-service/register-routing-callback. The callbackPath description matches how the SDK resolves it from the request URL.
  • Python sibling pages don't carry the same error. python/agents/agent-base/on-swml-request.mdx reads only a header, and python/agents/swml-service/on-request.mdx already reads request_data["call"]. For parity, the request_data description on the Python on_swml_request page could gain the same payload link in a follow-up. That isn't a blocker here.
  • The remaining caller_id_number hits in the server SDK pages are SWAIG function payloads covered by Fix SWAIG caller_id_num, SWML user_event and Relay version reference (#973, #974) #977, or ${caller_id_number} expansion in the add-mcp-server pages. Leaving them out of this PR is right.
  • Pre-existing, not introduced here: the context param on on-swml-request.mdx is typed any, while the SDK declares Hono's Context, and it's undefined when handleRequest() is called directly rather than through the served route. Worth a follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript onRequest / onSwmlRequest samples read a top-level caller_id_number the SWML request body may not carry

2 participants