From 40522292b16efff5fb25d4f02000391cacda07ef Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 09:23:25 +0900 Subject: [PATCH 1/9] Fix /chat fails due to permisson being not granted Signed-off-by: Kenta Kozuka --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 97881ed..d73287c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ GitHub Action for performing code reviews using the OpenAI API. ## Initial Setup -`code-butler` works with GitHub Actions. +`code-butler` works with GitHub Actions. Please refer to the following example to set up `.github/workflows/code-butler.yml`. ```yaml @@ -16,6 +16,7 @@ name: Code Butler permissions: contents: read + issues: write pull-requests: write on: From 862a7fbc067f22b513c878e4bb954814c11bb8b9 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 09:55:24 +0900 Subject: [PATCH 2/9] Add translate Signed-off-by: Kenta Kozuka --- src/github.ts | 12 ++++++++++++ src/main.ts | 23 +++++++++++++++++++++++ src/prompt.ts | 12 ++++++++++++ 3 files changed, 47 insertions(+) diff --git a/src/github.ts b/src/github.ts index ab3926b..d17a95f 100644 --- a/src/github.ts +++ b/src/github.ts @@ -28,3 +28,15 @@ export async function createGitHubComment(message: string): Promise { body: message }) } + +export async function editGitHubComment(message: string, commentId: number): Promise { + const token = core.getInput('GITHUB_TOKEN', { required: true }) + const octokit = github.getOctokit(token) + + await octokit.rest.issues.updateComment({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + comment_id: commentId, + body: message + }) +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index c270276..7a7efc0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -51,6 +51,29 @@ export async function run(): Promise { break } + case 'translate': { + const comment = core.getInput('comment_body', { required: false }) + + if (comment === '') { + core.setFailed('Comment body is missing') + } + + const systemPrompt = prompt.getTranslateSystemPrompt() + const responseMessage = ai.completionRequest( + core.getInput('OPENAI_API_KEY', { required: true }), + systemPrompt, + comment + ) + const response = await responseMessage + if (response === '') { + core.setFailed('Response content is missing') + } + + const commentId = core.getInput('comment_id', { required: false }) + await github.editGitHubComment(comment + '\n\n' + response, commentId) + + break + } default: core.setFailed('Unknown command') break diff --git a/src/prompt.ts b/src/prompt.ts index 28e30e4..79ea1b1 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -63,6 +63,14 @@ const chatSystemPrompt = ` Also, don't repeat the prompt in your answer. ` +const translateSystemPrompt = ` + I want you to act as an English translator, spelling corrector and improver. + I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. + I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. + Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. + Also, don't repeat the prompt in your answer. +` + export function getCodeReviewSystemPrompt(): string { return codeReviewSystemPrompt } @@ -70,3 +78,7 @@ export function getCodeReviewSystemPrompt(): string { export function getChatSystemPrompt(): string { return chatSystemPrompt } + +export function getTranslateSystemPrompt(): string { + return chatSystemPrompt +} From e957962beab315dc6654bfcf1a0485b97fbb7eb5 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 10:07:10 +0900 Subject: [PATCH 3/9] test translate Signed-off-by: Kenta Kozuka --- dist/index.js | 41 +++++++++++++++++++++++++++++++++++++++-- src/github.ts | 7 +++++-- src/main.ts | 5 ++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index 5883345..d7ef081 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11467,7 +11467,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.createGitHubComment = exports.getPullRequestDiff = void 0; +exports.editGitHubComment = exports.createGitHubComment = exports.getPullRequestDiff = void 0; const core = __importStar(__nccwpck_require__(2186)); const github = __importStar(__nccwpck_require__(5438)); async function getPullRequestDiff() { @@ -11495,6 +11495,17 @@ async function createGitHubComment(message) { }); } exports.createGitHubComment = createGitHubComment; +async function editGitHubComment(message, commentId) { + const token = core.getInput('GITHUB_TOKEN', { required: true }); + const octokit = github.getOctokit(token); + await octokit.rest.issues.updateComment({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + comment_id: commentId, + body: message + }); +} +exports.editGitHubComment = editGitHubComment; /***/ }), @@ -11566,6 +11577,21 @@ async function run() { await github.createGitHubComment(response); break; } + case 'translate': { + const comment = core.getInput('comment_body', { required: false }); + if (comment === '') { + core.setFailed('Comment body is missing'); + } + const systemPrompt = prompt.getTranslateSystemPrompt(); + const responseMessage = ai.completionRequest(core.getInput('OPENAI_API_KEY', { required: true }), systemPrompt, comment); + const response = await responseMessage; + if (response === '') { + core.setFailed('Response content is missing'); + } + const commentId = core.getInput('comment_id', { required: false }); + await github.editGitHubComment(comment + '\n\n' + response, parseInt(commentId)); + break; + } default: core.setFailed('Unknown command'); break; @@ -11588,7 +11614,7 @@ exports.run = run; "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getChatSystemPrompt = exports.getCodeReviewSystemPrompt = void 0; +exports.getTranslateSystemPrompt = exports.getChatSystemPrompt = exports.getCodeReviewSystemPrompt = void 0; const codeReviewSystemPrompt = ` You are PR Reviewer, a language model tasked with reviewing Git pull requests. Your role is to provide valuable and concise feedback for the PR, with a primary focus on evaluating the new code introduced in the changes (lines starting with '+'). @@ -11652,6 +11678,13 @@ const chatSystemPrompt = ` Please ignore the '/chat' at the beginning of the question. Also, don't repeat the prompt in your answer. `; +const translateSystemPrompt = (/* unused pure expression or super */ null && (` + I want you to act as an English translator, spelling corrector and improver. + I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. + I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. + Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. + Also, don't repeat the prompt in your answer. +`)); function getCodeReviewSystemPrompt() { return codeReviewSystemPrompt; } @@ -11660,6 +11693,10 @@ function getChatSystemPrompt() { return chatSystemPrompt; } exports.getChatSystemPrompt = getChatSystemPrompt; +function getTranslateSystemPrompt() { + return chatSystemPrompt; +} +exports.getTranslateSystemPrompt = getTranslateSystemPrompt; /***/ }), diff --git a/src/github.ts b/src/github.ts index d17a95f..997f3dd 100644 --- a/src/github.ts +++ b/src/github.ts @@ -29,7 +29,10 @@ export async function createGitHubComment(message: string): Promise { }) } -export async function editGitHubComment(message: string, commentId: number): Promise { +export async function editGitHubComment( + message: string, + commentId: number +): Promise { const token = core.getInput('GITHUB_TOKEN', { required: true }) const octokit = github.getOctokit(token) @@ -39,4 +42,4 @@ export async function editGitHubComment(message: string, commentId: number): Pro comment_id: commentId, body: message }) -} \ No newline at end of file +} diff --git a/src/main.ts b/src/main.ts index 7a7efc0..d100c28 100644 --- a/src/main.ts +++ b/src/main.ts @@ -70,7 +70,10 @@ export async function run(): Promise { } const commentId = core.getInput('comment_id', { required: false }) - await github.editGitHubComment(comment + '\n\n' + response, commentId) + await github.editGitHubComment( + comment + '\n\n' + response, + parseInt(commentId) + ) break } From 58b36e53d25b23904ca812ed4eb27a10dad69b71 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 10:31:38 +0900 Subject: [PATCH 4/9] test translate Signed-off-by: Kenta Kozuka --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index d100c28..28ff749 100644 --- a/src/main.ts +++ b/src/main.ts @@ -52,6 +52,7 @@ export async function run(): Promise { break } case 'translate': { + const commentId = core.getInput('comment_id', { required: false }) const comment = core.getInput('comment_body', { required: false }) if (comment === '') { @@ -69,7 +70,6 @@ export async function run(): Promise { core.setFailed('Response content is missing') } - const commentId = core.getInput('comment_id', { required: false }) await github.editGitHubComment( comment + '\n\n' + response, parseInt(commentId) From db973c10239d4d612b93f107e29f2681d46d6080 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 10:36:17 +0900 Subject: [PATCH 5/9] test translate Signed-off-by: Kenta Kozuka --- src/prompt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prompt.ts b/src/prompt.ts index 79ea1b1..9251847 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -80,5 +80,5 @@ export function getChatSystemPrompt(): string { } export function getTranslateSystemPrompt(): string { - return chatSystemPrompt + return translateSystemPrompt } From ad406fe535e1a4f23b06ac312c749e6cd98be069 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 10:37:09 +0900 Subject: [PATCH 6/9] test translate Signed-off-by: Kenta Kozuka --- dist/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index d7ef081..e62fc46 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11578,6 +11578,7 @@ async function run() { break; } case 'translate': { + const commentId = core.getInput('comment_id', { required: false }); const comment = core.getInput('comment_body', { required: false }); if (comment === '') { core.setFailed('Comment body is missing'); @@ -11588,7 +11589,6 @@ async function run() { if (response === '') { core.setFailed('Response content is missing'); } - const commentId = core.getInput('comment_id', { required: false }); await github.editGitHubComment(comment + '\n\n' + response, parseInt(commentId)); break; } @@ -11678,13 +11678,13 @@ const chatSystemPrompt = ` Please ignore the '/chat' at the beginning of the question. Also, don't repeat the prompt in your answer. `; -const translateSystemPrompt = (/* unused pure expression or super */ null && (` +const translateSystemPrompt = ` I want you to act as an English translator, spelling corrector and improver. I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. Also, don't repeat the prompt in your answer. -`)); +`; function getCodeReviewSystemPrompt() { return codeReviewSystemPrompt; } @@ -11694,7 +11694,7 @@ function getChatSystemPrompt() { } exports.getChatSystemPrompt = getChatSystemPrompt; function getTranslateSystemPrompt() { - return chatSystemPrompt; + return translateSystemPrompt; } exports.getTranslateSystemPrompt = getTranslateSystemPrompt; From 745abbd4d7067b363f8d75a546b98a438315e705 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 10:39:51 +0900 Subject: [PATCH 7/9] test translate Signed-off-by: Kenta Kozuka --- dist/index.js | 3 ++- src/main.ts | 2 +- src/prompt.ts | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index e62fc46..902562d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11587,7 +11587,7 @@ async function run() { const responseMessage = ai.completionRequest(core.getInput('OPENAI_API_KEY', { required: true }), systemPrompt, comment); const response = await responseMessage; if (response === '') { - core.setFailed('Response content is missing'); + return; } await github.editGitHubComment(comment + '\n\n' + response, parseInt(commentId)); break; @@ -11683,6 +11683,7 @@ const translateSystemPrompt = ` I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. + If the detected language is English, return empty string. Also, don't repeat the prompt in your answer. `; function getCodeReviewSystemPrompt() { diff --git a/src/main.ts b/src/main.ts index 28ff749..1770511 100644 --- a/src/main.ts +++ b/src/main.ts @@ -67,7 +67,7 @@ export async function run(): Promise { ) const response = await responseMessage if (response === '') { - core.setFailed('Response content is missing') + return } await github.editGitHubComment( diff --git a/src/prompt.ts b/src/prompt.ts index 9251847..75d7073 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -68,6 +68,7 @@ const translateSystemPrompt = ` I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. + If the detected language is English, return empty string. Also, don't repeat the prompt in your answer. ` From 29d55b55f34082f8cfcc0a047654eaa692aff842 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 10:44:57 +0900 Subject: [PATCH 8/9] test translate Signed-off-by: Kenta Kozuka --- dist/index.js | 7 +++++-- src/main.ts | 5 ++++- src/prompt.ts | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 902562d..04e7807 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11587,7 +11587,10 @@ async function run() { const responseMessage = ai.completionRequest(core.getInput('OPENAI_API_KEY', { required: true }), systemPrompt, comment); const response = await responseMessage; if (response === '') { - return; + core.setFailed('Response content is missing'); + } + if (response === 'NO_REPLY') { + break; } await github.editGitHubComment(comment + '\n\n' + response, parseInt(commentId)); break; @@ -11683,7 +11686,7 @@ const translateSystemPrompt = ` I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. - If the detected language is English, return empty string. + If the detected language is English, answer 'NO_REPLY'. Also, don't repeat the prompt in your answer. `; function getCodeReviewSystemPrompt() { diff --git a/src/main.ts b/src/main.ts index 1770511..87c931e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -67,7 +67,10 @@ export async function run(): Promise { ) const response = await responseMessage if (response === '') { - return + core.setFailed('Response content is missing') + } + if (response === 'NO_REPLY') { + break } await github.editGitHubComment( diff --git a/src/prompt.ts b/src/prompt.ts index 75d7073..489314f 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -68,7 +68,7 @@ const translateSystemPrompt = ` I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. - If the detected language is English, return empty string. + If the detected language is English, answer 'NO_REPLY'. Also, don't repeat the prompt in your answer. ` From 5205a9f9292758b22d3c9e7f5931e3e59ef7a0c0 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Thu, 5 Oct 2023 10:48:16 +0900 Subject: [PATCH 9/9] test translate Signed-off-by: Kenta Kozuka --- dist/index.js | 1 + src/main.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/index.js b/dist/index.js index 04e7807..246fe7b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -11590,6 +11590,7 @@ async function run() { core.setFailed('Response content is missing'); } if (response === 'NO_REPLY') { + console.log('Skipping comment'); break; } await github.editGitHubComment(comment + '\n\n' + response, parseInt(commentId)); diff --git a/src/main.ts b/src/main.ts index 87c931e..92146da 100644 --- a/src/main.ts +++ b/src/main.ts @@ -70,6 +70,7 @@ export async function run(): Promise { core.setFailed('Response content is missing') } if (response === 'NO_REPLY') { + console.log('Skipping comment') break }