Enhance EnterpriseActionKitToolAdapter to support custom project IDs (#297)

* Enhance EnterpriseActionKitToolAdapter to support custom project IDs

- Updated the EnterpriseActionKitToolAdapter and EnterpriseActionTool classes to accept an optional project_id parameter, allowing for greater flexibility in API interactions.
- Modified API URL construction to utilize the provided project_id instead of a hardcoded default.
- Updated the CrewaiEnterpriseTools factory function to accept and pass the project_id to the adapter.

* for factory in mind
This commit is contained in:
Lorenze Jay
2025-05-04 10:41:54 -07:00
committed by GitHub
parent 67be0c674d
commit 93d043bcd4
2 changed files with 42 additions and 6 deletions

View File

@@ -4,8 +4,9 @@ from typing import List, Any, Dict, Optional
import json
from crewai.tools import BaseTool
# DEFAULTS
ENTERPRISE_ACTION_KIT_PROJECT_ID = "dd525517-df22-49d2-a69e-6a0eed211166"
ENTERPRISE_ACTION_KIT_PROJECT_URL = "https://worker-actionkit.tools.crewai.com/projects"
class EnterpriseActionTool(BaseTool):
@@ -18,6 +19,12 @@ class EnterpriseActionTool(BaseTool):
action_schema: Dict[str, Any] = Field(
default={}, description="The schema of the action"
)
enterprise_action_kit_project_id: str = Field(
default=ENTERPRISE_ACTION_KIT_PROJECT_ID, description="The project id"
)
enterprise_action_kit_project_url: str = Field(
default=ENTERPRISE_ACTION_KIT_PROJECT_URL, description="The project url"
)
def __init__(
self,
@@ -26,6 +33,8 @@ class EnterpriseActionTool(BaseTool):
enterprise_action_token: str,
action_name: str,
action_schema: Dict[str, Any],
enterprise_action_kit_project_url: str = ENTERPRISE_ACTION_KIT_PROJECT_URL,
enterprise_action_kit_project_id: str = ENTERPRISE_ACTION_KIT_PROJECT_ID,
):
schema_props = (
action_schema.get("function", {})
@@ -74,12 +83,17 @@ class EnterpriseActionTool(BaseTool):
self.action_name = action_name
self.action_schema = action_schema
if enterprise_action_kit_project_id is not None:
self.enterprise_action_kit_project_id = enterprise_action_kit_project_id
if enterprise_action_kit_project_url is not None:
self.enterprise_action_kit_project_url = enterprise_action_kit_project_url
def _run(self, **kwargs) -> str:
"""Execute the specific enterprise action with validated parameters."""
try:
params = {k: v for k, v in kwargs.items() if v is not None}
api_url = f"https://worker-actionkit.tools.crewai.com/projects/{ENTERPRISE_ACTION_KIT_PROJECT_ID}/actions"
api_url = f"{self.enterprise_action_kit_project_url}/{self.enterprise_action_kit_project_id}/actions"
headers = {
"Authorization": f"Bearer {self.enterprise_action_token}",
"Content-Type": "application/json",
@@ -104,7 +118,12 @@ class EnterpriseActionTool(BaseTool):
class EnterpriseActionKitToolAdapter:
"""Adapter that creates BaseTool instances for enterprise actions."""
def __init__(self, enterprise_action_token: str):
def __init__(
self,
enterprise_action_token: str,
enterprise_action_kit_project_url: str = ENTERPRISE_ACTION_KIT_PROJECT_URL,
enterprise_action_kit_project_id: str = ENTERPRISE_ACTION_KIT_PROJECT_ID,
):
"""Initialize the adapter with an enterprise action token."""
if not enterprise_action_token:
raise ValueError("enterprise_action_token is required")
@@ -112,6 +131,8 @@ class EnterpriseActionKitToolAdapter:
self.enterprise_action_token = enterprise_action_token
self._actions_schema = {}
self._tools = None
self.enterprise_action_kit_project_id = enterprise_action_kit_project_id
self.enterprise_action_kit_project_url = enterprise_action_kit_project_url
def tools(self) -> List[BaseTool]:
"""Get the list of tools created from enterprise actions.
@@ -127,7 +148,7 @@ class EnterpriseActionKitToolAdapter:
def _fetch_actions(self):
"""Fetch available actions from the API."""
try:
actions_url = f"https://worker-actionkit.tools.crewai.com/projects/{ENTERPRISE_ACTION_KIT_PROJECT_ID}/actions"
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"}
@@ -189,6 +210,8 @@ class EnterpriseActionKitToolAdapter:
action_name=action_name,
action_schema=action_schema,
enterprise_action_token=self.enterprise_action_token,
enterprise_action_kit_project_id=self.enterprise_action_kit_project_id,
enterprise_action_kit_project_url=self.enterprise_action_kit_project_url,
)
tools.append(tool)

View File

@@ -10,7 +10,9 @@ from crewai_tools.adapters.enterprise_adapter import EnterpriseActionKitToolAdap
def CrewaiEnterpriseTools(
enterprise_token: t.Optional[str] = None,
actions_list: t.Optional[t.List[str]] = None
actions_list: t.Optional[t.List[str]] = None,
enterprise_action_kit_project_id: t.Optional[str] = None,
enterprise_action_kit_project_url: t.Optional[str] = None,
) -> t.List[BaseTool]:
"""Factory function that returns crewai enterprise tools.
@@ -30,7 +32,18 @@ def CrewaiEnterpriseTools(
"No enterprise token provided. Please provide a token or set the CREWAI_ENTEPRISE_TOOLS_TOKEN environment variable."
)
adapter = EnterpriseActionKitToolAdapter(enterprise_token)
adapter_kwargs = {"enterprise_action_token": enterprise_token}
if enterprise_action_kit_project_id is not None:
adapter_kwargs["enterprise_action_kit_project_id"] = (
enterprise_action_kit_project_id
)
if enterprise_action_kit_project_url is not None:
adapter_kwargs["enterprise_action_kit_project_url"] = (
enterprise_action_kit_project_url
)
adapter = EnterpriseActionKitToolAdapter(**adapter_kwargs)
all_tools = adapter.tools()
if actions_list is None: