Files
accounted/lib/ai/config.ts
T
Jakob Wennberg c7a75d069d feat(ai): job-shaped AI service with OpenAI-compatible backend, extraction-first; stop extracting every inbox document twice (#1740)
* 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>
2026-08-20 19:39:08 +02:00

181 lines
6.7 KiB
TypeScript

import {
hasAiCredentials,
resolveAiProvider,
toProviderModelId,
type AiProvider,
} from './provider'
import type { AiCapabilities, AiPdfMode, AiStatus, AiTier } from './types'
/**
* Environment parsing for the AI layer. Read on every call (cheap string
* reads) so tests and the smoke script see env changes; the service cache in
* lib/ai/index.ts keys on the resolved config, not on this module's state.
*
* Variables (all optional; legacy BEDROCK_* names keep working):
*
* AI_PROVIDER bedrock | anthropic | openai-compatible (else auto-detect)
* AI_BASE_URL OpenAI-compatible endpoint (Swedish provider or a local model)
* AI_API_KEY optional: only when that endpoint requires auth
* AI_MODEL default model id for every tier
* AI_ASSISTANT_MODEL per-tier overrides (fallbacks: BEDROCK_SONNET_MODEL_ID,
* AI_HEAVY_MODEL BEDROCK_OPUS_MODEL_ID, BEDROCK_MODEL_ID respectively)
* AI_EXTRACTION_MODEL
* AI_EXTRACTION_MAX_TOKENS output cap for document extraction (fallback BEDROCK_MAX_TOKENS, then 8192)
* AI_VISION OpenAI-compatible only: the configured models accept images (default true)
* AI_STRICT_JSON OpenAI-compatible only: use response_format json_schema (default false)
* AI_PDF_MODE auto | native | rasterize (auto = native on Claude, rasterize elsewhere)
* AI_PDF_MAX_PAGES pages rasterized per PDF (default 4)
*/
export interface ResolvedAiConfig {
provider: AiProvider
/** Credentials present (and, for OpenAI-compatible, a model id). */
configured: boolean
reason: AiStatus['reason']
baseUrl: string | null
apiKey: string | null
/** Bare model ids per tier (null only when OpenAI-compatible has none configured). */
models: Record<AiTier, string | null>
extractionMaxTokens: number
vision: boolean
strictJson: boolean
pdfMode: AiPdfMode
pdfMaxPages: number
}
const DEFAULT_CLAUDE_MODEL = 'claude-sonnet-5'
const DEFAULT_EXTRACTION_MAX_TOKENS = 8192
const DEFAULT_PDF_MAX_PAGES = 4
function env(name: string): string | null {
const v = process.env[name]
if (v === undefined) return null
const trimmed = v.trim()
return trimmed.length > 0 ? trimmed : null
}
function envBool(name: string, fallback: boolean): boolean {
const v = env(name)?.toLowerCase()
if (v === null || v === undefined) return fallback
if (v === 'true' || v === '1' || v === 'yes' || v === 'on') return true
if (v === 'false' || v === '0' || v === 'no' || v === 'off') return false
return fallback
}
// Use the env value only if it's a positive number: `||` would also fall back
// on a deliberate `0`, masking what is really an invalid configuration rather
// than the intent to disable.
function envPositiveInt(name: string): number | null {
const raw = env(name)
if (raw === null) return null
const parsed = Number(raw)
return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : null
}
const LEGACY_TIER_VARS: Record<AiTier, string> = {
assistant: 'BEDROCK_SONNET_MODEL_ID',
heavy: 'BEDROCK_OPUS_MODEL_ID',
extraction: 'BEDROCK_MODEL_ID',
}
const TIER_VARS: Record<AiTier, string> = {
assistant: 'AI_ASSISTANT_MODEL',
heavy: 'AI_HEAVY_MODEL',
extraction: 'AI_EXTRACTION_MODEL',
}
/**
* Model id for a tier, most specific wins: AI_<TIER>_MODEL, then the legacy
* Bedrock-era tier variable, then AI_MODEL, then the Claude default on the
* Anthropic family. An OpenAI-compatible endpoint has no sane default model,
* so the result is null there and the status reports `no_model`.
*/
export function resolveTierModel(tier: AiTier, provider: AiProvider = resolveAiProvider()): string | null {
const specific = env(TIER_VARS[tier]) ?? env(LEGACY_TIER_VARS[tier]) ?? env('AI_MODEL')
if (specific) return specific
return provider === 'openai-compatible' ? null : DEFAULT_CLAUDE_MODEL
}
export function readAiConfig(): ResolvedAiConfig {
const provider = resolveAiProvider()
const models: Record<AiTier, string | null> = {
assistant: resolveTierModel('assistant', provider),
heavy: resolveTierModel('heavy', provider),
extraction: resolveTierModel('extraction', provider),
}
const credentials = hasAiCredentials()
const isOpenAiCompatible = provider === 'openai-compatible'
const hasModels = !isOpenAiCompatible || Object.values(models).every((m) => m !== null)
const pdfModeRaw = env('AI_PDF_MODE')?.toLowerCase()
const pdfMode: AiPdfMode =
pdfModeRaw === 'native' || pdfModeRaw === 'rasterize'
? pdfModeRaw
: isOpenAiCompatible
? 'rasterize'
: 'native'
return {
provider,
configured: credentials && hasModels,
reason: !credentials ? 'no_credentials' : !hasModels ? 'no_model' : 'ok',
baseUrl: isOpenAiCompatible ? env('AI_BASE_URL') : null,
apiKey: isOpenAiCompatible ? env('AI_API_KEY') : null,
models,
extractionMaxTokens:
envPositiveInt('AI_EXTRACTION_MAX_TOKENS') ??
envPositiveInt('BEDROCK_MAX_TOKENS') ??
DEFAULT_EXTRACTION_MAX_TOKENS,
vision: envBool('AI_VISION', true),
strictJson: envBool('AI_STRICT_JSON', false),
pdfMode,
pdfMaxPages: envPositiveInt('AI_PDF_MAX_PAGES') ?? DEFAULT_PDF_MAX_PAGES,
}
}
export function capabilitiesFor(cfg: ResolvedAiConfig): AiCapabilities {
if (cfg.provider === 'openai-compatible') {
return {
pdfNative: cfg.pdfMode === 'native',
imageInput: cfg.vision,
toolUse: true,
forcedToolChoice: false,
strictJsonSchema: cfg.strictJson,
}
}
return {
pdfNative: true,
imageInput: true,
toolUse: true,
forcedToolChoice: true,
strictJsonSchema: false,
}
}
/**
* Single source of truth for "is AI wired up here, and for what". Cheap: env
* reads only, no network. Drives the extraction fail-fast path (a document is
* stamped `skipped:ai_unconfigured` instead of waiting 30 s for nothing), the
* status route, agent routes (503 instead of a stream that dies), and the
* smoke script.
*/
export function getAiStatus(): AiStatus {
const cfg = readAiConfig()
const capabilities = capabilitiesFor(cfg)
const models: Record<AiTier, string | null> = {
assistant: cfg.models.assistant ? toProviderModelId(cfg.models.assistant, cfg.provider) : null,
heavy: cfg.models.heavy ? toProviderModelId(cfg.models.heavy, cfg.provider) : null,
extraction: cfg.models.extraction ? toProviderModelId(cfg.models.extraction, cfg.provider) : null,
}
return {
provider: cfg.provider,
configured: cfg.configured,
reason: cfg.reason,
capabilities,
models,
pdfMode: cfg.pdfMode,
// The chat loop still speaks the Anthropic messages surface directly.
assistantAvailable: cfg.configured && cfg.provider !== 'openai-compatible',
}
}