Skip to content

Commit 548f247

Browse files
committed
Merge branch 'gagik/fix-long-prompts' of github.com:mongodb-js/vscode into gagik/filter-namespace
2 parents bec38d8 + 2445dc5 commit 548f247

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

src/participant/participant.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
} from './prompts/schema';
3636
import {
3737
chatResultFeedbackKindToTelemetryValue,
38-
ParticipantErrorTypes,
3938
TelemetryEventTypes,
4039
} from '../telemetry/telemetryService';
4140
import { DocsChatbotAIService } from './docsChatbotAIService';
@@ -45,6 +44,7 @@ import type { ModelInput } from './prompts/promptBase';
4544
import { processStreamWithIdentifiers } from './streamParsing';
4645
import type { PromptIntent } from './prompts/intent';
4746
import type { DataService } from 'mongodb-data-service';
47+
import { ParticipantErrorTypes } from '../types/participantErrorTypes';
4848

4949
const log = createLogger('participant');
5050

src/participant/prompts/promptBase.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
InternalPromptPurpose,
55
ParticipantPromptProperties,
66
} from '../../telemetry/telemetryService';
7+
import { ParticipantErrorTypes } from '../../types/participantErrorTypes';
78

89
export interface PromptArgsBase {
910
request: {
@@ -188,6 +189,16 @@ export abstract class PromptBase<TArgs extends PromptArgsBase> {
188189
}
189190

190191
if (historyItem instanceof vscode.ChatResponseTurn) {
192+
if (
193+
historyItem.result.errorDetails?.message ===
194+
ParticipantErrorTypes.FILTERED
195+
) {
196+
// If the response led to a filtered error, we do not want the
197+
// error-causing message to be sent again so we remove it.
198+
messages.pop();
199+
continue;
200+
}
201+
191202
let message = '';
192203

193204
// Skip a response to an empty user prompt message or connect message.

src/telemetry/telemetryService.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { NewConnectionTelemetryEventProperties } from './connectionTelemetr
1313
import type { ShellEvaluateResult } from '../types/playgroundType';
1414
import type { StorageController } from '../storage';
1515
import type { ParticipantResponseType } from '../participant/constants';
16+
import { ParticipantErrorTypes } from '../types/participantErrorTypes';
1617

1718
const log = createLogger('telemetry');
1819
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -193,14 +194,6 @@ export enum TelemetryEventTypes {
193194
PARTICIPANT_RESPONSE_GENERATED = 'Participant Response Generated',
194195
}
195196

196-
export enum ParticipantErrorTypes {
197-
CHAT_MODEL_OFF_TOPIC = 'Chat Model Off Topic',
198-
INVALID_PROMPT = 'Invalid Prompt',
199-
FILTERED = 'Filtered by Responsible AI Service',
200-
OTHER = 'Other',
201-
DOCS_CHATBOT_API = 'Docs Chatbot API Issue',
202-
}
203-
204197
/**
205198
* This controller manages telemetry.
206199
*/

src/test/suite/participant/participant.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Prompts } from '../../../participant/prompts';
3434
import { createMarkdownLink } from '../../../participant/markdown';
3535
import EXTENSION_COMMANDS from '../../../commands';
3636
import { getContentLength } from '../../../participant/prompts/promptBase';
37+
import { ParticipantErrorTypes } from '../../../types/participantErrorTypes';
3738

3839
// The Copilot's model in not available in tests,
3940
// therefore we need to mock its methods and returning values.
@@ -2502,6 +2503,68 @@ Schema:
25022503
getContentLength(messages[1])
25032504
);
25042505
});
2506+
2507+
suite('with invalid messages', function () {
2508+
test('filters disallowed messages', async function () {
2509+
const chatRequestMock = {
2510+
prompt: 'find all docs by a name example',
2511+
};
2512+
2513+
chatContextStub = {
2514+
history: [
2515+
Object.assign(Object.create(vscode.ChatRequestTurn.prototype), {
2516+
prompt: 'give me the count of all people in the prod database',
2517+
command: 'query',
2518+
references: [],
2519+
participant: CHAT_PARTICIPANT_ID,
2520+
}),
2521+
Object.assign(Object.create(vscode.ChatRequestTurn.prototype), {
2522+
prompt: 'some disallowed message',
2523+
command: 'query',
2524+
references: [],
2525+
participant: CHAT_PARTICIPANT_ID,
2526+
}),
2527+
Object.assign(Object.create(vscode.ChatResponseTurn.prototype), {
2528+
result: {
2529+
errorDetails: {
2530+
message: ParticipantErrorTypes.FILTERED,
2531+
},
2532+
},
2533+
response: [],
2534+
participant: CHAT_PARTICIPANT_ID,
2535+
}),
2536+
Object.assign(Object.create(vscode.ChatRequestTurn.prototype), {
2537+
prompt: 'ok message',
2538+
references: [],
2539+
participant: CHAT_PARTICIPANT_ID,
2540+
}),
2541+
],
2542+
};
2543+
const { messages } = await Prompts.generic.buildMessages({
2544+
context: chatContextStub,
2545+
request: chatRequestMock,
2546+
connectionNames: [],
2547+
});
2548+
2549+
expect(messages).to.have.lengthOf(4);
2550+
2551+
const messageContents = messages.map((message) => {
2552+
// There may be different types for the messages' content
2553+
const content = Array.isArray(message.content)
2554+
? message.content.map((sub) => sub.value).join('')
2555+
: message.content;
2556+
2557+
return content;
2558+
});
2559+
2560+
// Skip the preset prompt and check that the rest are correct.
2561+
expect(messageContents.slice(1)).deep.equal([
2562+
'give me the count of all people in the prod database',
2563+
'ok message',
2564+
'find all docs by a name example',
2565+
]);
2566+
});
2567+
});
25052568
});
25062569

25072570
suite('telemetry', function () {

src/types/participantErrorTypes.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum ParticipantErrorTypes {
2+
CHAT_MODEL_OFF_TOPIC = 'Chat Model Off Topic',
3+
INVALID_PROMPT = 'Invalid Prompt',
4+
FILTERED = 'Filtered by Responsible AI Service',
5+
OTHER = 'Other',
6+
DOCS_CHATBOT_API = 'Docs Chatbot API Issue',
7+
}

0 commit comments

Comments
 (0)