-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR-Codex init #4
Open
jjranalli
wants to merge
20
commits into
codex-base
Choose a base branch
from
codex-init
base: codex-base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3c403b8
Merge branch 'dev'
jjranalli 8798c11
Merge branch 'dev'
jjranalli 6394088
Merge branch 'dev'
jjranalli 42c7509
Merge branch 'dev'
jjranalli 39a899e
fix: handle null descriptions and improved response formatting
jjranalli b3334ba
refactor: rename testPayload
jjranalli 88c7cf4
feat: add testPayloadComment
jjranalli c732a77
feat: add reply script
jjranalli ffdfc81
fix: refactor and optimize summarizePullRequest
jjranalli f96bb0b
fix: comments
jjranalli 6031ad8
fix: rename
jjranalli 1d1832a
refactor: remove unneeded data from testPayloadComment
jjranalli 1884524
feat: add replyIssueComment
jjranalli fbfc6e8
nit: change codex-ask to ask-codex
jjranalli e5d6b04
wip
jjranalli 746e584
fix: optimize diff parse
jjranalli cde8059
fix: prompts and logic
jjranalli 6ebf83c
Merge branch 'dev'
jjranalli b2bd020
fix: prompt
jjranalli 679f69b
Merge branch 'dev'
jjranalli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ PR-Codex is currently free. | |
--- | ||
|
||
Made by [dlabs](https://dlabs.app) | ||
|
||
<!-- wip --> |
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 |
---|---|---|
@@ -1,22 +1,24 @@ | ||
export function joinStringsUntilMaxLength( | ||
parsedFiles: string[], | ||
maxLength: number | ||
): string { | ||
let combinedString = "" | ||
) { | ||
let codeDiff = "" | ||
let currentLength = 0 | ||
let maxLengthExceeded = false | ||
|
||
for (const file of parsedFiles) { | ||
const fileLength = file.length | ||
|
||
if (currentLength + fileLength <= maxLength) { | ||
combinedString += file | ||
codeDiff += file | ||
currentLength += fileLength | ||
} else { | ||
maxLengthExceeded = true | ||
const remainingLength = maxLength - currentLength | ||
combinedString += file.slice(0, remainingLength) | ||
codeDiff += file.slice(0, remainingLength) | ||
break | ||
} | ||
} | ||
|
||
return combinedString | ||
return { codeDiff, maxLengthExceeded } | ||
} |
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,55 @@ | ||
import { Octokit } from "@octokit/rest" | ||
import { ChatCompletionRequestMessage } from "openai-streams" | ||
import { generateChatGpt } from "../utils/generateChatGpt" | ||
import { getCodeDiff } from "../utils/getCodeDiff" | ||
|
||
export const startDescription = "\n\n<!-- start pr-codex -->" | ||
export const endDescription = "<!-- end pr-codex -->" | ||
const systemPrompt = | ||
"You are a Git diff assistant. Given a code diff, you answer any question related to it. Be concise. Always wrap file names, functions, objects and similar in backticks (`)." | ||
|
||
export async function replyIssueComment(payload: any, octokit: Octokit) { | ||
// Get relevant PR information | ||
const { repository, issue, sender, comment } = payload | ||
|
||
const question = comment.body.split("/ask-codex")[1].trim() | ||
|
||
if (question) { | ||
const { owner, repo, issue_number } = { | ||
owner: repository.owner.login, | ||
repo: repository.name, | ||
issue_number: issue.number | ||
} | ||
|
||
// Get the diff content using Octokit and GitHub API | ||
const { codeDiff } = await getCodeDiff(owner, repo, issue_number, octokit) | ||
|
||
// If there are changes, trigger workflow | ||
if (codeDiff?.length != 0) { | ||
const messages: ChatCompletionRequestMessage[] = [ | ||
{ | ||
role: "system", | ||
content: `${systemPrompt}\n\nHere is the code diff:\n\n${codeDiff}` | ||
}, | ||
{ | ||
role: "user", | ||
content: `${question}` | ||
} | ||
] | ||
|
||
const codexResponse = await generateChatGpt(messages) | ||
|
||
const description = `> ${question}\n\n@${sender.login} ${codexResponse}` | ||
|
||
await octokit.issues.createComment({ | ||
owner, | ||
repo, | ||
issue_number, | ||
body: description | ||
}) | ||
|
||
return codexResponse | ||
} | ||
throw new Error("No changes in PR") | ||
} | ||
} |
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 |
---|---|---|
@@ -1,139 +1,81 @@ | ||
import { Octokit } from "@octokit/rest" | ||
import { ChatCompletionRequestMessage, OpenAI } from "openai-streams" | ||
import { yieldStream } from "yield-stream" | ||
import { parseDiff } from "../utils/parseDiff" | ||
import { joinStringsUntilMaxLength } from "./joinStringsUntilMaxLength" | ||
import { ChatCompletionRequestMessage } from "openai-streams" | ||
import { generateChatGpt } from "../utils/generateChatGpt" | ||
import { getCodeDiff } from "../utils/getCodeDiff" | ||
|
||
export const startDescription = "<!-- start pr-codex -->" | ||
export const startDescription = "\n\n<!-- start pr-codex -->" | ||
export const endDescription = "<!-- end pr-codex -->" | ||
const systemPrompt = | ||
"You are a Git diff assistant. Given a code diff, you provide a clear and concise description of its content. Always wrap file names, functions, objects and similar in backticks (`)." | ||
|
||
export async function summarizePullRequest(payload: any, octokit: Octokit) { | ||
// Get relevant PR information | ||
const pr = payload.pull_request | ||
const { owner, repo, number } = { | ||
const { owner, repo, pull_number } = { | ||
owner: pr.base.repo.owner.login, | ||
repo: pr.base.repo.name, | ||
number: pr.number | ||
pull_number: pr.number | ||
} | ||
|
||
// Get the diff content using Octokit and GitHub API | ||
const compareResponse = await octokit.rest.repos.compareCommits({ | ||
const { codeDiff, skippedFiles, maxLengthExceeded } = await getCodeDiff( | ||
owner, | ||
repo, | ||
base: pr.base.sha, | ||
head: pr.head.sha, | ||
mediaType: { | ||
format: "diff" | ||
} | ||
}) | ||
const diffContent = String(compareResponse.data) | ||
|
||
// Parses the diff content and returns the parsed files. | ||
// If the number of changes in a file is greater than 1k changes, the file will be skipped. | ||
// The codeDiff is the joined string of parsed files, up to a max length of 10k. | ||
const maxChanges = 1000 | ||
const { parsedFiles, skippedFiles } = parseDiff(diffContent, maxChanges) | ||
const codeDiff = joinStringsUntilMaxLength(parsedFiles, 10000) | ||
pull_number, | ||
octokit | ||
) | ||
|
||
// If there are changes, trigger workflow | ||
if (codeDiff?.length != 0) { | ||
const systemPrompt = `You are a Git diff assistant. Always begin with "This PR". Given a code diff, you provide a simple description in prose, in less than 300 chars, which sums up the changes. Continue with "\n\n### Detailed summary\n" and make a comprehensive list of all changes, excluding any eventual skipped files. Be concise. Always wrap file names, functions, objects and similar in backticks (\`).${ | ||
skippedFiles.length != 0 | ||
? ` After the list, conclude with "\n\n> " and mention that the following files were skipped due to too many changes: ${skippedFiles.join( | ||
"," | ||
)}.` | ||
: "" | ||
}` | ||
|
||
const messages: ChatCompletionRequestMessage[] = [ | ||
{ | ||
role: "system", | ||
content: systemPrompt | ||
content: `${systemPrompt}\n\nHere is the code diff:\n\n${codeDiff}` | ||
}, | ||
{ | ||
role: "user", | ||
content: `Here is the code diff:\n\n${codeDiff}` | ||
content: | ||
'Starting with "This PR", clearly explain the focus of this PR in prose, in less than 300 characters. Then follow up with "\n\n### Detailed summary\n" and make a comprehensive list of all changes.' | ||
} | ||
] | ||
|
||
const summary = await generateChatGpt(messages) | ||
const codexResponse = await generateChatGpt(messages) | ||
|
||
// Check if the PR already has a comment from the bot | ||
const hasCodexCommented = | ||
payload.action == "synchronize" && | ||
pr.body?.split("\n\n" + startDescription).length > 1 | ||
|
||
// if (firstComment) { | ||
// // Edit pinned bot comment to the PR | ||
// await octokit.issues.updateComment({ | ||
// owner, | ||
// repo, | ||
// comment_id: firstComment.id, | ||
// body: summary | ||
// }) | ||
// } else { | ||
// // Add a comment to the PR | ||
// await octokit.issues.createComment({ | ||
// owner, | ||
// repo, | ||
// issue_number: number, | ||
// body: summary | ||
// }) | ||
// } | ||
pr.body?.split(startDescription).length > 1 | ||
|
||
const prCodexText = `\n\n${startDescription}\n\n---\n\n## PR-Codex overview\n${summary}\n\n${endDescription}` | ||
const prCodexText = `${startDescription}\n\n${ | ||
(hasCodexCommented ? pr.body.split(startDescription)[0].trim() : pr.body) | ||
? "---\n\n" | ||
: "" | ||
}## PR-Codex overview\n${codexResponse}${ | ||
skippedFiles.length != 0 | ||
? `\n\n> The following files were skipped due to too many changes: ${skippedFiles.join( | ||
", " | ||
)}` | ||
: "" | ||
}${ | ||
maxLengthExceeded | ||
? "\n\n> The code diff exceeds the max number of characters, so this overview may be incomplete. Keep PRs small to avoid this issue." | ||
: "" | ||
}\n\n✨ Ask PR-Codex anything about this PR by commenting below with \`/ask-codex {your question}\`\n\n${endDescription}` | ||
|
||
const description = hasCodexCommented | ||
? pr.body.split("\n\n" + startDescription)[0] + | ||
? pr.body.split(startDescription)[0] + | ||
prCodexText + | ||
pr.body.split(endDescription)[1] | ||
: pr.body + prCodexText | ||
: (pr.body ?? "") + prCodexText | ||
|
||
await octokit.issues.update({ | ||
owner, | ||
repo, | ||
issue_number: number, | ||
issue_number: pull_number, | ||
body: description | ||
}) | ||
|
||
return summary | ||
return codexResponse | ||
} | ||
throw new Error("No changes in PR") | ||
} | ||
|
||
const generateChatGpt = async (messages: ChatCompletionRequestMessage[]) => { | ||
const DECODER = new TextDecoder() | ||
let text = "" | ||
|
||
try { | ||
const stream = await OpenAI( | ||
"chat", | ||
{ | ||
model: "gpt-3.5-turbo", | ||
temperature: 0.7, | ||
messages | ||
}, | ||
{ apiKey: process.env.OPENAI_API_KEY } | ||
) | ||
|
||
for await (const chunk of yieldStream(stream)) { | ||
try { | ||
const decoded: string = DECODER.decode(chunk) | ||
|
||
if (decoded === undefined) | ||
throw new Error( | ||
"No choices in response. Decoded response: " + | ||
JSON.stringify(decoded) | ||
) | ||
|
||
text += decoded | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
|
||
return text | ||
} |
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,33 @@ | ||
import dotenv from "dotenv" | ||
import { handleGithubAuth } from "../lib/handleGithubAuth" | ||
import { replyIssueComment } from "../lib/replyIssueComment" | ||
import { testPayloadComment } from "../utils/github/testPayloadComment" | ||
|
||
dotenv.config() | ||
|
||
// Customize payload in `utils/testPayloadComment` | ||
|
||
async function main() { | ||
try { | ||
const octokit = await handleGithubAuth(testPayloadComment) | ||
|
||
console.log("Generating comment...") | ||
|
||
const comment = await replyIssueComment(testPayloadComment, octokit) | ||
|
||
console.log( | ||
"PR-Codex commented:\n\n", | ||
comment, | ||
"\n\nView on Github: https://github.com/decentralizedlabs/pr-codex/pull/4" | ||
) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
|
||
main() | ||
.then(() => process.exit(0)) | ||
.catch((error) => { | ||
console.error(error) | ||
process.exit(1) | ||
}) |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/codex explain this