File size: 1,877 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { SupportedBatchEndpoint } from "@/shared/constants/batchEndpoints";

type BatchRouteHandler = (request: Request) => Promise<Response> | Response;

const handlerLoaders: Record<SupportedBatchEndpoint, () => Promise<BatchRouteHandler>> = {
  "/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<SupportedBatchEndpoint, BatchRouteHandler>();

async function getHandler(endpoint: SupportedBatchEndpoint): Promise<BatchRouteHandler> {
  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<string, unknown>;
  apiKey?: string | null;
}): Promise<Response> {
  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,
};