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 <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
This commit is contained in:
Jakob Wennberg
2026-08-20 21:18:21 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Opus 4.8
parent febb4cc0c2
commit ff4425d10e
15 changed files with 770 additions and 30 deletions
@@ -6,6 +6,14 @@ 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'
@@ -21,6 +29,8 @@ function supabaseWith(company: { name?: string; entity_type?: string } | null):
beforeEach(() => {
vi.clearAllMocks()
generateText.mockResolvedValue({ text: 'Svar', model: 'qwen3.8', usage: {} })
buildLedgerTools.mockReturnValue([])
buildAssistantSnapshot.mockResolvedValue('')
})
describe('answerAssistantQuestion', () => {
@@ -69,4 +79,54 @@ describe('answerAssistantQuestion', () => {
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)
})
})
@@ -0,0 +1,89 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
import type { AgentTool } from '@/lib/agent/tools/types'
const getMany = vi.fn()
vi.mock('@/lib/agent/tools/registry', () => ({
agentToolRegistry: { getMany: (names: string[]) => getMany(names) },
}))
import { buildLedgerTools, LEDGER_READ_TOOLS } from '../ledger-tools'
function tool(name: string, ann?: AgentTool['annotations']): AgentTool {
return {
name,
description: `desc ${name}`,
inputSchema: { type: 'object', properties: {} },
...(ann ? { annotations: ann } : {}),
execute: vi.fn().mockResolvedValue({ tool: name }),
}
}
const supabase = {} as SupabaseClient
beforeEach(() => vi.clearAllMocks())
describe('buildLedgerTools', () => {
it('requests exactly the read whitelist from the registry', () => {
getMany.mockReturnValue([])
buildLedgerTools(supabase, 'c1', 'u1')
expect(getMany).toHaveBeenCalledWith([...LEDGER_READ_TOOLS])
})
it('keeps read-only + unannotated tools, drops writable/destructive ones', () => {
getMany.mockReturnValue([
tool('gnubok_get_income_statement', { readOnlyHint: true }),
tool('gnubok_list_accounts'), // unannotated: on the curated read whitelist, kept
tool('gnubok_categorize_transaction', { readOnlyHint: false }),
tool('gnubok_delete_voucher', { destructiveHint: true }),
])
const defs = buildLedgerTools(supabase, 'c1', 'u1')
expect(defs.map((d) => d.name)).toEqual(['gnubok_get_income_statement', 'gnubok_list_accounts'])
})
it('adapts each tool: name/description/jsonSchema and an execute bound to the actor', async () => {
const src = tool('gnubok_get_vat_report', { readOnlyHint: true })
getMany.mockReturnValue([src])
const defs = buildLedgerTools(supabase, 'company-9', 'user-7', 'conv-3')
expect(defs).toHaveLength(1)
const def = defs[0]
expect(def.name).toBe('gnubok_get_vat_report')
expect(def.description).toBe('desc gnubok_get_vat_report')
expect(def.jsonSchema).toEqual({ type: 'object', properties: {} })
const out = await def.execute({ period: '2026-07' })
expect(out).toEqual({ tool: 'gnubok_get_vat_report' })
expect(src.execute).toHaveBeenCalledWith(
{ period: '2026-07' },
'company-9',
'user-7',
supabase,
{ type: 'agent_chat', id: 'conv-3' },
)
})
it('omits the actor id when there is no conversation', async () => {
const src = tool('gnubok_get_trial_balance', { readOnlyHint: true })
getMany.mockReturnValue([src])
const [def] = buildLedgerTools(supabase, 'c1', 'u1')
await def.execute({})
expect(src.execute).toHaveBeenCalledWith({}, 'c1', 'u1', supabase, { type: 'agent_chat' })
})
it('returns nothing when the registry is empty (core-only build)', () => {
getMany.mockReturnValue([])
expect(buildLedgerTools(supabase, 'c1', 'u1')).toEqual([])
})
it('never lists a write or memory-write tool on the whitelist', () => {
for (const bad of [
'gnubok_categorize_transaction',
'gnubok_create_invoice',
'gnubok_approve_supplier_invoice',
'gnubok_remember_fact',
'gnubok_forget_fact',
]) {
expect(LEDGER_READ_TOOLS).not.toContain(bad)
}
})
})
+80
View File
@@ -0,0 +1,80 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
const getDeadlines = vi.fn()
vi.mock('@/lib/deadlines/status-engine', () => ({
getDeadlinesNeedingAttention: (...a: unknown[]) => getDeadlines(...a),
}))
import { buildAssistantSnapshot } from '../snapshot'
function supabaseWith(settings: Record<string, unknown> | null): SupabaseClient {
const chain = {
select: () => chain,
eq: () => chain,
maybeSingle: async () => ({ data: settings, error: null }),
}
return { from: () => chain } as unknown as SupabaseClient
}
beforeEach(() => {
vi.clearAllMocks()
getDeadlines.mockResolvedValue({ overdue: [], actionNeeded: [] })
})
describe('buildAssistantSnapshot', () => {
it('summarises the company status line', async () => {
const snap = await buildAssistantSnapshot(
supabaseWith({
vat_registered: true,
moms_period: 'quarterly',
accounting_method: 'accrual',
pays_salaries: true,
}),
'c1',
)
expect(snap).toContain('momsregistrerad (momsperiod: quarterly)')
expect(snap).toContain('bokföringsmetod: fakturametod')
expect(snap).toContain('betalar löner')
})
it('handles a non-VAT, cash-method company', async () => {
const snap = await buildAssistantSnapshot(
supabaseWith({ vat_registered: false, accounting_method: 'cash', pays_salaries: false }),
'c1',
)
expect(snap).toContain('ej momsregistrerad')
expect(snap).toContain('bokföringsmetod: kontantmetod')
expect(snap).toContain('betalar inte löner')
})
it('lists deadlines that need attention (overdue first, capped)', async () => {
getDeadlines.mockResolvedValue({
overdue: [{ id: '1', title: 'Momsdeklaration', due_date: '2026-08-12', tax_deadline_type: 'vat' }],
actionNeeded: [{ id: '2', title: 'Arbetsgivardeklaration', due_date: '2026-08-17', tax_deadline_type: 'employer' }],
})
const snap = await buildAssistantSnapshot(supabaseWith(null), 'c1')
expect(snap).toContain('Deadlines som behöver åtgärd:')
expect(snap).toContain('Momsdeklaration (2026-08-12)')
expect(snap).toContain('Arbetsgivardeklaration (2026-08-17)')
})
it('is best-effort: a failing settings query still yields the deadlines line', async () => {
const throwing = {
from: () => ({
select: () => ({ eq: () => ({ maybeSingle: async () => { throw new Error('db down') } }) }),
}),
} as unknown as SupabaseClient
getDeadlines.mockResolvedValue({
overdue: [],
actionNeeded: [{ id: '2', title: 'Moms', due_date: '2026-09-12', tax_deadline_type: 'vat' }],
})
const snap = await buildAssistantSnapshot(throwing, 'c1')
expect(snap).toContain('Moms (2026-09-12)')
})
it('returns an empty string when there is nothing to say', async () => {
const snap = await buildAssistantSnapshot(supabaseWith(null), 'c1')
expect(snap).toBe('')
})
})
File diff suppressed because one or more lines are too long
+86
View File
@@ -0,0 +1,86 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import type { AiToolDef } from '@/lib/ai'
import { agentToolRegistry } from '@/lib/agent/tools/registry'
import type { AgentActorContext } from '@/lib/agent/tools/types'
/**
* The read-only tools the single-call assistant may call to answer questions
* about the ledger (audit Option A: "single-call actions over the existing MCP
* tool functions"). This is what makes the console behave like an MCP client:
* the model asks for a report, we run the real tool, feed the JSON back.
*
* The list is the read slice of general.help's old tool whitelist: the
* analytical reports and the lookups across the working set. Write/staging
* tools (categorize, create_invoice, approve_supplier_invoice, stage_year_end,
* …) and the memory-write tools (remember/forget) are deliberately absent: the
* console has no ApprovalCard surface, so it reads and guides only; write
* actions belong to the page-specific intents where a single entity is in
* focus and the user expects a staged card.
*
* The registry is populated by the mcp-server extension at init
* (ensureInitialized). In a core-only build it is empty, so this returns an
* empty list and the assistant answers from the prompt + snapshot alone: the
* graceful, correct degradation.
*/
export const LEDGER_READ_TOOLS: readonly string[] = [
// Reports (the canonical analytical surface)
'gnubok_get_income_statement',
'gnubok_get_balance_sheet',
'gnubok_get_trial_balance',
'gnubok_get_general_ledger',
'gnubok_get_kpi_report',
'gnubok_get_vat_report',
'gnubok_vat_close_check',
'gnubok_get_ar_ledger',
'gnubok_get_supplier_ledger',
'gnubok_get_reconciliation_status',
'gnubok_get_salary_journal',
'gnubok_year_end_readiness',
// Lookups across the working set
'gnubok_query_journal',
'gnubok_list_uncategorized_transactions',
'gnubok_list_transactions_without_documents',
'gnubok_list_invoices',
'gnubok_list_customers',
'gnubok_list_suppliers',
'gnubok_list_supplier_invoices',
'gnubok_list_accounts',
'gnubok_list_fiscal_periods',
'gnubok_list_employees',
'gnubok_list_inbox_items',
'gnubok_list_unmatched_documents',
'gnubok_list_voucher_gaps',
'gnubok_explain_voucher_gap',
'gnubok_get_counterparty_templates',
]
/**
* Build the read-only tool defs for one company/user, bound to the same actor
* context the streaming runtime uses (`agent_chat` + the conversation id, for
* BFL audit). Each def's execute dispatches the real registered tool.
*/
export function buildLedgerTools(
supabase: SupabaseClient,
companyId: string,
userId: string,
conversationId?: string,
): AiToolDef[] {
const actor: AgentActorContext = {
type: 'agent_chat',
...(conversationId ? { id: conversationId } : {}),
}
return agentToolRegistry
.getMany([...LEDGER_READ_TOOLS])
// Belt-and-suspenders on top of the curated whitelist: never expose a tool
// the registry itself marks writable or destructive, even if it somehow
// ended up on the list.
.filter((t) => t.annotations?.readOnlyHint !== false && t.annotations?.destructiveHint !== true)
.map((t) => ({
name: t.name,
description: t.description,
jsonSchema: t.inputSchema,
execute: (args: Record<string, unknown>) =>
t.execute(args, companyId, userId, supabase, actor),
}))
}
+76
View File
@@ -0,0 +1,76 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import { getDeadlinesNeedingAttention } from '@/lib/deadlines/status-engine'
/**
* A compact, always-on grounding block for the single-call assistant.
*
* This is the reliability backstop for the "MCP tools + snapshot" design: it
* lets a model that does NOT call tools (a weaker local model, or one whose
* function-calling is off) still answer the standing-status questions from
* this block alone. It carries the company's standing profile and the
* deadlines that need attention: it deliberately does NOT carry figures
* (result, expenses, VAT amounts, transactions), which come from the read
* tools so they are always live and never stale in a prompt.
*
* Company-scoped: reads only this company's own rows. Every part is
* best-effort: a slow or failing query drops that line, never the answer.
*/
function accountingMethodLabel(method: string | null | undefined): string | null {
if (!method) return null
const m = method.toLowerCase()
if (m.includes('cash') || m.includes('kontant')) return 'kontantmetod'
if (m.includes('accrual') || m.includes('faktura')) return 'fakturametod'
return method
}
export async function buildAssistantSnapshot(
supabase: SupabaseClient,
companyId: string,
): Promise<string> {
const lines: string[] = []
try {
const { data } = await supabase
.from('company_settings')
.select('vat_registered, moms_period, accounting_method, pays_salaries')
.eq('company_id', companyId)
.maybeSingle()
const row = data as {
vat_registered?: boolean | null
moms_period?: string | null
accounting_method?: string | null
pays_salaries?: boolean | null
} | null
if (row) {
const parts: string[] = []
parts.push(
row.vat_registered
? `momsregistrerad${row.moms_period ? ` (momsperiod: ${row.moms_period})` : ''}`
: 'ej momsregistrerad',
)
const method = accountingMethodLabel(row.accounting_method)
if (method) parts.push(`bokföringsmetod: ${method}`)
parts.push(row.pays_salaries ? 'betalar löner' : 'betalar inte löner')
lines.push(`Status: ${parts.join(', ')}.`)
}
} catch {
// best-effort: skip the status line
}
try {
const { overdue, actionNeeded } = await getDeadlinesNeedingAttention(supabase, companyId)
const soon = [...overdue, ...actionNeeded].slice(0, 6)
if (soon.length > 0) {
lines.push(
`Deadlines som behöver åtgärd: ${soon
.map((d) => `${d.title} (${d.due_date})`)
.join('; ')}.`,
)
}
} catch {
// best-effort: skip the deadlines line
}
return lines.join('\n')
}