- Renamed `check_environment` to `check_api_key_configured` for clarity, simplifying the API key validation logic. - Removed the blocking behavior of the API key check during application startup, allowing the app to run while providing a prompt for configuration. - Updated `LocalAgentApp` to accept an `api_configured` parameter, enabling conditional messaging for API key setup. - Enhanced the `SandboxRunner` to support backup management and improved execution result handling with detailed metrics. - Integrated data governance strategies into the `HistoryManager`, ensuring compliance and improved data management. - Added privacy settings and metrics tracking across various components to enhance user experience and application safety.
107 lines
3.5 KiB
Python
107 lines
3.5 KiB
Python
"""
|
|
需求分析异常分级系统
|
|
用于区分不同类型的需求分析失败,并采取相应的处理策略
|
|
"""
|
|
|
|
|
|
class RequirementAnalysisException(Exception):
|
|
"""需求分析异常基类"""
|
|
|
|
def __init__(self, message: str, severity: str = "medium"):
|
|
"""
|
|
Args:
|
|
message: 异常描述
|
|
severity: 严重程度 (critical/high/medium/low)
|
|
"""
|
|
super().__init__(message)
|
|
self.severity = severity
|
|
|
|
|
|
class CriticalInfoMissingException(RequirementAnalysisException):
|
|
"""关键信息缺失异常 - 必须澄清才能继续"""
|
|
|
|
def __init__(self, message: str, missing_fields: list = None):
|
|
super().__init__(message, severity="critical")
|
|
self.missing_fields = missing_fields or []
|
|
|
|
|
|
class AmbiguousRequirementException(RequirementAnalysisException):
|
|
"""需求歧义异常 - 建议澄清"""
|
|
|
|
def __init__(self, message: str, ambiguous_parts: list = None):
|
|
super().__init__(message, severity="high")
|
|
self.ambiguous_parts = ambiguous_parts or []
|
|
|
|
|
|
class LowConfidenceException(RequirementAnalysisException):
|
|
"""低置信度异常 - 可以继续但建议澄清"""
|
|
|
|
def __init__(self, message: str, confidence: float = 0.0):
|
|
super().__init__(message, severity="medium")
|
|
self.confidence = confidence
|
|
|
|
|
|
class CheckerFailureException(RequirementAnalysisException):
|
|
"""检查器本身失败异常 - 可以降级处理"""
|
|
|
|
def __init__(self, message: str, original_error: Exception = None):
|
|
super().__init__(message, severity="low")
|
|
self.original_error = original_error
|
|
|
|
|
|
def classify_requirement_error(result: dict = None, error: Exception = None) -> RequirementAnalysisException:
|
|
"""
|
|
根据检查结果或错误对象,分类异常类型
|
|
|
|
Args:
|
|
result: 需求完整性检查结果
|
|
error: 原始异常对象
|
|
|
|
Returns:
|
|
分类后的异常对象
|
|
"""
|
|
# 如果是检查器本身失败
|
|
if error is not None:
|
|
return CheckerFailureException(
|
|
f"需求完整性检查器失败: {str(error)}",
|
|
original_error=error
|
|
)
|
|
|
|
# 如果没有结果,视为检查器失败
|
|
if result is None:
|
|
return CheckerFailureException("需求完整性检查返回空结果")
|
|
|
|
is_complete = result.get('is_complete', True)
|
|
confidence = result.get('confidence', 1.0)
|
|
reason = result.get('reason', '未知原因')
|
|
|
|
# 明确标记为不完整
|
|
if not is_complete:
|
|
# 检查是否有关键信息缺失标记
|
|
missing_info = result.get('missing_info', [])
|
|
critical_fields = result.get('critical_fields', [])
|
|
|
|
if critical_fields or len(missing_info) > 2:
|
|
# 关键信息缺失
|
|
return CriticalInfoMissingException(
|
|
f"关键信息缺失: {reason}",
|
|
missing_fields=critical_fields or missing_info
|
|
)
|
|
else:
|
|
# 一般歧义
|
|
return AmbiguousRequirementException(
|
|
f"需求存在歧义: {reason}",
|
|
ambiguous_parts=missing_info
|
|
)
|
|
|
|
# 标记为完整但置信度低
|
|
if is_complete and confidence < 0.7:
|
|
return LowConfidenceException(
|
|
f"需求置信度较低 ({confidence:.1%}): {reason}",
|
|
confidence=confidence
|
|
)
|
|
|
|
# 其他情况视为检查器问题
|
|
return CheckerFailureException(f"需求检查结果异常: {reason}")
|
|
|