zedai-voice
Version:
Siri-like voice banking SDK for React Native - Complete voice-powered fintech solution with wake word detection and beautiful UI
663 lines (650 loc) • 31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _TransferFlow = _interopRequireDefault(require("./TransferFlow"));
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 _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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
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 _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 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 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/flows/FlowManager.js
var FlowManager = /*#__PURE__*/function () {
function FlowManager(apiClient, webhookManager, configManager, securityManager) {
_classCallCheck(this, FlowManager);
this.apiClient = apiClient;
this.webhookManager = webhookManager;
this.configManager = configManager;
this.securityManager = securityManager;
this.activeFlows = new Map();
this.flowHistory = new Map();
}
return _createClass(FlowManager, [{
key: "startFlow",
value: function () {
var _startFlow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(sessionId, intent) {
var entities,
flow,
flowType,
result,
_args = arguments,
_t,
_t2;
return _regenerator().w(function (_context) {
while (1) switch (_context.n) {
case 0:
entities = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
_context.p = 1;
_Logger["default"].info('Starting new flow', {
sessionId: sessionId,
intent: intent,
entities: entities
});
// End any existing flow for this session
if (!this.activeFlows.has(sessionId)) {
_context.n = 2;
break;
}
_context.n = 2;
return this.endFlow(sessionId);
case 2:
flow = null;
flowType = null; // Determine which flow to start based on intent
_t = intent;
_context.n = _t === _Constants.INTENTS.TRANSFER_MONEY ? 3 : _t === _Constants.INTENTS.HELP ? 4 : 5;
break;
case 3:
flow = new _TransferFlow["default"](this.apiClient, this.webhookManager, this.configManager, this.securityManager);
flowType = 'TRANSFER';
return _context.a(3, 7);
case 4:
return _context.a(2, this.handleHelpIntent(sessionId));
case 5:
if (!(entities.amount || entities.account_number || entities.bank_name)) {
_context.n = 6;
break;
}
flow = new _TransferFlow["default"](this.apiClient, this.webhookManager, this.configManager, this.securityManager);
flowType = 'TRANSFER';
intent = _Constants.INTENTS.TRANSFER_MONEY; // Override intent
_context.n = 7;
break;
case 6:
return _context.a(2, this.handleUnknownIntent(sessionId, intent));
case 7:
if (!flow) {
_context.n = 9;
break;
}
_context.n = 8;
return flow.start(sessionId, intent, entities);
case 8:
result = _context.v;
// Store the active flow
this.activeFlows.set(sessionId, {
flow: flow,
type: flowType,
startTime: Date.now(),
lastActivity: Date.now()
});
_Logger["default"].info('Flow started successfully', {
sessionId: sessionId,
flowType: flowType,
state: flow.getState()
});
return _context.a(2, _objectSpread(_objectSpread({}, result), {}, {
flowType: flowType,
sessionId: sessionId
}));
case 9:
_context.n = 11;
break;
case 10:
_context.p = 10;
_t2 = _context.v;
_Logger["default"].error('Failed to start flow', {
error: _t2.message,
sessionId: sessionId,
intent: intent
});
return _context.a(2, {
response: this.configManager.getResponse('networkError'),
error: _t2.message,
sessionId: sessionId
});
case 11:
return _context.a(2);
}
}, _callee, this, [[1, 10]]);
}));
function startFlow(_x, _x2) {
return _startFlow.apply(this, arguments);
}
return startFlow;
}()
}, {
key: "continueFlow",
value: function () {
var _continueFlow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(sessionId, intent) {
var entities,
activeFlow,
result,
_args2 = arguments,
_t3;
return _regenerator().w(function (_context2) {
while (1) switch (_context2.n) {
case 0:
entities = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : {};
_context2.p = 1;
activeFlow = this.activeFlows.get(sessionId);
if (activeFlow) {
_context2.n = 3;
break;
}
_Logger["default"].info('No active flow, starting new flow', {
sessionId: sessionId,
intent: intent
});
_context2.n = 2;
return this.startFlow(sessionId, intent, entities);
case 2:
return _context2.a(2, _context2.v);
case 3:
// Update last activity
activeFlow.lastActivity = Date.now();
_Logger["default"].info('Continuing existing flow', {
sessionId: sessionId,
flowType: activeFlow.type,
intent: intent,
currentState: activeFlow.flow.getState()
});
// Handle the intent in the current flow
_context2.n = 4;
return activeFlow.flow.handleIntent(intent, entities);
case 4:
result = _context2.v;
// Check if flow is completed
if (activeFlow.flow.isCompleted()) {
_Logger["default"].info('Flow completed', {
sessionId: sessionId,
flowType: activeFlow.type,
finalState: activeFlow.flow.getState()
});
// Move to history and remove from active flows
this.moveFlowToHistory(sessionId, activeFlow);
}
return _context2.a(2, _objectSpread(_objectSpread({}, result), {}, {
flowType: activeFlow.type,
sessionId: sessionId
}));
case 5:
_context2.p = 5;
_t3 = _context2.v;
_Logger["default"].error('Failed to continue flow', {
error: _t3.message,
sessionId: sessionId,
intent: intent
});
return _context2.a(2, {
response: this.configManager.getResponse('networkError'),
error: _t3.message,
sessionId: sessionId
});
}
}, _callee2, this, [[1, 5]]);
}));
function continueFlow(_x3, _x4) {
return _continueFlow.apply(this, arguments);
}
return continueFlow;
}()
}, {
key: "handleIntent",
value: function () {
var _handleIntent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(sessionId, intent) {
var entities,
activeFlow,
_args3 = arguments;
return _regenerator().w(function (_context3) {
while (1) switch (_context3.n) {
case 0:
entities = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : {};
// Main entry point for handling intents
activeFlow = this.activeFlows.get(sessionId);
if (!activeFlow) {
_context3.n = 2;
break;
}
_context3.n = 1;
return this.continueFlow(sessionId, intent, entities);
case 1:
return _context3.a(2, _context3.v);
case 2:
_context3.n = 3;
return this.startFlow(sessionId, intent, entities);
case 3:
return _context3.a(2, _context3.v);
case 4:
return _context3.a(2);
}
}, _callee3, this);
}));
function handleIntent(_x5, _x6) {
return _handleIntent.apply(this, arguments);
}
return handleIntent;
}()
}, {
key: "handleHelpIntent",
value: function handleHelpIntent(sessionId) {
_Logger["default"].info('Handling help intent', {
sessionId: sessionId
});
return {
response: this.configManager.getResponse('help'),
sessionId: sessionId,
intent: _Constants.INTENTS.HELP
};
}
}, {
key: "handleUnknownIntent",
value: function handleUnknownIntent(sessionId, intent) {
_Logger["default"].info('Handling unknown intent', {
sessionId: sessionId,
intent: intent
});
return {
response: this.configManager.getResponse('unknownCommand'),
sessionId: sessionId,
intent: _Constants.INTENTS.UNKNOWN,
suggestions: ["Try saying 'transfer money'", "Say 'send 5000 to account 1234567890 at GTBank'", "Ask for 'help' to learn more"]
};
}
}, {
key: "endFlow",
value: function () {
var _endFlow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(sessionId) {
var reason,
activeFlow,
pendingRequests,
_iterator,
_step,
requestId,
_args4 = arguments;
return _regenerator().w(function (_context4) {
while (1) switch (_context4.n) {
case 0:
reason = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : 'manual';
activeFlow = this.activeFlows.get(sessionId);
if (!activeFlow) {
_context4.n = 1;
break;
}
_Logger["default"].info('Ending flow', {
sessionId: sessionId,
flowType: activeFlow.type,
reason: reason,
duration: Date.now() - activeFlow.startTime
});
// Cancel any pending PIN requests
if (activeFlow.type === 'TRANSFER') {
pendingRequests = this.webhookManager.getPendingRequests();
_iterator = _createForOfIteratorHelper(pendingRequests);
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
requestId = _step.value;
this.webhookManager.cancelPINRequest(requestId);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
// Move to history
this.moveFlowToHistory(sessionId, activeFlow, reason);
return _context4.a(2, true);
case 1:
return _context4.a(2, false);
}
}, _callee4, this);
}));
function endFlow(_x7) {
return _endFlow.apply(this, arguments);
}
return endFlow;
}()
}, {
key: "moveFlowToHistory",
value: function moveFlowToHistory(sessionId, activeFlow) {
var endReason = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'completed';
var historyEntry = {
type: activeFlow.type,
startTime: activeFlow.startTime,
endTime: Date.now(),
duration: Date.now() - activeFlow.startTime,
endReason: endReason,
finalState: activeFlow.flow.getState(),
transferData: activeFlow.type === 'TRANSFER' ? activeFlow.flow.getTransferData() : null
};
// Store in history (keep last 10 flows per session)
if (!this.flowHistory.has(sessionId)) {
this.flowHistory.set(sessionId, []);
}
var history = this.flowHistory.get(sessionId);
history.push(historyEntry);
// Keep only last 10 entries
if (history.length > 10) {
history.shift();
}
// Remove from active flows
this.activeFlows["delete"](sessionId);
_Logger["default"].debug('Flow moved to history', {
sessionId: sessionId,
historyEntry: historyEntry
});
}
}, {
key: "getActiveFlow",
value: function getActiveFlow(sessionId) {
return this.activeFlows.get(sessionId);
}
}, {
key: "getFlowHistory",
value: function getFlowHistory(sessionId) {
return this.flowHistory.get(sessionId) || [];
}
}, {
key: "isFlowActive",
value: function isFlowActive(sessionId) {
return this.activeFlows.has(sessionId);
}
}, {
key: "getActiveFlowType",
value: function getActiveFlowType(sessionId) {
var activeFlow = this.activeFlows.get(sessionId);
return activeFlow ? activeFlow.type : null;
}
}, {
key: "getActiveFlowState",
value: function getActiveFlowState(sessionId) {
var activeFlow = this.activeFlows.get(sessionId);
return activeFlow ? activeFlow.flow.getState() : null;
}
}, {
key: "getAllActiveSessions",
value: function getAllActiveSessions() {
return Array.from(this.activeFlows.keys());
}
}, {
key: "getFlowStats",
value: function getFlowStats() {
var activeCount = this.activeFlows.size;
var totalHistoryCount = Array.from(this.flowHistory.values()).reduce(function (sum, history) {
return sum + history.length;
}, 0);
var flowTypeStats = {};
var _iterator2 = _createForOfIteratorHelper(this.activeFlows),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _step2$value = _slicedToArray(_step2.value, 2),
sessionId = _step2$value[0],
activeFlow = _step2$value[1];
flowTypeStats[activeFlow.type] = (flowTypeStats[activeFlow.type] || 0) + 1;
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return {
activeFlows: activeCount,
totalCompletedFlows: totalHistoryCount,
activeFlowsByType: flowTypeStats,
activeSessions: this.getAllActiveSessions()
};
}
}, {
key: "cleanupStaleFlows",
value: function () {
var _cleanupStaleFlows = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5() {
var maxAgeMs,
now,
staleFlows,
_iterator3,
_step3,
_step3$value,
_sessionId,
activeFlow,
age,
_i,
_staleFlows,
sessionId,
_args5 = arguments;
return _regenerator().w(function (_context5) {
while (1) switch (_context5.n) {
case 0:
maxAgeMs = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : 1800000;
// 30 minutes default
now = Date.now();
staleFlows = [];
_iterator3 = _createForOfIteratorHelper(this.activeFlows);
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
_step3$value = _slicedToArray(_step3.value, 2), _sessionId = _step3$value[0], activeFlow = _step3$value[1];
age = now - activeFlow.lastActivity;
if (age > maxAgeMs) {
staleFlows.push(_sessionId);
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
_Logger["default"].info('Cleaning up stale flows', {
staleCount: staleFlows.length,
maxAge: maxAgeMs
});
_i = 0, _staleFlows = staleFlows;
case 1:
if (!(_i < _staleFlows.length)) {
_context5.n = 3;
break;
}
sessionId = _staleFlows[_i];
_context5.n = 2;
return this.endFlow(sessionId, 'timeout');
case 2:
_i++;
_context5.n = 1;
break;
case 3:
return _context5.a(2, staleFlows.length);
}
}, _callee5, this);
}));
function cleanupStaleFlows() {
return _cleanupStaleFlows.apply(this, arguments);
}
return cleanupStaleFlows;
}()
}, {
key: "resetFlow",
value: function () {
var _resetFlow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(sessionId) {
var activeFlow;
return _regenerator().w(function (_context6) {
while (1) switch (_context6.n) {
case 0:
activeFlow = this.activeFlows.get(sessionId);
if (!(activeFlow && activeFlow.flow.reset)) {
_context6.n = 1;
break;
}
_Logger["default"].info('Resetting flow', {
sessionId: sessionId,
flowType: activeFlow.type
});
activeFlow.flow.reset();
activeFlow.lastActivity = Date.now();
return _context6.a(2, {
response: "I've reset our conversation. How can I help you?",
sessionId: sessionId,
flowType: activeFlow.type,
state: activeFlow.flow.getState()
});
case 1:
return _context6.a(2, {
response: "No active conversation to reset. How can I help you?",
sessionId: sessionId
});
case 2:
return _context6.a(2);
}
}, _callee6, this);
}));
function resetFlow(_x8) {
return _resetFlow.apply(this, arguments);
}
return resetFlow;
}()
}, {
key: "cancelCurrentFlow",
value: function () {
var _cancelCurrentFlow = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(sessionId) {
var activeFlow, result;
return _regenerator().w(function (_context7) {
while (1) switch (_context7.n) {
case 0:
activeFlow = this.activeFlows.get(sessionId);
if (!activeFlow) {
_context7.n = 3;
break;
}
_Logger["default"].info('Cancelling current flow', {
sessionId: sessionId,
flowType: activeFlow.type
});
// Let the flow handle cancellation
_context7.n = 1;
return activeFlow.flow.handleIntent(_Constants.INTENTS.CANCEL, {});
case 1:
result = _context7.v;
_context7.n = 2;
return this.endFlow(sessionId, 'cancelled');
case 2:
return _context7.a(2, result);
case 3:
return _context7.a(2, {
response: "There's no active conversation to cancel. How can I help you?",
sessionId: sessionId
});
case 4:
return _context7.a(2);
}
}, _callee7, this);
}));
function cancelCurrentFlow(_x9) {
return _cancelCurrentFlow.apply(this, arguments);
}
return cancelCurrentFlow;
}()
}, {
key: "getFlowContext",
value: function getFlowContext(sessionId) {
var activeFlow = this.activeFlows.get(sessionId);
if (!activeFlow) {
return null;
}
return {
sessionId: sessionId,
flowType: activeFlow.type,
state: activeFlow.flow.getState(),
startTime: activeFlow.startTime,
lastActivity: activeFlow.lastActivity,
duration: Date.now() - activeFlow.startTime,
transferData: activeFlow.type === 'TRANSFER' ? activeFlow.flow.getTransferData() : null
};
}
}, {
key: "handlePINResponse",
value: function () {
var _handlePINResponse = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(requestId, pin) {
var signature,
result,
_args8 = arguments,
_t4;
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
});
// Delegate to webhook manager
_context8.n = 2;
return this.webhookManager.receivePIN(requestId, pin, signature);
case 2:
result = _context8.v;
return _context8.a(2, result);
case 3:
_context8.p = 3;
_t4 = _context8.v;
_Logger["default"].error('PIN response handling failed', {
error: _t4.message,
requestId: requestId
});
return _context8.a(2, {
success: false,
error: _t4.message
});
}
}, _callee8, this, [[1, 3]]);
}));
function handlePINResponse(_x0, _x1) {
return _handlePINResponse.apply(this, arguments);
}
return handlePINResponse;
}()
}, {
key: "cleanup",
value: function cleanup() {
_Logger["default"].info('Cleaning up flow manager', {
activeFlows: this.activeFlows.size,
totalHistory: Array.from(this.flowHistory.values()).reduce(function (sum, h) {
return sum + h.length;
}, 0)
});
// End all active flows
var activeSessionIds = Array.from(this.activeFlows.keys());
for (var _i2 = 0, _activeSessionIds = activeSessionIds; _i2 < _activeSessionIds.length; _i2++) {
var sessionId = _activeSessionIds[_i2];
this.endFlow(sessionId, 'cleanup');
}
// Clear history
this.flowHistory.clear();
}
}]);
}();
var _default = exports["default"] = FlowManager;