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
feat(llm): choose DeepSeek V4's reasoning effort from the chat
Elian Doran4d0f53bff801
diff --git a/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.spec.tsx b/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.spec.tsxindex ac439e26e4..bcc227a877 100644--- a/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.spec.tsx@@ -96,6 +96,19 @@ describe("ModelSelection", () => {expect(onChange).not.toHaveBeenCalled();});+ it("refreshes the stored metadata of selected models from the listing", async () => {+ // Stored before the provider listed effort levels, with an old price; `gone` is no longer listed.+ const stale: LlmModelInfo = { id: "gpt-4.1", name: "GPT-4.1", pricing: { input: 1, output: 1 } };+ const gone: LlmModelInfo = { id: "gone", name: "Gone" };+ const fresh: LlmModelInfo = { ...MODELS[0], reasoningEfforts: ["low", "high"], defaultReasoningEffort: "high" };+ fetchProviderModelsMock.mockResolvedValue([fresh, MODELS[1], MODELS[2]]);+ const onChange = vi.fn();++ await renderSelection({ query: { provider: "openai" }, selected: [stale, gone], onChange, autoSelectDefaults: true });+ expect(onChange).toHaveBeenCalledOnce();+ expect(onChange).toHaveBeenCalledWith([fresh, gone]);+ });+it("leaves an empty selection untouched when auto-select is off", async () => {// A provider deliberately emptied to "hide all" — reopening its editor// must not silently re-populate it (see shouldSeedDefaultModels).diff --git a/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.tsx b/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.tsxindex 23f9075219..cb8267cdad 100644--- a/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.tsx+++ b/apps/client/src/widgets/type_widgets/options/llm/ModelSelection.tsx@@ -55,6 +55,11 @@ export default function ModelSelection({ query, timeoutMs, selected, onChange, asetLoading(false);if (autoSelectDefaults && selected.length === 0 && fetched.length > 0) {onChange(defaultSelectedModels(fetched));+ return;+ }+ const refreshed = refreshSelectedModels(selected, fetched);+ if (refreshed) {+ onChange(refreshed);}}).catch(err => {@@ -129,6 +134,17 @@ function defaultSelectedModels(models: LlmModelInfo[]): LlmModelInfo[] {return models.filter(model => model.recommended);}+/**+ * The selection with each stored model replaced by its listed entry, so saving the provider picks+ * up new metadata such as `reasoningEfforts` or a changed price. A model the listing no longer+ * carries stays as stored. Returns undefined when nothing changed.+ */+function refreshSelectedModels(selected: LlmModelInfo[], fetched: LlmModelInfo[]): LlmModelInfo[] | undefined {+ const listed = new Map(fetched.map(model => [model.id, model]));+ const refreshed = selected.map(model => listed.get(model.id) ?? model);+ return JSON.stringify(refreshed) === JSON.stringify(selected) ? undefined : refreshed;+}+/** Row label: model name plus a cost hint (per-Mtok price / subscription) when known. */function ModelLabel({ model }: { model: LlmModelInfo }) {const cost = formatModelCost(model);diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.htmlindex a74a269c9e..6ec3da068c 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html@@ -119,6 +119,24 @@ class="admonition note"><p>Currently only the search native to the LLM provider is supported. Externalsearch providers such as Exa, Tavily & SearXNG are not yet supported.</p></aside>+<h3>Thinking</h3>+<p>Some models reason before they answer. Their reasoning appears above the+ reply as a collapsible <em>Thought process</em> line.</p>+<p>How much a model thinks is set per chat, in one of two ways depending+ on the model:</p>+<ul>+ <li>Most models have an <em>Extended thinking</em> switch, next to <em>Web search</em>.</li>+ <li>Models that offer several levels of reasoning (DeepSeek V4, OpenAI Codex,+ Antigravity) have a <span class="tn-icon bx bx-brain"></span> reasoning effort+ dropdown next to the model selector instead. <em>None</em>, where offered,+ turns thinking off; higher levels give better answers to hard questions,+ but take longer and cost more.</li>+</ul>+<aside class="admonition note">+ <p>The effort dropdown only appears once Trilium knows the model's levels.+ For a DeepSeek provider set up with an earlier version of Trilium, edit+ the provider in the model selection box and press <em>Save</em> once.</p>+</aside><h3>Note access (tools)</h3><p>Tools allow the agentic AI to understand and operate on notes directlywithin your Trilium instance.</p>diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 73742b9d47..226a400bc1 100644--- a/docs/User Guide/User Guide/AI.md+++ b/docs/User Guide/User Guide/AI.md@@ -90,6 +90,18 @@ This feature is on by default but it can easily be disabled by clicking on the m> [!NOTE]> Currently only the search native to the LLM provider is supported. External search providers such as Exa, Tavily & SearXNG are not yet supported.+### Thinking++Some models reason before they answer. Their reasoning appears above the reply as a collapsible _Thought process_ line.++How much a model thinks is set per chat, in one of two ways depending on the model:++* Most models have an _Extended thinking_ switch, next to _Web search_.+* Models that offer several levels of reasoning (DeepSeek V4, OpenAI Codex, Antigravity) have a <span class="tn-icon bx bx-brain"></span> reasoning effort dropdown next to the model selector instead. _None_, where offered, turns thinking off; higher levels give better answers to hard questions, but take longer and cost more.++> [!NOTE]+> The effort dropdown only appears once Trilium knows the model's levels. For a DeepSeek provider set up with an earlier version of Trilium, edit the provider in the model selection box and press _Save_ once.+### Note access (tools)Tools allow the agentic AI to understand and operate on notes directly within your Trilium instance.diff --git a/packages/trilium-core/src/services/llm/providers/base_provider.ts b/packages/trilium-core/src/services/llm/providers/base_provider.tsindex 5067f174e8..3c5b3832aa 100644--- a/packages/trilium-core/src/services/llm/providers/base_provider.ts+++ b/packages/trilium-core/src/services/llm/providers/base_provider.ts@@ -413,6 +413,10 @@ export abstract class BaseProvider implements LlmProvider {onError: () => {},telemetry: TELEMETRY_OFF};+ const providerOptions = this.chatProviderOptions(config);+ if (providerOptions) {+ streamOptions.providerOptions = providerOptions;+ }const tools = this.buildTools(config);if (Object.keys(tools).length > 0) {@@ -424,6 +428,11 @@ export abstract class BaseProvider implements LlmProvider {return streamText(streamOptions);}+ /** Provider-specific `providerOptions` for a chat turn, such as a reasoning effort. */+ protected chatProviderOptions(_config: LlmProviderConfig): Parameters<typeof streamText>[0]["providerOptions"] {+ return undefined;+ }+/*** This provider's slice of the committed price table. Overridable so tests* can inject a table without a real JSON file.diff --git a/packages/trilium-core/src/services/llm/providers/deepseek.spec.ts b/packages/trilium-core/src/services/llm/providers/deepseek.spec.tsindex b3dfad4af0..227ff56b63 100644--- a/packages/trilium-core/src/services/llm/providers/deepseek.spec.ts+++ b/packages/trilium-core/src/services/llm/providers/deepseek.spec.ts@@ -30,6 +30,7 @@ import type { LlmStreamChunk } from "@triliumnext/commons";import { installGlobalFetchAsApiTransport } from "../../../test/request_provider.js";import { streamToChunks } from "../stream.js";+import type { LlmProviderConfig } from "../types.js";import { DeepSeekProvider, deepSeekModelName } from "./deepseek.js";import { llmFetch } from "./fetch.js";@@ -213,8 +214,7 @@ describe("DeepSeekProvider streaming", () => {{ choices: [{ index: 0, delta: { content: "Pick B." } }] },{ choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 5, completion_tokens: 7 } }];- const body = `${events.map(e => `data: ${JSON.stringify({ id: "c1", created: 0, model: "deepseek-v4-pro", ...e })}\n\n`).join("")}data: [DONE]\n\n`;- const fetchMock = vi.fn(async () => new Response(body, { headers: { "content-type": "text/event-stream" } }));+ const fetchMock = vi.fn(async () => sseResponse(events));vi.stubGlobal("fetch", fetchMock);const provider = new DeepSeekProvider("sk-deep");@@ -232,6 +232,58 @@ describe("DeepSeekProvider streaming", () => {});});+describe("DeepSeekProvider reasoning effort", () => {+ const fetchMock = vi.fn();+ const okJson = (body: unknown) => ({ ok: true, json: async () => body });++ beforeEach(() => {+ fetchMock.mockReset();+ vi.stubGlobal("fetch", fetchMock);+ });++ it("offers the effort levels on V4 models only", async () => {+ fetchMock.mockResolvedValue(okJson({ data: [{ id: "deepseek-v4-pro" }, { id: "deepseek-flash" }, { id: "deepseek-chat" }] }));+ const models = await new DeepSeekProvider("sk-deep").listModels();++ for (const id of ["deepseek-v4-pro", "deepseek-flash"]) {+ expect(models.find(m => m.id === id)).toMatchObject({+ reasoningEfforts: ["none", "low", "high", "max"],+ defaultReasoningEffort: "high"+ });+ }+ const legacy = models.find(m => m.id === "deepseek-chat");+ expect(legacy).toBeDefined();+ expect(legacy).not.toHaveProperty("reasoningEfforts");+ });++ it("sends the chosen effort, and the default when the chat has none", async () => {+ fetchMock.mockImplementation(async () => sseResponse([+ { choices: [{ index: 0, delta: { role: "assistant", content: "Ok" }, finish_reason: "stop" }] }+ ]));+ const provider = new DeepSeekProvider("sk-deep");+ const requestBody = async (config: LlmProviderConfig) => {+ fetchMock.mockClear();+ for await (const _ of streamToChunks(provider.chat([{ role: "user", content: "Hi" }], config))) { /* drain */ }+ expect(fetchMock).toHaveBeenCalledOnce();+ return JSON.parse(String((fetchMock.mock.calls[0][1] as RequestInit).body));+ };++ expect(await requestBody({ model: "deepseek-v4-pro", reasoningEffort: "max" }))+ .toMatchObject({ thinking: { type: "enabled" }, reasoning_effort: "max" });+ expect(await requestBody({ model: "deepseek-v4-pro" }))+ .toMatchObject({ thinking: { type: "enabled" }, reasoning_effort: "high" });++ const none = await requestBody({ model: "deepseek-v4-pro", reasoningEffort: "none" });+ expect(none).toMatchObject({ thinking: { type: "disabled" } });+ expect(none).not.toHaveProperty("reasoning_effort");++ // A model without levels is left to DeepSeek's own default.+ const legacy = await requestBody({ model: "deepseek-chat", reasoningEffort: "max" });+ expect(legacy).not.toHaveProperty("thinking");+ expect(legacy).not.toHaveProperty("reasoning_effort");+ });+});+describe("DeepSeekProvider recommendations", () => {it("pre-selects the current line but not the superseded coder models", () => {const provider = new DeepSeekProvider("sk-deep");@@ -256,3 +308,9 @@ describe("deepSeekModelName", () => {expect(deepSeekModelName("llama3.2")).toBe("llama3.2");});});++/** A Chat Completions stream carrying `events`, as DeepSeek sends it. */+function sseResponse(events: object[]) {+ const body = `${events.map(e => `data: ${JSON.stringify({ id: "c1", created: 0, model: "deepseek-v4-pro", ...e })}\n\n`).join("")}data: [DONE]\n\n`;+ return new Response(body, { headers: { "content-type": "text/event-stream" } });+}diff --git a/packages/trilium-core/src/services/llm/providers/deepseek.ts b/packages/trilium-core/src/services/llm/providers/deepseek.tsindex a305db99d1..a16acbb8c2 100644--- a/packages/trilium-core/src/services/llm/providers/deepseek.ts+++ b/packages/trilium-core/src/services/llm/providers/deepseek.ts@@ -1,6 +1,7 @@-import { createDeepSeek, type DeepSeekProvider as DeepSeekSDKProvider } from "@ai-sdk/deepseek";+import { createDeepSeek, type DeepSeekLanguageModelChatOptions, type DeepSeekProvider as DeepSeekSDKProvider } from "@ai-sdk/deepseek";+import type { LlmReasoningEffort } from "@triliumnext/commons";-import type { ModelInfo } from "../types.js";+import type { LlmProviderConfig, ModelInfo } from "../types.js";import { BaseProvider, type RemoteModel } from "./base_provider.js";import { llmFetch } from "./fetch.js";@@ -11,6 +12,11 @@ import { llmFetch } from "./fetch.js";*/const OFFICIAL_BASE_URL = "https://api.deepseek.com/v1";+/** The thinking levels of a V4 model, weakest first: `none` turns thinking off, the rest set `reasoning_effort`. */+const V4_REASONING_EFFORTS = [ "none", "low", "high", "max" ] as const satisfies LlmReasoningEffort[];+type V4ReasoningEffort = (typeof V4_REASONING_EFFORTS)[number];+const V4_DEFAULT_REASONING_EFFORT: V4ReasoningEffort = "high";+/*** DeepSeek, over its OpenAI-compatible API. `@ai-sdk/deepseek` rather than* `@ai-sdk/openai` reads the thinking from `reasoning_content` and sends it back@@ -48,6 +54,29 @@ export class DeepSeekProvider extends BaseProvider {return this.deepseek.chat(modelId);}+ /** V4 models get the reasoning effort dropdown in place of the extended thinking switch. */+ override async listModels(): Promise<ModelInfo[]> {+ const models = await super.listModels();+ return models.map(m => isDeepSeekV4Model(m.id)+ ? { ...m, reasoningEfforts: [ ...V4_REASONING_EFFORTS ], defaultReasoningEffort: V4_DEFAULT_REASONING_EFFORT }+ : m);+ }++ /**+ * Sends the chat's effort to a V4 model, or the default when the chat has none, so the+ * level the dropdown shows is the one DeepSeek runs at.+ */+ protected override chatProviderOptions(config: LlmProviderConfig) {+ if (!isDeepSeekV4Model(config.model || this.defaultModel)) {+ return undefined;+ }+ const effort = V4_REASONING_EFFORTS.find(level => level === config.reasoningEffort) ?? V4_DEFAULT_REASONING_EFFORT;+ const options: DeepSeekLanguageModelChatOptions = effort === "none"+ ? { thinking: { type: "disabled" } }+ : { thinking: { type: "enabled" }, reasoningEffort: effort };+ return { deepseek: options };+ }+/** Everything DeepSeek lists is a chat model, so nothing is filtered out. */protected override async fetchRemoteModels(): Promise<RemoteModel[] | null> {const payload = await this.fetchJson(`${this.baseURL ?? OFFICIAL_BASE_URL}/models`, {@@ -148,3 +177,8 @@ export function deepSeekModelName(id: string): string {.map(word => (/^v\d+$/i.test(word) ? word.toUpperCase() : word.charAt(0).toUpperCase() + word.slice(1)));return `DeepSeek ${words.join(" ")}`;}++/** The V4 line, which takes `thinking` and `reasoning_effort`; the same test `@ai-sdk/deepseek` applies. */+function isDeepSeekV4Model(id: string): boolean {+ return id.includes("deepseek-v4") || id.startsWith("deepseek-flash") || id.startsWith("deepseek-pro");+}