Repository: Significant-Gravitas/Auto-GPT
File: autogpt_platform/backend/backend/server/v2/store/db.py (Python)

Code before:
    featured: bool = False,
    creators: list[str] | None = None,
    sorted_by: str | None = None,
    search_query: str | None = None,
    category: str | None = None,
    page: int = 1,
    page_size: int = 20,
) -> backend.server.v2.store.model.StoreAgentsResponse:
    """
    Get PUBLIC store agents from the StoreAgent view
    """
    logger.debug(
        f"Getting store agents. featured={featured}, creators={creators}, sorted_by={sorted_by}, search={search_query}, category={category}, page={page}"
    )
    sanitized_query = sanitize_query(search_query)

    where_clause: prisma.types.StoreAgentWhereInput = {"is_available": True}
    if featured:
        where_clause["featured"] = featured
    if creators:
        where_clause["creator_username"] = {"in": creators}
    if category:
        where_clause["categories"] = {"has": category}

    if sanitized_query:
        await log_search_term(sanitized_query)
        where_clause["OR"] = [
            {"agent_name": {"contains": sanitized_query, "mode": "insensitive"}},
            {"description": {"contains": sanitized_query, "mode": "insensitive"}},
        ]

    order_by = []
    if sorted_by == "rating":
        order_by.append({"rating": "desc"})
    elif sorted_by == "runs":
        order_by.append({"runs": "desc"})
    elif sorted_by == "name":
        order_by.append({"agent_name": "asc"})

    try:
        agents = await prisma.models.StoreAgent.prisma().find_many(
            where=where_clause,
            order=order_by,
            skip=(page - 1) * page_size,
            take=page_size,
        )

        total = await prisma.models.StoreAgent.prisma().count(where=where_clause)
        total_pages = (total + page_size - 1) // page_size

        store_agents: list[backend.server.v2.store.model.StoreAgent] = []

Review comment (security):
Nit: to keep this from adding latency to the response, I'd recommend putting the log statement in a `finally` clause like this:
```py
try:
    # ... query etc
    return results
except ...:
    # error handling
finally:
    await log_search_term(sanitized_query)  # this runs after the function has returned
```

Code after:
    featured: bool = False,
    creators: list[str] | None = None,
    sorted_by: str | None = None,
    search_query: str | None = None,
    category: str | None = None,
    page: int = 1,
    page_size: int = 20,
) -> backend.server.v2.store.model.StoreAgentsResponse:
    """
    Get PUBLIC store agents from the StoreAgent view
    """
    logger.debug(
        f"Getting store agents. featured={featured}, creators={creators}, sorted_by={sorted_by}, search={search_query}, category={category}, page={page}"
    )
    search_term = sanitize_query(search_query)
    where_clause: prisma.types.StoreAgentWhereInput = {"is_available": True}
    if featured:
        where_clause["featured"] = featured
    if creators:
        where_clause["creator_username"] = {"in": creators}
    if category:
        where_clause["categories"] = {"has": category}

    if search_term:
        where_clause["OR"] = [
            {"agent_name": {"contains": search_term, "mode": "insensitive"}},
            {"description": {"contains": search_term, "mode": "insensitive"}},
        ]

    order_by = []
    if sorted_by == "rating":
        order_by.append({"rating": "desc"})
    elif sorted_by == "runs":
        order_by.append({"runs": "desc"})
    elif sorted_by == "name":
        order_by.append({"agent_name": "asc"})

    try:
        agents = await prisma.models.StoreAgent.prisma().find_many(
            where=where_clause,
            order=order_by,
            skip=(page - 1) * page_size,
            take=page_size,
        )

        total = await prisma.models.StoreAgent.prisma().count(where=where_clause)
        total_pages = (total + page_size - 1) // page_size

        store_agents: list[backend.server.v2.store.model.StoreAgent] = []
        for agent in agents:
            try: