refactor(LibrarySortMenu): Add LAST_EXECUTION sort with proper backend implementation

This commit is contained in:
keerthi
2025-04-27 12:17:37 +05:30
parent 862c57e405
commit e06e8676be
2 changed files with 11 additions and 5 deletions

View File

@@ -39,7 +39,7 @@ async def list_library_agents(
Args:
user_id: The ID of the user whose LibraryAgents we want to retrieve.
search_term: Optional string to filter agents by name/description.
sort_by: Sorting field (createdAt, updatedAt, isFavorite, isCreatedByUser).
sort_by: Sorting field (createdAt, updatedAt, lastExecution).
page: Current page (1-indexed).
page_size: Number of items per page.
@@ -87,12 +87,17 @@ async def list_library_agents(
]
# Determine sorting
order_by: prisma.types.LibraryAgentOrderByInput | None = None
order_by: list[dict] = []
if sort_by == library_model.LibraryAgentSort.CREATED_AT:
order_by = {"createdAt": "asc"}
order_by = [{"createdAt": "asc"}]
elif sort_by == library_model.LibraryAgentSort.UPDATED_AT:
order_by = {"updatedAt": "desc"}
order_by = [{"updatedAt": "desc"}]
elif sort_by == library_model.LibraryAgentSort.LAST_EXECUTION:
order_by = [
{"AgentGraph": {"Executions": {"startedAt": "desc"}}},
{"updatedAt": "desc"} # fallback if no executions
]
try:
library_agents = await prisma.models.LibraryAgent.prisma().find_many(
@@ -367,7 +372,7 @@ async def add_store_agent_to_library(
Args:
store_listing_version_id: The ID of the store listing version containing the agent.
user_id: The users library to which the agent is being added.
user_id: The user's library to which the agent is being added.
Returns:
The newly created LibraryAgent if successfully added, the existing corresponding one if any.

View File

@@ -235,6 +235,7 @@ class LibraryAgentSort(str, Enum):
CREATED_AT = "createdAt"
UPDATED_AT = "updatedAt"
LAST_EXECUTION = "lastExecution"
class LibraryAgentUpdateRequest(pydantic.BaseModel):