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: diff --git a/dist/index.js b/dist/index.js index 5883345..246fe7b 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,25 @@ async function run() { await github.createGitHubComment(response); 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'); + } + 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'); + } + if (response === 'NO_REPLY') { + console.log('Skipping comment'); + break; + } + await github.editGitHubComment(comment + '\n\n' + response, parseInt(commentId)); + break; + } default: core.setFailed('Unknown command'); break; @@ -11588,7 +11618,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 +11682,14 @@ const chatSystemPrompt = ` Please ignore the '/chat' at the beginning of the question. 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. + If the detected language is English, answer 'NO_REPLY'. + Also, don't repeat the prompt in your answer. +`; function getCodeReviewSystemPrompt() { return codeReviewSystemPrompt; } @@ -11660,6 +11698,10 @@ function getChatSystemPrompt() { return chatSystemPrompt; } exports.getChatSystemPrompt = getChatSystemPrompt; +function getTranslateSystemPrompt() { + return translateSystemPrompt; +} +exports.getTranslateSystemPrompt = getTranslateSystemPrompt; /***/ }), diff --git a/src/github.ts b/src/github.ts index ab3926b..997f3dd 100644 --- a/src/github.ts +++ b/src/github.ts @@ -28,3 +28,18 @@ 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 + }) +} diff --git a/src/main.ts b/src/main.ts index c270276..92146da 100644 --- a/src/main.ts +++ b/src/main.ts @@ -51,6 +51,36 @@ 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 === '') { + 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') + } + if (response === 'NO_REPLY') { + console.log('Skipping comment') + break + } + + await github.editGitHubComment( + comment + '\n\n' + response, + parseInt(commentId) + ) + + break + } default: core.setFailed('Unknown command') break diff --git a/src/prompt.ts b/src/prompt.ts index 28e30e4..489314f 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -63,6 +63,15 @@ 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. + If the detected language is English, answer 'NO_REPLY'. + Also, don't repeat the prompt in your answer. +` + export function getCodeReviewSystemPrompt(): string { return codeReviewSystemPrompt } @@ -70,3 +79,7 @@ export function getCodeReviewSystemPrompt(): string { export function getChatSystemPrompt(): string { return chatSystemPrompt } + +export function getTranslateSystemPrompt(): string { + return translateSystemPrompt +}