|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any, Callable, Optional, Union |
| 16 | +from warnings import warn |
| 17 | + |
| 18 | +from aiohttp import ClientSession |
| 19 | + |
| 20 | +from .tools import AsyncToolboxTool |
| 21 | +from .utils import ManifestSchema, _load_manifest |
| 22 | + |
| 23 | + |
| 24 | +# This class is an internal implementation detail and is not exposed to the |
| 25 | +# end-user. It should not be used directly by external code. Changes to this |
| 26 | +# class will not be considered breaking changes to the public API. |
| 27 | +class AsyncToolboxClient: |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + url: str, |
| 32 | + session: ClientSession, |
| 33 | + ): |
| 34 | + """ |
| 35 | + Initializes the AsyncToolboxClient for the Toolbox service at the given URL. |
| 36 | +
|
| 37 | + Args: |
| 38 | + url: The base URL of the Toolbox service. |
| 39 | + session: An HTTP client session. |
| 40 | + """ |
| 41 | + self.__url = url |
| 42 | + self.__session = session |
| 43 | + |
| 44 | + async def aload_tool( |
| 45 | + self, |
| 46 | + tool_name: str, |
| 47 | + auth_tokens: dict[str, Callable[[], str]] = {}, |
| 48 | + auth_headers: Optional[dict[str, Callable[[], str]]] = None, |
| 49 | + bound_params: dict[str, Union[Any, Callable[[], Any]]] = {}, |
| 50 | + strict: bool = True, |
| 51 | + ) -> AsyncToolboxTool: |
| 52 | + """ |
| 53 | + Loads the tool with the given tool name from the Toolbox service. |
| 54 | +
|
| 55 | + Args: |
| 56 | + tool_name: The name of the tool to load. |
| 57 | + auth_tokens: An optional mapping of authentication source names to |
| 58 | + functions that retrieve ID tokens. |
| 59 | + auth_headers: Deprecated. Use `auth_tokens` instead. |
| 60 | + bound_params: An optional mapping of parameter names to their |
| 61 | + bound values. |
| 62 | + strict: If True, raises a ValueError if any of the given bound |
| 63 | + parameters are missing from the schema or require |
| 64 | + authentication. If False, only issues a warning. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + A tool loaded from the Toolbox. |
| 68 | + """ |
| 69 | + if auth_headers: |
| 70 | + if auth_tokens: |
| 71 | + warn( |
| 72 | + "Both `auth_tokens` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_tokens` will be used.", |
| 73 | + DeprecationWarning, |
| 74 | + ) |
| 75 | + else: |
| 76 | + warn( |
| 77 | + "Argument `auth_headers` is deprecated. Use `auth_tokens` instead.", |
| 78 | + DeprecationWarning, |
| 79 | + ) |
| 80 | + auth_tokens = auth_headers |
| 81 | + |
| 82 | + url = f"{self.__url}/api/tool/{tool_name}" |
| 83 | + manifest: ManifestSchema = await _load_manifest(url, self.__session) |
| 84 | + |
| 85 | + return AsyncToolboxTool( |
| 86 | + tool_name, |
| 87 | + manifest.tools[tool_name], |
| 88 | + self.__url, |
| 89 | + self.__session, |
| 90 | + auth_tokens, |
| 91 | + bound_params, |
| 92 | + strict, |
| 93 | + ) |
| 94 | + |
| 95 | + async def aload_toolset( |
| 96 | + self, |
| 97 | + toolset_name: Optional[str] = None, |
| 98 | + auth_tokens: dict[str, Callable[[], str]] = {}, |
| 99 | + auth_headers: Optional[dict[str, Callable[[], str]]] = None, |
| 100 | + bound_params: dict[str, Union[Any, Callable[[], Any]]] = {}, |
| 101 | + strict: bool = True, |
| 102 | + ) -> list[AsyncToolboxTool]: |
| 103 | + """ |
| 104 | + Loads tools from the Toolbox service, optionally filtered by toolset |
| 105 | + name. |
| 106 | +
|
| 107 | + Args: |
| 108 | + toolset_name: The name of the toolset to load. If not provided, |
| 109 | + all tools are loaded. |
| 110 | + auth_tokens: An optional mapping of authentication source names to |
| 111 | + functions that retrieve ID tokens. |
| 112 | + auth_headers: Deprecated. Use `auth_tokens` instead. |
| 113 | + bound_params: An optional mapping of parameter names to their |
| 114 | + bound values. |
| 115 | + strict: If True, raises a ValueError if any of the given bound |
| 116 | + parameters are missing from the schema or require |
| 117 | + authentication. If False, only issues a warning. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + A list of all tools loaded from the Toolbox. |
| 121 | + """ |
| 122 | + if auth_headers: |
| 123 | + if auth_tokens: |
| 124 | + warn( |
| 125 | + "Both `auth_tokens` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_tokens` will be used.", |
| 126 | + DeprecationWarning, |
| 127 | + ) |
| 128 | + else: |
| 129 | + warn( |
| 130 | + "Argument `auth_headers` is deprecated. Use `auth_tokens` instead.", |
| 131 | + DeprecationWarning, |
| 132 | + ) |
| 133 | + auth_tokens = auth_headers |
| 134 | + |
| 135 | + url = f"{self.__url}/api/toolset/{toolset_name or ''}" |
| 136 | + manifest: ManifestSchema = await _load_manifest(url, self.__session) |
| 137 | + tools: list[AsyncToolboxTool] = [] |
| 138 | + |
| 139 | + for tool_name, tool_schema in manifest.tools.items(): |
| 140 | + tools.append( |
| 141 | + AsyncToolboxTool( |
| 142 | + tool_name, |
| 143 | + tool_schema, |
| 144 | + self.__url, |
| 145 | + self.__session, |
| 146 | + auth_tokens, |
| 147 | + bound_params, |
| 148 | + strict, |
| 149 | + ) |
| 150 | + ) |
| 151 | + return tools |
| 152 | + |
| 153 | + def load_tool( |
| 154 | + self, |
| 155 | + tool_name: str, |
| 156 | + auth_tokens: dict[str, Callable[[], str]] = {}, |
| 157 | + auth_headers: Optional[dict[str, Callable[[], str]]] = None, |
| 158 | + bound_params: dict[str, Union[Any, Callable[[], Any]]] = {}, |
| 159 | + strict: bool = True, |
| 160 | + ) -> AsyncToolboxTool: |
| 161 | + raise NotImplementedError("Synchronous methods not supported by async client.") |
| 162 | + |
| 163 | + def load_toolset( |
| 164 | + self, |
| 165 | + toolset_name: Optional[str] = None, |
| 166 | + auth_tokens: dict[str, Callable[[], str]] = {}, |
| 167 | + auth_headers: Optional[dict[str, Callable[[], str]]] = None, |
| 168 | + bound_params: dict[str, Union[Any, Callable[[], Any]]] = {}, |
| 169 | + strict: bool = True, |
| 170 | + ) -> list[AsyncToolboxTool]: |
| 171 | + raise NotImplementedError("Synchronous methods not supported by async client.") |
0 commit comments