2025-03-10 17:23:46 -04:00
|
|
|
const axios = require('axios');
|
2024-12-17 13:12:57 -05:00
|
|
|
const { EventSource } = require('eventsource');
|
2025-02-10 21:56:08 +01:00
|
|
|
const { Time, CacheKeys } = require('librechat-data-provider');
|
2025-04-09 18:38:48 -04:00
|
|
|
const { MCPManager, FlowStateManager } = require('librechat-mcp');
|
2023-12-14 07:49:27 -05:00
|
|
|
const logger = require('./winston');
|
|
|
|
|
2024-12-17 13:12:57 -05:00
|
|
|
global.EventSource = EventSource;
|
|
|
|
|
|
|
|
let mcpManager = null;
|
2025-02-10 21:56:08 +01:00
|
|
|
let flowManager = null;
|
2024-12-17 13:12:57 -05:00
|
|
|
|
|
|
|
/**
|
2025-04-09 18:38:48 -04:00
|
|
|
* @returns {MCPManager}
|
2024-12-17 13:12:57 -05:00
|
|
|
*/
|
2025-04-09 18:38:48 -04:00
|
|
|
function getMCPManager() {
|
2024-12-17 13:12:57 -05:00
|
|
|
if (!mcpManager) {
|
|
|
|
mcpManager = MCPManager.getInstance(logger);
|
|
|
|
}
|
|
|
|
return mcpManager;
|
|
|
|
}
|
|
|
|
|
2025-02-10 21:56:08 +01:00
|
|
|
/**
|
|
|
|
* @param {(key: string) => Keyv} getLogStores
|
2025-04-09 18:38:48 -04:00
|
|
|
* @returns {FlowStateManager}
|
2025-02-10 21:56:08 +01:00
|
|
|
*/
|
2025-04-09 18:38:48 -04:00
|
|
|
function getFlowStateManager(getLogStores) {
|
2025-02-10 21:56:08 +01:00
|
|
|
if (!flowManager) {
|
|
|
|
flowManager = new FlowStateManager(getLogStores(CacheKeys.FLOWS), {
|
|
|
|
ttl: Time.ONE_MINUTE * 3,
|
|
|
|
logger,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return flowManager;
|
|
|
|
}
|
|
|
|
|
2025-01-29 19:46:58 -05:00
|
|
|
/**
|
|
|
|
* Sends message data in Server Sent Events format.
|
|
|
|
* @param {ServerResponse} res - The server response.
|
|
|
|
* @param {{ data: string | Record<string, unknown>, event?: string }} event - The message event.
|
|
|
|
* @param {string} event.event - The type of event.
|
|
|
|
* @param {string} event.data - The message to be sent.
|
|
|
|
*/
|
|
|
|
const sendEvent = (res, event) => {
|
|
|
|
if (typeof event.data === 'string' && event.data.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.write(`event: message\ndata: ${JSON.stringify(event)}\n\n`);
|
|
|
|
};
|
|
|
|
|
2025-03-11 14:44:54 -04:00
|
|
|
/**
|
|
|
|
* Creates and configures an Axios instance with optional proxy settings.
|
|
|
|
*
|
|
|
|
* @typedef {import('axios').AxiosInstance} AxiosInstance
|
|
|
|
* @typedef {import('axios').AxiosProxyConfig} AxiosProxyConfig
|
|
|
|
*
|
|
|
|
* @returns {AxiosInstance} A configured Axios instance
|
|
|
|
* @throws {Error} If there's an issue creating the Axios instance or parsing the proxy URL
|
|
|
|
*/
|
2025-03-10 17:23:46 -04:00
|
|
|
function createAxiosInstance() {
|
|
|
|
const instance = axios.create();
|
|
|
|
|
|
|
|
if (process.env.proxy) {
|
2025-03-11 14:44:54 -04:00
|
|
|
try {
|
|
|
|
const url = new URL(process.env.proxy);
|
|
|
|
|
|
|
|
/** @type {AxiosProxyConfig} */
|
|
|
|
const proxyConfig = {
|
|
|
|
host: url.hostname.replace(/^\[|\]$/g, ''),
|
|
|
|
protocol: url.protocol.replace(':', ''),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (url.port) {
|
|
|
|
proxyConfig.port = parseInt(url.port, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.defaults.proxy = proxyConfig;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error parsing proxy URL:', error);
|
|
|
|
throw new Error(`Invalid proxy URL: ${process.env.proxy}`);
|
|
|
|
}
|
2025-03-10 17:23:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-12-14 07:49:27 -05:00
|
|
|
module.exports = {
|
|
|
|
logger,
|
2025-01-29 19:46:58 -05:00
|
|
|
sendEvent,
|
2024-12-17 13:12:57 -05:00
|
|
|
getMCPManager,
|
2025-03-10 17:23:46 -04:00
|
|
|
createAxiosInstance,
|
2025-02-10 21:56:08 +01:00
|
|
|
getFlowStateManager,
|
2023-12-14 07:49:27 -05:00
|
|
|
};
|