Skip to content
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

fix: Allow manual execute action for any action #1147

Merged
merged 4 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 3 additions & 8 deletions python/composio/tools/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _limit_file_search_response(response: t.Dict) -> t.Dict:

# To be populated by get_tools(), from within subclasses like
# composio_openai's Toolset.
self._requested_actions: t.Optional[t.List[str]] = None
self._requested_actions: t.List[str] = []

def _validating_connection_ids(
self,
Expand Down Expand Up @@ -848,6 +848,7 @@ def execute_action(
text: t.Optional[str] = None,
*,
processors: t.Optional[ProcessorsType] = None,
_check_requested_actions: bool = False,
) -> t.Dict:
"""
Execute an action on a given entity.
Expand All @@ -861,10 +862,7 @@ def execute_action(
:return: Output object from the function call
"""
action = Action(action)
if (
self._requested_actions is not None
and action.slug not in self._requested_actions
):
if _check_requested_actions and action.slug not in self._requested_actions:
raise ComposioSDKError(
f"Action {action.slug} is being called, but was never requested by the toolset. "
"Make sure that the actions you are trying to execute are requested in your "
Expand Down Expand Up @@ -1088,9 +1086,6 @@ def get_action_schemas(

if _populate_requested:
action_names = [item.name for item in items]
if self._requested_actions is None:
self._requested_actions = []

self._requested_actions += action_names

return items
Expand Down
8 changes: 7 additions & 1 deletion python/plugins/autogen/composio_autogen/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ def register_tools(
:param entity_id: Entity ID to use for executing function calls.
"""
self.validate_tools(apps=apps, actions=actions, tags=tags)
schemas = self.get_action_schemas(actions=actions, apps=apps, tags=tags)
schemas = self.get_action_schemas(
actions=actions,
apps=apps,
tags=tags,
_populate_requested=True,
)
for schema in schemas:
self._register_schema_to_autogen(
schema=schema.model_dump(
Expand Down Expand Up @@ -121,6 +126,7 @@ def execute_action(**kwargs: t.Any) -> t.Dict:
action=Action(value=name),
params=kwargs,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

function = types.FunctionType(
Expand Down
1 change: 1 addition & 0 deletions python/plugins/camel/composio_camel/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def function(**kwargs: t.Any) -> t.Dict:
action=Action(value=name),
params=kwargs,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

return OpenAIFunction(
Expand Down
1 change: 1 addition & 0 deletions python/plugins/claude/composio_claude/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def execute_tool_call(
action=Action(value=tool_call.name),
params=t.cast(t.Dict, tool_call.input),
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

def handle_tool_calls(
Expand Down
1 change: 1 addition & 0 deletions python/plugins/crew_ai/composio_crewai/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def execute(**kwargs: t.Any) -> t.Dict:
action=Action(value=action),
params=kwargs,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

class Wrapper(BaseTool):
Expand Down
5 changes: 4 additions & 1 deletion python/plugins/google/composio_google/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ def get_tool(
),
)
for tool in self.get_action_schemas(
actions=actions, apps=apps, tags=tags
actions=actions,
apps=apps,
tags=tags,
_populate_requested=True,
)
]
)
Expand Down
1 change: 1 addition & 0 deletions python/plugins/griptape/composio_griptape/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def _execute_task(params: t.Dict) -> t.Dict:
action=Action(value=name),
params=params,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

class GripTapeTool(BaseTool):
Expand Down
1 change: 1 addition & 0 deletions python/plugins/julep/composio_julep/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def handle_tool_calls( # type: ignore[override]
action=Action(value=tool_function["name"]),
params=json.loads(tool_function["arguments"]),
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)
)
except json.JSONDecodeError:
Expand Down
1 change: 1 addition & 0 deletions python/plugins/langchain/composio_langchain/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def function(**kwargs: t.Any) -> t.Dict:
action=action,
params=kwargs,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

action_func = types.FunctionType(
Expand Down
1 change: 1 addition & 0 deletions python/plugins/llamaindex/composio_llamaindex/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def function(**kwargs: t.Any) -> t.Dict:
action=Action(value=action),
params=kwargs,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

action_func = types.FunctionType(
Expand Down
1 change: 1 addition & 0 deletions python/plugins/lyzr/composio_lyzr/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def function(**kwargs: t.Any) -> t.Dict:
action=Action(value=name),
params=kwargs,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

action_func = types.FunctionType(
Expand Down
1 change: 1 addition & 0 deletions python/plugins/openai/composio_openai/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def execute_tool_call(
action=tool_call.function.name,
params=json.loads(tool_call.function.arguments),
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)

def handle_tool_calls(
Expand Down
1 change: 1 addition & 0 deletions python/plugins/phidata/composio_phidata/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def function(**kwargs: t.Any) -> str:
action=Action(value=name),
params=kwargs,
entity_id=entity_id or self.entity_id,
_check_requested_actions=True,
)
)

Expand Down
1 change: 1 addition & 0 deletions python/plugins/praisonai/composio_praisonai/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def execute_tool(self, tool_identifier, params):
action=Action(value=tool_identifier),
params=params,
entity_id=self.entity_id,
_check_requested_actions=True,
)

def _process_basetool(
Expand Down
17 changes: 14 additions & 3 deletions python/tests/test_tools/test_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,12 @@ def test_invalid_handle_tool_calls() -> None:
toolset.get_tools(actions=[Action.GMAIL_FETCH_EMAILS])
with pytest.raises(ComposioSDKError) as exc:
with mock.patch.object(toolset, "_execute_remote"):
toolset.execute_action(Action.HACKERNEWS_GET_FRONTPAGE, {})
toolset.execute_action(
Action.HACKERNEWS_GET_FRONTPAGE,
{},
# This is passed as True by all tools
_check_requested_actions=True,
)

assert (
"Action HACKERNEWS_GET_FRONTPAGE is being called, but was never requested by the toolset."
Expand All @@ -553,9 +558,15 @@ def test_invalid_handle_tool_calls() -> None:
# Ensure it does NOT fail if a subsequent get_tools added that action
toolset.get_tools(actions=[Action.HACKERNEWS_GET_FRONTPAGE])
with mock.patch.object(toolset, "_execute_remote"):
toolset.execute_action(Action.HACKERNEWS_GET_FRONTPAGE, {})
toolset.execute_action(
Action.HACKERNEWS_GET_FRONTPAGE,
{},
# This is passed as True by all tools
_check_requested_actions=True,
)

# Ensure it DOES NOT fail if get_tools is never called
# Ensure it DOES NOT fail if execute_action is called manually, not by a tool
toolset = LangchainToolSet()
toolset.get_tools(actions=[Action.GMAIL_FETCH_EMAILS])
with mock.patch.object(toolset, "_execute_remote"):
toolset.execute_action(Action.HACKERNEWS_GET_FRONTPAGE, {})
Loading