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.

fix(platform): stop library, home and AutoPilot pages waiting on the scheduler (#15393)

Nicholas Tindleb15e217158b9
diff --git a/autogpt_platform/backend/backend/api/features/home/service.py b/autogpt_platform/backend/backend/api/features/home/service.py
index a4327d9508..8317cff47d 100644
--- a/autogpt_platform/backend/backend/api/features/home/service.py
+++ b/autogpt_platform/backend/backend/api/features/home/service.py
@@ -32,6 +32,7 @@ from backend.data.execution_cost_summary import (
from backend.executor.scheduler import CopilotTurnJobInfo, GraphExecutionJobInfo
from backend.util.clients import get_scheduler_client
from backend.util.feature_flag import Flag, is_feature_enabled
+from backend.util.settings import Settings
from backend.util.timezone_utils import get_user_timezone_or_utc
from .briefing import without_summaries
@@ -39,6 +40,7 @@ from .compose import compose_home_dashboard
from .models import HomeDashboardResponse
logger = logging.getLogger(__name__)
+settings = Settings()
_EXECUTION_LIMIT = 300
_REVIEW_LIMIT = 100
@@ -225,8 +227,13 @@ async def _get_schedules(
# Deliberately owner-scoped: executions, reviews and cost totals on this page
# are all personal, so org/team schedules would surface upcoming runs whose
# outcomes and approvals could never appear anywhere else on the dashboard.
+ # Bounded like the library's schedule badges: a slow scheduler costs the
+ # upcoming-runs rows, not the whole dashboard.
try:
- return await get_scheduler_client().get_execution_schedules(user_id=user_id)
+ return await asyncio.wait_for(
+ get_scheduler_client().get_execution_schedules(user_id=user_id),
+ timeout=settings.config.schedule_read_timeout_seconds,
+ )
except Exception:
logger.warning(
"Home could not load schedules for user %s", user_id[:_LOG_ID_CHARS]
diff --git a/autogpt_platform/backend/backend/api/features/library/_get_many.py b/autogpt_platform/backend/backend/api/features/library/_get_many.py
new file mode 100644
index 0000000000..7ebaf33972
--- /dev/null
+++ b/autogpt_platform/backend/backend/api/features/library/_get_many.py
@@ -0,0 +1,95 @@
+import asyncio
+import logging
+
+import prisma.models
+from pydantic import BaseModel
+
+import backend.api.features.library.model as library_model
+import backend.data.graph as graph_db
+from backend.data.includes import library_agent_include
+
+from ._schedule_info import _fetch_schedule_info
+from .db import _fetch_execution_counts, _fetch_marketplace_details
+
+logger = logging.getLogger(__name__)
+
+MAX_LIBRARY_AGENTS_PER_BATCH = 50
+# Each agent's details cost a marketplace lookup plus a sub-graph traversal;
+# a full batch must not take every database connection at once.
+_DETAIL_LOAD_CONCURRENCY = 10
+
+
+async def get_library_agents_by_ids(
+ ids: list[str], user_id: str
+) -> list[library_model.LibraryAgent]:
+ """What ``get_library_agent`` returns, for several agents in one read.
+
+ Workflow card grids need the full view (credentials and trigger info for
+ the run button). Fetching it card by card multiplied every scheduler read
+ by the number of cards: one AutoPilot page load sent 205 at once. Here the
+ schedule info and run counts are read once for the whole batch.
+
+ Unlike the single getter, ``execution_count`` is the agent's full run
+ count rather than the length of its few included executions.
+
+ Results keep the order of ``ids``. Unknown, deleted, foreign or unparseable
+ agents are left out rather than failing the batch, so one bad card can't
+ blank the grid.
+ """
+ unique_ids = list(dict.fromkeys(ids))
+ if not unique_ids:
+ return []
+ rows = await prisma.models.LibraryAgent.prisma().find_many(
+ where={"id": {"in": unique_ids}, "userId": user_id, "isDeleted": False},
+ include=library_agent_include(user_id),
+ )
+ limit = asyncio.Semaphore(_DETAIL_LOAD_CONCURRENCY)
+ schedule_info, execution_counts, details = await asyncio.gather(
+ _fetch_schedule_info(user_id),
+ # The include loads only the latest few executions; cards show the total.
+ _fetch_execution_counts(user_id, [row.agentGraphId for row in rows]),
+ asyncio.gather(*(_load_details(row, limit) for row in rows)),
+ )
+
+ by_id: dict[str, library_model.LibraryAgent] = {}
+ for detail in details:
+ if detail is None:
+ continue
+ try:
+ by_id[detail.row.id] = library_model.LibraryAgent.from_db(
+ detail.row,
+ sub_graphs=detail.sub_graphs,
+ store_listing=detail.store_listing,
+ profile=detail.profile,
+ schedule_info=schedule_info,
+ execution_count_override=execution_counts.get(detail.row.agentGraphId),
+ )
+ except Exception as e:
+ logger.error(f"Error parsing LibraryAgent #{detail.row.id}: {e}")
+ return [by_id[agent_id] for agent_id in unique_ids if agent_id in by_id]
+
+
+class _AgentDetails(BaseModel):
+ row: prisma.models.LibraryAgent
+ store_listing: prisma.models.StoreListing | None
+ profile: prisma.models.Profile | None
+ sub_graphs: list[prisma.models.AgentGraph]
+
+
+async def _load_details(
+ row: prisma.models.LibraryAgent, limit: asyncio.Semaphore
+) -> _AgentDetails | None:
+ if not row.AgentGraph:
+ return None
+ try:
+ async with limit:
+ (store_listing, profile), sub_graphs = await asyncio.gather(
+ _fetch_marketplace_details(row.AgentGraph.id),
+ graph_db.get_sub_graphs(row.AgentGraph),
+ )
+ except Exception as e:
+ logger.error(f"Error loading LibraryAgent #{row.id}: {e}")
+ return None
+ return _AgentDetails(
+ row=row, store_listing=store_listing, profile=profile, sub_graphs=sub_graphs
+ )
diff --git a/autogpt_platform/backend/backend/api/features/library/_schedule_info.py b/autogpt_platform/backend/backend/api/features/library/_schedule_info.py
index 0c41e43965..7a7ff9155e 100644
--- a/autogpt_platform/backend/backend/api/features/library/_schedule_info.py
+++ b/autogpt_platform/backend/backend/api/features/library/_schedule_info.py
@@ -1,10 +1,13 @@
+import asyncio
import logging
from datetime import datetime, timezone
from typing import Optional
from backend.util.clients import get_scheduler_client
+from backend.util.settings import Settings
logger = logging.getLogger(__name__)
+settings = Settings()
async def _fetch_schedule_info(
@@ -13,12 +16,19 @@ async def _fetch_schedule_info(
"""Fetch a map of graph ID to earliest next-run time.
When ``graph_id`` is provided, narrow the scheduler query to that graph.
+
+ Schedule badges decorate library reads; they must never hold the read
+ itself hostage. A slow or overloaded scheduler costs the badges after
+ ``schedule_read_timeout_seconds``, not the page after the 300s RPC default.
"""
try:
scheduler_client = get_scheduler_client()
- schedules = await scheduler_client.get_graph_execution_schedules(
- graph_id=graph_id,
- user_id=user_id,
+ schedules = await asyncio.wait_for(
+ scheduler_client.get_graph_execution_schedules(
+ graph_id=graph_id,
+ user_id=user_id,
+ ),
+ timeout=settings.config.schedule_read_timeout_seconds,
)
earliest: dict[str, tuple[datetime, str]] = {}
for schedule in schedules:
@@ -29,6 +39,13 @@ async def _fetch_schedule_info(
if current is None or parsed < current[0]:
earliest[schedule.graph_id] = (parsed, schedule.next_run_time)
return {graph_id: iso for graph_id, (_, iso) in earliest.items()}
+ except asyncio.TimeoutError:
+ logger.warning(
+ "Scheduler did not answer within "
+ f"{settings.config.schedule_read_timeout_seconds}s; library agents "
+ "render without schedule info"
+ )
+ return {}
except Exception:
logger.warning("Failed to fetch schedules for library agents", exc_info=True)
return {}
diff --git a/autogpt_platform/backend/backend/api/features/library/model.py b/autogpt_platform/backend/backend/api/features/library/model.py
index 20f41ace71..0e4670e4b0 100644
--- a/autogpt_platform/backend/backend/api/features/library/model.py
+++ b/autogpt_platform/backend/backend/api/features/library/model.py
@@ -307,7 +307,9 @@ class LibraryAgent(pydantic.BaseModel):
for e in executions
if e.executionStatus == prisma.enums.AgentExecutionStatus.COMPLETED
)
- success_rate = (success_count / execution_count) * 100
+ # Over the included runs, not the total: an override counts runs
+ # the include never loaded, so their outcomes are unknown here.
+ success_rate = (success_count / len(executions)) * 100
correctness_scores = []
for e in executions:
diff --git a/autogpt_platform/backend/backend/api/features/library/model_test.py b/autogpt_platform/backend/backend/api/features/library/model_test.py
index 1f8cc3ce84..d23ecc0e18 100644
--- a/autogpt_platform/backend/backend/api/features/library/model_test.py
+++ b/autogpt_platform/backend/backend/api/features/library/model_test.py
@@ -139,6 +139,36 @@ def test_from_db_execution_count_override_covers_success_rate():
assert result.success_rate == 100.0
+def test_from_db_success_rate_uses_the_included_runs_not_the_full_count():
+ """The override reports every run, but only the latest few are loaded;
+ dividing their successes by the full count made 100 good runs read 1%."""
+ now = datetime.datetime.now(datetime.timezone.utc)
+ completed = prisma.models.AgentGraphExecution(
+ id="exec-1",
+ agentGraphId="g1",
+ agentGraphVersion=1,
+ userId="u1",
+ executionStatus=prisma.enums.AgentExecutionStatus.COMPLETED,
+ createdAt=now,
+ updatedAt=now,
+ isDeleted=False,
+ isShared=False,
+ visibility=prisma.enums.ResourceVisibility.PRIVATE,
+ )
+ failed = completed.model_copy(
+ update={
+ "id": "exec-2",
+ "executionStatus": prisma.enums.AgentExecutionStatus.FAILED,
+ }
+ )
+ agent = _make_library_agent(executions=[completed, failed])
+
+ result = library_model.LibraryAgent.from_db(agent, execution_count_override=100)
+
+ assert result.execution_count == 100
+ assert result.success_rate == 50.0
+
+
@pytest.mark.asyncio
async def test_agent_preset_from_db(test_user_id: str):
# Create mock DB agent
diff --git a/autogpt_platform/backend/backend/api/features/library/routes/agents.py b/autogpt_platform/backend/backend/api/features/library/routes/agents.py
index 199635231a..c2e6a76232 100644
--- a/autogpt_platform/backend/backend/api/features/library/routes/agents.py
+++ b/autogpt_platform/backend/backend/api/features/library/routes/agents.py
@@ -4,6 +4,7 @@ import autogpt_libs.auth as autogpt_auth_lib
from fastapi import APIRouter, Body, HTTPException, Query, Security, status
from fastapi.responses import Response
+import backend.api.features.library._get_many as get_many
from backend.data.onboarding import OnboardingStep, complete_onboarding_step
from .. import db as library_db
@@ -102,6 +103,22 @@ async def list_favorite_library_agents(
)
[email protected]("/batch", summary="Get Library Agents By Ids")
+async def get_library_agents_by_ids(
+ ids: list[str] = Query(
+ ...,
+ min_length=1,
+ max_length=get_many.MAX_LIBRARY_AGENTS_PER_BATCH,
+ description="Library agent IDs; unknown or inaccessible ones are omitted",
+ ),
+ user_id: str = Security(autogpt_auth_lib.get_user_id),
+) -> list[library_model.LibraryAgent]:
+ """
+ Get several library agents at once, as `GET /agents/{id}` would return each.
+ """
+ return await get_many.get_library_agents_by_ids(ids=ids, user_id=user_id)
+
+
@router.get("/{library_agent_id}", summary="Get Library Agent")
async def get_library_agent(
library_agent_id: str,
diff --git a/autogpt_platform/backend/backend/api/features/library/schedule_read_timeout_test.py b/autogpt_platform/backend/backend/api/features/library/schedule_read_timeout_test.py
new file mode 100644
index 0000000000..dc60a14040
--- /dev/null
+++ b/autogpt_platform/backend/backend/api/features/library/schedule_read_timeout_test.py
@@ -0,0 +1,153 @@
+"""Library and Home reads must not wait on a slow scheduler.
+
+Real database, real read paths; only the scheduler RPC client is replaced.
+Before the bounded read, a scheduler that never answered held every one of
+these reads for the 300s RPC timeout (BUILDER-8RR/8SK, 504s on
+``/library/agents/{id}`` and ``/home``).
+"""
+
+import asyncio
+import time
+import uuid
+from datetime import datetime, timedelta, timezone
+from typing import Literal
+
+import pytest
+
+from backend.api.features.home import service as home_service
+from backend.api.features.library import _get_many, _schedule_info
+from backend.api.features.library import db as library_db
+from backend.api.model import CreateGraph
+from backend.data.user import get_or_create_user
+from backend.executor.scheduler import GraphExecutionJobInfo
+from backend.usecases.sample import create_test_graph
+from backend.util.test import SpinTestServer
+
+_READ_TIMEOUT_S = 0.5
+# Generous enough for a slow CI database, far below the 60s the stub hangs.
+_PROMPT_S = 5.0
+
+SchedulerMode = Literal["healthy", "hangs", "errors"]
+
+
+class _SchedulerStub:
+ """Stands in for ``SchedulerClient`` at the RPC boundary."""
+
+ def __init__(self, mode: SchedulerMode, schedules: list[GraphExecutionJobInfo]):
+ self.mode = mode
+ self.schedules = schedules
+
+ async def get_graph_execution_schedules(
+ self, graph_id: str | None = None, user_id: str | None = None
+ ) -> list[GraphExecutionJobInfo]:
+ await self._misbehave()
+ return [
+ s
+ for s in self.schedules
+ if s.user_id == user_id and graph_id in (None, s.graph_id)
+ ]
+
+ async def get_execution_schedules(
+ self, user_id: str | None = None
+ ) -> list[GraphExecutionJobInfo]:
+ await self._misbehave()
+ return [s for s in self.schedules if s.user_id == user_id]
+
+ async def _misbehave(self) -> None:
+ if self.mode == "hangs":
+ await asyncio.sleep(60)
+ if self.mode == "errors":
+ raise ConnectionError("scheduler unavailable")
+
+
+async def _timed(coro):
+ started = time.monotonic()
+ result = await asyncio.wait_for(coro, timeout=30)
+ return result, time.monotonic() - started
+
+
[email protected](loop_scope="session")
[email protected]("mode", ["healthy", "hangs", "errors"])
+async def test_library_and_home_reads_do_not_wait_on_the_scheduler(
+ server: SpinTestServer, monkeypatch: pytest.MonkeyPatch, mode: SchedulerMode
+):
+ user = await get_or_create_user(
+ {
+ "sub": str(uuid.uuid4()),
+ "email": f"schedule-read-{uuid.uuid4().hex[:8]}@example.com",
+ "name": "Schedule Read",
+ }
+ )
+ graph = await server.agent_server.test_create_graph(
+ CreateGraph(graph=create_test_graph()), user.id
+ )
+ agent = await library_db.get_library_agent_by_graph_id(user.id, graph.id)
+ assert agent is not None
+
+ next_run = (datetime.now(timezone.utc) + timedelta(hours=1)).isoformat()
+ schedule = GraphExecutionJobInfo(
+ id=f"schedule-{uuid.uuid4().hex[:8]}",
+ name="Hourly",
+ next_run_time=next_run,
+ user_id=user.id,
+ graph_id=graph.id,
+ graph_version=graph.version,
+ cron="0 * * * *",
+ input_data={},
+ )
+ scheduler = _SchedulerStub(mode, [schedule])
+ for module in (_schedule_info, home_service):
+ monkeypatch.setattr(module, "get_scheduler_client", lambda: scheduler)
+ monkeypatch.setattr(
+ module.settings.config, "schedule_read_timeout_seconds", _READ_TIMEOUT_S
+ )
+ scheduled = mode == "healthy"
+
+ single, elapsed = await _timed(library_db.get_library_agent(agent.id, user.id))
+ assert elapsed < _PROMPT_S
+ assert (single.id, single.graph_id) == (agent.id, graph.id)
+ assert single.is_scheduled is scheduled
+ assert single.next_scheduled_run == (next_run if scheduled else None)
+
+ listing, elapsed = await _timed(
+ library_db.list_library_agents(user_id=user.id, page_size=50)
+ )
+ assert elapsed < _PROMPT_S
+ [listed] = [a for a in listing.agents if a.id == agent.id]
+ assert listed.is_scheduled is scheduled
+
+ batch, elapsed = await _timed(
+ _get_many.get_library_agents_by_ids([agent.id, "missing-id"], user.id)
+ )
+ assert elapsed < _PROMPT_S
+ # The batch answers exactly what the single read does, for every card.
+ assert [a.model_dump() for a in batch] == [single.model_dump()]
+
+ home, elapsed = await _timed(home_service.build_home_dashboard(user_id=user.id))
+ assert elapsed < _PROMPT_S
+ upcoming_ids = [task.id for task in home.upcoming_tasks]
+ assert upcoming_ids == ([schedule.id] if scheduled else [])
+
+
[email protected](loop_scope="session")
+async def test_batch_read_leaves_out_other_users_agents(server: SpinTestServer):
+ owner, stranger = [
+ await get_or_create_user(
+ {
+ "sub": str(uuid.uuid4()),
+ "email": f"batch-{uuid.uuid4().hex[:8]}@example.com",
+ "name": "Batch Read",
+ }
+ )
+ for _ in range(2)
+ ]
+ graph = await server.agent_server.test_create_graph(
+ CreateGraph(graph=create_test_graph()), owner.id
+ )
+ agent = await library_db.get_library_agent_by_graph_id(owner.id, graph.id)
+ assert agent is not None
+
+ assert await _get_many.get_library_agents_by_ids([agent.id], stranger.id) == []
+ assert [
+ a.id for a in await _get_many.get_library_agents_by_ids([agent.id], owner.id)
+ ] == [agent.id]
diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py
index 232932f03d..e877108db1 100644
--- a/autogpt_platform/backend/backend/util/settings.py
+++ b/autogpt_platform/backend/backend/util/settings.py
@@ -604,6 +604,16 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):
description="The pool size for the scheduler database connection pool",
)
+ schedule_read_timeout_seconds: float = Field(
+ default=5.0,
+ gt=0,
+ description=(
+ "How long a page read (library agents, Home) waits for the scheduler "
+ "before rendering without schedule badges. Healthy reads take well "
+ "under a second; the Vercel proxy gives up at 30s."
+ ),
+ )
+
# Prefer the cluster env var so the new image can co-exist with old-image
# pods still reading the unsuffixed RABBITMQ_HOST during a rollout.
rabbitmq_host: str = Field(
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsx b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsx
index d6f0f005e2..4b45607b6c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/__tests__/main.test.tsx
@@ -15,7 +15,7 @@ import {
getUpdateExpertBudgetMockHandler,
} from "@/app/api/__generated__/endpoints/experts/experts.msw";
import {
- getGetV2GetLibraryAgentMockHandler200,
+ getGetV2GetLibraryAgentsByIdsMockHandler200,
getGetV2ListLibraryAgentsMockHandler200,
} from "@/app/api/__generated__/endpoints/library/library.msw";
import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
@@ -425,10 +425,12 @@ describe("ExpertDetailPage", () => {
const user = userEvent.setup();
// The chain stands in for a workflow with no image of its own.
server.use(
- getGetV2GetLibraryAgentMockHandler200({
- ...ownLibraryAgent,
- id: "lib-1",
- } as LibraryAgent),
+ getGetV2GetLibraryAgentsByIdsMockHandler200([
+ {
+ ...ownLibraryAgent,
+ id: "lib-1",
+ } as LibraryAgent,
+ ]),
);
render(<ExpertDetailPage />);
@@ -446,11 +448,13 @@ describe("ExpertDetailPage", () => {
test("shows each workflow's total runs from its library agent", async () => {
server.use(
- getGetV2GetLibraryAgentMockHandler200({
- ...ownLibraryAgent,
- id: "lib-1",
- execution_count: 12,
- } as LibraryAgent),
+ getGetV2GetLibraryAgentsByIdsMockHandler200([
+ {
+ ...ownLibraryAgent,
+ id: "lib-1",
+ execution_count: 12,
+ } as LibraryAgent,
+ ]),
);
render(<ExpertDetailPage />);
@@ -479,11 +483,13 @@ describe("ExpertDetailPage", () => {
test("opens the run dialog from the card's Run button", async () => {
const user = userEvent.setup();
server.use(
- getGetV2GetLibraryAgentMockHandler200({
- ...ownLibraryAgent,
- id: "lib-1",
- name: "Content Calendar",
- } as LibraryAgent),
+ getGetV2GetLibraryAgentsByIdsMockHandler200([
+ {
+ ...ownLibraryAgent,
+ id: "lib-1",
+ name: "Content Calendar",
+ } as LibraryAgent,
+ ]),
);
render(<ExpertDetailPage />);
@@ -599,20 +605,22 @@ describe("ExpertDetailPage", () => {
test("stacks the credentials a workflow will use beside Run", async () => {
server.use(
- getGetV2GetLibraryAgentMockHandler200({
- ...ownLibraryAgent,
- id: "lib-1",
- credentials_input_schema: {
- type: "object",
- properties: {
- github: { credentials_provider: ["github"] },
- notion: { credentials_provider: ["notion"] },
- slack: { credentials_provider: ["slack"] },
- linear: { credentials_provider: ["linear"] },
- discord: { credentials_provider: ["discord"] },
+ getGetV2GetLibraryAgentsByIdsMockHandler200([
+ {
+ ...ownLibraryAgent,
+ id: "lib-1",
+ credentials_input_schema: {
+ type: "object",
+ properties: {
+ github: { credentials_provider: ["github"] },
+ notion: { credentials_provider: ["notion"] },
+ slack: { credentials_provider: ["slack"] },
+ linear: { credentials_provider: ["linear"] },
+ discord: { credentials_provider: ["discord"] },
+ },
},
- },
- } as LibraryAgent),
+ } as LibraryAgent,
+ ]),
);
render(<ExpertDetailPage />);
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowCard.tsx b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowCard.tsx
index f824253fca..ae6ca44ce9 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowCard.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowCard.tsx
@@ -1,6 +1,7 @@
"use client";
import { ExpertWorkflowRef } from "@/app/api/__generated__/models/expertWorkflowRef";
+import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
import { Icon } from "@/components/atoms/Icon/Icon";
import { Text } from "@/components/atoms/Text/Text";
import { safeHumanizeCronExpression } from "@/lib/cron-expression-utils";
@@ -20,6 +21,7 @@ import { WorkflowCredentialStack } from "./WorkflowCredentialStack";
interface Props {
workflow: ExpertWorkflowRef;
+ libraryAgent: LibraryAgent | undefined;
expertId?: string;
coverColor: string | undefined;
onAsk?: (prompt: string) => void;
@@ -27,13 +29,13 @@ interface Props {
export function ExpertWorkflowCard({
workflow,
+ libraryAgent,
expertId,
coverColor,
onAsk,
}: Props) {
const {
name,
- libraryAgent,
runCount,
isTriggerWorkflow,
status,
@@ -44,7 +46,11 @@ export function ExpertWorkflowCard({
chatPrompt,
openRun,
openTriggers,
- } = useExpertWorkflowCard({ workflow, expertId });
+ } = useExpertWorkflowCard({
+ workflow,
+ libraryAgent,
+ expertId,
+ });
const [hasImageError, setHasImageError] = useState(false);
return (
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowListItem.tsx b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowListItem.tsx
index fb2fb91752..1b9bf95aa0 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowListItem.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowListItem.tsx
@@ -1,6 +1,7 @@
"use client";
import { ExpertWorkflowRef } from "@/app/api/__generated__/models/expertWorkflowRef";
+import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
import { Icon } from "@/components/atoms/Icon/Icon";
import { Text } from "@/components/atoms/Text/Text";
import { safeHumanizeCronExpression } from "@/lib/cron-expression-utils";
@@ -21,6 +22,7 @@ import { WorkflowCredentialStack } from "./WorkflowCredentialStack";
interface Props {
workflow: ExpertWorkflowRef;
+ libraryAgent: LibraryAgent | undefined;
expertId?: string;
accentClassName: string;
onAsk?: (prompt: string) => void;
@@ -28,13 +30,13 @@ interface Props {
export function ExpertWorkflowListItem({
workflow,
+ libraryAgent,
expertId,
accentClassName,
onAsk,
}: Props) {
const {
name,
- libraryAgent,
runCount,
isTriggerWorkflow,
status,
@@ -45,7 +47,11 @@ export function ExpertWorkflowListItem({
chatPrompt,
openRun,
openTriggers,
- } = useExpertWorkflowCard({ workflow, expertId });
+ } = useExpertWorkflowCard({
+ workflow,
+ libraryAgent,
+ expertId,
+ });
const [hasImageError, setHasImageError] = useState(false);
const meta = [
workflow.schedule_cron
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowsSection.tsx b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowsSection.tsx
index 941b974a82..152ab70dfe 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowsSection.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/ExpertWorkflowsSection.tsx
@@ -18,6 +18,7 @@ import {
import { ExpertWorkflowCard } from "./ExpertWorkflowCard";
import { ExpertWorkflowListItem } from "./ExpertWorkflowListItem";
import { FilterIconMenu } from "./FilterIconMenu";
+import { useExpertWorkflowAgents } from "./useExpertWorkflowAgents";
import { useExpertWorkflowsView } from "./useExpertWorkflowsView";
import { ViewToggle } from "./ViewToggle";
@@ -51,6 +52,7 @@ export function ExpertWorkflowsSection({
const [filter, setFilter] = useState<WorkflowFilter>("all");
const { view, setView } = useExpertWorkflowsView();
const visible = filterExpertWorkflows(workflows, query, filter);
+ const agentsByID = useExpertWorkflowAgents(workflows);
return (
<section>
@@ -101,6 +103,7 @@ export function ExpertWorkflowsSection({
<ExpertWorkflowListItem
key={workflow.id}
workflow={workflow}
+ libraryAgent={agentsByID.get(workflow.library_agent_id ?? "")}
expertId={expertId}
accentClassName={accentClassName}
onAsk={onAskWorkflow}
@@ -113,6 +116,7 @@ export function ExpertWorkflowsSection({
<ExpertWorkflowCard
key={workflow.id}
workflow={workflow}
+ libraryAgent={agentsByID.get(workflow.library_agent_id ?? "")}
expertId={expertId}
coverColor={coverColor}
onAsk={onAskWorkflow}
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertWorkflowAgents.ts b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertWorkflowAgents.ts
new file mode 100644
index 0000000000..f6f9e0ce21
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertWorkflowAgents.ts
@@ -0,0 +1,24 @@
+import { getGetV2GetLibraryAgentsByIdsQueryOptions } from "@/app/api/__generated__/endpoints/library/library";
+import { ExpertWorkflowRef } from "@/app/api/__generated__/models/expertWorkflowRef";
+import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
+import { okData } from "@/app/api/helpers";
+import { useQueries } from "@tanstack/react-query";
+import { chunkLibraryAgentIDs } from "../../helpers";
+
+export function useExpertWorkflowAgents(workflows: ExpertWorkflowRef[]) {
+ const chunks = chunkLibraryAgentIDs(workflows);
+ const results = useQueries({
+ queries: chunks.map((ids) =>
+ getGetV2GetLibraryAgentsByIdsQueryOptions(
+ { ids },
+ { query: { select: (res) => okData(res) ?? [] } },
+ ),
+ ),
+ });
+
+ const agentsByID = new Map<string, LibraryAgent>();
+ for (const result of results) {
+ for (const agent of result.data ?? []) agentsByID.set(agent.id, agent);
+ }
+ return agentsByID;
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertWorkflowCard.ts b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertWorkflowCard.ts
index 330544ca89..f7eafd0ad4 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertWorkflowCard.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/team/[expertId]/components/useExpertWorkflowCard.ts
@@ -1,7 +1,6 @@
-import { useGetV2GetLibraryAgent } from "@/app/api/__generated__/endpoints/library/library";
import { ExpertWorkflowRef } from "@/app/api/__generated__/models/expertWorkflowRef";
import { GraphExecutionMeta } from "@/app/api/__generated__/models/graphExecutionMeta";
-import { okData } from "@/app/api/helpers";
+import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
import {
Alert01Icon,
Calendar03Icon,
@@ -41,22 +40,18 @@ const STATUS = {
interface Args {
workflow: ExpertWorkflowRef;
+ libraryAgent: LibraryAgent | undefined;
expertId?: string;
}
-export function useExpertWorkflowCard({ workflow, expertId }: Args) {
+export function useExpertWorkflowCard({
+ workflow,
+ libraryAgent,
+ expertId,
+}: Args) {
const router = useRouter();
const name = workflow.name ?? "Unnamed workflow";
const needsSetup = workflowNeedsSetup(workflow);
- const { data: libraryAgent } = useGetV2GetLibraryAgent(
- workflow.library_agent_id ?? "",
- {
- query: {
- select: okData,
- enabled: Boolean(workflow.library_agent_id),
- },
- },
- );
const libraryHref = workflow.library_agent_id
? `/library/agents/${workflow.library_agent_id}`
: null;
@@ -84,7 +79,6 @@ export function useExpertWorkflowCard({ workflow, expertId }: Args) {
return {
name,
needsSetup,
- libraryAgent,
runCount: libraryAgent?.execution_count,
isTriggerWorkflow,
status: STATUS[statusKind],
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/autopilot/__tests__/main.test.tsx b/autogpt_platform/frontend/src/app/(platform)/team/autopilot/__tests__/main.test.tsx
index ae73c54bf8..f70abd6b57 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/autopilot/__tests__/main.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/team/autopilot/__tests__/main.test.tsx
@@ -1,6 +1,6 @@
import { getListExpertsMockHandler } from "@/app/api/__generated__/endpoints/experts/experts.msw";
import {
- getGetV2GetLibraryAgentMockHandler200,
+ getGetV2GetLibraryAgentsByIdsMockHandler200,
getGetV2ListLibraryAgentsMockHandler200,
} from "@/app/api/__generated__/endpoints/library/library.msw";
import { getGetV1ListExecutionSchedulesForAUserMockHandler } from "@/app/api/__generated__/endpoints/schedules/schedules.msw";
@@ -198,7 +198,7 @@ beforeEach(() => {
getGetV2ListLibraryAgentsMockHandler200(
libraryResponse([mariasAgent, freeAgent, strayAgent]),
),
- getGetV2GetLibraryAgentMockHandler200(freeAgent),
+ getGetV2GetLibraryAgentsByIdsMockHandler200([freeAgent, strayAgent]),
getListCopilotSkillsMockHandler200(librarySkills),
);
});
@@ -294,6 +294,62 @@ describe("AutopilotPage", () => {
expect(pagesRequested).toEqual(["1", "2"]);
});
+ test("loads every workflow card's details in a few batched requests, not one per card", async () => {
+ // A user with a big library: one request per card sent 205 at once.
+ const agents = Array.from({ length: 120 }, (_, i) =>
+ makeLibraryAgent({
+ id: `lib-${String(i).padStart(3, "0")}`,
+ graph_id: `graph-many-${i}`,
+ name: `Workflow ${i}`,
+ execution_count: i,
+ }),
+ );
+ const singleReads: string[] = [];
+ const batchSizes: number[] = [];
+ function countLibraryReads({ request }: { request: Request }) {
+ const url = new URL(request.url);
+ if (url.pathname.endsWith("/api/library/agents/batch")) {
+ batchSizes.push(url.searchParams.getAll("ids").length);
+ } else if (/\/api\/library\/agents\/lib-[^/]+$/.test(url.pathname)) {
+ singleReads.push(url.pathname);
+ }
+ }
+ server.use(
+ getGetV2ListLibraryAgentsMockHandler200({
+ ...libraryResponse(agents),
+ pagination: {
+ total_items: agents.length,
+ total_pages: 1,
+ current_page: 1,
+ page_size: agents.length,
+ },
+ } as LibraryAgentResponse),
+ getGetV2GetLibraryAgentsByIdsMockHandler200(({ request }) => {
+ const ids = new URL(request.url).searchParams.getAll("ids");
+ return agents.filter((agent) => ids.includes(agent.id));
+ }),
+ );
+ server.events.on("request:start", countLibraryReads);
+
+ try {
+ render(<AutopilotPage />);
+ await screen.findByRole("heading", { name: "Otto" });
+ await openTab("Workflows");
+
+ expect(await screen.findAllByTestId("expert-workflow-row")).toHaveLength(
+ 120,
+ );
+ const lastCard = (await screen.findByText("Workflow 119")).closest(
+ "[data-testid='expert-workflow-row']",
+ ) as HTMLElement;
+ expect(await within(lastCard).findByText(/119 runs/)).toBeDefined();
+ expect(batchSizes.sort((a, b) => a - b)).toEqual([20, 50, 50]);
+ expect(singleReads).toEqual([]);
+ } finally {
+ server.events.removeListener("request:start", countLibraryReads);
+ }
+ }, 30_000);
+
test("lists the library skills no expert has claimed as Otto's", async () => {
render(<AutopilotPage />);
await screen.findByRole("heading", { name: "Otto" });
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/team/helpers.ts
index 061066c9f3..df5766fed0 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/team/helpers.ts
@@ -393,3 +393,22 @@ export const SETUP_POLL_MS = 2_000;
export function isSettingUp(experts: Expert[]) {
return experts.some((expert) => expert.setup_status === "installing");
}
+
+/** Matches the backend's per-request cap on `GET /library/agents/batch`. */
+const LIBRARY_AGENT_BATCH_SIZE = 50;
+
+/** Sorted so filtering or re-ordering the grid reuses the cached batches. */
+export function chunkLibraryAgentIDs(workflows: ExpertWorkflowRef[]) {
+ const ids = [
+ ...new Set(
+ workflows
+ .map((workflow) => workflow.library_agent_id)
+ .filter((id): id is string => Boolean(id)),
+ ),
+ ].sort();
+ const chunks: string[][] = [];
+ for (let start = 0; start < ids.length; start += LIBRARY_AGENT_BATCH_SIZE) {
+ chunks.push(ids.slice(start, start + LIBRARY_AGENT_BATCH_SIZE));
+ }
+ return chunks;
+}
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 6a5daf57b0..5298c5a18b 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -9800,6 +9800,56 @@
}
}
},
+ "/api/library/agents/batch": {
+ "get": {
+ "tags": ["v2", "library", "private"],
+ "summary": "Get Library Agents By Ids",
+ "description": "Get several library agents at once, as `GET /agents/{id}` would return each.",
+ "operationId": "getV2Get library agents by ids",
+ "security": [{ "HTTPBearerJWT": [] }],
+ "parameters": [
+ {
+ "name": "ids",
+ "in": "query",
+ "required": true,
+ "schema": {
+ "type": "array",
+ "items": { "type": "string" },
+ "minItems": 1,
+ "maxItems": 50,
+ "description": "Library agent IDs; unknown or inaccessible ones are omitted",
+ "title": "Ids"
+ },
+ "description": "Library agent IDs; unknown or inaccessible ones are omitted"
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "Successful Response",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "array",
+ "items": { "$ref": "#/components/schemas/LibraryAgent" },
+ "title": "Response Getv2Get Library Agents By Ids"
+ }
+ }
+ }
+ },
+ "401": {
+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"
+ },
+ "422": {
+ "description": "Validation Error",
+ "content": {
+ "application/json": {
+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }
+ }
+ }
+ }
+ }
+ }
+ },
"/api/library/agents/by-graph/{graph_id}": {
"get": {
"tags": ["v2", "library", "private"],