(req: Request)
| 340 | // Main request handler |
| 341 | // ---------------------------------------------------------------------------- |
| 342 | async function handleRequest(req: Request): Promise<Response> { |
| 343 | const url = new URL(req.url); |
| 344 | const { pathname } = url; |
| 345 | |
| 346 | // ------------------------------------------------ GET /debug |
| 347 | if (pathname === "/debug" && req.method === "GET") { |
| 348 | const enable = url.searchParams.get("enable"); |
| 349 | if (enable !== null) DEBUG_MODE = enable === "true"; |
| 350 | return jsonResponse({ debug_mode: DEBUG_MODE }); |
| 351 | } |
| 352 | |
| 353 | // ------------------------------------------------ GET /v1/models (requires auth) |
| 354 | if (pathname === "/v1/models" && req.method === "GET") { |
| 355 | try { |
| 356 | requireAuth(req); |
| 357 | } catch (e) { |
| 358 | return jsonResponse({ error: "unauthorized" }, e.message === "403" ? 403 : 401); |
| 359 | } |
| 360 | return jsonResponse({ |
| 361 | object: "list", |
| 362 | data: AVAILABLE_MODELS.map((m) => ({ |
| 363 | id: m.id, |
| 364 | object: "model", |
| 365 | created: Math.floor(Date.now() / 1000), |
| 366 | owned_by: m.owned_by, |
| 367 | name: `${m.name} (${m.model_name})`, |
| 368 | })), |
| 369 | }); |
| 370 | } |
| 371 | |
| 372 | // ------------------------------------------------ GET /models (public) |
| 373 | if (pathname === "/models" && req.method === "GET") { |
| 374 | return jsonResponse({ |
| 375 | object: "list", |
| 376 | data: AVAILABLE_MODELS.map((m) => ({ |
| 377 | id: m.id, |
| 378 | object: "model", |
| 379 | created: Math.floor(Date.now() / 1000), |
| 380 | owned_by: m.owned_by, |
| 381 | name: `${m.name} (${m.model_name})`, |
| 382 | })), |
| 383 | }); |
| 384 | } |
| 385 | |
| 386 | // ------------------------------------------------ POST /v1/chat/completions |
| 387 | if (pathname === "/v1/chat/completions" && req.method === "POST") { |
| 388 | // auth |
| 389 | try { |
| 390 | requireAuth(req); |
| 391 | } catch (e) { |
| 392 | return jsonResponse({ error: "unauthorized" }, e.message === "403" ? 403 : 401); |
| 393 | } |
| 394 | |
| 395 | // parse body |
| 396 | let body: ChatCompletionRequest; |
| 397 | try { |
| 398 | body = await req.json(); |
| 399 | } catch { |
nothing calls this directly
no test coverage detected