Skip to content

Commit 79b067a

Browse files
committed
test: WIP for E2E tests
1 parent e859a54 commit 79b067a

File tree

4 files changed

+60
-78
lines changed

4 files changed

+60
-78
lines changed

packages/toolbox-core/src/toolbox_core/sync_client.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,46 @@ def close(self):
8585
coro = self.__async_client.close()
8686
asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
8787

88+
async def aload_tool(
89+
self,
90+
name: str,
91+
auth_token_getters: dict[str, Callable[[], str]] = {},
92+
bound_params: Mapping[str, Union[Callable[[], Any], Any]] = {},
93+
) -> ToolboxSyncTool:
94+
"""
95+
Asynchronously loads a tool from the server.
96+
"""
97+
coro = self.__async_client.load_tool(name, auth_token_getters, bound_params)
98+
99+
if not self.__loop or not self.__thread:
100+
raise ValueError("Background loop or thread cannot be None.")
101+
102+
async_tool = await asyncio.run_coroutine_threadsafe(coro, self.__loop)
103+
return ToolboxSyncTool(async_tool, self.__loop, self.__thread)
104+
105+
async def aload_toolset(
106+
self,
107+
name: Optional[str] = None,
108+
auth_token_getters: dict[str, Callable[[], str]] = {},
109+
bound_params: Mapping[str, Union[Callable[[], Any], Any]] = {},
110+
strict: bool = False,
111+
) -> list[ToolboxSyncTool]:
112+
"""
113+
Asynchronously fetches a toolset and loads all tools defined within it.
114+
"""
115+
coro = self.__async_client.load_toolset(
116+
name, auth_token_getters, bound_params, strict
117+
)
118+
119+
if not self.__loop or not self.__thread:
120+
raise ValueError("Background loop or thread cannot be None.")
121+
122+
async_tools = await asyncio.run_coroutine_threadsafe(coro, self.__loop)
123+
return [
124+
ToolboxSyncTool(async_tool, self.__loop, self.__thread)
125+
for async_tool in async_tools
126+
]
127+
88128
def load_tool(
89129
self,
90130
name: str,
@@ -110,13 +150,7 @@ def load_tool(
110150
for execution. The specific arguments and behavior of the callable
111151
depend on the tool itself.
112152
"""
113-
coro = self.__async_client.load_tool(name, auth_token_getters, bound_params)
114-
115-
if not self.__loop or not self.__thread:
116-
raise ValueError("Background loop or thread cannot be None.")
117-
118-
async_tool = asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
119-
return ToolboxSyncTool(async_tool, self.__loop, self.__thread)
153+
return self.aload_tool(name, auth_token_getters, bound_params).result()
120154

121155
def load_toolset(
122156
self,
@@ -147,18 +181,7 @@ def load_toolset(
147181
Raises:
148182
ValueError: If validation fails based on the `strict` flag.
149183
"""
150-
coro = self.__async_client.load_toolset(
151-
name, auth_token_getters, bound_params, strict
152-
)
153-
154-
if not self.__loop or not self.__thread:
155-
raise ValueError("Background loop or thread cannot be None.")
156-
157-
async_tools = asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
158-
return [
159-
ToolboxSyncTool(async_tool, self.__loop, self.__thread)
160-
for async_tool in async_tools
161-
]
184+
return self.aload_toolset(name, auth_token_getters, bound_params, strict).result()
162185

163186
def add_headers(
164187
self, headers: Mapping[str, Union[Callable, Coroutine, str]]

packages/toolbox-core/src/toolbox_core/sync_tool.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ def _auth_service_token_getters(self) -> Mapping[str, Callable[[], str]]:
129129
def _client_headers(self) -> Mapping[str, Union[Callable, Coroutine, str]]:
130130
return self.__async_tool._client_headers
131131

132+
async def acall(self, *args: Any, **kwargs: Any) -> str:
133+
"""
134+
Asynchronously calls the remote tool with the provided arguments.
135+
"""
136+
coro = self.__async_tool(*args, **kwargs)
137+
return await asyncio.run_coroutine_threadsafe(coro, self.__loop)
138+
132139
def __call__(self, *args: Any, **kwargs: Any) -> str:
133140
"""
134141
Synchronously calls the remote tool with the provided arguments.
@@ -143,8 +150,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> str:
143150
Returns:
144151
The string result returned by the remote tool execution.
145152
"""
146-
coro = self.__async_tool(*args, **kwargs)
147-
return asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
153+
return self.acall(*args, **kwargs).result()
148154

149155
def add_auth_token_getters(
150156
self,

packages/toolbox-langchain/src/toolbox_langchain/client.py

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import asyncio
15+
from asyncio import wrap_future
1616
from typing import Any, Callable, Optional, Union
1717
from warnings import warn
1818

@@ -85,30 +85,12 @@ async def aload_tool(
8585
)
8686
auth_token_getters = auth_headers
8787

88-
coro = self.__core_client._async_client.load_tool(
88+
core_tool = await wrap_future(self.__core_client.aload_tool(
8989
name=tool_name,
9090
auth_token_getters=auth_token_getters,
9191
bound_params=bound_params,
92-
)
93-
94-
if not self.__core_client._loop:
95-
# If a loop has not been provided, attempt to run in current thread.
96-
core_tool = await coro
97-
else:
98-
# Otherwise, run in the background thread.
99-
core_tool = await asyncio.wrap_future(
100-
asyncio.run_coroutine_threadsafe(coro, self.__core_client._loop)
101-
)
102-
103-
if not self.__core_client._loop or not self.__core_client._thread:
104-
raise ValueError("Background loop or thread cannot be None.")
105-
106-
core_sync_tool = ToolboxSyncTool(
107-
core_tool,
108-
self.__core_client._loop,
109-
self.__core_client._thread,
110-
)
111-
return ToolboxTool(core_tool=core_sync_tool)
92+
))
93+
return ToolboxTool(core_tool=core_tool)
11294

11395
async def aload_toolset(
11496
self,
@@ -167,36 +149,16 @@ async def aload_toolset(
167149
)
168150
auth_token_getters = auth_headers
169151

170-
coro = self.__core_client._async_client.load_toolset(
152+
core_tools = await wrap_future(self.__core_client.aload_toolset(
171153
name=toolset_name,
172154
auth_token_getters=auth_token_getters,
173155
bound_params=bound_params,
174156
strict=strict,
175-
)
176-
177-
if not self.__core_client._loop:
178-
# If a loop has not been provided, attempt to run in current thread.
179-
core_tools = await coro
180-
else:
181-
# Otherwise, run in the background thread.
182-
core_tools = await asyncio.wrap_future(
183-
asyncio.run_coroutine_threadsafe(coro, self.__core_client._loop)
184-
)
185-
186-
if not self.__core_client._loop or not self.__core_client._thread:
187-
raise ValueError("Background loop or thread cannot be None.")
157+
))
188158

189-
core_sync_tools = [
190-
ToolboxSyncTool(
191-
core_tool,
192-
self.__core_client._loop,
193-
self.__core_client._thread,
194-
)
195-
for core_tool in core_tools
196-
]
197159
tools = []
198-
for core_sync_tool in core_sync_tools:
199-
tools.append(ToolboxTool(core_tool=core_sync_tool))
160+
for core_tool in core_tools:
161+
tools.append(ToolboxTool(core_tool=core_tool))
200162
return tools
201163

202164
def load_tool(

packages/toolbox-langchain/src/toolbox_langchain/tools.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import asyncio
15+
from asyncio import wrap_future
1616
from typing import Any, Callable, Union
1717

1818
from deprecated import deprecated
@@ -50,16 +50,7 @@ def _run(self, **kwargs: Any) -> str:
5050
return self.__core_tool(**kwargs)
5151

5252
async def _arun(self, **kwargs: Any) -> str:
53-
coro = self.__core_tool._async_tool(**kwargs)
54-
55-
# If a loop has not been provided, attempt to run in current thread.
56-
if not self.__core_tool._loop:
57-
return await coro
58-
59-
# Otherwise, run in the background thread.
60-
return await asyncio.wrap_future(
61-
asyncio.run_coroutine_threadsafe(coro, self.__core_tool._loop)
62-
)
53+
return await wrap_future(self.__core_tool._async_tool.arun(**kwargs))
6354

6455
def add_auth_token_getters(
6556
self, auth_token_getters: dict[str, Callable[[], str]]

0 commit comments

Comments
 (0)