Files
9router/src/app/api/usage/providers/route.js
T

41 lines
1.3 KiB
JavaScript
Raw Normal View History

2026-02-09 11:30:42 +08:00
import { NextResponse } from "next/server";
import { getDistinctProviders } from "@/lib/requestDetailsDb";
2026-02-09 11:30:42 +08:00
import { getProviderNodes } from "@/lib/localDb";
import { AI_PROVIDERS, getProviderByAlias } from "@/shared/constants/providers";
/**
* GET /api/usage/providers
* Returns list of unique providers from request details
*/
export async function GET() {
try {
// Query DISTINCT provider column directly — avoids parsing every row's
// full JSON blob (can be hundreds of MB), which previously caused OOM.
const providerIds = await getDistinctProviders();
2026-02-09 11:30:42 +08:00
const providerNodes = await getProviderNodes();
const nodeMap = {};
for (const node of providerNodes) {
nodeMap[node.id] = node.name;
}
const providers = providerIds.map(providerId => {
2026-02-09 11:30:42 +08:00
let name = providerId;
if (nodeMap[providerId]) {
name = nodeMap[providerId];
} else {
2026-02-09 11:30:42 +08:00
const providerConfig = getProviderByAlias(providerId) || AI_PROVIDERS[providerId];
if (providerConfig?.name) name = providerConfig.name;
2026-02-09 11:30:42 +08:00
}
return { id: providerId, name };
2026-02-09 11:30:42 +08:00
});
return NextResponse.json({ providers });
} catch (error) {
console.error("[API] Failed to get providers:", error);
return NextResponse.json(
{ error: "Failed to fetch providers" },
{ status: 500 }
);
}
}