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>
This commit is contained in:
co-authored by
Jakob Wennberg
Claude Fable 5
parent
f75ea2384d
commit
3edbf0a2e3
@@ -3,6 +3,7 @@ import { createAiClient, toProviderModelId, type AiClient } from '../provider'
|
||||
import type { ResolvedAiConfig } from '../config'
|
||||
import { capabilitiesFor } from '../config'
|
||||
import type {
|
||||
AiChatTurn,
|
||||
AiDocumentInput,
|
||||
AiService,
|
||||
AiTier,
|
||||
@@ -39,6 +40,22 @@ const EMPTY_USAGE: AiUsage = {
|
||||
cacheReadInputTokens: 0,
|
||||
}
|
||||
|
||||
/**
|
||||
* Earlier turns as real message turns, then the prompt as the final user
|
||||
* message. With no history this is exactly the single-message array every
|
||||
* non-chat caller sent before (hosted stays byte-identical for them).
|
||||
*/
|
||||
function messagesFor(
|
||||
prompt: string,
|
||||
history: AiChatTurn[] | undefined,
|
||||
): Anthropic.Messages.MessageParam[] {
|
||||
const prior: Anthropic.Messages.MessageParam[] = (history ?? []).map((t) => ({
|
||||
role: t.role,
|
||||
content: t.text,
|
||||
}))
|
||||
return [...prior, { role: 'user', content: prompt }]
|
||||
}
|
||||
|
||||
/** Sum usage across the turns of a tool loop so the reported cost is truthful. */
|
||||
function addUsage(a: AiUsage, b: AiUsage): AiUsage {
|
||||
const s = (x: number | null, y: number | null) => (x ?? 0) + (y ?? 0)
|
||||
@@ -136,7 +153,7 @@ export function createAnthropicFamilyService(cfg: ResolvedAiConfig): AiService {
|
||||
model,
|
||||
max_tokens: req.maxTokens,
|
||||
...(req.system ? { system: req.system } : {}),
|
||||
messages: [{ role: 'user', content: req.prompt }],
|
||||
messages: messagesFor(req.prompt, req.history),
|
||||
}
|
||||
const resp = await getClient().messages.create(params)
|
||||
return { text: textOf(resp), model, usage: usageOf(resp) }
|
||||
@@ -151,7 +168,7 @@ export function createAnthropicFamilyService(cfg: ResolvedAiConfig): AiService {
|
||||
input_schema: t.jsonSchema as Anthropic.Messages.Tool['input_schema'],
|
||||
}))
|
||||
const byName = new Map<string, AiToolDef>(req.tools.map((t) => [t.name, t]))
|
||||
const messages: Anthropic.Messages.MessageParam[] = [{ role: 'user', content: req.prompt }]
|
||||
const messages: Anthropic.Messages.MessageParam[] = messagesFor(req.prompt, req.history)
|
||||
const maxSteps = req.maxSteps ?? DEFAULT_MAX_STEPS
|
||||
let usage = EMPTY_USAGE
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import { capabilitiesFor, type ResolvedAiConfig } from '../config'
|
||||
import { extractJsonObject } from '../json'
|
||||
import { rasterizePdf } from '../rasterize-pdf'
|
||||
import type {
|
||||
AiChatTurn,
|
||||
AiDocumentInput,
|
||||
AiService,
|
||||
AiTier,
|
||||
@@ -29,6 +30,16 @@ import type {
|
||||
|
||||
const DEFAULT_MAX_STEPS = 4
|
||||
|
||||
/** Earlier turns as message turns, then the prompt as the final user message. */
|
||||
function messagesWithHistory(prompt: string, history: AiChatTurn[]): ModelMessage[] {
|
||||
const prior: ModelMessage[] = history.map((t) =>
|
||||
t.role === 'assistant'
|
||||
? { role: 'assistant', content: t.text }
|
||||
: { role: 'user', content: t.text },
|
||||
)
|
||||
return [...prior, { role: 'user', content: prompt }]
|
||||
}
|
||||
|
||||
/**
|
||||
* Map our provider-agnostic tool defs onto the AI SDK's tool() shape. The SDK
|
||||
* runs the loop itself (calls execute, feeds the result back) up to the
|
||||
@@ -163,7 +174,12 @@ export function createOpenAICompatibleService(cfg: ResolvedAiConfig): AiService
|
||||
const result = await generateText({
|
||||
model: provider(model),
|
||||
...(req.system ? { system: req.system } : {}),
|
||||
prompt: req.prompt,
|
||||
// The SDK takes either `prompt` or `messages`, never both: a plain
|
||||
// single-turn call keeps `prompt`; a conversation sends the earlier
|
||||
// turns as real messages with the prompt as the final user turn.
|
||||
...(req.history && req.history.length > 0
|
||||
? { messages: messagesWithHistory(req.prompt, req.history) }
|
||||
: { prompt: req.prompt }),
|
||||
maxOutputTokens: req.maxTokens,
|
||||
...(tools ? { tools, stopWhen: stepCountIs(req.maxSteps ?? DEFAULT_MAX_STEPS) } : {}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user