fix: correct tools search API response mapping and add collection search

- Map tools search response from data.results.tools to expected format
- Map package.npmPackageName to top-level npmPackageName for UI
- Add search parameter support to collections API
This commit is contained in:
Ajax Davis 2026-01-03 03:34:17 +10:00
parent 5a0a21fd60
commit 4f95046249
2 changed files with 28 additions and 5 deletions

View file

@ -58,14 +58,23 @@ export async function GET(request: NextRequest): Promise<NextResponse<ApiRespons
);
}
// Parse pagination params
// Parse pagination and search params
const searchParams = request.nextUrl.searchParams;
const limit = Math.min(Math.max(Number.parseInt(searchParams.get('limit') || '20', 10), 1), 50);
const offset = Math.max(Number.parseInt(searchParams.get('offset') || '0', 10), 0);
const search = searchParams.get('search')?.trim();
// Fetch collections with tool count
const collections = await prisma.collection.findMany({
where: { userId: session.user.id },
where: {
userId: session.user.id,
...(search && {
OR: [
{ name: { contains: search, mode: 'insensitive' } },
{ description: { contains: search, mode: 'insensitive' } },
],
}),
},
include: {
_count: { select: { tools: true } },
},

View file

@ -209,10 +209,24 @@ export default function AgentDetailPage(): React.ReactElement {
try {
const response = await fetch(`/api/tools/search?q=${encodeURIComponent(query)}&limit=10`);
const data = await response.json();
if (data.success) {
// Filter out tools already attached
if (data.success && data.results?.tools) {
// Filter out tools already attached and map to expected shape
const attachedToolIds = new Set(agentTools.map((t) => t.toolId));
const filtered = (data.data || []).filter((t: SearchTool) => !attachedToolIds.has(t.id));
const filtered = data.results.tools
.filter((t: { id: string }) => !attachedToolIds.has(t.id))
.map(
(t: {
id: string;
name: string;
description: string | null;
package: { npmPackageName: string };
}) => ({
id: t.id,
name: t.name,
description: t.description,
npmPackageName: t.package.npmPackageName,
})
);
setToolSearchResults(filtered);
}
} catch (err) {