Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(ai): add anthropic adapter support (#1298)

Corentin Thomasset0b866438d7f0
diff --git a/apps/papra-server/package.json b/apps/papra-server/package.json
index 75b1a81b..63cce8a1 100644
--- a/apps/papra-server/package.json
+++ b/apps/papra-server/package.json
@@ -64,6 +64,7 @@
"@paralleldrive/cuid2": "catalog:",
"@sindresorhus/slugify": "^3.0.0",
"@tanstack/ai": "^0.28.0",
+ "@tanstack/ai-anthropic": "^0.15.11",
"@tanstack/ai-openai": "^0.14.1",
"@valibot/to-json-schema": "^1.6.0",
"better-auth": "catalog:",
diff --git a/apps/papra-server/src/modules/ai/adapters/ai-adapters.constants.ts b/apps/papra-server/src/modules/ai/adapters/ai-adapters.constants.ts
index 840b9077..461eb10b 100644
--- a/apps/papra-server/src/modules/ai/adapters/ai-adapters.constants.ts
+++ b/apps/papra-server/src/modules/ai/adapters/ai-adapters.constants.ts
@@ -1,7 +1,7 @@
import { objectKeys } from '@papra/std';
import { OPENAI_COMPATIBLE_ADAPTERS } from './openai-compatible/openai-compatible.providers';
-export const AI_ADAPTERS_NAMES = objectKeys(OPENAI_COMPATIBLE_ADAPTERS);
+export const AI_ADAPTERS_NAMES = [...objectKeys(OPENAI_COMPATIBLE_ADAPTERS), 'anthropic'] as const;
export type AiAdapterName = (typeof AI_ADAPTERS_NAMES)[number];
export const AI_DEFAULT_ADAPTER_NAME = 'openai' satisfies AiAdapterName;
diff --git a/apps/papra-server/src/modules/ai/adapters/ai-adapters.registry.ts b/apps/papra-server/src/modules/ai/adapters/ai-adapters.registry.ts
index 7abf42cc..748fc5c5 100644
--- a/apps/papra-server/src/modules/ai/adapters/ai-adapters.registry.ts
+++ b/apps/papra-server/src/modules/ai/adapters/ai-adapters.registry.ts
@@ -3,8 +3,10 @@ import type { Config } from '../../config/config.types';
import type { AiAdapter, AiAdapterFactory } from './ai-adapters.types';
import { OPENAI_COMPATIBLE_ADAPTERS } from './openai-compatible/openai-compatible.providers';
import { buildOpenAiCompatibleTextAdapter } from './openai-compatible/openai-compatible.adapters';
+import type { AiAdapterName } from './ai-adapters.constants';
+import { buildAnthropicAdapter } from './anthropic/anthropic.ai-adapters';
-export const modelAdapterFactories = mapValues(
+const openaiCompatibleAdapterFactories = mapValues(
OPENAI_COMPATIBLE_ADAPTERS,
(_adapter, name): AiAdapterFactory =>
({ config }: { config: Config }): AiAdapter => ({
@@ -12,6 +14,11 @@ export const modelAdapterFactories = mapValues(
}),
);
+export const modelAdapterFactories = {
+ ...openaiCompatibleAdapterFactories,
+ anthropic: buildAnthropicAdapter,
+} as const satisfies Record<AiAdapterName, AiAdapterFactory>;
+
export const modelAdapterNames = objectKeys(modelAdapterFactories);
export type ModelAdapterNames = (typeof modelAdapterNames)[number];
diff --git a/apps/papra-server/src/modules/ai/adapters/anthropic/anthropic.ai-adapters.config.ts b/apps/papra-server/src/modules/ai/adapters/anthropic/anthropic.ai-adapters.config.ts
new file mode 100644
index 00000000..32e5c52d
--- /dev/null
+++ b/apps/papra-server/src/modules/ai/adapters/anthropic/anthropic.ai-adapters.config.ts
@@ -0,0 +1,20 @@
+import * as v from 'valibot';
+import type { AppConfigDefinition } from '../../../config/config.types';
+import { urlSchema } from '../../../config/config.schemas';
+
+export const anthropicAdaptersConfig = {
+ baseUrl: {
+ doc: 'Base URL for the Anthropic API.',
+ schema: v.optional(urlSchema),
+ env: `ANTHROPIC_BASE_URL`,
+ default: undefined,
+ showInDocumentation: false,
+ },
+ apiKey: {
+ doc: 'API key for the Anthropic API.',
+ schema: v.string(),
+ env: `ANTHROPIC_API_KEY`,
+ default: '',
+ showInDocumentation: false,
+ },
+} as const satisfies AppConfigDefinition;
diff --git a/apps/papra-server/src/modules/ai/adapters/anthropic/anthropic.ai-adapters.ts b/apps/papra-server/src/modules/ai/adapters/anthropic/anthropic.ai-adapters.ts
new file mode 100644
index 00000000..4a2cd782
--- /dev/null
+++ b/apps/papra-server/src/modules/ai/adapters/anthropic/anthropic.ai-adapters.ts
@@ -0,0 +1,18 @@
+import type { Config } from '../../../config/config.types';
+import type { AiAdapter } from '../ai-adapters.types';
+import { createAnthropicChat } from '@tanstack/ai-anthropic';
+import type { AnthropicChatModel } from '@tanstack/ai-anthropic';
+
+export function buildAnthropicAdapter({ config }: { config: Config }): AiAdapter {
+ const { apiKey, baseUrl } = config.ai.adapters.anthropic;
+
+ return {
+ getTextAdapter: ({ modelName }) => {
+ return createAnthropicChat(
+ modelName as AnthropicChatModel,
+ apiKey,
+ baseUrl ? { baseURL: baseUrl } : undefined,
+ );
+ },
+ };
+}
diff --git a/apps/papra-server/src/modules/ai/ai.config.ts b/apps/papra-server/src/modules/ai/ai.config.ts
index 729c9e09..e4236987 100644
--- a/apps/papra-server/src/modules/ai/ai.config.ts
+++ b/apps/papra-server/src/modules/ai/ai.config.ts
@@ -5,6 +5,7 @@ import * as v from 'valibot';
import { AI_DEFAULT_MODEL_ENV_KEY } from './ai.constants';
import { AI_ADAPTERS_NAMES, AI_DEFAULT_ADAPTER_NAME } from './adapters/ai-adapters.constants';
import { openAiAdaptersConfig } from './adapters/openai-compatible/openai-compatible.config';
+import { anthropicAdaptersConfig } from './adapters/anthropic/anthropic.ai-adapters.config';
export const aiConfig = {
isEnabled: {
@@ -14,7 +15,10 @@ export const aiConfig = {
default: false,
showInDocumentation: false,
},
- adapters: openAiAdaptersConfig,
+ adapters: {
+ anthropic: anthropicAdaptersConfig,
+ ...openAiAdaptersConfig,
+ },
defaultAdapterName: {
doc: `Default AI adapter to use when no specific adapter is specified in the model id. Available adapters: ${AI_ADAPTERS_NAMES.join(', ')}`,
schema: v.picklist(AI_ADAPTERS_NAMES),
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a154ad25..36a34dbe 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -18,6 +18,9 @@ catalogs:
'@paralleldrive/cuid2':
specifier: ^3.3.0
version: 3.3.0
+ '@types/node':
+ specifier: ^26.0.0
+ version: 26.0.1
'@unocss/reset':
specifier: 66.6.0
version: 66.6.0
@@ -155,7 +158,7 @@ importers:
dependencies:
'@better-auth/expo':
specifier: 'catalog:'
'@corentinth/chisels':
specifier: 'catalog:'
version: 2.1.0
@@ -333,7 +336,7 @@ importers:
version: 8.21.3([email protected])
better-auth:
specifier: 'catalog:'
class-variance-authority:
specifier: ^0.7.1
version: 0.7.1
@@ -479,6 +482,9 @@ importers:
'@tanstack/ai':
specifier: ^0.28.0
version: 0.28.0
+ '@tanstack/ai-anthropic':
+ specifier: ^0.15.11
+ version: 0.15.11(@tanstack/[email protected])([email protected])
'@tanstack/ai-openai':
specifier: ^0.14.1
@@ -487,7 +493,7 @@ importers:
better-auth:
specifier: 'catalog:'
busboy:
specifier: ^1.6.0
version: 1.6.0
@@ -898,6 +904,15 @@ packages:
resolution: {integrity: sha512-MGQsmw10ZyI+EJo45CdSER4zEb+p31LpDAFp2Z3gkSd1yqVZGi0Ebx++YTEMonJy4oChEMLsxZ64j8FH6sSqtQ==}
+ '@anthropic-ai/[email protected]':
+ resolution: {integrity: sha512-wOf7AUeJPitcVpvKO4UMu63mWH5SaVipkGd7OOQJt/G6VYGlV8D2Gp9dLxOrttDJh/9gqPqdaBwDGcBevumeAg==}
+ hasBin: true
+ peerDependencies:
+ zod: ^3.25.0 || ^4.0.0
+ peerDependenciesMeta:
+ zod:
+ optional: true
+
'@astrojs/[email protected]':
resolution: {integrity: sha512-A5UW8uIuErLWEoRQvzgXpO1gTjUFtK8r7nU2Z7GewAMxUb7bPvpk11qaKKgxqXlHJWlAvaaxy+Xg28A6bmQ1Tg==}
hasBin: true
@@ -2755,7 +2770,7 @@ packages:
resolution: {integrity: sha512-+Lla7nYSiHZirgK+U/uYzsLv/X+HaJienbD5AKX1UQZHYfWaP+9uuQluRB4GrEVWF0GZ7vEVp/jzaOT9k/SQlg==}
- engines: {node: '>=0.10.0'}
+ engines: {'0': node >=0.10.0}
resolution: {integrity: sha512-hY/OdRaJMs5WsVPuVSZ+RLH3VObJmL/pv5CGCHEZHN2PxZjSZSdctyKV8UcFBXTF0yIKNAJ9XLs1dlNYXHh4Cw==}
@@ -4556,6 +4571,12 @@ packages:
resolution: {integrity: sha512-5lSsMOTXURePglDfvuAQUqkGek9Hg2kksOYay2m0+XR++b2NWYL/4sWyuvVBIs8oKnJaxkdi9whaL/sqN13afw==}
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-4H/5sSNYeR5ToIrc2TtNUHlDfYjgYqbTv8hY5LGfLgJq/cKvU+p/WT5u+MHBZlMnOXk44X23QryDOI78b94WqA==}
+ peerDependencies:
+ '@tanstack/ai': ^0.38.0
+ zod: ^4.0.0
+
'@tanstack/[email protected]':
resolution: {integrity: sha512-Bhla99u9zBDio1Vm2zfFn75wYCaUi2+ijsL6MEMYPfqPK9BhXSTFHFGlo8EDrw0P0LaDI56g05gncVsgxNjpgw==}
@@ -4579,6 +4600,9 @@ packages:
'@tanstack/[email protected]':
resolution: {integrity: sha512-QZNgbhoPhh5snlMf21SuF4QNTJme4aldvBx2ppMefqfMQHBQPFDJl6TxHDNW8voIzQe42rE5U9XfoWrS0K/xFQ==}
+ '@tanstack/[email protected]':
+ resolution: {integrity: sha512-fgbjd5DohL3k5rTWr/KInauLVYMiHVm0cnmTsNrzL3cr4wWhEld5vmFSrjiwUWACAuONjZa+uBXuS+ZSO8fwDQ==}
+
'@tanstack/[email protected]':
resolution: {integrity: sha512-DT02H5CeqtsdH90bb1hx3zIxboiyMjuQPzbj7ov3uVkMWIFjuCjhgJbc7w27AY38ewd4d8G63QHYj4AnMSEB9Q==}
engines: {node: '>=18'}
@@ -7407,6 +7431,10 @@ packages:
resolution: {integrity: sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==}
+ resolution: {integrity: sha512-+DWg8jCJG2TEnpy7kOm/7/AxaYoaRbjVB4LFZLySZlWn8exGs3A4OLJR966cVvU26N7X9TWxl+Jsw7dzAqKT6g==}
+ engines: {node: '>=16'}
+
resolution: {integrity: sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==}
@@ -9761,6 +9789,9 @@ packages:
resolution: {integrity: sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw==}
+ resolution: {integrity: sha512-FPAhNPFMrkwz76P7cdjdmiShwMynZYN6SgOujD1urY4oNm80Ou9oMdmbR45LotcKOXoy7wSmHkRFE6Mxbrhefw==}
+
resolution: {integrity: sha512-2qxI/FZVDPbzh63GwWIZYE7daWKtwXZYuyc8YNq0iTmMUwn4mL0jRLsp6hfFlgbdRSR4x2ppe+E86FnvEpN7Nw==}
engines: {node: '>=14.13.1'}
@@ -10721,6 +10752,13 @@ snapshots:
package-manager-detector: 1.6.0
tinyexec: 1.2.4
+ dependencies:
+ json-schema-to-ts: 3.1.1
+ standardwebhooks: 1.0.0
+ optionalDependencies:
+ zod: 4.4.3
+
dependencies:
'@astrojs/language-server': 2.16.10([email protected])([email protected])
@@ -11833,7 +11871,7 @@ snapshots:
'@bcoe/[email protected]': {}
dependencies:
'@better-auth/utils': 0.4.0
'@better-fetch/fetch': 1.1.21
@@ -11845,6 +11883,25 @@ snapshots:
nanostores: 1.3.0
zod: 4.4.3
+ dependencies:
+ '@better-auth/utils': 0.4.0
+ '@better-fetch/fetch': 1.1.21
+ '@opentelemetry/semantic-conventions': 1.41.1
+ '@standard-schema/spec': 1.1.0
+ better-call: 1.3.5([email protected])
+ jose: 6.2.3
+ kysely: 0.28.17
+ nanostores: 1.3.0
+ zod: 4.4.3
+
+ dependencies:
+ '@better-auth/utils': 0.4.0
+ optionalDependencies:
+ drizzle-orm: 0.45.2(@libsql/[email protected])([email protected])
+
dependencies:
@@ -11852,9 +11909,9 @@ snapshots:
optionalDependencies:
drizzle-orm: 0.45.2(@libsql/[email protected])([email protected])
dependencies:
'@better-fetch/fetch': 1.1.21
better-call: 1.3.5([email protected])
@@ -11878,6 +11935,13 @@ snapshots:
expo-network: 8.0.8([email protected])([email protected])
+ dependencies:
+ '@better-auth/utils': 0.4.0
+ optionalDependencies:
+ kysely: 0.28.17
+
dependencies:
@@ -11885,21 +11949,42 @@ snapshots:
optionalDependencies:
kysely: 0.28.17
+ dependencies:
+ '@better-auth/utils': 0.4.0
+
dependencies:
'@better-auth/utils': 0.4.0
+ dependencies:
+ '@better-auth/utils': 0.4.0
+
dependencies:
'@better-auth/utils': 0.4.0
+ dependencies:
+ '@better-auth/utils': 0.4.0
+
dependencies:
'@better-auth/utils': 0.4.0
+ dependencies:
+ '@better-auth/utils': 0.4.0
+ '@better-fetch/fetch': 1.1.21
+
dependencies:
@@ -14712,6 +14797,13 @@ snapshots:
dependencies:
tslib: 2.8.1
+ dependencies:
+ '@anthropic-ai/sdk': 0.97.1([email protected])
+ '@tanstack/ai': 0.28.0
+ '@tanstack/ai-utils': 0.3.1
+ zod: 4.4.3
+
'@tanstack/[email protected]':
dependencies:
'@tanstack/ai': 0.31.0
@@ -14742,6 +14834,8 @@ snapshots:
'@tanstack/[email protected]': {}
+ '@tanstack/[email protected]': {}
+
'@tanstack/[email protected]':
dependencies:
'@ag-ui/core': 0.0.52
@@ -14986,7 +15080,7 @@ snapshots:
dependencies:
- '@types/node': 24.13.2
+ '@types/node': 26.0.1
dependencies:
@@ -15960,7 +16054,7 @@ snapshots:
dependencies:
tweetnacl: 0.14.5
dependencies:
@@ -15973,7 +16067,37 @@ snapshots:
'@better-fetch/fetch': 1.1.21
'@noble/ciphers': 2.2.0
'@noble/hashes': 2.0.1
- better-call: 1.3.5([email protected])
+ better-call: 1.3.5([email protected])
+ defu: 6.1.4
+ jose: 6.2.3
+ kysely: 0.28.17
+ nanostores: 1.3.0
+ zod: 4.4.3
+ optionalDependencies:
+ drizzle-kit: 0.31.10
+ drizzle-orm: 0.45.2(@libsql/[email protected])([email protected])
+ react: 19.2.7
+ react-dom: 19.2.7([email protected])
+ solid-js: 1.9.13
+ transitivePeerDependencies:
+ - '@cloudflare/workers-types'
+ - '@opentelemetry/api'
+
+ dependencies:
+ '@better-auth/memory-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
+ '@better-auth/mongo-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
+ '@better-auth/prisma-adapter': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])
+ '@better-auth/telemetry': 1.6.11(@better-auth/[email protected](@better-auth/[email protected])(@better-fetch/[email protected])([email protected]([email protected]))([email protected])([email protected])([email protected]))(@better-auth/[email protected])(@better-fetch/[email protected])
+ '@better-auth/utils': 0.4.0
+ '@better-fetch/fetch': 1.1.21
+ '@noble/ciphers': 2.2.0
+ '@noble/hashes': 2.0.1
+ better-call: 1.3.5([email protected])
defu: 6.1.4
jose: 6.2.3
kysely: 0.28.17
@@ -18253,6 +18377,11 @@ snapshots:
+ dependencies:
+ '@babel/runtime': 7.27.0
+ ts-algebra: 2.0.0
+
@@ -21351,6 +21480,8 @@ snapshots:
+