Merge pull request #1687 from benedict-lee/feat/add-conversation-history-on-line-question

Improvement: Enhance ask_line tool by adding PR review comment threads as context
This commit is contained in:
ofir-frd
2025-04-24 09:32:43 +03:00
committed by GitHub
5 changed files with 108 additions and 2 deletions

View File

@ -14,10 +14,10 @@ from pr_agent.algo.utils import ModelType
from pr_agent.config_loader import get_settings
from pr_agent.git_providers import get_git_provider
from pr_agent.git_providers.git_provider import get_main_pr_language
from pr_agent.git_providers.github_provider import GithubProvider
from pr_agent.log import get_logger
from pr_agent.servers.help import HelpMessage
class PR_LineQuestions:
def __init__(self, pr_url: str, args=None, ai_handler: partial[BaseAiHandler,] = LiteLLMAIHandler):
self.question_str = self.parse_args(args)
@ -35,6 +35,7 @@ class PR_LineQuestions:
"question": self.question_str,
"full_hunk": "",
"selected_lines": "",
"conversation_history": "",
}
self.token_handler = TokenHandler(self.git_provider.pr,
self.vars,
@ -56,6 +57,12 @@ class PR_LineQuestions:
# if get_settings().config.publish_output:
# self.git_provider.publish_comment("Preparing answer...", is_temporary=True)
# set conversation history if enabled
# currently only supports GitHub provider
if get_settings().pr_questions.use_conversation_history and isinstance(self.git_provider, GithubProvider):
conversation_history = self._load_conversation_history()
self.vars["conversation_history"] = conversation_history
self.patch_with_lines = ""
ask_diff = get_settings().get('ask_diff_hunk', "")
line_start = get_settings().get('line_start', '')
@ -92,6 +99,54 @@ class PR_LineQuestions:
self.git_provider.publish_comment(model_answer_sanitized)
return ""
def _load_conversation_history(self) -> str:
"""Generate conversation history from the code review thread
Returns:
str: The formatted conversation history
"""
comment_id = get_settings().get('comment_id', '')
file_path = get_settings().get('file_name', '')
line_number = get_settings().get('line_end', '')
# early return if any required parameter is missing
if not all([comment_id, file_path, line_number]):
get_logger().error("Missing required parameters for conversation history")
return ""
try:
# retrieve thread comments
thread_comments = self.git_provider.get_review_thread_comments(comment_id)
# filter and prepare comments
filtered_comments = []
for comment in thread_comments:
body = getattr(comment, 'body', '')
# skip empty comments, current comment(will be added as a question at prompt)
if not body or not body.strip() or comment_id == comment.id:
continue
user = comment.user
author = user.login if hasattr(user, 'login') else 'Unknown'
filtered_comments.append((author, body))
# transform conversation history to string using the same pattern as get_commit_messages
if filtered_comments:
comment_count = len(filtered_comments)
get_logger().info(f"Loaded {comment_count} comments from the code review thread")
# Format as numbered list, similar to get_commit_messages
conversation_history_str = "\n".join([f"{i + 1}. {author}: {body}"
for i, (author, body) in enumerate(filtered_comments)])
return conversation_history_str
return ""
except Exception as e:
get_logger().error(f"Error processing conversation history, error: {e}")
return ""
async def _get_prediction(self, model: str):
variables = copy.deepcopy(self.vars)