Skip to content

Commit

Permalink
added comment reply events - major refactor contn'd
Browse files Browse the repository at this point in the history
  • Loading branch information
suitedaces committed Dec 27, 2023
1 parent e5d3229 commit 77bcf2f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
15 changes: 13 additions & 2 deletions src/handlers/PRCommentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ export class PRCommentHandler {
// Read event data
const event = JSON.parse(readFileSync(eventPath, 'utf8'));
const { pull_request: pr, comment } = event;

// TODO: remove
await this.gitHubService.addReactionToComment(pr.base.repo.owner.login, pr.base.repo.name, comment.id, 'laugh');

// Check if Dexter is mentioned in the comment, if not then skip
if (!comment.body.includes('Dexter','dexter','dexter-ai')) {
console.log('Dexter not mentioned, skipping response...');
return;
}

// Add reaction to the comment
await this.gitHubService.addReactionToComment(pr.base.repo.owner.login, pr.base.repo.name, comment.id, 'eyes');



// Fetch PR metadata and diff
const prDetails: PRMetadata = await this.gitHubService.getPRMetadata(eventPath);
Expand All @@ -36,10 +47,10 @@ export class PRCommentHandler {
const prompt = createPRCommentResponsePrompt(prDetails, discussionThread, comment.body, comment.user.login, diffFiles)

// Get AI response for the comment
const aiResponse = await this.aiService.getAIPullRequestResponse(prompt, this.appConfig.OPENAI_API_MODEL);
const aiResponse = await this.aiService.getAIPullRequestCommentResponse(prompt, this.appConfig.OPENAI_API_MODEL);

// Process AI response and create a comment reply
const replyMessage = aiResponse?.[0]?.reviewComment || `Sorry, can't help you with that, ${comment.user.login}! 😭`;
const replyMessage = aiResponse || `Sorry, can't help you with that, ${comment.user.login} (blame OpenAI!) 😭`;

// Reply to the comment in the PR
await this.gitHubService.createReviewCommentReply(prDetails.owner, prDetails.repo, prDetails.pull_number, comment.id, replyMessage);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const githubService = new GitHubService(appConfig.GITHUB_TOKEN);
const aiService = new OpenAIService(appConfig.OPENAI_API_KEY);

const prHandler = new PRHandler(githubService, aiService, appConfig);
const prCommentHandler = new PRCommentHandler(githubService);
const prCommentHandler = new PRCommentHandler(githubService, aiService, appConfig);

// Example event data structure
const event = {
Expand Down
24 changes: 24 additions & 0 deletions src/services/OpenAIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,28 @@ export class OpenAIService {
return null;
}
}

async getAIPullRequestCommentResponse(prompt: string, model: string): Promise<string> {
const queryConfig = {
model,
temperature: 0.3,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
};

try {
const response = await this.openai.chat.completions.create({
...queryConfig,
messages: [{ role: "system", content: prompt }],
});

const res = response.choices[0].message?.content?.trim() || "";
return res;
} catch (error) {
console.error("Error in OpenAI Service:", error);
return "I'm sorry, I couldn't generate a response at this time.";
}
}
}
19 changes: 11 additions & 8 deletions src/utils/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ export function createPRReviewPrompt(file: File, chunk: Chunk, prDetails: PRMeta
export function createPRCommentResponsePrompt(prDetails: PRMetadata, discussionThread: string, triggeringComment: string, commenterName: string, codeDiffs: File[]): string {
const basePrompt = createBasePrompt(prDetails);
let codeDiffContext = '';
for (const { filePath, chunk } of codeDiffs) {
const codeDiff = `Code to Review (File: ${file.path}): \n\`\`\`\ndiff ${chunk.content} ${chunk.changes.map(c => `${c.ln ? c.ln : c.ln2} ${c.content}`).join("\n")}\n\`\`\`\n`;
codeDiffContext += codeDiff + '\n';

for (const file of codeDiffs) {
for (const chunk of file.chunks) {
const codeDiff = `Code to Review (File: ${file.path}): \n\`\`\`\ndiff ${chunk.content} ${chunk.changes.map(c => `${c.ln || c.ln2}: ${c.content}`).join("\n")}\n\`\`\`\n`;
codeDiffContext += codeDiff + '\n';
}
}

const prompt = `${basePrompt}
Task: Respond to a comment in a pull request with a suitable reply
Task: Respond to a comment in a pull request adhering to the instructions given to you.
Code diff in the pull request: ${codeDiffContext}
Code diff in the pull request:
${codeDiffContext}
Discussion Thread:
${discussionThread}
Reply to Comment by ${commenterName}: "${triggeringComment}"
Based on the provided discussion context and the specific query in the triggering comment, generate a suitable response that addresses ${commenterName}'s concerns or questions in EXACTLY this JSON
Based on the provided discussion context and the specific query in the triggering comment, generate a suitable response that addresses ${commenterName}'s concerns or questions in JSON format.
`;

console.log("Prompt: ", prompt);

return prompt;
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"compilerOptions": {
"target": "ESNext",
"module": "CommonJS",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true
}
}
}

0 comments on commit 77bcf2f

Please sign in to comment.