import type { SupportedBatchEndpoint } from "@/shared/constants/batchEndpoints"; type BatchRouteHandler = (request: Request) => Promise | Response; const handlerLoaders: Record Promise> = { "/v1/responses": async () => (await import("@/app/api/v1/responses/route")).POST, "/v1/chat/completions": async () => (await import("@/app/api/v1/chat/completions/route")).POST, "/v1/embeddings": async () => (await import("@/app/api/v1/embeddings/route")).POST, "/v1/completions": async () => (await import("@/app/api/v1/completions/route")).POST, "/v1/moderations": async () => (await import("@/app/api/v1/moderations/route")).POST, "/v1/images/generations": async () => (await import("@/app/api/v1/images/generations/route")).POST, "/v1/videos/generations": async () => (await import("@/app/api/v1/videos/generations/route")).POST, }; const handlerCache = new Map(); async function getHandler(endpoint: SupportedBatchEndpoint): Promise { const cached = handlerCache.get(endpoint); if (cached) return cached; const handler = await handlerLoaders[endpoint](); handlerCache.set(endpoint, handler); return handler; } async function dispatchBatchApiRequest({ endpoint, body, apiKey, }: { endpoint: SupportedBatchEndpoint; body: Record; apiKey?: string | null; }): Promise { const headers = new Headers({ "Content-Type": "application/json" }); if (apiKey) { headers.set("Authorization", `Bearer ${apiKey}`); } const handler = await getHandler(endpoint); const request = new Request(`http://localhost${endpoint}`, { method: "POST", headers, body: JSON.stringify(body), }); return await handler(request); } export const dispatch = { dispatchBatchApiRequest, };