Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/api/features/store/content_handlers.py (Python)

Code before:
        from backend.data.block import get_blocks

        # Get all available blocks
        all_blocks = get_blocks()

        # Check which ones have embeddings
        if not all_blocks:
            return []

        block_ids = list(all_blocks.keys())

        # Query for existing embeddings
        placeholders = ",".join([f"${i+1}" for i in range(len(block_ids))])
        existing_result = await query_raw_with_schema(
            f"""
            SELECT "contentId"
            FROM {{schema_prefix}}"UnifiedContentEmbedding"
            WHERE "contentType" = 'BLOCK'::{{schema_prefix}}"ContentType"
            AND "contentId" = ANY(ARRAY[{placeholders}])
            """,
            *block_ids,
        )

        existing_ids = {row["contentId"] for row in existing_result}

        # Filter blocks: exclude already-embedded, disabled, and graph-only blocks
        # IMPORTANT: Filter BEFORE slicing to batch_size to avoid returning empty
        # batches when all first N blocks are excluded (causing processing stall)
        eligible_blocks = []
        for block_id, block_cls in all_blocks.items():
            if block_id in existing_ids:
                continue
            try:
                block_instance = block_cls()
                if block_instance.disabled:
                    continue
                if (
                    block_instance.block_type in EXCLUDED_BLOCK_TYPES
                    or block_id in EXCLUDED_BLOCK_IDS
                ):
                    continue
                eligible_blocks.append((block_id, block_cls, block_instance))
            except Exception as e:
                logger.warning(f"Failed to instantiate block {block_id}: {e}")
                continue

        # Convert to ContentItem (now safe to slice after filtering)
        items = []
        for block_id, block_cls, block_instance in eligible_blocks[:batch_size]:
            try:
                # Build searchable text from block metadata

Review comment (suggestion):
This handler is the one used for doing semantic search on the block entirely, if we want exlude the result specifically for copilot search then do it outside of here and solely on the copilot tools code

Code after:

        # Get all available blocks
        all_blocks = get_blocks()

        # Check which ones have embeddings
        if not all_blocks:
            return []

        block_ids = list(all_blocks.keys())

        # Query for existing embeddings
        placeholders = ",".join([f"${i+1}" for i in range(len(block_ids))])
        existing_result = await query_raw_with_schema(
            f"""
            SELECT "contentId"
            FROM {{schema_prefix}}"UnifiedContentEmbedding"
            WHERE "contentType" = 'BLOCK'::{{schema_prefix}}"ContentType"
            AND "contentId" = ANY(ARRAY[{placeholders}])
            """,
            *block_ids,
        )

        existing_ids = {row["contentId"] for row in existing_result}
        missing_blocks = [
            (block_id, block_cls)
            for block_id, block_cls in all_blocks.items()
            if block_id not in existing_ids
        ]

        # Convert to ContentItem
        items = []
        for block_id, block_cls in missing_blocks[:batch_size]:
            try:
                block_instance = block_cls()

                # Skip disabled blocks - they shouldn't be indexed
                if block_instance.disabled:
                    continue

                # Build searchable text from block metadata
                parts = []
                if hasattr(block_instance, "name") and block_instance.name:
                    parts.append(block_instance.name)
                if (
                    hasattr(block_instance, "description")
                    and block_instance.description
                ):
                    parts.append(block_instance.description)
                if hasattr(block_instance, "categories") and block_instance.categories:
                    # Convert BlockCategory enum to strings
                    parts.append(