Skip to content

feat(toolbox-core): updated generated docstring to include parameters and their descriptions #127

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 64 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
5f1b2b0
feat: add authenticated parameters support
kurtisvg Mar 30, 2025
34bec37
chore: add asyncio dep
kurtisvg Mar 30, 2025
f0024bf
chore: run itest
kurtisvg Mar 30, 2025
a646285
chore: add type hint
kurtisvg Mar 30, 2025
0dc7034
fix: call tool instead of client
kurtisvg Mar 30, 2025
61d32aa
chore: correct arg name
kurtisvg Mar 31, 2025
744ade9
feat: add support for bound parameters
kurtisvg Mar 31, 2025
b875771
chore: add tests for bound parameters
kurtisvg Mar 31, 2025
138d8d9
docs: update syntax error on readme (#121)
Yuan325 Mar 31, 2025
e2f5b4d
ci: added release please config (#112)
twishabansal Apr 1, 2025
280a79f
added basic e2e tests
twishabansal Apr 1, 2025
63808ce
change license year
twishabansal Apr 1, 2025
55f5ca0
add test deps
twishabansal Apr 1, 2025
ed16488
fix tests
twishabansal Apr 1, 2025
f0991ab
fix tests
twishabansal Apr 1, 2025
cc6d568
fix tests
twishabansal Apr 1, 2025
1f63b0d
add new test case
twishabansal Apr 1, 2025
66e88ab
fix docstring
twishabansal Apr 1, 2025
1fa1b7a
added todo
twishabansal Apr 1, 2025
12af5fa
cleanup
twishabansal Apr 1, 2025
9c3ba38
add bind param test case
twishabansal Apr 1, 2025
d75828e
make bind params dynamic
twishabansal Apr 1, 2025
fef1f7f
try fix test errors
twishabansal Apr 1, 2025
cd701b7
lint
twishabansal Apr 1, 2025
7a5a1bd
remove redundant test
twishabansal Apr 1, 2025
e343e78
test fix
twishabansal Apr 1, 2025
11d62b4
fix docstring
twishabansal Apr 1, 2025
87e00cc
feat: add authenticated parameters support
kurtisvg Mar 30, 2025
6b263ad
chore: add asyncio dep
kurtisvg Mar 30, 2025
5fe541f
chore: run itest
kurtisvg Mar 30, 2025
e9d7a31
chore: add type hint
kurtisvg Mar 30, 2025
58c55cf
fix: call tool instead of client
kurtisvg Mar 30, 2025
c1a482a
chore: correct arg name
kurtisvg Mar 31, 2025
c1ac2cd
chore: address feedback
kurtisvg Apr 2, 2025
bcba462
chore: address more feedback
kurtisvg Apr 2, 2025
c8491a9
feat: add support for bound parameters
kurtisvg Mar 31, 2025
c26c453
chore: add tests for bound parameters
kurtisvg Mar 31, 2025
3abe9e4
chore: address feedback
kurtisvg Apr 2, 2025
3200f00
Merge branch 'kvg-bound-params' into twisha-core-e2e
twishabansal Apr 2, 2025
9482f37
revert package file changes
twishabansal Apr 2, 2025
6dca7f9
fix error message
twishabansal Apr 2, 2025
4d562bd
revert package files
twishabansal Apr 2, 2025
cc7ccad
lint
twishabansal Apr 2, 2025
84e66a6
fix error message
twishabansal Apr 2, 2025
fe2a332
Update packages/toolbox-core/tests/test_e2e.py
twishabansal Apr 2, 2025
3c67596
try changing docstring to include args and their descriptions
twishabansal Apr 2, 2025
183ea48
fix docstring
twishabansal Apr 2, 2025
df96f53
lint
twishabansal Apr 2, 2025
8023460
fix test
twishabansal Apr 2, 2025
3520277
lint
twishabansal Apr 2, 2025
beab901
added return type annotation
twishabansal Apr 2, 2025
10601aa
Add docstrings
twishabansal Apr 2, 2025
65b729d
use and update schema
twishabansal Apr 2, 2025
c613ae8
lint
twishabansal Apr 2, 2025
9e63c37
lint
twishabansal Apr 2, 2025
ce72925
Merge branch 'main' into twisha-core-docstring
twishabansal Apr 3, 2025
53655ea
revert changes to e2e test file
twishabansal Apr 3, 2025
d6c0b34
change string to str and in arg type
twishabansal Apr 3, 2025
1260987
small change
twishabansal Apr 3, 2025
57843e9
fix imports
twishabansal Apr 3, 2025
4adda1b
remove toolschema usage
twishabansal Apr 3, 2025
5cc8978
lint
twishabansal Apr 3, 2025
1dffce2
move create_docstring method outside the class.
twishabansal Apr 3, 2025
2187cea
lint
twishabansal Apr 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/toolbox-core/src/toolbox_core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def __parse_tool(
session=self.__session,
base_url=self.__base_url,
name=name,
desc=schema.description,
params=[p.to_param() for p in params],
description=schema.description,
params=params,
# create a read-only values for the maps to prevent mutation
required_authn_params=types.MappingProxyType(authn_params),
auth_service_token_getters=types.MappingProxyType(auth_token_getters),
Expand Down
51 changes: 31 additions & 20 deletions packages/toolbox-core/src/toolbox_core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import asyncio
import types
from inspect import Parameter, Signature
from inspect import Signature
from typing import (
Any,
Callable,
Expand All @@ -28,6 +28,8 @@

from aiohttp import ClientSession

from toolbox_core.protocol import ParameterSchema


class ToolboxTool:
"""
Expand All @@ -47,8 +49,8 @@ def __init__(
session: ClientSession,
base_url: str,
name: str,
desc: str,
params: Sequence[Parameter],
description: str,
params: Sequence[ParameterSchema],
required_authn_params: Mapping[str, list[str]],
auth_service_token_getters: Mapping[str, Callable[[], str]],
bound_params: Mapping[str, Union[Callable[[], Any], Any]],
Expand All @@ -61,31 +63,30 @@ def __init__(
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.
description: The description of the remote tool.
params: The args of the tool.
required_authn_params: A dict of required authenticated parameters to a list
of services that provide values for them.
auth_service_token_getters: A dict of authService -> token (or callables that
produce a token)
bound_params: A mapping of parameter names to bind to specific values or
callables that are called to produce values as needed.

"""

# used to invoke the toolbox API
self.__session: ClientSession = session
self.__base_url: str = base_url
self.__url = f"{base_url}/api/tool/{name}/invoke"

self.__desc = desc
self.__description = description
self.__params = params
inspect_type_params = [param.to_param() for param in self.__params]

# the following properties are set to help anyone that might inspect it determine usage
self.__name__ = name
self.__doc__ = desc
self.__signature__ = Signature(parameters=params, return_annotation=str)
self.__annotations__ = {p.name: p.annotation for p in params}
self.__doc__ = self._create_docstring()
self.__signature__ = Signature(
parameters=inspect_type_params, return_annotation=str
)
self.__annotations__ = {p.name: p.annotation for p in inspect_type_params}
# TODO: self.__qualname__ ??

# map of parameter name to auth service required by it
Expand All @@ -95,13 +96,25 @@ def __init__(
# map of parameter name to value (or callable that produces that value)
self.__bound_parameters = bound_params

def _create_docstring(self) -> str:
"""Convert a tool schema into its function docstring"""
docstring = self.__description
if not self.__params:
return docstring
docstring += "\n\nArgs:"
for p in self.__params:
docstring += (
f"\n {p.name} ({p.to_param().annotation.__name__}): {p.description}"
)
return docstring

def __copy(
self,
session: Optional[ClientSession] = None,
base_url: Optional[str] = None,
name: Optional[str] = None,
desc: Optional[str] = None,
params: Optional[list[Parameter]] = None,
description: Optional[str] = None,
params: Optional[Sequence[ParameterSchema]] = None,
required_authn_params: Optional[Mapping[str, list[str]]] = None,
auth_service_token_getters: Optional[Mapping[str, Callable[[], str]]] = None,
bound_params: Optional[Mapping[str, Union[Callable[[], Any], Any]]] = None,
Expand All @@ -113,9 +126,8 @@ def __copy(
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.
description: The description of the remote tool.
params: The args of the tool.
required_authn_params: A dict of required authenticated parameters that need
a auth_service_token_getter set for them yet.
auth_service_token_getters: A dict of authService -> token (or callables
Expand All @@ -129,7 +141,7 @@ def __copy(
session=check(session, self.__session),
base_url=check(base_url, self.__base_url),
name=check(name, self.__name__),
desc=check(desc, self.__desc),
description=check(description, self.__description),
params=check(params, self.__params),
required_authn_params=check(
required_authn_params, self.__required_authn_params
Expand Down Expand Up @@ -258,7 +270,6 @@ def bind_parameters(
for p in self.__params:
if p.name not in bound_params:
new_params.append(p)

all_bound_params = dict(self.__bound_parameters)
all_bound_params.update(bound_params)

Expand Down
6 changes: 5 additions & 1 deletion packages/toolbox-core/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ async def test_load_tool_success(aioresponses, test_tool_str):
assert callable(loaded_tool)
# Assert introspection attributes are set correctly
assert loaded_tool.__name__ == TOOL_NAME
assert loaded_tool.__doc__ == test_tool_str.description
expected_description = (
test_tool_str.description
+ f"\n\nArgs:\n param1 (str): Description of Param1"
)
assert loaded_tool.__doc__ == expected_description

# Assert signature inspection
sig = inspect.signature(loaded_tool)
Expand Down