# ToolValidator 的检查方法
class ToolValidator:
# ... (初始化如上)
def _validate_schema(self, params: dict, schema: dict) -> Tuple[bool, list, dict]:
"""检查必填字段和字段类型"""
warnings, corrections = [], {}
properties = schema.get('properties', {})
required = schema.get('required', [])
for field in required:
if field not in params:
warnings.append(f"Missing required field: {field}")
for field, value in params.items():
if field not in properties:
warnings.append(f"Unknown field: {field}")
continue
expected_type = properties[field].get('type')
if not self._check_type(value, expected_type):
warnings.append(f"Field '{field}': expected {expected_type}")
corrected = self._try_convert_type(value, expected_type)
if corrected is not None:
corrections[field] = f"Auto-converted to {expected_type}"
return len(warnings) == 0, warnings, corrections
def _validate_semantics(self, params: dict, schema: dict, tool_name: str) -> Tuple[bool, list, dict]:
"""检查 URL、日期、数值范围的有效性"""
warnings, corrections = [], {}
if 'url' in params and not self._is_valid_url(params['url']):
warnings.append(f"Invalid URL format: {params['url']}")
if 'date' in params and not self._is_valid_date(params['date']):
warnings.append(f"Invalid date format: {params['date']}")
for field in ['limit', 'page', 'score']:
if field in params and isinstance(params[field], (int, float)):
if field == 'limit' and not (1 <= params[field] <= 1000):
warnings.append(f"{field} out of range: {params[field]}")
corrections[field] = "Clamped to valid range"
return len(warnings) == 0, warnings, corrections
def _validate_context(self, params: dict, context: dict, tool_name: str) -> Tuple[bool, list]:
"""检查权限和操作冲突"""
warnings = []
if tool_name == 'delete_file' and context.get('user_role') == 'viewer':
warnings.append("User may not have permission to delete files")
if 'previous_query' in context and params.get('query') == context['previous_query']:
warnings.append("Query is identical to previous one")
return len(warnings) == 0, warnings
# ... (省略辅助方法)