2024-02-01 18:11:57 +08:00
|
|
|
import json
|
2024-01-23 19:58:23 +08:00
|
|
|
from json import dumps
|
2024-02-09 15:21:33 +08:00
|
|
|
from typing import Any, Union
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-02-01 18:11:57 +08:00
|
|
|
import httpx
|
|
|
|
import requests
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2024-02-05 18:48:30 +08:00
|
|
|
import core.helper.ssrf_proxy as ssrf_proxy
|
2024-01-23 19:58:23 +08:00
|
|
|
from core.tools.entities.tool_bundle import ApiBasedToolBundle
|
|
|
|
from core.tools.entities.tool_entities import ToolInvokeMessage
|
|
|
|
from core.tools.errors import ToolProviderCredentialValidationError
|
2024-02-01 18:11:57 +08:00
|
|
|
from core.tools.tool.tool import Tool
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ApiTool(Tool):
|
|
|
|
api_bundle: ApiBasedToolBundle
|
|
|
|
|
|
|
|
"""
|
|
|
|
Api tool
|
|
|
|
"""
|
2024-02-09 15:21:33 +08:00
|
|
|
def fork_tool_runtime(self, meta: dict[str, Any]) -> 'Tool':
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
fork a new tool with meta data
|
|
|
|
|
|
|
|
:param meta: the meta data of a tool call processing, tenant_id is required
|
|
|
|
:return: the new tool
|
|
|
|
"""
|
|
|
|
return self.__class__(
|
|
|
|
identity=self.identity.copy() if self.identity else None,
|
|
|
|
parameters=self.parameters.copy() if self.parameters else None,
|
|
|
|
description=self.description.copy() if self.description else None,
|
|
|
|
api_bundle=self.api_bundle.copy() if self.api_bundle else None,
|
|
|
|
runtime=Tool.Runtime(**meta)
|
|
|
|
)
|
|
|
|
|
2024-02-09 15:21:33 +08:00
|
|
|
def validate_credentials(self, credentials: dict[str, Any], parameters: dict[str, Any], format_only: bool = False) -> str:
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
validate the credentials for Api tool
|
|
|
|
"""
|
|
|
|
# assemble validate request and request parameters
|
|
|
|
headers = self.assembling_request(parameters)
|
|
|
|
|
|
|
|
if format_only:
|
|
|
|
return
|
|
|
|
|
|
|
|
response = self.do_http_request(self.api_bundle.server_url, self.api_bundle.method, headers, parameters)
|
|
|
|
# validate response
|
2024-02-05 18:48:30 +08:00
|
|
|
return self.validate_and_parse_response(response)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
2024-02-09 15:21:33 +08:00
|
|
|
def assembling_request(self, parameters: dict[str, Any]) -> dict[str, Any]:
|
2024-01-23 19:58:23 +08:00
|
|
|
headers = {}
|
|
|
|
credentials = self.runtime.credentials or {}
|
|
|
|
|
|
|
|
if 'auth_type' not in credentials:
|
|
|
|
raise ToolProviderCredentialValidationError('Missing auth_type')
|
|
|
|
|
|
|
|
if credentials['auth_type'] == 'api_key':
|
|
|
|
api_key_header = 'api_key'
|
|
|
|
|
|
|
|
if 'api_key_header' in credentials:
|
|
|
|
api_key_header = credentials['api_key_header']
|
|
|
|
|
|
|
|
if 'api_key_value' not in credentials:
|
|
|
|
raise ToolProviderCredentialValidationError('Missing api_key_value')
|
|
|
|
|
|
|
|
headers[api_key_header] = credentials['api_key_value']
|
|
|
|
|
|
|
|
needed_parameters = [parameter for parameter in self.api_bundle.parameters if parameter.required]
|
|
|
|
for parameter in needed_parameters:
|
|
|
|
if parameter.required and parameter.name not in parameters:
|
|
|
|
raise ToolProviderCredentialValidationError(f"Missing required parameter {parameter.name}")
|
|
|
|
|
|
|
|
if parameter.default is not None and parameter.name not in parameters:
|
|
|
|
parameters[parameter.name] = parameter.default
|
|
|
|
|
|
|
|
return headers
|
|
|
|
|
|
|
|
def validate_and_parse_response(self, response: Union[httpx.Response, requests.Response]) -> str:
|
|
|
|
"""
|
|
|
|
validate the response
|
|
|
|
"""
|
|
|
|
if isinstance(response, httpx.Response):
|
|
|
|
if response.status_code >= 400:
|
|
|
|
raise ToolProviderCredentialValidationError(f"Request failed with status code {response.status_code}")
|
2024-01-30 22:22:58 +08:00
|
|
|
if not response.content:
|
|
|
|
return 'Empty response from the tool, please check your parameters and try again.'
|
|
|
|
try:
|
|
|
|
response = response.json()
|
|
|
|
try:
|
|
|
|
return json.dumps(response, ensure_ascii=False)
|
|
|
|
except Exception as e:
|
|
|
|
return json.dumps(response)
|
|
|
|
except Exception as e:
|
|
|
|
return response.text
|
2024-01-23 19:58:23 +08:00
|
|
|
elif isinstance(response, requests.Response):
|
|
|
|
if not response.ok:
|
|
|
|
raise ToolProviderCredentialValidationError(f"Request failed with status code {response.status_code}")
|
2024-01-30 22:22:58 +08:00
|
|
|
if not response.content:
|
|
|
|
return 'Empty response from the tool, please check your parameters and try again.'
|
|
|
|
try:
|
|
|
|
response = response.json()
|
|
|
|
try:
|
|
|
|
return json.dumps(response, ensure_ascii=False)
|
|
|
|
except Exception as e:
|
|
|
|
return json.dumps(response)
|
|
|
|
except Exception as e:
|
|
|
|
return response.text
|
2024-01-23 19:58:23 +08:00
|
|
|
else:
|
|
|
|
raise ValueError(f'Invalid response type {type(response)}')
|
|
|
|
|
2024-02-09 15:21:33 +08:00
|
|
|
def do_http_request(self, url: str, method: str, headers: dict[str, Any], parameters: dict[str, Any]) -> httpx.Response:
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
do http request depending on api bundle
|
|
|
|
"""
|
|
|
|
method = method.lower()
|
|
|
|
|
|
|
|
params = {}
|
|
|
|
path_params = {}
|
|
|
|
body = {}
|
|
|
|
cookies = {}
|
|
|
|
|
|
|
|
# check parameters
|
|
|
|
for parameter in self.api_bundle.openapi.get('parameters', []):
|
|
|
|
if parameter['in'] == 'path':
|
|
|
|
value = ''
|
|
|
|
if parameter['name'] in parameters:
|
|
|
|
value = parameters[parameter['name']]
|
|
|
|
elif parameter['required']:
|
|
|
|
raise ToolProviderCredentialValidationError(f"Missing required parameter {parameter['name']}")
|
2024-02-20 16:07:43 +08:00
|
|
|
else:
|
|
|
|
value = (parameter.get('schema', {}) or {}).get('default', '')
|
2024-01-23 19:58:23 +08:00
|
|
|
path_params[parameter['name']] = value
|
|
|
|
|
|
|
|
elif parameter['in'] == 'query':
|
|
|
|
value = ''
|
|
|
|
if parameter['name'] in parameters:
|
|
|
|
value = parameters[parameter['name']]
|
|
|
|
elif parameter['required']:
|
|
|
|
raise ToolProviderCredentialValidationError(f"Missing required parameter {parameter['name']}")
|
2024-02-20 16:07:43 +08:00
|
|
|
else:
|
|
|
|
value = (parameter.get('schema', {}) or {}).get('default', '')
|
2024-01-23 19:58:23 +08:00
|
|
|
params[parameter['name']] = value
|
|
|
|
|
|
|
|
elif parameter['in'] == 'cookie':
|
|
|
|
value = ''
|
|
|
|
if parameter['name'] in parameters:
|
|
|
|
value = parameters[parameter['name']]
|
|
|
|
elif parameter['required']:
|
|
|
|
raise ToolProviderCredentialValidationError(f"Missing required parameter {parameter['name']}")
|
2024-02-20 16:07:43 +08:00
|
|
|
else:
|
|
|
|
value = (parameter.get('schema', {}) or {}).get('default', '')
|
2024-01-23 19:58:23 +08:00
|
|
|
cookies[parameter['name']] = value
|
|
|
|
|
|
|
|
elif parameter['in'] == 'header':
|
|
|
|
value = ''
|
|
|
|
if parameter['name'] in parameters:
|
|
|
|
value = parameters[parameter['name']]
|
|
|
|
elif parameter['required']:
|
|
|
|
raise ToolProviderCredentialValidationError(f"Missing required parameter {parameter['name']}")
|
2024-02-20 16:07:43 +08:00
|
|
|
else:
|
|
|
|
value = (parameter.get('schema', {}) or {}).get('default', '')
|
2024-01-23 19:58:23 +08:00
|
|
|
headers[parameter['name']] = value
|
|
|
|
|
|
|
|
# check if there is a request body and handle it
|
|
|
|
if 'requestBody' in self.api_bundle.openapi and self.api_bundle.openapi['requestBody'] is not None:
|
|
|
|
# handle json request body
|
|
|
|
if 'content' in self.api_bundle.openapi['requestBody']:
|
|
|
|
for content_type in self.api_bundle.openapi['requestBody']['content']:
|
|
|
|
headers['Content-Type'] = content_type
|
|
|
|
body_schema = self.api_bundle.openapi['requestBody']['content'][content_type]['schema']
|
|
|
|
required = body_schema['required'] if 'required' in body_schema else []
|
|
|
|
properties = body_schema['properties'] if 'properties' in body_schema else {}
|
|
|
|
for name, property in properties.items():
|
|
|
|
if name in parameters:
|
|
|
|
# convert type
|
|
|
|
try:
|
|
|
|
value = parameters[name]
|
|
|
|
if property['type'] == 'integer':
|
|
|
|
value = int(value)
|
|
|
|
elif property['type'] == 'number':
|
|
|
|
# check if it is a float
|
|
|
|
if '.' in value:
|
|
|
|
value = float(value)
|
|
|
|
else:
|
|
|
|
value = int(value)
|
|
|
|
elif property['type'] == 'boolean':
|
|
|
|
value = bool(value)
|
|
|
|
body[name] = value
|
|
|
|
except ValueError as e:
|
|
|
|
body[name] = parameters[name]
|
|
|
|
elif name in required:
|
|
|
|
raise ToolProviderCredentialValidationError(
|
|
|
|
f"Missing required parameter {name} in operation {self.api_bundle.operation_id}"
|
|
|
|
)
|
|
|
|
elif 'default' in property:
|
|
|
|
body[name] = property['default']
|
|
|
|
else:
|
|
|
|
body[name] = None
|
|
|
|
break
|
|
|
|
|
|
|
|
# replace path parameters
|
|
|
|
for name, value in path_params.items():
|
|
|
|
url = url.replace(f'{{{name}}}', value)
|
|
|
|
|
|
|
|
# parse http body data if needed, for GET/HEAD/OPTIONS/TRACE, the body is ignored
|
|
|
|
if 'Content-Type' in headers:
|
|
|
|
if headers['Content-Type'] == 'application/json':
|
|
|
|
body = dumps(body)
|
|
|
|
else:
|
|
|
|
body = body
|
|
|
|
|
|
|
|
# do http request
|
|
|
|
if method == 'get':
|
2024-02-05 18:48:30 +08:00
|
|
|
response = ssrf_proxy.get(url, params=params, headers=headers, cookies=cookies, timeout=10, follow_redirects=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
elif method == 'post':
|
2024-02-05 18:48:30 +08:00
|
|
|
response = ssrf_proxy.post(url, params=params, headers=headers, cookies=cookies, data=body, timeout=10, follow_redirects=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
elif method == 'put':
|
2024-02-05 18:48:30 +08:00
|
|
|
response = ssrf_proxy.put(url, params=params, headers=headers, cookies=cookies, data=body, timeout=10, follow_redirects=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
elif method == 'delete':
|
|
|
|
"""
|
|
|
|
request body data is unsupported for DELETE method in standard http protocol
|
|
|
|
however, OpenAPI 3.0 supports request body data for DELETE method, so we support it here by using requests
|
|
|
|
"""
|
2024-02-05 18:48:30 +08:00
|
|
|
response = ssrf_proxy.delete(url, params=params, headers=headers, cookies=cookies, data=body, timeout=10, allow_redirects=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
elif method == 'patch':
|
2024-02-05 18:48:30 +08:00
|
|
|
response = ssrf_proxy.patch(url, params=params, headers=headers, cookies=cookies, data=body, timeout=10, follow_redirects=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
elif method == 'head':
|
2024-02-05 18:48:30 +08:00
|
|
|
response = ssrf_proxy.head(url, params=params, headers=headers, cookies=cookies, timeout=10, follow_redirects=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
elif method == 'options':
|
2024-02-05 18:48:30 +08:00
|
|
|
response = ssrf_proxy.options(url, params=params, headers=headers, cookies=cookies, timeout=10, follow_redirects=True)
|
2024-01-23 19:58:23 +08:00
|
|
|
else:
|
|
|
|
raise ValueError(f'Invalid http method {method}')
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
2024-02-09 15:21:33 +08:00
|
|
|
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMessage | list[ToolInvokeMessage]:
|
2024-01-23 19:58:23 +08:00
|
|
|
"""
|
|
|
|
invoke http request
|
|
|
|
"""
|
|
|
|
# assemble request
|
2024-01-31 11:58:07 +08:00
|
|
|
headers = self.assembling_request(tool_parameters)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
# do http request
|
2024-01-31 11:58:07 +08:00
|
|
|
response = self.do_http_request(self.api_bundle.server_url, self.api_bundle.method, headers, tool_parameters)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
# validate response
|
|
|
|
response = self.validate_and_parse_response(response)
|
|
|
|
|
|
|
|
# assemble invoke message
|
|
|
|
return self.create_text_message(response)
|
|
|
|
|