Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): opt the Anthropic provider into direct browser access

Elian Doran84e9814402dc
diff --git a/packages/trilium-core/src/services/llm/providers/anthropic.spec.ts b/packages/trilium-core/src/services/llm/providers/anthropic.spec.ts
index 369f47bcc0..f51b7bf721 100644
--- a/packages/trilium-core/src/services/llm/providers/anthropic.spec.ts
+++ b/packages/trilium-core/src/services/llm/providers/anthropic.spec.ts
@@ -46,20 +46,27 @@ describe("AnthropicProvider construction", () => {
it("forwards apiKey only when no baseURL provided", () => {
new AnthropicProvider("sk-ant-test");
expect(createAnthropicMock).toHaveBeenCalledTimes(1);
- expect(createAnthropicMock).toHaveBeenCalledWith({ apiKey: "sk-ant-test" });
+ expect(createAnthropicMock).toHaveBeenCalledWith({
+ apiKey: "sk-ant-test",
+ headers: { "anthropic-dangerous-direct-browser-access": "true" }
+ });
});
it("forwards apiKey and baseURL when both provided", () => {
new AnthropicProvider("sk-ant-test", "https://proxy.example.com/v1");
expect(createAnthropicMock).toHaveBeenCalledWith({
apiKey: "sk-ant-test",
+ headers: { "anthropic-dangerous-direct-browser-access": "true" },
baseURL: "https://proxy.example.com/v1"
});
});
it("omits baseURL when empty string is provided", () => {
new AnthropicProvider("sk-ant-test", "");
- expect(createAnthropicMock).toHaveBeenCalledWith({ apiKey: "sk-ant-test" });
+ expect(createAnthropicMock).toHaveBeenCalledWith({
+ apiKey: "sk-ant-test",
+ headers: { "anthropic-dangerous-direct-browser-access": "true" }
+ });
});
it("throws when apiKey is missing", () => {
@@ -312,7 +319,13 @@ describe("AnthropicProvider model listing", () => {
expect(fetchMock).toHaveBeenCalledWith(
"https://api.anthropic.com/v1/models?limit=1000",
expect.objectContaining({
- headers: { "x-api-key": "sk-ant", "anthropic-version": "2023-06-01" }
+ // The browser-access header rides along so the standalone build's
+ // worker is not blocked by CORS before the request leaves the page.
+ headers: {
+ "x-api-key": "sk-ant",
+ "anthropic-version": "2023-06-01",
+ "anthropic-dangerous-direct-browser-access": "true"
+ }
})
);
expect(models.map((m) => m.id)).toEqual(["claude-sonnet-5", "claude-omega-6"]);
diff --git a/packages/trilium-core/src/services/llm/providers/anthropic.ts b/packages/trilium-core/src/services/llm/providers/anthropic.ts
index d491c03278..7e29c452b8 100644
--- a/packages/trilium-core/src/services/llm/providers/anthropic.ts
+++ b/packages/trilium-core/src/services/llm/providers/anthropic.ts
@@ -7,6 +7,18 @@ import { BaseProvider, buildModelMessage, type RemoteModel } from "./base_provid
const OFFICIAL_BASE_URL = "https://api.anthropic.com/v1";
+/**
+ * Opts this origin into Anthropic's CORS policy, without which a request made
+ * from a page — the standalone build's worker — is rejected by the browser
+ * before it reaches the API. It is what the official SDK's
+ * `dangerouslyAllowBrowser` puts on the wire, and it is inert on the server.
+ *
+ * The "dangerous" it names is the API key travelling from an environment the
+ * user can read. Standalone already holds the key locally: its whole database
+ * lives in this browser.
+ */
+const BROWSER_ACCESS_HEADER = { "anthropic-dangerous-direct-browser-access": "true" };
+
/** Anthropic ephemeral prompt-caching breakpoint. */
const CACHE_CONTROL = { anthropic: { cacheControl: { type: "ephemeral" as const } } };
@@ -31,7 +43,7 @@ export class AnthropicProvider extends BaseProvider {
if (!apiKey) {
throw new Error("API key is required for Anthropic provider");
}
- this.anthropic = createAnthropic({ apiKey, ...(baseURL && { baseURL }) });
+ this.anthropic = createAnthropic({ apiKey, headers: BROWSER_ACCESS_HEADER, ...(baseURL && { baseURL }) });
}
protected createModel(modelId: string) {
@@ -42,7 +54,8 @@ export class AnthropicProvider extends BaseProvider {
protected override async fetchRemoteModels(): Promise<RemoteModel[] | null> {
const payload = await this.fetchJson(`${this.baseURL ?? OFFICIAL_BASE_URL}/models?limit=1000`, {
"x-api-key": this.apiKey,
- "anthropic-version": "2023-06-01"
+ "anthropic-version": "2023-06-01",
+ ...BROWSER_ACCESS_HEADER
});
const data = (payload as { data?: unknown }).data;
if (!Array.isArray(data)) {