Repository navigation
Read caller details from call in TypeScript SWML request hook pages - #988
Devon-White wants to merge 1 commit into
Conversation
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.
|
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 PR descriptionThe "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 Technical
Style
Other
|
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 undercall, and the caller's address iscall.from. There is no top-levelcaller_id_number, so both samples printed'unknown'on every call.onSwmlRequest:rawDatais now typedSwmlRequestData(the SDK's exported type), with a nestedrawData.callentry listing the call keys. The sample readsrawData.call?.from.callbackPathononSwmlRequest: the old text said it came from acallback_pathbody field. The SDK derives it from the request URL by matching paths registered withregisterRoutingCallback(). The description now says that.onRequest: the SDK's request handler callsonSwmlRequest()directly and never callsonRequest(), so the old page ("called on every inbound request") and its sample (anonRequestoverride) described a hook that never runs for served requests. The page now saysonRequestpasses a body toonSwmlRequest(), and the sample overridesonSwmlRequest()and callsonRequest()with a sample body to preview the modifications. Thedescriptionfrontmatter and the AgentBase index card match.What it was verified against
{ call, vars, envs, params? }.callis the flattened call state:call_id,node_id,segment_id,tag,call_state,direction,type(phone/sip/webrtc),from,to, andproject_id/space_id. Phone calls addfrom_number/to_number. Phone and SIP calls addheaderswhen present, and SIP calls addsip_data.fromis 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, whichAgentBasedoesn't serve.signalwire-typescriptat41317bc, published as@signalwire/sdk3.15.3):src/PlatformContracts.generated.ts:SwmlRequestData(call?,vars?,envs?,params?) andSwmlRequestCall(call_id,from,to,from_number,to_number,headers, …), exported from the package root.src/AgentBase.ts:onRequest(requestData?: SwmlRequestData | null, callbackPath?)returnsthis.onSwmlRequest(requestData ?? {}, …).handleRequestcallsthis.onSwmlRequest(parsedBody, callbackPath, context)directly. No code in the package callsonRequest.src/SWMLService.ts_callbackPathForUrl:callbackPathis the registered routing-callback path that the request URL matches.Sweep of the rest of
fern/products/server-sdksNo other page reads a top-level
caller_id_numberfrom the SWML request body. The Python guides and reference already readrequest_data.get("call", {}), and the PythonSWMLService.on_requestpage usesrequest_data["call"].get("from"). PythonAgentBasehas noon_requestpage, and itson_swml_requestsample reads only a header. The remainingcaller_id_numberhits are not the SWML request body, so this PR leaves them alone:raw_datareads inguides/build-ai-agents/state-management.mdx,guides/build-ai-agents/call-transfer.mdx, and theon-function-call/swaig-function/executepages (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 theadd-mcp-serverpages (Python and TypeScript). That is a variable expanded inside the AI session, not a request-body key.Notes for reviewers
onRequestchange 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 callonRequestfrom its request handler, that's an SDK change, and the page should then go back to describing a request-time hook.onSwmlRequestsample still callsthis.updateGlobalData()inside the hook, as before. That mutates agent-wide state for every concurrent call. Returning{ global_data: … }, as theonRequestsample does, would be safer, but changing it was out of scope here.envsreaches customers with any content. I left it out of the page.Verification
tsc --strictagainst@signalwire/sdk3.15.3: clean.onRequestsample withtsx: it prints{ global_data: { caller_number: '+15551234567' } }, which matches the inline comment.yarn fern-md-check: all 2895 MDX files valid.slug:(on-swml-request,swml-service/set-on-request-callback,swml-service/register-routing-callback). No anchors are involved.Preview