tools.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. import inspect
  2. import logging
  3. import re
  4. import inspect
  5. import aiohttp
  6. import asyncio
  7. import yaml
  8. import json
  9. from pydantic import BaseModel
  10. from pydantic.fields import FieldInfo
  11. from typing import (
  12. Any,
  13. Awaitable,
  14. Callable,
  15. get_type_hints,
  16. get_args,
  17. get_origin,
  18. Dict,
  19. List,
  20. Tuple,
  21. Union,
  22. Optional,
  23. Type,
  24. )
  25. from functools import update_wrapper, partial
  26. from fastapi import Request
  27. from pydantic import BaseModel, Field, create_model
  28. from langchain_core.utils.function_calling import (
  29. convert_to_openai_function as convert_pydantic_model_to_openai_function_spec,
  30. )
  31. from open_webui.models.tools import Tools
  32. from open_webui.models.users import UserModel
  33. from open_webui.utils.plugin import load_tool_module_by_id
  34. from open_webui.env import (
  35. SRC_LOG_LEVELS,
  36. AIOHTTP_CLIENT_TIMEOUT,
  37. AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER_DATA,
  38. AIOHTTP_CLIENT_SESSION_TOOL_SERVER_SSL,
  39. )
  40. import copy
  41. log = logging.getLogger(__name__)
  42. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  43. def get_async_tool_function_and_apply_extra_params(
  44. function: Callable, extra_params: dict
  45. ) -> Callable[..., Awaitable]:
  46. sig = inspect.signature(function)
  47. extra_params = {k: v for k, v in extra_params.items() if k in sig.parameters}
  48. partial_func = partial(function, **extra_params)
  49. # Remove the 'frozen' keyword arguments from the signature
  50. # python-genai uses the signature to infer the tool properties for native function calling
  51. parameters = []
  52. for name, parameter in sig.parameters.items():
  53. # Exclude keyword arguments that are frozen
  54. if name in extra_params:
  55. continue
  56. # Keep remaining parameters
  57. parameters.append(parameter)
  58. new_sig = inspect.Signature(
  59. parameters=parameters, return_annotation=sig.return_annotation
  60. )
  61. if inspect.iscoroutinefunction(function):
  62. # wrap the functools.partial as python-genai has trouble with it
  63. # https://github.com/googleapis/python-genai/issues/907
  64. async def new_function(*args, **kwargs):
  65. return await partial_func(*args, **kwargs)
  66. else:
  67. # Make it a coroutine function when it is not already
  68. async def new_function(*args, **kwargs):
  69. return partial_func(*args, **kwargs)
  70. update_wrapper(new_function, function)
  71. new_function.__signature__ = new_sig
  72. new_function.__function__ = function # type: ignore
  73. new_function.__extra_params__ = extra_params # type: ignore
  74. return new_function
  75. def get_updated_tool_function(function: Callable, extra_params: dict):
  76. # Get the original function and merge updated params
  77. __function__ = getattr(function, "__function__", None)
  78. __extra_params__ = getattr(function, "__extra_params__", None)
  79. if __function__ is not None and __extra_params__ is not None:
  80. return get_async_tool_function_and_apply_extra_params(
  81. __function__,
  82. {**__extra_params__, **extra_params},
  83. )
  84. return function
  85. async def get_tools(
  86. request: Request, tool_ids: list[str], user: UserModel, extra_params: dict
  87. ) -> dict[str, dict]:
  88. tools_dict = {}
  89. for tool_id in tool_ids:
  90. tool = Tools.get_tool_by_id(tool_id)
  91. if tool is None:
  92. if tool_id.startswith("server:"):
  93. splits = tool_id.split(":")
  94. if len(splits) == 2:
  95. type = "openapi"
  96. server_id = splits[1]
  97. elif len(splits) == 3:
  98. type = splits[1]
  99. server_id = splits[2]
  100. server_id_splits = server_id.split("|")
  101. if len(server_id_splits) == 2:
  102. server_id = server_id_splits[0]
  103. function_names = server_id_splits[1].split(",")
  104. if type == "openapi":
  105. tool_server_data = None
  106. for server in await get_tool_servers(request):
  107. if server["id"] == server_id:
  108. tool_server_data = server
  109. break
  110. if tool_server_data is None:
  111. log.warning(f"Tool server data not found for {server_id}")
  112. continue
  113. tool_server_idx = tool_server_data.get("idx", 0)
  114. tool_server_connection = (
  115. request.app.state.config.TOOL_SERVER_CONNECTIONS[
  116. tool_server_idx
  117. ]
  118. )
  119. specs = tool_server_data.get("specs", [])
  120. for spec in specs:
  121. function_name = spec["name"]
  122. auth_type = tool_server_connection.get("auth_type", "bearer")
  123. cookies = {}
  124. headers = {
  125. "Content-Type": "application/json",
  126. }
  127. if auth_type == "bearer":
  128. headers["Authorization"] = (
  129. f"Bearer {tool_server_connection.get('key', '')}"
  130. )
  131. elif auth_type == "none":
  132. # No authentication
  133. pass
  134. elif auth_type == "session":
  135. cookies = request.cookies
  136. headers["Authorization"] = (
  137. f"Bearer {request.state.token.credentials}"
  138. )
  139. elif auth_type == "system_oauth":
  140. cookies = request.cookies
  141. oauth_token = extra_params.get("__oauth_token__", None)
  142. if oauth_token:
  143. headers["Authorization"] = (
  144. f"Bearer {oauth_token.get('access_token', '')}"
  145. )
  146. connection_headers = tool_server_connection.get("headers", None)
  147. if connection_headers and isinstance(connection_headers, dict):
  148. for key, value in connection_headers.items():
  149. headers[key] = value
  150. def make_tool_function(
  151. function_name, tool_server_data, headers
  152. ):
  153. async def tool_function(**kwargs):
  154. return await execute_tool_server(
  155. url=tool_server_data["url"],
  156. headers=headers,
  157. cookies=cookies,
  158. name=function_name,
  159. params=kwargs,
  160. server_data=tool_server_data,
  161. )
  162. return tool_function
  163. tool_function = make_tool_function(
  164. function_name, tool_server_data, headers
  165. )
  166. callable = get_async_tool_function_and_apply_extra_params(
  167. tool_function,
  168. {},
  169. )
  170. tool_dict = {
  171. "tool_id": tool_id,
  172. "callable": callable,
  173. "spec": spec,
  174. # Misc info
  175. "type": "external",
  176. }
  177. # Handle function name collisions
  178. while function_name in tools_dict:
  179. log.warning(
  180. f"Tool {function_name} already exists in another tools!"
  181. )
  182. # Prepend server ID to function name
  183. function_name = f"{server_id}_{function_name}"
  184. tools_dict[function_name] = tool_dict
  185. else:
  186. continue
  187. else:
  188. continue
  189. else:
  190. module = request.app.state.TOOLS.get(tool_id, None)
  191. if module is None:
  192. module, _ = load_tool_module_by_id(tool_id)
  193. request.app.state.TOOLS[tool_id] = module
  194. extra_params["__id__"] = tool_id
  195. # Set valves for the tool
  196. if hasattr(module, "valves") and hasattr(module, "Valves"):
  197. valves = Tools.get_tool_valves_by_id(tool_id) or {}
  198. module.valves = module.Valves(**valves)
  199. if hasattr(module, "UserValves"):
  200. extra_params["__user__"]["valves"] = module.UserValves( # type: ignore
  201. **Tools.get_user_valves_by_id_and_user_id(tool_id, user.id)
  202. )
  203. for spec in tool.specs:
  204. # TODO: Fix hack for OpenAI API
  205. # Some times breaks OpenAI but others don't. Leaving the comment
  206. for val in spec.get("parameters", {}).get("properties", {}).values():
  207. if val.get("type") == "str":
  208. val["type"] = "string"
  209. # Remove internal reserved parameters (e.g. __id__, __user__)
  210. spec["parameters"]["properties"] = {
  211. key: val
  212. for key, val in spec["parameters"]["properties"].items()
  213. if not key.startswith("__")
  214. }
  215. # convert to function that takes only model params and inserts custom params
  216. function_name = spec["name"]
  217. tool_function = getattr(module, function_name)
  218. callable = get_async_tool_function_and_apply_extra_params(
  219. tool_function, extra_params
  220. )
  221. # TODO: Support Pydantic models as parameters
  222. if callable.__doc__ and callable.__doc__.strip() != "":
  223. s = re.split(":(param|return)", callable.__doc__, 1)
  224. spec["description"] = s[0]
  225. else:
  226. spec["description"] = function_name
  227. tool_dict = {
  228. "tool_id": tool_id,
  229. "callable": callable,
  230. "spec": spec,
  231. # Misc info
  232. "metadata": {
  233. "file_handler": hasattr(module, "file_handler")
  234. and module.file_handler,
  235. "citation": hasattr(module, "citation") and module.citation,
  236. },
  237. }
  238. # Handle function name collisions
  239. while function_name in tools_dict:
  240. log.warning(
  241. f"Tool {function_name} already exists in another tools!"
  242. )
  243. # Prepend tool ID to function name
  244. function_name = f"{tool_id}_{function_name}"
  245. tools_dict[function_name] = tool_dict
  246. return tools_dict
  247. def parse_description(docstring: str | None) -> str:
  248. """
  249. Parse a function's docstring to extract the description.
  250. Args:
  251. docstring (str): The docstring to parse.
  252. Returns:
  253. str: The description.
  254. """
  255. if not docstring:
  256. return ""
  257. lines = [line.strip() for line in docstring.strip().split("\n")]
  258. description_lines: list[str] = []
  259. for line in lines:
  260. if re.match(r":param", line) or re.match(r":return", line):
  261. break
  262. description_lines.append(line)
  263. return "\n".join(description_lines)
  264. def parse_docstring(docstring):
  265. """
  266. Parse a function's docstring to extract parameter descriptions in reST format.
  267. Args:
  268. docstring (str): The docstring to parse.
  269. Returns:
  270. dict: A dictionary where keys are parameter names and values are descriptions.
  271. """
  272. if not docstring:
  273. return {}
  274. # Regex to match `:param name: description` format
  275. param_pattern = re.compile(r":param (\w+):\s*(.+)")
  276. param_descriptions = {}
  277. for line in docstring.splitlines():
  278. match = param_pattern.match(line.strip())
  279. if not match:
  280. continue
  281. param_name, param_description = match.groups()
  282. if param_name.startswith("__"):
  283. continue
  284. param_descriptions[param_name] = param_description
  285. return param_descriptions
  286. def convert_function_to_pydantic_model(func: Callable) -> type[BaseModel]:
  287. """
  288. Converts a Python function's type hints and docstring to a Pydantic model,
  289. including support for nested types, default values, and descriptions.
  290. Args:
  291. func: The function whose type hints and docstring should be converted.
  292. model_name: The name of the generated Pydantic model.
  293. Returns:
  294. A Pydantic model class.
  295. """
  296. type_hints = get_type_hints(func)
  297. signature = inspect.signature(func)
  298. parameters = signature.parameters
  299. docstring = func.__doc__
  300. function_description = parse_description(docstring)
  301. function_param_descriptions = parse_docstring(docstring)
  302. field_defs = {}
  303. for name, param in parameters.items():
  304. type_hint = type_hints.get(name, Any)
  305. default_value = param.default if param.default is not param.empty else ...
  306. param_description = function_param_descriptions.get(name, None)
  307. if param_description:
  308. field_defs[name] = (
  309. type_hint,
  310. Field(default_value, description=param_description),
  311. )
  312. else:
  313. field_defs[name] = type_hint, default_value
  314. model = create_model(func.__name__, **field_defs)
  315. model.__doc__ = function_description
  316. return model
  317. def get_functions_from_tool(tool: object) -> list[Callable]:
  318. return [
  319. getattr(tool, func)
  320. for func in dir(tool)
  321. if callable(
  322. getattr(tool, func)
  323. ) # checks if the attribute is callable (a method or function).
  324. and not func.startswith(
  325. "__"
  326. ) # filters out special (dunder) methods like init, str, etc. — these are usually built-in functions of an object that you might not need to use directly.
  327. and not inspect.isclass(
  328. getattr(tool, func)
  329. ) # ensures that the callable is not a class itself, just a method or function.
  330. ]
  331. def get_tool_specs(tool_module: object) -> list[dict]:
  332. function_models = map(
  333. convert_function_to_pydantic_model, get_functions_from_tool(tool_module)
  334. )
  335. specs = [
  336. convert_pydantic_model_to_openai_function_spec(function_model)
  337. for function_model in function_models
  338. ]
  339. return specs
  340. def resolve_schema(schema, components):
  341. """
  342. Recursively resolves a JSON schema using OpenAPI components.
  343. """
  344. if not schema:
  345. return {}
  346. if "$ref" in schema:
  347. ref_path = schema["$ref"]
  348. ref_parts = ref_path.strip("#/").split("/")
  349. resolved = components
  350. for part in ref_parts[1:]: # Skip the initial 'components'
  351. resolved = resolved.get(part, {})
  352. return resolve_schema(resolved, components)
  353. resolved_schema = copy.deepcopy(schema)
  354. # Recursively resolve inner schemas
  355. if "properties" in resolved_schema:
  356. for prop, prop_schema in resolved_schema["properties"].items():
  357. resolved_schema["properties"][prop] = resolve_schema(
  358. prop_schema, components
  359. )
  360. if "items" in resolved_schema:
  361. resolved_schema["items"] = resolve_schema(resolved_schema["items"], components)
  362. return resolved_schema
  363. def convert_openapi_to_tool_payload(openapi_spec):
  364. """
  365. Converts an OpenAPI specification into a custom tool payload structure.
  366. Args:
  367. openapi_spec (dict): The OpenAPI specification as a Python dict.
  368. Returns:
  369. list: A list of tool payloads.
  370. """
  371. tool_payload = []
  372. for path, methods in openapi_spec.get("paths", {}).items():
  373. for method, operation in methods.items():
  374. if operation.get("operationId"):
  375. tool = {
  376. "name": operation.get("operationId"),
  377. "description": operation.get(
  378. "description",
  379. operation.get("summary", "No description available."),
  380. ),
  381. "parameters": {"type": "object", "properties": {}, "required": []},
  382. }
  383. # Extract path and query parameters
  384. for param in operation.get("parameters", []):
  385. param_name = param["name"]
  386. param_schema = param.get("schema", {})
  387. description = param_schema.get("description", "")
  388. if not description:
  389. description = param.get("description") or ""
  390. if param_schema.get("enum") and isinstance(
  391. param_schema.get("enum"), list
  392. ):
  393. description += (
  394. f". Possible values: {', '.join(param_schema.get('enum'))}"
  395. )
  396. param_property = {
  397. "type": param_schema.get("type"),
  398. "description": description,
  399. }
  400. # Include items property for array types (required by OpenAI)
  401. if param_schema.get("type") == "array" and "items" in param_schema:
  402. param_property["items"] = param_schema["items"]
  403. tool["parameters"]["properties"][param_name] = param_property
  404. if param.get("required"):
  405. tool["parameters"]["required"].append(param_name)
  406. # Extract and resolve requestBody if available
  407. request_body = operation.get("requestBody")
  408. if request_body:
  409. content = request_body.get("content", {})
  410. json_schema = content.get("application/json", {}).get("schema")
  411. if json_schema:
  412. resolved_schema = resolve_schema(
  413. json_schema, openapi_spec.get("components", {})
  414. )
  415. if resolved_schema.get("properties"):
  416. tool["parameters"]["properties"].update(
  417. resolved_schema["properties"]
  418. )
  419. if "required" in resolved_schema:
  420. tool["parameters"]["required"] = list(
  421. set(
  422. tool["parameters"]["required"]
  423. + resolved_schema["required"]
  424. )
  425. )
  426. elif resolved_schema.get("type") == "array":
  427. tool["parameters"] = (
  428. resolved_schema # special case for array
  429. )
  430. tool_payload.append(tool)
  431. return tool_payload
  432. async def set_tool_servers(request: Request):
  433. request.app.state.TOOL_SERVERS = await get_tool_servers_data(
  434. request.app.state.config.TOOL_SERVER_CONNECTIONS
  435. )
  436. if request.app.state.redis is not None:
  437. await request.app.state.redis.set(
  438. "tool_servers", json.dumps(request.app.state.TOOL_SERVERS)
  439. )
  440. return request.app.state.TOOL_SERVERS
  441. async def get_tool_servers(request: Request):
  442. tool_servers = []
  443. if request.app.state.redis is not None:
  444. try:
  445. tool_servers = json.loads(await request.app.state.redis.get("tool_servers"))
  446. request.app.state.TOOL_SERVERS = tool_servers
  447. except Exception as e:
  448. log.error(f"Error fetching tool_servers from Redis: {e}")
  449. if not tool_servers:
  450. tool_servers = await set_tool_servers(request)
  451. return tool_servers
  452. async def get_tool_server_data(url: str, headers: Optional[dict]) -> Dict[str, Any]:
  453. _headers = {
  454. "Accept": "application/json",
  455. "Content-Type": "application/json",
  456. }
  457. if headers:
  458. _headers.update(headers)
  459. error = None
  460. try:
  461. timeout = aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER_DATA)
  462. async with aiohttp.ClientSession(timeout=timeout, trust_env=True) as session:
  463. async with session.get(
  464. url, headers=_headers, ssl=AIOHTTP_CLIENT_SESSION_TOOL_SERVER_SSL
  465. ) as response:
  466. if response.status != 200:
  467. error_body = await response.json()
  468. raise Exception(error_body)
  469. text_content = None
  470. # Check if URL ends with .yaml or .yml to determine format
  471. if url.lower().endswith((".yaml", ".yml")):
  472. text_content = await response.text()
  473. res = yaml.safe_load(text_content)
  474. else:
  475. text_content = await response.text()
  476. try:
  477. res = json.loads(text_content)
  478. except json.JSONDecodeError:
  479. try:
  480. res = yaml.safe_load(text_content)
  481. except Exception as e:
  482. raise e
  483. except Exception as err:
  484. log.exception(f"Could not fetch tool server spec from {url}")
  485. if isinstance(err, dict) and "detail" in err:
  486. error = err["detail"]
  487. else:
  488. error = str(err)
  489. raise Exception(error)
  490. log.debug(f"Fetched data: {res}")
  491. return res
  492. async def get_tool_servers_data(servers: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
  493. # Prepare list of enabled servers along with their original index
  494. tasks = []
  495. server_entries = []
  496. for idx, server in enumerate(servers):
  497. if (
  498. server.get("config", {}).get("enable")
  499. and server.get("type", "openapi") == "openapi"
  500. ):
  501. info = server.get("info", {})
  502. auth_type = server.get("auth_type", "bearer")
  503. token = None
  504. if auth_type == "bearer":
  505. token = server.get("key", "")
  506. elif auth_type == "none":
  507. # No authentication
  508. pass
  509. id = info.get("id")
  510. if not id:
  511. id = str(idx)
  512. server_url = server.get("url")
  513. spec_type = server.get("spec_type", "url")
  514. # Create async tasks to fetch data
  515. task = None
  516. if spec_type == "url":
  517. # Path (to OpenAPI spec URL) can be either a full URL or a path to append to the base URL
  518. openapi_path = server.get("path", "openapi.json")
  519. spec_url = get_tool_server_url(server_url, openapi_path)
  520. # Fetch from URL
  521. task = get_tool_server_data(
  522. spec_url,
  523. {"Authorization": f"Bearer {token}"} if token else None,
  524. )
  525. elif spec_type == "json" and server.get("spec", ""):
  526. # Use provided JSON spec
  527. spec_json = None
  528. try:
  529. spec_json = json.loads(server.get("spec", ""))
  530. except Exception as e:
  531. log.error(f"Error parsing JSON spec for tool server {id}: {e}")
  532. if spec_json:
  533. task = asyncio.sleep(
  534. 0,
  535. result=spec_json,
  536. )
  537. if task:
  538. tasks.append(task)
  539. server_entries.append((id, idx, server, server_url, info, token))
  540. # Execute tasks concurrently
  541. responses = await asyncio.gather(*tasks, return_exceptions=True)
  542. # Build final results with index and server metadata
  543. results = []
  544. for (id, idx, server, url, info, _), response in zip(server_entries, responses):
  545. if isinstance(response, Exception):
  546. log.error(f"Failed to connect to {url} OpenAPI tool server")
  547. continue
  548. response = {
  549. "openapi": response,
  550. "info": response.get("info", {}),
  551. "specs": convert_openapi_to_tool_payload(response),
  552. }
  553. openapi_data = response.get("openapi", {})
  554. if info and isinstance(openapi_data, dict):
  555. openapi_data["info"] = openapi_data.get("info", {})
  556. if "name" in info:
  557. openapi_data["info"]["title"] = info.get("name", "Tool Server")
  558. if "description" in info:
  559. openapi_data["info"]["description"] = info.get("description", "")
  560. results.append(
  561. {
  562. "id": str(id),
  563. "idx": idx,
  564. "url": server.get("url"),
  565. "openapi": openapi_data,
  566. "info": response.get("info"),
  567. "specs": response.get("specs"),
  568. }
  569. )
  570. return results
  571. async def execute_tool_server(
  572. url: str,
  573. headers: Dict[str, str],
  574. cookies: Dict[str, str],
  575. name: str,
  576. params: Dict[str, Any],
  577. server_data: Dict[str, Any],
  578. ) -> Tuple[Dict[str, Any], Optional[Dict[str, Any]]]:
  579. error = None
  580. try:
  581. openapi = server_data.get("openapi", {})
  582. paths = openapi.get("paths", {})
  583. matching_route = None
  584. for route_path, methods in paths.items():
  585. for http_method, operation in methods.items():
  586. if isinstance(operation, dict) and operation.get("operationId") == name:
  587. matching_route = (route_path, methods)
  588. break
  589. if matching_route:
  590. break
  591. if not matching_route:
  592. raise Exception(f"No matching route found for operationId: {name}")
  593. route_path, methods = matching_route
  594. method_entry = None
  595. for http_method, operation in methods.items():
  596. if operation.get("operationId") == name:
  597. method_entry = (http_method.lower(), operation)
  598. break
  599. if not method_entry:
  600. raise Exception(f"No matching method found for operationId: {name}")
  601. http_method, operation = method_entry
  602. path_params = {}
  603. query_params = {}
  604. body_params = {}
  605. for param in operation.get("parameters", []):
  606. param_name = param["name"]
  607. param_in = param["in"]
  608. if param_name in params:
  609. if param_in == "path":
  610. path_params[param_name] = params[param_name]
  611. elif param_in == "query":
  612. query_params[param_name] = params[param_name]
  613. final_url = f"{url}{route_path}"
  614. for key, value in path_params.items():
  615. final_url = final_url.replace(f"{{{key}}}", str(value))
  616. if query_params:
  617. query_string = "&".join(f"{k}={v}" for k, v in query_params.items())
  618. final_url = f"{final_url}?{query_string}"
  619. if operation.get("requestBody", {}).get("content"):
  620. if params:
  621. body_params = params
  622. async with aiohttp.ClientSession(
  623. trust_env=True, timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT)
  624. ) as session:
  625. request_method = getattr(session, http_method.lower())
  626. if http_method in ["post", "put", "patch", "delete"]:
  627. async with request_method(
  628. final_url,
  629. json=body_params,
  630. headers=headers,
  631. cookies=cookies,
  632. ssl=AIOHTTP_CLIENT_SESSION_TOOL_SERVER_SSL,
  633. allow_redirects=False,
  634. ) as response:
  635. if response.status >= 400:
  636. text = await response.text()
  637. raise Exception(f"HTTP error {response.status}: {text}")
  638. try:
  639. response_data = await response.json()
  640. except Exception:
  641. response_data = await response.text()
  642. response_headers = response.headers
  643. return (response_data, response_headers)
  644. else:
  645. async with request_method(
  646. final_url,
  647. headers=headers,
  648. cookies=cookies,
  649. ssl=AIOHTTP_CLIENT_SESSION_TOOL_SERVER_SSL,
  650. allow_redirects=False,
  651. ) as response:
  652. if response.status >= 400:
  653. text = await response.text()
  654. raise Exception(f"HTTP error {response.status}: {text}")
  655. try:
  656. response_data = await response.json()
  657. except Exception:
  658. response_data = await response.text()
  659. response_headers = response.headers
  660. return (response_data, response_headers)
  661. except Exception as err:
  662. error = str(err)
  663. log.exception(f"API Request Error: {error}")
  664. return ({"error": error}, None)
  665. def get_tool_server_url(url: Optional[str], path: str) -> str:
  666. """
  667. Build the full URL for a tool server, given a base url and a path.
  668. """
  669. if "://" in path:
  670. # If it contains "://", it's a full URL
  671. return path
  672. if not path.startswith("/"):
  673. # Ensure the path starts with a slash
  674. path = f"/{path}"
  675. return f"{url}{path}"