You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tool.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import json
  2. from collections.abc import Generator
  3. from dataclasses import dataclass
  4. from os import getenv
  5. from typing import Any, Optional, Union
  6. from urllib.parse import urlencode
  7. import httpx
  8. from core.file.file_manager import download
  9. from core.helper import ssrf_proxy
  10. from core.tools.__base.tool import Tool
  11. from core.tools.__base.tool_runtime import ToolRuntime
  12. from core.tools.entities.tool_bundle import ApiToolBundle
  13. from core.tools.entities.tool_entities import ToolEntity, ToolInvokeMessage, ToolProviderType
  14. from core.tools.errors import ToolInvokeError, ToolParameterValidationError, ToolProviderCredentialValidationError
  15. API_TOOL_DEFAULT_TIMEOUT = (
  16. int(getenv("API_TOOL_DEFAULT_CONNECT_TIMEOUT", "10")),
  17. int(getenv("API_TOOL_DEFAULT_READ_TIMEOUT", "60")),
  18. )
  19. @dataclass
  20. class ParsedResponse:
  21. """Represents a parsed HTTP response with type information"""
  22. content: Union[str, dict]
  23. is_json: bool
  24. def to_string(self) -> str:
  25. """Convert response to string format for credential validation"""
  26. if isinstance(self.content, dict):
  27. return json.dumps(self.content, ensure_ascii=False)
  28. return str(self.content)
  29. class ApiTool(Tool):
  30. """
  31. Api tool
  32. """
  33. def __init__(self, entity: ToolEntity, api_bundle: ApiToolBundle, runtime: ToolRuntime, provider_id: str):
  34. super().__init__(entity, runtime)
  35. self.api_bundle = api_bundle
  36. self.provider_id = provider_id
  37. def fork_tool_runtime(self, runtime: ToolRuntime):
  38. """
  39. fork a new tool with metadata
  40. :return: the new tool
  41. """
  42. if self.api_bundle is None:
  43. raise ValueError("api_bundle is required")
  44. return self.__class__(
  45. entity=self.entity,
  46. api_bundle=self.api_bundle.model_copy(),
  47. runtime=runtime,
  48. provider_id=self.provider_id,
  49. )
  50. def validate_credentials(
  51. self, credentials: dict[str, Any], parameters: dict[str, Any], format_only: bool = False
  52. ) -> str:
  53. """
  54. validate the credentials for Api tool
  55. """
  56. # assemble validate request and request parameters
  57. headers = self.assembling_request(parameters)
  58. if format_only:
  59. return ""
  60. response = self.do_http_request(self.api_bundle.server_url, self.api_bundle.method, headers, parameters)
  61. # validate response
  62. parsed_response = self.validate_and_parse_response(response)
  63. # For credential validation, always return as string
  64. return parsed_response.to_string()
  65. def tool_provider_type(self) -> ToolProviderType:
  66. return ToolProviderType.API
  67. def assembling_request(self, parameters: dict[str, Any]) -> dict[str, Any]:
  68. headers = {}
  69. if self.runtime is None:
  70. raise ToolProviderCredentialValidationError("runtime not initialized")
  71. credentials = self.runtime.credentials or {}
  72. if "auth_type" not in credentials:
  73. raise ToolProviderCredentialValidationError("Missing auth_type")
  74. if credentials["auth_type"] in ("api_key_header", "api_key"): # backward compatibility:
  75. api_key_header = "Authorization"
  76. if "api_key_header" in credentials:
  77. api_key_header = credentials["api_key_header"]
  78. if "api_key_value" not in credentials:
  79. raise ToolProviderCredentialValidationError("Missing api_key_value")
  80. elif not isinstance(credentials["api_key_value"], str):
  81. raise ToolProviderCredentialValidationError("api_key_value must be a string")
  82. if "api_key_header_prefix" in credentials:
  83. api_key_header_prefix = credentials["api_key_header_prefix"]
  84. if api_key_header_prefix == "basic" and credentials["api_key_value"]:
  85. credentials["api_key_value"] = f"Basic {credentials['api_key_value']}"
  86. elif api_key_header_prefix == "bearer" and credentials["api_key_value"]:
  87. credentials["api_key_value"] = f"Bearer {credentials['api_key_value']}"
  88. elif api_key_header_prefix == "custom":
  89. pass
  90. headers[api_key_header] = credentials["api_key_value"]
  91. elif credentials["auth_type"] == "api_key_query":
  92. # For query parameter authentication, we don't add anything to headers
  93. # The query parameter will be added in do_http_request method
  94. pass
  95. needed_parameters = [parameter for parameter in (self.api_bundle.parameters or []) if parameter.required]
  96. for parameter in needed_parameters:
  97. if parameter.required and parameter.name not in parameters:
  98. if parameter.default is not None:
  99. parameters[parameter.name] = parameter.default
  100. else:
  101. raise ToolParameterValidationError(f"Missing required parameter {parameter.name}")
  102. return headers
  103. def validate_and_parse_response(self, response: httpx.Response) -> ParsedResponse:
  104. """
  105. validate the response and return parsed content with type information
  106. :return: ParsedResponse with content and is_json flag
  107. """
  108. if isinstance(response, httpx.Response):
  109. if response.status_code >= 400:
  110. raise ToolInvokeError(f"Request failed with status code {response.status_code} and {response.text}")
  111. if not response.content:
  112. return ParsedResponse(
  113. "Empty response from the tool, please check your parameters and try again.", False
  114. )
  115. # Check content type
  116. content_type = response.headers.get("content-type", "").lower()
  117. is_json_content_type = "application/json" in content_type
  118. # Try to parse as JSON
  119. try:
  120. json_response = response.json()
  121. # If content-type indicates JSON, return as JSON object
  122. if is_json_content_type:
  123. return ParsedResponse(json_response, True)
  124. else:
  125. # If content-type doesn't indicate JSON, treat as text regardless of content
  126. return ParsedResponse(response.text, False)
  127. except Exception:
  128. # Not valid JSON, return as text
  129. return ParsedResponse(response.text, False)
  130. else:
  131. raise ValueError(f"Invalid response type {type(response)}")
  132. @staticmethod
  133. def get_parameter_value(parameter, parameters):
  134. if parameter["name"] in parameters:
  135. return parameters[parameter["name"]]
  136. elif parameter.get("required", False):
  137. raise ToolParameterValidationError(f"Missing required parameter {parameter['name']}")
  138. else:
  139. return (parameter.get("schema", {}) or {}).get("default", "")
  140. def do_http_request(
  141. self, url: str, method: str, headers: dict[str, Any], parameters: dict[str, Any]
  142. ) -> httpx.Response:
  143. """
  144. do http request depending on api bundle
  145. """
  146. method = method.lower()
  147. params = {}
  148. path_params = {}
  149. # FIXME: body should be a dict[str, Any] but it changed a lot in this function
  150. body: Any = {}
  151. cookies = {}
  152. files = []
  153. # Add API key to query parameters if auth_type is api_key_query
  154. if self.runtime and self.runtime.credentials:
  155. credentials = self.runtime.credentials
  156. if credentials.get("auth_type") == "api_key_query":
  157. api_key_query_param = credentials.get("api_key_query_param", "key")
  158. api_key_value = credentials.get("api_key_value")
  159. if api_key_value:
  160. params[api_key_query_param] = api_key_value
  161. # check parameters
  162. for parameter in self.api_bundle.openapi.get("parameters", []):
  163. value = self.get_parameter_value(parameter, parameters)
  164. if parameter["in"] == "path":
  165. path_params[parameter["name"]] = value
  166. elif parameter["in"] == "query":
  167. if value != "":
  168. params[parameter["name"]] = value
  169. elif parameter["in"] == "cookie":
  170. cookies[parameter["name"]] = value
  171. elif parameter["in"] == "header":
  172. headers[parameter["name"]] = str(value)
  173. # check if there is a request body and handle it
  174. if "requestBody" in self.api_bundle.openapi and self.api_bundle.openapi["requestBody"] is not None:
  175. # handle json request body
  176. if "content" in self.api_bundle.openapi["requestBody"]:
  177. for content_type in self.api_bundle.openapi["requestBody"]["content"]:
  178. headers["Content-Type"] = content_type
  179. body_schema = self.api_bundle.openapi["requestBody"]["content"][content_type]["schema"]
  180. # handle ref schema
  181. if "$ref" in body_schema:
  182. ref_path = body_schema["$ref"].split("/")
  183. ref_name = ref_path[-1]
  184. if (
  185. "components" in self.api_bundle.openapi
  186. and "schemas" in self.api_bundle.openapi["components"]
  187. ):
  188. if ref_name in self.api_bundle.openapi["components"]["schemas"]:
  189. body_schema = self.api_bundle.openapi["components"]["schemas"][ref_name]
  190. required = body_schema.get("required", [])
  191. properties = body_schema.get("properties", {})
  192. for name, property in properties.items():
  193. if name in parameters:
  194. # multiple file upload: if the type is array and the items have format as binary
  195. if property.get("type") == "array" and property.get("items", {}).get("format") == "binary":
  196. # parameters[name] should be a list of file objects.
  197. for f in parameters[name]:
  198. files.append((name, (f.filename, download(f), f.mime_type)))
  199. elif property.get("format") == "binary":
  200. f = parameters[name]
  201. files.append((name, (f.filename, download(f), f.mime_type)))
  202. elif "$ref" in property:
  203. body[name] = parameters[name]
  204. else:
  205. # convert type
  206. body[name] = self._convert_body_property_type(property, parameters[name])
  207. elif name in required:
  208. raise ToolParameterValidationError(
  209. f"Missing required parameter {name} in operation {self.api_bundle.operation_id}"
  210. )
  211. elif "default" in property:
  212. body[name] = property["default"]
  213. else:
  214. # omit optional parameters that weren't provided, instead of setting them to None
  215. pass
  216. break
  217. # replace path parameters
  218. for name, value in path_params.items():
  219. url = url.replace(f"{{{name}}}", f"{value}")
  220. # parse http body data if needed
  221. if "Content-Type" in headers:
  222. if headers["Content-Type"] == "application/json":
  223. body = json.dumps(body)
  224. elif headers["Content-Type"] == "application/x-www-form-urlencoded":
  225. body = urlencode(body)
  226. else:
  227. body = body
  228. # if there is a file upload, remove the Content-Type header
  229. # so that httpx can automatically generate the boundary header required for multipart/form-data.
  230. # issue: https://github.com/langgenius/dify/issues/13684
  231. # reference: https://stackoverflow.com/questions/39280438/fetch-missing-boundary-in-multipart-form-data-post
  232. if files:
  233. headers.pop("Content-Type", None)
  234. if method in {
  235. "get",
  236. "head",
  237. "post",
  238. "put",
  239. "delete",
  240. "patch",
  241. "options",
  242. "GET",
  243. "POST",
  244. "PUT",
  245. "PATCH",
  246. "DELETE",
  247. "HEAD",
  248. "OPTIONS",
  249. }:
  250. response: httpx.Response = getattr(ssrf_proxy, method.lower())(
  251. url,
  252. params=params,
  253. headers=headers,
  254. cookies=cookies,
  255. data=body,
  256. files=files,
  257. timeout=API_TOOL_DEFAULT_TIMEOUT,
  258. follow_redirects=True,
  259. )
  260. return response
  261. else:
  262. raise ValueError(f"Invalid http method {method}")
  263. def _convert_body_property_any_of(
  264. self, property: dict[str, Any], value: Any, any_of: list[dict[str, Any]], max_recursive=10
  265. ) -> Any:
  266. if max_recursive <= 0:
  267. raise Exception("Max recursion depth reached")
  268. for option in any_of or []:
  269. try:
  270. if "type" in option:
  271. # Attempt to convert the value based on the type.
  272. if option["type"] == "integer" or option["type"] == "int":
  273. return int(value)
  274. elif option["type"] == "number":
  275. if "." in str(value):
  276. return float(value)
  277. else:
  278. return int(value)
  279. elif option["type"] == "string":
  280. return str(value)
  281. elif option["type"] == "boolean":
  282. if str(value).lower() in {"true", "1"}:
  283. return True
  284. elif str(value).lower() in {"false", "0"}:
  285. return False
  286. else:
  287. continue # Not a boolean, try next option
  288. elif option["type"] == "null" and not value:
  289. return None
  290. else:
  291. continue # Unsupported type, try next option
  292. elif "anyOf" in option and isinstance(option["anyOf"], list):
  293. # Recursive call to handle nested anyOf
  294. return self._convert_body_property_any_of(property, value, option["anyOf"], max_recursive - 1)
  295. except ValueError:
  296. continue # Conversion failed, try next option
  297. # If no option succeeded, you might want to return the value as is or raise an error
  298. return value # or raise ValueError(f"Cannot convert value '{value}' to any specified type in anyOf")
  299. def _convert_body_property_type(self, property: dict[str, Any], value: Any) -> Any:
  300. try:
  301. if "type" in property:
  302. if property["type"] == "integer" or property["type"] == "int":
  303. return int(value)
  304. elif property["type"] == "number":
  305. # check if it is a float
  306. if "." in str(value):
  307. return float(value)
  308. else:
  309. return int(value)
  310. elif property["type"] == "string":
  311. return str(value)
  312. elif property["type"] == "boolean":
  313. return bool(value)
  314. elif property["type"] == "null":
  315. if value is None:
  316. return None
  317. elif property["type"] == "object" or property["type"] == "array":
  318. if isinstance(value, str):
  319. try:
  320. return json.loads(value)
  321. except ValueError:
  322. return value
  323. elif isinstance(value, dict):
  324. return value
  325. else:
  326. return value
  327. else:
  328. raise ValueError(f"Invalid type {property['type']} for property {property}")
  329. elif "anyOf" in property and isinstance(property["anyOf"], list):
  330. return self._convert_body_property_any_of(property, value, property["anyOf"])
  331. except ValueError:
  332. return value
  333. def _invoke(
  334. self,
  335. user_id: str,
  336. tool_parameters: dict[str, Any],
  337. conversation_id: Optional[str] = None,
  338. app_id: Optional[str] = None,
  339. message_id: Optional[str] = None,
  340. ) -> Generator[ToolInvokeMessage, None, None]:
  341. """
  342. invoke http request
  343. """
  344. response: httpx.Response | str = ""
  345. # assemble request
  346. headers = self.assembling_request(tool_parameters)
  347. # do http request
  348. response = self.do_http_request(self.api_bundle.server_url, self.api_bundle.method, headers, tool_parameters)
  349. # validate response
  350. parsed_response = self.validate_and_parse_response(response)
  351. # assemble invoke message based on response type
  352. if parsed_response.is_json and isinstance(parsed_response.content, dict):
  353. yield self.create_json_message(parsed_response.content)
  354. else:
  355. # Convert to string if needed and create text message
  356. text_response = (
  357. parsed_response.content if isinstance(parsed_response.content, str) else str(parsed_response.content)
  358. )
  359. yield self.create_text_message(text_response)