<commit_before>import { Logger } from 'botpress/sdk'
import HTTPServer from 'core/server'
import AuthService, { TOKEN_AUDIENCE } from 'core/services/auth/auth-service'
import { ConverseService } from 'core/services/converse'
import { RequestHandler, Router } from 'express'
import joi from 'joi'
import _ from 'lodash'

import { CustomRouter } from '../customRouter'
import { checkTokenHeader } from '../util'

// this schema ensures a non breaking api signature (> 11.5)
// see https://botpress.io/docs/build/channels/#usage-public-api
const conversePayloadSchema = {
  type: joi.string().valid('text'), // add other types as we need
  text: joi.string().required(),
  includedContexts: joi
    .array()
    .items(joi.string())
    .optional()
    .default(['global'])
}

export class ConverseRouter extends CustomRouter {
  private checkTokenHeader!: RequestHandler

  constructor(
    logger: Logger,
    private converseService: ConverseService,
    private authService: AuthService,
    private httpServer: HTTPServer
  ) {
    super('Converse', logger, Router({ mergeParams: true }))
    this.checkTokenHeader = checkTokenHeader(this.authService, TOKEN_AUDIENCE)
    this.setupRoutes()
  }

  private async validatePayload(req, res, next) {
    try {
      await joi.validate(req.body, conversePayloadSchema)
      next()
    } catch (err) {
      res.status(400).send(err.message)
    }
  }

  setupRoutes() {
    this.router.post(
      '/:userId',
      this.httpServer.extractExternalToken,
      this.validatePayload,
      this.asyncMiddleware(async (req, res) => {
        const { userId, botId } = req.params
        const params = req.query.include

        if (params && params.toLowerCase() !== 'responses') {
          return res.status(401).send("Unauthenticated converse API can only return 'responses'")
        }

        const rawOutput = await this.converseService.sendMessage(
          botId,
          userId,
          _.omit(req.body, ['includedContexts']),
          req.credentials,
          req.body.includedContexts || ['global']
        )
        const formatedOutput = this.prepareResponse(rawOutput, params)

        return res.json(formatedOutput)
      })
    )

    this.router.post(
      '/:userId/secured',
      this.checkTokenHeader,
      this.httpServer.extractExternalToken,
      this.asyncMiddleware(async (req, res) => {
        const { userId, botId } = req.params

        const rawOutput = await this.converseService.sendMessage(
          botId,
          userId,
          _.omit(req.body, ['includedContexts']),
          req.credentials,
          req.body.includedContexts || ['global']
        )
        const formatedOutput = this.prepareResponse(rawOutput, req.query.include)

        return res.json(formatedOutput)
      })
    )
  }
<commit_msg>chore(converse): added missing comments<commit_after>import { Logger } from 'botpress/sdk'
import HTTPServer from 'core/server'
import AuthService, { TOKEN_AUDIENCE } from 'core/services/auth/auth-service'
import { ConverseService } from 'core/services/converse'
import { RequestHandler, Router } from 'express'
import joi from 'joi'
import _ from 'lodash'

import { CustomRouter } from '../customRouter'
import { checkTokenHeader } from '../util'

// this schema ensures a non breaking api signature (> 11.5)
// see https://botpress.io/docs/build/channels/#usage-public-api
const conversePayloadSchema = {
  type: joi.string().valid('text'), // add other types as we need
  text: joi.string().required(),
  includedContexts: joi
    .array()
    .items(joi.string())
    .optional()
    .default(['global'])
}

export class ConverseRouter extends CustomRouter {
  private checkTokenHeader!: RequestHandler

  constructor(
    logger: Logger,
    private converseService: ConverseService,
    private authService: AuthService,
    private httpServer: HTTPServer
  ) {
    super('Converse', logger, Router({ mergeParams: true }))
    this.checkTokenHeader = checkTokenHeader(this.authService, TOKEN_AUDIENCE)
    this.setupRoutes()
  }

  private async validatePayload(req, res, next) {
    try {
      await joi.validate(req.body, conversePayloadSchema)
      next()
    } catch (err) {
      res.status(400).send(err.message)
    }
  }

  setupRoutes() {
    this.router.post(
      '/:userId',
      this.httpServer.extractExternalToken,
      this.validatePayload,
      this.asyncMiddleware(async (req, res) => {
        const { userId, botId } = req.params
        const params = req.query.include

        if (params && params.toLowerCase() !== 'responses') {
          return res.status(401).send("Unauthenticated converse API can only return 'responses'")
        }

        const rawOutput = await this.converseService.sendMessage(
          botId,
          userId,
          _.omit(req.body, ['includedContexts']),
          req.credentials,
          req.body.includedContexts || ['global']
        )
        const formatedOutput = this.prepareResponse(rawOutput, params)

        return res.json(formatedOutput)
      })
    )

    this.router.post(
      '/:userId/secured',
      this.checkTokenHeader,
      // Secured endpoint does not validate schema on purpose
      // DO NOT add this middleware: this.validatePayload
      // This is to validate user-created (non-trusted) payloads only
      this.httpServer.extractExternalToken,
      this.asyncMiddleware(async (req, res) => {
        const { userId, botId } = req.params

        const rawOutput = await this.converseService.sendMessage(
          botId,
          userId,
          _.omit(req.body, ['includedContexts']),
          req.credentials,
          req.body.includedContexts || ['global']
        )
        const formatedOutput = this.prepareResponse(rawOutput, req.query.include)

        return res.json(formatedOutput)
      })
    )
  }

{'fraction_non_alphanumeric': 0.11664953751284686, 'fraction_numerical': 0.003083247687564234, 'mean_word_length': 2.861772486772487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '913712', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1681, 'n_words': 385}