@telia-ace/knowledge-widget-bot-provider 1.1.8 → 1.2.0-rc.1

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.
@@ -6,7 +6,7 @@ import { useContainer as N, useDispatch as R, useConversation as T } from "@teli
6
6
  import { FormBuilder as _ } from "@telia-ace/widget-forms";
7
7
  import { appendClassNames as j } from "@telia-ace/widget-utilities";
8
8
  import { useState as G, useRef as I, useEffect as k, useCallback as V } from "react";
9
- import { u as S } from "./index-2593adbc.mjs";
9
+ import { u as S } from "./index-cc087f12.mjs";
10
10
  import "@telia-ace/widget-conversation";
11
11
  import "@telia-ace/widget-services";
12
12
  const tt = ({
@@ -1,3 +1,3 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("react/jsx-runtime"),A=require("@emotion/styled"),S=require("@telia-ace/knowledge-data-client"),R=require("@telia-ace/knowledge-widget-adapters"),p=require("@telia-ace/knowledge-widget-ui"),T=require("@telia-ace/widget-forms"),$=require("@telia-ace/widget-utilities"),c=require("react"),F=require("./index-1a47fa98.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");const z=({contactMethodId:n,guideId:o,groupId:m,className:x})=>{var C,v,w;const[e,s]=c.useState(),d=p.useContainer(),D=p.useDispatch(),l=c.useRef(!0),b=p.useConversation();c.useEffect(()=>{const{events:u}=d.get("$widget"),q=u.subscribe("bot-provider:form-submitted",async(f,r)=>{var M;if(((M=r.form.meta)==null?void 0:M.groupId)!==m||r.id!==e.id)return;b&&(r.form.meta||(r.form.meta={}),r.form.meta.conversation=b);const y=await d.getAsync("contactMethods"),t={...e,body:{...e.body,form:r.form}},{errors:i,valid:h,errorTypes:N}=await y.validate(t);if(!h){const k=new T.FormBuilder(e.body.form);s(void 0),k.setValidationErrors(i),t.body={...t.body,form:k.get()},s({...t});return}const E=await y.submit(t);t.confirmation=E,s(t)}),a=u.subscribe("bot-provider:evaluate",async(f,r)=>{if(r.id!==n)return;const t=await(await d.getAsync("contactMethods")).evaluate(e);let i={...e,body:{...e.body,form:t}};const h=F.updateForm(i,{submit:"Skicka",errors:{},meta:{groupId:m}});i={...i,body:h},s(i)});return()=>{q(),a()}},[d,e,s,b]);const g=c.useCallback(async u=>{const a=await(await S.DataClient.getInstance(d)).fetch(S.DataType.ContactMethod,{contactMethodId:u,guideId:o},{noCache:!0}),f=F.updateForm(a,{submit:"Skicka",errors:{},meta:{groupId:m}});if(a.body=f,a.body.guideId=o,!l.current)return null;s(a)},[l]);return c.useEffect(()=>(g(n),()=>{l.current=!1}),[n,g]),e?j.jsx(B,{...e,body:{...e.body,form:e.body.form?{...e.body.form,data:(C=e.body.form)==null?void 0:C.data,layout:{...e.body.form.layout},meta:(v=e.body.form)==null?void 0:v.meta,schema:(w=e.body.form)==null?void 0:w.schema}:void 0,guideId:e.body.guideId},inline:!0,expanded:!0,dispatch:D,className:$.appendClassNames("humany-bot-contact-method",x)}):null},B=A(R.ContactMethod)`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("react/jsx-runtime"),A=require("@emotion/styled"),S=require("@telia-ace/knowledge-data-client"),R=require("@telia-ace/knowledge-widget-adapters"),p=require("@telia-ace/knowledge-widget-ui"),T=require("@telia-ace/widget-forms"),$=require("@telia-ace/widget-utilities"),c=require("react"),F=require("./index-cb940de4.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");const z=({contactMethodId:n,guideId:o,groupId:m,className:x})=>{var C,v,w;const[e,s]=c.useState(),d=p.useContainer(),D=p.useDispatch(),l=c.useRef(!0),b=p.useConversation();c.useEffect(()=>{const{events:u}=d.get("$widget"),q=u.subscribe("bot-provider:form-submitted",async(f,r)=>{var M;if(((M=r.form.meta)==null?void 0:M.groupId)!==m||r.id!==e.id)return;b&&(r.form.meta||(r.form.meta={}),r.form.meta.conversation=b);const y=await d.getAsync("contactMethods"),t={...e,body:{...e.body,form:r.form}},{errors:i,valid:h,errorTypes:N}=await y.validate(t);if(!h){const k=new T.FormBuilder(e.body.form);s(void 0),k.setValidationErrors(i),t.body={...t.body,form:k.get()},s({...t});return}const E=await y.submit(t);t.confirmation=E,s(t)}),a=u.subscribe("bot-provider:evaluate",async(f,r)=>{if(r.id!==n)return;const t=await(await d.getAsync("contactMethods")).evaluate(e);let i={...e,body:{...e.body,form:t}};const h=F.updateForm(i,{submit:"Skicka",errors:{},meta:{groupId:m}});i={...i,body:h},s(i)});return()=>{q(),a()}},[d,e,s,b]);const g=c.useCallback(async u=>{const a=await(await S.DataClient.getInstance(d)).fetch(S.DataType.ContactMethod,{contactMethodId:u,guideId:o},{noCache:!0}),f=F.updateForm(a,{submit:"Skicka",errors:{},meta:{groupId:m}});if(a.body=f,a.body.guideId=o,!l.current)return null;s(a)},[l]);return c.useEffect(()=>(g(n),()=>{l.current=!1}),[n,g]),e?j.jsx(B,{...e,body:{...e.body,form:e.body.form?{...e.body.form,data:(C=e.body.form)==null?void 0:C.data,layout:{...e.body.form.layout},meta:(v=e.body.form)==null?void 0:v.meta,schema:(w=e.body.form)==null?void 0:w.schema}:void 0,guideId:e.body.guideId},inline:!0,expanded:!0,dispatch:D,className:$.appendClassNames("humany-bot-contact-method",x)}):null},B=A(R.ContactMethod)`
2
2
  padding: ${n=>{var o;return(o=n.theme.sizes)==null?void 0:o.normal}} ${n=>{var o;return(o=n.theme.sizes)==null?void 0:o.medium}};
3
3
  `;exports.default=z;
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),u=require("@emotion/styled"),y=require("@telia-ace/knowledge-widget-adapters"),g=require("@telia-ace/knowledge-widget-ui"),l=require("react"),b=require("./contact-method-29b72b18.js");require("@telia-ace/knowledge-data-client");require("@telia-ace/widget-forms");require("@telia-ace/widget-utilities");require("./index-1a47fa98.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");const j=({actions:e,entryId:t,groupId:x})=>{const q=g.useDispatch(),d=g.useContainer(),[h,C]=l.useState([]),[o,M]=l.useState([]);return l.useEffect(()=>{d.getAsync("contactMethods").then(n=>{if(!n)return;const c=e.map(async i=>{var m;if(!((m=i.data)!=null&&m.clientName))return null;const s=await n.handleAdapters(i.data);return{...s,...i.data,description:i.data.description?i.data.description:s.description,onClick:(p,r)=>{if(typeof s.onClick=="function"&&s.onClick(p,r),!p.defaultPrevented)if(r.inline&&!r.expanded)M([...o,r.id]);else{const{events:f}=d.get("$widget");f.dispatch("provider:open-contact-method",{entryId:t,contactMethod:r,actionKey:i.actionKey}),f.dispatch("tracking:contact-method-list-item-clicked",r)}}}});Promise.all(c).then(i=>{C(i.filter(s=>!!s))})})},[e,d,t,o]),h.length?a.jsx(k,{className:"humany-bot-contact-list",contactMethods:h,dispatch:q,itemRenderer:(n,c)=>(o.includes(n.id)||n.expanded)&&n.inline?a.jsx(S,{contactMethodId:n.id,groupId:x,guideId:n.body.guideId}):a.jsx($,{...n,...c})}):null},k=u(y.ContactList)`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),u=require("@emotion/styled"),y=require("@telia-ace/knowledge-widget-adapters"),g=require("@telia-ace/knowledge-widget-ui"),l=require("react"),b=require("./contact-method-d88ce2cc.js");require("@telia-ace/knowledge-data-client");require("@telia-ace/widget-forms");require("@telia-ace/widget-utilities");require("./index-cb940de4.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");const j=({actions:e,entryId:t,groupId:x})=>{const q=g.useDispatch(),d=g.useContainer(),[h,C]=l.useState([]),[o,M]=l.useState([]);return l.useEffect(()=>{d.getAsync("contactMethods").then(n=>{if(!n)return;const c=e.map(async i=>{var m;if(!((m=i.data)!=null&&m.clientName))return null;const s=await n.handleAdapters(i.data);return{...s,...i.data,description:i.data.description?i.data.description:s.description,onClick:(p,r)=>{if(typeof s.onClick=="function"&&s.onClick(p,r),!p.defaultPrevented)if(r.inline&&!r.expanded)M([...o,r.id]);else{const{events:f}=d.get("$widget");f.dispatch("provider:open-contact-method",{entryId:t,contactMethod:r,actionKey:i.actionKey}),f.dispatch("tracking:contact-method-list-item-clicked",r)}}}});Promise.all(c).then(i=>{C(i.filter(s=>!!s))})})},[e,d,t,o]),h.length?a.jsx(k,{className:"humany-bot-contact-list",contactMethods:h,dispatch:q,itemRenderer:(n,c)=>(o.includes(n.id)||n.expanded)&&n.inline?a.jsx(S,{contactMethodId:n.id,groupId:x,guideId:n.body.guideId}):a.jsx($,{...n,...c})}):null},k=u(y.ContactList)`
2
2
  > ul > li {
3
3
  a {
4
4
  display: flex;
@@ -3,11 +3,11 @@ import m from "@emotion/styled";
3
3
  import { ContactList as M, ContactMethod as $ } from "@telia-ace/knowledge-widget-adapters";
4
4
  import { useDispatch as b, useContainer as k } from "@telia-ace/knowledge-widget-ui";
5
5
  import { useState as f, useEffect as E } from "react";
6
- import S from "./contact-method-adeea373.mjs";
6
+ import S from "./contact-method-b5669c64.mjs";
7
7
  import "@telia-ace/knowledge-data-client";
8
8
  import "@telia-ace/widget-forms";
9
9
  import "@telia-ace/widget-utilities";
10
- import "./index-2593adbc.mjs";
10
+ import "./index-cc087f12.mjs";
11
11
  import "@telia-ace/widget-conversation";
12
12
  import "@telia-ace/widget-services";
13
13
  const F = ({ actions: t, entryId: e, groupId: y }) => {
@@ -0,0 +1 @@
1
+ "use strict";var H=Object.defineProperty;var R=(n,t,e)=>t in n?H(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var y=(n,t,e)=>(R(n,typeof t!="symbol"?t+"":t,e),e);const m=require("@telia-ace/widget-conversation"),N=require("@telia-ace/widget-services"),K=require("@telia-ace/widget-utilities"),P=require("@telia-ace/widget-forms");var f=(n=>(n.ContactMethod="ace-knowledge-bot-contact-method",n.ContactList="ace-knowledge-bot-contact-list",n.FeedbackList="ace-knowledge-bot-feedback-list",n.NotificationList="ace-knowledge-bot-notification-list",n.NoGoodAlternative="ace-knowledge-bot-no-good-alternative",n))(f||{});const $=n=>{if(n){const{type:t,content:e}=n;if(t&&e)return{type:t,content:e}}},A=({isUser:n,items:t=[],meta:e={}})=>{const s=K.uuid();return{id:s,isUser:n,items:t.map(a=>({id:a.id||K.uuid(),isUser:n,groupId:s,message:a.message||[]})),meta:e}},U=({id:n,title:t,defaultIcon:e,exits:[s],exitType:i,confirmationText:a,expandFormAutomatically:c,form:g,description:E},S={})=>{const{adapter:r}=s||{Adapter:{}},{clientName:o="",settings:h={},customClientName:v=""}=r,{validation:l}=S,d={id:n.toString(),title:t,description:E,symbol:$(e),clientName:o==="custom.adapter"?v:o,expanded:!0,inline:!0,body:{}};if(o.indexOf("phone")>-1||o.indexOf("freetext")>-1){const{displayTextBeforeClick:M,textBeforeClick:p}=h;M==="true"&&(d.expanded=!1,d.title=p),o.indexOf("phone.text")>-1?d.body.phoneNumber=h.phoneNumber:d.body.freetext=h.freetext}return(o.indexOf("chat.popup")>-1||o.indexOf("link")>-1||o==="custom.adapter"&&i==="custom")&&(d.body=h,d.body.confirmationFallback=a),o.indexOf("ace")>-1&&(d.body=h),g&&g.hasValueComponents&&(l?d.body.form=P.FormBuilder.fromLegacyForm(n.toString(),g,l).get():d.body.form=P.FormBuilder.fromLegacyForm(n.toString(),g).get(),c||(d.expanded=d.inline=!1),d.body=B(d,{submit:"Submit"})),S.guideId&&(d.body.guideId=S.guideId),d},G=n=>{const{localState:t}=n;return n.tags.includes("category")&&n.tags.includes("contact")?m.ConversationMessageListItemType.ItemList:n.tags.includes("suggestion")||(t==null?void 0:t.$type)==="Humany.Matching.Bots.Conversations.GuideListState, Humany.Matching"?m.ConversationMessageListItemType.LinkList:(t==null?void 0:t.$type)==="Humany.Matching.Bots.Conversations.States.GuideFeedbackState, Humany.Matching"?f.FeedbackList:(t==null?void 0:t.$type)==="Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching"?f.ContactList:m.ConversationMessageListItemType.ButtonList},q=(n,t)=>{const{knowledgeBotEndpoint:e}=t.properties();if(e)return`${e}/conversations`;const{data:s}=n.get("$settings");if(!s||!s.projection)throw new Error("Missing Knowledge Bot endpoint");return`${s.projection}/conversations`},B=(n,{submit:t,errors:e,meta:s})=>{var i;if((i=n==null?void 0:n.body)!=null&&i.form){const{body:a}=n,c=new P.FormBuilder(a.form);return a.form.layout.submit||c.createComponent(P.submitButton(t,"submit")),e&&c.setValidationErrors(e),s&&c.setMeta(s),{...a,form:c.get()}}return(n==null?void 0:n.body)||{}},O=async(n,t,e)=>{(await(await n.getAsync("dataClient")).getClient()).parameters.set(t,e)},b=({messageId:n,message:t,group:e})=>({id:n,message:t,groupId:e.id,isUser:e.isUser}),V=async(n,t,e)=>{const s=(r,o,h,v={actions:[],entryId:o.id,groupId:h})=>{const l=[];return v.actions=o.content.items.map(d=>{var p;const M={label:d.title,actionKey:d.actionKey,symbol:d.symbol?d.symbol:void 0,description:d.description||void 0};if(d.data){const w=o.actions[d.actionKey];r===f.ContactList?(l.push(d.data.id),M.data=U(d.data,{guideId:(p=w==null?void 0:w.guideId)==null?void 0:p.toString()})):M.data=d.data}return M}),r===f.ContactList&&(v.contactMethodIds=l),v},i=async(r,o,h=[],v)=>{var d,M;const l=K.uuid();switch(r.type!=="List"&&r.tags.includes("guide")&&((M=(d=r.content.hints)==null?void 0:d[0])!=null&&M.id)&&await O(n,"LastGuideId",r.content.hints[0].id),r.type){case"List":{const{header:p,items:w,html:x}=r.content,C=[];let I={actions:[],entryId:r.id,groupId:o.id};if(I.tags=r.tags||null,p&&(I.header=p),(w==null?void 0:w.length)>0){const u=G(r);if(I=s(u,r,o.id,I),h.length){const L=h.find(k=>k.tags.includes("paging")),T=h.find(k=>k.tags.includes("fallback"));if(L!=null&&L.content&&(o.meta.paging={actions:L.actions,items:L.content.items,localState:L.localState},L.content.items.forEach(k=>{I.actions.push({label:k.title,actionKey:k.actionKey,data:{groupId:v?v.id:o.id}})})),T){const k=T.content.items.find(F=>F.actionKey==="help");k&&(C.push(b({messageId:l,message:[m.ConversationMessageListItemType.Separator,void 0],group:o})),C.push(b({messageId:l,message:[f.NoGoodAlternative,{body:k.title}],group:o})))}}C.unshift(b({messageId:l,message:[u,I],group:o}))}return x&&C.unshift(b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:x}],group:o})),C}case"Text":{const p=[];if(p.push(b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:r.content.html}],group:o})),h.length){const w=h.find(u=>{var L;return u.type==="List"&&((L=u.localState)==null?void 0:L.$type)==="Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching"}),x=h.find(u=>u.tags.includes("dialog")),C=h.find(u=>u.tags.includes("feedback")),I=h.find(u=>u.tags.includes("contact")&&u.type==="Contact");if(I&&p.push(b({messageId:l,message:[f.ContactMethod,{contactMethodId:I.content.id,groupId:o.id,guideId:I.content.guideId}],group:o})),x&&p.push(b({messageId:l,message:[m.ConversationMessageListItemType.ButtonList,{actions:x.content.items.map(u=>({label:u.title,actionKey:u.actionKey,data:u.data}))}],group:o})),w){const u=s(f.ContactList,w,o.id);p.push(b({messageId:l,message:[m.ConversationMessageListItemType.Separator,void 0],group:o})),p.push(b({messageId:l,message:[f.ContactList,u],group:o}))}C&&(C.content.html&&p.push(b({messageId:l,group:o,message:[m.ConversationMessageListItemType.HTML,{body:C.content.html}]})),p.push(b({messageId:l,group:o,message:[f.FeedbackList,{actions:C.content.items.map(u=>({actionKey:u.actionKey,label:u.title}))}]})))}return p}case"Contact":return[b({messageId:l,message:[f.ContactMethod,{contactMethodId:r.content.id,groupId:o.id,guideId:r.content.guideId}],group:o})];case"CategorizedNoticeList":{const p=[b({messageId:l,message:[f.NotificationList,{id:r.content.item.id.toString(),header:r.content.item.title,items:r.content.item.listNotices,symbol:r.content.item.defaultIcon}],group:o})];return(r.content.notices||[]).forEach(w=>{p.push(b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:w.body}],group:o}))}),p}case"footerNotice":return[b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:r.content.item.body}],group:o})];case"Error":return[b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:r.content.message||"Something went wrong."}],group:o})];default:return[]}},a=r=>["CategorizedNoticeList","NoticeList"].includes(r.type)?(r.content.items||[]).reduce((o,h)=>(r.content.isInitialNotices&&o.push({...r,id:K.uuid(),type:h.type||r.type,content:{$type:r.content.$type,isInitialNotices:r.content.isInitialNotices,notices:h.notices,item:h}}),o),[]):r,c=new Map([["Text",1],["List",2],["Contact",3],["CategorizedNoticeList",10]]),g=[],E=t.sort((r,o)=>{const v=c.get(r.type)||20,l=c.get(o.type)||20;return v-l}).filter(r=>!r.relation).reduce((r,o)=>r.concat(a(o)),[]),S=r=>typeof r.actions.selector<"u";for(const r of E){const o=t.filter(l=>l.relation===r.id&&!S(l)),h=A({isUser:!1});(await i(r,h,o,e)).forEach(l=>{h.items.push(l)}),h.items.length&&g.push(h)}return g};class j{constructor(t,e,s,i){y(this,"globalState");y(this,"localState");y(this,"clientId");y(this,"entries");this.endpoint=t,this.events=e,this.store=s,this.container=i,this.clientId=K.uuid(),this.entries=[]}async start({silent:t}){const e=await this.store.read("globalState"),s=await this.store.read("localState"),i=await this.store.read("entries");if(this.globalState=e||this.globalState,this.localState=s||this.localState,this.entries=i||this.entries,t)return;const a=this.createInitialEnvelope();return await this.postEnvelope(a)}async sendMessage(t){const e={command:{phrase:t},globalState:this.globalState,clientId:this.clientId};return await this.postEnvelope(e)}async helpAction(){return this.postEnvelope({action:{key:"help",content:{$type:"Humany.Matching.Bots.Conversations.Actions.Help, Humany.Matching"}},globalState:this.globalState,clientId:this.clientId})}async nextAction(t){var s;const{paging:e}=t.meta;if((s=e.actions)!=null&&s.next)return this.postEnvelope({action:{key:"next",content:e.actions.next},localState:e.localState,globalState:this.globalState,clientId:this.clientId},t).then(i=>{const a=t.items.findIndex(c=>c.message[0]==="link-list");return a===-1||!i?t:{...t,meta:{...t.meta,...i[0].meta},items:[...t.items.slice(0,a),{...t.items[a],message:[i[0].items[0].message[0],{...t.items[a].message[1],...i[0].items[0].message[1]}]},...t.items.slice(a+1)]}})}async showContactMethodAction(t){const e=this.entries.find(i=>i.id===t.entryId),s=e==null?void 0:e.actions[t.actionKey];if(s)return this.postEnvelope({action:{key:t.actionKey,content:s},localState:e.localState,globalState:this.globalState,clientId:this.clientId})}async defaultAction(t){const e=this.getEntryOfAction(t);if(e)return this.postEnvelope({action:{key:t,content:e.actions[t]},localState:e.localState,globalState:this.globalState,clientId:this.clientId})}createInitialEnvelope(){return{command:{site:this.endpoint},clientId:this.clientId,globalState:{},funnel:this.container.get("$widget").name}}async postEnvelope(t,e){try{const s=await fetch(this.endpoint,this.getHttpOptions(t));if(!s.ok)throw s;const i=await s.json();return this.handleResponse(i,e)}catch(s){process.env.NODE_ENV!=="production"&&console.warn("Something went wrong when posting the envelope.");let i="Something went wrong";return(s.message==="Failed to fetch"||s.message==="NetworkError when attempting to fetch resource.")&&(i="Lost connection... Please try again."),this.events.dispatch("tracking:http-client-error",{message:i,error:s}),this.handleResponse({entries:[{id:K.uuid(),type:"Error",timestamp:new Date().toISOString(),content:{message:i},actions:{},tags:[]}]})}}async handleResponse(t,e){this.entries=this.entries.concat(t.entries),this.store.write(this.entries,"entries");const s=t.entries.find(a=>a.type==="Widget");return s&&this.events.dispatch("provider:widget-entry",s.content),t.globalState&&(this.globalState=t.globalState,this.store.write(t.globalState,"globalState")),this.localState=t.entries.reduce((a,c)=>c.localState?c.localState:a,void 0),this.store.write(this.localState,"localState"),await V(this.container,t.entries,e)}getHttpOptions(t){const e={method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"}};return t.funnel=this.container.get("$widget").name,t.localState=t.localState||this.localState,Object.assign({},e,{body:JSON.stringify(t),credentials:"same-origin"})}getEntryOfAction(t){return this.entries.find(e=>!!e.actions[t])}}class z{constructor(t){y(this,"enabled",!0);y(this,"botProviderGlobalStateKey","bot-provider-global-state");y(this,"botProviderLocalStateKey","bot-provider-local-state");y(this,"botProviderEntriesKey","bot-provider-entries");y(this,"botProviderMetaStateKey","bot-provider-conversation-meta");this.container=t}getKey(t){let e;switch(t){case"localState":e=this.botProviderLocalStateKey;break;case"globalState":e=this.botProviderGlobalStateKey;break;case"entries":e=this.botProviderEntriesKey;break;case"meta":e=this.botProviderMetaStateKey;break}return e}async read(t){const e=this.getKey(t);if(!(!this.enabled||!e))return N.readStorage(this.container,e)}async write(t,e){const s=this.getKey(e);if(!this.enabled||!s)return;(await N.createStorageWriter(this.container,s,N.StorageCategory.Necessary))(t)}}class W{constructor(t,e,s){y(this,"agent");y(this,"user");y(this,"httpClient");y(this,"events");y(this,"groups",[]);y(this,"store");this.container=t,this.conversation=e;const{events:i}=this.container.get("$widget");this.events=i,this.store=new z(t),this.agent=e.createAgent({id:"knowledge-bot-agent",name:"",avatar:void 0}),this.user=e.user;const a=q(t,s);this.httpClient=new j(a,i,this.store,this.container),this.events.subscribe("provider:widget-entry",(c,g)=>{this.onWidgetEntryReceived(g)}),this.events.subscribe("provider:open-contact-method",async(c,g)=>{const E=await this.httpClient.showContactMethodAction(g);E&&(g.contactMethod.title&&this.printUserMessage(g.contactMethod.title),this.onMessagesReceived(E))})}async initialize(){var a;const t=this.conversation.loading(),e=await this.store.read("meta");e!=null&&e.agent&&this.agent.update({name:e.agent.name,avatar:e.agent.avatar});const s=await this.conversation.isRehydrated(),i=await this.httpClient.start({silent:((a=this.conversation.options)==null?void 0:a.silent)||s});i&&this.onMessagesReceived(i),t()}async onMessagesReceived(t){this.groups=this.groups.concat(t),t.forEach(e=>{const s=e.items.map(i=>i.message);e.isUser?e.writer=this.user.print(s):e.writer=this.agent.print(s)})}async onInput(t){const e=typeof t=="string"?t:t.actionKey;switch(e){case"next":{if(typeof t=="string")return;const s=this.groups.find(i=>{var a;return i.id===((a=t.data)==null?void 0:a.groupId)});if(s){const i=await this.httpClient.nextAction(s);if(i){const a=i.items.map(c=>c.message);s.meta.paging=i==null?void 0:i.meta.paging,s.writer.update(a)}}}break;case"help":{const s=this.conversation.loading(),i=await this.httpClient.helpAction();s(),i&&this.onMessagesReceived(i)}break;default:{typeof t!="string"&&t.label&&this.printUserMessage(t.label);const s=this.conversation.loading(),i=await this.httpClient.defaultAction(e);i&&this.onMessagesReceived(i),s()}break}}printUserMessage(t){const e=A({isUser:!0,items:[{id:K.uuid(),isUser:!0,message:[m.ConversationMessageListItemType.HTML,{body:t}]}]});return this.onMessagesReceived([e]),e}async onMessageSend(t){const e=this.conversation.loading(),s=await this.httpClient.sendMessage(t);await O(this.container,"phrase",t),this.printUserMessage(t),s&&this.onMessagesReceived(s),e()}async onWidgetEntryReceived(t){var r,o,h,v,l;if(t.agent){const d={name:t.agent.name,avatar:t.agent.avatar};this.agent.update(d),(await N.createStorageWriter(this.container,"bot-provider-conversation-meta",N.StorageCategory.Necessary))({agent:d})}const s=(await this.container.getAsync("components")).components().ofType("widget-header").select();if(!s)return;const i=await this.container.get("settings"),a=i==null?void 0:i.components["widget-header"],c=(o=(r=a==null?void 0:a.properties)==null?void 0:r.actions)==null?void 0:o.avatar,g=(h=a==null?void 0:a.properties)==null?void 0:h.header,E=(v=a==null?void 0:a.properties)==null?void 0:v.tagline,S={};!g&&(t!=null&&t.heading)&&(S.header=t.heading),!E&&(t!=null&&t.tagline)&&(S.tagline=t.tagline),!c&&((l=t==null?void 0:t.agent)!=null&&l.avatar)&&(S.actions={avatar:{position:"left",symbol:{type:"Uri",content:t.agent.avatar}}}),Object.keys(S).length>0&&s.writeProperties(S)}}const _=async n=>{const t=await m.ConversationPlatform.getInstance(n);m.registerCustomMessageComponent(n,f.ContactList,Promise.resolve().then(()=>require("./contact-method-list-6b716469.js"))),m.registerCustomMessageComponent(n,f.ContactMethod,Promise.resolve().then(()=>require("./contact-method-d88ce2cc.js"))),m.registerCustomMessageComponent(n,f.NotificationList,Promise.resolve().then(()=>require("./notification-list-d4d9695d.js"))),m.registerCustomMessageComponent(n,f.NoGoodAlternative,Promise.resolve().then(()=>require("./no-good-alternative-0ced8ab9.js"))),m.registerCustomMessageComponent(n,f.FeedbackList,Promise.resolve().then(()=>require("./feedback-list-48c7bffb.js"))),t.registerProvider("ace-knowledge-bot",(e,s)=>{const i=new W(n,e,s);i.initialize();const a=[];return a.push(s.actions.watch("conversation.user-submit",(c,g)=>{i.onMessageSend(c.text)})),a.push(s.actions.watch("conversation.action",(c,g)=>(i.onInput(c),g(c)))),a.push(s.actions.create("evaluate",c=>{const{events:g}=n.get("$widget");g.dispatch("bot-provider:evaluate",c)})),a.push(s.actions.create("submit",c=>{const{events:g}=n.get("$widget");g.dispatch("bot-provider:form-submitted",c)})),()=>{a.forEach(c=>{c()})}})};exports.KnowledgeBotProviderPlugin=_;exports.updateForm=B;
@@ -1,31 +1,31 @@
1
1
  var F = Object.defineProperty;
2
- var G = (s, t, e) => t in s ? F(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
- var f = (s, t, e) => (G(s, typeof t != "symbol" ? t + "" : t, e), e);
4
- import { ConversationMessageListItemType as y, ConversationPlatform as T, registerCustomMessageComponent as N } from "@telia-ace/widget-conversation";
5
- import { readStorage as U, createStorageWriter as O, StorageCategory as B } from "@telia-ace/widget-services";
6
- import { uuid as E } from "@telia-ace/widget-utilities";
7
- import { FormBuilder as H, submitButton as V } from "@telia-ace/widget-forms";
8
- var p = /* @__PURE__ */ ((s) => (s.ContactMethod = "ace-knowledge-bot-contact-method", s.ContactList = "ace-knowledge-bot-contact-list", s.FeedbackList = "ace-knowledge-bot-feedback-list", s.NotificationList = "ace-knowledge-bot-notification-list", s.NoGoodAlternative = "ace-knowledge-bot-no-good-alternative", s))(p || {});
2
+ var T = (s, t, e) => t in s ? F(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
+ var f = (s, t, e) => (T(s, typeof t != "symbol" ? t + "" : t, e), e);
4
+ import { ConversationMessageListItemType as v, ConversationPlatform as G, registerCustomMessageComponent as N } from "@telia-ace/widget-conversation";
5
+ import { readStorage as U, createStorageWriter as P, StorageCategory as B } from "@telia-ace/widget-services";
6
+ import { uuid as K } from "@telia-ace/widget-utilities";
7
+ import { FormBuilder as A, submitButton as V } from "@telia-ace/widget-forms";
8
+ var m = /* @__PURE__ */ ((s) => (s.ContactMethod = "ace-knowledge-bot-contact-method", s.ContactList = "ace-knowledge-bot-contact-list", s.FeedbackList = "ace-knowledge-bot-feedback-list", s.NotificationList = "ace-knowledge-bot-notification-list", s.NoGoodAlternative = "ace-knowledge-bot-no-good-alternative", s))(m || {});
9
9
  const j = (s) => {
10
10
  if (s) {
11
11
  const { type: t, content: e } = s;
12
12
  if (t && e)
13
13
  return { type: t, content: e };
14
14
  }
15
- }, P = ({
15
+ }, H = ({
16
16
  isUser: s,
17
17
  items: t = [],
18
18
  meta: e = {}
19
19
  }) => {
20
- const i = E();
20
+ const n = K();
21
21
  return {
22
- id: i,
22
+ id: n,
23
23
  isUser: s,
24
- items: t.map((c) => ({
25
- id: c.id || E(),
24
+ items: t.map((a) => ({
25
+ id: a.id || K(),
26
26
  isUser: s,
27
- groupId: i,
28
- message: c.message || []
27
+ groupId: n,
28
+ message: a.message || []
29
29
  })),
30
30
  meta: e
31
31
  };
@@ -33,60 +33,60 @@ const j = (s) => {
33
33
  id: s,
34
34
  title: t,
35
35
  defaultIcon: e,
36
- exits: [i],
37
- exitType: n,
38
- confirmationText: c,
36
+ exits: [n],
37
+ exitType: i,
38
+ confirmationText: a,
39
39
  expandFormAutomatically: r,
40
- form: u,
40
+ form: g,
41
41
  description: M
42
- }, K = {}) => {
43
- const { adapter: a } = i || { Adapter: {} }, {
42
+ }, w = {}) => {
43
+ const { adapter: c } = n || { Adapter: {} }, {
44
44
  clientName: o = "",
45
45
  settings: h = {},
46
- customClientName: S = ""
47
- } = a, { validation: l } = K, d = {
46
+ customClientName: y = ""
47
+ } = c, { validation: l } = w, d = {
48
48
  id: s.toString(),
49
49
  title: t,
50
50
  description: M,
51
51
  symbol: j(e),
52
- clientName: o === "custom.adapter" ? S : o,
52
+ clientName: o === "custom.adapter" ? y : o,
53
53
  expanded: !0,
54
54
  inline: !0,
55
55
  body: {}
56
56
  };
57
57
  if (o.indexOf("phone") > -1 || o.indexOf("freetext") > -1) {
58
- const { displayTextBeforeClick: k, textBeforeClick: m } = h;
59
- k === "true" && (d.expanded = !1, d.title = m), o.indexOf("phone.text") > -1 ? d.body.phoneNumber = h.phoneNumber : d.body.freetext = h.freetext;
58
+ const { displayTextBeforeClick: k, textBeforeClick: u } = h;
59
+ k === "true" && (d.expanded = !1, d.title = u), o.indexOf("phone.text") > -1 ? d.body.phoneNumber = h.phoneNumber : d.body.freetext = h.freetext;
60
60
  }
61
- return (o.indexOf("chat.popup") > -1 || o.indexOf("link") > -1 || o === "custom.adapter" && n === "custom") && (d.body = h, d.body.confirmationFallback = c), o.indexOf("ace") > -1 && (d.body = h), u && u.hasValueComponents && (l ? d.body.form = H.fromLegacyForm(
61
+ return (o.indexOf("chat.popup") > -1 || o.indexOf("link") > -1 || o === "custom.adapter" && i === "custom") && (d.body = h, d.body.confirmationFallback = a), o.indexOf("ace") > -1 && (d.body = h), g && g.hasValueComponents && (l ? d.body.form = A.fromLegacyForm(
62
62
  s.toString(),
63
- u,
63
+ g,
64
64
  l
65
- ).get() : d.body.form = H.fromLegacyForm(
65
+ ).get() : d.body.form = A.fromLegacyForm(
66
66
  s.toString(),
67
- u
68
- ).get(), r || (d.expanded = d.inline = !1), d.body = D(d, { submit: "Submit" })), K.guideId && (d.body.guideId = K.guideId), d;
67
+ g
68
+ ).get(), r || (d.expanded = d.inline = !1), d.body = D(d, { submit: "Submit" })), w.guideId && (d.body.guideId = w.guideId), d;
69
69
  }, W = (s) => {
70
70
  const { localState: t } = s;
71
- return s.tags.includes("category") && s.tags.includes("contact") ? y.ItemList : s.tags.includes("suggestion") ? y.LinkList : (t == null ? void 0 : t.$type) === "Humany.Matching.Bots.Conversations.GuideListState, Humany.Matching" ? y.LinkList : (t == null ? void 0 : t.$type) === "Humany.Matching.Bots.Conversations.States.GuideFeedbackState, Humany.Matching" ? p.FeedbackList : (t == null ? void 0 : t.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching" ? p.ContactList : y.ButtonList;
71
+ return s.tags.includes("category") && s.tags.includes("contact") ? v.ItemList : s.tags.includes("suggestion") ? v.LinkList : (t == null ? void 0 : t.$type) === "Humany.Matching.Bots.Conversations.GuideListState, Humany.Matching" ? v.LinkList : (t == null ? void 0 : t.$type) === "Humany.Matching.Bots.Conversations.States.GuideFeedbackState, Humany.Matching" ? m.FeedbackList : (t == null ? void 0 : t.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching" ? m.ContactList : v.ButtonList;
72
72
  }, _ = (s, t) => {
73
73
  const { knowledgeBotEndpoint: e } = t.properties();
74
74
  if (e)
75
75
  return `${e}/conversations`;
76
- const { data: i } = s.get("$settings");
77
- if (!i || !i.projection)
76
+ const { data: n } = s.get("$settings");
77
+ if (!n || !n.projection)
78
78
  throw new Error("Missing Knowledge Bot endpoint");
79
- return `${i.projection}/conversations`;
79
+ return `${n.projection}/conversations`;
80
80
  }, D = (s, {
81
81
  submit: t,
82
82
  errors: e,
83
- meta: i
83
+ meta: n
84
84
  }) => {
85
- var n;
86
- if ((n = s == null ? void 0 : s.body) != null && n.form) {
87
- const { body: c } = s, r = new H(c.form);
88
- return c.form.layout.submit || r.createComponent(V(t, "submit")), e && r.setValidationErrors(e), i && r.setMeta(i), {
89
- ...c,
85
+ var i;
86
+ if ((i = s == null ? void 0 : s.body) != null && i.form) {
87
+ const { body: a } = s, r = new A(a.form);
88
+ return a.form.layout.submit || r.createComponent(V(t, "submit")), e && r.setValidationErrors(e), n && r.setMeta(n), {
89
+ ...a,
90
90
  form: r.get()
91
91
  };
92
92
  }
@@ -103,14 +103,14 @@ const j = (s) => {
103
103
  groupId: e.id,
104
104
  isUser: e.isUser
105
105
  }), J = async (s, t, e) => {
106
- const i = (a, o, h, S = {
106
+ const n = (c, o, h, y = {
107
107
  actions: [],
108
108
  entryId: o.id,
109
109
  groupId: h
110
110
  }) => {
111
111
  const l = [];
112
- return S.actions = o.content.items.map((d) => {
113
- var m;
112
+ return y.actions = o.content.items.map((d) => {
113
+ var u;
114
114
  const k = {
115
115
  label: d.title,
116
116
  actionKey: d.actionKey,
@@ -118,252 +118,252 @@ const j = (s) => {
118
118
  description: d.description || void 0
119
119
  };
120
120
  if (d.data) {
121
- const v = o.actions[d.actionKey];
122
- a === p.ContactList ? (l.push(d.data.id), k.data = z(d.data, {
123
- guideId: (m = v == null ? void 0 : v.guideId) == null ? void 0 : m.toString()
121
+ const S = o.actions[d.actionKey];
122
+ c === m.ContactList ? (l.push(d.data.id), k.data = z(d.data, {
123
+ guideId: (u = S == null ? void 0 : S.guideId) == null ? void 0 : u.toString()
124
124
  })) : k.data = d.data;
125
125
  }
126
126
  return k;
127
- }), a === p.ContactList && (S.contactMethodIds = l), S;
128
- }, n = async (a, o, h = [], S) => {
127
+ }), c === m.ContactList && (y.contactMethodIds = l), y;
128
+ }, i = async (c, o, h = [], y) => {
129
129
  var d, k;
130
- const l = E();
131
- switch (a.type !== "List" && a.tags.includes("guide") && ((k = (d = a.content.hints) == null ? void 0 : d[0]) != null && k.id) && await R(
130
+ const l = K();
131
+ switch (c.type !== "List" && c.tags.includes("guide") && ((k = (d = c.content.hints) == null ? void 0 : d[0]) != null && k.id) && await R(
132
132
  s,
133
133
  "LastGuideId",
134
- a.content.hints[0].id
135
- ), a.type) {
134
+ c.content.hints[0].id
135
+ ), c.type) {
136
136
  case "List": {
137
- const { header: m, items: v, html: x } = a.content, w = [];
138
- let I = {
137
+ const { header: u, items: S, html: x } = c.content, I = [];
138
+ let L = {
139
139
  actions: [],
140
- entryId: a.id,
140
+ entryId: c.id,
141
141
  groupId: o.id
142
142
  };
143
- if (I.tags = a.tags || null, m && (I.header = m), (v == null ? void 0 : v.length) > 0) {
144
- const g = W(a);
145
- if (I = i(
146
- g,
147
- a,
143
+ if (L.tags = c.tags || null, u && (L.header = u), (S == null ? void 0 : S.length) > 0) {
144
+ const p = W(c);
145
+ if (L = n(
146
+ p,
147
+ c,
148
148
  o.id,
149
- I
149
+ L
150
150
  ), h.length) {
151
- const L = h.find(
152
- (C) => C.tags.includes("paging")
153
- ), A = h.find(
154
- (C) => C.tags.includes("fallback")
151
+ const C = h.find(
152
+ (E) => E.tags.includes("paging")
153
+ ), O = h.find(
154
+ (E) => E.tags.includes("fallback")
155
155
  );
156
- if (L != null && L.content && (o.meta.paging = {
157
- actions: L.actions,
158
- items: L.content.items,
159
- localState: L.localState
160
- }, L.content.items.forEach((C) => {
161
- I.actions.push({
162
- label: C.title,
163
- actionKey: C.actionKey,
156
+ if (C != null && C.content && (o.meta.paging = {
157
+ actions: C.actions,
158
+ items: C.content.items,
159
+ localState: C.localState
160
+ }, C.content.items.forEach((E) => {
161
+ L.actions.push({
162
+ label: E.title,
163
+ actionKey: E.actionKey,
164
164
  data: {
165
- groupId: S ? S.id : o.id
165
+ groupId: y ? y.id : o.id
166
166
  }
167
167
  });
168
- })), A) {
169
- const C = A.content.items.find(
168
+ })), O) {
169
+ const E = O.content.items.find(
170
170
  ($) => $.actionKey === "help"
171
171
  );
172
- C && (w.push(
172
+ E && (I.push(
173
173
  b({
174
174
  messageId: l,
175
175
  message: [
176
- y.Separator,
176
+ v.Separator,
177
177
  void 0
178
178
  ],
179
179
  group: o
180
180
  })
181
- ), w.push(
181
+ ), I.push(
182
182
  b({
183
183
  messageId: l,
184
184
  message: [
185
- p.NoGoodAlternative,
186
- { body: C.title }
185
+ m.NoGoodAlternative,
186
+ { body: E.title }
187
187
  ],
188
188
  group: o
189
189
  })
190
190
  ));
191
191
  }
192
192
  }
193
- w.unshift(
193
+ I.unshift(
194
194
  b({
195
195
  messageId: l,
196
- message: [g, I],
196
+ message: [p, L],
197
197
  group: o
198
198
  })
199
199
  );
200
200
  }
201
- return x && w.unshift(
201
+ return x && I.unshift(
202
202
  b({
203
203
  messageId: l,
204
- message: [y.HTML, { body: x }],
204
+ message: [v.HTML, { body: x }],
205
205
  group: o
206
206
  })
207
- ), w;
207
+ ), I;
208
208
  }
209
209
  case "Text": {
210
- const m = [];
211
- if (m.push(
210
+ const u = [];
211
+ if (u.push(
212
212
  b({
213
213
  messageId: l,
214
214
  message: [
215
- y.HTML,
216
- { body: a.content.html }
215
+ v.HTML,
216
+ { body: c.content.html }
217
217
  ],
218
218
  group: o
219
219
  })
220
220
  ), h.length) {
221
- const v = h.find(
222
- (g) => {
223
- var L;
224
- return g.type === "List" && ((L = g.localState) == null ? void 0 : L.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching";
221
+ const S = h.find(
222
+ (p) => {
223
+ var C;
224
+ return p.type === "List" && ((C = p.localState) == null ? void 0 : C.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching";
225
225
  }
226
226
  ), x = h.find(
227
- (g) => g.tags.includes("dialog")
228
- ), w = h.find(
229
- (g) => g.tags.includes("feedback")
227
+ (p) => p.tags.includes("dialog")
230
228
  ), I = h.find(
231
- (g) => g.tags.includes("contact") && g.type === "Contact"
229
+ (p) => p.tags.includes("feedback")
230
+ ), L = h.find(
231
+ (p) => p.tags.includes("contact") && p.type === "Contact"
232
232
  );
233
- if (I && m.push(
233
+ if (L && u.push(
234
234
  b({
235
235
  messageId: l,
236
236
  message: [
237
- p.ContactMethod,
237
+ m.ContactMethod,
238
238
  {
239
- contactMethodId: I.content.id,
239
+ contactMethodId: L.content.id,
240
240
  groupId: o.id,
241
- guideId: I.content.guideId
241
+ guideId: L.content.guideId
242
242
  }
243
243
  ],
244
244
  group: o
245
245
  })
246
- ), x && m.push(
246
+ ), x && u.push(
247
247
  b({
248
248
  messageId: l,
249
249
  message: [
250
- y.ButtonList,
250
+ v.ButtonList,
251
251
  {
252
- actions: x.content.items.map((g) => ({
253
- label: g.title,
254
- actionKey: g.actionKey,
255
- data: g.data
252
+ actions: x.content.items.map((p) => ({
253
+ label: p.title,
254
+ actionKey: p.actionKey,
255
+ data: p.data
256
256
  }))
257
257
  }
258
258
  ],
259
259
  group: o
260
260
  })
261
- ), v) {
262
- const g = i(
263
- p.ContactList,
264
- v,
261
+ ), S) {
262
+ const p = n(
263
+ m.ContactList,
264
+ S,
265
265
  o.id
266
266
  );
267
- m.push(
267
+ u.push(
268
268
  b({
269
269
  messageId: l,
270
270
  message: [
271
- y.Separator,
271
+ v.Separator,
272
272
  void 0
273
273
  ],
274
274
  group: o
275
275
  })
276
- ), m.push(
276
+ ), u.push(
277
277
  b({
278
278
  messageId: l,
279
- message: [p.ContactList, g],
279
+ message: [m.ContactList, p],
280
280
  group: o
281
281
  })
282
282
  );
283
283
  }
284
- w && (w.content.html && m.push(
284
+ I && (I.content.html && u.push(
285
285
  b({
286
286
  messageId: l,
287
287
  group: o,
288
288
  message: [
289
- y.HTML,
289
+ v.HTML,
290
290
  {
291
- body: w.content.html
291
+ body: I.content.html
292
292
  }
293
293
  ]
294
294
  })
295
- ), m.push(
295
+ ), u.push(
296
296
  b({
297
297
  messageId: l,
298
298
  group: o,
299
299
  message: [
300
- p.FeedbackList,
300
+ m.FeedbackList,
301
301
  {
302
- actions: w.content.items.map((g) => ({
303
- actionKey: g.actionKey,
304
- label: g.title
302
+ actions: I.content.items.map((p) => ({
303
+ actionKey: p.actionKey,
304
+ label: p.title
305
305
  }))
306
306
  }
307
307
  ]
308
308
  })
309
309
  ));
310
310
  }
311
- return m;
311
+ return u;
312
312
  }
313
313
  case "Contact":
314
314
  return [
315
315
  b({
316
316
  messageId: l,
317
317
  message: [
318
- p.ContactMethod,
318
+ m.ContactMethod,
319
319
  {
320
- contactMethodId: a.content.id,
320
+ contactMethodId: c.content.id,
321
321
  groupId: o.id,
322
- guideId: a.content.guideId
322
+ guideId: c.content.guideId
323
323
  }
324
324
  ],
325
325
  group: o
326
326
  })
327
327
  ];
328
328
  case "CategorizedNoticeList": {
329
- const m = [
329
+ const u = [
330
330
  b({
331
331
  messageId: l,
332
332
  message: [
333
- p.NotificationList,
333
+ m.NotificationList,
334
334
  {
335
- id: a.content.item.id.toString(),
336
- header: a.content.item.title,
337
- items: a.content.item.listNotices,
338
- symbol: a.content.item.defaultIcon
335
+ id: c.content.item.id.toString(),
336
+ header: c.content.item.title,
337
+ items: c.content.item.listNotices,
338
+ symbol: c.content.item.defaultIcon
339
339
  }
340
340
  ],
341
341
  group: o
342
342
  })
343
343
  ];
344
- return (a.content.notices || []).forEach((v) => {
345
- m.push(
344
+ return (c.content.notices || []).forEach((S) => {
345
+ u.push(
346
346
  b({
347
347
  messageId: l,
348
348
  message: [
349
- y.HTML,
349
+ v.HTML,
350
350
  {
351
- body: v.body
351
+ body: S.body
352
352
  }
353
353
  ],
354
354
  group: o
355
355
  })
356
356
  );
357
- }), m;
357
+ }), u;
358
358
  }
359
359
  case "footerNotice":
360
360
  return [
361
361
  b({
362
362
  messageId: l,
363
363
  message: [
364
- y.HTML,
364
+ v.HTML,
365
365
  {
366
- body: a.content.item.body
366
+ body: c.content.item.body
367
367
  }
368
368
  ],
369
369
  group: o
@@ -374,9 +374,9 @@ const j = (s) => {
374
374
  b({
375
375
  messageId: l,
376
376
  message: [
377
- y.HTML,
377
+ v.HTML,
378
378
  {
379
- body: a.content.message || "Something went wrong."
379
+ body: c.content.message || "Something went wrong."
380
380
  }
381
381
  ],
382
382
  group: o
@@ -385,49 +385,49 @@ const j = (s) => {
385
385
  default:
386
386
  return [];
387
387
  }
388
- }, c = (a) => ["CategorizedNoticeList", "NoticeList"].includes(a.type) ? (a.content.items || []).reduce((o, h) => (a.content.isInitialNotices && o.push({
389
- ...a,
390
- id: E(),
391
- type: h.type || a.type,
388
+ }, a = (c) => ["CategorizedNoticeList", "NoticeList"].includes(c.type) ? (c.content.items || []).reduce((o, h) => (c.content.isInitialNotices && o.push({
389
+ ...c,
390
+ id: K(),
391
+ type: h.type || c.type,
392
392
  content: {
393
- $type: a.content.$type,
394
- isInitialNotices: a.content.isInitialNotices,
393
+ $type: c.content.$type,
394
+ isInitialNotices: c.content.isInitialNotices,
395
395
  notices: h.notices,
396
396
  item: h
397
397
  }
398
- }), o), []) : a, r = /* @__PURE__ */ new Map([
398
+ }), o), []) : c, r = /* @__PURE__ */ new Map([
399
399
  ["Text", 1],
400
400
  ["List", 2],
401
401
  ["Contact", 3],
402
402
  ["CategorizedNoticeList", 10]
403
- ]), u = [], M = t.sort((a, o) => {
404
- const S = r.get(a.type) || 20, l = r.get(o.type) || 20;
405
- return S - l;
406
- }).filter((a) => !a.relation).reduce((a, o) => a.concat(c(o)), []), K = (a) => typeof a.actions.selector < "u";
407
- for (const a of M) {
403
+ ]), g = [], M = t.sort((c, o) => {
404
+ const y = r.get(c.type) || 20, l = r.get(o.type) || 20;
405
+ return y - l;
406
+ }).filter((c) => !c.relation).reduce((c, o) => c.concat(a(o)), []), w = (c) => typeof c.actions.selector < "u";
407
+ for (const c of M) {
408
408
  const o = t.filter(
409
- (l) => l.relation === a.id && !K(l)
410
- ), h = P({ isUser: !1 });
411
- (await n(a, h, o, e)).forEach((l) => {
409
+ (l) => l.relation === c.id && !w(l)
410
+ ), h = H({ isUser: !1 });
411
+ (await i(c, h, o, e)).forEach((l) => {
412
412
  h.items.push(l);
413
- }), h.items.length && u.push(h);
413
+ }), h.items.length && g.push(h);
414
414
  }
415
- return u;
415
+ return g;
416
416
  };
417
417
  class q {
418
- constructor(t, e, i, n) {
418
+ constructor(t, e, n, i) {
419
419
  f(this, "globalState");
420
420
  f(this, "localState");
421
421
  f(this, "clientId");
422
422
  f(this, "entries");
423
- this.endpoint = t, this.events = e, this.store = i, this.container = n, this.clientId = E(), this.entries = [];
423
+ this.endpoint = t, this.events = e, this.store = n, this.container = i, this.clientId = K(), this.entries = [];
424
424
  }
425
425
  async start({ silent: t }) {
426
- const e = await this.store.read("globalState"), i = await this.store.read("localState"), n = await this.store.read("entries");
427
- if (this.globalState = e || this.globalState, this.localState = i || this.localState, this.entries = n || this.entries, t)
426
+ const e = await this.store.read("globalState"), n = await this.store.read("localState"), i = await this.store.read("entries");
427
+ if (this.globalState = e || this.globalState, this.localState = n || this.localState, this.entries = i || this.entries, t)
428
428
  return;
429
- const c = this.createInitialEnvelope();
430
- return await this.postEnvelope(c);
429
+ const a = this.createInitialEnvelope();
430
+ return await this.postEnvelope(a);
431
431
  }
432
432
  async sendMessage(t) {
433
433
  const e = {
@@ -450,9 +450,9 @@ class q {
450
450
  });
451
451
  }
452
452
  async nextAction(t) {
453
- var i;
453
+ var n;
454
454
  const { paging: e } = t.meta;
455
- if ((i = e.actions) != null && i.next)
455
+ if ((n = e.actions) != null && n.next)
456
456
  return this.postEnvelope(
457
457
  {
458
458
  action: {
@@ -464,40 +464,40 @@ class q {
464
464
  clientId: this.clientId
465
465
  },
466
466
  t
467
- ).then((n) => {
468
- const c = t.items.findIndex(
467
+ ).then((i) => {
468
+ const a = t.items.findIndex(
469
469
  (r) => r.message[0] === "link-list"
470
470
  );
471
- return c === -1 || !n ? t : {
471
+ return a === -1 || !i ? t : {
472
472
  ...t,
473
473
  meta: {
474
474
  ...t.meta,
475
- ...n[0].meta
475
+ ...i[0].meta
476
476
  },
477
477
  items: [
478
- ...t.items.slice(0, c),
478
+ ...t.items.slice(0, a),
479
479
  {
480
- ...t.items[c],
480
+ ...t.items[a],
481
481
  message: [
482
- n[0].items[0].message[0],
482
+ i[0].items[0].message[0],
483
483
  {
484
- ...t.items[c].message[1],
485
- ...n[0].items[0].message[1]
484
+ ...t.items[a].message[1],
485
+ ...i[0].items[0].message[1]
486
486
  }
487
487
  ]
488
488
  },
489
- ...t.items.slice(c + 1)
489
+ ...t.items.slice(a + 1)
490
490
  ]
491
491
  };
492
492
  });
493
493
  }
494
494
  async showContactMethodAction(t) {
495
- const e = this.entries.find((n) => n.id === t.entryId), i = e == null ? void 0 : e.actions[t.actionKey];
496
- if (i)
495
+ const e = this.entries.find((i) => i.id === t.entryId), n = e == null ? void 0 : e.actions[t.actionKey];
496
+ if (n)
497
497
  return this.postEnvelope({
498
498
  action: {
499
499
  key: t.actionKey,
500
- content: i
500
+ content: n
501
501
  },
502
502
  localState: e.localState,
503
503
  globalState: this.globalState,
@@ -527,25 +527,25 @@ class q {
527
527
  }
528
528
  async postEnvelope(t, e) {
529
529
  try {
530
- const i = await fetch(
530
+ const n = await fetch(
531
531
  this.endpoint,
532
532
  this.getHttpOptions(t)
533
533
  );
534
- if (!i.ok)
535
- throw i;
536
- const n = await i.json();
537
- return this.handleResponse(n, e);
538
- } catch (i) {
534
+ if (!n.ok)
535
+ throw n;
536
+ const i = await n.json();
537
+ return this.handleResponse(i, e);
538
+ } catch (n) {
539
539
  process.env.NODE_ENV !== "production" && console.warn("Something went wrong when posting the envelope.");
540
- let n = "Something went wrong";
541
- return (i.message === "Failed to fetch" || i.message === "NetworkError when attempting to fetch resource.") && (n = "Lost connection... Please try again."), this.events.dispatch("tracking:http-client-error", { message: n, error: i }), this.handleResponse({
540
+ let i = "Something went wrong";
541
+ return (n.message === "Failed to fetch" || n.message === "NetworkError when attempting to fetch resource.") && (i = "Lost connection... Please try again."), this.events.dispatch("tracking:http-client-error", { message: i, error: n }), this.handleResponse({
542
542
  entries: [
543
543
  {
544
- id: E(),
544
+ id: K(),
545
545
  type: "Error",
546
546
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
547
547
  content: {
548
- message: n
548
+ message: i
549
549
  },
550
550
  actions: {},
551
551
  tags: []
@@ -556,9 +556,9 @@ class q {
556
556
  }
557
557
  async handleResponse(t, e) {
558
558
  this.entries = this.entries.concat(t.entries), this.store.write(this.entries, "entries");
559
- const i = t.entries.find((c) => c.type === "Widget");
560
- return i && this.events.dispatch("provider:widget-entry", i.content), t.globalState && (this.globalState = t.globalState, this.store.write(t.globalState, "globalState")), this.localState = t.entries.reduce(
561
- (c, r) => r.localState ? r.localState : c,
559
+ const n = t.entries.find((a) => a.type === "Widget");
560
+ return n && this.events.dispatch("provider:widget-entry", n.content), t.globalState && (this.globalState = t.globalState, this.store.write(t.globalState, "globalState")), this.localState = t.entries.reduce(
561
+ (a, r) => r.localState ? r.localState : a,
562
562
  void 0
563
563
  ), this.store.write(this.localState, "localState"), await J(
564
564
  this.container,
@@ -617,18 +617,18 @@ class Q {
617
617
  return U(this.container, e);
618
618
  }
619
619
  async write(t, e) {
620
- const i = this.getKey(e);
621
- if (!this.enabled || !i)
620
+ const n = this.getKey(e);
621
+ if (!this.enabled || !n)
622
622
  return;
623
- (await O(
623
+ (await P(
624
624
  this.container,
625
- i,
625
+ n,
626
626
  B.Necessary
627
627
  ))(t);
628
628
  }
629
629
  }
630
630
  class X {
631
- constructor(t, e, i) {
631
+ constructor(t, e, n) {
632
632
  f(this, "agent");
633
633
  f(this, "user");
634
634
  f(this, "httpClient");
@@ -636,48 +636,48 @@ class X {
636
636
  f(this, "groups", []);
637
637
  f(this, "store");
638
638
  this.container = t, this.conversation = e;
639
- const { events: n } = this.container.get("$widget");
640
- this.events = n, this.store = new Q(t), this.agent = e.createAgent({
639
+ const { events: i } = this.container.get("$widget");
640
+ this.events = i, this.store = new Q(t), this.agent = e.createAgent({
641
641
  id: "knowledge-bot-agent",
642
642
  // we need to specify an ID here to allow rehydration to work
643
643
  name: "",
644
644
  avatar: void 0
645
645
  }), this.user = e.user;
646
- const c = _(t, i);
646
+ const a = _(t, n);
647
647
  this.httpClient = new q(
648
- c,
649
- n,
648
+ a,
649
+ i,
650
650
  this.store,
651
651
  this.container
652
652
  ), this.events.subscribe(
653
653
  "provider:widget-entry",
654
- (r, u) => {
655
- this.onWidgetEntryReceived(u);
654
+ (r, g) => {
655
+ this.onWidgetEntryReceived(g);
656
656
  }
657
657
  ), this.events.subscribe(
658
658
  "provider:open-contact-method",
659
- async (r, u) => {
660
- const M = await this.httpClient.showContactMethodAction(u);
661
- M && (u.contactMethod.title && this.printUserMessage(u.contactMethod.title), this.onMessagesReceived(M));
659
+ async (r, g) => {
660
+ const M = await this.httpClient.showContactMethodAction(g);
661
+ M && (g.contactMethod.title && this.printUserMessage(g.contactMethod.title), this.onMessagesReceived(M));
662
662
  }
663
663
  );
664
664
  }
665
665
  async initialize() {
666
- var c;
666
+ var a;
667
667
  const t = this.conversation.loading(), e = await this.store.read("meta");
668
668
  e != null && e.agent && this.agent.update({
669
669
  name: e.agent.name,
670
670
  avatar: e.agent.avatar
671
671
  });
672
- const i = await this.conversation.isRehydrated(), n = await this.httpClient.start({
673
- silent: ((c = this.conversation.options) == null ? void 0 : c.silent) || i
672
+ const n = await this.conversation.isRehydrated(), i = await this.httpClient.start({
673
+ silent: ((a = this.conversation.options) == null ? void 0 : a.silent) || n
674
674
  });
675
- n && this.onMessagesReceived(n), t();
675
+ i && this.onMessagesReceived(i), t();
676
676
  }
677
677
  async onMessagesReceived(t) {
678
678
  this.groups = this.groups.concat(t), t.forEach((e) => {
679
- const i = e.items.map((n) => n.message);
680
- e.isUser ? e.writer = this.user.print(i) : e.writer = this.agent.print(i);
679
+ const n = e.items.map((i) => i.message);
680
+ e.isUser ? e.writer = this.user.print(n) : e.writer = this.agent.print(n);
681
681
  });
682
682
  }
683
683
  async onInput(t) {
@@ -687,123 +687,137 @@ class X {
687
687
  {
688
688
  if (typeof t == "string")
689
689
  return;
690
- const i = this.groups.find(
691
- (n) => {
692
- var c;
693
- return n.id === ((c = t.data) == null ? void 0 : c.groupId);
690
+ const n = this.groups.find(
691
+ (i) => {
692
+ var a;
693
+ return i.id === ((a = t.data) == null ? void 0 : a.groupId);
694
694
  }
695
695
  );
696
- if (i) {
697
- const n = await this.httpClient.nextAction(i);
698
- if (n) {
699
- const c = n.items.map((r) => r.message);
700
- i.meta.paging = n == null ? void 0 : n.meta.paging, i.writer.update(c);
696
+ if (n) {
697
+ const i = await this.httpClient.nextAction(n);
698
+ if (i) {
699
+ const a = i.items.map((r) => r.message);
700
+ n.meta.paging = i == null ? void 0 : i.meta.paging, n.writer.update(a);
701
701
  }
702
702
  }
703
703
  }
704
704
  break;
705
705
  case "help":
706
706
  {
707
- const i = this.conversation.loading(), n = await this.httpClient.helpAction();
708
- i(), n && this.onMessagesReceived(n);
707
+ const n = this.conversation.loading(), i = await this.httpClient.helpAction();
708
+ n(), i && this.onMessagesReceived(i);
709
709
  }
710
710
  break;
711
711
  default:
712
712
  {
713
713
  typeof t != "string" && t.label && this.printUserMessage(t.label);
714
- const i = this.conversation.loading(), n = await this.httpClient.defaultAction(e);
715
- n && this.onMessagesReceived(n), i();
714
+ const n = this.conversation.loading(), i = await this.httpClient.defaultAction(e);
715
+ i && this.onMessagesReceived(i), n();
716
716
  }
717
717
  break;
718
718
  }
719
719
  }
720
720
  printUserMessage(t) {
721
- const e = P({
721
+ const e = H({
722
722
  isUser: !0,
723
723
  items: [
724
724
  {
725
- id: E(),
725
+ id: K(),
726
726
  isUser: !0,
727
- message: [y.HTML, { body: t }]
727
+ message: [v.HTML, { body: t }]
728
728
  }
729
729
  ]
730
730
  });
731
731
  return this.onMessagesReceived([e]), e;
732
732
  }
733
733
  async onMessageSend(t) {
734
- const e = this.conversation.loading(), i = await this.httpClient.sendMessage(t);
735
- await R(this.container, "phrase", t), this.printUserMessage(t), i && this.onMessagesReceived(i), e();
734
+ const e = this.conversation.loading(), n = await this.httpClient.sendMessage(t);
735
+ await R(this.container, "phrase", t), this.printUserMessage(t), n && this.onMessagesReceived(n), e();
736
736
  }
737
737
  async onWidgetEntryReceived(t) {
738
+ var c, o, h, y, l;
738
739
  if (t.agent) {
739
- const e = {
740
+ const d = {
740
741
  name: t.agent.name,
741
742
  avatar: t.agent.avatar
742
743
  };
743
- this.agent.update(e), (await O(
744
+ this.agent.update(d), (await P(
744
745
  this.container,
745
746
  "bot-provider-conversation-meta",
746
747
  B.Necessary
747
748
  ))({
748
- agent: e
749
+ agent: d
749
750
  });
750
751
  }
752
+ const n = (await this.container.getAsync("components")).components().ofType("widget-header").select();
753
+ if (!n)
754
+ return;
755
+ const i = await this.container.get("settings"), a = i == null ? void 0 : i.components["widget-header"], r = (o = (c = a == null ? void 0 : a.properties) == null ? void 0 : c.actions) == null ? void 0 : o.avatar, g = (h = a == null ? void 0 : a.properties) == null ? void 0 : h.header, M = (y = a == null ? void 0 : a.properties) == null ? void 0 : y.tagline, w = {};
756
+ !g && (t != null && t.heading) && (w.header = t.heading), !M && (t != null && t.tagline) && (w.tagline = t.tagline), !r && ((l = t == null ? void 0 : t.agent) != null && l.avatar) && (w.actions = {
757
+ avatar: {
758
+ position: "left",
759
+ symbol: {
760
+ type: "Uri",
761
+ content: t.agent.avatar
762
+ }
763
+ }
764
+ }), Object.keys(w).length > 0 && n.writeProperties(w);
751
765
  }
752
766
  }
753
- const st = async (s) => {
754
- const t = await T.getInstance(s);
767
+ const it = async (s) => {
768
+ const t = await G.getInstance(s);
755
769
  N(
756
770
  s,
757
- p.ContactList,
758
- import("./contact-method-list-bbcc3f56.mjs")
771
+ m.ContactList,
772
+ import("./contact-method-list-dae09f9d.mjs")
759
773
  ), N(
760
774
  s,
761
- p.ContactMethod,
762
- import("./contact-method-adeea373.mjs")
775
+ m.ContactMethod,
776
+ import("./contact-method-b5669c64.mjs")
763
777
  ), N(
764
778
  s,
765
- p.NotificationList,
779
+ m.NotificationList,
766
780
  import("./notification-list-3e483308.mjs")
767
781
  ), N(
768
782
  s,
769
- p.NoGoodAlternative,
783
+ m.NoGoodAlternative,
770
784
  import("./no-good-alternative-c6825c80.mjs")
771
785
  ), N(
772
786
  s,
773
- p.FeedbackList,
787
+ m.FeedbackList,
774
788
  import("./feedback-list-0cf5a7d8.mjs")
775
- ), t.registerProvider("ace-knowledge-bot", (e, i) => {
776
- const n = new X(
789
+ ), t.registerProvider("ace-knowledge-bot", (e, n) => {
790
+ const i = new X(
777
791
  s,
778
792
  e,
779
- i
793
+ n
780
794
  );
781
- n.initialize();
782
- const c = [];
783
- return c.push(
784
- i.actions.watch("conversation.user-submit", (r, u) => {
785
- n.onMessageSend(r.text);
795
+ i.initialize();
796
+ const a = [];
797
+ return a.push(
798
+ n.actions.watch("conversation.user-submit", (r, g) => {
799
+ i.onMessageSend(r.text);
786
800
  })
787
- ), c.push(
788
- i.actions.watch("conversation.action", (r, u) => (n.onInput(r), u(r)))
789
- ), c.push(
790
- i.actions.create("evaluate", (r) => {
791
- const { events: u } = s.get("$widget");
792
- u.dispatch("bot-provider:evaluate", r);
801
+ ), a.push(
802
+ n.actions.watch("conversation.action", (r, g) => (i.onInput(r), g(r)))
803
+ ), a.push(
804
+ n.actions.create("evaluate", (r) => {
805
+ const { events: g } = s.get("$widget");
806
+ g.dispatch("bot-provider:evaluate", r);
793
807
  })
794
- ), c.push(
795
- i.actions.create("submit", (r) => {
796
- const { events: u } = s.get("$widget");
797
- u.dispatch("bot-provider:form-submitted", r);
808
+ ), a.push(
809
+ n.actions.create("submit", (r) => {
810
+ const { events: g } = s.get("$widget");
811
+ g.dispatch("bot-provider:form-submitted", r);
798
812
  })
799
813
  ), () => {
800
- c.forEach((r) => {
814
+ a.forEach((r) => {
801
815
  r();
802
816
  });
803
817
  };
804
818
  });
805
819
  };
806
820
  export {
807
- st as K,
821
+ it as K,
808
822
  D as u
809
823
  };
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index-1a47fa98.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");require("@telia-ace/widget-utilities");require("@telia-ace/widget-forms");exports.ConversationPlugin=e.KnowledgeBotProviderPlugin;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index-cb940de4.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");require("@telia-ace/widget-utilities");require("@telia-ace/widget-forms");exports.ConversationPlugin=e.KnowledgeBotProviderPlugin;
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { K as n } from "./index-2593adbc.mjs";
1
+ import { K as n } from "./index-cc087f12.mjs";
2
2
  import "@telia-ace/widget-conversation";
3
3
  import "@telia-ace/widget-services";
4
4
  import "@telia-ace/widget-utilities";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telia-ace/knowledge-widget-bot-provider",
3
- "version": "1.1.8",
3
+ "version": "1.2.0-rc.1",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
@@ -10,15 +10,15 @@
10
10
  }
11
11
  },
12
12
  "dependencies": {
13
- "@telia-ace/knowledge-data-client": "^1.1.8",
14
- "@telia-ace/knowledge-widget-adapters": "^1.1.8",
15
- "@telia-ace/knowledge-widget-ui": "^1.1.8",
16
- "@telia-ace/widget-conversation": "^1.1.8",
17
- "@telia-ace/widget-forms": "^1.1.8",
18
- "@telia-ace/widget-services": "^1.1.8",
19
- "@telia-ace/widget-utilities": "^1.1.8",
13
+ "@telia-ace/knowledge-data-client": "^1.2.0-rc.1",
14
+ "@telia-ace/knowledge-widget-adapters": "^1.2.0-rc.1",
15
+ "@telia-ace/knowledge-widget-ui": "^1.2.0-rc.1",
16
+ "@telia-ace/widget-conversation": "^1.2.0-rc.1",
17
+ "@telia-ace/widget-forms": "^1.2.0-rc.1",
18
+ "@telia-ace/widget-services": "^1.2.0-rc.1",
19
+ "@telia-ace/widget-utilities": "^1.2.0-rc.1",
20
20
  "@webprovisions/platform": "^1.1.4",
21
- "@telia-ace/knowledge-widget-core": "^1.1.8"
21
+ "@telia-ace/knowledge-widget-core": "^1.2.0-rc.1"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "@emotion/react": "11.11.1",
package/index-1a47fa98.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var F=Object.defineProperty;var R=(i,t,e)=>t in i?F(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e;var y=(i,t,e)=>(R(i,typeof t!="symbol"?t+"":t,e),e);const m=require("@telia-ace/widget-conversation"),N=require("@telia-ace/widget-services"),k=require("@telia-ace/widget-utilities"),P=require("@telia-ace/widget-forms");var f=(i=>(i.ContactMethod="ace-knowledge-bot-contact-method",i.ContactList="ace-knowledge-bot-contact-list",i.FeedbackList="ace-knowledge-bot-feedback-list",i.NotificationList="ace-knowledge-bot-notification-list",i.NoGoodAlternative="ace-knowledge-bot-no-good-alternative",i))(f||{});const $=i=>{if(i){const{type:t,content:e}=i;if(t&&e)return{type:t,content:e}}},H=({isUser:i,items:t=[],meta:e={}})=>{const s=k.uuid();return{id:s,isUser:i,items:t.map(r=>({id:r.id||k.uuid(),isUser:i,groupId:s,message:r.message||[]})),meta:e}},G=({id:i,title:t,defaultIcon:e,exits:[s],exitType:n,confirmationText:r,expandFormAutomatically:c,form:u,description:E},K={})=>{const{adapter:a}=s||{Adapter:{}},{clientName:o="",settings:g={},customClientName:S=""}=a,{validation:l}=K,d={id:i.toString(),title:t,description:E,symbol:$(e),clientName:o==="custom.adapter"?S:o,expanded:!0,inline:!0,body:{}};if(o.indexOf("phone")>-1||o.indexOf("freetext")>-1){const{displayTextBeforeClick:M,textBeforeClick:p}=g;M==="true"&&(d.expanded=!1,d.title=p),o.indexOf("phone.text")>-1?d.body.phoneNumber=g.phoneNumber:d.body.freetext=g.freetext}return(o.indexOf("chat.popup")>-1||o.indexOf("link")>-1||o==="custom.adapter"&&n==="custom")&&(d.body=g,d.body.confirmationFallback=r),o.indexOf("ace")>-1&&(d.body=g),u&&u.hasValueComponents&&(l?d.body.form=P.FormBuilder.fromLegacyForm(i.toString(),u,l).get():d.body.form=P.FormBuilder.fromLegacyForm(i.toString(),u).get(),c||(d.expanded=d.inline=!1),d.body=B(d,{submit:"Submit"})),K.guideId&&(d.body.guideId=K.guideId),d},U=i=>{const{localState:t}=i;return i.tags.includes("category")&&i.tags.includes("contact")?m.ConversationMessageListItemType.ItemList:i.tags.includes("suggestion")||(t==null?void 0:t.$type)==="Humany.Matching.Bots.Conversations.GuideListState, Humany.Matching"?m.ConversationMessageListItemType.LinkList:(t==null?void 0:t.$type)==="Humany.Matching.Bots.Conversations.States.GuideFeedbackState, Humany.Matching"?f.FeedbackList:(t==null?void 0:t.$type)==="Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching"?f.ContactList:m.ConversationMessageListItemType.ButtonList},q=(i,t)=>{const{knowledgeBotEndpoint:e}=t.properties();if(e)return`${e}/conversations`;const{data:s}=i.get("$settings");if(!s||!s.projection)throw new Error("Missing Knowledge Bot endpoint");return`${s.projection}/conversations`},B=(i,{submit:t,errors:e,meta:s})=>{var n;if((n=i==null?void 0:i.body)!=null&&n.form){const{body:r}=i,c=new P.FormBuilder(r.form);return r.form.layout.submit||c.createComponent(P.submitButton(t,"submit")),e&&c.setValidationErrors(e),s&&c.setMeta(s),{...r,form:c.get()}}return(i==null?void 0:i.body)||{}},A=async(i,t,e)=>{(await(await i.getAsync("dataClient")).getClient()).parameters.set(t,e)},b=({messageId:i,message:t,group:e})=>({id:i,message:t,groupId:e.id,isUser:e.isUser}),V=async(i,t,e)=>{const s=(a,o,g,S={actions:[],entryId:o.id,groupId:g})=>{const l=[];return S.actions=o.content.items.map(d=>{var p;const M={label:d.title,actionKey:d.actionKey,symbol:d.symbol?d.symbol:void 0,description:d.description||void 0};if(d.data){const v=o.actions[d.actionKey];a===f.ContactList?(l.push(d.data.id),M.data=G(d.data,{guideId:(p=v==null?void 0:v.guideId)==null?void 0:p.toString()})):M.data=d.data}return M}),a===f.ContactList&&(S.contactMethodIds=l),S},n=async(a,o,g=[],S)=>{var d,M;const l=k.uuid();switch(a.type!=="List"&&a.tags.includes("guide")&&((M=(d=a.content.hints)==null?void 0:d[0])!=null&&M.id)&&await A(i,"LastGuideId",a.content.hints[0].id),a.type){case"List":{const{header:p,items:v,html:x}=a.content,w=[];let C={actions:[],entryId:a.id,groupId:o.id};if(C.tags=a.tags||null,p&&(C.header=p),(v==null?void 0:v.length)>0){const h=U(a);if(C=s(h,a,o.id,C),g.length){const I=g.find(L=>L.tags.includes("paging")),T=g.find(L=>L.tags.includes("fallback"));if(I!=null&&I.content&&(o.meta.paging={actions:I.actions,items:I.content.items,localState:I.localState},I.content.items.forEach(L=>{C.actions.push({label:L.title,actionKey:L.actionKey,data:{groupId:S?S.id:o.id}})})),T){const L=T.content.items.find(O=>O.actionKey==="help");L&&(w.push(b({messageId:l,message:[m.ConversationMessageListItemType.Separator,void 0],group:o})),w.push(b({messageId:l,message:[f.NoGoodAlternative,{body:L.title}],group:o})))}}w.unshift(b({messageId:l,message:[h,C],group:o}))}return x&&w.unshift(b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:x}],group:o})),w}case"Text":{const p=[];if(p.push(b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:a.content.html}],group:o})),g.length){const v=g.find(h=>{var I;return h.type==="List"&&((I=h.localState)==null?void 0:I.$type)==="Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching"}),x=g.find(h=>h.tags.includes("dialog")),w=g.find(h=>h.tags.includes("feedback")),C=g.find(h=>h.tags.includes("contact")&&h.type==="Contact");if(C&&p.push(b({messageId:l,message:[f.ContactMethod,{contactMethodId:C.content.id,groupId:o.id,guideId:C.content.guideId}],group:o})),x&&p.push(b({messageId:l,message:[m.ConversationMessageListItemType.ButtonList,{actions:x.content.items.map(h=>({label:h.title,actionKey:h.actionKey,data:h.data}))}],group:o})),v){const h=s(f.ContactList,v,o.id);p.push(b({messageId:l,message:[m.ConversationMessageListItemType.Separator,void 0],group:o})),p.push(b({messageId:l,message:[f.ContactList,h],group:o}))}w&&(w.content.html&&p.push(b({messageId:l,group:o,message:[m.ConversationMessageListItemType.HTML,{body:w.content.html}]})),p.push(b({messageId:l,group:o,message:[f.FeedbackList,{actions:w.content.items.map(h=>({actionKey:h.actionKey,label:h.title}))}]})))}return p}case"Contact":return[b({messageId:l,message:[f.ContactMethod,{contactMethodId:a.content.id,groupId:o.id,guideId:a.content.guideId}],group:o})];case"CategorizedNoticeList":{const p=[b({messageId:l,message:[f.NotificationList,{id:a.content.item.id.toString(),header:a.content.item.title,items:a.content.item.listNotices,symbol:a.content.item.defaultIcon}],group:o})];return(a.content.notices||[]).forEach(v=>{p.push(b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:v.body}],group:o}))}),p}case"footerNotice":return[b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:a.content.item.body}],group:o})];case"Error":return[b({messageId:l,message:[m.ConversationMessageListItemType.HTML,{body:a.content.message||"Something went wrong."}],group:o})];default:return[]}},r=a=>["CategorizedNoticeList","NoticeList"].includes(a.type)?(a.content.items||[]).reduce((o,g)=>(a.content.isInitialNotices&&o.push({...a,id:k.uuid(),type:g.type||a.type,content:{$type:a.content.$type,isInitialNotices:a.content.isInitialNotices,notices:g.notices,item:g}}),o),[]):a,c=new Map([["Text",1],["List",2],["Contact",3],["CategorizedNoticeList",10]]),u=[],E=t.sort((a,o)=>{const S=c.get(a.type)||20,l=c.get(o.type)||20;return S-l}).filter(a=>!a.relation).reduce((a,o)=>a.concat(r(o)),[]),K=a=>typeof a.actions.selector<"u";for(const a of E){const o=t.filter(l=>l.relation===a.id&&!K(l)),g=H({isUser:!1});(await n(a,g,o,e)).forEach(l=>{g.items.push(l)}),g.items.length&&u.push(g)}return u};class j{constructor(t,e,s,n){y(this,"globalState");y(this,"localState");y(this,"clientId");y(this,"entries");this.endpoint=t,this.events=e,this.store=s,this.container=n,this.clientId=k.uuid(),this.entries=[]}async start({silent:t}){const e=await this.store.read("globalState"),s=await this.store.read("localState"),n=await this.store.read("entries");if(this.globalState=e||this.globalState,this.localState=s||this.localState,this.entries=n||this.entries,t)return;const r=this.createInitialEnvelope();return await this.postEnvelope(r)}async sendMessage(t){const e={command:{phrase:t},globalState:this.globalState,clientId:this.clientId};return await this.postEnvelope(e)}async helpAction(){return this.postEnvelope({action:{key:"help",content:{$type:"Humany.Matching.Bots.Conversations.Actions.Help, Humany.Matching"}},globalState:this.globalState,clientId:this.clientId})}async nextAction(t){var s;const{paging:e}=t.meta;if((s=e.actions)!=null&&s.next)return this.postEnvelope({action:{key:"next",content:e.actions.next},localState:e.localState,globalState:this.globalState,clientId:this.clientId},t).then(n=>{const r=t.items.findIndex(c=>c.message[0]==="link-list");return r===-1||!n?t:{...t,meta:{...t.meta,...n[0].meta},items:[...t.items.slice(0,r),{...t.items[r],message:[n[0].items[0].message[0],{...t.items[r].message[1],...n[0].items[0].message[1]}]},...t.items.slice(r+1)]}})}async showContactMethodAction(t){const e=this.entries.find(n=>n.id===t.entryId),s=e==null?void 0:e.actions[t.actionKey];if(s)return this.postEnvelope({action:{key:t.actionKey,content:s},localState:e.localState,globalState:this.globalState,clientId:this.clientId})}async defaultAction(t){const e=this.getEntryOfAction(t);if(e)return this.postEnvelope({action:{key:t,content:e.actions[t]},localState:e.localState,globalState:this.globalState,clientId:this.clientId})}createInitialEnvelope(){return{command:{site:this.endpoint},clientId:this.clientId,globalState:{},funnel:this.container.get("$widget").name}}async postEnvelope(t,e){try{const s=await fetch(this.endpoint,this.getHttpOptions(t));if(!s.ok)throw s;const n=await s.json();return this.handleResponse(n,e)}catch(s){process.env.NODE_ENV!=="production"&&console.warn("Something went wrong when posting the envelope.");let n="Something went wrong";return(s.message==="Failed to fetch"||s.message==="NetworkError when attempting to fetch resource.")&&(n="Lost connection... Please try again."),this.events.dispatch("tracking:http-client-error",{message:n,error:s}),this.handleResponse({entries:[{id:k.uuid(),type:"Error",timestamp:new Date().toISOString(),content:{message:n},actions:{},tags:[]}]})}}async handleResponse(t,e){this.entries=this.entries.concat(t.entries),this.store.write(this.entries,"entries");const s=t.entries.find(r=>r.type==="Widget");return s&&this.events.dispatch("provider:widget-entry",s.content),t.globalState&&(this.globalState=t.globalState,this.store.write(t.globalState,"globalState")),this.localState=t.entries.reduce((r,c)=>c.localState?c.localState:r,void 0),this.store.write(this.localState,"localState"),await V(this.container,t.entries,e)}getHttpOptions(t){const e={method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"}};return t.funnel=this.container.get("$widget").name,t.localState=t.localState||this.localState,Object.assign({},e,{body:JSON.stringify(t),credentials:"same-origin"})}getEntryOfAction(t){return this.entries.find(e=>!!e.actions[t])}}class z{constructor(t){y(this,"enabled",!0);y(this,"botProviderGlobalStateKey","bot-provider-global-state");y(this,"botProviderLocalStateKey","bot-provider-local-state");y(this,"botProviderEntriesKey","bot-provider-entries");y(this,"botProviderMetaStateKey","bot-provider-conversation-meta");this.container=t}getKey(t){let e;switch(t){case"localState":e=this.botProviderLocalStateKey;break;case"globalState":e=this.botProviderGlobalStateKey;break;case"entries":e=this.botProviderEntriesKey;break;case"meta":e=this.botProviderMetaStateKey;break}return e}async read(t){const e=this.getKey(t);if(!(!this.enabled||!e))return N.readStorage(this.container,e)}async write(t,e){const s=this.getKey(e);if(!this.enabled||!s)return;(await N.createStorageWriter(this.container,s,N.StorageCategory.Necessary))(t)}}class W{constructor(t,e,s){y(this,"agent");y(this,"user");y(this,"httpClient");y(this,"events");y(this,"groups",[]);y(this,"store");this.container=t,this.conversation=e;const{events:n}=this.container.get("$widget");this.events=n,this.store=new z(t),this.agent=e.createAgent({id:"knowledge-bot-agent",name:"",avatar:void 0}),this.user=e.user;const r=q(t,s);this.httpClient=new j(r,n,this.store,this.container),this.events.subscribe("provider:widget-entry",(c,u)=>{this.onWidgetEntryReceived(u)}),this.events.subscribe("provider:open-contact-method",async(c,u)=>{const E=await this.httpClient.showContactMethodAction(u);E&&(u.contactMethod.title&&this.printUserMessage(u.contactMethod.title),this.onMessagesReceived(E))})}async initialize(){var r;const t=this.conversation.loading(),e=await this.store.read("meta");e!=null&&e.agent&&this.agent.update({name:e.agent.name,avatar:e.agent.avatar});const s=await this.conversation.isRehydrated(),n=await this.httpClient.start({silent:((r=this.conversation.options)==null?void 0:r.silent)||s});n&&this.onMessagesReceived(n),t()}async onMessagesReceived(t){this.groups=this.groups.concat(t),t.forEach(e=>{const s=e.items.map(n=>n.message);e.isUser?e.writer=this.user.print(s):e.writer=this.agent.print(s)})}async onInput(t){const e=typeof t=="string"?t:t.actionKey;switch(e){case"next":{if(typeof t=="string")return;const s=this.groups.find(n=>{var r;return n.id===((r=t.data)==null?void 0:r.groupId)});if(s){const n=await this.httpClient.nextAction(s);if(n){const r=n.items.map(c=>c.message);s.meta.paging=n==null?void 0:n.meta.paging,s.writer.update(r)}}}break;case"help":{const s=this.conversation.loading(),n=await this.httpClient.helpAction();s(),n&&this.onMessagesReceived(n)}break;default:{typeof t!="string"&&t.label&&this.printUserMessage(t.label);const s=this.conversation.loading(),n=await this.httpClient.defaultAction(e);n&&this.onMessagesReceived(n),s()}break}}printUserMessage(t){const e=H({isUser:!0,items:[{id:k.uuid(),isUser:!0,message:[m.ConversationMessageListItemType.HTML,{body:t}]}]});return this.onMessagesReceived([e]),e}async onMessageSend(t){const e=this.conversation.loading(),s=await this.httpClient.sendMessage(t);await A(this.container,"phrase",t),this.printUserMessage(t),s&&this.onMessagesReceived(s),e()}async onWidgetEntryReceived(t){if(t.agent){const e={name:t.agent.name,avatar:t.agent.avatar};this.agent.update(e),(await N.createStorageWriter(this.container,"bot-provider-conversation-meta",N.StorageCategory.Necessary))({agent:e})}}}const _=async i=>{const t=await m.ConversationPlatform.getInstance(i);m.registerCustomMessageComponent(i,f.ContactList,Promise.resolve().then(()=>require("./contact-method-list-542b9f49.js"))),m.registerCustomMessageComponent(i,f.ContactMethod,Promise.resolve().then(()=>require("./contact-method-29b72b18.js"))),m.registerCustomMessageComponent(i,f.NotificationList,Promise.resolve().then(()=>require("./notification-list-d4d9695d.js"))),m.registerCustomMessageComponent(i,f.NoGoodAlternative,Promise.resolve().then(()=>require("./no-good-alternative-0ced8ab9.js"))),m.registerCustomMessageComponent(i,f.FeedbackList,Promise.resolve().then(()=>require("./feedback-list-48c7bffb.js"))),t.registerProvider("ace-knowledge-bot",(e,s)=>{const n=new W(i,e,s);n.initialize();const r=[];return r.push(s.actions.watch("conversation.user-submit",(c,u)=>{n.onMessageSend(c.text)})),r.push(s.actions.watch("conversation.action",(c,u)=>(n.onInput(c),u(c)))),r.push(s.actions.create("evaluate",c=>{const{events:u}=i.get("$widget");u.dispatch("bot-provider:evaluate",c)})),r.push(s.actions.create("submit",c=>{const{events:u}=i.get("$widget");u.dispatch("bot-provider:form-submitted",c)})),()=>{r.forEach(c=>{c()})}})};exports.KnowledgeBotProviderPlugin=_;exports.updateForm=B;