ff4425d10e
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>
90 lines
3.2 KiB
TypeScript
90 lines
3.2 KiB
TypeScript
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)
|
|
}
|
|
})
|
|
})
|