From e4cb8bf797ff882843f8fb247d48744bfb102f32 Mon Sep 17 00:00:00 2001 From: Lorenze Jay <63378463+lorenzejay@users.noreply.github.com> Date: Wed, 25 Jun 2025 10:17:26 -0700 Subject: [PATCH] Lorenze/better env vars setup enterprise tools (#343) * refactor: remove token validation from EnterpriseActionKitToolAdapter and CrewaiEnterpriseTools This commit simplifies the initialization of the EnterpriseActionKitToolAdapter and CrewaiEnterpriseTools by removing the explicit validation for the enterprise action token. The token can now be set to None without raising an error, allowing for more flexible usage. * added loggers for monitoring * fixed typo * fix: enhance token handling in EnterpriseActionKitToolAdapter and CrewaiEnterpriseTools This commit improves the handling of the enterprise action token by allowing it to be fetched from environment variables if not provided. It adds checks to ensure the token is set before making API requests, enhancing robustness and flexibility. * removed redundancy * test: add new test for environment token fallback in CrewaiEnterpriseTools This update introduces a new test case to verify that the environment token is used when no token is provided during the initialization of CrewaiEnterpriseTools. Additionally, minor formatting adjustments were made to existing assertions for consistency. * test: update environment token test to clear environment variables This change modifies the test for CrewaiEnterpriseTools to ensure that the environment variables are cleared before setting the test token. This ensures a clean test environment and prevents potential interference from other tests. * drop redundancy --- src/crewai_tools/adapters/enterprise_adapter.py | 15 ++++++++++++--- .../crewai_enterprise_tools.py | 6 ++++-- tests/tools/crewai_enterprise_tools_test.py | 13 +++++++++---- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/crewai_tools/adapters/enterprise_adapter.py b/src/crewai_tools/adapters/enterprise_adapter.py index 34238602e..6799d7ea8 100644 --- a/src/crewai_tools/adapters/enterprise_adapter.py +++ b/src/crewai_tools/adapters/enterprise_adapter.py @@ -1,7 +1,8 @@ -import requests -from pydantic import Field, create_model -from typing import List, Any, Dict, Optional +import os import json +import requests +from typing import List, Any, Dict, Optional +from pydantic import Field, create_model from crewai.tools import BaseTool # DEFAULTS @@ -146,6 +147,14 @@ class EnterpriseActionKitToolAdapter: def _fetch_actions(self): """Fetch available actions from the API.""" try: + if ( + self.enterprise_action_token is None + or self.enterprise_action_token == "" + ): + self.enterprise_action_token = os.environ.get( + "CREWAI_ENTERPRISE_TOOLS_TOKEN" + ) + actions_url = f"{self.enterprise_action_kit_project_url}/{self.enterprise_action_kit_project_id}/actions" headers = {"Authorization": f"Bearer {self.enterprise_action_token}"} params = {"format": "json_schema"} 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 e531afeed..302d34164 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 @@ -31,9 +31,11 @@ def CrewaiEnterpriseTools( Returns: A ToolCollection of BaseTool instances for enterprise actions """ - if enterprise_token is None: + + if enterprise_token is None or enterprise_token == "": enterprise_token = os.environ.get("CREWAI_ENTERPRISE_TOOLS_TOKEN") - logger.warning("No enterprise token provided") + if not enterprise_token: + logger.warning("No enterprise token provided") adapter_kwargs = {"enterprise_action_token": enterprise_token} diff --git a/tests/tools/crewai_enterprise_tools_test.py b/tests/tools/crewai_enterprise_tools_test.py index 7a649028d..c49d35afb 100644 --- a/tests/tools/crewai_enterprise_tools_test.py +++ b/tests/tools/crewai_enterprise_tools_test.py @@ -15,7 +15,7 @@ class TestCrewaiEnterpriseTools(unittest.TestCase): self._create_mock_tool("tool3", "Tool 3 Description"), ] self.adapter_patcher = patch( - 'crewai_tools.tools.crewai_enterprise_tools.crewai_enterprise_tools.EnterpriseActionKitToolAdapter' + "crewai_tools.tools.crewai_enterprise_tools.crewai_enterprise_tools.EnterpriseActionKitToolAdapter" ) self.MockAdapter = self.adapter_patcher.start() @@ -55,16 +55,21 @@ class TestCrewaiEnterpriseTools(unittest.TestCase): CrewaiEnterpriseTools( enterprise_token="test-token", enterprise_action_kit_project_id="project-id", - enterprise_action_kit_project_url="project-url" + enterprise_action_kit_project_url="project-url", ) self.MockAdapter.assert_called_once_with( enterprise_action_token="test-token", enterprise_action_kit_project_id="project-id", - enterprise_action_kit_project_url="project-url" + enterprise_action_kit_project_url="project-url", ) @patch.dict(os.environ, {"CREWAI_ENTERPRISE_TOOLS_TOKEN": "env-token"}) def test_uses_environment_token(self): CrewaiEnterpriseTools() - self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token") \ No newline at end of file + self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token") + + @patch.dict(os.environ, {"CREWAI_ENTERPRISE_TOOLS_TOKEN": "env-token"}) + def test_uses_environment_token_when_no_token_provided(self): + CrewaiEnterpriseTools(enterprise_token="") + self.MockAdapter.assert_called_once_with(enterprise_action_token="env-token")