82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
import { NextFunction, Request, Response, Router } from "express";
|
|
import { DEFAULT_MODEL, DEFAULT_OPENAI_BASE_URL } from "../config";
|
|
import { OpenAIResponsesClient } from "../services/openaiResponsesClient";
|
|
import { ok } from "../utils/http";
|
|
|
|
export function buildTestConnectionRouter(client: OpenAIResponsesClient): Router {
|
|
const router = Router();
|
|
|
|
const handler = async (req: Request, res: Response, next: NextFunction) => {
|
|
try {
|
|
const body = (req.body ?? {}) as Record<string, unknown>;
|
|
const llmProvider = body.llmProvider === "local" ? "local" : "openai";
|
|
const model = String(body.model ?? DEFAULT_MODEL);
|
|
const baseUrl = String(body.baseUrl ?? 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: boolean | null = null;
|
|
let modelsCount: number | null = 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;
|
|
}
|
|
}
|
|
|
|
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: Request, res: Response, next: NextFunction) => {
|
|
try {
|
|
const body = (req.body ?? {}) as Record<string, unknown>;
|
|
const models = await client.listModels({
|
|
llmProvider: body.llmProvider === "local" ? "local" : "openai",
|
|
apiKey: String(body.apiKey ?? process.env.OPENAI_API_KEY ?? ""),
|
|
model: String(body.model ?? DEFAULT_MODEL),
|
|
baseUrl: String(body.baseUrl ?? DEFAULT_OPENAI_BASE_URL)
|
|
});
|
|
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;
|
|
}
|