Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): name the provider when the AI assistant has no model to send

Elian Doran921e148b974c
diff --git a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.spec.ts b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.spec.ts
new file mode 100644
index 0000000000..94dc908196
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.spec.ts
@@ -0,0 +1,76 @@
+import type { LlmChatConfig } from "@triliumnext/commons";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("../../../services/i18n.js", () => ({ t: (key: string) => key }));
+vi.mock("../../../services/options.js", () => ({
+ default: { getJson: () => storedProviders }
+}));
+vi.mock("../../../services/llm_chat.js", () => ({
+ streamChatCompletion: vi.fn(async (_messages, config, callbacks) => {
+ requestedConfigs.push(config);
+ callbacks.onChunk("<p>hi</p>");
+ callbacks.onDone();
+ })
+}));
+
+import buildAiAssistantStream from "./ai_assistant_stream.js";
+
+/** The `llmProviders` option as the mocked `options.getJson` will return it. */
+let storedProviders: unknown = null;
+/** The config each `streamChatCompletion` call was made with, in order. */
+let requestedConfigs: LlmChatConfig[] = [];
+
+beforeEach(() => {
+ storedProviders = null;
+ requestedConfigs = [];
+});
+
+/** Runs one completion through the built stream and returns the config it sent. */
+async function runOnce(): Promise<LlmChatConfig> {
+ const stream = buildAiAssistantStream();
+ if (!stream) {
+ throw new Error("expected the assistant to be enabled");
+ }
+ await stream({ query: "Fix typos", context: "<p>teh</p>" }, () => {}, new AbortController().signal);
+ const [config] = requestedConfigs;
+ return config;
+}
+
+describe("buildAiAssistantStream", () => {
+ it("stays disabled when no provider is configured", () => {
+ expect(buildAiAssistantStream()).toBeUndefined();
+ storedProviders = [];
+ expect(buildAiAssistantStream()).toBeUndefined();
+ });
+
+ it("uses the first provider's default model, else its first", async () => {
+ storedProviders = [{
+ id: "cfg-openai",
+ provider: "openai",
+ selectedModels: [{ id: "gpt-5-mini" }, { id: "gpt-5", isDefault: true }]
+ }];
+ expect(await runOnce()).toEqual({ model: "gpt-5", provider: "openai", providerId: "cfg-openai" });
+
+ requestedConfigs = [];
+ storedProviders = [{ id: "cfg-openai", provider: "openai", selectedModels: [{ id: "gpt-5-mini" }] }];
+ expect(await runOnce()).toEqual({ model: "gpt-5-mini", provider: "openai", providerId: "cfg-openai" });
+ });
+
+ it("skips providers without a selected model", async () => {
+ storedProviders = [
+ { id: "cfg-openai", provider: "openai" },
+ { id: "cfg-ollama", provider: "ollama", selectedModels: [{ id: "llama4" }] }
+ ];
+ expect(await runOnce()).toEqual({ model: "llama4", provider: "ollama", providerId: "cfg-ollama" });
+ });
+
+ it("still names the provider when no provider has a selected model", async () => {
+ // Without this the request names nothing and the server falls back to Anthropic, which
+ // either fails outright or silently answers from the wrong provider.
+ storedProviders = [
+ { id: "cfg-openai", provider: "openai" },
+ { id: "cfg-ollama", provider: "ollama", selectedModels: [] }
+ ];
+ expect(await runOnce()).toEqual({ provider: "openai", providerId: "cfg-openai" });
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts
index 7b6d2d5402..1e98b4645c 100644
--- a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts
+++ b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts
@@ -153,17 +153,25 @@ function readProviderConfigs(): StoredProviderConfig[] {
}
/**
- * The provider/model the assistant uses: the first configured provider's default model (or its
- * first model). The same resolution the LLM chat starts out with — a per-run model picker in the
- * balloon can come later.
+ * The provider/model the assistant uses: the first configured provider that has a model selected,
+ * preferring its default. The same resolution the LLM chat starts out with — a per-run model
+ * picker in the balloon can come later.
+ *
+ * The provider is always named, even when no model can be: the server falls back to
+ * `getProviderByType("anthropic")` for a request that names none (`runChat` in
+ * `packages/trilium-core/src/services/llm/chat.ts`), which would fail outright for an
+ * OpenAI-only setup and silently use the wrong provider for a mixed one. Naming the provider
+ * without a model instead lets the server resolve that provider's own default.
*/
function pickDefaultModel(): LlmChatConfig {
- for (const config of readProviderConfigs()) {
+ const configs = readProviderConfigs();
+ for (const config of configs) {
const model = config.selectedModels?.find((m) => m.isDefault) ?? config.selectedModels?.[0];
if (model) {
return { model: model.id, provider: config.provider, providerId: config.id };
}
}
- // No selected models anywhere: let the server resolve the provider's own default.
- return {};
+
+ const [first] = configs;
+ return first ? { provider: first.provider, providerId: first.id } : {};
}