2026-02-21 19:03:05 -08:00
|
|
|
import { DEFAULT_PROVIDER } from "../../agents/defaults.js";
|
|
|
|
|
import { buildAllowedModelSet } from "../../agents/model-selection.js";
|
|
|
|
|
import { loadConfig } from "../../config/config.js";
|
2026-01-04 04:05:18 +01:00
|
|
|
import {
|
|
|
|
|
ErrorCodes,
|
|
|
|
|
errorShape,
|
|
|
|
|
formatValidationErrors,
|
|
|
|
|
validateModelsListParams,
|
|
|
|
|
} from "../protocol/index.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
import type { GatewayRequestHandlers } from "./types.js";
|
2026-01-04 04:05:18 +01:00
|
|
|
|
|
|
|
|
export const modelsHandlers: GatewayRequestHandlers = {
|
|
|
|
|
"models.list": async ({ params, respond, context }) => {
|
|
|
|
|
if (!validateModelsListParams(params)) {
|
|
|
|
|
respond(
|
|
|
|
|
false,
|
|
|
|
|
undefined,
|
|
|
|
|
errorShape(
|
|
|
|
|
ErrorCodes.INVALID_REQUEST,
|
|
|
|
|
`invalid models.list params: ${formatValidationErrors(validateModelsListParams.errors)}`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
2026-02-21 19:03:05 -08:00
|
|
|
const catalog = await context.loadGatewayModelCatalog();
|
|
|
|
|
const cfg = loadConfig();
|
|
|
|
|
const { allowedCatalog } = buildAllowedModelSet({
|
|
|
|
|
cfg,
|
|
|
|
|
catalog,
|
|
|
|
|
defaultProvider: DEFAULT_PROVIDER,
|
|
|
|
|
});
|
|
|
|
|
const models = allowedCatalog.length > 0 ? allowedCatalog : catalog;
|
2026-01-04 04:05:18 +01:00
|
|
|
respond(true, { models }, undefined);
|
|
|
|
|
} catch (err) {
|
2026-01-14 14:31:43 +00:00
|
|
|
respond(false, undefined, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
|
2026-01-04 04:05:18 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|