Skip to content

Wolf Middleware

Hao Luo edited this page Aug 10, 2018 · 3 revisions

Introduction

Wolf Middleware facilitates the passing of the wolfStore (which has the wolfState) from one stage to the next. It also provides some helper functions for you to easily get the messages from the final wolf stage.

To Use

To utilize the wolfMiddleware, you'd need pass it to the botAdapter like so

const {ConversationState, Promiseable, TurnContext} from 'botbuilder'
const {wolfMiddleware} from 'botbuilder-wolf'

// ... bot boilerplate code

adapter.use(
  ...wolfMiddleware(
    // Wolf Middleware Parameters here.. (see more detail below)
  ) 
)

Note: the wolfMiddleware function actually creates an array of bot middlewares, so you'd need to spread the array across the use parameters using the spread operator ...

Wolf Middleware Parameters

Here is the wolfMiddleware code above with the parameters and their associated types.

const {ConversationState, Promiseable, TurnContext} from 'botbuilder'
const {wolfMiddleware} from 'botbuilder-wolf'

// ... bot boilerplate code

adapter.use(
  ...wolfMiddleware(
    conversationState: ConversationState,
    userMessageDataFunc: (context: TurnContext) => Promiseable<NlpResult>,
    getAbilitiesFunc: (context: TurnContext) => Promiseable<Ability[]>,
    defaultAbility: string,
    storeCreator: (wolfStateFromConvoState: {[key: string]: any} | null) => Store<WolfState>,
    getSlotDataFunc?: (context: TurnContext) => Promiseable<IncomingSlotData[]>
  ) 
)

conversationState

  • Type: ConversationState

This is the conversationState that you create on the bot level, such as

  const conversationState = new ConversationState(new MemoryStorage())

userMessageDataFunc

  • Type: Function (can be async)
    • Parameters: context
    • Return: NlpResult

This is a function where you are given the context of the bot, and on every turn you'd need to output an NlpResult for Wolf to process. Please refer to Types to see what NlpResult looks like.

getAbilitiesFunc

  • Type: Function (can be async)
    • Parameters: context
    • Return: Ability[] This is a function that gets the abilities definition on every turn. the return resu

defaultAbility

  • Type: string This is the default ability if the nlp does not find an intent and there is no focused ability (in an active conversation)

storeCreator

  • Type: Function
    • Parameters: WolfState - wolfState from the conversation state (from the previous turn)
    • Return: Store

Wolf lets you configure how to create the WolfStore. Wolf uses Redux underneath, which means you can use powerful tools like Redux Devtools to inspect exactly what is going on with the bot.

botbuilder-wolf has a createWolfStore which you pass in the redux middleware, and compose function.

In most case, you just call the createWolfStore function and leave the parameters empty to create the default storeCreator.

See the alarmBot in the examples directory for how to enable the redux devtools.

getSlotDataFunc

  • Type: Function (can be async)
    • Parameters: context
    • Return: IncomingSlotData

On every turn, wolf will try to see if there is any external slot data that it should set, as well as take in the user message.

Clone this wiki locally