@@ -1328,13 +1328,10 @@ async def get_agents(
1328
1328
# Default to created_at
1329
1329
query = query .order ("created_at" , desc = (sort_order == "desc" ))
1330
1330
1331
- # Execute query to get total count first
1332
- count_result = await query .execute ()
1333
- total_count = count_result .count
1334
-
1335
- # Now get the actual data with pagination
1331
+ # Get paginated data and total count in one request
1336
1332
query = query .range (offset , offset + limit - 1 )
1337
1333
agents_result = await query .execute ()
1334
+ total_count = agents_result .count if agents_result .count is not None else 0
1338
1335
1339
1336
if not agents_result .data :
1340
1337
logger .info (f"No agents found for user: { user_id } " )
@@ -1352,21 +1349,35 @@ async def get_agents(
1352
1349
agents_data = agents_result .data
1353
1350
1354
1351
# First, fetch version data for all agents to ensure we have correct tool info
1352
+ # Do this in a single batched query instead of per-agent service calls
1355
1353
agent_version_map = {}
1356
- for agent in agents_data :
1357
- if agent .get ('current_version_id' ):
1358
- try :
1359
- version_service = await _get_version_service ()
1360
-
1361
- version_obj = await version_service .get_version (
1362
- agent_id = agent ['agent_id' ],
1363
- version_id = agent ['current_version_id' ],
1364
- user_id = user_id
1365
- )
1366
- version_dict = version_obj .to_dict ()
1367
- agent_version_map [agent ['agent_id' ]] = version_dict
1368
- except Exception as e :
1369
- logger .warning (f"Failed to get version data for agent { agent ['agent_id' ]} : { e } " )
1354
+ version_ids = list ({agent ['current_version_id' ] for agent in agents_data if agent .get ('current_version_id' )})
1355
+ if version_ids :
1356
+ try :
1357
+ versions_result = await client .table ('agent_versions' ).select (
1358
+ 'version_id, agent_id, version_number, version_name, is_active, created_at, updated_at, created_by, config'
1359
+ ).in_ ('version_id' , version_ids ).execute ()
1360
+
1361
+ for row in (versions_result .data or []):
1362
+ config = row .get ('config' ) or {}
1363
+ tools = config .get ('tools' ) or {}
1364
+ version_dict = {
1365
+ 'version_id' : row ['version_id' ],
1366
+ 'agent_id' : row ['agent_id' ],
1367
+ 'version_number' : row ['version_number' ],
1368
+ 'version_name' : row ['version_name' ],
1369
+ 'system_prompt' : config .get ('system_prompt' , '' ),
1370
+ 'configured_mcps' : tools .get ('mcp' , []),
1371
+ 'custom_mcps' : tools .get ('custom_mcp' , []),
1372
+ 'agentpress_tools' : tools .get ('agentpress' , {}),
1373
+ 'is_active' : row .get ('is_active' , False ),
1374
+ 'created_at' : row .get ('created_at' ),
1375
+ 'updated_at' : row .get ('updated_at' ) or row .get ('created_at' ),
1376
+ 'created_by' : row .get ('created_by' ),
1377
+ }
1378
+ agent_version_map [row ['agent_id' ]] = version_dict
1379
+ except Exception as e :
1380
+ logger .warning (f"Failed to batch load versions for agents: { e } " )
1370
1381
1371
1382
# Apply tool-based filters using version data
1372
1383
if has_mcp_tools is not None or has_agentpress_tools is not None or tools :
0 commit comments