File size: 1,019 Bytes
dcd5e1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextRequest, NextResponse } from "next/server";
import { MODELS } from "@/lib/providers";

export async function POST(request: NextRequest) {
  const body = await request.json();
  const { model, provider, openrouterApiKey } = body;

  // Enhanced OpenRouter detection logic (same as main API)
  const isExplicitOpenRouter = provider === "openrouter" || !!openrouterApiKey;
  const modelExistsInHF = MODELS.find((m) => m.value === model || m.label === model);
  const isOpenRouterRequest = isExplicitOpenRouter || (!modelExistsInHF && model);
  
  const selectedModel = !isOpenRouterRequest 
    ? MODELS.find((m) => m.value === model || m.label === model)
    : null;

  return NextResponse.json({
    input: { model, provider, hasApiKey: !!openrouterApiKey },
    detection: {
      isExplicitOpenRouter,
      modelExistsInHF: !!modelExistsInHF,
      isOpenRouterRequest,
      selectedModel: selectedModel?.value || null,
      modelToUse: isOpenRouterRequest ? model : selectedModel?.value
    }
  });
}