Skip to content

Commit 7531d19

Browse files
authored
feat: add support for parsing actions list from environment variables (crewAIInc#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.
1 parent 6496d41 commit 7531d19

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

crewai_tools/tools/crewai_enterprise_tools/crewai_enterprise_tools.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import typing as t
77
import logging
8+
import json
89
from crewai.tools import BaseTool
910
from crewai_tools.adapters.enterprise_adapter import EnterpriseActionKitToolAdapter
1011
from crewai_tools.adapters.tool_collection import ToolCollection
@@ -50,6 +51,31 @@ def CrewaiEnterpriseTools(
5051

5152
adapter = EnterpriseActionKitToolAdapter(**adapter_kwargs)
5253
all_tools = adapter.tools()
54+
parsed_actions_list = _parse_actions_list(actions_list)
5355

5456
# Filter tools based on the provided list
55-
return ToolCollection(all_tools).filter_by_names(actions_list)
57+
return ToolCollection(all_tools).filter_by_names(parsed_actions_list)
58+
59+
60+
# ENTERPRISE INJECTION ONLY
61+
def _parse_actions_list(actions_list: t.Optional[t.List[str]]) -> t.List[str] | None:
62+
"""Parse a string representation of a list of tool names to a list of tool names.
63+
64+
Args:
65+
actions_list: A string representation of a list of tool names.
66+
67+
Returns:
68+
A list of tool names.
69+
"""
70+
if actions_list is not None:
71+
return actions_list
72+
73+
actions_list_from_env = os.environ.get("CREWAI_ENTERPRISE_TOOLS_ACTIONS_LIST")
74+
if actions_list_from_env is None:
75+
return None
76+
77+
try:
78+
return json.loads(actions_list_from_env)
79+
except json.JSONDecodeError:
80+
logger.warning(f"Failed to parse actions_list as JSON: {actions_list_from_env}")
81+
return None

tests/tools/crewai_enterprise_tools_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,16 @@ def test_uses_environment_token(self):
7373
def test_uses_environment_token_when_no_token_provided(self):
7474
CrewaiEnterpriseTools(enterprise_token="")
7575
self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token")
76+
77+
@patch.dict(
78+
os.environ,
79+
{
80+
"CREWAI_ENTERPRISE_TOOLS_TOKEN": "env-token",
81+
"CREWAI_ENTERPRISE_TOOLS_ACTIONS_LIST": '["tool1", "tool3"]',
82+
},
83+
)
84+
def test_uses_environment_actions_list(self):
85+
tools = CrewaiEnterpriseTools()
86+
self.assertEqual(len(tools), 2)
87+
self.assertEqual(tools[0].name, "tool1")
88+
self.assertEqual(tools[1].name, "tool3")

0 commit comments

Comments
 (0)