c7a75d069d
* feat(ai): job-shaped AI service with OpenAI-compatible backend, extraction-first; stop extracting every inbox document twice Sovereign plan WS1 PR1 (#1406 Tier 2, extraction-first, aligned with the AI surface audit). lib/ai grows a job-shaped service (generateText / generateStructured / extractFromDocument; no streaming members yet, see plan rule R3): - services/anthropic-family delegates to the existing createAiClient() and sends the exact request literals the inbox extractor sent before (request-shape tests deep-equal them), so hosted Bedrock stays byte-identical. - services/openai-compatible talks to any chat-completions endpoint (BYO Swedish provider) via Vercel AI SDK 6.x, exact-pinned and guarded: images as parts, PDFs rasterized with poppler (AI_PDF_MODE) or sent natively, AI_VISION / AI_STRICT_JSON declared, honest skips (ai_no_vision, pdf_rasterizer_missing) instead of fake failures. - config.ts: AI_PROVIDER/AI_BASE_URL/AI_API_KEY/AI_MODEL and per-tier AI_*_MODEL with the legacy BEDROCK_* names kept as the same overrides; getAiStatus() is the single source of truth for "is AI wired up". - provider.ts: openai-compatible in the auto-detect chain (after Bedrock and the direct API); createAiClient() refuses it loudly. Document extraction moves onto the service and gets the audit's fixes: - Inbox documents were extracted TWICE (pipeline A ran inside uploadDocument() before the inbox row existed, so its dedupe branch never fired; 3 707 + 1 666 calls / 30 d). The inbox now declares extractionOwner on the upload, the extension yields, and the inbox mirrors its single outcome onto document_attachments from every writer (sync, deferred, attach, retry, MCP). - Every "no extraction will ever happen" outcome is stamped (skipped:no_ai_entitlement / ai_unconfigured / system_generated / ...); the status route maps the quiet ones to 'disabled' on the first poll instead of a 30 s client timeout. Prod showed 309 of the 327 never-extracted uploads were the paywall working silently. - Self-generated documents (our own invoice PDFs, payout files) are no longer OCR'd. - Agent invoke answers 503 ai_unconfigured when the deployment has no assistant backend, distinct from the paywall. Guard: new direct-ai-client antipattern check (shrink-only allowlist of the pre-abstraction SDK callers) plus exact pins for @anthropic-ai/sdk, ai and @ai-sdk/openai-compatible. Verified: 15 958 unit tests green, guards, lint ratchet, typecheck, and a live smoke against hosted Bedrock through the new service (ping, streamed tool turn, thinking+cache, PDF extraction). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(ai): make AI_API_KEY optional for OpenAI-compatible endpoints (keyless local model servers) A local model server (llama.cpp's server, Ollama /v1, LM Studio, vLLM) usually has no auth. Before, the OpenAI-compatible backend required both AI_BASE_URL and AI_API_KEY to count as configured, so running Accounted on a local model meant setting a meaningless placeholder key. - resolveAiProvider / hasAiCredentials: a base URL alone is now enough. - services/openai-compatible: only send Authorization: Bearer when AI_API_KEY is set, so a keyless server is never handed an empty bearer; a hosted provider that needs a key still sets it. - Docs (SELF-HOSTING Option 3: local-model example, key marked optional), DECISIONS. Verified: with no AI_API_KEY, just AI_BASE_URL + AI_MODEL, getAiStatus() reports configured=true / provider=openai-compatible (live). lib/ai suite 71 green; tsc, guards, lint clean. Bedrock/Anthropic logic unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
191 lines
6.4 KiB
TypeScript
191 lines
6.4 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import Anthropic from '@anthropic-ai/sdk'
|
|
import AnthropicBedrock from '@anthropic-ai/bedrock-sdk'
|
|
import {
|
|
aiCredentialPrefix,
|
|
createAiClient,
|
|
hasAiCredentials,
|
|
resolveAiProvider,
|
|
toProviderModelId,
|
|
} from '../provider'
|
|
|
|
const AI_ENV_KEYS = [
|
|
'AI_PROVIDER',
|
|
'AI_BASE_URL',
|
|
'AI_API_KEY',
|
|
'ANTHROPIC_API_KEY',
|
|
'AWS_ACCESS_KEY_ID',
|
|
'AWS_SECRET_ACCESS_KEY',
|
|
'AWS_REGION',
|
|
] as const
|
|
|
|
let saved: Partial<Record<(typeof AI_ENV_KEYS)[number], string | undefined>> = {}
|
|
|
|
beforeEach(() => {
|
|
saved = {}
|
|
for (const key of AI_ENV_KEYS) {
|
|
saved[key] = process.env[key]
|
|
delete process.env[key]
|
|
}
|
|
})
|
|
|
|
afterEach(() => {
|
|
for (const key of AI_ENV_KEYS) {
|
|
if (saved[key] === undefined) delete process.env[key]
|
|
else process.env[key] = saved[key]
|
|
}
|
|
})
|
|
|
|
describe('resolveAiProvider', () => {
|
|
it('uses Bedrock when static AWS keys are set (hosted stays unchanged)', () => {
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIAEXAMPLE'
|
|
process.env.AWS_SECRET_ACCESS_KEY = 'secret'
|
|
expect(resolveAiProvider()).toBe('bedrock')
|
|
})
|
|
|
|
it('uses the direct API when only an Anthropic key is set (self-hosted)', () => {
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
expect(resolveAiProvider()).toBe('anthropic')
|
|
})
|
|
|
|
// The load-bearing precedence case: an operator who adds an Anthropic key
|
|
// for a side experiment must not silently move production inference out of
|
|
// eu-north-1, which is a deliberate BFL/GDPR posture rather than a default.
|
|
it('prefers Bedrock when both credential sets are present', () => {
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIAEXAMPLE'
|
|
process.env.AWS_SECRET_ACCESS_KEY = 'secret'
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
expect(resolveAiProvider()).toBe('bedrock')
|
|
})
|
|
|
|
it('lets AI_PROVIDER override the credential-based guess, both ways', () => {
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIAEXAMPLE'
|
|
process.env.AWS_SECRET_ACCESS_KEY = 'secret'
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
|
|
process.env.AI_PROVIDER = 'anthropic'
|
|
expect(resolveAiProvider()).toBe('anthropic')
|
|
|
|
process.env.AI_PROVIDER = 'bedrock'
|
|
expect(resolveAiProvider()).toBe('bedrock')
|
|
})
|
|
|
|
it('accepts AI_PROVIDER case-insensitively and trimmed', () => {
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
process.env.AI_PROVIDER = ' Bedrock '
|
|
expect(resolveAiProvider()).toBe('bedrock')
|
|
})
|
|
|
|
it('ignores an unrecognised AI_PROVIDER rather than failing closed', () => {
|
|
process.env.AI_PROVIDER = 'openai'
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
expect(resolveAiProvider()).toBe('anthropic')
|
|
})
|
|
|
|
// Hosted infrastructure that injects credentials via instance profile / IRSA
|
|
// sets no env vars at all: that must still resolve to Bedrock so the AWS
|
|
// credential provider chain gets its chance.
|
|
it('falls back to Bedrock when nothing is configured', () => {
|
|
expect(resolveAiProvider()).toBe('bedrock')
|
|
})
|
|
})
|
|
|
|
describe('hasAiCredentials', () => {
|
|
it('is true for a complete AWS static key pair', () => {
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIAEXAMPLE'
|
|
process.env.AWS_SECRET_ACCESS_KEY = 'secret'
|
|
expect(hasAiCredentials()).toBe(true)
|
|
})
|
|
|
|
it('is false for a half-configured AWS key pair', () => {
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIAEXAMPLE'
|
|
expect(hasAiCredentials()).toBe(false)
|
|
})
|
|
|
|
it('is true for an OpenAI-compatible base URL with no key (keyless local server)', () => {
|
|
process.env.AI_BASE_URL = 'http://localhost:11434/v1'
|
|
expect(hasAiCredentials()).toBe(true)
|
|
})
|
|
|
|
it('is true for an Anthropic key', () => {
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
expect(hasAiCredentials()).toBe(true)
|
|
})
|
|
|
|
it('is false when nothing is configured', () => {
|
|
expect(hasAiCredentials()).toBe(false)
|
|
})
|
|
|
|
// AI_PROVIDER names the backend but does not conjure a credential for it.
|
|
it('is false when AI_PROVIDER names a backend with no key', () => {
|
|
process.env.AI_PROVIDER = 'anthropic'
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIAEXAMPLE'
|
|
process.env.AWS_SECRET_ACCESS_KEY = 'secret'
|
|
expect(hasAiCredentials()).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('createAiClient', () => {
|
|
it('builds a Bedrock client when AWS keys are set', () => {
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIAEXAMPLE'
|
|
process.env.AWS_SECRET_ACCESS_KEY = 'secret'
|
|
expect(createAiClient()).toBeInstanceOf(AnthropicBedrock)
|
|
})
|
|
|
|
it('builds a direct Anthropic client when only an Anthropic key is set', () => {
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
expect(createAiClient()).toBeInstanceOf(Anthropic)
|
|
})
|
|
|
|
it('builds a Bedrock client for the AWS provider chain when nothing is set', () => {
|
|
expect(createAiClient()).toBeInstanceOf(AnthropicBedrock)
|
|
})
|
|
})
|
|
|
|
describe('toProviderModelId', () => {
|
|
it('passes the bare id through for the direct API', () => {
|
|
expect(toProviderModelId('claude-sonnet-5', 'anthropic')).toBe('claude-sonnet-5')
|
|
})
|
|
|
|
it('adds the eu inference-profile prefix for Bedrock', () => {
|
|
expect(toProviderModelId('claude-sonnet-5', 'bedrock')).toBe('eu.anthropic.claude-sonnet-5')
|
|
})
|
|
|
|
// An operator override may already be written in provider form; prefixing it
|
|
// again would produce eu.anthropic.eu.anthropic.… and 404 at call time.
|
|
it('leaves an already-prefixed id alone', () => {
|
|
expect(toProviderModelId('eu.anthropic.claude-sonnet-5', 'bedrock')).toBe(
|
|
'eu.anthropic.claude-sonnet-5'
|
|
)
|
|
expect(toProviderModelId('anthropic.claude-sonnet-5', 'bedrock')).toBe(
|
|
'anthropic.claude-sonnet-5'
|
|
)
|
|
})
|
|
|
|
it('defaults to the environment-resolved provider', () => {
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-example'
|
|
expect(toProviderModelId('claude-sonnet-5')).toBe('claude-sonnet-5')
|
|
})
|
|
})
|
|
|
|
describe('aiCredentialPrefix', () => {
|
|
it('returns only the non-secret Anthropic key prefix', () => {
|
|
process.env.ANTHROPIC_API_KEY = 'sk-ant-api03-SECRETSECRETSECRET'
|
|
const prefix = aiCredentialPrefix()
|
|
expect(prefix).toBe('sk-ant-api03')
|
|
expect(prefix).not.toContain('SECRET')
|
|
})
|
|
|
|
it('returns only the AWS access-key-id class prefix', () => {
|
|
process.env.AWS_ACCESS_KEY_ID = 'AKIASECRETSECRET'
|
|
process.env.AWS_SECRET_ACCESS_KEY = 'secret'
|
|
const prefix = aiCredentialPrefix()
|
|
expect(prefix).toBe('AKIA')
|
|
expect(prefix).not.toContain('SECRET')
|
|
})
|
|
|
|
it('returns null when nothing is configured', () => {
|
|
expect(aiCredentialPrefix()).toBeNull()
|
|
})
|
|
})
|