3829b6add3
* feat(ai): resolve the Claude backend from the environment Tier 1 of #1406: a self-hosted deployment can now run every AI feature on a plain ANTHROPIC_API_KEY, with no AWS account. Hosted behaviour is unchanged. lib/ai/provider.ts resolves the backend once, from the environment: AI_PROVIDER explicit override, bedrock|anthropic AWS static key pair Bedrock ANTHROPIC_API_KEY the direct Anthropic API nothing set Bedrock, so the AWS credential provider chain (instance profile, IRSA) still resolves Bedrock deliberately wins when both credential sets are present. EU residency in eu-north-1 is a BFL/GDPR posture rather than a default, so adding an Anthropic key for an experiment must not silently move production inference out of the region. AI_PROVIDER is the way to say you meant it. Model ids are written bare in code and prefixed to eu.anthropic.* only for Bedrock, which needs the cross-region inference profile for on-demand throughput. An operator override that already carries a prefix passes through untouched, so BEDROCK_MODEL_ID and friends keep working as written. Converted call sites: the agent composer, invoice-inbox extraction, the document-extraction model label, and both receipt-hunt clients. The last two are not named in the issue, which predates receipt-hunt landing in main. @anthropic-ai/sdk is declared at 0.95.0, the version @anthropic-ai/bedrock-sdk 0.29.1 already pulled in transitively, so the lockfile dedupes to one copy with no new download. scripts/smoke-bedrock.ts becomes scripts/smoke-ai.ts and grows two steps. Unit tests can only prove which provider and model id get resolved; they cannot prove the resulting request is one the backend accepts. The script now sends real traffic over all three shapes the app uses: a plain create, a streamed turn carrying adaptive thinking, an effort level, an hour-long cache breakpoint and a tool, and document extraction end to end when given a file. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> * docs(self-hosting): document the AI smoke test The script added alongside the provider split is what closes the #1406 acceptance criterion ("document extraction and the assistant both work"), so a self-hoster needs to know it exists. Covers both invocations and states that it exits non-zero, which is what makes it usable as a post-deploy check. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> * test(ai): split the smoke test's thinking probe from its tool probe The combined probe could not falsify what it claimed to. It asked a question that needs a tool call, so the tool was used and adaptive thinking correctly declined to reason about it: the zero thinking-block count that came back was uninformative rather than a signal. 2a keeps the tool and drops thinking. 2b asks a question with several dependent steps (reverse charge, then a partial deduction, then the affected boxes) so that a model honouring the parameter must reason, and reports the thinking text length as well as the block count, since display:"summarized" can yield blocks with empty text. The cached system prompt is also padded past the 1024-token minimum cacheable prefix. Below that the API caches nothing and reports no error, so the old probe's cache counters read zero whether or not caching worked. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> * fix(document-extraction): stop requiring AWS_REGION in the manifest The extension now needs one of two credential sets, AWS static keys or ANTHROPIC_API_KEY, and the manifest schema cannot express "one of". Since requiredEnvVars only drives a build-time warning and never gates anything, listing AWS_REGION told every self-hoster running the direct API to set a variable that has no effect for them. The description was also still promising Sonnet 4.6 via Bedrock specifically, which is no longer what the extension does. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> * fix(ai): read documentKind defensively in the smoke test The field arrived with the receipt-aware extraction work, so referencing it directly stops the script compiling against any checkout from before that landed. tsconfig includes **/*.ts and next.config does not disable type checking, so on such a checkout this failed the production build rather than just the script: caught while preparing a test branch for a self-hosted instance that had not synced yet. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> * fix(deps): restore the nested @swc/helpers entry in the lockfile Declaring @anthropic-ai/sdk with `npm install --package-lock-only` also pruned node_modules/next-intl/node_modules/@swc/helpers@0.5.23, an optional peer entry the local npm 11 considers redundant and the image's npm 10.9.8 does not. The result passed every local check and failed `npm ci` inside the Docker build, which is the only place the lockfile is actually enforced. The lockfile is now the previous one plus the single root dependency line, verified with `npm ci --dry-run`. @anthropic-ai/sdk needed nothing else: it was already in the tree as a transitive dependency of @anthropic-ai/bedrock-sdk. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> * Update DECISIONS.md Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update Docker documentation for AI provider credentials Clarify the role of credentials in AI provider selection and document extraction requirements. * Update SELF-HOSTING.md with smoke-ai script details Clarify usage of smoke-ai script for credential checks and document extraction. * Improve error handling and logging in smoke-ai script * fix(ai): complete plain-key self-hosting path Signed-off-by: Emil <emilmattsson14@gmail.com> --------- Signed-off-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> Signed-off-by: Emil <emilmattsson14@gmail.com> Co-authored-by: Bjorn Bergenheim <29535152+bjornbergenheim@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
118 lines
4.9 KiB
TypeScript
118 lines
4.9 KiB
TypeScript
import Anthropic from '@anthropic-ai/sdk'
|
|
import AnthropicBedrock from '@anthropic-ai/bedrock-sdk'
|
|
|
|
/**
|
|
* Which backend Claude traffic goes to.
|
|
*
|
|
* Hosted runs on AWS Bedrock: keeping inference inside eu-north-1 is a
|
|
* deliberate BFL/GDPR posture for Swedish accounting data, not an
|
|
* implementation detail. Self-hosted deployments generally have no AWS
|
|
* account at all, so they get the direct Anthropic API with a plain
|
|
* ANTHROPIC_API_KEY.
|
|
*
|
|
* See https://github.com/erp-mafia/accounted/issues/1406.
|
|
*/
|
|
export type AiProvider = 'bedrock' | 'anthropic'
|
|
|
|
export type AiClient = Anthropic | AnthropicBedrock
|
|
|
|
/**
|
|
* Resolve the provider from the environment, or null when neither backend has
|
|
* usable credentials.
|
|
*
|
|
* Precedence is deliberate:
|
|
*
|
|
* 1. `AI_PROVIDER` wins when set. The escape hatch for a deployment that has
|
|
* both credential sets and needs to say which one it means.
|
|
* 2. Static AWS keys mean Bedrock. This is what keeps hosted byte-identical:
|
|
* an operator who adds an Anthropic key for a side experiment must not
|
|
* silently move production inference out of eu-north-1.
|
|
* 3. Otherwise an Anthropic key means the direct API. This is the
|
|
* self-hosted path.
|
|
* 4. Otherwise Bedrock without static keys, so the AWS credential provider
|
|
* chain (instance profile, IRSA, EKS pod identity) still resolves on
|
|
* hosted infrastructure that injects credentials rather than setting env
|
|
* vars. `hasAiCredentials()` reports false here: we cannot see the chain
|
|
* from this side, so callers that need a cheap pre-flight treat it as
|
|
* unconfigured rather than paying a request to find out.
|
|
*/
|
|
export function resolveAiProvider(): AiProvider {
|
|
const explicit = (process.env.AI_PROVIDER ?? '').trim().toLowerCase()
|
|
if (explicit === 'bedrock' || explicit === 'anthropic') return explicit
|
|
|
|
if (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) return 'bedrock'
|
|
if (process.env.ANTHROPIC_API_KEY) return 'anthropic'
|
|
return 'bedrock'
|
|
}
|
|
|
|
/**
|
|
* Whether this deployment has credentials we can see from the environment.
|
|
*
|
|
* Used by call sites that must degrade quietly rather than throw: document
|
|
* extraction returns an empty result instead of failing an upload. Returns
|
|
* false for the AWS provider chain (case 4 above) because it is not visible
|
|
* here; that path was already treated the same way before the direct-API
|
|
* option existed.
|
|
*/
|
|
export function hasAiCredentials(): boolean {
|
|
return resolveAiProvider() === 'anthropic'
|
|
? !!process.env.ANTHROPIC_API_KEY
|
|
: !!(process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY)
|
|
}
|
|
|
|
/**
|
|
* Build a client for the resolved provider. Both expose the same
|
|
* `messages.create` / `messages.stream` surface, which is all this codebase
|
|
* uses of either SDK.
|
|
*/
|
|
export function createAiClient(): AiClient {
|
|
if (resolveAiProvider() === 'anthropic') {
|
|
const apiKey = process.env.ANTHROPIC_API_KEY
|
|
// Omit the key when unset so the SDK resolves it itself and fails at call
|
|
// time: throwing here would take down every route that merely imports a
|
|
// module touching AI.
|
|
return apiKey ? new Anthropic({ apiKey }) : new Anthropic()
|
|
}
|
|
|
|
const awsRegion = process.env.AWS_REGION || 'eu-north-1'
|
|
const awsAccessKey = process.env.AWS_ACCESS_KEY_ID
|
|
const awsSecretKey = process.env.AWS_SECRET_ACCESS_KEY
|
|
// When both static keys are present, pass them. Otherwise omit them so the
|
|
// SDK falls back to the AWS credential provider chain. The two-overload SDK
|
|
// refuses a mix.
|
|
return awsAccessKey && awsSecretKey
|
|
? new AnthropicBedrock({ awsRegion, awsAccessKey, awsSecretKey })
|
|
: new AnthropicBedrock({ awsRegion })
|
|
}
|
|
|
|
/**
|
|
* Map a bare Anthropic model id to the form the resolved provider expects.
|
|
*
|
|
* Bedrock needs the `eu.` inference-profile prefix: a bare
|
|
* `anthropic.claude-sonnet-5` is rejected because on-demand throughput
|
|
* requires the cross-region profile. The direct API takes the bare id and
|
|
* rejects both prefixes.
|
|
*
|
|
* Ids that already carry a provider prefix pass through untouched, so an
|
|
* operator-supplied override in either form keeps working.
|
|
*/
|
|
export function toProviderModelId(bareModelId: string, provider = resolveAiProvider()): string {
|
|
if (provider === 'anthropic') return bareModelId
|
|
if (bareModelId.startsWith('eu.') || bareModelId.startsWith('anthropic.')) return bareModelId
|
|
return `eu.anthropic.${bareModelId}`
|
|
}
|
|
|
|
/**
|
|
* Non-secret identification of the configured credential, for startup logs.
|
|
* Anthropic keys carry a public prefix (`sk-ant-api03` for a standard API key,
|
|
* `sk-ant-oat` for an OAuth token); AWS access key ids carry `AKIA` for a
|
|
* long-term IAM user key and `ASIA` for an STS/role credential. Never returns
|
|
* any part of a secret.
|
|
*/
|
|
export function aiCredentialPrefix(): string | null {
|
|
if (resolveAiProvider() === 'anthropic') {
|
|
return process.env.ANTHROPIC_API_KEY?.slice(0, 12) ?? null
|
|
}
|
|
return process.env.AWS_ACCESS_KEY_ID?.slice(0, 4) ?? null
|
|
}
|