UNPKG

zedai-voice

Version:

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

484 lines (465 loc) 22.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; 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 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/IntentParser.js var IntentParser = /*#__PURE__*/function () { function IntentParser(openaiConfig) { _classCallCheck(this, IntentParser); this.apiKey = openaiConfig.apiKey; this.model = openaiConfig.gptModel || _Constants.OPENAI_CONFIG.GPT_MODEL; this.maxTokens = openaiConfig.maxTokens || _Constants.OPENAI_CONFIG.MAX_TOKENS; this.temperature = openaiConfig.temperature || _Constants.OPENAI_CONFIG.TEMPERATURE; this.baseURL = 'https://api.openai.com/v1'; this.systemPrompt = _Constants.OPENAI_CONFIG.SYSTEM_PROMPT; } return _createClass(IntentParser, [{ key: "parseIntent", value: function () { var _parseIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(text) { var context, _result$choices$, userPrompt, response, errorData, result, content, parsed, intentResult, _args = arguments, _t; return _regenerator().w(function (_context) { while (1) switch (_context.n) { case 0: context = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}; _context.p = 1; _Logger["default"].info('Parsing intent', { text: text.substring(0, 100) + '...' }); userPrompt = this.buildUserPrompt(text, context); _context.n = 2; return this.makeGPTRequest({ messages: [{ role: 'system', content: this.systemPrompt }, { role: 'user', content: userPrompt }], max_tokens: this.maxTokens, temperature: this.temperature, response_format: { type: "json_object" } }); case 2: response = _context.v; if (response.ok) { _context.n = 4; break; } _context.n = 3; return response.json()["catch"](function () { return {}; }); case 3: errorData = _context.v; _Logger["default"].error('OpenAI GPT API error', { status: response.status, error: errorData }); throw new Error("OpenAI GPT API error: ".concat(response.status)); case 4: _context.n = 5; return response.json(); case 5: result = _context.v; content = (_result$choices$ = result.choices[0]) === null || _result$choices$ === void 0 || (_result$choices$ = _result$choices$.message) === null || _result$choices$ === void 0 ? void 0 : _result$choices$.content; if (content) { _context.n = 6; break; } throw new Error('Empty response from OpenAI'); case 6: parsed = JSON.parse(content); intentResult = this.validateAndNormalizeIntent(parsed, text); _Logger["default"].info('Intent parsed successfully', { intent: intentResult.intent, entities: Object.keys(intentResult.entities), confidence: intentResult.confidence }); return _context.a(2, intentResult); case 7: _context.p = 7; _t = _context.v; _Logger["default"].error('Intent parsing failed', { error: _t.message, text: text.substring(0, 50) + '...' }); // Fallback to rule-based parsing return _context.a(2, this.fallbackIntentParsing(text)); } }, _callee, this, [[1, 7]]); })); function parseIntent(_x) { return _parseIntent.apply(this, arguments); } return parseIntent; }() }, { key: "buildUserPrompt", value: function buildUserPrompt(text, context) { var prompt = "Parse this banking voice command: \"".concat(text, "\""); if (context.currentFlow) { prompt += "\n\nContext: User is currently in ".concat(context.currentFlow, " flow."); } if (context.expectedIntent) { prompt += "\n\nExpected intent type: ".concat(context.expectedIntent); } if (context.previousEntities) { prompt += "\n\nPrevious entities: ".concat(JSON.stringify(context.previousEntities)); } return prompt; } }, { key: "validateAndNormalizeIntent", value: function validateAndNormalizeIntent(parsed, originalText) { // Validate intent var intent = parsed.intent || _Constants.INTENTS.UNKNOWN; var validIntents = Object.values(_Constants.INTENTS); var normalizedIntent = validIntents.includes(intent) ? intent : _Constants.INTENTS.UNKNOWN; // Validate and normalize entities var entities = this.normalizeEntities(parsed.entities || {}, originalText); // Calculate confidence var confidence = this.calculateIntentConfidence(normalizedIntent, entities, originalText); return { intent: normalizedIntent, entities: entities, confidence: confidence, originalText: originalText, rawResponse: parsed }; } }, { key: "normalizeEntities", value: function normalizeEntities(entities, originalText) { var normalized = {}; // Normalize amount if (entities.amount) { var amount = this.normalizeAmount(entities.amount); if (amount !== null) { normalized.amount = amount; } } // Normalize account number if (entities.account_number) { var accountNumber = this.normalizeAccountNumber(entities.account_number); if (accountNumber) { normalized.account_number = accountNumber; } } // Normalize bank name if (entities.bank_name) { normalized.bank_name = this.normalizeBankName(entities.bank_name); } // Normalize confirmation if (entities.confirmation) { normalized.confirmation = this.normalizeConfirmation(entities.confirmation); } // Extract any missed entities from original text var extractedEntities = this.extractEntitiesFromText(originalText); Object.assign(normalized, extractedEntities); return normalized; } }, { key: "normalizeAmount", value: function normalizeAmount(amount) { if (typeof amount === 'number') { return amount; } if (typeof amount === 'string') { // Remove currency symbols and commas var cleaned = amount.replace(/[₦,\s]/g, ''); var parsed = parseFloat(cleaned); if (!isNaN(parsed) && parsed > 0) { return parsed; } } return null; } }, { key: "normalizeAccountNumber", value: function normalizeAccountNumber(accountNumber) { if (typeof accountNumber !== 'string') { return null; } // Remove spaces and non-numeric characters var cleaned = accountNumber.replace(/\D/g, ''); // Valid account numbers are typically 8-15 digits if (cleaned.length >= 8 && cleaned.length <= 15) { return cleaned; } return null; } }, { key: "normalizeBankName", value: function normalizeBankName(bankName) { if (typeof bankName !== 'string') { return null; } // Capitalize first letter of each word return bankName.toLowerCase().split(' ').map(function (word) { return word.charAt(0).toUpperCase() + word.slice(1); }).join(' ').trim(); } }, { key: "normalizeConfirmation", value: function normalizeConfirmation(confirmation) { if (typeof confirmation !== 'string') { return null; } var positive = ['yes', 'yeah', 'yep', 'correct', 'right', 'true', 'confirm']; var negative = ['no', 'nope', 'wrong', 'false', 'cancel', 'incorrect']; var lower = confirmation.toLowerCase(); if (positive.some(function (word) { return lower.includes(word); })) { return 'yes'; } if (negative.some(function (word) { return lower.includes(word); })) { return 'no'; } return confirmation; } }, { key: "extractEntitiesFromText", value: function extractEntitiesFromText(text) { var entities = {}; // Extract amounts using regex var amountPattern = /(?:₦|naira|NGN|N)?\s*(\d+(?:,\d{3})*(?:\.\d{2})?)/gi; var amountMatch = text.match(amountPattern); if (amountMatch && !entities.amount) { var amount = this.normalizeAmount(amountMatch[0]); if (amount) { entities.amount = amount; } } // Extract account numbers (sequences of 8-15 digits) var accountPattern = /\b(\d{8,15})\b/g; var accountMatch = text.match(accountPattern); if (accountMatch && !entities.account_number) { entities.account_number = accountMatch[0]; } // Extract bank names (common Nigerian banks) var bankPatterns = [/GTBank|Guaranty Trust Bank/i, /Access Bank/i, /First Bank|FirstBank/i, /UBA|United Bank for Africa/i, /Zenith Bank/i, /Fidelity Bank/i, /Sterling Bank/i, /Union Bank/i, /Polaris Bank/i, /Stanbic IBTC/i]; for (var _i = 0, _bankPatterns = bankPatterns; _i < _bankPatterns.length; _i++) { var pattern = _bankPatterns[_i]; var match = text.match(pattern); if (match && !entities.bank_name) { entities.bank_name = this.normalizeBankName(match[0]); break; } } return entities; } }, { key: "calculateIntentConfidence", value: function calculateIntentConfidence(intent, entities, originalText) { var confidence = 0.5; // Base confidence // Intent-specific confidence adjustments switch (intent) { case _Constants.INTENTS.TRANSFER_MONEY: if (entities.amount) confidence += 0.2; if (entities.account_number) confidence += 0.2; if (entities.bank_name) confidence += 0.1; break; case _Constants.INTENTS.CONFIRM_YES: case _Constants.INTENTS.CONFIRM_NO: var confirmationWords = ['yes', 'no', 'correct', 'wrong', 'right']; if (confirmationWords.some(function (word) { return originalText.toLowerCase().includes(word); })) { confidence += 0.3; } break; case _Constants.INTENTS.CANCEL: var cancelWords = ['cancel', 'stop', 'abort', 'quit']; if (cancelWords.some(function (word) { return originalText.toLowerCase().includes(word); })) { confidence += 0.3; } break; } // Text quality adjustments if (originalText.length > 10) confidence += 0.1; if (originalText.split(/\s+/).length > 3) confidence += 0.1; // Penalize very short or unclear text if (originalText.length < 5) confidence -= 0.2; return Math.max(0.1, Math.min(1.0, confidence)); } }, { key: "fallbackIntentParsing", value: function fallbackIntentParsing(text) { _Logger["default"].info('Using fallback intent parsing', { text: text }); var lowerText = text.toLowerCase(); var entities = this.extractEntitiesFromText(text); // Rule-based intent detection var intent = _Constants.INTENTS.UNKNOWN; if (lowerText.includes('transfer') || lowerText.includes('send')) { intent = _Constants.INTENTS.TRANSFER_MONEY; } else if (lowerText.includes('yes') || lowerText.includes('correct') || lowerText.includes('right')) { intent = _Constants.INTENTS.CONFIRM_YES; } else if (lowerText.includes('no') || lowerText.includes('wrong') || lowerText.includes('incorrect')) { intent = _Constants.INTENTS.CONFIRM_NO; } else if (lowerText.includes('cancel') || lowerText.includes('stop')) { intent = _Constants.INTENTS.CANCEL; } else if (lowerText.includes('help')) { intent = _Constants.INTENTS.HELP; } else if (entities.account_number) { intent = _Constants.INTENTS.PROVIDE_ACCOUNT; } else if (entities.amount) { intent = _Constants.INTENTS.PROVIDE_AMOUNT; } else if (entities.bank_name) { intent = _Constants.INTENTS.PROVIDE_BANK; } return { intent: intent, entities: entities, confidence: 0.6, // Lower confidence for fallback originalText: text, fallback: true }; } }, { key: "makeGPTRequest", value: function () { var _makeGPTRequest = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(payload) { var controller, timeoutId, response, _t2; return _regenerator().w(function (_context2) { while (1) switch (_context2.n) { case 0: controller = new AbortController(); timeoutId = setTimeout(function () { return controller.abort(); }, _Constants.API_TIMEOUTS.INTENT_PARSING); _context2.p = 1; _context2.n = 2; return fetch("".concat(this.baseURL, "/chat/completions"), { method: 'POST', headers: { 'Authorization': "Bearer ".concat(this.apiKey), 'Content-Type': 'application/json' }, body: JSON.stringify(_objectSpread({ model: this.model }, payload)), signal: controller.signal }); case 2: response = _context2.v; clearTimeout(timeoutId); return _context2.a(2, response); case 3: _context2.p = 3; _t2 = _context2.v; clearTimeout(timeoutId); if (!(_t2.name === 'AbortError')) { _context2.n = 4; break; } throw new Error('Intent parsing timeout'); case 4: throw _t2; case 5: return _context2.a(2); } }, _callee2, this, [[1, 3]]); })); function makeGPTRequest(_x2) { return _makeGPTRequest.apply(this, arguments); } return makeGPTRequest; }() }, { key: "testConnection", value: function () { var _testConnection = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() { var response, _t3; return _regenerator().w(function (_context3) { while (1) switch (_context3.n) { case 0: _context3.p = 0; _context3.n = 1; return this.makeGPTRequest({ messages: [{ role: 'user', content: 'Test connection' }], max_tokens: 5 }); case 1: response = _context3.v; if (!response.ok) { _context3.n = 2; break; } _Logger["default"].info('GPT connection test successful'); return _context3.a(2, { connected: true }); case 2: _Logger["default"].error('GPT connection test failed', { status: response.status }); return _context3.a(2, { connected: false, error: "HTTP ".concat(response.status) }); case 3: _context3.n = 5; break; case 4: _context3.p = 4; _t3 = _context3.v; _Logger["default"].error('GPT connection test failed', { error: _t3.message }); return _context3.a(2, { connected: false, error: _t3.message }); case 5: return _context3.a(2); } }, _callee3, this, [[0, 4]]); })); function testConnection() { return _testConnection.apply(this, arguments); } return testConnection; }() }]); }(); var _default = exports["default"] = IntentParser;