(req: Request, isInternal: boolean = false, chatType?: ChatType)
| 988 | * @param {boolean} isInternal |
| 989 | */ |
| 990 | export const utilBuildChatflow = async (req: Request, isInternal: boolean = false, chatType?: ChatType): Promise<any> => { |
| 991 | const appServer = getRunningExpressApp() |
| 992 | |
| 993 | const chatflowid = req.params.id |
| 994 | |
| 995 | // Check if chatflow exists |
| 996 | const chatflow = await appServer.AppDataSource.getRepository(ChatFlow).findOneBy({ |
| 997 | id: chatflowid |
| 998 | }) |
| 999 | if (!chatflow) { |
| 1000 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Chatflow ${chatflowid} not found`) |
| 1001 | } |
| 1002 | |
| 1003 | const isAgentFlow = chatflow.type === 'MULTIAGENT' |
| 1004 | const httpProtocol = req.get('x-forwarded-proto') || req.protocol |
| 1005 | const baseURL = `${httpProtocol}://${req.get('host')}` |
| 1006 | const incomingInput: IncomingInput = req.body || {} // Ensure incomingInput is never undefined |
| 1007 | const chatId = incomingInput.chatId ?? incomingInput.overrideConfig?.sessionId ?? uuidv4() |
| 1008 | const files = (req.files as Express.Multer.File[]) || [] |
| 1009 | const abortControllerId = `${chatflow.id}_${chatId}` |
| 1010 | const isTool = req.get('flowise-tool') === 'true' |
| 1011 | const isEvaluation: boolean = req.headers['X-Flowise-Evaluation'] || req.body.evaluation |
| 1012 | let evaluationRunId = '' |
| 1013 | evaluationRunId = req.body.evaluationRunId |
| 1014 | if (isEvaluation && chatflow.type !== 'AGENTFLOW' && req.body.evaluationRunId) { |
| 1015 | // this is needed for the collection of token metrics for non-agent flows, |
| 1016 | // for agentflows the execution trace has the info needed |
| 1017 | const newEval = { |
| 1018 | evaluation: { |
| 1019 | status: true, |
| 1020 | evaluationRunId |
| 1021 | } |
| 1022 | } |
| 1023 | chatflow.analytic = JSON.stringify(newEval) |
| 1024 | } |
| 1025 | |
| 1026 | let organizationId = '' |
| 1027 | |
| 1028 | try { |
| 1029 | // Validate API Key if its external API request |
| 1030 | if (!isInternal) { |
| 1031 | const isKeyValidated = await validateFlowAPIKey(req, chatflow) |
| 1032 | if (!isKeyValidated) { |
| 1033 | throw new InternalFlowiseError(StatusCodes.UNAUTHORIZED, `Unauthorized`) |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | // This can be public API, so we can only get orgId from the chatflow |
| 1038 | const chatflowWorkspaceId = chatflow.workspaceId |
| 1039 | const workspace = await appServer.AppDataSource.getRepository(Workspace).findOneBy({ |
| 1040 | id: chatflowWorkspaceId |
| 1041 | }) |
| 1042 | if (!workspace) { |
| 1043 | throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Workspace ${chatflowWorkspaceId} not found`) |
| 1044 | } |
| 1045 | const workspaceId = workspace.id |
| 1046 | |
| 1047 | const org = await appServer.AppDataSource.getRepository(Organization).findOneBy({ |
no test coverage detected