Skip to content

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.

feat(frontend): add search functionality in new block menu (#11121)

Abhimanyu Yadavdf5b3486768a
diff --git a/autogpt_platform/backend/backend/server/v2/builder/model.py b/autogpt_platform/backend/backend/server/v2/builder/model.py
index a84bfc06ff..e1a7e744fd 100644
--- a/autogpt_platform/backend/backend/server/v2/builder/model.py
+++ b/autogpt_platform/backend/backend/server/v2/builder/model.py
@@ -53,16 +53,6 @@ class ProviderResponse(BaseModel):
pagination: Pagination
-# Search
-class SearchRequest(BaseModel):
- search_query: str | None = None
- filter: list[FilterType] | None = None
- by_creator: list[str] | None = None
- search_id: str | None = None
- page: int | None = None
- page_size: int | None = None
-
-
class SearchBlocksResponse(BaseModel):
blocks: BlockResponse
total_block_count: int
diff --git a/autogpt_platform/backend/backend/server/v2/builder/routes.py b/autogpt_platform/backend/backend/server/v2/builder/routes.py
index 6ed6fa1e29..e9859d2e98 100644
--- a/autogpt_platform/backend/backend/server/v2/builder/routes.py
+++ b/autogpt_platform/backend/backend/server/v2/builder/routes.py
@@ -128,30 +128,34 @@ async def get_providers(
)
+# Not using post method because on frontend, orval doesn't support Infinite Query with POST method.
"/search",
summary="Builder search",
tags=["store", "private"],
response_model=builder_model.SearchResponse,
)
async def search(
- options: builder_model.SearchRequest,
user_id: Annotated[str, fastapi.Security(get_user_id)],
+ search_query: Annotated[str | None, fastapi.Query()] = None,
+ filter: Annotated[list[str] | None, fastapi.Query()] = None,
+ search_id: Annotated[str | None, fastapi.Query()] = None,
+ by_creator: Annotated[list[str] | None, fastapi.Query()] = None,
+ page: Annotated[int, fastapi.Query()] = 1,
+ page_size: Annotated[int, fastapi.Query()] = 50,
) -> builder_model.SearchResponse:
"""
Search for blocks (including integrations), marketplace agents, and user library agents.
"""
# If no filters are provided, then we will return all types
- if not options.filter:
- options.filter = [
+ if not filter:
+ filter = [
"blocks",
"integrations",
"marketplace_agents",
"my_agents",
]
- options.search_query = sanitize_query(options.search_query)
- options.page = options.page or 1
- options.page_size = options.page_size or 50
+ search_query = sanitize_query(search_query)
# Blocks&Integrations
blocks = builder_model.SearchBlocksResponse(
@@ -162,13 +166,13 @@ async def search(
total_block_count=0,
total_integration_count=0,
)
- if "blocks" in options.filter or "integrations" in options.filter:
+ if "blocks" in filter or "integrations" in filter:
blocks = builder_db.search_blocks(
- include_blocks="blocks" in options.filter,
- include_integrations="integrations" in options.filter,
- query=options.search_query or "",
- page=options.page,
- page_size=options.page_size,
+ include_blocks="blocks" in filter,
+ include_integrations="integrations" in filter,
+ query=search_query or "",
+ page=page,
+ page_size=page_size,
)
# Library Agents
@@ -176,12 +180,12 @@ async def search(
agents=[],
pagination=Pagination.empty(),
)
- if "my_agents" in options.filter:
+ if "my_agents" in filter:
my_agents = await library_db.list_library_agents(
user_id=user_id,
- search_term=options.search_query,
- page=options.page,
- page_size=options.page_size,
+ search_term=search_query,
+ page=page,
+ page_size=page_size,
)
# Marketplace Agents
@@ -189,12 +193,12 @@ async def search(
agents=[],
pagination=Pagination.empty(),
)
- if "marketplace_agents" in options.filter:
+ if "marketplace_agents" in filter:
marketplace_agents = await store_db.get_store_agents(
- creators=options.by_creator,
- search_query=options.search_query,
- page=options.page,
- page_size=options.page_size,
+ creators=by_creator,
+ search_query=search_query,
+ page=page,
+ page_size=page_size,
)
more_pages = False
@@ -214,7 +218,7 @@ async def search(
"marketplace_agents": marketplace_agents.pagination.total_items,
"my_agents": my_agents.pagination.total_items,
},
- page=options.page,
+ page=page,
more_pages=more_pages,
)
diff --git a/autogpt_platform/frontend/orval.config.ts b/autogpt_platform/frontend/orval.config.ts
index 46425db64b..de305c1acc 100644
--- a/autogpt_platform/frontend/orval.config.ts
+++ b/autogpt_platform/frontend/orval.config.ts
@@ -67,6 +67,12 @@ export default defineConfig({
useQuery: true,
},
},
+ "getV2Builder search": {
+ query: {
+ useInfinite: true,
+ useInfiniteQueryParam: "page",
+ },
+ },
},
},
},
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/BlockMenuSearch.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/BlockMenuSearch.tsx
index fb766fa28c..2ad3df909b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/BlockMenuSearch.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/BlockMenuSearch.tsx
@@ -1,12 +1,101 @@
import { Text } from "@/components/atoms/Text/Text";
+import { useBlockMenuSearch } from "./useBlockMenuSearch";
+import { InfiniteScroll } from "@/components/contextual/InfiniteScroll/InfiniteScroll";
+import { LoadingSpinner } from "@/components/__legacy__/ui/loading";
+import { SearchResponseItemsItem } from "@/app/api/__generated__/models/searchResponseItemsItem";
+import { MarketplaceAgentBlock } from "../MarketplaceAgentBlock";
+import { Block } from "../Block";
+import { UGCAgentBlock } from "../UGCAgentBlock";
+import { getSearchItemType } from "./helper";
+import { useBlockMenuStore } from "../../../stores/blockMenuStore";
+import { blockMenuContainerStyle } from "../style";
+import { cn } from "@/lib/utils";
+import { NoSearchResult } from "../NoSearchResult";
+import { useNodeStore } from "../../../stores/nodeStore";
export const BlockMenuSearch = () => {
+ const {
+ allSearchData,
+ isFetchingNextPage,
+ fetchNextPage,
+ hasNextPage,
+ searchLoading,
+ } = useBlockMenuSearch();
+ const { searchQuery } = useBlockMenuStore();
+ const addBlock = useNodeStore((state) => state.addBlock);
+
+ if (searchLoading) {
+ return (
+ <div
+ className={cn(
+ blockMenuContainerStyle,
+ "flex items-center justify-center",
+ )}
+ >
+ <LoadingSpinner className="size-13" />
+ </div>
+ );
+ }
+
+ if (allSearchData.length === 0) {
+ return <NoSearchResult />;
+ }
+
return (
- // This is just a temporary text, will content inside in it [in follow-up prs]
- <div className="flex h-full w-full items-center justify-center">
- <Text variant="h3" className="text-green-300">
- This is the block menu search
- </Text>
+ <div className={blockMenuContainerStyle}>
+ <Text variant="body-medium">Search results</Text>
+ <InfiniteScroll
+ isFetchingNextPage={isFetchingNextPage}
+ fetchNextPage={fetchNextPage}
+ hasNextPage={hasNextPage}
+ loader={<LoadingSpinner className="size-13" />}
+ className="space-y-2.5"
+ >
+ {allSearchData.map((item: SearchResponseItemsItem, index: number) => {
+ const { type, data } = getSearchItemType(item);
+ // backend give support to these 3 types only [right now] - we need to give support to integration and ai agent types in follow up PRs
+ switch (type) {
+ case "store_agent":
+ return (
+ <MarketplaceAgentBlock
+ key={index}
+ slug={data.slug}
+ highlightedText={searchQuery}
+ title={data.agent_name}
+ image_url={data.agent_image}
+ creator_name={data.creator}
+ number_of_runs={data.runs}
+ loading={false}
+ />
+ );
+ case "block":
+ return (
+ <Block
+ key={index}
+ title={data.name}
+ highlightedText={searchQuery}
+ description={data.description}
+ onClick={() => addBlock(data)}
+ />
+ );
+
+ case "library_agent":
+ return (
+ <UGCAgentBlock
+ key={index}
+ title={data.name}
+ highlightedText={searchQuery}
+ image_url={data.image_url}
+ version={data.graph_version}
+ edited_time={data.updated_at}
+ />
+ );
+
+ default:
+ return null;
+ }
+ })}
+ </InfiniteScroll>
</div>
);
};
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/helper.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/helper.ts
new file mode 100644
index 0000000000..830b75ad4e
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/helper.ts
@@ -0,0 +1,25 @@
+import { BlockInfo } from "@/app/api/__generated__/models/blockInfo";
+import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
+import { SearchResponseItemsItem } from "@/app/api/__generated__/models/searchResponseItemsItem";
+import { StoreAgent } from "@/app/api/__generated__/models/storeAgent";
+
+export const getSearchItemType = (
+ item: SearchResponseItemsItem,
+):
+ | { type: "store_agent"; data: StoreAgent }
+ | { type: "library_agent"; data: LibraryAgent }
+ | { type: "block"; data: BlockInfo } => {
+ if ("slug" in item && "agent_name" in item && "creator" in item) {
+ return { type: "store_agent", data: item as StoreAgent };
+ }
+
+ if ("graph_id" in item && "graph_version" in item && "creator_name" in item) {
+ return { type: "library_agent", data: item as LibraryAgent };
+ }
+
+ if ("inputSchema" in item && "outputSchema" in item && "uiType" in item) {
+ return { type: "block", data: item as BlockInfo };
+ }
+
+ throw new Error("Unknown item type");
+};
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/useBlockMenuSearch.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/useBlockMenuSearch.ts
new file mode 100644
index 0000000000..2eb3936159
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/NewBlockMenu/BlockMenuSearch/useBlockMenuSearch.ts
@@ -0,0 +1,44 @@
+import { useBlockMenuStore } from "../../../stores/blockMenuStore";
+import { useGetV2BuilderSearchInfinite } from "@/app/api/__generated__/endpoints/store/store";
+import { SearchResponse } from "@/app/api/__generated__/models/searchResponse";
+
+export const useBlockMenuSearch = () => {
+ const { searchQuery } = useBlockMenuStore();
+
+ const {
+ data: searchData,
+ fetchNextPage,
+ hasNextPage,
+ isFetchingNextPage,
+ isLoading: searchLoading,
+ } = useGetV2BuilderSearchInfinite(
+ {
+ page: 1,
+ page_size: 8,
+ search_query: searchQuery,
+ },
+ {
+ query: {
+ getNextPageParam: (lastPage, allPages) => {
+ const pagination = lastPage.data as SearchResponse;
+ const isMore = pagination.more_pages;
+ return isMore ? allPages.length + 1 : undefined;
+ },
+ },
+ },
+ );
+
+ const allSearchData =
+ searchData?.pages?.flatMap((page) => {
+ const response = page.data as SearchResponse;
+ return response.items;
+ }) ?? [];
+
+ return {
+ allSearchData,
+ isFetchingNextPage,
+ fetchNextPage,
+ hasNextPage,
+ searchLoading,
+ };
+};
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 68a2ed5f21..8584067eeb 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -3445,19 +3445,68 @@
}
},
"/api/builder/search": {
- "post": {
+ "get": {
"tags": ["v2", "store", "private"],
"summary": "Builder search",
"description": "Search for blocks (including integrations), marketplace agents, and user library agents.",
- "operationId": "postV2Builder search",
- "requestBody": {
- "content": {
- "application/json": {
- "schema": { "$ref": "#/components/schemas/SearchRequest" }
+ "operationId": "getV2Builder search",
+ "security": [{ "HTTPBearerJWT": [] }],
+ "parameters": [
+ {
+ "name": "search_query",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "anyOf": [{ "type": "string" }, { "type": "null" }],
+ "title": "Search Query"
}
},
- "required": true
- },
+ {
+ "name": "filter",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "anyOf": [
+ { "type": "array", "items": { "type": "string" } },
+ { "type": "null" }
+ ],
+ "title": "Filter"
+ }
+ },
+ {
+ "name": "search_id",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "anyOf": [{ "type": "string" }, { "type": "null" }],
+ "title": "Search Id"
+ }
+ },
+ {
+ "name": "by_creator",
+ "in": "query",
+ "required": false,
+ "schema": {
+ "anyOf": [
+ { "type": "array", "items": { "type": "string" } },
+ { "type": "null" }
+ ],
+ "title": "By Creator"
+ }
+ },
+ {
+ "name": "page",
+ "in": "query",
+ "required": false,
+ "schema": { "type": "integer", "default": 1, "title": "Page" }
+ },
+ {
+ "name": "page_size",
+ "in": "query",
+ "required": false,
+ "schema": { "type": "integer", "default": 50, "title": "Page Size" }
+ }
+ ],
"responses": {
"200": {
"description": "Successful Response",
@@ -3478,8 +3527,7 @@
"401": {
"$ref": "#/components/responses/HTTP401NotAuthenticatedError"
}
- },
- "security": [{ "HTTPBearerJWT": [] }]
+ }
}
},
"/api/builder/counts": {
@@ -7344,53 +7392,6 @@
"required": ["name", "cron", "inputs"],
"title": "ScheduleCreationRequest"
},
- "SearchRequest": {
- "properties": {
- "search_query": {
- "anyOf": [{ "type": "string" }, { "type": "null" }],
- "title": "Search Query"
- },
- "filter": {
- "anyOf": [
- {
- "items": {
- "type": "string",
- "enum": [
- "blocks",
- "integrations",
- "marketplace_agents",
- "my_agents"
- ]
- },
- "type": "array"
- },
- { "type": "null" }
- ],
- "title": "Filter"
- },
- "by_creator": {
- "anyOf": [
- { "items": { "type": "string" }, "type": "array" },
- { "type": "null" }
- ],
- "title": "By Creator"
- },
- "search_id": {
- "anyOf": [{ "type": "string" }, { "type": "null" }],
- "title": "Search Id"
- },
- "page": {
- "anyOf": [{ "type": "integer" }, { "type": "null" }],
- "title": "Page"
- },
- "page_size": {
- "anyOf": [{ "type": "integer" }, { "type": "null" }],
- "title": "Page Size"
- }
- },
- "type": "object",
- "title": "SearchRequest"
- },
"SearchResponse": {
"properties": {
"items": {