mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-01 07:13:00 +00:00
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:
@@ -4,8 +4,9 @@ from typing import List, Any, Dict, Optional
|
|||||||
import json
|
import json
|
||||||
from crewai.tools import BaseTool
|
from crewai.tools import BaseTool
|
||||||
|
|
||||||
|
# DEFAULTS
|
||||||
ENTERPRISE_ACTION_KIT_PROJECT_ID = "dd525517-df22-49d2-a69e-6a0eed211166"
|
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):
|
class EnterpriseActionTool(BaseTool):
|
||||||
@@ -18,6 +19,12 @@ class EnterpriseActionTool(BaseTool):
|
|||||||
action_schema: Dict[str, Any] = Field(
|
action_schema: Dict[str, Any] = Field(
|
||||||
default={}, description="The schema of the action"
|
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__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -26,6 +33,8 @@ class EnterpriseActionTool(BaseTool):
|
|||||||
enterprise_action_token: str,
|
enterprise_action_token: str,
|
||||||
action_name: str,
|
action_name: str,
|
||||||
action_schema: Dict[str, Any],
|
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 = (
|
schema_props = (
|
||||||
action_schema.get("function", {})
|
action_schema.get("function", {})
|
||||||
@@ -74,12 +83,17 @@ class EnterpriseActionTool(BaseTool):
|
|||||||
self.action_name = action_name
|
self.action_name = action_name
|
||||||
self.action_schema = action_schema
|
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:
|
def _run(self, **kwargs) -> str:
|
||||||
"""Execute the specific enterprise action with validated parameters."""
|
"""Execute the specific enterprise action with validated parameters."""
|
||||||
try:
|
try:
|
||||||
params = {k: v for k, v in kwargs.items() if v is not None}
|
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 = {
|
headers = {
|
||||||
"Authorization": f"Bearer {self.enterprise_action_token}",
|
"Authorization": f"Bearer {self.enterprise_action_token}",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -104,7 +118,12 @@ class EnterpriseActionTool(BaseTool):
|
|||||||
class EnterpriseActionKitToolAdapter:
|
class EnterpriseActionKitToolAdapter:
|
||||||
"""Adapter that creates BaseTool instances for enterprise actions."""
|
"""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."""
|
"""Initialize the adapter with an enterprise action token."""
|
||||||
if not enterprise_action_token:
|
if not enterprise_action_token:
|
||||||
raise ValueError("enterprise_action_token is required")
|
raise ValueError("enterprise_action_token is required")
|
||||||
@@ -112,6 +131,8 @@ class EnterpriseActionKitToolAdapter:
|
|||||||
self.enterprise_action_token = enterprise_action_token
|
self.enterprise_action_token = enterprise_action_token
|
||||||
self._actions_schema = {}
|
self._actions_schema = {}
|
||||||
self._tools = None
|
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]:
|
def tools(self) -> List[BaseTool]:
|
||||||
"""Get the list of tools created from enterprise actions.
|
"""Get the list of tools created from enterprise actions.
|
||||||
@@ -127,7 +148,7 @@ class EnterpriseActionKitToolAdapter:
|
|||||||
def _fetch_actions(self):
|
def _fetch_actions(self):
|
||||||
"""Fetch available actions from the API."""
|
"""Fetch available actions from the API."""
|
||||||
try:
|
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}"}
|
headers = {"Authorization": f"Bearer {self.enterprise_action_token}"}
|
||||||
params = {"format": "json_schema"}
|
params = {"format": "json_schema"}
|
||||||
|
|
||||||
@@ -189,6 +210,8 @@ class EnterpriseActionKitToolAdapter:
|
|||||||
action_name=action_name,
|
action_name=action_name,
|
||||||
action_schema=action_schema,
|
action_schema=action_schema,
|
||||||
enterprise_action_token=self.enterprise_action_token,
|
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)
|
tools.append(tool)
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ from crewai_tools.adapters.enterprise_adapter import EnterpriseActionKitToolAdap
|
|||||||
|
|
||||||
def CrewaiEnterpriseTools(
|
def CrewaiEnterpriseTools(
|
||||||
enterprise_token: t.Optional[str] = None,
|
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]:
|
) -> t.List[BaseTool]:
|
||||||
"""Factory function that returns crewai enterprise tools.
|
"""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."
|
"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()
|
all_tools = adapter.tools()
|
||||||
|
|
||||||
if actions_list is None:
|
if actions_list is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user