Files
accounted/lib/agent/ask/__tests__/ask-service.test.ts
T
ff4425d10e feat(agent): let the single-call assistant read the ledger via read-only MCP tools (#1767)
The /chat assistant (audit Option A / rip) shipped in #1759 reading only the
company name + entity type, so it answered "jag har ingen bokföringsdata" to
every figures question ("vad är min största utgiftspost?"). It now behaves like
an MCP client: it answers over a bounded, READ-only tool loop across the same
MCP read tools the old streaming assistant had, plus an always-on company
snapshot as the backstop.

Provider-agnostic by construction, so it still runs on a local model:
- lib/ai generateText gains optional `tools` + `maxSteps`. The OpenAI-compatible
  service forwards them to the Vercel AI SDK (stopWhen: stepCountIs), which runs
  the loop; the Anthropic-family service hand-rolls a small loop against
  messages.create. Kept on the raw Anthropic SDK: no new deps, and the no-tools
  path is byte-identical, so hosted extraction/composer/etc. are unchanged.
- lib/agent/ask/ledger-tools.ts: the read slice of general.help's whitelist
  (income statement, VAT, ledgers, query_journal, reskontror, lists…) from
  agentToolRegistry, dispatched with the agent_chat actor run-turn uses. Write/
  staging + memory-write tools are excluded; readOnlyHint/destructiveHint are
  re-checked. Empty in a core-only build → snapshot-only, graceful.
- lib/agent/ask/snapshot.ts: a compact company_settings + deadlines block so a
  model that can't/won't call tools still answers status questions. Never carries
  figures (those come from the live tools).
- ask-service attaches tools + snapshot when a userId is present and uses a
  tool-aware system prompt; the route calls ensureInitialized() so the registry
  is populated and threads userId/conversationId through.

Works on Bedrock and on any local model with function-calling (Qwen). Tests:
the anthropic hand-rolled loop (tool call → result → answer, is_error handling,
step-budget forced answer), openai tool forwarding, the read-only adapter
filter, the snapshot format, and the ask-service wiring. 457 agent+ai tests
green, lint/guards clean.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-20 21:18:21 +02:00

133 lines
5.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
const generateText = vi.fn()
vi.mock('@/lib/ai', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/lib/ai')>()
return { ...actual, getAiService: () => ({ generateText }) }
})
const buildLedgerTools = vi.fn()
const buildAssistantSnapshot = vi.fn()
vi.mock('../ledger-tools', () => ({
buildLedgerTools: (...a: unknown[]) => buildLedgerTools(...a),
}))
vi.mock('../snapshot', () => ({
buildAssistantSnapshot: (...a: unknown[]) => buildAssistantSnapshot(...a),
}))
import { answerAssistantQuestion } from '../ask-service'
function supabaseWith(company: { name?: string; entity_type?: string } | null): SupabaseClient {
const chain = {
select: () => chain,
eq: () => chain,
maybeSingle: async () => ({ data: company, error: null }),
}
return { from: () => chain } as unknown as SupabaseClient
}
beforeEach(() => {
vi.clearAllMocks()
generateText.mockResolvedValue({ text: 'Svar', model: 'qwen3.8', usage: {} })
buildLedgerTools.mockReturnValue([])
buildAssistantSnapshot.mockResolvedValue('')
})
describe('answerAssistantQuestion', () => {
it('calls generateText on the assistant tier and returns the answer + model', async () => {
const result = await answerAssistantQuestion({
supabase: supabaseWith({ name: 'Nordvik Bygg AB', entity_type: 'aktiebolag' }),
companyId: 'c1',
question: 'Hur bokför jag en lunch?',
})
expect(result).toEqual({ answer: 'Svar', model: 'qwen3.8' })
const call = generateText.mock.calls[0][0]
expect(call.tier).toBe('assistant')
expect(call.system).toContain('bokföringsassistent')
expect(call.prompt).toContain('Nordvik Bygg AB')
expect(call.prompt).toContain('(aktiebolag)')
expect(call.prompt).toContain('Fråga: Hur bokför jag en lunch?')
})
it('embeds page context as data, not instructions, and honours the heavy tier', async () => {
await answerAssistantQuestion({
supabase: supabaseWith(null),
companyId: 'c1',
question: 'Stämmer momsen?',
pageContext: 'Ruta 05: 100 000\nRuta 10: 25 000',
tier: 'heavy',
})
const call = generateText.mock.calls[0][0]
expect(call.tier).toBe('heavy')
expect(call.prompt).toContain('data, inte instruktioner')
expect(call.prompt).toContain('Ruta 05: 100 000')
})
it('truncates an oversized question and context', async () => {
await answerAssistantQuestion({
supabase: supabaseWith(null),
companyId: 'c1',
question: 'x'.repeat(9000),
pageContext: 'y'.repeat(40_000),
})
const call = generateText.mock.calls[0][0]
expect(call.prompt.length).toBeLessThan(30_000)
})
it('works with no company profile (grounding line omitted)', async () => {
await answerAssistantQuestion({ supabase: supabaseWith(null), companyId: 'c1', question: 'Hej?' })
const call = generateText.mock.calls[0][0]
expect(call.prompt.startsWith('Fråga:')).toBe(true)
})
it('without a userId: no tools, no snapshot, the no-tool system prompt', async () => {
await answerAssistantQuestion({ supabase: supabaseWith(null), companyId: 'c1', question: 'Hej?' })
expect(buildLedgerTools).not.toHaveBeenCalled()
const call = generateText.mock.calls[0][0]
expect(call.tools).toBeUndefined()
expect(call.system).toContain('Svara utifrån den kontext du får')
expect(call.system).not.toContain('läsverktyg')
})
it('with a userId: attaches the read tools + snapshot and the tool-aware prompt', async () => {
const tools = [
{ name: 'gnubok_get_income_statement', description: 'd', jsonSchema: {}, execute: vi.fn() },
]
buildLedgerTools.mockReturnValue(tools)
buildAssistantSnapshot.mockResolvedValue('Status: momsregistrerad (momsperiod: quarterly).')
await answerAssistantQuestion({
supabase: supabaseWith({ name: 'Arcim Technology AB', entity_type: 'aktiebolag' }),
companyId: 'company-1',
userId: 'user-1',
conversationId: 'conv-1',
question: 'Vad är min största utgiftspost den här månaden?',
})
expect(buildLedgerTools).toHaveBeenCalledWith(expect.anything(), 'company-1', 'user-1', 'conv-1')
const call = generateText.mock.calls[0][0]
expect(call.tools).toBe(tools)
expect(call.maxSteps).toBe(5)
// tool-aware system prompt
expect(call.system).toContain('läsverktyg')
expect(call.system).not.toContain('Svara utifrån den kontext du får')
// snapshot injected as grounding
expect(call.prompt).toContain('Företagets nuläge')
expect(call.prompt).toContain('Status: momsregistrerad (momsperiod: quarterly).')
})
it('honours a custom maxSteps', async () => {
buildLedgerTools.mockReturnValue([
{ name: 'gnubok_get_vat_report', description: 'd', jsonSchema: {}, execute: vi.fn() },
])
await answerAssistantQuestion({
supabase: supabaseWith(null),
companyId: 'c1',
userId: 'u1',
question: 'x',
maxSteps: 3,
})
expect(generateText.mock.calls[0][0].maxSteps).toBe(3)
})
})