@scout9/app 1.0.0-alpha.0.8.9 → 1.0.0-alpha.0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,23 @@
1
1
  'use strict';
2
2
 
3
+ function _callSuper(t, o, e) {
4
+ return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
5
+ }
6
+ function _construct(t, e, r) {
7
+ if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
8
+ var o = [null];
9
+ o.push.apply(o, e);
10
+ var p = new (t.bind.apply(t, o))();
11
+ return r && _setPrototypeOf(p, r.prototype), p;
12
+ }
13
+ function _isNativeReflectConstruct() {
14
+ try {
15
+ var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
16
+ } catch (t) {}
17
+ return (_isNativeReflectConstruct = function () {
18
+ return !!t;
19
+ })();
20
+ }
3
21
  function _iterableToArrayLimit(r, l) {
4
22
  var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
5
23
  if (null != t) {
@@ -438,6 +456,68 @@ function _defineProperty(obj, key, value) {
438
456
  }
439
457
  return obj;
440
458
  }
459
+ function _inherits(subClass, superClass) {
460
+ if (typeof superClass !== "function" && superClass !== null) {
461
+ throw new TypeError("Super expression must either be null or a function");
462
+ }
463
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
464
+ constructor: {
465
+ value: subClass,
466
+ writable: true,
467
+ configurable: true
468
+ }
469
+ });
470
+ Object.defineProperty(subClass, "prototype", {
471
+ writable: false
472
+ });
473
+ if (superClass) _setPrototypeOf(subClass, superClass);
474
+ }
475
+ function _getPrototypeOf(o) {
476
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
477
+ return o.__proto__ || Object.getPrototypeOf(o);
478
+ };
479
+ return _getPrototypeOf(o);
480
+ }
481
+ function _setPrototypeOf(o, p) {
482
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
483
+ o.__proto__ = p;
484
+ return o;
485
+ };
486
+ return _setPrototypeOf(o, p);
487
+ }
488
+ function _isNativeFunction(fn) {
489
+ try {
490
+ return Function.toString.call(fn).indexOf("[native code]") !== -1;
491
+ } catch (e) {
492
+ return typeof fn === "function";
493
+ }
494
+ }
495
+ function _wrapNativeSuper(Class) {
496
+ var _cache = typeof Map === "function" ? new Map() : undefined;
497
+ _wrapNativeSuper = function _wrapNativeSuper(Class) {
498
+ if (Class === null || !_isNativeFunction(Class)) return Class;
499
+ if (typeof Class !== "function") {
500
+ throw new TypeError("Super expression must either be null or a function");
501
+ }
502
+ if (typeof _cache !== "undefined") {
503
+ if (_cache.has(Class)) return _cache.get(Class);
504
+ _cache.set(Class, Wrapper);
505
+ }
506
+ function Wrapper() {
507
+ return _construct(Class, arguments, _getPrototypeOf(this).constructor);
508
+ }
509
+ Wrapper.prototype = Object.create(Class.prototype, {
510
+ constructor: {
511
+ value: Wrapper,
512
+ enumerable: false,
513
+ writable: true,
514
+ configurable: true
515
+ }
516
+ });
517
+ return _setPrototypeOf(Wrapper, Class);
518
+ };
519
+ return _wrapNativeSuper(Class);
520
+ }
441
521
  function _objectWithoutPropertiesLoose(source, excluded) {
442
522
  if (source == null) return {};
443
523
  var target = {};
@@ -465,6 +545,20 @@ function _objectWithoutProperties(source, excluded) {
465
545
  }
466
546
  return target;
467
547
  }
548
+ function _assertThisInitialized(self) {
549
+ if (self === void 0) {
550
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
551
+ }
552
+ return self;
553
+ }
554
+ function _possibleConstructorReturn(self, call) {
555
+ if (call && (typeof call === "object" || typeof call === "function")) {
556
+ return call;
557
+ } else if (call !== void 0) {
558
+ throw new TypeError("Derived constructors may only return object or undefined");
559
+ }
560
+ return _assertThisInitialized(self);
561
+ }
468
562
  function _slicedToArray(arr, i) {
469
563
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
470
564
  }
@@ -616,7 +710,9 @@ function _classPrivateMethodInitSpec(obj, privateSet) {
616
710
  privateSet.add(obj);
617
711
  }
618
712
 
713
+ exports._assertThisInitialized = _assertThisInitialized;
619
714
  exports._asyncToGenerator = _asyncToGenerator;
715
+ exports._callSuper = _callSuper;
620
716
  exports._classCallCheck = _classCallCheck;
621
717
  exports._classPrivateFieldGet = _classPrivateFieldGet;
622
718
  exports._classPrivateFieldInitSpec = _classPrivateFieldInitSpec;
@@ -627,9 +723,11 @@ exports._classStaticPrivateFieldSpecGet = _classStaticPrivateFieldSpecGet;
627
723
  exports._createClass = _createClass;
628
724
  exports._createForOfIteratorHelper = _createForOfIteratorHelper;
629
725
  exports._defineProperty = _defineProperty;
726
+ exports._inherits = _inherits;
630
727
  exports._objectSpread2 = _objectSpread2;
631
728
  exports._objectWithoutProperties = _objectWithoutProperties;
632
729
  exports._regeneratorRuntime = _regeneratorRuntime;
633
730
  exports._slicedToArray = _slicedToArray;
634
731
  exports._toConsumableArray = _toConsumableArray;
635
732
  exports._typeof = _typeof;
733
+ exports._wrapNativeSuper = _wrapNativeSuper;
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-606d8129.cjs");
3
+ var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
4
4
  var require$$0$4 = require('util');
5
5
  var require$$0$3 = require('stream');
6
6
  var require$$1 = require('path');
@@ -21,7 +21,7 @@ var node_url = require('node:url');
21
21
  var node_events = require('node:events');
22
22
  var Stream = require('node:stream');
23
23
  var node_string_decoder = require('node:string_decoder');
24
- var macros = require("./macros-9ad79144.cjs");
24
+ var macros = require("./macros-77983cef.cjs");
25
25
  var readline = require('node:readline');
26
26
  var process$2 = require('node:process');
27
27
  var node_os = require('node:os');
@@ -35338,7 +35338,7 @@ function _loadUserPackageJson() {
35338
35338
  targetPkgUrl = isTest ? packageTestJsonUrl : packageJsonUrl;
35339
35339
  _context2.t0 = JSON;
35340
35340
  _context2.next = 10;
35341
- return fs__default["default"].readFile(new URL(targetPkgUrl, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('dev-c58715b1.js', document.baseURI).href))), 'utf-8');
35341
+ return fs__default["default"].readFile(new URL(targetPkgUrl, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('dev-024a0325.js', document.baseURI).href))), 'utf-8');
35342
35342
  case 10:
35343
35343
  _context2.t1 = _context2.sent;
35344
35344
  pkg = _context2.t0.parse.call(_context2.t0, _context2.t1);
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-606d8129.cjs");
4
- var dev = require("./dev-c58715b1.cjs");
5
- var macros = require("./macros-9ad79144.cjs");
3
+ var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
4
+ var dev = require("./dev-024a0325.cjs");
5
+ var macros = require("./macros-77983cef.cjs");
6
6
  var require$$0 = require('fs');
7
7
  var require$$2$1 = require('events');
8
8
  var require$$1 = require('path');
@@ -29483,7 +29483,7 @@ class Body {
29483
29483
  }
29484
29484
  const {
29485
29485
  toFormData
29486
- } = await Promise.resolve().then(function () { return require("./multipart-parser-ec335b80.cjs"); });
29486
+ } = await Promise.resolve().then(function () { return require("./multipart-parser-593e5511.cjs"); });
29487
29487
  return toFormData(this.body, ct);
29488
29488
  }
29489
29489
 
@@ -41884,7 +41884,7 @@ function _loadUserPackageJson() {
41884
41884
  targetPkgUrl = isTest ? packageTestJsonUrl : packageJsonUrl;
41885
41885
  _context.t0 = JSON;
41886
41886
  _context.next = 10;
41887
- return fs__default["default"].readFile(new URL(targetPkgUrl, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-9c237a7b.js', document.baseURI).href))), 'utf-8');
41887
+ return fs__default["default"].readFile(new URL(targetPkgUrl, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-93d877d8.js', document.baseURI).href))), 'utf-8');
41888
41888
  case 10:
41889
41889
  _context.t1 = _context.sent;
41890
41890
  pkg = _context.t0.parse.call(_context.t0, _context.t1);
@@ -43176,7 +43176,7 @@ var ProjectFiles = /*#__PURE__*/function () {
43176
43176
  return ProjectFiles;
43177
43177
  }();
43178
43178
 
43179
- var __filename$1 = node_url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-9c237a7b.js', document.baseURI).href)));
43179
+ var __filename$1 = node_url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-93d877d8.js', document.baseURI).href)));
43180
43180
  var __dirname$1 = path__default["default"].dirname(__filename$1);
43181
43181
  function zipDirectory(source, out) {
43182
43182
  var archive = archiver$1('tar', {
@@ -43391,7 +43391,7 @@ function _buildApp() {
43391
43391
  case 11:
43392
43392
  _context4.t0 = JSON;
43393
43393
  _context4.next = 14;
43394
- return fs__default["default"].readFile(new URL(templatePackagePath, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-9c237a7b.js', document.baseURI).href))), 'utf-8');
43394
+ return fs__default["default"].readFile(new URL(templatePackagePath, (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('index-93d877d8.js', document.baseURI).href))), 'utf-8');
43395
43395
  case 14:
43396
43396
  _context4.t1 = _context4.sent;
43397
43397
  packageTemplate = _context4.t0.parse.call(_context4.t0, _context4.t1);
package/dist/index.cjs CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require("./index-9c237a7b.cjs");
6
- var dev = require("./dev-c58715b1.cjs");
7
- require("./_rollupPluginBabelHelpers-606d8129.cjs");
8
- require("./macros-9ad79144.cjs");
5
+ var index = require("./index-93d877d8.cjs");
6
+ var dev = require("./dev-024a0325.cjs");
7
+ require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
8
+ require("./macros-77983cef.cjs");
9
9
  require('fs');
10
10
  require('events');
11
11
  require('path');
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-606d8129.cjs");
3
+ var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
4
4
 
5
5
  var util;
6
6
  (function (util) {
@@ -2,9 +2,9 @@
2
2
 
3
3
  require('node:fs');
4
4
  require('node:path');
5
- var index = require("./index-9c237a7b.cjs");
6
- require("./_rollupPluginBabelHelpers-606d8129.cjs");
7
- require("./dev-c58715b1.cjs");
5
+ var index = require("./index-93d877d8.cjs");
6
+ require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
7
+ require("./dev-024a0325.cjs");
8
8
  require('util');
9
9
  require('stream');
10
10
  require('path');
@@ -23,7 +23,7 @@ require('node:url');
23
23
  require('node:events');
24
24
  require('node:stream');
25
25
  require('node:string_decoder');
26
- require("./macros-9ad79144.cjs");
26
+ require("./macros-77983cef.cjs");
27
27
  require('node:readline');
28
28
  require('node:process');
29
29
  require('node:os');
package/dist/schemas.cjs CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var macros = require("./macros-9ad79144.cjs");
6
- require("./_rollupPluginBabelHelpers-606d8129.cjs");
5
+ var macros = require("./macros-77983cef.cjs");
6
+ require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
7
7
 
8
8
 
9
9
 
package/dist/spirits.cjs CHANGED
@@ -2,14 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-606d8129.cjs");
5
+ var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
6
6
 
7
7
  var _excluded = ["keywords"];
8
8
  /**
9
9
  * @typedef {Object} Document
10
10
  * @property {string} id
11
11
  */
12
-
13
12
  /**
14
13
  * Represents a change with before and after states of a given type.
15
14
  * @template Type The type of the before and after properties.
@@ -17,7 +16,6 @@ var _excluded = ["keywords"];
17
16
  * @property {Type} before - The state before the change.
18
17
  * @property {Type} after - The state after the change.
19
18
  */
20
-
21
19
  /**
22
20
  * @typedef {Object} ConversationData
23
21
  * @property {import('@scout9/app').Scout9ProjectBuildConfig} config - used to define generation and extract persona metadata
@@ -28,7 +26,6 @@ var _excluded = ["keywords"];
28
26
  * @property {import('@scout9/app').ConversationProgress} progress - progress checklist for manual/auto ingress workflows
29
27
  * @property {any} context - event context
30
28
  */
31
-
32
29
  /**
33
30
  * @typedef {Object} ParseOutput
34
31
  * @property {Array<import('@scout9/admin').Message>} messages
@@ -36,7 +33,6 @@ var _excluded = ["keywords"];
36
33
  * @property {import('@scout9/admin').Message} message
37
34
  * @property {any} context
38
35
  */
39
-
40
36
  /**
41
37
  * @typedef {Object} WorkflowOutput
42
38
  * @property {Array<import('@scout9/app').WorkflowResponseSlot>} slots
@@ -44,7 +40,6 @@ var _excluded = ["keywords"];
44
40
  * @property {import('@scout9/app').Conversation} conversation
45
41
  * @property {any} context
46
42
  */
47
-
48
43
  /**
49
44
  * @typedef {Object} GenerateOutput
50
45
  * @property {import('@scout9/admin').GenerateResponse | undefined} generate
@@ -52,44 +47,37 @@ var _excluded = ["keywords"];
52
47
  * @property {import('@scout9/app').Conversation} conversation
53
48
  * @property {any} context
54
49
  */
55
-
56
50
  /**
57
51
  * @callback ParseFun
58
52
  * @param {string} message - message to send
59
53
  * @param {string | undefined} language - language to parse in, defaults to "en" for english
60
54
  * @returns {Promise<import('@scout9/admin').ParseResponse>}
61
55
  */
62
-
63
56
  /**
64
57
  * @callback ContextualizerFun
65
58
  * @param {Pick<import('@scout9/app').WorkflowEvent, 'messages' | 'conversation'>} args - message to send
66
59
  * @returns {Promise<import('@scout9/app').WorkflowEvent['messages']>}
67
60
  */
68
-
69
61
  /**
70
62
  * @callback WorkflowFun
71
63
  * @param {import('@scout9/app').WorkflowEvent} event - conversation data
72
64
  * @returns {Promise<import('@scout9/app').WorkflowResponse>}
73
65
  */
74
-
75
66
  /**
76
67
  * @callback GenerateFun
77
68
  * @param {import('@scout9/admin').GenerateRequestOneOf1} data - data to generate from
78
69
  * @returns {Promise<import('@scout9/admin').GenerateResponse>}
79
70
  */
80
-
81
71
  /**
82
72
  * @callback TransformerFun
83
73
  * @param {import('@scout9/admin').PmtTransformRequest} data - data to generate from
84
74
  * @returns {Promise<import('@scout9/admin').PmtTransformResponse>}
85
75
  */
86
-
87
76
  /**
88
77
  * @callback IdGeneratorFun
89
78
  * @param {import('@scout9/admin').Message['role']} prefix
90
79
  * @returns {string}
91
80
  */
92
-
93
81
  /**
94
82
  * @callback StatusCallback
95
83
  * @param {string} message
@@ -98,7 +86,6 @@ var _excluded = ["keywords"];
98
86
  * @param {any | undefined} [payload]
99
87
  * @returns {void}
100
88
  */
101
-
102
89
  /**
103
90
  * @typedef {Object} CustomerSpiritCallbacks
104
91
  * @property {ParseFun} parser
@@ -109,7 +96,47 @@ var _excluded = ["keywords"];
109
96
  * @property {IdGeneratorFun} idGenerator
110
97
  * @property {StatusCallback | undefined} [progress]
111
98
  */
99
+ var SpiritError = /*#__PURE__*/function (_Error) {
100
+ _rollupPluginBabelHelpers._inherits(SpiritError, _Error);
101
+ /**
102
+ * @param {string} message - Description of the error.
103
+ * @param {string} step - The step or phase in which the error occurred.
104
+ */
105
+ function SpiritError(message, step) {
106
+ var _this;
107
+ _rollupPluginBabelHelpers._classCallCheck(this, SpiritError);
108
+ _this = _rollupPluginBabelHelpers._callSuper(this, SpiritError, [message]);
109
+ _this.name = _this.constructor.name;
110
+ _this.step = step;
111
+
112
+ // Ensures the stack trace starts from where this error was created
113
+ if (Error.captureStackTrace) {
114
+ Error.captureStackTrace(_rollupPluginBabelHelpers._assertThisInitialized(_this), _this.constructor);
115
+ }
116
+ return _this;
117
+ }
112
118
 
119
+ /**
120
+ *
121
+ * @param {unknown} err
122
+ * @param {string} step
123
+ * @returns {SpiritError}
124
+ */
125
+ _rollupPluginBabelHelpers._createClass(SpiritError, null, [{
126
+ key: "fromError",
127
+ value: function fromError(err, step) {
128
+ if (err instanceof SpiritError) return err;
129
+ if (err instanceof Error) {
130
+ var wrapped = new SpiritError(err.message, step);
131
+ wrapped.stack = err.stack;
132
+ return wrapped;
133
+ }
134
+ // fallback for non-Error values
135
+ return new SpiritError(String(err), step);
136
+ }
137
+ }]);
138
+ return SpiritError;
139
+ }( /*#__PURE__*/_rollupPluginBabelHelpers._wrapNativeSuper(Error));
113
140
  /**
114
141
  * @typedef {Object} ConversationEvent
115
142
  * @property {(Change<import('@scout9/app').Conversation> & {
@@ -130,7 +157,7 @@ var Spirits = {
130
157
  * @returns {Promise<ConversationEvent>}
131
158
  */
132
159
  customer: function () {
133
- var _customer = _rollupPluginBabelHelpers._asyncToGenerator( /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _callee(input) {
160
+ var _customer = _rollupPluginBabelHelpers._asyncToGenerator( /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _callee2(input) {
134
161
  var _recentUserMessage;
135
162
  var onError,
136
163
  customer,
@@ -161,6 +188,8 @@ var Spirits = {
161
188
  incrementLockAttempt,
162
189
  _addInstruction,
163
190
  addInstruction,
191
+ onStatus,
192
+ wrapStep,
164
193
  persona,
165
194
  invalidRoles,
166
195
  parsePayload,
@@ -233,11 +262,11 @@ var Spirits = {
233
262
  transformResponse,
234
263
  _agentMessages,
235
264
  _lastAgentMessage,
236
- _args3 = arguments;
237
- return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _callee$(_context3) {
238
- while (1) switch (_context3.prev = _context3.next) {
265
+ _args4 = arguments;
266
+ return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _callee2$(_context4) {
267
+ while (1) switch (_context4.prev = _context4.next) {
239
268
  case 0:
240
- onError = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : function () {};
269
+ onError = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : function () {};
241
270
  customer = input.customer, config = input.config, parser = input.parser, contextualizer = input.contextualizer, workflow = input.workflow, generator = input.generator, transformer = input.transformer, idGenerator = input.idGenerator, _input$progress = input.progress, progress = _input$progress === void 0 ? function (message, level, type, payload) {} : _input$progress, messageBefore = input.message, contextBefore = input.context, messagesBefore = input.messages, conversationBefore = input.conversation;
242
271
  conversation = input.conversation, messages = input.messages, context = input.context, message = input.message; // Storing post process events here
243
272
  followup = [];
@@ -328,42 +357,78 @@ var Spirits = {
328
357
  if (changedConversation) {
329
358
  progress('Updated conversation', 'info', 'UPDATE_CONVERSATION', newConversation);
330
359
  }
331
- }; // 1. Check inputs
360
+ };
361
+ onStatus = function onStatus(statusType) {
362
+ var completeOrError = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
363
+ progress("".concat(statusType, ": ").concat(completeOrError), 'info', 'STATUS', _rollupPluginBabelHelpers._defineProperty({}, statusType, completeOrError));
364
+ };
365
+ /**
366
+ * @param {Promise<any>} prom
367
+ * @param {string} step
368
+ */
369
+ wrapStep = /*#__PURE__*/function () {
370
+ var _ref = _rollupPluginBabelHelpers._asyncToGenerator( /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _callee(prom, step) {
371
+ var result;
372
+ return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _callee$(_context) {
373
+ while (1) switch (_context.prev = _context.next) {
374
+ case 0:
375
+ _context.prev = 0;
376
+ _context.next = 3;
377
+ return prom;
378
+ case 3:
379
+ result = _context.sent;
380
+ onStatus(step);
381
+ return _context.abrupt("return", result);
382
+ case 8:
383
+ _context.prev = 8;
384
+ _context.t0 = _context["catch"](0);
385
+ onStatus(step, _context.t0.message || 'UNHANDLED ERROR');
386
+ throw SpiritError.fromError(_context.t0, step);
387
+ case 12:
388
+ case "end":
389
+ return _context.stop();
390
+ }
391
+ }, _callee, null, [[0, 8]]);
392
+ }));
393
+ return function wrapStep(_x2, _x3) {
394
+ return _ref.apply(this, arguments);
395
+ };
396
+ }(); // 1. Check inputs
332
397
  if (conversation.$agent) {
333
- _context3.next = 15;
398
+ _context4.next = 17;
334
399
  break;
335
400
  }
336
401
  throw new Error("SpiritsError: No agent found in conversation, must define \".$agent\" in the conversation");
337
- case 15:
402
+ case 17:
338
403
  persona = (config.persona || config.personas || config.agents).find(function (p) {
339
404
  return p.id === conversation.$agent;
340
405
  });
341
406
  if (persona) {
342
- _context3.next = 20;
407
+ _context4.next = 22;
343
408
  break;
344
409
  }
345
410
  if (!(config.persona || config.personas || config.agents).some(function (a) {
346
411
  return !a.id;
347
412
  })) {
348
- _context3.next = 19;
413
+ _context4.next = 21;
349
414
  break;
350
415
  }
351
416
  throw new Error("SpiritsError: No persona found (\"".concat(conversation.$agent, "\") in provided config, some persona's did not contain an \"id\" (Internal Mapping Error)"));
352
- case 19:
417
+ case 21:
353
418
  throw new Error("SpiritsError: No persona found (\"".concat(conversation.$agent, "\") in provided config"));
354
- case 20:
419
+ case 22:
355
420
  if (messages.every(function (m) {
356
421
  return !!m.id;
357
422
  })) {
358
- _context3.next = 22;
423
+ _context4.next = 24;
359
424
  break;
360
425
  }
361
426
  throw new Error("SpiritsError: Every message must have an \".id\", ensure all messages have an id assigned before running");
362
- case 22:
427
+ case 24:
363
428
  if (messages.every(function (m) {
364
429
  return m.role === 'customer' || m.role === 'agent' || m.role === 'system' || m.role === 'tool';
365
430
  })) {
366
- _context3.next = 25;
431
+ _context4.next = 27;
367
432
  break;
368
433
  }
369
434
  invalidRoles = messages.filter(function (m) {
@@ -372,7 +437,7 @@ var Spirits = {
372
437
  throw new Error("SpiritsError: Every message must have a role of \"customer\", \"agent\", or \"system\". Got invalid roles: ".concat(invalidRoles.map(function (m) {
373
438
  return m.role;
374
439
  }).join(', ')));
375
- case 25:
440
+ case 27:
376
441
  // if message is not in messages, then add it
377
442
  if (!messages.find(function (m) {
378
443
  return m.id === input.message.id;
@@ -382,10 +447,10 @@ var Spirits = {
382
447
 
383
448
  // 2. Parse the message
384
449
  progress('Parsing message', 'info', 'SET_PROCESSING', 'user');
385
- _context3.next = 29;
386
- return parser(message.content, 'en');
387
- case 29:
388
- parsePayload = _context3.sent;
450
+ _context4.next = 31;
451
+ return wrapStep(parser(message.content, 'en'), 'parse');
452
+ case 31:
453
+ parsePayload = _context4.sent;
389
454
  if (parsePayload.intent) {
390
455
  message.intent = parsePayload.intent;
391
456
  }
@@ -474,19 +539,19 @@ var Spirits = {
474
539
 
475
540
  // 3. Run the contextualizer
476
541
  progress('Running contextualizer', 'info', 'SET_PROCESSING', 'system');
477
- _context3.next = 47;
478
- return contextualizer({
542
+ _context4.next = 49;
543
+ return wrapStep(contextualizer({
479
544
  conversation: conversation,
480
545
  messages: messages
481
- });
482
- case 47:
483
- newContextMessages = _context3.sent;
546
+ }), 'contextualize');
547
+ case 49:
548
+ newContextMessages = _context4.sent;
484
549
  _iterator = _rollupPluginBabelHelpers._createForOfIteratorHelper(newContextMessages);
485
- _context3.prev = 49;
550
+ _context4.prev = 51;
486
551
  _loop = /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _loop() {
487
552
  var contextMessage;
488
- return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _loop$(_context) {
489
- while (1) switch (_context.prev = _context.next) {
553
+ return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _loop$(_context2) {
554
+ while (1) switch (_context2.prev = _context2.next) {
490
555
  case 0:
491
556
  contextMessage = _step.value;
492
557
  if (!messages.find(function (mes) {
@@ -499,36 +564,36 @@ var Spirits = {
499
564
  }
500
565
  case 2:
501
566
  case "end":
502
- return _context.stop();
567
+ return _context2.stop();
503
568
  }
504
569
  }, _loop);
505
570
  });
506
571
  _iterator.s();
507
- case 52:
572
+ case 54:
508
573
  if ((_step = _iterator.n()).done) {
509
- _context3.next = 56;
574
+ _context4.next = 58;
510
575
  break;
511
576
  }
512
- return _context3.delegateYield(_loop(), "t0", 54);
513
- case 54:
514
- _context3.next = 52;
515
- break;
577
+ return _context4.delegateYield(_loop(), "t0", 56);
516
578
  case 56:
517
- _context3.next = 61;
579
+ _context4.next = 54;
518
580
  break;
519
581
  case 58:
520
- _context3.prev = 58;
521
- _context3.t1 = _context3["catch"](49);
522
- _iterator.e(_context3.t1);
523
- case 61:
524
- _context3.prev = 61;
582
+ _context4.next = 63;
583
+ break;
584
+ case 60:
585
+ _context4.prev = 60;
586
+ _context4.t1 = _context4["catch"](51);
587
+ _iterator.e(_context4.t1);
588
+ case 63:
589
+ _context4.prev = 63;
525
590
  _iterator.f();
526
- return _context3.finish(61);
527
- case 64:
591
+ return _context4.finish(63);
592
+ case 66:
528
593
  // 4. Run the workflow
529
594
  progress('Running workflow', 'info', 'SET_PROCESSING', 'system');
530
- _context3.next = 67;
531
- return workflow({
595
+ _context4.next = 69;
596
+ return wrapStep(workflow({
532
597
  messages: messages,
533
598
  conversation: conversation,
534
599
  context: context,
@@ -543,7 +608,7 @@ var Spirits = {
543
608
  initial: conversation.intent || null
544
609
  },
545
610
  stagnationCount: conversation.lockAttempts || 0
546
- }).then(function (res) {
611
+ }), 'workflow').then(function (res) {
547
612
  return Array.isArray(res) ? res : [res];
548
613
  }).then(function (slots) {
549
614
  return slots.reduce(function (accumulator, slot) {
@@ -556,8 +621,8 @@ var Spirits = {
556
621
  return accumulator;
557
622
  }, []);
558
623
  });
559
- case 67:
560
- slots = _context3.sent;
624
+ case 69:
625
+ slots = _context4.sent;
561
626
  hasNoInstructions = slots.every(function (s) {
562
627
  return !s.instructions || Array.isArray(s.instructions) && s.instructions.length === 0;
563
628
  });
@@ -583,20 +648,20 @@ var Spirits = {
583
648
  resettedIntent = false;
584
649
  /** @type {Array<string> | undefined} */
585
650
  _iterator2 = _rollupPluginBabelHelpers._createForOfIteratorHelper(slots);
586
- _context3.prev = 75;
651
+ _context4.prev = 77;
587
652
  _iterator2.s();
588
- case 77:
653
+ case 79:
589
654
  if ((_step2 = _iterator2.n()).done) {
590
- _context3.next = 148;
655
+ _context4.next = 150;
591
656
  break;
592
657
  }
593
658
  _step2$value = _step2.value, forward = _step2$value.forward, forwardNote = _step2$value.forwardNote, instructions = _step2$value.instructions, removeInstructions = _step2$value.removeInstructions, manualMessage = _step2$value.message, scheduled = _step2$value.scheduled, resetIntent = _step2$value.resetIntent, secondsDelay = _step2$value.secondsDelay, contextUpsert = _step2$value.contextUpsert, anticipate = _step2$value.anticipate, slotFollowup = _step2$value.followup, slotEntityContextUpsert = _step2$value.entityContextUpsert, tasks = _step2$value.tasks;
594
659
  if (!anticipate) {
595
- _context3.next = 92;
660
+ _context4.next = 94;
596
661
  break;
597
662
  }
598
663
  if (!Array.isArray(anticipate)) {
599
- _context3.next = 87;
664
+ _context4.next = 89;
600
665
  break;
601
666
  }
602
667
  // 'literal' anticipation
@@ -616,11 +681,11 @@ var Spirits = {
616
681
  slots: _slots,
617
682
  map: map
618
683
  });
619
- _context3.next = 92;
684
+ _context4.next = 94;
620
685
  break;
621
- case 87:
686
+ case 89:
622
687
  if (!('yes' in anticipate && 'no' in anticipate && 'did' in anticipate)) {
623
- _context3.next = 91;
688
+ _context4.next = 93;
624
689
  break;
625
690
  }
626
691
  // "did" anticipation
@@ -632,11 +697,11 @@ var Spirits = {
632
697
  },
633
698
  did: anticipate.did
634
699
  });
635
- _context3.next = 92;
700
+ _context4.next = 94;
636
701
  break;
637
- case 91:
702
+ case 93:
638
703
  throw new Error("Invalid anticipate payload \"".concat(JSON.stringify(anticipate), "\""));
639
- case 92:
704
+ case 94:
640
705
  // tasks from auto/manual ingress to execute
641
706
  if (!!tasks && Array.isArray(tasks) && !!tasks.length) {
642
707
  if (!_tasks) _tasks = [];
@@ -692,19 +757,19 @@ var Spirits = {
692
757
 
693
758
  // Insert instructions context
694
759
  if (!instructions) {
695
- _context3.next = 111;
760
+ _context4.next = 113;
696
761
  break;
697
762
  }
698
763
  if (!(typeof instructions === 'string')) {
699
- _context3.next = 101;
764
+ _context4.next = 103;
700
765
  break;
701
766
  }
702
767
  addInstruction(instructions, previousLockAttempt);
703
- _context3.next = 111;
768
+ _context4.next = 113;
704
769
  break;
705
- case 101:
770
+ case 103:
706
771
  if (!Array.isArray(instructions)) {
707
- _context3.next = 106;
772
+ _context4.next = 108;
708
773
  break;
709
774
  }
710
775
  _iterator5 = _rollupPluginBabelHelpers._createForOfIteratorHelper(instructions);
@@ -722,29 +787,29 @@ var Spirits = {
722
787
  } finally {
723
788
  _iterator5.f();
724
789
  }
725
- _context3.next = 111;
790
+ _context4.next = 113;
726
791
  break;
727
- case 106:
792
+ case 108:
728
793
  if (!(_rollupPluginBabelHelpers._typeof(instructions) === 'object' && 'content' in instructions)) {
729
- _context3.next = 110;
794
+ _context4.next = 112;
730
795
  break;
731
796
  }
732
797
  addInstruction(instructions.content, previousLockAttempt, instructions.id);
733
- _context3.next = 111;
798
+ _context4.next = 113;
734
799
  break;
735
- case 110:
800
+ case 112:
736
801
  throw new Error("SpiritsError: instructions must be a string or array or {content: \"<instruction>\"}, got: ".concat(JSON.stringify(instructions)));
737
- case 111:
802
+ case 113:
738
803
  if (!removeInstructions) {
739
- _context3.next = 128;
804
+ _context4.next = 130;
740
805
  break;
741
806
  }
742
807
  _iterator6 = _rollupPluginBabelHelpers._createForOfIteratorHelper(removeInstructions);
743
- _context3.prev = 113;
808
+ _context4.prev = 115;
744
809
  _loop2 = /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _loop2() {
745
810
  var instructionId, index;
746
- return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _loop2$(_context2) {
747
- while (1) switch (_context2.prev = _context2.next) {
811
+ return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _loop2$(_context3) {
812
+ while (1) switch (_context3.prev = _context3.next) {
748
813
  case 0:
749
814
  instructionId = _step6.value;
750
815
  index = messages.findIndex(function (m) {
@@ -760,34 +825,34 @@ var Spirits = {
760
825
  }
761
826
  case 3:
762
827
  case "end":
763
- return _context2.stop();
828
+ return _context3.stop();
764
829
  }
765
830
  }, _loop2);
766
831
  });
767
832
  _iterator6.s();
768
- case 116:
833
+ case 118:
769
834
  if ((_step6 = _iterator6.n()).done) {
770
- _context3.next = 120;
835
+ _context4.next = 122;
771
836
  break;
772
837
  }
773
- return _context3.delegateYield(_loop2(), "t2", 118);
774
- case 118:
775
- _context3.next = 116;
776
- break;
838
+ return _context4.delegateYield(_loop2(), "t2", 120);
777
839
  case 120:
778
- _context3.next = 125;
840
+ _context4.next = 118;
779
841
  break;
780
842
  case 122:
781
- _context3.prev = 122;
782
- _context3.t3 = _context3["catch"](113);
783
- _iterator6.e(_context3.t3);
784
- case 125:
785
- _context3.prev = 125;
843
+ _context4.next = 127;
844
+ break;
845
+ case 124:
846
+ _context4.prev = 124;
847
+ _context4.t3 = _context4["catch"](115);
848
+ _iterator6.e(_context4.t3);
849
+ case 127:
850
+ _context4.prev = 127;
786
851
  _iterator6.f();
787
- return _context3.finish(125);
788
- case 128:
852
+ return _context4.finish(127);
853
+ case 130:
789
854
  if (!manualMessage) {
790
- _context3.next = 144;
855
+ _context4.next = 146;
791
856
  break;
792
857
  }
793
858
  /** @type {import('@scout9/admin').Message} */
@@ -799,25 +864,25 @@ var Spirits = {
799
864
  time: new Date().toISOString()
800
865
  };
801
866
  if (!(_rollupPluginBabelHelpers._typeof(manualMessage) === 'object')) {
802
- _context3.next = 136;
867
+ _context4.next = 138;
803
868
  break;
804
869
  }
805
870
  Object.assign(manualMessageObj, manualMessage);
806
871
  manualMessageObj.role = 'agent';
807
872
  manualMessageObj.time = new Date().toISOString();
808
- _context3.next = 141;
873
+ _context4.next = 143;
809
874
  break;
810
- case 136:
875
+ case 138:
811
876
  if (!(typeof manualMessage === 'string')) {
812
- _context3.next = 140;
877
+ _context4.next = 142;
813
878
  break;
814
879
  }
815
880
  manualMessageObj.content = manualMessage;
816
- _context3.next = 141;
881
+ _context4.next = 143;
817
882
  break;
818
- case 140:
883
+ case 142:
819
884
  throw new Error('Manual message must be of type "string" or "DirectMessage"');
820
- case 141:
885
+ case 143:
821
886
  if (scheduled) {
822
887
  manualMessageObj.time = new Date(scheduled * 1000).toISOString();
823
888
  manualMessageObj.scheduled = manualMessageObj.time;
@@ -829,7 +894,7 @@ var Spirits = {
829
894
  }
830
895
  messages.push(manualMessageObj);
831
896
  progress('Added manual message', 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
832
- case 144:
897
+ case 146:
833
898
  if (contextUpsert) {
834
899
  context = updateContext(context, contextUpsert);
835
900
  progress('Upserted context', 'info', 'UPDATE_CONTEXT', contextUpsert);
@@ -837,21 +902,21 @@ var Spirits = {
837
902
  if (resetIntent) {
838
903
  resettedIntent = true;
839
904
  }
840
- case 146:
841
- _context3.next = 77;
842
- break;
843
905
  case 148:
844
- _context3.next = 153;
906
+ _context4.next = 79;
845
907
  break;
846
908
  case 150:
847
- _context3.prev = 150;
848
- _context3.t4 = _context3["catch"](75);
849
- _iterator2.e(_context3.t4);
850
- case 153:
851
- _context3.prev = 153;
909
+ _context4.next = 155;
910
+ break;
911
+ case 152:
912
+ _context4.prev = 152;
913
+ _context4.t4 = _context4["catch"](77);
914
+ _iterator2.e(_context4.t4);
915
+ case 155:
916
+ _context4.prev = 155;
852
917
  _iterator2.f();
853
- return _context3.finish(153);
854
- case 156:
918
+ return _context4.finish(155);
919
+ case 158:
855
920
  if (resettedIntent && !_forward) {
856
921
  conversation.intent = null;
857
922
  conversation.intentScore = null;
@@ -870,14 +935,14 @@ var Spirits = {
870
935
  // 5. Generate response
871
936
  // If conversation previously locked, don't generate
872
937
  if (input.conversation.locked) {
873
- _context3.next = 208;
938
+ _context4.next = 215;
874
939
  break;
875
940
  }
876
941
  if (!((!conversation.locked || !hasNoInstructions) && !!hasNoCustomMessage)) {
877
- _context3.next = 174;
942
+ _context4.next = 178;
878
943
  break;
879
944
  }
880
- _context3.prev = 159;
945
+ _context4.prev = 161;
881
946
  progress('Generating message', 'info', 'SET_PROCESSING', 'system');
882
947
 
883
948
  /** @type {import('@scout9/admin').GenerateRequestOneOf1} */
@@ -891,10 +956,10 @@ var Spirits = {
891
956
  if (!!_tasks && Array.isArray(_tasks) && !!_tasks.length) {
892
957
  generatorInput.tasks = _tasks;
893
958
  }
894
- _context3.next = 165;
895
- return generator(generatorInput);
896
- case 165:
897
- generatorPayload = _context3.sent;
959
+ _context4.next = 167;
960
+ return wrapStep(generator(generatorInput), 'generate');
961
+ case 167:
962
+ generatorPayload = _context4.sent;
898
963
  if (!generatorPayload.send) {
899
964
  progress('Generated response', 'failed', undefined, {
900
965
  error: ((_generatorPayload$err = generatorPayload.errors) === null || _generatorPayload$err === void 0 ? void 0 : _generatorPayload$err.join('\n\n')) || 'Unknown Reason'
@@ -933,10 +998,10 @@ var Spirits = {
933
998
  };
934
999
 
935
1000
  // Copy any other non-nullish fields without overwriting base
936
- return Object.entries(message).reduce(function (acc, _ref) {
937
- var _ref2 = _rollupPluginBabelHelpers._slicedToArray(_ref, 2),
938
- key = _ref2[0],
939
- value = _ref2[1];
1001
+ return Object.entries(message).reduce(function (acc, _ref2) {
1002
+ var _ref3 = _rollupPluginBabelHelpers._slicedToArray(_ref2, 2),
1003
+ key = _ref3[0],
1004
+ value = _ref3[1];
940
1005
  if (!Object.prototype.hasOwnProperty.call(acc, key) && value != null) {
941
1006
  acc[key] = value;
942
1007
  }
@@ -984,39 +1049,44 @@ var Spirits = {
984
1049
  }
985
1050
  }
986
1051
  }
987
- _context3.next = 174;
1052
+ _context4.next = 176;
1053
+ break;
1054
+ case 171:
1055
+ _context4.prev = 171;
1056
+ _context4.t5 = _context4["catch"](161);
1057
+ onError(_context4.t5);
1058
+ console.error("Spirits: Locking conversation, error generating response: ".concat(_context4.t5.message));
1059
+ conversation = lockConversation(conversation, 'API: ' + _context4.t5.message);
1060
+ case 176:
1061
+ _context4.next = 179;
988
1062
  break;
989
- case 169:
990
- _context3.prev = 169;
991
- _context3.t5 = _context3["catch"](159);
992
- onError(_context3.t5);
993
- console.error("Spirits: Locking conversation, error generating response: ".concat(_context3.t5.message));
994
- conversation = lockConversation(conversation, 'API: ' + _context3.t5.message);
995
- case 174:
1063
+ case 178:
1064
+ onStatus('generate', 'ignored');
1065
+ case 179:
996
1066
  if (!(messagesToTransform.length && transformer)) {
997
- _context3.next = 207;
1067
+ _context4.next = 212;
998
1068
  break;
999
1069
  }
1000
- _context3.prev = 175;
1070
+ _context4.prev = 180;
1001
1071
  _iterator4 = _rollupPluginBabelHelpers._createForOfIteratorHelper(messagesToTransform);
1002
- _context3.prev = 177;
1072
+ _context4.prev = 182;
1003
1073
  _iterator4.s();
1004
- case 179:
1074
+ case 184:
1005
1075
  if ((_step4 = _iterator4.n()).done) {
1006
- _context3.next = 190;
1076
+ _context4.next = 195;
1007
1077
  break;
1008
1078
  }
1009
1079
  _step4.value;
1010
- _context3.next = 183;
1011
- return transformer({
1080
+ _context4.next = 188;
1081
+ return wrapStep(transformer({
1012
1082
  message: messagesToTransform,
1013
1083
  persona: persona,
1014
1084
  customer: customer.id,
1015
1085
  messages: messages,
1016
1086
  context: context
1017
- });
1018
- case 183:
1019
- transformResponse = _context3.sent;
1087
+ }), 'transform');
1088
+ case 188:
1089
+ transformResponse = _context4.sent;
1020
1090
  progress('Generated response', 'success', undefined, undefined);
1021
1091
  // Check if already had message
1022
1092
  _agentMessages = messages.filter(function (m) {
@@ -1035,39 +1105,46 @@ var Spirits = {
1035
1105
  });
1036
1106
  progress('Added agent message', 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
1037
1107
  }
1038
- case 188:
1039
- _context3.next = 179;
1108
+ case 193:
1109
+ _context4.next = 184;
1040
1110
  break;
1041
- case 190:
1042
- _context3.next = 195;
1043
- break;
1044
- case 192:
1045
- _context3.prev = 192;
1046
- _context3.t6 = _context3["catch"](177);
1047
- _iterator4.e(_context3.t6);
1048
1111
  case 195:
1049
- _context3.prev = 195;
1050
- _iterator4.f();
1051
- return _context3.finish(195);
1052
- case 198:
1053
- _context3.next = 205;
1112
+ _context4.next = 200;
1054
1113
  break;
1114
+ case 197:
1115
+ _context4.prev = 197;
1116
+ _context4.t6 = _context4["catch"](182);
1117
+ _iterator4.e(_context4.t6);
1055
1118
  case 200:
1056
- _context3.prev = 200;
1057
- _context3.t7 = _context3["catch"](175);
1058
- console.error("Locking conversation, error transforming response: ".concat(_context3.t7.message));
1059
- conversation = lockConversation(conversation, 'API: ' + _context3.t7.message);
1060
- onError(_context3.t7);
1119
+ _context4.prev = 200;
1120
+ _iterator4.f();
1121
+ return _context4.finish(200);
1122
+ case 203:
1123
+ _context4.next = 210;
1124
+ break;
1061
1125
  case 205:
1062
- _context3.next = 208;
1126
+ _context4.prev = 205;
1127
+ _context4.t7 = _context4["catch"](180);
1128
+ console.error("Locking conversation, error transforming response: ".concat(_context4.t7.message));
1129
+ conversation = lockConversation(conversation, 'API: ' + _context4.t7.message);
1130
+ onError(_context4.t7);
1131
+ case 210:
1132
+ _context4.next = 213;
1063
1133
  break;
1064
- case 207:
1134
+ case 212:
1065
1135
  if (messagesToTransform.length) {
1066
1136
  console.warn("No transformer provided");
1137
+ onStatus('transform', 'ignored');
1067
1138
  }
1068
- case 208:
1139
+ case 213:
1140
+ _context4.next = 217;
1141
+ break;
1142
+ case 215:
1143
+ onStatus('generate', 'ignored');
1144
+ onStatus('transform', 'ignored');
1145
+ case 217:
1069
1146
  progress('Parsing message', 'info', 'SET_PROCESSING', null);
1070
- return _context3.abrupt("return", {
1147
+ return _context4.abrupt("return", {
1071
1148
  conversation: {
1072
1149
  before: conversationBefore,
1073
1150
  after: conversation,
@@ -1089,11 +1166,11 @@ var Spirits = {
1089
1166
  followup: followup,
1090
1167
  entityContextUpsert: entityContextUpsert
1091
1168
  });
1092
- case 210:
1169
+ case 219:
1093
1170
  case "end":
1094
- return _context3.stop();
1171
+ return _context4.stop();
1095
1172
  }
1096
- }, _callee, null, [[49, 58, 61, 64], [75, 150, 153, 156], [113, 122, 125, 128], [159, 169], [175, 200], [177, 192, 195, 198]]);
1173
+ }, _callee2, null, [[51, 60, 63, 66], [77, 152, 155, 158], [115, 124, 127, 130], [161, 171], [180, 205], [182, 197, 200, 203]]);
1097
1174
  }));
1098
1175
  function customer(_x) {
1099
1176
  return _customer.apply(this, arguments);
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var dev = require("./dev-c58715b1.cjs");
6
- require("./_rollupPluginBabelHelpers-606d8129.cjs");
5
+ var dev = require("./dev-024a0325.cjs");
6
+ require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
7
7
  require('util');
8
8
  require('stream');
9
9
  require('path');
@@ -24,7 +24,7 @@ require('node:url');
24
24
  require('node:events');
25
25
  require('node:stream');
26
26
  require('node:string_decoder');
27
- require("./macros-9ad79144.cjs");
27
+ require("./macros-77983cef.cjs");
28
28
  require('node:readline');
29
29
  require('node:process');
30
30
  require('node:os');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scout9/app",
3
- "version": "1.0.0-alpha.0.8.9",
3
+ "version": "1.0.0-alpha.0.9.0",
4
4
  "description": "Build and deploy your Scout9 app for SMS auto replies",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -104,6 +104,40 @@
104
104
  */
105
105
 
106
106
 
107
+ class SpiritError extends Error {
108
+ /**
109
+ * @param {string} message - Description of the error.
110
+ * @param {string} step - The step or phase in which the error occurred.
111
+ */
112
+ constructor(message, step) {
113
+ super(message);
114
+ this.name = this.constructor.name;
115
+ this.step = step;
116
+
117
+ // Ensures the stack trace starts from where this error was created
118
+ if (Error.captureStackTrace) {
119
+ Error.captureStackTrace(this, this.constructor);
120
+ }
121
+ }
122
+
123
+ /**
124
+ *
125
+ * @param {unknown} err
126
+ * @param {string} step
127
+ * @returns {SpiritError}
128
+ */
129
+ static fromError(err, step) {
130
+ if (err instanceof SpiritError) return err;
131
+ if (err instanceof Error) {
132
+ const wrapped = new SpiritError(err.message, step, { cause: err });
133
+ wrapped.stack = err.stack;
134
+ return wrapped;
135
+ }
136
+ // fallback for non-Error values
137
+ return new SpiritError(String(err), step);
138
+ }
139
+ }
140
+
107
141
  /**
108
142
  * @typedef {Object} ConversationEvent
109
143
  * @property {(Change<import('@scout9/app').Conversation> & {
@@ -258,6 +292,25 @@ export const Spirits = {
258
292
  }
259
293
  };
260
294
 
295
+ const onStatus = (statusType, completeOrError = true) => {
296
+ progress(`${statusType}: ${completeOrError}`, 'info', 'STATUS', {[statusType]: completeOrError});
297
+ }
298
+
299
+ /**
300
+ * @param {Promise<any>} prom
301
+ * @param {string} step
302
+ */
303
+ const wrapStep = async (prom, step) => {
304
+ try {
305
+ const result = await prom;
306
+ onStatus(step);
307
+ return result;
308
+ } catch (error) {
309
+ onStatus(step, error.message || 'UNHANDLED ERROR');
310
+ throw SpiritError.fromError(error, step);
311
+ }
312
+ }
313
+
261
314
  // 1. Check inputs
262
315
  if (!conversation.$agent) {
263
316
  throw new Error(`SpiritsError: No agent found in conversation, must define ".$agent" in the conversation`);
@@ -284,7 +337,8 @@ export const Spirits = {
284
337
 
285
338
  // 2. Parse the message
286
339
  progress('Parsing message', 'info', 'SET_PROCESSING', 'user');
287
- const parsePayload = await parser(message.content, 'en');
340
+ const parsePayload = await wrapStep(parser(message.content, 'en'), 'parse');
341
+
288
342
  if (parsePayload.intent) {
289
343
  message.intent = parsePayload.intent;
290
344
  }
@@ -370,7 +424,7 @@ export const Spirits = {
370
424
 
371
425
  // 3. Run the contextualizer
372
426
  progress('Running contextualizer', 'info', 'SET_PROCESSING', 'system');
373
- const newContextMessages = await contextualizer({ conversation, messages });
427
+ const newContextMessages = await wrapStep(contextualizer({ conversation, messages }), 'contextualize');
374
428
  for (const contextMessage of newContextMessages) {
375
429
  if (!messages.find(mes => mes.content === contextMessage.content)) {
376
430
  messages.push(contextMessage);
@@ -383,7 +437,7 @@ export const Spirits = {
383
437
  // 4. Run the workflow
384
438
  progress('Running workflow', 'info', 'SET_PROCESSING', 'system');
385
439
 
386
- const slots = await workflow({
440
+ const slots = await wrapStep(workflow({
387
441
  messages,
388
442
  conversation,
389
443
  context,
@@ -396,7 +450,7 @@ export const Spirits = {
396
450
  initial: conversation.intent || null
397
451
  },
398
452
  stagnationCount: conversation.lockAttempts || 0
399
- })
453
+ }), 'workflow')
400
454
  .then((res) => Array.isArray(res) ? res : [res])
401
455
  .then((slots) => slots.reduce((accumulator, slot) => {
402
456
  if ('toJSON' in slot) {
@@ -646,7 +700,7 @@ export const Spirits = {
646
700
  if (!!_tasks && Array.isArray(_tasks) && !!_tasks.length) {
647
701
  generatorInput.tasks = _tasks;
648
702
  }
649
- const generatorPayload = await generator(generatorInput);
703
+ const generatorPayload = await wrapStep(generator(generatorInput), 'generate');
650
704
  if (!generatorPayload.send) {
651
705
  progress(
652
706
  'Generated response',
@@ -749,18 +803,20 @@ export const Spirits = {
749
803
  console.error(`Spirits: Locking conversation, error generating response: ${e.message}`);
750
804
  conversation = lockConversation(conversation, 'API: ' + e.message);
751
805
  }
806
+ } else {
807
+ onStatus('generate', 'ignored');
752
808
  }
753
809
 
754
810
  if (messagesToTransform.length && transformer) {
755
811
  try {
756
812
  for (const messageToTransform of messagesToTransform) {
757
- const transformResponse = await transformer({
813
+ const transformResponse = await wrapStep(transformer({
758
814
  message: messagesToTransform,
759
815
  persona,
760
816
  customer: customer.id,
761
817
  messages,
762
818
  context: context
763
- });
819
+ }), 'transform');
764
820
 
765
821
  progress('Generated response', 'success', undefined, undefined);
766
822
  // Check if already had message
@@ -787,7 +843,11 @@ export const Spirits = {
787
843
  }
788
844
  } else if (messagesToTransform.length) {
789
845
  console.warn(`No transformer provided`);
846
+ onStatus('transform', 'ignored');
790
847
  }
848
+ } else {
849
+ onStatus('generate', 'ignored');
850
+ onStatus('transform', 'ignored');
791
851
  }
792
852
 
793
853
  progress('Parsing message', 'info', 'SET_PROCESSING', null);