Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<UIMessageChunk>({
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<typeof createScriptedTransport>) {
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
Expand Down
10 changes: 7 additions & 3 deletions packages/compass-assistant/src/compass-assistant-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
Loading