-
Notifications
You must be signed in to change notification settings - Fork 15
feat(toolbox-core): add basic implementation #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
266e148
chore: create init module
kurtisvg a647c58
feat: add basic toolbox-core implementation
kurtisvg 553b737
chore: remove pycache
kurtisvg f62433a
chore: correct aioresponses module
kurtisvg 5374670
Merge branch 'main' into kvg-toolbox-core-basic
twishabansal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,12 @@ | |
|
||
# vscode | ||
.vscode/ | ||
|
||
# python | ||
env | ||
venv | ||
*.pyc | ||
.python-version | ||
**.egg-info/ | ||
__pycache__/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
aiohttp==3.11.14 | ||
pydantic==2.10.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from inspect import Parameter | ||
from typing import Optional, Type | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class ParameterSchema(BaseModel): | ||
""" | ||
Schema for a tool parameter. | ||
""" | ||
|
||
name: str | ||
type: str | ||
description: str | ||
authSources: Optional[list[str]] = None | ||
items: Optional["ParameterSchema"] = None | ||
|
||
def __get_type(self) -> Type: | ||
if self.type == "string": | ||
return str | ||
elif self.type == "integer": | ||
return int | ||
elif self.type == "float": | ||
return float | ||
elif self.type == "boolean": | ||
return bool | ||
elif self.type == "array": | ||
if self.items is None: | ||
raise Exception("Unexpected value: type is 'list' but items is None") | ||
return list[self._items.to_type()] # type: ignore | ||
|
||
raise ValueError(f"Unsupported schema type: {self.type}") | ||
|
||
def to_param(self) -> Parameter: | ||
return Parameter( | ||
self.name, | ||
Parameter.POSITIONAL_OR_KEYWORD, | ||
annotation=self.__get_type(), | ||
) | ||
|
||
|
||
class ToolSchema(BaseModel): | ||
""" | ||
Schema for a tool. | ||
""" | ||
|
||
description: str | ||
parameters: list[ParameterSchema] | ||
authRequired: list[str] = [] | ||
|
||
|
||
class ManifestSchema(BaseModel): | ||
""" | ||
Schema for the Toolbox manifest. | ||
""" | ||
|
||
serverVersion: str | ||
tools: dict[str, ToolSchema] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from inspect import Parameter, Signature | ||
from typing import Any | ||
|
||
from aiohttp import ClientSession | ||
|
||
|
||
class ToolboxTool: | ||
""" | ||
A callable proxy object representing a specific tool on a remote Toolbox server. | ||
|
||
Instances of this class behave like asynchronous functions. When called, they | ||
send a request to the corresponding tool's endpoint on the Toolbox server with | ||
the provided arguments. | ||
|
||
It utilizes Python's introspection features (`__name__`, `__doc__`, | ||
`__signature__`, `__annotations__`) so that standard tools like `help()` | ||
and `inspect` work as expected. | ||
""" | ||
|
||
__url: str | ||
__session: ClientSession | ||
__signature__: Signature | ||
|
||
def __init__( | ||
self, | ||
session: ClientSession, | ||
base_url: str, | ||
name: str, | ||
desc: str, | ||
params: list[Parameter], | ||
): | ||
""" | ||
Initializes a callable that will trigger the tool invocation through the Toolbox server. | ||
|
||
Args: | ||
session: The `aiohttp.ClientSession` used for making API requests. | ||
base_url: The base URL of the Toolbox server API. | ||
name: The name of the remote tool. | ||
desc: The description of the remote tool (used as its docstring). | ||
params: A list of `inspect.Parameter` objects defining the tool's | ||
arguments and their types/defaults. | ||
""" | ||
|
||
# used to invoke the toolbox API | ||
self.__session = session | ||
self.__url = f"{base_url}/api/tool/{name}/invoke" | ||
|
||
# the following properties are set to help anyone that might inspect it determine | ||
self.__name__ = name | ||
self.__doc__ = desc | ||
self.__signature__ = Signature(parameters=params, return_annotation=str) | ||
self.__annotations__ = {p.name: p.annotation for p in params} | ||
# TODO: self.__qualname__ ?? | ||
|
||
async def __call__(self, *args: Any, **kwargs: Any) -> str: | ||
""" | ||
Asynchronously calls the remote tool with the provided arguments. | ||
|
||
Validates arguments against the tool's signature, then sends them | ||
as a JSON payload in a POST request to the tool's invoke URL. | ||
|
||
Args: | ||
*args: Positional arguments for the tool. | ||
**kwargs: Keyword arguments for the tool. | ||
|
||
Returns: | ||
The string result returned by the remote tool execution. | ||
""" | ||
all_args = self.__signature__.bind(*args, **kwargs) | ||
all_args.apply_defaults() # Include default values if not provided | ||
payload = all_args.arguments | ||
|
||
async with self.__session.post( | ||
self.__url, | ||
json=payload, | ||
) as resp: | ||
ret = await resp.json() | ||
if "error" in ret: | ||
# TODO: better error | ||
raise Exception(ret["error"]) | ||
return ret.get("result", ret) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.