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
@@ -146,6 +146,36 @@ describe('generateText / generateStructured', () => {
|
||||
expect(result.text).toBe('{"ok":true}')
|
||||
})
|
||||
|
||||
it('generateText sends earlier turns as real message turns before the prompt', async () => {
|
||||
const svc = createAnthropicFamilyService(readAiConfig())
|
||||
await svc.generateText({
|
||||
tier: 'assistant',
|
||||
system: 'S',
|
||||
prompt: 'Och förra månaden?',
|
||||
maxTokens: 50,
|
||||
history: [
|
||||
{ role: 'user', text: 'Vad är min största utgift?' },
|
||||
{ role: 'assistant', text: '12 345 kr på 5010.' },
|
||||
],
|
||||
})
|
||||
expect(mockCreate.mock.calls[0][0].messages).toEqual([
|
||||
{ role: 'user', content: 'Vad är min största utgift?' },
|
||||
{ role: 'assistant', content: '12 345 kr på 5010.' },
|
||||
{ role: 'user', content: 'Och förra månaden?' },
|
||||
])
|
||||
})
|
||||
|
||||
it('generateText with an empty history is byte-identical to the single-turn call', async () => {
|
||||
const svc = createAnthropicFamilyService(readAiConfig())
|
||||
await svc.generateText({ tier: 'assistant', system: 'S', prompt: 'Hej', maxTokens: 50, history: [] })
|
||||
expect(mockCreate.mock.calls[0][0]).toEqual({
|
||||
model: 'eu.anthropic.claude-sonnet-5',
|
||||
max_tokens: 50,
|
||||
system: 'S',
|
||||
messages: [{ role: 'user', content: 'Hej' }],
|
||||
})
|
||||
})
|
||||
|
||||
it('generateStructured forces one named tool and returns its input', async () => {
|
||||
mockCreate.mockResolvedValueOnce({
|
||||
content: [{ type: 'tool_use', id: 't1', name: 'verdict', input: { paired: true } }],
|
||||
@@ -237,6 +267,29 @@ describe('generateText read-only tool loop', () => {
|
||||
expect(result.usage.outputTokens).toBe(13)
|
||||
})
|
||||
|
||||
it('keeps the earlier turns in front of the tool loop', async () => {
|
||||
const svc = createAnthropicFamilyService(readAiConfig())
|
||||
mockCreate.mockResolvedValueOnce({
|
||||
stop_reason: 'end_turn',
|
||||
content: [{ type: 'text', text: 'Svar.' }],
|
||||
usage: { input_tokens: 1, output_tokens: 1 },
|
||||
})
|
||||
await svc.generateText({
|
||||
tier: 'assistant',
|
||||
prompt: 'Och förra månaden?',
|
||||
maxTokens: 50,
|
||||
history: [
|
||||
{ role: 'user', text: 'Vad är min största utgift?' },
|
||||
{ role: 'assistant', text: '12 345 kr på 5010.' },
|
||||
],
|
||||
tools: [{ name: 't', description: 'd', jsonSchema: { type: 'object' }, execute: vi.fn() }],
|
||||
})
|
||||
const messages = mockCreate.mock.calls[0][0].messages
|
||||
expect(messages).toHaveLength(3)
|
||||
expect(messages[0]).toEqual({ role: 'user', content: 'Vad är min största utgift?' })
|
||||
expect(messages[2]).toEqual({ role: 'user', content: 'Och förra månaden?' })
|
||||
})
|
||||
|
||||
it('surfaces a tool failure as an is_error result rather than throwing', async () => {
|
||||
const execute = vi.fn().mockRejectedValue(new Error('report timeout'))
|
||||
const svc = createAnthropicFamilyService(readAiConfig())
|
||||
|
||||
@@ -102,6 +102,25 @@ describe('createOpenAICompatibleService', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('generateText sends earlier turns as message turns before the prompt', async () => {
|
||||
const svc = createOpenAICompatibleService(readAiConfig())
|
||||
await svc.generateText({
|
||||
tier: 'assistant',
|
||||
system: 'S',
|
||||
prompt: 'Och förra månaden?',
|
||||
maxTokens: 50,
|
||||
history: [
|
||||
{ role: 'user', text: 'Vad är min största utgift?' },
|
||||
{ role: 'assistant', text: '12 345 kr på 5010.' },
|
||||
],
|
||||
})
|
||||
const prompt = promptOf()
|
||||
expect(prompt.map((m) => m.role)).toEqual(['system', 'user', 'assistant', 'user'])
|
||||
expect(prompt[1].content).toEqual([{ type: 'text', text: 'Vad är min största utgift?' }])
|
||||
expect(prompt[2].content).toEqual([{ type: 'text', text: '12 345 kr på 5010.' }])
|
||||
expect(prompt[3].content).toEqual([{ type: 'text', text: 'Och förra månaden?' }])
|
||||
})
|
||||
|
||||
it('generateText forwards read-only tools to the model when provided', async () => {
|
||||
const svc = createOpenAICompatibleService(readAiConfig())
|
||||
const execute = vi.fn().mockResolvedValue({ ok: true })
|
||||
|
||||
Reference in New Issue
Block a user