Files
accounted/lib/ai/__tests__/rasterize-pdf.test.ts
T
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

77 lines
3.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { writeFile } from 'node:fs/promises'
// Fake pdftoppm: the module shells out with (-r dpi -png -f 1 -l N input prefix)
// and reads back <prefix>-<n>.png. The fake writes those files itself, so the
// read-back, ordering and cleanup paths run for real on a temp dir.
const execFileMock = vi.fn()
vi.mock('node:child_process', () => ({
execFile: (...args: unknown[]) => execFileMock(...args),
}))
import { rasterizePdf } from '../rasterize-pdf'
type Cb = (err: Error | null, out?: { stdout: string; stderr: string }) => void
beforeEach(() => {
vi.clearAllMocks()
})
describe('rasterizePdf', () => {
it('renders the first pages in order and cleans up', async () => {
execFileMock.mockImplementation((bin: string, args: string[], _opts: unknown, cb: Cb) => {
expect(bin).toBe('pdftoppm')
expect(args.slice(0, 6)).toEqual(['-r', '110', '-png', '-f', '1', '-l'])
const prefix = args[args.length - 1]
void (async () => {
// Two pages, written out of order to prove numeric sorting.
await writeFile(`${prefix}-2.png`, Buffer.from('PAGE2'))
await writeFile(`${prefix}-1.png`, Buffer.from('PAGE1'))
cb(null, { stdout: '', stderr: '' })
})()
})
const result = await rasterizePdf(Buffer.from('%PDF'), { maxPages: 4 })
expect(result.ok).toBe(true)
if (!result.ok) return
expect(result.pageCount).toBe(2)
expect(result.pages.map((p) => p.toString())).toEqual(['PAGE1', 'PAGE2'])
expect(result.mediaType).toBe('image/png')
const args = execFileMock.mock.calls[0][1] as string[]
expect(args[6]).toBe('4') // -l maxPages
})
it('reports a missing binary as rasterizer_missing', async () => {
execFileMock.mockImplementation((_bin: string, _args: string[], _opts: unknown, cb: Cb) => {
const err = Object.assign(new Error('spawn pdftoppm ENOENT'), { code: 'ENOENT' })
cb(err)
})
const result = await rasterizePdf(Buffer.from('%PDF'), { maxPages: 4 })
expect(result).toEqual({ ok: false, reason: 'rasterizer_missing' })
})
it('reports any other failure as failed with the message', async () => {
execFileMock.mockImplementation((_bin: string, _args: string[], _opts: unknown, cb: Cb) => {
cb(new Error('Syntax Error: Couldn\'t read xref table'))
})
const result = await rasterizePdf(Buffer.from('not a pdf'), { maxPages: 2 })
expect(result).toMatchObject({ ok: false, reason: 'failed' })
})
it('treats a run that produced no pages as failed', async () => {
execFileMock.mockImplementation((_bin: string, _args: string[], _opts: unknown, cb: Cb) => {
cb(null, { stdout: '', stderr: '' })
})
const result = await rasterizePdf(Buffer.from('%PDF'), { maxPages: 1 })
expect(result).toMatchObject({ ok: false, reason: 'failed' })
})
it('honours a binary override', async () => {
execFileMock.mockImplementation((bin: string, _args: string[], _opts: unknown, cb: Cb) => {
expect(bin).toBe('/opt/poppler/bin/pdftoppm')
cb(new Error('x'))
})
await rasterizePdf(Buffer.from('%PDF'), { maxPages: 1, binary: '/opt/poppler/bin/pdftoppm' })
expect(execFileMock).toHaveBeenCalled()
})
})