UNPKG

zedai-voice

Version:

Siri-like voice banking SDK for React Native - Complete voice-powered fintech solution with wake word detection and beautiful UI

582 lines (577 loc) 29.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _reactNative = require("react-native"); var _Logger = _interopRequireDefault(require("../utils/Logger")); 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 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 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/PermissionManager.js var PermissionManager = /*#__PURE__*/function () { function PermissionManager() { _classCallCheck(this, PermissionManager); this.permissions = { microphone: false, storage: false, backgroundAudio: false }; this.requestInProgress = false; } return _createClass(PermissionManager, [{ key: "checkAndRequestAllPermissions", value: function () { var _checkAndRequestAllPermissions = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() { var allGranted, _t; return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: _context.p = 0; _Logger["default"].info('Checking and requesting voice banking permissions...'); if (!this.requestInProgress) { _context.n = 1; break; } _Logger["default"].warn('Permission request already in progress'); return _context.a(2, this.permissions); case 1: this.requestInProgress = true; // Check existing permissions first _context.n = 2; return this.checkExistingPermissions(); case 2: if (this.permissions.microphone) { _context.n = 3; break; } _context.n = 3; return this.requestMicrophonePermission(); case 3: if (!(!this.permissions.storage && _reactNative.Platform.OS === 'android')) { _context.n = 4; break; } _context.n = 4; return this.requestStoragePermission(); case 4: if (this.permissions.backgroundAudio) { _context.n = 5; break; } _context.n = 5; return this.requestBackgroundAudioPermission(); case 5: this.requestInProgress = false; allGranted = Object.values(this.permissions).every(function (p) { return p; }); _Logger["default"].info('Permission check completed', { permissions: this.permissions, allGranted: allGranted }); return _context.a(2, { allGranted: allGranted, permissions: this.permissions }); case 6: _context.p = 6; _t = _context.v; this.requestInProgress = false; _Logger["default"].error('Permission request failed', { error: _t.message }); throw _t; case 7: return _context.a(2); } }, _callee, this, [[0, 6]]); })); function checkAndRequestAllPermissions() { return _checkAndRequestAllPermissions.apply(this, arguments); } return checkAndRequestAllPermissions; }() }, { key: "checkExistingPermissions", value: function () { var _checkExistingPermissions = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() { var micPermission, storagePermission; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: if (!(_reactNative.Platform.OS === 'android')) { _context2.n = 3; break; } _context2.n = 1; return _reactNative.PermissionsAndroid.check(_reactNative.PermissionsAndroid.PERMISSIONS.RECORD_AUDIO); case 1: micPermission = _context2.v; _context2.n = 2; return _reactNative.PermissionsAndroid.check(_reactNative.PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE); case 2: storagePermission = _context2.v; this.permissions.microphone = micPermission; this.permissions.storage = storagePermission; this.permissions.backgroundAudio = true; // Android doesn't need special permission for this _context2.n = 4; break; case 3: // iOS permissions are handled differently - they're requested when needed // We'll assume they're available and let the system handle the request this.permissions.microphone = true; // Will be requested by the system this.permissions.storage = true; this.permissions.backgroundAudio = true; case 4: return _context2.a(2); } }, _callee2, this); })); function checkExistingPermissions() { return _checkExistingPermissions.apply(this, arguments); } return checkExistingPermissions; }() }, { key: "requestMicrophonePermission", value: function () { var _requestMicrophonePermission = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() { var granted, _t2; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: _context3.p = 0; _Logger["default"].info('Requesting microphone permission...'); if (!(_reactNative.Platform.OS === 'android')) { _context3.n = 4; break; } _context3.n = 1; return this.showPermissionExplanation('Microphone Access Required', 'Voice Banking needs microphone access to listen for your voice commands like "transfer money" and "hey [bank name]". This enables hands-free banking.', 'Grant Microphone Access'); case 1: _context3.n = 2; return _reactNative.PermissionsAndroid.request(_reactNative.PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, { title: 'Voice Banking Microphone Permission', message: 'Voice Banking needs access to your microphone to listen for voice commands and enable hands-free banking.', buttonNeutral: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'Allow' }); case 2: granted = _context3.v; this.permissions.microphone = granted === _reactNative.PermissionsAndroid.RESULTS.GRANTED; if (this.permissions.microphone) { _context3.n = 3; break; } _context3.n = 3; return this.handlePermissionDenied('microphone'); case 3: _context3.n = 5; break; case 4: // iOS - permission will be requested automatically by the system this.permissions.microphone = true; case 5: _context3.n = 7; break; case 6: _context3.p = 6; _t2 = _context3.v; _Logger["default"].error('Microphone permission request failed', { error: _t2.message }); this.permissions.microphone = false; case 7: return _context3.a(2); } }, _callee3, this, [[0, 6]]); })); function requestMicrophonePermission() { return _requestMicrophonePermission.apply(this, arguments); } return requestMicrophonePermission; }() }, { key: "requestStoragePermission", value: function () { var _requestStoragePermission = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() { var granted, _t3; return _regenerator().w(function (_context4) { while (1) switch (_context4.n) { case 0: _context4.p = 0; if (!(_reactNative.Platform.OS !== 'android')) { _context4.n = 1; break; } return _context4.a(2); case 1: _Logger["default"].info('Requesting storage permission...'); _context4.n = 2; return this.showPermissionExplanation('Storage Access Required', 'Voice Banking needs storage access to temporarily save voice recordings for processing. Audio files are deleted immediately after processing.', 'Grant Storage Access'); case 2: _context4.n = 3; return _reactNative.PermissionsAndroid.request(_reactNative.PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { title: 'Voice Banking Storage Permission', message: 'Voice Banking needs storage access to temporarily process voice commands.', buttonNeutral: 'Ask Me Later', buttonNegative: 'Cancel', buttonPositive: 'Allow' }); case 3: granted = _context4.v; this.permissions.storage = granted === _reactNative.PermissionsAndroid.RESULTS.GRANTED; if (this.permissions.storage) { _context4.n = 4; break; } _context4.n = 4; return this.handlePermissionDenied('storage'); case 4: _context4.n = 6; break; case 5: _context4.p = 5; _t3 = _context4.v; _Logger["default"].error('Storage permission request failed', { error: _t3.message }); this.permissions.storage = false; case 6: return _context4.a(2); } }, _callee4, this, [[0, 5]]); })); function requestStoragePermission() { return _requestStoragePermission.apply(this, arguments); } return requestStoragePermission; }() }, { key: "requestBackgroundAudioPermission", value: function () { var _requestBackgroundAudioPermission = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() { return _regenerator().w(function (_context5) { while (1) switch (_context5.n) { case 0: try { // This is more about app configuration than runtime permission // The wake word detection needs to work when app is in foreground if (_reactNative.Platform.OS === 'ios') { // iOS requires background modes to be configured in Info.plist // We'll provide instructions to the developer _Logger["default"].info('iOS background audio configuration required in Info.plist'); } this.permissions.backgroundAudio = true; } catch (error) { _Logger["default"].error('Background audio permission setup failed', { error: error.message }); this.permissions.backgroundAudio = false; } case 1: return _context5.a(2); } }, _callee5, this); })); function requestBackgroundAudioPermission() { return _requestBackgroundAudioPermission.apply(this, arguments); } return requestBackgroundAudioPermission; }() }, { key: "showPermissionExplanation", value: function () { var _showPermissionExplanation = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(title, message, buttonText) { return _regenerator().w(function (_context6) { while (1) switch (_context6.n) { case 0: return _context6.a(2, new Promise(function (resolve) { _reactNative.Alert.alert(title, message, [{ text: 'Cancel', style: 'cancel', onPress: function onPress() { return resolve(false); } }, { text: buttonText, onPress: function onPress() { return resolve(true); } }]); })); } }, _callee6); })); function showPermissionExplanation(_x, _x2, _x3) { return _showPermissionExplanation.apply(this, arguments); } return showPermissionExplanation; }() }, { key: "handlePermissionDenied", value: function () { var _handlePermissionDenied = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(permissionType) { var messages, config; return _regenerator().w(function (_context7) { while (1) switch (_context7.n) { case 0: messages = { microphone: { title: 'Microphone Access Denied', message: 'Voice Banking needs microphone access to work. You can enable it in Settings > Apps > [Your App] > Permissions.', settingsAction: 'Open Settings' }, storage: { title: 'Storage Access Denied', message: 'Voice Banking needs storage access for voice processing. You can enable it in Settings > Apps > [Your App] > Permissions.', settingsAction: 'Open Settings' } }; config = messages[permissionType]; if (config) { _context7.n = 1; break; } return _context7.a(2); case 1: return _context7.a(2, new Promise(function (resolve) { _reactNative.Alert.alert(config.title, config.message, [{ text: 'Continue Without', style: 'cancel', onPress: function onPress() { return resolve(false); } }, { text: config.settingsAction, onPress: function onPress() { _reactNative.Linking.openSettings(); resolve(false); } }]); })); } }, _callee7); })); function handlePermissionDenied(_x4) { return _handlePermissionDenied.apply(this, arguments); } return handlePermissionDenied; }() }, { key: "requestPermissionWithRetry", value: function () { var _requestPermissionWithRetry = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(permissionType) { var maxRetries, attempt, _args8 = arguments, _t4; return _regenerator().w(function (_context8) { while (1) switch (_context8.n) { case 0: maxRetries = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : 2; attempt = 0; case 1: if (!(attempt < maxRetries)) { _context8.n = 9; break; } _Logger["default"].info("Requesting ".concat(permissionType, " permission (attempt ").concat(attempt + 1, ")")); _t4 = permissionType; _context8.n = _t4 === 'microphone' ? 2 : _t4 === 'storage' ? 4 : 6; break; case 2: _context8.n = 3; return this.requestMicrophonePermission(); case 3: return _context8.a(3, 6); case 4: _context8.n = 5; return this.requestStoragePermission(); case 5: return _context8.a(3, 6); case 6: if (!this.permissions[permissionType]) { _context8.n = 7; break; } return _context8.a(2, true); case 7: if (!(attempt < maxRetries - 1)) { _context8.n = 8; break; } _context8.n = 8; return new Promise(function (resolve) { return setTimeout(resolve, 1000); }); case 8: attempt++; _context8.n = 1; break; case 9: return _context8.a(2, false); } }, _callee8, this); })); function requestPermissionWithRetry(_x5) { return _requestPermissionWithRetry.apply(this, arguments); } return requestPermissionWithRetry; }() }, { key: "getPermissionStatus", value: function getPermissionStatus() { return _objectSpread({}, this.permissions); } }, { key: "hasRequiredPermissions", value: function hasRequiredPermissions() { // Microphone is absolutely required if (!this.permissions.microphone) return false; // Storage is required on Android if (_reactNative.Platform.OS === 'android' && !this.permissions.storage) return false; return true; } }, { key: "getMissingPermissions", value: function getMissingPermissions() { var missing = []; if (!this.permissions.microphone) { missing.push({ type: 'microphone', reason: 'Required for voice commands and wake word detection', critical: true }); } if (_reactNative.Platform.OS === 'android' && !this.permissions.storage) { missing.push({ type: 'storage', reason: 'Required for voice processing on Android', critical: true }); } if (!this.permissions.backgroundAudio) { missing.push({ type: 'backgroundAudio', reason: 'Required for wake word detection when app is in background', critical: false }); } return missing; } }, { key: "showInitialPermissionPrompt", value: function () { var _showInitialPermissionPrompt = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(bankName) { var title, message; return _regenerator().w(function (_context9) { while (1) switch (_context9.n) { case 0: title = "Welcome to ".concat(bankName, " Voice Banking!"); message = "To enable voice commands like \"Hey ".concat(bankName, ", transfer money\", we need access to your microphone.\n\nYour voice data is processed securely and never stored."); return _context9.a(2, new Promise(function (resolve) { _reactNative.Alert.alert(title, message, [{ text: 'Not Now', style: 'cancel', onPress: function onPress() { return resolve(false); } }, { text: 'Enable Voice Banking', onPress: function onPress() { return resolve(true); } }]); })); } }, _callee9); })); function showInitialPermissionPrompt(_x6) { return _showInitialPermissionPrompt.apply(this, arguments); } return showInitialPermissionPrompt; }() }, { key: "showPermissionSummary", value: function () { var _showPermissionSummary = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(bankName) { var status, missing, criticalMissing, permissionList; return _regenerator().w(function (_context0) { while (1) switch (_context0.n) { case 0: status = this.getPermissionStatus(); missing = this.getMissingPermissions(); if (!(missing.length === 0)) { _context0.n = 1; break; } _reactNative.Alert.alert('Voice Banking Ready! 🎉', "All permissions granted. You can now say \"Hey ".concat(bankName, "\" to start voice banking."), [{ text: 'Great!', style: 'default' }]); return _context0.a(2); case 1: criticalMissing = missing.filter(function (p) { return p.critical; }); if (criticalMissing.length > 0) { permissionList = criticalMissing.map(function (p) { return "\u2022 ".concat(p.type, ": ").concat(p.reason); }).join('\n'); _reactNative.Alert.alert('Additional Permissions Needed', "Voice Banking needs these permissions to work properly:\n\n".concat(permissionList, "\n\nYou can grant them later in Settings."), [{ text: 'Continue', style: 'default' }, { text: 'Open Settings', onPress: function onPress() { return _reactNative.Linking.openSettings(); } }]); } case 2: return _context0.a(2); } }, _callee0, this); })); function showPermissionSummary(_x7) { return _showPermissionSummary.apply(this, arguments); } return showPermissionSummary; }() // iOS Info.plist configuration helper }, { key: "getIOSConfigurationInstructions", value: function getIOSConfigurationInstructions() { return { infoPlist: { 'NSMicrophoneUsageDescription': 'This app uses the microphone for voice banking commands and hands-free money transfers.', 'NSSpeechRecognitionUsageDescription': 'This app uses speech recognition to process voice banking commands like "transfer money".', 'UIBackgroundModes': ['audio', 'background-processing'] }, instructions: ['1. Add the above keys to your iOS Info.plist file', '2. Customize the usage descriptions to match your app', '3. Background modes enable wake word detection', '4. Test on a physical device (not simulator)'] }; } // Android manifest configuration helper }, { key: "getAndroidConfigurationInstructions", value: function getAndroidConfigurationInstructions() { return { manifest: ['<uses-permission android:name="android.permission.RECORD_AUDIO" />', '<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />', '<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />', '<uses-permission android:name="android.permission.WAKE_LOCK" />', '<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />'], instructions: ['1. Add the above permissions to your AndroidManifest.xml', '2. The SDK will request these permissions at runtime', '3. WAKE_LOCK enables background wake word detection', '4. Test permissions on different Android versions'] }; } }]); }(); var _default = exports["default"] = PermissionManager;