Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(ai-assistant): name the model a run resolves to, not the one stored
Elian Doran6593d9e701f4
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 56c3968f35..5ddd73c35c 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2845,7 +2845,6 @@"group_translate": "Translate","group_custom": "Custom","model": "Model: {{model}}",- "model_default": "Default","command_make_shorter": "Make shorter","command_make_longer": "Make longer","command_simplify": "Simplify language",diff --git a/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.ts b/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.tsindex 23481c87f6..3d8af00471 100644--- a/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/ai_model_picker.spec.ts@@ -63,12 +63,32 @@ describe("the AI assistant's model picker", () => {]);});- it("names the row for the default while nothing has been picked, and drops the provider with one configured", () => {+ // Before anything is picked the row still names a model — the one a run would resolve to —+ // rather than saying "Default", which answers nothing. The provider goes with one configured.+ it("names what a run would resolve to while nothing has been picked", () => {stored.llmProviders = [TWO_PROVIDERS[0]];const picker = buildAiModelPicker();- expect(picker[0].label).toBe("ai_assistant.model(model=ai_assistant.model_default)");- expect(labelsOf(picker)).toEqual([["Opus 5", "bx bx-empty"], ["Sonnet 5", "bx bx-empty"]]);+ expect(picker[0].label).toBe("ai_assistant.model(model=Sonnet 5)");+ expect(labelsOf(picker)).toEqual([["Opus 5", "bx bx-empty"], ["Sonnet 5", "bx bx-check"]]);+ });++ // Nothing flagged, so the first one answers — and the row says so.+ it("falls back to the first model when none is flagged as the default", () => {+ stored.llmProviders = [{ ...TWO_PROVIDERS[0], selectedModels: TWO_PROVIDERS[0].selectedModels.map(({ id, name }) => ({ id, name })) }];++ expect(buildAiModelPicker()[0].label).toBe("ai_assistant.model(model=Opus 5)");+ });++ // The stored model is gone, so the row names the one that replaced it rather than a stale name.+ it("names the fallback once the stored model is no longer on offer", () => {+ stored.llmProviders = TWO_PROVIDERS;+ stored.aiAssistantModel = { model: "gpt-4", provider: "openai", providerId: "cfg-openai" };++ const picker = buildAiModelPicker();+ expect(picker[0].label).toBe("ai_assistant.model(model=Sonnet 5 (Anthropic))");+ expect(labelsOf(picker).filter(([, icon]) => icon === "bx bx-check"))+ .toEqual([["Sonnet 5 (Anthropic)", "bx bx-check"]]);});// A row offering the only answer there is takes up space and settles nothing.diff --git a/apps/client/src/widgets/type_widgets/text/ai_model_picker.ts b/apps/client/src/widgets/type_widgets/text/ai_model_picker.tsindex e16eb4b3fe..035b382b4c 100644--- a/apps/client/src/widgets/type_widgets/text/ai_model_picker.ts+++ b/apps/client/src/widgets/type_widgets/text/ai_model_picker.ts@@ -19,7 +19,14 @@ import { shortModelName } from "../llm_chat/model_name.js";*/export function buildAiModelPicker(): AiQuickActionFooter[] {const { models, groups } = readSelectedModels();- if (models.length < 2) {++ // What a run resolves to, not what was stored: before anything is picked the row would+ // otherwise say "Default", which names no model and leaves the question it exists to answer —+ // which one am I about to spend tokens on — unanswered.+ const current = resolveModel(models);++ // A picker offering the only answer there is takes up space and settles nothing.+ if (!current || models.length < 2) {return [];}@@ -30,9 +37,8 @@ export function buildAiModelPicker(): AiQuickActionFooter[] {return groups.length > 1 && model.providerName ? `${short} (${model.providerName})` : short;};- const current = resolveStoredModel(models);return [{- label: t("ai_assistant.model", { model: current ? name(current) : t("ai_assistant.model_default") }),+ label: t("ai_assistant.model", { model: name(current) }),iconClass: "bx bx-chip",children: models.map((model) => ({label: name(model),@@ -49,9 +55,7 @@ export function buildAiModelPicker(): AiQuickActionFooter[] {}/**- * The model a run should use: the stored one while it is still on offer, and otherwise the same- * first-provider default the assistant used before there was anything to store — a model can be- * deselected or its provider deleted long after it was picked.+ * The provider and model a run is made with.** 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@@ -62,7 +66,7 @@ export function buildAiModelPicker(): AiQuickActionFooter[] {export function pickModel(): LlmChatConfig {const { models, groups } = readSelectedModels();- const chosen = resolveStoredModel(models) ?? models.find((model) => model.isDefault) ?? models[0];+ const chosen = resolveModel(models);if (chosen) {return { model: chosen.id, provider: chosen.provider, providerId: chosen.providerId };}@@ -71,6 +75,18 @@ export function pickModel(): LlmChatConfig {return first ? { provider: first.provider, providerId: first.id } : {};}+/**+ * The model a run answers with: the stored one while it is still on offer, and otherwise the same+ * first-provider default the assistant used before there was anything to store — a model can be+ * deselected, or its provider deleted, long after it was picked.+ *+ * The menu asks this too rather than asking what was stored, so the model it names and ticks is the+ * one that will actually answer.+ */+function resolveModel(models: ModelOption[]): ModelOption | undefined {+ return resolveStoredModel(models) ?? models.find((model) => model.isDefault) ?? models[0];+}+/** What the `aiAssistantModel` option holds — the three fields it takes to name a model exactly. */interface StoredModel {model: string;