Skip to content

Commit b38d231

Browse files
Trim baseURL whitespace before endpoint normalization
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 7d75401 commit b38d231

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

docs/tasks/streams.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ Subsequent reconnect calls will retry stale inactive-state cleanup until it succ
654654
If `onError` is omitted, reconnect still returns `null` and continues without callback reporting.
655655

656656
`baseURL` supports optional path prefixes and trailing slashes; both trigger and stream URLs
657-
are normalized consistently.
657+
are normalized consistently, and surrounding whitespace is trimmed before normalization.
658658

659659
For richer TypeScript ergonomics in app code, `@trigger.dev/ai` also exports:
660660

packages/ai/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
- Added reconnect cleanup error reporting for stale inactive state while still returning `null`.
2121
- Added retry semantics for stale inactive reconnect cleanup on subsequent reconnect attempts.
2222
- Added consistent baseURL normalization for trigger and stream endpoints (including path prefixes and trailing slashes).
23+
- Added surrounding-whitespace trimming for `baseURL` before endpoint normalization.

packages/ai/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ both cleanup steps (`set` inactive state and `delete`) even if one of them fails
159159

160160
- `baseURL` supports optional path prefixes (for example reverse-proxy mounts).
161161
- Trailing slashes are normalized automatically before trigger/stream requests.
162+
- Surrounding whitespace is trimmed before normalization.
162163

163164
## `ai.tool(...)` example
164165

packages/ai/src/chatTransport.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,71 @@ describe("TriggerChatTransport", function () {
479479
);
480480
});
481481

482+
it("trims surrounding whitespace from baseURL values", async function () {
483+
let observedTriggerPath: string | undefined;
484+
let observedStreamPath: string | undefined;
485+
486+
const server = await startServer(function (req, res) {
487+
if (req.method === "POST") {
488+
observedTriggerPath = req.url ?? "";
489+
}
490+
491+
if (req.method === "GET") {
492+
observedStreamPath = req.url ?? "";
493+
}
494+
495+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
496+
res.writeHead(200, {
497+
"content-type": "application/json",
498+
"x-trigger-jwt": "pk_run_trimmed_baseurl",
499+
});
500+
res.end(JSON.stringify({ id: "run_trimmed_baseurl" }));
501+
return;
502+
}
503+
504+
if (req.method === "GET" && req.url === "/realtime/v1/streams/run_trimmed_baseurl/chat-stream") {
505+
res.writeHead(200, {
506+
"content-type": "text/event-stream",
507+
});
508+
writeSSE(
509+
res,
510+
"1-0",
511+
JSON.stringify({ type: "text-start", id: "trimmed_baseurl_1" })
512+
);
513+
writeSSE(
514+
res,
515+
"2-0",
516+
JSON.stringify({ type: "text-end", id: "trimmed_baseurl_1" })
517+
);
518+
res.end();
519+
return;
520+
}
521+
522+
res.writeHead(404);
523+
res.end();
524+
});
525+
526+
const transport = new TriggerChatTransport({
527+
task: "chat-task",
528+
accessToken: "pk_trigger",
529+
baseURL: ` ${server.url}/ `,
530+
stream: "chat-stream",
531+
});
532+
533+
const stream = await transport.sendMessages({
534+
trigger: "submit-message",
535+
chatId: "chat-trimmed-baseurl",
536+
messageId: undefined,
537+
messages: [],
538+
abortSignal: undefined,
539+
});
540+
541+
const chunks = await readChunks(stream);
542+
expect(chunks).toHaveLength(2);
543+
expect(observedTriggerPath).toBe("/api/v1/tasks/chat-task/trigger");
544+
expect(observedStreamPath).toBe("/realtime/v1/streams/run_trimmed_baseurl/chat-stream");
545+
});
546+
482547
it("combines path prefixes with run and stream URL encoding", async function () {
483548
let observedTriggerPath: string | undefined;
484549
let observedStreamPath: string | undefined;

packages/ai/src/chatTransport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ function resolvePayloadMapper<
460460
}
461461

462462
function normalizeBaseUrl(baseURL: string) {
463-
return baseURL.replace(/\/+$/, "");
463+
return baseURL.trim().replace(/\/+$/, "");
464464
}
465465

466466
function createTransportRequest<UI_MESSAGE extends UIMessage>(

0 commit comments

Comments
 (0)