Skip to content

Commit

Permalink
use enums
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Dec 20, 2024
1 parent 70d6aad commit 048e51b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
77 changes: 42 additions & 35 deletions src/participant/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ import { processStreamWithIdentifiers } from './streamParsing';
import type { PromptIntent } from './prompts/intent';
import { isPlayground, getSelectedText, getAllText } from '../utils/playground';
import type { DataService } from 'mongodb-data-service';
import { ParticipantErrorTypes } from './participantErrorTypes';
import {
ExportToPlaygroundFailure,
ParticipantErrorTypes,
} from './participantErrorTypes';
import type PlaygroundResultProvider from '../editors/playgroundResultProvider';
import { isExportToLanguageResult } from '../types/playgroundType';
import { PromptHistory } from './prompts/promptHistory';
Expand Down Expand Up @@ -1792,7 +1795,9 @@ export default class ParticipantController {
const codeToExport = getSelectedText() || getAllText();

try {
const content = await vscode.window.withProgress(
const contentOrFailure = await vscode.window.withProgress<
string | ExportToPlaygroundFailure | null
>(
{
location: vscode.ProgressLocation.Notification,
title: 'Exporting code to a playground...',
Expand All @@ -1805,69 +1810,71 @@ export default class ParticipantController {
request: { prompt: codeToExport },
});
} catch (error) {
void vscode.window.showErrorMessage(
'Failed to generate a MongoDB Playground. Please ensure your code block contains a MongoDB query.'
);

this._telemetryService.trackExportToPlaygroundFailed({
input_length: codeToExport?.length,
details: 'modelInput',
});
return null;
return ExportToPlaygroundFailure.STREAM_CHAT_RESPONSE;
}

const result = await Promise.race([
this.streamChatResponseWithExportToLanguage({
modelInput,
token,
}),
new Promise<'cancelled'>((resolve) =>
new Promise<ExportToPlaygroundFailure>((resolve) =>
token.onCancellationRequested(() => {
log.info('The export to a playground operation was canceled.');
resolve('cancelled');
resolve(ExportToPlaygroundFailure.CANCELLED);
})
),
]);

if (result === 'cancelled') {
return null;
if (result === ExportToPlaygroundFailure.CANCELLED) {
return ExportToPlaygroundFailure.CANCELLED;
}

if (!result || result?.includes("Sorry, I can't assist with that.")) {
void vscode.window.showErrorMessage(
'Failed to generate a MongoDB Playground. Please ensure your code block contains a MongoDB query.'
);

this._telemetryService.trackExportToPlaygroundFailed({
input_length: codeToExport?.length,
details: 'streamChatResponseWithExportToLanguage',
});

return null;
return ExportToPlaygroundFailure.STREAM_CHAT_RESPONSE;
}

return result;
}
);

if (!content) {
if (
!contentOrFailure ||
contentOrFailure === ExportToPlaygroundFailure.CANCELLED
) {
return true;
}

try {
await vscode.commands.executeCommand(
EXTENSION_COMMANDS.OPEN_PARTICIPANT_CODE_IN_PLAYGROUND,
{
runnableContent: content,
}
if (
Object.values(ExportToPlaygroundFailure).includes(
contentOrFailure as ExportToPlaygroundFailure
)
) {
void vscode.window.showErrorMessage(
'Failed to generate a MongoDB Playground. Please ensure your code block contains a MongoDB query.'
);

// Content in this case is already equal to the failureType; this is just to make it explicit
// and avoid accidentally sending actual contents of the message.
const failureType = Object.values(ExportToPlaygroundFailure).find(
(value) => value === contentOrFailure
);
} catch (error) {
this._telemetryService.trackExportToPlaygroundFailed({
input_length: content.length,
details: 'executeCommand',
input_length: codeToExport?.length,

details: failureType,
});
return false;
}

const content = contentOrFailure;
await vscode.commands.executeCommand(
EXTENSION_COMMANDS.OPEN_PARTICIPANT_CODE_IN_PLAYGROUND,
{
runnableContent: content,
}
);

return true;
} catch (error) {
const message = formatError(error).message;
Expand Down
6 changes: 6 additions & 0 deletions src/participant/participantErrorTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ export enum ParticipantErrorTypes {
OTHER = 'Other',
DOCS_CHATBOT_API = 'Docs Chatbot API Issue',
}

export enum ExportToPlaygroundFailure {
CANCELLED = 'cancelled',
MODEL_INPUT = 'modelInput',
STREAM_CHAT_RESPONSE = 'streamChatResponseWithExportToLanguage',
}
2 changes: 1 addition & 1 deletion src/telemetry/telemetryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type DocumentEditedTelemetryEventProperties = {

type ExportToPlaygroundFailedEventProperties = {
input_length: number | undefined;
details: string;
details?: string;
};

type PlaygroundExportedToLanguageTelemetryEventProperties = {
Expand Down

0 comments on commit 048e51b

Please sign in to comment.