UNPKG

langflow-chatbot

Version:

Add a Langflow-powered chatbot to your website.

68 lines (67 loc) 3.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleRequest = handleRequest; const url_1 = require("url"); const apiPaths_1 = require("../config/apiPaths"); const request_utils_1 = require("./request-utils"); // Import moved handlers const configHandlers_1 = require("./configHandlers"); const flowsHandlers_1 = require("./langflow/flowsHandlers"); const historyHandlers_1 = require("./langflow/historyHandlers"); const chatHandlers_1 = require("./langflow/chatHandlers"); // This function will be called by LangflowProxyService, passing necessary dependencies async function handleRequest(req, res, chatbotConfigurations, langflowClient, // Can be undefined if not initialized langflowEndpointUrl, langflowApiKey, // Function to make direct API requests, passed from the main service makeDirectLangflowApiRequest, proxyApiBasePath, preParsedBody, isBodyPreParsed) { const { method, url: rawUrl } = req; if (!rawUrl) { (0, request_utils_1.sendJsonError)(res, 400, "URL is required."); return; } const base = `http://${req.headers.host || 'localhost'}`; const parsedUrl = new url_1.URL(rawUrl, base); const pathname = parsedUrl.pathname; // Path prefixes are now direct matches as base path is stripped by caller const configRequestPathPrefix = apiPaths_1.PROFILE_CONFIG_ENDPOINT_PREFIX + '/'; const chatRequestPathPrefix = apiPaths_1.PROFILE_CHAT_ENDPOINT_PREFIX + '/'; if (method === 'GET' && pathname.startsWith(configRequestPathPrefix)) { const profileId = pathname.substring(configRequestPathPrefix.length); await (0, configHandlers_1.handleGetChatbotConfigRequest)(profileId, res, chatbotConfigurations, proxyApiBasePath); } else if (pathname.startsWith(chatRequestPathPrefix)) { const remainingPath = pathname.substring(chatRequestPathPrefix.length); const parts = remainingPath.split('/').filter(p => p.length > 0); const profileId = parts[0]; if (!profileId) { (0, request_utils_1.sendJsonError)(res, 400, "profileId missing in chat URL."); return; } const profile = chatbotConfigurations.get(profileId); if (!profile) { (0, request_utils_1.sendJsonError)(res, 404, `Chatbot profile with profileId '${profileId}' not found.`); return; } const flowIdToUse = profile.server.flowId; if (method === 'POST' && parts.length === 1) { const serverAllowsStream = profile.server.enableStream !== false; // Access enableStream from profile.server, default to true await (0, chatHandlers_1.handleChatMessageRequest)(req, res, flowIdToUse, serverAllowsStream, langflowClient, preParsedBody, isBodyPreParsed); } else if (method === 'GET' && parts.length === 2 && parts[1] === 'history') { const sessionId = parsedUrl.searchParams.get('session_id'); await (0, historyHandlers_1.handleGetChatHistoryRequest)(res, flowIdToUse, sessionId, makeDirectLangflowApiRequest); } else { (0, request_utils_1.sendJsonError)(res, 404, "Chat endpoint not found or method not supported for the path."); } } else if (method === 'GET' && pathname === apiPaths_1.PROXY_FLOWS_SUFFIX) { await (0, flowsHandlers_1.handleGetFlowsRequest)(req, res, makeDirectLangflowApiRequest); } else if (method === 'GET' && pathname === apiPaths_1.PROXY_PROFILES_SUFFIX) { await (0, configHandlers_1.handleListChatbotProfilesRequest)(req, res, chatbotConfigurations); } else { (0, request_utils_1.sendJsonError)(res, 404, "Endpoint not found or method not supported."); } }