Skip to content

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

fix: isolate Chat routing and preserve self-hosted auth (#1146)

Brett6dc6221a58b8
diff --git a/src/helpers/chatRouting.js b/src/helpers/chatRouting.js
new file mode 100644
index 000000000..de648c85b
--- /dev/null
+++ b/src/helpers/chatRouting.js
@@ -0,0 +1,35 @@
+const CLOUD_CHAT_PROVIDERS = new Set([
+ "openai",
+ "groq",
+ "gemini",
+ "anthropic",
+ "tinfoil",
+ "custom",
+ "openrouter",
+ "corti",
+]);
+
+// Resolve Chat from Chat-owned settings only. In particular, this must never
+// consult Dictation Cleanup's mode or endpoint.
+export function resolveChatRoute({ provider, lanUrl, customApiKey, isEnterpriseProvider = false }) {
+ // An explicit self-hosted URL is the caller's declared route — it wins even
+ // over a stale enterprise provider id left in settings.
+ const baseUrl = lanUrl?.trim() || "";
+ if (baseUrl) {
+ return {
+ kind: "self-hosted",
+ baseUrl,
+ apiKey: customApiKey?.trim() || "",
+ };
+ }
+
+ if (isEnterpriseProvider) {
+ return { kind: "enterprise", baseUrl: "", apiKey: "" };
+ }
+
+ if (!CLOUD_CHAT_PROVIDERS.has(provider)) {
+ return { kind: "local", baseUrl: "", apiKey: "" };
+ }
+
+ return { kind: "provider", baseUrl: "", apiKey: "" };
+}
diff --git a/src/services/ReasoningService.ts b/src/services/ReasoningService.ts
index e0efbd789..7ec7ae576 100644
--- a/src/services/ReasoningService.ts
+++ b/src/services/ReasoningService.ts
@@ -21,6 +21,7 @@ import { PROVIDER_REGISTRY, type ProviderContext } from "./ai/inferenceProviders
import { getConfiguredOpenAIBase } from "./ai/openaiBase";
import { applyThinkingSuppression } from "./ai/thinkingSuppression";
import { clearTinfoilClientCache } from "./ai/tinfoilClient";
+import { resolveChatRoute } from "../helpers/chatRouting";
export type AgentStreamChunk =
| { type: "content"; text: string }
@@ -385,32 +386,21 @@ class ReasoningService extends BaseReasoningService {
provider: string,
config: ReasoningConfig & { systemPrompt: string }
): AsyncGenerator<string, void, unknown> {
- const cloudProviders = [
- "openai",
- "groq",
- "gemini",
- "anthropic",
- "tinfoil",
- "custom",
- "openrouter",
- "corti",
- ];
- const isLocalProvider = !cloudProviders.includes(provider);
-
- const settings = getSettings();
- const lanOverride = config.lanUrl?.trim();
- const isLanCleanup = !!lanOverride || this.isLanCleanupMode();
+ const route = resolveChatRoute({
+ provider,
+ lanUrl: config.lanUrl,
+ customApiKey: config.customApiKey,
+ });
+ const isLocalProvider = route.kind === "local";
+ const isLanChat = route.kind === "self-hosted";
let endpoint: string;
let apiKey = "";
- if (isLanCleanup) {
- const rawUrl = lanOverride || settings.cleanupRemoteUrl.trim();
- const baseUrl = ensureV1Suffix(rawUrl);
+ if (isLanChat) {
+ const baseUrl = ensureV1Suffix(route.baseUrl);
endpoint = buildApiUrl(baseUrl, "/chat/completions");
- apiKey =
- config.customApiKey?.trim() ||
- (lanOverride ? "" : settings.cleanupCustomApiKey?.trim() || "");
+ apiKey = route.apiKey;
} else if (isLocalProvider) {
const serverResult = await window.electronAPI.llamaServerStart(model);
if (!serverResult.success || !serverResult.port) {
@@ -452,7 +442,7 @@ class ReasoningService extends BaseReasoningService {
}
const apiConfig = getOpenAiApiConfig(model, provider);
- const useOldTokenParam = isLocalProvider || isLanCleanup || provider === "groq";
+ const useOldTokenParam = isLocalProvider || isLanChat || provider === "groq";
const requestBody: Record<string, unknown> = {
model,
@@ -472,14 +462,14 @@ class ReasoningService extends BaseReasoningService {
}
}
- applyThinkingSuppression(requestBody, model, isLanCleanup ? "lan" : provider, config);
+ applyThinkingSuppression(requestBody, model, isLanChat ? "lan" : provider, config);
logger.logReasoning("AGENT_STREAM_REQUEST", {
endpoint,
model,
provider,
isLocal: isLocalProvider,
- isLan: !!isLanCleanup,
+ isLan: isLanChat,
messageCount: messages.length,
});
@@ -561,7 +551,7 @@ class ReasoningService extends BaseReasoningService {
if (!content) continue;
const stripThinking =
- (isLocalProvider || isLanCleanup) && config.disableThinking !== false;
+ (isLocalProvider || isLanChat) && config.disableThinking !== false;
if (stripThinking) {
if (insideThinkBlock) {
const endIdx = content.indexOf("</think>");
@@ -607,27 +597,17 @@ class ReasoningService extends BaseReasoningService {
config: ReasoningConfig & { systemPrompt: string },
tools?: Record<string, import("ai").Tool>
): AsyncGenerator<AgentStreamChunk, void, unknown> {
- const lanOverride = config.lanUrl?.trim();
- // An explicit self-hosted URL is the caller's declared route — it must win
- // even when a stale enterprise provider id is left in the scope's settings.
- const isEnterprise = !lanOverride && isEnterpriseProvider(provider);
-
- const cloudProviders = [
- "openai",
- "groq",
- "gemini",
- "anthropic",
- "tinfoil",
- "custom",
- "openrouter",
- "corti",
- ];
- const isLocalProvider = !isEnterprise && !cloudProviders.includes(provider);
-
- const settings = getSettings();
- const isLanCleanup = !isEnterprise && (!!lanOverride || this.isLanCleanupMode());
+ const route = resolveChatRoute({
+ provider,
+ lanUrl: config.lanUrl,
+ customApiKey: config.customApiKey,
+ isEnterpriseProvider: isEnterpriseProvider(provider),
+ });
+ const isEnterprise = route.kind === "enterprise";
+ const isLocalProvider = route.kind === "local";
+ const isLanChat = route.kind === "self-hosted";
- if ((isLocalProvider || isLanCleanup) && !tools) {
+ if ((isLocalProvider || isLanChat) && !tools) {
const contentGen = this.processTextStreaming(messages, model, provider, config);
for await (const text of contentGen) {
yield { type: "content", text };
@@ -642,12 +622,9 @@ class ReasoningService extends BaseReasoningService {
if (isEnterprise) {
// Enterprise SDKs run in the main process; the model below proxies
// doStream over IPC, so no key or base URL is resolved here.
- } else if (isLanCleanup) {
- const rawUrl = lanOverride || settings.cleanupRemoteUrl.trim();
- baseURL = ensureV1Suffix(rawUrl);
- apiKey =
- config.customApiKey?.trim() ||
- (lanOverride ? "" : settings.cleanupCustomApiKey?.trim() || "");
+ } else if (isLanChat) {
+ apiKey = route.apiKey;
+ baseURL = ensureV1Suffix(route.baseUrl);
} else if (isLocalProvider) {
const serverResult = await window.electronAPI.llamaServerStart(model);
if (!serverResult.success || !serverResult.port) {
@@ -666,7 +643,7 @@ class ReasoningService extends BaseReasoningService {
? config.baseUrl?.trim() || getConfiguredOpenAIBase()
: undefined;
}
- const aiProvider = isLocalProvider || isLanCleanup ? "local" : provider;
+ const aiProvider = isLocalProvider || isLanChat ? "local" : provider;
// OpenRouter ids are never in the local registry, so the supportsThinking
// exemption below can't apply — honor the toggle directly.
const openrouterDisableThinking = provider === "openrouter" && config.disableThinking === true;
@@ -699,7 +676,7 @@ class ReasoningService extends BaseReasoningService {
messageCount: messages.length,
});
- const useTemperature = isLocalProvider || isLanCleanup || apiConfig.supportsTemperature;
+ const useTemperature = isLocalProvider || isLanChat || apiConfig.supportsTemperature;
// cancelActiveStream() aborts this controller; streamText propagates it
// into doStream, cancelling the enterprise IPC proxy's request in main.
diff --git a/test/helpers/chatRouting.test.js b/test/helpers/chatRouting.test.js
new file mode 100644
index 000000000..0633adaec
--- /dev/null
+++ b/test/helpers/chatRouting.test.js
@@ -0,0 +1,76 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+const load = () => import("../../src/helpers/chatRouting.js");
+
+test("custom BYOK Chat stays on its provider route", async () => {
+ const { resolveChatRoute } = await load();
+ const route = resolveChatRoute({
+ provider: "custom",
+ customApiKey: "litellm-key",
+ });
+
+ assert.deepEqual(route, { kind: "provider", baseUrl: "", apiKey: "" });
+});
+
+test("built-in provider Chat stays on its provider route", async () => {
+ const { resolveChatRoute } = await load();
+ const route = resolveChatRoute({ provider: "gemini" });
+
+ assert.equal(route.kind, "provider");
+});
+
+test("local Chat stays on its local route", async () => {
+ const { resolveChatRoute } = await load();
+ const route = resolveChatRoute({ provider: "ollama" });
+
+ assert.equal(route.kind, "local");
+});
+
+test("explicit self-hosted Chat URL wins over a stale enterprise provider", async () => {
+ const { resolveChatRoute } = await load();
+ const route = resolveChatRoute({
+ provider: "bedrock",
+ lanUrl: " http://127.0.0.1:4000/v1 ",
+ customApiKey: " secret-key ",
+ isEnterpriseProvider: true,
+ });
+
+ assert.deepEqual(route, {
+ kind: "self-hosted",
+ baseUrl: "http://127.0.0.1:4000/v1",
+ apiKey: "secret-key",
+ });
+});
+
+test("self-hosted Chat preserves an empty optional API key", async () => {
+ const { resolveChatRoute } = await load();
+ const route = resolveChatRoute({
+ provider: "custom",
+ lanUrl: "http://127.0.0.1:11434/v1",
+ });
+
+ assert.equal(route.kind, "self-hosted");
+ assert.equal(route.apiKey, "");
+});
+
+test("enterprise Chat stays enterprise without a Chat LAN URL", async () => {
+ const { resolveChatRoute } = await load();
+ const route = resolveChatRoute({
+ provider: "vertex",
+ isEnterpriseProvider: true,
+ });
+
+ assert.equal(route.kind, "enterprise");
+});
+
+test("blank Chat LAN URL does not activate self-hosted routing", async () => {
+ const { resolveChatRoute } = await load();
+ const route = resolveChatRoute({
+ provider: "custom",
+ lanUrl: " ",
+ customApiKey: "secret-key",
+ });
+
+ assert.equal(route.kind, "provider");
+});