@telia-ace/knowledge-widget-bot-provider 0.0.1-alpha.45 → 0.0.1-alpha.48

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.
Files changed (129) hide show
  1. package/{lib-esm → dist}/client/chat.client.d.ts +0 -0
  2. package/{lib-esm → dist}/client/contact-method.client.d.ts +0 -0
  3. package/{lib-esm → dist}/client/http.client.d.ts +0 -0
  4. package/{lib-esm → dist}/client/middleware.client.d.ts +0 -0
  5. package/dist/contact-method.js +102 -0
  6. package/dist/contact-method.js.map +1 -0
  7. package/{lib-esm → dist}/contract/custom-component/contact-method-item.d.ts +0 -0
  8. package/{lib-esm → dist}/contract/custom-component/contact-method-list.d.ts +0 -0
  9. package/{lib-esm → dist}/contract/custom-component/contact-method.d.ts +0 -0
  10. package/{lib-esm → dist}/contract/custom-component/entry-context.d.ts +0 -0
  11. package/{lib-esm → dist}/contract/enum/server-entry-type.enum.d.ts +0 -0
  12. package/{lib-esm → dist}/contract/model/conversation-api-model/list.model.d.ts +0 -0
  13. package/{lib-esm → dist}/contract/model/conversation-api-model/message.model.d.ts +0 -0
  14. package/{lib-esm → dist}/contract/model/http/action.model.d.ts +0 -0
  15. package/{lib-esm → dist}/contract/model/http/client-action.model.d.ts +0 -0
  16. package/{lib-esm → dist}/contract/model/http/client-envelope.model.d.ts +0 -0
  17. package/{lib-esm → dist}/contract/model/http/connect-options.model.d.ts +0 -0
  18. package/{lib-esm → dist}/contract/model/http/global-state.model.d.ts +0 -0
  19. package/{lib-esm → dist}/contract/model/http/http-client-options.model.d.ts +0 -0
  20. package/{lib-esm → dist}/contract/model/http/server-entry.model.d.ts +0 -0
  21. package/{lib-esm → dist}/contract/model/http/server-envelope.model.d.ts +0 -0
  22. package/{lib-esm → dist}/contract/model/middleware/aggregator-state.model.d.ts +0 -0
  23. package/{lib-esm → dist}/contract/model/shared/loose-object.model.d.ts +0 -0
  24. package/{lib-esm → dist}/helper/chat.helper.d.ts +0 -0
  25. package/{lib-esm → dist}/helper/http.helper.d.ts +0 -0
  26. package/{lib-esm → dist}/helper/middleware.helper.d.ts +0 -0
  27. package/{lib-esm → dist}/helper/store.helper.d.ts +0 -0
  28. package/{lib-esm → dist}/index.d.ts +0 -0
  29. package/dist/index.js +1131 -0
  30. package/dist/index.js.map +1 -0
  31. package/{lib-esm → dist}/interface/entry.interface.d.ts +0 -0
  32. package/{lib-esm → dist}/interface/middleware/paging.interface.d.ts +0 -0
  33. package/{lib-esm → dist}/interface/middleware/processor.interface.d.ts +0 -0
  34. package/{lib-esm → dist}/plugin.d.ts +0 -0
  35. package/{lib-esm → dist}/utils.d.ts +0 -0
  36. package/package.json +14 -15
  37. package/lib/client/chat.client.d.ts +0 -16
  38. package/lib/client/chat.client.js +0 -145
  39. package/lib/client/contact-method.client.d.ts +0 -34
  40. package/lib/client/contact-method.client.js +0 -337
  41. package/lib/client/http.client.d.ts +0 -26
  42. package/lib/client/http.client.js +0 -211
  43. package/lib/client/middleware.client.d.ts +0 -14
  44. package/lib/client/middleware.client.js +0 -125
  45. package/lib/contract/custom-component/contact-method-item.d.ts +0 -9
  46. package/lib/contract/custom-component/contact-method-item.js +0 -34
  47. package/lib/contract/custom-component/contact-method-list.d.ts +0 -7
  48. package/lib/contract/custom-component/contact-method-list.js +0 -13
  49. package/lib/contract/custom-component/contact-method.d.ts +0 -3
  50. package/lib/contract/custom-component/contact-method.js +0 -35
  51. package/lib/contract/custom-component/entry-context.d.ts +0 -5
  52. package/lib/contract/custom-component/entry-context.js +0 -4
  53. package/lib/contract/enum/server-entry-type.enum.d.ts +0 -7
  54. package/lib/contract/enum/server-entry-type.enum.js +0 -8
  55. package/lib/contract/model/conversation-api-model/list.model.d.ts +0 -5
  56. package/lib/contract/model/conversation-api-model/list.model.js +0 -1
  57. package/lib/contract/model/conversation-api-model/message.model.d.ts +0 -7
  58. package/lib/contract/model/conversation-api-model/message.model.js +0 -1
  59. package/lib/contract/model/http/action.model.d.ts +0 -3
  60. package/lib/contract/model/http/action.model.js +0 -1
  61. package/lib/contract/model/http/client-action.model.d.ts +0 -4
  62. package/lib/contract/model/http/client-action.model.js +0 -2
  63. package/lib/contract/model/http/client-envelope.model.d.ts +0 -9
  64. package/lib/contract/model/http/client-envelope.model.js +0 -1
  65. package/lib/contract/model/http/connect-options.model.d.ts +0 -5
  66. package/lib/contract/model/http/connect-options.model.js +0 -1
  67. package/lib/contract/model/http/global-state.model.d.ts +0 -8
  68. package/lib/contract/model/http/global-state.model.js +0 -1
  69. package/lib/contract/model/http/http-client-options.model.d.ts +0 -5
  70. package/lib/contract/model/http/http-client-options.model.js +0 -1
  71. package/lib/contract/model/http/server-entry.model.d.ts +0 -11
  72. package/lib/contract/model/http/server-entry.model.js +0 -1
  73. package/lib/contract/model/http/server-envelope.model.d.ts +0 -10
  74. package/lib/contract/model/http/server-envelope.model.js +0 -1
  75. package/lib/contract/model/middleware/aggregator-state.model.d.ts +0 -4
  76. package/lib/contract/model/middleware/aggregator-state.model.js +0 -2
  77. package/lib/contract/model/shared/loose-object.model.d.ts +0 -3
  78. package/lib/contract/model/shared/loose-object.model.js +0 -1
  79. package/lib/helper/chat.helper.d.ts +0 -8
  80. package/lib/helper/chat.helper.js +0 -18
  81. package/lib/helper/http.helper.d.ts +0 -13
  82. package/lib/helper/http.helper.js +0 -28
  83. package/lib/helper/middleware.helper.d.ts +0 -12
  84. package/lib/helper/middleware.helper.js +0 -68
  85. package/lib/helper/store.helper.d.ts +0 -23
  86. package/lib/helper/store.helper.js +0 -169
  87. package/lib/index.d.ts +0 -2
  88. package/lib/index.js +0 -2
  89. package/lib/interface/entry.interface.d.ts +0 -20
  90. package/lib/interface/entry.interface.js +0 -290
  91. package/lib/interface/middleware/paging.interface.d.ts +0 -4
  92. package/lib/interface/middleware/paging.interface.js +0 -53
  93. package/lib/interface/middleware/processor.interface.d.ts +0 -10
  94. package/lib/interface/middleware/processor.interface.js +0 -118
  95. package/lib/plugin.d.ts +0 -3
  96. package/lib/plugin.js +0 -154
  97. package/lib/utils.d.ts +0 -2
  98. package/lib/utils.js +0 -79
  99. package/lib-esm/client/chat.client.js +0 -145
  100. package/lib-esm/client/contact-method.client.js +0 -337
  101. package/lib-esm/client/http.client.js +0 -211
  102. package/lib-esm/client/middleware.client.js +0 -125
  103. package/lib-esm/contract/custom-component/contact-method-item.js +0 -34
  104. package/lib-esm/contract/custom-component/contact-method-list.js +0 -13
  105. package/lib-esm/contract/custom-component/contact-method.js +0 -35
  106. package/lib-esm/contract/custom-component/entry-context.js +0 -4
  107. package/lib-esm/contract/enum/server-entry-type.enum.js +0 -8
  108. package/lib-esm/contract/model/conversation-api-model/list.model.js +0 -1
  109. package/lib-esm/contract/model/conversation-api-model/message.model.js +0 -1
  110. package/lib-esm/contract/model/http/action.model.js +0 -1
  111. package/lib-esm/contract/model/http/client-action.model.js +0 -2
  112. package/lib-esm/contract/model/http/client-envelope.model.js +0 -1
  113. package/lib-esm/contract/model/http/connect-options.model.js +0 -1
  114. package/lib-esm/contract/model/http/global-state.model.js +0 -1
  115. package/lib-esm/contract/model/http/http-client-options.model.js +0 -1
  116. package/lib-esm/contract/model/http/server-entry.model.js +0 -1
  117. package/lib-esm/contract/model/http/server-envelope.model.js +0 -1
  118. package/lib-esm/contract/model/middleware/aggregator-state.model.js +0 -2
  119. package/lib-esm/contract/model/shared/loose-object.model.js +0 -1
  120. package/lib-esm/helper/chat.helper.js +0 -18
  121. package/lib-esm/helper/http.helper.js +0 -28
  122. package/lib-esm/helper/middleware.helper.js +0 -68
  123. package/lib-esm/helper/store.helper.js +0 -169
  124. package/lib-esm/index.js +0 -2
  125. package/lib-esm/interface/entry.interface.js +0 -290
  126. package/lib-esm/interface/middleware/paging.interface.js +0 -53
  127. package/lib-esm/interface/middleware/processor.interface.js +0 -118
  128. package/lib-esm/plugin.js +0 -154
  129. package/lib-esm/utils.js +0 -79
package/dist/index.js ADDED
@@ -0,0 +1,1131 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+ import { ContactMethodPlatform } from "@telia-ace/knowledge-widget-adapters";
41
+ import { DataType, DataClient } from "@telia-ace/knowledge-widget-types-grid";
42
+ import { ConversationMessageListItemType, ConversationPlatform, registerCustomMessageComponent } from "@telia-ace/widget-conversation";
43
+ import { v4 } from "uuid";
44
+ import { createStorageWriter, StorageCategory, readStorage } from "@telia-ace/widget-services";
45
+ import { FormBuilder, submitButton } from "@telia-ace/widget-forms";
46
+ var ServerEntryTypeEnum = /* @__PURE__ */ ((ServerEntryTypeEnum2) => {
47
+ ServerEntryTypeEnum2[ServerEntryTypeEnum2["Widget"] = 0] = "Widget";
48
+ ServerEntryTypeEnum2[ServerEntryTypeEnum2["Text"] = 1] = "Text";
49
+ ServerEntryTypeEnum2[ServerEntryTypeEnum2["Contact"] = 2] = "Contact";
50
+ ServerEntryTypeEnum2[ServerEntryTypeEnum2["List"] = 3] = "List";
51
+ ServerEntryTypeEnum2[ServerEntryTypeEnum2["Error"] = 4] = "Error";
52
+ return ServerEntryTypeEnum2;
53
+ })(ServerEntryTypeEnum || {});
54
+ class ChatHelper {
55
+ static setUser(user) {
56
+ this.user = user;
57
+ }
58
+ static setAgent(agent) {
59
+ this.agent = agent;
60
+ }
61
+ static getUser() {
62
+ return this.user;
63
+ }
64
+ static getAgent() {
65
+ return this.agent;
66
+ }
67
+ }
68
+ const _MiddlewareHelper = class {
69
+ static setContainer(container) {
70
+ _MiddlewareHelper.container = container;
71
+ }
72
+ static setWidget(widget) {
73
+ this.widget = widget;
74
+ }
75
+ static getContainer() {
76
+ return this.container;
77
+ }
78
+ static getWidget() {
79
+ return this.widget;
80
+ }
81
+ static getEntrySessionStorage() {
82
+ return readStorage(this.container, this.sessionMessageStorageVariable);
83
+ }
84
+ };
85
+ let MiddlewareHelper = _MiddlewareHelper;
86
+ MiddlewareHelper.sessionMessageStorageVariable = "conversation-plugin-messages";
87
+ MiddlewareHelper.setEntrySessionStorage = (messages) => __async(this, null, function* () {
88
+ return createStorageWriter(_MiddlewareHelper.container, _MiddlewareHelper.sessionMessageStorageVariable, StorageCategory.Necessary).then((write) => {
89
+ return write(messages);
90
+ });
91
+ });
92
+ const botProviderGlobalStateSessionKey = "bot-provider-global-state";
93
+ const _StoreHelper = class {
94
+ constructor() {
95
+ _StoreHelper.globalState = {
96
+ completedInteractionsCount: 0,
97
+ lastBotGuide: 0,
98
+ site: "",
99
+ invincibleParameters: {}
100
+ };
101
+ }
102
+ static insertOrUpdateMessageArrayStore(messageArray, entry) {
103
+ return __async(this, null, function* () {
104
+ for (const message of messageArray.filter((o) => !!o)) {
105
+ if (!this.messageStore.find((o) => o.id == message.id)) {
106
+ this.insertMessageToStore(message);
107
+ } else {
108
+ this.updateMessageInStore(message, entry);
109
+ }
110
+ }
111
+ yield MiddlewareHelper.setEntrySessionStorage(this.messageStore);
112
+ });
113
+ }
114
+ static insertMessageToStore(message) {
115
+ this.messageStore.push(message);
116
+ }
117
+ static updateMessageInStore(message, entry) {
118
+ message.conversationEntry = entry;
119
+ let index = this.messageStore.findIndex((o) => o.id == message.id);
120
+ this.messageStore[index] = message;
121
+ }
122
+ static checkIdInMessageStore(id) {
123
+ return this.messageStore.find((o) => o.id == id) === void 0;
124
+ }
125
+ static getLocalState(action) {
126
+ const entry = this.getEntry(action);
127
+ return entry == null ? void 0 : entry.localState;
128
+ }
129
+ static getGlobalState() {
130
+ return _StoreHelper.globalState;
131
+ }
132
+ static setGlobalState(state) {
133
+ _StoreHelper.globalState = state;
134
+ return createStorageWriter(MiddlewareHelper.getContainer(), botProviderGlobalStateSessionKey, StorageCategory.Necessary).then((write) => {
135
+ return write(state);
136
+ });
137
+ }
138
+ static getAction(action) {
139
+ const entry = this.getEntry(action);
140
+ return {
141
+ key: action,
142
+ content: entry == null ? void 0 : entry.actions[action]
143
+ };
144
+ }
145
+ static getCommand(text) {
146
+ return { phrase: text };
147
+ }
148
+ static getEntry(action) {
149
+ let entry = void 0;
150
+ this.getEntryStore().forEach((o) => {
151
+ if (o.actions[action]) {
152
+ entry = o;
153
+ }
154
+ });
155
+ return entry;
156
+ }
157
+ static getMessage(id) {
158
+ return this.messageStore.find((o) => o.id == id);
159
+ }
160
+ static getEntryStore() {
161
+ return this.messageStore.filter((o) => !!o.entry).map((o) => {
162
+ return o.entry;
163
+ });
164
+ }
165
+ static rehydrateState() {
166
+ return __async(this, null, function* () {
167
+ const state = yield readStorage(MiddlewareHelper.getContainer(), botProviderGlobalStateSessionKey);
168
+ if (state) {
169
+ this.setGlobalState(state);
170
+ }
171
+ });
172
+ }
173
+ };
174
+ let StoreHelper = _StoreHelper;
175
+ StoreHelper.messageStore = [];
176
+ const _ContactMethodClient = class {
177
+ constructor(container) {
178
+ this.handleNewForm = (errors, entry) => __async(this, null, function* () {
179
+ this.updateForm(entry.contactMethods, {
180
+ submit: "Submit",
181
+ errors
182
+ });
183
+ entry.entry.update({
184
+ contactMethods: entry.contactMethods,
185
+ type: "ace-knowledge-contact-method",
186
+ noPadding: true
187
+ });
188
+ });
189
+ this.handleContactMethod = (input = "") => __async(this, null, function* () {
190
+ var _a;
191
+ if (input == "contact") {
192
+ yield this.handleContactCategoryList(input);
193
+ } else {
194
+ let contactMethods = yield this.loadContactMethods(input.split("/")[1], input.split("/")[0]);
195
+ if (!!((_a = contactMethods.body) == null ? void 0 : _a.form)) {
196
+ this.handleForm(input, contactMethods);
197
+ } else if (!contactMethods.expanded && contactMethods.inline) {
198
+ this.handleSingleContactMethod(contactMethods);
199
+ } else if (!!contactMethods.contactMethods) {
200
+ this.handleContactMethodList(input, contactMethods);
201
+ }
202
+ }
203
+ });
204
+ this.handleContactCategoryList = (input) => __async(this, null, function* () {
205
+ let newGuid = v4();
206
+ let type = DataType.ContactMethodCategories;
207
+ let contactMethods = yield this.loadContactMethods(type);
208
+ this.chatClient.printUserMessageAndStoreIt(input);
209
+ this.printMessage(newGuid, contactMethods);
210
+ });
211
+ this.updateForm = (contactMethod, properties) => {
212
+ var _a;
213
+ if ((_a = contactMethod == null ? void 0 : contactMethod.body) == null ? void 0 : _a.form) {
214
+ const { body } = contactMethod;
215
+ const builder = new FormBuilder(body.form);
216
+ if (!body.form.layout || !body.form.layout.submit) {
217
+ builder.createComponent(submitButton(properties.submit, "submit"));
218
+ }
219
+ if (properties.errors) {
220
+ builder.setValidationErrors(properties.errors);
221
+ }
222
+ return __spreadProps(__spreadValues({}, body), {
223
+ form: builder.get()
224
+ });
225
+ }
226
+ return (contactMethod == null ? void 0 : contactMethod.body) || {};
227
+ };
228
+ this.chatClient = new ChatClient(container);
229
+ }
230
+ static initialize(container, dataClient, contactMethodPlatform, agent) {
231
+ this.dataClient = dataClient;
232
+ this.contactMethodPlatform = contactMethodPlatform;
233
+ this.agent = agent;
234
+ this.container = container;
235
+ }
236
+ validate(form) {
237
+ let entry = _ContactMethodClient.contactMessageEntryArray.find((o) => o.id == form.schema.id);
238
+ _ContactMethodClient.contactMethodPlatform.validate(entry.contactMethods).then((result) => {
239
+ this.handleNewForm(result.errors, entry);
240
+ if (result.valid) {
241
+ this.submit(entry);
242
+ this.removeForm(entry);
243
+ }
244
+ });
245
+ }
246
+ handleForm(input, contactMethods) {
247
+ let title = input.split("/")[2];
248
+ contactMethods.expanded = true;
249
+ this.updateForm(contactMethods, {
250
+ submit: "Submit",
251
+ errors: void 0
252
+ });
253
+ this.printMessage(contactMethods.id, contactMethods, title);
254
+ }
255
+ addContactMethod(contactMethod) {
256
+ contactMethod.expanded = true;
257
+ this.updateForm(contactMethod, {
258
+ submit: "Submit",
259
+ errors: void 0
260
+ });
261
+ return this.printMessage(contactMethod.id, contactMethod);
262
+ }
263
+ expandContactMethod(input) {
264
+ const message = StoreHelper.getMessage(input.entryId);
265
+ if ((message == null ? void 0 : message.conversationEntry) && Array.isArray(message.message)) {
266
+ const messageItemIndex = message.message.findIndex(([listType]) => listType === "ace-knowledge-contact-method");
267
+ if (messageItemIndex > -1) {
268
+ const [messageItemType, messageItemContent] = message.message[messageItemIndex];
269
+ const updatedMessageItemContent = __spreadProps(__spreadValues({}, messageItemContent), {
270
+ contactMethods: messageItemContent.contactMethods.map((cm) => {
271
+ cm.expanded = true;
272
+ return cm;
273
+ })
274
+ });
275
+ const updatedMessageItems = [
276
+ ...message.message.slice(0, messageItemIndex),
277
+ [messageItemType, updatedMessageItemContent],
278
+ ...message.message.slice(messageItemIndex + 1)
279
+ ];
280
+ message.conversationEntry.writer({ items: updatedMessageItems });
281
+ }
282
+ }
283
+ }
284
+ handleContactMethodList(input, contactMethods) {
285
+ let title = input.split("/")[2];
286
+ let newGuid = v4();
287
+ this.printMessage(newGuid, contactMethods, title);
288
+ }
289
+ handleSingleContactMethod(contactMethods) {
290
+ let entry = _ContactMethodClient.contactMessageEntryArray.find((o) => o.contactMethods.contactMethods.find((t) => t.id == contactMethods.id));
291
+ let curCM = entry.contactMethods.contactMethods.find((t) => t.id == contactMethods.id);
292
+ curCM.expanded = true;
293
+ let message = {
294
+ contactMethods: entry.contactMethods,
295
+ type: "ace-knowledge-contact-method",
296
+ noPadding: true
297
+ };
298
+ StoreHelper.insertOrUpdateMessageArrayStore([
299
+ {
300
+ id: entry.id,
301
+ message,
302
+ isUser: false
303
+ }
304
+ ]);
305
+ entry.entry.update(message);
306
+ }
307
+ submit(entry) {
308
+ _ContactMethodClient.contactMethodPlatform.submit(entry.contactMethods).then((result) => {
309
+ if (!!result)
310
+ _ContactMethodClient.agent.print(result);
311
+ });
312
+ }
313
+ removeForm(entry) {
314
+ entry.entry.remove();
315
+ _ContactMethodClient.contactMessageEntryArray.splice(_ContactMethodClient.contactMessageEntryArray.indexOf(entry), 1);
316
+ }
317
+ printMessage(newGuid, contactMethods, title = void 0) {
318
+ if (_ContactMethodClient.contactMessageEntryArray.find((o) => o.id == contactMethods.id)) {
319
+ let entry = _ContactMethodClient.contactMessageEntryArray.find((o) => o.id == contactMethods.id);
320
+ let message = {
321
+ contactMethods,
322
+ type: "ace-knowledge-contact-method",
323
+ noPadding: true
324
+ };
325
+ entry.entry.update(message);
326
+ StoreHelper.insertOrUpdateMessageArrayStore([
327
+ {
328
+ id: entry.guid,
329
+ message,
330
+ isUser: false
331
+ }
332
+ ]);
333
+ return message;
334
+ } else {
335
+ if (title)
336
+ this.chatClient.printUserMessageAndStoreIt(title);
337
+ let message = {
338
+ contactMethods,
339
+ type: "ace-knowledge-contact-method",
340
+ noPadding: true
341
+ };
342
+ let entry = _ContactMethodClient.agent.print(message);
343
+ StoreHelper.insertOrUpdateMessageArrayStore([
344
+ {
345
+ id: newGuid,
346
+ message,
347
+ isUser: false
348
+ }
349
+ ]);
350
+ _ContactMethodClient.contactMessageEntryArray.push({
351
+ id: newGuid,
352
+ contactMethods,
353
+ entry
354
+ });
355
+ return message;
356
+ }
357
+ }
358
+ loadContactMethods(dataType, id = void 0) {
359
+ switch (dataType) {
360
+ case DataType.ContactMethodCategories: {
361
+ return _ContactMethodClient.container.getAsync("matchingClient").then((matchingClient) => {
362
+ return matchingClient.customRequest("/contactmethods", "GET", {});
363
+ }).then((contactMethods) => {
364
+ return contactMethods;
365
+ });
366
+ }
367
+ case DataType.ContactMethods: {
368
+ if (!!id) {
369
+ return _ContactMethodClient.dataClient.fetch(DataType.ContactMethod, { contactMethodId: id }).then((contactMethods) => contactMethods);
370
+ } else {
371
+ return _ContactMethodClient.dataClient.fetch(DataType.ContactMethods, {}).then((contactMethods) => contactMethods);
372
+ }
373
+ }
374
+ case DataType.ContactMethodCategories: {
375
+ return _ContactMethodClient.dataClient.fetch(DataType.ContactMethods, { contactMethodCategoryId: id }).then((contactMethods) => contactMethods);
376
+ }
377
+ default: {
378
+ return Promise.resolve(void 0);
379
+ }
380
+ }
381
+ }
382
+ };
383
+ let ContactMethodClient = _ContactMethodClient;
384
+ ContactMethodClient.contactMessageEntryArray = [];
385
+ const getContactMethodEntry = (contactMethodId) => {
386
+ const entries = StoreHelper.getEntryStore();
387
+ return entries.find((e) => e.content.id === +contactMethodId);
388
+ };
389
+ const convertLegacySymbol = (symbol) => {
390
+ if (symbol) {
391
+ const { type, content } = symbol;
392
+ if (type && content) {
393
+ return { type, content };
394
+ }
395
+ }
396
+ };
397
+ const contactMethodFromLegacy = ({
398
+ id,
399
+ title,
400
+ defaultIcon,
401
+ exits: [exit],
402
+ exitType,
403
+ confirmationText,
404
+ expandFormAutomatically,
405
+ form,
406
+ description
407
+ }, validation) => {
408
+ const { adapter } = exit || { Adapter: {} };
409
+ const { clientName = "", settings: adapterSettings = {}, customClientName = "" } = adapter;
410
+ const settings = {
411
+ id: id.toString(),
412
+ title,
413
+ description,
414
+ symbol: convertLegacySymbol(defaultIcon),
415
+ clientName: clientName === "custom.adapter" ? customClientName : clientName,
416
+ expanded: true,
417
+ inline: true,
418
+ body: {}
419
+ };
420
+ if (clientName.indexOf("phone") > -1 || clientName.indexOf("freetext") > -1) {
421
+ const { displayTextBeforeClick, textBeforeClick } = adapterSettings;
422
+ if (displayTextBeforeClick === "true") {
423
+ settings.expanded = false;
424
+ settings.title = textBeforeClick;
425
+ }
426
+ if (clientName.indexOf("phone.text") > -1) {
427
+ settings.body.phoneNumber = adapterSettings.phoneNumber;
428
+ } else {
429
+ settings.body.freetext = adapterSettings.freetext;
430
+ }
431
+ }
432
+ if (clientName.indexOf("chat.popup") > -1 || clientName.indexOf("link") > -1 || clientName === "custom.adapter" && exitType === "custom") {
433
+ settings.body = adapterSettings;
434
+ settings.body.confirmationFallback = confirmationText;
435
+ }
436
+ if (clientName.indexOf("ace") > -1) {
437
+ settings.body = adapterSettings;
438
+ }
439
+ if (form && form.hasValueComponents) {
440
+ if (validation) {
441
+ settings.body.form = FormBuilder.fromLegacyForm(id.toString(), form, validation).get();
442
+ } else {
443
+ settings.body.form = FormBuilder.fromLegacyForm(id.toString(), form).get();
444
+ }
445
+ if (!expandFormAutomatically) {
446
+ settings.expanded = settings.inline = false;
447
+ }
448
+ }
449
+ return settings;
450
+ };
451
+ class EntryInterface {
452
+ constructor(container) {
453
+ this.container = container;
454
+ this.helpActionKey = "help";
455
+ this.nextActionKey = "next";
456
+ }
457
+ transform(entry, feedbackEntry) {
458
+ return __async(this, null, function* () {
459
+ const entryType = ServerEntryTypeEnum[entry.type];
460
+ switch (entryType) {
461
+ case ServerEntryTypeEnum.List:
462
+ return {
463
+ id: entry.id,
464
+ message: this.listTransform(entry, feedbackEntry),
465
+ entry,
466
+ isUser: false
467
+ };
468
+ case ServerEntryTypeEnum.Text:
469
+ return {
470
+ id: entry.id,
471
+ message: this.textTransform(entry, feedbackEntry),
472
+ entry,
473
+ isUser: false
474
+ };
475
+ case ServerEntryTypeEnum.Contact:
476
+ return {
477
+ id: entry.id,
478
+ message: yield this.transformContactMethod(entry),
479
+ entry,
480
+ isUser: false
481
+ };
482
+ }
483
+ });
484
+ }
485
+ transformContactMethod(entry) {
486
+ return __async(this, null, function* () {
487
+ const dataClient = yield this.container.getAsync("dataClient");
488
+ const contactMethod = yield dataClient.fetch(DataType.ContactMethod, {
489
+ contactMethodId: entry.content.id
490
+ });
491
+ const cmClient = new ContactMethodClient(this.container);
492
+ cmClient.addContactMethod(contactMethod);
493
+ });
494
+ }
495
+ transformArray(entryArray) {
496
+ return __async(this, null, function* () {
497
+ let feedbackEntry = entryArray.find((o) => o.tags.find((t) => t == "gentle" || t == "feedback"));
498
+ let returnArray = [];
499
+ for (const o of entryArray.filter((t) => t !== feedbackEntry)) {
500
+ let x = yield this.transform(o, feedbackEntry);
501
+ if (!!x)
502
+ returnArray.push(x);
503
+ }
504
+ return returnArray;
505
+ });
506
+ }
507
+ getUserMessage(action) {
508
+ let entry = StoreHelper.getEntry(action);
509
+ return this.getTitleFromEntry(entry, action);
510
+ }
511
+ listTransform(entry, feedbackEntry) {
512
+ if (entry.type === "Contact") {
513
+ return;
514
+ }
515
+ if (!entry.content)
516
+ return;
517
+ let object = this.getListActions(entry.content.items, this.getContentText(entry), entry.actions, entry.localState);
518
+ if (!!feedbackEntry) {
519
+ let feedbackObject = this.getListActions(feedbackEntry.content.items, this.getContentText(feedbackEntry), feedbackEntry.actions, feedbackEntry.localState);
520
+ StoreHelper.insertMessageToStore({
521
+ id: feedbackEntry.id,
522
+ message: void 0,
523
+ entry: feedbackEntry,
524
+ isUser: false
525
+ });
526
+ return object.concat(feedbackObject);
527
+ }
528
+ return object;
529
+ }
530
+ getContentText(entry) {
531
+ if (entry.content.html)
532
+ return { body: entry.content.html };
533
+ if (entry.content.header)
534
+ return { title: entry.content.header };
535
+ }
536
+ textTransform(entry, feedbackEntry) {
537
+ if (!!entry.content.items)
538
+ return this.listTransform(entry, feedbackEntry);
539
+ let returnObj = [
540
+ ConversationMessageListItemType.HTML,
541
+ { body: entry.content.html.replace("\n", "") }
542
+ ];
543
+ if (!!feedbackEntry) {
544
+ let feedbackObject = this.getListActions(feedbackEntry.content.items, this.getContentText(feedbackEntry), feedbackEntry.actions, feedbackEntry.localState);
545
+ StoreHelper.insertMessageToStore({
546
+ id: feedbackEntry.id,
547
+ message: void 0,
548
+ entry: feedbackEntry,
549
+ isUser: false
550
+ });
551
+ feedbackObject.unshift(returnObj);
552
+ return feedbackObject;
553
+ }
554
+ return returnObj;
555
+ }
556
+ getListActions(items, text, actions, state) {
557
+ let list = [];
558
+ let mainListContent = {};
559
+ const mainListType = this.getListType(state);
560
+ let mainList = [mainListType, { actions: mainListContent }];
561
+ if (mainListType === "ace-knowledge-contact-method") {
562
+ mainList[1] = __spreadProps(__spreadValues({}, mainList[1]), {
563
+ contactMethods: items.map((item) => {
564
+ const formatted = contactMethodFromLegacy(item.data);
565
+ return formatted;
566
+ }),
567
+ entry: {
568
+ entryId: state.entryId
569
+ }
570
+ });
571
+ }
572
+ if (text && text.title) {
573
+ list.push([ConversationMessageListItemType.Header, text.title]);
574
+ }
575
+ if (text && text.body) {
576
+ list.push([ConversationMessageListItemType.HTML, { body: text.body }]);
577
+ }
578
+ list.push(mainList);
579
+ items.forEach((o) => {
580
+ switch (o.actionKey) {
581
+ case this.helpActionKey: {
582
+ list.push([ConversationMessageListItemType.Separator, void 0]);
583
+ list.push([
584
+ ConversationMessageListItemType.NoGoodAlternative,
585
+ { body: o.title }
586
+ ]);
587
+ break;
588
+ }
589
+ case this.nextActionKey: {
590
+ if (!!actions["next"] && actions["next"].take === items.filter((o2) => o2.actionKey !== "help").length + 2)
591
+ mainListContent[o.actionKey] = o.title;
592
+ break;
593
+ }
594
+ default: {
595
+ mainListContent[o.actionKey] = o.title;
596
+ }
597
+ }
598
+ });
599
+ return list;
600
+ }
601
+ getListType(state) {
602
+ if ((state == null ? void 0 : state.$type) === "Humany.Matching.Bots.Conversations.GuideListState, Humany.Matching") {
603
+ return ConversationMessageListItemType.LinkList;
604
+ } else if ((state == null ? void 0 : state.$type) === "Humany.Matching.Bots.Conversations.States.GuideFeedbackState, Humany.Matching") {
605
+ return ConversationMessageListItemType.FeedbackList;
606
+ } else if ((state == null ? void 0 : state.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching") {
607
+ return "ace-knowledge-contact-method";
608
+ } else
609
+ return ConversationMessageListItemType.ButtonList;
610
+ }
611
+ getTitleFromEntry(entry, action) {
612
+ if (!!entry) {
613
+ const entryType = ServerEntryTypeEnum[entry.type];
614
+ switch (entryType) {
615
+ case ServerEntryTypeEnum.List:
616
+ return this.getTitleFromList(entry, action);
617
+ case ServerEntryTypeEnum.Text:
618
+ return this.textTransform(entry);
619
+ }
620
+ }
621
+ }
622
+ getTitleFromList(entry, action) {
623
+ return entry.content.items.find((o) => o.actionKey == action).title;
624
+ }
625
+ }
626
+ class ChatClient {
627
+ constructor(container) {
628
+ this.entryInterface = new EntryInterface(container);
629
+ }
630
+ static initialize(user, agent) {
631
+ ChatHelper.setUser(user);
632
+ ChatHelper.setAgent(agent);
633
+ }
634
+ processMessageArrayAndStoreIt(messages) {
635
+ return __async(this, null, function* () {
636
+ for (const message of messages.filter((o) => o && o != null && !!o.message)) {
637
+ if (message.isUser)
638
+ this.postUserMessage(message);
639
+ else
640
+ this.postAgentMessage(message);
641
+ }
642
+ yield StoreHelper.insertOrUpdateMessageArrayStore(messages);
643
+ });
644
+ }
645
+ updateMessageArrayAndStoreIt(messages) {
646
+ let conversationEntry = void 0;
647
+ messages.forEach((o) => {
648
+ let message = StoreHelper.getMessage(o.id);
649
+ message == null ? void 0 : message.conversationEntry.update(o.message);
650
+ conversationEntry = message == null ? void 0 : message.conversationEntry;
651
+ });
652
+ StoreHelper.insertOrUpdateMessageArrayStore(messages, conversationEntry);
653
+ }
654
+ printUserMessageAndStoreIt(action) {
655
+ if (action == "next" || action == "help")
656
+ return;
657
+ let entry = StoreHelper.getEntry(action);
658
+ let message = {
659
+ id: v4(),
660
+ message: !entry ? action : this.getTitleFromEntry(entry, action),
661
+ isUser: true
662
+ };
663
+ this.postUserMessage(message);
664
+ StoreHelper.insertOrUpdateMessageArrayStore([message]);
665
+ }
666
+ postUserMessage(message) {
667
+ ChatHelper.getUser().print(message.message);
668
+ }
669
+ postAgentMessage(message) {
670
+ message.conversationEntry = ChatHelper.getAgent().print(message.message);
671
+ }
672
+ getTitleFromEntry(entry, action) {
673
+ const entryType = ServerEntryTypeEnum[entry.type];
674
+ switch (entryType) {
675
+ case ServerEntryTypeEnum.List:
676
+ return this.getTitleFromList(entry, action);
677
+ case ServerEntryTypeEnum.Text:
678
+ return this.getTitleFromText(entry, action);
679
+ default:
680
+ return "";
681
+ }
682
+ }
683
+ getTitleFromText(entry, action) {
684
+ if (!!entry.content.items)
685
+ return this.getTitleFromList(entry, action);
686
+ else
687
+ return entry.content.html;
688
+ }
689
+ getTitleFromList(entry, action) {
690
+ return entry.content.items.find((o) => o.actionKey == action).title;
691
+ }
692
+ }
693
+ const _HttpHelper = class {
694
+ static combineUrl(basePath, path) {
695
+ return basePath + path;
696
+ }
697
+ static getCredentials() {
698
+ return "same-origin";
699
+ }
700
+ };
701
+ let HttpHelper = _HttpHelper;
702
+ HttpHelper.fetchOptions = {
703
+ method: "POST",
704
+ headers: {
705
+ Accept: "application/json",
706
+ "Content-Type": "application/json"
707
+ }
708
+ };
709
+ HttpHelper.managedSubdomainSuffixes = ["stage", "alpha", "beta", "gamma"];
710
+ HttpHelper.parseSubdomain = (input) => {
711
+ const index = input.lastIndexOf("-");
712
+ const lastPart = input.substr(index + 1);
713
+ if (_HttpHelper.managedSubdomainSuffixes.indexOf(lastPart) > -1) {
714
+ return input.substr(0, index);
715
+ }
716
+ return input;
717
+ };
718
+ class PagingInterface {
719
+ process(entries) {
720
+ const pagingEntry = entries.find((e) => e.tags.indexOf("paging") > -1);
721
+ if (pagingEntry) {
722
+ const stream = StoreHelper.getEntryStore().filter((e) => e.tags.indexOf("paging") > -1 && e.id === pagingEntry.id);
723
+ if (stream.length > 0) {
724
+ const last = stream.reduce((acc, entry) => {
725
+ if (entry.actions.next && entry.actions.next.take >= acc.actions.next.take) {
726
+ return entry;
727
+ }
728
+ return acc;
729
+ }, stream[0]);
730
+ if (last.actions.next.take > pagingEntry.actions.next.take) {
731
+ entries.forEach((entry) => {
732
+ if (entry.id === pagingEntry.id) {
733
+ entry.actions.next = __spreadProps(__spreadValues({}, entry.actions.next), {
734
+ take: last.actions.next.take,
735
+ skip: last.actions.next.skip
736
+ });
737
+ }
738
+ });
739
+ }
740
+ }
741
+ entries.forEach((entry) => {
742
+ if (entry.id === pagingEntry.id && entry.content && entry.content.timestamp) {
743
+ entry.content = __spreadProps(__spreadValues({}, entry.content), {
744
+ items: entry.content && entry.content.items || []
745
+ });
746
+ }
747
+ });
748
+ }
749
+ return entries;
750
+ }
751
+ }
752
+ class ProcessorInterface {
753
+ process(entries) {
754
+ let processedEntries = entries;
755
+ processedEntries = this.deletePaging(processedEntries);
756
+ processedEntries = this.delete(processedEntries);
757
+ processedEntries = this.merge(processedEntries);
758
+ processedEntries = this.moveLoader(processedEntries);
759
+ return processedEntries;
760
+ }
761
+ processUpdate(entry, updateEntryArray) {
762
+ updateEntryArray.filter((t) => t.type == "List").forEach((o) => {
763
+ var _a;
764
+ (_a = o.content) == null ? void 0 : _a.items.forEach((t) => {
765
+ if (!(entry == null ? void 0 : entry.content.items.find((p) => p.actionKey == t.actionKey)))
766
+ entry == null ? void 0 : entry.content.items.push(t);
767
+ });
768
+ Object.keys(o.actions).forEach((t) => {
769
+ this.handleActionUpdateOrInsert(entry == null ? void 0 : entry.actions, t, o.actions);
770
+ });
771
+ });
772
+ var itemArray = entry == null ? void 0 : entry.content.items;
773
+ if (itemArray.find((o) => o.actionKey == "next")) {
774
+ itemArray.push(itemArray.splice(itemArray.indexOf(itemArray.find((o) => o.actionKey == "next")), 1)[0]);
775
+ }
776
+ if (itemArray.find((o) => o.actionKey == "help")) {
777
+ itemArray.push(itemArray.splice(itemArray.indexOf(itemArray.find((o) => o.actionKey == "help")), 1)[0]);
778
+ }
779
+ return entry ? [entry] : [];
780
+ }
781
+ handleActionUpdateOrInsert(currentArray, actionKey, action) {
782
+ if (!!currentArray)
783
+ currentArray[actionKey] = action[actionKey];
784
+ }
785
+ deletePaging(entries) {
786
+ return entries.filter((entry) => {
787
+ const isEmptyPaging = entry.type === "List" && entry.tags && entry.tags.indexOf("paging") > -1 && typeof entry.content === "undefined";
788
+ return !isEmptyPaging;
789
+ });
790
+ }
791
+ delete(entries) {
792
+ return entries.filter((entry) => entry.content !== null);
793
+ }
794
+ merge(entries) {
795
+ const output = [];
796
+ const merge = (source, target = {}) => {
797
+ const content = {};
798
+ if (source == null) {
799
+ return null;
800
+ }
801
+ if (target) {
802
+ Object.keys(target).forEach((key) => {
803
+ content[key] = target[key];
804
+ });
805
+ }
806
+ Object.keys(source).forEach((key) => {
807
+ if (key == "items") {
808
+ if (!content[key])
809
+ content[key] = [];
810
+ content[key] = content[key].concat(source[key]);
811
+ } else if (key == "html")
812
+ content[key] = content[key] + source[key];
813
+ else
814
+ content[key] = source[key];
815
+ });
816
+ return content;
817
+ };
818
+ const mergeLocalState = (source, target) => {
819
+ if (source == null)
820
+ return null;
821
+ if (Object.keys(source.actions).length === 0)
822
+ return target.localState;
823
+ if (Object.keys(target.actions).length === 0)
824
+ return source.localState;
825
+ return target.localState;
826
+ };
827
+ entries.forEach((entry) => {
828
+ if (entry.relation && !entry.tags.find((o) => o == "gentle" || o == "feedback")) {
829
+ let existingEntry = entries.find((o) => o.id == entry.relation);
830
+ if (existingEntry) {
831
+ const existingIndex = output.indexOf(existingEntry);
832
+ existingEntry.localState = mergeLocalState(entry, existingEntry);
833
+ existingEntry.content = merge(entry.content, existingEntry.content);
834
+ existingEntry.actions = merge(entry.actions, existingEntry.actions);
835
+ output[existingIndex] = existingEntry;
836
+ }
837
+ } else {
838
+ output.push(entry);
839
+ }
840
+ });
841
+ return output;
842
+ }
843
+ moveLoader(entries) {
844
+ const loaderEntry = entries.find((e) => e.id === "loader");
845
+ if (!loaderEntry) {
846
+ return entries;
847
+ }
848
+ const loaderIndex = entries.indexOf(loaderEntry);
849
+ if (loaderIndex > -1) {
850
+ entries.splice(entries.length - 1, 0, entries.splice(loaderIndex, 1)[0]);
851
+ }
852
+ return entries;
853
+ }
854
+ }
855
+ class MiddlewareClient {
856
+ constructor(container) {
857
+ this.paging = new PagingInterface();
858
+ this.processor = new ProcessorInterface();
859
+ this.entryInterface = new EntryInterface(container);
860
+ this.chatClient = new ChatClient(container);
861
+ }
862
+ static initialize(container, widget) {
863
+ MiddlewareHelper.setContainer(container);
864
+ MiddlewareHelper.setWidget(widget);
865
+ }
866
+ handleEntries(entries, action) {
867
+ return __async(this, null, function* () {
868
+ const widgetEntry = (entries || []).find((e) => e.type === "Widget");
869
+ if (widgetEntry) {
870
+ const agent = ChatHelper.getAgent();
871
+ if (agent) {
872
+ agent.update({
873
+ name: widgetEntry.content.agent.name,
874
+ avatar: widgetEntry.content.agent.avatar
875
+ });
876
+ const write = yield createStorageWriter(MiddlewareHelper.getContainer(), "bot-provider-conversation-meta", StorageCategory.Necessary);
877
+ write({
878
+ agent: {
879
+ name: widgetEntry.content.agent.name,
880
+ avatar: widgetEntry.content.agent.avatar
881
+ }
882
+ });
883
+ }
884
+ }
885
+ let o = this.executeMiddlewares(entries, false, action);
886
+ let messageArray = yield this.entryInterface.transformArray(o);
887
+ this.chatClient.processMessageArrayAndStoreIt(messageArray);
888
+ });
889
+ }
890
+ handleUpdate(entries, action) {
891
+ return __async(this, null, function* () {
892
+ let o = this.executeMiddlewares(entries, true, action);
893
+ let messageArray = yield this.entryInterface.transformArray(o);
894
+ this.chatClient.updateMessageArrayAndStoreIt(messageArray);
895
+ });
896
+ }
897
+ checkForUpdate(input) {
898
+ return input === "next";
899
+ }
900
+ executeMiddlewares(entries, isUpdate, action) {
901
+ if (isUpdate && action)
902
+ entries = this.processor.processUpdate(StoreHelper.getEntry(action), entries);
903
+ else {
904
+ entries = this.processor.process(entries);
905
+ }
906
+ entries = this.paging.process(entries);
907
+ return entries;
908
+ }
909
+ }
910
+ class HttpClient {
911
+ constructor(endpoint, clientId, container) {
912
+ this.options = {
913
+ credentials: HttpHelper.getCredentials(),
914
+ clientId
915
+ };
916
+ this.apiEndpoint = endpoint;
917
+ this.projectionPath = window.location.href;
918
+ this.endpointUrl = HttpHelper.combineUrl(this.projectionPath, "conversations");
919
+ this.clientId = this.options.clientId;
920
+ this.middlewareClient = new MiddlewareClient(container);
921
+ this.chatClient = new ChatClient(container);
922
+ this.container = container;
923
+ }
924
+ firstHit() {
925
+ return __async(this, null, function* () {
926
+ const entries = yield MiddlewareHelper.getEntrySessionStorage();
927
+ if (entries && entries.length > 0) {
928
+ yield StoreHelper.rehydrateState();
929
+ const meta = yield readStorage(this.container, "bot-provider-conversation-meta");
930
+ if (meta == null ? void 0 : meta.agent) {
931
+ const agent = ChatHelper.getAgent();
932
+ agent.update({
933
+ name: meta.agent.name,
934
+ avatar: meta.agent.avatar
935
+ });
936
+ }
937
+ yield this.chatClient.processMessageArrayAndStoreIt(entries);
938
+ } else {
939
+ let envelope = this.constructFirstHitEnvelope();
940
+ yield this.postAndHandleTheData(envelope);
941
+ }
942
+ });
943
+ }
944
+ handleUpdate(action) {
945
+ let envelope = this.constructInputEnvelope(action);
946
+ return this.postAndHandleTheUpdateData(envelope, action);
947
+ }
948
+ handleInput(action) {
949
+ let envelope = this.constructInputEnvelope(action);
950
+ return this.postAndHandleTheData(envelope, action);
951
+ }
952
+ handleCategories() {
953
+ let envelope = this.constructCategoriesEnvelope();
954
+ return this.postAndHandleTheData(envelope);
955
+ }
956
+ handlePhrase(phrase) {
957
+ const envelope = this.constructPhraseEnvelope(phrase);
958
+ const id = v4();
959
+ const entry = {
960
+ id,
961
+ type: "Text",
962
+ timestamp: new Date(),
963
+ content: {
964
+ $type: "Humany.Matching.Bots.Conversations.Content.Text, Humany.Matching",
965
+ html: `${phrase}`
966
+ },
967
+ actions: {},
968
+ tags: {}
969
+ };
970
+ const message = {
971
+ id,
972
+ message: ["html", { body: `<p>${phrase}</p>` }],
973
+ entry,
974
+ isUser: true,
975
+ conversationEntry: {}
976
+ };
977
+ StoreHelper.insertMessageToStore(message);
978
+ return this.postAndHandleTheData(envelope);
979
+ }
980
+ postAndHandleTheData(envelope, action) {
981
+ return fetch(this.apiEndpoint, this.getHttpOptions(envelope)).then((response) => {
982
+ if (response.ok)
983
+ return response.json();
984
+ throw response;
985
+ }).then((data) => {
986
+ this.handleResponseData(data, action);
987
+ }).catch((err) => console.log(err));
988
+ }
989
+ postAndHandleTheUpdateData(envelope, action) {
990
+ return fetch(this.apiEndpoint, this.getHttpOptions(envelope)).then((response) => {
991
+ if (response.ok)
992
+ return response.json();
993
+ throw response;
994
+ }).then((data) => {
995
+ this.handleUpdateResponseData(data, action);
996
+ }).catch((err) => console.log(err));
997
+ }
998
+ getHttpOptions(envelope) {
999
+ return Object.assign({}, HttpHelper.fetchOptions, {
1000
+ body: JSON.stringify(envelope),
1001
+ credentials: this.options.credentials
1002
+ });
1003
+ }
1004
+ constructInputEnvelope(action) {
1005
+ return {
1006
+ action: StoreHelper.getAction(action),
1007
+ localState: StoreHelper.getLocalState(action),
1008
+ globalState: StoreHelper.getGlobalState(),
1009
+ clientId: this.clientId
1010
+ };
1011
+ }
1012
+ constructCategoriesEnvelope() {
1013
+ return {
1014
+ action: {
1015
+ content: {
1016
+ $type: "Humany.Matching.Bots.Conversations.Actions.BrowseCategories, Humany.Matching"
1017
+ },
1018
+ key: "browseCategories"
1019
+ },
1020
+ globalState: StoreHelper.getGlobalState(),
1021
+ clientId: this.clientId
1022
+ };
1023
+ }
1024
+ constructPhraseEnvelope(phrase) {
1025
+ return {
1026
+ command: { phrase },
1027
+ globalState: StoreHelper.getGlobalState(),
1028
+ clientId: this.clientId
1029
+ };
1030
+ }
1031
+ constructFirstHitEnvelope() {
1032
+ return {
1033
+ command: { site: this.endpointUrl },
1034
+ clientId: this.clientId,
1035
+ globalState: {}
1036
+ };
1037
+ }
1038
+ handleResponseData(envelope, action) {
1039
+ StoreHelper.setGlobalState(envelope.globalState);
1040
+ return this.middlewareClient.handleEntries(envelope.entries, action);
1041
+ }
1042
+ handleUpdateResponseData(envelope, action) {
1043
+ return this.middlewareClient.handleUpdate(envelope.entries, action);
1044
+ }
1045
+ }
1046
+ const getKnowledgeBotEndpoint = (container, component) => {
1047
+ const { knowledgeBotEndpoint } = component.properties();
1048
+ if (knowledgeBotEndpoint) {
1049
+ return `${knowledgeBotEndpoint}/conversations`;
1050
+ }
1051
+ const { data } = container.get("$settings");
1052
+ if (!data || !data.projection)
1053
+ throw new Error("Missing Knowledge Bot endpoint");
1054
+ return `${data.projection}/conversations`;
1055
+ };
1056
+ const KnowledgeBotProviderPlugin = (container) => __async(this, null, function* () {
1057
+ const dataClient = yield DataClient.getInstance(container);
1058
+ const platform = yield ConversationPlatform.getInstance(container);
1059
+ const contactMethodPlatform = yield ContactMethodPlatform.getInstance(container);
1060
+ registerCustomMessageComponent(container, "ace-knowledge-contact-method", import("./contact-method.js"));
1061
+ platform.registerProvider("chat.bot", (conversation, component) => {
1062
+ const endpoint = getKnowledgeBotEndpoint(container, component);
1063
+ const clientId = v4();
1064
+ const agent = conversation.createAgent({
1065
+ name: "",
1066
+ avatar: void 0
1067
+ });
1068
+ ChatClient.initialize(conversation.user, agent);
1069
+ ContactMethodClient.initialize(container, dataClient, contactMethodPlatform, agent);
1070
+ MiddlewareClient.initialize(container, container.get("$widget"));
1071
+ const httpClient = new HttpClient(endpoint, clientId, container);
1072
+ const firstLoader = conversation.loading();
1073
+ httpClient.firstHit().then(firstLoader);
1074
+ component.actions.create("expand", (input) => {
1075
+ const cmClient = new ContactMethodClient(container);
1076
+ cmClient.expandContactMethod(input);
1077
+ });
1078
+ component.actions.create("evaluate", (input) => {
1079
+ const cmClient = new ContactMethodClient(container);
1080
+ cmClient.handleNewForm(input.form, input.form.id);
1081
+ });
1082
+ component.actions.create("validate", (input) => {
1083
+ const cmClient = new ContactMethodClient(container);
1084
+ cmClient.validate(input.form);
1085
+ });
1086
+ component.actions.create("submit", (input) => {
1087
+ var _a;
1088
+ if ((_a = input == null ? void 0 : input.form) == null ? void 0 : _a.schema.id) {
1089
+ getContactMethodEntry(input.form.schema.id);
1090
+ }
1091
+ const cmClient = new ContactMethodClient(container);
1092
+ cmClient.validate(input.form);
1093
+ });
1094
+ component.actions.watch("conversation.user-typing", (input, next) => {
1095
+ return next(input);
1096
+ });
1097
+ component.actions.watch("conversation.user-submit", (input, next) => {
1098
+ const httpClient2 = new HttpClient(endpoint, clientId, container);
1099
+ const loader = conversation.loading();
1100
+ httpClient2.handlePhrase(input.text).then(loader);
1101
+ return next(input);
1102
+ });
1103
+ component.actions.watch("conversation.form", (input, next) => {
1104
+ return next(input);
1105
+ });
1106
+ component.actions.watch("conversation.action", (input, next) => {
1107
+ const chatClient = new ChatClient(container);
1108
+ const cmClient = new ContactMethodClient(container);
1109
+ const loader = conversation.loading();
1110
+ if (input.split("/").length > 1) {
1111
+ cmClient.handleContactMethod(input).then(loader);
1112
+ return next(input);
1113
+ }
1114
+ const httpClient2 = new HttpClient(endpoint, clientId, container);
1115
+ if (input == "burger-clicked") {
1116
+ httpClient2.handleCategories().then(loader);
1117
+ return next(input);
1118
+ }
1119
+ const middlewareClient = new MiddlewareClient(container);
1120
+ if (middlewareClient.checkForUpdate(input)) {
1121
+ httpClient2.handleUpdate(input).then(loader);
1122
+ return next(input);
1123
+ }
1124
+ chatClient.printUserMessageAndStoreIt(input);
1125
+ httpClient2.handleInput(input).then(loader);
1126
+ return next(input);
1127
+ });
1128
+ });
1129
+ });
1130
+ export { KnowledgeBotProviderPlugin as ConversationPlugin };
1131
+ //# sourceMappingURL=index.js.map