16
16
from warnings import warn
17
17
18
18
from aiohttp import ClientSession
19
+ from toolbox_core .client import ToolboxClient as ToolboxCoreClient
19
20
20
- from .tools import AsyncToolboxTool
21
- from .utils import ManifestSchema , _load_manifest
21
+ from .async_tools import AsyncToolboxTool
22
22
23
23
24
24
# This class is an internal implementation detail and is not exposed to the
@@ -38,67 +38,72 @@ def __init__(
38
38
url: The base URL of the Toolbox service.
39
39
session: An HTTP client session.
40
40
"""
41
- self .__url = url
42
- self .__session = session
41
+ self .__core_client = ToolboxCoreClient (url = url , session = session )
43
42
44
43
async def aload_tool (
45
44
self ,
46
45
tool_name : str ,
47
- auth_tokens : dict [str , Callable [[], str ]] = {},
46
+ auth_token_getters : dict [str , Callable [[], str ]] = {},
47
+ auth_tokens : Optional [dict [str , Callable [[], str ]]] = None ,
48
48
auth_headers : Optional [dict [str , Callable [[], str ]]] = None ,
49
49
bound_params : dict [str , Union [Any , Callable [[], Any ]]] = {},
50
- strict : bool = True ,
51
50
) -> AsyncToolboxTool :
52
51
"""
53
52
Loads the tool with the given tool name from the Toolbox service.
54
53
55
54
Args:
56
55
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.
56
+ auth_token_getters: An optional mapping of authentication source
57
+ names to functions that retrieve ID tokens.
58
+ auth_tokens: Deprecated. Use `auth_token_getters` instead.
59
+ auth_headers: Deprecated. Use `auth_token_getters` instead.
60
60
bound_params: An optional mapping of parameter names to their
61
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
62
66
63
Returns:
67
64
A tool loaded from the Toolbox.
68
65
"""
66
+ if auth_tokens :
67
+ if auth_token_getters :
68
+ warn (
69
+ "Both `auth_token_getters` and `auth_tokens` are provided. `auth_tokens` is deprecated, and `auth_token_getters` will be used." ,
70
+ DeprecationWarning ,
71
+ )
72
+ else :
73
+ warn (
74
+ "Argument `auth_tokens` is deprecated. Use `auth_token_getters` instead." ,
75
+ DeprecationWarning ,
76
+ )
77
+ auth_token_getters = auth_tokens
78
+
69
79
if auth_headers :
70
- if auth_tokens :
80
+ if auth_token_getters :
71
81
warn (
72
- "Both `auth_tokens ` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_tokens ` will be used." ,
82
+ "Both `auth_token_getters ` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_token_getters ` will be used." ,
73
83
DeprecationWarning ,
74
84
)
75
85
else :
76
86
warn (
77
- "Argument `auth_headers` is deprecated. Use `auth_tokens ` instead." ,
87
+ "Argument `auth_headers` is deprecated. Use `auth_token_getters ` instead." ,
78
88
DeprecationWarning ,
79
89
)
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 ,
90
+ auth_token_getters = auth_headers
91
+
92
+ core_tool = await self .__core_client .load_tool (
93
+ name = tool_name ,
94
+ auth_token_getters = auth_token_getters ,
95
+ bound_params = bound_params ,
93
96
)
97
+ return AsyncToolboxTool (core_tool = core_tool )
94
98
95
99
async def aload_toolset (
96
100
self ,
97
101
toolset_name : Optional [str ] = None ,
98
- auth_tokens : dict [str , Callable [[], str ]] = {},
102
+ auth_token_getters : dict [str , Callable [[], str ]] = {},
103
+ auth_tokens : Optional [dict [str , Callable [[], str ]]] = None ,
99
104
auth_headers : Optional [dict [str , Callable [[], str ]]] = None ,
100
105
bound_params : dict [str , Union [Any , Callable [[], Any ]]] = {},
101
- strict : bool = True ,
106
+ strict : bool = False ,
102
107
) -> list [AsyncToolboxTool ]:
103
108
"""
104
109
Loads tools from the Toolbox service, optionally filtered by toolset
@@ -107,65 +112,76 @@ async def aload_toolset(
107
112
Args:
108
113
toolset_name: The name of the toolset to load. If not provided,
109
114
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.
115
+ auth_token_getters: An optional mapping of authentication source
116
+ names to functions that retrieve ID tokens.
117
+ auth_tokens: Deprecated. Use `auth_token_getters` instead.
118
+ auth_headers: Deprecated. Use `auth_token_getters` instead.
113
119
bound_params: An optional mapping of parameter names to their
114
120
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.
121
+ strict: If True, raises an error if *any* loaded tool instance fails
122
+ to utilize at least one provided parameter or auth token (if any
123
+ provided). If False (default), raises an error only if a
124
+ user-provided parameter or auth token cannot be applied to *any*
125
+ loaded tool across the set.
118
126
119
127
Returns:
120
128
A list of all tools loaded from the Toolbox.
121
129
"""
122
- if auth_headers :
123
- if auth_tokens :
130
+ if auth_tokens :
131
+ if auth_token_getters :
124
132
warn (
125
- "Both `auth_tokens ` and `auth_headers ` are provided. `auth_headers ` is deprecated, and `auth_tokens ` will be used." ,
133
+ "Both `auth_token_getters ` and `auth_tokens ` are provided. `auth_tokens ` is deprecated, and `auth_token_getters ` will be used." ,
126
134
DeprecationWarning ,
127
135
)
128
136
else :
129
137
warn (
130
- "Argument `auth_headers ` is deprecated. Use `auth_tokens ` instead." ,
138
+ "Argument `auth_tokens ` is deprecated. Use `auth_token_getters ` instead." ,
131
139
DeprecationWarning ,
132
140
)
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 ,
141
+ auth_token_getters = auth_tokens
142
+
143
+ if auth_headers :
144
+ if auth_token_getters :
145
+ warn (
146
+ "Both `auth_token_getters` and `auth_headers` are provided. `auth_headers` is deprecated, and `auth_token_getters` will be used." ,
147
+ DeprecationWarning ,
148
+ )
149
+ else :
150
+ warn (
151
+ "Argument `auth_headers` is deprecated. Use `auth_token_getters` instead." ,
152
+ DeprecationWarning ,
149
153
)
150
- )
154
+ auth_token_getters = auth_headers
155
+
156
+ core_tools = await self .__core_client .load_toolset (
157
+ name = toolset_name ,
158
+ auth_token_getters = auth_token_getters ,
159
+ bound_params = bound_params ,
160
+ strict = strict ,
161
+ )
162
+
163
+ tools = []
164
+ for core_tool in core_tools :
165
+ tools .append (AsyncToolboxTool (core_tool = core_tool ))
151
166
return tools
152
167
153
168
def load_tool (
154
169
self ,
155
170
tool_name : str ,
156
- auth_tokens : dict [str , Callable [[], str ]] = {},
171
+ auth_token_getters : dict [str , Callable [[], str ]] = {},
172
+ auth_tokens : Optional [dict [str , Callable [[], str ]]] = None ,
157
173
auth_headers : Optional [dict [str , Callable [[], str ]]] = None ,
158
174
bound_params : dict [str , Union [Any , Callable [[], Any ]]] = {},
159
- strict : bool = True ,
160
175
) -> AsyncToolboxTool :
161
176
raise NotImplementedError ("Synchronous methods not supported by async client." )
162
177
163
178
def load_toolset (
164
179
self ,
165
180
toolset_name : Optional [str ] = None ,
166
- auth_tokens : dict [str , Callable [[], str ]] = {},
181
+ auth_token_getters : dict [str , Callable [[], str ]] = {},
182
+ auth_tokens : Optional [dict [str , Callable [[], str ]]] = None ,
167
183
auth_headers : Optional [dict [str , Callable [[], str ]]] = None ,
168
184
bound_params : dict [str , Union [Any , Callable [[], Any ]]] = {},
169
- strict : bool = True ,
185
+ strict : bool = False ,
170
186
) -> list [AsyncToolboxTool ]:
171
187
raise NotImplementedError ("Synchronous methods not supported by async client." )
0 commit comments