feat(tool system): Tool system reengineering

This commit is contained in:
谢俊男
2025-12-25 17:30:20 +08:00
parent 3bcaead413
commit 04be3088a2
25 changed files with 1887 additions and 3325 deletions

View File

@@ -2,7 +2,6 @@
import base64
import hashlib
import hmac
import time
from typing import Dict, Any, Tuple
from urllib.parse import quote
import aiohttp
@@ -51,8 +50,9 @@ class AuthManager:
except Exception as e:
return False, f"验证认证配置时出错: {e}"
def _validate_api_key_config(self, auth_config: Dict[str, Any]) -> Tuple[bool, str]:
@staticmethod
def _validate_api_key_config(auth_config: Dict[str, Any]) -> Tuple[bool, str]:
"""验证API Key认证配置
Args:
@@ -79,8 +79,9 @@ class AuthManager:
return False, "API Key位置必须是 header、query 或 cookie"
return True, ""
def _validate_bearer_token_config(self, auth_config: Dict[str, Any]) -> Tuple[bool, str]:
@staticmethod
def _validate_bearer_token_config(auth_config: Dict[str, Any]) -> Tuple[bool, str]:
"""验证Bearer Token认证配置
Args:
@@ -135,9 +136,9 @@ class AuthManager:
except Exception as e:
logger.error(f"应用认证时出错: {e}")
return url, headers, params
@staticmethod
def _apply_api_key_auth(
self,
auth_config: Dict[str, Any],
url: str,
headers: Dict[str, str],
@@ -176,9 +177,9 @@ class AuthManager:
headers["Cookie"] = cookie_value
return url, headers, params
@staticmethod
def _apply_bearer_token_auth(
self,
auth_config: Dict[str, Any],
url: str,
headers: Dict[str, str],
@@ -260,8 +261,9 @@ class AuthManager:
except Exception as e:
logger.error(f"解密认证配置失败: {e}")
return encrypted_config
def _encrypt_string(self, value: str, key: str) -> str:
@staticmethod
def _encrypt_string(value: str, key: str) -> str:
"""加密字符串
Args:
@@ -289,8 +291,9 @@ class AuthManager:
except Exception as e:
logger.error(f"加密字符串失败: {e}")
return value
def _decrypt_string(self, encrypted_value: str, key: str) -> str:
@staticmethod
def _decrypt_string(encrypted_value: str, key: str) -> str:
"""解密字符串
Args:
@@ -471,8 +474,9 @@ class AuthManager:
"error": f"测试认证时出错: {e}",
"auth_type": auth_type.value
}
def get_auth_config_template(self, auth_type: AuthType) -> Dict[str, Any]:
@staticmethod
def get_auth_config_template(auth_type: AuthType) -> Dict[str, Any]:
"""获取认证配置模板
Args:
@@ -498,8 +502,9 @@ class AuthManager:
}
return templates.get(auth_type, {})
def mask_sensitive_config(self, auth_config: Dict[str, Any]) -> Dict[str, Any]:
@staticmethod
def mask_sensitive_config(auth_config: Dict[str, Any]) -> Dict[str, Any]:
"""遮蔽认证配置中的敏感信息
Args:

View File

@@ -5,7 +5,8 @@ import aiohttp
from urllib.parse import urljoin
from app.models.tool_model import ToolType, AuthType
from app.core.tools.base import BaseTool, ToolParameter, ToolResult, ParameterType
from app.core.tools.base import BaseTool
from app.schemas.tool_schema import ToolParameter, ToolResult, ParameterType
from app.core.logging_config import get_business_logger
logger = get_business_logger()
@@ -173,8 +174,9 @@ class CustomTool(BaseTool):
}
return operations
def _convert_openapi_type(self, openapi_type: str) -> ParameterType:
@staticmethod
def _convert_openapi_type(openapi_type: str) -> ParameterType:
"""转换OpenAPI类型到内部类型"""
type_mapping = {
"string": ParameterType.STRING,
@@ -239,8 +241,9 @@ class CustomTool(BaseTool):
headers["Authorization"] = f"Bearer {token}"
return headers
def _build_request_data(self, operation: Dict[str, Any], params: Dict[str, Any]) -> Optional[Dict[str, Any]]:
@staticmethod
def _build_request_data(operation: Dict[str, Any], params: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""构建请求数据"""
if operation["method"] in ["POST", "PUT", "PATCH"]:
request_body = operation.get("request_body")
@@ -284,6 +287,7 @@ class CustomTool(BaseTool):
try:
return await response.json()
except Exception as e:
logger.error(f"解析HTTP响应JSON失败: {str(e)}")
return await response.text()
@classmethod

View File

@@ -10,6 +10,9 @@ from app.core.logging_config import get_business_logger
logger = get_business_logger()
# 为了兼容性,创建别名
# SchemaParser = OpenAPISchemaParser = None
class OpenAPISchemaParser:
"""OpenAPI Schema解析器 - 解析OpenAPI 3.0规范"""
@@ -88,8 +91,9 @@ class OpenAPISchemaParser:
except Exception as e:
logger.error(f"解析schema内容失败: {e}")
return False, {}, str(e)
def _parse_content(self, content: str, content_type: str) -> Optional[Dict[str, Any]]:
@staticmethod
def _parse_content(content: str, content_type: str) -> Optional[Dict[str, Any]]:
"""解析内容为字典
Args:
@@ -101,7 +105,7 @@ class OpenAPISchemaParser:
"""
try:
# 根据内容类型解析
if 'json' in content_type:
if 'application/json' in content_type:
return json.loads(content)
elif 'yaml' in content_type or 'yml' in content_type:
return yaml.safe_load(content)
@@ -228,8 +232,9 @@ class OpenAPISchemaParser:
}
return operations
def _extract_parameters(self, operation: Dict[str, Any]) -> Dict[str, Any]:
@staticmethod
def _extract_parameters(operation: Dict[str, Any]) -> Dict[str, Any]:
"""提取操作参数
Args:
@@ -266,8 +271,9 @@ class OpenAPISchemaParser:
}
return parameters
def _extract_request_body(self, operation: Dict[str, Any]) -> Optional[Dict[str, Any]]:
@staticmethod
def _extract_request_body(operation: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""提取请求体信息
Args:
@@ -298,8 +304,9 @@ class OpenAPISchemaParser:
"schema": schema,
"content_types": list(content.keys())
}
def _extract_responses(self, operation: Dict[str, Any]) -> Dict[str, Any]:
@staticmethod
def _extract_responses(operation: Dict[str, Any]) -> Dict[str, Any]:
"""提取响应信息
Args:
@@ -331,8 +338,9 @@ class OpenAPISchemaParser:
}
return responses
def generate_tool_parameters(self, operations: Dict[str, Any]) -> List[Dict[str, Any]]:
@staticmethod
def generate_tool_parameters(operations: Dict[str, Any]) -> List[Dict[str, Any]]:
"""生成工具参数定义
Args:
@@ -396,7 +404,7 @@ class OpenAPISchemaParser:
parameters.extend(all_params.values())
return parameters
def validate_operation_parameters(self, operation: Dict[str, Any], params: Dict[str, Any]) -> Tuple[bool, List[str]]:
"""验证操作参数
@@ -447,8 +455,9 @@ class OpenAPISchemaParser:
errors.append(f"请求体参数 {prop_name} 类型错误,期望: {prop_type}")
return len(errors) == 0, errors
def _validate_parameter_type(self, value: Any, expected_type: str) -> bool:
@staticmethod
def _validate_parameter_type(value: Any, expected_type: str) -> bool:
"""验证参数类型
Args:
@@ -474,4 +483,7 @@ class OpenAPISchemaParser:
if expected_python_type:
return isinstance(value, expected_python_type)
return True
return True
# 为了兼容性,创建别名
SchemaParser = OpenAPISchemaParser