Files
accounted/components/agent/ChatNewStarter.tsx
T
Jakob Wennberg 3f6f1ab06e 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>
2026-08-20 20:35:16 +02:00

82 lines
2.9 KiB
TypeScript

'use client'
import { useRouter } from 'next/navigation'
import { useState } from 'react'
import AgentChat from './AgentChat'
import AskConsole from './AskConsole'
import { CHAT_INTENT_ID } from '@/lib/agent/ask/persist'
import AgentAvatar from './AgentAvatar'
import SandboxAgentPreview from './SandboxAgentPreview'
import { useAgentSheet } from './AgentSheetProvider'
import { useCompanyOptional } from '@/contexts/CompanyContext'
// Inline starter used by suggestion chips and ⌘K. Mirrors ChatIntakeStarter
// but accepts any intent + seed so we don't fork the intake-specific
// onboarding path. When AgentChat emits the new conversation_id, the URL
// is swapped to /chat/[id] so reload / share / browser-back all work.
export default function ChatNewStarter({
intentId,
seedUserMessage,
}: {
intentId: string
seedUserMessage?: string
}) {
const router = useRouter()
const { identity } = useAgentSheet()
const companyCtx = useCompanyOptional()
const isSandbox = companyCtx?.isSandbox ?? false
const agentName = identity.displayName?.trim() || 'Din assistent'
const [swapped, setSwapped] = useState(false)
const isSingleCall = intentId === CHAT_INTENT_ID
const swapToConversation = (id: string) => {
// Swap once, after the turn is created. For the single-call console both
// the question and the answer are already persisted by the time we get the
// id back, so /chat/[id] hydrates the full thread.
if (swapped) return
setSwapped(true)
router.replace(`/chat/${id}`)
}
return (
<>
<header className="flex items-center gap-3 border-b border-border px-6 py-4 shrink-0">
<AgentAvatar avatarId={identity.avatarId} size="sm" alt={agentName} />
<div className="min-w-0">
<h1 className="font-display text-lg tracking-tight truncate">{agentName}</h1>
<p className="text-xs text-muted-foreground truncate">
{isSandbox ? 'Förhandsvisning: avstängd i sandlådan' : 'Ny konversation'}
</p>
</div>
</header>
<div className="flex-1 min-h-0 flex flex-col">
{isSandbox ? (
<SandboxAgentPreview agentName={agentName} />
) : isSingleCall ? (
<AskConsole
seedUserMessage={seedUserMessage}
initialConversationId={null}
onConversationCreated={swapToConversation}
scrollerClassName="px-6 py-8"
/>
) : (
<AgentChat
intentId={intentId}
seedUserMessage={seedUserMessage}
initialMessages={[]}
initialConversationId={null}
onFirstTurnComplete={(id) => {
// Wait for the first turn to finish before swapping the URL:
// otherwise the unmount aborts the in-flight stream and
// /chat/[id] hydrates with only the user message.
swapToConversation(id)
}}
scrollerClassName="px-6 py-8"
/>
)}
</div>
</>
)
}