|
| 1 | +import { Octokit } from "@octokit/rest" |
| 2 | +import { ChatCompletionRequestMessage } from "openai-streams" |
| 3 | +import { generateChatGpt } from "../utils/generateChatGpt" |
| 4 | +import { getCodeDiff } from "../utils/getCodeDiff" |
| 5 | + |
| 6 | +export const startDescription = "\n\n<!-- start pr-codex -->" |
| 7 | +export const endDescription = "<!-- end pr-codex -->" |
| 8 | +const systemPrompt = |
| 9 | + "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 (`)." |
| 10 | + |
| 11 | +export async function replyIssueComment(payload: any, octokit: Octokit) { |
| 12 | + // Get relevant PR information |
| 13 | + const { repository, issue, sender, comment } = payload |
| 14 | + |
| 15 | + const question = comment.body.split("/codex-ask")[1].trim() |
| 16 | + |
| 17 | + if (question) { |
| 18 | + const { owner, repo, issue_number } = { |
| 19 | + owner: repository.owner.login, |
| 20 | + repo: repository.name, |
| 21 | + issue_number: issue.number |
| 22 | + } |
| 23 | + |
| 24 | + // Get the diff content using Octokit and GitHub API |
| 25 | + const { codeDiff } = await getCodeDiff(owner, repo, issue_number, octokit) |
| 26 | + |
| 27 | + // If there are changes, trigger workflow |
| 28 | + if (codeDiff?.length != 0) { |
| 29 | + const messages: ChatCompletionRequestMessage[] = [ |
| 30 | + { |
| 31 | + role: "system", |
| 32 | + content: systemPrompt |
| 33 | + }, |
| 34 | + { |
| 35 | + role: "user", |
| 36 | + content: `${question}\n\nHere is the code diff:\n\n${codeDiff}` |
| 37 | + } |
| 38 | + ] |
| 39 | + |
| 40 | + const codexResponse = await generateChatGpt(messages) |
| 41 | + |
| 42 | + const description = `> ${question}\n\n@${sender.login} ${codexResponse}` |
| 43 | + |
| 44 | + await octokit.issues.createComment({ |
| 45 | + owner, |
| 46 | + repo, |
| 47 | + issue_number, |
| 48 | + body: description |
| 49 | + }) |
| 50 | + |
| 51 | + return codexResponse |
| 52 | + } |
| 53 | + throw new Error("No changes in PR") |
| 54 | + } |
| 55 | +} |
0 commit comments