Files
accounted/app/api/agent/__tests__/invoke.test.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

283 lines
9.8 KiB
TypeScript

/**
* Tests for POST /api/agent/invoke, focused on conversation ownership.
*
* RLS on agent_conversations/agent_messages is company-scoped, not user-scoped
* (migration 20260517204000), so the route itself has to prove that a resumed
* conversation_id belongs to the caller. Without that check, a member could
* post a colleague's conversation id and have their history loaded into the
* prompt (and their own turns appended to it).
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { NextResponse } from 'next/server'
import { createQueuedMockSupabase, createMockRequest, parseJsonResponse } from '@/tests/helpers'
const { supabase, enqueue, reset } = createQueuedMockSupabase()
const requireAuthMock = vi.fn()
vi.mock('@/lib/auth/require-auth', () => ({
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
}))
vi.mock('@/lib/company/context', () => ({
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
}))
const checkRateMock = vi.fn()
vi.mock('@/lib/rate-limits/agent', () => ({
checkAgentRateLimit: (...args: unknown[]) => checkRateMock(...args),
agentRateLimitResponseBody: () => ({ error: 'För många förfrågningar.' }),
}))
vi.mock('@/lib/sandbox/guard', () => ({
guardSandbox: vi.fn().mockResolvedValue(null),
}))
vi.mock('@/lib/entitlements/has-capability', () => ({
requireCapability: vi.fn().mockResolvedValue(null),
}))
vi.mock('@/lib/entitlements/keys', () => ({
CAPABILITY: { ai: 'ai' },
}))
vi.mock('@/lib/init', () => ({
ensureInitialized: vi.fn(),
}))
const getIntentMock = vi.fn()
vi.mock('@/lib/agent/intents/registry', () => ({
getIntent: (...args: unknown[]) => getIntentMock(...args),
}))
// Deployment-level AI availability (distinct from the per-company paywall).
// Default: available, so the ownership tests below exercise the real flow.
const aiStatusMock = vi.fn()
vi.mock('@/lib/ai', () => ({
getAiStatus: () => aiStatusMock(),
}))
const runChatTurnMock = vi.fn()
vi.mock('@/lib/agent/chat/run-turn', () => ({
runChatTurn: (...args: unknown[]) => runChatTurnMock(...args),
friendlyModelError: () => 'Något gick fel hos assistenten.',
}))
import { POST } from '../invoke/route'
const CONVERSATION_ID = '11111111-1111-4111-8111-111111111111'
function body(overrides: Record<string, unknown> = {}) {
return {
intent_id: 'general.help',
user_message: 'Hur gick juli?',
...overrides,
}
}
/**
* Queue the reads the route performs before it resolves the conversation.
*
* Ownership is validated ahead of every side effect and of the company/profile
* reads, so a rejected request costs exactly one membership read plus the
* conversation lookup.
*/
function enqueuePreamble() {
enqueue({ data: { role: 'owner' } }) // company_members
}
/** The company + profile reads that only happen once a request is accepted. */
function enqueueAcceptedTail() {
enqueue({ data: { name: 'Nordvik Bygg AB' } }) // companies
enqueue({ data: { full_name: 'Johan Nordvik' } }) // profiles
}
beforeEach(() => {
vi.clearAllMocks()
reset()
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase, error: null })
checkRateMock.mockResolvedValue({ ok: true })
getIntentMock.mockReturnValue({ id: 'general.help', sheetTitle: 'Assistenten' })
runChatTurnMock.mockResolvedValue(undefined)
aiStatusMock.mockReturnValue({ configured: true, assistantAvailable: true, provider: 'bedrock' })
})
describe('POST /api/agent/invoke', () => {
it('returns 401 when not authenticated', async () => {
requireAuthMock.mockResolvedValue({
user: null,
supabase,
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
})
const res = await POST(createMockRequest('/api/agent/invoke', { method: 'POST', body: body() }))
const { status } = await parseJsonResponse(res)
expect(status).toBe(401)
})
it('returns 400 on an invalid body', async () => {
const res = await POST(createMockRequest('/api/agent/invoke', { method: 'POST', body: { intent_id: '' } }))
const { status } = await parseJsonResponse(res)
expect(status).toBe(400)
})
it('returns 400 for an unknown intent', async () => {
getIntentMock.mockReturnValue(undefined)
const res = await POST(createMockRequest('/api/agent/invoke', { method: 'POST', body: body() }))
const { status } = await parseJsonResponse<{ error: string }>(res)
expect(status).toBe(400)
})
// A self-host without an AI key, or on an OpenAI-compatible endpoint the
// chat loop does not speak yet: say so up front with a distinct code, never
// open a stream that dies on the first model call, never confuse it with
// the paywall.
it('returns 503 ai_unconfigured when the deployment has no assistant backend', async () => {
aiStatusMock.mockReturnValue({ configured: false, assistantAvailable: false, provider: 'bedrock' })
enqueuePreamble()
const res = await POST(createMockRequest('/api/agent/invoke', { method: 'POST', body: body() }))
const { status, body: json } = await parseJsonResponse<{ error: string; code: string }>(res)
expect(status).toBe(503)
expect(json.code).toBe('ai_unconfigured')
expect(runChatTurnMock).not.toHaveBeenCalled()
})
it('returns 403 when the user is not a member of the company', async () => {
enqueue({ data: null }) // company_members: no row
const res = await POST(createMockRequest('/api/agent/invoke', { method: 'POST', body: body() }))
const { status } = await parseJsonResponse(res)
expect(status).toBe(403)
})
it('returns 404 when resuming a conversation owned by another user', async () => {
enqueuePreamble()
enqueue({
data: {
id: CONVERSATION_ID,
user_id: 'user-2', // someone else in the same company
company_id: 'company-1',
intent_id: 'general.help',
},
})
const res = await POST(
createMockRequest('/api/agent/invoke', { method: 'POST', body: body({ conversation_id: CONVERSATION_ID }) }),
)
const { status, body: json } = await parseJsonResponse<{ error: string }>(res)
expect(status).toBe(404)
expect(json.error).toBe('Konversationen hittades inte.')
expect(runChatTurnMock).not.toHaveBeenCalled()
})
it('returns 404 when resuming a conversation from another company', async () => {
enqueuePreamble()
enqueue({
data: {
id: CONVERSATION_ID,
user_id: 'user-1', // same user...
company_id: 'company-2', // ...but a company other than the active one
intent_id: 'general.help',
},
})
const res = await POST(
createMockRequest('/api/agent/invoke', { method: 'POST', body: body({ conversation_id: CONVERSATION_ID }) }),
)
const { status } = await parseJsonResponse(res)
expect(status).toBe(404)
expect(runChatTurnMock).not.toHaveBeenCalled()
})
it('returns 404 when the conversation does not exist', async () => {
enqueuePreamble()
enqueue({ data: null })
const res = await POST(
createMockRequest('/api/agent/invoke', { method: 'POST', body: body({ conversation_id: CONVERSATION_ID }) }),
)
const { status } = await parseJsonResponse(res)
expect(status).toBe(404)
expect(runChatTurnMock).not.toHaveBeenCalled()
})
it('returns 400 when the conversation belongs to a different intent', async () => {
enqueuePreamble()
enqueue({
data: {
id: CONVERSATION_ID,
user_id: 'user-1',
company_id: 'company-1',
intent_id: 'vat.review', // tool loadout differs from general.help
},
})
const res = await POST(
createMockRequest('/api/agent/invoke', { method: 'POST', body: body({ conversation_id: CONVERSATION_ID }) }),
)
const { status } = await parseJsonResponse(res)
expect(status).toBe(400)
expect(runChatTurnMock).not.toHaveBeenCalled()
})
it('runs the turn when the caller owns the conversation', async () => {
enqueuePreamble()
enqueue({
data: {
id: CONVERSATION_ID,
user_id: 'user-1',
company_id: 'company-1',
intent_id: 'general.help',
},
})
enqueueAcceptedTail()
const res = await POST(
createMockRequest('/api/agent/invoke', { method: 'POST', body: body({ conversation_id: CONVERSATION_ID }) }),
)
expect(res.status).toBe(200)
// The route streams NDJSON; draining it is enough to know the turn ran.
await res.text()
expect(runChatTurnMock).toHaveBeenCalledTimes(1)
})
it('hands the first-turn profile summary to the turn instead of re-reading it', async () => {
// On a first turn the route reads agent_profiles to build the intent's
// prompt template. run-turn needs the same value for the system prompt, so
// it is passed through rather than read a second time.
getIntentMock.mockReturnValue({
id: 'general.help',
sheetTitle: 'Assistenten',
capture: vi.fn().mockResolvedValue({ some: 'context' }),
promptTemplate: vi.fn().mockReturnValue('templated first turn'),
})
enqueuePreamble()
enqueue({ data: { id: CONVERSATION_ID } }) // conversation insert
enqueueAcceptedTail()
enqueue({ data: { profile_summary: 'Byggkonsult, K2' } }) // agent_profiles
enqueue({ data: [] }) // agent_memory
const res = await POST(
createMockRequest('/api/agent/invoke', {
method: 'POST',
body: { intent_id: 'general.help' }, // no user_message => first turn
}),
)
await res.text()
expect(runChatTurnMock).toHaveBeenCalledTimes(1)
const args = runChatTurnMock.mock.calls[0]![0] as {
preloadedProfileSummary?: string | null
userMessage: string
userMessageHidden?: boolean
}
expect(args.preloadedProfileSummary).toBe('Byggkonsult, K2')
expect(args.userMessage).toBe('templated first turn')
expect(args.userMessageHidden).toBe(true)
})
})