Refactor error handling to config-driven approach with centralized error rules
Made-with: Cursor
This commit is contained in:
parent
b1288c5064
commit
b669b6ffc1
20 changed files with 1056 additions and 991 deletions
49
src/app/api/providers/suggested-models/route.js
Normal file
49
src/app/api/providers/suggested-models/route.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
import { NextResponse } from "next/server";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const FILTERS = {
|
||||
"openrouter-free": (models) =>
|
||||
models
|
||||
.filter(
|
||||
(m) =>
|
||||
m.pricing?.prompt === "0" &&
|
||||
m.pricing?.completion === "0" &&
|
||||
m.context_length >= 200000
|
||||
)
|
||||
.map((m) => ({ id: m.id, name: m.name, contextLength: m.context_length }))
|
||||
.sort((a, b) => b.contextLength - a.contextLength),
|
||||
|
||||
"opencode-free": (models) =>
|
||||
models
|
||||
.filter((m) => m.id?.endsWith("-free"))
|
||||
.map((m) => ({ id: m.id, name: m.id })),
|
||||
};
|
||||
|
||||
export async function GET(request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const url = searchParams.get("url");
|
||||
const type = searchParams.get("type");
|
||||
|
||||
if (!url || !type) {
|
||||
return NextResponse.json({ error: "Missing url or type" }, { status: 400 });
|
||||
}
|
||||
|
||||
const filter = FILTERS[type];
|
||||
if (!filter) {
|
||||
return NextResponse.json({ error: "Unknown filter type" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) {
|
||||
return NextResponse.json({ data: [] });
|
||||
}
|
||||
const json = await res.json();
|
||||
const raw = json.data ?? json.models ?? json;
|
||||
const data = filter(Array.isArray(raw) ? raw : []);
|
||||
return NextResponse.json({ data });
|
||||
} catch {
|
||||
return NextResponse.json({ data: [] });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue