Repository navigation
feat(whatsapp): B-Twilio-2 slice 1 — WABA transport adapter (dormant, flag-off; SMS send generalized) - #69
Conversation
…er (WhatsApp send via Twilio, flag-off) Generalizes the B-Twilio-1 send primitive to serve both SMS and WhatsApp (WABA) via Twilio — same Messages.json endpoint, only the address prefix differs. transport.ts: sendTwilioMessage (channel-parametrized: scoped-API-key auth, form-encode, discriminated result, secret hygiene — all shared), twilioAddress (whatsapp: prefix, idempotent), bareAddress (inbound strip for opt-out keying/routing so a STOP from whatsapp:+1... opts out the same peer). send.ts now delegates to it (SMS transport) — public API + landed tests unchanged (delegation parity, send.test 8/8 green). Vendor-agnostic rail holds: only this extension is Twilio-aware. FLAG-OFF: nothing wired to the live Baileys whatsapp channel — lands dormant + safe. 12 red-first transport tests incl. WABA send prefixing + no-leak. Full twilio suite 90/90; tsgo+lint+fmt clean. Next: slice 2 (dual-path flag + config schema, Baileys default). Generated-With: Claude Code Co-Authored-By: cto <[email protected]> Co-Authored-By: Claude <[email protected]>
📝 WalkthroughWalkthroughThe Twilio REST implementation moves into a shared transport supporting SMS and WhatsApp address handling, typed results, authentication, response parsing, and error normalization. ChangesTwilio transport extraction
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant sendSms
participant sendTwilioMessage
participant Twilio
sendSms->>sendTwilioMessage: Forward SMS message and credentials
sendTwilioMessage->>Twilio: POST form-encoded message with Basic auth
Twilio-->>sendTwilioMessage: Return response JSON or error
sendTwilioMessage-->>sendSms: Return SendMessageResult
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The formal models extracted constants ( This check is informational (not blocking merges yet). If this change is intentional, follow up by updating the formal models repo or regenerating the extracted artifacts there. |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@extensions/twilio/src/transport.ts`:
- Around line 79-86: Update the Twilio request around doFetch to apply a
configurable timeout via an abort signal, ensuring stalled connections are
cancelled. When the timeout expires, return the existing { ok: false, status:
null } result, while preserving normal response handling and existing error
behavior for other failures.
- Around line 87-100: Update the failure handling in the transport request flow
to redact every occurrence of auth.apiKeySecret from both caught exception
messages and HTTP response-derived messages before returning the { ok: false }
result. Apply the same fail-closed sanitization after JSON message extraction
and raw-body fallback, preserving status values and successful responses
unchanged.
- Around line 91-103: Update the response handling around both response.text()
calls in the transport request flow to catch rejected response-stream reads and
return the existing { ok: false } result shape with an appropriate error
message. Preserve the current JSON message extraction for successfully read
error bodies and the successful response path when its body is readable.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 4eacc4fb-4744-470d-afc1-01f9424b696e
📒 Files selected for processing (3)
extensions/twilio/src/send.tsextensions/twilio/src/transport.test.tsextensions/twilio/src/transport.ts
| response = await doFetch(url, { | ||
| method: "POST", | ||
| headers: { | ||
| Authorization: `Basic ${basic}`, | ||
| "Content-Type": "application/x-www-form-urlencoded", | ||
| }, | ||
| body: form, | ||
| }); |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Bound the Twilio request with a timeout.
No abort signal or timeout is applied, so a stalled Twilio connection can leave this promise pending indefinitely. Add a configurable timeout and return the existing { ok: false, status: null } result when it expires.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@extensions/twilio/src/transport.ts` around lines 79 - 86, Update the Twilio
request around doFetch to apply a configurable timeout via an abort signal,
ensuring stalled connections are cancelled. When the timeout expires, return the
existing { ok: false, status: null } result, while preserving normal response
handling and existing error behavior for other failures.
| } catch (err) { | ||
| return { ok: false, status: null, error: err instanceof Error ? err.message : String(err) }; | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| const text = await response.text(); | ||
| let message = text; | ||
| try { | ||
| const parsed = JSON.parse(text) as { message?: string }; | ||
| if (parsed.message) message = parsed.message; | ||
| } catch { | ||
| // non-JSON body — surface the raw text | ||
| } | ||
| return { ok: false, status: response.status, error: message }; |
There was a problem hiding this comment.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Redact the API-key secret from every surfaced error.
Thrown messages and raw HTTP bodies are returned verbatim. Either can contain auth.apiKeySecret, violating the stated fail-closed secret-hygiene contract. Redact the known secret or return normalized messages before constructing the failure result.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@extensions/twilio/src/transport.ts` around lines 87 - 100, Update the failure
handling in the transport request flow to redact every occurrence of
auth.apiKeySecret from both caught exception messages and HTTP response-derived
messages before returning the { ok: false } result. Apply the same fail-closed
sanitization after JSON message extraction and raw-body fallback, preserving
status values and successful responses unchanged.
| if (!response.ok) { | ||
| const text = await response.text(); | ||
| let message = text; | ||
| try { | ||
| const parsed = JSON.parse(text) as { message?: string }; | ||
| if (parsed.message) message = parsed.message; | ||
| } catch { | ||
| // non-JSON body — surface the raw text | ||
| } | ||
| return { ok: false, status: response.status, error: message }; | ||
| } | ||
|
|
||
| const text = await response.text(); |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Handle response-body read failures without throwing.
Both response.text() calls are outside error handling. A rejected response stream therefore escapes the discriminated-result contract instead of returning { ok: false }.
Proposed fix
+ let text: string;
+ try {
+ text = await response.text();
+ } catch {
+ return { ok: false, status: response.status, error: "unable to read Twilio response body" };
+ }
+
if (!response.ok) {
- const text = await response.text();
let message = text;
// ...
}
- const text = await response.text();📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (!response.ok) { | |
| const text = await response.text(); | |
| let message = text; | |
| try { | |
| const parsed = JSON.parse(text) as { message?: string }; | |
| if (parsed.message) message = parsed.message; | |
| } catch { | |
| // non-JSON body — surface the raw text | |
| } | |
| return { ok: false, status: response.status, error: message }; | |
| } | |
| const text = await response.text(); | |
| let text: string; | |
| try { | |
| text = await response.text(); | |
| } catch { | |
| return { ok: false, status: response.status, error: "unable to read Twilio response body" }; | |
| } | |
| if (!response.ok) { | |
| let message = text; | |
| try { | |
| const parsed = JSON.parse(text) as { message?: string }; | |
| if (parsed.message) message = parsed.message; | |
| } catch { | |
| // non-JSON body — surface the raw text | |
| } | |
| return { ok: false, status: response.status, error: message }; | |
| } | |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@extensions/twilio/src/transport.ts` around lines 91 - 103, Update the
response handling around both response.text() calls in the transport request
flow to catch rejected response-stream reads and return the existing { ok: false
} result shape with an appropriate error message. Preserve the current JSON
message extraction for successfully read error bodies and the successful
response path when its body is readable.
Slice 1 of the Baileys→WABA migration (incremental landing per CTO directive): generalizes the landed Twilio send primitive to serve sms + whatsapp (same Messages.json; whatsapp: address prefix, idempotent + inbound bare-strip so WhatsApp STOP keys the same opt-out store). Lands DORMANT — nothing wired to the live Baileys channel; no live-channel behavior change. Slice 2 = dual-path flag + schema (with dormant prod-reject); slice 3 = cutover tooling. Cutover gated on Twilio test subaccount + E2E + explicit CTO go.
🤖 Generated with Claude Code
Summary by CodeRabbit