langflow-chatbot
Version:
Add a Langflow-powered chatbot to your website.
94 lines (93 loc) • 4.51 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadBaseConfig = loadBaseConfig;
exports.loadInstanceConfig = loadInstanceConfig;
/**
* @file config-loader.ts
* @description This file is responsible for loading base and instance-specific configurations
* from YAML files during the application startup. It parses these files to provide
* essential parameters like Langflow connection details and chatbot profiles.
* This module is intended to be used early in the application lifecycle, and the
* configurations it loads are generally static thereafter.
*/
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const uiConstants_1 = require("../../config/uiConstants");
// New private helper function to retrieve environment variables
function getEnvVariable(variableName) {
return process.env[variableName];
}
function loadBaseConfig() {
console.log("ConfigLoader: Loading base configuration from environment variables and UI constants.");
const envEndpointUrl = getEnvVariable('LANGFLOW_ENDPOINT_URL');
const envApiKey = getEnvVariable('LANGFLOW_API_KEY');
if (!envEndpointUrl) {
throw new Error("Langflow endpoint URL is not defined in environment variable LANGFLOW_ENDPOINT_URL.");
}
console.log(`ConfigLoader: Using LANGFLOW_ENDPOINT_URL from environment: ${envEndpointUrl}`);
let api_key;
if (envApiKey) {
console.log("ConfigLoader: Using LANGFLOW_API_KEY from environment.");
api_key = envApiKey;
} // api_key remains undefined if not in env, which is acceptable.
const serverDefaults = {
enableStream: uiConstants_1.DEFAULT_ENABLE_STREAM,
datetimeFormat: uiConstants_1.DEFAULT_DATETIME_FORMAT,
// flowId is mandatory and instance-specific, so no default here.
};
const chatbotDefaults = {
floatingWidget: {
useFloating: uiConstants_1.DEFAULT_USE_FLOATING,
floatPosition: uiConstants_1.DEFAULT_FLOAT_POSITION,
},
labels: {
widgetTitle: uiConstants_1.DEFAULT_WIDGET_TITLE,
userSender: uiConstants_1.DEFAULT_USER_SENDER,
botSender: uiConstants_1.DEFAULT_BOT_SENDER,
errorSender: uiConstants_1.DEFAULT_ERROR_SENDER,
systemSender: uiConstants_1.DEFAULT_SYSTEM_SENDER,
},
template: {
mainContainerTemplate: uiConstants_1.DEFAULT_MAIN_CONTAINER_TEMPLATE,
inputAreaTemplate: uiConstants_1.DEFAULT_INPUT_AREA_TEMPLATE,
messageTemplate: uiConstants_1.DEFAULT_MESSAGE_TEMPLATE,
},
};
return {
langflowConnection: { endpoint_url: envEndpointUrl, api_key },
serverDefaults: serverDefaults,
chatbotDefaults: chatbotDefaults,
};
}
function loadInstanceConfig(instanceConfigPath) {
const absolutePath = path_1.default.resolve(instanceConfigPath);
console.log(`ConfigLoader: Loading instance-specific chatbot profiles from: ${absolutePath}`);
if (!fs_1.default.existsSync(absolutePath)) {
throw new Error(`Instance configuration file (YAML) not found at ${absolutePath}.`);
}
const fileContents = fs_1.default.readFileSync(absolutePath, 'utf-8');
const parsedConfig = js_yaml_1.default.load(fileContents);
if (!parsedConfig.profiles || !Array.isArray(parsedConfig.profiles)) {
throw new Error(`Instance YAML config missing required 'profiles' array. Path: ${absolutePath}`);
}
// Validate and structure each profile
return parsedConfig.profiles.map((p, index) => {
if (!p.profileId || !p.server?.flowId) {
throw new Error(`ConfigLoader: Profile at index ${index} is missing required 'profileId' or 'server.flowId'. Path: ${absolutePath}`);
}
const completeProfile = {
profileId: p.profileId,
server: {
flowId: p.server.flowId,
enableStream: p.server.enableStream, // Will be undefined if not present, handled by defaults later
datetimeFormat: p.server.datetimeFormat, // Will be undefined if not present
},
chatbot: p.chatbot || {}, // Ensure chatbot object exists, even if empty
}; // Type assertion
return completeProfile;
});
}