feat(chat): single-call console for general.help, persisted, runs on a local model (#1762)

RIP-3 cutover. The free-form /chat panel (general.help) now answers through a
page-scoped single-call console (AskConsole → POST /api/agent/ask) instead of
the streaming Anthropic runtime, so the in-app assistant runs on ANY configured
backend, including a local OpenAI-compatible model (Qwen behind
llama.cpp/Ollama/vLLM). No tool loop, no NDJSON stream, no Anthropic wire format.

Threads still persist: the ask route gains an opt-in persist branch that writes
both turns to agent_conversations/agent_messages as canonical Anthropic text
blocks, so the /chat sidebar and "resume a thread" keep working across old
streaming threads and new single-call ones. Page-scoped one-off asks (a report
page) omit persist and stay stateless.

Scope: only general.help is wired to the console. The tool-loop intents
(transaction.categorization, invoice.draft, supplier_invoice.review) and the
docked AgentSheet still use AgentChat + run-turn.ts because they stage
operations and need the tool loop, so run-turn.ts is intentionally NOT deleted
here (the plan gates its deletion on "once nothing calls them"; RIP-4 migrates
the rest).

- lib/agent/ask/persist.ts: resolveChatConversation (create/resume, ownership),
  persistUserTurn, persistAssistantTurn (append + roll last_message_* forward)
- app/api/agent/ask/route.ts: persist branch (resolve → user turn → answer →
  assistant turn), returns conversation_id; 404 on a foreign conversation
- components/agent/AskConsole.tsx: the console UI (approved sign-off design):
  user bubble + bare-prose answer, thinking indicator, empty/503/paywall states
- ChatConversationView / ChatNewStarter: branch general.help → AskConsole,
  every other intent keeps AgentChat unchanged

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-08-20 20:35:16 +02:00
committed by GitHub
co-authored by Jakob Wennberg Claude Opus 4.8
parent a071a0220b
commit 3f6f1ab06e
8 changed files with 872 additions and 4 deletions
+90
View File
@@ -18,6 +18,14 @@ const aiStatus = vi.fn()
vi.mock('@/lib/ai', () => ({ getAiStatus: () => aiStatus() }))
const answer = vi.fn()
vi.mock('@/lib/agent/ask/ask-service', () => ({ answerAssistantQuestion: (...a: unknown[]) => answer(...a) }))
const resolveConv = vi.fn()
const persistUser = vi.fn()
const persistAssistant = vi.fn()
vi.mock('@/lib/agent/ask/persist', () => ({
resolveChatConversation: (...a: unknown[]) => resolveConv(...a),
persistUserTurn: (...a: unknown[]) => persistUser(...a),
persistAssistantTurn: (...a: unknown[]) => persistAssistant(...a),
}))
import { POST } from '../route'
@@ -31,6 +39,9 @@ beforeEach(() => {
requireCapability.mockResolvedValue(null)
aiStatus.mockReturnValue({ configured: true, assistantAvailable: false, provider: 'openai-compatible' })
answer.mockResolvedValue({ answer: 'Svar', model: 'qwen3.8' })
resolveConv.mockResolvedValue({ ok: true, conversationId: 'conv-9', created: true })
persistUser.mockResolvedValue(undefined)
persistAssistant.mockResolvedValue(undefined)
})
const body = (o: Record<string, unknown> = {}) => ({ question: 'Hur gick juli?', ...o })
@@ -71,4 +82,83 @@ describe('POST /api/agent/ask', () => {
expect(b.code).toBe('ai_unconfigured')
expect(answer).not.toHaveBeenCalled()
})
it('stateless (no persist): never touches the conversation tables', async () => {
await POST(createMockRequest('/x', { method: 'POST', body: body() }))
expect(resolveConv).not.toHaveBeenCalled()
expect(persistUser).not.toHaveBeenCalled()
expect(persistAssistant).not.toHaveBeenCalled()
})
describe('persist: true (chat console)', () => {
it('creates/resumes the thread, writes both turns, returns the conversation id', async () => {
const res = await POST(
createMockRequest('/x', {
method: 'POST',
body: body({ persist: true, context_ref: 'report:vat:2026-07' }),
}),
)
const { status, body: b } = await parseJsonResponse<{
data: { answer: string; model: string; conversation_id: string }
}>(res)
expect(status).toBe(200)
expect(b.data.conversation_id).toBe('conv-9')
expect(b.data.answer).toBe('Svar')
// Order matters: resolve → user turn → answer → assistant turn.
expect(resolveConv).toHaveBeenCalledWith(
supabase,
'user-1',
'company-1',
undefined,
'Hur gick juli?',
'report:vat:2026-07',
)
expect(persistUser).toHaveBeenCalledWith(supabase, 'conv-9', 'Hur gick juli?')
expect(answer).toHaveBeenCalled()
expect(persistAssistant).toHaveBeenCalledWith(supabase, 'conv-9', 'Svar')
})
it('resumes with a supplied conversation_id', async () => {
resolveConv.mockResolvedValue({ ok: true, conversationId: 'conv-7', created: false })
const res = await POST(
createMockRequest('/x', {
method: 'POST',
body: body({ persist: true, conversation_id: '11111111-1111-4111-8111-111111111111' }),
}),
)
const { status, body: b } = await parseJsonResponse<{ data: { conversation_id: string } }>(res)
expect(status).toBe(200)
expect(b.data.conversation_id).toBe('conv-7')
expect(resolveConv).toHaveBeenCalledWith(
supabase,
'user-1',
'company-1',
'11111111-1111-4111-8111-111111111111',
'Hur gick juli?',
undefined,
)
})
it("404s on a conversation that isn't the user's, without answering or persisting", async () => {
resolveConv.mockResolvedValue({ ok: false, reason: 'not_found' })
const res = await POST(
createMockRequest('/x', {
method: 'POST',
body: body({ persist: true, conversation_id: '22222222-2222-4222-8222-222222222222' }),
}),
)
expect(res.status).toBe(404)
expect(persistUser).not.toHaveBeenCalled()
expect(answer).not.toHaveBeenCalled()
expect(persistAssistant).not.toHaveBeenCalled()
})
it('still 503s (no write) when no backend is configured', async () => {
aiStatus.mockReturnValue({ configured: false })
const res = await POST(createMockRequest('/x', { method: 'POST', body: body({ persist: true }) }))
expect(res.status).toBe(503)
expect(resolveConv).not.toHaveBeenCalled()
expect(persistUser).not.toHaveBeenCalled()
})
})
})
+54 -1
View File
@@ -8,6 +8,11 @@ import { requireCapability } from '@/lib/entitlements/has-capability'
import { CAPABILITY } from '@/lib/entitlements/keys'
import { getAiStatus } from '@/lib/ai'
import { answerAssistantQuestion } from '@/lib/agent/ask/ask-service'
import {
resolveChatConversation,
persistUserTurn,
persistAssistantTurn,
} from '@/lib/agent/ask/persist'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
/**
@@ -27,6 +32,15 @@ const Schema = z.object({
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> {
@@ -73,7 +87,43 @@ export async function POST(request: Request): Promise<Response> {
)
}
// Stateless page-scoped ask: one answer, nothing written.
if (parsed.data.persist !== true) {
try {
const result = await answerAssistantQuestion({
supabase,
companyId,
question: parsed.data.question,
pageContext: parsed.data.context,
tier: parsed.data.tier,
})
return NextResponse.json({ data: result })
} catch (err) {
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
await persistUserTurn(supabase, conversationId, parsed.data.question)
const result = await answerAssistantQuestion({
supabase,
companyId,
@@ -81,7 +131,10 @@ export async function POST(request: Request): Promise<Response> {
pageContext: parsed.data.context,
tier: parsed.data.tier,
})
return NextResponse.json({ data: result })
await persistAssistantTurn(supabase, conversationId, result.answer)
return NextResponse.json({ data: { ...result, conversation_id: conversationId } })
} catch (err) {
return NextResponse.json({ error: getUserErrorMessage(err) }, { status: 500 })
}