Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

botbuilder-wit

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

botbuilder-wit - npm Package Compare versions

Comparing version 1.0.6 to 1.1.0

lib/adapters/CacheAdapter.js

92

lib/WitRecognizer.js

@@ -12,5 +12,38 @@ "use strict";

var node_wit_1 = require("node-wit");
var crypto = require("crypto");
var RedisAdapter_1 = require("./adapters/RedisAdapter");
var MemcachedAdapter_1 = require("./adapters/MemcachedAdapter");
var CacheClients;
(function (CacheClients) {
CacheClients[CacheClients["Unknown"] = 0] = "Unknown";
CacheClients[CacheClients["Redis"] = 1] = "Redis";
CacheClients[CacheClients["Memcached"] = 2] = "Memcached";
})(CacheClients || (CacheClients = {}));
var WitRecognizer = (function () {
function WitRecognizer(accessToken) {
function WitRecognizer(accessToken, options) {
if (options === void 0) { options = {}; }
this.cacheAdapter = null;
var cache = options.cache;
if (!accessToken || typeof accessToken !== 'string') {
throw new Error('Invalid argument. Constructor must be invoked with an accessToken of type "string".');
}
this.witClient = new node_wit_1.Wit({ accessToken: accessToken });
if (cache) {
var expire = typeof options.expire === 'number' ? options.expire : 3 * 3600;
var clientType = this.getClientType(cache);
if (clientType !== CacheClients.Unknown) {
var _message = this.witClient.message;
this.witClient.message = this.witDecorator(_message);
}
switch (clientType) {
case CacheClients.Redis:
this.cacheAdapter = new RedisAdapter_1.default(cache, expire);
break;
case CacheClients.Memcached:
this.cacheAdapter = new MemcachedAdapter_1.default(cache, expire);
break;
default:
throw new Error("Invalid cache client. View the module's README.md for more details => https://github.com/sebsylvester/botbuilder-wit/blob/master/README.md");
}
}
}

@@ -72,2 +105,59 @@ WitRecognizer.prototype.recognize = function (context, done) {

};
WitRecognizer.prototype.getClientType = function (client) {
var clientType = CacheClients.Unknown;
if (typeof client === 'object' && client.constructor) {
switch (client.constructor.name) {
case 'RedisClient':
clientType = CacheClients.Redis;
break;
case 'Client':
clientType = CacheClients.Memcached;
}
}
return clientType;
};
WitRecognizer.prototype.witDecorator = function (message) {
var _this = this;
return function (utterance) {
return new Promise(function (resolve, reject) {
var hash = crypto.createHash('sha256');
hash.update(utterance);
var key = hash.digest('hex');
_this.cacheAdapter.get(key, function (error, result) {
if (error) {
console.error(error);
return resolve(null);
}
try {
resolve(result ? JSON.parse(result) : null);
}
catch (error) {
resolve(null);
}
});
}).then(function (result) {
if (result) {
return Promise.resolve(result);
}
else {
var witPromise = message(utterance);
witPromise.then(function (result) {
if (!result.error) {
var hash = crypto.createHash('sha256');
hash.update(utterance);
var key = hash.digest('hex');
var value = JSON.stringify(result);
_this.cacheAdapter.set(key, value, function (error, result) {
if (error)
console.error(error);
});
}
}).catch(function (error) {
console.error(error);
});
return witPromise;
}
});
};
};
return WitRecognizer;

@@ -74,0 +164,0 @@ }());

10

package.json
{
"name": "botbuilder-wit",
"version": "1.0.6",
"description": "Provides Wit.ai NLP integration for the Microsoft Bot Builder SDK",
"version": "1.1.0",
"description": "Provides Wit.ai NLP integration for the Microsoft Bot Builder SDK. Supports caching with Redis and Memcached.",
"main": "lib/WitRecognizer.js",
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"pretest": "tsc test/*.ts --module commonjs --sourcemap",
"test": "nyc mocha",
"pretest:watch": "tsc test/*.ts --module commonjs --sourcemap --watch",
"test": "nyc --reporter=lcov mocha",
"posttest": "nyc report --reporter=json && codecov -f coverage/*.json"

@@ -41,2 +43,2 @@ },

}
}
}

@@ -7,3 +7,3 @@ # botbuilder-wit

Node.js module that provides [Wit.ai](https://wit.ai) NLP integration for the [Microsoft Bot Builder SDK](https://dev.botframework.com/).
Node.js module that provides [Wit.ai](https://wit.ai) NLP integration for the [Microsoft Bot Builder SDK](https://dev.botframework.com/), with built-in support for caching with Redis and Memcached.

@@ -14,3 +14,3 @@ ## Installation

## Usage
## General Usage
This package does **not** work with Wit.ai's *Story* feature. It was designed to be used in conjunction with the IntentDialog class.

@@ -29,2 +29,19 @@ ```

## Enable Response Caching
The response object from Wit.ai can easily be cached with Redis or Memcached.
```
// Create a Redis client...
const redis = require('redis');
const redisClient = redis.createClient(options);
// Or Memcached client
const Memcached = require('memcached');
const memcached = new Memcached('hostname:11211');
// Configure the recognizer to use the client
// Set an optional key expire duration in seconds, defaults to 3 hours
const recognizer = new WitRecognizer('Wit.ai_access_token', { cache: redisClient, expire: 3600 });
```
## Using Entities

@@ -31,0 +48,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc