-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate code suggestions via AI (#90)
* feat: generate code suggestions via AI * avoid multiple icons * multiple reviews * refactor a single line of code * extensions suggestions * dont blindly believe ai * add delay to DOM manipulation * improve type checks * remove description tone from code suggestions * update to opensauced api endpoint * update stream selector * fix: showing the icon (#104) --------- Co-authored-by: Abdurrahman Rajab <[email protected]>
- Loading branch information
Showing
9 changed files
with
199 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/content-scripts/components/AICodeRefactor/ChangeSuggestorButton.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { createHtmlElement } from "../../../utils/createHtmlElement"; | ||
import openSaucedLogoIcon from "../../../assets/opensauced-icon.svg"; | ||
import { GITHUB_PR_SUGGESTION_TEXT_AREA_SELECTOR, SUPABASE_LOGIN_URL } from "../../../constants"; | ||
import { generateCodeSuggestion } from "../../../utils/aiprdescription/openai"; | ||
import { isOutOfContextBounds } from "../../../utils/fetchGithubAPIData"; | ||
import { insertTextAtCursor } from "../../../utils/aiprdescription/cursorPositionInsert"; | ||
import { getAIDescriptionConfig } from "../../../utils/aiprdescription/descriptionconfig"; | ||
import { getAuthToken, isLoggedIn } from "../../../utils/checkAuthentication"; | ||
|
||
export const ChangeSuggestorButton = (commentNode: HTMLElement) => { | ||
const changeSuggestorButton = createHtmlElement("a", { | ||
innerHTML: `<span id="ai-change-gen" class="toolbar-item btn-octicon"> | ||
<img class="octicon octicon-heading" height="16px" width="16px" id="ai-description-button-logo" src=${chrome.runtime.getURL(openSaucedLogoIcon)}> | ||
</span> | ||
<tool-tip for="ai-change-gen" class="sr-only" role="tooltip">Get Refactor Suggestions</tool-tip>`, | ||
onclick: async () => handleSubmit(commentNode), | ||
id: "os-ai-change-gen", | ||
}); | ||
|
||
return changeSuggestorButton; | ||
}; | ||
|
||
const handleSubmit = async (commentNode: HTMLElement) => { | ||
try { | ||
if (!(await isLoggedIn())) { | ||
return window.open(SUPABASE_LOGIN_URL, "_blank"); | ||
} | ||
const logo = document.getElementById("ai-description-button-logo"); | ||
|
||
if (!logo) { | ||
return; | ||
} | ||
|
||
const descriptionConfig = await getAIDescriptionConfig(); | ||
|
||
if (!descriptionConfig) { | ||
return; | ||
} | ||
if (!descriptionConfig.enabled) { | ||
return alert("AI PR description is disabled!"); | ||
} | ||
|
||
logo.classList.toggle("animate-spin"); | ||
|
||
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, [] ], descriptionConfig.config.maxInputLength)) { | ||
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 generateCodeSuggestion( | ||
token, | ||
descriptionConfig.config.language, | ||
descriptionConfig.config.length, | ||
descriptionConfig.config.temperature / 10, | ||
selectedCode, | ||
); | ||
|
||
logo.classList.toggle("animate-spin"); | ||
if (!suggestionStream) { | ||
return console.error("No description was generated!"); | ||
} | ||
const textArea = commentNode.querySelector(GITHUB_PR_SUGGESTION_TEXT_AREA_SELECTOR)!; | ||
|
||
void insertTextAtCursor(textArea as HTMLTextAreaElement, suggestionStream); | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
console.error("Description generation error:", error.message); | ||
} | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ChangeSuggestorButton } from "../../content-scripts/components/AICodeRefactor/ChangeSuggestorButton"; | ||
import { GITHUB_REVIEW_SUGGESTION_SELECTOR } from "../../constants"; | ||
import { isPublicRepository } from "../fetchGithubAPIData"; | ||
|
||
const injectChangeSuggestorButton = async (commentNode: HTMLElement) => { | ||
if (!(await isPublicRepository(window.location.href))) { | ||
return; | ||
} | ||
|
||
const suggestChangesIcon = commentNode.getElementsByClassName(GITHUB_REVIEW_SUGGESTION_SELECTOR)[0]; | ||
const changeSuggestorButton = ChangeSuggestorButton(commentNode); | ||
|
||
if (suggestChangesIcon.querySelector("#os-ai-change-gen")) { | ||
return; | ||
} | ||
suggestChangesIcon.insertBefore(changeSuggestorButton, suggestChangesIcon.firstChild); | ||
}; | ||
|
||
export default injectChangeSuggestorButton; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const prEditWatch = (callback: () => void, delayInMs = 0) => { | ||
const observer = new MutationObserver((mutationList: MutationRecord[], observer: MutationObserver) => { | ||
for (const mutation of mutationList) { | ||
if (Array.from((mutation.target as HTMLElement).classList).includes("is-comment-editing")) { | ||
setTimeout(callback, delayInMs); | ||
observer.disconnect(); | ||
} | ||
} | ||
}); | ||
|
||
observer.observe(document.body, { attributes: true, subtree: true }); | ||
}; | ||
|
||
export const prReviewWatch = (callback: (node: HTMLElement) => void, delayInMs = 0) => { | ||
const githubCommentSelector = "inline-comment-form-container"; | ||
const observer = new MutationObserver((mutationList: MutationRecord[], observer: MutationObserver) => { | ||
mutationList.forEach(mutation => { | ||
if (Array.from((mutation.target as HTMLElement).classList).includes(githubCommentSelector)) { | ||
setTimeout(() => { | ||
const commentNodes = document.getElementsByClassName(githubCommentSelector); | ||
|
||
Array.from(commentNodes).forEach(node => { | ||
callback(node as HTMLElement); | ||
}); | ||
}, delayInMs); | ||
} | ||
}); | ||
}); | ||
|
||
observer.observe(document.body, { attributes: true, subtree: true, childList: true }); | ||
}; | ||
|
||
export default prEditWatch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters