-
-
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: OpenSauced AI PR description (#79)
* WIP just starting * not sure why it is not appending new element * feat: AI highlight button * feat: cursor position text insert, pr info * feat: AI description config window * feat: pr actions dropdown * chore: Create PR description button * feat: OpenAI Completion * feat: type response from stream * feat: Config window new values, save button * chore: Default description config setters * chore: form config submit * refactor: formatting, linting fixes * chore: description config type update, dropdown toggle * chore: disable state handle * chore: updated title * fix: missing base branch, spinner * chore: request errors to console * feat: conditional diff, commitmsg fetching * feat: Input context length check, redirect when logged out * feat: highlights dropdown collapse * feat: Edit PR button injection, tokenizer length check * feat: Private repository check * feat: "Description generated using OpenSauced" append --------- Co-authored-by: Brian 'bdougie' Douglas <[email protected]>
- Loading branch information
Showing
22 changed files
with
873 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
73 changes: 73 additions & 0 deletions
73
src/content-scripts/components/GenerateAIDescription/DescriptionGeneratorButton.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,73 @@ | ||
import { createHtmlElement } from "../../../utils/createHtmlElement"; | ||
import openSaucedLogoIcon from "../../../assets/opensauced-icon.svg"; | ||
import { getPullRequestAPIURL } from "../../../utils/urlMatchers"; | ||
import { getDescriptionContext, isContextWithinBounds } from "../../../utils/fetchGithubAPIData"; | ||
import { generateDescription } from "../../../utils/aiprdescription/openai"; | ||
import { GITHUB_PR_COMMENT_TEXT_AREA_SELECTOR, OPEN_AI_COMPLETION_MODEL_NAME, SUPABASE_LOGIN_URL } from "../../../constants"; | ||
import { insertAtCursorFromStream } from "../../../utils/aiprdescription/cursorPositionInsert"; | ||
import { getAIDescriptionConfig } from "../../../utils/aiprdescription/descriptionconfig"; | ||
import { isLoggedIn } from "../../../utils/checkAuthentication"; | ||
|
||
export const DescriptionGeneratorButton = () => { | ||
const descriptionGeneratorButton = createHtmlElement("a", { | ||
innerHTML: `<span id="ai-description-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-description-gen">Generate PR description</tool-tip>`, | ||
onclick: handleSubmit, | ||
|
||
}); | ||
|
||
return descriptionGeneratorButton; | ||
}; | ||
|
||
const handleSubmit = async () => { | ||
try { | ||
if (!(await isLoggedIn())) { | ||
return window.open(SUPABASE_LOGIN_URL, "_blank"); | ||
} | ||
const logo = document.getElementById("ai-description-button-logo"); | ||
|
||
if (!logo) { | ||
return; | ||
} | ||
const url = getPullRequestAPIURL(window.location.href); | ||
const descriptionConfig = await getAIDescriptionConfig(); | ||
|
||
if (!descriptionConfig) { | ||
return; | ||
} | ||
if (!descriptionConfig.enabled) { | ||
return alert("AI PR description is disabled!"); | ||
} | ||
logo.classList.toggle("animate-spin"); | ||
const [diff, commitMessages] = await getDescriptionContext(url, descriptionConfig.config.source); | ||
|
||
if (!isContextWithinBounds([diff, commitMessages], descriptionConfig.config.maxInputLength)) { | ||
logo.classList.toggle("animate-spin"); | ||
return alert(`Max input length exceeded. Try setting the description source to commit-messages.`); | ||
} | ||
const descriptionStream = await generateDescription( | ||
descriptionConfig.config.openai_api_key, | ||
OPEN_AI_COMPLETION_MODEL_NAME, | ||
descriptionConfig.config.language, | ||
descriptionConfig.config.length, | ||
descriptionConfig.config.temperature / 10, | ||
descriptionConfig.config.tone, | ||
diff, | ||
commitMessages, | ||
); | ||
|
||
logo.classList.toggle("animate-spin"); | ||
if (!descriptionStream) { | ||
return console.error("No description was generated!"); | ||
} | ||
const textArea = document.getElementsByName(GITHUB_PR_COMMENT_TEXT_AREA_SELECTOR)[0] as HTMLTextAreaElement; | ||
|
||
void insertAtCursorFromStream(textArea, descriptionStream); | ||
} 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
Oops, something went wrong.