feat(sdk): functional baseURL and fetch override on chat transports#3655
Conversation
TriggerChatTransport, AgentChat, and chat.createStartSessionAction now accept a string-or-function baseURL so callers can route per endpoint — e.g. .in/append through a trusted edge proxy while keeping .out SSE direct. The same surfaces add a fetch override for header injection, custom retries, or proxy rewrites that go beyond URL routing; SSE GETs are covered via a new fetchClient option on SSEStreamSubscription. streamBaseURL on TriggerChatTransport is kept as a backwards-compat alias and continues to win for the "out" endpoint when set. Plain-string baseURL still applies to every endpoint, matching prior behavior.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📜 Recent review details⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (30)
WalkthroughThis PR extends the chat routing infrastructure to support custom per-endpoint base URLs and fetch overrides. SSEStreamSubscription gains an optional Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
Reference example for the trusted-edge-signals pattern. A minimal
Cloudflare Worker (references/ai-chat/cf-worker) rewrites POST
/api/v1/sessions and POST /realtime/v1/sessions/{id}/in/append to
inject server-trusted bot-management signals into payload.metadata.__cf
before forwarding upstream. The new cfTrustTestAgent declares the
namespace in clientDataSchema and echoes the values back so the
round-trip is visible in the streamed response.
cf-worker README walks through the wire-up and shows the function-form
baseURL snippet for routing .out SSE direct while .in/append flows
through the worker.
…est input in SSE wrapper When chat.createStartSessionAction was given a baseURL or fetch override, the inline POSTs for /api/v1/sessions and /api/v1/auth/jwt/claims forgot to forward apiClientManager.branchName as the x-trigger-branch header. Callers running against a preview branch (via TRIGGER_PREVIEW_BRANCH or VERCEL_GIT_COMMIT_REF) would route to the wrong environment in override mode. Extracted the header builder to a shared helper. The SSE fetch wrappers in TriggerChatTransport.subscribeToSessionStream and AgentChat.subscribeToSessionStream cast to typeof fetch but coerced non-string input via .toString(), which yields "[object Request]" on Request objects and discards the Request's intrinsic method/headers/ signal. Replaced the coercion with explicit branches for string, URL, and Request — Request inputs now extract url + init properly with any caller-provided init taking precedence (matches fetch(Request, init) semantics).
When AgentChat.appendInputChunk fails non-2xx after the inline-POST refactor, it was throwing a plain Error. Callers of sendRaw, sendMessage, sendHandover, and sendHandoverSkip inspecting error.name === "TriggerApiError" or error.status saw "Error" / undefined instead of the values the prior ApiClient/zodfetch path produced. Synthesize the same shape here so the public error contract is preserved. Matches the equivalent fix already applied to TriggerChatTransport.
Summary
TriggerChatTransport,AgentChat, andchat.createStartSessionActionnow accept a string-or-functionbaseURLso callers can route per endpoint — e.g..in/appendthrough a trusted edge proxy while keeping.outSSE direct. The same surfaces add afetchoverride for header injection, custom retries, or proxy rewrites that go beyond URL routing. SSE GETs are covered too via a newfetchClientoption onSSEStreamSubscription.streamBaseURLonTriggerChatTransportis kept as a backwards-compat alias and continues to win for the"out"endpoint when set. Plain-stringbaseURLstill applies to every endpoint, matching prior behavior.Test plan
chat.test.ts— two new cases covering function-formbaseURLand thefetchoverride (40 tests, all green).clientDatatyped viaclientDataSchema.