Skip to content

Commit 36288e9

Browse files
committed
feat(toolbox-llamaindex)!: Update tool and client codebase
1 parent e77b5f3 commit 36288e9

File tree

9 files changed

+1432
-1232
lines changed

9 files changed

+1432
-1232
lines changed

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

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from warnings import warn
1717

1818
from aiohttp import ClientSession
19+
from toolbox_core.client import ToolboxClient as ToolboxCoreClient
1920

20-
from .tools import AsyncToolboxTool
21-
from .utils import ManifestSchema, _load_manifest
21+
from .async_tools import AsyncToolboxTool
2222

2323

2424
# This class is an internal implementation detail and is not exposed to the
@@ -38,67 +38,72 @@ def __init__(
3838
url: The base URL of the Toolbox service.
3939
session: An HTTP client session.
4040
"""
41-
self.__url = url
42-
self.__session = session
41+
self.__core_client = ToolboxCoreClient(url=url, session=session)
4342

4443
async def aload_tool(
4544
self,
4645
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,
4848
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
4949
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
50-
strict: bool = True,
5150
) -> AsyncToolboxTool:
5251
"""
5352
Loads the tool with the given tool name from the Toolbox service.
5453
5554
Args:
5655
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.
6060
bound_params: An optional mapping of parameter names to their
6161
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.
6562
6663
Returns:
6764
A tool loaded from the Toolbox.
6865
"""
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+
6979
if auth_headers:
70-
if auth_tokens:
80+
if auth_token_getters:
7181
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.",
7383
DeprecationWarning,
7484
)
7585
else:
7686
warn(
77-
"Argument `auth_headers` is deprecated. Use `auth_tokens` instead.",
87+
"Argument `auth_headers` is deprecated. Use `auth_token_getters` instead.",
7888
DeprecationWarning,
7989
)
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,
9396
)
97+
return AsyncToolboxTool(core_tool=core_tool)
9498

9599
async def aload_toolset(
96100
self,
97101
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,
99104
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
100105
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
101-
strict: bool = True,
106+
strict: bool = False,
102107
) -> list[AsyncToolboxTool]:
103108
"""
104109
Loads tools from the Toolbox service, optionally filtered by toolset
@@ -107,65 +112,76 @@ async def aload_toolset(
107112
Args:
108113
toolset_name: The name of the toolset to load. If not provided,
109114
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.
113119
bound_params: An optional mapping of parameter names to their
114120
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.
118126
119127
Returns:
120128
A list of all tools loaded from the Toolbox.
121129
"""
122-
if auth_headers:
123-
if auth_tokens:
130+
if auth_tokens:
131+
if auth_token_getters:
124132
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.",
126134
DeprecationWarning,
127135
)
128136
else:
129137
warn(
130-
"Argument `auth_headers` is deprecated. Use `auth_tokens` instead.",
138+
"Argument `auth_tokens` is deprecated. Use `auth_token_getters` instead.",
131139
DeprecationWarning,
132140
)
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,
149153
)
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))
151166
return tools
152167

153168
def load_tool(
154169
self,
155170
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,
157173
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
158174
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
159-
strict: bool = True,
160175
) -> AsyncToolboxTool:
161176
raise NotImplementedError("Synchronous methods not supported by async client.")
162177

163178
def load_toolset(
164179
self,
165180
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,
167183
auth_headers: Optional[dict[str, Callable[[], str]]] = None,
168184
bound_params: dict[str, Union[Any, Callable[[], Any]]] = {},
169-
strict: bool = True,
185+
strict: bool = False,
170186
) -> list[AsyncToolboxTool]:
171187
raise NotImplementedError("Synchronous methods not supported by async client.")

0 commit comments

Comments
 (0)