|
| 1 | +import { |
| 2 | + SUPABASE_LOGIN_URL, |
| 3 | + GITHUB_PR_SUGGESTION_TEXT_AREA_SELECTOR, |
| 4 | +} from "../../../constants"; |
| 5 | +import { insertTextAtCursor } from "../../../utils/ai-utils/cursorPositionInsert"; |
| 6 | +import { |
| 7 | + DescriptionConfig, |
| 8 | + getAIDescriptionConfig, |
| 9 | +} from "../../../utils/ai-utils/descriptionconfig"; |
| 10 | +import { getAuthToken, isLoggedIn } from "../../../utils/checkAuthentication"; |
| 11 | +import { createHtmlElement } from "../../../utils/createHtmlElement"; |
| 12 | +import { isOutOfContextBounds } from "../../../utils/fetchGithubAPIData"; |
| 13 | + |
| 14 | +type SuggestionGenerator = ( |
| 15 | + token: string, |
| 16 | + code: string, |
| 17 | + config: DescriptionConfig |
| 18 | +) => Promise<string | undefined>; |
| 19 | + |
| 20 | +export const AICodeReviewMenu = (items: HTMLLIElement[]) => { |
| 21 | + const menu = createHtmlElement("div", { |
| 22 | + className: "SelectMenu js-slash-command-menu hidden mt-6", |
| 23 | + innerHTML: `<div class="SelectMenu-modal no-underline"> |
| 24 | + <header class="SelectMenu-header"> |
| 25 | + <div class="flex-1"> |
| 26 | + <svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" class="octicon octicon-code-square"> |
| 27 | + <path d="M0 1.75C0 .784.784 0 1.75 0h12.5C15.216 0 16 .784 16 1.75v12.5A1.75 1.75 0 0 1 14.25 16H1.75A1.75 1.75 0 0 1 0 14.25Zm1.75-.25a.25.25 0 0 0-.25.25v12.5c0 .138.112.25.25.25h12.5a.25.25 0 0 0 .25-.25V1.75a.25.25 0 0 0-.25-.25Zm7.47 3.97a.75.75 0 0 1 1.06 0l2 2a.75.75 0 0 1 0 1.06l-2 2a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734L10.69 8 9.22 6.53a.75.75 0 0 1 0-1.06ZM6.78 6.53 5.31 8l1.47 1.47a.749.749 0 0 1-.326 1.275.749.749 0 0 1-.734-.215l-2-2a.75.75 0 0 1 0-1.06l2-2a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042Z"></path> |
| 28 | + </svg> |
| 29 | + <span class="color-fg-muted text-small pl-1">OpenSauced.ai</span> |
| 30 | + </div> |
| 31 | + <div class="Label Label--success">AI</div> |
| 32 | + <a class="ml-1 color-fg-muted d-block" target="_blank" href="https://github.com/orgs/open-sauced/discussions"> |
| 33 | + Give feedback |
| 34 | + </a> |
| 35 | + </header> |
| 36 | + <div class="SelectMenu-list js-command-list-container" style="max-height: 270px;" id="combobox-5123"> |
| 37 | + <ul role="listbox" class="SelectMenu-list js-slash-command-menu-items"> |
| 38 | + </ul> |
| 39 | + </div> |
| 40 | + </div>`, |
| 41 | + }); |
| 42 | + |
| 43 | + menu.querySelector("ul")?.append(...items); |
| 44 | + |
| 45 | + document.addEventListener("click", event => { |
| 46 | + if (event.target instanceof HTMLElement) { |
| 47 | + menu.classList.add("hidden"); |
| 48 | + } |
| 49 | + }); |
| 50 | + return menu; |
| 51 | +}; |
| 52 | + |
| 53 | +export const AICodeReviewMenuItem = (title: string, description: string, suggestionGenerator: SuggestionGenerator, commentNode: HTMLElement) => { |
| 54 | + const menuItem = createHtmlElement("li", { |
| 55 | + className: "SelectMenu-item d-block slash-command-menu-item", |
| 56 | + role: "option", |
| 57 | + onclick: () => { |
| 58 | + void handleSubmit(suggestionGenerator, commentNode); |
| 59 | + }, |
| 60 | + innerHTML: `<h5>${title}</h5> |
| 61 | + <span class="command-description">${description}</span>`, |
| 62 | + }); |
| 63 | + |
| 64 | + return menuItem; |
| 65 | +}; |
| 66 | + |
| 67 | +const handleSubmit = async ( |
| 68 | + suggestionGenerator: SuggestionGenerator, |
| 69 | + commentNode: HTMLElement, |
| 70 | +) => { |
| 71 | + const logo = commentNode.querySelector("#ai-description-button-logo"); |
| 72 | + const button = commentNode.querySelector("#os-ai-change-gen"); |
| 73 | + |
| 74 | + try { |
| 75 | + if (!(await isLoggedIn())) { |
| 76 | + return window.open(SUPABASE_LOGIN_URL, "_blank"); |
| 77 | + } |
| 78 | + |
| 79 | + if (!logo || !button) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const descriptionConfig = await getAIDescriptionConfig(); |
| 84 | + |
| 85 | + if (!descriptionConfig) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + logo.classList.toggle("animate-spin"); |
| 90 | + button.classList.toggle("pointer-events-none"); |
| 91 | + |
| 92 | + const selectedLines = document.querySelectorAll( |
| 93 | + ".code-review.selected-line", |
| 94 | + ); |
| 95 | + let selectedCode = Array.from(selectedLines) |
| 96 | + .map(line => line.textContent) |
| 97 | + .join("\n"); |
| 98 | + |
| 99 | + // find input with name="position" and get its value |
| 100 | + if (!selectedCode) { |
| 101 | + const positionElement = commentNode.querySelector( |
| 102 | + "input[name=position]", |
| 103 | + )!; |
| 104 | + const position = positionElement.getAttribute("value")!; |
| 105 | + |
| 106 | + const codeDiv = document.querySelector(`[data-line-number="${position}"]`) |
| 107 | + ?.nextSibling?.nextSibling as HTMLElement; |
| 108 | + |
| 109 | + selectedCode = |
| 110 | + codeDiv.getElementsByClassName("blob-code-inner")[0].textContent!; |
| 111 | + } |
| 112 | + if ( |
| 113 | + isOutOfContextBounds( |
| 114 | + [selectedCode, [] ], |
| 115 | + descriptionConfig.config.maxInputLength, |
| 116 | + ) |
| 117 | + ) { |
| 118 | + logo.classList.toggle("animate-spin"); |
| 119 | + return alert( |
| 120 | + `Max input length exceeded. Try reducing the number of selected lines to refactor.`, |
| 121 | + ); |
| 122 | + } |
| 123 | + const token = await getAuthToken(); |
| 124 | + const suggestionStream = await suggestionGenerator( |
| 125 | + token, |
| 126 | + selectedCode, |
| 127 | + descriptionConfig, |
| 128 | + ); |
| 129 | + |
| 130 | + logo.classList.toggle("animate-spin"); |
| 131 | + button.classList.toggle("pointer-events-none"); |
| 132 | + if (!suggestionStream) { |
| 133 | + return console.error("No description was generated!"); |
| 134 | + } |
| 135 | + const textArea = commentNode.querySelector( |
| 136 | + GITHUB_PR_SUGGESTION_TEXT_AREA_SELECTOR, |
| 137 | + )!; |
| 138 | + |
| 139 | + insertTextAtCursor(textArea as HTMLTextAreaElement, suggestionStream); |
| 140 | + } catch (error: unknown) { |
| 141 | + logo?.classList.toggle("animate-spin"); |
| 142 | + button?.classList.toggle("pointer-events-none"); |
| 143 | + |
| 144 | + if (error instanceof Error) { |
| 145 | + console.error("Description generation error:", error.message); |
| 146 | + } |
| 147 | + } |
| 148 | +}; |
0 commit comments