Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions app/api/tools/[agentId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { listTools } from "@bitte-ai/data";
import { NextResponse } from "next/server";
import { kv } from "@vercel/kv";
import { BittePrimitiveNames } from "@/lib/constants";
import { FunctionDefinition } from "openai/resources/shared.mjs";

const getPingsByTool = async (toolName: string): Promise<number | null> => {
return await kv.get<number>(`smart-action:v1.0:tool:${toolName}:pings`);
};

export async function GET(
_request: Request,
{ params }: { params: Promise<{ agentId: string }> }
) {
try {
const agentId = (await params).agentId;

// Get all tools and filter by agentId
const allTools = await listTools();
const tools = allTools.filter((tool) => tool.agentId === agentId);

if (tools.length === 0) {
return NextResponse.json([]);
}

// Add isPrimitive flag to each tool
const toolsWithPrimitiveFlags = tools.map((tool) => ({
...tool,
isPrimitive: BittePrimitiveNames.includes(tool.id),
}));

// Add pings data to each tool
const toolsWithPings = await Promise.all(
toolsWithPrimitiveFlags.map(async (tool) => {
const pings = await getPingsByTool(
(tool.function as unknown as FunctionDefinition).name
);
return {
...tool,
pings: pings || 0,
};
})
);

// Sort by pings (most pinged first)
const sortedTools = toolsWithPings.sort((a, b) => b.pings - a.pings);

return NextResponse.json(sortedTools);
} catch (error) {
console.error("Error fetching agent tools:", error);
return NextResponse.json(
{ error: "Failed to fetch agent tools" },
{ status: 500 }
);
}
}