|
|
@@ -195,8 +195,8 @@ class AIReviewWorkflow:
|
|
|
}
|
|
|
|
|
|
logger.info(f"保存审查结果")
|
|
|
- with open('temp/AI审查结果.json', "w",encoding='utf-8') as f:
|
|
|
- json.dump(result, f, ensure_ascii=False, indent=2, default=str)
|
|
|
+ # with open('temp/AI审查结果.json', "w",encoding='utf-8') as f:
|
|
|
+ # json.dump(result, f, ensure_ascii=False, indent=2, default=str)
|
|
|
|
|
|
return review_results
|
|
|
|
|
|
@@ -850,26 +850,69 @@ class AIReviewCoreFun:
|
|
|
|
|
|
# 如果直接解析失败,查找JSON代码块
|
|
|
if json_data is None:
|
|
|
- json_pattern = r'```json\s*(.*?)\s*```'
|
|
|
- json_matches = re.findall(json_pattern, response, re.DOTALL)
|
|
|
+ # 改进的正则表达式:正确处理多行JSON和转义字符
|
|
|
+ json_pattern = r'```json\s*(\[[\s\S]*?\]|\{[\s\S]*?\})\s*```'
|
|
|
+ json_matches = re.findall(json_pattern, response)
|
|
|
+
|
|
|
+ # 如果改进的正则仍然匹配不到,使用更宽松的模式
|
|
|
+ if not json_matches:
|
|
|
+ json_pattern = r'```json\s*(.*?)\s*```'
|
|
|
+ json_matches = re.findall(json_pattern, response, re.DOTALL)
|
|
|
+
|
|
|
else:
|
|
|
json_matches = [response_stripped] # 使用整个响应作为JSON
|
|
|
|
|
|
+ # 如果所有方法都失败,尝试最后的JSON提取策略
|
|
|
+ if not json_matches:
|
|
|
+ logger.warning("标准JSON提取失败,尝试智能JSON提取策略")
|
|
|
+
|
|
|
+ # 策略1: 查找被JSON数组/对象包围的内容
|
|
|
+ array_pattern = r'\[\s*\{.*?\}\s*\]'
|
|
|
+ object_pattern = r'\{[^{}]*"issue_point"[^{}]*\}'
|
|
|
+
|
|
|
+ array_matches = re.findall(array_pattern, response, re.DOTALL)
|
|
|
+ if array_matches:
|
|
|
+ json_matches = array_matches
|
|
|
+ logger.info(f"数组模式匹配成功,找到 {len(array_matches)} 个JSON数组")
|
|
|
+ else:
|
|
|
+ object_matches = re.findall(object_pattern, response, re.DOTALL)
|
|
|
+ if object_matches:
|
|
|
+ json_matches = object_matches
|
|
|
+ logger.info(f"对象模式匹配成功,找到 {len(object_matches)} 个JSON对象")
|
|
|
+
|
|
|
+ # 最后的备选方案:查找包含关键JSON字段的内容
|
|
|
+ if not json_matches:
|
|
|
+ logger.warning("所有JSON提取策略失败,查找包含关键字段的内容")
|
|
|
+ key_pattern = r'[^`]*["\']issue_point["\'][^`]*["\']location["\'][^`]*'
|
|
|
+ key_matches = re.findall(key_pattern, response, re.DOTALL)
|
|
|
+ if key_matches:
|
|
|
+ # 尝试将匹配内容包装为有效JSON
|
|
|
+ for match in key_matches:
|
|
|
+ # 简单包装,可能需要更复杂的逻辑
|
|
|
+ wrapped_match = f'[{match}]' if not match.strip().startswith('[') else match
|
|
|
+ json_matches.append(wrapped_match)
|
|
|
+ logger.info(f"关键字段模式匹配成功,生成 {len(json_matches)} 个JSON候选")
|
|
|
+
|
|
|
if json_matches:
|
|
|
+ logger.info(f"成功匹配到 {len(json_matches)} 个JSON代码块")
|
|
|
# 解析找到的JSON
|
|
|
- for json_str in json_matches:
|
|
|
+ for i, json_str in enumerate(json_matches):
|
|
|
try:
|
|
|
# 如果是直接解析的JSON,不需要重新解析
|
|
|
if json_str == response_stripped and json_data is not None:
|
|
|
issue_data = json_data
|
|
|
+ logger.debug(f"使用直接解析的JSON数据 (第{i+1}个)")
|
|
|
else:
|
|
|
# 清理JSON字符串
|
|
|
json_str = json_str.strip()
|
|
|
if not json_str:
|
|
|
+ logger.warning(f"第{i+1}个JSON代码块为空,跳过")
|
|
|
continue
|
|
|
|
|
|
+ logger.debug(f"尝试解析第{i+1}个JSON: {json_str[:100]}...")
|
|
|
# 解析JSON
|
|
|
issue_data = json.loads(json_str)
|
|
|
+ logger.info(f"第{i+1}个JSON解析成功,包含 {len(issue_data) if isinstance(issue_data, (list, dict)) else 0} 个元素")
|
|
|
|
|
|
risk_level = issue_data.get("risk_level", "")
|
|
|
|