-
Notifications
You must be signed in to change notification settings - Fork 169
Review Agent #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Review Agent #298
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d37daee
push review agent implementation
msukkari 17fce5c
feedback
msukkari 12002f2
wip integrating review agent into monorepo
msukkari a81cd24
move review agent to web
msukkari 79dd288
feedback
msukkari ef978fd
feedback
msukkari 944aeb5
add rate limit throttling to octokit
msukkari 2d0977e
configure agent ui in app
msukkari 871d410
docs
msukkari a242a69
add review command logic and add logging for review agent to data cac…
msukkari 5323cfa
fix bug with llm returning multiple reviews in single invocation
msukkari c586558
fix doc link bug
msukkari 2307106
feedback and improved docs for review agent
msukkari 0ccd4a8
review agent doc nits
msukkari fa40daf
merge main
msukkari ec9526f
mcp doc nit
msukkari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { Header } from "../components/header"; | ||
| import Link from "next/link"; | ||
| import Image from "next/image"; | ||
| import { NavigationMenu } from "../components/navigationMenu"; | ||
| import { FaRobot, FaCogs } from "react-icons/fa"; | ||
| import { MdRocketLaunch } from "react-icons/md"; | ||
|
|
||
| const agents = [ | ||
| { | ||
| id: "review-agent", | ||
| name: "Review Agent", | ||
| description: "An agent that reviews your PRs. Uses the code indexed on Sourcebot to provide codebase wide context.", | ||
| deployUrl: "/agents/review-agent/deploy", | ||
| configureUrl: "/agents/review-agent/configure", | ||
| }, | ||
| // Add more agents here as needed | ||
| ]; | ||
|
|
||
| export default function AgentsPage({ params: { domain } }: { params: { domain: string } }) { | ||
| return ( | ||
| <div className="flex flex-col items-center overflow-hidden min-h-screen"> | ||
| <NavigationMenu domain={domain} /> | ||
| <div className="w-full max-w-6xl px-4 mt-12 mb-24"> | ||
| <div | ||
| className={ | ||
| agents.length === 1 | ||
| ? "flex justify-center items-center min-h-[60vh]" | ||
| : "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-10" | ||
| } | ||
| > | ||
| {agents.map((agent) => ( | ||
| <div | ||
| key={agent.id} | ||
| className={ | ||
| agents.length === 1 | ||
| ? "relative flex flex-col items-center border border-border rounded-2xl p-8 bg-card shadow-xl w-full max-w-xl" | ||
| : "relative flex flex-col items-center border border-border rounded-2xl p-8 bg-card shadow-xl" | ||
| } | ||
| > | ||
| {/* Name and description */} | ||
| <div className="flex flex-col items-center w-full"> | ||
| <h2 className="font-bold text-2xl mb-4 mt-2 text-center text-foreground drop-shadow-sm"> | ||
| {agent.name} | ||
| </h2> | ||
| <p className="text-base text-muted-foreground text-center mb-4 min-h-[56px]"> | ||
| {agent.description} | ||
| </p> | ||
| </div> | ||
| {/* Actions */} | ||
| <div className="flex flex-row gap-4 justify-center w-full mt-2"> | ||
| <Link | ||
| href={agent.deployUrl} | ||
| className="flex items-center justify-center gap-2 px-5 py-2.5 rounded-md bg-primary text-primary-foreground font-mono font-semibold text-base border border-primary shadow-sm hover:bg-primary/80 focus:outline-none focus:ring-2 focus:ring-primary/60 transition w-1/2" | ||
| > | ||
| <MdRocketLaunch className="text-lg" /> Deploy | ||
| </Link> | ||
| <Link | ||
| href={agent.configureUrl} | ||
| className="flex items-center justify-center gap-2 px-5 py-2.5 rounded-md bg-muted text-foreground font-mono font-semibold text-base border border-border shadow-sm hover:bg-card/80 focus:outline-none focus:ring-2 focus:ring-border/60 transition w-1/2" | ||
| > | ||
| <FaCogs className="text-lg" /> Configure | ||
| </Link> | ||
| </div> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| 'use server'; | ||
|
|
||
| import { NextRequest } from "next/server"; | ||
| import { App } from "octokit"; | ||
| import { WebhookEventDefinition } from "@octokit/webhooks/types"; | ||
| import { env } from "@/env.mjs"; | ||
| import { processGitHubPullRequest } from "@/features/agents/review-agent/app"; | ||
| import fs from "fs"; | ||
|
|
||
| let githubApp: App | undefined; | ||
| if (env.GITHUB_APP_ID && env.GITHUB_APP_WEBHOOK_SECRET && env.GITHUB_APP_PRIVATE_KEY_PATH) { | ||
| try { | ||
| const privateKey = fs.readFileSync(env.GITHUB_APP_PRIVATE_KEY_PATH, "utf8"); | ||
| githubApp = new App({ | ||
| appId: env.GITHUB_APP_ID, | ||
| privateKey: privateKey, | ||
| webhooks: { | ||
| secret: env.GITHUB_APP_WEBHOOK_SECRET, | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| console.error(`Error initializing GitHub app: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| function isPullRequestEvent(eventHeader: string, payload: unknown): payload is WebhookEventDefinition<"pull-request-opened"> | WebhookEventDefinition<"pull-request-synchronize"> { | ||
| return eventHeader === "pull_request" && typeof payload === "object" && payload !== null && "action" in payload && typeof payload.action === "string" && (payload.action === "opened" || payload.action === "synchronize"); | ||
| } | ||
|
|
||
| export const POST = async (request: NextRequest) => { | ||
| const body = await request.json(); | ||
| const headers = Object.fromEntries(request.headers.entries()); | ||
|
|
||
| const githubEvent = headers['x-github-event']; | ||
| if (githubEvent) { | ||
| console.log('GitHub event received:', githubEvent); | ||
msukkari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!githubApp) { | ||
| console.warn('Received GitHub webhook event but GitHub app env vars are not set'); | ||
| return Response.json({ status: 'ok' }); | ||
| } | ||
|
|
||
| if (isPullRequestEvent(githubEvent, body)) { | ||
| if (!body.installation) { | ||
| console.error('Received github pull request event but installation is not present'); | ||
| return Response.json({ status: 'ok' }); | ||
| } | ||
|
|
||
| const installationId = body.installation.id; | ||
| const octokit = await githubApp.getInstallationOctokit(installationId); | ||
|
|
||
| await processGitHubPullRequest(octokit, body); | ||
| } | ||
| } | ||
|
|
||
| return Response.json({ status: 'ok' }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Octokit } from "octokit"; | ||
| import { WebhookEventDefinition } from "@octokit/webhooks/types"; | ||
| import { generatePrReviews } from "@/features/agents/review-agent/nodes/generatePrReview"; | ||
| import { githubPushPrReviews } from "@/features/agents/review-agent/nodes/githubPushPrReviews"; | ||
| import { githubPrParser } from "@/features/agents/review-agent/nodes/githubPrParser"; | ||
| import { env } from "@/env.mjs"; | ||
|
|
||
| const rules = [ | ||
| "Do NOT provide general feedback, summaries, explanations of changes, or praises for making good additions.", | ||
| "Do NOT provide any advice that is not actionable or directly related to the changes.", | ||
| "Do NOT provide any comments or reviews on code that you believe is good, correct, or a good addition. Your job is only to identify issues and provide feedback on how to fix them.", | ||
| "If a review for a chunk contains different reviews at different line ranges, return a seperate review object for each line range.", | ||
| "Focus solely on offering specific, objective insights based on the given context and refrain from making broad comments about potential impacts on the system or question intentions behind the changes.", | ||
| "Keep comments concise and to the point. Every comment must highlight a specific issue and provide a clear and actionable solution to the developer.", | ||
| "If there are no issues found on a line range, do NOT respond with any comments. This includes comments such as \"No issues found\" or \"LGTM\"." | ||
| ] | ||
|
|
||
| export async function processGitHubPullRequest(octokit: Octokit, payload: WebhookEventDefinition<"pull-request-opened"> | WebhookEventDefinition<"pull-request-synchronize">) { | ||
| console.log(`Received a pull request event for #${payload.pull_request.number}`); | ||
|
|
||
| if (!env.OPENAI_API_KEY) { | ||
| console.error("OPENAI_API_KEY is not set, skipping review agent"); | ||
| return; | ||
| } | ||
|
|
||
| const prPayload = await githubPrParser(octokit, payload); | ||
| const fileDiffReviews = await generatePrReviews(prPayload, rules); | ||
| await githubPushPrReviews(octokit, prPayload, fileDiffReviews); | ||
| } |
40 changes: 40 additions & 0 deletions
40
packages/web/src/features/agents/review-agent/nodes/fetchFileContent.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { sourcebot_context, sourcebot_pr_payload } from "@/features/agents/review-agent/types"; | ||
| import { fileSourceResponseSchema } from "@/features/search/schemas"; | ||
| import { base64Decode } from "@/lib/utils"; | ||
|
|
||
| export const fetchFileContent = async (pr_payload: sourcebot_pr_payload, filename: string): Promise<sourcebot_context> => { | ||
| console.log("Executing fetch_file_content"); | ||
|
|
||
| const repoPath = pr_payload.hostDomain + "/" + pr_payload.owner + "/" + pr_payload.repo; | ||
msukkari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const fileSourceRequest = { | ||
| fileName: filename, | ||
| repository: repoPath, | ||
| } | ||
| console.log(JSON.stringify(fileSourceRequest, null, 2)); | ||
|
|
||
| const response = await fetch('http://localhost:3000/api/source', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Org-Domain': '~' | ||
msukkari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
msukkari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| body: JSON.stringify(fileSourceRequest) | ||
| }); | ||
|
|
||
msukkari marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!response.ok) { | ||
| throw new Error(`Failed to fetch file content for ${filename} from ${repoPath}: ${response.statusText}`); | ||
| } | ||
|
|
||
| const responseData = await response.json(); | ||
| const fileSourceResponse = fileSourceResponseSchema.parse(responseData); | ||
| const fileContent = base64Decode(fileSourceResponse.source); | ||
|
|
||
| const fileContentContext: sourcebot_context = { | ||
| type: "file_content", | ||
| description: `The content of the file ${filename}`, | ||
| context: fileContent, | ||
| } | ||
|
|
||
| console.log("Completed fetch_file_content"); | ||
| return fileContentContext; | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
packages/web/src/features/agents/review-agent/nodes/generateDiffReviewPrompt.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { sourcebot_diff, sourcebot_context, sourcebot_diff_review_schema } from "@/features/agents/review-agent/types"; | ||
| import { zodToJsonSchema } from "zod-to-json-schema"; | ||
|
|
||
| export const generateDiffReviewPrompt = async (diff: sourcebot_diff, context: sourcebot_context[], rules: string[]) => { | ||
| console.log("Executing generate_diff_review_prompt"); | ||
|
|
||
| const prompt = ` | ||
| You are an expert software engineer that excells at reviewing code changes. Given the input, additional context, and rules defined below, review the code changes and provide a detailed review. The review you provide | ||
| must conform to all of the rules defined below. The output format of your review must conform to the output format defined below. | ||
|
|
||
| # Input | ||
|
|
||
| The input is the old and new code snippets, which represent a single hunk from a git diff. The old code snippet is the code before the changes were made, and the new code snippet is the code after the changes were made. Each code snippet | ||
| is a sequence of lines each with a line number. | ||
|
|
||
| ## Old Code Snippet | ||
|
|
||
| \`\`\` | ||
| ${diff.oldSnippet} | ||
| \`\`\` | ||
|
|
||
| ## New Code Snippet | ||
|
|
||
| \`\`\` | ||
| ${diff.newSnippet} | ||
| \`\`\` | ||
|
|
||
| # Additional Context | ||
|
|
||
| ${context.map(c => `${c.type}: ${c.description}\n\n${c.context}`).join("\n\n----------------------\n\n")} | ||
|
|
||
| # Rules | ||
|
|
||
| - ${rules.join("\n- ")} | ||
|
|
||
| # Output Format (JSON Schema) | ||
| The output must be a valid JSON object that conforms to the following JSON schema. Do NOT respond with anything other than the JSON object. Do NOT respond with | ||
| the JSON object in a markdown code block. | ||
| ${JSON.stringify(zodToJsonSchema(sourcebot_diff_review_schema), null, 2)} | ||
| `; | ||
|
|
||
| console.log("Completed generate_diff_review_prompt"); | ||
| return prompt; | ||
| } |
49 changes: 49 additions & 0 deletions
49
packages/web/src/features/agents/review-agent/nodes/generatePrReview.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { sourcebot_pr_payload, sourcebot_diff_review, sourcebot_file_diff_review, sourcebot_context } from "@/features/agents/review-agent/types"; | ||
| import { generateDiffReviewPrompt } from "@/features/agents/review-agent/nodes/generateDiffReviewPrompt"; | ||
| import { invokeDiffReviewLlm } from "@/features/agents/review-agent/nodes/invokeDiffReviewLlm"; | ||
| import { fetchFileContent } from "@/features/agents/review-agent/nodes/fetchFileContent"; | ||
|
|
||
| export const generatePrReviews = async (pr_payload: sourcebot_pr_payload, rules: string[]): Promise<sourcebot_file_diff_review[]> => { | ||
| console.log("Executing generate_pr_reviews"); | ||
|
|
||
| const file_diff_reviews: sourcebot_file_diff_review[] = []; | ||
| for (const file_diff of pr_payload.file_diffs) { | ||
| const reviews: sourcebot_diff_review[] = []; | ||
|
|
||
| for (const diff of file_diff.diffs) { | ||
| try { | ||
| const fileContentContext = await fetchFileContent(pr_payload, file_diff.to); | ||
| const context: sourcebot_context[] = [ | ||
| { | ||
| type: "pr_title", | ||
| description: "The title of the pull request", | ||
| context: pr_payload.title, | ||
| }, | ||
| { | ||
| type: "pr_description", | ||
| description: "The description of the pull request", | ||
| context: pr_payload.description, | ||
| }, | ||
| fileContentContext, | ||
| ]; | ||
|
|
||
| const prompt = await generateDiffReviewPrompt(diff, context, rules); | ||
|
|
||
| const diffReview = await invokeDiffReviewLlm(prompt); | ||
| reviews.push(diffReview); | ||
| } catch (error) { | ||
| console.error(`Error fetching file content for ${file_diff.to}: ${error}`); | ||
| } | ||
| } | ||
msukkari marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (reviews.length > 0) { | ||
| file_diff_reviews.push({ | ||
| filename: file_diff.to, | ||
| reviews: reviews, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| console.log("Completed generate_pr_reviews"); | ||
| return file_diff_reviews; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.