diff --git a/pr_agent/tools/pr_code_suggestions.py b/pr_agent/tools/pr_code_suggestions.py index 777ffd99..d08c723b 100644 --- a/pr_agent/tools/pr_code_suggestions.py +++ b/pr_agent/tools/pr_code_suggestions.py @@ -35,7 +35,6 @@ class PRCodeSuggestions: get_logger().info(f"Setting max_model_tokens to {MAX_CONTEXT_TOKENS_IMPROVE} for PR improve") get_settings().config.max_model_tokens = MAX_CONTEXT_TOKENS_IMPROVE - # extended mode try: self.is_extended = self._get_is_extended(args or []) @@ -116,7 +115,7 @@ class PRCodeSuggestions: # require self-review if get_settings().pr_code_suggestions.demand_code_suggestions_self_review: - text= get_settings().pr_code_suggestions.code_suggestions_self_review_text + text = get_settings().pr_code_suggestions.code_suggestions_self_review_text pr_body += f"\n\n- [ ] {text}" if get_settings().pr_code_suggestions.approve_pr_on_self_review: pr_body += ' ' @@ -193,8 +192,9 @@ class PRCodeSuggestions: # self-reflect on suggestions if get_settings().pr_code_suggestions.self_reflect_on_suggestions: - model = get_settings().config.model_turbo # use turbo model for self-reflection, since it is an easier task - response_reflect = await self.self_reflect_on_suggestions(data["code_suggestions"], patches_diff, model=model) + model = get_settings().config.model_turbo # use turbo model for self-reflection, since it is an easier task + response_reflect = await self.self_reflect_on_suggestions(data["code_suggestions"], patches_diff, + model=model) if response_reflect: response_reflect_yaml = load_yaml(response_reflect) code_suggestions_feedback = response_reflect_yaml["code_suggestions"] @@ -203,7 +203,7 @@ class PRCodeSuggestions: try: suggestion["score"] = code_suggestions_feedback[i]["suggestion_score"] suggestion["score_why"] = code_suggestions_feedback[i]["why"] - except Exception as e: # + except Exception as e: # get_logger().error(f"Error processing suggestion score {i}", artifact={"suggestion": suggestion, "code_suggestions_feedback": code_suggestions_feedback[i]}) @@ -226,7 +226,7 @@ class PRCodeSuggestions: suggestion['improved_code'] = suggestion['improved_code'][:max_code_suggestion_length] suggestion['improved_code'] += f"\n{suggestion_truncation_message}" get_logger().info(f"Truncated suggestion from {len(suggestion['improved_code'])} " - f"characters to {max_code_suggestion_length} characters") + f"characters to {max_code_suggestion_length} characters") return suggestion def _prepare_pr_code_suggestions(self, predictions: str) -> Dict: @@ -240,17 +240,24 @@ class PRCodeSuggestions: one_sentence_summary_list = [] for i, suggestion in enumerate(data['code_suggestions']): try: - if (not suggestion or 'one_sentence_summary' not in suggestion or - 'label' not in suggestion or 'relevant_file' not in suggestion): - get_logger().debug(f"Skipping suggestion {i + 1}, because it is invalid: {suggestion}") + needed_keys = ['one_sentence_summary', 'label', 'relevant_file', 'relevant_lines_start', 'relevant_lines_end'] + is_valid_keys = True + for key in needed_keys: + if key not in suggestion: + is_valid_keys = False + get_logger().debug(f"Skipping suggestion {i + 1}, because it does not contain '{key}':\n'{suggestion}") + break + if not is_valid_keys: continue if suggestion['one_sentence_summary'] in one_sentence_summary_list: get_logger().debug(f"Skipping suggestion {i + 1}, because it is a duplicate: {suggestion}") continue - if 'const' in suggestion['suggestion_content'] and 'instead' in suggestion['suggestion_content'] and 'let' in suggestion['suggestion_content']: - get_logger().debug(f"Skipping suggestion {i + 1}, because it uses 'const instead let': {suggestion}") + if 'const' in suggestion['suggestion_content'] and 'instead' in suggestion[ + 'suggestion_content'] and 'let' in suggestion['suggestion_content']: + get_logger().debug( + f"Skipping suggestion {i + 1}, because it uses 'const instead let': {suggestion}") continue if ('existing_code' in suggestion) and ('improved_code' in suggestion): @@ -258,7 +265,7 @@ class PRCodeSuggestions: get_logger().debug( f"edited improved suggestion {i + 1}, because equal to existing code: {suggestion['existing_code']}") if get_settings().pr_code_suggestions.commitable_code_suggestions: - suggestion['improved_code'] = "" # we need 'existing_code' to locate the code in the PR + suggestion['improved_code'] = "" # we need 'existing_code' to locate the code in the PR else: suggestion['existing_code'] = "" suggestion = self._truncate_if_needed(suggestion) @@ -279,12 +286,15 @@ class PRCodeSuggestions: if not data['code_suggestions']: get_logger().info('No suggestions found to improve this PR.') if self.progress_response: - return self.git_provider.edit_comment(self.progress_response, body='No suggestions found to improve this PR.') + return self.git_provider.edit_comment(self.progress_response, + body='No suggestions found to improve this PR.') else: return self.git_provider.publish_comment('No suggestions found to improve this PR.') for d in data['code_suggestions']: try: + if get_settings().config.verbosity_level >= 2: + get_logger().info(f"suggestion: {d}") relevant_file = d['relevant_file'].strip() relevant_lines_start = int(d['relevant_lines_start']) # absolute position relevant_lines_end = int(d['relevant_lines_end']) @@ -300,8 +310,8 @@ class PRCodeSuggestions: else: body = f"**Suggestion:** {content} [{label}]\n```suggestion\n" + new_code_snippet + "\n```" code_suggestions.append({'body': body, 'relevant_file': relevant_file, - 'relevant_lines_start': relevant_lines_start, - 'relevant_lines_end': relevant_lines_end}) + 'relevant_lines_start': relevant_lines_start, + 'relevant_lines_end': relevant_lines_end}) except Exception: get_logger().info(f"Could not parse suggestion: {d}") @@ -477,14 +487,15 @@ class PRCodeSuggestions: # sort suggestions_labels by the suggestion with the highest score if get_settings().pr_code_suggestions.self_reflect_on_suggestions: - suggestions_labels = dict(sorted(suggestions_labels.items(), key=lambda x: max([s['score'] for s in x[1]]), reverse=True)) + suggestions_labels = dict( + sorted(suggestions_labels.items(), key=lambda x: max([s['score'] for s in x[1]]), reverse=True)) # sort the suggestions inside each label group by score for label, suggestions in suggestions_labels.items(): suggestions_labels[label] = sorted(suggestions, key=lambda x: x['score'], reverse=True) - + counter_suggestions = 0 for label, suggestions in suggestions_labels.items(): - num_suggestions=len(suggestions) + num_suggestions = len(suggestions) pr_body += f"""