diff --git a/packages/ai/src/chatTransport.test.ts b/packages/ai/src/chatTransport.test.ts index 70d58fe8b..06a110860 100644 --- a/packages/ai/src/chatTransport.test.ts +++ b/packages/ai/src/chatTransport.test.ts @@ -214,6 +214,65 @@ describe("TriggerChatTransport", function () { expect(observedStreamPath).toBe("/realtime/v1/streams/run%2Fwith%20space/chat-stream"); }); + it("normalizes trailing slash 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_trailing_baseurl", + }); + res.end(JSON.stringify({ id: "run_trailing_baseurl" })); + return; + } + + if (req.method === "GET") { + observedStreamPath = req.url ?? ""; + } + + if (req.method === "GET" && req.url === "/realtime/v1/streams/run_trailing_baseurl/chat-stream") { + res.writeHead(200, { + "content-type": "text/event-stream", + }); + writeSSE( + res, + "1-0", + JSON.stringify({ type: "text-start", id: "trailing_1" }) + ); + writeSSE( + res, + "2-0", + JSON.stringify({ type: "text-end", id: "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-trailing-baseurl", + messageId: undefined, + messages: [], + abortSignal: undefined, + }); + + const chunks = await readChunks(stream); + expect(chunks).toHaveLength(2); + expect(observedStreamPath).toBe("/realtime/v1/streams/run_trailing_baseurl/chat-stream"); + }); + it("uses defined stream object id when provided", async function () { let observedStreamPath: string | undefined;