108 lines
4.1 KiB
JavaScript
108 lines
4.1 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.buildSharedLlmConfigRouter = buildSharedLlmConfigRouter;
|
|
const express_1 = require("express");
|
|
const config_1 = require("../config");
|
|
const http_1 = require("../utils/http");
|
|
const files_1 = require("../utils/files");
|
|
const path_1 = __importDefault(require("path"));
|
|
function sanitizeString(value, fallback) {
|
|
if (typeof value !== "string") {
|
|
return fallback;
|
|
}
|
|
const trimmed = value.trim();
|
|
return trimmed.length > 0 ? trimmed : fallback;
|
|
}
|
|
function sanitizeNumber(value, fallback) {
|
|
if (typeof value === "number" && Number.isFinite(value)) {
|
|
return value;
|
|
}
|
|
if (typeof value === "string" && value.trim().length > 0) {
|
|
const parsed = Number(value);
|
|
if (Number.isFinite(parsed)) {
|
|
return parsed;
|
|
}
|
|
}
|
|
return fallback;
|
|
}
|
|
function buildFallbackRecord() {
|
|
return {
|
|
schema_version: "shared_llm_connection_v1",
|
|
updated_at: "",
|
|
connection: {
|
|
llmProvider: "local",
|
|
model: "qwen2.5-14b-instruct-1m",
|
|
baseUrl: "http://127.0.0.1:1234/v1",
|
|
temperature: 0,
|
|
maxOutputTokens: 900
|
|
}
|
|
};
|
|
}
|
|
function loadSharedRecord() {
|
|
const fallback = buildFallbackRecord();
|
|
const parsed = (0, files_1.readJsonFile)(config_1.SHARED_LLM_CONNECTION_FILE, null);
|
|
if (!parsed || typeof parsed !== "object" || !("connection" in parsed) || !parsed.connection) {
|
|
return null;
|
|
}
|
|
const connection = parsed.connection;
|
|
return {
|
|
schema_version: "shared_llm_connection_v1",
|
|
updated_at: sanitizeString(parsed.updated_at, ""),
|
|
connection: {
|
|
llmProvider: connection.llmProvider === "local" ? "local" : "openai",
|
|
model: sanitizeString(connection.model, fallback.connection.model),
|
|
baseUrl: sanitizeString(connection.baseUrl, fallback.connection.baseUrl),
|
|
temperature: sanitizeNumber(connection.temperature, fallback.connection.temperature),
|
|
maxOutputTokens: Math.max(1, Math.trunc(sanitizeNumber(connection.maxOutputTokens, fallback.connection.maxOutputTokens)))
|
|
}
|
|
};
|
|
}
|
|
function buildSharedLlmConfigRouter() {
|
|
const router = (0, express_1.Router)();
|
|
router.get("/api/llm/shared-connection", (_req, res, next) => {
|
|
try {
|
|
const record = loadSharedRecord();
|
|
(0, http_1.ok)(res, {
|
|
ok: true,
|
|
connection: record?.connection ?? null,
|
|
updated_at: record?.updated_at ?? null,
|
|
exists: Boolean(record)
|
|
});
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
router.post("/api/llm/shared-connection", (req, res, next) => {
|
|
try {
|
|
const body = (req.body ?? {});
|
|
const fallback = buildFallbackRecord();
|
|
const record = {
|
|
schema_version: "shared_llm_connection_v1",
|
|
updated_at: new Date().toISOString(),
|
|
connection: {
|
|
llmProvider: body.llmProvider === "local" ? "local" : "openai",
|
|
model: sanitizeString(body.model, fallback.connection.model),
|
|
baseUrl: sanitizeString(body.baseUrl, fallback.connection.baseUrl),
|
|
temperature: sanitizeNumber(body.temperature, fallback.connection.temperature),
|
|
maxOutputTokens: Math.max(1, Math.trunc(sanitizeNumber(body.maxOutputTokens, fallback.connection.maxOutputTokens)))
|
|
}
|
|
};
|
|
(0, files_1.ensureDir)(path_1.default.dirname(config_1.SHARED_LLM_CONNECTION_FILE));
|
|
(0, files_1.writeJsonFile)(config_1.SHARED_LLM_CONNECTION_FILE, record);
|
|
(0, http_1.ok)(res, {
|
|
ok: true,
|
|
connection: record.connection,
|
|
updated_at: record.updated_at
|
|
});
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
return router;
|
|
}
|