diff --git a/src/common/Utils.ts b/src/common/Utils.ts index 0eddc9e2..30e7c934 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -8,7 +8,8 @@ import * as vscode from "vscode"; import { EXTENSION_ID, EXTENSION_NAME, SETTINGS_EXPERIMENTAL_STORE_NAME } from "../client/constants"; import { CUSTOM_TELEMETRY_FOR_POWER_PAGES_SETTING_NAME } from "./OneDSLoggerTelemetry/telemetryConstants"; import { PacWrapper } from "../client/pac/PacWrapper"; -import { AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, PAC_SUCCESS } from "./copilot/constants"; +import { AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, DataverseEntityNameMap, EntityFieldMap, FieldTypeMap, PAC_SUCCESS } from "./copilot/constants"; +import { IActiveFileData, IActiveFileParams } from "./copilot/model"; export function getSelectedCode(editor: vscode.TextEditor): string { if (!editor) { @@ -152,3 +153,29 @@ export async function createAuthProfileExp(pacWrapper: PacWrapper | undefined) { return; } } + +export function getActiveEditorContent(): IActiveFileData { + const activeEditor = vscode.window.activeTextEditor; + const activeFileData: IActiveFileData = { + activeFileContent: '', + activeFileParams: { + dataverseEntity: '', + entityField: '', + fieldType: '' + } as IActiveFileParams + }; + if (activeEditor) { + const document = activeEditor.document; + const fileName = document.fileName; + const relativeFileName = vscode.workspace.asRelativePath(fileName); + + const activeFileParams: string[] = getLastThreePartsOfFileName(relativeFileName); + + activeFileData.activeFileContent = document.getText(); + activeFileData.activeFileParams.dataverseEntity = DataverseEntityNameMap.get(activeFileParams[0]) || ""; + activeFileData.activeFileParams.entityField = EntityFieldMap.get(activeFileParams[1]) || ""; + activeFileData.activeFileParams.fieldType = FieldTypeMap.get(activeFileParams[2]) || ""; + } + + return activeFileData; +} diff --git a/src/common/copilot/PowerPagesCopilot.ts b/src/common/copilot/PowerPagesCopilot.ts index e5049451..3df7c01d 100644 --- a/src/common/copilot/PowerPagesCopilot.ts +++ b/src/common/copilot/PowerPagesCopilot.ts @@ -10,9 +10,9 @@ import { dataverseAuthentication, intelligenceAPIAuthentication } from "../Authe import { v4 as uuidv4 } from 'uuid' import { PacWrapper } from "../../client/pac/PacWrapper"; import { ITelemetry } from "../../client/telemetry/ITelemetry"; -import { ADX_ENTITYFORM, ADX_ENTITYLIST, AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, AuthProfileNotFound, COPILOT_UNAVAILABLE, CopilotDisclaimer, CopilotStylePathSegments, DataverseEntityNameMap, EXPLAIN_CODE, EntityFieldMap, FieldTypeMap, PAC_SUCCESS, SELECTED_CODE_INFO, SELECTED_CODE_INFO_ENABLED, THUMBS_DOWN, THUMBS_UP, UserPrompt, WebViewMessage, sendIconSvg } from "./constants"; -import { IActiveFileParams, IActiveFileData, IOrgInfo } from './model'; -import { createAuthProfileExp, escapeDollarSign, getLastThreePartsOfFileName, getNonce, getSelectedCode, getSelectedCodeLineRange, getUserName, openWalkthrough, showConnectedOrgMessage, showInputBoxAndGetOrgUrl, showProgressWithNotification } from "../Utils"; +import { ADX_ENTITYFORM, ADX_ENTITYLIST, AUTH_CREATE_FAILED, AUTH_CREATE_MESSAGE, AuthProfileNotFound, COPILOT_UNAVAILABLE, CopilotDisclaimer, CopilotStylePathSegments, EXPLAIN_CODE, PAC_SUCCESS, SELECTED_CODE_INFO, SELECTED_CODE_INFO_ENABLED, THUMBS_DOWN, THUMBS_UP, UserPrompt, WebViewMessage, sendIconSvg } from "./constants"; +import { IActiveFileParams, IOrgInfo } from './model'; +import { createAuthProfileExp, escapeDollarSign, getActiveEditorContent, getNonce, getSelectedCode, getSelectedCodeLineRange, getUserName, openWalkthrough, showConnectedOrgMessage, showInputBoxAndGetOrgUrl, showProgressWithNotification } from "../Utils"; import { CESUserFeedback } from "./user-feedback/CESSurvey"; import { ActiveOrgOutput } from "../../client/pac/PacTypes"; import { CopilotWalkthroughEvent, CopilotCopyCodeToClipboardEvent, CopilotInsertCodeToEditorEvent, CopilotLoadedEvent, CopilotOrgChangedEvent, CopilotUserFeedbackThumbsDownEvent, CopilotUserFeedbackThumbsUpEvent, CopilotUserPromptedEvent, CopilotCodeLineCountEvent, CopilotClearChatEvent, CopilotNotAvailable, CopilotExplainCode, CopilotExplainCodeSize, CopilotNotAvailableECSConfig } from "./telemetry/telemetryConstants"; @@ -230,7 +230,7 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider { sendTelemetryEvent(this.telemetry, { eventName: CopilotUserPromptedEvent, copilotSessionId: sessionID, aibEndpoint: this.aibEndpoint ?? '', orgId: orgID, isSuggestedPrompt: String(data.value.isSuggestedPrompt) }); //TODO: Add active Editor info orgID ? (async () => { - const { activeFileParams } = this.getActiveEditorContent(); + const { activeFileParams } = getActiveEditorContent(); await this.authenticateAndSendAPIRequest(data.value.userPrompt, activeFileParams, orgID, this.telemetry); })() : (() => { @@ -426,32 +426,6 @@ export class PowerPagesCopilot implements vscode.WebviewViewProvider { } } - private getActiveEditorContent(): IActiveFileData { - const activeEditor = vscode.window.activeTextEditor; - const activeFileData: IActiveFileData = { - activeFileContent: '', - activeFileParams: { - dataverseEntity: '', - entityField: '', - fieldType: '' - } as IActiveFileParams - }; - if (activeEditor) { - const document = activeEditor.document; - const fileName = document.fileName; - const relativeFileName = vscode.workspace.asRelativePath(fileName); - - const activeFileParams: string[] = getLastThreePartsOfFileName(relativeFileName); - - activeFileData.activeFileContent = document.getText(); - activeFileData.activeFileParams.dataverseEntity = DataverseEntityNameMap.get(activeFileParams[0]) || ""; - activeFileData.activeFileParams.entityField = EntityFieldMap.get(activeFileParams[1]) || ""; - activeFileData.activeFileParams.fieldType = FieldTypeMap.get(activeFileParams[2]) || ""; - } - - return activeFileData; - } - public sendMessageToWebview(message: WebViewMessage) { if (this._view) { this._view.webview.postMessage(message);