diff --git a/src/adapters/openai-responses-url.ts b/src/adapters/openai-responses-url.ts index 15f28b03c5..aecc296c70 100644 --- a/src/adapters/openai-responses-url.ts +++ b/src/adapters/openai-responses-url.ts @@ -7,8 +7,10 @@ const TRAILING_V1 = /\/v1\/?$/; * Custom `responsesPath` stays on the adapter; this helper is only the legacy /v1/responses branch. */ export function openaiResponsesUrl(baseUrl: string): string { - const trimmed = baseUrl.trim().replace(TRAILING_SLASHES, ""); - const withoutEndpoint = trimmed.replace(TRAILING_RESPONSES, ""); + const url = new URL(baseUrl.trim()); + const trimmedPath = url.pathname.replace(TRAILING_SLASHES, ""); + const withoutEndpoint = trimmedPath.replace(TRAILING_RESPONSES, ""); const withoutV1 = withoutEndpoint.replace(TRAILING_V1, ""); - return `${withoutV1}/v1/responses`; + url.pathname = `${withoutV1}/v1/responses`; + return url.toString(); } diff --git a/tests/openai-responses-passthrough.test.ts b/tests/openai-responses-passthrough.test.ts index a2b7d234c5..8521b05fe0 100644 --- a/tests/openai-responses-passthrough.test.ts +++ b/tests/openai-responses-passthrough.test.ts @@ -153,6 +153,7 @@ describe("OpenAI Responses key-auth URL construction", () => { ["https://api.openai.example/v1/", "https://api.openai.example/v1/responses"], ["https://api.openai.example/v1/responses", "https://api.openai.example/v1/responses"], ["https://api.openai.example/v1/responses/", "https://api.openai.example/v1/responses"], + ["https://responses", "https://responses/v1/responses"], ] as const) { expect(buildKeyAuthUrl(baseUrl)).toBe(expectedUrl); }