@scout9/app 1.0.0-alpha.1.0.6 → 1.0.0-alpha.1.0.7

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,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
4
- var spirits = require("./spirits-b3f15e97.cjs");
4
+ var spirits = require("./spirits-c773fc77.cjs");
5
5
  var require$$0$4 = require('util');
6
6
  var require$$0$3 = require('stream');
7
7
  var require$$1 = require('path');
@@ -35307,7 +35307,7 @@ function _loadUserPackageJson() {
35307
35307
  targetPkgUrl = isTest ? packageTestJsonUrl : packageJsonUrl;
35308
35308
  _context2.t0 = JSON;
35309
35309
  _context2.next = 10;
35310
- 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-767919ab.js', document.baseURI).href))), 'utf-8');
35310
+ 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-74c0ba94.js', document.baseURI).href))), 'utf-8');
35311
35311
  case 10:
35312
35312
  _context2.t1 = _context2.sent;
35313
35313
  pkg = _context2.t0.parse.call(_context2.t0, _context2.t1);
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  var _rollupPluginBabelHelpers = require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
4
- var dev = require("./dev-767919ab.cjs");
4
+ var dev = require("./dev-74c0ba94.cjs");
5
5
  var macros = require("./macros-c0acaa02.cjs");
6
- var spirits = require("./spirits-b3f15e97.cjs");
6
+ var spirits = require("./spirits-c773fc77.cjs");
7
7
  var require$$0 = require('fs');
8
8
  var require$$2$1 = require('events');
9
9
  var require$$1 = require('path');
@@ -29484,7 +29484,7 @@ class Body {
29484
29484
  }
29485
29485
  const {
29486
29486
  toFormData
29487
- } = await Promise.resolve().then(function () { return require("./multipart-parser-3054b741.cjs"); });
29487
+ } = await Promise.resolve().then(function () { return require("./multipart-parser-531d0e35.cjs"); });
29488
29488
  return toFormData(this.body, ct);
29489
29489
  }
29490
29490
 
@@ -41885,7 +41885,7 @@ function _loadUserPackageJson() {
41885
41885
  targetPkgUrl = isTest ? packageTestJsonUrl : packageJsonUrl;
41886
41886
  _context.t0 = JSON;
41887
41887
  _context.next = 10;
41888
- 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-81dce472.js', document.baseURI).href))), 'utf-8');
41888
+ 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-37eb47ef.js', document.baseURI).href))), 'utf-8');
41889
41889
  case 10:
41890
41890
  _context.t1 = _context.sent;
41891
41891
  pkg = _context.t0.parse.call(_context.t0, _context.t1);
@@ -43177,7 +43177,7 @@ var ProjectFiles = /*#__PURE__*/function () {
43177
43177
  return ProjectFiles;
43178
43178
  }();
43179
43179
 
43180
- 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-81dce472.js', document.baseURI).href)));
43180
+ 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-37eb47ef.js', document.baseURI).href)));
43181
43181
  var __dirname$1 = path__default["default"].dirname(__filename$1);
43182
43182
  function zipDirectory(source, out) {
43183
43183
  var archive = archiver$1('tar', {
@@ -43392,7 +43392,7 @@ function _buildApp() {
43392
43392
  case 11:
43393
43393
  _context4.t0 = JSON;
43394
43394
  _context4.next = 14;
43395
- 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-81dce472.js', document.baseURI).href))), 'utf-8');
43395
+ 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-37eb47ef.js', document.baseURI).href))), 'utf-8');
43396
43396
  case 14:
43397
43397
  _context4.t1 = _context4.sent;
43398
43398
  packageTemplate = _context4.t0.parse.call(_context4.t0, _context4.t1);
package/dist/index.cjs CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require("./index-81dce472.cjs");
6
- var dev = require("./dev-767919ab.cjs");
5
+ var index = require("./index-37eb47ef.cjs");
6
+ var dev = require("./dev-74c0ba94.cjs");
7
7
  require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
8
8
  require("./macros-c0acaa02.cjs");
9
- require("./spirits-b3f15e97.cjs");
9
+ require("./spirits-c773fc77.cjs");
10
10
  require('fs');
11
11
  require('events');
12
12
  require('path');
@@ -2,10 +2,10 @@
2
2
 
3
3
  require('node:fs');
4
4
  require('node:path');
5
- var index = require("./index-81dce472.cjs");
5
+ var index = require("./index-37eb47ef.cjs");
6
6
  require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
7
- require("./dev-767919ab.cjs");
8
- require("./spirits-b3f15e97.cjs");
7
+ require("./dev-74c0ba94.cjs");
8
+ require("./spirits-c773fc77.cjs");
9
9
  require('util');
10
10
  require('stream');
11
11
  require('path');
@@ -4692,7 +4692,8 @@ function enforceMonotonicInPlace(arr) {
4692
4692
  }
4693
4693
  }
4694
4694
 
4695
- var _excluded = ["keywords"];
4695
+ var _excluded = ["id"],
4696
+ _excluded2 = ["keywords"];
4696
4697
 
4697
4698
  /**
4698
4699
  * @typedef {Object} Document
@@ -4957,6 +4958,7 @@ var Spirits = {
4957
4958
  lockConversation,
4958
4959
  incrementLockAttempt,
4959
4960
  _addInstruction,
4961
+ instructionsAdded,
4960
4962
  addInstruction,
4961
4963
  onStatus,
4962
4964
  wrapStep,
@@ -4965,6 +4967,9 @@ var Spirits = {
4965
4967
  parsePayload,
4966
4968
  index,
4967
4969
  _message,
4970
+ patch,
4971
+ id,
4972
+ updatedFields,
4968
4973
  previousUserMessages,
4969
4974
  oldKeyCount,
4970
4975
  newKeyCount,
@@ -5029,6 +5034,7 @@ var Spirits = {
5029
5034
  _transformResponse$me,
5030
5035
  transformResponse,
5031
5036
  transformedMessages,
5037
+ appendedList,
5032
5038
  _iterator9,
5033
5039
  _step9,
5034
5040
  _loop,
@@ -5179,13 +5185,15 @@ var Spirits = {
5179
5185
  isEmptySystemMessage = function isEmptySystemMessage(m) {
5180
5186
  return (m === null || m === void 0 ? void 0 : m.role) === 'system' && (typeof m.content !== 'string' || m.content.trim() === '');
5181
5187
  };
5182
- updateConversation = function updateConversation(previousConversation, conversationUpdates) {
5183
- progress('Update conversation', 'info', 'UPDATE_CONVERSATION', conversationUpdates);
5188
+ updateConversation = function updateConversation(previousConversation, conversationUpdates, note) {
5189
+ var _note = ["Updated conversation fields", note].filter(Boolean).join(', ');
5190
+ progress("".concat(_note, ": ").concat(JSON.stringify(Object.keys(conversationUpdates))), 'info', 'UPDATE_CONVERSATION', conversationUpdates);
5184
5191
  emitUpdateConversation(conversationUpdates);
5185
5192
  return _rollupPluginBabelHelpers._objectSpread2(_rollupPluginBabelHelpers._objectSpread2({}, previousConversation), conversationUpdates);
5186
5193
  };
5187
- updateContext = function updateContext(previousContext, newContext) {
5188
- progress('Update context', 'info', 'UPDATE_CONTEXT', newContext);
5194
+ updateContext = function updateContext(previousContext, newContext, note) {
5195
+ var _note = ["Updated context fields", note].filter(Boolean).join(', ');
5196
+ progress("".concat(_note, ": ").concat(JSON.stringify(Object.keys(newContext))), 'info', 'UPDATE_CONTEXT', newContext);
5189
5197
  emitUpdateContext(newContext);
5190
5198
  return _rollupPluginBabelHelpers._objectSpread2(_rollupPluginBabelHelpers._objectSpread2({}, previousContext), newContext);
5191
5199
  };
@@ -5202,7 +5210,7 @@ var Spirits = {
5202
5210
  return updateConversation(_conversation, {
5203
5211
  locked: true,
5204
5212
  lockedReason: _conversation.lockedReason || reason || 'Unknown'
5205
- });
5213
+ }, 'lock conversation');
5206
5214
  };
5207
5215
  incrementLockAttempt = function incrementLockAttempt(_conversation, _config) {
5208
5216
  var max = (_config === null || _config === void 0 ? void 0 : _config.maxLockAttempts) || 3;
@@ -5217,8 +5225,11 @@ var Spirits = {
5217
5225
  patch.locked = true;
5218
5226
  patch.lockedReason = "Max lock attempts exceeded (".concat(next, " > ").concat(max, ")");
5219
5227
  }
5220
- var updated = updateConversation(_conversation, patch);
5221
- return updated;
5228
+ var updated = updateConversation(_conversation, patch, 'increment lock');
5229
+ return {
5230
+ updated: updated,
5231
+ patch: patch
5232
+ };
5222
5233
  };
5223
5234
  _addInstruction = function _addInstruction(instruction, _messages, _conversation, _config, previousLockAttempt, id) {
5224
5235
  var systemMessages = _messages.filter(function (m) {
@@ -5226,7 +5237,7 @@ var Spirits = {
5226
5237
  });
5227
5238
  var lastSystemMessage = systemMessages[systemMessages.length - 1];
5228
5239
  var addedMessage = false;
5229
- var changedConversation = false;
5240
+ var changedConversation = [];
5230
5241
 
5231
5242
  // If instruction does not equal previous system message, add it, otherwise lock attempt
5232
5243
  if (!lastSystemMessage || instruction !== lastSystemMessage.content) {
@@ -5249,8 +5260,11 @@ var Spirits = {
5249
5260
  // Handle repeated instruction
5250
5261
  // Increment lock attempt if instructions are repeated and we haven't already incremented lock attempt (for example if a forward is provided)
5251
5262
  if (previousLockAttempt === (_conversation.lockAttempts || 0)) {
5252
- _conversation = incrementLockAttempt(_conversation, _config);
5253
- changedConversation = true;
5263
+ var _incrementLockAttempt = incrementLockAttempt(_conversation, _config),
5264
+ updated = _incrementLockAttempt.updated,
5265
+ patch = _incrementLockAttempt.patch;
5266
+ _conversation = updated;
5267
+ changedConversation = Object.keys(patch);
5254
5268
  }
5255
5269
  }
5256
5270
  return {
@@ -5260,6 +5274,7 @@ var Spirits = {
5260
5274
  changedConversation: changedConversation
5261
5275
  };
5262
5276
  };
5277
+ instructionsAdded = 0;
5263
5278
  addInstruction = function addInstruction(instruction, previousLockAttempt) {
5264
5279
  var id = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : idGenerator('sys');
5265
5280
  var _addInstruction2 = _addInstruction(instruction, messages, conversation, config, previousLockAttempt, id),
@@ -5270,10 +5285,11 @@ var Spirits = {
5270
5285
  conversation = newConversation;
5271
5286
  messages = newMessages;
5272
5287
  if (addedMessage) {
5273
- progress('Added instruction', 'info', 'ADD_MESSAGE', newMessages[newMessages.length - 1]);
5288
+ instructionsAdded++;
5289
+ progress("Added ".concat(instructionsAdded, " instruction").concat(instructionsAdded === 1 ? '' : 's'), 'info', 'ADD_MESSAGE', newMessages[newMessages.length - 1]);
5274
5290
  }
5275
- if (changedConversation) {
5276
- progress('Updated conversation', 'info', 'UPDATE_CONVERSATION', newConversation);
5291
+ if (changedConversation.length) {
5292
+ progress("Updated conversation fields: ".concat(JSON.stringify(changedConversation)), 'info', 'UPDATE_CONVERSATION', newConversation);
5277
5293
  }
5278
5294
  };
5279
5295
  onStatus = function onStatus(statusType) {
@@ -5326,40 +5342,40 @@ var Spirits = {
5326
5342
  };
5327
5343
  }(); // 1. Check inputs
5328
5344
  if (conversation.$agent) {
5329
- _context5.next = 33;
5345
+ _context5.next = 34;
5330
5346
  break;
5331
5347
  }
5332
5348
  throw new Error("SpiritsError: No agent found in conversation, must define \".$agent\" in the conversation");
5333
- case 33:
5349
+ case 34:
5334
5350
  persona = (config.persona || config.personas || config.agents).find(function (p) {
5335
5351
  return p.id === conversation.$agent;
5336
5352
  });
5337
5353
  if (persona) {
5338
- _context5.next = 38;
5354
+ _context5.next = 39;
5339
5355
  break;
5340
5356
  }
5341
5357
  if (!(config.persona || config.personas || config.agents).some(function (a) {
5342
5358
  return !a.id;
5343
5359
  })) {
5344
- _context5.next = 37;
5360
+ _context5.next = 38;
5345
5361
  break;
5346
5362
  }
5347
5363
  throw new Error("SpiritsError: No persona found (\"".concat(conversation.$agent, "\") in provided config, some persona's did not contain an \"id\" (Internal Mapping Error)"));
5348
- case 37:
5349
- throw new Error("SpiritsError: No persona found (\"".concat(conversation.$agent, "\") in provided config"));
5350
5364
  case 38:
5365
+ throw new Error("SpiritsError: No persona found (\"".concat(conversation.$agent, "\") in provided config"));
5366
+ case 39:
5351
5367
  if (messages.every(function (m) {
5352
5368
  return !!m.id;
5353
5369
  })) {
5354
- _context5.next = 40;
5370
+ _context5.next = 41;
5355
5371
  break;
5356
5372
  }
5357
5373
  throw new Error("SpiritsError: Every message must have an \".id\", ensure all messages have an id assigned before running");
5358
- case 40:
5374
+ case 41:
5359
5375
  if (messages.every(function (m) {
5360
5376
  return m.role === 'customer' || m.role === 'agent' || m.role === 'system' || m.role === 'tool';
5361
5377
  })) {
5362
- _context5.next = 43;
5378
+ _context5.next = 44;
5363
5379
  break;
5364
5380
  }
5365
5381
  invalidRoles = messages.filter(function (m) {
@@ -5368,7 +5384,7 @@ var Spirits = {
5368
5384
  throw new Error("SpiritsError: Every message must have a role of \"customer\", \"agent\", or \"system\". Got invalid roles: ".concat(invalidRoles.map(function (m) {
5369
5385
  return m.role;
5370
5386
  }).join(', ')));
5371
- case 43:
5387
+ case 44:
5372
5388
  // Normalize existing message times ONCE at the start
5373
5389
  enforceMonotonicWithEmits(messages, 1);
5374
5390
 
@@ -5384,9 +5400,9 @@ var Spirits = {
5384
5400
  }
5385
5401
 
5386
5402
  // 2. Parse the message
5387
- _context5.next = 49;
5403
+ _context5.next = 50;
5388
5404
  return wrapStep(parser(message.content, 'en'), 'parse', 'parsing message');
5389
- case 49:
5405
+ case 50:
5390
5406
  parsePayload = _context5.sent;
5391
5407
  if (parsePayload.intent) {
5392
5408
  message.intent = parsePayload.intent;
@@ -5418,7 +5434,7 @@ var Spirits = {
5418
5434
  message = _message;
5419
5435
  message = pushMessage(messages, _message);
5420
5436
  emitAddMessage(message);
5421
- progress('Added message', 'info', 'ADD_MESSAGE', _message);
5437
+ progress("Added \"".concat(_message.role, "\" message"), 'info', 'ADD_MESSAGE', _message);
5422
5438
  } else {
5423
5439
  messages[index].context = parsePayload.context;
5424
5440
  messages[index].entities = parsePayload.entities;
@@ -5429,7 +5445,7 @@ var Spirits = {
5429
5445
  messages[index].intentScore = parsePayload.intentScore;
5430
5446
  }
5431
5447
  message = messages[index];
5432
- emitUpdateMessage(_rollupPluginBabelHelpers._objectSpread2(_rollupPluginBabelHelpers._objectSpread2({
5448
+ patch = _rollupPluginBabelHelpers._objectSpread2(_rollupPluginBabelHelpers._objectSpread2({
5433
5449
  id: messages[index].id,
5434
5450
  context: parsePayload.context,
5435
5451
  entities: parsePayload.entities
@@ -5437,8 +5453,10 @@ var Spirits = {
5437
5453
  intent: parsePayload.intent
5438
5454
  } : {}), typeof parsePayload.intentScore === 'number' ? {
5439
5455
  intentScore: parsePayload.intentScore
5440
- } : {}));
5441
- progress('Parsed message', 'success', 'UPDATE_MESSAGE', message);
5456
+ } : {});
5457
+ emitUpdateMessage(patch);
5458
+ id = patch.id, updatedFields = _rollupPluginBabelHelpers._objectWithoutProperties(patch, _excluded);
5459
+ progress("updated ".concat(id, " message fields ").concat(JSON.stringify(Object.keys(updatedFields))), 'info', 'UPDATE_MESSAGE', message);
5442
5460
  }
5443
5461
  // If this is the first user message, then update conversations intent
5444
5462
  previousUserMessages = messages.filter(function (m) {
@@ -5448,11 +5466,7 @@ var Spirits = {
5448
5466
  conversation = updateConversation(conversation, {
5449
5467
  intent: parsePayload.intent,
5450
5468
  intentScore: (parsePayload === null || parsePayload === void 0 ? void 0 : parsePayload.intentScore) || 0
5451
- });
5452
- progress('Updated conversation intent', 'info', 'UPDATE_CONVERSATION', {
5453
- intent: parsePayload.intent,
5454
- intentScore: (parsePayload === null || parsePayload === void 0 ? void 0 : parsePayload.intentScore) || 0
5455
- });
5469
+ }, 'intent mapping');
5456
5470
  }
5457
5471
  oldKeyCount = Object.keys(context).length;
5458
5472
  context = updateContext(context, parsePayload.context);
@@ -5463,12 +5477,7 @@ var Spirits = {
5463
5477
  locked: false,
5464
5478
  lockAttempts: 0,
5465
5479
  lockedReason: ''
5466
- });
5467
- progress('Reset lock', 'info', 'UPDATE_CONVERSATION', {
5468
- locked: false,
5469
- lockAttempts: 0,
5470
- lockedReason: ''
5471
- });
5480
+ }, 'reset lock attempts');
5472
5481
  }
5473
5482
  noNewContext = Object.keys(parsePayload.context).length === 0; // upsert parse system messages
5474
5483
  if (parsePayload.contextMessages.length) {
@@ -5509,15 +5518,15 @@ var Spirits = {
5509
5518
 
5510
5519
  // 3. Run the contextualizer
5511
5520
  // progress('Running contextualizer', 'info', 'SET_PROCESSING', 'system');
5512
- _context5.next = 66;
5521
+ _context5.next = 67;
5513
5522
  return wrapStep(contextualizer({
5514
5523
  conversation: conversation,
5515
5524
  messages: messages
5516
5525
  }), 'contextualize', 'contextualizing');
5517
- case 66:
5526
+ case 67:
5518
5527
  newContextMessages = _context5.sent;
5519
5528
  _iterator6 = _rollupPluginBabelHelpers._createForOfIteratorHelper(newContextMessages);
5520
- _context5.prev = 68;
5529
+ _context5.prev = 69;
5521
5530
  _loop2 = /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _loop2() {
5522
5531
  var contextMessage;
5523
5532
  return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _loop2$(_context3) {
@@ -5533,7 +5542,7 @@ var Spirits = {
5533
5542
  return messageKey(mes) === messageKey(contextMessage);
5534
5543
  })) {
5535
5544
  emitAddMessage(pushMessage(messages, contextMessage));
5536
- progress("Added context", 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
5545
+ progress("Added context ".concat(message.role, " message"), 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
5537
5546
  } else {
5538
5547
  progress("Already have system context, skipping", 'info');
5539
5548
  }
@@ -5544,28 +5553,28 @@ var Spirits = {
5544
5553
  }, _loop2);
5545
5554
  });
5546
5555
  _iterator6.s();
5547
- case 71:
5556
+ case 72:
5548
5557
  if ((_step6 = _iterator6.n()).done) {
5549
- _context5.next = 75;
5558
+ _context5.next = 76;
5550
5559
  break;
5551
5560
  }
5552
- return _context5.delegateYield(_loop2(), "t0", 73);
5553
- case 73:
5554
- _context5.next = 71;
5561
+ return _context5.delegateYield(_loop2(), "t0", 74);
5562
+ case 74:
5563
+ _context5.next = 72;
5555
5564
  break;
5556
- case 75:
5557
- _context5.next = 80;
5565
+ case 76:
5566
+ _context5.next = 81;
5558
5567
  break;
5559
- case 77:
5560
- _context5.prev = 77;
5561
- _context5.t1 = _context5["catch"](68);
5568
+ case 78:
5569
+ _context5.prev = 78;
5570
+ _context5.t1 = _context5["catch"](69);
5562
5571
  _iterator6.e(_context5.t1);
5563
- case 80:
5564
- _context5.prev = 80;
5572
+ case 81:
5573
+ _context5.prev = 81;
5565
5574
  _iterator6.f();
5566
- return _context5.finish(80);
5567
- case 83:
5568
- _context5.next = 85;
5575
+ return _context5.finish(81);
5576
+ case 84:
5577
+ _context5.next = 86;
5569
5578
  return wrapStep(workflow({
5570
5579
  messages: messages,
5571
5580
  conversation: conversation,
@@ -5594,7 +5603,7 @@ var Spirits = {
5594
5603
  return accumulator;
5595
5604
  }, []);
5596
5605
  });
5597
- case 85:
5606
+ case 86:
5598
5607
  slots = _context5.sent;
5599
5608
  hasNoInstructions = slots.every(function (s) {
5600
5609
  return !s.instructions || Array.isArray(s.instructions) && s.instructions.length === 0;
@@ -5654,43 +5663,38 @@ var Spirits = {
5654
5663
  enforceMonotonicWithEmits(messagesToTransform, 1);
5655
5664
  previousLockAttempt = conversation.lockAttempts || 0; // Used to track
5656
5665
  if (hasNoInstructions && noNewContext) {
5657
- conversation = incrementLockAttempt(conversation, config);
5666
+ conversation = incrementLockAttempt(conversation, config).updated;
5658
5667
  } else {
5659
5668
  conversation = updateConversation(conversation, {
5660
5669
  lockAttempts: 0,
5661
5670
  locked: false,
5662
5671
  lockedReason: ''
5663
- });
5664
- progress('Reset lock', 'info', 'UPDATE_CONVERSATION', {
5665
- lockAttempts: 0,
5666
- locked: false,
5667
- lockedReason: ''
5668
- });
5672
+ }, 'reset lock');
5669
5673
  }
5670
5674
  resettedIntent = false;
5671
5675
  /** @type {Array<string> | undefined} */
5672
5676
  _iterator7 = _rollupPluginBabelHelpers._createForOfIteratorHelper(slots);
5673
- _context5.prev = 94;
5677
+ _context5.prev = 95;
5674
5678
  _iterator7.s();
5675
- case 96:
5679
+ case 97:
5676
5680
  if ((_step7 = _iterator7.n()).done) {
5677
- _context5.next = 151;
5681
+ _context5.next = 152;
5678
5682
  break;
5679
5683
  }
5680
5684
  _step7$value = _step7.value, forward = _step7$value.forward, forwardNote = _step7$value.forwardNote, instructions = _step7$value.instructions, removeInstructions = _step7$value.removeInstructions, _step7$value.message, _step7$value.scheduled, resetIntent = _step7$value.resetIntent, _step7$value.secondsDelay, contextUpsert = _step7$value.contextUpsert, anticipate = _step7$value.anticipate, slotFollowup = _step7$value.followup, slotEntityContextUpsert = _step7$value.entityContextUpsert, tasks = _step7$value.tasks;
5681
5685
  if (!anticipate) {
5682
- _context5.next = 111;
5686
+ _context5.next = 112;
5683
5687
  break;
5684
5688
  }
5685
5689
  if (!Array.isArray(anticipate)) {
5686
- _context5.next = 106;
5690
+ _context5.next = 107;
5687
5691
  break;
5688
5692
  }
5689
5693
  // 'literal' anticipation
5690
5694
  _slots = {};
5691
5695
  map = [];
5692
5696
  for (i = 0; i < anticipate.length; i++) {
5693
- _anticipate$i = anticipate[i], keywords = _anticipate$i.keywords, _slot = _rollupPluginBabelHelpers._objectWithoutProperties(_anticipate$i, _excluded);
5697
+ _anticipate$i = anticipate[i], keywords = _anticipate$i.keywords, _slot = _rollupPluginBabelHelpers._objectWithoutProperties(_anticipate$i, _excluded2);
5694
5698
  slotId = "".concat(i);
5695
5699
  _slots[slotId] = _slot;
5696
5700
  map.push({
@@ -5702,12 +5706,12 @@ var Spirits = {
5702
5706
  type: 'literal',
5703
5707
  slots: _slots,
5704
5708
  map: map
5705
- });
5706
- _context5.next = 111;
5709
+ }, 'anticipation slots');
5710
+ _context5.next = 112;
5707
5711
  break;
5708
- case 106:
5712
+ case 107:
5709
5713
  if (!('yes' in anticipate && 'no' in anticipate && 'did' in anticipate)) {
5710
- _context5.next = 110;
5714
+ _context5.next = 111;
5711
5715
  break;
5712
5716
  }
5713
5717
  // "did" anticipation
@@ -5718,12 +5722,12 @@ var Spirits = {
5718
5722
  no: anticipate.no
5719
5723
  },
5720
5724
  did: anticipate.did
5721
- });
5722
- _context5.next = 111;
5725
+ }, 'anticipation slots');
5726
+ _context5.next = 112;
5723
5727
  break;
5724
- case 110:
5725
- throw new Error("Invalid anticipate payload \"".concat(JSON.stringify(anticipate), "\""));
5726
5728
  case 111:
5729
+ throw new Error("Invalid anticipate payload \"".concat(JSON.stringify(anticipate), "\""));
5730
+ case 112:
5727
5731
  // tasks from auto/manual ingress to execute
5728
5732
  if (!!tasks && Array.isArray(tasks) && !!tasks.length) {
5729
5733
  if (!_tasks) _tasks = [];
@@ -5744,7 +5748,7 @@ var Spirits = {
5744
5748
  if (typeof forward === 'string') {
5745
5749
  conversation = updateConversation(conversation, {
5746
5750
  forwarded: forward
5747
- });
5751
+ }, 'forward');
5748
5752
  emitAddMessage(pushMessage(messages, {
5749
5753
  id: idGenerator("sys"),
5750
5754
  role: "system",
@@ -5755,7 +5759,7 @@ var Spirits = {
5755
5759
  } else if (typeof forward === 'boolean') {
5756
5760
  conversation = updateConversation(conversation, {
5757
5761
  forwarded: conversation.$agent
5758
- });
5762
+ }, 'forward');
5759
5763
  emitAddMessage(pushMessage(messages, {
5760
5764
  id: idGenerator("sys"),
5761
5765
  role: "system",
@@ -5766,7 +5770,7 @@ var Spirits = {
5766
5770
  } else {
5767
5771
  conversation = updateConversation(conversation, {
5768
5772
  forwarded: forward.to
5769
- });
5773
+ }, 'forward');
5770
5774
  emitAddMessage(pushMessage(messages, {
5771
5775
  id: idGenerator("sys"),
5772
5776
  role: "system",
@@ -5779,19 +5783,19 @@ var Spirits = {
5779
5783
 
5780
5784
  // Insert instructions context
5781
5785
  if (!instructions) {
5782
- _context5.next = 130;
5786
+ _context5.next = 131;
5783
5787
  break;
5784
5788
  }
5785
5789
  if (!(typeof instructions === 'string')) {
5786
- _context5.next = 120;
5790
+ _context5.next = 121;
5787
5791
  break;
5788
5792
  }
5789
5793
  addInstruction(instructions, previousLockAttempt);
5790
- _context5.next = 130;
5794
+ _context5.next = 131;
5791
5795
  break;
5792
- case 120:
5796
+ case 121:
5793
5797
  if (!Array.isArray(instructions)) {
5794
- _context5.next = 125;
5798
+ _context5.next = 126;
5795
5799
  break;
5796
5800
  }
5797
5801
  _iterator10 = _rollupPluginBabelHelpers._createForOfIteratorHelper(instructions);
@@ -5809,25 +5813,25 @@ var Spirits = {
5809
5813
  } finally {
5810
5814
  _iterator10.f();
5811
5815
  }
5812
- _context5.next = 130;
5816
+ _context5.next = 131;
5813
5817
  break;
5814
- case 125:
5818
+ case 126:
5815
5819
  if (!(_rollupPluginBabelHelpers._typeof(instructions) === 'object' && 'content' in instructions)) {
5816
- _context5.next = 129;
5820
+ _context5.next = 130;
5817
5821
  break;
5818
5822
  }
5819
5823
  addInstruction(instructions.content, previousLockAttempt, instructions.id);
5820
- _context5.next = 130;
5824
+ _context5.next = 131;
5821
5825
  break;
5822
- case 129:
5823
- throw new Error("SpiritsError: instructions must be a string or array or {content: \"<instruction>\"}, got: ".concat(JSON.stringify(instructions)));
5824
5826
  case 130:
5827
+ throw new Error("SpiritsError: instructions must be a string or array or {content: \"<instruction>\"}, got: ".concat(JSON.stringify(instructions)));
5828
+ case 131:
5825
5829
  if (!removeInstructions) {
5826
- _context5.next = 147;
5830
+ _context5.next = 148;
5827
5831
  break;
5828
5832
  }
5829
5833
  _iterator11 = _rollupPluginBabelHelpers._createForOfIteratorHelper(removeInstructions);
5830
- _context5.prev = 132;
5834
+ _context5.prev = 133;
5831
5835
  _loop3 = /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _loop3() {
5832
5836
  var instructionId, index, removed;
5833
5837
  return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _loop3$(_context4) {
@@ -5853,27 +5857,27 @@ var Spirits = {
5853
5857
  }, _loop3);
5854
5858
  });
5855
5859
  _iterator11.s();
5856
- case 135:
5860
+ case 136:
5857
5861
  if ((_step11 = _iterator11.n()).done) {
5858
- _context5.next = 139;
5862
+ _context5.next = 140;
5859
5863
  break;
5860
5864
  }
5861
- return _context5.delegateYield(_loop3(), "t2", 137);
5862
- case 137:
5863
- _context5.next = 135;
5865
+ return _context5.delegateYield(_loop3(), "t2", 138);
5866
+ case 138:
5867
+ _context5.next = 136;
5864
5868
  break;
5865
- case 139:
5866
- _context5.next = 144;
5869
+ case 140:
5870
+ _context5.next = 145;
5867
5871
  break;
5868
- case 141:
5869
- _context5.prev = 141;
5870
- _context5.t3 = _context5["catch"](132);
5872
+ case 142:
5873
+ _context5.prev = 142;
5874
+ _context5.t3 = _context5["catch"](133);
5871
5875
  _iterator11.e(_context5.t3);
5872
- case 144:
5873
- _context5.prev = 144;
5876
+ case 145:
5877
+ _context5.prev = 145;
5874
5878
  _iterator11.f();
5875
- return _context5.finish(144);
5876
- case 147:
5879
+ return _context5.finish(145);
5880
+ case 148:
5877
5881
  // @TODO this logic is now handled at messagesToTransform's initialization, maybe move back down here
5878
5882
  // if (manualMessage) {
5879
5883
 
@@ -5911,26 +5915,25 @@ var Spirits = {
5911
5915
 
5912
5916
  if (contextUpsert) {
5913
5917
  context = updateContext(context, contextUpsert);
5914
- progress('Upserted context', 'info', 'UPDATE_CONTEXT', contextUpsert);
5915
5918
  }
5916
5919
  if (resetIntent) {
5917
5920
  resettedIntent = true;
5918
5921
  }
5919
- case 149:
5920
- _context5.next = 96;
5922
+ case 150:
5923
+ _context5.next = 97;
5921
5924
  break;
5922
- case 151:
5923
- _context5.next = 156;
5925
+ case 152:
5926
+ _context5.next = 157;
5924
5927
  break;
5925
- case 153:
5926
- _context5.prev = 153;
5927
- _context5.t4 = _context5["catch"](94);
5928
+ case 154:
5929
+ _context5.prev = 154;
5930
+ _context5.t4 = _context5["catch"](95);
5928
5931
  _iterator7.e(_context5.t4);
5929
- case 156:
5930
- _context5.prev = 156;
5932
+ case 157:
5933
+ _context5.prev = 157;
5931
5934
  _iterator7.f();
5932
- return _context5.finish(156);
5933
- case 159:
5935
+ return _context5.finish(157);
5936
+ case 160:
5934
5937
  enforceMonotonicWithEmits(messages, 1);
5935
5938
  if (resettedIntent && !_forward) {
5936
5939
  conversation = updateConversation(conversation, {
@@ -5939,27 +5942,20 @@ var Spirits = {
5939
5942
  locked: false,
5940
5943
  lockedReason: '',
5941
5944
  lockAttempts: 0
5942
- });
5943
- progress('Reset conversation intent', 'info', 'UPDATE_CONVERSATION', {
5944
- intent: null,
5945
- intentScore: null,
5946
- locked: false,
5947
- lockAttempts: 0,
5948
- lockedReason: ''
5949
- });
5945
+ }, 'reset conversation intent');
5950
5946
  }
5951
5947
 
5952
5948
  // 5. Generate response
5953
5949
  // If conversation previously locked, don't generate
5954
5950
  if (input.conversation.locked) {
5955
- _context5.next = 214;
5951
+ _context5.next = 217;
5956
5952
  break;
5957
5953
  }
5958
5954
  if (!((!conversation.locked || !hasNoInstructions) && !!hasNoCustomMessage)) {
5959
- _context5.next = 179;
5955
+ _context5.next = 180;
5960
5956
  break;
5961
5957
  }
5962
- _context5.prev = 163;
5958
+ _context5.prev = 164;
5963
5959
  // progress('Generating message', 'info', 'SET_PROCESSING', 'system');
5964
5960
  /** @type {import('@scout9/admin').GenerateRequestOneOf1} */
5965
5961
  generatorInput = {
@@ -5972,9 +5968,9 @@ var Spirits = {
5972
5968
  if (!!_tasks && Array.isArray(_tasks) && !!_tasks.length) {
5973
5969
  generatorInput.tasks = _tasks;
5974
5970
  }
5975
- _context5.next = 168;
5971
+ _context5.next = 169;
5976
5972
  return wrapStep(generator(generatorInput), 'generate');
5977
- case 168:
5973
+ case 169:
5978
5974
  generatorPayload = _context5.sent;
5979
5975
  if (!generatorPayload.send) {
5980
5976
  progress('Generated response send rejected', 'error', undefined, {
@@ -6079,26 +6075,26 @@ var Spirits = {
6079
6075
  }
6080
6076
  }
6081
6077
  }
6082
- _context5.next = 177;
6078
+ _context5.next = 178;
6083
6079
  break;
6084
- case 172:
6085
- _context5.prev = 172;
6086
- _context5.t5 = _context5["catch"](163);
6080
+ case 173:
6081
+ _context5.prev = 173;
6082
+ _context5.t5 = _context5["catch"](164);
6087
6083
  onError(_context5.t5);
6088
6084
  console.error("Spirits: Locking conversation, error generating response: ".concat(_context5.t5.message));
6089
6085
  conversation = lockConversation(conversation, 'API: ' + _context5.t5.message);
6090
- case 177:
6091
- _context5.next = 180;
6086
+ case 178:
6087
+ _context5.next = 181;
6092
6088
  break;
6093
- case 179:
6094
- onStatus('generate', 'ignored');
6095
6089
  case 180:
6090
+ onStatus('generate', 'ignored');
6091
+ case 181:
6096
6092
  if (!(messagesToTransform.length && transformer)) {
6097
- _context5.next = 211;
6093
+ _context5.next = 214;
6098
6094
  break;
6099
6095
  }
6100
- _context5.prev = 181;
6101
- _context5.next = 184;
6096
+ _context5.prev = 182;
6097
+ _context5.next = 185;
6102
6098
  return wrapStep(transformer({
6103
6099
  // message: messagesToTransform,
6104
6100
  addedMessages: messagesToTransform,
@@ -6107,15 +6103,16 @@ var Spirits = {
6107
6103
  messages: messages,
6108
6104
  context: context
6109
6105
  }), 'transform');
6110
- case 184:
6106
+ case 185:
6111
6107
  transformResponse = _context5.sent;
6112
6108
  // @TODO check for duplicates, or have already sent the message
6113
6109
  transformedMessages = (_transformResponse$me = transformResponse.messages) !== null && _transformResponse$me !== void 0 && _transformResponse$me.length ? transformResponse.messages : [{
6114
6110
  role: 'agent',
6115
6111
  content: transformResponse.message
6116
6112
  }];
6113
+ appendedList = [];
6117
6114
  _iterator9 = _rollupPluginBabelHelpers._createForOfIteratorHelper(transformedMessages);
6118
- _context5.prev = 187;
6115
+ _context5.prev = 189;
6119
6116
  _loop = /*#__PURE__*/_rollupPluginBabelHelpers._regeneratorRuntime().mark(function _loop() {
6120
6117
  var message, adjusted, prior, _adjusted$contentGene2, _adjusted$contentTran, appended;
6121
6118
  return _rollupPluginBabelHelpers._regeneratorRuntime().wrap(function _loop$(_context2) {
@@ -6145,7 +6142,7 @@ var Spirits = {
6145
6142
  adjusted.contentTransformed = adjusted.content;
6146
6143
  }
6147
6144
  appended = pushMessage(messages, adjusted);
6148
- progress("Added agent message", "info", "ADD_MESSAGE", appended);
6145
+ appendedList.push(appended);
6149
6146
  emitAddMessage(appended);
6150
6147
  case 7:
6151
6148
  case "end":
@@ -6154,52 +6151,53 @@ var Spirits = {
6154
6151
  }, _loop);
6155
6152
  });
6156
6153
  _iterator9.s();
6157
- case 190:
6154
+ case 192:
6158
6155
  if ((_step9 = _iterator9.n()).done) {
6159
- _context5.next = 194;
6156
+ _context5.next = 196;
6160
6157
  break;
6161
6158
  }
6162
- return _context5.delegateYield(_loop(), "t6", 192);
6163
- case 192:
6164
- _context5.next = 190;
6165
- break;
6159
+ return _context5.delegateYield(_loop(), "t6", 194);
6166
6160
  case 194:
6167
- _context5.next = 199;
6161
+ _context5.next = 192;
6168
6162
  break;
6169
6163
  case 196:
6170
- _context5.prev = 196;
6171
- _context5.t7 = _context5["catch"](187);
6164
+ _context5.next = 201;
6165
+ break;
6166
+ case 198:
6167
+ _context5.prev = 198;
6168
+ _context5.t7 = _context5["catch"](189);
6172
6169
  _iterator9.e(_context5.t7);
6173
- case 199:
6174
- _context5.prev = 199;
6170
+ case 201:
6171
+ _context5.prev = 201;
6175
6172
  _iterator9.f();
6176
- return _context5.finish(199);
6177
- case 202:
6178
- _context5.next = 209;
6179
- break;
6173
+ return _context5.finish(201);
6180
6174
  case 204:
6181
- _context5.prev = 204;
6182
- _context5.t8 = _context5["catch"](181);
6175
+ progress("Added ".concat(appendedList.length, " persona message").concat(appendedList.length === 1 ? '' : 's'), "info", "ADD_MESSAGES", appendedList);
6176
+ _context5.next = 212;
6177
+ break;
6178
+ case 207:
6179
+ _context5.prev = 207;
6180
+ _context5.t8 = _context5["catch"](182);
6183
6181
  console.error("Spirits: Locking conversation, error transforming response: ".concat(_context5.t8.message));
6184
6182
  conversation = lockConversation(conversation, 'API: ' + _context5.t8.message);
6185
6183
  onError(_context5.t8);
6186
- case 209:
6187
- _context5.next = 212;
6184
+ case 212:
6185
+ _context5.next = 215;
6188
6186
  break;
6189
- case 211:
6187
+ case 214:
6190
6188
  if (messagesToTransform.length) {
6191
6189
  console.warn("Spirits: No transformer provided");
6192
6190
  onStatus('transform', 'ignored');
6193
6191
  } else {
6194
6192
  onStatus('transform', 'ignored');
6195
6193
  }
6196
- case 212:
6197
- _context5.next = 216;
6194
+ case 215:
6195
+ _context5.next = 219;
6198
6196
  break;
6199
- case 214:
6197
+ case 217:
6200
6198
  onStatus('generate', 'ignored');
6201
6199
  onStatus('transform', 'ignored');
6202
- case 216:
6200
+ case 219:
6203
6201
  emptySystemMessages = messages.filter(isEmptySystemMessage);
6204
6202
  if (emptySystemMessages.length) {
6205
6203
  progress('Empty system messages detected post-run', 'error', 'EMPTY_SYSTEM_MESSAGE_FINAL', {
@@ -6211,7 +6209,7 @@ var Spirits = {
6211
6209
  console.error('Spirits: Empty system messages detected', emptySystemMessages);
6212
6210
  }
6213
6211
  logToolPairingIssues(messages, 'final');
6214
- progress("Spirits.customer start ended in ".concat(elapsedSeconds(rootStart), "s"), 'success', undefined, undefined);
6212
+ progress("Spirits.customer executed in ".concat(elapsedSeconds(rootStart), "s total"), 'success', undefined, undefined);
6215
6213
  return _context5.abrupt("return", {
6216
6214
  conversation: {
6217
6215
  before: conversationBefore,
@@ -6234,11 +6232,11 @@ var Spirits = {
6234
6232
  followup: followup,
6235
6233
  entityContextUpsert: entityContextUpsert
6236
6234
  });
6237
- case 221:
6235
+ case 224:
6238
6236
  case "end":
6239
6237
  return _context5.stop();
6240
6238
  }
6241
- }, _callee2, null, [[68, 77, 80, 83], [94, 153, 156, 159], [132, 141, 144, 147], [163, 172], [181, 204], [187, 196, 199, 202]]);
6239
+ }, _callee2, null, [[69, 78, 81, 84], [95, 154, 157, 160], [133, 142, 145, 148], [164, 173], [182, 207], [189, 198, 201, 204]]);
6242
6240
  }));
6243
6241
  function customer(_x) {
6244
6242
  return _customer.apply(this, arguments);
package/dist/spirits.cjs CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
6
- var spirits = require("./spirits-b3f15e97.cjs");
6
+ var spirits = require("./spirits-c773fc77.cjs");
7
7
 
8
8
 
9
9
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var dev = require("./dev-767919ab.cjs");
5
+ var dev = require("./dev-74c0ba94.cjs");
6
6
  require("./_rollupPluginBabelHelpers-9c73c95c.cjs");
7
- require("./spirits-b3f15e97.cjs");
7
+ require("./spirits-c773fc77.cjs");
8
8
  require('util');
9
9
  require('stream');
10
10
  require('path');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scout9/app",
3
- "version": "1.0.0-alpha.1.0.6",
3
+ "version": "1.0.0-alpha.1.0.7",
4
4
  "description": "Build and deploy your Scout9 app for SMS auto replies",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -329,8 +329,9 @@ export const Spirits = {
329
329
  m?.role === 'system' &&
330
330
  (typeof m.content !== 'string' || m.content.trim() === '');
331
331
 
332
- const updateConversation = (previousConversation, conversationUpdates) => {
333
- progress('Update conversation', 'info', 'UPDATE_CONVERSATION', conversationUpdates);
332
+ const updateConversation = (previousConversation, conversationUpdates, note) => {
333
+ const _note = [`Updated conversation fields`, note].filter(Boolean).join(', ')
334
+ progress(`${_note}: ${JSON.stringify(Object.keys(conversationUpdates))}`, 'info', 'UPDATE_CONVERSATION', conversationUpdates);
334
335
  emitUpdateConversation(conversationUpdates);
335
336
  return {
336
337
  ...previousConversation,
@@ -338,8 +339,9 @@ export const Spirits = {
338
339
  };
339
340
  };
340
341
 
341
- const updateContext = (previousContext, newContext) => {
342
- progress('Update context', 'info', 'UPDATE_CONTEXT', newContext);
342
+ const updateContext = (previousContext, newContext, note) => {
343
+ const _note = [`Updated context fields`, note].filter(Boolean).join(', ')
344
+ progress(`${_note}: ${JSON.stringify(Object.keys(newContext))}`, 'info', 'UPDATE_CONTEXT', newContext);
343
345
  emitUpdateContext(newContext);
344
346
  return {
345
347
  ...previousContext,
@@ -359,7 +361,8 @@ export const Spirits = {
359
361
  const lockConversation = (_conversation, reason) => {
360
362
  return updateConversation(
361
363
  _conversation,
362
- { locked: true, lockedReason: _conversation.lockedReason || reason || 'Unknown' }
364
+ { locked: true, lockedReason: _conversation.lockedReason || reason || 'Unknown' },
365
+ 'lock conversation'
363
366
  );
364
367
  };
365
368
 
@@ -375,8 +378,8 @@ export const Spirits = {
375
378
  patch.locked = true;
376
379
  patch.lockedReason = `Max lock attempts exceeded (${next} > ${max})`;
377
380
  }
378
- const updated = updateConversation(_conversation, patch);
379
- return updated;
381
+ const updated = updateConversation(_conversation, patch, 'increment lock');
382
+ return {updated, patch}
380
383
  };
381
384
 
382
385
  const _addInstruction = (
@@ -390,7 +393,7 @@ export const Spirits = {
390
393
  const systemMessages = _messages.filter(m => m.role === 'system');
391
394
  const lastSystemMessage = systemMessages[systemMessages.length - 1];
392
395
  let addedMessage = false;
393
- let changedConversation = false;
396
+ let changedConversation = [];
394
397
 
395
398
  // If instruction does not equal previous system message, add it, otherwise lock attempt
396
399
  if (!lastSystemMessage || instruction !== lastSystemMessage.content) {
@@ -413,8 +416,9 @@ export const Spirits = {
413
416
  // Handle repeated instruction
414
417
  // Increment lock attempt if instructions are repeated and we haven't already incremented lock attempt (for example if a forward is provided)
415
418
  if (previousLockAttempt === (_conversation.lockAttempts || 0)) {
416
- _conversation = incrementLockAttempt(_conversation, _config);
417
- changedConversation = true;
419
+ const {updated, patch} = incrementLockAttempt(_conversation, _config);
420
+ _conversation = updated;
421
+ changedConversation = Object.keys(patch);
418
422
  }
419
423
  }
420
424
  return {
@@ -425,6 +429,7 @@ export const Spirits = {
425
429
  };
426
430
  };
427
431
 
432
+ let instructionsAdded = 0;
428
433
  const addInstruction = (instruction, previousLockAttempt, id = idGenerator('sys')) => {
429
434
  const {
430
435
  conversation: newConversation,
@@ -435,10 +440,11 @@ export const Spirits = {
435
440
  conversation = newConversation;
436
441
  messages = newMessages;
437
442
  if (addedMessage) {
438
- progress('Added instruction', 'info', 'ADD_MESSAGE', newMessages[newMessages.length - 1]);
443
+ instructionsAdded++;
444
+ progress(`Added ${instructionsAdded} instruction${instructionsAdded === 1 ? '' : 's'}`, 'info', 'ADD_MESSAGE', newMessages[newMessages.length - 1]);
439
445
  }
440
- if (changedConversation) {
441
- progress('Updated conversation', 'info', 'UPDATE_CONVERSATION', newConversation);
446
+ if (changedConversation.length) {
447
+ progress(`Updated conversation fields: ${JSON.stringify(changedConversation)}`, 'info', 'UPDATE_CONVERSATION', newConversation);
442
448
  }
443
449
  };
444
450
 
@@ -539,7 +545,7 @@ export const Spirits = {
539
545
  message = _message;
540
546
  message = pushMessage(messages, _message);
541
547
  emitAddMessage(message);
542
- progress('Added message', 'info', 'ADD_MESSAGE', _message);
548
+ progress(`Added "${_message.role}" message`, 'info', 'ADD_MESSAGE', _message);
543
549
  } else {
544
550
  messages[index].context = parsePayload.context;
545
551
  messages[index].entities = parsePayload.entities;
@@ -550,14 +556,16 @@ export const Spirits = {
550
556
  messages[index].intentScore = parsePayload.intentScore;
551
557
  }
552
558
  message = messages[index];
553
- emitUpdateMessage({
559
+ const patch = {
554
560
  id: messages[index].id,
555
561
  context: parsePayload.context,
556
562
  entities: parsePayload.entities,
557
563
  ...(parsePayload.intent ? { intent: parsePayload.intent } : {}),
558
564
  ...(typeof parsePayload.intentScore === 'number' ? { intentScore: parsePayload.intentScore } : {}),
559
- });
560
- progress('Parsed message', 'success', 'UPDATE_MESSAGE', message);
565
+ };
566
+ emitUpdateMessage(patch);
567
+ const {id, ...updatedFields} = patch;
568
+ progress(`updated ${id} message fields ${JSON.stringify(Object.keys(updatedFields))}`, 'info', 'UPDATE_MESSAGE', message);
561
569
  }
562
570
  // If this is the first user message, then update conversations intent
563
571
  const previousUserMessages = messages.filter(m => m.role === 'customer' && m.content !== message.content);
@@ -565,13 +573,7 @@ export const Spirits = {
565
573
  conversation = updateConversation(conversation, {
566
574
  intent: parsePayload.intent,
567
575
  intentScore: parsePayload?.intentScore || 0,
568
- });
569
- progress(
570
- 'Updated conversation intent',
571
- 'info',
572
- 'UPDATE_CONVERSATION',
573
- { intent: parsePayload.intent, intentScore: parsePayload?.intentScore || 0 }
574
- );
576
+ }, 'intent mapping');
575
577
  }
576
578
  const oldKeyCount = Object.keys(context).length;
577
579
  context = updateContext(context, parsePayload.context);
@@ -583,8 +585,7 @@ export const Spirits = {
583
585
  locked: false,
584
586
  lockAttempts: 0,
585
587
  lockedReason: '',
586
- });
587
- progress('Reset lock', 'info', 'UPDATE_CONVERSATION', { locked: false, lockAttempts: 0, lockedReason: '' });
588
+ }, 'reset lock attempts');
588
589
  }
589
590
 
590
591
  const noNewContext = Object.keys(parsePayload.context).length === 0;
@@ -626,7 +627,7 @@ export const Spirits = {
626
627
  });
627
628
  } else if (!messages.find(mes => messageKey(mes) === messageKey(contextMessage))) {
628
629
  emitAddMessage(pushMessage(messages, contextMessage));
629
- progress(`Added context`, 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
630
+ progress(`Added context ${message.role} message`, 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
630
631
  } else {
631
632
  progress(`Already have system context, skipping`, 'info');
632
633
  }
@@ -718,14 +719,13 @@ export const Spirits = {
718
719
  const previousLockAttempt = conversation.lockAttempts || 0; // Used to track
719
720
 
720
721
  if (hasNoInstructions && noNewContext) {
721
- conversation = incrementLockAttempt(conversation, config);
722
+ conversation = incrementLockAttempt(conversation, config).updated;
722
723
  } else {
723
724
  conversation = updateConversation(conversation, {
724
725
  lockAttempts: 0,
725
726
  locked: false,
726
727
  lockedReason: '',
727
- });
728
- progress('Reset lock', 'info', 'UPDATE_CONVERSATION', { lockAttempts: 0, locked: false, lockedReason: '' });
728
+ }, 'reset lock');
729
729
  }
730
730
 
731
731
  let resettedIntent = false;
@@ -770,7 +770,7 @@ export const Spirits = {
770
770
  type: 'literal',
771
771
  slots,
772
772
  map
773
- });
773
+ }, 'anticipation slots');
774
774
  } else if ('yes' in anticipate && 'no' in anticipate && 'did' in anticipate) {
775
775
  // "did" anticipation
776
776
  conversation = updateConversation(conversation, {
@@ -780,7 +780,7 @@ export const Spirits = {
780
780
  no: anticipate.no
781
781
  },
782
782
  did: anticipate.did
783
- });
783
+ }, 'anticipation slots');
784
784
  } else {
785
785
  throw new Error(`Invalid anticipate payload "${JSON.stringify(anticipate)}"`);
786
786
  }
@@ -806,7 +806,7 @@ export const Spirits = {
806
806
  _forward = forward;
807
807
  _forwardNote = forwardNote;
808
808
  if (typeof forward === 'string') {
809
- conversation = updateConversation(conversation, { forwarded: forward });
809
+ conversation = updateConversation(conversation, { forwarded: forward }, 'forward');
810
810
  emitAddMessage(pushMessage(messages, {
811
811
  id: idGenerator("sys"),
812
812
  role: "system",
@@ -815,7 +815,7 @@ export const Spirits = {
815
815
  }));
816
816
  progress(`Forwarded to "${forward}"`, 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
817
817
  } else if (typeof forward === 'boolean') {
818
- conversation = updateConversation(conversation, { forwarded: conversation.$agent });
818
+ conversation = updateConversation(conversation, { forwarded: conversation.$agent }, 'forward');
819
819
  emitAddMessage(pushMessage(messages, {
820
820
  id: idGenerator("sys"),
821
821
  role: "system",
@@ -825,7 +825,7 @@ export const Spirits = {
825
825
  progress(`Forwarded to agent`, 'info', 'ADD_MESSAGE', messages[messages.length - 1]);
826
826
 
827
827
  } else {
828
- conversation = updateConversation(conversation, { forwarded: forward.to });
828
+ conversation = updateConversation(conversation, { forwarded: forward.to }, 'forward');
829
829
  emitAddMessage(pushMessage(messages, {
830
830
  id: idGenerator("sys"),
831
831
  role: "system",
@@ -914,7 +914,6 @@ export const Spirits = {
914
914
 
915
915
  if (contextUpsert) {
916
916
  context = updateContext(context, contextUpsert);
917
- progress('Upserted context', 'info', 'UPDATE_CONTEXT', contextUpsert);
918
917
  }
919
918
 
920
919
  if (resetIntent) {
@@ -932,13 +931,7 @@ export const Spirits = {
932
931
  locked: false,
933
932
  lockedReason: '',
934
933
  lockAttempts: 0,
935
- });
936
- progress(
937
- 'Reset conversation intent',
938
- 'info',
939
- 'UPDATE_CONVERSATION',
940
- { intent: null, intentScore: null, locked: false, lockAttempts: 0, lockedReason: '' }
941
- );
934
+ }, 'reset conversation intent');
942
935
  }
943
936
 
944
937
  // 5. Generate response
@@ -1103,6 +1096,7 @@ export const Spirits = {
1103
1096
  ? transformResponse.messages
1104
1097
  : [{ role: 'agent', content: transformResponse.message }];
1105
1098
 
1099
+ const appendedList = [];
1106
1100
  for (const message of transformedMessages) {
1107
1101
  const adjusted = {
1108
1102
  id: idGenerator('agent'),
@@ -1129,10 +1123,12 @@ export const Spirits = {
1129
1123
  }
1130
1124
 
1131
1125
  const appended = pushMessage(messages, adjusted);
1132
- progress("Added agent message", "info", "ADD_MESSAGE", appended);
1126
+ appendedList.push(appended);
1133
1127
  emitAddMessage(appended);
1134
1128
  }
1135
1129
 
1130
+ progress(`Added ${appendedList.length} persona message${appendedList.length === 1 ? '' : 's'}`, "info", "ADD_MESSAGES", appendedList);
1131
+
1136
1132
  } catch (e) {
1137
1133
  console.error(`Spirits: Locking conversation, error transforming response: ${e.message}`);
1138
1134
  conversation = lockConversation(conversation, 'API: ' + e.message);
@@ -1159,8 +1155,8 @@ export const Spirits = {
1159
1155
  }
1160
1156
 
1161
1157
  logToolPairingIssues(messages, 'final');
1162
-
1163
- progress(`Spirits.customer start ended in ${elapsedSeconds(rootStart)}s`, 'success', undefined, undefined);
1158
+
1159
+ progress(`Spirits.customer executed in ${elapsedSeconds(rootStart)}s total`, 'success', undefined, undefined);
1164
1160
 
1165
1161
  return {
1166
1162
  conversation: {