From 32e90bfcfcc5adab1d0b883297e84dfafcc8402f Mon Sep 17 00:00:00 2001 From: Viraj <35092918+angrybayblade@users.noreply.github.com> Date: Mon, 23 Dec 2024 15:18:29 +0530 Subject: [PATCH] fix: limit action name chars (#1076) --- python/composio/tools/toolset.py | 5 ++ .../langchain/composio_langchain/toolset.py | 1 + python/tests/test_example.py | 3 +- python/tests/test_tools/test_toolset.py | 62 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/python/composio/tools/toolset.py b/python/composio/tools/toolset.py index e140d844053..6ddc471f5fe 100644 --- a/python/composio/tools/toolset.py +++ b/python/composio/tools/toolset.py @@ -136,12 +136,14 @@ class ComposioToolSet(WithLogger): # pylint: disable=too-many-public-methods _runtime: str = "composio" _description_char_limit: int = 1024 + _action_name_char_limit: t.Optional[int] = None _log_ingester_client: t.Optional[LogIngester] = None def __init_subclass__( cls, runtime: t.Optional[str] = None, description_char_limit: t.Optional[int] = None, + action_name_char_limit: t.Optional[int] = None, ) -> None: if runtime is None: warnings.warn( @@ -154,6 +156,7 @@ def __init_subclass__( f"description_char_limit is not set on {cls.__name__}, using 1024 as default" ) cls._description_char_limit = description_char_limit or 1024 + cls._action_name_char_limit = action_name_char_limit def __init__( self, @@ -987,6 +990,8 @@ def _process_schema(self, action_item: ActionModel) -> ActionModel: action=Action(action_item.name.upper()), properties=action_item.parameters.properties, ) + if self._action_name_char_limit is not None: + action_item.name = action_item.name[: self._action_name_char_limit] return action_item def create_trigger_listener(self, timeout: float = 15.0) -> TriggerSubscription: diff --git a/python/plugins/langchain/composio_langchain/toolset.py b/python/plugins/langchain/composio_langchain/toolset.py index 6132dd4ea37..d517569c27c 100644 --- a/python/plugins/langchain/composio_langchain/toolset.py +++ b/python/plugins/langchain/composio_langchain/toolset.py @@ -29,6 +29,7 @@ class ComposioToolSet( BaseComposioToolSet, runtime="langchain", description_char_limit=1024, + action_name_char_limit=64, ): """ Composio toolset for Langchain framework. diff --git a/python/tests/test_example.py b/python/tests/test_example.py index 8e539e70f96..b1fe77e1f27 100644 --- a/python/tests/test_example.py +++ b/python/tests/test_example.py @@ -143,7 +143,8 @@ "file": EXAMPLES_PATH / "quickstarters" / "sql_agent" - / "sql_agent_plotter_crewai" / "run_issue.py", + / "sql_agent_plotter_crewai" + / "run_issue.py", "match": { "type": "stdout", "values": ["composio_output/CODEINTERPRETER_GET_FILE_CMD_default_"], diff --git a/python/tests/test_tools/test_toolset.py b/python/tests/test_tools/test_toolset.py index 046ef05a032..d914ee8a29f 100644 --- a/python/tests/test_tools/test_toolset.py +++ b/python/tests/test_tools/test_toolset.py @@ -4,6 +4,7 @@ import logging import re +import typing as t from unittest import mock import pytest @@ -328,3 +329,64 @@ def test_execute_action_param_serialization() -> None: text=None, session_id=mock.ANY, ) + + +class TestSubclassInit: + + def test_runtime(self): + + class SomeToolsetExtention(ComposioToolSet): + pass + + assert ( + SomeToolsetExtention._runtime # pylint: disable=protected-access + == "composio" + ) + + class SomeOtherToolsetExtention(ComposioToolSet, runtime="some_toolset"): + pass + + assert ( + SomeOtherToolsetExtention._runtime # pylint: disable=protected-access + == "some_toolset" + ) + + def test_description_char_limit(self) -> None: + + char_limit = 512 + (schema,) = ComposioToolSet().get_action_schemas( + actions=[ + Action.FILETOOL_GIT_CLONE, + ] + ) + assert len(t.cast(str, schema.description)) > char_limit + + class SomeToolsetExtention(ComposioToolSet, description_char_limit=char_limit): + pass + + (schema,) = SomeToolsetExtention().get_action_schemas( + actions=[ + Action.FILETOOL_GIT_CLONE, + ] + ) + assert len(t.cast(str, schema.description)) == char_limit + + def test_action_name_char_limit(self) -> None: + + char_limit = 12 + (schema,) = ComposioToolSet().get_action_schemas( + actions=[ + Action.FILETOOL_GIT_CLONE, + ] + ) + assert len(t.cast(str, schema.name)) > char_limit + + class SomeToolsetExtention(ComposioToolSet, action_name_char_limit=char_limit): + pass + + (schema,) = SomeToolsetExtention().get_action_schemas( + actions=[ + Action.FILETOOL_GIT_CLONE, + ] + ) + assert len(t.cast(str, schema.name)) == char_limit