@telia-ace/widget-conversation 1.1.2-rc.13 → 1.1.2-rc.15
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.
|
@@ -510,12 +510,14 @@
|
|
|
510
510
|
margin: 0;
|
|
511
511
|
}
|
|
512
512
|
`,Pe=y.forwardRef(({className:t,timestamp:e,sender:o,type:r,content:a},l)=>{const d=y.useCallback(c=>{let h="first";return a.length>1&&(h=c===0?"first":c+1===a.length?"last":"middle"),h},[a]),{userLabel:m=""}=n.useProperties(),u=()=>s.jsx(s.Fragment,{children:a.map(c=>{const{key:h,body:x,title:$,form:z,actions:p}=c,w=!!x&&!$&&!z&&!p;return s.jsx(ge,{className:t,discrete:w,ref:l,timestamp:e,fullWidth:!!z||!!p,children:w&&x?s.jsx(n.Html,{html:x}):s.jsx(I,{isSystem:!0,content:c})},h)})}),f=()=>s.jsxs(s.Fragment,{children:[s.jsx(K,{className:"humany-sr-only",children:s.jsx(k,{name:o==null?void 0:o.name,timestamp:e})}),s.jsx(F,{sender:o,timestamp:e,className:t,children:s.jsx(H,{className:"humany-message-content-bundle",children:a.map((c,h)=>s.jsx(A,{className:"humany-message-container",left:!0,ref:l,children:s.jsx(I,{content:c,stacked:d(h)})},c.key))})})]}),j=()=>s.jsxs(s.Fragment,{children:[s.jsx(K,{className:"humany-sr-only",children:s.jsx(k,{name:m,timestamp:e})}),s.jsx(be,{timestamp:e,className:t,children:s.jsx(H,{className:"humany-message-content-bundle",children:a.map((c,h)=>s.jsx(A,{className:"humany-message-container",right:!0,ref:l,children:s.jsx(I,{isUser:!0,content:c,stacked:d(h)})},c.key))})})]});switch(r){case v.ConversationMessageType.System:return u();case v.ConversationMessageType.Agent:return f();case v.ConversationMessageType.User:return j();default:return null}}),I=({content:t,isUser:e,isSystem:o,stacked:r})=>{const a=n.useDispatch(),{title:l,body:d,actions:m,form:u,items:f}=t;return f?s.jsx(O,{className:"humany-message-content",user:!!e,stacked:r,children:s.jsx(Ke,{items:f,dispatch:a})}):s.jsxs(O,{className:"humany-message-content",padded:!o,user:!!e,stacked:r,fullWidth:!!u||!!m,children:[l&&s.jsx(N,{children:s.jsx(oe,{variant:"h2",children:l})}),d&&s.jsx(N,{children:s.jsx(ce,{html:d})}),u&&s.jsx(N,{children:s.jsx(le,{...u,onChange:(c,h)=>{a("form",{data:h,formKey:u.schema.id,actionKey:c})}})}),m&&s.jsx(N,{children:s.jsx(de,{items:Object.keys(m).map(c=>({label:m[c],actionKey:c})),renderItem:c=>s.jsx("li",{children:s.jsx(me,{onClick:()=>{a("action",c.actionKey)},children:c.label})},c.actionKey)})})]})},K=i.div`
|
|
513
|
-
position: absolute;
|
|
514
|
-
left: -10000px;
|
|
515
|
-
top: auto;
|
|
516
|
-
width: 1px;
|
|
513
|
+
position: absolute !important;
|
|
517
514
|
height: 1px;
|
|
515
|
+
width: 1px;
|
|
518
516
|
overflow: hidden;
|
|
517
|
+
clip: rect(1px 1px 1px 1px);
|
|
518
|
+
clip: rect(1px, 1px, 1px, 1px);
|
|
519
|
+
left: -10000000px;
|
|
520
|
+
top: auto;
|
|
519
521
|
`,Fe=()=>{const{skipToLatestMessageLabel:t="Skip to latest message"}=n.useProperties(),e=n.useContainer(),o=()=>{const r=e.get("widgetDOMElement");if(r){const a=r.querySelectorAll(".humany-conversation-entry");a.length&&a[a.length-1].focus()}};return s.jsx(We,{className:"humany-skip-to-latest-message",onKeyDown:r=>{(r.key==="Enter"||r.keyCode===13)&&o()},onClick:o,children:t})},We=i.button`
|
|
520
522
|
position: absolute;
|
|
521
523
|
bottom: 60px;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as n, jsxs as u, Fragment as L } from "react/jsx-runtime";
|
|
2
2
|
import a from "@emotion/styled";
|
|
3
|
-
import { Input as ne, borderTabStyle as
|
|
3
|
+
import { Input as ne, borderTabStyle as p, Button as M, SymbolBadge as x, useIsTabbing as re, useDispatch as _, useProperties as C, useScroll as oe, Typography as F, invertedHoverButton as be, Html as Y, List as se, buttonCss as R, ItemList as B, itemList as K, Link as $e, useComponent as ve, genericHtml as xe, headingElement as X, usePrevious as ke, list as we, useContainer as ae, WidgetOverlayOutlet as ie, ConversationContext as Ce } from "@telia-ace/widget-ui";
|
|
4
4
|
import { appendClassNames as H, htmlParentContains as Se } from "@telia-ace/widget-utilities";
|
|
5
5
|
import { css as b, keyframes as ze } from "@emotion/react";
|
|
6
6
|
import Le, { useState as D, useEffect as G, forwardRef as J, useContext as Ne, Suspense as Me, useRef as le, useMemo as Ie, useCallback as ce } from "react";
|
|
@@ -17,18 +17,18 @@ const Te = ({ className: t }) => {
|
|
|
17
17
|
sendButtonLabel: h = "Send message",
|
|
18
18
|
inputMultiline: $ = !0,
|
|
19
19
|
secondaryAction: v
|
|
20
|
-
} = C(), [T,
|
|
20
|
+
} = C(), [T, y] = oe(!0);
|
|
21
21
|
G(() => {
|
|
22
|
-
if (
|
|
23
|
-
const { lineHeight: g, maxHeight: A } = getComputedStyle(
|
|
22
|
+
if (y.current) {
|
|
23
|
+
const { lineHeight: g, maxHeight: A } = getComputedStyle(y.current);
|
|
24
24
|
if (g && g.indexOf("px") > -1) {
|
|
25
|
-
const I = g.split("px"),
|
|
26
|
-
s(
|
|
27
|
-
const
|
|
28
|
-
c(parseInt(
|
|
25
|
+
const I = g.split("px"), pe = parseInt(I[0], 10);
|
|
26
|
+
s(pe);
|
|
27
|
+
const ye = A.split("px");
|
|
28
|
+
c(parseInt(ye[0], 10));
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
}, [
|
|
31
|
+
}, [y.current]);
|
|
32
32
|
const z = (g) => {
|
|
33
33
|
!g.shiftKey || !$ ? (g.preventDefault(), e && e.length > 0 && (m("user-submit", { text: e }), o(""), E(""))) : e.length < 1 && g.preventDefault();
|
|
34
34
|
}, ge = (g) => {
|
|
@@ -37,12 +37,12 @@ const Te = ({ className: t }) => {
|
|
|
37
37
|
...v
|
|
38
38
|
}));
|
|
39
39
|
}, fe = (g) => {
|
|
40
|
-
if ($ &&
|
|
41
|
-
const A =
|
|
40
|
+
if ($ && y.current) {
|
|
41
|
+
const A = y.current.scrollHeight;
|
|
42
42
|
if (A > r) {
|
|
43
|
-
|
|
43
|
+
y.current.style.height = "auto";
|
|
44
44
|
let I = A;
|
|
45
|
-
g.length < 1 && (I = r),
|
|
45
|
+
g.length < 1 && (I = r), y.current.style.height = `${I}px`, I >= i ? y.current.style.overflowY = "scroll" : y.current.style.overflowY = "hidden";
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
}, E = (g) => {
|
|
@@ -83,7 +83,7 @@ const Te = ({ className: t }) => {
|
|
|
83
83
|
placeholder: f,
|
|
84
84
|
type: "textarea",
|
|
85
85
|
rows: 1,
|
|
86
|
-
ref:
|
|
86
|
+
ref: y
|
|
87
87
|
}
|
|
88
88
|
) : /* @__PURE__ */ n(
|
|
89
89
|
Oe,
|
|
@@ -137,7 +137,7 @@ const Te = ({ className: t }) => {
|
|
|
137
137
|
transition: height 200ms linear;
|
|
138
138
|
outline: none;
|
|
139
139
|
&:focus-visible {
|
|
140
|
-
${
|
|
140
|
+
${p};
|
|
141
141
|
outline-offset: 12px;
|
|
142
142
|
}
|
|
143
143
|
`, Oe = a(ne)`
|
|
@@ -176,7 +176,7 @@ const Te = ({ className: t }) => {
|
|
|
176
176
|
border: none;
|
|
177
177
|
color: #a6a6a6;
|
|
178
178
|
&:focus-visible {
|
|
179
|
-
${
|
|
179
|
+
${p};
|
|
180
180
|
}
|
|
181
181
|
`, Re = a(de)`
|
|
182
182
|
border-right: ${(t) => t.theme.border};
|
|
@@ -199,7 +199,7 @@ const Te = ({ className: t }) => {
|
|
|
199
199
|
return (e = t.theme.colors) == null ? void 0 : e.accent;
|
|
200
200
|
}};
|
|
201
201
|
border-top: ${(t) => t.theme.inputBorder};
|
|
202
|
-
${(t) => t["data-has-focus"] && t.isTabbing &&
|
|
202
|
+
${(t) => t["data-has-focus"] && t.isTabbing && p}
|
|
203
203
|
textarea {
|
|
204
204
|
${(t) => !!t.scrollableCss && t.scrollableCss}
|
|
205
205
|
}
|
|
@@ -425,7 +425,7 @@ const Te = ({ className: t }) => {
|
|
|
425
425
|
&:focus {
|
|
426
426
|
${(t) => {
|
|
427
427
|
var e;
|
|
428
|
-
return ((e = t.theme.accessibility) == null ? void 0 : e.isTabbing) &&
|
|
428
|
+
return ((e = t.theme.accessibility) == null ? void 0 : e.isTabbing) && p;
|
|
429
429
|
}};
|
|
430
430
|
}
|
|
431
431
|
}
|
|
@@ -452,7 +452,7 @@ const Te = ({ className: t }) => {
|
|
|
452
452
|
width: 100%;
|
|
453
453
|
color: #fff;
|
|
454
454
|
&:focus-visible {
|
|
455
|
-
${
|
|
455
|
+
${p}
|
|
456
456
|
background-color: transparent;
|
|
457
457
|
}
|
|
458
458
|
`, Z = J(
|
|
@@ -688,7 +688,7 @@ const Te = ({ className: t }) => {
|
|
|
688
688
|
overflow: hidden;
|
|
689
689
|
text-overflow: ellipsis;
|
|
690
690
|
&:focus-visible {
|
|
691
|
-
${
|
|
691
|
+
${p}
|
|
692
692
|
background-color: transparent;
|
|
693
693
|
}
|
|
694
694
|
|
|
@@ -716,7 +716,7 @@ const Te = ({ className: t }) => {
|
|
|
716
716
|
overflow: hidden;
|
|
717
717
|
text-overflow: ellipsis;
|
|
718
718
|
&:focus-visible {
|
|
719
|
-
${
|
|
719
|
+
${p}
|
|
720
720
|
background-color: transparent;
|
|
721
721
|
}
|
|
722
722
|
|
|
@@ -738,7 +738,7 @@ const Te = ({ className: t }) => {
|
|
|
738
738
|
a {
|
|
739
739
|
color: ${(t) => t.theme.colors.link};
|
|
740
740
|
&:focus-visible {
|
|
741
|
-
${
|
|
741
|
+
${p};
|
|
742
742
|
}
|
|
743
743
|
}
|
|
744
744
|
`, lt = ({ header: t, actions: e, dispatch: o }) => /* @__PURE__ */ n(
|
|
@@ -854,7 +854,7 @@ const Te = ({ className: t }) => {
|
|
|
854
854
|
margin: 0;
|
|
855
855
|
|
|
856
856
|
&:focus-visible {
|
|
857
|
-
${
|
|
857
|
+
${p}
|
|
858
858
|
span {
|
|
859
859
|
color: ${(t) => {
|
|
860
860
|
var e;
|
|
@@ -979,7 +979,7 @@ const Te = ({ className: t }) => {
|
|
|
979
979
|
}}
|
|
980
980
|
|
|
981
981
|
&:focus-visible {
|
|
982
|
-
${
|
|
982
|
+
${p}
|
|
983
983
|
span {
|
|
984
984
|
color: ${(t) => {
|
|
985
985
|
var e;
|
|
@@ -991,7 +991,7 @@ const Te = ({ className: t }) => {
|
|
|
991
991
|
`, gt = () => /* @__PURE__ */ n(ft, { className: "humany-conversation-separator" }), ft = a.div`
|
|
992
992
|
${(t) => t.theme.border && `border-bottom: ${t.theme.border};`}
|
|
993
993
|
width: 100%;
|
|
994
|
-
`,
|
|
994
|
+
`, pt = ({ text: t }) => /* @__PURE__ */ n(yt, { className: "humany-conversation-text", variant: "span", children: t }), yt = a(F)`
|
|
995
995
|
${S}
|
|
996
996
|
`, bt = ({
|
|
997
997
|
header: t = "Video call",
|
|
@@ -1067,7 +1067,7 @@ const Te = ({ className: t }) => {
|
|
|
1067
1067
|
border-radius: 100%;
|
|
1068
1068
|
|
|
1069
1069
|
&:focus-visible {
|
|
1070
|
-
${
|
|
1070
|
+
${p}
|
|
1071
1071
|
background-color: transparent;
|
|
1072
1072
|
|
|
1073
1073
|
i,
|
|
@@ -1104,7 +1104,7 @@ const Te = ({ className: t }) => {
|
|
|
1104
1104
|
renderItem: (o) => {
|
|
1105
1105
|
switch (o.type) {
|
|
1106
1106
|
case w.Text:
|
|
1107
|
-
return /* @__PURE__ */ n(
|
|
1107
|
+
return /* @__PURE__ */ n(pt, { text: o.content });
|
|
1108
1108
|
case w.HTML:
|
|
1109
1109
|
return /* @__PURE__ */ n(at, { ...o.content });
|
|
1110
1110
|
case w.Separator:
|
|
@@ -1137,7 +1137,7 @@ const Te = ({ className: t }) => {
|
|
|
1137
1137
|
},
|
|
1138
1138
|
[s]
|
|
1139
1139
|
), { userLabel: d = "" } = C(), m = () => /* @__PURE__ */ n(L, { children: s.map((l) => {
|
|
1140
|
-
const { key: h, body: $, title: v, form: T, actions:
|
|
1140
|
+
const { key: h, body: $, title: v, form: T, actions: y } = l, z = !!$ && !v && !T && !y;
|
|
1141
1141
|
return /* @__PURE__ */ n(
|
|
1142
1142
|
Ge,
|
|
1143
1143
|
{
|
|
@@ -1145,7 +1145,7 @@ const Te = ({ className: t }) => {
|
|
|
1145
1145
|
discrete: z,
|
|
1146
1146
|
ref: i,
|
|
1147
1147
|
timestamp: e,
|
|
1148
|
-
fullWidth: !!T || !!
|
|
1148
|
+
fullWidth: !!T || !!y,
|
|
1149
1149
|
children: z && $ ? /* @__PURE__ */ n(Y, { html: $ }) : /* @__PURE__ */ n(P, { isSystem: !0, content: l })
|
|
1150
1150
|
},
|
|
1151
1151
|
h
|
|
@@ -1265,12 +1265,14 @@ const Te = ({ className: t }) => {
|
|
|
1265
1265
|
}
|
|
1266
1266
|
);
|
|
1267
1267
|
}, te = a.div`
|
|
1268
|
-
position: absolute;
|
|
1269
|
-
left: -10000px;
|
|
1270
|
-
top: auto;
|
|
1271
|
-
width: 1px;
|
|
1268
|
+
position: absolute !important;
|
|
1272
1269
|
height: 1px;
|
|
1270
|
+
width: 1px;
|
|
1273
1271
|
overflow: hidden;
|
|
1272
|
+
clip: rect(1px 1px 1px 1px);
|
|
1273
|
+
clip: rect(1px, 1px, 1px, 1px);
|
|
1274
|
+
left: -10000000px;
|
|
1275
|
+
top: auto;
|
|
1274
1276
|
`, St = () => {
|
|
1275
1277
|
const { skipToLatestMessageLabel: t = "Skip to latest message" } = C(), e = ae(), o = () => {
|
|
1276
1278
|
const r = e.get("widgetDOMElement");
|
|
@@ -1310,7 +1312,7 @@ const Te = ({ className: t }) => {
|
|
|
1310
1312
|
&:focus-visible {
|
|
1311
1313
|
left: 50%;
|
|
1312
1314
|
transform: translateX(-50%);
|
|
1313
|
-
${
|
|
1315
|
+
${p};
|
|
1314
1316
|
}
|
|
1315
1317
|
`, he = () => {
|
|
1316
1318
|
const t = ce((d) => {
|
|
@@ -1369,7 +1371,7 @@ const Te = ({ className: t }) => {
|
|
|
1369
1371
|
}};
|
|
1370
1372
|
${(t) => !!t.scrollableCss && t.scrollableCss};
|
|
1371
1373
|
&:focus-visible {
|
|
1372
|
-
${(t) =>
|
|
1374
|
+
${(t) => p(t)};
|
|
1373
1375
|
}
|
|
1374
1376
|
`, W = a.div`
|
|
1375
1377
|
margin: 0;
|
|
@@ -1405,7 +1407,7 @@ const Te = ({ className: t }) => {
|
|
|
1405
1407
|
}
|
|
1406
1408
|
|
|
1407
1409
|
&:focus-visible {
|
|
1408
|
-
${
|
|
1410
|
+
${p}
|
|
1409
1411
|
border-radius: ${(t) => {
|
|
1410
1412
|
var e;
|
|
1411
1413
|
return (e = t.theme.sizes) == null ? void 0 : e.small;
|
|
@@ -1524,7 +1526,7 @@ const Te = ({ className: t }) => {
|
|
|
1524
1526
|
${(t) => t.theme.borderRadius && `border-radius: ${t.theme.borderRadius};`}
|
|
1525
1527
|
|
|
1526
1528
|
&:focus-visible {
|
|
1527
|
-
${
|
|
1529
|
+
${p};
|
|
1528
1530
|
}
|
|
1529
1531
|
|
|
1530
1532
|
svg line {
|
package/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
"use strict";var $=Object.defineProperty;var q=(o,e,t)=>e in o?$(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var d=(o,e,t)=>(q(o,typeof e!="symbol"?e+"":e,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const D=require("@telia-ace/widget-core"),p=require("@telia-ace/widget-services"),E=require("@telia-ace/widget-utilities"),L=require("@telia-ace/widget-forms"),M=require("@telia-ace/widget-plugins"),N=require("@telia-ace/widget-ui"),z=require("react");class U{constructor(e){d(this,"cache",new Map);d(this,"subscriptions",[]);this.controller=e;const{events:t}=e.container.get("$widget"),s=t.subscribe("data-client:fetched",(r,n)=>{n.type==="contact-method"&&this.cache.set(n.response.id,n.response)});this.subscriptions.push(s)}getConversationLogs(){return this.controller.messages.reduce((t,s)=>{const r=k(s),{key:n,content:a,type:i,sender:c,timestamp:h}=r.content,u=this.parseEntry(a,i,c,h);return u&&t.push(u),t},[])}parseEntry(e,t,s,r){const n=h=>{let u=[],l=[],m;if(Array.isArray(h)){const[g,f]=h;m=f}else m=h;return this.createMessageContent(m).length&&(l=[...l,this.createMessageContent(m)]),u=[...u,...this.createMessageOptions(m)],{messagesAsList:l,options:u}};let a=[],i=[];if(Array.isArray(e.items)){const{items:h}=e;h.forEach(u=>{const l=n(u);a=[...a,...l.options],i=[...i,...l.messagesAsList]})}else{const h=n(e);a=[...a,...h.options],i=[...i,...h.messagesAsList]}return{options:a,message:i.length?i.join(`
|
|
2
2
|
`):void 0,source:this.createMessageSource(t),alias:this.createAlias(t,s),timestamp:r}}createMessageSource(e){return e}createAlias(e,t){return e===y.System?"system":(t==null?void 0:t.name)||"Me"}handleContactMethod(e){if(!this.cache.has(e))return;const t=this.cache.get(e);let s="";return s=this.appendText(s,`${t.clientName}:`),s=this.appendText(s,t.title),t.description&&(s=this.appendText(s,t.title)),s}getDetails(e,t=!0){if(!e)return"";const{header:s,title:r,label:n,description:a,text:i,html:c,body:h,contactMethodId:u}=e;let l="";if(u){const m=this.handleContactMethod(u.toString());m&&(l=this.appendText(l,m,t))}return s&&(l=this.appendText(l,s,t)),r&&(l=this.appendText(l,r,t)),a&&(l=this.appendText(l,a,t)),i&&(l=this.appendText(l,i,t)),n&&(l=this.appendText(l,n,t)),c&&(l=this.appendText(l,c,t)),h&&typeof h=="string"&&(l=this.appendText(l,h,t)),l}createMessageContent(e){let t="";return t=this.getDetails(e),t}createMessageOptions(e){let t=[],s=[];return Array.isArray(e==null?void 0:e.actions)?s=e.actions:typeof(e==null?void 0:e.actions)=="object"&&(s=Object.entries(e.actions).map(([r,n])=>({actionKey:r,label:n}))),s.length&&(t=s.map(r=>this.getDetails(r,!1))),t}appendText(e,t,s=!0){return s&&e.length?`${e}
|
|
3
|
-
${t}`:e.length?`${e} ${t}`:t}dispose(){this.subscriptions.forEach(e=>{e()})}}class P{constructor(e,t){d(this,"id");d(this,"name");d(this,"avatar");d(this,"print",(e,t,s=null)=>{const r={name:this.name,avatar:this.avatar,id:this.id},n=k(e,t);return s=s??n.timestamp,this.provider.createEntry(n.key,n.content,y.Agent,r,s)});d(this,"typing",()=>this.provider.setTypingState({name:this.name,avatar:this.avatar}));this.provider=e,this.id=t.id||E.uuid(),this.name=t.name,this.avatar=t.avatar}update(e){var r;const{name:t,avatar:s}=e;s===null?this.avatar=void 0:s&&(this.avatar=s),t===null?this.name=void 0:t&&(this.name=t),(r=this.provider.controller)==null||r.events.dispatch("conversation:agent-updated",{agent:this})}}class A{constructor(e,t,s){d(this,"subscriptions",[]);d(this,"options");d(this,"user");this.name=e,this.container=t,this.controller=s,this.user=s==null?void 0:s.user,this.options={silent:!1}}static create(e,t,s){return new A(e,t,s)}createAgent(e={}){var t;if(this.controller){const s=this.controller.getAgentById(e.id),r=s?new P(this,{...s,...e}):new P(this,e);return s||(t=this.controller)==null||t.events.dispatch("conversation:agent-created",{agent:r}),r}}print(e){var t;return(t=this.controller)==null?void 0:t.print(e)}setTypingState(e,t){var s;return(s=this.controller)==null?void 0:s.setTypingState(e,t)}loading(){return this.controller?this.controller.loader():()=>()=>{}}createEntry(e,t,s,r,n){var a;return(a=this.controller)==null?void 0:a.createEntry(e,t,s,r,n)}dispose(){var e;process.env.NODE_ENV!=="production"&&console.log(`conversation-provider: dispose (${this.name})`),(e=this.controller)==null||e.disposeProvider(this),this.controller=null,this.subscriptions.forEach(t=>t())}isRehydrated(){var e;return((e=this.controller)==null?void 0:e.isRehydrated())||Promise.resolve(!1)}getHistory(){var e;return((e=this.controller)==null?void 0:e.getHistory())||[]}complete(){var e;(e=this.controller)==null||e.onCompleteProvider()}setOptions(e={silent:!1}){this.options=e}onUnreadMessage(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.onUnreadMessage(e);return this.subscriptions.push(s),()=>{s()}}}onMounted(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.events.subscribe("conversation:component-mount-changed",e);return this.subscriptions.push(s),e({},this.controller.getComponentMountedState()),()=>{s()}}}}const O=o=>`conversation-${o}`;class K{constructor(){d(this,"_locking",Promise.resolve());d(this,"_locks",0)}isLocked(){return this._locks>0}lock(){this._locks+=1;let e;const t=new Promise(r=>e=()=>{this._locks-=1,r()}),s=this._locking.then(()=>e);return this._locking=this._locking.then(()=>t),s}}class Q{constructor(e,t,s){d(this,"sessionStorageKey");d(this,"locker",new K);d(this,"initialStoredAgents",[]);this.container=e,this.conversation=t,this.enabled=s,this.sessionStorageKey=O(t.id),t.onStarted(async()=>{if(t.messages.length>0)return;const{agents:r}=await this.read(),n=[];r&&r.forEach(i=>{const c=t.providerIndex(i.providerName),h=c>-1?t.registeredProviders[c]:t.createProvider(i.providerName),u=new P(h,{name:i.name,avatar:i.avatar,id:i.id});n.push(u)});const{messages:a}=await this.read();a.forEach(i=>{switch(i.type){case"agent":{const c=n.find(h=>{var u;return h.id===((u=i.sender)==null?void 0:u.id)});c&&c.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"user":{t.user.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"system":default:t.print(i.content.items||i.content,i.timestamp)}})}),t.onMessageCreated(async(r,n)=>{this.write(n.message,"messages")}),t.events.subscribe("conversation:agent-created",async(r,n)=>{const a={id:n.agent.id,name:n.agent.name,avatar:n.agent.avatar,providerName:n.agent.provider.name};await this.write(a,"agents")}),t.events.subscribe("conversation:agent-updated",async(r,n)=>{const{agents:a}=await this.read();await this.write([...a.map(i=>i.id===n.agent.id?{...i,name:n.agent.name,avatar:n.agent.avatar}:i)],"agents","update")})}setInitialData(e){this.initialStoredAgents=e.agents}async isRehydrated(){const{messages:e}=await this.read();return e.length>0}getStoredAgent(e){if(this.enabled)return this.initialStoredAgents.find(t=>t.id===e)}async write(e,t,s="add"){if(!this.enabled)return;const r=await this.locker.lock();return p.createStorageWriter(this.container,this.sessionStorageKey,p.StorageCategory.Necessary).then(async n=>{if(s==="add"){const a=await this.read();if(t==="messages"&&a.messages.findIndex(c=>c.key===e.key)>-1){r();return}const i={...a};return t==="messages"?i.messages=[...i.messages,e]:t==="agents"&&(i.agents=[...i.agents,e]),n({...a,...i}).then(()=>{r()})}if(s==="update"&&t==="agents"){const a=await this.read();return n({...a,agents:e}).then(()=>{r()})}return n(e).then(()=>{r()})})}async read(){return p.readStorage(this.container,this.sessionStorageKey).then(e=>e||{messages:[],agents:[]})}}class j{constructor(e){d(this,"print",(e,t,s=null)=>{const r=k(e,t);return s=s??r.timestamp,this.controller.createEntry(r.key,r.content,y.User,void 0,s)});this.controller=e}}const x=async(o,e,t)=>{const s=await o.getAsync("conversation"),r=await o.getAsync("components"),n=s.controllers.find(a=>a.conversation.id===e);if(n){const a=[...n.conversation.providers];a.forEach(c=>{n.conversation.setProviderOptions(c,{silent:!0})}),n.conversation.queueProviders(a),n.conversation.setProviders([t]);const i=r.components().ofType("conversation");r.write({key:"setConversationProvider",attributes:{properties:{providers:[t]}},target:i,provider:"localStorage"})}};class H{constructor(e){this.writer=e}update(e){const t=this.getNormalizedSettings(e);this.writer(t)}remove(){this.writer(null)}getNormalizedSettings(e){return typeof e=="string"?{body:e}:e.length>-1&&typeof e=="object"?{items:e}:e}}var y=(o=>(o.User="user",o.Agent="agent",o.System="system",o))(y||{});const k=(o,e)=>{let t,s=(e==null?void 0:e.key)||E.uuid(),r=Date.now();if(Array.isArray(o)&&o.length>-1)typeof o[0]=="string"?t={items:[o]}:t={items:o};else if(typeof o=="string")t={body:o};else if(o=o||{},o.type)t={...o};else{if(t={body:o.body,actions:o.actions,title:o.title},o.key&&(s=o.key),o.timestamp&&(r=o.timestamp),typeof o.form=="function"){const n=L.FormBuilder.create(s);o.form(n);const a=n.get();t.form=a}o.timestamp===null&&(r=null)}return{content:t,key:s,timestamp:r}},W=(o,e)=>{if(!e)return;const t=JSON.stringify(e),s=o.findIndex(r=>t===JSON.stringify(r));if(s>-1)return o.splice(s,1)};class B{constructor(e,t,s){d(this,"formHandlers",{});d(this,"user",new j(this));d(this,"events");d(this,"providers",[]);d(this,"active",!1);d(this,"queuedProviders",[]);d(this,"registeredProviders",[]);d(this,"loaders",0);d(this,"componentMounted",!1);d(this,"store");d(this,"messages",[]);d(this,"typingActors",[]);d(this,"conversationHistory");d(this,"print",(e,t=null)=>{const s=k(e);return t=t??s.timestamp,this.createEntry(s.key,s.content,"system",void 0,t)});this.container=e,this.id=t;const r=e.get("$widget");this.events=r.events.createChild(this),this.store=new Q(e,this,s.rehydrate),this.conversationHistory=new U(this)}setInitialStorage(e){this.store.setInitialData(e)}setProviders(e){const t=[],s=[];this.providers.reduce((r,n)=>(e.indexOf(n)===-1&&r.push(n),r),[]).forEach(r=>{const n=this.providers.indexOf(r);n>-1&&(t.push(r),this.providers.splice(n,1))}),e.forEach(r=>{this.providers.indexOf(r)===-1&&(s.push(r),this.providers.push(r))}),(s.length||t.length)&&this.events.dispatch("conversation:providers-changed",{added:s,removed:t})}createProvider(e){const t=this.providerIndex(e);if(t>-1)return this.registeredProviders[t];const s=A.create(e,this.container,this);return this.registeredProviders.push(s),s}providerIndex(e){return this.registeredProviders.findIndex(t=>t.name===e)}getAgentById(e){if(e)return this.store.getStoredAgent(e)}dispose(){this.conversationHistory.dispose(),this.registeredProviders.forEach(e=>e.dispose()),this.messages=[],this.events.dispatch("conversation:controller-disposed",this),this.active=!1}onDispose(e){return this.events.subscribe("conversation:controller-disposed",e)}onStarted(e){return this.events.subscribe("conversation:started",e)}onMessageCreated(e){return this.events.subscribe("conversation:message-created",e)}isRehydrated(){return this.store.isRehydrated()}disposeProvider(e){const t=this.providerIndex(e.name);t>-1&&this.registeredProviders.splice(t,1),this.events.dispatch("conversation:providers-changed",{removed:[e.name]}),this.registeredProviders.length||this.dispose()}onProvidersChange(e){return this.events.subscribe("conversation:providers-changed",e)}setComponentMountedState(e){this.componentMounted!==e&&(this.componentMounted=e,e&&(this.events.dispatch("conversation:unread-message",!1),this.events.dispatch("conversation:started",!0),this.active=!0),this.events.dispatch("conversation:component-mount-changed",e))}setProviderOptions(e,t){const s=this.providerIndex(e);s>-1&&this.registeredProviders[s].setOptions(t)}changeProvider(e){this.registeredProviders.find(s=>s.name===e)&&x(this.container,this.id,e)}queueProviders(e){this.queuedProviders=e}onCompleteProvider(){const e=this.queuedProviders.pop();e&&x(this.container,this.id,e)}getComponentMountedState(){return this.componentMounted}onUnreadMessage(e){return this.events.subscribe("conversation:unread-message",e)}createEntry(e,t,s,r,n){const a={key:e,content:t,type:s,sender:r||null,timestamp:n||n===null?n:Date.now()};this.messages.push(a),a.type==="agent"&&W(this.typingActors,r);const i=new H(c=>{if(c!==null){const{timestamp:h=void 0,...u}=c;a.content=u,h&&(a.timestamp=h)}else{const h=this.messages.indexOf(a);a.content=null,h>-1&&this.messages.splice(h,1)}this.events.dispatch("conversation:message-updated",{message:a}),this.update()});return this.events.dispatch("conversation:message-created",{message:a}),this.update(),this.componentMounted||this.events.dispatch("conversation:unread-message",!0),i}update(){this.events.dispatch("conversation:updated",{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors})}getMessageStream(){return{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors}}setTypingState(e,t){return this.typingActors.indexOf(e)===-1&&(this.typingActors.push(e),this.update()),()=>{W(this.typingActors,e),this.update()}}loader(){this.loaders=this.loaders+1;let e=!1;return this.loaders===1&&this.update(),()=>{e||(e=!0,this.loaders=this.loaders-1,this.loaders===0&&this.update())}}submitForm(e,t){this.formHandlers[e]={...this.formHandlers[e],submit:t}}validateForm(e,t){this.formHandlers[e]={...this.formHandlers[e],validate:t}}getHistory(){return this.conversationHistory.getConversationLogs()}}const F=(o,e)=>{const t=o.split("_contact");return e.widgets.get(t[0])},I=o=>{const e=o.get("$widget"),{name:t,implementation:s}=o.get("$widget"),r=t.indexOf("_contact")>-1?F(t,s):e,{data:n}=o.get("$settings"),{trigger:a}=r.container.get("$settings"),i=`${t.replace("_contact-method","").replace("_contact","")}_contact-method`,c=s.widgets.get(i);return c&&!a?{chatWidget:c,mainWidget:r,data:n}:{mainWidget:r,data:n}},G=o=>{const{chatWidget:e}=I(o);return!!e},R=(o,e,t)=>{const{mainWidget:s,data:r,chatWidget:n}=I(o);return n?n.state==="activated"?n.invoke("open").then(()=>{n.container.getAsync("conversation").then(a=>{a.navigateToConversationIfActive(e.id).then(()=>M.closeAllModals(o))})}):n.activate().then(()=>{const{trigger:a}=n.container.get("$settings");return a.action="hide",n.invoke("setSettings",()=>({data:r,trigger:a,activate:!0})).then(()=>n.container.getAsync("components").then(i=>{const c=i.components().ofType("contact-method");i.write({key:"setContactMethodId",attributes:{properties:{id:e.id,guideId:e.guideId}},target:c,provider:"localStorage"});const h=i.components().ofType("conversation");i.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:h,provider:"localStorage"})}))}).then(()=>n.invoke("attach",{key:"contact-method",triggerDOMElement:t,withRenderState:"open"})).then(()=>(p.createStorageWriter(n.container,"activeStickyWidget",p.StorageCategory.Necessary,{medium:p.StorageMedium.Local,scope:p.StorageScope.Implementation}).then(a=>a(n.name)),n.container.getAsync("conversation").then(a=>{a.setConversationEndedBehavior(S.Deactivate)}).then(()=>M.closeAllModals(o)).then(()=>!1))):(s.container.getAsync("conversation").then(a=>a.navigateToConversationIfActive(e.id).then(()=>a.setConversationEndedBehavior(S.Navigate))),!n)},J=(o,e,t="conversation")=>{const{mainWidget:s,chatWidget:r}=I(o);return r?r.container.getAsync("router").then(n=>{n.navigate(t,{id:e.id})}):s.container.getAsync("conversation").then(n=>{var a;(a=n.setConversationEndedBehavior(S.Navigate))==null||a.then(()=>{s.container.getAsync("router").then(i=>{s.container.getAsync("components").then(c=>{const h=c.components().ofType("conversation");c.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:h,provider:"localStorage"})}).then(()=>{i.navigate(t,{id:e.id})})})})})},b="conversations";var S=(o=>(o.Deactivate="deactivate",o.Navigate="navigate",o.None="none",o))(S||{});class w{constructor(e,t){d(this,"events");d(this,"controllers",[]);d(this,"providers",[]);d(this,"disposeProviders",{});d(this,"subscriptions",[]);d(this,"storageWriter",null);d(this,"router",null);d(this,"buttonQuery",null);d(this,"sessionInfo",null);this.container=e,this.components=t;const s=e.get("$widget");this.events=s.events.createChild(this)}initialize(){p.createStorageWriter(this.container,b,p.StorageCategory.Necessary,{medium:p.StorageMedium.Local}).then(e=>this.storageWriter=e),p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>{this.buttonQuery=this.components.components().ofType("conversation-return-button"),e&&(this.sessionInfo=e,e.id&&e.route&&this.writeToComponents({active:!0,alert:{show:!1}}))}),this.container.getAsync("router").then(e=>this.router=e),this.events.subscribe("conversation:ended",(e,t)=>{var s,r,n;switch((s=this.sessionInfo)==null?void 0:s.conversationEndedBehavior){case"deactivate":this.container.get("$widget").deactivate();break;case"navigate":const i=(r=this.router)==null?void 0:r.getInitialRoute();i&&((n=this.router)==null||n.navigate(i.name));break}this.writeToComponents({active:!1}),this.sessionInfo=null,this.storageWriter&&this.storageWriter()})}addSubscription(e){this.subscriptions.push(e)}static getInstance(e,t="conversation"){return E.lock(e)(()=>e.getAsync(t).then(s=>{let r=s;return r||D.ComponentPlatform.getInstance(e).then(n=>(r=new w(e,n),r.initialize(),e.registerAsync(t,()=>r),r))}))}navigateToConversationIfActive(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{var s;if(t&&t.id===e&&t.route){const{name:r,params:n}=t.route;(s=this.router)==null||s.navigate(r,n)}})}setConversationEndedBehavior(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{let s=t;return s?s.conversationEndedBehavior=e:s={conversationEndedBehavior:e},this.sessionInfo=s,this.storageWriter&&this.storageWriter(this.sessionInfo)||Promise.resolve()})}async createController(e,t){const s=O(e),{agents:r}=await p.readStorage(this.container,s)||{agents:[]},n=this.controllers.findIndex(i=>i.conversation.id===e);let a;return n>-1?a=this.controllers[n].conversation:a=new B(this.container,e,t),a.setInitialStorage({agents:r||[]}),a}registerProvider(e,t){const s={name:e,handler:t};this.providers.push(s),this.controllers.filter(r=>r.conversation.providers.indexOf(e)>-1).forEach(r=>{this.invokeHandler(s,r)})}registerController(e,t){const s=this.controllers.findIndex(n=>n.conversation.id===e.id);let r;if(s<0){r={conversation:e,component:t},this.controllers.push(r);const n=a=>{a&&this.providers.filter(i=>a.indexOf(i.name)>-1).forEach(i=>{this.invokeHandler(i,r)})};return this.subscriptions.push(e.onDispose((a,i)=>{const c=this.controllers.findIndex(h=>h.conversation===i);c>-1&&this.controllers.splice(c,1),this.components.components().ofType("conversation-return-button").select().writeProperties({active:!1}),this.events.dispatch("conversation:ended",{},{bubbles:!0})})),this.subscriptions.push(e.onProvidersChange((a,i)=>{const{added:c,removed:h=[]}=i;h.forEach(u=>{(this.disposeProviders[u]||[]).forEach(l=>{l()})}),n(c)})),this.subscriptions.push(e.onStarted((a,i)=>{var h,u;const c={id:e.id,route:(h=this.router)==null?void 0:h.getRouteData(),conversationEndedBehavior:(u=this.sessionInfo)==null?void 0:u.conversationEndedBehavior};this.sessionInfo=c,this.storageWriter&&this.storageWriter(c),this.writeToComponents({active:i})})),this.subscriptions.push(e.onUnreadMessage((a,i)=>{this.events.dispatch("conversation:unread-message",i),this.writeToComponents({alert:{show:i}})})),n(e.providers),!0}return r=this.controllers[s],!1}writeToComponents(e,t="conversationReturnButtonState"){this.buttonQuery&&this.components.write({key:"conversationReturnButtonState",attributes:{properties:e},target:this.buttonQuery,provider:"localStorage"})}invokeHandler(e,t){const s=e.handler(t.conversation.createProvider(e.name),t.component);s&&(this.disposeProviders[e.name]||(this.disposeProviders[e.name]=[]),this.disposeProviders[e.name].push(s))}getComponentRoute(){return p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>e==null?void 0:e.route)}dispose(){Object.keys(this.disposeProviders).forEach(e=>{this.disposeProviders[e].forEach(t=>{t()})}),this.subscriptions.forEach(e=>{e()}),this.controllers=[],this.providers=[]}static async handleContactMethodClick(e,t,s,r){if(G(t)){e.preventDefault();let n=document.getElementById("humany-dynamic-anchor");n||(n=document.createElement("a"),n.setAttribute("href","#"),n.id="humany-dynamic-anchor",document.body.appendChild(n)),R(t,s,n)}R(t,s)}static async handleContactMethodSubmit(e,t){var s,r;return(r=(s=t==null?void 0:t.body.form.meta)==null?void 0:s.conversation)!=null&&r.id?x(e,t.body.form.meta.conversation.id,t.clientName):J(e,t)}}const V=(o,e)=>o.reduce((t,s,r)=>{var a,i;const n={...s,content:[{...s.content,key:s.key}]};if(t.length>0&&s.type!==y.System){const c=t[t.length-1];if((!e&&e!==0||Math.abs(c.timestamp-s.timestamp)<e)&&((a=c.sender)==null?void 0:a.name)===((i=s.sender)==null?void 0:i.name)&&c.type===s.type)return c.timestamp=s.timestamp,c.content.push({...s.content,key:s.key}),t}return t.push(n),t},[]),Y=o=>Promise.all([w.getInstance(o),o.getAsync("router")]).then(([e,t])=>N.createReactComponent(o,"conversation",Promise.resolve().then(()=>require("./conversation-8ebf1b1a.js")),async s=>{const{rehydrate:r=!1}=s.properties(),a=t.getRouteData().params.id||s.node.name||"component",i=await e.createController(a,{rehydrate:r}),c=e.registerController(i,s),h=3*60*1e3,u=v=>{const{providers:g=[]}=v;if(g){const f=Array.isArray(g)?g:[g];return i.setProviders(f),f}};u(s.properties(u)),i.setComponentMountedState(!0);const l=v=>{const{loading:g,typingActors:f,messages:C}=v;s.writeProperties({loading:g,typingActors:f,messages:V(C,h)})};l(i.getMessageStream()),c&&(s.writeProperties({conversationControllerId:i.id}),s.actions.create("user-submit",(v,g)=>{if(g){const{preventDefault:f,validationPromise:C}=g;if(f){C&&C().then(T=>{T&&i.user.print(T)});return}}i.user.print(v.text)}),s.actions.create("user-typing",(v,g)=>{}),s.actions.create("form",(v,g)=>{}),s.actions.create("action",(v,g)=>{}),s.actions.create("overlay-mounted",(v,g)=>{}),s.actions.create("show-overlay",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}),s.actions.create("hide-overlay",(v,g)=>{s.writeProperties({overlay:null})}),s.actions.create("show-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!0,secondary:!1}})}),s.actions.create("hide-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}));const m=i.events.subscribe("conversation:updated",(v,g)=>{l(g)});return()=>{i.setComponentMountedState(!1),m(),i.active||l({messages:[],typingActors:[],loading:!1})}},{reuseController:!0})),X=o=>Promise.all([D.ComponentPlatform.getInstance(o),w.getInstance(o),o.getAsync("router")]).then(([e,t,s])=>N.createReactComponent(o,"conversation-return-button",Promise.resolve().then(()=>require("./return-button-f1f43984.js")),r=>{r.actions.create("toggle",(n={})=>{var a;(a=t.getComponentRoute())==null||a.then(i=>{i&&s.navigate(i.name,i.params)})})}));var _=(o=>(o.LinkList="link-list",o.Separator="separator",o.ButtonList="button-list",o.ItemList="item-list",o.Text="text",o.HTML="html",o.VideoRequest="video-request",o))(_||{});const Z=(o,e,t)=>o.getAsync("componentResolver").then(async s=>{(await w.getInstance(o)).addSubscription(s.registerComponent(e,z.lazy(()=>t)))});exports.Agent=P;exports.ConversationComponent=Y;exports.ConversationController=B;exports.ConversationEndedBehavior=S;exports.ConversationEntry=H;exports.ConversationMessageListItemType=_;exports.ConversationMessageType=y;exports.ConversationPlatform=w;exports.ConversationProvider=A;exports.ConversationReturnButtonComponent=X;exports.registerCustomMessageComponent=Z;
|
|
3
|
+
${t}`:e.length?`${e} ${t}`:t}dispose(){this.subscriptions.forEach(e=>{e()})}}class P{constructor(e,t){d(this,"id");d(this,"name");d(this,"avatar");d(this,"print",(e,t,s=null)=>{const r={name:this.name,avatar:this.avatar,id:this.id},n=k(e,t);return s=s??n.timestamp,this.provider.createEntry(n.key,n.content,y.Agent,r,s)});d(this,"typing",()=>this.provider.setTypingState({name:this.name,avatar:this.avatar}));this.provider=e,this.id=t.id||E.uuid(),this.name=t.name,this.avatar=t.avatar}update(e){var r;const{name:t,avatar:s}=e;s===null?this.avatar=void 0:s&&(this.avatar=s),t===null?this.name=void 0:t&&(this.name=t),(r=this.provider.controller)==null||r.events.dispatch("conversation:agent-updated",{agent:this})}}class A{constructor(e,t,s){d(this,"subscriptions",[]);d(this,"options");d(this,"user");this.name=e,this.container=t,this.controller=s,this.user=s==null?void 0:s.user,this.options={silent:!1}}static create(e,t,s){return new A(e,t,s)}createAgent(e={}){var t;if(this.controller){const s=this.controller.getAgentById(e.id),r=s?new P(this,{...s,...e}):new P(this,e);return s||(t=this.controller)==null||t.events.dispatch("conversation:agent-created",{agent:r}),r}}print(e){var t;return(t=this.controller)==null?void 0:t.print(e)}setTypingState(e,t){var s;return(s=this.controller)==null?void 0:s.setTypingState(e,t)}loading(){return this.controller?this.controller.loader():()=>()=>{}}createEntry(e,t,s,r,n){var a;return(a=this.controller)==null?void 0:a.createEntry(e,t,s,r,n)}dispose(){var e;process.env.NODE_ENV!=="production"&&console.log(`conversation-provider: dispose (${this.name})`),(e=this.controller)==null||e.disposeProvider(this),this.controller=null,this.subscriptions.forEach(t=>t())}isRehydrated(){var e;return((e=this.controller)==null?void 0:e.isRehydrated())||Promise.resolve(!1)}getHistory(){var e;return((e=this.controller)==null?void 0:e.getHistory())||[]}complete(){var e;(e=this.controller)==null||e.onCompleteProvider()}setOptions(e={silent:!1}){this.options=e}onUnreadMessage(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.onUnreadMessage(e);return this.subscriptions.push(s),()=>{s()}}}onMounted(e){var t;if(this.controller){const s=(t=this.controller)==null?void 0:t.events.subscribe("conversation:component-mount-changed",e);return this.subscriptions.push(s),e({},this.controller.getComponentMountedState()),()=>{s()}}}}const O=o=>`conversation-${o}`;class K{constructor(){d(this,"_locking",Promise.resolve());d(this,"_locks",0)}isLocked(){return this._locks>0}lock(){this._locks+=1;let e;const t=new Promise(r=>e=()=>{this._locks-=1,r()}),s=this._locking.then(()=>e);return this._locking=this._locking.then(()=>t),s}}class Q{constructor(e,t,s){d(this,"sessionStorageKey");d(this,"locker",new K);d(this,"initialStoredAgents",[]);this.container=e,this.conversation=t,this.enabled=s,this.sessionStorageKey=O(t.id),t.onStarted(async()=>{if(t.messages.length>0)return;const{agents:r}=await this.read(),n=[];r&&r.forEach(i=>{const c=t.providerIndex(i.providerName),h=c>-1?t.registeredProviders[c]:t.createProvider(i.providerName),u=new P(h,{name:i.name,avatar:i.avatar,id:i.id});n.push(u)});const{messages:a}=await this.read();a.forEach(i=>{switch(i.type){case"agent":{const c=n.find(h=>{var u;return h.id===((u=i.sender)==null?void 0:u.id)});c&&c.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"user":{t.user.print(i.content.items||i.content,{key:i.key},i.timestamp);return}case"system":default:t.print(i.content.items||i.content,i.timestamp)}})}),t.onMessageCreated(async(r,n)=>{this.write(n.message,"messages")}),t.events.subscribe("conversation:agent-created",async(r,n)=>{const a={id:n.agent.id,name:n.agent.name,avatar:n.agent.avatar,providerName:n.agent.provider.name};await this.write(a,"agents")}),t.events.subscribe("conversation:agent-updated",async(r,n)=>{const{agents:a}=await this.read();await this.write([...a.map(i=>i.id===n.agent.id?{...i,name:n.agent.name,avatar:n.agent.avatar}:i)],"agents","update")})}setInitialData(e){this.initialStoredAgents=e.agents}async isRehydrated(){const{messages:e}=await this.read();return e.length>0}getStoredAgent(e){if(this.enabled)return this.initialStoredAgents.find(t=>t.id===e)}async write(e,t,s="add"){if(!this.enabled)return;const r=await this.locker.lock();return p.createStorageWriter(this.container,this.sessionStorageKey,p.StorageCategory.Necessary).then(async n=>{if(s==="add"){const a=await this.read();if(t==="messages"&&a.messages.findIndex(c=>c.key===e.key)>-1){r();return}const i={...a};return t==="messages"?i.messages=[...i.messages,e]:t==="agents"&&(i.agents=[...i.agents,e]),n({...a,...i}).then(()=>{r()})}if(s==="update"&&t==="agents"){const a=await this.read();return n({...a,agents:e}).then(()=>{r()})}return n(e).then(()=>{r()})})}async read(){return p.readStorage(this.container,this.sessionStorageKey).then(e=>e||{messages:[],agents:[]})}}class j{constructor(e){d(this,"print",(e,t,s=null)=>{const r=k(e,t);return s=s??r.timestamp,this.controller.createEntry(r.key,r.content,y.User,void 0,s)});this.controller=e}}const x=async(o,e,t)=>{const s=await o.getAsync("conversation"),r=await o.getAsync("components"),n=s.controllers.find(a=>a.conversation.id===e);if(n){const a=[...n.conversation.providers];a.forEach(c=>{n.conversation.setProviderOptions(c,{silent:!0})}),n.conversation.queueProviders(a),n.conversation.setProviders([t]);const i=r.components().ofType("conversation");r.write({key:"setConversationProvider",attributes:{properties:{providers:[t]}},target:i,provider:"localStorage"})}};class H{constructor(e){this.writer=e}update(e){const t=this.getNormalizedSettings(e);this.writer(t)}remove(){this.writer(null)}getNormalizedSettings(e){return typeof e=="string"?{body:e}:e.length>-1&&typeof e=="object"?{items:e}:e}}var y=(o=>(o.User="user",o.Agent="agent",o.System="system",o))(y||{});const k=(o,e)=>{let t,s=(e==null?void 0:e.key)||E.uuid(),r=Date.now();if(Array.isArray(o)&&o.length>-1)typeof o[0]=="string"?t={items:[o]}:t={items:o};else if(typeof o=="string")t={body:o};else if(o=o||{},o.type)t={...o};else{if(t={body:o.body,actions:o.actions,title:o.title},o.key&&(s=o.key),o.timestamp&&(r=o.timestamp),typeof o.form=="function"){const n=L.FormBuilder.create(s);o.form(n);const a=n.get();t.form=a}o.timestamp===null&&(r=null)}return{content:t,key:s,timestamp:r}},W=(o,e)=>{if(!e)return;const t=JSON.stringify(e),s=o.findIndex(r=>t===JSON.stringify(r));if(s>-1)return o.splice(s,1)};class B{constructor(e,t,s){d(this,"formHandlers",{});d(this,"user",new j(this));d(this,"events");d(this,"providers",[]);d(this,"active",!1);d(this,"queuedProviders",[]);d(this,"registeredProviders",[]);d(this,"loaders",0);d(this,"componentMounted",!1);d(this,"store");d(this,"messages",[]);d(this,"typingActors",[]);d(this,"conversationHistory");d(this,"print",(e,t=null)=>{const s=k(e);return t=t??s.timestamp,this.createEntry(s.key,s.content,"system",void 0,t)});this.container=e,this.id=t;const r=e.get("$widget");this.events=r.events.createChild(this),this.store=new Q(e,this,s.rehydrate),this.conversationHistory=new U(this)}setInitialStorage(e){this.store.setInitialData(e)}setProviders(e){const t=[],s=[];this.providers.reduce((r,n)=>(e.indexOf(n)===-1&&r.push(n),r),[]).forEach(r=>{const n=this.providers.indexOf(r);n>-1&&(t.push(r),this.providers.splice(n,1))}),e.forEach(r=>{this.providers.indexOf(r)===-1&&(s.push(r),this.providers.push(r))}),(s.length||t.length)&&this.events.dispatch("conversation:providers-changed",{added:s,removed:t})}createProvider(e){const t=this.providerIndex(e);if(t>-1)return this.registeredProviders[t];const s=A.create(e,this.container,this);return this.registeredProviders.push(s),s}providerIndex(e){return this.registeredProviders.findIndex(t=>t.name===e)}getAgentById(e){if(e)return this.store.getStoredAgent(e)}dispose(){this.conversationHistory.dispose(),this.registeredProviders.forEach(e=>e.dispose()),this.messages=[],this.events.dispatch("conversation:controller-disposed",this),this.active=!1}onDispose(e){return this.events.subscribe("conversation:controller-disposed",e)}onStarted(e){return this.events.subscribe("conversation:started",e)}onMessageCreated(e){return this.events.subscribe("conversation:message-created",e)}isRehydrated(){return this.store.isRehydrated()}disposeProvider(e){const t=this.providerIndex(e.name);t>-1&&this.registeredProviders.splice(t,1),this.events.dispatch("conversation:providers-changed",{removed:[e.name]}),this.registeredProviders.length||this.dispose()}onProvidersChange(e){return this.events.subscribe("conversation:providers-changed",e)}setComponentMountedState(e){this.componentMounted!==e&&(this.componentMounted=e,e&&(this.events.dispatch("conversation:unread-message",!1),this.events.dispatch("conversation:started",!0),this.active=!0),this.events.dispatch("conversation:component-mount-changed",e))}setProviderOptions(e,t){const s=this.providerIndex(e);s>-1&&this.registeredProviders[s].setOptions(t)}changeProvider(e){this.registeredProviders.find(s=>s.name===e)&&x(this.container,this.id,e)}queueProviders(e){this.queuedProviders=e}onCompleteProvider(){const e=this.queuedProviders.pop();e&&x(this.container,this.id,e)}getComponentMountedState(){return this.componentMounted}onUnreadMessage(e){return this.events.subscribe("conversation:unread-message",e)}createEntry(e,t,s,r,n){const a={key:e,content:t,type:s,sender:r||null,timestamp:n||n===null?n:Date.now()};this.messages.push(a),a.type==="agent"&&W(this.typingActors,r);const i=new H(c=>{if(c!==null){const{timestamp:h=void 0,...u}=c;a.content=u,h&&(a.timestamp=h)}else{const h=this.messages.indexOf(a);a.content=null,h>-1&&this.messages.splice(h,1)}this.events.dispatch("conversation:message-updated",{message:a}),this.update()});return this.events.dispatch("conversation:message-created",{message:a}),this.update(),this.componentMounted||this.events.dispatch("conversation:unread-message",!0),i}update(){this.events.dispatch("conversation:updated",{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors})}getMessageStream(){return{messages:this.messages,loading:this.loaders>0,typingActors:this.typingActors}}setTypingState(e,t){return this.typingActors.indexOf(e)===-1&&(this.typingActors.push(e),this.update()),()=>{W(this.typingActors,e),this.update()}}loader(){this.loaders=this.loaders+1;let e=!1;return this.loaders===1&&this.update(),()=>{e||(e=!0,this.loaders=this.loaders-1,this.loaders===0&&this.update())}}submitForm(e,t){this.formHandlers[e]={...this.formHandlers[e],submit:t}}validateForm(e,t){this.formHandlers[e]={...this.formHandlers[e],validate:t}}getHistory(){return this.conversationHistory.getConversationLogs()}}const F=(o,e)=>{const t=o.split("_contact");return e.widgets.get(t[0])},I=o=>{const e=o.get("$widget"),{name:t,implementation:s}=o.get("$widget"),r=t.indexOf("_contact")>-1?F(t,s):e,{data:n}=o.get("$settings"),{trigger:a}=r.container.get("$settings"),i=`${t.replace("_contact-method","").replace("_contact","")}_contact-method`,c=s.widgets.get(i);return c&&!a?{chatWidget:c,mainWidget:r,data:n}:{mainWidget:r,data:n}},G=o=>{const{chatWidget:e}=I(o);return!!e},R=(o,e,t)=>{const{mainWidget:s,data:r,chatWidget:n}=I(o);return n?n.state==="activated"?n.invoke("open").then(()=>{n.container.getAsync("conversation").then(a=>{a.navigateToConversationIfActive(e.id).then(()=>M.closeAllModals(o))})}):n.activate().then(()=>{const{trigger:a}=n.container.get("$settings");return a.action="hide",n.invoke("setSettings",()=>({data:r,trigger:a,activate:!0})).then(()=>n.container.getAsync("components").then(i=>{const c=i.components().ofType("contact-method");i.write({key:"setContactMethodId",attributes:{properties:{id:e.id,guideId:e.guideId}},target:c,provider:"localStorage"});const h=i.components().ofType("conversation");i.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:h,provider:"localStorage"})}))}).then(()=>n.invoke("attach",{key:"contact-method",triggerDOMElement:t,withRenderState:"open"})).then(()=>(p.createStorageWriter(n.container,"activeStickyWidget",p.StorageCategory.Necessary,{medium:p.StorageMedium.Local,scope:p.StorageScope.Implementation}).then(a=>a(n.name)),n.container.getAsync("conversation").then(a=>{a.setConversationEndedBehavior(S.Deactivate)}).then(()=>M.closeAllModals(o)).then(()=>!1))):(s.container.getAsync("conversation").then(a=>a.navigateToConversationIfActive(e.id).then(()=>a.setConversationEndedBehavior(S.Navigate))),!n)},J=(o,e,t="conversation")=>{const{mainWidget:s,chatWidget:r}=I(o);return r?r.container.getAsync("router").then(n=>{n.navigate(t,{id:e.id})}):s.container.getAsync("conversation").then(n=>{var a;(a=n.setConversationEndedBehavior(S.Navigate))==null||a.then(()=>{s.container.getAsync("router").then(i=>{s.container.getAsync("components").then(c=>{const h=c.components().ofType("conversation");c.write({key:"setConversationProvider",attributes:{properties:{providers:[e.clientName]}},target:h,provider:"localStorage"})}).then(()=>{i.navigate(t,{id:e.id})})})})})},b="conversations";var S=(o=>(o.Deactivate="deactivate",o.Navigate="navigate",o.None="none",o))(S||{});class w{constructor(e,t){d(this,"events");d(this,"controllers",[]);d(this,"providers",[]);d(this,"disposeProviders",{});d(this,"subscriptions",[]);d(this,"storageWriter",null);d(this,"router",null);d(this,"buttonQuery",null);d(this,"sessionInfo",null);this.container=e,this.components=t;const s=e.get("$widget");this.events=s.events.createChild(this)}initialize(){p.createStorageWriter(this.container,b,p.StorageCategory.Necessary,{medium:p.StorageMedium.Local}).then(e=>this.storageWriter=e),p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>{this.buttonQuery=this.components.components().ofType("conversation-return-button"),e&&(this.sessionInfo=e,e.id&&e.route&&this.writeToComponents({active:!0,alert:{show:!1}}))}),this.container.getAsync("router").then(e=>this.router=e),this.events.subscribe("conversation:ended",(e,t)=>{var s,r,n;switch((s=this.sessionInfo)==null?void 0:s.conversationEndedBehavior){case"deactivate":this.container.get("$widget").deactivate();break;case"navigate":const i=(r=this.router)==null?void 0:r.getInitialRoute();i&&((n=this.router)==null||n.navigate(i.name));break}this.writeToComponents({active:!1}),this.sessionInfo=null,this.storageWriter&&this.storageWriter()})}addSubscription(e){this.subscriptions.push(e)}static getInstance(e,t="conversation"){return E.lock(e)(()=>e.getAsync(t).then(s=>{let r=s;return r||D.ComponentPlatform.getInstance(e).then(n=>(r=new w(e,n),r.initialize(),e.registerAsync(t,()=>r),r))}))}navigateToConversationIfActive(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{var s;if(t&&t.id===e&&t.route){const{name:r,params:n}=t.route;(s=this.router)==null||s.navigate(r,n)}})}setConversationEndedBehavior(e){return p.readStorage(this.container,b,p.StorageMedium.Local).then(t=>{let s=t;return s?s.conversationEndedBehavior=e:s={conversationEndedBehavior:e},this.sessionInfo=s,this.storageWriter&&this.storageWriter(this.sessionInfo)||Promise.resolve()})}async createController(e,t){const s=O(e),{agents:r}=await p.readStorage(this.container,s)||{agents:[]},n=this.controllers.findIndex(i=>i.conversation.id===e);let a;return n>-1?a=this.controllers[n].conversation:a=new B(this.container,e,t),a.setInitialStorage({agents:r||[]}),a}registerProvider(e,t){const s={name:e,handler:t};this.providers.push(s),this.controllers.filter(r=>r.conversation.providers.indexOf(e)>-1).forEach(r=>{this.invokeHandler(s,r)})}registerController(e,t){const s=this.controllers.findIndex(n=>n.conversation.id===e.id);let r;if(s<0){r={conversation:e,component:t},this.controllers.push(r);const n=a=>{a&&this.providers.filter(i=>a.indexOf(i.name)>-1).forEach(i=>{this.invokeHandler(i,r)})};return this.subscriptions.push(e.onDispose((a,i)=>{const c=this.controllers.findIndex(h=>h.conversation===i);c>-1&&this.controllers.splice(c,1),this.components.components().ofType("conversation-return-button").select().writeProperties({active:!1}),this.events.dispatch("conversation:ended",{},{bubbles:!0})})),this.subscriptions.push(e.onProvidersChange((a,i)=>{const{added:c,removed:h=[]}=i;h.forEach(u=>{(this.disposeProviders[u]||[]).forEach(l=>{l()})}),n(c)})),this.subscriptions.push(e.onStarted((a,i)=>{var h,u;const c={id:e.id,route:(h=this.router)==null?void 0:h.getRouteData(),conversationEndedBehavior:(u=this.sessionInfo)==null?void 0:u.conversationEndedBehavior};this.sessionInfo=c,this.storageWriter&&this.storageWriter(c),this.writeToComponents({active:i})})),this.subscriptions.push(e.onUnreadMessage((a,i)=>{this.events.dispatch("conversation:unread-message",i),this.writeToComponents({alert:{show:i}})})),n(e.providers),!0}return r=this.controllers[s],!1}writeToComponents(e,t="conversationReturnButtonState"){this.buttonQuery&&this.components.write({key:"conversationReturnButtonState",attributes:{properties:e},target:this.buttonQuery,provider:"localStorage"})}invokeHandler(e,t){const s=e.handler(t.conversation.createProvider(e.name),t.component);s&&(this.disposeProviders[e.name]||(this.disposeProviders[e.name]=[]),this.disposeProviders[e.name].push(s))}getComponentRoute(){return p.readStorage(this.container,b,p.StorageMedium.Local).then(e=>e==null?void 0:e.route)}dispose(){Object.keys(this.disposeProviders).forEach(e=>{this.disposeProviders[e].forEach(t=>{t()})}),this.subscriptions.forEach(e=>{e()}),this.controllers=[],this.providers=[]}static async handleContactMethodClick(e,t,s,r){if(G(t)){e.preventDefault();let n=document.getElementById("humany-dynamic-anchor");n||(n=document.createElement("a"),n.setAttribute("href","#"),n.id="humany-dynamic-anchor",document.body.appendChild(n)),R(t,s,n)}R(t,s)}static async handleContactMethodSubmit(e,t){var s,r;return(r=(s=t==null?void 0:t.body.form.meta)==null?void 0:s.conversation)!=null&&r.id?x(e,t.body.form.meta.conversation.id,t.clientName):J(e,t)}}const V=(o,e)=>o.reduce((t,s,r)=>{var a,i;const n={...s,content:[{...s.content,key:s.key}]};if(t.length>0&&s.type!==y.System){const c=t[t.length-1];if((!e&&e!==0||Math.abs(c.timestamp-s.timestamp)<e)&&((a=c.sender)==null?void 0:a.name)===((i=s.sender)==null?void 0:i.name)&&c.type===s.type)return c.timestamp=s.timestamp,c.content.push({...s.content,key:s.key}),t}return t.push(n),t},[]),Y=o=>Promise.all([w.getInstance(o),o.getAsync("router")]).then(([e,t])=>N.createReactComponent(o,"conversation",Promise.resolve().then(()=>require("./conversation-56aaee96.js")),async s=>{const{rehydrate:r=!1}=s.properties(),a=t.getRouteData().params.id||s.node.name||"component",i=await e.createController(a,{rehydrate:r}),c=e.registerController(i,s),h=3*60*1e3,u=v=>{const{providers:g=[]}=v;if(g){const f=Array.isArray(g)?g:[g];return i.setProviders(f),f}};u(s.properties(u)),i.setComponentMountedState(!0);const l=v=>{const{loading:g,typingActors:f,messages:C}=v;s.writeProperties({loading:g,typingActors:f,messages:V(C,h)})};l(i.getMessageStream()),c&&(s.writeProperties({conversationControllerId:i.id}),s.actions.create("user-submit",(v,g)=>{if(g){const{preventDefault:f,validationPromise:C}=g;if(f){C&&C().then(T=>{T&&i.user.print(T)});return}}i.user.print(v.text)}),s.actions.create("user-typing",(v,g)=>{}),s.actions.create("form",(v,g)=>{}),s.actions.create("action",(v,g)=>{}),s.actions.create("overlay-mounted",(v,g)=>{}),s.actions.create("show-overlay",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}),s.actions.create("hide-overlay",(v,g)=>{s.writeProperties({overlay:null})}),s.actions.create("show-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!0,secondary:!1}})}),s.actions.create("hide-overlay-conversation",(v,g)=>{s.writeProperties({overlay:{conversation:!1,secondary:!1}})}));const m=i.events.subscribe("conversation:updated",(v,g)=>{l(g)});return()=>{i.setComponentMountedState(!1),m(),i.active||l({messages:[],typingActors:[],loading:!1})}},{reuseController:!0})),X=o=>Promise.all([D.ComponentPlatform.getInstance(o),w.getInstance(o),o.getAsync("router")]).then(([e,t,s])=>N.createReactComponent(o,"conversation-return-button",Promise.resolve().then(()=>require("./return-button-f1f43984.js")),r=>{r.actions.create("toggle",(n={})=>{var a;(a=t.getComponentRoute())==null||a.then(i=>{i&&s.navigate(i.name,i.params)})})}));var _=(o=>(o.LinkList="link-list",o.Separator="separator",o.ButtonList="button-list",o.ItemList="item-list",o.Text="text",o.HTML="html",o.VideoRequest="video-request",o))(_||{});const Z=(o,e,t)=>o.getAsync("componentResolver").then(async s=>{(await w.getInstance(o)).addSubscription(s.registerComponent(e,z.lazy(()=>t)))});exports.Agent=P;exports.ConversationComponent=Y;exports.ConversationController=B;exports.ConversationEndedBehavior=S;exports.ConversationEntry=H;exports.ConversationMessageListItemType=_;exports.ConversationMessageType=y;exports.ConversationPlatform=w;exports.ConversationProvider=A;exports.ConversationReturnButtonComponent=X;exports.registerCustomMessageComponent=Z;
|
package/index.mjs
CHANGED
|
@@ -939,7 +939,7 @@ const ee = (o, e) => o.reduce((t, s, r) => {
|
|
|
939
939
|
]).then(([e, t]) => _(
|
|
940
940
|
o,
|
|
941
941
|
"conversation",
|
|
942
|
-
import("./conversation-
|
|
942
|
+
import("./conversation-8fab4c79.mjs"),
|
|
943
943
|
async (s) => {
|
|
944
944
|
const { rehydrate: r = !1 } = s.properties(), a = t.getRouteData().params.id || s.node.name || "component", i = await e.createController(a, {
|
|
945
945
|
rehydrate: r
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telia-ace/widget-conversation",
|
|
3
|
-
"version": "1.1.2-rc.
|
|
3
|
+
"version": "1.1.2-rc.15",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"types": "./index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -10,13 +10,13 @@
|
|
|
10
10
|
}
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@telia-ace/widget-core": "^1.1.2-rc.
|
|
14
|
-
"@telia-ace/widget-forms": "^1.1.2-rc.
|
|
15
|
-
"@telia-ace/widget-plugins": "^1.1.2-rc.
|
|
16
|
-
"@telia-ace/widget-routing": "^1.1.2-rc.
|
|
17
|
-
"@telia-ace/widget-services": "^1.1.2-rc.
|
|
18
|
-
"@telia-ace/widget-ui": "^1.1.2-rc.
|
|
19
|
-
"@telia-ace/widget-utilities": "^1.1.2-rc.
|
|
13
|
+
"@telia-ace/widget-core": "^1.1.2-rc.15",
|
|
14
|
+
"@telia-ace/widget-forms": "^1.1.2-rc.15",
|
|
15
|
+
"@telia-ace/widget-plugins": "^1.1.2-rc.15",
|
|
16
|
+
"@telia-ace/widget-routing": "^1.1.2-rc.15",
|
|
17
|
+
"@telia-ace/widget-services": "^1.1.2-rc.15",
|
|
18
|
+
"@telia-ace/widget-ui": "^1.1.2-rc.15",
|
|
19
|
+
"@telia-ace/widget-utilities": "^1.1.2-rc.15",
|
|
20
20
|
"@webprovisions/platform": "^1.1.4"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|