From 8723e66807f73bdad7115a26efddee5ff87955e6 Mon Sep 17 00:00:00 2001 From: Lorenze Jay <63378463+lorenzejay@users.noreply.github.com> Date: Wed, 25 Jun 2025 11:14:41 -0700 Subject: [PATCH] feat: add support for parsing actions list from environment variables (#346) * feat: add support for parsing actions list from environment variables This commit introduces a new function, _parse_actions_list, to handle the parsing of a string representation of a list of tool names from environment variables. The CrewaiEnterpriseTools now utilizes this function to filter tools based on the parsed actions list, enhancing flexibility in tool selection. Additionally, a new test case is added to verify the correct usage of the environment actions list. * test: simplify environment actions list test setup This commit refactors the test for CrewaiEnterpriseTools to streamline the setup of environment variables. The environment token and actions list are now set in a single patch.dict call, improving readability and reducing redundancy in the test code. --- .../crewai_enterprise_tools.py | 28 ++++++++++++++++++- tests/tools/crewai_enterprise_tools_test.py | 13 +++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/crewai_tools/tools/crewai_enterprise_tools/crewai_enterprise_tools.py b/src/crewai_tools/tools/crewai_enterprise_tools/crewai_enterprise_tools.py index 302d34164..0a56dee67 100644 --- a/src/crewai_tools/tools/crewai_enterprise_tools/crewai_enterprise_tools.py +++ b/src/crewai_tools/tools/crewai_enterprise_tools/crewai_enterprise_tools.py @@ -5,6 +5,7 @@ Crewai Enterprise Tools import os import typing as t import logging +import json from crewai.tools import BaseTool from crewai_tools.adapters.enterprise_adapter import EnterpriseActionKitToolAdapter from crewai_tools.adapters.tool_collection import ToolCollection @@ -50,6 +51,31 @@ def CrewaiEnterpriseTools( adapter = EnterpriseActionKitToolAdapter(**adapter_kwargs) all_tools = adapter.tools() + parsed_actions_list = _parse_actions_list(actions_list) # Filter tools based on the provided list - return ToolCollection(all_tools).filter_by_names(actions_list) + return ToolCollection(all_tools).filter_by_names(parsed_actions_list) + + +# ENTERPRISE INJECTION ONLY +def _parse_actions_list(actions_list: t.Optional[t.List[str]]) -> t.List[str] | None: + """Parse a string representation of a list of tool names to a list of tool names. + + Args: + actions_list: A string representation of a list of tool names. + + Returns: + A list of tool names. + """ + if actions_list is not None: + return actions_list + + actions_list_from_env = os.environ.get("CREWAI_ENTERPRISE_TOOLS_ACTIONS_LIST") + if actions_list_from_env is None: + return None + + try: + return json.loads(actions_list_from_env) + except json.JSONDecodeError: + logger.warning(f"Failed to parse actions_list as JSON: {actions_list_from_env}") + return None diff --git a/tests/tools/crewai_enterprise_tools_test.py b/tests/tools/crewai_enterprise_tools_test.py index c49d35afb..d7a868472 100644 --- a/tests/tools/crewai_enterprise_tools_test.py +++ b/tests/tools/crewai_enterprise_tools_test.py @@ -73,3 +73,16 @@ class TestCrewaiEnterpriseTools(unittest.TestCase): def test_uses_environment_token_when_no_token_provided(self): CrewaiEnterpriseTools(enterprise_token="") self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token") + + @patch.dict( + os.environ, + { + "CREWAI_ENTERPRISE_TOOLS_TOKEN": "env-token", + "CREWAI_ENTERPRISE_TOOLS_ACTIONS_LIST": '["tool1", "tool3"]', + }, + ) + def test_uses_environment_actions_list(self): + tools = CrewaiEnterpriseTools() + self.assertEqual(len(tools), 2) + self.assertEqual(tools[0].name, "tool1") + self.assertEqual(tools[1].name, "tool3")