zedai-voice
Version:
Siri-like voice banking SDK for React Native - Complete voice-powered fintech solution with wake word detection and beautiful UI
1,070 lines (1,035 loc) • 45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _ConfigManager = _interopRequireDefault(require("./ConfigManager"));
var _AudioManager = _interopRequireDefault(require("./AudioManager"));
var _SpeechToText = _interopRequireDefault(require("../services/SpeechToText"));
var _IntentParser = _interopRequireDefault(require("./IntentParser"));
var _TextToSpeech = _interopRequireDefault(require("../services/TextToSpeech"));
var _APIClient = _interopRequireDefault(require("../services/APIClient"));
var _WebhookManager = _interopRequireDefault(require("../services/WebhookManager"));
var _FlowManager = _interopRequireDefault(require("../flows/FlowManager"));
var _SecurityManager = _interopRequireDefault(require("../security/SecurityManager"));
var _Logger = _interopRequireDefault(require("../utils/Logger"));
var _Constants = require("../utils/Constants");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t1 in e) "default" !== _t1 && {}.hasOwnProperty.call(e, _t1) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t1)) && (i.get || i.set) ? o(f, _t1, i) : f[_t1] = e[_t1]); return f; })(e, t); }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // src/core/VoiceEngine.js
var VoiceEngine = /*#__PURE__*/function () {
function VoiceEngine(config) {
_classCallCheck(this, VoiceEngine);
try {
// Initialize configuration
this.configManager = new _ConfigManager["default"](config);
this.config = this.configManager.getConfig();
// Initialize security
this.securityManager = new _SecurityManager["default"](this.config.security);
// Initialize services
this.audioManager = new _AudioManager["default"](this.config.audio);
this.speechToText = new _SpeechToText["default"](this.config.openai);
this.intentParser = new _IntentParser["default"](this.config.openai);
this.textToSpeech = new _TextToSpeech["default"]({
language: 'en-US',
rate: 0.5,
pitch: 1.0
});
this.apiClient = new _APIClient["default"](this.config, this.securityManager);
this.webhookManager = new _WebhookManager["default"](this.config, this.securityManager);
this.flowManager = new _FlowManager["default"](this.apiClient, this.webhookManager, this.configManager, this.securityManager);
// State management
this.state = _Constants.VOICE_STATES.IDLE;
this.currentSession = null;
this.isInitialized = false;
this.listeners = new Map();
// Performance tracking
this.metrics = {
totalInteractions: 0,
successfulTransfers: 0,
failedTransfers: 0,
averageResponseTime: 0,
lastInteractionTime: null
};
_Logger["default"].info('VoiceEngine created successfully');
} catch (error) {
_Logger["default"].error('VoiceEngine creation failed', {
error: error.message
});
throw error;
}
}
return _createClass(VoiceEngine, [{
key: "initialize",
value: function () {
var _initialize = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
var AutoDeploymentManager, deploymentResult, _t;
return _regenerator().w(function (_context) {
while (1) switch (_context.n) {
case 0:
_context.p = 0;
_Logger["default"].info('Initializing VoiceEngine...');
// Import auto-deployment manager
_context.n = 1;
return Promise.resolve().then(function () {
return _interopRequireWildcard(require('../core/AutoDeploymentManager'));
});
case 1:
AutoDeploymentManager = _context.v["default"];
// Create auto-deployment manager
this.autoDeployment = new AutoDeploymentManager(this, null, this.config);
// Auto-deploy with permissions and UI setup
_context.n = 2;
return this.autoDeployment.autoDeploy();
case 2:
deploymentResult = _context.v;
if (!deploymentResult.success) {
_Logger["default"].warn('Auto-deployment had issues', {
reason: deploymentResult.reason
});
// Continue with basic initialization even if deployment has issues
}
// Test connections
_context.n = 3;
return this.testConnections();
case 3:
// Setup event listeners
this.setupEventListeners();
// Initialize audio manager
_context.n = 4;
return this.audioManager.initializeAudio();
case 4:
this.isInitialized = true;
this.setState(_Constants.VOICE_STATES.IDLE);
_Logger["default"].info('VoiceEngine initialized successfully');
this.emit('initialized', {
success: true,
deployment: deploymentResult
});
return _context.a(2, {
success: true,
deployment: deploymentResult
});
case 5:
_context.p = 5;
_t = _context.v;
_Logger["default"].error('VoiceEngine initialization failed', {
error: _t.message
});
this.emit('error', {
error: _t.message,
code: _Constants.ERROR_CODES.INVALID_CONFIG
});
throw _t;
case 6:
return _context.a(2);
}
}, _callee, this, [[0, 5]]);
}));
function initialize() {
return _initialize.apply(this, arguments);
}
return initialize;
}()
}, {
key: "testConnections",
value: function () {
var _testConnections = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
var tests, results;
return _regenerator().w(function (_context2) {
while (1) switch (_context2.n) {
case 0:
_Logger["default"].info('Testing service connections...');
_context2.n = 1;
return Promise.allSettled([this.speechToText.testConnection(), this.intentParser.testConnection(), this.apiClient.testConnection(), this.webhookManager.testWebhook()]);
case 1:
tests = _context2.v;
results = {
speechToText: tests[0].status === 'fulfilled' ? tests[0].value : {
connected: false,
error: tests[0].reason
},
intentParser: tests[1].status === 'fulfilled' ? tests[1].value : {
connected: false,
error: tests[1].reason
},
apiClient: tests[2].status === 'fulfilled' ? tests[2].value : {
connected: false,
error: tests[2].reason
},
webhook: tests[3].status === 'fulfilled' ? tests[3].value : {
connected: false,
error: tests[3].reason
}
};
_Logger["default"].info('Connection test results', results);
// Emit connection status
this.emit('connectionTest', results);
return _context2.a(2, results);
}
}, _callee2, this);
}));
function testConnections() {
return _testConnections.apply(this, arguments);
}
return testConnections;
}()
}, {
key: "setupEventListeners",
value: function setupEventListeners() {
// Audio manager events
this.audioManager.on('speechResults', this.handleSpeechResults.bind(this));
this.audioManager.on('speechError', this.handleSpeechError.bind(this));
this.audioManager.on('speechStart', this.handleSpeechStart.bind(this));
this.audioManager.on('speechEnd', this.handleSpeechEnd.bind(this));
this.audioManager.on('stateChanged', this.handleAudioStateChange.bind(this));
// TTS events
this.textToSpeech.on('start', this.handleTTSStart.bind(this));
this.textToSpeech.on('finish', this.handleTTSFinish.bind(this));
this.textToSpeech.on('cancel', this.handleTTSCancel.bind(this));
_Logger["default"].debug('Event listeners setup complete');
}
// Main public methods
}, {
key: "startListening",
value: function () {
var _startListening = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
var _t2;
return _regenerator().w(function (_context3) {
while (1) switch (_context3.n) {
case 0:
_context3.p = 0;
if (this.isInitialized) {
_context3.n = 1;
break;
}
_context3.n = 1;
return this.initialize();
case 1:
if (!(this.state === _Constants.VOICE_STATES.LISTENING)) {
_context3.n = 2;
break;
}
_Logger["default"].warn('Already listening');
return _context3.a(2, {
success: false,
error: 'Already listening'
});
case 2:
_Logger["default"].info('Starting voice listening...');
// Create session if needed
if (!this.currentSession) {
this.currentSession = this.securityManager.createSession('voice_user');
}
// Start audio recording
_context3.n = 3;
return this.audioManager.startListening();
case 3:
this.setState(_Constants.VOICE_STATES.LISTENING);
this.emit('listeningStarted', {
sessionId: this.currentSession
});
return _context3.a(2, {
success: true,
sessionId: this.currentSession
});
case 4:
_context3.p = 4;
_t2 = _context3.v;
_Logger["default"].error('Failed to start listening', {
error: _t2.message
});
this.setState(_Constants.VOICE_STATES.ERROR);
this.emit('error', {
error: _t2.message,
code: _Constants.ERROR_CODES.SPEECH_RECOGNITION_FAILED
});
return _context3.a(2, {
success: false,
error: _t2.message
});
}
}, _callee3, this, [[0, 4]]);
}));
function startListening() {
return _startListening.apply(this, arguments);
}
return startListening;
}()
}, {
key: "stopListening",
value: function () {
var _stopListening = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
var _t3;
return _regenerator().w(function (_context4) {
while (1) switch (_context4.n) {
case 0:
_context4.p = 0;
if (!(this.state !== _Constants.VOICE_STATES.LISTENING)) {
_context4.n = 1;
break;
}
_Logger["default"].warn('Not currently listening');
return _context4.a(2, {
success: false,
error: 'Not listening'
});
case 1:
_Logger["default"].info('Stopping voice listening...');
_context4.n = 2;
return this.audioManager.stopListening();
case 2:
this.setState(_Constants.VOICE_STATES.PROCESSING);
this.emit('listeningStopped', {
sessionId: this.currentSession
});
return _context4.a(2, {
success: true
});
case 3:
_context4.p = 3;
_t3 = _context4.v;
_Logger["default"].error('Failed to stop listening', {
error: _t3.message
});
this.setState(_Constants.VOICE_STATES.ERROR);
return _context4.a(2, {
success: false,
error: _t3.message
});
}
}, _callee4, this, [[0, 3]]);
}));
function stopListening() {
return _stopListening.apply(this, arguments);
}
return stopListening;
}()
}, {
key: "speak",
value: function () {
var _speak = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(text) {
var options,
_args5 = arguments,
_t4;
return _regenerator().w(function (_context5) {
while (1) switch (_context5.n) {
case 0:
options = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
_context5.p = 1;
if (!(!text || typeof text !== 'string')) {
_context5.n = 2;
break;
}
throw new Error('Text is required for speech');
case 2:
_Logger["default"].info('Speaking response', {
text: text.substring(0, 100) + '...'
});
this.setState(_Constants.VOICE_STATES.SPEAKING);
this.emit('speakingStarted', {
text: text,
sessionId: this.currentSession
});
_context5.n = 3;
return this.textToSpeech.speak(text, options);
case 3:
return _context5.a(2, {
success: true
});
case 4:
_context5.p = 4;
_t4 = _context5.v;
_Logger["default"].error('Speech failed', {
error: _t4.message
});
this.setState(_Constants.VOICE_STATES.ERROR);
this.emit('error', {
error: _t4.message,
code: 'SPEECH_FAILED'
});
return _context5.a(2, {
success: false,
error: _t4.message
});
}
}, _callee5, this, [[1, 4]]);
}));
function speak(_x) {
return _speak.apply(this, arguments);
}
return speak;
}()
}, {
key: "processVoiceCommand",
value: function () {
var _processVoiceCommand = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(text) {
var startTime, intentResult, flowResult, responseTime, errorResponse, _t5;
return _regenerator().w(function (_context6) {
while (1) switch (_context6.n) {
case 0:
_context6.p = 0;
if (!this.currentSession) {
this.currentSession = this.securityManager.createSession('voice_user');
}
startTime = Date.now();
_Logger["default"].info('Processing voice command', {
text: text.substring(0, 100) + '...',
sessionId: this.currentSession
});
this.setState(_Constants.VOICE_STATES.PROCESSING);
this.emit('processingStarted', {
text: text,
sessionId: this.currentSession
});
// Parse intent
_context6.n = 1;
return this.intentParser.parseIntent(text, {
currentFlow: this.flowManager.getActiveFlowType(this.currentSession),
sessionId: this.currentSession
});
case 1:
intentResult = _context6.v;
_Logger["default"].info('Intent parsed', {
intent: intentResult.intent,
confidence: intentResult.confidence,
entities: Object.keys(intentResult.entities)
});
// Handle intent through flow manager
_context6.n = 2;
return this.flowManager.handleIntent(this.currentSession, intentResult.intent, intentResult.entities);
case 2:
flowResult = _context6.v;
if (!flowResult.response) {
_context6.n = 3;
break;
}
_context6.n = 3;
return this.speak(flowResult.response);
case 3:
// Update metrics
responseTime = Date.now() - startTime;
this.updateMetrics(responseTime, flowResult);
this.setState(_Constants.VOICE_STATES.IDLE);
this.emit('processingCompleted', _objectSpread(_objectSpread({}, flowResult), {}, {
responseTime: responseTime,
sessionId: this.currentSession
}));
return _context6.a(2, _objectSpread(_objectSpread({
success: true
}, flowResult), {}, {
responseTime: responseTime,
intentResult: intentResult
}));
case 4:
_context6.p = 4;
_t5 = _context6.v;
_Logger["default"].error('Voice command processing failed', {
error: _t5.message,
text: text === null || text === void 0 ? void 0 : text.substring(0, 50)
});
this.setState(_Constants.VOICE_STATES.ERROR);
errorResponse = this.configManager.getResponse('networkError');
_context6.n = 5;
return this.speak(errorResponse)["catch"](function () {});
case 5:
// Don't fail if speech fails
this.emit('error', {
error: _t5.message,
code: _Constants.ERROR_CODES.INTENT_PARSING_FAILED,
sessionId: this.currentSession
});
return _context6.a(2, {
success: false,
error: _t5.message,
response: errorResponse
});
}
}, _callee6, this, [[0, 4]]);
}));
function processVoiceCommand(_x2) {
return _processVoiceCommand.apply(this, arguments);
}
return processVoiceCommand;
}() // Event handlers
}, {
key: "handleSpeechResults",
value: function () {
var _handleSpeechResults = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(event) {
var _t6;
return _regenerator().w(function (_context7) {
while (1) switch (_context7.n) {
case 0:
_context7.p = 0;
_Logger["default"].debug('Speech results received', {
text: event.text
});
// Process the transcribed text
_context7.n = 1;
return this.processVoiceCommand(event.text);
case 1:
_context7.n = 3;
break;
case 2:
_context7.p = 2;
_t6 = _context7.v;
_Logger["default"].error('Speech results handling failed', {
error: _t6.message
});
this.handleError(_t6);
case 3:
return _context7.a(2);
}
}, _callee7, this, [[0, 2]]);
}));
function handleSpeechResults(_x3) {
return _handleSpeechResults.apply(this, arguments);
}
return handleSpeechResults;
}()
}, {
key: "handleSpeechError",
value: function handleSpeechError(event) {
_Logger["default"].error('Speech recognition error', event);
this.setState(_Constants.VOICE_STATES.ERROR);
var errorResponse = this.configManager.getResponse('audioError');
this.speak(errorResponse)["catch"](function () {});
this.emit('speechError', {
error: event.error,
sessionId: this.currentSession
});
}
}, {
key: "handleSpeechStart",
value: function handleSpeechStart(event) {
_Logger["default"].debug('Speech started');
this.emit('speechStarted', {
sessionId: this.currentSession
});
}
}, {
key: "handleSpeechEnd",
value: function handleSpeechEnd(event) {
_Logger["default"].debug('Speech ended');
this.emit('speechEnded', {
sessionId: this.currentSession
});
}
}, {
key: "handleAudioStateChange",
value: function handleAudioStateChange(event) {
_Logger["default"].debug('Audio state changed', event);
this.emit('audioStateChanged', _objectSpread(_objectSpread({}, event), {}, {
sessionId: this.currentSession
}));
}
}, {
key: "handleTTSStart",
value: function handleTTSStart(event) {
_Logger["default"].debug('TTS started');
this.emit('speechStarted', {
sessionId: this.currentSession
});
}
}, {
key: "handleTTSFinish",
value: function handleTTSFinish(event) {
_Logger["default"].debug('TTS finished');
this.setState(_Constants.VOICE_STATES.IDLE);
this.emit('speechFinished', {
sessionId: this.currentSession
});
}
}, {
key: "handleTTSCancel",
value: function handleTTSCancel(event) {
_Logger["default"].debug('TTS cancelled');
this.setState(_Constants.VOICE_STATES.IDLE);
this.emit('speechCancelled', {
sessionId: this.currentSession
});
}
}, {
key: "handleError",
value: function handleError(error) {
_Logger["default"].error('VoiceEngine error', {
error: error.message
});
this.setState(_Constants.VOICE_STATES.ERROR);
this.emit('error', {
error: error.message,
sessionId: this.currentSession
});
}
// PIN handling (called by the app)
}, {
key: "handlePINResponse",
value: function () {
var _handlePINResponse = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(requestId, pin) {
var signature,
result,
_args8 = arguments,
_t7;
return _regenerator().w(function (_context8) {
while (1) switch (_context8.n) {
case 0:
signature = _args8.length > 2 && _args8[2] !== undefined ? _args8[2] : null;
_context8.p = 1;
_Logger["default"].info('Handling PIN response', {
requestId: requestId
});
_context8.n = 2;
return this.flowManager.handlePINResponse(requestId, pin, signature);
case 2:
result = _context8.v;
this.emit('pinResponseProcessed', {
requestId: requestId,
success: result.success,
sessionId: this.currentSession
});
return _context8.a(2, result);
case 3:
_context8.p = 3;
_t7 = _context8.v;
_Logger["default"].error('PIN response handling failed', {
error: _t7.message,
requestId: requestId
});
return _context8.a(2, {
success: false,
error: _t7.message
});
}
}, _callee8, this, [[1, 3]]);
}));
function handlePINResponse(_x4, _x5) {
return _handlePINResponse.apply(this, arguments);
}
return handlePINResponse;
}() // State management
}, {
key: "setState",
value: function setState(newState) {
if (this.state !== newState) {
var oldState = this.state;
this.state = newState;
_Logger["default"].debug('VoiceEngine state changed', {
oldState: oldState,
newState: newState
});
this.emit('stateChanged', {
oldState: oldState,
newState: newState,
sessionId: this.currentSession
});
}
}
}, {
key: "getState",
value: function getState() {
return this.state;
}
// Session management
}, {
key: "createNewSession",
value: function createNewSession() {
this.currentSession = this.securityManager.createSession('voice_user');
_Logger["default"].info('New session created', {
sessionId: this.currentSession
});
this.emit('sessionCreated', {
sessionId: this.currentSession
});
return this.currentSession;
}
}, {
key: "getCurrentSession",
value: function getCurrentSession() {
return this.currentSession;
}
}, {
key: "endCurrentSession",
value: function () {
var _endCurrentSession = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
return _regenerator().w(function (_context9) {
while (1) switch (_context9.n) {
case 0:
if (!this.currentSession) {
_context9.n = 2;
break;
}
_Logger["default"].info('Ending current session', {
sessionId: this.currentSession
});
// End any active flows
_context9.n = 1;
return this.flowManager.endFlow(this.currentSession, 'session_ended');
case 1:
// Invalidate security session
this.securityManager.invalidateSession(this.currentSession);
this.emit('sessionEnded', {
sessionId: this.currentSession
});
this.currentSession = null;
case 2:
return _context9.a(2);
}
}, _callee9, this);
}));
function endCurrentSession() {
return _endCurrentSession.apply(this, arguments);
}
return endCurrentSession;
}() // Flow management
}, {
key: "cancelCurrentFlow",
value: function () {
var _cancelCurrentFlow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0() {
return _regenerator().w(function (_context0) {
while (1) switch (_context0.n) {
case 0:
if (!this.currentSession) {
_context0.n = 2;
break;
}
_context0.n = 1;
return this.flowManager.cancelCurrentFlow(this.currentSession);
case 1:
return _context0.a(2, _context0.v);
case 2:
return _context0.a(2, {
response: "No active conversation to cancel."
});
}
}, _callee0, this);
}));
function cancelCurrentFlow() {
return _cancelCurrentFlow.apply(this, arguments);
}
return cancelCurrentFlow;
}()
}, {
key: "resetCurrentFlow",
value: function () {
var _resetCurrentFlow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1() {
return _regenerator().w(function (_context1) {
while (1) switch (_context1.n) {
case 0:
if (!this.currentSession) {
_context1.n = 2;
break;
}
_context1.n = 1;
return this.flowManager.resetFlow(this.currentSession);
case 1:
return _context1.a(2, _context1.v);
case 2:
return _context1.a(2, {
response: "No active conversation to reset."
});
}
}, _callee1, this);
}));
function resetCurrentFlow() {
return _resetCurrentFlow.apply(this, arguments);
}
return resetCurrentFlow;
}()
}, {
key: "getFlowContext",
value: function getFlowContext() {
if (this.currentSession) {
return this.flowManager.getFlowContext(this.currentSession);
}
return null;
}
// Configuration updates
}, {
key: "updateConfig",
value: function () {
var _updateConfig = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(newConfig) {
var _t8;
return _regenerator().w(function (_context10) {
while (1) switch (_context10.n) {
case 0:
_context10.p = 0;
_Logger["default"].info('Updating configuration');
this.configManager.updateConfig(newConfig);
this.config = this.configManager.getConfig();
// Update services with new config
if (newConfig.apiEndpoints) {
this.apiClient.updateEndpoints(newConfig.apiEndpoints);
}
if (newConfig.apiKey) {
this.apiClient.setApiKey(newConfig.apiKey);
}
if (newConfig.webhookUrl) {
this.webhookManager.updateWebhookUrl(newConfig.webhookUrl);
}
this.emit('configUpdated', {
config: this.config
});
return _context10.a(2, {
success: true
});
case 1:
_context10.p = 1;
_t8 = _context10.v;
_Logger["default"].error('Configuration update failed', {
error: _t8.message
});
return _context10.a(2, {
success: false,
error: _t8.message
});
}
}, _callee10, this, [[0, 1]]);
}));
function updateConfig(_x6) {
return _updateConfig.apply(this, arguments);
}
return updateConfig;
}() // Metrics and monitoring
}, {
key: "updateMetrics",
value: function updateMetrics(responseTime, flowResult) {
this.metrics.totalInteractions++;
this.metrics.lastInteractionTime = Date.now();
// Update average response time
if (this.metrics.averageResponseTime === 0) {
this.metrics.averageResponseTime = responseTime;
} else {
this.metrics.averageResponseTime = (this.metrics.averageResponseTime + responseTime) / 2;
}
// Track transfer outcomes
if (flowResult.completed) {
this.metrics.successfulTransfers++;
} else if (flowResult.failed) {
this.metrics.failedTransfers++;
}
}
}, {
key: "getMetrics",
value: function getMetrics() {
return _objectSpread(_objectSpread({}, this.metrics), {}, {
securityMetrics: this.securityManager.getSecurityMetrics(),
flowStats: this.flowManager.getFlowStats(),
currentState: this.state,
isInitialized: this.isInitialized,
currentSession: this.currentSession
});
}
// Event system
}, {
key: "on",
value: function on(event, callback) {
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event).push(callback);
}
}, {
key: "off",
value: function off(event, callback) {
if (this.listeners.has(event)) {
var callbacks = this.listeners.get(event);
var index = callbacks.indexOf(callback);
if (index > -1) {
callbacks.splice(index, 1);
}
}
}
}, {
key: "emit",
value: function emit(event, data) {
if (this.listeners.has(event)) {
this.listeners.get(event).forEach(function (callback) {
try {
callback(data);
} catch (error) {
_Logger["default"].error('Event callback error', {
event: event,
error: error.message
});
}
});
}
}
// Utility methods
}, {
key: "isListening",
value: function isListening() {
return this.state === _Constants.VOICE_STATES.LISTENING;
}
}, {
key: "isSpeaking",
value: function isSpeaking() {
return this.state === _Constants.VOICE_STATES.SPEAKING;
}
}, {
key: "isProcessing",
value: function isProcessing() {
return this.state === _Constants.VOICE_STATES.PROCESSING;
}
}, {
key: "isIdle",
value: function isIdle() {
return this.state === _Constants.VOICE_STATES.IDLE;
}
}, {
key: "hasActiveFlow",
value: function hasActiveFlow() {
return this.currentSession && this.flowManager.isFlowActive(this.currentSession);
}
// Cleanup
}, {
key: "cleanup",
value: function () {
var _cleanup = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11() {
var _t9;
return _regenerator().w(function (_context11) {
while (1) switch (_context11.n) {
case 0:
_context11.p = 0;
_Logger["default"].info('Cleaning up VoiceEngine...');
// Stop any active audio
if (!this.audioManager.isListening()) {
_context11.n = 1;
break;
}
_context11.n = 1;
return this.audioManager.cancelListening();
case 1:
if (!this.textToSpeech.isSpeaking()) {
_context11.n = 2;
break;
}
_context11.n = 2;
return this.textToSpeech.stop();
case 2:
_context11.n = 3;
return this.endCurrentSession();
case 3:
_context11.n = 4;
return this.audioManager.cleanup();
case 4:
_context11.n = 5;
return this.textToSpeech.cleanup();
case 5:
this.webhookManager.cleanup();
this.flowManager.cleanup();
this.securityManager.clearSensitiveData();
// Clear listeners
this.listeners.clear();
this.isInitialized = false;
this.setState(_Constants.VOICE_STATES.IDLE);
_Logger["default"].info('VoiceEngine cleanup completed');
_context11.n = 7;
break;
case 6:
_context11.p = 6;
_t9 = _context11.v;
_Logger["default"].error('VoiceEngine cleanup failed', {
error: _t9.message
});
case 7:
return _context11.a(2);
}
}, _callee11, this, [[0, 6]]);
}));
function cleanup() {
return _cleanup.apply(this, arguments);
}
return cleanup;
}() // Health check
}, {
key: "healthCheck",
value: function () {
var _healthCheck = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12() {
var health, connections, metrics, totalTransfers, failureRate, _t0;
return _regenerator().w(function (_context12) {
while (1) switch (_context12.n) {
case 0:
health = {
status: 'healthy',
timestamp: Date.now(),
services: {},
issues: []
};
_context12.p = 1;
// Check initialization
if (!this.isInitialized) {
health.issues.push('VoiceEngine not initialized');
health.status = 'unhealthy';
}
// Check current state
if (this.state === _Constants.VOICE_STATES.ERROR) {
health.issues.push('VoiceEngine in error state');
health.status = 'degraded';
}
// Check service connections
_context12.n = 2;
return this.testConnections();
case 2:
connections = _context12.v;
health.services = connections;
// Check for failed connections
Object.entries(connections).forEach(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
service = _ref2[0],
result = _ref2[1];
if (!result.connected) {
health.issues.push("".concat(service, " connection failed: ").concat(result.error));
health.status = 'degraded';
}
});
// Check metrics
metrics = this.getMetrics();
health.metrics = metrics;
// Check for high failure rate
totalTransfers = metrics.successfulTransfers + metrics.failedTransfers;
if (totalTransfers > 0) {
failureRate = metrics.failedTransfers / totalTransfers;
if (failureRate > 0.5) {
health.issues.push('High transfer failure rate');
health.status = 'degraded';
}
}
_context12.n = 4;
break;
case 3:
_context12.p = 3;
_t0 = _context12.v;
health.status = 'unhealthy';
health.issues.push("Health check failed: ".concat(_t0.message));
case 4:
return _context12.a(2, health);
}
}, _callee12, this, [[1, 3]]);
}));
function healthCheck() {
return _healthCheck.apply(this, arguments);
}
return healthCheck;
}()
}]);
}();
var _default = exports["default"] = VoiceEngine;