Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(api): join paths before query strings on pasted base URLs (#1310)

Henry Sua925d771937c
diff --git a/src/components/OpenAICompatiblePanel.tsx b/src/components/OpenAICompatiblePanel.tsx
index 6fdb74567..6a3847285 100644
--- a/src/components/OpenAICompatiblePanel.tsx
+++ b/src/components/OpenAICompatiblePanel.tsx
@@ -279,7 +279,7 @@ export default function OpenAICompatiblePanel({
}, [applyBase, isDraftDirty, trimmedDraft, loadRemoteModels]);
const displayedModels = isDraftDirty ? [] : modelOptions;
- const queryUrl = hasBase ? `${normalizedBase}/models` : `${baseUrlPlaceholder}/models`;
+ const queryUrl = buildApiUrl(hasBase ? normalizedBase : baseUrlPlaceholder, "/models");
return (
<>
diff --git a/src/config/constants.ts b/src/config/constants.ts
index fd4b093fc..029434c45 100644
--- a/src/config/constants.ts
+++ b/src/config/constants.ts
@@ -1,9 +1,36 @@
+// Split query/hash so path-suffix stripping and later joins operate on the
+// path only. Provider docs and Azure/gateway pastes often include ?api-version=
+// (or similar) on a full /chat/completions URL (#1309).
+function splitUrlDecorators(value: string): { path: string; query: string; hash: string } {
+ let path = value;
+ let hash = "";
+ const hashIndex = path.indexOf("#");
+ if (hashIndex >= 0) {
+ hash = path.slice(hashIndex);
+ path = path.slice(0, hashIndex);
+ }
+ let query = "";
+ const queryIndex = path.indexOf("?");
+ if (queryIndex >= 0) {
+ query = path.slice(queryIndex);
+ path = path.slice(0, queryIndex);
+ }
+ return { path, query, hash };
+}
+
+function joinUrlDecorators(path: string, query: string, hash: string): string {
+ return `${path}${query}${hash}`;
+}
+
// API Configuration helpers
export const normalizeBaseUrl = (value?: string | null): string => {
if (!value) return "";
- let normalized = value.trim();
- if (!normalized) return "";
+ const trimmed = value.trim();
+ if (!trimmed) return "";
+
+ const { path: rawPath, query, hash } = splitUrlDecorators(trimmed);
+ let normalized = rawPath;
// Remove common API endpoint suffixes to get the base URL
const suffixReplacements: Array<[RegExp, string]> = [
@@ -25,7 +52,7 @@ export const normalizeBaseUrl = (value?: string | null): string => {
}
}
- return normalized.replace(/\/+$/, "");
+ return joinUrlDecorators(normalized.replace(/\/+$/, ""), query, hash);
};
export const buildApiUrl = (base: string, path: string): string => {
@@ -34,13 +61,15 @@ export const buildApiUrl = (base: string, path: string): string => {
return normalizedBase;
}
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
- return `${normalizedBase}${normalizedPath}`;
+ const { path: originAndPath, query, hash } = splitUrlDecorators(normalizedBase);
+ return joinUrlDecorators(`${originAndPath}${normalizedPath}`, query, hash);
};
export const ensureV1Suffix = (base: string): string => {
if (!base) return base;
const normalized = normalizeBaseUrl(base) || base;
- return normalized.endsWith("/v1") ? normalized : `${normalized}/v1`;
+ const { path, query, hash } = splitUrlDecorators(normalized);
+ return joinUrlDecorators(path.endsWith("/v1") ? path : `${path}/v1`, query, hash);
};
// Ordered bases to try when listing models from an OpenAI-compatible server.
@@ -50,8 +79,11 @@ export const ensureV1Suffix = (base: string): string => {
export const getModelListBaseCandidates = (base: string): string[] => {
const normalized = normalizeBaseUrl(base);
if (!normalized) return [];
- const nativeApiMatch = normalized.match(/^(.+?)\/api\/v[01]$/i);
- if (nativeApiMatch) return [normalized, `${nativeApiMatch[1]}/v1`];
+ const { path, query, hash } = splitUrlDecorators(normalized);
+ const nativeApiMatch = path.match(/^(.+?)\/api\/v[01]$/i);
+ if (nativeApiMatch) {
+ return [normalized, joinUrlDecorators(`${nativeApiMatch[1]}/v1`, query, hash)];
+ }
const withV1 = ensureV1Suffix(normalized);
return withV1 === normalized ? [normalized] : [normalized, withV1];
};
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index ac48beaeb..36e5d14d9 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -2955,12 +2955,14 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
// shape returns DeploymentNotFound. Build the deployment-style URL.
// The api-version defaults to a transcribe-capable preview; a user can
// override it by appending ?api-version=... to their endpoint URL.
- const azureUrl = buildAzureTranscriptionUrl(normalizedBase, deployment);
+ // Built from the raw base — normalization strips the /audio/transcriptions
+ // suffix that marks a deployment the user pinned.
+ const azureUrl = buildAzureTranscriptionUrl(base, deployment);
if (azureUrl) {
endpoint = azureUrl;
logger.debug(
"STT endpoint: built Azure deployment URL",
- { base: normalizedBase, deployment, endpoint },
+ { base, deployment, endpoint },
"transcription"
);
} else {
diff --git a/test/helpers/apiBaseUrl.test.js b/test/helpers/apiBaseUrl.test.js
index d479b543e..7615660fe 100644
--- a/test/helpers/apiBaseUrl.test.js
+++ b/test/helpers/apiBaseUrl.test.js
@@ -100,3 +100,49 @@ test("buildApiUrl with an empty path returns just the normalized base", async ()
assert.equal(buildApiUrl("https://api.openai.com/v1", ""), "https://api.openai.com/v1");
});
+
+// Regression for #1309: query/hash must stay attached to the origin+path, not
+// absorb later path joins. Provider docs and Azure gateways often include
+// ?api-version=… on the pasted completions URL.
+test("pasted endpoint URLs with query strings still strip path suffixes", async () => {
+ const { normalizeBaseUrl } = await load();
+
+ assert.equal(
+ normalizeBaseUrl("https://api.example.com/v1/chat/completions?api-version=2024-02-01"),
+ "https://api.example.com/v1?api-version=2024-02-01"
+ );
+ assert.equal(
+ normalizeBaseUrl("https://api.example.com/v1/models?api-key=secret#section"),
+ "https://api.example.com/v1?api-key=secret#section"
+ );
+});
+
+test("buildApiUrl inserts the path before any query or hash", async () => {
+ const { buildApiUrl } = await load();
+
+ assert.equal(
+ buildApiUrl("https://api.example.com/v1/chat/completions?api-version=2024-02-01", "/responses"),
+ "https://api.example.com/v1/responses?api-version=2024-02-01"
+ );
+ assert.equal(
+ buildApiUrl("https://gateway.example.com/v1?api-key=secret", "/models"),
+ "https://gateway.example.com/v1/models?api-key=secret"
+ );
+ assert.equal(
+ buildApiUrl("https://gateway.example.com/v1?api-key=secret#frag", "chat/completions"),
+ "https://gateway.example.com/v1/chat/completions?api-key=secret#frag"
+ );
+});
+
+test("ensureV1Suffix checks the path, not characters after the query", async () => {
+ const { ensureV1Suffix } = await load();
+
+ assert.equal(
+ ensureV1Suffix("https://gateway.example.com/v1?api-key=secret"),
+ "https://gateway.example.com/v1?api-key=secret"
+ );
+ assert.equal(
+ ensureV1Suffix("https://gateway.example.com?api-key=secret"),
+ "https://gateway.example.com/v1?api-key=secret"
+ );
+});
diff --git a/test/helpers/azureTranscriptionUrl.test.js b/test/helpers/azureTranscriptionUrl.test.js
index fedc0ac71..a0f73415d 100644
--- a/test/helpers/azureTranscriptionUrl.test.js
+++ b/test/helpers/azureTranscriptionUrl.test.js
@@ -7,10 +7,7 @@ const assert = require("node:assert/strict");
test("isAzureOpenAIEndpoint detects Azure resource hosts", async () => {
const { isAzureOpenAIEndpoint } = await import("../../src/utils/urlUtils.ts");
- assert.equal(
- isAzureOpenAIEndpoint("https://r.cognitiveservices.azure.com/openai/v1"),
- true
- );
+ assert.equal(isAzureOpenAIEndpoint("https://r.cognitiveservices.azure.com/openai/v1"), true);
assert.equal(isAzureOpenAIEndpoint("https://r.openai.azure.com"), true);
assert.equal(isAzureOpenAIEndpoint("https://r.services.ai.azure.com/openai/v1"), true);
assert.equal(isAzureOpenAIEndpoint("https://api.openai.com/v1"), false);
@@ -51,3 +48,31 @@ test("buildAzureTranscriptionUrl returns null when no deployment name is availab
assert.equal(buildAzureTranscriptionUrl("https://r.cognitiveservices.azure.com", ""), null);
});
+
+// getTranscriptionEndpoint feeds buildAzureTranscriptionUrl the raw base, not the
+// normalized one, because normalizeBaseUrl strips the /audio/transcriptions suffix
+// that marks a deployment the user pinned by pasting their full Azure endpoint.
+test("a pinned Azure endpoint survives base-URL normalization", async () => {
+ const { buildAzureTranscriptionUrl } = await import("../../src/utils/urlUtils.ts");
+ const { normalizeBaseUrl } = await import("../../src/config/constants.ts");
+
+ for (const pinned of [
+ "https://r.openai.azure.com/openai/deployments/my-deploy/audio/transcriptions?api-version=2024-06-01",
+ "https://r.openai.azure.com/openai/deployments/my-deploy/audio/transcriptions",
+ ]) {
+ assert.equal(buildAzureTranscriptionUrl(pinned, "whisper-1"), pinned);
+ assert.notEqual(buildAzureTranscriptionUrl(normalizeBaseUrl(pinned), "whisper-1"), pinned);
+ }
+});
+
+test("a bare Azure resource base still rebuilds from the deployment name", async () => {
+ const { buildAzureTranscriptionUrl } = await import("../../src/utils/urlUtils.ts");
+ const { normalizeBaseUrl } = await import("../../src/config/constants.ts");
+
+ const bare = "https://r.openai.azure.com";
+
+ assert.equal(
+ buildAzureTranscriptionUrl(bare, "my-deploy"),
+ buildAzureTranscriptionUrl(normalizeBaseUrl(bare), "my-deploy")
+ );
+});
diff --git a/test/helpers/modelListBaseCandidates.test.js b/test/helpers/modelListBaseCandidates.test.js
index bb389c488..0035693a5 100644
--- a/test/helpers/modelListBaseCandidates.test.js
+++ b/test/helpers/modelListBaseCandidates.test.js
@@ -60,3 +60,25 @@ test("normalizes trailing slashes and endpoint suffixes before deriving candidat
]);
assert.deepEqual(getModelListBaseCandidates(""), []);
});
+
+// Regression for #1309: query strings must not break /v1 detection or native
+// /api/v0|/api/v1 sibling derivation.
+test("preserves query strings while deriving /v1 candidates", async () => {
+ const { getModelListBaseCandidates } = await load();
+
+ assert.deepEqual(getModelListBaseCandidates("https://gateway.example.com/v1?api-key=secret"), [
+ "https://gateway.example.com/v1?api-key=secret",
+ ]);
+ assert.deepEqual(getModelListBaseCandidates("https://gateway.example.com?api-key=secret"), [
+ "https://gateway.example.com?api-key=secret",
+ "https://gateway.example.com/v1?api-key=secret",
+ ]);
+ assert.deepEqual(getModelListBaseCandidates("http://127.0.0.1:1234/api/v1?token=abc"), [
+ "http://127.0.0.1:1234/api/v1?token=abc",
+ "http://127.0.0.1:1234/v1?token=abc",
+ ]);
+ assert.deepEqual(
+ getModelListBaseCandidates("https://api.example.com/v1/models?api-version=2024-02-01"),
+ ["https://api.example.com/v1?api-version=2024-02-01"]
+ );
+});