Merge pull request #1 from lukehollenback/custom-prompts

Added custom prompt support.
This commit is contained in:
Luke Hollenback 2024-04-02 11:07:30 -06:00 committed by GitHub
commit f578b5b665
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 4 deletions

View file

@ -15,6 +15,10 @@ inputs:
description: "Glob patterns to exclude files from the diff analysis"
required: false
default: ""
custom_prompts:
description: "Custom commands to augment the agent's prompts with. Each line is an individual command."
required: false
default: ""
runs:
using: "node16"
main: "dist/index.js"

View file

@ -58,14 +58,15 @@ async function getDiff(
async function analyzeCode(
parsedDiff: File[],
prDetails: PRDetails
prDetails: PRDetails,
customPrompts: string
): Promise<Array<{ body: string; path: string; line: number }>> {
const comments: Array<{ body: string; path: string; line: number }> = [];
for (const file of parsedDiff) {
if (file.to === "/dev/null") continue; // Ignore deleted files
for (const chunk of file.chunks) {
const prompt = createPrompt(file, chunk, prDetails);
const prompt = createPrompt(file, chunk, prDetails, customPrompts);
const aiResponse = await getAIResponse(prompt);
if (aiResponse) {
const newComments = createComment(file, chunk, aiResponse);
@ -78,7 +79,7 @@ async function analyzeCode(
return comments;
}
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails, customPrompts: string): string {
return `Your task is to review pull requests. Instructions:
- Provide the response in following JSON format: {"reviews": [{"lineNumber": <line_number>, "reviewComment": "<review comment>"}]}
- Do not give positive comments or compliments.
@ -86,6 +87,7 @@ function createPrompt(file: File, chunk: Chunk, prDetails: PRDetails): string {
- Write the comment in GitHub Markdown format.
- Use the given description only for the overall context and only comment the code.
- IMPORTANT: NEVER suggest adding comments to the code.
${customPrompts}
Review the following code diff in the file "${
file.to
@ -232,7 +234,11 @@ async function main() {
);
});
const comments = await analyzeCode(filteredDiff, prDetails);
const customPrompts = core.getMultilineInput("custom_prompts")
.map(customPrompt => `- ${customPrompt}`)
.join("\n")
const comments = await analyzeCode(filteredDiff, prDetails, customPrompts);
if (comments.length > 0) {
await createReviewComment(
prDetails.owner,