Dialog engine is executing before nlu-extras incoming handler finishes its job

Hi

I am using nlu-extras module and I have modified its incoming handler which breaks the sentence into small phrases and retrieve intents from google’s dialog flow for each phrase. But before finishing all the phrases, Dialog engine is running. I confirmed it by adding logs in the nlu-extras middleware handler.
For small sentences/queries, its working fine.

Here is the code sample of incoming handler:-

export const registerMiddleware = async (bp: typeof sdk) => {

  bp.events.registerMiddleware({

    name: 'nlu-extras.incoming',

    direction: 'incoming',

    order: 129,

    description:

      'Process natural language in the form of text. Structured data with an action and parameters for that action is injected in the incoming message event.',

    handler: async (event: sdk.IO.IncomingEvent, next: sdk.IO.MiddlewareNextCallback) => {

      const config = configPerBot[event.botId]

      const enabled = config && config.dialogflow && config.dialogflow.enabled

      if (!enabled || !event.preview || event.hasFlag(bp.IO.WellKnownFlags.SKIP_NATIVE_NLU)) {

        return next()

      }

      try {

        // Divide the text into multiple sentences 

        const texts = getTexts(event.preview) // returns an array of phrases

        const textResults = [] // A

        for (const text of texts) {

          bp.logger.info(`dialogflow.ts text ${text}`)

          const nluResult = await predict(config.dialogflow, text, event.nlu.language, event.nlu.includedContexts)

          bp.logger.info(`dialogflow.ts: predict: result: ${JSON.stringify(nluResult, undefined, 3)}`)

          textResults.push({

            text,

            nluResult

          })

        }
        // IGNORE CODE BELOW , For now just getting the first intent match
        const result = textResults[0].nluResult

        if (config.primary === 'dialogflow-nlu') {

          bp.logger.info(`dialogflow.ts: predict: intent chosen ${JSON.stringify(result, undefined, 3)}`)

          event.nlu['engine'] = 'dialogflow'

          event.nlu['botpress-nlu'] = _.cloneDeep(event.nlu)

          event.nlu.intent.name = result.intent

          event.nlu.intent.confidence = result.confidence

          // Object.assign(event.nlu, { intents: [event.nlu.intent] })

          Object.assign(event.nlu, { intents: textResults.map(result => result.nluResult.intent) })

          Object.keys(event.nlu.slots).forEach((key) => { delete event.nlu.slots[key] })

          Object.assign(event.nlu.slots, result.slots)

          // Set Entities in the NLU

          ///// ********

          event.nlu.entities.splice(0, event.nlu.entities.length)

          const bpfs = bp.ghost.forBot(event.botId)

          const dataset = await getDataset(bpfs)

          const entities = dataset.entities

          const intent = dataset.intents.find(it => it.name.toLowerCase() === result.intent.toLowerCase())

          if (intent) {

            const slotsName = Object.keys(event.nlu.slots)

            intent.slots.map(slot => {

              if (slotsName.includes(slot.name)) {

                const entityName = `${slot && slot.entities && slot.entities.length ? entities.some(y => y.name === slot.entities[0]) ? slot.entities[0] : `${slot.entities[0]}` : 'any'}` // '@sys.any' // TODO

                event.nlu.entities.push({

                  name: entityName,

                  data: {

                    value: event.nlu.slots[slot.name],

                    extras: {},

                    unit: '' // TODO: Fill All Values Accordingly

                  },

                  meta: {

                    confidence: 1,

                    provider: '',

                    source: '',

                    start: 0,

                    end: 0,

                    raw: event.preview,

                    sensitive: false

                  },

                  type: entityName

                })

              }

            })

          }

          // }

        } else {

          event.nlu['engine'] = 'botpress'

          event.nlu['dialogflow-nlu'] = { ...result, engine: 'dialogflow' }

        }

      } catch (err) {

        bp.logger.warn(`Error extracting NLU from Dialogflow: ${err.message}`)

      } finally {

        next()

      }

    }

  })

}

Is there any time limit allotted to a handler?