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(platform/library): Add 'Most Recently Ran' sorting (#13666)

Shreyash3fa88a70efd2
diff --git a/autogpt_platform/backend/backend/api/features/library/db.py b/autogpt_platform/backend/backend/api/features/library/db.py
index 621899cdcf..d9633edd0b 100644
--- a/autogpt_platform/backend/backend/api/features/library/db.py
+++ b/autogpt_platform/backend/backend/api/features/library/db.py
@@ -204,12 +204,27 @@ async def list_library_agents(
},
]
- order_by: prisma.types.LibraryAgentOrderByInput | None = None
+ order_by: (
+ prisma.types.LibraryAgentOrderByInput
+ | list[prisma.types.LibraryAgentOrderByInput]
+ | None
+ ) = None
if sort_by == library_model.LibraryAgentSort.CREATED_AT:
order_by = {"createdAt": "asc"}
elif sort_by == library_model.LibraryAgentSort.UPDATED_AT:
order_by = {"updatedAt": "desc"}
+ elif sort_by == library_model.LibraryAgentSort.LAST_RUN:
+ # lastRunAt desc with never-run agents last, ordered by updatedAt among
+ # themselves. Prisma Python's types omit the nulls option, but the query
+ # engine honors it at runtime.
+ order_by = cast(
+ list[prisma.types.LibraryAgentOrderByInput],
+ [
+ {"lastRunAt": {"sort": "desc", "nulls": "last"}},
+ {"updatedAt": "desc"},
+ ],
+ )
library_agents = await prisma.models.LibraryAgent.prisma().find_many(
where=where_clause,
diff --git a/autogpt_platform/backend/backend/api/features/library/model.py b/autogpt_platform/backend/backend/api/features/library/model.py
index 5dc79fc73a..d49a262385 100644
--- a/autogpt_platform/backend/backend/api/features/library/model.py
+++ b/autogpt_platform/backend/backend/api/features/library/model.py
@@ -175,6 +175,7 @@ class LibraryAgent(pydantic.BaseModel):
created_at: datetime.datetime
updated_at: datetime.datetime
+ last_run_at: datetime.datetime | None = None
name: str
description: str
@@ -347,6 +348,7 @@ class LibraryAgent(pydantic.BaseModel):
status=status,
created_at=created_at,
updated_at=updated_at,
+ last_run_at=agent.lastRunAt,
name=graph.name,
description=graph.description,
instructions=graph.instructions,
@@ -584,6 +586,7 @@ class LibraryAgentSort(str, Enum):
CREATED_AT = "createdAt"
UPDATED_AT = "updatedAt"
+ LAST_RUN = "lastRunAt"
class LibraryAgentUpdateRequest(pydantic.BaseModel):
diff --git a/autogpt_platform/backend/backend/data/execution.py b/autogpt_platform/backend/backend/data/execution.py
index 181dce3efc..6717301bd5 100644
--- a/autogpt_platform/backend/backend/data/execution.py
+++ b/autogpt_platform/backend/backend/data/execution.py
@@ -25,6 +25,7 @@ from prisma.models import (
AgentNodeExecution,
AgentNodeExecutionInputOutput,
AgentNodeExecutionKeyValueData,
+ LibraryAgent,
SharedExecutionFile,
UserWorkspace,
UserWorkspaceFile,
@@ -920,6 +921,26 @@ async def create_graph_execution(
include=GRAPH_EXECUTION_INCLUDE_WITH_NODES,
)
+ # Advance lastRunAt only forward (using this execution's creation time), so a
+ # delayed older execution can't overwrite a newer run's timestamp.
+ run_time = result.createdAt
+ try:
+ await LibraryAgent.prisma().update_many(
+ where={
+ "agentGraphId": graph_id,
+ "userId": user_id,
+ "OR": [
+ {"lastRunAt": None},
+ {"lastRunAt": {"lt": run_time}},
+ ],
+ },
+ data={"lastRunAt": run_time},
+ )
+ except Exception as e:
+ logger.warning(
+ f"Failed to update LibraryAgent lastRunAt for graph {graph_id}: {e}"
+ )
+
return GraphExecutionWithNodes.from_db(result)
diff --git a/autogpt_platform/backend/migrations/20260723120000_add_library_agent_last_run_at/migration.sql b/autogpt_platform/backend/migrations/20260723120000_add_library_agent_last_run_at/migration.sql
new file mode 100644
index 0000000000..309c6a4770
--- /dev/null
+++ b/autogpt_platform/backend/migrations/20260723120000_add_library_agent_last_run_at/migration.sql
@@ -0,0 +1,15 @@
+-- Nullable "last run" timestamp for sorting the library by most recently run.
+-- AlterTable
+ALTER TABLE "LibraryAgent" ADD COLUMN "lastRunAt" TIMESTAMP(3);
+
+-- Backfill from each agent's most recent execution so existing rows aren't all
+-- NULL. Uses createdAt to match create_graph_execution's forward-path stamping.
+UPDATE "LibraryAgent" la
+SET "lastRunAt" = sub.last_run
+FROM (
+ SELECT "agentGraphId", "userId", MAX("createdAt") AS last_run
+ FROM "AgentGraphExecution"
+ GROUP BY "agentGraphId", "userId"
+) sub
+WHERE la."agentGraphId" = sub."agentGraphId"
+ AND la."userId" = sub."userId";
diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prisma
index 6f094492e8..2f6c0014b1 100644
--- a/autogpt_platform/backend/schema.prisma
+++ b/autogpt_platform/backend/schema.prisma
@@ -601,6 +601,7 @@ model LibraryAgent {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
+ lastRunAt DateTime?
userId String
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
diff --git a/autogpt_platform/backend/snapshots/lib_agts_search b/autogpt_platform/backend/snapshots/lib_agts_search
index 8100ad64e4..04fc2e30af 100644
--- a/autogpt_platform/backend/snapshots/lib_agts_search
+++ b/autogpt_platform/backend/snapshots/lib_agts_search
@@ -10,6 +10,7 @@
"status": "COMPLETED",
"created_at": "2023-01-01T00:00:00",
"updated_at": "2023-01-01T00:00:00",
+ "last_run_at": null,
"name": "Test Agent 1",
"description": "Test Description 1",
"instructions": null,
@@ -60,6 +61,7 @@
"status": "COMPLETED",
"created_at": "2023-01-01T00:00:00",
"updated_at": "2023-01-01T00:00:00",
+ "last_run_at": null,
"name": "Test Agent 2",
"description": "Test Description 2",
"instructions": null,
diff --git a/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/LibrarySortMenu.tsx b/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/LibrarySortMenu.tsx
index ffbc7d09c3..903abb6395 100644
--- a/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/LibrarySortMenu.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/LibrarySortMenu.tsx
@@ -35,6 +35,7 @@ export function LibrarySortMenu({ setLibrarySort }: Props) {
<SelectItem value={LibraryAgentSort.updatedAt}>
Last Modified
</SelectItem>
+ <SelectItem value={LibraryAgentSort.lastRunAt}>Last Run</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
diff --git a/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/useLibrarySortMenu.ts b/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/useLibrarySortMenu.ts
index e6d6f2d127..5dcfdf9292 100644
--- a/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/useLibrarySortMenu.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/library/components/LibrarySortMenu/useLibrarySortMenu.ts
@@ -15,6 +15,8 @@ export function useLibrarySortMenu({ setLibrarySort }: Props) {
return "Creation Date";
case LibraryAgentSort.updatedAt:
return "Last Modified";
+ case LibraryAgentSort.lastRunAt:
+ return "Last Run";
default:
return "Last Modified";
}
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index d7bdb39cdf..bce17529d7 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -17760,6 +17760,13 @@
"format": "date-time",
"title": "Updated At"
},
+ "last_run_at": {
+ "anyOf": [
+ { "type": "string", "format": "date-time" },
+ { "type": "null" }
+ ],
+ "title": "Last Run At"
+ },
"name": { "type": "string", "title": "Name" },
"description": { "type": "string", "title": "Description" },
"instructions": {
@@ -18092,7 +18099,7 @@
},
"LibraryAgentSort": {
"type": "string",
- "enum": ["createdAt", "updatedAt"],
+ "enum": ["createdAt", "updatedAt", "lastRunAt"],
"title": "LibraryAgentSort",
"description": "Possible sort options for sorting library agents."
},
diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts
index 5934dcaf16..e15ed79df8 100644
--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts
+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts
@@ -600,6 +600,7 @@ export type LibraryAgentPresetUpdatable = Partial<
export enum LibraryAgentSortEnum {
CREATED_AT = "createdAt",
UPDATED_AT = "updatedAt",
+ LAST_RUN = "lastRunAt",
}
/* *** CREDENTIALS *** */