From 274ea6abfaa61e5eb5ca5623cefc7eefc8961328 Mon Sep 17 00:00:00 2001 From: Eduarda Scharnhorst Date: Thu, 19 Nov 2026 17:56:36 +0000 Subject: [PATCH] fix(compass-assistant): resend after auto-run tool calls complete --- .../src/compass-assistant-provider.spec.tsx | 90 +++++++++++++++++++ .../src/compass-assistant-provider.tsx | 10 ++- 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/packages/compass-assistant/src/compass-assistant-provider.spec.tsx b/packages/compass-assistant/src/compass-assistant-provider.spec.tsx index 7e31e74380d..0e771f9900a 100644 --- a/packages/compass-assistant/src/compass-assistant-provider.spec.tsx +++ b/packages/compass-assistant/src/compass-assistant-provider.spec.tsx @@ -9,6 +9,7 @@ import { } from '@mongodb-js/testing-library-compass'; import { expect } from 'chai'; import sinon from 'sinon'; +import type { UIMessageChunk } from 'ai'; import { DrawerAnchor, @@ -1385,6 +1386,95 @@ describe('CompassAssistantProvider', function () { ); }); + describe('sending automatically after tool calls', function () { + function createScriptedTransport(responses: UIMessageChunk[][]) { + const sendMessages = sinon.stub(); + responses.forEach((chunks, i) => { + sendMessages.onCall(i).callsFake(() => + Promise.resolve( + new ReadableStream({ + start(controller) { + chunks.forEach((chunk) => controller.enqueue(chunk)); + controller.close(); + }, + }) + ) + ); + }); + return { + sendMessages, + reconnectToStream: () => Promise.resolve(null), + }; + } + + const toolStep: UIMessageChunk[] = [ + { type: 'start' }, + { type: 'start-step' }, + { + type: 'tool-input-available', + toolCallId: 'call-1', + toolName: 'list-databases', + input: {}, + }, + { + type: 'tool-output-available', + toolCallId: 'call-1', + output: { databases: [] }, + }, + { type: 'finish-step' }, + { type: 'finish' }, + ]; + + const answerStep: UIMessageChunk[] = [ + { type: 'start' }, + { type: 'start-step' }, + { type: 'text-start', id: 't1' }, + { type: 'text-delta', id: 't1', delta: 'No databases found' }, + { type: 'text-end', id: 't1' }, + { type: 'finish-step' }, + { type: 'finish' }, + ]; + + function createChat(transport: ReturnType) { + return createDefaultChat({ + options: { transport }, + originForPrompt: 'foo', + appNameForPrompt: 'bar', + atlasService: { + assistantApiEndpoint: () => 'https://example.com/assistant/api/v1', + } as unknown as AtlasService, + logger: createNoopLogger(), + track: () => undefined, + }); + } + + it('asks the model to respond after a tool that needed no approval ran', async function () { + const transport = createScriptedTransport([toolStep, answerStep]); + const chat = createChat(transport); + + await chat.sendMessage({ text: 'list my databases' }); + + await waitFor(() => { + expect(transport.sendMessages).to.have.been.calledTwice; + }); + await waitFor(() => { + expect(chat.status).to.equal('ready'); + const last = chat.messages[chat.messages.length - 1]; + expect(last.parts.some((p) => p.type === 'text')).to.be.true; + }); + }); + + it('does not send again after a plain text response', async function () { + const transport = createScriptedTransport([answerStep]); + const chat = createChat(transport); + + await chat.sendMessage({ text: 'hello' }); + await waitFor(() => expect(chat.status).to.equal('ready')); + + expect(transport.sendMessages).to.have.been.calledOnce; + }); + }); + describe('model selection', function () { async function sendMessageAndGetRequestedModel( enableSkillsInAssistant: boolean diff --git a/packages/compass-assistant/src/compass-assistant-provider.tsx b/packages/compass-assistant/src/compass-assistant-provider.tsx index 59a3bf08659..bec825839eb 100644 --- a/packages/compass-assistant/src/compass-assistant-provider.tsx +++ b/packages/compass-assistant/src/compass-assistant-provider.tsx @@ -47,7 +47,10 @@ import { import type { AtlasAdminApiService } from '@mongodb-js/atlas-admin-api/provider'; import { atlasAdminApiServiceLocator } from '@mongodb-js/atlas-admin-api/provider'; import { createOpenAI } from '@ai-sdk/openai'; -import { lastAssistantMessageIsCompleteWithApprovalResponses } from 'ai'; +import { + lastAssistantMessageIsCompleteWithApprovalResponses, + lastAssistantMessageIsCompleteWithToolCalls, +} from 'ai'; import type { ToolSet } from 'ai'; import type { CollectionSubtab, @@ -1006,8 +1009,9 @@ export function createDefaultChat({ }, }).responses(modelVersion), }), - sendAutomaticallyWhen: - lastAssistantMessageIsCompleteWithApprovalResponses, + sendAutomaticallyWhen: (options) => + lastAssistantMessageIsCompleteWithApprovalResponses(options) || + lastAssistantMessageIsCompleteWithToolCalls(options), onError: (err: Error) => { logger.log.error( logger.mongoLogId(1_001_000_370),