Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): recommend the newest model generation by default

Elian Doran15096a78a93c
diff --git a/apps/server/src/services/llm/index.spec.ts b/apps/server/src/services/llm/index.spec.ts
index a12c661fbb..23ae556248 100644
--- a/apps/server/src/services/llm/index.spec.ts
+++ b/apps/server/src/services/llm/index.spec.ts
@@ -24,6 +24,14 @@ function makeProviderMock(tag: string) {
getAvailableModels() {
return [{ id: `${tag}-model`, name: `${tag} Model` }];
}
+ listModels() {
+ // A current model plus a preview one — the fallback recommendation
+ // rule (non-openai) keeps the former and drops the latter.
+ return Promise.resolve([
+ { id: `${tag}-model`, name: `${tag} Model` },
+ { id: `${tag}-preview`, name: `${tag} Preview` }
+ ]);
+ }
};
}
@@ -151,11 +159,14 @@ describe("llm/index provider registry", () => {
});
describe("listProviderModels", () => {
- it("lists models for ad-hoc credentials, tagged with the default-recommended flag", async () => {
+ it("lists models for ad-hoc credentials, tagged with the recommended flag", async () => {
// No saved config needed — the add/edit flow passes raw credentials.
- const models = await listProviderModels("openai", "sk-test", "http://localhost:11434/v1");
- // The curated (fallback) model is non-legacy → recommended by default.
- expect(models).toEqual([{ id: "openai-model", name: "openai Model", recommended: true }]);
+ // (Provider-specific recommendation rules are covered in model_listing.spec.)
+ const models = await listProviderModels("anthropic", "k");
+ expect(models).toEqual([
+ { id: "anthropic-model", name: "anthropic Model", recommended: true },
+ { id: "anthropic-preview", name: "anthropic Preview", recommended: false }
+ ]);
});
it("throws for an unknown provider type", async () => {
diff --git a/apps/server/src/services/llm/index.ts b/apps/server/src/services/llm/index.ts
index 62ef50c299..486c5eeeef 100644
--- a/apps/server/src/services/llm/index.ts
+++ b/apps/server/src/services/llm/index.ts
@@ -3,7 +3,7 @@ import { getLog, options as optionService } from "@triliumnext/core";
import { AnthropicProvider } from "./providers/anthropic.js";
import { ClaudeAgentProvider } from "./providers/claude_agent.js";
import { GoogleProvider } from "./providers/google.js";
-import { isRecommendedByDefault } from "./providers/model_listing.js";
+import { recommendedModelIds } from "./providers/model_listing.js";
import { OpenAiProvider } from "./providers/openai.js";
import type { LlmProvider, ModelInfo } from "./types.js";
@@ -139,7 +139,8 @@ export async function listProviderModels(provider: string, apiKey: string, baseU
const models = await (instance.listModels?.() ?? instance.getAvailableModels());
// Tag the default-selected set here so the recommendation rule lives on the
// server (next to the model metadata) rather than in the client picker.
- return models.map(model => ({ ...model, recommended: isRecommendedByDefault(model, provider) }));
+ const recommended = recommendedModelIds(models, provider);
+ return models.map(model => ({ ...model, recommended: recommended.has(model.id) }));
}
/**
diff --git a/apps/server/src/services/llm/providers/model_listing.spec.ts b/apps/server/src/services/llm/providers/model_listing.spec.ts
index f135bd4dbc..bfdbf54ec5 100644
--- a/apps/server/src/services/llm/providers/model_listing.spec.ts
+++ b/apps/server/src/services/llm/providers/model_listing.spec.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import type { ModelInfo } from "../types.js";
-import { isGoogleChatModel, isOpenAiChatModel, isRecommendedByDefault, mergeModelLists, openAiModelName } from "./model_listing.js";
+import { isGoogleChatModel, isOpenAiChatModel, mergeModelLists, openAiModelName, recommendedModelIds } from "./model_listing.js";
const CURATED: ModelInfo[] = [
{ id: "gpt-4.1", name: "GPT-4.1", pricing: { input: 2, output: 8 }, contextWindow: 1047576, isDefault: true, costMultiplier: 1 },
@@ -189,22 +189,43 @@ describe("isGoogleChatModel", () => {
});
});
-describe("isRecommendedByDefault", () => {
- it("recommends current models and rejects legacy ones for any provider", () => {
- expect(isRecommendedByDefault({ id: "gpt-4.1" }, "openai")).toBe(true);
- expect(isRecommendedByDefault({ id: "claude-sonnet-5" }, "anthropic")).toBe(true);
- expect(isRecommendedByDefault({ id: "gpt-4o", isLegacy: true }, "openai")).toBe(false);
+describe("recommendedModelIds", () => {
+ // A representative slice of a live OpenAI /models response.
+ const OPENAI = [
+ "gpt-4.1", "gpt-4.1-mini", "gpt-4.1-nano", "gpt-4o", "gpt-4", "gpt-3.5-turbo",
+ "gpt-5", "gpt-5-mini", "gpt-5-chat-latest", "gpt-5.4", "gpt-5.4-mini", "gpt-5.4-pro",
+ "gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna",
+ "o1", "o1-pro", "o3", "o3-mini", "o4-mini"
+ ].map(id => ({ id }));
+
+ it("recommends only the newest generation of each OpenAI line, core tiers only", () => {
+ const ids = recommendedModelIds(OPENAI, "openai");
+ // Newest GPT generation (5.6) + newest o-series (o4) — nothing older.
+ expect([...ids].sort()).toEqual(["gpt-5.6-luna", "gpt-5.6-sol", "gpt-5.6-terra", "o4-mini"]);
+ });
+
+ it("excludes -pro and -chat-latest even within the newest generation", () => {
+ const ids = recommendedModelIds([
+ { id: "gpt-9-sol" }, { id: "gpt-9-pro" }, { id: "gpt-9-chat-latest" }
+ ], "openai");
+ expect([...ids]).toEqual(["gpt-9-sol"]);
+ });
+
+ it("degrades to the newest available generation for an older-only endpoint", () => {
+ const ids = recommendedModelIds([{ id: "gpt-4" }, { id: "gpt-4.1" }, { id: "gpt-4o" }], "openai");
+ expect([...ids]).toEqual(["gpt-4.1"]); // 4.1 > 4o (4.0) > 4
});
- it("excludes Gemini preview models, but keeps the stable line-up", () => {
- expect(isRecommendedByDefault({ id: "gemini-2.5-flash" }, "google")).toBe(true);
- expect(isRecommendedByDefault({ id: "gemini-3.6-flash" }, "google")).toBe(true);
- expect(isRecommendedByDefault({ id: "gemini-3-flash-preview" }, "google")).toBe(false);
- expect(isRecommendedByDefault({ id: "gemini-3.1-pro-preview" }, "google")).toBe(false);
+ it("recommends nothing for an OpenAI-compatible endpoint with no gpt/o models", () => {
+ expect(recommendedModelIds([{ id: "llama3.2" }, { id: "qwen2.5" }], "openai").size).toBe(0);
});
- it("does not apply the preview rule to non-Google providers", () => {
- // A hypothetical OpenAI-compatible endpoint serving a "preview" id.
- expect(isRecommendedByDefault({ id: "some-preview-model" }, "openai")).toBe(true);
+ it("falls back to non-preview, non-legacy models for other providers", () => {
+ const ids = recommendedModelIds([
+ { id: "gemini-2.5-flash" },
+ { id: "gemini-3-flash-preview" },
+ { id: "gemini-2.0-flash", isLegacy: true }
+ ], "google");
+ expect([...ids]).toEqual(["gemini-2.5-flash"]);
});
});
diff --git a/apps/server/src/services/llm/providers/model_listing.ts b/apps/server/src/services/llm/providers/model_listing.ts
index c556d446f0..264012fd96 100644
--- a/apps/server/src/services/llm/providers/model_listing.ts
+++ b/apps/server/src/services/llm/providers/model_listing.ts
@@ -127,17 +127,60 @@ export function isGoogleChatModel(id: string): boolean {
}
/**
- * Whether a model should be pre-selected by default when adding/resetting a
- * provider. Legacy models are never recommended; per-provider tweaks refine it
- * further — for Google, preview models are excluded so only the stable line-up
- * is suggested (the user can still tick previews manually).
+ * The ids pre-selected by default when adding/resetting a provider. Derived
+ * purely from the model ids the endpoint returns — no curated list, no
+ * hardcoded model names.
+ *
+ * For OpenAI the recency signal is the version number, across two parallel
+ * lines (the GPT line `gpt-<version>` and the reasoning o-series `o<n>`): we
+ * recommend only the newest generation of each line, core tiers only — so
+ * today's `gpt-5.6-{sol,terra,luna}` and `o4-mini`, not the dozens of older
+ * gpt-4 through 5.5 and o1/o3 models (which stay in the picker, unchecked).
+ * When gpt-5.7 / o5 ship they win automatically.
+ *
+ * Other providers fall back to "everything that isn't a preview or legacy".
*/
-export function isRecommendedByDefault(model: { id: string; isLegacy?: boolean }, provider: string): boolean {
- if (model.isLegacy) {
- return false;
+export function recommendedModelIds(models: RecommendCandidate[], provider: string): Set<string> {
+ if (provider === "openai") {
+ return openAiRecommendedIds(models);
}
- if (provider === "google" && /preview/i.test(model.id)) {
- return false;
+ return new Set(models.filter(m => !m.isLegacy && !/preview/i.test(m.id)).map(m => m.id));
+}
+
+interface RecommendCandidate {
+ id: string;
+ isLegacy?: boolean;
+}
+
+/** Newest generation of each OpenAI line (GPT + o-series), excluding -pro/-chat-latest variants. */
+function openAiRecommendedIds(models: RecommendCandidate[]): Set<string> {
+ // Premium (`-pro`) and rolling-alias (`-chat-latest`) variants are never a
+ // default; drop them before picking the newest generation.
+ const core = models.filter(m => !/-pro$/.test(m.id) && !/-chat-latest$/.test(m.id));
+ return new Set([
+ ...newestOfLine(core, /^gpt-/, gptVersion),
+ ...newestOfLine(core, /^o\d/, oSeriesVersion)
+ ]);
+}
+
+/** Ids in `models` matching `line` whose parsed version equals the line's maximum. */
+function newestOfLine(models: RecommendCandidate[], line: RegExp, version: (id: string) => number): string[] {
+ const family = models.filter(m => line.test(m.id));
+ if (family.length === 0) {
+ return [];
}
- return true;
+ const max = Math.max(...family.map(m => version(m.id)));
+ return family.filter(m => version(m.id) === max).map(m => m.id);
+}
+
+/** `gpt-5.6-sol` → 5.6, `gpt-4.1` → 4.1, `gpt-4o` → 4. */
+function gptVersion(id: string): number {
+ const match = /^gpt-(\d+(?:\.\d+)?)/.exec(id);
+ return match ? parseFloat(match[1]) : 0;
+}
+
+/** `o4-mini` → 4, `o3` → 3. */
+function oSeriesVersion(id: string): number {
+ const match = /^o(\d+)/.exec(id);
+ return match ? parseInt(match[1], 10) : 0;
}