botium-core 1.15.5 → 1.15.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "botium-core",
3
- "version": "1.15.5",
3
+ "version": "1.15.6",
4
4
  "description": "The Selenium for Chatbots",
5
5
  "main": "index.js",
6
6
  "module": "dist/botium-es.js",
@@ -104,6 +104,13 @@ module.exports = {
104
104
  SIMPLEREST_INBOUND_ORDER_UNSETTLED_EVENTS_JSONPATH: 'SIMPLEREST_INBOUND_ORDER_UNSETTLED_EVENTS_JSONPATH',
105
105
  SIMPLEREST_INBOUND_DEBOUNCE_TIMEOUT: 'SIMPLEREST_INBOUND_DEBOUNCE_TIMEOUT',
106
106
  SIMPLEREST_COOKIE_REPLICATION: 'SIMPLEREST_COOKIE_REPLICATION',
107
+ SIMPLEREST_NLP_INTENT_JSONPATH: 'SIMPLEREST_NLP_INTENT_JSONPATH',
108
+ SIMPLEREST_NLP_CONFIDENCE_JSONPATH: 'SIMPLEREST_NLP_CONFIDENCE_JSONPATH',
109
+ SIMPLEREST_NLP_CONFIDENCE_DIVIDEBY100: 'SIMPLEREST_NLP_CONFIDENCE_DIVIDEBY100',
110
+ SIMPLEREST_NLP_FALLBACK_INTENTS: 'SIMPLEREST_NLP_FALLBACK_INTENTS',
111
+ SIMPLEREST_NLP_LIST_JSONPATH: 'SIMPLEREST_NLP_LIST_JSONPATH',
112
+ SIMPLEREST_NLP_LIST_INTENT_JSONPATH: 'SIMPLEREST_NLP_LIST_INTENT_JSONPATH',
113
+ SIMPLEREST_NLP_LIST_CONFIDENCE_JSONPATH: 'SIMPLEREST_NLP_LIST_CONFIDENCE_JSONPATH',
107
114
  // Script Compiler
108
115
  SCRIPTING_TXT_EOL: 'SCRIPTING_TXT_EOL',
109
116
  // ROW_PER_MESSAGE or QUESTION_ANSWER
@@ -496,6 +496,76 @@ module.exports = class SimpleRestContainer {
496
496
  })
497
497
  debug(`found response cards: ${util.inspect(cards)}`)
498
498
 
499
+ let nlp = { intent: {} }
500
+ const _normalizeIntent = (intent) => {
501
+ if ('name' in intent) {
502
+ if (!intent.name) {
503
+ intent.name = 'None'
504
+ }
505
+ const fallbackIntents = this.caps[Capabilities.SIMPLEREST_NLP_FALLBACK_INTENTS]
506
+ if (fallbackIntents) {
507
+ intent.incomprehension = this.caps[Capabilities.SIMPLEREST_NLP_FALLBACK_INTENTS].includes(intent.name) ? true : undefined
508
+ } else {
509
+ intent.incomprehension = intent.name.toLowerCase() === 'none' ? true : undefined
510
+ }
511
+ }
512
+
513
+ if (!_.isNil(intent.confidence)) {
514
+ if (typeof intent.confidence === 'string') {
515
+ intent.confidence = Number(intent.confidence)
516
+ }
517
+ if (_.isNil(this.caps[Capabilities.SIMPLEREST_NLP_CONFIDENCE_DIVIDEBY100]) ? intent.confidence > 1 : this.caps[Capabilities.SIMPLEREST_NLP_CONFIDENCE_DIVIDEBY100]) {
518
+ intent.confidence /= 100
519
+ }
520
+ }
521
+
522
+ return intent
523
+ }
524
+ const jsonPathIntent = this.caps[Capabilities.SIMPLEREST_NLP_INTENT_JSONPATH]
525
+ if (jsonPathIntent) {
526
+ const name = jp.query(jsonPathRoot, jsonPathIntent)
527
+ nlp.intent.name = name?.length ? name[0] : null
528
+ }
529
+ const jsonPathConfidence = this.caps[Capabilities.SIMPLEREST_NLP_CONFIDENCE_JSONPATH]
530
+ if (jsonPathConfidence) {
531
+ const confidence = jp.query(jsonPathRoot, jsonPathConfidence)
532
+ nlp.intent.confidence = confidence?.length ? confidence[0] : null
533
+ }
534
+ _normalizeIntent(nlp.intent)
535
+ const jsonPathList = this.caps[Capabilities.SIMPLEREST_NLP_LIST_JSONPATH]
536
+ const jsonPathListIntent = this.caps[Capabilities.SIMPLEREST_NLP_LIST_INTENT_JSONPATH]
537
+ const jsonPathListConfidence = this.caps[Capabilities.SIMPLEREST_NLP_LIST_CONFIDENCE_JSONPATH]
538
+ if (jsonPathList) {
539
+ const list = jp.query(jsonPathRoot, jsonPathList)
540
+ if (list?.length) {
541
+ const intentList = list[0].map(e => {
542
+ const name = jp.query(e, jsonPathListIntent)
543
+ const res = { name: name?.length ? name[0] : null }
544
+ if (jsonPathListConfidence) {
545
+ const confidence = jp.query(e, jsonPathListConfidence)
546
+ res.confidence = confidence?.length ? confidence[0] : null
547
+ }
548
+ _normalizeIntent(res)
549
+ return res
550
+ })
551
+ if (intentList.length > 0 && !nlp.intent.name) {
552
+ nlp.intent.name = intentList[0].name
553
+ nlp.intent.confidence = intentList[0].confidence
554
+ }
555
+
556
+ if (nlp.intent.name === intentList[0].name) {
557
+ intentList.shift()
558
+ }
559
+
560
+ nlp.intent.intents = intentList
561
+ }
562
+ }
563
+ if (Object.keys(nlp.intent).length > 0) {
564
+ debug(`found response nlp: ${util.inspect(nlp)}`)
565
+ } else {
566
+ nlp = null
567
+ }
568
+
499
569
  let hasMessageText = false
500
570
  const jsonPathsTexts = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, this.caps)
501
571
  for (const jsonPath of jsonPathsTexts) {
@@ -509,18 +579,18 @@ module.exports = class SimpleRestContainer {
509
579
  if (!messageText) continue
510
580
 
511
581
  hasMessageText = true
512
- const botMsg = { sourceData: body, messageText, media, buttons, cards }
582
+ const botMsg = { sourceData: body, messageText, media, buttons, nlp }
513
583
  await executeHook(this.caps, this.responseHook, Object.assign({ botMsg, botMsgRoot: jsonPathRoot, messageTextIndex }, this.view))
514
584
  result.push(botMsg)
515
585
  }
516
586
  }
517
587
 
518
588
  if (!hasMessageText) {
519
- const botMsg = { messageText: '', sourceData: body, media, buttons, cards }
589
+ const botMsg = { messageText: '', sourceData: body, media, buttons, cards, nlp }
520
590
  const beforeHookKeys = Object.keys(botMsg)
521
591
  await executeHook(this.caps, this.responseHook, Object.assign({ botMsg, botMsgRoot: jsonPathRoot }, this.view))
522
592
  const afterHookKeys = Object.keys(botMsg)
523
- if (beforeHookKeys.length !== afterHookKeys.length || !!(botMsg.messageText && botMsg.messageText.length > 0) || botMsg.media.length > 0 || botMsg.buttons.length > 0 || botMsg.cards.length > 0 || !this.caps[Capabilities.SIMPLEREST_IGNORE_EMPTY]) {
593
+ if (beforeHookKeys.length !== afterHookKeys.length || !!(botMsg.messageText && botMsg.messageText.length > 0) || botMsg.media.length > 0 || botMsg.buttons.length > 0 || botMsg.cards.length > 0 || botMsg.nlp || !this.caps[Capabilities.SIMPLEREST_IGNORE_EMPTY]) {
524
594
  result.push(botMsg)
525
595
  }
526
596
  }
@@ -1190,6 +1190,68 @@ describe('connectors.simplerest', function () {
1190
1190
 
1191
1191
  await container.Clean()
1192
1192
  })
1193
+
1194
+ it('should extract intent and confidence', async function () {
1195
+ const myCaps = Object.assign({}, myCapsGet, {
1196
+ [Capabilities.SIMPLEREST_NLP_INTENT_JSONPATH]: '$.intent.intentname',
1197
+ [Capabilities.SIMPLEREST_NLP_CONFIDENCE_JSONPATH]: '$.intent.intentconfidence'
1198
+ })
1199
+ const driver = new BotDriver(myCaps)
1200
+ const container = await driver.Build()
1201
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
1202
+
1203
+ await container.Start()
1204
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({
1205
+ intent: {
1206
+ intentname: 'iname',
1207
+ intentconfidence: '50'
1208
+ }
1209
+ }, {}, true)
1210
+
1211
+ assert.equal(msgs?.length, 1)
1212
+ assert.exists(msgs[0].nlp)
1213
+ assert.exists(msgs[0].nlp.intent)
1214
+ assert.equal(msgs[0].nlp.intent.name, 'iname')
1215
+ assert.equal(msgs[0].nlp.intent.confidence, 0.5)
1216
+
1217
+ await container.Clean()
1218
+ })
1219
+
1220
+ it('should extract intent list', async function () {
1221
+ const myCaps = Object.assign({}, myCapsGet, {
1222
+ [Capabilities.SIMPLEREST_NLP_LIST_JSONPATH]: '$.intents',
1223
+ [Capabilities.SIMPLEREST_NLP_LIST_INTENT_JSONPATH]: '$.intentname',
1224
+ [Capabilities.SIMPLEREST_NLP_LIST_CONFIDENCE_JSONPATH]: '$.intentconfidence'
1225
+ })
1226
+ const driver = new BotDriver(myCaps)
1227
+ const container = await driver.Build()
1228
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
1229
+
1230
+ await container.Start()
1231
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({
1232
+ intents: [
1233
+ {
1234
+ intentname: 'iname1',
1235
+ intentconfidence: '50'
1236
+ },
1237
+ {
1238
+ intentname: 'iname2',
1239
+ intentconfidence: 25
1240
+ }
1241
+ ]
1242
+ }, {}, true)
1243
+
1244
+ assert.equal(msgs?.length, 1)
1245
+ assert.exists(msgs[0].nlp)
1246
+ assert.exists(msgs[0].nlp.intent)
1247
+ assert.equal(msgs[0].nlp.intent.name, 'iname1')
1248
+ assert.equal(msgs[0].nlp.intent.confidence, 0.5)
1249
+ assert.equal(msgs[0].nlp.intent.intents.length, 1)
1250
+ assert.equal(msgs[0].nlp.intent.intents[0].name, 'iname2')
1251
+ assert.equal(msgs[0].nlp.intent.intents[0].confidence, 0.25)
1252
+
1253
+ await container.Clean()
1254
+ })
1193
1255
  })
1194
1256
 
1195
1257
  describe('parseCapabilities', function () {