Spaces:
Sleeping
Sleeping
Sgridda
commited on
Commit
·
8eb5cff
1
Parent(s):
4eca884
udpated message response
Browse files
main.py
CHANGED
|
@@ -89,11 +89,11 @@ def run_ai_inference(diff: str) -> str:
|
|
| 89 |
messages = [
|
| 90 |
{
|
| 91 |
"role": "system",
|
| 92 |
-
"content": "You are a code reviewer. Analyze the diff and
|
| 93 |
},
|
| 94 |
{
|
| 95 |
"role": "user",
|
| 96 |
-
"content": f"
|
| 97 |
}
|
| 98 |
]
|
| 99 |
|
|
@@ -120,22 +120,48 @@ def parse_ai_response(response_text: str) -> list[ReviewComment]:
|
|
| 120 |
"""
|
| 121 |
print(f"Raw AI Response:\n---\n{response_text}\n---")
|
| 122 |
|
|
|
|
| 123 |
json_match = re.search(r'\[.*?\]', response_text, re.DOTALL)
|
| 124 |
if not json_match:
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
try:
|
| 136 |
comments_data = json.loads(json_string)
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
except (json.JSONDecodeError, TypeError, KeyError) as e:
|
| 140 |
print(f"Error parsing JSON from AI response: {e}")
|
| 141 |
print(f"Invalid JSON string: {json_string}")
|
|
|
|
| 89 |
messages = [
|
| 90 |
{
|
| 91 |
"role": "system",
|
| 92 |
+
"content": "You are a code reviewer. Analyze the diff and respond with ONLY a JSON array. No other text, no markdown, no explanations. Just the JSON array with file_path, line_number, and comment_text fields."
|
| 93 |
},
|
| 94 |
{
|
| 95 |
"role": "user",
|
| 96 |
+
"content": f"Analyze this specific diff and provide review comments:\n{diff[:800]}" # Slightly reduced for faster processing
|
| 97 |
}
|
| 98 |
]
|
| 99 |
|
|
|
|
| 120 |
"""
|
| 121 |
print(f"Raw AI Response:\n---\n{response_text}\n---")
|
| 122 |
|
| 123 |
+
# Try to find JSON array, handling both direct JSON and markdown-wrapped JSON
|
| 124 |
json_match = re.search(r'\[.*?\]', response_text, re.DOTALL)
|
| 125 |
if not json_match:
|
| 126 |
+
# Try to find JSON inside markdown code blocks
|
| 127 |
+
markdown_match = re.search(r'```json\s*(\[.*?\])\s*```', response_text, re.DOTALL)
|
| 128 |
+
if markdown_match:
|
| 129 |
+
json_match = markdown_match
|
| 130 |
+
json_string = markdown_match.group(1)
|
| 131 |
+
else:
|
| 132 |
+
print("Warning: Could not find a JSON array in the AI response.")
|
| 133 |
+
# Return a simple fallback comment
|
| 134 |
+
return [ReviewComment(
|
| 135 |
+
file_path="unknown",
|
| 136 |
+
line_number=1,
|
| 137 |
+
comment_text="AI review completed - no specific issues found."
|
| 138 |
+
)]
|
| 139 |
+
else:
|
| 140 |
+
json_string = json_match.group(0)
|
| 141 |
|
| 142 |
try:
|
| 143 |
comments_data = json.loads(json_string)
|
| 144 |
+
if not isinstance(comments_data, list):
|
| 145 |
+
print("Warning: AI response is not a JSON array")
|
| 146 |
+
return [ReviewComment(
|
| 147 |
+
file_path="unknown",
|
| 148 |
+
line_number=1,
|
| 149 |
+
comment_text="AI review completed - format issue."
|
| 150 |
+
)]
|
| 151 |
+
|
| 152 |
+
validated_comments = []
|
| 153 |
+
for item in comments_data:
|
| 154 |
+
try:
|
| 155 |
+
validated_comments.append(ReviewComment(**item))
|
| 156 |
+
except (TypeError, ValueError) as e:
|
| 157 |
+
print(f"Skipping invalid comment: {item}, error: {e}")
|
| 158 |
+
|
| 159 |
+
return validated_comments if validated_comments else [ReviewComment(
|
| 160 |
+
file_path="unknown",
|
| 161 |
+
line_number=1,
|
| 162 |
+
comment_text="AI review completed - no valid comments found."
|
| 163 |
+
)]
|
| 164 |
+
|
| 165 |
except (json.JSONDecodeError, TypeError, KeyError) as e:
|
| 166 |
print(f"Error parsing JSON from AI response: {e}")
|
| 167 |
print(f"Invalid JSON string: {json_string}")
|