Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): show DeepSeek's thinking by talking to it through @ai-sdk/deepseek

Elian Doranf29535eb8ef0
diff --git a/apps/server/package.json b/apps/server/package.json
index 3bd9bd7bd9..f4d868cc0c 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -34,6 +34,7 @@
"dependencies": {
"@agentclientprotocol/codex-acp": "1.13.1",
"@ai-sdk/anthropic": "4.0.60",
+ "@ai-sdk/deepseek": "3.0.50",
"@ai-sdk/google": "4.0.77",
"@ai-sdk/openai": "4.0.72",
"@anthropic-ai/claude-agent-sdk": "^0.3.207",
diff --git a/packages/trilium-core/package.json b/packages/trilium-core/package.json
index 48f5ee31ce..9215ec8744 100644
--- a/packages/trilium-core/package.json
+++ b/packages/trilium-core/package.json
@@ -9,6 +9,7 @@
},
"dependencies": {
"@ai-sdk/anthropic": "4.0.60",
+ "@ai-sdk/deepseek": "3.0.50",
"@ai-sdk/google": "4.0.77",
"@ai-sdk/openai": "4.0.72",
"@braintree/sanitize-url": "7.1.2",
diff --git a/packages/trilium-core/src/services/llm/providers/deepseek.spec.ts b/packages/trilium-core/src/services/llm/providers/deepseek.spec.ts
index ad97349932..b3dfad4af0 100644
--- a/packages/trilium-core/src/services/llm/providers/deepseek.spec.ts
+++ b/packages/trilium-core/src/services/llm/providers/deepseek.spec.ts
@@ -1,17 +1,22 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
-const createOpenAiMock = vi.fn();
-const chatMock = vi.fn(() => ({}));
-
-vi.mock("@ai-sdk/openai", () => ({
- createOpenAI: (opts: unknown) => {
- createOpenAiMock(opts);
- const fn: any = () => ({});
- fn.chat = chatMock;
- return fn;
- }
+const { createDeepSeekMock, chatMock } = vi.hoisted(() => ({
+ createDeepSeekMock: vi.fn(),
+ chatMock: vi.fn()
}));
+// Spies around the real SDK, so a stream fed through `fetch` is parsed as it is in production.
+vi.mock("@ai-sdk/deepseek", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@ai-sdk/deepseek")>();
+ return {
+ createDeepSeek: (opts: Parameters<typeof actual.createDeepSeek>[0]) => {
+ createDeepSeekMock(opts);
+ const sdk = actual.createDeepSeek(opts);
+ return { chat: (modelId: string) => { chatMock(modelId); return sdk.chat(modelId); } };
+ }
+ };
+});
+
const { generateTextMock } = vi.hoisted(() => ({
generateTextMock: vi.fn(async () => ({ text: " A generated title " }) as any)
}));
@@ -21,8 +26,11 @@ vi.mock("ai", async (importOriginal) => {
return { ...actual, generateText: generateTextMock };
});
-import { DeepSeekProvider, deepSeekModelName } from "./deepseek.js";
+import type { LlmStreamChunk } from "@triliumnext/commons";
+
import { installGlobalFetchAsApiTransport } from "../../../test/request_provider.js";
+import { streamToChunks } from "../stream.js";
+import { DeepSeekProvider, deepSeekModelName } from "./deepseek.js";
import { llmFetch } from "./fetch.js";
// A provider reaches its endpoint through the request provider rather than the global `fetch`, so
@@ -31,19 +39,19 @@ beforeEach(installGlobalFetchAsApiTransport);
describe("DeepSeekProvider construction", () => {
beforeEach(() => {
- createOpenAiMock.mockClear();
+ createDeepSeekMock.mockClear();
chatMock.mockClear();
});
it("points at the official endpoint unless overridden, and requires a key", () => {
new DeepSeekProvider("sk-deep");
- expect(createOpenAiMock).toHaveBeenCalledWith({ apiKey: "sk-deep", baseURL: "https://api.deepseek.com/v1", fetch: llmFetch });
+ expect(createDeepSeekMock).toHaveBeenCalledWith({ apiKey: "sk-deep", baseURL: "https://api.deepseek.com/v1", fetch: llmFetch });
// An override reaches a gateway in front of DeepSeek; a blank one is no override.
new DeepSeekProvider("sk-deep", "https://gateway.example/v1");
- expect(createOpenAiMock).toHaveBeenLastCalledWith({ apiKey: "sk-deep", baseURL: "https://gateway.example/v1", fetch: llmFetch });
+ expect(createDeepSeekMock).toHaveBeenLastCalledWith({ apiKey: "sk-deep", baseURL: "https://gateway.example/v1", fetch: llmFetch });
new DeepSeekProvider("sk-deep", "");
- expect(createOpenAiMock).toHaveBeenLastCalledWith({ apiKey: "sk-deep", baseURL: "https://api.deepseek.com/v1", fetch: llmFetch });
+ expect(createDeepSeekMock).toHaveBeenLastCalledWith({ apiKey: "sk-deep", baseURL: "https://api.deepseek.com/v1", fetch: llmFetch });
expect(() => new DeepSeekProvider("")).toThrow(/API key is required/);
});
@@ -197,6 +205,33 @@ describe("DeepSeekProvider title generation", () => {
});
});
+describe("DeepSeekProvider streaming", () => {
+ it("streams `reasoning_content` as thinking ahead of the answer", async () => {
+ const events = [
+ { choices: [{ index: 0, delta: { role: "assistant", reasoning_content: "Weighing " } }] },
+ { choices: [{ index: 0, delta: { reasoning_content: "the options." } }] },
+ { 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" } }));
+ vi.stubGlobal("fetch", fetchMock);
+
+ const provider = new DeepSeekProvider("sk-deep");
+ const chunks: LlmStreamChunk[] = [];
+ for await (const chunk of streamToChunks(provider.chat([{ role: "user", content: "A or B?" }], { model: "deepseek-v4-pro" }))) {
+ chunks.push(chunk);
+ }
+
+ expect(fetchMock).toHaveBeenCalledWith("https://api.deepseek.com/v1/chat/completions", expect.anything());
+ expect(chunks.filter(c => c.type === "thinking" || c.type === "text")).toEqual([
+ { type: "thinking", content: "Weighing " },
+ { type: "thinking", content: "the options." },
+ { type: "text", content: "Pick B." }
+ ]);
+ });
+});
+
describe("DeepSeekProvider recommendations", () => {
it("pre-selects the current line but not the superseded coder models", () => {
const provider = new DeepSeekProvider("sk-deep");
diff --git a/packages/trilium-core/src/services/llm/providers/deepseek.ts b/packages/trilium-core/src/services/llm/providers/deepseek.ts
index c36fc0442b..a305db99d1 100644
--- a/packages/trilium-core/src/services/llm/providers/deepseek.ts
+++ b/packages/trilium-core/src/services/llm/providers/deepseek.ts
@@ -1,4 +1,4 @@
-import { createOpenAI, type OpenAIProvider as OpenAISDKProvider } from "@ai-sdk/openai";
+import { createDeepSeek, type DeepSeekProvider as DeepSeekSDKProvider } from "@ai-sdk/deepseek";
import type { ModelInfo } from "../types.js";
import { BaseProvider, type RemoteModel } from "./base_provider.js";
@@ -12,7 +12,9 @@ import { llmFetch } from "./fetch.js";
const OFFICIAL_BASE_URL = "https://api.deepseek.com/v1";
/**
- * DeepSeek, over its OpenAI-compatible API. It could be reached through the
+ * DeepSeek, over its OpenAI-compatible API. `@ai-sdk/deepseek` rather than
+ * `@ai-sdk/openai` reads the thinking from `reasoning_content` and sends it back
+ * with later turns. DeepSeek could also be reached through the
* generic custom-endpoint card, but a card of its own is what makes its models
* priced: {@link BaseProvider.getProviderPrices} keys the committed table by
* provider name, and DeepSeek publishes bare ids (`deepseek-chat`) that the
@@ -32,22 +34,18 @@ export class DeepSeekProvider extends BaseProvider {
/** Whether the two above have been replaced by ids the endpoint actually offers. */
private defaultsFromListing = false;
- private openai: OpenAISDKProvider;
+ private deepseek: DeepSeekSDKProvider;
constructor(apiKey: string, baseURL?: string) {
super(apiKey, baseURL);
if (!apiKey) {
throw new Error("API key is required for DeepSeek provider");
}
- this.openai = createOpenAI({ apiKey, baseURL: this.baseURL ?? OFFICIAL_BASE_URL, fetch: llmFetch });
+ this.deepseek = createDeepSeek({ apiKey, baseURL: this.baseURL ?? OFFICIAL_BASE_URL, fetch: llmFetch });
}
- /**
- * Chat Completions rather than the Responses API, which DeepSeek doesn't
- * implement — the same reason the self-hosted provider pins `.chat()`.
- */
protected createModel(modelId: string) {
- return this.openai.chat(modelId);
+ return this.deepseek.chat(modelId);
}
/** Everything DeepSeek lists is a chat model, so nothing is filtered out. */
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 00b2f83f46..3dfdb0d7b8 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -640,6 +640,9 @@ importers:
'@ai-sdk/anthropic':
specifier: 4.0.60
version: 4.0.60([email protected])
+ '@ai-sdk/deepseek':
+ specifier: 3.0.50
+ version: 3.0.50([email protected])
'@ai-sdk/google':
specifier: 4.0.77
version: 4.0.77([email protected])
@@ -1414,6 +1417,9 @@ importers:
'@ai-sdk/anthropic':
specifier: 4.0.60
version: 4.0.60([email protected])
+ '@ai-sdk/deepseek':
+ specifier: 3.0.50
+ version: 3.0.50([email protected])
'@ai-sdk/google':
specifier: 4.0.77
version: 4.0.77([email protected])
@@ -1577,6 +1583,12 @@ packages:
peerDependencies:
zod: 4.6.5
+ '@ai-sdk/[email protected]':
+ resolution: {integrity: sha512-xqQwo+nlsr/LQDU1TTk14LpW6a8BlepkCMadjj8Myoq9eOMZ7zv7Y/YC62OMTQYjC1dds7oYtZWMN6nTpMeKzg==}
+ engines: {node: '>=22'}
+ peerDependencies:
+ zod: 4.6.5
+
'@ai-sdk/[email protected]':
resolution: {integrity: sha512-n0Q88UUASYQBGK3Ogs9pCe8h3ZXeTHFSuGOLBJAyH73MVGNKUv9w8EJX2f340WUF3eDWWOV305gO7YBpSCPblA==}
engines: {node: '>=22'}
@@ -12071,6 +12083,12 @@ snapshots:
'@ai-sdk/provider-utils': 5.0.45([email protected])
zod: 4.6.5
+ dependencies:
+ '@ai-sdk/provider': 4.0.17
+ '@ai-sdk/provider-utils': 5.0.45([email protected])
+ zod: 4.6.5
+
dependencies:
'@ai-sdk/provider': 4.0.17