wingbot 3.25.1 → 3.26.0-alpha.4

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": "wingbot",
3
- "version": "3.25.1",
3
+ "version": "3.26.0-alpha.4",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/BotApp.js CHANGED
@@ -22,6 +22,7 @@ const DEFAULT_API_URL = 'https://orchestrator-api.wingbot.ai';
22
22
  /** @typedef {import('./Processor').Plugin} Plugin */
23
23
 
24
24
  /** @typedef {import('./CallbackAuditLog')} AuditLog */
25
+ /** @typedef {import('./BotAppSender').TlsOptions} TlsOptions */
25
26
 
26
27
  /**
27
28
  * @typedef {object} BotAppOptions
@@ -32,6 +33,7 @@ const DEFAULT_API_URL = 'https://orchestrator-api.wingbot.ai';
32
33
  * @prop {ChatLogStorage} [chatLogStorage]
33
34
  * @prop {boolean} [preferSynchronousResponse]
34
35
  * @prop {AuditLog} [auditLog]
36
+ * @prop {TlsOptions} [tls]
35
37
  *
36
38
  * @typedef {ProcessorOptions & BotAppOptions} Options
37
39
  */
@@ -63,6 +65,7 @@ class BotApp {
63
65
  appId = null,
64
66
  preferSynchronousResponse = false,
65
67
  auditLog = null,
68
+ tls = null,
66
69
  ...processorOptions
67
70
  } = options;
68
71
 
@@ -70,6 +73,7 @@ class BotApp {
70
73
  this._fetch = fetch; // mock
71
74
  this._appId = appId;
72
75
  this._auditLog = auditLog;
76
+ this._tls = tls;
73
77
 
74
78
  let { apiUrl } = options;
75
79
 
@@ -96,6 +100,18 @@ class BotApp {
96
100
  this._preferSynchronousResponse = preferSynchronousResponse;
97
101
  }
98
102
 
103
+ /**
104
+ * Get authorization token for wingbot orchestrator
105
+ *
106
+ * @param {string} body
107
+ * @param {string} secret
108
+ * @param {string} appId
109
+ * @returns {Promise<string>}
110
+ */
111
+ static signBody (body, secret, appId) {
112
+ return BotAppSender.signBody(body, secret, appId);
113
+ }
114
+
99
115
  /**
100
116
  * Returns processor plugin, which updates thread context automatically
101
117
  *
@@ -211,7 +227,8 @@ class BotApp {
211
227
  appId,
212
228
  secret,
213
229
  mid,
214
- fetch: this._fetch
230
+ fetch: this._fetch,
231
+ tls: this._tls
215
232
  };
216
233
 
217
234
  const sender = new BotAppSender(options, senderId, message, this._senderLogger);
@@ -6,17 +6,25 @@
6
6
  const jwt = require('jsonwebtoken');
7
7
  const { default: fetch, Headers } = require('node-fetch');
8
8
  const crypto = require('crypto');
9
+ const { Agent } = require('https');
9
10
  const { promisify } = require('util');
10
11
  const ReturnSender = require('./ReturnSender');
11
12
 
12
13
  /** @typedef {import('./ReturnSender').ReturnSenderOptions} ReturnSenderOptions */
13
14
  /** @typedef {import('./ReturnSender').ChatLogStorage} ChatLogStorage */
14
15
 
16
+ /**
17
+ * @typedef {object} TlsOptions
18
+ * @prop {string|Promise<string>} key
19
+ * @prop {string|Promise<string>} cert
20
+ */
21
+
15
22
  /**
16
23
  * @typedef {object} BotAppSenderOptions
17
24
  * @prop {string} apiUrl
18
25
  * @prop {string} pageId
19
26
  * @prop {string} appId
27
+ * @prop {TlsOptions} [tls]
20
28
  * @prop {string} [mid]
21
29
  * @prop {Function} [fetch]
22
30
  * @prop {string|Promise<string>} secret
@@ -47,6 +55,9 @@ class BotAppSender extends ReturnSender {
47
55
 
48
56
  this._secret = Promise.resolve(options.secret);
49
57
 
58
+ this._tls = options.tls;
59
+ this._agent = null;
60
+
50
61
  this._fetch = options.fetch || fetch;
51
62
  }
52
63
 
@@ -65,6 +76,21 @@ class BotAppSender extends ReturnSender {
65
76
  }, goodSecret);
66
77
  }
67
78
 
79
+ async _getAgent () {
80
+ if (!this._tls) {
81
+ return null;
82
+ }
83
+ if (!this._agent) {
84
+ const [key, cert] = await Promise.all([
85
+ Promise.resolve(this._tls.key), Promise.resolve(this._tls.cert)
86
+ ]);
87
+
88
+ this._agent = new Agent({ cert, key });
89
+ }
90
+
91
+ return this._agent;
92
+ }
93
+
68
94
  async _send (payload) {
69
95
  // attach sender
70
96
  if (typeof payload.sender === 'undefined') {
@@ -78,14 +104,19 @@ class BotAppSender extends ReturnSender {
78
104
 
79
105
  const body = JSON.stringify(payload);
80
106
 
81
- const token = await BotAppSender.signBody(body, this._secret, this._appId);
107
+ const [token, agent] = await Promise.all([
108
+ BotAppSender.signBody(body, this._secret, this._appId),
109
+ this._getAgent()
110
+ ]);
82
111
 
83
112
  const headers = new Headers();
84
113
 
85
114
  headers.set('Authorization', token);
86
115
  headers.set('Content-Type', 'application/json');
87
116
 
88
- const response = await this._fetch(this._apiUrl, { headers, body, method: 'POST' })
117
+ const response = await this._fetch(this._apiUrl, {
118
+ headers, body, agent, method: 'POST'
119
+ })
89
120
  .then((r) => r.json());
90
121
 
91
122
  const { request, errors = null } = response;
@@ -179,7 +179,7 @@ function getSetState (setState = {}, req, res = null, useState = null) {
179
179
  const useValue = typeof value === 'string'
180
180
  ? handlebars.compile(value)(stateData(req, res))
181
181
  .split(/(?<!\\),/g)
182
- .map((v) => v.trim())
182
+ .map((v) => v.replace(/\\,/g, ',').trim())
183
183
  : value;
184
184
  values = toArray(useValue);
185
185
  }
@@ -284,12 +284,12 @@ class CustomEntityDetectionModel {
284
284
  * @param {string} text
285
285
  * @param {string} [singleEntity]
286
286
  * @param {string[]} expectedEntities
287
+ * @param {DetectedEntity[]} [entities] - previously detected entities to include
287
288
  * @returns {Promise<DetectedEntity[]>}
288
289
  */
289
- async resolveEntities (text, singleEntity = null, expectedEntities = []) {
290
+ async resolveEntities (text, singleEntity = null, expectedEntities = [], entities = []) {
290
291
  const resolved = new Set();
291
292
  let missing = Array.from(this._entityDetectors.keys());
292
- const entities = [];
293
293
 
294
294
  while (missing.length !== 0) {
295
295
  let detect = [];