Skip to content

Commit 2206a83

Browse files
committed
feat(toolbox-llamaindex): Add context manager support for sync and async clients
1 parent 2378598 commit 2206a83

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

packages/toolbox-llamaindex/src/toolbox_llamaindex/async_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,28 @@ def load_toolset(
190190
strict: bool = False,
191191
) -> list[AsyncToolboxTool]:
192192
raise NotImplementedError("Synchronous methods not supported by async client.")
193+
194+
async def close(self):
195+
"""Close the underlying synchronous client."""
196+
await self.__core_client.close()
197+
198+
async def __aenter__(self):
199+
"""
200+
Enter the runtime context related to this client instance.
201+
202+
Allows the client to be used as an asynchronous context manager
203+
(e.g., `async with AsyncToolboxClient(...) as client:`).
204+
205+
Returns:
206+
self: The client instance itself.
207+
"""
208+
return self
209+
210+
async def __aexit__(self, exc_type, exc_val, exc_tb):
211+
"""
212+
Exit the runtime context and close the internally managed session.
213+
214+
Allows the client to be used as an asynchronous context manager
215+
(e.g., `async with AsyncToolboxClient(...) as client:`).
216+
"""
217+
await self.close()

packages/toolbox-llamaindex/src/toolbox_llamaindex/client.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,49 @@ def load_toolset(
292292
for core_sync_tool in core_sync_tools:
293293
tools.append(ToolboxTool(core_tool=core_sync_tool))
294294
return tools
295+
296+
def close(self):
297+
"""Close the underlying synchronous client."""
298+
self.__core_client.close()
299+
300+
async def __aenter__(self):
301+
"""
302+
Enter the runtime context related to this client instance.
303+
304+
Allows the client to be used as an asynchronous context manager
305+
(e.g., `async with ToolboxClient(...) as client:`).
306+
307+
Returns:
308+
self: The client instance itself.
309+
"""
310+
return self
311+
312+
async def __aexit__(self, exc_type, exc_val, exc_tb):
313+
"""
314+
Exit the runtime context and close the internally managed session.
315+
316+
Allows the client to be used as an asynchronous context manager
317+
(e.g., `async with ToolboxClient(...) as client:`).
318+
"""
319+
self.close()
320+
321+
def __enter__(self):
322+
"""
323+
Enter the runtime context related to this client instance.
324+
325+
Allows the client to be used as a context manager
326+
(e.g., `with ToolboxClient(...) as client:`).
327+
328+
Returns:
329+
self: The client instance itself.
330+
"""
331+
return self
332+
333+
def __exit__(self, exc_type, exc_val, exc_tb):
334+
"""
335+
Exit the runtime context and close the internally managed session.
336+
337+
Allows the client to be used as a context manager
338+
(e.g., `with ToolboxClient(...) as client:`).
339+
"""
340+
self.close()

0 commit comments

Comments
 (0)