|
1 | 1 | import { Octokit } from "@octokit/rest" |
2 | 2 | import { ChatCompletionRequestMessage } from "openai-streams" |
| 3 | +import { codexCommand } from "../utils/constants" |
3 | 4 | import { generateChatGpt } from "../utils/generateChatGpt" |
4 | 5 | import { getCodeDiff } from "../utils/getCodeDiff" |
5 | 6 |
|
6 | | -export const startDescription = "\n\n<!-- start pr-codex -->" |
7 | | -export const endDescription = "<!-- end pr-codex -->" |
8 | 7 | 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 (`)." |
| 8 | + "You are a Git diff assistant. Given a code diff, you answer any question related to it. Be concise. Use line breaks and lists to improve readability. Always wrap file names, functions, objects and similar in backticks (`)." |
10 | 9 |
|
11 | 10 | export async function replyIssueComment(payload: any, octokit: Octokit) { |
12 | 11 | // Get relevant PR information |
13 | | - const { repository, issue, sender, comment } = payload |
| 12 | + const { repository, issue, sender, comment, pull_request } = payload |
14 | 13 |
|
15 | | - const question = comment.body.split("/ask-codex")[1].trim() |
| 14 | + const question = comment.body.split(codexCommand)[1].trim() |
16 | 15 |
|
17 | 16 | if (question) { |
18 | | - const { owner, repo, issue_number } = { |
| 17 | + const { owner, repo, number, diff_hunk } = { |
19 | 18 | owner: repository.owner.login, |
20 | 19 | repo: repository.name, |
21 | | - issue_number: issue.number |
| 20 | + number: issue?.number ?? pull_request.number, |
| 21 | + diff_hunk: comment?.diff_hunk |
22 | 22 | } |
23 | 23 |
|
24 | | - // Get the diff content using Octokit and GitHub API |
25 | | - const { codeDiff } = await getCodeDiff(owner, repo, issue_number, octokit) |
| 24 | + // Get the diff content |
| 25 | + const { codeDiff } = diff_hunk |
| 26 | + ? { codeDiff: diff_hunk } |
| 27 | + : await getCodeDiff(owner, repo, number, octokit) |
26 | 28 |
|
27 | 29 | // If there are changes, trigger workflow |
28 | 30 | if (codeDiff?.length != 0) { |
29 | 31 | const messages: ChatCompletionRequestMessage[] = [ |
30 | 32 | { |
31 | 33 | role: "system", |
32 | | - content: systemPrompt |
| 34 | + content: `${systemPrompt}\n\nHere is the code diff:\n\n${codeDiff}` |
33 | 35 | }, |
34 | 36 | { |
35 | 37 | role: "user", |
36 | | - content: `${question}\n\nHere is the code diff:\n\n${codeDiff}` |
| 38 | + content: `${question}` |
37 | 39 | } |
38 | 40 | ] |
39 | 41 |
|
40 | 42 | const codexResponse = await generateChatGpt(messages) |
41 | 43 |
|
42 | 44 | const description = `> ${question}\n\n@${sender.login} ${codexResponse}` |
43 | 45 |
|
44 | | - await octokit.issues.createComment({ |
45 | | - owner, |
46 | | - repo, |
47 | | - issue_number, |
48 | | - body: description |
49 | | - }) |
| 46 | + if (diff_hunk) { |
| 47 | + const { commit_id, path, line, side, start_line, start_side, id } = { |
| 48 | + commit_id: comment.commit_id, |
| 49 | + path: comment.path, |
| 50 | + line: comment.line, |
| 51 | + side: comment.side, |
| 52 | + start_line: comment.start_line, |
| 53 | + start_side: comment.start_side, |
| 54 | + id: comment.id |
| 55 | + } |
| 56 | + |
| 57 | + await octokit.pulls.createReviewComment({ |
| 58 | + owner, |
| 59 | + repo, |
| 60 | + pull_number: number, |
| 61 | + body: description, |
| 62 | + commit_id, |
| 63 | + path, |
| 64 | + line, |
| 65 | + side, |
| 66 | + start_line, |
| 67 | + start_side, |
| 68 | + in_reply_to: id |
| 69 | + }) |
| 70 | + } else { |
| 71 | + await octokit.issues.createComment({ |
| 72 | + owner, |
| 73 | + repo, |
| 74 | + issue_number: number, |
| 75 | + body: description |
| 76 | + }) |
| 77 | + } |
50 | 78 |
|
51 | 79 | return codexResponse |
52 | 80 | } |
|
0 commit comments