Files
accounted/lib/hooks/use-document-extraction.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

97 lines
2.8 KiB
TypeScript

'use client'
import { useEffect, useState } from 'react'
// Polls GET /api/documents/:id/extraction-status until the AI extraction
// pipeline completes, fails, or times out. Returns the derived status the
// upload UI binds to.
//
// "Disabled" semantics: the server answers 'disabled' as soon as it knows
// no extraction will happen (AI not configured on this deployment, company
// not entitled, self-generated document: see the route), so the UI can
// quietly fall back ("Uppladdat" without an AI hint) on the first poll: no
// scary error for a feature the customer didn't pay for, and no 30 s hang
// on a self-host without an AI key. The client-side timeout stays as the
// last resort for the one case the server cannot see: the
// document-extraction extension switched off entirely (the column stays
// NULL forever).
//
// Reasonable timeout: typical extraction takes 2-8s on Sonnet via Bedrock.
// 30s is generous and keeps the UX responsive on flaky links.
const POLL_INTERVAL_MS = 1500
const EXTRACTION_TIMEOUT_MS = 30_000
export type ExtractionStatus =
| 'idle'
| 'running'
| 'succeeded'
| 'failed'
| 'unsupported'
| 'disabled'
interface State {
status: ExtractionStatus
// Hint to consumers: how long we've been polling. Lets the UI swap the
// copy after a few seconds ("Läser fakturan…" → "Tar lite längre än
// vanligt…") without re-rendering.
elapsedMs: number
}
export function useDocumentExtraction(documentId: string | null | undefined): State {
const [state, setState] = useState<State>({ status: 'idle', elapsedMs: 0 })
useEffect(() => {
if (!documentId) {
setState({ status: 'idle', elapsedMs: 0 })
return
}
let cancelled = false
const startedAt = Date.now()
setState({ status: 'running', elapsedMs: 0 })
async function tick(): Promise<void> {
if (cancelled) return
const elapsedMs = Date.now() - startedAt
if (elapsedMs > EXTRACTION_TIMEOUT_MS) {
setState({ status: 'disabled', elapsedMs })
return
}
try {
const res = await fetch(`/api/documents/${documentId}/extraction-status`)
if (cancelled) return
if (res.ok) {
const json = (await res.json()) as {
data: { status: ExtractionStatus }
}
const status = json.data.status
if (status !== 'running') {
setState({ status, elapsedMs })
return
}
setState({ status: 'running', elapsedMs })
}
// Non-ok responses fall through to retry; transient 5xx shouldn't
// collapse the UI to "failed".
} catch {
// Network blip: keep polling.
}
setTimeout(() => {
if (!cancelled) void tick()
}, POLL_INTERVAL_MS)
}
void tick()
return () => {
cancelled = true
}
}, [documentId])
return state
}