From a736fa9768d44c9be289a5ddbd6bfc8029ff9c12 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 15 Feb 2026 03:46:00 +0000 Subject: [PATCH] Normalize baseURL once for trigger and stream endpoints Co-authored-by: Eric Allam --- packages/ai/src/chatTransport.test.ts | 62 +++++++++++++++++++++++++++ packages/ai/src/chatTransport.ts | 9 ++-- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/packages/ai/src/chatTransport.test.ts b/packages/ai/src/chatTransport.test.ts index 2e688f1c0..e7ad6f6e3 100644 --- a/packages/ai/src/chatTransport.test.ts +++ b/packages/ai/src/chatTransport.test.ts @@ -273,6 +273,68 @@ describe("TriggerChatTransport", function () { expect(observedStreamPath).toBe("/realtime/v1/streams/run_trailing_baseurl/chat-stream"); }); + it("normalizes repeated trailing slashes in baseURL for stream URLs", async function () { + let observedStreamPath: string | undefined; + + const server = await startServer(function (req, res) { + if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") { + res.writeHead(200, { + "content-type": "application/json", + "x-trigger-jwt": "pk_run_multi_trailing_baseurl", + }); + res.end(JSON.stringify({ id: "run_multi_trailing_baseurl" })); + return; + } + + if (req.method === "GET") { + observedStreamPath = req.url ?? ""; + } + + if ( + req.method === "GET" && + req.url === "/realtime/v1/streams/run_multi_trailing_baseurl/chat-stream" + ) { + res.writeHead(200, { + "content-type": "text/event-stream", + }); + writeSSE( + res, + "1-0", + JSON.stringify({ type: "text-start", id: "multi_trailing_1" }) + ); + writeSSE( + res, + "2-0", + JSON.stringify({ type: "text-end", id: "multi_trailing_1" }) + ); + res.end(); + return; + } + + res.writeHead(404); + res.end(); + }); + + const transport = new TriggerChatTransport({ + task: "chat-task", + accessToken: "pk_trigger", + baseURL: `${server.url}///`, + stream: "chat-stream", + }); + + const stream = await transport.sendMessages({ + trigger: "submit-message", + chatId: "chat-multi-trailing-baseurl", + messageId: undefined, + messages: [], + abortSignal: undefined, + }); + + const chunks = await readChunks(stream); + expect(chunks).toHaveLength(2); + expect(observedStreamPath).toBe("/realtime/v1/streams/run_multi_trailing_baseurl/chat-stream"); + }); + it("supports baseURL path prefixes for trigger and stream routes", async function () { let observedTriggerPath: string | undefined; let observedStreamPath: string | undefined; diff --git a/packages/ai/src/chatTransport.ts b/packages/ai/src/chatTransport.ts index c3eb6dd59..8f058a938 100644 --- a/packages/ai/src/chatTransport.ts +++ b/packages/ai/src/chatTransport.ts @@ -145,7 +145,7 @@ export class TriggerChatTransport< this.payloadMapper = resolvePayloadMapper(options.payloadMapper); this.triggerOptions = options.triggerOptions; this.runStore = options.runStore ?? new InMemoryTriggerChatRunStore(); - this.baseURL = options.baseURL ?? "https://api.trigger.dev"; + this.baseURL = normalizeBaseUrl(options.baseURL ?? "https://api.trigger.dev"); this.previewBranch = options.previewBranch; this.requestOptions = options.requestOptions; this.triggerClient = new ApiClient( @@ -374,11 +374,10 @@ export class TriggerChatTransport< } private createStreamUrl(runId: string, streamKey: string): string { - const normalizedBaseUrl = this.baseURL.replace(/\/$/, ""); const encodedRunId = encodeURIComponent(runId); const encodedStreamKey = encodeURIComponent(streamKey); - return `${normalizedBaseUrl}/realtime/v1/streams/${encodedRunId}/${encodedStreamKey}`; + return `${this.baseURL}/realtime/v1/streams/${encodedRunId}/${encodedStreamKey}`; } private async markRunInactiveAndDelete(runState: TriggerChatRunState) { @@ -460,6 +459,10 @@ function resolvePayloadMapper< return createDefaultPayload as TriggerChatPayloadMapper; } +function normalizeBaseUrl(baseURL: string) { + return baseURL.replace(/\/+$/, ""); +} + function createTransportRequest( options: TriggerChatSendMessagesOptions ): TriggerChatTransportRequest {