Skip to content
This repository was archived by the owner on Mar 9, 2026. It is now read-only.

Commit 0a9102a

Browse files
committed
New command: review that will do a code review for [un]staged changes or a commit
1 parent d7e0197 commit 0a9102a

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

aicodebot/cli.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from rich.style import Style
1111
import click, datetime, openai, os, random, subprocess, sys, tempfile, webbrowser
1212

13-
# Create a Console object
1413
console = Console()
1514
bot_style = Style(color="#30D5C8")
1615
DEFAULT_MAX_TOKENS = 1024
@@ -212,6 +211,45 @@ def fun_fact(verbose):
212211
console.print(response, style=bot_style)
213212

214213

214+
@cli.command
215+
@click.option("--commit", "-c", help="The commit hash to review.")
216+
@click.option("--verbose", "-v")
217+
def review(commit, verbose):
218+
"""Use AI to do a code review, with [un]staged changes, or a specified commit."""
219+
setup_environment()
220+
221+
if commit:
222+
# If a commit hash is specified, get the diff for that commit
223+
diff = exec_and_get_output(["git", "show", commit])
224+
else:
225+
# If no commit hash is specified, get the diff for changes, staged or not
226+
staged_files = exec_and_get_output(["git", "diff", "--name-only", "--cached"])
227+
base_git_diff = ["git", "diff", "-U10"] # Tell diff to provide 10 lines of context
228+
if not staged_files:
229+
# Get the diff for all changes since the last commit
230+
diff = exec_and_get_output(base_git_diff + ["HEAD"])
231+
else:
232+
# If some files are staged, get the diff for those files
233+
diff = exec_and_get_output(base_git_diff + ["--cached"])
234+
235+
if not diff:
236+
console.print("No changes to commit.")
237+
sys.exit(0)
238+
239+
# Load the prompt
240+
prompt = load_prompt(Path(__file__).parent / "prompts" / "review.yaml")
241+
242+
# Set up the language model
243+
llm = OpenAI(temperature=0.1, max_tokens=DEFAULT_MAX_TOKENS)
244+
245+
# Set up the chain
246+
chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose)
247+
248+
with console.status("Reviewing", spinner="point"):
249+
response = chain.run(diff)
250+
console.print(response, style=bot_style)
251+
252+
215253
@cli.command()
216254
def version():
217255
"""Print the version number."""

aicodebot/prompts/review.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
_type: prompt
2+
template_format: f-string
3+
input_variables: ["diff"]
4+
template: |
5+
You are an expert code reviewer.
6+
You know how to give constructive feedback.
7+
You know how to give feedback that is actionable.
8+
You know how to give feedback that is kind.
9+
You know how to give feedback that is specific.
10+
11+
Review this code change:
12+
13+
BEGIN DIFF
14+
{diff}
15+
END DIFF
16+
17+
If the changes look good and don't require any feedback, then just respond with "LGTM" (looks good to me).

0 commit comments

Comments
 (0)