fix(models): support single model lookup
Support single model lookup by replacing the one-segment models route with a catch-all route that preserves capability kind paths while allowing provider-prefixed IDs like cc/claude-sonnet-5.
This commit is contained in:
78
src/app/api/v1/models/[...model]/route.js
Normal file
78
src/app/api/v1/models/[...model]/route.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import { buildModelsList } from "../route.js";
|
||||
|
||||
// URL slug → service kind(s). `web` covers both webSearch and webFetch.
|
||||
const KIND_SLUG_MAP = {
|
||||
"image": ["image"],
|
||||
"tts": ["tts"],
|
||||
"stt": ["stt"],
|
||||
"embedding": ["embedding"],
|
||||
"image-to-text": ["imageToText"],
|
||||
"web": ["webSearch", "webFetch"],
|
||||
};
|
||||
|
||||
const LLM_KIND = "llm";
|
||||
|
||||
export async function OPTIONS() {
|
||||
return new Response(null, {
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "*",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function json(data, options = {}) {
|
||||
return Response.json(data, {
|
||||
...options,
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /v1/models/{kind} - OpenAI-compatible models list filtered by capability.
|
||||
* GET /v1/models/{provider}/{model} - OpenAI-compatible single model lookup.
|
||||
* Supported kinds: image, tts, stt, embedding, image-to-text, web.
|
||||
*/
|
||||
export async function GET(_request, { params }) {
|
||||
try {
|
||||
const { model } = await params;
|
||||
const path = Array.isArray(model) ? model : [model];
|
||||
const identifier = path.filter(Boolean).join("/");
|
||||
const kindFilter = path.length === 1 ? KIND_SLUG_MAP[identifier] : null;
|
||||
|
||||
if (kindFilter) {
|
||||
const data = await buildModelsList(kindFilter);
|
||||
return json({ object: "list", data });
|
||||
}
|
||||
|
||||
// Match the same LLM catalog exposed by GET /v1/models. A catch-all
|
||||
// parameter is required because provider-prefixed IDs contain a slash.
|
||||
const models = await buildModelsList([LLM_KIND]);
|
||||
const matchedModel = models.find((candidate) => candidate.id === identifier);
|
||||
|
||||
if (!matchedModel) {
|
||||
return json(
|
||||
{
|
||||
error: {
|
||||
message: `The model '${identifier}' does not exist or you do not have access to it.`,
|
||||
type: "invalid_request_error",
|
||||
code: "model_not_found",
|
||||
},
|
||||
},
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
return json(matchedModel);
|
||||
} catch (error) {
|
||||
console.log("Error fetching model:", error);
|
||||
return json(
|
||||
{ error: { message: error.message, type: "server_error" } },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
import { buildModelsList } from "../route.js";
|
||||
|
||||
// URL slug → service kind(s). `web` covers both webSearch and webFetch.
|
||||
const KIND_SLUG_MAP = {
|
||||
"image": ["image"],
|
||||
"tts": ["tts"],
|
||||
"stt": ["stt"],
|
||||
"embedding": ["embedding"],
|
||||
"image-to-text": ["imageToText"],
|
||||
"web": ["webSearch", "webFetch"],
|
||||
};
|
||||
|
||||
export async function OPTIONS() {
|
||||
return new Response(null, {
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "*",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /v1/models/{kind} - OpenAI-compatible models list filtered by capability.
|
||||
* Supported kinds: image, tts, stt, embedding, image-to-text, web.
|
||||
*/
|
||||
export async function GET(_request, { params }) {
|
||||
try {
|
||||
const { kind } = await params;
|
||||
const kindFilter = KIND_SLUG_MAP[kind];
|
||||
|
||||
if (!kindFilter) {
|
||||
return Response.json(
|
||||
{
|
||||
error: {
|
||||
message: `Unknown model kind: ${kind}. Supported: ${Object.keys(KIND_SLUG_MAP).join(", ")}`,
|
||||
type: "invalid_request_error",
|
||||
},
|
||||
},
|
||||
{ status: 404, headers: { "Access-Control-Allow-Origin": "*" } }
|
||||
);
|
||||
}
|
||||
|
||||
const data = await buildModelsList(kindFilter);
|
||||
return Response.json({ object: "list", data }, {
|
||||
headers: { "Access-Control-Allow-Origin": "*" },
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Error fetching models by kind:", error);
|
||||
return Response.json(
|
||||
{ error: { message: error.message, type: "server_error" } },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user