Skip to content

Commit fc9740a

Browse files
authored
Fix for node-crashing "undefined (reading 'streaming')" error (#4104)
* check for undefined variables in buildChatflow to avoid node crashing exceptions * Add null check on error Middleware as well
1 parent da04289 commit fc9740a

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

packages/server/src/middlewares/errors/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function errorHandlerMiddleware(err: InternalFlowiseError, req: Request, r
1212
// Provide error stack trace only in development
1313
stack: process.env.NODE_ENV === 'development' ? err.stack : {}
1414
}
15-
if (!req.body.streaming || req.body.streaming === 'false') {
15+
if (!req.body || !req.body.streaming || req.body.streaming === 'false') {
1616
res.setHeader('Content-Type', 'application/json')
1717
res.status(displayedError.statusCode).json(displayedError)
1818
}

packages/server/src/utils/buildChatflow.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,18 @@ export const executeFlow = async ({
237237
files,
238238
signal
239239
}: IExecuteFlowParams) => {
240-
const question = incomingInput.question
240+
// Ensure incomingInput has all required properties with default values
241+
incomingInput = {
242+
history: [],
243+
streaming: false,
244+
...incomingInput
245+
}
246+
247+
const question = incomingInput.question || '' // Ensure question is never undefined
241248
let overrideConfig = incomingInput.overrideConfig ?? {}
242249
const uploads = incomingInput.uploads
243250
const prependMessages = incomingInput.history ?? []
244-
const streaming = incomingInput.streaming
251+
const streaming = incomingInput.streaming ?? false
245252
const userMessageDateTime = new Date()
246253
const chatflowid = chatflow.id
247254

@@ -748,13 +755,18 @@ const checkIfStreamValid = async (
748755
nodes: IReactFlowNode[],
749756
streaming: boolean | string | undefined
750757
): Promise<boolean> => {
758+
// If streaming is undefined, set to false by default
759+
if (streaming === undefined) {
760+
streaming = false
761+
}
762+
751763
// Once custom function ending node exists, flow is always unavailable to stream
752764
const isCustomFunctionEndingNode = endingNodes.some((node) => node.data?.outputs?.output === 'EndingNode')
753765
if (isCustomFunctionEndingNode) return false
754766

755767
let isStreamValid = false
756768
for (const endingNode of endingNodes) {
757-
const endingNodeData = endingNode.data
769+
const endingNodeData = endingNode.data || {} // Ensure endingNodeData is never undefined
758770

759771
const isEndingNode = endingNodeData?.outputs?.output === 'EndingNode'
760772

@@ -800,7 +812,7 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
800812
const isAgentFlow = chatflow.type === 'MULTIAGENT'
801813
const httpProtocol = req.get('x-forwarded-proto') || req.protocol
802814
const baseURL = `${httpProtocol}://${req.get('host')}`
803-
const incomingInput: IncomingInput = req.body
815+
const incomingInput: IncomingInput = req.body || {} // Ensure incomingInput is never undefined
804816
const chatId = incomingInput.chatId ?? incomingInput.overrideConfig?.sessionId ?? uuidv4()
805817
const files = (req.files as Express.Multer.File[]) || []
806818
const abortControllerId = `${chatflow.id}_${chatId}`
@@ -815,7 +827,7 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
815827
}
816828

817829
const executeData: IExecuteFlowParams = {
818-
incomingInput: req.body,
830+
incomingInput, // Use the defensively created incomingInput variable
819831
chatflow,
820832
chatId,
821833
baseURL,

0 commit comments

Comments
 (0)