From 5b327ff4a6b9ce98e4b405740f2d7c8407359c69 Mon Sep 17 00:00:00 2001 From: Eugeny Shingarev Date: Thu, 23 Feb 2017 03:21:14 +0700 Subject: [PATCH] [*] minor readme updates, + updating version due to fixes of other contributor, + exporting Constants, + rebuild libs --- ApiAiClient.js | 2 +- ApiAiClient.ts | 2 +- ApiAiStreamClient.ts | 2 +- README.md | 7 +- es6/ApiAiClient.js | 15 +-- es6/{Constants.js => ApiAiConstants.js} | 19 ++-- es6/Request/TTSRequest.js | 4 +- es6/Stream/StreamClient.js | 10 +- es6/_build.js | 2 +- package.json | 2 +- spec/Client.spec.ts | 12 +-- target/ApiAi.js | 122 +++++++++++----------- target/ApiAi.min.js | 2 +- target/ApiAi.streamless.js | 112 ++++++++++---------- target/ApiAi.streamless.min.js | 2 +- ts/ApiAiClient.ts | 19 ++-- ts/ApiAiConstants.ts | 15 +++ ts/Constants.ts | 18 ---- ts/Interfaces.ts | 8 +- ts/Request/LocalTTSRequest.ts | 2 +- ts/Request/TTSRequest.ts | 4 +- ts/Request/VoiceRequest.ts | 4 +- ts/_build.streamless.ts | 2 +- ts/_build.ts | 2 +- tsconfig.es6src.json => tsconfig.es6.json | 0 25 files changed, 194 insertions(+), 195 deletions(-) rename es6/{Constants.js => ApiAiConstants.js} (70%) create mode 100644 ts/ApiAiConstants.ts delete mode 100644 ts/Constants.ts rename tsconfig.es6src.json => tsconfig.es6.json (100%) diff --git a/ApiAiClient.js b/ApiAiClient.js index 3c3cadb..8e71e79 100644 --- a/ApiAiClient.js +++ b/ApiAiClient.js @@ -1 +1 @@ -export {ApiAiClient} from './es6/ApiAiClient' \ No newline at end of file +export * from './es6/ApiAiClient' \ No newline at end of file diff --git a/ApiAiClient.ts b/ApiAiClient.ts index 3e59589..957841b 100644 --- a/ApiAiClient.ts +++ b/ApiAiClient.ts @@ -1 +1 @@ -export * from './ts/ApiAiClient' \ No newline at end of file +export * from "./ts/ApiAiClient" diff --git a/ApiAiStreamClient.ts b/ApiAiStreamClient.ts index 95792b4..6ec3ef7 100644 --- a/ApiAiStreamClient.ts +++ b/ApiAiStreamClient.ts @@ -1,2 +1,2 @@ /// -export {ApiAiStreamClient} from './ts/ApiAiStreamClient' \ No newline at end of file +export {ApiAiStreamClient} from "./ts/ApiAiStreamClient" diff --git a/README.md b/README.md index c11b1e0..ac026f8 100644 --- a/README.md +++ b/README.md @@ -96,11 +96,11 @@ Code above should work for both TypeScript and simple ES6 *Note:* If you are going to build es5 version of your bundle with ApiAiClient inside, please add some typings for promises (e.g. @types/es6-promise) -You also can import and use all defined interfaces: +You also can import and use all defined interfaces and ApiAiConstants: ```javascript -import {IRequestOptions, IServerResponse} from "api-ai-javascript/ApiAiClient" - +import {IRequestOptions, IServerResponse, ApiAiConstants} from "api-ai-javascript/ApiAiClient" +const lang = ApiAiConstants.AVAILABLE_LANGUAGES.EN; ``` You can find full list of interfaces [here](ts/Interfaces.ts) @@ -124,6 +124,7 @@ You can find full list of interfaces [here](ts/Interfaces.ts) ## 2.0.0-beta.15 * minor fixes, minor readme updates +* exported constants ## 2.0.0-beta.14 * minor fixes diff --git a/es6/ApiAiClient.js b/es6/ApiAiClient.js index efdefd4..2320cbf 100644 --- a/es6/ApiAiClient.js +++ b/es6/ApiAiClient.js @@ -1,18 +1,19 @@ -import Constants from "./Constants"; +import { ApiAiConstants } from "./ApiAiConstants"; import { ApiAiClientConfigurationError } from "./Errors"; import { EventRequest } from "./Request/EventRequest"; import TextRequest from "./Request/TextRequest"; import { TTSRequest } from "./Request/TTSRequest"; export * from "./Interfaces"; +export { ApiAiConstants } from "./ApiAiConstants"; export class ApiAiClient { constructor(options) { if (!options || !options.accessToken) { throw new ApiAiClientConfigurationError("Access token is required for new ApiAi.Client instance"); } this.accessToken = options.accessToken; - this.apiLang = options.lang || Constants.DEFAULT_CLIENT_LANG; - this.apiVersion = options.version || Constants.DEFAULT_API_VERSION; - this.apiBaseUrl = options.baseUrl || Constants.DEFAULT_BASE_URL; + this.apiLang = options.lang || ApiAiConstants.DEFAULT_CLIENT_LANG; + this.apiVersion = options.version || ApiAiConstants.DEFAULT_API_VERSION; + this.apiBaseUrl = options.baseUrl || ApiAiConstants.DEFAULT_BASE_URL; this.sessionId = options.sessionId || this.guid(); this.streamClientClass = options.streamClientClass || null; } @@ -54,13 +55,13 @@ export class ApiAiClient { return this.accessToken; } getApiVersion() { - return (this.apiVersion) ? this.apiVersion : Constants.DEFAULT_API_VERSION; + return (this.apiVersion) ? this.apiVersion : ApiAiConstants.DEFAULT_API_VERSION; } getApiLang() { - return (this.apiLang) ? this.apiLang : Constants.DEFAULT_CLIENT_LANG; + return (this.apiLang) ? this.apiLang : ApiAiConstants.DEFAULT_CLIENT_LANG; } getApiBaseUrl() { - return (this.apiBaseUrl) ? this.apiBaseUrl : Constants.DEFAULT_BASE_URL; + return (this.apiBaseUrl) ? this.apiBaseUrl : ApiAiConstants.DEFAULT_BASE_URL; } setSessionId(sessionId) { this.sessionId = sessionId; diff --git a/es6/Constants.js b/es6/ApiAiConstants.js similarity index 70% rename from es6/Constants.js rename to es6/ApiAiConstants.js index 9e4370a..03c5f55 100644 --- a/es6/Constants.js +++ b/es6/ApiAiConstants.js @@ -1,5 +1,5 @@ -var Constants; -(function (Constants) { +export var ApiAiConstants; +(function (ApiAiConstants) { var AVAILABLE_LANGUAGES; (function (AVAILABLE_LANGUAGES) { AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["EN"] = "en"] = "EN"; @@ -17,12 +17,11 @@ var Constants; AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["PT"] = "pt"] = "PT"; AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["RU"] = "ru"] = "RU"; AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["UK"] = "uk"] = "UK"; - })(AVAILABLE_LANGUAGES = Constants.AVAILABLE_LANGUAGES || (Constants.AVAILABLE_LANGUAGES = {})); - Constants.VERSION = "2.0.0-beta.8"; - Constants.DEFAULT_BASE_URL = "https://api.api.ai/v1/"; - Constants.DEFAULT_API_VERSION = "20150910"; - Constants.DEFAULT_CLIENT_LANG = AVAILABLE_LANGUAGES.EN; + })(AVAILABLE_LANGUAGES = ApiAiConstants.AVAILABLE_LANGUAGES || (ApiAiConstants.AVAILABLE_LANGUAGES = {})); + ApiAiConstants.VERSION = "2.0.0-beta.15"; + ApiAiConstants.DEFAULT_BASE_URL = "https://api.api.ai/v1/"; + ApiAiConstants.DEFAULT_API_VERSION = "20150910"; + ApiAiConstants.DEFAULT_CLIENT_LANG = AVAILABLE_LANGUAGES.EN; // @todo: make configurable, ideally fix non-working v1 - Constants.DEFAULT_TTS_HOST = "https://api.api.ai/api/tts"; -})(Constants || (Constants = {})); -export default Constants; + ApiAiConstants.DEFAULT_TTS_HOST = "https://api.api.ai/api/tts"; +})(ApiAiConstants || (ApiAiConstants = {})); diff --git a/es6/Request/TTSRequest.js b/es6/Request/TTSRequest.js index 2012266..84cebec 100644 --- a/es6/Request/TTSRequest.js +++ b/es6/Request/TTSRequest.js @@ -1,4 +1,4 @@ -import Constants from "../Constants"; +import { ApiAiConstants } from "../ApiAiConstants"; import { ApiAiClientConfigurationError, ApiAiRequestError } from "../Errors"; import XhrRequest from "../XhrRequest"; import Request from "./Request"; @@ -13,7 +13,7 @@ export class TTSRequest extends Request { throw new ApiAiRequestError(reason); }; // this.requestMethod = XhrRequest.Method.GET; - this.uri = Constants.DEFAULT_TTS_HOST; + this.uri = ApiAiConstants.DEFAULT_TTS_HOST; const AudioContext = window.AudioContext || webkitAudioContext; if (!TTSRequest.audioContext) { TTSRequest.audioContext = new AudioContext(); diff --git a/es6/Stream/StreamClient.js b/es6/Stream/StreamClient.js index 98da5a5..823adf2 100644 --- a/es6/Stream/StreamClient.js +++ b/es6/Stream/StreamClient.js @@ -49,9 +49,6 @@ class StreamClient { this.onError(IStreamClient.ERROR.ERR_CLIENT, "No user media support"); } } - getGainNode() { - return this.gainNode; - } startListening() { if (!this.recorder) { this.onError(IStreamClient.ERROR.ERR_AUDIO, "Recorder undefined"); @@ -61,6 +58,10 @@ class StreamClient { this.onError(IStreamClient.ERROR.ERR_AUDIO, "No web socket connection"); return; } + if (this.ws.readyState === WebSocket.CLOSED) { + this.onError(IStreamClient.ERROR.ERR_NETWORK, "WebSocket is in 'closed' state"); + return; + } const isUseVad = (endOfSpeechCallback) => { this.resampleProcessor = this.audioContext.createResampleProcessor(256, 1, 1, 16000); this.mediaStreamSource.connect(this.resampleProcessor); @@ -132,10 +133,7 @@ class StreamClient { this.mediaStreamSource = this.audioContext.createMediaStreamSource(stream); this.onEvent(IStreamClient.EVENT.MSG_MEDIA_STREAM_CREATED, "Media stream created"); this.userSpeechAnalyser = this.audioContext.createAnalyser(); - this.gainNode = this.audioContext.createGain(); this.mediaStreamSource.connect(this.userSpeechAnalyser); - this.mediaStreamSource.connect(this.gainNode); - this.gainNode.connect(this.audioContext.destination); this.recorder = new Recorder(this.mediaStreamSource); this.onEvent(IStreamClient.EVENT.MSG_INIT_RECORDER, "Recorder initialized"); if (onInit) { diff --git a/es6/_build.js b/es6/_build.js index b08d59e..c36a020 100644 --- a/es6/_build.js +++ b/es6/_build.js @@ -1,2 +1,2 @@ -export { ApiAiClient } from "./ApiAiClient"; +export * from "./ApiAiClient"; export { ApiAiStreamClient } from "./ApiAiStreamClient"; diff --git a/package.json b/package.json index de91873..e7483dc 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ }, "scripts": { "start": "webpack-dev-server", - "build": "webpack && webpack --env.streamless && webpack --env.compress && webpack --env.streamless --env.compress && rimraf ./srces6 && tsc -p ./tsconfig.es6src.json", + "build": "webpack && webpack --env.streamless && webpack --env.compress && webpack --env.streamless --env.compress && rimraf ./es6 && tsc -p ./tsconfig.es6.json", "test": "karma start" }, "repository": { diff --git a/spec/Client.spec.ts b/spec/Client.spec.ts index 5fbec67..0481ed1 100644 --- a/spec/Client.spec.ts +++ b/spec/Client.spec.ts @@ -1,5 +1,5 @@ import {ApiAiClient} from "../ts/ApiAiClient"; -import Constants from "../ts/Constants"; +import {ApiAiConstants} from "../ts/ApiAiConstants"; const ACCESS_TOKEN = "AT"; const expect = chai.expect; @@ -23,9 +23,9 @@ describe("ApiAi.Client", () => { }); it("should use valid credentials", () => { - expect(client.getApiLang()).to.eq(Constants.DEFAULT_CLIENT_LANG); - expect(client.getApiVersion()).to.eq(Constants.DEFAULT_API_VERSION); - expect(client.getApiBaseUrl()).to.eq(Constants.DEFAULT_BASE_URL); + expect(client.getApiLang()).to.eq(ApiAiConstants.DEFAULT_CLIENT_LANG); + expect(client.getApiVersion()).to.eq(ApiAiConstants.DEFAULT_API_VERSION); + expect(client.getApiBaseUrl()).to.eq(ApiAiConstants.DEFAULT_BASE_URL); expect(client.getAccessToken()).to.eq(ACCESS_TOKEN); }); @@ -35,13 +35,13 @@ describe("ApiAi.Client", () => { const sessionId = "test"; const innerClient = new ApiAiClient({ accessToken: ACCESS_TOKEN, - lang: Constants.AVAILABLE_LANGUAGES.DE, + lang: ApiAiConstants.AVAILABLE_LANGUAGES.DE, baseUrl, sessionId, version }); - expect(innerClient.getApiLang()).to.eq(Constants.AVAILABLE_LANGUAGES.DE); + expect(innerClient.getApiLang()).to.eq(ApiAiConstants.AVAILABLE_LANGUAGES.DE); expect(innerClient.getApiVersion()).to.eq(version); expect(innerClient.getApiBaseUrl()).to.eq(baseUrl); expect(innerClient.getAccessToken()).to.eq(ACCESS_TOKEN); diff --git a/target/ApiAi.js b/target/ApiAi.js index 750dd55..23a69b4 100644 --- a/target/ApiAi.js +++ b/target/ApiAi.js @@ -73,6 +73,41 @@ var ApiAi = "use strict"; +var ApiAiConstants; +(function (ApiAiConstants) { + var AVAILABLE_LANGUAGES; + (function (AVAILABLE_LANGUAGES) { + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["EN"] = "en"] = "EN"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["DE"] = "de"] = "DE"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ES"] = "es"] = "ES"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["PT_BR"] = "pt-BR"] = "PT_BR"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_HK"] = "zh-HK"] = "ZH_HK"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_CN"] = "zh-CN"] = "ZH_CN"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_TW"] = "zh-TW"] = "ZH_TW"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["NL"] = "nl"] = "NL"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["FR"] = "fr"] = "FR"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["IT"] = "it"] = "IT"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["JA"] = "ja"] = "JA"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["KO"] = "ko"] = "KO"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["PT"] = "pt"] = "PT"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["RU"] = "ru"] = "RU"; + AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["UK"] = "uk"] = "UK"; + })(AVAILABLE_LANGUAGES = ApiAiConstants.AVAILABLE_LANGUAGES || (ApiAiConstants.AVAILABLE_LANGUAGES = {})); + ApiAiConstants.VERSION = "2.0.0-beta.15"; + ApiAiConstants.DEFAULT_BASE_URL = "https://api.api.ai/v1/"; + ApiAiConstants.DEFAULT_API_VERSION = "20150910"; + ApiAiConstants.DEFAULT_CLIENT_LANG = AVAILABLE_LANGUAGES.EN; + // @todo: make configurable, ideally fix non-working v1 + ApiAiConstants.DEFAULT_TTS_HOST = "https://api.api.ai/api/tts"; +})(ApiAiConstants = exports.ApiAiConstants || (exports.ApiAiConstants = {})); + + +/***/ }), +/* 1 */ +/***/ (function(module, exports, __webpack_require__) { + +"use strict"; + var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } @@ -114,12 +149,12 @@ exports.ApiAiRequestError = ApiAiRequestError; /***/ }), -/* 1 */ +/* 2 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; -var Errors_1 = __webpack_require__(0); +var Errors_1 = __webpack_require__(1); var XhrRequest_1 = __webpack_require__(5); var Request = (function () { function Request(apiAiClient, options) { @@ -165,43 +200,6 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = Request; -/***/ }), -/* 2 */ -/***/ (function(module, exports, __webpack_require__) { - -"use strict"; - -var Constants; -(function (Constants) { - var AVAILABLE_LANGUAGES; - (function (AVAILABLE_LANGUAGES) { - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["EN"] = "en"] = "EN"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["DE"] = "de"] = "DE"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ES"] = "es"] = "ES"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["PT_BR"] = "pt-BR"] = "PT_BR"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_HK"] = "zh-HK"] = "ZH_HK"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_CN"] = "zh-CN"] = "ZH_CN"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["ZH_TW"] = "zh-TW"] = "ZH_TW"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["NL"] = "nl"] = "NL"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["FR"] = "fr"] = "FR"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["IT"] = "it"] = "IT"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["JA"] = "ja"] = "JA"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["KO"] = "ko"] = "KO"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["PT"] = "pt"] = "PT"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["RU"] = "ru"] = "RU"; - AVAILABLE_LANGUAGES[AVAILABLE_LANGUAGES["UK"] = "uk"] = "UK"; - })(AVAILABLE_LANGUAGES = Constants.AVAILABLE_LANGUAGES || (Constants.AVAILABLE_LANGUAGES = {})); - Constants.VERSION = "2.0.0-beta.8"; - Constants.DEFAULT_BASE_URL = "https://api.api.ai/v1/"; - Constants.DEFAULT_API_VERSION = "20150910"; - Constants.DEFAULT_CLIENT_LANG = AVAILABLE_LANGUAGES.EN; - // @todo: make configurable, ideally fix non-working v1 - Constants.DEFAULT_TTS_HOST = "https://api.api.ai/api/tts"; -})(Constants || (Constants = {})); -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = Constants; - - /***/ }), /* 3 */ /***/ (function(module, exports, __webpack_require__) { @@ -538,8 +536,10 @@ exports.default = XhrRequest; "use strict"; -var ApiAiClient_1 = __webpack_require__(7); -exports.ApiAiClient = ApiAiClient_1.ApiAiClient; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +__export(__webpack_require__(7)); var ApiAiStreamClient_1 = __webpack_require__(8); exports.ApiAiStreamClient = ApiAiStreamClient_1.ApiAiStreamClient; @@ -553,21 +553,23 @@ exports.ApiAiStreamClient = ApiAiStreamClient_1.ApiAiStreamClient; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } -var Constants_1 = __webpack_require__(2); -var Errors_1 = __webpack_require__(0); +var ApiAiConstants_1 = __webpack_require__(0); +var Errors_1 = __webpack_require__(1); var EventRequest_1 = __webpack_require__(9); var TextRequest_1 = __webpack_require__(11); var TTSRequest_1 = __webpack_require__(10); __export(__webpack_require__(3)); +var ApiAiConstants_2 = __webpack_require__(0); +exports.ApiAiConstants = ApiAiConstants_2.ApiAiConstants; var ApiAiClient = (function () { function ApiAiClient(options) { if (!options || !options.accessToken) { throw new Errors_1.ApiAiClientConfigurationError("Access token is required for new ApiAi.Client instance"); } this.accessToken = options.accessToken; - this.apiLang = options.lang || Constants_1.default.DEFAULT_CLIENT_LANG; - this.apiVersion = options.version || Constants_1.default.DEFAULT_API_VERSION; - this.apiBaseUrl = options.baseUrl || Constants_1.default.DEFAULT_BASE_URL; + this.apiLang = options.lang || ApiAiConstants_1.ApiAiConstants.DEFAULT_CLIENT_LANG; + this.apiVersion = options.version || ApiAiConstants_1.ApiAiConstants.DEFAULT_API_VERSION; + this.apiBaseUrl = options.baseUrl || ApiAiConstants_1.ApiAiConstants.DEFAULT_BASE_URL; this.sessionId = options.sessionId || this.guid(); this.streamClientClass = options.streamClientClass || null; } @@ -613,13 +615,13 @@ var ApiAiClient = (function () { return this.accessToken; }; ApiAiClient.prototype.getApiVersion = function () { - return (this.apiVersion) ? this.apiVersion : Constants_1.default.DEFAULT_API_VERSION; + return (this.apiVersion) ? this.apiVersion : ApiAiConstants_1.ApiAiConstants.DEFAULT_API_VERSION; }; ApiAiClient.prototype.getApiLang = function () { - return (this.apiLang) ? this.apiLang : Constants_1.default.DEFAULT_CLIENT_LANG; + return (this.apiLang) ? this.apiLang : ApiAiConstants_1.ApiAiConstants.DEFAULT_CLIENT_LANG; }; ApiAiClient.prototype.getApiBaseUrl = function () { - return (this.apiBaseUrl) ? this.apiBaseUrl : Constants_1.default.DEFAULT_BASE_URL; + return (this.apiBaseUrl) ? this.apiBaseUrl : ApiAiConstants_1.ApiAiConstants.DEFAULT_BASE_URL; }; ApiAiClient.prototype.setSessionId = function (sessionId) { this.sessionId = sessionId; @@ -689,7 +691,7 @@ var __extends = (this && this.__extends) || function (d, b) { function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; -var Request_1 = __webpack_require__(1); +var Request_1 = __webpack_require__(2); var EventRequest = (function (_super) { __extends(EventRequest, _super); function EventRequest() { @@ -711,10 +713,10 @@ var __extends = (this && this.__extends) || function (d, b) { function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; -var Constants_1 = __webpack_require__(2); -var Errors_1 = __webpack_require__(0); +var ApiAiConstants_1 = __webpack_require__(0); +var Errors_1 = __webpack_require__(1); var XhrRequest_1 = __webpack_require__(5); -var Request_1 = __webpack_require__(1); +var Request_1 = __webpack_require__(2); var TTSRequest = (function (_super) { __extends(TTSRequest, _super); function TTSRequest(apiAiClient, options) { @@ -728,7 +730,7 @@ var TTSRequest = (function (_super) { throw new Errors_1.ApiAiRequestError(reason); }; // this.requestMethod = XhrRequest.Method.GET; - _this.uri = Constants_1.default.DEFAULT_TTS_HOST; + _this.uri = ApiAiConstants_1.ApiAiConstants.DEFAULT_TTS_HOST; var AudioContext = window.AudioContext || webkitAudioContext; if (!TTSRequest.audioContext) { TTSRequest.audioContext = new AudioContext(); @@ -791,7 +793,7 @@ var __extends = (this && this.__extends) || function (d, b) { function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; -var Request_1 = __webpack_require__(1); +var Request_1 = __webpack_require__(2); var TextRequest = (function (_super) { __extends(TextRequest, _super); function TextRequest() { @@ -1154,9 +1156,6 @@ var StreamClient = (function () { this.onError(Interfaces_1.IStreamClient.ERROR.ERR_CLIENT, "No user media support"); } }; - StreamClient.prototype.getGainNode = function () { - return this.gainNode; - }; StreamClient.prototype.startListening = function () { var _this = this; if (!this.recorder) { @@ -1167,6 +1166,10 @@ var StreamClient = (function () { this.onError(Interfaces_1.IStreamClient.ERROR.ERR_AUDIO, "No web socket connection"); return; } + if (this.ws.readyState === WebSocket.CLOSED) { + this.onError(Interfaces_1.IStreamClient.ERROR.ERR_NETWORK, "WebSocket is in 'closed' state"); + return; + } var isUseVad = function (endOfSpeechCallback) { _this.resampleProcessor = _this.audioContext.createResampleProcessor(256, 1, 1, 16000); _this.mediaStreamSource.connect(_this.resampleProcessor); @@ -1239,10 +1242,7 @@ var StreamClient = (function () { this.mediaStreamSource = this.audioContext.createMediaStreamSource(stream); this.onEvent(Interfaces_1.IStreamClient.EVENT.MSG_MEDIA_STREAM_CREATED, "Media stream created"); this.userSpeechAnalyser = this.audioContext.createAnalyser(); - this.gainNode = this.audioContext.createGain(); this.mediaStreamSource.connect(this.userSpeechAnalyser); - this.mediaStreamSource.connect(this.gainNode); - this.gainNode.connect(this.audioContext.destination); this.recorder = new Recorder_1.default(this.mediaStreamSource); this.onEvent(Interfaces_1.IStreamClient.EVENT.MSG_INIT_RECORDER, "Recorder initialized"); if (onInit) { diff --git a/target/ApiAi.min.js b/target/ApiAi.min.js index 2bb6274..47749f3 100644 --- a/target/ApiAi.min.js +++ b/target/ApiAi.min.js @@ -1 +1 @@ -var ApiAi=function(e){function __webpack_require__(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,__webpack_require__),i.l=!0,i.exports}var t={};return __webpack_require__.m=e,__webpack_require__.c=t,__webpack_require__.i=function(e){return e},__webpack_require__.d=function(e,t,n){__webpack_require__.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:n})},__webpack_require__.n=function(e){var t=e&&e.__esModule?function getDefault(){return e.default}:function getModuleExports(){return e};return __webpack_require__.d(t,"a",t),t},__webpack_require__.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},__webpack_require__.p="/target/",__webpack_require__(__webpack_require__.s=17)}([function(e,t,n){"use strict";var i=this&&this.__extends||function(e,t){function __(){this.constructor=e}for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);e.prototype=null===t?Object.create(t):(__.prototype=t.prototype,new __)},r=function(e){function ApiAiBaseError(t){var n=e.call(this,t)||this;return n.message=t,n.stack=(new Error).stack,n}return i(ApiAiBaseError,e),ApiAiBaseError}(Error),o=function(e){function ApiAiClientConfigurationError(t){var n=e.call(this,t)||this;return n.name="ApiAiClientConfigurationError",n}return i(ApiAiClientConfigurationError,e),ApiAiClientConfigurationError}(r);t.ApiAiClientConfigurationError=o;var s=function(e){function ApiAiRequestError(t,n){void 0===n&&(n=null);var i=e.call(this,t)||this;return i.message=t,i.code=n,i.name="ApiAiRequestError",i}return i(ApiAiRequestError,e),ApiAiRequestError}(r);t.ApiAiRequestError=s},function(e,t,n){"use strict";var i=n(0),r=n(5),o=function(){function Request(e,t){this.apiAiClient=e,this.options=t,this.uri=this.apiAiClient.getApiBaseUrl()+"query?v="+this.apiAiClient.getApiVersion(),this.requestMethod=r.default.Method.POST,this.headers={Authorization:"Bearer "+this.apiAiClient.getAccessToken()},this.options.lang=this.apiAiClient.getApiLang(),this.options.sessionId=this.apiAiClient.getSessionId()}return Request.handleSuccess=function(e){return Promise.resolve(JSON.parse(e.responseText))},Request.handleError=function(e){var t=null;try{var n=JSON.parse(e.responseText);t=n.status&&n.status.errorDetails?new i.ApiAiRequestError(n.status.errorDetails,n.status.code):new i.ApiAiRequestError(e.statusText,e.status)}catch(n){t=new i.ApiAiRequestError(e.statusText,e.status)}return Promise.reject(t)},Request.prototype.perform=function(e){void 0===e&&(e=null);var t=e?e:this.options;return r.default.ajax(this.requestMethod,this.uri,t,this.headers).then(Request.handleSuccess.bind(this)).catch(Request.handleError.bind(this))},Request}();Object.defineProperty(t,"__esModule",{value:!0}),t.default=o},function(e,t,n){"use strict";var i;!function(e){var t;!function(e){e[e.EN="en"]="EN",e[e.DE="de"]="DE",e[e.ES="es"]="ES",e[e.PT_BR="pt-BR"]="PT_BR",e[e.ZH_HK="zh-HK"]="ZH_HK",e[e.ZH_CN="zh-CN"]="ZH_CN",e[e.ZH_TW="zh-TW"]="ZH_TW",e[e.NL="nl"]="NL",e[e.FR="fr"]="FR",e[e.IT="it"]="IT",e[e.JA="ja"]="JA",e[e.KO="ko"]="KO",e[e.PT="pt"]="PT",e[e.RU="ru"]="RU",e[e.UK="uk"]="UK"}(t=e.AVAILABLE_LANGUAGES||(e.AVAILABLE_LANGUAGES={})),e.VERSION="2.0.0-beta.8",e.DEFAULT_BASE_URL="https://api.api.ai/v1/",e.DEFAULT_API_VERSION="20150910",e.DEFAULT_CLIENT_LANG=t.EN,e.DEFAULT_TTS_HOST="https://api.api.ai/api/tts"}(i||(i={})),Object.defineProperty(t,"__esModule",{value:!0}),t.default=i},function(e,t,n){"use strict";var i;!function(e){var t;!function(e){e[e.ERR_NETWORK=0]="ERR_NETWORK",e[e.ERR_AUDIO=1]="ERR_AUDIO",e[e.ERR_SERVER=2]="ERR_SERVER",e[e.ERR_CLIENT=3]="ERR_CLIENT"}(t=e.ERROR||(e.ERROR={}));var n;!function(e){e[e.MSG_WAITING_MICROPHONE=0]="MSG_WAITING_MICROPHONE",e[e.MSG_MEDIA_STREAM_CREATED=1]="MSG_MEDIA_STREAM_CREATED",e[e.MSG_INIT_RECORDER=2]="MSG_INIT_RECORDER",e[e.MSG_RECORDING=3]="MSG_RECORDING",e[e.MSG_SEND=4]="MSG_SEND",e[e.MSG_SEND_EMPTY=5]="MSG_SEND_EMPTY",e[e.MSG_SEND_EOS_OR_JSON=6]="MSG_SEND_EOS_OR_JSON",e[e.MSG_WEB_SOCKET=7]="MSG_WEB_SOCKET",e[e.MSG_WEB_SOCKET_OPEN=8]="MSG_WEB_SOCKET_OPEN",e[e.MSG_WEB_SOCKET_CLOSE=9]="MSG_WEB_SOCKET_CLOSE",e[e.MSG_STOP=10]="MSG_STOP",e[e.MSG_CONFIG_CHANGED=11]="MSG_CONFIG_CHANGED"}(n=e.EVENT||(e.EVENT={}))}(i=t.IStreamClient||(t.IStreamClient={}))},function(e,t,n){"use strict";function _resamplerJs(){function Resampler(e,t,n,i,r){this.fromSampleRate=e,this.toSampleRate=t,this.channels=0|n,this.outputBufferSize=i,this.noReturn=!!r,this.initialize()}Resampler.prototype.initialize=function(){if(this.fromSampleRate<=0||this.toSampleRate<=0||this.channels<=0)throw new Error("Invalid settings specified for the resampler.");this.fromSampleRate==this.toSampleRate?(this.resampler=this.bypassResampler,this.ratioWeight=1):(this.resampler=function(e){var t=Math.min(e.length,this.outputBufferSize);if(t%this.channels!=0)throw new Error("Buffer was of incorrect sample length.");if(t<=0)return this.noReturn?0:[];for(var n=0,i=new Array(this.channels),r=0;r0&&o=s)){for(r=0;r0?r:0)]*n;h+=n,n=0;break}for(r=0;r=200&&a.status<300?o(a):s(a)},a.onerror=function(){s(a)}})},XhrRequest.get=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.GET,e,t,n,i)},XhrRequest.post=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.POST,e,t,n,i)},XhrRequest.put=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.PUT,e,t,n,i)},XhrRequest.delete=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.DELETE,e,t,n,i)},XhrRequest.createXMLHTTPObject=function(){for(var e=null,t=0,n=XhrRequest.XMLHttpFactories;tthis.chunkSize;)e=this.array_data.splice(0,this.chunkSize),this.callback&&this.callback(e)},MagicBuffer.prototype.drop=function(){this.array_data.splice(0,this.array_data.length)},AudioContext.prototype.createEndOfSpeechProcessor=function(e){var t=this.createScriptProcessor(e,1,1);t.endOfSpeechCallback=null;var n=new r.default;t.vad=n;var i=new MagicBuffer(160);return i.callback=function(e){var r=n.process(e);"CONTINUE"!==r&&t.endOfSpeechCallback&&(t.endOfSpeechCallback(),i.drop())},t.onaudioprocess=function(e){var t=e.inputBuffer.getChannelData(0),n=e.outputBuffer.getChannelData(0);i.push(t);for(var r=0;r0&&i>0?t.substring(n+1,i):t},Recorder}();Object.defineProperty(t,"__esModule",{value:!0}),t.default=o},function(e,t,n){"use strict";var i=function(){function RecorderWorker(){}return RecorderWorker.createRecorderWorker=function(){return function _recorderWorkerJs(){function init(n){new Function(n.resamplerInitializerBody)(),e=n.sampleRate,t=new navigator.Resampler(e,16e3,1,51200,!1)}function record(e){i.push(e[0]),n+=e[0].length}function export16kMono(e){var r=mergeBuffers(i,n),o=t.resampler(r),s=encodeRAW(o),a=new Blob([s],{type:e});this.postMessage(a)}function getBuffer(){var e=[];e.push(mergeBuffers(i,n)),this.postMessage(e)}function clear(){n=0,i=[]}function mergeBuffers(e,t){for(var n=new Float32Array(t),i=0,r=0;r0?(this.ws.send(e),this.onEvent(i.IStreamClient.EVENT.MSG_SEND,"Send: blob: "+e.type+", "+e.size)):this.onEvent(i.IStreamClient.EVENT.MSG_SEND_EMPTY,"Send: blob: "+e.type+", EMPTY"):(this.ws.send(e),this.onEvent(i.IStreamClient.EVENT.MSG_SEND_EOS_OR_JSON,"Send string: "+e))},StreamClient}();s.CONTENT_TYPE="content-type=audio/x-raw,+layout=(string)interleaved,+rate=(int)16000,+format=(string)S16LE,+channels=(int)1",s.INTERVAL=250,s.TAG_END_OF_SENTENCE="EOS",Object.defineProperty(t,"__esModule",{value:!0}),t.default=s},function(e,t,n){"use strict";var i=function(){function VAD(){this.reset()}return VAD.prototype.process=function(e){var t=this.frameActive(e);if(this.time=160*this.frameNumber/16e3,t)this.lastActiveTime>=0&&this.time-this.lastActiveTime=this.minSequenceCount&&(this.lastSequenceTime=this.time,this.silenceLengthMilis=Math.max(this.minSilenceLengthMilis,this.silenceLengthMilis-(this.maxSilenceLengthMilis-this.minSilenceLengthMilis)/4))):this.sequenceCounter=1,this.lastSequenceTime=this.time;else if(this.time-this.lastSequenceTime>this.silenceLengthMilis)return this.lastSequenceTime>0?"TERMINATE":"NO_SPEECH";return"CONTINUE"},VAD.prototype.frameActive=function(e){for(var t=0,n=0,i=0,r=e.length,o=0;o0?1:-1,0!=i&&s!=i&&n++,i=s}this.frameNumber+=1;var a=!1;return this.frameNumber0&&o=s)){for(r=0;r0?r:0)]*n;h+=n,n=0;break}for(r=0;r=200&&a.status<300?o(a):s(a)},a.onerror=function(){s(a)}})},XhrRequest.get=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.GET,e,t,n,i)},XhrRequest.post=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.POST,e,t,n,i)},XhrRequest.put=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.PUT,e,t,n,i)},XhrRequest.delete=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.DELETE,e,t,n,i)},XhrRequest.createXMLHTTPObject=function(){for(var e=null,t=0,n=XhrRequest.XMLHttpFactories;tthis.chunkSize;)e=this.array_data.splice(0,this.chunkSize),this.callback&&this.callback(e)},MagicBuffer.prototype.drop=function(){this.array_data.splice(0,this.array_data.length)},AudioContext.prototype.createEndOfSpeechProcessor=function(e){var t=this.createScriptProcessor(e,1,1);t.endOfSpeechCallback=null;var n=new r.default;t.vad=n;var i=new MagicBuffer(160);return i.callback=function(e){var r=n.process(e);"CONTINUE"!==r&&t.endOfSpeechCallback&&(t.endOfSpeechCallback(),i.drop())},t.onaudioprocess=function(e){var t=e.inputBuffer.getChannelData(0),n=e.outputBuffer.getChannelData(0);i.push(t);for(var r=0;r0&&i>0?t.substring(n+1,i):t},Recorder}();Object.defineProperty(t,"__esModule",{value:!0}),t.default=o},function(e,t,n){"use strict";var i=function(){function RecorderWorker(){}return RecorderWorker.createRecorderWorker=function(){return function _recorderWorkerJs(){function init(n){new Function(n.resamplerInitializerBody)(),e=n.sampleRate,t=new navigator.Resampler(e,16e3,1,51200,!1)}function record(e){i.push(e[0]),n+=e[0].length}function export16kMono(e){var r=mergeBuffers(i,n),o=t.resampler(r),s=encodeRAW(o),a=new Blob([s],{type:e});this.postMessage(a)}function getBuffer(){var e=[];e.push(mergeBuffers(i,n)),this.postMessage(e)}function clear(){n=0,i=[]}function mergeBuffers(e,t){for(var n=new Float32Array(t),i=0,r=0;r0?(this.ws.send(e),this.onEvent(i.IStreamClient.EVENT.MSG_SEND,"Send: blob: "+e.type+", "+e.size)):this.onEvent(i.IStreamClient.EVENT.MSG_SEND_EMPTY,"Send: blob: "+e.type+", EMPTY"):(this.ws.send(e),this.onEvent(i.IStreamClient.EVENT.MSG_SEND_EOS_OR_JSON,"Send string: "+e))},StreamClient}();s.CONTENT_TYPE="content-type=audio/x-raw,+layout=(string)interleaved,+rate=(int)16000,+format=(string)S16LE,+channels=(int)1",s.INTERVAL=250,s.TAG_END_OF_SENTENCE="EOS",Object.defineProperty(t,"__esModule",{value:!0}),t.default=s},function(e,t,n){"use strict";var i=function(){function VAD(){this.reset()}return VAD.prototype.process=function(e){var t=this.frameActive(e);if(this.time=160*this.frameNumber/16e3,t)this.lastActiveTime>=0&&this.time-this.lastActiveTime=this.minSequenceCount&&(this.lastSequenceTime=this.time,this.silenceLengthMilis=Math.max(this.minSilenceLengthMilis,this.silenceLengthMilis-(this.maxSilenceLengthMilis-this.minSilenceLengthMilis)/4))):this.sequenceCounter=1,this.lastSequenceTime=this.time;else if(this.time-this.lastSequenceTime>this.silenceLengthMilis)return this.lastSequenceTime>0?"TERMINATE":"NO_SPEECH";return"CONTINUE"},VAD.prototype.frameActive=function(e){for(var t=0,n=0,i=0,r=e.length,o=0;o0?1:-1,0!=i&&s!=i&&n++,i=s}this.frameNumber+=1;var a=!1;return this.frameNumber=200&&s.status<300?o(s):u(s)},s.onerror=function(){u(s)}})},XhrRequest.get=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.GET,e,t,n,i)},XhrRequest.post=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.POST,e,t,n,i)},XhrRequest.put=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.PUT,e,t,n,i)},XhrRequest.delete=function(e,t,n,i){return void 0===t&&(t=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.DELETE,e,t,n,i)},XhrRequest.createXMLHTTPObject=function(){for(var e=null,t=0,n=XhrRequest.XMLHttpFactories;t=200&&u.status<300?o(u):s(u)},u.onerror=function(){s(u)}})},XhrRequest.get=function(t,e,n,i){return void 0===e&&(e=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.GET,t,e,n,i)},XhrRequest.post=function(t,e,n,i){return void 0===e&&(e=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.POST,t,e,n,i)},XhrRequest.put=function(t,e,n,i){return void 0===e&&(e=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.PUT,t,e,n,i)},XhrRequest.delete=function(t,e,n,i){return void 0===e&&(e=null),void 0===n&&(n=null),void 0===i&&(i={}),XhrRequest.ajax(XhrRequest.Method.DELETE,t,e,n,i)},XhrRequest.createXMLHTTPObject=function(){for(var t=null,e=0,n=XhrRequest.XMLHttpFactories;e void; diff --git a/ts/Request/LocalTTSRequest.ts b/ts/Request/LocalTTSRequest.ts index 99afb65..4a82a32 100644 --- a/ts/Request/LocalTTSRequest.ts +++ b/ts/Request/LocalTTSRequest.ts @@ -5,4 +5,4 @@ export class LocalTTSRequest { private getLanguage() { } -} \ No newline at end of file +} diff --git a/ts/Request/TTSRequest.ts b/ts/Request/TTSRequest.ts index 62e4396..dde3cff 100644 --- a/ts/Request/TTSRequest.ts +++ b/ts/Request/TTSRequest.ts @@ -1,5 +1,5 @@ import {ApiAiClient} from "../ApiAiClient"; -import Constants from "../Constants"; +import {ApiAiConstants} from "../ApiAiConstants"; import {ApiAiClientConfigurationError, ApiAiRequestError} from "../Errors"; import {IRequestOptions} from "../Interfaces"; import XhrRequest from "../XhrRequest"; @@ -14,7 +14,7 @@ export class TTSRequest extends Request { constructor(protected apiAiClient: ApiAiClient, options: IRequestOptions = {}) { super(apiAiClient, options); // this.requestMethod = XhrRequest.Method.GET; - this.uri = Constants.DEFAULT_TTS_HOST; + this.uri = ApiAiConstants.DEFAULT_TTS_HOST; const AudioContext = window.AudioContext || webkitAudioContext; if (!TTSRequest.audioContext) { diff --git a/ts/Request/VoiceRequest.ts b/ts/Request/VoiceRequest.ts index 101593e..c789d63 100644 --- a/ts/Request/VoiceRequest.ts +++ b/ts/Request/VoiceRequest.ts @@ -1,4 +1,4 @@ -import {Client} from "../Client"; +import {ApiAiClient} from "../ApiAiClient"; import {IRequestOptions} from "../Interfaces"; import Request from "./Request"; @@ -7,7 +7,7 @@ import Request from "./Request"; */ class VoiceRequest extends Request { - constructor(client: Client, options: IRequestOptions = {}) { + constructor(client: ApiAiClient, options: IRequestOptions = {}) { super(client, options); } diff --git a/ts/_build.streamless.ts b/ts/_build.streamless.ts index 2ab2cd9..0377eac 100644 --- a/ts/_build.streamless.ts +++ b/ts/_build.streamless.ts @@ -1 +1 @@ -export {ApiAiClient} from "./ApiAiClient"; \ No newline at end of file +export * from "./ApiAiClient"; diff --git a/ts/_build.ts b/ts/_build.ts index 270a767..1597828 100644 --- a/ts/_build.ts +++ b/ts/_build.ts @@ -1,2 +1,2 @@ -export {ApiAiClient} from "./ApiAiClient"; +export * from "./ApiAiClient"; export {ApiAiStreamClient} from "./ApiAiStreamClient"; diff --git a/tsconfig.es6src.json b/tsconfig.es6.json similarity index 100% rename from tsconfig.es6src.json rename to tsconfig.es6.json