@telia-ace/knowledge-widget-bot-provider 1.1.1-rc.3 → 1.1.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.
@@ -1,3 +1,3 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("react/jsx-runtime"),A=require("@emotion/styled"),F=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"),k=require("./index-76cfe150.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[t,s]=c.useState(),d=p.useContainer(),D=p.useDispatch(),b=c.useRef(!0),l=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!==t.id)return;l&&(r.form.meta||(r.form.meta={}),r.form.meta.conversation=l);const y=await d.getAsync("contactMethods"),e={...t,body:{...t.body,form:r.form}},{errors:i,valid:h,errorTypes:N}=await y.validate(e);if(!h){const S=new T.FormBuilder(t.body.form);s(void 0),S.setValidationErrors(i),e.body={...e.body,form:S.get()},s({...e});return}const E=await y.submit(e);e.confirmation=E,s(e)}),a=u.subscribe("bot-provider:evaluate",async(f,r)=>{if(r.id!==n)return;const e=await(await d.getAsync("contactMethods")).evaluate(t);let i={...t,body:{...t.body,form:e}};const h=k.updateForm(i,{submit:"Submit",errors:{},meta:{groupId:m}});i={...i,body:h},s(i)});return()=>{q(),a()}},[d,t,s,l]);const g=c.useCallback(async u=>{const a=await(await F.DataClient.getInstance(d)).fetch(F.DataType.ContactMethod,{contactMethodId:u,guideId:o},{noCache:!0}),f=k.updateForm(a,{submit:"Submit",errors:{},meta:{groupId:m}});if(a.body=f,a.body.guideId=o,!b.current)return null;s(a)},[b]);return c.useEffect(()=>(g(n),()=>{b.current=!1}),[n,g]),t?j.jsx(B,{...t,body:{...t.body,form:t.body.form?{...t.body.form,data:(C=t.body.form)==null?void 0:C.data,layout:{...t.body.form.layout},meta:(v=t.body.form)==null?void 0:v.meta,schema:(w=t.body.form)==null?void 0:w.schema}:void 0,guideId:t.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"),F=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"),k=require("./index-e264a8f9.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[t,s]=c.useState(),d=p.useContainer(),D=p.useDispatch(),b=c.useRef(!0),l=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!==t.id)return;l&&(r.form.meta||(r.form.meta={}),r.form.meta.conversation=l);const y=await d.getAsync("contactMethods"),e={...t,body:{...t.body,form:r.form}},{errors:i,valid:h,errorTypes:N}=await y.validate(e);if(!h){const S=new T.FormBuilder(t.body.form);s(void 0),S.setValidationErrors(i),e.body={...e.body,form:S.get()},s({...e});return}const E=await y.submit(e);e.confirmation=E,s(e)}),a=u.subscribe("bot-provider:evaluate",async(f,r)=>{if(r.id!==n)return;const e=await(await d.getAsync("contactMethods")).evaluate(t);let i={...t,body:{...t.body,form:e}};const h=k.updateForm(i,{submit:"Submit",errors:{},meta:{groupId:m}});i={...i,body:h},s(i)});return()=>{q(),a()}},[d,t,s,l]);const g=c.useCallback(async u=>{const a=await(await F.DataClient.getInstance(d)).fetch(F.DataType.ContactMethod,{contactMethodId:u,guideId:o},{noCache:!0}),f=k.updateForm(a,{submit:"Submit",errors:{},meta:{groupId:m}});if(a.body=f,a.body.guideId=o,!b.current)return null;s(a)},[b]);return c.useEffect(()=>(g(n),()=>{b.current=!1}),[n,g]),t?j.jsx(B,{...t,body:{...t.body,form:t.body.form?{...t.body.form,data:(C=t.body.form)==null?void 0:C.data,layout:{...t.body.form.layout},meta:(v=t.body.form)==null?void 0:v.meta,schema:(w=t.body.form)==null?void 0:w.schema}:void 0,guideId:t.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;
@@ -6,7 +6,7 @@ import { useContainer as R, useDispatch as T, useConversation as _ } from "@teli
6
6
  import { FormBuilder as j } from "@telia-ace/widget-forms";
7
7
  import { appendClassNames as k } from "@telia-ace/widget-utilities";
8
8
  import { useState as G, useRef as I, useEffect as S, useCallback as V } from "react";
9
- import { u as $ } from "./index-ddac8cac.mjs";
9
+ import { u as $ } from "./index-ff32882c.mjs";
10
10
  import "@telia-ace/widget-conversation";
11
11
  import "@telia-ace/widget-services";
12
12
  const tt = ({
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),l=require("@emotion/styled"),g=require("@telia-ace/knowledge-widget-adapters"),f=require("@telia-ace/knowledge-widget-ui"),u=require("react"),b=require("./contact-method-be72fba9.js");require("@telia-ace/knowledge-data-client");require("@telia-ace/widget-forms");require("@telia-ace/widget-utilities");require("./index-76cfe150.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");const j=({actions:e,entryId:t,groupId:y})=>{const x=f.useDispatch(),d=f.useContainer(),[h,q]=u.useState([]),[o,C]=u.useState([]);return u.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)C([...o,r.id]);else{const{events:M}=d.get("$widget");M.dispatch("provider:open-contact-method",{entryId:t,contactMethod:r,actionKey:i.actionKey})}}}});Promise.all(c).then(i=>{q(i.filter(s=>!!s))})})},[e,d,t,o]),h.length?a.jsx(S,{className:"humany-bot-contact-list",contactMethods:h,dispatch:x,itemRenderer:(n,c)=>(o.includes(n.id)||n.expanded)&&n.inline?a.jsx($,{contactMethodId:n.id,groupId:y,guideId:n.body.guideId}):a.jsx(k,{...n,...c})}):null},S=l(g.ContactList)`
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react/jsx-runtime"),l=require("@emotion/styled"),g=require("@telia-ace/knowledge-widget-adapters"),f=require("@telia-ace/knowledge-widget-ui"),u=require("react"),b=require("./contact-method-46dd96f8.js");require("@telia-ace/knowledge-data-client");require("@telia-ace/widget-forms");require("@telia-ace/widget-utilities");require("./index-e264a8f9.js");require("@telia-ace/widget-conversation");require("@telia-ace/widget-services");const j=({actions:e,entryId:t,groupId:y})=>{const x=f.useDispatch(),d=f.useContainer(),[h,q]=u.useState([]),[o,C]=u.useState([]);return u.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)C([...o,r.id]);else{const{events:M}=d.get("$widget");M.dispatch("provider:open-contact-method",{entryId:t,contactMethod:r,actionKey:i.actionKey})}}}});Promise.all(c).then(i=>{q(i.filter(s=>!!s))})})},[e,d,t,o]),h.length?a.jsx(S,{className:"humany-bot-contact-list",contactMethods:h,dispatch:x,itemRenderer:(n,c)=>(o.includes(n.id)||n.expanded)&&n.inline?a.jsx($,{contactMethodId:n.id,groupId:y,guideId:n.body.guideId}):a.jsx(k,{...n,...c})}):null},S=l(g.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 E } from "@telia-ace/knowledge-widget-ui";
5
5
  import { useState as f, useEffect as S } from "react";
6
- import j from "./contact-method-37007137.mjs";
6
+ import j from "./contact-method-5b71554e.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-ddac8cac.mjs";
10
+ import "./index-ff32882c.mjs";
11
11
  import "@telia-ace/widget-conversation";
12
12
  import "@telia-ace/widget-services";
13
13
  const F = ({ actions: t, entryId: e, groupId: h }) => {
@@ -0,0 +1 @@
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 I={actions:[],entryId:a.id,groupId:o.id};if(p&&(I.header=p),(v==null?void 0:v.length)>0){const h=U(a);if(I=s(h,a,o.id,I),g.length){const C=g.find(L=>L.tags.includes("paging")),T=g.find(L=>L.tags.includes("fallback"));if(C!=null&&C.content&&(o.meta.paging={actions:C.actions,items:C.content.items,localState:C.localState},C.content.items.forEach(L=>{I.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,I],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 C;return h.type==="List"&&((C=h.localState)==null?void 0:C.$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")),I=g.find(h=>h.tags.includes("contact")&&h.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(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.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-93f20ff3.js"))),m.registerCustomMessageComponent(i,f.ContactMethod,Promise.resolve().then(()=>require("./contact-method-46dd96f8.js"))),m.registerCustomMessageComponent(i,f.NotificationList,Promise.resolve().then(()=>require("./notification-list-2ba3e3d1.js"))),m.registerCustomMessageComponent(i,f.NoGoodAlternative,Promise.resolve().then(()=>require("./no-good-alternative-553f1e16.js"))),m.registerCustomMessageComponent(i,f.FeedbackList,Promise.resolve().then(()=>require("./feedback-list-34683249.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;
@@ -37,7 +37,7 @@ const j = (s) => {
37
37
  exitType: n,
38
38
  confirmationText: c,
39
39
  expandFormAutomatically: r,
40
- form: g,
40
+ form: u,
41
41
  description: M
42
42
  }, K = {}) => {
43
43
  const { adapter: a } = i || { Adapter: {} }, { clientName: o = "", settings: h = {}, customClientName: S = "" } = a, { validation: l } = K, d = {
@@ -54,7 +54,7 @@ const j = (s) => {
54
54
  const { displayTextBeforeClick: k, textBeforeClick: m } = h;
55
55
  k === "true" && (d.expanded = !1, d.title = m), o.indexOf("phone.text") > -1 ? d.body.phoneNumber = h.phoneNumber : d.body.freetext = h.freetext;
56
56
  }
57
- 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), g && g.hasValueComponents && (l ? d.body.form = H.fromLegacyForm(s.toString(), g, l).get() : d.body.form = H.fromLegacyForm(s.toString(), g).get(), r || (d.expanded = d.inline = !1), d.body = D(d, { submit: "Submit" })), K.guideId && (d.body.guideId = K.guideId), d;
57
+ 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(s.toString(), u, l).get() : d.body.form = H.fromLegacyForm(s.toString(), u).get(), r || (d.expanded = d.inline = !1), d.body = D(d, { submit: "Submit" })), K.guideId && (d.body.guideId = K.guideId), d;
58
58
  }, W = (s) => {
59
59
  const { localState: t } = s;
60
60
  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;
@@ -130,9 +130,9 @@ const j = (s) => {
130
130
  groupId: o.id
131
131
  };
132
132
  if (m && (L.header = m), (v == null ? void 0 : v.length) > 0) {
133
- const u = W(a);
133
+ const g = W(a);
134
134
  if (L = i(
135
- u,
135
+ g,
136
136
  a,
137
137
  o.id,
138
138
  L
@@ -182,7 +182,7 @@ const j = (s) => {
182
182
  w.unshift(
183
183
  b({
184
184
  messageId: l,
185
- message: [u, L],
185
+ message: [g, L],
186
186
  group: o
187
187
  })
188
188
  );
@@ -208,16 +208,16 @@ const j = (s) => {
208
208
  })
209
209
  ), h.length) {
210
210
  const v = h.find(
211
- (u) => {
211
+ (g) => {
212
212
  var I;
213
- return u.type === "List" && ((I = u.localState) == null ? void 0 : I.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching";
213
+ return g.type === "List" && ((I = g.localState) == null ? void 0 : I.$type) === "Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching";
214
214
  }
215
215
  ), x = h.find(
216
- (u) => u.tags.includes("dialog")
216
+ (g) => g.tags.includes("dialog")
217
217
  ), w = h.find(
218
- (u) => u.tags.includes("feedback")
218
+ (g) => g.tags.includes("feedback")
219
219
  ), L = h.find(
220
- (u) => u.tags.includes("contact") && u.type === "Contact"
220
+ (g) => g.tags.includes("contact") && g.type === "Contact"
221
221
  );
222
222
  if (L && m.push(
223
223
  b({
@@ -238,16 +238,17 @@ const j = (s) => {
238
238
  message: [
239
239
  y.ButtonList,
240
240
  {
241
- actions: x.content.items.map((u) => ({
242
- label: u.title,
243
- actionKey: u.actionKey
241
+ actions: x.content.items.map((g) => ({
242
+ label: g.title,
243
+ actionKey: g.actionKey,
244
+ data: g.data
244
245
  }))
245
246
  }
246
247
  ],
247
248
  group: o
248
249
  })
249
250
  ), v) {
250
- const u = i(
251
+ const g = i(
251
252
  p.ContactList,
252
253
  v,
253
254
  o.id
@@ -264,7 +265,7 @@ const j = (s) => {
264
265
  ), m.push(
265
266
  b({
266
267
  messageId: l,
267
- message: [p.ContactList, u],
268
+ message: [p.ContactList, g],
268
269
  group: o
269
270
  })
270
271
  );
@@ -287,9 +288,9 @@ const j = (s) => {
287
288
  message: [
288
289
  p.FeedbackList,
289
290
  {
290
- actions: w.content.items.map((u) => ({
291
- actionKey: u.actionKey,
292
- label: u.title
291
+ actions: w.content.items.map((g) => ({
292
+ actionKey: g.actionKey,
293
+ label: g.title
293
294
  }))
294
295
  }
295
296
  ]
@@ -388,7 +389,7 @@ const j = (s) => {
388
389
  ["List", 2],
389
390
  ["Contact", 3],
390
391
  ["CategorizedNoticeList", 10]
391
- ]), g = [], M = t.sort((a, o) => {
392
+ ]), u = [], M = t.sort((a, o) => {
392
393
  const S = r.get(a.type) || 20, l = r.get(o.type) || 20;
393
394
  return S - l;
394
395
  }).filter((a) => !a.relation).reduce((a, o) => a.concat(c(o)), []), K = (a) => typeof a.actions.selector < "u";
@@ -398,9 +399,9 @@ const j = (s) => {
398
399
  ), h = P({ isUser: !1 });
399
400
  (await n(a, h, o, e)).forEach((l) => {
400
401
  h.items.push(l);
401
- }), h.items.length && g.push(h);
402
+ }), h.items.length && u.push(h);
402
403
  }
403
- return g;
404
+ return u;
404
405
  };
405
406
  class q {
406
407
  constructor(t, e, i, n) {
@@ -623,14 +624,14 @@ class X {
623
624
  this.container
624
625
  ), this.events.subscribe(
625
626
  "provider:widget-entry",
626
- (r, g) => {
627
- this.onWidgetEntryReceived(g);
627
+ (r, u) => {
628
+ this.onWidgetEntryReceived(u);
628
629
  }
629
630
  ), this.events.subscribe(
630
631
  "provider:open-contact-method",
631
- async (r, g) => {
632
- const M = await this.httpClient.showContactMethodAction(g);
633
- M && (g.contactMethod.title && this.printUserMessage(g.contactMethod.title), this.onMessagesReceived(M));
632
+ async (r, u) => {
633
+ const M = await this.httpClient.showContactMethodAction(u);
634
+ M && (u.contactMethod.title && this.printUserMessage(u.contactMethod.title), this.onMessagesReceived(M));
634
635
  }
635
636
  );
636
637
  }
@@ -727,11 +728,11 @@ const st = async (s) => {
727
728
  N(
728
729
  s,
729
730
  p.ContactList,
730
- import("./contact-method-list-672114d0.mjs")
731
+ import("./contact-method-list-e487c035.mjs")
731
732
  ), N(
732
733
  s,
733
734
  p.ContactMethod,
734
- import("./contact-method-37007137.mjs")
735
+ import("./contact-method-5b71554e.mjs")
735
736
  ), N(
736
737
  s,
737
738
  p.NotificationList,
@@ -749,20 +750,20 @@ const st = async (s) => {
749
750
  n.initialize();
750
751
  const c = [];
751
752
  return c.push(
752
- i.actions.watch("conversation.user-submit", (r, g) => {
753
+ i.actions.watch("conversation.user-submit", (r, u) => {
753
754
  n.onMessageSend(r.text);
754
755
  })
755
756
  ), c.push(
756
- i.actions.watch("conversation.action", (r, g) => (n.onInput(r), g(r)))
757
+ i.actions.watch("conversation.action", (r, u) => (n.onInput(r), u(r)))
757
758
  ), c.push(
758
759
  i.actions.create("evaluate", (r) => {
759
- const { events: g } = s.get("$widget");
760
- g.dispatch("bot-provider:evaluate", r);
760
+ const { events: u } = s.get("$widget");
761
+ u.dispatch("bot-provider:evaluate", r);
761
762
  })
762
763
  ), c.push(
763
764
  i.actions.create("submit", (r) => {
764
- const { events: g } = s.get("$widget");
765
- g.dispatch("bot-provider:form-submitted", r);
765
+ const { events: u } = s.get("$widget");
766
+ u.dispatch("bot-provider:form-submitted", r);
766
767
  })
767
768
  ), () => {
768
769
  c.forEach((r) => {
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index-76cfe150.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-e264a8f9.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-ddac8cac.mjs";
1
+ import { K as n } from "./index-ff32882c.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.1-rc.3",
3
+ "version": "1.1.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.1-rc.3",
14
- "@telia-ace/knowledge-widget-adapters": "^1.1.1-rc.3",
15
- "@telia-ace/knowledge-widget-ui": "^1.1.1-rc.3",
16
- "@telia-ace/widget-conversation": "^1.1.1-rc.3",
17
- "@telia-ace/widget-forms": "^1.1.1-rc.3",
18
- "@telia-ace/widget-services": "^1.1.1-rc.3",
19
- "@telia-ace/widget-utilities": "^1.1.1-rc.3",
13
+ "@telia-ace/knowledge-data-client": "^1.1.1",
14
+ "@telia-ace/knowledge-widget-adapters": "^1.1.1",
15
+ "@telia-ace/knowledge-widget-ui": "^1.1.1",
16
+ "@telia-ace/widget-conversation": "^1.1.1",
17
+ "@telia-ace/widget-forms": "^1.1.1",
18
+ "@telia-ace/widget-services": "^1.1.1",
19
+ "@telia-ace/widget-utilities": "^1.1.1",
20
20
  "@webprovisions/platform": "^1.1.4",
21
- "@telia-ace/knowledge-widget-core": "^1.1.1-rc.3"
21
+ "@telia-ace/knowledge-widget-core": "^1.1.1"
22
22
  },
23
23
  "peerDependencies": {
24
24
  "@emotion/react": "11.11.1",
package/index-76cfe150.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:h,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),h&&h.hasValueComponents&&(l?d.body.form=P.FormBuilder.fromLegacyForm(i.toString(),h,l).get():d.body.form=P.FormBuilder.fromLegacyForm(i.toString(),h).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 I={actions:[],entryId:a.id,groupId:o.id};if(p&&(I.header=p),(v==null?void 0:v.length)>0){const u=U(a);if(I=s(u,a,o.id,I),g.length){const C=g.find(L=>L.tags.includes("paging")),T=g.find(L=>L.tags.includes("fallback"));if(C!=null&&C.content&&(o.meta.paging={actions:C.actions,items:C.content.items,localState:C.localState},C.content.items.forEach(L=>{I.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:[u,I],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(u=>{var C;return u.type==="List"&&((C=u.localState)==null?void 0:C.$type)==="Humany.Matching.Bots.Conversations.States.ContactState, Humany.Matching"}),x=g.find(u=>u.tags.includes("dialog")),w=g.find(u=>u.tags.includes("feedback")),I=g.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}))}],group:o})),v){const u=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,u],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(u=>({actionKey:u.actionKey,label:u.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]]),h=[],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&&h.push(g)}return h};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.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,h)=>{this.onWidgetEntryReceived(h)}),this.events.subscribe("provider:open-contact-method",async(c,h)=>{const E=await this.httpClient.showContactMethodAction(h);E&&(h.contactMethod.title&&this.printUserMessage(h.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-a44fe57a.js"))),m.registerCustomMessageComponent(i,f.ContactMethod,Promise.resolve().then(()=>require("./contact-method-be72fba9.js"))),m.registerCustomMessageComponent(i,f.NotificationList,Promise.resolve().then(()=>require("./notification-list-2ba3e3d1.js"))),m.registerCustomMessageComponent(i,f.NoGoodAlternative,Promise.resolve().then(()=>require("./no-good-alternative-553f1e16.js"))),m.registerCustomMessageComponent(i,f.FeedbackList,Promise.resolve().then(()=>require("./feedback-list-34683249.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,h)=>{n.onMessageSend(c.text)})),r.push(s.actions.watch("conversation.action",(c,h)=>(n.onInput(c),h(c)))),r.push(s.actions.create("evaluate",c=>{const{events:h}=i.get("$widget");h.dispatch("bot-provider:evaluate",c)})),r.push(s.actions.create("submit",c=>{const{events:h}=i.get("$widget");h.dispatch("bot-provider:form-submitted",c)})),()=>{r.forEach(c=>{c()})}})};exports.KnowledgeBotProviderPlugin=_;exports.updateForm=B;