Skip to content

Commit

Permalink
fix: limit action name chars (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybayblade authored Dec 23, 2024
1 parent 4bad19c commit 32e90bf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions python/composio/tools/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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:
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 @@ -29,6 +29,7 @@ class ComposioToolSet(
BaseComposioToolSet,
runtime="langchain",
description_char_limit=1024,
action_name_char_limit=64,
):
"""
Composio toolset for Langchain framework.
Expand Down
3 changes: 2 additions & 1 deletion python/tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_"],
Expand Down
62 changes: 62 additions & 0 deletions python/tests/test_tools/test_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
import re
import typing as t
from unittest import mock

import pytest
Expand Down Expand Up @@ -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

0 comments on commit 32e90bf

Please sign in to comment.