AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
refactor(backend/copilot): delete find_block, the tool nothing calls (#14648)
Nicholas Tindle44066613cbbb
diff --git a/autogpt_platform/backend/backend/copilot/tools/find_block.py b/autogpt_platform/backend/backend/copilot/tools/find_block.pydeleted file mode 100644index 883767754d..0000000000--- a/autogpt_platform/backend/backend/copilot/tools/find_block.py+++ /dev/null@@ -1,314 +0,0 @@-import logging-import re-from typing import Any--from prisma.enums import ContentType--from backend.blocks import get_block, get_blocks-from backend.blocks._base import BlockType-from backend.copilot.capabilities.block_meta import (- COPILOT_EXCLUDED_BLOCK_IDS,- COPILOT_EXCLUDED_BLOCK_TYPES,-)-from backend.copilot.context import get_current_permissions-from backend.copilot.model import ChatSession-from backend.data.db_accessors import search--from .base import BaseTool, ToolResponseBase-from .helpers import get_block_provider-from .models import (- BlockInfoSummary,- BlockListResponse,- ErrorResponse,- NoResultsResponse,-)-from .utils import is_uuid--logger = logging.getLogger(__name__)--_TARGET_RESULTS = 10-# Over-fetch to compensate for post-hoc filtering of graph-only blocks.-# 40 is 2x current removed; speed of query 10 vs 40 is minimial-_OVERFETCH_PAGE_SIZE = 40-# Cap on registry hits for queries that name a block class directly.-_MAX_EXACT_NAME_MATCHES = 3---class FindBlockTool(BaseTool):- """Tool for searching available blocks."""-- @property- def name(self) -> str:- return "find_block"-- @property- def description(self) -> str:- return (- "Search blocks by name or description. Returns block IDs for run_block. "- "Always call this FIRST to get block IDs before using run_block. "- "Then call run_block with the block's id and empty input_data to see its detailed schema."- )-- @property- def parameters(self) -> dict[str, Any]:- return {- "type": "object",- "properties": {- "query": {- "type": "string",- "description": "Search keywords (e.g. 'email', 'http', 'ai').",- },- "for_agent_generation": {- "type": "boolean",- "description": (- "Set to true when searching for blocks to use inside an agent graph "- "(e.g. AgentInputBlock, AgentOutputBlock, OrchestratorBlock). "- "Bypasses the CoPilot-only filter so graph-only blocks are visible."- ),- "default": False,- },- },- "required": ["query"],- }-- @property- def requires_auth(self) -> bool:- return True-- async def _execute(- self,- user_id: str | None,- session: ChatSession,- query: str = "",- for_agent_generation: bool = False,- **kwargs,- ) -> ToolResponseBase:- """Search for blocks matching the query.-- Args:- user_id: User ID (required)- session: Chat session- query: Search query- for_agent_generation: When True, bypasses the CoPilot exclusion filter- so graph-only blocks (INPUT, OUTPUT, ORCHESTRATOR, etc.) are visible.-- Returns:- BlockListResponse: List of matching blocks- NoResultsResponse: No blocks found- ErrorResponse: Error message- """- query = (query or "").strip()- session_id = session.session_id-- if not query:- return ErrorResponse(- message="Please provide a search query or block ID",- session_id=session_id,- )-- try:- # Direct ID lookup if query looks like a UUID- if is_uuid(query):- block = get_block(query.lower())- if block:- if block.disabled:- return NoResultsResponse(- message=f"Block '{block.name}' (ID: {block.id}) is disabled and cannot be used.",- suggestions=["Search for an alternative block by name"],- session_id=session_id,- )- is_excluded = (- block.block_type in COPILOT_EXCLUDED_BLOCK_TYPES- or block.id in COPILOT_EXCLUDED_BLOCK_IDS- )- if is_excluded:- # Graph-only blocks (INPUT, OUTPUT, MCP_TOOL, AGENT, etc.) are- # exposed when building an agent graph so the LLM can inspect- # their schemas and wire them as nodes. In CoPilot direct use- # they are not executable — guide the LLM to the right tool.- if not for_agent_generation:- if block.block_type == BlockType.MCP_TOOL:- message = (- f"Block '{block.name}' (ID: {block.id}) cannot be "- "run directly in CoPilot. Use run_mcp_tool for "- "interactive MCP execution, or call find_block with "- "for_agent_generation=true to embed it in an agent graph."- )- else:- message = (- f"Block '{block.name}' (ID: {block.id}) is not available "- "in CoPilot. It can only be used within agent graphs."- )- return NoResultsResponse(- message=message,- suggestions=[- "Search for an alternative block by name",- "Use this block in an agent graph instead",- ],- session_id=session_id,- )-- # Check block-level permissions — hide denied blocks entirely- perms = get_current_permissions()- if perms is not None and not perms.is_block_allowed(- block.id, block.name- ):- return NoResultsResponse(- message=f"No blocks found for '{query}'",- suggestions=[- "Search for an alternative block by name",- ],- session_id=session_id,- )-- summary = BlockInfoSummary(- id=block.id,- name=block.name,- description=(- block.optimized_description or block.description or ""- ),- categories=[c.value for c in block.categories],- provider=get_block_provider(block),- )- return BlockListResponse(- message=(- f"Found block '{block.name}' by ID. "- "To see inputs/outputs and execute it, use "- "run_block with the block's 'id' - providing "- "no inputs."- ),- blocks=[summary],- count=1,- query=query,- session_id=session_id,- )-- # Search for blocks using hybrid search- results, total = await search().unified_hybrid_search(- query=query,- content_types=[ContentType.BLOCK],- page=1,- page_size=_OVERFETCH_PAGE_SIZE,- )-- # Prepend exact class-name matches from the live registry. The- # hybrid search can miss or bury exact block names: CamelCase- # queries get no lexical signal (the index stores the split- # form), and index rows can lag behind block renames. Queries- # like "OrchestratorBlock" must always resolve.- exact_ids = _find_block_ids_by_name(query)- if exact_ids:- seen = set(exact_ids)- results = [{"content_id": bid} for bid in exact_ids] + [- r for r in results if r["content_id"] not in seen- ]-- if not results:- return NoResultsResponse(- message=f"No blocks found for '{query}'",- suggestions=[- "Try broader keywords like 'email', 'http', 'text', 'ai'",- "Check spelling of technical terms",- ],- session_id=session_id,- )-- # Enrich results with block information- perms = get_current_permissions()- blocks: list[BlockInfoSummary] = []- for result in results:- block_id = result["content_id"]- block = get_block(block_id)-- # Skip disabled blocks- if not block or block.disabled:- continue-- # Graph-only blocks (INPUT, OUTPUT, MCP_TOOL, AGENT, etc.) are- # skipped in CoPilot direct use but surfaced for agent graph building.- if not for_agent_generation and (- block.block_type in COPILOT_EXCLUDED_BLOCK_TYPES- or block.id in COPILOT_EXCLUDED_BLOCK_IDS- ):- continue-- # Skip blocks denied by execution permissions- if perms is not None and not perms.is_block_allowed(- block.id, block.name- ):- continue-- summary = BlockInfoSummary(- id=block_id,- name=block.name,- description=block.optimized_description or block.description or "",- categories=[c.value for c in block.categories],- provider=get_block_provider(block),- )-- blocks.append(summary)-- if len(blocks) >= _TARGET_RESULTS:- break-- if blocks and len(blocks) < _TARGET_RESULTS:- logger.debug(- "find_block returned %d/%d results for query '%s' "- "(filtered %d excluded/disabled blocks)",- len(blocks),- _TARGET_RESULTS,- query,- len(results) - len(blocks),- )-- if not blocks:- return NoResultsResponse(- message=f"No blocks found for '{query}'",- suggestions=[- "Try broader keywords like 'email', 'http', 'text', 'ai'",- ],- session_id=session_id,- )-- return BlockListResponse(- message=(- f"Found {len(blocks)} block(s) matching '{query}'. "- "To see a block's inputs/outputs and execute it, use run_block with the block's 'id' - providing no inputs."- ),- blocks=blocks,- count=len(blocks),- query=query,- session_id=session_id,- )-- except Exception as e:- logger.error(f"Error searching blocks: {e}", exc_info=True)- return ErrorResponse(- message="Failed to search blocks",- error=str(e),- session_id=session_id,- )---def _find_block_ids_by_name(query: str) -> list[str]:- """Resolve a query that names a block class to its block ID(s).-- Matches case-insensitively, ignoring spaces/punctuation, with an optional- "Block" suffix — so "OrchestratorBlock", "orchestrator block" and- "orchestrator" all resolve to OrchestratorBlock. Exclusion/permission- filtering is the caller's job (same path as regular search results).- """- normalized = _normalize_block_name(query)- if not normalized:- return []- matches = [- block_id- for block_id, block_cls in get_blocks().items()- if _normalize_block_name(block_cls.__name__)- in (normalized, f"{normalized}block")- ]- return matches[:_MAX_EXACT_NAME_MATCHES]---def _normalize_block_name(text: str) -> str:- return re.sub(r"[^a-z0-9]", "", text.lower())diff --git a/autogpt_platform/backend/backend/copilot/tools/find_block_test.py b/autogpt_platform/backend/backend/copilot/tools/find_block_test.pydeleted file mode 100644index f165f9a3e5..0000000000--- a/autogpt_platform/backend/backend/copilot/tools/find_block_test.py+++ /dev/null@@ -1,975 +0,0 @@-"""Tests for block filtering in FindBlockTool."""--from unittest.mock import AsyncMock, MagicMock, patch--import pytest--from backend.blocks._base import BlockType--from ._test_data import make_session-from .find_block import (- COPILOT_EXCLUDED_BLOCK_IDS,- COPILOT_EXCLUDED_BLOCK_TYPES,- FindBlockTool,- _find_block_ids_by_name,-)-from .models import BlockListResponse, NoResultsResponse--_TEST_USER_ID = "test-user-find-block"---def make_mock_block(- block_id: str,- name: str,- block_type: BlockType,- disabled: bool = False,- input_schema: dict | None = None,- output_schema: dict | None = None,- credentials_fields: dict | None = None,- static_output: bool = False,-):- """Create a mock block for testing."""- mock = MagicMock()- mock.id = block_id- mock.name = name- mock.description = f"{name} description"- mock.block_type = block_type- mock.disabled = disabled- mock.static_output = static_output- mock.input_schema = MagicMock()- mock.input_schema.jsonschema.return_value = input_schema or {- "properties": {},- "required": [],- }- mock.input_schema.get_credentials_fields.return_value = credentials_fields or {}- mock.output_schema = MagicMock()- mock.output_schema.jsonschema.return_value = output_schema or {}- mock.categories = []- mock.optimized_description = None-- # Mock get_info() for include_schemas support- mock_info = MagicMock()- mock_info.inputSchema = input_schema or {"properties": {}, "required": []}- mock_info.outputSchema = output_schema or {}- mock_info.staticOutput = static_output- mock.get_info.return_value = mock_info-- return mock---class TestFindBlockFiltering:- """Tests for block filtering in FindBlockTool."""-- def test_excluded_block_types_contains_expected_types(self):- """Verify COPILOT_EXCLUDED_BLOCK_TYPES contains all graph-only types."""- assert BlockType.INPUT in COPILOT_EXCLUDED_BLOCK_TYPES- assert BlockType.OUTPUT in COPILOT_EXCLUDED_BLOCK_TYPES- assert BlockType.WEBHOOK in COPILOT_EXCLUDED_BLOCK_TYPES- assert BlockType.WEBHOOK_MANUAL in COPILOT_EXCLUDED_BLOCK_TYPES- assert BlockType.NOTE in COPILOT_EXCLUDED_BLOCK_TYPES- assert BlockType.HUMAN_IN_THE_LOOP in COPILOT_EXCLUDED_BLOCK_TYPES- assert BlockType.AGENT in COPILOT_EXCLUDED_BLOCK_TYPES-- def test_excluded_block_ids_contains_orchestrator(self):- """Verify OrchestratorBlock is in COPILOT_EXCLUDED_BLOCK_IDS."""- assert "3b191d9f-356f-482d-8238-ba04b6d18381" in COPILOT_EXCLUDED_BLOCK_IDS-- @pytest.mark.asyncio(loop_scope="session")- async def test_excluded_block_type_filtered_from_results(self):- """Verify blocks with excluded BlockTypes are filtered from search results."""- session = make_session(user_id=_TEST_USER_ID)-- # Mock search returns an INPUT block (excluded) and a STANDARD block (included)- search_results = [- {"content_id": "input-block-id", "score": 0.9},- {"content_id": "standard-block-id", "score": 0.8},- ]-- input_block = make_mock_block("input-block-id", "Input Block", BlockType.INPUT)- standard_block = make_mock_block(- "standard-block-id", "HTTP Request", BlockType.STANDARD- )-- def mock_get_block(block_id):- return {- "input-block-id": input_block,- "standard-block-id": standard_block,- }.get(block_id)-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 2)- )-- with patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ):- with patch(- "backend.copilot.tools.find_block.get_block",- side_effect=mock_get_block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query="test"- )-- # Should only return the standard block, not the INPUT block- assert isinstance(response, BlockListResponse)- assert len(response.blocks) == 1- assert response.blocks[0].id == "standard-block-id"-- @pytest.mark.asyncio(loop_scope="session")- async def test_excluded_block_id_filtered_from_results(self):- """Verify OrchestratorBlock is filtered from search results."""- session = make_session(user_id=_TEST_USER_ID)-- orchestrator_id = "3b191d9f-356f-482d-8238-ba04b6d18381"- search_results = [- {"content_id": orchestrator_id, "score": 0.9},- {"content_id": "normal-block-id", "score": 0.8},- ]-- # OrchestratorBlock has STANDARD type but is excluded by ID- smart_block = make_mock_block(- orchestrator_id, "Orchestrator", BlockType.STANDARD- )- normal_block = make_mock_block(- "normal-block-id", "Normal Block", BlockType.STANDARD- )-- def mock_get_block(block_id):- return {- orchestrator_id: smart_block,- "normal-block-id": normal_block,- }.get(block_id)-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 2)- )-- with patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ):- with patch(- "backend.copilot.tools.find_block.get_block",- side_effect=mock_get_block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query="decision"- )-- # Should only return normal block, not OrchestratorBlock- assert isinstance(response, BlockListResponse)- assert len(response.blocks) == 1- assert response.blocks[0].id == "normal-block-id"-- @pytest.mark.asyncio(loop_scope="session")- async def test_for_agent_generation_exposes_excluded_blocks_in_search(self):- """With for_agent_generation=True, excluded block types appear in search results."""- session = make_session(user_id=_TEST_USER_ID)-- search_results = [- {"content_id": "input-block-id", "score": 0.9},- {"content_id": "output-block-id", "score": 0.8},- ]- input_block = make_mock_block("input-block-id", "Agent Input", BlockType.INPUT)- output_block = make_mock_block(- "output-block-id", "Agent Output", BlockType.OUTPUT- )-- def mock_get_block(block_id):- return {- "input-block-id": input_block,- "output-block-id": output_block,- }.get(block_id)-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 2)- )-- with patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ):- with patch(- "backend.copilot.tools.find_block.get_block",- side_effect=mock_get_block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="agent input",- for_agent_generation=True,- )-- assert isinstance(response, BlockListResponse)- assert len(response.blocks) == 2- block_ids = {b.id for b in response.blocks}- assert "input-block-id" in block_ids- assert "output-block-id" in block_ids-- @pytest.mark.asyncio(loop_scope="session")- async def test_mcp_tool_exposed_with_for_agent_generation_in_search(self):- """MCP_TOOL blocks appear in search results when for_agent_generation=True."""- session = make_session(user_id=_TEST_USER_ID)-- search_results = [- {"content_id": "mcp-block-id", "score": 0.9},- {"content_id": "standard-block-id", "score": 0.8},- ]- mcp_block = make_mock_block("mcp-block-id", "MCP Tool", BlockType.MCP_TOOL)- standard_block = make_mock_block(- "standard-block-id", "Normal Block", BlockType.STANDARD- )-- def mock_get_block(block_id):- return {- "mcp-block-id": mcp_block,- "standard-block-id": standard_block,- }.get(block_id)-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 2)- )-- with patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ):- with patch(- "backend.copilot.tools.find_block.get_block",- side_effect=mock_get_block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="mcp tool",- for_agent_generation=True,- )-- assert isinstance(response, BlockListResponse)- assert len(response.blocks) == 2- assert any(b.id == "mcp-block-id" for b in response.blocks)- assert any(b.id == "standard-block-id" for b in response.blocks)-- @pytest.mark.asyncio(loop_scope="session")- async def test_mcp_tool_excluded_without_for_agent_generation_in_search(self):- """MCP_TOOL blocks are excluded from search in normal CoPilot mode."""- session = make_session(user_id=_TEST_USER_ID)-- search_results = [- {"content_id": "mcp-block-id", "score": 0.9},- {"content_id": "standard-block-id", "score": 0.8},- ]- mcp_block = make_mock_block("mcp-block-id", "MCP Tool", BlockType.MCP_TOOL)- standard_block = make_mock_block(- "standard-block-id", "Normal Block", BlockType.STANDARD- )-- def mock_get_block(block_id):- return {- "mcp-block-id": mcp_block,- "standard-block-id": standard_block,- }.get(block_id)-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 2)- )-- with patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ):- with patch(- "backend.copilot.tools.find_block.get_block",- side_effect=mock_get_block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="mcp tool",- for_agent_generation=False,- )-- assert isinstance(response, BlockListResponse)- assert len(response.blocks) == 1- assert response.blocks[0].id == "standard-block-id"-- @pytest.mark.asyncio(loop_scope="session")- async def test_for_agent_generation_exposes_excluded_ids_in_search(self):- """With for_agent_generation=True, excluded block IDs appear in search results."""- session = make_session(user_id=_TEST_USER_ID)- orchestrator_id = next(iter(COPILOT_EXCLUDED_BLOCK_IDS))-- search_results = [- {"content_id": orchestrator_id, "score": 0.9},- {"content_id": "normal-block-id", "score": 0.8},- ]- orchestrator_block = make_mock_block(- orchestrator_id, "Orchestrator", BlockType.STANDARD- )- normal_block = make_mock_block(- "normal-block-id", "Normal Block", BlockType.STANDARD- )-- def mock_get_block(block_id):- return {- orchestrator_id: orchestrator_block,- "normal-block-id": normal_block,- }.get(block_id)-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 2)- )-- with patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ):- with patch(- "backend.copilot.tools.find_block.get_block",- side_effect=mock_get_block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="orchestrator",- for_agent_generation=True,- )-- assert isinstance(response, BlockListResponse)- assert len(response.blocks) == 2- block_ids = {b.id for b in response.blocks}- assert orchestrator_id in block_ids- assert "normal-block-id" in block_ids-- @pytest.mark.asyncio(loop_scope="session")- async def test_response_size_average_chars_per_block(self):- """Measure average chars per block in the serialized response."""- session = make_session(user_id=_TEST_USER_ID)-- # Realistic block definitions modeled after real blocks- block_defs = [- {- "id": "http-block-id",- "name": "Send Web Request",- "input_schema": {- "properties": {- "url": {- "type": "string",- "description": "The URL to send the request to",- },- "method": {- "type": "string",- "description": "The HTTP method to use",- },- "headers": {- "type": "object",- "description": "Headers to include in the request",- },- "json_format": {- "type": "boolean",- "description": "If true, send the body as JSON",- },- "body": {- "type": "object",- "description": "Form/JSON body payload",- },- "credentials": {- "type": "object",- "description": "HTTP credentials",- },- },- "required": ["url", "method"],- },- "output_schema": {- "properties": {- "response": {- "type": "object",- "description": "The response from the server",- },- "client_error": {- "type": "object",- "description": "Errors on 4xx status codes",- },- "server_error": {- "type": "object",- "description": "Errors on 5xx status codes",- },- "error": {- "type": "string",- "description": "Errors for all other exceptions",- },- },- },- "credentials_fields": {"credentials": True},- },- {- "id": "email-block-id",- "name": "Send Email",- "input_schema": {- "properties": {- "to_email": {- "type": "string",- "description": "Recipient email address",- },- "subject": {- "type": "string",- "description": "Subject of the email",- },- "body": {- "type": "string",- "description": "Body of the email",- },- "config": {- "type": "object",- "description": "SMTP Config",- },- "credentials": {- "type": "object",- "description": "SMTP credentials",- },- },- "required": ["to_email", "subject", "body", "credentials"],- },- "output_schema": {- "properties": {- "status": {- "type": "string",- "description": "Status of the email sending operation",- },- "error": {- "type": "string",- "description": "Error message if sending failed",- },- },- },- "credentials_fields": {"credentials": True},- },- {- "id": "claude-code-block-id",- "name": "Claude Code",- "input_schema": {- "properties": {- "e2b_credentials": {- "type": "object",- "description": "API key for E2B platform",- },- "anthropic_credentials": {- "type": "object",- "description": "API key for Anthropic",- },- "prompt": {- "type": "string",- "description": "Task or instruction for Claude Code",- },- "timeout": {- "type": "integer",- "description": "Sandbox timeout in seconds",- },- "setup_commands": {- "type": "array",- "description": "Shell commands to run before execution",- },- "working_directory": {- "type": "string",- "description": "Working directory for Claude Code",- },- "session_id": {- "type": "string",- "description": "Session ID to resume a conversation",- },- "sandbox_id": {- "type": "string",- "description": "Sandbox ID to reconnect to",- },- "conversation_history": {- "type": "string",- "description": "Previous conversation history",- },- "dispose_sandbox": {- "type": "boolean",- "description": "Whether to dispose sandbox after execution",- },- },- "required": [- "e2b_credentials",- "anthropic_credentials",- "prompt",- ],- },- "output_schema": {- "properties": {- "response": {- "type": "string",- "description": "Output from Claude Code execution",- },- "files": {- "type": "array",- "description": "Files created/modified by Claude Code",- },- "conversation_history": {- "type": "string",- "description": "Full conversation history",- },- "session_id": {- "type": "string",- "description": "Session ID for this conversation",- },- "sandbox_id": {- "type": "string",- "description": "ID of the sandbox instance",- },- "error": {- "type": "string",- "description": "Error message if execution failed",- },- },- },- "credentials_fields": {- "e2b_credentials": True,- "anthropic_credentials": True,- },- },- ]-- search_results = [- {"content_id": d["id"], "score": 0.9 - i * 0.1}- for i, d in enumerate(block_defs)- ]- mock_blocks = {- d["id"]: make_mock_block(- block_id=d["id"],- name=d["name"],- block_type=BlockType.STANDARD,- input_schema=d["input_schema"],- output_schema=d["output_schema"],- credentials_fields=d["credentials_fields"],- )- for d in block_defs- }-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, len(search_results))- )-- with (- patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ),- patch(- "backend.copilot.tools.find_block.get_block",- side_effect=lambda bid: mock_blocks.get(bid),- ),- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query="test"- )-- assert isinstance(response, BlockListResponse)- assert response.count == len(block_defs)-- total_chars = len(response.model_dump_json())- avg_chars = total_chars // response.count-- # Print for visibility in test output- print(f"\nTotal response size: {total_chars} chars")- print(f"Number of blocks: {response.count}")- print(f"Average chars per block: {avg_chars}")-- # The old response was ~90K for 10 blocks (~9K per block).- # Previous optimization reduced it to ~1.5K per block (no raw JSON schemas).- # Now with only id/name/description, we expect ~300 chars per block.- assert avg_chars < 500, (- f"Average chars per block ({avg_chars}) exceeds 500. "- f"Total response: {total_chars} chars for {response.count} blocks."- )-- @pytest.mark.asyncio(loop_scope="session")- async def test_schemas_always_omitted(self):- """Schemas are never returned — full schemas context-bomb the model- (see run_block's schema-on-demand flow instead)."""- session = make_session(user_id=_TEST_USER_ID)- input_schema = {"properties": {"url": {"type": "string"}}, "required": ["url"]}- output_schema = {"properties": {"result": {"type": "string"}}}-- search_results = [{"content_id": "block-1", "score": 0.9}]- block = make_mock_block(- "block-1",- "Test Block",- BlockType.STANDARD,- input_schema=input_schema,- output_schema=output_schema,- )-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 1)- )-- with (- patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ),- patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ),- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="test",- )-- assert isinstance(response, BlockListResponse)- assert response.blocks[0].input_schema == {}- assert response.blocks[0].output_schema == {}- assert response.blocks[0].static_output is False-- @pytest.mark.asyncio(loop_scope="session")- async def test_include_schemas_arg_is_ignored(self):- """A stray include_schemas=True argument (e.g. replayed from an old- transcript) must not re-enable schema payloads."""- session = make_session(user_id=_TEST_USER_ID)- input_schema = {"properties": {"url": {"type": "string"}}, "required": ["url"]}- output_schema = {"properties": {"result": {"type": "string"}}}-- search_results = [{"content_id": "block-1", "score": 0.9}]- block = make_mock_block(- "block-1",- "Test Block",- BlockType.STANDARD,- input_schema=input_schema,- output_schema=output_schema,- static_output=True,- )-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 1)- )-- with (- patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ),- patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ),- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="test",- include_schemas=True,- )-- assert isinstance(response, BlockListResponse)- assert response.blocks[0].input_schema == {}- assert response.blocks[0].output_schema == {}---class TestFindBlockDirectLookup:- """Tests for direct UUID lookup in FindBlockTool."""-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_found(self):- """UUID query returns the block directly without search."""- session = make_session(user_id=_TEST_USER_ID)- block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"- block = make_mock_block(block_id, "Test Block", BlockType.STANDARD)-- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query=block_id- )-- assert isinstance(response, BlockListResponse)- assert response.count == 1- assert response.blocks[0].id == block_id- assert response.blocks[0].name == "Test Block"-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_not_found_falls_through(self):- """UUID that doesn't match any block falls through to search."""- session = make_session(user_id=_TEST_USER_ID)- block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(return_value=([], 0))-- with (- patch(- "backend.copilot.tools.find_block.get_block",- return_value=None,- ),- patch(- "backend.copilot.tools.find_block.search",- return_value=mock_search_db,- ),- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query=block_id- )-- assert isinstance(response, NoResultsResponse)-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_disabled_block(self):- """UUID matching a disabled block returns NoResultsResponse."""- session = make_session(user_id=_TEST_USER_ID)- block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"- block = make_mock_block(- block_id, "Disabled Block", BlockType.STANDARD, disabled=True- )-- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query=block_id- )-- assert isinstance(response, NoResultsResponse)- assert "disabled" in response.message.lower()-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_excluded_block_type(self):- """UUID matching an excluded block type returns NoResultsResponse."""- session = make_session(user_id=_TEST_USER_ID)- block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"- block = make_mock_block(block_id, "Input Block", BlockType.INPUT)-- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query=block_id- )-- assert isinstance(response, NoResultsResponse)- assert "not available" in response.message.lower()-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_excluded_block_id(self):- """UUID matching an excluded block ID returns NoResultsResponse."""- session = make_session(user_id=_TEST_USER_ID)- orchestrator_id = "3b191d9f-356f-482d-8238-ba04b6d18381"- block = make_mock_block(orchestrator_id, "Orchestrator", BlockType.STANDARD)-- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID, session=session, query=orchestrator_id- )-- assert isinstance(response, NoResultsResponse)- assert "not available" in response.message.lower()-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_excluded_block_type_allowed_with_for_agent_generation(- self,- ):- """With for_agent_generation=True, excluded block types (INPUT) are visible."""- session = make_session(user_id=_TEST_USER_ID)- block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"- block = make_mock_block(block_id, "Agent Input Block", BlockType.INPUT)-- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query=block_id,- for_agent_generation=True,- )-- assert isinstance(response, BlockListResponse)- assert response.count == 1- assert response.blocks[0].id == block_id-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_mcp_tool_exposed_with_for_agent_generation(self):- """MCP_TOOL blocks are returned by UUID lookup when for_agent_generation=True."""- session = make_session(user_id=_TEST_USER_ID)- block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"- block = make_mock_block(block_id, "MCP Tool", BlockType.MCP_TOOL)-- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query=block_id,- for_agent_generation=True,- )-- assert isinstance(response, BlockListResponse)- assert response.blocks[0].id == block_id-- @pytest.mark.asyncio(loop_scope="session")- async def test_uuid_lookup_mcp_tool_excluded_without_for_agent_generation(self):- """MCP_TOOL blocks are excluded by UUID lookup in normal CoPilot mode."""- session = make_session(user_id=_TEST_USER_ID)- block_id = "a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d"- block = make_mock_block(block_id, "MCP Tool", BlockType.MCP_TOOL)-- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=block,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query=block_id,- for_agent_generation=False,- )-- assert isinstance(response, NoResultsResponse)- assert "run_mcp_tool" in response.message---class TestExactNameLookup:- """Tests for the exact class-name registry lookup that backstops search."""-- def test_matches_class_name_in_common_spellings(self):- registry = {"orch-id": type("OrchestratorBlock", (), {})}- with patch(- "backend.copilot.tools.find_block.get_blocks", return_value=registry- ):- assert _find_block_ids_by_name("OrchestratorBlock") == ["orch-id"]- assert _find_block_ids_by_name("orchestrator block") == ["orch-id"]- assert _find_block_ids_by_name("orchestrator") == ["orch-id"]-- def test_no_match_for_keyword_queries(self):- registry = {"orch-id": type("OrchestratorBlock", (), {})}- with patch(- "backend.copilot.tools.find_block.get_blocks", return_value=registry- ):- assert _find_block_ids_by_name("send email") == []- assert _find_block_ids_by_name("AI orchestrator agent mode") == []- assert _find_block_ids_by_name("") == []-- @pytest.mark.asyncio(loop_scope="session")- async def test_exact_name_query_survives_bad_search_ranking(self):- """An exact class-name query returns the block even when hybrid- search misses it entirely (stale index row / CamelCase lexical gap)."""- session = make_session(user_id=_TEST_USER_ID)-- # Hybrid search returns only unrelated junk — the real failure mode.- search_results = [{"content_id": "junk-id", "score": 0.9}]-- orch_block = make_mock_block("orch-id", "OrchestratorBlock", BlockType.STANDARD)- junk_block = make_mock_block("junk-id", "JSONEncoderBlock", BlockType.STANDARD)- registry = {- "orch-id": type("OrchestratorBlock", (), {}),- "junk-id": type("JSONEncoderBlock", (), {}),- }-- def mock_get_block(block_id):- return {"orch-id": orch_block, "junk-id": junk_block}.get(block_id)-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(- return_value=(search_results, 1)- )-- with patch(- "backend.copilot.tools.find_block.search", return_value=mock_search_db- ):- with patch(- "backend.copilot.tools.find_block.get_block",- side_effect=mock_get_block,- ):- with patch(- "backend.copilot.tools.find_block.get_blocks",- return_value=registry,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="OrchestratorBlock",- for_agent_generation=True,- )-- assert isinstance(response, BlockListResponse)- assert response.blocks[0].id == "orch-id"- assert [b.id for b in response.blocks] == ["orch-id", "junk-id"]-- @pytest.mark.asyncio(loop_scope="session")- async def test_exact_name_match_still_filtered_in_copilot_mode(self):- """Exact-name hits go through the same exclusion filter: a graph-only- block resolved by name is still hidden without for_agent_generation."""- session = make_session(user_id=_TEST_USER_ID)-- orchestrator_id = "3b191d9f-356f-482d-8238-ba04b6d18381"- assert orchestrator_id in COPILOT_EXCLUDED_BLOCK_IDS- orch_block = make_mock_block(- orchestrator_id, "OrchestratorBlock", BlockType.STANDARD- )- registry = {orchestrator_id: type("OrchestratorBlock", (), {})}-- mock_search_db = MagicMock()- mock_search_db.unified_hybrid_search = AsyncMock(return_value=([], 0))-- with patch(- "backend.copilot.tools.find_block.search", return_value=mock_search_db- ):- with patch(- "backend.copilot.tools.find_block.get_block",- return_value=orch_block,- ):- with patch(- "backend.copilot.tools.find_block.get_blocks",- return_value=registry,- ):- tool = FindBlockTool()- response = await tool._execute(- user_id=_TEST_USER_ID,- session=session,- query="OrchestratorBlock",- for_agent_generation=False,- )-- assert isinstance(response, NoResultsResponse)diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.pyindex e617f30d9e..abe8a47dd5 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py@@ -778,11 +778,10 @@ async def prepare_block_for_execution(Returns:BlockPreparation on success, or a ToolResponseBase error/setup response."""- # Lazy import: find_block imports from .base and .models (siblings), not- # from helpers — no actual circular dependency exists today. Kept lazy as a- # precaution since find_block is the block-registry module and future changes- # could introduce a cycle.- from .find_block import COPILOT_EXCLUDED_BLOCK_IDS, COPILOT_EXCLUDED_BLOCK_TYPES+ from backend.copilot.capabilities.block_meta import (+ COPILOT_EXCLUDED_BLOCK_IDS,+ COPILOT_EXCLUDED_BLOCK_TYPES,+ )block = get_block(block_id)if not block:diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers_test.py b/autogpt_platform/backend/backend/copilot/tools/helpers_test.pyindex a0005fd4c2..a4b79da005 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers_test.py@@ -815,16 +815,19 @@ def _make_simple_block(def _patch_excluded(block_ids: set | None = None, block_types: set | None = None):+ # ``prepare_block_execution`` imports these from ``block_meta`` inside the+ # function, so the source module is the patch target. They were read from+ # ``tools.find_block`` until that module went; ``create=True`` meant the+ # patch kept "working" against a name that was no longer there, and the+ # exclusions under test silently stopped being exercised.return (patch(- "backend.copilot.tools.find_block.COPILOT_EXCLUDED_BLOCK_IDS",+ "backend.copilot.capabilities.block_meta.COPILOT_EXCLUDED_BLOCK_IDS",new=block_ids or set(),- create=True,),patch(- "backend.copilot.tools.find_block.COPILOT_EXCLUDED_BLOCK_TYPES",+ "backend.copilot.capabilities.block_meta.COPILOT_EXCLUDED_BLOCK_TYPES",new=block_types or set(),- create=True,),)