Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Generator<Data>

The conversation generator can walk a formation tree and yield chat bot messages. It provides an event emitter interface to easily hook into the walking of the tree.

The events are generated in the following lifecycle of a formation:

     +------+
+----> pump <-----------------------+
|    +--+---+                       |
|       |                           |
|    ...v....                       |
|    | walk |                       |
|    '''+''''                       |
|       |                           |
|    +--v---+                       |
|    | step +---+                   |
|    +--+---+   |                   |
|       |       |                   |
|       |   +---v--+                |
|       | +-+ jump |                |
|       | | +------+                |
|       | |                         |
|   +---v-v---+                     |
|   | process +---+                 |
|   +----+-+--+   |                 |
|        |        |                 |
|        |   +----v----+            |
|        | +-+ feature |            |
|        | | +---------+            |
|        | |                        |
|  +-----v-v---+                    |
|  | statement +-+                  |
|  +-----+-----+ |                  |
|        |       |                  |
|    ....v....   |                  |
+----+ yield |   |                  |
     ''''+''''   |                  |
         |       |                  |
         |  +----v-----+            |
         |  | expected |            |
         |  +----+-----+            |
         |       |                  |
         |    +--v---+              |
         |    | hint +-----+        |
         |    +--+---+     |        |
         |       |         |        |
         |       |   +-----v------+ |
         |       | +-+ suggestion | |
         |       | | +------------+ |
         |       | |                |
         |    +--v-v--+             |
         |    | input |             |
         |    +---+---+             |
         |        |                 |
         |    ....v....             |
         |    + yield |             |
         |    '''''''''             |
         |        |                 |
         |   +----v-----+           |
         |   | classify +-----------+
         |   +----+-----+
         |        |
     +---v--+     |
     | done <-----+
     +------+

The events, surrounded in boxes are:

  • pump - the iterator has been pumped but the underlying formation walker has not yet updated all the properties
         on the generator such as `id`, `link`, etc. <a href="../interfaces/_lib_event_payload_ipump_.ieventpayloadpumpimmutable.html">IEventPayloadPumpImmutable</a>
  • step - the underlying walker has moved to the next formation, the properties on the generator will have
         updated such as `id`, `link`, etc. <a href="../interfaces/_lib_event_payload_istep_.ieventpayloadstepimmutable.html">IEventPayloadStepImmutable</a>
  • jump - if the generator has had the jump property set, this event will be raised. The jump property allows
         the generator to ignore the formation link ID and move to the specified jump ID.
         <a href="../interfaces/_lib_event_payload_ijump_.ieventpayloadjumpimmutable.html">IEventPayloadJumpImmutable</a>
  • process - when a formation discourse is processed. IEventPayloadProcessImmutable
  • feature - the formation has attached features in the metadata. The event will be emitted for each feature and
            the corresponding feature metadata will be provided. <a href="../interfaces/_lib_event_payload_ifeature_.ieventpayloadfeatureimmutable.html">IEventPayloadFeatureImmutable</a>
  • statement - the formation discourse is a IDiscourseStatement. Statements are yielded as a non-interactive
              chat message. <a href="../interfaces/_lib_event_payload_istatement_.ieventpayloadstatementimmutable.html">IEventPayloadStatementImmutable</a>
  • expected - an expected discourse which creates an interactive experience with the user. The user will be
             prompted for a response. <a href="../interfaces/_lib_event_payload_iexpected_.ieventpayloadexpectedimmutable.html">IEventPayloadExpectedImmutable</a>
  • hint - the response required for an expected formation can hint at when type of input is required.
             <a href="../interfaces/_lib_event_payload_ihint_.ieventpayloadhintimmutable.html">IEventPayloadHintImmutable</a>
  • suggestions - the expected formation can provide a set of suggested inputs to avoid the user having to type a
                response. If there are no suggestions provided the event will not raise.
                <a href="../interfaces/_lib_event_payload_isuggestions_.ieventpayloadsuggestionsimmutable.html">IEventPayloadSuggestionsImmutable</a>
  • input - the user has provide a response to the expected discourse. IEventPayloadInputImmutable
  • classify - The input has been classified against the expected answers. IEventPayloadExpectedImmutable
  • done - the generator has completed walking the formation. IEventPayloadDoneImmutable

Type parameters

  • Data

Hierarchy

Implements

Index

Constructors

constructor

Properties

Protected context

context: IContextMutable

Protected done

done: boolean = false

Protected emitter

emitter: IEventEmitterExtended<IEventRecord<Data>>

jump

jump: IGuidImmutable | undefined = undefined

Can be set to jump the iteration to a formation GUID on the next iterator pump

locale

locale: ILocale

Accessors

cache

  • get cache(): ICacheMutable<Data>
  • set cache(value: ICacheMutable<Data>): void
  • Retrieves cache that is being used to retrieve the messages

    Returns ICacheMutable<Data>

  • Sets cache that is being used to retrieve the messages

    Parameters

    • value: ICacheMutable<Data>

    Returns void

ctx

  • get ctx(): IContextImmutable

discourse

  • get discourse(): IDiscourseImmutable<Data> | undefined
  • The current formation discourse.

    Returns IDiscourseImmutable<Data> | undefined

features

  • get features(): ReadonlySet<string>
  • The features of the formation. This is pulled from the metadata

    Returns ReadonlySet<string>

id

  • get id(): IGuidImmutable
  • Retrieves the ID that is currently being processed

    Returns IGuidImmutable

link

  • get link(): ILinkImmutable | undefined
  • The current formation link. This can be used to determine what the next formation will be. It is highly likely you will need to use redirect as well

    Returns ILinkImmutable | undefined

    undefined when the current formation is not known

meta

  • get meta(): IFormationMetaImmutable
  • The formation metadata. This can be used to determine extra information about the formation. Of particular note are the features.

    Returns IFormationMetaImmutable

options

  • get options(): ILinkProcessOptions
  • set options(value: ILinkProcessOptions): void
  • Retrieves options to use when processing the links

    Returns ILinkProcessOptions

  • Set options to use when processing the links

    Parameters

    • value: ILinkProcessOptions

    Returns void

redirect

  • get redirect(): IGuidImmutable | undefined
  • This is the current redirection state of the walker. This is used to ignore the link target and go straight to this GUID. This can be used with link to determine which formation will be walked to.

    Returns IGuidImmutable | undefined

tags

  • get tags(): ReadonlyArray<string>
  • The formation tags. Can be used to determine formation metadata

    Returns ReadonlyArray<string>

Methods

__@asyncIterator

Protected classify

  • classify(response: IInputImmutable, targets: TargetsMutableImmutable): IClassificationIterator
  • Performs classification of a users response.

    example
    Searching for a match ```typescript import { isITargetMeaning, isITargetIntent } from '@ef-carbon/classification'; for await (const { target, confidence } of this.classifiy(input, targets)) { if (confidence > 0.7) { if (isITargetMeaning(target)) { console.log(`Users input matched the '${target.meaning}' meaning`) } else if (isITargetIntent(target)) { console.log(`Users intent is '${target.intent}'`) } } } ```

    Parameters

    • response: IInputImmutable

      the input that the user provided

    • targets: TargetsMutableImmutable

      the classification targets to match the user input against

    Returns IClassificationIterator

    an iterator that returns a stream of target matches with a confidence level

Protected Optional convertSuggestion

  • convertSuggestion(suggestion: Data): IInputImmutable
  • Override this to provide conversion of non-string data types into inputs. This will populate the suggestions

    Parameters

    • suggestion: Data

      the data that is provided as a suggested input

    Returns IInputImmutable

    a constructed input interface

dispose

  • dispose(): this

Protected expected

  • expected(discourse: IDiscourseExpectedImmutable<Data>): IChatBotIterator<Data>
  • Derived classes can implement this function to process expected discourse interactions. Usually this will keep yielding a message until the expected answer is given. This base implementation is really dumb:

    • It doesn't do anything with the suggested answers from the discourse as they need to be converted from Data into IInputImmutable and there is no need to require everybody to implement that interface. It does provide the answer when Data is a string, though, to help out the demo application.
    • It will not loop until a certain confidence has been reached that the input is an expected answer. It does not yield any extra, helpful, messages explaining what is going on. Implementations highly likely want to provide a more friendly user experience whilst also respecting this.locale.
    • It updates the context.response with the results of the classification using the standard classifySet

    Parameters

    • discourse: IDiscourseExpectedImmutable<Data>

      an interaction with the user that requires an expected response

    Returns IChatBotIterator<Data>

    yields chat bot message data

Protected isDone

  • isDone(): Promise<boolean>

iterator

  • iterator(options?: IParameters): IAsyncIterableIterator<Data>
  • Parameters

    • Optional options: IParameters

    Returns IAsyncIterableIterator<Data>

Protected mapSuggestions

  • mapSuggestions(suggestions: ReadonlyArray<Data>, convertor: function | undefined): Array<IInputImmutable> | undefined
  • Maps the suggestions from the data type to the canonical user input classes. Can be used as a hook to modify the returned suggestions from the formation

    Parameters

    • suggestions: ReadonlyArray<Data>

      the suggestions provided by the formation

    • convertor: function | undefined

      the conversion function to use to convert the suggestions. Will usually be this.convertSuggestion or but can be built in conversion functions if Data is a known type

    Returns Array<IInputImmutable> | undefined

    the mapped suggestions to the input interfaces

off

on

once

Protected process

  • process(discourse: IDiscourseImmutable<Data>): IChatBotIterator<Data>
  • Performs the processing of a conversation discourse piece

    Parameters

    • discourse: IDiscourseImmutable<Data>

    Returns IChatBotIterator<Data>

Protected statement

  • statement(discourse: IDiscourseStatementImmutable<Data>): IChatBotIterator<Data>
  • Derived classes can implement this function to process statements. Usually this just forwards on the conversation discourse statement, verbatim. The processing function can update the local conversation context at this.context

    protected async *statement(discourse: IDiscourseStatementImmutable<Data>): IAsyncIterableIteratorStatement<Data> {
      this.context.statements = this.context.hasOwnProperty('statements') ? ++this.context.statements : 1;
      yield { data: discourse.statement };
    }

    Parameters

    • discourse: IDiscourseStatementImmutable<Data>

      a statement to show to the user

    Returns IChatBotIterator<Data>

    yields chat bot message data