From 292534b808388774bf910ad847d1adbfffe2d66c Mon Sep 17 00:00:00 2001 From: Brian 'bdougie' Douglas Date: Thu, 15 Aug 2024 18:51:20 -0700 Subject: [PATCH] removes all generative AI features --- .../AICodeReview/AICodeReviewButton.ts | 49 ---- .../AICodeReview/AICodeReviewMenu.ts | 133 ---------- src/content-scripts/github.ts | 41 +-- src/popup/pages/posthighlight.tsx | 239 ++++++++---------- src/popup/pages/settings.tsx | 77 +++--- src/utils/ai-utils/configurationReducer.ts | 35 --- src/utils/ai-utils/openai.ts | 144 ----------- src/utils/dom-utils/changeSuggestorButton.ts | 9 - src/utils/fetchGithubAPIData.ts | 16 -- 9 files changed, 144 insertions(+), 599 deletions(-) delete mode 100644 src/content-scripts/components/AICodeReview/AICodeReviewButton.ts delete mode 100644 src/content-scripts/components/AICodeReview/AICodeReviewMenu.ts delete mode 100644 src/utils/ai-utils/configurationReducer.ts delete mode 100644 src/utils/ai-utils/openai.ts diff --git a/src/content-scripts/components/AICodeReview/AICodeReviewButton.ts b/src/content-scripts/components/AICodeReview/AICodeReviewButton.ts deleted file mode 100644 index 273ed1f0..00000000 --- a/src/content-scripts/components/AICodeReview/AICodeReviewButton.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { createHtmlElement } from "../../../utils/createHtmlElement"; -import openSaucedLogoIcon from "../../../assets/opensauced-icon.svg"; -import { generateCodeExplanation, generateCodeSuggestion, generateCodeTest } from "../../../utils/ai-utils/openai"; -import { - AICodeReviewMenu, - AICodeReviewMenuItem, -} from "./AICodeReviewMenu"; - - -export const AICodeReviewButton = (commentNode: HTMLElement) => { - const changeSuggestorButton = createHtmlElement("a", { - innerHTML: ` - - `, - onclick: (event: MouseEvent) => { - event.stopPropagation(); - menu.classList.toggle("hidden"); - }, - id: "os-ai-change-gen", - }); - - const refactorCode = AICodeReviewMenuItem( - "Refactor Code", - "Generate a code refactor", - generateCodeSuggestion, - commentNode, - ); - const testCode = AICodeReviewMenuItem( - "Test Code", - "Generate a test for the code", - generateCodeTest, - commentNode, - ); - const explainCode = AICodeReviewMenuItem( - "Explain Code", - "Generate an explanation for the code", - generateCodeExplanation, - commentNode, - ); - - const menu = AICodeReviewMenu([refactorCode, testCode, explainCode]); - - changeSuggestorButton.append(menu); - return changeSuggestorButton; -}; - - diff --git a/src/content-scripts/components/AICodeReview/AICodeReviewMenu.ts b/src/content-scripts/components/AICodeReview/AICodeReviewMenu.ts deleted file mode 100644 index 7df148f8..00000000 --- a/src/content-scripts/components/AICodeReview/AICodeReviewMenu.ts +++ /dev/null @@ -1,133 +0,0 @@ -import { GITHUB_PR_SUGGESTION_TEXT_AREA_Attribute } from "../../../constants"; -import { insertTextAtCursor } from "../../../utils/ai-utils/cursorPositionInsert"; -import { getAuthToken, isLoggedIn, optLogIn } from "../../../utils/checkAuthentication"; -import { createHtmlElement } from "../../../utils/createHtmlElement"; -import { isOutOfContextBounds } from "../../../utils/fetchGithubAPIData"; - -type SuggestionGenerator = ( - token: string, - code: string, -) => Promise; - -export const AICodeReviewMenu = (items: HTMLLIElement[]) => { - const menu = createHtmlElement("div", { - className: "SelectMenu js-slash-command-menu hidden mt-6", - innerHTML: `
-
-
- - OpenSauced.ai -
-
AI
- - Give feedback - -
-
-
    -
-
-
`, - }); - - menu.querySelector("ul")?.append(...items); - - document.addEventListener("click", event => { - if (event.target instanceof HTMLElement) { - menu.classList.add("hidden"); - } - }); - return menu; -}; - -export const AICodeReviewMenuItem = (title: string, description: string, suggestionGenerator: SuggestionGenerator, commentNode: HTMLElement) => { - const menuItem = createHtmlElement("li", { - className: "SelectMenu-item d-block slash-command-menu-item", - role: "option", - onclick: () => { - void handleSubmit(suggestionGenerator, commentNode); - }, - innerHTML: `
${title}
- ${description}`, - }); - - return menuItem; -}; - -const handleSubmit = async ( - suggestionGenerator: SuggestionGenerator, - commentNode: HTMLElement, -) => { - const logo = commentNode.querySelector("#ai-description-button-logo"); - const button = commentNode.querySelector("#os-ai-change-gen"); - - try { - if (!(await isLoggedIn())) { - return void optLogIn(); - } - - if (!logo || !button) { - return; - } - - logo.classList.toggle("animate-spin"); - button.classList.toggle("pointer-events-none"); - - const selectedLines = document.querySelectorAll( - ".code-review.selected-line", - ); - let selectedCode = Array.from(selectedLines) - .map(line => line.textContent) - .join("\n"); - - // find input with name="position" and get its value - if (!selectedCode) { - const positionElement = commentNode.querySelector( - "input[name=position]", - )!; - const position = positionElement.getAttribute("value")!; - - const codeDiv = document.querySelector(`[data-line-number="${position}"]`) - ?.nextSibling?.nextSibling as HTMLElement; - - selectedCode = - codeDiv.getElementsByClassName("blob-code-inner")[0].textContent!; - } - if ( - isOutOfContextBounds( - [selectedCode, [] ], - 3900, - ) - ) { - logo.classList.toggle("animate-spin"); - return alert( - `Max input length exceeded. Try reducing the number of selected lines to refactor.`, - ); - } - const token = await getAuthToken(); - const suggestionStream = await suggestionGenerator( - token, - selectedCode, - ); - - logo.classList.toggle("animate-spin"); - button.classList.toggle("pointer-events-none"); - if (!suggestionStream) { - return console.error("No description was generated!"); - } - const textArea = commentNode.querySelector( - GITHUB_PR_SUGGESTION_TEXT_AREA_Attribute, - )!; - - insertTextAtCursor(textArea as HTMLTextAreaElement, suggestionStream); - } catch (error: unknown) { - logo?.classList.toggle("animate-spin"); - button?.classList.toggle("pointer-events-none"); - - if (error instanceof Error) { - console.error("Description generation error:", error.message); - } - } -}; diff --git a/src/content-scripts/github.ts b/src/content-scripts/github.ts index 8113ce74..b60857f3 100644 --- a/src/content-scripts/github.ts +++ b/src/content-scripts/github.ts @@ -1,37 +1,19 @@ import { getGithubUsername, isGithubProfilePage, - isGithubPullRequestPage, - isGithubRepoPage, - isPullRequestCreatePage, - isPullRequestFilesChangedPage, } from "../utils/urlMatchers"; import { isOpenSaucedUser } from "../utils/fetchOpenSaucedApiData"; import injectViewOnOpenSauced from "../utils/dom-utils/viewOnOpenSauced"; import injectInviteToOpenSauced from "../utils/dom-utils/inviteToOpenSauced"; import { prefersDarkMode } from "../utils/colorPreference"; -import injectAddPRToHighlightsButton from "../utils/dom-utils/addPRToHighlights"; -// import injectRepoVotingButtons from "../utils/dom-utils/repoVotingButtons"; import domUpdateWatch from "../utils/dom-utils/domUpdateWatcher"; -import injectChangeSuggestorButton from "../utils/dom-utils/changeSuggestorButton"; -import prEditWatch, { prReviewWatch } from "../utils/dom-utils/prWatcher"; -import injectChatDialog from "../utils/dom-utils/addChatDialog"; -import { pageUrlWatch } from "../utils/dom-utils/pageUrlWatcher"; const processGithubPage = async () => { if (prefersDarkMode(document.cookie)) { document.documentElement.classList.add("dark"); } - if (isPullRequestCreatePage(window.location.href)) { - // void injectDescriptionGeneratorButton(); - } else if (isPullRequestFilesChangedPage(window.location.href)) { - prReviewWatch(injectChangeSuggestorButton, 500); - } else if (isGithubPullRequestPage(window.location.href)) { - prEditWatch(injectChangeSuggestorButton, 500); - // void injectDescriptionGeneratorButton(); - void injectAddPRToHighlightsButton(); - } else if (isGithubProfilePage(window.location.href)) { + if (isGithubProfilePage(window.location.href)) { const username = getGithubUsername(window.location.href); if (!username) { @@ -42,29 +24,8 @@ const processGithubPage = async () => { } else { injectInviteToOpenSauced(username); } - } else if (isGithubRepoPage(window.location.href)) { - const ownerName = getGithubUsername(window.location.href) ?? ""; - const repoName = window.location.href.split("/").pop() ?? ""; - - await injectChatDialog(ownerName, repoName); - - pageUrlWatch(() => { - if (document.getElementById("repo-query-root")) { - document.getElementById("repo-query-root")?.remove(); - } - }, 50); } - /* - * commenting out repo voting because it's not ready yet // issue #106 - * } else if (isGithubRepoPage(window.location.href)) { - * const ownerName = getGithubUsername(window.location.href) ?? ""; - * const repoName = window.location.href.split("/").pop() ?? ""; - * - * await injectRepoVotingButtons(ownerName, repoName); - * } - */ - domUpdateWatch(processGithubPage, 50); }; diff --git a/src/popup/pages/posthighlight.tsx b/src/popup/pages/posthighlight.tsx index 0498ff7e..16ecc07a 100644 --- a/src/popup/pages/posthighlight.tsx +++ b/src/popup/pages/posthighlight.tsx @@ -6,138 +6,119 @@ import toast, { Toaster } from "react-hot-toast"; import { createHighlight } from "../../utils/fetchOpenSaucedApiData"; import { goBack, goTo } from "react-chrome-extension-router"; import { OPEN_SAUCED_INSIGHTS_DOMAIN } from "../../constants"; -import { getAiDescription } from "../../content-scripts/components/GenerateAIDescription/DescriptionGeneratorButton"; import Home from "./home"; -const PostOnHighlight = ({ prUrl, prTitle }: { prUrl: string, prTitle: string }) => { - const { authToken, user } = useAuth(); - const [pageURL, setPageURL] = useState(""); - const [highlightTitle, setHighlightTitle] = useState(""); - const [highlightContent, setHighlightContent] = useState(""); - const [isSendButtonEnabled, enableSendButton] = useState(true); - - const generateAiDescription = () => { - enableSendButton(false); - const description = getAiDescription(prUrl); - - toast.promise(description, { - loading: "Generating summary...", - success: data => { - enableSendButton(true); - setHighlightContent(data); - - return "Successfully Generated Summary"; - }, - error: e => { - enableSendButton(true); - return `Uh oh, there was an error! ${e.message}`; - }, - }).catch(console.error); - }; - - - // post highlight function - const postHighlight = () => { - enableSendButton(false); - const postHighlightAPI = createHighlight((authToken ?? ""), pageURL, highlightTitle, highlightContent); - - toast.promise(postHighlightAPI, { - loading: "Loading ...", - success: data => { - enableSendButton(true); - if (!data.ok) { - throw new Error(`Status code ${data.status}`); - } - goTo(Home, { forceRefresh: true }); - return ( - - - See the highlight live - - - ); - }, - error: e => { - enableSendButton(true); - return `Uh oh, there was an error! ${e.message}`; - }, - }).catch(console.error); - }; - - useEffect(() => { - setPageURL(prUrl); - setHighlightTitle(prTitle); - }, []); - - return ( -
- - -
- -
-
- - - OpenSauced logo -
-
- -
- - setHighlightTitle(e.target.value)} - /> - -