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

import { ClassifierByBot } from './typings'

export const registerMiddleware = async (bp: typeof sdk, classifiers: ClassifierByBot) => {
  bp.events.registerMiddleware({
    name: 'knowledge.incoming',
    direction: 'incoming',
    handler: async (event, next) => {
      if (event.type !== 'text' || !event.preview) {
        next()
      }

      const results = await classifiers[event.botId].predict(event.preview)

      for (const result of results.filter(r => r.confidence > 0.5)) {
        const { content, page, paragraph, name } = result
        const details = `I found this: \n_"${content}"_ \nin ${name} on page ${page}, par. ${paragraph}`

        event.suggestedReplies.push({
          confidence: result.confidence,
          payloads: [{ type: 'text', text: details, markdown: true }],
          intent: 'kdb' // TODO: remove intent requirement in decision engine, add source
        })
      }

      next()<commit_msg>added minimum length for knowledge queries<commit_after>import * as sdk from 'botpress/sdk'

import { ClassifierByBot } from './typings'

export const registerMiddleware = async (bp: typeof sdk, classifiers: ClassifierByBot) => {
  const MIN_TEXT_LENGTH = 10

  bp.events.registerMiddleware({
    name: 'knowledge.incoming',
    direction: 'incoming',
    handler: async (event, next) => {
      if (event.type !== 'text' || !event.preview || event.preview.length < MIN_TEXT_LENGTH) {
        return next()
      }

      const results = await classifiers[event.botId].predict(event.preview)

      for (const result of results.filter(r => r.confidence > 0.5)) {
        const { content, page, paragraph, name } = result
        const details = `I found this: \n_"${content}"_ \nin ${name} on page ${page}, par. ${paragraph}`

        event.suggestedReplies.push({
          confidence: result.confidence,
          payloads: [{ type: 'text', text: details, markdown: true }],
          intent: 'kdb' // TODO: remove intent requirement in decision engine, add source
        })
      }

      next()
{'fraction_non_alphanumeric': 0.1339712918660287, 'fraction_numerical': 0.0028708133971291866, 'mean_word_length': 2.930451127819549, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, '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': '4544327', 'n_tokens_mistral': 618, 'n_tokens_neox': 596, 'n_words': 178}