(req: Request, res: Response, next: NextFunction)
| 12 | |
| 13 | // Send input message and get prediction result (External) |
| 14 | const createPrediction = async (req: Request, res: Response, next: NextFunction) => { |
| 15 | try { |
| 16 | if (typeof req.params === 'undefined' || !req.params.id) { |
| 17 | throw new InternalFlowiseError( |
| 18 | StatusCodes.PRECONDITION_FAILED, |
| 19 | `Error: predictionsController.createPrediction - id not provided!` |
| 20 | ) |
| 21 | } |
| 22 | if (!req.body) { |
| 23 | throw new InternalFlowiseError( |
| 24 | StatusCodes.PRECONDITION_FAILED, |
| 25 | `Error: predictionsController.createPrediction - body not provided!` |
| 26 | ) |
| 27 | } |
| 28 | const workspaceId = req.user?.activeWorkspaceId |
| 29 | |
| 30 | const chatflow = await chatflowsService.getChatflowById(req.params.id, workspaceId) |
| 31 | if (!chatflow) { |
| 32 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${req.params.id} not found`) |
| 33 | } |
| 34 | let isDomainAllowed = true |
| 35 | let unauthorizedOriginError = 'This site is not allowed to access this chatbot' |
| 36 | logger.info(`[server]: Request originated from ${req.headers.origin || 'UNKNOWN ORIGIN'}`) |
| 37 | if (chatflow.chatbotConfig) { |
| 38 | const parsedConfig = JSON.parse(chatflow.chatbotConfig) |
| 39 | // check whether the first one is not empty. if it is empty that means the user set a value and then removed it. |
| 40 | const isValidAllowedOrigins = parsedConfig.allowedOrigins?.length && parsedConfig.allowedOrigins[0] !== '' |
| 41 | unauthorizedOriginError = parsedConfig.allowedOriginsError || 'This site is not allowed to access this chatbot' |
| 42 | if (isValidAllowedOrigins && req.headers.origin) { |
| 43 | const originHeader = req.headers.origin |
| 44 | const origin = new URL(originHeader).host |
| 45 | isDomainAllowed = |
| 46 | parsedConfig.allowedOrigins.filter((domain: string) => { |
| 47 | try { |
| 48 | const allowedOrigin = new URL(domain).host |
| 49 | return origin === allowedOrigin |
| 50 | } catch (e) { |
| 51 | return false |
| 52 | } |
| 53 | }).length > 0 |
| 54 | } |
| 55 | } |
| 56 | if (isDomainAllowed) { |
| 57 | const streamable = await chatflowsService.checkIfChatflowIsValidForStreaming(req.params.id) |
| 58 | const isStreamingRequested = req.body.streaming === 'true' || req.body.streaming === true |
| 59 | if (streamable?.isStreaming && isStreamingRequested) { |
| 60 | const sseStreamer = getRunningExpressApp().sseStreamer |
| 61 | |
| 62 | let chatId = req.body.chatId |
| 63 | if (!req.body.chatId) { |
| 64 | chatId = req.body.chatId ?? req.body.overrideConfig?.sessionId ?? uuidv4() |
| 65 | req.body.chatId = chatId |
| 66 | } |
| 67 | const isQueueMode = process.env.MODE === MODE.QUEUE |
| 68 | try { |
| 69 | sseStreamer.addExternalClient(chatId, res) |
| 70 | res.setHeader('Content-Type', 'text/event-stream') |
| 71 | res.setHeader('Cache-Control', 'no-cache') |
nothing calls this directly
no test coverage detected