Files
accounted/app/api/agent/ask/route.ts
T
Jakob Wennberg 3edbf0a2e3 fix(agent): chat console keeps its thread across turns and reloads (#1859)
Three user-reported failures in the assistant panel, one root cause each:

1. "The chat asks what I'm referring to" when continuing a thread. The
   single-call console (general.help, AskConsole -> /api/agent/ask) was
   stateless since the 08-20 model-agnostic cutover: conversationId was only
   the tool actor id, so every turn was answered blind, reload or not.
   The provider-agnostic GenerateTextRequest gains an optional `history`
   (real message turns before the prompt, in both the Anthropic-family and
   the OpenAI-compatible adapter; absent/empty leaves the request
   byte-identical to the single-turn call). The route loads the thread's
   earlier turns server-side (loadChatHistory: text only, hidden and tool
   rows dropped, alternation repaired, newest 16 rows / 10k chars) before
   writing the new question, and hands them to the model.

2. A full page reload (the deploy prompt's "Ladda om") closed the docked
   panel and dropped the thread from view. The panel now remembers its open
   thread per tab in sessionStorage (lib/agent-panel/session-restore) and
   the provider reopens it on mount; the sheet loads it exactly like a pick
   from "Tidigare konversationer". Close and "Ny konversation" forget it; a
   thread that no longer opens is dropped instead of retried on every reload.

3. "Can't type any more" once the update banner shows. DeployReloadPrompt's
   full-width wrapper sits at z-[60] after the panel in DOM order and
   swallowed clicks on the panel's composer; only the card takes input now.


Claude-Session: https://claude.ai/code/session_01VjoXN3xdNZrHZeYA6qMi3g

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-24 16:37:30 +02:00

192 lines
7.7 KiB
TypeScript

import { NextResponse } from 'next/server'
import { z } from 'zod'
import { ensureInitialized } from '@/lib/init'
import { requireAuth } from '@/lib/auth/require-auth'
import { getActiveCompanyId } from '@/lib/company/context'
import { checkAgentRateLimit, agentRateLimitResponseBody } from '@/lib/rate-limits/agent'
import { guardSandbox } from '@/lib/sandbox/guard'
import { requireCapability } from '@/lib/entitlements/has-capability'
import { CAPABILITY } from '@/lib/entitlements/keys'
import { getAiStatus } from '@/lib/ai'
import { createLogger } from '@/lib/logger'
import { answerAssistantQuestion } from '@/lib/agent/ask/ask-service'
import { EmptyModelAnswerError } from '@/lib/agent/ask/errors'
import {
resolveChatConversation,
loadChatHistory,
persistUserTurn,
persistAssistantTurn,
} from '@/lib/agent/ask/persist'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
// The assistant answers over the read-only MCP tools, which are registered
// into the agent tool registry by the mcp-server extension at load. Without
// this the registry is empty and the assistant falls back to snapshot-only,
// so a hosted deploy would silently lose its ledger tools.
ensureInitialized()
const log = createLogger('api.agent.ask')
// A tool-loop answer can take minutes (several model turns, each preceded by
// real report reads). The plan default cap would kill the function mid-answer
// and the client would see a silent empty stop; 300 s matches the other
// long-running model surfaces (app/api/receipt-hunt/run).
export const maxDuration = 300
// The Swedish body AskConsole's !res.ok branch renders verbatim.
const EMPTY_ANSWER_MESSAGE = 'Assistenten gav inget svar. Försök igen.'
/**
* POST /api/agent/ask: a single-call, provider-agnostic assistant answer over a
* bounded read-only tool loop.
*
* Unlike POST /api/agent/invoke (the streaming Anthropic chat runtime, which
* is gated on `assistantAvailable` and only runs on the Anthropic family),
* this endpoint answers through getAiService().generateText, so it runs on ANY
* configured backend, including an OpenAI-compatible local model. It is
* therefore gated on `configured`, not `assistantAvailable`. The service
* attaches the read-only MCP tools so it can fetch real figures (audit Option
* A / rip): a page posts its context and a question, gets one answer back.
*/
const Schema = z.object({
question: z.string().min(1).max(4000),
context: z.string().max(24_000).optional(),
tier: z.enum(['assistant', 'heavy']).optional(),
company_id: z.string().uuid().optional(),
// Chat-console persistence (opt-in). When `persist` is true, the turn is
// written to agent_conversations/agent_messages so the /chat sidebar keeps
// working. Page-scoped one-off actions (a report page asking a question)
// omit it and stay stateless. `conversation_id` resumes an existing
// general.help thread; omitted means "create one". `context_ref` binds a
// fresh thread to a page ("report:vat:2026-07") for the context chip.
persist: z.boolean().optional(),
conversation_id: z.string().uuid().nullable().optional(),
context_ref: z.string().max(200).nullable().optional(),
})
export async function POST(request: Request): Promise<Response> {
const { user, supabase, error } = await requireAuth()
if (error) return error
const rate = await checkAgentRateLimit(supabase, user.id)
if (!rate.ok) return NextResponse.json(agentRateLimitResponseBody(rate), { status: 429 })
let body: unknown
try {
body = await request.json()
} catch {
return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 })
}
const parsed = Schema.safeParse(body)
if (!parsed.success) {
return NextResponse.json({ error: 'Ogiltig fråga.', type: 'validation_error' }, { status: 400 })
}
const companyId = parsed.data.company_id ?? (await getActiveCompanyId(supabase, user.id))
if (!companyId) return NextResponse.json({ error: 'No active company' }, { status: 400 })
const { data: membership } = await supabase
.from('company_members')
.select('user_id')
.eq('company_id', companyId)
.eq('user_id', user.id)
.maybeSingle()
if (!membership) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
const blocked = await guardSandbox(supabase, companyId)
if (blocked) return blocked
const capBlocked = await requireCapability(supabase, companyId, CAPABILITY.ai)
if (capBlocked) return capBlocked
// Distinct from the paywall: no AI backend configured at all. Unlike the
// chat loop, ANY provider works here, so we gate on `configured`.
if (!getAiStatus().configured) {
return NextResponse.json(
{ error: 'Assistenten är inte konfigurerad på den här installationen.', code: 'ai_unconfigured' },
{ status: 503 },
)
}
// Stateless page-scoped ask: one answer, nothing written.
if (parsed.data.persist !== true) {
try {
const result = await answerAssistantQuestion({
supabase,
companyId,
userId: user.id,
question: parsed.data.question,
pageContext: parsed.data.context,
tier: parsed.data.tier,
})
return NextResponse.json({ data: result })
} catch (err) {
if (err instanceof EmptyModelAnswerError) {
// Already logged with model + usage by ask-service.
return NextResponse.json(
{ error: EMPTY_ANSWER_MESSAGE, code: 'empty_model_answer' },
{ status: 502 },
)
}
log.error('assistant ask failed', err, { companyId, persist: false })
return NextResponse.json({ error: getUserErrorMessage(err) }, { status: 500 })
}
}
// Persisted chat-console turn: resolve/create the thread, write the question,
// answer once, write the answer. Resolve BEFORE the model call so a bad
// conversation id 404s without spending a request; the user turn is written
// before the answer so a mid-call failure still leaves the question in the
// thread (the user can retry), matching the streaming runtime's semantics.
try {
const resolved = await resolveChatConversation(
supabase,
user.id,
companyId,
parsed.data.conversation_id,
parsed.data.question,
parsed.data.context_ref,
)
if (!resolved.ok) {
return NextResponse.json({ error: 'Konversationen hittades inte.' }, { status: 404 })
}
const { conversationId } = resolved
// A resumed thread carries its earlier turns into the model call; read
// them BEFORE the new question is written so it is not sent twice. A
// thread created just now has nothing to load.
const history = resolved.created ? [] : await loadChatHistory(supabase, conversationId)
await persistUserTurn(supabase, conversationId, parsed.data.question)
const result = await answerAssistantQuestion({
supabase,
companyId,
userId: user.id,
conversationId,
question: parsed.data.question,
pageContext: parsed.data.context,
tier: parsed.data.tier,
history,
})
// answerAssistantQuestion throws EmptyModelAnswerError on an empty answer,
// so an empty assistant turn is never persisted: the thread keeps the
// question (retryable) but records no blank reply.
await persistAssistantTurn(supabase, conversationId, result.answer)
return NextResponse.json({ data: { ...result, conversation_id: conversationId } })
} catch (err) {
if (err instanceof EmptyModelAnswerError) {
// Already logged with model + usage by ask-service.
return NextResponse.json(
{ error: EMPTY_ANSWER_MESSAGE, code: 'empty_model_answer' },
{ status: 502 },
)
}
log.error('assistant ask failed', err, { companyId, persist: true })
return NextResponse.json({ error: getUserErrorMessage(err) }, { status: 500 })
}
}