@telia-ace/knowledge-widget-bot-provider 0.0.1-alpha.55 → 0.0.1-alpha.56

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/dist/index2.js DELETED
@@ -1,917 +0,0 @@
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 { ConversationMessageListItemType, ConversationPlatform, registerCustomMessageComponent } from "@telia-ace/widget-conversation";
41
- import { readStorage, createStorageWriter, StorageCategory } from "@telia-ace/widget-services";
42
- import { uuid } from "@telia-ace/widget-utilities";
43
- import { FormBuilder, submitButton } from "@telia-ace/widget-forms";
44
- var CustomMessageTypes = /* @__PURE__ */ ((CustomMessageTypes2) => {
45
- CustomMessageTypes2["ContactMethod"] = "ace-knowledge-bot-contact-method";
46
- CustomMessageTypes2["ContactList"] = "ace-knowledge-bot-contact-list";
47
- CustomMessageTypes2["FeedbackList"] = "ace-knowledge-bot-feedback-list";
48
- CustomMessageTypes2["NotificationList"] = "ace-knowledge-bot-notification-list";
49
- CustomMessageTypes2["NoGoodAlternative"] = "ace-knowledge-bot-no-good-alternative";
50
- return CustomMessageTypes2;
51
- })(CustomMessageTypes || {});
52
- const convertLegacySymbol = (symbol) => {
53
- if (symbol) {
54
- const { type, content } = symbol;
55
- if (type && content) {
56
- return { type, content };
57
- }
58
- }
59
- };
60
- const createGroup = ({
61
- isUser,
62
- items = [],
63
- meta = {}
64
- }) => {
65
- const groupId = uuid();
66
- const group = {
67
- id: groupId,
68
- isUser,
69
- items: items.map((i) => {
70
- return {
71
- id: i.id || uuid(),
72
- isUser,
73
- groupId,
74
- message: i.message || []
75
- };
76
- }),
77
- meta
78
- };
79
- return group;
80
- };
81
- const contactMethodFromLegacy = ({
82
- id,
83
- title,
84
- defaultIcon,
85
- exits: [exit],
86
- exitType,
87
- confirmationText,
88
- expandFormAutomatically,
89
- form,
90
- description
91
- }, options = {}) => {
92
- const { adapter } = exit || { Adapter: {} };
93
- const { clientName = "", settings: adapterSettings = {}, customClientName = "" } = adapter;
94
- const { validation } = options;
95
- const settings = {
96
- id: id.toString(),
97
- title,
98
- description,
99
- symbol: convertLegacySymbol(defaultIcon),
100
- clientName: clientName === "custom.adapter" ? customClientName : clientName,
101
- expanded: true,
102
- inline: true,
103
- body: {}
104
- };
105
- if (clientName.indexOf("phone") > -1 || clientName.indexOf("freetext") > -1) {
106
- const { displayTextBeforeClick, textBeforeClick } = adapterSettings;
107
- if (displayTextBeforeClick === "true") {
108
- settings.expanded = false;
109
- settings.title = textBeforeClick;
110
- }
111
- if (clientName.indexOf("phone.text") > -1) {
112
- settings.body.phoneNumber = adapterSettings.phoneNumber;
113
- } else {
114
- settings.body.freetext = adapterSettings.freetext;
115
- }
116
- }
117
- if (clientName.indexOf("chat.popup") > -1 || clientName.indexOf("link") > -1 || clientName === "custom.adapter" && exitType === "custom") {
118
- settings.body = adapterSettings;
119
- settings.body.confirmationFallback = confirmationText;
120
- }
121
- if (clientName.indexOf("ace") > -1) {
122
- settings.body = adapterSettings;
123
- }
124
- if (form && form.hasValueComponents) {
125
- if (validation) {
126
- settings.body.form = FormBuilder.fromLegacyForm(id.toString(), form, validation).get();
127
- } else {
128
- settings.body.form = FormBuilder.fromLegacyForm(id.toString(), form).get();
129
- }
130
- if (!expandFormAutomatically) {
131
- settings.expanded = settings.inline = false;
132
- }
133
- settings.body = updateForm(settings, { submit: "Submit" });
134
- }
135
- if (options.guideId) {
136
- settings.body.guideId = options.guideId;
137
- }
138
- return settings;
139
- };
140
- const getListType = (state) => {
141
- if ((state == null ? void 0 : state.$type) === "Humany.Matching.Bots.Conversations.GuideListState, Humany.Matching") {
142
- return ConversationMessageListItemType.LinkList;
143
- } else if ((state == null ? void 0 : state.$type) === "Humany.Matching.Bots.Conversations.States.GuideFeedbackState, Humany.Matching") {
144
- return CustomMessageTypes.FeedbackList;
145
- } else if ((state == null ? void 0 : state.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching") {
146
- return CustomMessageTypes.ContactList;
147
- } else
148
- return ConversationMessageListItemType.ButtonList;
149
- };
150
- const getKnowledgeBotEndpoint = (container, component) => {
151
- const { knowledgeBotEndpoint } = component.properties();
152
- if (knowledgeBotEndpoint) {
153
- return `${knowledgeBotEndpoint}/conversations`;
154
- }
155
- const { data } = container.get("$settings");
156
- if (!data || !data.projection)
157
- throw new Error("Missing Knowledge Bot endpoint");
158
- return `${data.projection}/conversations`;
159
- };
160
- const updateForm = (contactMethod, {
161
- submit,
162
- errors,
163
- meta
164
- }) => {
165
- var _a;
166
- if ((_a = contactMethod == null ? void 0 : contactMethod.body) == null ? void 0 : _a.form) {
167
- const { body } = contactMethod;
168
- const builder = new FormBuilder(body.form);
169
- if (!body.form.layout.submit) {
170
- builder.createComponent(submitButton(submit, "submit"));
171
- }
172
- if (errors) {
173
- builder.setValidationErrors(errors);
174
- }
175
- if (meta) {
176
- builder.setMeta(meta);
177
- }
178
- return __spreadProps(__spreadValues({}, body), {
179
- form: builder.get()
180
- });
181
- }
182
- return (contactMethod == null ? void 0 : contactMethod.body) || {};
183
- };
184
- const createGroupItem = ({
185
- messageId,
186
- message,
187
- group
188
- }) => {
189
- return {
190
- id: messageId,
191
- message,
192
- groupId: group.id,
193
- isUser: group.isUser
194
- };
195
- };
196
- const processEntries = (entries, prevGroup) => __async(this, null, function* () {
197
- const createListContent = (listType, entry, groupId, listContent = {
198
- actions: [],
199
- entryId: entry.id,
200
- groupId
201
- }) => {
202
- const contactMethodIds = [];
203
- listContent.actions = entry.content.items.map((i) => {
204
- var _a;
205
- const action = {
206
- label: i.title,
207
- actionKey: i.actionKey,
208
- symbol: !!i.symbol ? i.symbol : void 0
209
- };
210
- if (i.data) {
211
- const entryAction = entry.actions[i.actionKey];
212
- if (listType === CustomMessageTypes.ContactList) {
213
- contactMethodIds.push(i.data.id);
214
- action.data = contactMethodFromLegacy(i.data, {
215
- guideId: (_a = entryAction == null ? void 0 : entryAction.guideId) == null ? void 0 : _a.toString()
216
- });
217
- } else {
218
- action.data = i.data;
219
- }
220
- }
221
- return action;
222
- });
223
- if (listType === CustomMessageTypes.ContactList) {
224
- listContent.contactMethodIds = contactMethodIds;
225
- }
226
- return listContent;
227
- };
228
- const transform = (_0, _1, ..._2) => __async(this, [_0, _1, ..._2], function* (entry, group, related = [], prevGroup2) {
229
- const messageId = uuid();
230
- switch (entry.type) {
231
- case "List": {
232
- const { header, items, html } = entry.content;
233
- const result2 = [];
234
- let listContent = {
235
- actions: [],
236
- entryId: entry.id,
237
- groupId: group.id
238
- };
239
- if (header) {
240
- listContent.header = header;
241
- }
242
- if ((items == null ? void 0 : items.length) > 0) {
243
- const listType = getListType(entry.localState);
244
- listContent = createListContent(listType, entry, group.id, listContent);
245
- if (!!related.length) {
246
- const pagingEntry = related.find((e) => e.tags.includes("paging"));
247
- const fallbackEntry = related.find((e) => e.tags.includes("fallback"));
248
- if (pagingEntry == null ? void 0 : pagingEntry.content) {
249
- group.meta.paging = {
250
- actions: pagingEntry.actions,
251
- items: pagingEntry.content.items,
252
- localState: pagingEntry.localState
253
- };
254
- pagingEntry.content.items.forEach((i) => {
255
- listContent.actions.push({
256
- label: i.title,
257
- actionKey: i.actionKey,
258
- data: {
259
- groupId: !!prevGroup2 ? prevGroup2.id : group.id
260
- }
261
- });
262
- });
263
- }
264
- if (fallbackEntry) {
265
- const fallbackAction = fallbackEntry.content.items.find((i) => i.actionKey === "help");
266
- if (fallbackAction) {
267
- result2.push(createGroupItem({
268
- messageId,
269
- message: [
270
- ConversationMessageListItemType.Separator,
271
- void 0
272
- ],
273
- group
274
- }));
275
- result2.push(createGroupItem({
276
- messageId,
277
- message: [
278
- CustomMessageTypes.NoGoodAlternative,
279
- { body: fallbackAction.title }
280
- ],
281
- group
282
- }));
283
- }
284
- }
285
- }
286
- result2.unshift(createGroupItem({
287
- messageId,
288
- message: [listType, listContent],
289
- group
290
- }));
291
- }
292
- if (html) {
293
- result2.unshift(createGroupItem({
294
- messageId,
295
- message: [ConversationMessageListItemType.HTML, { body: html }],
296
- group
297
- }));
298
- }
299
- return result2;
300
- }
301
- case "Text": {
302
- const result2 = [];
303
- result2.push(createGroupItem({
304
- messageId,
305
- message: [
306
- ConversationMessageListItemType.HTML,
307
- { body: entry.content.html }
308
- ],
309
- group
310
- }));
311
- if (!!related.length) {
312
- const contactMethodListEntry = related.find((e) => {
313
- var _a;
314
- return e.type === "List" && ((_a = e.localState) == null ? void 0 : _a.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching";
315
- });
316
- const dialogListEntry = related.find((e) => e.tags.includes("dialog"));
317
- const feedbackEntry = related.find((e) => e.tags.includes("feedback"));
318
- if (dialogListEntry) {
319
- result2.push(createGroupItem({
320
- messageId,
321
- message: [
322
- ConversationMessageListItemType.ButtonList,
323
- {
324
- actions: dialogListEntry.content.items.map((i) => {
325
- return {
326
- label: i.title,
327
- actionKey: i.actionKey
328
- };
329
- })
330
- }
331
- ],
332
- group
333
- }));
334
- }
335
- if (contactMethodListEntry) {
336
- const listContent = createListContent(CustomMessageTypes.ContactList, contactMethodListEntry, group.id);
337
- result2.push(createGroupItem({
338
- messageId,
339
- message: [ConversationMessageListItemType.Separator, void 0],
340
- group
341
- }));
342
- result2.push(createGroupItem({
343
- messageId,
344
- message: [CustomMessageTypes.ContactList, listContent],
345
- group
346
- }));
347
- }
348
- if (feedbackEntry) {
349
- if (feedbackEntry.content.html) {
350
- result2.push(createGroupItem({
351
- messageId,
352
- group,
353
- message: [
354
- ConversationMessageListItemType.HTML,
355
- {
356
- body: feedbackEntry.content.html
357
- }
358
- ]
359
- }));
360
- }
361
- result2.push(createGroupItem({
362
- messageId,
363
- group,
364
- message: [
365
- CustomMessageTypes.FeedbackList,
366
- {
367
- actions: feedbackEntry.content.items.map((i) => {
368
- return {
369
- actionKey: i.actionKey,
370
- label: i.title
371
- };
372
- })
373
- }
374
- ]
375
- }));
376
- }
377
- }
378
- return result2;
379
- }
380
- case "Contact": {
381
- return [
382
- createGroupItem({
383
- messageId,
384
- message: [
385
- CustomMessageTypes.ContactMethod,
386
- {
387
- contactMethodId: entry.content.id,
388
- groupId: group.id,
389
- guideId: entry.content.guideId
390
- }
391
- ],
392
- group
393
- })
394
- ];
395
- }
396
- case "CategorizedNoticeList": {
397
- const result2 = [
398
- createGroupItem({
399
- messageId,
400
- message: [
401
- CustomMessageTypes.NotificationList,
402
- {
403
- id: entry.content.item.id.toString(),
404
- header: entry.content.item.title,
405
- items: entry.content.item.listNotices,
406
- symbol: entry.content.item.defaultIcon
407
- }
408
- ],
409
- group
410
- })
411
- ];
412
- (entry.content.notices || []).forEach((notice) => {
413
- result2.push(createGroupItem({
414
- messageId,
415
- message: [
416
- ConversationMessageListItemType.HTML,
417
- {
418
- body: notice.body
419
- }
420
- ],
421
- group
422
- }));
423
- });
424
- return result2;
425
- }
426
- case "footerNotice": {
427
- return [
428
- createGroupItem({
429
- messageId,
430
- message: [
431
- ConversationMessageListItemType.HTML,
432
- {
433
- body: entry.content.item.body
434
- }
435
- ],
436
- group
437
- })
438
- ];
439
- }
440
- case "Error": {
441
- return [
442
- createGroupItem({
443
- messageId,
444
- message: [
445
- ConversationMessageListItemType.HTML,
446
- {
447
- body: entry.content.message || "Something went wrong."
448
- }
449
- ],
450
- group
451
- })
452
- ];
453
- }
454
- default:
455
- return [];
456
- }
457
- });
458
- const splitEntry = (entry) => {
459
- if (["CategorizedNoticeList", "NoticeList"].includes(entry.type)) {
460
- return (entry.content.items || []).reduce((acc, item) => {
461
- if (!entry.content.isInitialNotices) {
462
- return acc;
463
- }
464
- acc.push(__spreadProps(__spreadValues({}, entry), {
465
- id: uuid(),
466
- type: item.type || entry.type,
467
- content: {
468
- $type: entry.content.$type,
469
- isInitialNotices: entry.content.isInitialNotices,
470
- notices: item.notices,
471
- item
472
- }
473
- }));
474
- return acc;
475
- }, []);
476
- }
477
- return entry;
478
- };
479
- const sortMap = /* @__PURE__ */ new Map([
480
- ["Text", 1],
481
- ["List", 2],
482
- ["Contact", 3],
483
- ["CategorizedNoticeList", 10]
484
- ]);
485
- const result = [];
486
- const filteredEntries = entries.sort((item, other) => {
487
- const defaultSortValue = 20;
488
- var sortValue = sortMap.get(item.type) || defaultSortValue;
489
- var sortValueOther = sortMap.get(other.type) || defaultSortValue;
490
- return sortValue - sortValueOther;
491
- }).filter((entry) => !entry.relation).reduce((acc, entry) => {
492
- return acc.concat(splitEntry(entry));
493
- }, []);
494
- for (const entry of filteredEntries) {
495
- const relatedEntries = entries.filter((other) => other.relation === entry.id);
496
- const group = createGroup({ isUser: false });
497
- const items = yield transform(entry, group, relatedEntries, prevGroup);
498
- items.forEach((item) => {
499
- group.items.push(item);
500
- });
501
- if (group.items.length) {
502
- result.push(group);
503
- }
504
- }
505
- return result;
506
- });
507
- class HttpClient {
508
- constructor(endpoint, events, store, container) {
509
- this.endpoint = endpoint;
510
- this.events = events;
511
- this.store = store;
512
- this.container = container;
513
- this.clientId = uuid();
514
- this.entries = [];
515
- }
516
- start() {
517
- return __async(this, null, function* () {
518
- const storedMessages = yield this.store.read();
519
- if (storedMessages && storedMessages.length > 0) {
520
- const storedGlobalState = this.store.readGlobalState();
521
- this.globalState = storedGlobalState;
522
- return storedMessages;
523
- } else {
524
- const envelope = this.createInitialEnvelope();
525
- const initialResponse = yield this.postEnvelope(envelope);
526
- return initialResponse;
527
- }
528
- });
529
- }
530
- sendMessage(message) {
531
- return __async(this, null, function* () {
532
- const envelope = {
533
- command: { phrase: message },
534
- globalState: this.globalState,
535
- clientId: this.clientId
536
- };
537
- const result = yield this.postEnvelope(envelope);
538
- return result;
539
- });
540
- }
541
- helpAction() {
542
- return __async(this, null, function* () {
543
- return this.postEnvelope({
544
- action: {
545
- key: "help",
546
- content: {
547
- $type: "Humany.Matching.Bots.Conversations.Actions.Help, Humany.Matching"
548
- }
549
- },
550
- globalState: this.globalState,
551
- clientId: this.clientId
552
- });
553
- });
554
- }
555
- nextAction(group) {
556
- return __async(this, null, function* () {
557
- var _a;
558
- const { paging } = group.meta;
559
- if (!((_a = paging.actions) == null ? void 0 : _a.next)) {
560
- return;
561
- }
562
- return this.postEnvelope({
563
- action: {
564
- key: "next",
565
- content: paging.actions.next
566
- },
567
- localState: paging.localState,
568
- globalState: this.globalState,
569
- clientId: this.clientId
570
- }, group).then((response) => {
571
- const listItemIndex = group.items.findIndex((item) => item.message[0] === "link-list");
572
- if (listItemIndex === -1 || !response) {
573
- return group;
574
- }
575
- return __spreadProps(__spreadValues({}, group), {
576
- meta: __spreadValues(__spreadValues({}, group.meta), response[0].meta),
577
- items: [
578
- ...group.items.slice(0, listItemIndex),
579
- __spreadProps(__spreadValues({}, group.items[listItemIndex]), {
580
- message: [
581
- response[0].items[0].message[0],
582
- __spreadValues(__spreadValues({}, group.items[listItemIndex].message[1]), response[0].items[0].message[1])
583
- ]
584
- }),
585
- ...group.items.slice(listItemIndex + 1)
586
- ]
587
- });
588
- });
589
- });
590
- }
591
- showContactMethodAction(data) {
592
- return __async(this, null, function* () {
593
- const entry = this.entries.find((e) => e.id === data.entryId);
594
- const actionItem = entry == null ? void 0 : entry.actions[data.actionKey];
595
- if (!actionItem) {
596
- return;
597
- }
598
- return this.postEnvelope({
599
- action: {
600
- key: data.actionKey,
601
- content: actionItem
602
- },
603
- localState: entry.localState,
604
- globalState: this.globalState,
605
- clientId: this.clientId
606
- });
607
- });
608
- }
609
- defaultAction(action) {
610
- return __async(this, null, function* () {
611
- const entry = this.getEntryOfAction(action);
612
- if (!entry) {
613
- return;
614
- }
615
- return this.postEnvelope({
616
- action: {
617
- key: action,
618
- content: entry.actions[action]
619
- },
620
- localState: entry.localState,
621
- globalState: this.globalState,
622
- clientId: this.clientId
623
- });
624
- });
625
- }
626
- createInitialEnvelope() {
627
- return {
628
- command: { site: this.endpoint },
629
- clientId: this.clientId,
630
- globalState: {},
631
- funnel: this.container.get("$widget").name
632
- };
633
- }
634
- postEnvelope(envelope, prevGroup) {
635
- return __async(this, null, function* () {
636
- try {
637
- const response = yield fetch(this.endpoint, this.getHttpOptions(envelope));
638
- if (!response.ok) {
639
- throw response;
640
- }
641
- const data = yield response.json();
642
- return this.handleResponse(data, prevGroup);
643
- } catch (error) {
644
- let message = "Something went wrong";
645
- if (error.message === "Failed to fetch") {
646
- message = "Lost connection... Please try again.";
647
- }
648
- return this.handleResponse({
649
- entries: [
650
- {
651
- id: uuid(),
652
- type: "Error",
653
- timestamp: new Date().toISOString(),
654
- content: {
655
- message
656
- },
657
- actions: {},
658
- tags: []
659
- }
660
- ]
661
- });
662
- }
663
- });
664
- }
665
- handleResponse(data, prevGroup) {
666
- return __async(this, null, function* () {
667
- this.entries = this.entries.concat(data.entries);
668
- const widgetEntry = data.entries.find((e) => e.type === "Widget");
669
- if (widgetEntry) {
670
- this.events.dispatch("provider:widget-entry", widgetEntry.content);
671
- }
672
- if (data.globalState) {
673
- this.globalState = data.globalState;
674
- }
675
- const groups = yield processEntries(data.entries, prevGroup);
676
- return groups;
677
- });
678
- }
679
- getHttpOptions(envelope) {
680
- const fetchOptions = {
681
- method: "POST",
682
- headers: {
683
- Accept: "application/json",
684
- "Content-Type": "application/json"
685
- }
686
- };
687
- envelope.funnel = this.container.get("$widget").name;
688
- return Object.assign({}, fetchOptions, {
689
- body: JSON.stringify(envelope),
690
- credentials: "same-origin"
691
- });
692
- }
693
- getEntryOfAction(action) {
694
- return this.entries.find((entry) => !!entry.actions[action]);
695
- }
696
- }
697
- class Store {
698
- constructor(container) {
699
- this.container = container;
700
- this.enabled = false;
701
- this.sessionMessageStorageVariable = "knowledge-bot-messages";
702
- this.botProviderGlobalStateKey = "bot-provider-global-state";
703
- this.botProviderMetaStateKey = "bot-provider-conversation-meta";
704
- }
705
- readGlobalState() {
706
- return __async(this, null, function* () {
707
- if (!this.enabled) {
708
- return void 0;
709
- }
710
- const state = yield readStorage(this.container, this.botProviderGlobalStateKey);
711
- return state;
712
- });
713
- }
714
- readMetaState() {
715
- return __async(this, null, function* () {
716
- if (!this.enabled) {
717
- return;
718
- }
719
- return readStorage(this.container, this.botProviderMetaStateKey);
720
- });
721
- }
722
- write(groups) {
723
- return __async(this, null, function* () {
724
- if (!this.enabled) {
725
- return;
726
- }
727
- return createStorageWriter(this.container, this.sessionMessageStorageVariable, StorageCategory.Necessary).then((write) => {
728
- return write(groups);
729
- });
730
- });
731
- }
732
- read() {
733
- return __async(this, null, function* () {
734
- if (!this.enabled) {
735
- return [];
736
- }
737
- return readStorage(this.container, this.sessionMessageStorageVariable).then((groups) => {
738
- return groups || [];
739
- });
740
- });
741
- }
742
- }
743
- class KnowledgeBotProvider {
744
- constructor(container, conversation, component) {
745
- this.container = container;
746
- this.conversation = conversation;
747
- this.groups = [];
748
- const { events } = this.container.get("$widget");
749
- this.events = events;
750
- this.store = new Store(container);
751
- this.agent = conversation.createAgent({
752
- name: "",
753
- avatar: void 0
754
- });
755
- this.user = conversation.user;
756
- const endpoint = getKnowledgeBotEndpoint(container, component);
757
- this.httpClient = new HttpClient(endpoint, events, this.store, this.container);
758
- this.events.subscribe("provider:widget-entry", (_event, data) => {
759
- this.onWidgetEntryReceived(data);
760
- });
761
- this.events.subscribe("provider:open-contact-method", (_event, data) => __async(this, null, function* () {
762
- const response = yield this.httpClient.showContactMethodAction(data);
763
- if (response) {
764
- if (!!data.contactMethod.title)
765
- this.printUserMessage(data.contactMethod.title);
766
- this.onMessagesReceived(response);
767
- }
768
- }));
769
- }
770
- initialize() {
771
- return __async(this, null, function* () {
772
- const loader = this.conversation.loading();
773
- const storedMeta = yield this.store.readMetaState();
774
- if (storedMeta == null ? void 0 : storedMeta.agent) {
775
- this.agent.update({
776
- name: storedMeta.agent.name,
777
- avatar: storedMeta.agent.avatar
778
- });
779
- }
780
- const response = yield this.httpClient.start();
781
- if (response) {
782
- this.onMessagesReceived(response);
783
- }
784
- loader();
785
- });
786
- }
787
- onMessagesReceived(groups) {
788
- return __async(this, null, function* () {
789
- this.groups = this.groups.concat(groups);
790
- yield this.store.write(this.groups);
791
- groups.forEach((group) => {
792
- const items = group.items.map((item) => item.message);
793
- if (group.isUser) {
794
- group.writer = this.user.print(items);
795
- } else {
796
- group.writer = this.agent.print(items);
797
- }
798
- });
799
- });
800
- }
801
- onInput(action) {
802
- return __async(this, null, function* () {
803
- const actionKey = typeof action === "string" ? action : action.actionKey;
804
- switch (actionKey) {
805
- case "next":
806
- {
807
- if (typeof action === "string") {
808
- return;
809
- }
810
- const group = this.groups.find((other) => {
811
- var _a;
812
- return other.id === ((_a = action.data) == null ? void 0 : _a.groupId);
813
- });
814
- if (group) {
815
- const response = yield this.httpClient.nextAction(group);
816
- if (response) {
817
- const combined = response.items.map((item) => item.message);
818
- group.meta.paging = response == null ? void 0 : response.meta.paging;
819
- group.writer.update(combined);
820
- }
821
- }
822
- }
823
- break;
824
- case "help":
825
- {
826
- const loader = this.conversation.loading();
827
- const response = yield this.httpClient.helpAction();
828
- loader();
829
- if (response) {
830
- this.onMessagesReceived(response);
831
- }
832
- }
833
- break;
834
- default:
835
- {
836
- if (typeof action !== "string" && action.label) {
837
- this.printUserMessage(action.label);
838
- }
839
- const loader = this.conversation.loading();
840
- const response = yield this.httpClient.defaultAction(actionKey);
841
- if (response) {
842
- this.onMessagesReceived(response);
843
- }
844
- loader();
845
- }
846
- break;
847
- }
848
- });
849
- }
850
- printUserMessage(message) {
851
- const group = createGroup({
852
- isUser: true,
853
- items: [
854
- {
855
- id: uuid(),
856
- isUser: true,
857
- message: [ConversationMessageListItemType.HTML, { body: message }]
858
- }
859
- ]
860
- });
861
- this.onMessagesReceived([group]);
862
- return group;
863
- }
864
- onMessageSend(message) {
865
- return __async(this, null, function* () {
866
- const loader = this.conversation.loading();
867
- const response = yield this.httpClient.sendMessage(message);
868
- this.printUserMessage(message);
869
- if (response) {
870
- this.onMessagesReceived(response);
871
- }
872
- loader();
873
- });
874
- }
875
- onWidgetEntryReceived(data) {
876
- return __async(this, null, function* () {
877
- const agentSettings = {
878
- name: data.agent.name,
879
- avatar: data.agent.avatar
880
- };
881
- this.agent.update(agentSettings);
882
- const write = yield createStorageWriter(this.container, "bot-provider-conversation-meta", StorageCategory.Necessary);
883
- write({
884
- agent: agentSettings
885
- });
886
- });
887
- }
888
- }
889
- const KnowledgeBotProviderPlugin = (container) => __async(this, null, function* () {
890
- const platform = yield ConversationPlatform.getInstance(container);
891
- registerCustomMessageComponent(container, CustomMessageTypes.ContactList, import("./contact-method-list.js"));
892
- registerCustomMessageComponent(container, CustomMessageTypes.ContactMethod, import("./contact-method.js"));
893
- registerCustomMessageComponent(container, CustomMessageTypes.NotificationList, import("./notification-list.js"));
894
- registerCustomMessageComponent(container, CustomMessageTypes.NoGoodAlternative, import("./no-good-alternative.js"));
895
- registerCustomMessageComponent(container, CustomMessageTypes.FeedbackList, import("./feedback-list.js"));
896
- platform.registerProvider("ace-knowledge-bot", (conversation, component) => {
897
- const provider = new KnowledgeBotProvider(container, conversation, component);
898
- provider.initialize();
899
- component.actions.watch("conversation.user-submit", (input, next) => {
900
- provider.onMessageSend(input.text);
901
- });
902
- component.actions.watch("conversation.action", (input, next) => {
903
- provider.onInput(input);
904
- return next(input);
905
- });
906
- component.actions.create("evaluate", (input) => {
907
- const { events } = container.get("$widget");
908
- events.dispatch("bot-provider:evaluate", input);
909
- });
910
- component.actions.create("submit", (input) => {
911
- const { events } = container.get("$widget");
912
- events.dispatch("bot-provider:form-submitted", input);
913
- });
914
- });
915
- });
916
- export { KnowledgeBotProviderPlugin as K, updateForm as u };
917
- //# sourceMappingURL=index2.js.map