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
import { type AnthropicProvider as AnthropicSDKProvider,createAnthropic } from "@ai-sdk/anthropic";
import type { LlmMessage } from "@triliumnext/commons";
import { type ModelMessage, streamText, type SystemModelMessage, type ToolSet } from "ai";
import type { LlmProviderConfig, ModelInfo, StreamResult } from "../types.js";
import { BaseProvider, buildModelMessage, type RemoteModel, TELEMETRY_OFF } from "./base_provider.js";
import { llmFetch } from "./fetch.js";
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 } } };
/**
* Models that support adaptive extended thinking. Opus 4.7 / 4.8 and the whole
* Claude 5 generation (Opus, Sonnet, Haiku, Fable, Mythos 5+) reject the manual
* `{ type: "enabled", budgetTokens }` form with a 400 and accept adaptive only;
* Opus 4.6 and Sonnet 4.6 accept both but prefer adaptive. Everything else
* (older Opus/Sonnet, Haiku 4.5, Mythos Preview) only supports the manual budget
* form.
*/
const ADAPTIVE_THINKING_MODELS =
/^claude-(?:opus-4-[678]|sonnet-4-6|(?:opus|sonnet|haiku|mythos)-[5-9]|fable-)/;
export class AnthropicProvider extends BaseProvider {
name = "anthropic";
protected defaultModel = "claude-sonnet-5";
protected titleModel = "claude-haiku-4-5-20251001";
private anthropic: AnthropicSDKProvider;
constructor(apiKey: string, baseURL?: string) {
super(apiKey, baseURL);
if (!apiKey) {
throw new Error("API key is required for Anthropic provider");
}
this.anthropic = createAnthropic({ apiKey, headers: BROWSER_ACCESS_HEADER, ...(baseURL && { baseURL }), fetch: llmFetch });
}
protected createModel(modelId: string) {
return this.anthropic(modelId);
}
/** List models from Anthropic's `/models` endpoint (all are chat models). */
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",
...BROWSER_ACCESS_HEADER
});
const data = (payload as { data?: unknown }).data;
if (!Array.isArray(data)) {
throw new Error("Unexpected /models response shape");
}
return data
.filter((m): m is { id: string; display_name?: string } => typeof (m as { id?: unknown }).id === "string")
.map(m => ({ id: m.id, name: m.display_name }));
}
protected override addWebSearchTool(tools: ToolSet): void {
// `ToolSet` pins its input parameter to `never` in two of its four arms, which a
// provider-executed tool declaring a real input matches none of. Upstream's to fix; drop
// the cast once `ai` and `@ai-sdk/anthropic` agree again.
tools.web_search = this.anthropic.tools.webSearch_20250305({
maxUses: 5
}) as ToolSet[string];
}
override recommendedModelIds(models: ModelInfo[]): Set<string> {
return anthropicRecommendedIds(models);
}
/**
* Override buildSystemMessage to add an Anthropic cache control breakpoint
* on the system prompt.
*/
protected override buildSystemMessage(systemPrompt: string | undefined): SystemModelMessage | undefined {
if (!systemPrompt) {
return undefined;
}
return {
role: "system",
content: systemPrompt,
providerOptions: CACHE_CONTROL
};
}
/**
* Override buildMessages to add Anthropic-specific cache control breakpoints.
*/
protected override buildMessages(chatMessages: LlmMessage[]): ModelMessage[] {
const coreMessages: ModelMessage[] = [];
for (let i = 0; i < chatMessages.length; i++) {
const m = chatMessages[i];
const isLastBeforeNewTurn = i === chatMessages.length - 2;
// Anthropic rejects empty text content blocks. For purely-empty
// string content (e.g. tool-only assistant turns), substitute a
// placeholder so the turn still has a body.
const normalized: LlmMessage = (typeof m.content === "string" && !m.content)
? { ...m, content: "(tool use)" }
: m;
const base = buildModelMessage(normalized);
coreMessages.push(
isLastBeforeNewTurn
? { ...base, providerOptions: CACHE_CONTROL }
: base
);
}
return coreMessages;
}
/**
* Override chat to add Anthropic-specific extended thinking support.
*/
override chat(messages: LlmMessage[], config: LlmProviderConfig, signal?: AbortSignal): StreamResult {
if (!config.enableExtendedThinking) {
return super.chat(messages, config, signal);
}
const systemPrompt = this.buildSystemPrompt(messages, config);
const chatMessages = this.applyNoteHint(messages.filter(m => m.role !== "system"), config);
const coreMessages = this.buildMessages(chatMessages);
const model = config.model || this.defaultModel;
const thinkingBudget = config.thinkingBudget || 10000;
const maxTokens = Math.max(config.maxTokens || 8096, thinkingBudget + 4000);
// Adaptive-capable models let Claude decide depth (and stream visible
// reasoning via `display: "summarized"`); older models take a manual budget.
const thinking = ADAPTIVE_THINKING_MODELS.test(model)
? { type: "adaptive" as const, display: "summarized" as const }
: { type: "enabled" as const, budgetTokens: thinkingBudget };
const streamOptions: Parameters<typeof streamText>[0] = {
model: this.createModel(model),
system: this.buildSystemMessage(systemPrompt),
messages: coreMessages,
maxOutputTokens: maxTokens,
// Reject any system message smuggled into `messages` (prompt injection guard).
allowSystemInMessages: false,
telemetry: TELEMETRY_OFF,
abortSignal: signal,
providerOptions: {
anthropic: { thinking }
}
};
this.addTools(streamOptions, config, 5);
return streamText(streamOptions);
}
}
/**
* Anthropic model id shape: `claude-<family>-<major>[-<minor>][-<YYYYMMDD>]`.
* The optional trailing 8-digit snapshot date is not part of the version —
* `claude-sonnet-4-20250514` is Sonnet 4.0, not 4.20250514 — so the minor
* group is capped at two digits to force the date into the snapshot group.
*/
const ANTHROPIC_MODEL = /^claude-([a-z]+)-(\d+)(?:-(\d{1,2}))?(?:-\d{8})?$/;
/**
* Claude families priced at the premium tier (2.5× Opus per token). They are
* never recommended; the user ticks them.
*/
const UNRECOMMENDED_FAMILIES = new Set(["fable", "mythos"]);
/**
* Recommend the newest version within each Claude family (Opus, Sonnet, Haiku,
* and any future one) — one model per family, so today's Opus 4.8, Sonnet 5 and
* Haiku 4.5. Older revisions and dated snapshots stay in the picker, unchecked,
* as do the {@link UNRECOMMENDED_FAMILIES}.
*
* Exported because the Claude Code subscription provider serves the same
* `claude-*` catalog and must apply the same rule, without inheriting from
* {@link AnthropicProvider} (it runs the Agent SDK, not the AI SDK).
*/
export function anthropicRecommendedIds(models: ModelInfo[]): Set<string> {
const byFamily = new Map<string, { id: string; version: number }[]>();
for (const model of models) {
const parsed = parseAnthropicModel(model.id);
if (!parsed || UNRECOMMENDED_FAMILIES.has(parsed.family)) {
continue;
}
const members = byFamily.get(parsed.family) ?? [];
members.push({ id: model.id, version: parsed.version });
byFamily.set(parsed.family, members);
}
const recommended = new Set<string>();
for (const members of byFamily.values()) {
// Strict `>` so the earliest listed model wins a version tie.
const newest = members.reduce((best, m) => (m.version > best.version ? m : best));
recommended.add(newest.id);
}
return recommended;
}
/**
* `claude-opus-4-8` → `{ family: "opus", version: 4.8 }`, `claude-sonnet-5` →
* version 5, `claude-sonnet-4-20250514` → version 4 (the trailing date is a
* snapshot, not a minor). Null for ids outside the `claude-<family>-<version>`
* shape, which the caller skips.
*
* Family and version are parsed together so the regex runs once per model
* rather than again on both sides of every version comparison.
*/
function parseAnthropicModel(id: string): { family: string; version: number } | null {
const match = ANTHROPIC_MODEL.exec(id);
if (!match) {
return null;
}
const [, family, major, minor] = match;
return { family, version: parseFloat(minor ? `${major}.${minor}` : major) };
}