NODEDC_1C/llm_normalizer/backend/dist/routes/testConnection.js

80 lines
2.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildTestConnectionRouter = buildTestConnectionRouter;
const express_1 = require("express");
const config_1 = require("../config");
const http_1 = require("../utils/http");
function buildTestConnectionRouter(client) {
const router = (0, express_1.Router)();
const handler = async (req, res, next) => {
try {
const body = (req.body ?? {});
const llmProvider = body.llmProvider === "local" ? "local" : "openai";
const model = String(body.model ?? config_1.DEFAULT_MODEL);
const baseUrl = String(body.baseUrl ?? config_1.DEFAULT_OPENAI_BASE_URL);
const apiKey = String(body.apiKey ?? process.env.OPENAI_API_KEY ?? "");
const result = await client.testConnection({
llmProvider,
apiKey,
model,
baseUrl
});
let modelFound = null;
let modelsCount = null;
if (llmProvider === "local") {
try {
const models = await client.listModels({
llmProvider,
apiKey,
model,
baseUrl
});
modelsCount = models.length;
modelFound = models.includes(model);
}
catch {
modelFound = null;
modelsCount = null;
}
}
(0, http_1.ok)(res, {
ok: true,
provider: llmProvider,
model: result.model,
model_found: modelFound,
models_count: modelsCount,
timestamp: new Date().toISOString()
});
}
catch (error) {
next(error);
}
};
const listModelsHandler = async (req, res, next) => {
try {
const body = (req.body ?? {});
const models = await client.listModels({
llmProvider: body.llmProvider === "local" ? "local" : "openai",
apiKey: String(body.apiKey ?? process.env.OPENAI_API_KEY ?? ""),
model: String(body.model ?? config_1.DEFAULT_MODEL),
baseUrl: String(body.baseUrl ?? config_1.DEFAULT_OPENAI_BASE_URL)
});
(0, http_1.ok)(res, {
ok: true,
models,
count: models.length,
timestamp: new Date().toISOString()
});
}
catch (error) {
next(error);
}
};
router.post("/api/llm/test-connection", handler);
router.post("/api/llm/models", listModelsHandler);
// Backward-compatible route for old frontend builds.
router.post("/api/openai/test-connection", handler);
router.post("/api/openai/models", listModelsHandler);
return router;
}