10 lines
34 KiB
JavaScript
10 lines
34 KiB
JavaScript
/**
|
|
* jibo-kb - Jibo Knowledge Base
|
|
* @version v4.3.4
|
|
* @license Copyright (c) 2017, Jibo, Inc. All rights reserved.
|
|
* All use of the Jibo SDK is subject to the Jibo SDK End User License Agreement (EULA)
|
|
* distributed herewith. If you did not receive a copy of the EULA, you may view a
|
|
* copy at https://developers.jibo.com/license.
|
|
*/
|
|
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.jiboKb=t()}}(function(){return function t(e,o,r){function i(s,a){if(!o[s]){if(!e[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(n)return n(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var f=o[s]={exports:{}};e[s][0].call(f.exports,function(t){var o=e[s][1][t];return i(o?o:t)},f,f.exports,t,e,o,r)}return o[s].exports}for(var n="function"==typeof require&&require,s=0;s<r.length;s++)i(r[s]);return i}({1:[function(t,e,o){"use strict";var r=this&&this.__decorate||function(t,e,o,r){var i,n=arguments.length,s=n<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,r);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(n<3?i(s):n>3?i(e,o,s):i(e,o))||s);return n>3&&s&&Object.defineProperty(e,o,s),s},i=t("fs-extra"),n=t("path"),s=t("stream"),a=t("request"),u=t("url"),l=t("uuid"),f=t("blob-to-buffer"),c=t("./decorators"),d=t("jibo-log"),p=new d("Asset"),h=function(){function t(t,e,o){t?this.setSelfFromFilenameOrURL(t):(this._id=l.v4(),this.subtype=e||"asset",this.ext=o)}return t.prototype.setRootDir=function(t){this.rootDir=t},t.prototype.filename=function(){var t=this._id+"."+this.subtype;return this.ext&&(t+="."+this.ext),t},t.prototype.fullFilenameOrURL=function(){if(!this.rootDir)throw new Error("asset rootDir not set");var t;return this.rootDir.startsWith("http://")?t=this._url():(t=this.filename(),t=n.join(this.rootDir,"assets",t)),t},t.prototype.toString=function(){return this.fullFilenameOrURL()},t.prototype.save=function(t,e){var o=this;this.rootDir.startsWith("http://")?t instanceof Buffer||t instanceof s.Stream?this._saveViaWeb(t,e):f(t,function(t,r){p.iferr(t,"blobToBuffer"),t?e(t):o._saveViaWeb(r,e)}):t instanceof Buffer?this._saveBuffer(t,e):t instanceof s.Stream?this._saveStream(t,e):f(t,function(t,r){p.iferr(t,"blobToBuffer"),t?e(t):o._saveBuffer(r,e)})},t.prototype.setSelfFromFilenameOrURL=function(t){t.startsWith("http://")?this._setSelfFromURL(t):this._setSelfFromFilename(t)},t.prototype.load=function(t){if(this.rootDir.startsWith("http://"))this._loadBufferViaWeb(t);else{var e=this.fullFilenameOrURL();i.readFile(e,function(o,r){p.iferr(o,"readFile on",e),t(o,r)})}},t.prototype.loadStream=function(){if(this.rootDir.startsWith("http://"))return this._loadStreamViaWeb();var t=this.fullFilenameOrURL();return i.createReadStream(t)},t.prototype.loadBlob=function(t){this.load(function(e,o){var r;p.iferr(e,"could not load asset file"),e||(r=new Blob(o,{type:"application/octet-binary"})),t(e,r)})},t.prototype.remove=function(t){if(this.rootDir.startsWith("http://"))this._removeViaWeb(t);else{var e=this.fullFilenameOrURL();p.info("removing asset file",e),i.unlink(e,function(o){p.iferr(o,"fs.unlink of",e),o||p.debug("asset file",e,"removed from disk"),t(o)})}},t.prototype._url=function(){if(!this.rootDir)throw new Error("asset rootDir not set");var t=this.filename();return this.rootDir+"/asset/"+t},t.prototype._saveViaWeb=function(t,e){var o=this,r=this._url(),i={body:t,headers:{"Content-Type":"application/octet-stream"}};a.post(r,i,function(t,i,n){t=o._checkStatusCode(t,i,n),p.iferr(t,"request.post",r),e(t,r)})},t.prototype._setSelfFromFilename=function(t){n.isAbsolute(t)&&(this.rootDir=n.dirname(n.dirname(t)),t=n.basename(t));var e=new RegExp("^([^.]*)[.]([^.]*)([.]([^.]*))?$"),o=t.match(e);if(!o)throw new Error("filename did not parse");this._id=o[1],this.subtype=o[2]||"",this.ext=o[4]||""},t.prototype._setSelfFromURL=function(t){var e=u.parse(t),o=e.pathname.split("/"),r=o.slice(0,3).join("/");this.rootDir="http://"+e.hostname+":"+e.port+"/"+r;var i=o[o.length-1];this._setSelfFromFilename(i)},t.prototype._removeViaWeb=function(t){var e=this,o=this._url();a.del(o,function(r,i,n){r=e._checkStatusCode(r,i,n),p.iferr(r,"request.delete",o),t(r)})},t.prototype._saveBuffer=function(t,e){if(!t)return void e();var o=this.fullFilenameOrURL();i.ensureDir(n.dirname(o),function(r){p.iferr(r,"ensureDir"),r?e(r):(p.info("writing asset file",o),i.writeFile(o,t,function(t){p.iferr(t,"file write error",o),e(t,o)}))})},t.prototype._saveStream=function(t,e){if(!t)return void e();var o=this.fullFilenameOrURL();i.ensureDir(n.dirname(o),function(r){if(p.iferr(r,"ensureDir"),r)e(r);else{p.info("writing asset file",o);var n=i.createWriteStream(o);t.pipe(n),n.on("finish",function(){e(null,o)}),n.on("error",function(t){p.error("file write error",o,t)})}})},t.prototype._loadBufferViaWeb=function(t){var e=this,o=this._url(),r={headers:{"Content-Type":"application/octet-stream"},encoding:null};a.get(o,r,function(r,i,n){r=e._checkStatusCode(r,i,n),p.iferr(r,"request.get",o),t(r,n)})},t.prototype._loadStreamViaWeb=function(){var t=this._url(),e={headers:{"Content-Type":"application/octet-stream"},encoding:null};return a.get(t,e)},t.prototype._checkStatusCode=function(t,e,o){if(void 0===o&&(o=null),!t&&(e.statusCode<200||e.statusCode>299)){var r="HTTP Error Code "+e.statusCode;o&&(r+=o),t=new Error(r)}return t},t}();r([c.promisify],h.prototype,"save",null),r([c.promisify],h.prototype,"load",null),r([c.promisify],h.prototype,"loadBlob",null),r([c.promisify],h.prototype,"remove",null),Object.defineProperty(o,"__esModule",{value:!0}),o.default=h},{"./decorators":12,"blob-to-buffer":void 0,"fs-extra":void 0,"jibo-log":void 0,path:void 0,request:void 0,stream:void 0,url:void 0,uuid:void 0}],2:[function(t,e,o){"use strict";var r=t("jibo-log"),i=new r("Cache"),n=function(){function t(){}return t.prototype.fetch=function(t,e){void 0===e&&(e=!1);var o=void 0;return t in this?o=this[t]:e||i.warn("fetch id",t,"not in cache"),o},t.prototype.add=function(t,e){void 0===e&&(e=!1),t&&t._id?this._add(t._id,t,e):i.error("add: can not add invalid object",t)},t.prototype.remove=function(t,e){void 0===e&&(e=!1);var o=this._toId(t);o?o in this?delete this[o]:e||i.warn("remove: id not in cache",o):i.error("remove: invalid idOrNode",t)},t.prototype.isPresent=function(t){var e=this._toId(t);return e||i.error("isPresent: invalid idOrNode",t),e in this},t.prototype.interceptLoad=function(t,e,o){var r=this,i=this.fetch(t,!0);i?setImmediate(function(){e(null,i)}):o(t,function(o,i){o||(i?r.add(i):r._add(t,null)),e(o,i)})},t.prototype._add=function(t,e,o){void 0===o&&(o=!1),t in this?null===this[t]?this[t]=e:Object.is(e,this[t])?o||i.warn("add: there was already an entry in the cache for node id",t):(i.warn("add: existing entry for node id",t,"and new object does not match it!"),this[t]=e):this[t]=e},t.prototype._toId=function(t){return"object"==typeof t&&(t=t._id),t},t}();Object.defineProperty(o,"__esModule",{value:!0}),o.default=n},{"jibo-log":void 0}],3:[function(t,e,o){"use strict";var r=t("nedb"),i=t("jibo-log"),n=new i("Database"),s=function(){function t(t){this.filename=t}return t.prototype.init=function(t){var e=this;this.nodes=new r({filename:this.filename}),this.nodes.loadDatabase(function(o){n.iferr(o,"Collection#loadDatabase for nodes",e.filename),o?t(o):e.nodes.count({},function(o,r){n.iferr(o,"error counting nodes in",e.filename),o||n.info(r,"KB node"+(1===r?"":"s")+" in",e.filename),t(o,r)})})},t.prototype.load=function(t,e){var o=this;this.nodes.findOne({_id:t},function(r,i){n.iferr(r,"Database#findOne of",t,"on",o.filename),e(r,i)})},t.prototype.loadNodeOfType=function(t,e){var o=this;this.nodes.findOne({type:t},function(r,i){n.iferr(r,"Database#findOne of type",t,"on",o.filename),e(r,i)})},t.prototype.loadNodesOfType=function(t,e){var o={type:t};this.nodes.find(o).exec(function(t,o){n.iferr(t,"Collection#find error"),e(t,o)})},t.prototype.save=function(t,e){var o=this;if(!t._id)throw new Error("node does not have an id");this.nodes.update({_id:t._id},t,{upsert:!0},function(r,i){n.iferr(r,"Collection#update error",o.filename),r&&(t._id=void 0),e&&e(r,t)})},t.prototype.remove=function(t,e){var o=this,r=this._toId(t);this.nodes.remove({_id:r},{},function(t,i){n.iferr(t,"Database#remove of",r,"on",o.filename),e&&e(t,i)})},t.prototype._toId=function(t){return"object"==typeof t&&(t=t._id),t},t}();Object.defineProperty(o,"__esModule",{value:!0}),o.default=s},{"jibo-log":void 0,nedb:void 0}],4:[function(t,e,o){"use strict";var r=t("fs"),i=t("fs-extra"),n=t("./KnowledgeDatabase"),s=t("jibo-log"),a=new s("DatabaseManager"),u=function(){function t(){}return t.get=function(t,e){var o=this,r=this.databases[t];r?setImmediate(function(){e(null,r)}):this.exists(t,function(i,s){i||!s?e(new Error("KB slice "+t+" does not exist, create it first")):(r=new n.default(t),o.databases[t]=r,r.init(function(t){a.iferr(t,"KnowledgeDatabase.init"),t?e(t):e(null,r)}))})},t.exists=function(t,e){var o=n.default.getKbFilename(t);r.access(o,function(t){var o=!t;e(null,o)})},Object.defineProperty(t.prototype,"databases",{get:function(){return t.databases},enumerable:!0,configurable:!0}),t.prototype.create=function(t,e){if(this.databases[t])setImmediate(function(){e(null,!1)});else{var o=n.default.getKbDirectory(t);i.ensureDir(o,function(o){if(o)e(o);else{var i=n.default.getKbFilename(t);r.open(i,"ax",function(t,o){var i=!1;t?t&&"EEXIST"===t.code&&(t=null,i=!1):(r.close(o),i=!0),e(t,i)})}})}},t.prototype.exists=function(e,o){t.exists(e,o)},t.prototype.get=function(e,o){t.get(e,o)},t.prototype.release=function(t){delete this.databases[t]},t}();u.databases={},o.DatabaseManager=u,Object.defineProperty(o,"__esModule",{value:!0}),o.default=u},{"./KnowledgeDatabase":6,fs:void 0,"fs-extra":void 0,"jibo-log":void 0}],5:[function(t,e,o){"use strict";var r=this&&this.__decorate||function(t,e,o,r){var i,n=arguments.length,s=n<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,r);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(n<3?i(s):n>3?i(e,o,s):i(e,o))||s);return n>3&&s&&Object.defineProperty(e,o,s),s},i=t("request"),n=t("querystring"),s=t("./decorators"),a=t("./Model"),u=t("./KnowledgeDatabase"),l=t("./Node"),f=t("./Asset"),c=t("./DatabaseManager"),d=t("./LoopModel"),p=t("./UserNode"),h=t("jibo-log"),y=new h("KnowledgeBase"),v=function(){function t(){this.Asset=f.default,this.KnowledgeDatabase=u.default,this.Model=a.default,this.Node=l.default,this.databaseManager=new c.default,this.onInitCallbacks=[],this.registerModelClass("jibo/loop",d.default),this.registerNodeClass("user",p.default,"jibo/loop")}return t.prototype.init=function(t,e){this.httpUrl="http://"+t.host+":"+t.port,process.nextTick(e),this.onInitCallbacks.forEach(function(t){process.nextTick(t)}),this.onInitCallbacks=[]},t.prototype.onInit=function(t){this.httpUrl?process.nextTick(function(){t(null)}):this.onInitCallbacks.push(t)},t.prototype.initLoop=function(){var t=this.createModel("/jibo/loop");this.loop=t},t.prototype.initMedia=function(){},t.prototype.createSlice=function(t,e,o){var r=this;"function"!=typeof e||o||(o=e,e=null),e=e||this.httpUrl;var s=n.escape(t),a=this.httpUrl+"/v1/kb/"+s+"/create";i.post(a,{json:!0},function(t,e,i){t=r._checkStatusCode(t,e,i),y.iferr(t,"request.post",a);var n=i&&i.created;o&&o(t,n)})},t.prototype.existsSlice=function(t,e,o){var r=this;"function"!=typeof e||o||(o=e,e=null),e=e||this.httpUrl;var s=n.escape(t),a=this.httpUrl+"/v1/kb/"+s+"/exists";i.get(a,{json:!0},function(t,e,i){t=r._checkStatusCode(t,e,i),y.iferr(t,"request.get",a);var n=i&&i.exists;o&&o(t,n)})},t.prototype.createModel=function(t,e){if(t=this._toArray(t),e=e||this.httpUrl,!e)throw new Error("Knowledge Base has not been inited");var o=[];t.forEach(function(t){if(!t.startsWith("/"))throw new Error("kb#createModel: kb names must be rooted (must start with /)");t=t.substr(1),o.push(t)});var r=this.findModelClass(o);return new r(o,e)},t.prototype.registerNodeClass=function(t,e,o){void 0===o&&(o="*"),l.default.registerNodeClass(t,e,o)},t.prototype.registerModelClass=function(t,e){a.default.registerModelClass(t,e)},t.prototype.findNodeClass=function(t,e){return l.default.findNodeClass(t,e)},t.prototype.findModelClass=function(t){return a.default.findModelClass(t)},t.prototype.removeSlice=function(t,e){var o=this,r=n.escape(t),s=this.httpUrl+"/v1/kb/"+r+"/remove/yesiamsure";i.del(s,function(t,r,i){t=o._checkStatusCode(t,r,i),y.iferr(t,"request.del",s),e&&e(t)})},t.prototype.removeAll=function(t){var e=this,o=this.httpUrl+"/v1/removeall/yesiamsure";i.del(o,function(r,i,n){r=e._checkStatusCode(r,i,n),y.iferr(r,"request.del",o),t&&t(r)})},t.prototype._checkStatusCode=function(t,e,o){if(void 0===o&&(o=null),!t&&(e.statusCode<200||e.statusCode>299)){var r="HTTP Error Code "+e.statusCode;o&&(r+=o),t=new Error(r)}return t},t.prototype._toArray=function(t){return Array.isArray(t)?t:[t]},t}();r([s.promisify],v.prototype,"onInit",null),r([s.promisify],v.prototype,"createSlice",null),r([s.promisify],v.prototype,"existsSlice",null),r([s.promisify],v.prototype,"removeSlice",null),r([s.promisify],v.prototype,"removeAll",null),Object.defineProperty(o,"__esModule",{value:!0}),o.default=v},{"./Asset":1,"./DatabaseManager":4,"./KnowledgeDatabase":6,"./LoopModel":7,"./Model":8,"./Node":9,"./UserNode":10,"./decorators":12,"jibo-log":void 0,querystring:void 0,request:void 0}],6:[function(t,e,o){"use strict";var r=t("fs-extra"),i=t("path"),n=t("./Database"),s=t("./Node"),a=t("jibo-log"),u=new a("KnowledgeDatabase"),l=["nodes","assets"],f={SIMULATOR:"SIMULATOR",REMOTELY:"REMOTELY",ON_ROBOT:"ON_ROBOT",UNIT_TESTS:"UNIT_TESTS"},c=function(){function t(e){this.kbName=e,t._validateKbName(e),this.dbDirectory=t.getKbDirectory(this.kbName),this.dbFilename=t.getKbFilename(this.kbName)}return t.getRootDirectory=function(){return this._onRobot()?"/opt/jibo/Knowledge":i.join(process.env.HOME||process.env.USERPROFILE,".jibo","kb")},t.getKbDirectory=function(e){t._validateKbName(e);var o=t.getRootDirectory();return i.join(o,e)},t.getKbFilename=function(e){t._validateKbName(e);var o=t.getKbDirectory(e);return i.join(o,"nodes")},t._validateKbName=function(t){if(t.length>200)throw new Error("kb name is too long");if(/[^a-z0-9\/_-]/.test(t))throw new Error("illegal character in kb name");if(t.endsWith("/"))throw new Error("kb name ends with a slash");var e=t.split("/");if(e.length>10)throw new Error("kb name has too many sub directories");e.forEach(function(t,e){if(0===t.length&&0!==e)throw new Error("kb name component is empty");if(t.length>75)throw new Error("kb name component is too long");if(/^[^a-z0-9]/.test(t))throw new Error("kb names must start with an alphanumeric character");if(l.indexOf(t)>=0)throw new Error("kb name is reserved word")})},t._onRobot=function(){var t=process.env.runMode||process.env.RUNMODE;return t||"linux"!==process.platform||"arm"!==process.arch||(t=f.ON_ROBOT),t===f.ON_ROBOT},t.prototype.init=function(t){var e=this;this._setupDirectory(function(o){u.iferr(o,"KnowledgeDatabase#_setupDirectory"),o||(e.database=new n.default(e.dbFilename),e.database.init(function(o,r){if(u.iferr(o,"Database#init"),o)t(o);else if(0===r){u.info("creating root node for",e.kbName);var i=s.default.findNodeClass("root",e.kbName),n=new i("root");e.adoptNodeAsOurOwn(n),n.save(function(o){u.iferr(o,"error creating initial root node for",e.kbName),t(o)})}else t(o)}))})},t.prototype.load=function(t,e){var o=this;this.database.load(t,function(t,r){if(t)e(t);else{var i=null;r&&(i=o.createNodeFromObject(r)),e(null,i)}})},t.prototype.loadRoot=function(t){var e=this;this.database.loadNodeOfType("root",function(o,r){u.iferr(o,"Database#loadNodeOfType");var i;!o&&r&&(i=e.createNodeFromObject(r)),t(o,i)})},t.prototype.save=function(t,e){t.setUpdated(),this.database.save(t,function(t){e&&e(t)})},t.prototype.remove=function(t,e){var o=this.toId(t);this.database.remove(o,e)},t.prototype.createNodeFromObject=function(t){var e=t.type||"node",o=s.default.findNodeClass(e,this.kbName),r=new o(null,null,t);return this.adoptNodeAsOurOwn(r),r},t.prototype.createNode=function(t,e){var o,r;if("function"==typeof t)o=t;else if(r=t,o=s.default.findNodeClass(r,this.kbName),"root"===r)throw new Error("can not create additional root nodes");var i;return i=r&&o===s.default?new o(r,e):new o(e),this.adoptNodeAsOurOwn(i),i},t.prototype.adoptNodeAsOurOwn=function(t){t.setKb(this)},t.prototype.getDirectory=function(){return this.dbDirectory},t.prototype.toId=function(t){return"object"==typeof t&&(t=t._id),t},t.prototype._setupDirectory=function(t){r.ensureDir(this.dbDirectory,t)},t}();Object.defineProperty(o,"__esModule",{value:!0}),o.default=c},{"./Database":3,"./Node":9,"fs-extra":void 0,"jibo-log":void 0,path:void 0}],7:[function(t,e,o){"use strict";var r=this&&this.__extends||function(t,e){function o(){this.constructor=t}for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)},i=this&&this.__decorate||function(t,e,o,r){var i,n=arguments.length,s=n<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,r);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(n<3?i(s):n>3?i(e,o,s):i(e,o))||s);return n>3&&s&&Object.defineProperty(e,o,s),s},n=t("request"),s=t("./decorators"),a=t("./Model"),u=t("jibo-log"),l=new u("LoopModel"),f=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return r(e,t),e.prototype.loadLoop=function(t){var e=this;this.loadLoopAll(function(o,r){o?t(o):t(null,e._onlyAccepted(r))})},e.prototype.loadLoopInvited=function(t){var e=this;this.loadLoopAll(function(o,r){o?t(o):t(null,e._onlyInvited(r))})},e.prototype.loadLoopActive=function(t){var e=this;this.loadLoopAll(function(o,r){o?t(o):t(null,e._onlyActive(r))})},e.prototype.loadLoopAll=function(t){var e=this;this.loadRoot(null,function(o,r){if(l.iferr(o,"loadRoot"),!o&&r){var i=r.getEdges("user");e.load(i,function(e,o){l.iferr(e,"load"),t(e,o)})}else t(o)})},e.prototype.getUserNodeById=function(t,e){this.loadLoop(function(o,r){if(o)return e(o);var i=r.find(function(e){return e.id===t});i?e(null,i):e("Could not find user node for loop id "+t)})},e.prototype.getWrittenNameById=function(t,e){this.getUserNodeById(t,function(t,o){return t?e(t):void e(null,o.getWrittenName())})},e.prototype.getSpokenNameById=function(t,e){this.getUserNodeById(t,function(t,o){return t?e(t):void e(null,o.toString())})},e.prototype.fetchLoop=function(){return this._onlyAccepted(this.fetchLoopAll())},e.prototype.fetchLoopInvited=function(){return this._onlyInvited(this.fetchLoopAll())},e.prototype.fetchLoopActive=function(){return this._onlyActive(this.fetchLoopAll())},e.prototype.fetchLoopAll=function(){return this.fetch(this.fetchRoot().getEdges("user"))},e.prototype.setPhoneticName=function(t,e,o){var r="object"==typeof t?t._id:t,i=this.httpUrl+"/v1/loop/updatePhoneticName";this.loadRoot(function(t,s){l.iferr(t,"loadRoot");var a={loopId:s.data.id,memberId:r,phoneticName:e};n.post(i,{json:a},function(t,e,r){l.iferr(t,"request.post",i),o&&o(t)})})},e.prototype.setEnrollmentFace=function(t,e,o){var r="object"==typeof t?t._id:t,i={memberId:r,face:!!e};this.setEnrollment(i,o)},e.prototype.setEnrollmentVoice=function(t,e,o){var r="object"==typeof t?t._id:t,i={memberId:r,voice:!!e};this.setEnrollment(i,o)},e.prototype.setEnrollment=function(t,e){var o=this,r=this.httpUrl+"/v1/loop/enrollment";this.loadRoot(function(i,s){l.iferr(i,"loadRoot"),t.loopId=s.data.id,n.post(r,{json:t},function(t,i,n){t=o._checkStatusCode(t,i,n),l.iferr(t,"request.post",r),e&&e(t)})})},e.prototype._onlyAccepted=function(t){return t.filter(function(t){return"accepted"===t.data.status})},e.prototype._onlyInvited=function(t){return t.filter(function(t){return"invited"===t.data.status})},e.prototype._onlyActive=function(t){return t.filter(function(t){return t.data.isActive})},e.prototype._checkStatusCode=function(t,e,o){if(void 0===o&&(o=null),!t&&(e.statusCode<200||e.statusCode>299)){var r="HTTP Error Code "+e.statusCode;o&&(r+=o),t=new Error(r)}return t},e}(a.default);i([s.promisify],f.prototype,"loadLoop",null),i([s.promisify],f.prototype,"loadLoopInvited",null),i([s.promisify],f.prototype,"loadLoopActive",null),i([s.promisify],f.prototype,"loadLoopAll",null),i([s.promisify],f.prototype,"getUserNodeById",null),i([s.promisify],f.prototype,"getWrittenNameById",null),i([s.promisify],f.prototype,"getSpokenNameById",null),i([s.promisify],f.prototype,"setPhoneticName",null),i([s.promisify],f.prototype,"setEnrollmentFace",null),i([s.promisify],f.prototype,"setEnrollmentVoice",null),i([s.promisify],f.prototype,"setEnrollment",null),Object.defineProperty(o,"__esModule",{value:!0}),o.default=f},{"./Model":8,"./decorators":12,"jibo-log":void 0,request:void 0}],8:[function(t,e,o){"use strict";var r=this&&this.__decorate||function(t,e,o,r){var i,n=arguments.length,s=n<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,r);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(n<3?i(s):n>3?i(e,o,s):i(e,o))||s);return n>3&&s&&Object.defineProperty(e,o,s),s},i=t("async"),n=t("./decorators"),s=t("./Cache"),a=t("./DatabaseManager"),u=t("./WebClient"),l=t("jibo-log"),f=new l("Model"),c=function(){function t(t,e){void 0===e&&(e=null);var o=this;this.kbNames=this._toArray(t),this.httpUrl=e,this.pool=[],this.httpUrl&&this.kbNames.forEach(function(t){var e=new u.default(t,o.httpUrl);o.pool.push(e)})}return t.registerModelClass=function(t,e){Array.isArray(t)||(t=[t]);var o=t.join(";"),r=this.modelClassRegistry;r[o]&&f.warn("registerModelClass: overwriting previous class registered for ",o),r[o]=e},t.findModelClass=function(e){Array.isArray(e)||(e=[e]);var o=e.join(";"),r=t,i=this.modelClassRegistry;return i[o]&&(r=i[o]),r},t.prototype.init=function(t){var e=this;if(this.httpUrl)setImmediate(t);else{var o=[];this.kbNames.forEach(function(t){o.push(function(e){a.default.get(t,e)})}),i.series(o,function(o,r){f.iferr(o,"KnowledgeDatabase#init"),o||(e.pool=r),t(o)})}},t.prototype.createModel=function(e,o){void 0===o&&(o=null),e=this._toArray(e),o=o||this.httpUrl;var r=this.kbNames[0],i=[];e.forEach(function(t){t.startsWith("/")||(t="/"+r+"/"+t),i.push(t)});var n=t.findModelClass(i);return new n(i,o)},t.prototype.createNode=function(t,e){var o=this.pool[0].createNode(t,e);return this.cache&&this.cache.add(o),o},t.prototype.load=function(t,e){Array.isArray(t)?this._loadArray(t,e):this._loadOne(t,e)},t.prototype.loadList=function(t,e){throw new Error("kb.Model.loadList is an unsupported method, use kb.Model.load")},t.prototype.fetchList=function(t,e){throw void 0===e&&(e=!1),new Error("kb.Model.fetchList is an unsupported method, use kb.Model.fetch")},t.prototype.loadRoot=function(t,e){var o=this;"function"!=typeof t||e||(e=t,t=null);var r;if(r=t?this._getKnowledgeDatabase(t):this.pool[0])if(this.cache){var i=this.fetchRoot(t,!0);i?setImmediate(function(){e(null,i)}):r.loadRoot(function(r,i){!r&&i&&(o.roots[t]=i),e(r,i)})}else r.loadRoot(e);else setImmediate(function(){var o=new Error("loadRoot kb slice name "+t+" not found");e(o)})},t.prototype.loadLayers=function(t,e,o){var r=function(t,e){return e()};this.visitLayers(t,e,r,o)},t.prototype.saveLayers=function(t,e,o){var r=function(t,e){return t.save(e)};this.visitLayers(t,e,r,o)},t.prototype.visitLayers=function(t,e,o,r){var n=this;e=this._toArray(e);var s={};s[t._id]=!0;var a=function(t,r){var u=[],l=[];t.forEach(function(t){t in s||(s[t]=!0,u.push(function(r){n.load(t,function(t,i){!t&&i?o(i,function(t){i.getEdges(e).forEach(function(t){t in s||l.push(t)}),r(t)}):r(t)})}))}),i.series(u,function(t){!t&&l.length?a(l,r):r(t)})};o(t,function(o){f.iferr(o,"action on initial node failed"),o?r(o):a(t.getEdges(e),r)})},t.prototype.begin=function(){var t=this.constructor,e=new t(this.kbNames,this.httpUrl);return e.enableCache(),e},t.prototype.fetch=function(t,e){var o=this;if(void 0===e&&(e=!1),this._assertCache(),Array.isArray(t)){var r=[],i=[];return t.forEach(function(t){var e=o.cache.fetch(t,!0);e||null===e?r.push(e):(r.push(void 0),i.push(t))}),i.length&&!e&&f.warn("fetchList ids were not in cache, skipped",i.join(", ")),r}return this.cache.fetch(t,e)},t.prototype.fetchRoot=function(t,e){void 0===e&&(e=!1),this._assertCache(),t||(t=this.pool[0].kbName);var o=null;return this.roots[t]?o=this.roots[t]:e||f.warn("fetchRoot for name",t,"root not in cache"),o},t.prototype.enableCache=function(){this.cache=new s.default,this.roots={}},t.prototype._load=function(t,e){var o=[];this.pool.forEach(function(e){o.push(function(o,r){r||(r=o,o=null),o?r(null,o):e.load(t,function(t,e){r(t,e)})})}),i.waterfall(o,e)},t.prototype._loadOne=function(t,e){var o=this;this.cache?this.cache.interceptLoad(t,e,function(t,e){o._load(t,e)}):this._load(t,e)},t.prototype._loadArray=function(t,e){var o=this,r=[];t.forEach(function(t){r.push(function(e){o._loadOne(t,e)})}),i.series(r,e)},t.prototype._getKnowledgeDatabase=function(t){for(var e,o=0;o<this.pool.length;o++)if(t===this.pool[o].kbName){e=this.pool[o];break}return e},t.prototype._assertCache=function(){if(!this.cache)throw new Error("fetch method called on a Model without a cache")},t.prototype._toArray=function(t){return Array.isArray(t)?t:[t]},t}();c.modelClassRegistry={},r([n.promisify],c.prototype,"load",null),r([n.promisify],c.prototype,"loadRoot",null),r([n.promisify],c.prototype,"loadLayers",null),r([n.promisify],c.prototype,"saveLayers",null),r([n.promisify_4],c.prototype,"visitLayers",null),Object.defineProperty(o,"__esModule",{value:!0}),o.default=c},{"./Cache":2,"./DatabaseManager":4,"./WebClient":11,"./decorators":12,async:void 0,"jibo-log":void 0}],9:[function(t,e,o){"use strict";var r=this&&this.__decorate||function(t,e,o,r){var i,n=arguments.length,s=n<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,r);else for(var a=t.length-1;a>=0;a--)(i=t[a])&&(s=(n<3?i(s):n>3?i(e,o,s):i(e,o))||s);return n>3&&s&&Object.defineProperty(e,o,s),s},i=t("lodash"),n=t("uuid"),s=t("async"),a=t("./decorators"),u=t("./Asset"),l=t("jibo-log"),f=new l("Node"),c=function(){function t(t,e,o){o&&i.extend(this,o),this._id=this._id||n.v4(),this.data=e||this.data||{},this.type=t||this.type||"node";var r=Number(new Date);this.created=this.created||r,this.updated=this.updated||r}return t.registerNodeClass=function(e,o,r){void 0===r&&(r="*");var i=t.nodeClassRegistry;i[r]&&i[r][e]&&f.warn("registerNodeClass: overwriting previous class registered for type",e),i[r]||(i[r]={}),i[r][e]=o},t.findNodeClass=function(e,o){var r=t,i=t.nodeClassRegistry;return i[o]&&i[o][e]?r=i[o][e]:i["*"]&&i["*"][e]&&(r=i["*"][e]),r},t.prototype.save=function(t){this.getKb().save(this,t)},t.prototype.remove=function(t){this.getKb().remove(this,t)},t.prototype.addEdges=function(t,e){var o=this;t=this._toArray(t),this.edges=this.edges||{},t.forEach(function(t){var r=o._resolveIdAndLayer(t,e,"Node#addEdges()"),i=r.id,n=r.l;o.edges[n]=o.edges[n]||[],o.edges[n].push(i)})},t.prototype.removeEdges=function(t,e){var o=this;t=this._toArray(t),this.edges&&t.forEach(function(t){var r=o._resolveIdAndLayer(t,e,"Node#removeEdges()"),n=r.id,s=r.l;o.edges[s]&&i.pull(o.edges[s],n)})},t.prototype.clearEdges=function(t){var e=this;t=this._toArray(t),this.edges&&t.forEach(function(t){e.edges[t]&&(e.edges[t]=[])})},t.prototype.getEdges=function(t){var e=this;t=this._toArray(t);var o=[];return t.forEach(function(t){var r=i.get(e,"edges."+t,[]);r.forEach(function(t){o.indexOf(t)<0&&o.push(t)})}),o},t.prototype.getLayers=function(){return this.edges?Object.keys(this.edges):[]},t.prototype.createAsset=function(t,e){var o=new u.default(null,t,e);if(this.getKb){var r=this.getKb().getDirectory();o.setRootDir(r)}else f.warn("createAsset: creating an asset from a node without a kb, rootDir not set");return this.addAssets(o),o},t.prototype.addAssets=function(t,e){var o=this;t=this._toArray(t),this.assets=this.assets||{},t.forEach(function(t){var r=e||t.subtype;o.assets[r]=o.assets[r]||[],o.assets[r].push(t.filename())})},t.prototype.getAssets=function(t){void 0===t&&(t="asset");var e=[];if(this.assets&&this.assets[t]){var o=this.assets[t],r=this.getKb().getDirectory();o.forEach(function(t){var o=new u.default(t);o.setRootDir(r),e.push(o)})}return e},t.prototype.getAllAssets=function(){var t=this,e=[],o=this.getAssetSubtypes();return o.forEach(function(o){e=e.concat(t.getAssets(o))}),e},t.prototype.getAssetSubtypes=function(){var t=i.keys(this.assets);return t},t.prototype.removeAsset=function(t,e){var o=t.subtype,r=this.assets[o].indexOf(t.filename());this.assets[o].splice(r,1),t.remove(e)},t.prototype.removeAllAssets=function(t){var e=this,o=this.getAssetSubtypes(),r=[];o.forEach(function(t){var o=e.getAssets(t);o.forEach(function(t){r.push(function(o){e.removeAsset(t,o)})})}),s.series(r,t)},t.prototype.setKb=function(t){return this.getKb=function(){return t},this},t.prototype.setUpdated=function(t){this.updated=t||Number(new Date)},t.prototype._resolveIdAndLayer=function(t,e,o){var r,i;if("string"==typeof t){if(r=t,!e)throw Error(o+": must specify layer when supplying ids");i=e}else{var n=t;r=n._id,i=e||n.type}return{id:r,l:i}},t.prototype._toArray=function(t){return Array.isArray(t)?t:[t]},t}();c.nodeClassRegistry={},r([a.promisify],c.prototype,"save",null),r([a.promisify],c.prototype,"remove",null),r([a.promisify],c.prototype,"removeAsset",null),r([a.promisify],c.prototype,"removeAllAssets",null),Object.defineProperty(o,"__esModule",{value:!0}),o.default=c},{"./Asset":1,"./decorators":12,async:void 0,"jibo-log":void 0,lodash:void 0,uuid:void 0}],10:[function(t,e,o){"use strict";var r=this&&this.__extends||function(t,e){function o(){this.constructor=t}for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)},i=t("./Node"),n=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return r(e,t),Object.defineProperty(e.prototype,"id",{get:function(){return this._id},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"firstName",{get:function(){return this.data.firstName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"lastName",{get:function(){return this.data.lastName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"nickName",{get:function(){return this.data.nickName},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"gender",{get:function(){return this.data.gender},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"isJibo",{get:function(){return!this.data.firstName},enumerable:!0,configurable:!0}),e.prototype.getWrittenName=function(){return this.data.nickName||this.data.firstName||"Jibo"},e.prototype.toString=function(){return this.data.phoneticName||this.getWrittenName()},e.prototype.getInitials=function(){return this.isJibo?"J":this.data.firstName&&this.data.lastName?this.data.firstName.charAt(0).toUpperCase()+this.data.lastName.charAt(0).toUpperCase():""},e}(i.default);Object.defineProperty(o,"__esModule",{value:!0}),o.default=n},{"./Node":9}],11:[function(t,e,o){"use strict";var r=this&&this.__extends||function(t,e){function o(){this.constructor=t}for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r]);t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)},i=t("request"),n=t("querystring"),s=t("./KnowledgeDatabase"),a=t("jibo-log"),u=new a("WebClient"),l=function(t){function e(e,o){var r=t.call(this,e)||this;return r.httpUrl=o.replace(/\/$/,""),r}return r(e,t),e.prototype.init=function(t){setImmediate(t)},e.prototype.load=function(t,e){var o=this,r=this._makeUrl("/node/load/"+t);i.get(r,{json:!0},function(t,i,n){if(t=o._checkStatusCode(t,i,n),u.iferr(t,"request.get",r),t)e(t);else{var s=null;n&&(s=o.createNodeFromObject(n)),e(null,s)}})},e.prototype.loadRoot=function(t){var e=this,o=this._makeUrl("/node/loadRoot");
|
|
i.get(o,{json:!0},function(r,i,n){r=e._checkStatusCode(r,i,n),u.iferr(r,"request.get",o);var s;!r&&n&&(s=e.createNodeFromObject(n)),t(r,s)})},e.prototype.save=function(t,e){var o=this,r=this._makeUrl("/node/save");i.post(r,{json:t},function(t,i,n){t=o._checkStatusCode(t,i,n),u.iferr(t,"request.post",r),e&&e(t)})},e.prototype.remove=function(t,e){var o=this,r=this.toId(t),n=this._makeUrl("/node/remove/"+r);i.del(n,function(t,r,i){t=o._checkStatusCode(t,r,i),u.iferr(t,"request.delete",n),e(t)})},e.prototype.getDirectory=function(){return this._makeUrl()},e.prototype._makeUrl=function(t){void 0===t&&(t="");var e=n.escape(this.kbName);return this.httpUrl+"/v1/kb/"+e+t},e.prototype._checkStatusCode=function(t,e,o){if(void 0===o&&(o=null),!t&&(e.statusCode<200||e.statusCode>299)){var r="HTTP Error Code "+e.statusCode;o&&(r+=o),t=new Error(r)}return t},e}(s.default);Object.defineProperty(o,"__esModule",{value:!0}),o.default=l},{"./KnowledgeDatabase":6,"jibo-log":void 0,querystring:void 0,request:void 0}],12:[function(t,e,o){"use strict";function r(t,e,o){var r=o.value;return o.value=function(){var t,e=this,o=Array.from(arguments),i=o.slice(-1)[0];return t="function"==typeof i?r.apply(this,o):new Promise(function(t,i){var n=function(e,o){e?i(e):t(o)},s=o.concat(n);r.apply(e,s)})},o}function i(t,e,o){var r=o.value;return o.value=function(){var t,e=Array.from(arguments),o=e[4];if("function"==typeof o)t=r.apply(this,e);else{var i=this;t=new Promise(function(t,o){var n=function(e,r){e?o(e):t(r)},s=e.concat(n);r.apply(i,s)})}return t},o}o.promisify=r,o.promisify_4=i},{}],13:[function(t,e,o){"use strict";function r(t){for(var e in t)o.hasOwnProperty(e)||(o[e]=t[e])}r(t("./KnowledgeBase"));var i=t("./Asset");o.Asset=i.default;var n=t("./Cache");o.Cache=n.default;var s=t("./Database");o.Database=s.default;var a=t("./DatabaseManager");o.DatabaseManager=a.default;var u=t("./KnowledgeBase");o.KnowledgeBase=u.default;var l=t("./KnowledgeDatabase");o.KnowledgeDatabase=l.default;var f=t("./LoopModel");o.LoopModel=f.default;var c=t("./Model");o.Model=c.default;var d=t("./Node");o.Node=d.default;var p=t("./UserNode");o.UserNode=p.default;var h=t("./WebClient");o.WebClient=h.default},{"./Asset":1,"./Cache":2,"./Database":3,"./DatabaseManager":4,"./KnowledgeBase":5,"./KnowledgeDatabase":6,"./LoopModel":7,"./Model":8,"./Node":9,"./UserNode":10,"./WebClient":11}]},{},[13])(13)}); |