1
+ import asyncio
1
2
from typing import Optional
2
3
3
4
from aiohttp import ClientSession
8
9
9
10
10
11
class ToolboxClient :
11
- def __init__ (self , url : str , session : ClientSession ):
12
+ def __init__ (self , url : str , session : Optional [ ClientSession ] = None ):
12
13
"""
13
14
Initializes the ToolboxClient for the Toolbox service at the given URL.
14
15
15
16
Args:
16
17
url: The base URL of the Toolbox service.
17
18
session: The HTTP client session.
19
+ Default: None
18
20
"""
19
21
self ._url : str = url
20
- self ._session = session
22
+ self ._should_close_session : bool = session != None
23
+ self ._session : ClientSession = session or ClientSession ()
24
+
25
+ async def close (self ) -> None :
26
+ """
27
+ Close the Toolbox client and its tools.
28
+ """
29
+ # We check whether _should_close_session is set or not since we do not
30
+ # want to close the session in case the user had passed their own
31
+ # ClientSession object, since then we expect the user to be owning its
32
+ # lifecycle.
33
+ if self ._session and self ._should_close_session :
34
+ await self ._session .close ()
35
+
36
+ def __del__ (self ):
37
+ try :
38
+ loop = asyncio .get_event_loop ()
39
+ if loop .is_running ():
40
+ loop .create_task (self .close ())
41
+ else :
42
+ loop .run_until_complete (self .close ())
43
+ except Exception :
44
+ # We "pass" assuming that the exception is thrown because the event
45
+ # loop is no longer running, but at that point the Session should
46
+ # have been closed already anyway.
47
+ pass
21
48
22
49
async def _load_tool_manifest (self , tool_name : str ) -> ManifestSchema :
23
50
"""
24
- Fetches and parses the YAML manifest for the given tool from the Toolbox service.
51
+ Fetches and parses the YAML manifest for the given tool from the Toolbox
52
+ service.
25
53
26
54
Args:
27
55
tool_name: The name of the tool to load.
@@ -40,7 +68,8 @@ async def _load_toolset_manifest(
40
68
41
69
Args:
42
70
toolset_name: The name of the toolset to load.
43
- Default: None. If not provided, then all the available tools are loaded.
71
+ Default: None. If not provided, then all the available tools are
72
+ loaded.
44
73
45
74
Returns:
46
75
The parsed Toolbox manifest.
@@ -52,7 +81,8 @@ def _generate_tool(
52
81
self , tool_name : str , manifest : ManifestSchema
53
82
) -> StructuredTool :
54
83
"""
55
- Creates a StructuredTool object and a dynamically generated BaseModel for the given tool.
84
+ Creates a StructuredTool object and a dynamically generated BaseModel
85
+ for the given tool.
56
86
57
87
Args:
58
88
tool_name: The name of the tool to generate.
@@ -93,7 +123,8 @@ async def load_toolset(
93
123
self , toolset_name : Optional [str ] = None
94
124
) -> list [StructuredTool ]:
95
125
"""
96
- Loads tools from the Toolbox service, optionally filtered by toolset name.
126
+ Loads tools from the Toolbox service, optionally filtered by toolset
127
+ name.
97
128
98
129
Args:
99
130
toolset_name: The name of the toolset to load.
0 commit comments