From a954584590e5427c988ebc212ba9f82b6c5c5022 Mon Sep 17 00:00:00 2001 From: Lucas Gomide Date: Fri, 6 Jun 2025 09:44:26 -0300 Subject: [PATCH] feat: filtering agents, tools and their actions by organization_uuid if present --- src/crewai/cli/plus_api.py | 22 ++++++- tests/cli/test_plus_api.py | 115 ++++++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 6 deletions(-) diff --git a/src/crewai/cli/plus_api.py b/src/crewai/cli/plus_api.py index ca429c04d..4d30e97b3 100644 --- a/src/crewai/cli/plus_api.py +++ b/src/crewai/cli/plus_api.py @@ -4,6 +4,7 @@ from urllib.parse import urljoin import requests +from crewai.cli.config import Settings from crewai.cli.version import get_crewai_version @@ -34,13 +35,25 @@ class PlusAPI: return session.request(method, url, headers=self.headers, **kwargs) def login_to_tool_repository(self): - return self._make_request("POST", f"{self.TOOLS_RESOURCE}/login") + settings = Settings() + payload = {} + if settings.org_uuid: + payload["organization_uuid"] = settings.org_uuid + return self._make_request("POST", f"{self.TOOLS_RESOURCE}/login", json=payload) def get_tool(self, handle: str): - return self._make_request("GET", f"{self.TOOLS_RESOURCE}/{handle}") + settings = Settings() + params = {} + if settings.org_uuid: + params["organization_uuid"] = settings.org_uuid + return self._make_request("GET", f"{self.TOOLS_RESOURCE}/{handle}", params=params) def get_agent(self, handle: str): - return self._make_request("GET", f"{self.AGENTS_RESOURCE}/{handle}") + settings = Settings() + params = {} + if settings.org_uuid: + params["organization_uuid"] = settings.org_uuid + return self._make_request("GET", f"{self.AGENTS_RESOURCE}/{handle}", params=params) def publish_tool( self, @@ -59,6 +72,9 @@ class PlusAPI: "description": description, "available_exports": available_exports, } + settings = Settings() + if settings.org_uuid: + params["organization_uuid"] = settings.org_uuid return self._make_request("POST", f"{self.TOOLS_RESOURCE}", json=params) def deploy_by_name(self, project_name: str) -> requests.Response: diff --git a/tests/cli/test_plus_api.py b/tests/cli/test_plus_api.py index da26ba35f..905aa9183 100644 --- a/tests/cli/test_plus_api.py +++ b/tests/cli/test_plus_api.py @@ -2,6 +2,7 @@ import os import unittest from unittest.mock import MagicMock, patch +from typing import Optional, List from crewai.cli.plus_api import PlusAPI @@ -25,19 +26,89 @@ class TestPlusAPI(unittest.TestCase): response = self.api.login_to_tool_repository() mock_make_request.assert_called_once_with( - "POST", "/crewai_plus/api/v1/tools/login" + "POST", "/crewai_plus/api/v1/tools/login", json={} + ) + self.assertEqual(response, mock_response) + + @patch("crewai.cli.plus_api.Settings") + @patch("crewai.cli.plus_api.PlusAPI._make_request") + def test_login_to_tool_repository_with_org_uuid(self, mock_make_request, mock_settings_class): + mock_settings = MagicMock() + mock_settings.org_uuid = "test-org-uuid" + mock_settings_class.return_value = mock_settings + + mock_response = MagicMock() + mock_make_request.return_value = mock_response + + response = self.api.login_to_tool_repository() + + mock_make_request.assert_called_once_with( + "POST", "/crewai_plus/api/v1/tools/login", + json={"organization_uuid": "test-org-uuid"} ) self.assertEqual(response, mock_response) + @patch("crewai.cli.plus_api.PlusAPI._make_request") + def test_get_agent(self, mock_make_request): + mock_response = MagicMock() + mock_make_request.return_value = mock_response + + response = self.api.get_agent("test_agent_handle") + mock_make_request.assert_called_once_with( + "GET", "/crewai_plus/api/v1/agents/test_agent_handle", params={} + ) + self.assertEqual(response, mock_response) + + @patch("crewai.cli.plus_api.Settings") + @patch("crewai.cli.plus_api.PlusAPI._make_request") + def test_get_agent_with_org_uuid(self, mock_make_request, mock_settings_class): + # Set up mock settings with org_uuid + mock_settings = MagicMock() + mock_settings.org_uuid = "test-org-uuid" + mock_settings_class.return_value = mock_settings + + # Set up mock response + mock_response = MagicMock() + mock_make_request.return_value = mock_response + + response = self.api.get_agent("test_agent_handle") + + # Verify the params include the organization_uuid + mock_make_request.assert_called_once_with( + "GET", "/crewai_plus/api/v1/agents/test_agent_handle", + params={"organization_uuid": "test-org-uuid"} + ) + self.assertEqual(response, mock_response) + @patch("crewai.cli.plus_api.PlusAPI._make_request") def test_get_tool(self, mock_make_request): mock_response = MagicMock() mock_make_request.return_value = mock_response response = self.api.get_tool("test_tool_handle") - mock_make_request.assert_called_once_with( - "GET", "/crewai_plus/api/v1/tools/test_tool_handle" + "GET", "/crewai_plus/api/v1/tools/test_tool_handle", params={} + ) + self.assertEqual(response, mock_response) + + @patch("crewai.cli.plus_api.Settings") + @patch("crewai.cli.plus_api.PlusAPI._make_request") + def test_get_tool_with_org_uuid(self, mock_make_request, mock_settings_class): + # Set up mock settings with org_uuid + mock_settings = MagicMock() + mock_settings.org_uuid = "test-org-uuid" + mock_settings_class.return_value = mock_settings + + # Set up mock response + mock_response = MagicMock() + mock_make_request.return_value = mock_response + + response = self.api.get_tool("test_tool_handle") + + # Verify the params include the organization_uuid + mock_make_request.assert_called_once_with( + "GET", "/crewai_plus/api/v1/tools/test_tool_handle", + params={"organization_uuid": "test-org-uuid"} ) self.assertEqual(response, mock_response) @@ -67,6 +138,44 @@ class TestPlusAPI(unittest.TestCase): "POST", "/crewai_plus/api/v1/tools", json=params ) self.assertEqual(response, mock_response) + + @patch("crewai.cli.plus_api.Settings") + @patch("crewai.cli.plus_api.PlusAPI._make_request") + def test_publish_tool_with_org_uuid(self, mock_make_request, mock_settings_class): + # Set up mock settings with org_uuid + mock_settings = MagicMock() + mock_settings.org_uuid = "test-org-uuid" + mock_settings_class.return_value = mock_settings + + # Set up mock response + mock_response = MagicMock() + mock_make_request.return_value = mock_response + + handle = "test_tool_handle" + public = True + version = "1.0.0" + description = "Test tool description" + encoded_file = "encoded_test_file" + + response = self.api.publish_tool( + handle, public, version, description, encoded_file + ) + + # Expected params including organization_uuid + expected_params = { + "handle": handle, + "public": public, + "version": version, + "file": encoded_file, + "description": description, + "available_exports": None, + "organization_uuid": "test-org-uuid", + } + + mock_make_request.assert_called_once_with( + "POST", "/crewai_plus/api/v1/tools", json=expected_params + ) + self.assertEqual(response, mock_response) @patch("crewai.cli.plus_api.PlusAPI._make_request") def test_publish_tool_without_description(self, mock_make_request):