From a4677a1aeca03554ad0a5c65fc54ed300bcaaf0b Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Mon, 11 Aug 2025 17:40:02 +0100 Subject: [PATCH 1/2] add --- app/api/agents/route.ts | 79 ++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/app/api/agents/route.ts b/app/api/agents/route.ts index 8657097..0889c74 100644 --- a/app/api/agents/route.ts +++ b/app/api/agents/route.ts @@ -3,6 +3,26 @@ import { kv } from "@vercel/kv"; import { listAgentsFiltered, createAgent, Agent } from "@bitte-ai/data"; import { stringifyJsonWithBigint } from "@/lib/utils"; +const getTotalPingsByAgentIds = async ( + agentIds: string[] +): Promise> => { + if (agentIds.length === 0) { + return {}; + } + + const pipeline = kv.pipeline(); + agentIds.forEach((id) => { + pipeline.get(`smart-action:v1.0:agent:${id}:pings`); + }); + + const values = await pipeline.exec(); + + return agentIds.reduce((acc, id, index) => { + acc[id] = values[index]; + return acc; + }, {} as Record); +}; + export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); @@ -17,33 +37,54 @@ export async function GET(request: NextRequest) { const accountId = searchParams.get("accountId") || undefined; const searchTerm = searchParams.get("searchTerm") || undefined; + // When verifiedOnly is false, we need to fetch all agents first to sort by pings properly + // before applying pagination. Otherwise, we paginate before sorting which gives wrong results. + const shouldSortByPings = verifiedOnly === false; + const agents = await listAgentsFiltered({ searchTerm, verified: verifiedOnly, chainIds, - offset, - limit, + offset: shouldSortByPings ? 0 : offset, + limit: shouldSortByPings ? 20000 : limit, // Get all agents when we need to sort by pings categories: category ? [category] : undefined, accountId, }); if (agents.length === 0) { - return NextResponse.json(agents, { status: 200 }); + const response = NextResponse.json(agents, { status: 200 }); + response.headers.set('Cache-Control', 'public, max-age=60, s-maxage=120'); + return response; } - const agentIds = agents.map((agent) => agent.id); + const agentIds = agents.map((agent: Agent) => agent.id); const pingsByAgent = await getTotalPingsByAgentIds(agentIds); - const agentsWithPings = agents.map((agent) => ({ + const agentsWithPings = agents.map((agent: Agent) => ({ ...agent, pings: pingsByAgent[agent.id] || 0, })); - const sortedAgents = agentsWithPings.sort( - (a, b) => (b.pings as number) - (a.pings as number) - ); + let finalAgents = agentsWithPings; + + // Sort by pings when verifiedOnly is false + if (shouldSortByPings) { + const sortedAgents = agentsWithPings.sort( + (a: any, b: any) => (b.pings as number) - (a.pings as number) + ); - return NextResponse.json(JSON.parse(stringifyJsonWithBigint(sortedAgents))); + // Apply pagination after sorting + finalAgents = sortedAgents.slice(offset, offset + limit); + } + + const result = JSON.parse(stringifyJsonWithBigint(finalAgents)); + + const response = NextResponse.json(result); + // Add cache headers for browser/CDN caching + response.headers.set('Cache-Control', 'public, max-age=60, s-maxage=120'); + response.headers.set('Vary', 'Accept-Encoding'); + + return response; } catch (error) { console.error("Error fetching agents:", error); return NextResponse.json( @@ -97,23 +138,3 @@ export async function POST(request: NextRequest) { ); } } - -const getTotalPingsByAgentIds = async ( - agentIds: string[] -): Promise> => { - if (agentIds.length === 0) { - return {}; - } - - const pipeline = kv.pipeline(); - agentIds.forEach((id) => { - pipeline.get(`smart-action:v1.0:agent:${id}:pings`); - }); - - const values = await pipeline.exec(); - - return agentIds.reduce((acc, id, index) => { - acc[id] = values[index]; - return acc; - }, {} as Record); -}; From 289e587044e3222b69e8abedfb594ed4d44574b0 Mon Sep 17 00:00:00 2001 From: Ruben Marcus Date: Tue, 12 Aug 2025 12:58:31 +0100 Subject: [PATCH 2/2] fix registry --- app/api/agents/route.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/api/agents/route.ts b/app/api/agents/route.ts index 0889c74..df70760 100644 --- a/app/api/agents/route.ts +++ b/app/api/agents/route.ts @@ -3,6 +3,8 @@ import { kv } from "@vercel/kv"; import { listAgentsFiltered, createAgent, Agent } from "@bitte-ai/data"; import { stringifyJsonWithBigint } from "@/lib/utils"; +type AgentWithPings = Agent & { pings: number }; + const getTotalPingsByAgentIds = async ( agentIds: string[] ): Promise> => { @@ -60,7 +62,7 @@ export async function GET(request: NextRequest) { const agentIds = agents.map((agent: Agent) => agent.id); const pingsByAgent = await getTotalPingsByAgentIds(agentIds); - const agentsWithPings = agents.map((agent: Agent) => ({ + const agentsWithPings: AgentWithPings[] = agents.map((agent: Agent) => ({ ...agent, pings: pingsByAgent[agent.id] || 0, })); @@ -70,7 +72,7 @@ export async function GET(request: NextRequest) { // Sort by pings when verifiedOnly is false if (shouldSortByPings) { const sortedAgents = agentsWithPings.sort( - (a: any, b: any) => (b.pings as number) - (a.pings as number) + (a: AgentWithPings, b: AgentWithPings) => b.pings - a.pings ); // Apply pagination after sorting