<commit_before>import * as sdk from 'botpress/sdk'
import _ from 'lodash'

import api from './api'
import { QnaStorage } from './qna'
import { initBot, initModule } from './setup'

const botScopedStorage: Map<string, QnaStorage> = new Map<string, QnaStorage>()

const onServerStarted = async (bp: typeof sdk) => {
  await initModule(bp, botScopedStorage)
}

const onServerReady = async (bp: typeof sdk) => {
  await api(bp, botScopedStorage)
}

const onBotMount = async (bp: typeof sdk, botId: string) => {
  await initBot(bp, botScopedStorage, botId)
}

const onBotUnmount = async (bp: typeof sdk, botId: string) => {
  botScopedStorage.delete(botId)
}

const onFlowChanged = async (bp: typeof sdk, botId: string, newFlow: sdk.Flow) => {
  const oldFlow = await bp.ghost.forBot(botId).readFileAsObject<sdk.Flow>('./flows', newFlow.location)
  const qnaStorage = await botScopedStorage.get(botId)
  const questions = await qnaStorage.getQuestions({ question: '', categories: [] }, { limit: 0, offset: 0 })

  // Detect nodes that has their name changed
  for (const oldNode of oldFlow.nodes) {
    for (const newNode of newFlow.nodes) {
      if (oldNode.id === newNode.id && oldNode.name !== newNode.name) {
        // Detected
        // Update all questions that refer to the old node name

        const updatedItems = questions.items
          .filter(q => {
            const sameFlow = q.data.redirectFlow === newFlow.name
            const sameNode = q.data.redirectNode === oldNode.name
            return sameFlow && sameNode
          })
          .map(q => {
            q.data.redirectNode = newNode.name
            return q
          })

        for (const q of updatedItems) {
          await qnaStorage.update(q.data, q.id)
          bp.logger.debug(`References to node "${oldNode.name}" has been updated to "${newNode.name}"`)
        }
      }
    }
  }
}

const entryPoint: sdk.ModuleEntryPoint = {
  onServerStarted,
  onServerReady,
  onBotMount,<commit_msg>nit fix<commit_after>import * as sdk from 'botpress/sdk'
import _ from 'lodash'

import api from './api'
import { QnaStorage } from './qna'
import { initBot, initModule } from './setup'

const botScopedStorage: Map<string, QnaStorage> = new Map<string, QnaStorage>()

const onServerStarted = async (bp: typeof sdk) => {
  await initModule(bp, botScopedStorage)
}

const onServerReady = async (bp: typeof sdk) => {
  await api(bp, botScopedStorage)
}

const onBotMount = async (bp: typeof sdk, botId: string) => {
  await initBot(bp, botScopedStorage, botId)
}

const onBotUnmount = async (bp: typeof sdk, botId: string) => {
  botScopedStorage.delete(botId)
}

const onFlowChanged = async (bp: typeof sdk, botId: string, newFlow: sdk.Flow) => {
  const oldFlow = await bp.ghost.forBot(botId).readFileAsObject<sdk.Flow>('./flows', newFlow.location)
  const qnaStorage = await botScopedStorage.get(botId)
  const questions = await qnaStorage.getQuestions({ question: '', categories: [] }, { limit: 0, offset: 0 })

  // Detect nodes that had their name changed
  for (const oldNode of oldFlow.nodes) {
    for (const newNode of newFlow.nodes) {
      // Update all questions that refer to the old node name
      if (oldNode.id === newNode.id && oldNode.name !== newNode.name) {
        const updatedItems = questions.items
          .filter(q => q.data.redirectFlow === newFlow.name && q.data.redirectNode === oldNode.name)
          .map(q => {
            q.data.redirectNode = newNode.name
            return q
          })

        for (const item of updatedItems) {
          await qnaStorage.update(item.data, item.id)
          bp.logger.debug(`References to node "${oldNode.name}" has been updated to "${newNode.name}"`)
        }
      }
    }
  }
}

const entryPoint: sdk.ModuleEntryPoint = {
  onServerStarted,
  onServerReady,
  onBotMount,
{'fraction_non_alphanumeric': 0.12424954320020883, 'fraction_numerical': 0.0010441138084051162, 'mean_word_length': 3.102783725910064, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4018530', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1212, 'n_words': 336}