npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mouthpiece

v0.3.0

Published

A conversation process flow for intelligent agents

Downloads

8

Readme

mouthpiece

A conversation process flow for intelligent agents.

Build Status

Setup

NPM

Install via npm:

npm install mouthpiece --save

Usage

Import and initialize the conversation with a config:

var config = {
  prepare: [doPrepare],
  converser: converser,
  actions: {
    anAction: anAction,
  }
};

var converse = require('mouthpiece/converse')(config);

converse('users utterance', context, function(err, result){
  if(err) {
    // handle it
  }
});

The config is used to setup the process flow as follows:

  • config.prepare - An Array of operations to run before calling the converser, optional.
  • config.converser - The main converser function that handles the agents dialog.
  • config.actions - An action map used to carry out actions in the converser output, optional.

Prepare operation signature:

function doPrepare(utterance, context, cb) {
  console.log('Carring out preparatory processing');
  process.nextTick(() =>
    cb(null, utterance, context); 
  );
}

Action operation signature:

function anAction(verb, conversation_result, cb) {
  console.log('Carrying out action ' + verb);
  process.nextTick(() =>
    cb(null, conversation_result);
  );
}

Converser operation signature:

function converser(utterance, context, cb){
  console.log('Carrying out conversation');
  var converser_result = { context: context };
  process.nextTick(() =>
    cb(null, converser_result);
  );
}

Converser result

The converser on sucessful completion must return a result object with the context property set to the updated context.

This is the format of the result expected by the onward flow:

var converser_result = {
  context: {
    do: 'anAction, anotherAction',
    replay: false,
    loop: false
  }
};

The returned context can also have two special properties that direct what happens next:

  • do - This can be array of strings or a comma separated string. Each string is a key into config.actions. Actions corresponding to each key are then executed in order with this conversation result passed in.
  • loop - This boolean if set to true will cause the converser service to be called again running the flow a second time but with the new context taken from this conversation result.
  • replay - This boolean if set to true will cause the conversation flow to be re-entered running it a second time in its entiriety including any prepare operations, but with the new context taken from this conversation result.

If converser_result.utterance is set it will be used as the utterance during loop and replay, otherwise the last user utterance is used. If converser_result.utterance is not set, it is set to the last user utterance before calling any actions.

Dealing with different converser result formats

If the converser service you are using does not return a result in the expected format described above, the onward flow will break and cause errors. There are two ways to address this.

Use a wrapper function

You can wrap the non-conforming converser in a function that will take the immediate output of the converser and transform it to the form described above:

function converser_wrapper(utterance, context, cb){
  console.log('Carrying out conversation');
  converser(utterance, context, function(err, result){
    if(err) return cb(err);
    
    // If result is not in the expected format this is your opportunity to correct it.
    var formatted_result = result; 
    
    cb(null, formatted_result);
  });
}

config.converser = converser_wrapper;

This approach is suitable when the format of the result is very different from the expected one and/or an asynchronous operation is needed to transform the result into the expected format.

Provide converser.transformResult()

You can provide a transformResult() operation on your converser, which will be called before passing its result onward into the flow. Your transformResult() will have the opportunity to transform the result into the format expected by the onward flow.

converser.transformResult = function(result){
  // If result is not in the expected format this is your opportunity to correct it.
  var formatted_result = result; 
  return formatted_result;
}

config.converser = converser;

This approach is light-weight and works well when the result only needs minor tweaks to transform to the expected format and this can be done synchronously.

Running tests

Step 1: Get the Code

git clone https://github.com/wallali/mouthpiece.git

Step 2: Running Tests

npm test

Debugging

mouthpiece uses the debug module to output debug messages to the console. To output all debug messages, run your node app with the DEBUG environment variable:

DEBUG=mouthpiece:* node your-app.js

This will output debugging messages from mouthpiece.

License

Apache 2.0