2023-06-06 19:43:53 +01:00
|
|
|
/* eslint-disable no-console */
|
2023-07-12 00:35:54 +01:00
|
|
|
import { FeedOptions } from "@azure/cosmos";
|
2023-06-16 08:25:23 +01:00
|
|
|
import {
|
|
|
|
Callout,
|
|
|
|
CommandBarButton,
|
2023-06-27 21:43:10 +01:00
|
|
|
DefaultButton,
|
2023-06-16 08:25:23 +01:00
|
|
|
DirectionalHint,
|
2023-06-27 21:43:10 +01:00
|
|
|
IButtonStyles,
|
2023-06-16 08:25:23 +01:00
|
|
|
IconButton,
|
|
|
|
Image,
|
|
|
|
Link,
|
|
|
|
Separator,
|
|
|
|
Spinner,
|
|
|
|
Stack,
|
2023-07-07 08:08:41 +01:00
|
|
|
TeachingBubble,
|
2023-06-16 08:25:23 +01:00
|
|
|
Text,
|
|
|
|
TextField,
|
|
|
|
} from "@fluentui/react";
|
2023-07-07 08:08:41 +01:00
|
|
|
import { useBoolean } from "@fluentui/react-hooks";
|
2023-08-21 15:16:32 +01:00
|
|
|
import {
|
2023-08-24 20:56:31 +01:00
|
|
|
PoolIdType,
|
2023-08-21 15:16:32 +01:00
|
|
|
QueryCopilotSampleContainerId,
|
|
|
|
QueryCopilotSampleContainerSchema,
|
|
|
|
ShortenedQueryCopilotSampleContainerSchema,
|
|
|
|
} from "Common/Constants";
|
2023-06-06 19:43:53 +01:00
|
|
|
import { getErrorMessage, handleError } from "Common/ErrorHandlingUtils";
|
|
|
|
import { shouldEnableCrossPartitionKey } from "Common/HeadersUtility";
|
|
|
|
import { MinimalQueryIterator } from "Common/IteratorUtilities";
|
2023-08-18 09:47:19 +01:00
|
|
|
import { createUri } from "Common/UrlUtility";
|
2023-06-06 19:43:53 +01:00
|
|
|
import { queryDocumentsPage } from "Common/dataAccess/queryDocumentsPage";
|
|
|
|
import { QueryResults } from "Contracts/ViewModels";
|
|
|
|
import { CommandButtonComponentProps } from "Explorer/Controls/CommandButton/CommandButtonComponent";
|
|
|
|
import { EditorReact } from "Explorer/Controls/Editor/EditorReact";
|
|
|
|
import { useCommandBar } from "Explorer/Menus/CommandBar/CommandBarComponentAdapter";
|
2023-08-18 09:47:19 +01:00
|
|
|
import { useNotebook } from "Explorer/Notebook/useNotebook";
|
2023-06-06 19:43:53 +01:00
|
|
|
import { SaveQueryPane } from "Explorer/Panes/SaveQueryPane/SaveQueryPane";
|
2023-07-06 08:49:34 +01:00
|
|
|
import { WelcomeModal } from "Explorer/QueryCopilot/Modal/WelcomeModal";
|
2023-06-28 09:11:03 +01:00
|
|
|
import { CopyPopup } from "Explorer/QueryCopilot/Popup/CopyPopup";
|
|
|
|
import { DeletePopup } from "Explorer/QueryCopilot/Popup/DeletePopup";
|
2023-08-24 08:07:29 +01:00
|
|
|
import { querySampleDocuments } from "Explorer/QueryCopilot/QueryCopilotUtilities";
|
|
|
|
import { SubmitFeedback } from "Explorer/QueryCopilot/Shared/QueryCopilotClient";
|
|
|
|
import { GenerateSQLQueryResponse, QueryCopilotProps } from "Explorer/QueryCopilot/Shared/QueryCopilotInterfaces";
|
2023-08-16 09:10:21 +01:00
|
|
|
import { SamplePrompts, SamplePromptsProps } from "Explorer/QueryCopilot/Shared/SamplePrompts/SamplePrompts";
|
2023-06-06 19:43:53 +01:00
|
|
|
import { QueryResultSection } from "Explorer/Tabs/QueryTab/QueryResultSection";
|
2023-07-27 19:41:41 +01:00
|
|
|
import { Action } from "Shared/Telemetry/TelemetryConstants";
|
|
|
|
import { traceFailure, traceStart, traceSuccess } from "Shared/Telemetry/TelemetryProcessor";
|
2023-06-27 21:43:10 +01:00
|
|
|
import { userContext } from "UserContext";
|
2023-06-06 19:43:53 +01:00
|
|
|
import { queryPagesUntilContentPresent } from "Utils/QueryUtils";
|
2023-06-16 08:25:23 +01:00
|
|
|
import { useQueryCopilot } from "hooks/useQueryCopilot";
|
2023-06-06 19:43:53 +01:00
|
|
|
import { useSidePanel } from "hooks/useSidePanel";
|
2023-07-07 08:08:41 +01:00
|
|
|
import React, { useRef, useState } from "react";
|
2023-06-06 19:43:53 +01:00
|
|
|
import SplitterLayout from "react-splitter-layout";
|
|
|
|
import ExecuteQueryIcon from "../../../images/ExecuteQuery.svg";
|
2023-06-27 21:43:10 +01:00
|
|
|
import HintIcon from "../../../images/Hint.svg";
|
2023-06-28 09:11:03 +01:00
|
|
|
import CopilotIcon from "../../../images/QueryCopilotNewLogo.svg";
|
2023-06-27 21:43:10 +01:00
|
|
|
import RecentIcon from "../../../images/Recent.svg";
|
2023-07-03 21:49:40 +01:00
|
|
|
import XErrorMessage from "../../../images/X-errorMessage.svg";
|
2023-06-06 19:43:53 +01:00
|
|
|
import SaveQueryIcon from "../../../images/save-cosmos.svg";
|
2023-06-26 08:13:30 +01:00
|
|
|
import { useTabs } from "../../hooks/useTabs";
|
2023-06-06 19:43:53 +01:00
|
|
|
|
2023-07-03 21:49:40 +01:00
|
|
|
interface SuggestedPrompt {
|
|
|
|
id: number;
|
|
|
|
text: string;
|
|
|
|
}
|
|
|
|
|
2023-06-27 21:43:10 +01:00
|
|
|
const promptStyles: IButtonStyles = {
|
|
|
|
root: { border: 0, selectors: { ":hover": { outline: "1px dashed #605e5c" } } },
|
|
|
|
label: { fontWeight: 400, textAlign: "left", paddingLeft: 8 },
|
|
|
|
};
|
|
|
|
|
2023-08-24 08:07:29 +01:00
|
|
|
export const QueryCopilotTab: React.FC<QueryCopilotProps> = ({ explorer }: QueryCopilotProps): JSX.Element => {
|
2023-07-07 08:08:41 +01:00
|
|
|
const [copilotTeachingBubbleVisible, { toggle: toggleCopilotTeachingBubbleVisible }] = useBoolean(false);
|
|
|
|
const inputEdited = useRef(false);
|
2023-08-03 08:23:31 +01:00
|
|
|
const {
|
|
|
|
hideFeedbackModalForLikedQueries,
|
|
|
|
userPrompt,
|
|
|
|
setUserPrompt,
|
|
|
|
generatedQuery,
|
|
|
|
setGeneratedQuery,
|
|
|
|
query,
|
|
|
|
setQuery,
|
|
|
|
selectedQuery,
|
|
|
|
setSelectedQuery,
|
|
|
|
isGeneratingQuery,
|
|
|
|
setIsGeneratingQuery,
|
|
|
|
isExecuting,
|
|
|
|
setIsExecuting,
|
|
|
|
likeQuery,
|
|
|
|
setLikeQuery,
|
|
|
|
dislikeQuery,
|
|
|
|
setDislikeQuery,
|
|
|
|
showCallout,
|
|
|
|
setShowCallout,
|
|
|
|
showSamplePrompts,
|
|
|
|
setShowSamplePrompts,
|
|
|
|
queryIterator,
|
|
|
|
setQueryIterator,
|
|
|
|
queryResults,
|
|
|
|
setQueryResults,
|
|
|
|
errorMessage,
|
|
|
|
setErrorMessage,
|
|
|
|
isSamplePromptsOpen,
|
|
|
|
setIsSamplePromptsOpen,
|
|
|
|
showDeletePopup,
|
|
|
|
setShowDeletePopup,
|
|
|
|
showFeedbackBar,
|
|
|
|
setShowFeedbackBar,
|
|
|
|
showCopyPopup,
|
|
|
|
setshowCopyPopup,
|
|
|
|
showErrorMessageBar,
|
|
|
|
setShowErrorMessageBar,
|
|
|
|
generatedQueryComments,
|
|
|
|
setGeneratedQueryComments,
|
2023-08-18 09:47:19 +01:00
|
|
|
shouldAllocateContainer,
|
|
|
|
setShouldAllocateContainer,
|
2023-08-03 08:23:31 +01:00
|
|
|
} = useQueryCopilot();
|
2023-06-28 09:11:03 +01:00
|
|
|
|
|
|
|
const sampleProps: SamplePromptsProps = {
|
|
|
|
isSamplePromptsOpen: isSamplePromptsOpen,
|
|
|
|
setIsSamplePromptsOpen: setIsSamplePromptsOpen,
|
|
|
|
setTextBox: setUserPrompt,
|
|
|
|
};
|
|
|
|
|
|
|
|
const copyGeneratedCode = () => {
|
|
|
|
if (!query) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const queryElement = document.createElement("textarea");
|
|
|
|
queryElement.value = query;
|
|
|
|
document.body.appendChild(queryElement);
|
|
|
|
queryElement.select();
|
|
|
|
document.execCommand("copy");
|
|
|
|
document.body.removeChild(queryElement);
|
|
|
|
|
|
|
|
setshowCopyPopup(true);
|
|
|
|
setTimeout(() => {
|
|
|
|
setshowCopyPopup(false);
|
|
|
|
}, 6000);
|
|
|
|
};
|
2023-06-06 19:43:53 +01:00
|
|
|
|
2023-06-27 21:43:10 +01:00
|
|
|
const cachedHistoriesString = localStorage.getItem(`${userContext.databaseAccount?.id}-queryCopilotHistories`);
|
2023-07-17 19:10:41 +01:00
|
|
|
const cachedHistories = cachedHistoriesString?.split("|");
|
2023-06-27 21:43:10 +01:00
|
|
|
const [histories, setHistories] = useState<string[]>(cachedHistories || []);
|
2023-07-03 21:49:40 +01:00
|
|
|
const suggestedPrompts: SuggestedPrompt[] = [
|
2023-07-17 19:10:41 +01:00
|
|
|
{ id: 1, text: 'Show all products that have the word "ultra" in the name or description' },
|
|
|
|
{ id: 2, text: "What are all of the possible categories for the products, and their counts?" },
|
|
|
|
{ id: 3, text: 'Show me all products that have been reviewed by someone with a username that contains "bob"' },
|
2023-07-03 21:49:40 +01:00
|
|
|
];
|
|
|
|
const [filteredHistories, setFilteredHistories] = useState<string[]>(histories);
|
|
|
|
const [filteredSuggestedPrompts, setFilteredSuggestedPrompts] = useState<SuggestedPrompt[]>(suggestedPrompts);
|
|
|
|
|
|
|
|
const handleUserPromptChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
2023-07-07 08:08:41 +01:00
|
|
|
inputEdited.current = true;
|
2023-07-03 21:49:40 +01:00
|
|
|
const { value } = event.target;
|
|
|
|
setUserPrompt(value);
|
|
|
|
|
|
|
|
// Filter history prompts
|
|
|
|
const filteredHistory = histories.filter((history) => history.toLowerCase().includes(value.toLowerCase()));
|
|
|
|
setFilteredHistories(filteredHistory);
|
|
|
|
|
|
|
|
// Filter suggested prompts
|
|
|
|
const filteredSuggested = suggestedPrompts.filter((prompt) =>
|
|
|
|
prompt.text.toLowerCase().includes(value.toLowerCase())
|
|
|
|
);
|
|
|
|
setFilteredSuggestedPrompts(filteredSuggested);
|
|
|
|
};
|
2023-06-27 21:43:10 +01:00
|
|
|
|
|
|
|
const updateHistories = (): void => {
|
2023-07-17 19:10:41 +01:00
|
|
|
const formattedUserPrompt = userPrompt.replace(/\s+/g, " ").trim();
|
|
|
|
const existingHistories = histories.map((history) => history.replace(/\s+/g, " ").trim());
|
|
|
|
|
|
|
|
const updatedHistories = existingHistories.filter(
|
|
|
|
(history) => history.toLowerCase() !== formattedUserPrompt.toLowerCase()
|
|
|
|
);
|
|
|
|
const newHistories = [formattedUserPrompt, ...updatedHistories.slice(0, 2)];
|
|
|
|
|
2023-06-27 21:43:10 +01:00
|
|
|
setHistories(newHistories);
|
2023-07-17 19:10:41 +01:00
|
|
|
localStorage.setItem(`${userContext.databaseAccount.id}-queryCopilotHistories`, newHistories.join("|"));
|
2023-06-27 21:43:10 +01:00
|
|
|
};
|
2023-07-03 21:49:40 +01:00
|
|
|
|
2023-06-16 08:25:23 +01:00
|
|
|
const generateSQLQuery = async (): Promise<void> => {
|
|
|
|
try {
|
2023-08-21 15:16:32 +01:00
|
|
|
if (shouldAllocateContainer && userContext.features.enableCopilotPhoenixGateaway) {
|
2023-08-24 20:56:31 +01:00
|
|
|
await explorer.allocateContainer(PoolIdType.QueryCopilot);
|
2023-08-18 09:47:19 +01:00
|
|
|
setShouldAllocateContainer(false);
|
|
|
|
}
|
|
|
|
|
2023-06-16 08:25:23 +01:00
|
|
|
setIsGeneratingQuery(true);
|
2023-06-26 08:13:30 +01:00
|
|
|
useTabs.getState().setIsTabExecuting(true);
|
2023-07-03 21:49:40 +01:00
|
|
|
useTabs.getState().setIsQueryErrorThrown(false);
|
2023-06-16 08:25:23 +01:00
|
|
|
const payload = {
|
2023-08-21 15:16:32 +01:00
|
|
|
containerSchema: userContext.features.enableCopilotFullSchema
|
|
|
|
? QueryCopilotSampleContainerSchema
|
|
|
|
: ShortenedQueryCopilotSampleContainerSchema,
|
2023-06-27 21:43:10 +01:00
|
|
|
userPrompt: userPrompt,
|
2023-06-16 08:25:23 +01:00
|
|
|
};
|
2023-06-28 09:11:03 +01:00
|
|
|
setShowDeletePopup(false);
|
2023-07-27 19:41:41 +01:00
|
|
|
useQueryCopilot.getState().refreshCorrelationId();
|
2023-08-18 09:47:19 +01:00
|
|
|
const serverInfo = useNotebook.getState().notebookServerInfo;
|
2023-08-21 15:16:32 +01:00
|
|
|
const queryUri = userContext.features.enableCopilotPhoenixGateaway
|
|
|
|
? createUri(serverInfo.notebookServerEndpoint, "generateSQLQuery")
|
|
|
|
: createUri("https://copilotorchestrater.azurewebsites.net/", "generateSQLQuery");
|
2023-08-18 09:47:19 +01:00
|
|
|
const response = await fetch(queryUri, {
|
2023-06-16 08:25:23 +01:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"content-type": "application/json",
|
2023-07-27 19:41:41 +01:00
|
|
|
"x-ms-correlationid": useQueryCopilot.getState().correlationId,
|
2023-06-16 08:25:23 +01:00
|
|
|
},
|
|
|
|
body: JSON.stringify(payload),
|
|
|
|
});
|
|
|
|
|
|
|
|
const generateSQLQueryResponse: GenerateSQLQueryResponse = await response?.json();
|
2023-08-18 09:47:19 +01:00
|
|
|
if (response.status === 404) {
|
|
|
|
setShouldAllocateContainer(true);
|
|
|
|
}
|
2023-07-13 18:50:49 +01:00
|
|
|
if (response.ok) {
|
|
|
|
if (generateSQLQueryResponse?.sql) {
|
|
|
|
let query = `-- **Prompt:** ${userPrompt}\r\n`;
|
|
|
|
if (generateSQLQueryResponse.explanation) {
|
|
|
|
query += `-- **Explanation of query:** ${generateSQLQueryResponse.explanation}\r\n`;
|
|
|
|
}
|
|
|
|
query += generateSQLQueryResponse.sql;
|
|
|
|
setQuery(query);
|
|
|
|
setGeneratedQuery(generateSQLQueryResponse.sql);
|
2023-07-27 19:41:41 +01:00
|
|
|
setGeneratedQueryComments(generateSQLQueryResponse.explanation);
|
2023-07-20 00:05:09 +01:00
|
|
|
setShowErrorMessageBar(false);
|
2023-06-16 08:25:23 +01:00
|
|
|
}
|
2023-07-13 18:50:49 +01:00
|
|
|
} else {
|
|
|
|
handleError(JSON.stringify(generateSQLQueryResponse), "copilotInternalServerError");
|
|
|
|
useTabs.getState().setIsQueryErrorThrown(true);
|
|
|
|
setShowErrorMessageBar(true);
|
2023-06-16 08:25:23 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
handleError(error, "executeNaturalLanguageQuery");
|
2023-07-03 21:49:40 +01:00
|
|
|
useTabs.getState().setIsQueryErrorThrown(true);
|
|
|
|
setShowErrorMessageBar(true);
|
2023-06-16 08:25:23 +01:00
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
setIsGeneratingQuery(false);
|
2023-06-26 08:13:30 +01:00
|
|
|
useTabs.getState().setIsTabExecuting(false);
|
2023-06-28 09:11:03 +01:00
|
|
|
setShowFeedbackBar(true);
|
2023-06-06 19:43:53 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onExecuteQueryClick = async (): Promise<void> => {
|
2023-07-27 19:41:41 +01:00
|
|
|
traceStart(Action.ExecuteQueryGeneratedFromQueryCopilot, {
|
|
|
|
correlationId: useQueryCopilot.getState().correlationId,
|
|
|
|
userPrompt: userPrompt,
|
|
|
|
generatedQuery: generatedQuery,
|
|
|
|
generatedQueryComments: generatedQueryComments,
|
|
|
|
executedQuery: selectedQuery || query,
|
|
|
|
});
|
2023-06-06 19:43:53 +01:00
|
|
|
const queryToExecute = selectedQuery || query;
|
2023-07-11 19:59:26 +01:00
|
|
|
const queryIterator = querySampleDocuments(queryToExecute, {
|
2023-06-06 19:43:53 +01:00
|
|
|
enableCrossPartitionQuery: shouldEnableCrossPartitionKey(),
|
|
|
|
} as FeedOptions);
|
|
|
|
setQueryIterator(queryIterator);
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
await queryDocumentsPerPage(0, queryIterator);
|
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
|
|
|
const queryDocumentsPerPage = async (firstItemIndex: number, queryIterator: MinimalQueryIterator): Promise<void> => {
|
|
|
|
try {
|
|
|
|
setIsExecuting(true);
|
2023-06-26 08:13:30 +01:00
|
|
|
useTabs.getState().setIsTabExecuting(true);
|
2023-07-03 21:49:40 +01:00
|
|
|
useTabs.getState().setIsQueryErrorThrown(false);
|
2023-06-06 19:43:53 +01:00
|
|
|
const queryResults: QueryResults = await queryPagesUntilContentPresent(
|
|
|
|
firstItemIndex,
|
|
|
|
async (firstItemIndex: number) =>
|
|
|
|
queryDocumentsPage(QueryCopilotSampleContainerId, queryIterator, firstItemIndex)
|
|
|
|
);
|
|
|
|
|
|
|
|
setQueryResults(queryResults);
|
|
|
|
setErrorMessage("");
|
2023-07-20 00:05:09 +01:00
|
|
|
setShowErrorMessageBar(false);
|
2023-07-27 19:41:41 +01:00
|
|
|
traceSuccess(Action.ExecuteQueryGeneratedFromQueryCopilot, {
|
|
|
|
correlationId: useQueryCopilot.getState().correlationId,
|
|
|
|
});
|
2023-06-06 19:43:53 +01:00
|
|
|
} catch (error) {
|
|
|
|
const errorMessage = getErrorMessage(error);
|
2023-07-27 19:41:41 +01:00
|
|
|
traceFailure(Action.ExecuteQueryGeneratedFromQueryCopilot, {
|
|
|
|
correlationId: useQueryCopilot.getState().correlationId,
|
|
|
|
errorMessage: errorMessage,
|
|
|
|
});
|
2023-06-06 19:43:53 +01:00
|
|
|
setErrorMessage(errorMessage);
|
|
|
|
handleError(errorMessage, "executeQueryCopilotTab");
|
2023-07-03 21:49:40 +01:00
|
|
|
useTabs.getState().setIsQueryErrorThrown(true);
|
|
|
|
setShowErrorMessageBar(true);
|
2023-06-06 19:43:53 +01:00
|
|
|
} finally {
|
|
|
|
setIsExecuting(false);
|
2023-06-26 08:13:30 +01:00
|
|
|
useTabs.getState().setIsTabExecuting(false);
|
2023-06-06 19:43:53 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getCommandbarButtons = (): CommandButtonComponentProps[] => {
|
|
|
|
const executeQueryBtnLabel = selectedQuery ? "Execute Selection" : "Execute Query";
|
|
|
|
const executeQueryBtn = {
|
|
|
|
iconSrc: ExecuteQueryIcon,
|
|
|
|
iconAlt: executeQueryBtnLabel,
|
|
|
|
onCommandClick: () => onExecuteQueryClick(),
|
|
|
|
commandButtonLabel: executeQueryBtnLabel,
|
|
|
|
ariaLabel: executeQueryBtnLabel,
|
|
|
|
hasPopup: false,
|
2023-07-14 23:01:16 +01:00
|
|
|
disabled: query?.trim() === "",
|
2023-06-06 19:43:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const saveQueryBtn = {
|
|
|
|
iconSrc: SaveQueryIcon,
|
|
|
|
iconAlt: "Save Query",
|
|
|
|
onCommandClick: () =>
|
|
|
|
useSidePanel.getState().openSidePanel("Save Query", <SaveQueryPane explorer={explorer} queryToSave={query} />),
|
|
|
|
commandButtonLabel: "Save Query",
|
|
|
|
ariaLabel: "Save Query",
|
|
|
|
hasPopup: false,
|
2023-07-14 23:01:16 +01:00
|
|
|
disabled: query?.trim() === "",
|
2023-06-06 19:43:53 +01:00
|
|
|
};
|
|
|
|
|
2023-07-17 19:10:41 +01:00
|
|
|
// Sample Prompts temporary disabled due current design
|
|
|
|
// const samplePromptsBtn = {
|
|
|
|
// iconSrc: SamplePromptsIcon,
|
|
|
|
// iconAlt: "Sample Prompts",
|
|
|
|
// onCommandClick: () => setIsSamplePromptsOpen(true),
|
|
|
|
// commandButtonLabel: "Sample Prompts",
|
|
|
|
// ariaLabel: "Sample Prompts",
|
|
|
|
// hasPopup: false,
|
|
|
|
// };
|
2023-06-28 09:11:03 +01:00
|
|
|
|
2023-07-17 19:10:41 +01:00
|
|
|
return [executeQueryBtn, saveQueryBtn];
|
2023-06-06 19:43:53 +01:00
|
|
|
};
|
2023-07-07 08:08:41 +01:00
|
|
|
const showTeachingBubble = (): void => {
|
2023-08-03 08:23:31 +01:00
|
|
|
const shouldShowTeachingBubble = !inputEdited.current && userPrompt.trim() === "";
|
|
|
|
if (shouldShowTeachingBubble) {
|
2023-07-07 08:08:41 +01:00
|
|
|
setTimeout(() => {
|
2023-08-03 08:23:31 +01:00
|
|
|
if (shouldShowTeachingBubble) {
|
2023-07-07 08:08:41 +01:00
|
|
|
toggleCopilotTeachingBubbleVisible();
|
|
|
|
inputEdited.current = true;
|
|
|
|
}
|
|
|
|
}, 30000);
|
|
|
|
}
|
|
|
|
};
|
2023-06-06 19:43:53 +01:00
|
|
|
|
2023-07-06 08:49:59 +01:00
|
|
|
const resetButtonState = () => {
|
|
|
|
setDislikeQuery(false);
|
|
|
|
setLikeQuery(false);
|
|
|
|
setShowCallout(false);
|
|
|
|
};
|
|
|
|
|
2023-07-27 18:45:42 +01:00
|
|
|
const startGenerateQueryProcess = () => {
|
|
|
|
updateHistories();
|
|
|
|
generateSQLQuery();
|
|
|
|
resetButtonState();
|
|
|
|
};
|
|
|
|
|
2023-06-06 19:43:53 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
useCommandBar.getState().setContextButtons(getCommandbarButtons());
|
2023-06-16 08:25:23 +01:00
|
|
|
}, [query, selectedQuery]);
|
|
|
|
|
2023-07-07 08:08:41 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
showTeachingBubble();
|
2023-07-10 19:30:53 +01:00
|
|
|
useTabs.getState().setIsQueryErrorThrown(false);
|
2023-07-07 08:08:41 +01:00
|
|
|
}, []);
|
|
|
|
|
2023-06-06 19:43:53 +01:00
|
|
|
return (
|
2023-07-17 19:10:41 +01:00
|
|
|
<Stack className="tab-pane" style={{ padding: 24, width: "100%" }}>
|
2023-08-02 16:37:37 +01:00
|
|
|
<div style={isGeneratingQuery ? { height: "100%" } : { overflowY: "auto", height: "100%" }}>
|
2023-07-17 19:10:41 +01:00
|
|
|
<Stack horizontal verticalAlign="center">
|
|
|
|
<Image src={CopilotIcon} />
|
|
|
|
<Text style={{ marginLeft: 8, fontWeight: 600, fontSize: 16 }}>Copilot</Text>
|
|
|
|
</Stack>
|
|
|
|
<Stack horizontal verticalAlign="center" style={{ marginTop: 16, width: "100%", position: "relative" }}>
|
|
|
|
<TextField
|
|
|
|
id="naturalLanguageInput"
|
|
|
|
value={userPrompt}
|
|
|
|
onChange={handleUserPromptChange}
|
|
|
|
onClick={() => {
|
|
|
|
inputEdited.current = true;
|
|
|
|
setShowSamplePrompts(true);
|
|
|
|
}}
|
2023-07-27 18:45:42 +01:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
startGenerateQueryProcess();
|
|
|
|
}
|
|
|
|
}}
|
2023-07-17 19:10:41 +01:00
|
|
|
style={{ lineHeight: 30 }}
|
|
|
|
styles={{ root: { width: "95%" } }}
|
|
|
|
disabled={isGeneratingQuery}
|
|
|
|
autoComplete="off"
|
|
|
|
/>
|
|
|
|
{copilotTeachingBubbleVisible && (
|
|
|
|
<TeachingBubble
|
|
|
|
calloutProps={{ directionalHint: DirectionalHint.bottomCenter }}
|
|
|
|
target="#naturalLanguageInput"
|
|
|
|
hasCloseButton={true}
|
|
|
|
closeButtonAriaLabel="Close"
|
|
|
|
onDismiss={toggleCopilotTeachingBubbleVisible}
|
|
|
|
hasSmallHeadline={true}
|
|
|
|
headline="Write a prompt"
|
2023-07-07 08:08:41 +01:00
|
|
|
>
|
2023-07-17 19:10:41 +01:00
|
|
|
Write a prompt here and Copilot will generate the query for you. You can also choose from our{" "}
|
|
|
|
<Link
|
|
|
|
onClick={() => {
|
|
|
|
setShowSamplePrompts(true);
|
|
|
|
toggleCopilotTeachingBubbleVisible();
|
|
|
|
}}
|
|
|
|
style={{ color: "white", fontWeight: 600 }}
|
|
|
|
>
|
|
|
|
sample prompts
|
|
|
|
</Link>{" "}
|
|
|
|
or write your own query
|
|
|
|
</TeachingBubble>
|
|
|
|
)}
|
|
|
|
<IconButton
|
|
|
|
iconProps={{ iconName: "Send" }}
|
|
|
|
disabled={isGeneratingQuery || !userPrompt.trim()}
|
|
|
|
style={{ marginLeft: 8 }}
|
2023-07-27 18:45:42 +01:00
|
|
|
onClick={() => startGenerateQueryProcess()}
|
2023-07-17 19:10:41 +01:00
|
|
|
/>
|
|
|
|
{isGeneratingQuery && <Spinner style={{ marginLeft: 8 }} />}
|
|
|
|
{showSamplePrompts && (
|
|
|
|
<Callout
|
|
|
|
styles={{ root: { minWidth: 400 } }}
|
|
|
|
target="#naturalLanguageInput"
|
|
|
|
isBeakVisible={false}
|
|
|
|
onDismiss={() => setShowSamplePrompts(false)}
|
|
|
|
directionalHintFixed={true}
|
|
|
|
directionalHint={DirectionalHint.bottomLeftEdge}
|
|
|
|
alignTargetEdge={true}
|
|
|
|
gapSpace={4}
|
|
|
|
>
|
|
|
|
<Stack>
|
|
|
|
{filteredHistories?.length > 0 && (
|
|
|
|
<Stack>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
width: "100%",
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: 600,
|
|
|
|
color: "#0078D4",
|
|
|
|
marginLeft: 16,
|
|
|
|
padding: "4px 0",
|
2023-06-27 21:43:10 +01:00
|
|
|
}}
|
|
|
|
>
|
2023-07-17 19:10:41 +01:00
|
|
|
Recent
|
|
|
|
</Text>
|
|
|
|
{filteredHistories.map((history, i) => (
|
|
|
|
<DefaultButton
|
|
|
|
key={i}
|
|
|
|
onClick={() => {
|
|
|
|
setUserPrompt(history);
|
|
|
|
setShowSamplePrompts(false);
|
|
|
|
}}
|
|
|
|
onRenderIcon={() => <Image src={RecentIcon} />}
|
|
|
|
styles={promptStyles}
|
|
|
|
>
|
|
|
|
{history}
|
|
|
|
</DefaultButton>
|
|
|
|
))}
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
{filteredSuggestedPrompts?.length > 0 && (
|
|
|
|
<Stack>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
width: "100%",
|
|
|
|
fontSize: 14,
|
|
|
|
fontWeight: 600,
|
|
|
|
color: "#0078D4",
|
|
|
|
marginLeft: 16,
|
|
|
|
padding: "4px 0",
|
2023-07-14 23:01:16 +01:00
|
|
|
}}
|
|
|
|
>
|
2023-07-17 19:10:41 +01:00
|
|
|
Suggested Prompts
|
|
|
|
</Text>
|
|
|
|
{filteredSuggestedPrompts.map((prompt) => (
|
|
|
|
<DefaultButton
|
|
|
|
key={prompt.id}
|
|
|
|
onClick={() => {
|
|
|
|
setUserPrompt(prompt.text);
|
|
|
|
setShowSamplePrompts(false);
|
|
|
|
}}
|
|
|
|
onRenderIcon={() => <Image src={HintIcon} />}
|
|
|
|
styles={promptStyles}
|
|
|
|
>
|
|
|
|
{prompt.text}
|
|
|
|
</DefaultButton>
|
|
|
|
))}
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
{(filteredHistories?.length > 0 || filteredSuggestedPrompts?.length > 0) && (
|
|
|
|
<Stack>
|
|
|
|
<Separator
|
|
|
|
styles={{
|
|
|
|
root: {
|
|
|
|
selectors: { "::before": { background: "#E1DFDD" } },
|
|
|
|
padding: 0,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
width: "100%",
|
|
|
|
fontSize: 14,
|
|
|
|
marginLeft: 16,
|
|
|
|
padding: "4px 0",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Learn about{" "}
|
2023-07-27 18:43:24 +01:00
|
|
|
<Link target="_blank" href="http://aka.ms/cdb-copilot-writing">
|
2023-07-17 19:10:41 +01:00
|
|
|
writing effective prompts
|
|
|
|
</Link>
|
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
</Stack>
|
|
|
|
</Callout>
|
|
|
|
)}
|
|
|
|
</Stack>
|
|
|
|
|
|
|
|
<Stack style={{ marginTop: 8, marginBottom: 24 }}>
|
|
|
|
<Text style={{ fontSize: 12 }}>
|
|
|
|
AI-generated content can have mistakes. Make sure it's accurate and appropriate before using it.{" "}
|
2023-07-27 18:43:24 +01:00
|
|
|
<Link href="http://aka.ms/cdb-copilot-preview-terms" target="_blank">
|
2023-07-17 19:10:41 +01:00
|
|
|
Read preview terms
|
|
|
|
</Link>
|
|
|
|
{showErrorMessageBar && (
|
|
|
|
<Stack style={{ backgroundColor: "#FEF0F1", padding: "4px 8px" }} horizontal verticalAlign="center">
|
|
|
|
<Image src={XErrorMessage} style={{ marginRight: "8px" }} />
|
|
|
|
<Text style={{ fontSize: 12 }}>
|
|
|
|
We ran into an error and were not able to execute query. Please try again after sometime
|
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
</Text>
|
|
|
|
</Stack>
|
|
|
|
|
|
|
|
{showFeedbackBar && (
|
|
|
|
<Stack style={{ backgroundColor: "#FFF8F0", padding: "2px 8px" }} horizontal verticalAlign="center">
|
|
|
|
<Text style={{ fontWeight: 600, fontSize: 12 }}>Provide feedback on the query generated</Text>
|
|
|
|
{showCallout && !hideFeedbackModalForLikedQueries && (
|
|
|
|
<Callout
|
|
|
|
style={{ padding: 8 }}
|
|
|
|
target="#likeBtn"
|
|
|
|
onDismiss={() => {
|
|
|
|
setShowCallout(false);
|
2023-08-24 08:07:29 +01:00
|
|
|
SubmitFeedback({
|
2023-08-18 09:47:19 +01:00
|
|
|
params: {
|
|
|
|
generatedQuery: generatedQuery,
|
|
|
|
likeQuery: likeQuery,
|
|
|
|
description: "",
|
|
|
|
userPrompt: userPrompt,
|
|
|
|
},
|
|
|
|
explorer: explorer,
|
2023-08-03 08:23:31 +01:00
|
|
|
});
|
2023-07-17 19:10:41 +01:00
|
|
|
}}
|
|
|
|
directionalHint={DirectionalHint.topCenter}
|
|
|
|
>
|
|
|
|
<Text>
|
|
|
|
Thank you. Need to give{" "}
|
|
|
|
<Link
|
|
|
|
onClick={() => {
|
|
|
|
setShowCallout(false);
|
|
|
|
useQueryCopilot.getState().openFeedbackModal(generatedQuery, true, userPrompt);
|
2023-07-14 23:01:16 +01:00
|
|
|
}}
|
|
|
|
>
|
2023-07-17 19:10:41 +01:00
|
|
|
more feedback?
|
|
|
|
</Link>
|
|
|
|
</Text>
|
|
|
|
</Callout>
|
|
|
|
)}
|
|
|
|
<IconButton
|
|
|
|
id="likeBtn"
|
|
|
|
style={{ marginLeft: 20 }}
|
|
|
|
iconProps={{ iconName: likeQuery === true ? "LikeSolid" : "Like" }}
|
|
|
|
onClick={() => {
|
|
|
|
setShowCallout(!likeQuery);
|
|
|
|
setLikeQuery(!likeQuery);
|
|
|
|
if (dislikeQuery) {
|
|
|
|
setDislikeQuery(!dislikeQuery);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<IconButton
|
|
|
|
style={{ margin: "0 10px" }}
|
|
|
|
iconProps={{ iconName: dislikeQuery === true ? "DislikeSolid" : "Dislike" }}
|
|
|
|
onClick={() => {
|
|
|
|
if (!dislikeQuery) {
|
|
|
|
useQueryCopilot.getState().openFeedbackModal(generatedQuery, false, userPrompt);
|
|
|
|
setLikeQuery(false);
|
|
|
|
}
|
|
|
|
setDislikeQuery(!dislikeQuery);
|
2023-06-28 09:11:03 +01:00
|
|
|
setShowCallout(false);
|
|
|
|
}}
|
2023-07-17 19:10:41 +01:00
|
|
|
/>
|
|
|
|
<Separator vertical style={{ color: "#EDEBE9" }} />
|
|
|
|
<CommandBarButton
|
|
|
|
onClick={copyGeneratedCode}
|
|
|
|
iconProps={{ iconName: "Copy" }}
|
|
|
|
style={{ margin: "0 10px", backgroundColor: "#FFF8F0", transition: "background-color 0.3s ease" }}
|
2023-06-28 09:11:03 +01:00
|
|
|
>
|
2023-07-17 19:10:41 +01:00
|
|
|
Copy code
|
|
|
|
</CommandBarButton>
|
|
|
|
<CommandBarButton
|
|
|
|
onClick={() => {
|
|
|
|
setShowDeletePopup(true);
|
|
|
|
}}
|
|
|
|
iconProps={{ iconName: "Delete" }}
|
|
|
|
style={{ margin: "0 10px", backgroundColor: "#FFF8F0", transition: "background-color 0.3s ease" }}
|
|
|
|
>
|
|
|
|
Delete code
|
|
|
|
</CommandBarButton>
|
|
|
|
</Stack>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Stack className="tabPaneContentContainer">
|
|
|
|
<SplitterLayout vertical={true} primaryIndex={0} primaryMinSize={100} secondaryMinSize={200}>
|
|
|
|
<EditorReact
|
|
|
|
language={"sql"}
|
|
|
|
content={query}
|
|
|
|
isReadOnly={false}
|
|
|
|
ariaLabel={"Editing Query"}
|
|
|
|
lineNumbers={"on"}
|
|
|
|
onContentChanged={(newQuery: string) => setQuery(newQuery)}
|
|
|
|
onContentSelected={(selectedQuery: string) => setSelectedQuery(selectedQuery)}
|
|
|
|
/>
|
|
|
|
<QueryResultSection
|
|
|
|
isMongoDB={false}
|
|
|
|
queryEditorContent={selectedQuery || query}
|
|
|
|
error={errorMessage}
|
|
|
|
queryResults={queryResults}
|
|
|
|
isExecuting={isExecuting}
|
|
|
|
executeQueryDocumentsPage={(firstItemIndex: number) =>
|
|
|
|
queryDocumentsPerPage(firstItemIndex, queryIterator)
|
2023-07-06 08:49:59 +01:00
|
|
|
}
|
2023-07-17 19:10:41 +01:00
|
|
|
/>
|
|
|
|
</SplitterLayout>
|
2023-06-28 09:11:03 +01:00
|
|
|
</Stack>
|
2023-07-17 19:10:41 +01:00
|
|
|
<WelcomeModal visible={localStorage.getItem("hideWelcomeModal") !== "true"} />
|
|
|
|
{isSamplePromptsOpen && <SamplePrompts sampleProps={sampleProps} />}
|
|
|
|
{query !== "" && query.trim().length !== 0 && (
|
|
|
|
<DeletePopup
|
|
|
|
showDeletePopup={showDeletePopup}
|
|
|
|
setShowDeletePopup={setShowDeletePopup}
|
|
|
|
setQuery={setQuery}
|
|
|
|
clearFeedback={resetButtonState}
|
|
|
|
showFeedbackBar={setShowFeedbackBar}
|
2023-06-06 19:43:53 +01:00
|
|
|
/>
|
2023-07-17 19:10:41 +01:00
|
|
|
)}
|
|
|
|
<CopyPopup showCopyPopup={showCopyPopup} setShowCopyPopup={setshowCopyPopup} />
|
|
|
|
</div>
|
2023-06-06 19:43:53 +01:00
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
};
|