langchain-arcade 1.4.4__py3-none-any.whl → 2.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_arcade/__init__.py +16 -6
- langchain_arcade-2.0.1.dist-info/METADATA +54 -0
- langchain_arcade-2.0.1.dist-info/RECORD +6 -0
- {langchain_arcade-1.4.4.dist-info → langchain_arcade-2.0.1.dist-info}/WHEEL +1 -1
- langchain_arcade/_utilities.py +0 -313
- langchain_arcade/manager.py +0 -848
- langchain_arcade-1.4.4.dist-info/METADATA +0 -195
- langchain_arcade-1.4.4.dist-info/RECORD +0 -8
- {langchain_arcade-1.4.4.dist-info → langchain_arcade-2.0.1.dist-info}/licenses/LICENSE +0 -0
langchain_arcade/manager.py
DELETED
|
@@ -1,848 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import warnings
|
|
3
|
-
from collections.abc import Iterator
|
|
4
|
-
from typing import Any, Optional, Union
|
|
5
|
-
|
|
6
|
-
from arcadepy import NOT_GIVEN, Arcade, AsyncArcade
|
|
7
|
-
from arcadepy.types import ToolDefinition
|
|
8
|
-
from arcadepy.types.shared import AuthorizationResponse
|
|
9
|
-
from langchain_core.tools import StructuredTool
|
|
10
|
-
|
|
11
|
-
from langchain_arcade._utilities import wrap_arcade_tool
|
|
12
|
-
|
|
13
|
-
ClientType = Union[Arcade, AsyncArcade]
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class LangChainToolManager:
|
|
17
|
-
"""
|
|
18
|
-
Base tool manager for LangChain framework.
|
|
19
|
-
Provides a common interface for both synchronous and asynchronous tool managers.
|
|
20
|
-
|
|
21
|
-
This class handles the storage and retrieval of tool definitions and provides
|
|
22
|
-
common functionality used by both synchronous and asynchronous implementations.
|
|
23
|
-
"""
|
|
24
|
-
|
|
25
|
-
def __init__(self) -> None:
|
|
26
|
-
self._tools: dict[str, ToolDefinition] = {}
|
|
27
|
-
|
|
28
|
-
@property
|
|
29
|
-
def tools(self) -> list[str]:
|
|
30
|
-
"""
|
|
31
|
-
Get the list of tools by name in the manager.
|
|
32
|
-
|
|
33
|
-
Returns:
|
|
34
|
-
A list of tool names (strings) currently stored in the manager.
|
|
35
|
-
"""
|
|
36
|
-
return list(self._tools.keys())
|
|
37
|
-
|
|
38
|
-
def __len__(self) -> int:
|
|
39
|
-
"""Return the number of tools in the manager."""
|
|
40
|
-
return len(self._tools)
|
|
41
|
-
|
|
42
|
-
def _get_client_config(self, **kwargs: Any) -> dict[str, Any]:
|
|
43
|
-
"""
|
|
44
|
-
Get the client configurations from environment variables and kwargs.
|
|
45
|
-
|
|
46
|
-
If api_key or base_url are in the kwargs, they will be used.
|
|
47
|
-
Otherwise, the environment variables ARCADE_API_KEY and ARCADE_BASE_URL will be used.
|
|
48
|
-
If both are provided, the kwargs will take precedence.
|
|
49
|
-
|
|
50
|
-
Args:
|
|
51
|
-
**kwargs: Keyword arguments that may contain api_key and base_url.
|
|
52
|
-
|
|
53
|
-
Returns:
|
|
54
|
-
A dictionary of client configuration parameters.
|
|
55
|
-
"""
|
|
56
|
-
client_kwargs = {
|
|
57
|
-
"api_key": kwargs.get("api_key", os.getenv("ARCADE_API_KEY")),
|
|
58
|
-
}
|
|
59
|
-
base_url = kwargs.get("base_url", os.getenv("ARCADE_BASE_URL"))
|
|
60
|
-
if base_url:
|
|
61
|
-
client_kwargs["base_url"] = base_url
|
|
62
|
-
return client_kwargs
|
|
63
|
-
|
|
64
|
-
def _get_tool_definition(self, tool_name: str) -> ToolDefinition:
|
|
65
|
-
"""
|
|
66
|
-
Get a tool definition by name, raising an error if not found.
|
|
67
|
-
|
|
68
|
-
Args:
|
|
69
|
-
tool_name: The name of the tool to retrieve.
|
|
70
|
-
|
|
71
|
-
Returns:
|
|
72
|
-
The ToolDefinition for the specified tool.
|
|
73
|
-
|
|
74
|
-
Raises:
|
|
75
|
-
ValueError: If the tool is not found in the manager.
|
|
76
|
-
"""
|
|
77
|
-
try:
|
|
78
|
-
return self._tools[tool_name]
|
|
79
|
-
except KeyError:
|
|
80
|
-
raise ValueError(f"Tool '{tool_name}' not found in this manager instance")
|
|
81
|
-
|
|
82
|
-
def __getitem__(self, tool_name: str) -> ToolDefinition:
|
|
83
|
-
"""
|
|
84
|
-
Get a tool definition by name using dictionary-like access.
|
|
85
|
-
|
|
86
|
-
Args:
|
|
87
|
-
tool_name: The name of the tool to retrieve.
|
|
88
|
-
|
|
89
|
-
Returns:
|
|
90
|
-
The ToolDefinition for the specified tool.
|
|
91
|
-
|
|
92
|
-
Raises:
|
|
93
|
-
ValueError: If the tool is not found in the manager.
|
|
94
|
-
"""
|
|
95
|
-
return self._get_tool_definition(tool_name)
|
|
96
|
-
|
|
97
|
-
def requires_auth(self, tool_name: str) -> bool:
|
|
98
|
-
"""
|
|
99
|
-
Check if a tool requires authorization.
|
|
100
|
-
|
|
101
|
-
Args:
|
|
102
|
-
tool_name: The name of the tool to check.
|
|
103
|
-
|
|
104
|
-
Returns:
|
|
105
|
-
True if the tool requires authorization, False otherwise.
|
|
106
|
-
"""
|
|
107
|
-
tool_def = self._get_tool_definition(tool_name)
|
|
108
|
-
if tool_def.requirements is None:
|
|
109
|
-
return False
|
|
110
|
-
return tool_def.requirements.authorization is not None
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
class ToolManager(LangChainToolManager):
|
|
114
|
-
"""
|
|
115
|
-
Synchronous Arcade tool manager for LangChain framework.
|
|
116
|
-
|
|
117
|
-
This class wraps Arcade tools as LangChain StructuredTool objects for integration
|
|
118
|
-
with synchronous operations.
|
|
119
|
-
|
|
120
|
-
Example:
|
|
121
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
122
|
-
>>> # Initialize with specific tools and toolkits
|
|
123
|
-
>>> manager.init_tools(tools=["Search.SearchGoogle"], toolkits=["Weather"])
|
|
124
|
-
>>> # Get tools as LangChain StructuredTools
|
|
125
|
-
>>> langchain_tools = manager.to_langchain()
|
|
126
|
-
>>> # Handle authorization for tools that require it
|
|
127
|
-
>>> if manager.requires_auth("Search.SearchGoogle"):
|
|
128
|
-
>>> auth_response = manager.authorize("Search.SearchGoogle", "user_123")
|
|
129
|
-
>>> manager.wait_for_auth(auth_response.id)
|
|
130
|
-
"""
|
|
131
|
-
|
|
132
|
-
def __init__(self, client: Optional[Arcade] = None, **kwargs: Any) -> None:
|
|
133
|
-
"""
|
|
134
|
-
Initialize the ToolManager.
|
|
135
|
-
|
|
136
|
-
Example:
|
|
137
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
138
|
-
>>> # or with an existing client
|
|
139
|
-
>>> client = Arcade(api_key="your-api-key")
|
|
140
|
-
>>> manager = ToolManager(client=client)
|
|
141
|
-
|
|
142
|
-
Args:
|
|
143
|
-
client: Optional Arcade client instance. If not provided, one will be created.
|
|
144
|
-
**kwargs: Additional keyword arguments to pass to the Arcade client if creating one.
|
|
145
|
-
Common options include api_key and base_url.
|
|
146
|
-
"""
|
|
147
|
-
super().__init__()
|
|
148
|
-
if client is None:
|
|
149
|
-
client_kwargs = self._get_client_config(**kwargs)
|
|
150
|
-
client = Arcade(**client_kwargs)
|
|
151
|
-
self._client = client
|
|
152
|
-
|
|
153
|
-
@property
|
|
154
|
-
def definitions(self) -> list[ToolDefinition]:
|
|
155
|
-
"""
|
|
156
|
-
Get the list of tool definitions in the manager.
|
|
157
|
-
|
|
158
|
-
Returns:
|
|
159
|
-
A list of ToolDefinition objects currently stored in the manager.
|
|
160
|
-
"""
|
|
161
|
-
return list(self._tools.values())
|
|
162
|
-
|
|
163
|
-
def __iter__(self) -> Iterator[tuple[str, ToolDefinition]]:
|
|
164
|
-
"""
|
|
165
|
-
Iterate over the tools in the manager as (name, definition) pairs.
|
|
166
|
-
|
|
167
|
-
Returns:
|
|
168
|
-
Iterator over (tool_name, tool_definition) tuples.
|
|
169
|
-
"""
|
|
170
|
-
yield from self._tools.items()
|
|
171
|
-
|
|
172
|
-
def to_langchain(
|
|
173
|
-
self, use_interrupts: bool = True, use_underscores: bool = True
|
|
174
|
-
) -> list[StructuredTool]:
|
|
175
|
-
"""
|
|
176
|
-
Get the tools in the manager as LangChain StructuredTool objects.
|
|
177
|
-
|
|
178
|
-
Args:
|
|
179
|
-
use_interrupts: Whether to use interrupts for the tool. This is useful
|
|
180
|
-
for LangGraph workflows where you need to handle tool
|
|
181
|
-
authorization through state transitions.
|
|
182
|
-
use_underscores: Whether to use underscores for the tool name instead of periods.
|
|
183
|
-
For example, "Search_SearchGoogle" vs "Search.SearchGoogle".
|
|
184
|
-
Some model providers like OpenAI work better with underscores.
|
|
185
|
-
|
|
186
|
-
Returns:
|
|
187
|
-
List of StructuredTool instances ready to use with LangChain.
|
|
188
|
-
"""
|
|
189
|
-
tool_map = _create_tool_map(self.definitions, use_underscores=use_underscores)
|
|
190
|
-
return [
|
|
191
|
-
wrap_arcade_tool(
|
|
192
|
-
self._client, tool_name, definition, langgraph=use_interrupts
|
|
193
|
-
)
|
|
194
|
-
for tool_name, definition in tool_map.items()
|
|
195
|
-
]
|
|
196
|
-
|
|
197
|
-
def init_tools(
|
|
198
|
-
self,
|
|
199
|
-
tools: Optional[list[str]] = None,
|
|
200
|
-
toolkits: Optional[list[str]] = None,
|
|
201
|
-
limit: Optional[int] = None,
|
|
202
|
-
offset: Optional[int] = None,
|
|
203
|
-
raise_on_empty: bool = True,
|
|
204
|
-
) -> list[StructuredTool]:
|
|
205
|
-
"""
|
|
206
|
-
Initialize the tools in the manager and return them as LangChain tools.
|
|
207
|
-
|
|
208
|
-
This will clear any existing tools in the manager and replace them with
|
|
209
|
-
the new tools specified by the tools and toolkits parameters.
|
|
210
|
-
|
|
211
|
-
Note: In version 2.0+, this method returns a list of StructuredTool objects.
|
|
212
|
-
In earlier versions, it returned None.
|
|
213
|
-
|
|
214
|
-
Example:
|
|
215
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
216
|
-
>>> langchain_tools = manager.init_tools(tools=["Search.SearchGoogle"])
|
|
217
|
-
>>> # Use these tools with a LangChain chain or agent
|
|
218
|
-
>>> agent = Agent(tools=langchain_tools, llm=llm)
|
|
219
|
-
|
|
220
|
-
Args:
|
|
221
|
-
tools: Optional list of specific tool names to include (e.g., "Search.SearchGoogle").
|
|
222
|
-
toolkits: Optional list of toolkit names to include all tools from (e.g., "Search").
|
|
223
|
-
limit: Optional limit on the number of tools to retrieve per request.
|
|
224
|
-
offset: Optional offset for paginated requests.
|
|
225
|
-
raise_on_empty: Whether to raise an error if no tools or toolkits are provided.
|
|
226
|
-
|
|
227
|
-
Returns:
|
|
228
|
-
List of StructuredTool instances ready to use with LangChain.
|
|
229
|
-
|
|
230
|
-
Raises:
|
|
231
|
-
ValueError: If no tools or toolkits are provided and raise_on_empty is True.
|
|
232
|
-
"""
|
|
233
|
-
tools_list = self._retrieve_tool_definitions(
|
|
234
|
-
tools, toolkits, raise_on_empty, limit, offset
|
|
235
|
-
)
|
|
236
|
-
self._tools = _create_tool_map(tools_list)
|
|
237
|
-
return self.to_langchain()
|
|
238
|
-
|
|
239
|
-
def authorize(self, tool_name: str, user_id: str) -> AuthorizationResponse:
|
|
240
|
-
"""
|
|
241
|
-
Authorize a user for a specific tool.
|
|
242
|
-
|
|
243
|
-
Example:
|
|
244
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
245
|
-
>>> manager.init_tools(tools=["Gmail.SendEmail"])
|
|
246
|
-
>>> auth_response = manager.authorize("Gmail.SendEmail", "user_123")
|
|
247
|
-
>>> # auth_response.auth_url contains the URL for the user to authorize
|
|
248
|
-
|
|
249
|
-
Args:
|
|
250
|
-
tool_name: The name of the tool to authorize.
|
|
251
|
-
user_id: The user ID to authorize. This should be a unique identifier for the user.
|
|
252
|
-
|
|
253
|
-
Returns:
|
|
254
|
-
AuthorizationResponse containing authorization details, including the auth_url
|
|
255
|
-
that should be presented to the user to complete authorization.
|
|
256
|
-
"""
|
|
257
|
-
return self._client.tools.authorize(tool_name=tool_name, user_id=user_id)
|
|
258
|
-
|
|
259
|
-
def is_authorized(self, authorization_id: str) -> bool:
|
|
260
|
-
"""
|
|
261
|
-
Check if a tool authorization is complete.
|
|
262
|
-
|
|
263
|
-
Example:
|
|
264
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
265
|
-
>>> auth_response = manager.authorize("Gmail.SendEmail", "user_123")
|
|
266
|
-
>>> # After user completes authorization
|
|
267
|
-
>>> is_complete = manager.is_authorized(auth_response.id)
|
|
268
|
-
|
|
269
|
-
Args:
|
|
270
|
-
authorization_id: The authorization ID to check. This can be the full AuthorizationResponse
|
|
271
|
-
object or just the ID string.
|
|
272
|
-
|
|
273
|
-
Returns:
|
|
274
|
-
True if the authorization is completed, False otherwise.
|
|
275
|
-
"""
|
|
276
|
-
# Handle case where entire AuthorizationResponse object is passed
|
|
277
|
-
if hasattr(authorization_id, "id"):
|
|
278
|
-
authorization_id = authorization_id.id
|
|
279
|
-
|
|
280
|
-
response = self._client.auth.status(id=authorization_id)
|
|
281
|
-
if response:
|
|
282
|
-
return response.status == "completed"
|
|
283
|
-
return False
|
|
284
|
-
|
|
285
|
-
def wait_for_auth(self, authorization_id: str) -> AuthorizationResponse:
|
|
286
|
-
"""
|
|
287
|
-
Wait for a tool authorization to complete. This method blocks until
|
|
288
|
-
the authorization is complete or fails.
|
|
289
|
-
|
|
290
|
-
Example:
|
|
291
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
292
|
-
>>> auth_response = manager.authorize("Gmail.SendEmail", "user_123")
|
|
293
|
-
>>> # Share auth_response.auth_url with the user
|
|
294
|
-
>>> # Wait for the user to complete authorization
|
|
295
|
-
>>> completed_auth = manager.wait_for_auth(auth_response.id)
|
|
296
|
-
|
|
297
|
-
Args:
|
|
298
|
-
authorization_id: The authorization ID to wait for. This can be the full
|
|
299
|
-
AuthorizationResponse object or just the ID string.
|
|
300
|
-
|
|
301
|
-
Returns:
|
|
302
|
-
AuthorizationResponse with the completed authorization details.
|
|
303
|
-
"""
|
|
304
|
-
# Handle case where entire AuthorizationResponse object is passed
|
|
305
|
-
if hasattr(authorization_id, "id"):
|
|
306
|
-
authorization_id = authorization_id.id
|
|
307
|
-
|
|
308
|
-
return self._client.auth.wait_for_completion(authorization_id)
|
|
309
|
-
|
|
310
|
-
def _retrieve_tool_definitions(
|
|
311
|
-
self,
|
|
312
|
-
tools: Optional[list[str]] = None,
|
|
313
|
-
toolkits: Optional[list[str]] = None,
|
|
314
|
-
raise_on_empty: bool = True,
|
|
315
|
-
limit: Optional[int] = None,
|
|
316
|
-
offset: Optional[int] = None,
|
|
317
|
-
) -> list[ToolDefinition]:
|
|
318
|
-
"""
|
|
319
|
-
Retrieve tool definitions from the Arcade client, accounting for pagination.
|
|
320
|
-
|
|
321
|
-
Args:
|
|
322
|
-
tools: Optional list of specific tool names to include.
|
|
323
|
-
toolkits: Optional list of toolkit names to include all tools from.
|
|
324
|
-
raise_on_empty: Whether to raise an error if no tools or toolkits are provided.
|
|
325
|
-
limit: Optional limit on the number of tools to retrieve per request.
|
|
326
|
-
offset: Optional offset for paginated requests.
|
|
327
|
-
|
|
328
|
-
Returns:
|
|
329
|
-
List of ToolDefinition instances.
|
|
330
|
-
|
|
331
|
-
Raises:
|
|
332
|
-
ValueError: If no tools or toolkits are provided and raise_on_empty is True.
|
|
333
|
-
"""
|
|
334
|
-
all_tools: list[ToolDefinition] = []
|
|
335
|
-
|
|
336
|
-
# If no specific tools or toolkits are requested, raise an error.
|
|
337
|
-
if not tools and not toolkits:
|
|
338
|
-
if raise_on_empty:
|
|
339
|
-
raise ValueError(
|
|
340
|
-
"No tools or toolkits provided to retrieve tool definitions."
|
|
341
|
-
)
|
|
342
|
-
return []
|
|
343
|
-
|
|
344
|
-
# Retrieve individual tools if specified
|
|
345
|
-
if tools:
|
|
346
|
-
for tool_id in tools:
|
|
347
|
-
single_tool = self._client.tools.get(name=tool_id)
|
|
348
|
-
all_tools.append(single_tool)
|
|
349
|
-
|
|
350
|
-
# Retrieve tools from specified toolkits
|
|
351
|
-
if toolkits:
|
|
352
|
-
for tk in toolkits:
|
|
353
|
-
# Convert None to NOT_GIVEN for Stainless client
|
|
354
|
-
paginated_tools = self._client.tools.list(
|
|
355
|
-
toolkit=tk,
|
|
356
|
-
limit=limit if limit is not None else NOT_GIVEN,
|
|
357
|
-
offset=offset if offset is not None else NOT_GIVEN,
|
|
358
|
-
)
|
|
359
|
-
all_tools.extend(paginated_tools)
|
|
360
|
-
|
|
361
|
-
return all_tools
|
|
362
|
-
|
|
363
|
-
def add_tool(self, tool_name: str) -> None:
|
|
364
|
-
"""
|
|
365
|
-
Add a single tool to the manager by name.
|
|
366
|
-
|
|
367
|
-
Unlike init_tools(), this method preserves existing tools in the manager
|
|
368
|
-
and only adds the specified tool.
|
|
369
|
-
|
|
370
|
-
Example:
|
|
371
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
372
|
-
>>> manager.add_tool("Gmail.SendEmail")
|
|
373
|
-
>>> manager.add_tool("Search.SearchGoogle")
|
|
374
|
-
>>> # Get all tools including newly added ones
|
|
375
|
-
>>> all_tools = manager.to_langchain()
|
|
376
|
-
|
|
377
|
-
Args:
|
|
378
|
-
tool_name: The fully qualified name of the tool to add (e.g., "Search.SearchGoogle")
|
|
379
|
-
|
|
380
|
-
Raises:
|
|
381
|
-
ValueError: If the tool cannot be found
|
|
382
|
-
"""
|
|
383
|
-
tool = self._client.tools.get(name=tool_name)
|
|
384
|
-
self._tools.update(_create_tool_map([tool]))
|
|
385
|
-
|
|
386
|
-
def add_toolkit(
|
|
387
|
-
self,
|
|
388
|
-
toolkit_name: str,
|
|
389
|
-
limit: Optional[int] = None,
|
|
390
|
-
offset: Optional[int] = None,
|
|
391
|
-
) -> None:
|
|
392
|
-
"""
|
|
393
|
-
Add all tools from a specific toolkit to the manager.
|
|
394
|
-
|
|
395
|
-
Unlike init_tools(), this method preserves existing tools in the manager
|
|
396
|
-
and only adds the tools from the specified toolkit.
|
|
397
|
-
|
|
398
|
-
Example:
|
|
399
|
-
>>> manager = ToolManager(api_key="your-api-key")
|
|
400
|
-
>>> manager.add_toolkit("Gmail")
|
|
401
|
-
>>> manager.add_toolkit("Search")
|
|
402
|
-
>>> # Get all tools including newly added ones
|
|
403
|
-
>>> all_tools = manager.to_langchain()
|
|
404
|
-
|
|
405
|
-
Args:
|
|
406
|
-
toolkit_name: The name of the toolkit to add (e.g., "Search")
|
|
407
|
-
limit: Optional limit on the number of tools to retrieve per request
|
|
408
|
-
offset: Optional offset for paginated requests
|
|
409
|
-
|
|
410
|
-
Raises:
|
|
411
|
-
ValueError: If the toolkit cannot be found or has no tools
|
|
412
|
-
"""
|
|
413
|
-
tools = self._client.tools.list(
|
|
414
|
-
toolkit=toolkit_name,
|
|
415
|
-
limit=NOT_GIVEN if limit is None else limit,
|
|
416
|
-
offset=NOT_GIVEN if offset is None else offset,
|
|
417
|
-
)
|
|
418
|
-
|
|
419
|
-
for tool in tools:
|
|
420
|
-
self._tools.update(_create_tool_map([tool]))
|
|
421
|
-
|
|
422
|
-
def get_tools(
|
|
423
|
-
self,
|
|
424
|
-
tools: Optional[list[str]] = None,
|
|
425
|
-
toolkits: Optional[list[str]] = None,
|
|
426
|
-
langgraph: bool = True,
|
|
427
|
-
) -> list[StructuredTool]:
|
|
428
|
-
"""
|
|
429
|
-
DEPRECATED: Return the tools in the manager as LangChain StructuredTool objects.
|
|
430
|
-
|
|
431
|
-
This method is deprecated and will be removed in a future major version.
|
|
432
|
-
Please use `init_tools()` to initialize tools and `to_langchain()` to convert them.
|
|
433
|
-
|
|
434
|
-
Args:
|
|
435
|
-
tools: Optional list of tool names to include.
|
|
436
|
-
toolkits: Optional list of toolkits to include.
|
|
437
|
-
langgraph: Whether to use LangGraph-specific behavior
|
|
438
|
-
such as NodeInterrupts for auth.
|
|
439
|
-
|
|
440
|
-
Returns:
|
|
441
|
-
List of StructuredTool instances.
|
|
442
|
-
"""
|
|
443
|
-
warnings.warn(
|
|
444
|
-
"get_tools() is deprecated and will be removed in the next major version. "
|
|
445
|
-
"Please use init_tools() to initialize tools and to_langchain() to convert them.",
|
|
446
|
-
DeprecationWarning,
|
|
447
|
-
stacklevel=2,
|
|
448
|
-
)
|
|
449
|
-
|
|
450
|
-
# Support existing usage pattern
|
|
451
|
-
if tools or toolkits:
|
|
452
|
-
self.init_tools(tools=tools, toolkits=toolkits)
|
|
453
|
-
|
|
454
|
-
return self.to_langchain(use_interrupts=langgraph)
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
class ArcadeToolManager(ToolManager):
|
|
458
|
-
"""
|
|
459
|
-
Deprecated alias for ToolManager.
|
|
460
|
-
|
|
461
|
-
ArcadeToolManager is deprecated and will be removed in the next major version.
|
|
462
|
-
Please use ToolManager instead.
|
|
463
|
-
"""
|
|
464
|
-
|
|
465
|
-
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
466
|
-
warnings.warn(
|
|
467
|
-
"ArcadeToolManager is deprecated and will be removed in the next major version. "
|
|
468
|
-
"Please use ToolManager instead.",
|
|
469
|
-
DeprecationWarning,
|
|
470
|
-
stacklevel=2,
|
|
471
|
-
)
|
|
472
|
-
super().__init__(*args, **kwargs)
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
class AsyncToolManager(LangChainToolManager):
|
|
476
|
-
"""
|
|
477
|
-
Async version of Arcade tool manager for LangChain framework.
|
|
478
|
-
|
|
479
|
-
This class wraps Arcade tools as LangChain StructuredTool objects for integration
|
|
480
|
-
with asynchronous operations.
|
|
481
|
-
|
|
482
|
-
Example:
|
|
483
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
484
|
-
>>> # Initialize with specific tools and toolkits
|
|
485
|
-
>>> await manager.init_tools(tools=["Search.SearchGoogle"], toolkits=["Weather"])
|
|
486
|
-
>>> # Get tools as LangChain StructuredTools
|
|
487
|
-
>>> langchain_tools = await manager.to_langchain()
|
|
488
|
-
>>> # Handle authorization for tools that require it
|
|
489
|
-
>>> if manager.requires_auth("Search.SearchGoogle"):
|
|
490
|
-
>>> auth_response = await manager.authorize("Search.SearchGoogle", "user_123")
|
|
491
|
-
>>> await manager.wait_for_auth(auth_response.id)
|
|
492
|
-
"""
|
|
493
|
-
|
|
494
|
-
def __init__(
|
|
495
|
-
self,
|
|
496
|
-
client: Optional[AsyncArcade] = None,
|
|
497
|
-
**kwargs: Any,
|
|
498
|
-
) -> None:
|
|
499
|
-
"""
|
|
500
|
-
Initialize the AsyncToolManager.
|
|
501
|
-
|
|
502
|
-
Example:
|
|
503
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
504
|
-
>>> # or with an existing client
|
|
505
|
-
>>> client = AsyncArcade(api_key="your-api-key")
|
|
506
|
-
>>> manager = AsyncToolManager(client=client)
|
|
507
|
-
|
|
508
|
-
Args:
|
|
509
|
-
client: Optional AsyncArcade client instance. If not provided, one will be created.
|
|
510
|
-
**kwargs: Additional keyword arguments to pass to the AsyncArcade client if creating one.
|
|
511
|
-
Common options include api_key and base_url.
|
|
512
|
-
"""
|
|
513
|
-
super().__init__()
|
|
514
|
-
if not client:
|
|
515
|
-
client_kwargs = self._get_client_config(**kwargs)
|
|
516
|
-
client = AsyncArcade(**client_kwargs)
|
|
517
|
-
self._client = client
|
|
518
|
-
|
|
519
|
-
@property
|
|
520
|
-
def definitions(self) -> list[ToolDefinition]:
|
|
521
|
-
"""
|
|
522
|
-
Get the list of tool definitions in the manager.
|
|
523
|
-
|
|
524
|
-
Returns:
|
|
525
|
-
A list of ToolDefinition objects currently stored in the manager.
|
|
526
|
-
"""
|
|
527
|
-
return list(self._tools.values())
|
|
528
|
-
|
|
529
|
-
def __iter__(self) -> Iterator[tuple[str, ToolDefinition]]:
|
|
530
|
-
"""
|
|
531
|
-
Iterate over the tools in the manager as (name, definition) pairs.
|
|
532
|
-
|
|
533
|
-
Returns:
|
|
534
|
-
Iterator over (tool_name, tool_definition) tuples.
|
|
535
|
-
"""
|
|
536
|
-
yield from self._tools.items()
|
|
537
|
-
|
|
538
|
-
async def init_tools(
|
|
539
|
-
self,
|
|
540
|
-
tools: Optional[list[str]] = None,
|
|
541
|
-
toolkits: Optional[list[str]] = None,
|
|
542
|
-
limit: Optional[int] = None,
|
|
543
|
-
offset: Optional[int] = None,
|
|
544
|
-
raise_on_empty: bool = True,
|
|
545
|
-
) -> list[StructuredTool]:
|
|
546
|
-
"""
|
|
547
|
-
Initialize the tools in the manager asynchronously and return them as LangChain tools.
|
|
548
|
-
|
|
549
|
-
This will clear any existing tools in the manager and replace them with
|
|
550
|
-
the new tools specified by the tools and toolkits parameters.
|
|
551
|
-
|
|
552
|
-
Example:
|
|
553
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
554
|
-
>>> langchain_tools = await manager.init_tools(tools=["Search.SearchGoogle"])
|
|
555
|
-
>>> # Use these tools with a LangChain chain or agent
|
|
556
|
-
>>> agent = Agent(tools=langchain_tools, llm=llm)
|
|
557
|
-
|
|
558
|
-
Args:
|
|
559
|
-
tools: Optional list of specific tool names to include (e.g., "Search.SearchGoogle").
|
|
560
|
-
toolkits: Optional list of toolkit names to include all tools from (e.g., "Search").
|
|
561
|
-
limit: Optional limit on the number of tools to retrieve per request.
|
|
562
|
-
offset: Optional offset for paginated requests.
|
|
563
|
-
raise_on_empty: Whether to raise an error if no tools or toolkits are provided.
|
|
564
|
-
|
|
565
|
-
Returns:
|
|
566
|
-
List of StructuredTool instances ready to use with LangChain.
|
|
567
|
-
|
|
568
|
-
Raises:
|
|
569
|
-
ValueError: If no tools or toolkits are provided and raise_on_empty is True.
|
|
570
|
-
"""
|
|
571
|
-
tools_list = await self._retrieve_tool_definitions(
|
|
572
|
-
tools, toolkits, raise_on_empty, limit, offset
|
|
573
|
-
)
|
|
574
|
-
self._tools.update(_create_tool_map(tools_list))
|
|
575
|
-
return await self.to_langchain()
|
|
576
|
-
|
|
577
|
-
async def to_langchain(
|
|
578
|
-
self, use_interrupts: bool = True, use_underscores: bool = True
|
|
579
|
-
) -> list[StructuredTool]:
|
|
580
|
-
"""
|
|
581
|
-
Get the tools in the manager as LangChain StructuredTool objects asynchronously.
|
|
582
|
-
|
|
583
|
-
Args:
|
|
584
|
-
use_interrupts: Whether to use interrupts for the tool. This is useful
|
|
585
|
-
for LangGraph workflows where you need to handle tool
|
|
586
|
-
authorization through state transitions.
|
|
587
|
-
use_underscores: Whether to use underscores for the tool name instead of periods.
|
|
588
|
-
For example, "Search_SearchGoogle" vs "Search.SearchGoogle".
|
|
589
|
-
Some model providers like OpenAI work better with underscores.
|
|
590
|
-
|
|
591
|
-
Returns:
|
|
592
|
-
List of StructuredTool instances ready to use with LangChain.
|
|
593
|
-
"""
|
|
594
|
-
tool_map = _create_tool_map(self.definitions, use_underscores=use_underscores)
|
|
595
|
-
return [
|
|
596
|
-
wrap_arcade_tool(
|
|
597
|
-
self._client, tool_name, definition, langgraph=use_interrupts
|
|
598
|
-
)
|
|
599
|
-
for tool_name, definition in tool_map.items()
|
|
600
|
-
]
|
|
601
|
-
|
|
602
|
-
async def authorize(self, tool_name: str, user_id: str) -> AuthorizationResponse:
|
|
603
|
-
"""
|
|
604
|
-
Authorize a user for a tool.
|
|
605
|
-
|
|
606
|
-
Example:
|
|
607
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
608
|
-
>>> await manager.init_tools(tools=["Gmail.SendEmail"])
|
|
609
|
-
>>> auth_response = await manager.authorize("Gmail.SendEmail", "user_123")
|
|
610
|
-
>>> # auth_response.auth_url contains the URL for the user to authorize
|
|
611
|
-
|
|
612
|
-
Args:
|
|
613
|
-
tool_name: The name of the tool to authorize.
|
|
614
|
-
user_id: The user ID to authorize. This should be a unique identifier for the user.
|
|
615
|
-
|
|
616
|
-
Returns:
|
|
617
|
-
AuthorizationResponse containing authorization details, including the auth_url
|
|
618
|
-
that should be presented to the user to complete authorization.
|
|
619
|
-
"""
|
|
620
|
-
return await self._client.tools.authorize(tool_name=tool_name, user_id=user_id)
|
|
621
|
-
|
|
622
|
-
async def is_authorized(self, authorization_id: str) -> bool:
|
|
623
|
-
"""
|
|
624
|
-
Check if a tool authorization is complete.
|
|
625
|
-
|
|
626
|
-
Example:
|
|
627
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
628
|
-
>>> auth_response = await manager.authorize("Gmail.SendEmail", "user_123")
|
|
629
|
-
>>> # After user completes authorization
|
|
630
|
-
>>> is_complete = await manager.is_authorized(auth_response.id)
|
|
631
|
-
|
|
632
|
-
Args:
|
|
633
|
-
authorization_id: The authorization ID to check. This can be the full AuthorizationResponse
|
|
634
|
-
object or just the ID string.
|
|
635
|
-
|
|
636
|
-
Returns:
|
|
637
|
-
True if the authorization is completed, False otherwise.
|
|
638
|
-
"""
|
|
639
|
-
# Handle case where entire AuthorizationResponse object is passed
|
|
640
|
-
if hasattr(authorization_id, "id"):
|
|
641
|
-
authorization_id = authorization_id.id
|
|
642
|
-
|
|
643
|
-
auth_status = await self._client.auth.status(id=authorization_id)
|
|
644
|
-
return auth_status.status == "completed"
|
|
645
|
-
|
|
646
|
-
async def wait_for_auth(self, authorization_id: str) -> AuthorizationResponse:
|
|
647
|
-
"""
|
|
648
|
-
Wait for a tool authorization to complete. This method blocks until
|
|
649
|
-
the authorization is complete or fails.
|
|
650
|
-
|
|
651
|
-
Example:
|
|
652
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
653
|
-
>>> auth_response = await manager.authorize("Gmail.SendEmail", "user_123")
|
|
654
|
-
>>> # Share auth_response.auth_url with the user
|
|
655
|
-
>>> # Wait for the user to complete authorization
|
|
656
|
-
>>> completed_auth = await manager.wait_for_auth(auth_response.id)
|
|
657
|
-
|
|
658
|
-
Args:
|
|
659
|
-
authorization_id: The authorization ID to wait for. This can be the full
|
|
660
|
-
AuthorizationResponse object or just the ID string.
|
|
661
|
-
|
|
662
|
-
Returns:
|
|
663
|
-
AuthorizationResponse with the completed authorization details.
|
|
664
|
-
"""
|
|
665
|
-
# Handle case where entire AuthorizationResponse object is passed
|
|
666
|
-
if hasattr(authorization_id, "id"):
|
|
667
|
-
authorization_id = authorization_id.id
|
|
668
|
-
|
|
669
|
-
return await self._client.auth.wait_for_completion(authorization_id)
|
|
670
|
-
|
|
671
|
-
async def _retrieve_tool_definitions(
|
|
672
|
-
self,
|
|
673
|
-
tools: Optional[list[str]] = None,
|
|
674
|
-
toolkits: Optional[list[str]] = None,
|
|
675
|
-
raise_on_empty: bool = True,
|
|
676
|
-
limit: Optional[int] = None,
|
|
677
|
-
offset: Optional[int] = None,
|
|
678
|
-
) -> list[ToolDefinition]:
|
|
679
|
-
"""
|
|
680
|
-
Retrieve tool definitions asynchronously from the Arcade client, accounting for pagination.
|
|
681
|
-
|
|
682
|
-
Args:
|
|
683
|
-
tools: Optional list of specific tool names to include.
|
|
684
|
-
toolkits: Optional list of toolkit names to include all tools from.
|
|
685
|
-
raise_on_empty: Whether to raise an error if no tools or toolkits are provided.
|
|
686
|
-
limit: Optional limit on the number of tools to retrieve per request.
|
|
687
|
-
offset: Optional offset for paginated requests.
|
|
688
|
-
|
|
689
|
-
Returns:
|
|
690
|
-
List of ToolDefinition instances.
|
|
691
|
-
|
|
692
|
-
Raises:
|
|
693
|
-
ValueError: If no tools or toolkits are provided and raise_on_empty is True.
|
|
694
|
-
"""
|
|
695
|
-
all_tools: list[ToolDefinition] = []
|
|
696
|
-
|
|
697
|
-
# If no specific tools or toolkits are requested, raise an error.
|
|
698
|
-
if not tools and not toolkits:
|
|
699
|
-
if raise_on_empty:
|
|
700
|
-
raise ValueError(
|
|
701
|
-
"No tools or toolkits provided to retrieve tool definitions."
|
|
702
|
-
)
|
|
703
|
-
return []
|
|
704
|
-
|
|
705
|
-
# First, gather single tools if the user specifically requested them.
|
|
706
|
-
if tools:
|
|
707
|
-
for tool_id in tools:
|
|
708
|
-
# ToolsResource.get(...) returns a single ToolDefinition.
|
|
709
|
-
single_tool = await self._client.tools.get(name=tool_id)
|
|
710
|
-
all_tools.append(single_tool)
|
|
711
|
-
|
|
712
|
-
# Next, gather tool definitions from any requested toolkits.
|
|
713
|
-
if toolkits:
|
|
714
|
-
for tk in toolkits:
|
|
715
|
-
# Convert None to NOT_GIVEN for Stainless client
|
|
716
|
-
paginated_tools = await self._client.tools.list(
|
|
717
|
-
toolkit=tk,
|
|
718
|
-
limit=NOT_GIVEN if limit is None else limit,
|
|
719
|
-
offset=NOT_GIVEN if offset is None else offset,
|
|
720
|
-
)
|
|
721
|
-
async for tool in paginated_tools:
|
|
722
|
-
all_tools.append(tool)
|
|
723
|
-
|
|
724
|
-
return all_tools
|
|
725
|
-
|
|
726
|
-
async def add_tool(self, tool_name: str) -> None:
|
|
727
|
-
"""
|
|
728
|
-
Add a single tool to the manager by name.
|
|
729
|
-
|
|
730
|
-
Unlike init_tools(), this method preserves existing tools in the manager
|
|
731
|
-
and only adds the specified tool.
|
|
732
|
-
|
|
733
|
-
Example:
|
|
734
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
735
|
-
>>> await manager.add_tool("Gmail.SendEmail")
|
|
736
|
-
>>> await manager.add_tool("Search.SearchGoogle")
|
|
737
|
-
>>> # Get all tools including newly added ones
|
|
738
|
-
>>> all_tools = await manager.to_langchain()
|
|
739
|
-
|
|
740
|
-
Args:
|
|
741
|
-
tool_name: The fully qualified name of the tool to add (e.g., "Search.SearchGoogle")
|
|
742
|
-
|
|
743
|
-
Raises:
|
|
744
|
-
ValueError: If the tool cannot be found
|
|
745
|
-
"""
|
|
746
|
-
tool = await self._client.tools.get(name=tool_name)
|
|
747
|
-
self._tools.update(_create_tool_map([tool]))
|
|
748
|
-
|
|
749
|
-
async def add_toolkit(
|
|
750
|
-
self,
|
|
751
|
-
toolkit_name: str,
|
|
752
|
-
limit: Optional[int] = None,
|
|
753
|
-
offset: Optional[int] = None,
|
|
754
|
-
) -> None:
|
|
755
|
-
"""
|
|
756
|
-
Add all tools from a specific toolkit to the manager.
|
|
757
|
-
|
|
758
|
-
Unlike init_tools(), this method preserves existing tools in the manager
|
|
759
|
-
and only adds the tools from the specified toolkit.
|
|
760
|
-
|
|
761
|
-
Example:
|
|
762
|
-
>>> manager = AsyncToolManager(api_key="your-api-key")
|
|
763
|
-
>>> await manager.add_toolkit("Gmail")
|
|
764
|
-
>>> await manager.add_toolkit("Search")
|
|
765
|
-
>>> # Get all tools including newly added ones
|
|
766
|
-
>>> all_tools = await manager.to_langchain()
|
|
767
|
-
|
|
768
|
-
Args:
|
|
769
|
-
toolkit_name: The name of the toolkit to add (e.g., "Search")
|
|
770
|
-
limit: Optional limit on the number of tools to retrieve per request
|
|
771
|
-
offset: Optional offset for paginated requests
|
|
772
|
-
|
|
773
|
-
Raises:
|
|
774
|
-
ValueError: If the toolkit cannot be found or has no tools
|
|
775
|
-
"""
|
|
776
|
-
paginated_tools = await self._client.tools.list(
|
|
777
|
-
toolkit=toolkit_name,
|
|
778
|
-
limit=NOT_GIVEN if limit is None else limit,
|
|
779
|
-
offset=NOT_GIVEN if offset is None else offset,
|
|
780
|
-
)
|
|
781
|
-
|
|
782
|
-
async for tool in paginated_tools:
|
|
783
|
-
self._tools.update(_create_tool_map([tool]))
|
|
784
|
-
|
|
785
|
-
async def get_tools(
|
|
786
|
-
self,
|
|
787
|
-
tools: Optional[list[str]] = None,
|
|
788
|
-
toolkits: Optional[list[str]] = None,
|
|
789
|
-
langgraph: bool = True,
|
|
790
|
-
) -> list[StructuredTool]:
|
|
791
|
-
"""
|
|
792
|
-
DEPRECATED: Return the tools in the manager as LangChain StructuredTool objects.
|
|
793
|
-
|
|
794
|
-
This method is deprecated and will be removed in a future major version.
|
|
795
|
-
Please use `init_tools()` to initialize tools and `to_langchain()` to convert them.
|
|
796
|
-
|
|
797
|
-
Args:
|
|
798
|
-
tools: Optional list of tool names to include.
|
|
799
|
-
toolkits: Optional list of toolkits to include.
|
|
800
|
-
langgraph: Whether to use LangGraph-specific behavior
|
|
801
|
-
such as NodeInterrupts for auth.
|
|
802
|
-
|
|
803
|
-
Returns:
|
|
804
|
-
List of StructuredTool instances.
|
|
805
|
-
"""
|
|
806
|
-
warnings.warn(
|
|
807
|
-
"get_tools() is deprecated and will be removed in the next major version. "
|
|
808
|
-
"Please use init_tools() to initialize tools and to_langchain() to convert them.",
|
|
809
|
-
DeprecationWarning,
|
|
810
|
-
stacklevel=2,
|
|
811
|
-
)
|
|
812
|
-
|
|
813
|
-
# Support existing usage pattern
|
|
814
|
-
if tools or toolkits:
|
|
815
|
-
return await self.init_tools(tools=tools, toolkits=toolkits)
|
|
816
|
-
return []
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
def _create_tool_map(
|
|
820
|
-
tools: list[ToolDefinition],
|
|
821
|
-
use_underscores: bool = True,
|
|
822
|
-
) -> dict[str, ToolDefinition]:
|
|
823
|
-
"""
|
|
824
|
-
Build a dictionary that maps the "full_tool_name" to the tool definition.
|
|
825
|
-
|
|
826
|
-
Args:
|
|
827
|
-
tools: List of ToolDefinition objects to map.
|
|
828
|
-
use_underscores: Whether to use underscores instead of periods in tool names.
|
|
829
|
-
For example, "Search_SearchGoogle" vs "Search.SearchGoogle".
|
|
830
|
-
|
|
831
|
-
Returns:
|
|
832
|
-
Dictionary mapping tool names to tool definitions.
|
|
833
|
-
|
|
834
|
-
Note:
|
|
835
|
-
This is a temporary solution to support the naming convention of certain model providers
|
|
836
|
-
like OpenAI, which work better with underscores in tool names.
|
|
837
|
-
"""
|
|
838
|
-
tool_map: dict[str, ToolDefinition] = {}
|
|
839
|
-
for tool in tools:
|
|
840
|
-
# Ensure toolkit name and tool name are not None before creating the key
|
|
841
|
-
toolkit_name = tool.toolkit.name if tool.toolkit and tool.toolkit.name else None
|
|
842
|
-
if toolkit_name and tool.name:
|
|
843
|
-
if use_underscores:
|
|
844
|
-
tool_name = f"{toolkit_name}_{tool.name}"
|
|
845
|
-
else:
|
|
846
|
-
tool_name = f"{toolkit_name}.{tool.name}"
|
|
847
|
-
tool_map[tool_name] = tool
|
|
848
|
-
return tool_map
|