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
This commit is contained in:
Lorenze Jay
2025-06-25 10:17:26 -07:00
committed by GitHub
parent 03917411b4
commit e4cb8bf797
3 changed files with 25 additions and 9 deletions

View File

@@ -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"}

View File

@@ -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}

View File

@@ -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")
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")