Files
baserow/web-frontend/test/unit/agent/replies.spec.js
Davide Silvestri 9e93fb8e36 chore(agent): keep agent tool approvals visible and show failure details to builders (#6883)
* fix: keep agent approval controls visible on every pending call

* fix: show private tool failure details to agent builders

* fix: align approval buttons and wrap them as a group

* fix: highlight failed tool calls and show the reason on demand

* fix: preserve native disclosure buttons in tool history

* fix: label the batch action Always approve all

* fix: simplify approval rows and open tool errors in a context

* fix: give the always approve action more room

* fix: show failed tool calls once in approval cards

* feat: show live tool execution status in agent conversations

* fix: align tool status icons across approval rows

* fix: shorten batch button to Always approve

* fix: explain future tool access in approval tooltips

* fix: right-align tool statuses with trailing icons

* fix: match the tool spinner to the status icons

* fix: give approval buttons equal widths

* fix: explain email failures at the agent boundary

* fix: map known integration failures to safe agent messages

* fix: wrap tool statuses and refresh private diagnostics on demand

* fix: order the agent diagnostics migration and verify finish payloads
2026-11-27 10:20:12 +01:00

584 lines
21 KiB
JavaScript

import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { flushPromises } from '@vue/test-utils'
import service from '@baserow/modules/core/services/chatRuns'
import {
composer,
conversation,
createAgentPage,
deferredReply,
destroyAgentPage,
editInstructions,
incompleteConfiguration,
message,
messages,
mockConversations,
mockMessages,
mockSaves,
mockSendMessage,
mockStartConversation,
openSection,
page,
selectConversation,
send,
sharedConversation,
sendMessage,
stop,
useFakeTimers,
withoutConfiguration,
conversationsPage,
} from './helpers'
vi.mock('@baserow/modules/core/services/chatRuns', () => ({ default: vi.fn() }))
const existingConversation = conversation('conversation-a', 'Conversation A')
const prompt = message('m1', 'Hello', { id: 1 })
const reply = (status, content = 'Hi there', { id = 2, ...extra } = {}) =>
message(`reply-${id}`, content, { id, role: 'assistant', status, ...extra })
const runningReply = (runId = 'run-1', id = 2) =>
reply('running', '', { id, runId })
const error = (wrapper) => wrapper.find('.chat-message__bubble--error').text()
const note = (wrapper) => wrapper.find('.chat-message__note').text()
const activity = (wrapper) => wrapper.get('.chat-composer__activity')
const announcement = (wrapper) =>
wrapper.get('.agent-chat__announcement[role="status"]').text()
const liveReply = (wrapper) => wrapper.get('.chat-message__bubble[aria-live]')
const typing = (wrapper) => wrapper.find('.chat-typing').exists()
const spinning = (wrapper) => wrapper.find('.chat-loading__spinner').exists()
const toolStep = (id, tool, status, extra = {}) => ({
id,
tool,
status,
changes_data: tool === 'create_rows',
input: {},
facts: {},
...extra,
})
describe('Agent replies', () => {
let testApp, mount, allowed, api, streams, cursor
beforeEach(async () => {
;({ testApp, mount, allowed } = await createAgentPage())
testApp.authenticate({ id: 1 })
streams = {}
cursor = 0
api = {
get: vi.fn().mockResolvedValue({ status: 'running' }),
cancel: vi.fn().mockResolvedValue({ status: 'running' }),
stream: vi.fn((runId, from, onRecord) => {
streams[runId] = onRecord
return new Promise(() => {})
}),
}
service.mockReturnValue(api)
})
afterEach(() => destroyAgentPage(testApp))
async function receive(runId, kind, data) {
await streams[runId]({ kind, data, cursor: `a:${++cursor}-0` })
await flushPromises()
}
const streamText = (runId, text) =>
receive(runId, 'text.delta', { segment: 0, text })
const finish = (runId, status = 'completed', saved = null) =>
receive(runId, 'run.finished', { status, message: saved })
const messageReads = () =>
testApp.mock.history.get.filter(({ url }) => url.includes('/messages/'))
const withConversation = (...responses) => {
mockConversations(testApp, conversationsPage([existingConversation]))
mockMessages(
testApp,
existingConversation.uuid,
...responses.map((items) =>
typeof items === 'function' ? items : page(items)
)
)
}
async function startReply() {
mockStartConversation(testApp, ({ data }) => {
const body = JSON.parse(data)
return [
200,
{
conversation: conversation(body.uuid, body.content, undefined, 5),
message: message(body.message_uuid, body.content, { id: 1 }),
reply: runningReply(),
},
]
})
const wrapper = await mount()
await sendMessage(wrapper, 'Hello')
return wrapper
}
test('sends, streams and shows the saved reply its run ends with', async () => {
const wrapper = await startReply()
expect(testApp.store.state.chatRuns.subscriptions).toEqual({ agent: 5 })
expect(api.stream.mock.calls.map(([runId]) => runId)).toEqual(['run-1'])
expect(messages(wrapper)).toEqual(['Hello', ''])
expect(typing(wrapper)).toBe(true)
expect(activity(wrapper).text()).toBe('agent.thinking')
expect(announcement(wrapper)).toBe('agent.replying')
expect(stop(wrapper).attributes('aria-label')).toBe('agent.stop')
await streamText('run-1', 'Hel')
expect(messages(wrapper)).toEqual(['Hello', 'Hel'])
expect([typing(wrapper), spinning(wrapper)]).toEqual([false, false])
expect(liveReply(wrapper).attributes('aria-busy')).toBe('true')
expect(activity(wrapper).text()).toBe('agent.writing')
await finish('run-1', 'completed', reply('completed', '**Hello** there'))
await composer(wrapper).setValue('Next')
expect(messages(wrapper)).toEqual(['Hello', 'Hello there'])
expect(wrapper.get('.chat-markdown strong').text()).toBe('Hello')
expect(spinning(wrapper)).toBe(false)
expect(liveReply(wrapper).attributes('aria-busy')).toBe('false')
expect(activity(wrapper).classes()).not.toContain(
'chat-composer__activity--visible'
)
expect(send(wrapper).element.disabled).toBe(false)
expect(announcement(wrapper)).toBe('')
expect(messageReads()).toEqual([])
})
test('shows live tool activity and replaces it with the saved outcome', async () => {
const wrapper = await startReply()
const running = toolStep('call-1', 'create_rows', 'running', {
input: {
requested_count: 3,
values: { table_id: 1, rows: [{ Name: 'Planning' }] },
},
})
await receive('run-1', 'step', running)
expect(announcement(wrapper)).toBe('toolPresentation.stepAnnouncement')
expect(wrapper.get('.chat-step-group').attributes('aria-busy')).toBe('true')
expect(wrapper.get('.chat-step-group__title').text()).toContain('3')
expect(typing(wrapper)).toBe(false)
expect(activity(wrapper).text()).toContain(
'agent.toolCreateRowsRunningCount'
)
expect(activity(wrapper).text()).toContain('3')
const card = wrapper.get('.chat-step-group')
expect(card.find('.chat-step-group__input').exists()).toBe(false)
await card.get('button').trigger('click')
expect(card.get('table').text()).toContain('Planning')
const done = {
...running,
status: 'done',
facts: { count: 2, table: 'Tasks' },
}
await finish(
'run-1',
'completed',
reply('completed', 'Added rows', { steps: [done] })
)
expect(wrapper.findAll('.chat-step-group')).toHaveLength(1)
expect(wrapper.get('.chat-step-group').attributes('aria-busy')).toBe(
'false'
)
expect(wrapper.get('.chat-step-group__title').text()).toContain('2')
expect(
wrapper.find('.chat-step-group .iconoir-check-circle').exists()
).toBe(true)
expect(activity(wrapper).classes()).not.toContain(
'chat-composer__activity--visible'
)
expect(card.get('table').text()).toContain('Planning')
})
test('shows workspace reads as completed while the agent considers its next call', async () => {
const wrapper = await startReply()
const read = toolStep('read-1', 'list_tables', 'running')
await receive('run-1', 'step', read)
await receive('run-1', 'step', { ...read, status: 'done' })
expect(wrapper.get('.chat-step-group').attributes('aria-busy')).toBe(
'false'
)
expect(
wrapper
.get('.chat-step-group__header .chat-step-group__status--visible')
.text()
).toBe('toolPresentation.statusDone')
expect(typing(wrapper)).toBe(false)
await receive('run-1', 'step', toolStep('read-2', 'list_views', 'running'))
expect(wrapper.findAll('.chat-step-group__line')).toHaveLength(2)
expect(wrapper.get('.chat-step-group').attributes('aria-busy')).toBe('true')
await receive('run-1', 'step', toolStep('read-2', 'list_views', 'done'))
await streamText('run-1', 'Here are the results')
expect(wrapper.get('.chat-step-group').attributes('aria-busy')).toBe(
'false'
)
expect(wrapper.text()).toContain('Here are the results')
expect(typing(wrapper)).toBe(true)
})
test('names the first running call while other calls queue or wait for approval', async () => {
const wrapper = await startReply()
await receive('run-1', 'step', toolStep('held', 'create_rows', 'waiting'))
await receive('run-1', 'step', toolStep('a', 'list_tables', 'running'))
await receive('run-1', 'step', toolStep('b', 'create_rows', 'running'))
expect(activity(wrapper).text()).toContain('agent.toolListTablesRunning')
await receive('run-1', 'step', toolStep('a', 'list_tables', 'done'))
expect(activity(wrapper).text()).toContain('agent.toolCreateRowsRunning')
})
test('reloads grouped reads and failed calls from saved message steps', async () => {
withConversation([
prompt,
reply('completed', 'Finished', {
steps: [
toolStep('a', 'list_tables', 'done'),
toolStep('failed-read', 'list_table_rows', 'failed', {
error_code: 'not_found',
}),
toolStep('b', 'get_table_schema', 'done', {
facts: { table: 'Tasks' },
}),
toolStep('c', 'create_rows', 'failed', {
input: { values: { table_id: 1, rows: [{ Name: 'Planning' }] } },
}),
],
}),
])
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
expect(wrapper.findAll('.chat-step-group')).toHaveLength(2)
expect(
wrapper.find('.chat-step-group .iconoir-warning-triangle').exists()
).toBe(true)
const reads = wrapper.findAll('.chat-step-group')[0]
const header = reads.get('.chat-step-group__header')
expect(header.find('.iconoir-check-circle').exists()).toBe(true)
expect(header.find('.iconoir-warning-triangle').exists()).toBe(false)
expect(reads.findAll('li .chat-tool-failure')).toHaveLength(1)
expect(reads.findAll('li .iconoir-warning-triangle')).toHaveLength(1)
expect(reads.findAll('li .iconoir-check-circle')).toHaveLength(2)
expect(reads.get('button').classes()).not.toContain('button')
expect(reads.findAll('li')).toHaveLength(3)
await reads.get('button').trigger('click')
expect(reads.findAll('li')).toHaveLength(0)
const change = wrapper.findAll('.chat-step-group')[1]
expect(change.find('pre').exists()).toBe(false)
await change.get('button').trigger('click')
expect(change.get('table').text()).toContain('Planning')
expect(api.stream).not.toHaveBeenCalled()
})
test('keeps Stop while the configuration stops being ready', async () => {
mockSaves(testApp, incompleteConfiguration('instructions'))
const wrapper = await startReply()
await openSection(wrapper, 'agent.instructions')
await editInstructions(wrapper, '')
await stop(wrapper).trigger('click')
await flushPromises()
expect(api.cancel).toHaveBeenCalledExactlyOnceWith('run-1')
})
test('a streamed chunk updates only the reply being written', async () => {
withConversation([
prompt,
reply('completed'),
message('m3', 'More', { id: 3 }),
runningReply('run-4', 4),
])
const updated = []
const wrapper = await mount({
mixins: [
{
updated() {
if (this.$options.__name === 'AgentMessage') {
updated.push(this.message.uuid)
} else if (this.$options.__name === 'ChatStepGroup') {
updated.push('steps')
}
},
},
],
})
await selectConversation(wrapper, existingConversation.title)
await receive('run-4', 'step', toolStep('a', 'create_rows', 'running'))
updated.length = 0
await streamText('run-4', 'Hel')
await streamText('run-4', 'lo')
expect(updated).toEqual(['reply-4', 'reply-4'])
})
test('stops a reply and shows its persisted cancellation', async () => {
const wrapper = await startReply()
await streamText('run-1', 'Hel')
await stop(wrapper).trigger('click')
await flushPromises()
expect(api.cancel).toHaveBeenCalledExactlyOnceWith('run-1')
expect(document.activeElement).toBe(composer(wrapper).element)
await finish('run-1', 'cancelled', reply('cancelled', 'Hel!'))
expect(messages(wrapper)).toEqual(['Hello', 'Hel!'])
expect(note(wrapper)).toBe('agent.replyStopped')
expect(announcement(wrapper)).toBe('agent.replyStopped')
})
test('announces a reply whose run ended without a final status as interrupted', async () => {
const wrapper = await startReply()
await testApp.store.dispatch('chatRuns/unavailable', 'run-1')
await flushPromises()
expect(announcement(wrapper)).toBe('agent.replyInterrupted')
})
test('a call that changed nothing shows no card and is not announced', async () => {
const wrapper = await startReply()
await receive('run-1', 'step', toolStep('a', 'create_rows', 'skipped'))
expect(wrapper.find('.chat-step-group').exists()).toBe(false)
expect(announcement(wrapper)).toBe('agent.replying')
})
test('reopening a running reply announces it without repeating its old steps', async () => {
mockConversations(
testApp,
conversationsPage([
existingConversation,
conversation('conversation-b', 'Conversation B'),
])
)
mockMessages(
testApp,
existingConversation.uuid,
page([prompt, runningReply('run-2')])
)
mockMessages(testApp, 'conversation-b', page([]))
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
await receive('run-2', 'step', toolStep('a', 'list_tables', 'done'))
expect(announcement(wrapper)).toBe('toolPresentation.stepAnnouncement')
await selectConversation(wrapper, 'Conversation B')
await selectConversation(wrapper, existingConversation.title)
expect(announcement(wrapper)).toBe('agent.replying')
})
// Logging out suspends followers; reopening the chat must resume them or Stop stays.
test('follows an active reply on reload and recovers its saved outcome after logout', async () => {
withConversation([prompt, runningReply('run-2')])
let wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
await streamText('run-2', 'Partial')
expect(messages(wrapper)).toEqual(['Hello', 'Partial'])
await testApp.store.dispatch('chatRuns/disconnect')
wrapper.unmount()
wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
await finish('run-2', 'completed', reply('completed'))
expect(messages(wrapper)).toEqual(['Hello', 'Hi there'])
expect(send(wrapper).attributes('aria-label')).toBe('agent.send')
})
test('queues a failed outcome behind the refresh started by another tab', async () => {
const startedRefresh = deferredReply()
const otherPrompt = message('m3', 'From another tab', { id: 3 })
withConversation([prompt, reply('completed')], startedRefresh.reply, [
prompt,
reply('completed'),
otherPrompt,
reply('failed', 'Partial', { id: 4, errorCode: 'failed' }),
])
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
await testApp.store.dispatch('chatRuns/handleChanged', {
runId: 'run-4',
runType: 'agent',
chatId: existingConversation.id,
})
await flushPromises()
await receive('run-4', 'run.started', {})
await finish('run-4', 'failed')
startedRefresh.resolve(
200,
page([prompt, reply('completed'), otherPrompt, runningReply('run-4', 4)])
)
await flushPromises()
expect(messages(wrapper)).toEqual([
'Hello',
'Hi there',
'From another tab',
'Partial',
])
expect(error(wrapper)).toBe('agent.replyFailed')
})
test('follows a conflicting reply while preserving the draft and clears its refusal in another chat', async () => {
testApp.dontFailOnErrorResponses()
const otherPrompt = message('m3', 'Other tab', { id: 3 })
mockConversations(
testApp,
conversationsPage([
existingConversation,
conversation('conversation-b', 'Conversation B'),
])
)
mockMessages(
testApp,
existingConversation.uuid,
page([prompt]),
page([prompt, otherPrompt, runningReply('run-3', 4)]),
page([prompt, otherPrompt, reply('completed', 'Hi there', { id: 4 })])
)
mockMessages(testApp, 'conversation-b', page([]))
mockSendMessage(testApp, existingConversation.uuid, [
409,
{ error: 'ERROR_CHAT_RUN_IN_PROGRESS' },
])
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
await sendMessage(wrapper, 'Again')
expect(wrapper.get('#agent-chat-send-error').text()).toBe(
'agent.replyInProgress'
)
expect(composer(wrapper).element.readOnly).toBe(false)
expect(stop(wrapper).exists()).toBe(true)
expect(api.stream.mock.calls.map(([runId]) => runId)).toEqual(['run-3'])
await selectConversation(wrapper, 'Conversation B')
expect(wrapper.find('#agent-chat-send-error').exists()).toBe(false)
await finish('run-3')
await selectConversation(wrapper, existingConversation.title)
expect(messages(wrapper)).toEqual(['Hello', 'Other tab', 'Hi there'])
expect(composer(wrapper).element.value).toBe('Again')
expect(wrapper.find('#agent-chat-send-error').exists()).toBe(false)
expect(send(wrapper).attributes('aria-label')).toBe('agent.send')
})
test('clears a refusal if the other reply already ended before the refresh', async () => {
testApp.dontFailOnErrorResponses()
withConversation([prompt])
mockSendMessage(testApp, existingConversation.uuid, [
409,
{ error: 'ERROR_CHAT_RUN_IN_PROGRESS' },
])
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
await sendMessage(wrapper, 'Again')
expect(wrapper.find('#agent-chat-send-error').exists()).toBe(false)
expect(send(wrapper).attributes('aria-label')).toBe('agent.send')
})
test.each([
[
'failed',
'timeout',
'Partial',
['Hello', 'Partial'],
'agent.replyTimedOut',
],
['running', '', '', ['Hello'], 'agent.replyInterrupted'],
])(
'shows why a saved %s reply did not complete',
async (status, errorCode, content, expectedMessages, expected) => {
withConversation([prompt, reply(status, content, { errorCode })])
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
expect(messages(wrapper)).toEqual(expectedMessages)
expect(wrapper.findAll('.chat-message__bubble--error')).toHaveLength(1)
expect(error(wrapper)).toBe(expected)
useFakeTimers()
await vi.advanceTimersByTimeAsync(11000)
expect(error(wrapper)).toBe(expected)
}
)
test.each([
['the reply they asked for', { id: 1, name: 'Viewer' }, true],
["another member's reply", { id: 2, name: 'Ada' }, false],
])(
'lets a member who may not update the agent stop %s in a shared conversation: %s',
async (_, sender, canStop) => {
mockConversations(
testApp,
conversationsPage([{ ...existingConversation, shared: true }])
)
mockMessages(
testApp,
existingConversation.uuid,
page([prompt, reply('running', '', { runId: 'run-5', sender })])
)
allowed.value = withoutConfiguration
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
expect(wrapper.find('.chat-composer__button--stop').exists()).toBe(
canStop
)
}
)
test('tells a member who may no longer send why, with Stop for their running private reply', async () => {
withConversation([prompt, runningReply('run-6')])
allowed.value = (operation) =>
withoutConfiguration(operation) && !operation.includes('create')
const wrapper = await mount()
await selectConversation(wrapper, existingConversation.title)
expect(wrapper.find('.chat-composer').exists()).toBe(false)
expect(wrapper.get('.agent-chat__shared-text').text()).toBe(
'agent.cannotSendMessages'
)
await wrapper.get('.agent-chat__shared .button').trigger('click')
await flushPromises()
expect(api.cancel).toHaveBeenCalledExactlyOnceWith('run-6')
})
test('lets only members who may update the agent stop a trigger run', async () => {
const shared = sharedConversation('conversation-a', 'Conversation A')
mockConversations(testApp, conversationsPage([shared]))
mockMessages(
testApp,
shared.uuid,
page([
message('t1', 'Trigger data', { id: 1, role: 'trigger' }),
reply('running', '', { id: 4, runId: 'run-4', sender: null }),
])
)
allowed.value = withoutConfiguration
const wrapper = await mount()
await selectConversation(wrapper, 'Conversation A')
expect(wrapper.find('.chat-composer').exists()).toBe(true)
expect(wrapper.find('.chat-composer__button--stop').exists()).toBe(false)
allowed.value = () => true
await flushPromises()
await wrapper.get('.chat-composer__button--stop').trigger('click')
await flushPromises()
expect(api.cancel).toHaveBeenCalledExactlyOnceWith('run-4')
})
})