@iai-lib/any-chat-widget 0.1.0-alpha.35 → 0.1.0-alpha.36
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.
|
@@ -7967,10 +7967,19 @@ async function xl({
|
|
|
7967
7967
|
}), t.chatManager.endChat("Transferred to live agent", !1);
|
|
7968
7968
|
const a = new Xn();
|
|
7969
7969
|
await a.createSession();
|
|
7970
|
-
const {
|
|
7970
|
+
const {
|
|
7971
|
+
summary: o,
|
|
7972
|
+
subject: l,
|
|
7973
|
+
guestCardId: d,
|
|
7974
|
+
...u
|
|
7975
|
+
} = e;
|
|
7971
7976
|
await a.createConversation({
|
|
7972
|
-
contact:
|
|
7977
|
+
contact: u,
|
|
7973
7978
|
attributes: {
|
|
7979
|
+
...d && {
|
|
7980
|
+
gc_id: d,
|
|
7981
|
+
"Chat.gc_id": d
|
|
7982
|
+
},
|
|
7974
7983
|
Question: l,
|
|
7975
7984
|
"AI Summary": o
|
|
7976
7985
|
}
|
|
@@ -500,7 +500,7 @@ Please report this to https://github.com/markedjs/marked.`,s){const n="<p>An err
|
|
|
500
500
|
</div>
|
|
501
501
|
`)}
|
|
502
502
|
</iai-chat-carousel>
|
|
503
|
-
`}function dl(s){return async function({id:t,params:n,chatWindow:i}){i.setInputDisabled(!0);const r={...n.guestCardId&&{guestCardId:n.guestCardId},...n.customerFirstName&&{firstName:n.customerFirstName},...n.customerLastName&&{lastName:n.customerLastName},...n.customerEmail&&{email:n.customerEmail},...n.customerPhoneNumber&&{phone:n.customerPhoneNumber},...n.desiredMoveInDate&&{moveInDate:n.desiredMoveInDate}},a=document.createElement("iai-chat-iframe-modal");a.title="Schedule a Tour",a.src=`${s}?${new URLSearchParams(r).toString()}`,a.showHeader=!1;const o=()=>{a.remove(),window.removeEventListener("beforeunload",u),i.setInputDisabled(!1)},l=async({detail:p})=>{if(p.type!=="tourScheduled")return;const{tourType:c,tourDate:m,tourTime:R,guestCardId:F}=p;o(),i.addMessage({role:"system",content:"Your tour has been scheduled!"}),await i.sendFunctionCallResponse(t,{status:"scheduled",tourType:c,tourDate:m,tourTime:R,guestCardId:F},!0)},d=async()=>{o(),i.addMessage({role:"system",content:"Tour Scheduler Closed"}),await i.sendFunctionCallResponse(t,{status:"canceled"},!0)},u=async()=>{o(),i.addMessage({role:"system",content:"Tour Scheduler Closed"})};a.addEventListener("message-received",l),a.addEventListener("close",d),window.addEventListener("beforeunload",u),document.body.appendChild(a),i.addMessage({role:"system",content:"Tour Scheduler Opened"})}}function yr(s){return{all:s=s||new Map,on:function(e,t){var n=s.get(e);n?n.push(t):s.set(e,[t])},off:function(e,t){var n=s.get(e);n&&(t?n.splice(n.indexOf(t)>>>0,1):s.set(e,[]))},emit:function(e,t){var n=s.get(e);n&&n.slice().map(function(i){i(t)}),(n=s.get("*"))&&n.slice().map(function(i){i(e,t)})}}}class ul{constructor(e){this.chatWsUrl=e,this._emitter=yr(),this.on=this._emitter.on.bind(this._emitter),this.off=this._emitter.off.bind(this._emitter),this.reconnectCodes=new Set([1006]),this.reconnectAttempts=0,this.maxReconnectAttempts=10}connect(){const e=new WebSocket(this.chatWsUrl);e.onopen=async()=>{this.reconnectAttempts=0,e.send(JSON.stringify({type:"ready"}))},e.onmessage=t=>{const n=JSON.parse(t.data);this._emitter.emit(n.type,n.data)},e.onclose=t=>{console.log("Disconnected: ",t.code,t.reason||"No reason provided"),this._emitter.emit("disconnected",t),this.reconnectCodes.has(t.code)&&this.reconnectAttempts<this.maxReconnectAttempts&&(console.log("Reconnecting..."),this._emitter.emit("reconnecting"),this.reconnectAttempts++,setTimeout(()=>this.connect(),this.reconnectAttempts*1e3))},e.onerror=t=>{console.log("Error:",t),this._emitter.emit("error",t)},this.ws=e}reconnect(){this.reconnectAttempts=0,this.connect()}disconnect(){var e;(e=this.ws)==null||e.close()}destroy(){this.disconnect(),this._emitter.all.clear()}}var cs;(s=>{(e=>{e.TEXT="TEXT"})(s.MessageType||(s.MessageType={})),(e=>{e.DELIVERED="DELIVERED",e.READ="READ",e.FAILED="FAILED"})(s.AcknowledgeType||(s.AcknowledgeType={})),(e=>{e.GENERIC="GENERIC",e.SMS="SMS",e.RCS="RCS",e.FACEBOOK="FACEBOOK",e.TWITTER="TWITTER",e.WHATSAPP="WHATSAPP",e.WECHAT="WECHAT",e.QQMOBILE="QQMOBILE",e.APPLE_BUSINESS_CHAT="APPLE_BUSINESS_CHAT",e.SKYPE="SKYPE",e.SNAPCHAT="SNAPCHAT",e.VIBER="VIBER",e.LINE="LINE",e.TELEGRAM="TELEGRAM",e.KAKAO="KAKAO"})(s.ContentType||(s.ContentType={})),(e=>{e.SYSTEM="SYSTEM",e.USER="USER"})(s.ConversationEventOrigin||(s.ConversationEventOrigin={})),(e=>{e.PENDING="PENDING",e.ACTIVE="ACTIVE",e.TERMINATED="TERMINATED"})(s.ConversationStatus||(s.ConversationStatus={}))})(cs||(cs={}));const hl=cs.AcknowledgeType,pl="Anyone Home",fl="IB Chat Leasing - Development";let ml="https://five9.ah.inhabit.work";const gl="https://five9.ah.inhabit.work".replace("http","ws").replace(/\/$/,""),En=class En{constructor(){Ys(this,Ve,void 0);Ys(this,Et,void 0);this._emitter=yr(),this.on=this._emitter.on.bind(this._emitter),this.off=this._emitter.off.bind(this._emitter),lt(this,Ve,null),lt(this,Et,null),this.conversationStarted=!1}serialize(){return JSON.stringify({sessionToken:this.sessionToken,conversationId:this.conversationId,conversationStarted:this.conversationStarted})}static deserialize(e){const t=JSON.parse(e),n=new En;return t.sessionToken&&n.setSession(t.sessionToken),t.conversationId&&n.setConversationId(t.conversationId),n.conversationStarted=t.conversationStarted||!1,n}static generateBaseUrl(e){return`https://${e}/appsvcs/rs/svc`}async createSession(){const e=await fetch(`${En.generateBaseUrl("app-scl.five9.com")}/auth/anon?cookieless=true`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({tenantName:pl})});return lt(this,Ve,await e.json()),hs(this,Ve)}setSession(e){lt(this,Ve,e)}setConversationId(e){lt(this,Et,e),this.five9WebSocket&&(this.five9WebSocket.destroy(),this.five9WebSocket=void 0,e&&this.connectToWebSocket())}connect(){if(!this.sessionToken)throw new Error("Session not created");if(!this.conversationId)throw new Error("Conversation not started");this.connectToWebSocket()}connectToWebSocket(){if(!this.conversationId)throw new Error("Conversation not started");this.five9WebSocket&&this.five9WebSocket.destroy();const e=new ul(`${gl}/conversations/${this.conversationId}/ws`);e.on("*",(t,n)=>{this._emitter.emit(t,n)}),e.on("conversationCreated",t=>{this.conversationStarted=!0,console.log("Conversation Created:",t)}),e.on("conversationAccepted",t=>{console.log("Conversation Accepted:",t)}),e.on("agentJoined",t=>{console.log("Agent Joined:",t)}),e.on("agentTransferredConversation",t=>{console.log("Agent Transferred Conversation:",t)}),e.on("conversationTransferredToGroup",t=>{console.log("Conversation Transferred To Group:",t)}),e.on("agentLeftConversation",t=>{console.log("Agent Left Conversation:",t)}),e.on("agentTyping",t=>{console.log("Agent Typing:",t)}),e.on("message",async t=>{console.log("Message:",t),await this.acknowledgeMessage({messages:[{messageId:t.messageId,type:hl.READ}]})}),e.on("conversationTerminated",t=>{console.log("Conversation Terminated:",t)}),e.on("error",t=>{console.log("Error:",t)}),this.five9WebSocket=e,e.connect()}async authenticatedFetch(e,t={}){const{sessionToken:n}=this;if(!n)throw new Error("Session not found");e=e.startsWith("/")?e:`/${e}`;const i=n.metadata.dataCenters[0].apiUrls[0].host,r=await fetch(`${En.generateBaseUrl(i)}${e}`,{...t,headers:{...t.headers,"Content-Type":"application/json",Accept:"application/json",Authorization:n.tokenId,farmId:n.context.farmId}});return r.status===204?null:r.json()}async getMetadata(){if(!this.sessionToken)throw new Error("Session not created");return this.authenticatedFetch("/auth/metadata",{method:"GET"})}async createConversation(e){if(!this.sessionToken)throw new Error("Session not created");const t=e.contact||{};Object.keys(t).forEach(r=>{const a=r;t[a]||delete t[a]});const n={...e,campaignName:fl,tenantId:this.tenantId,callbackUrl:ml};console.log("body",n);const i=await this.authenticatedFetch("/conversations",{method:"POST",body:JSON.stringify(n)});return this.setConversationId(i.id),i}async sendMessage(e){const{conversationId:t,conversationStarted:n}=this;if(!t||!n)throw new Error("Conversation not started");const i={messageType:cs.MessageType.TEXT,message:e};await this.authenticatedFetch(`/conversations/${t}/messages`,{method:"POST",body:JSON.stringify(i)})}async sendTyping(){const{conversationId:e}=this;if(!e)throw new Error("Conversation not started");await this.authenticatedFetch(`/conversations/${e}/messages/typing`,{method:"PUT"})}async acknowledgeMessage(e){const{conversationId:t}=this;if(!t)throw new Error("Conversation not started");await this.authenticatedFetch(`/conversations/${t}/messages/acknowledge`,{method:"PUT",body:JSON.stringify(e)})}async terminateConversation(){const{conversationId:e}=this;if(!e)throw new Error("Conversation not started");await this.authenticatedFetch(`/conversations/${e}/terminate`,{method:"POST"}),this.destroy()}destroy(){this.five9WebSocket&&this.five9WebSocket.destroy(),lt(this,Ve,null),lt(this,Et,null)}get sessionToken(){return hs(this,Ve)}get conversationId(){return hs(this,Et)}get dataCenter(){var e;return(e=this.sessionToken)==null?void 0:e.metadata.dataCenters[0]}get apiUrl(){var e;return(e=this.dataCenter)==null?void 0:e.apiUrls[0].host}get tenantId(){var e;return(e=this.sessionToken)==null?void 0:e.orgId}get tokenId(){var e;return(e=this.sessionToken)==null?void 0:e.tokenId}get farmId(){var e;return(e=this.sessionToken)==null?void 0:e.context.farmId}};Ve=new WeakMap,Et=new WeakMap;let ds=En;function Sn(s){return`any-${s||"main"}-chatSession`}async function yl({id:s,params:e,chatWindow:t,html:n}){t=t;const i=async()=>{t.removeMessage("transferMessageId"),await t.sendFunctionCallResponse(s,{transfer:"Declined"},!0),t.setInputDisabled(!1)},r=async()=>{t.setInputDisabled(!0),t.removeMessage("transferMessageId"),t.addMessage({role:"system",content:"Please wait while we connect you to a live agent"}),t.chatManager.endChat("Transferred to live agent",!1);const a=new ds;await a.createSession();const{summary:o,subject:l,...
|
|
503
|
+
`}function dl(s){return async function({id:t,params:n,chatWindow:i}){i.setInputDisabled(!0);const r={...n.guestCardId&&{guestCardId:n.guestCardId},...n.customerFirstName&&{firstName:n.customerFirstName},...n.customerLastName&&{lastName:n.customerLastName},...n.customerEmail&&{email:n.customerEmail},...n.customerPhoneNumber&&{phone:n.customerPhoneNumber},...n.desiredMoveInDate&&{moveInDate:n.desiredMoveInDate}},a=document.createElement("iai-chat-iframe-modal");a.title="Schedule a Tour",a.src=`${s}?${new URLSearchParams(r).toString()}`,a.showHeader=!1;const o=()=>{a.remove(),window.removeEventListener("beforeunload",u),i.setInputDisabled(!1)},l=async({detail:p})=>{if(p.type!=="tourScheduled")return;const{tourType:c,tourDate:m,tourTime:R,guestCardId:F}=p;o(),i.addMessage({role:"system",content:"Your tour has been scheduled!"}),await i.sendFunctionCallResponse(t,{status:"scheduled",tourType:c,tourDate:m,tourTime:R,guestCardId:F},!0)},d=async()=>{o(),i.addMessage({role:"system",content:"Tour Scheduler Closed"}),await i.sendFunctionCallResponse(t,{status:"canceled"},!0)},u=async()=>{o(),i.addMessage({role:"system",content:"Tour Scheduler Closed"})};a.addEventListener("message-received",l),a.addEventListener("close",d),window.addEventListener("beforeunload",u),document.body.appendChild(a),i.addMessage({role:"system",content:"Tour Scheduler Opened"})}}function yr(s){return{all:s=s||new Map,on:function(e,t){var n=s.get(e);n?n.push(t):s.set(e,[t])},off:function(e,t){var n=s.get(e);n&&(t?n.splice(n.indexOf(t)>>>0,1):s.set(e,[]))},emit:function(e,t){var n=s.get(e);n&&n.slice().map(function(i){i(t)}),(n=s.get("*"))&&n.slice().map(function(i){i(e,t)})}}}class ul{constructor(e){this.chatWsUrl=e,this._emitter=yr(),this.on=this._emitter.on.bind(this._emitter),this.off=this._emitter.off.bind(this._emitter),this.reconnectCodes=new Set([1006]),this.reconnectAttempts=0,this.maxReconnectAttempts=10}connect(){const e=new WebSocket(this.chatWsUrl);e.onopen=async()=>{this.reconnectAttempts=0,e.send(JSON.stringify({type:"ready"}))},e.onmessage=t=>{const n=JSON.parse(t.data);this._emitter.emit(n.type,n.data)},e.onclose=t=>{console.log("Disconnected: ",t.code,t.reason||"No reason provided"),this._emitter.emit("disconnected",t),this.reconnectCodes.has(t.code)&&this.reconnectAttempts<this.maxReconnectAttempts&&(console.log("Reconnecting..."),this._emitter.emit("reconnecting"),this.reconnectAttempts++,setTimeout(()=>this.connect(),this.reconnectAttempts*1e3))},e.onerror=t=>{console.log("Error:",t),this._emitter.emit("error",t)},this.ws=e}reconnect(){this.reconnectAttempts=0,this.connect()}disconnect(){var e;(e=this.ws)==null||e.close()}destroy(){this.disconnect(),this._emitter.all.clear()}}var cs;(s=>{(e=>{e.TEXT="TEXT"})(s.MessageType||(s.MessageType={})),(e=>{e.DELIVERED="DELIVERED",e.READ="READ",e.FAILED="FAILED"})(s.AcknowledgeType||(s.AcknowledgeType={})),(e=>{e.GENERIC="GENERIC",e.SMS="SMS",e.RCS="RCS",e.FACEBOOK="FACEBOOK",e.TWITTER="TWITTER",e.WHATSAPP="WHATSAPP",e.WECHAT="WECHAT",e.QQMOBILE="QQMOBILE",e.APPLE_BUSINESS_CHAT="APPLE_BUSINESS_CHAT",e.SKYPE="SKYPE",e.SNAPCHAT="SNAPCHAT",e.VIBER="VIBER",e.LINE="LINE",e.TELEGRAM="TELEGRAM",e.KAKAO="KAKAO"})(s.ContentType||(s.ContentType={})),(e=>{e.SYSTEM="SYSTEM",e.USER="USER"})(s.ConversationEventOrigin||(s.ConversationEventOrigin={})),(e=>{e.PENDING="PENDING",e.ACTIVE="ACTIVE",e.TERMINATED="TERMINATED"})(s.ConversationStatus||(s.ConversationStatus={}))})(cs||(cs={}));const hl=cs.AcknowledgeType,pl="Anyone Home",fl="IB Chat Leasing - Development";let ml="https://five9.ah.inhabit.work";const gl="https://five9.ah.inhabit.work".replace("http","ws").replace(/\/$/,""),En=class En{constructor(){Ys(this,Ve,void 0);Ys(this,Et,void 0);this._emitter=yr(),this.on=this._emitter.on.bind(this._emitter),this.off=this._emitter.off.bind(this._emitter),lt(this,Ve,null),lt(this,Et,null),this.conversationStarted=!1}serialize(){return JSON.stringify({sessionToken:this.sessionToken,conversationId:this.conversationId,conversationStarted:this.conversationStarted})}static deserialize(e){const t=JSON.parse(e),n=new En;return t.sessionToken&&n.setSession(t.sessionToken),t.conversationId&&n.setConversationId(t.conversationId),n.conversationStarted=t.conversationStarted||!1,n}static generateBaseUrl(e){return`https://${e}/appsvcs/rs/svc`}async createSession(){const e=await fetch(`${En.generateBaseUrl("app-scl.five9.com")}/auth/anon?cookieless=true`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({tenantName:pl})});return lt(this,Ve,await e.json()),hs(this,Ve)}setSession(e){lt(this,Ve,e)}setConversationId(e){lt(this,Et,e),this.five9WebSocket&&(this.five9WebSocket.destroy(),this.five9WebSocket=void 0,e&&this.connectToWebSocket())}connect(){if(!this.sessionToken)throw new Error("Session not created");if(!this.conversationId)throw new Error("Conversation not started");this.connectToWebSocket()}connectToWebSocket(){if(!this.conversationId)throw new Error("Conversation not started");this.five9WebSocket&&this.five9WebSocket.destroy();const e=new ul(`${gl}/conversations/${this.conversationId}/ws`);e.on("*",(t,n)=>{this._emitter.emit(t,n)}),e.on("conversationCreated",t=>{this.conversationStarted=!0,console.log("Conversation Created:",t)}),e.on("conversationAccepted",t=>{console.log("Conversation Accepted:",t)}),e.on("agentJoined",t=>{console.log("Agent Joined:",t)}),e.on("agentTransferredConversation",t=>{console.log("Agent Transferred Conversation:",t)}),e.on("conversationTransferredToGroup",t=>{console.log("Conversation Transferred To Group:",t)}),e.on("agentLeftConversation",t=>{console.log("Agent Left Conversation:",t)}),e.on("agentTyping",t=>{console.log("Agent Typing:",t)}),e.on("message",async t=>{console.log("Message:",t),await this.acknowledgeMessage({messages:[{messageId:t.messageId,type:hl.READ}]})}),e.on("conversationTerminated",t=>{console.log("Conversation Terminated:",t)}),e.on("error",t=>{console.log("Error:",t)}),this.five9WebSocket=e,e.connect()}async authenticatedFetch(e,t={}){const{sessionToken:n}=this;if(!n)throw new Error("Session not found");e=e.startsWith("/")?e:`/${e}`;const i=n.metadata.dataCenters[0].apiUrls[0].host,r=await fetch(`${En.generateBaseUrl(i)}${e}`,{...t,headers:{...t.headers,"Content-Type":"application/json",Accept:"application/json",Authorization:n.tokenId,farmId:n.context.farmId}});return r.status===204?null:r.json()}async getMetadata(){if(!this.sessionToken)throw new Error("Session not created");return this.authenticatedFetch("/auth/metadata",{method:"GET"})}async createConversation(e){if(!this.sessionToken)throw new Error("Session not created");const t=e.contact||{};Object.keys(t).forEach(r=>{const a=r;t[a]||delete t[a]});const n={...e,campaignName:fl,tenantId:this.tenantId,callbackUrl:ml};console.log("body",n);const i=await this.authenticatedFetch("/conversations",{method:"POST",body:JSON.stringify(n)});return this.setConversationId(i.id),i}async sendMessage(e){const{conversationId:t,conversationStarted:n}=this;if(!t||!n)throw new Error("Conversation not started");const i={messageType:cs.MessageType.TEXT,message:e};await this.authenticatedFetch(`/conversations/${t}/messages`,{method:"POST",body:JSON.stringify(i)})}async sendTyping(){const{conversationId:e}=this;if(!e)throw new Error("Conversation not started");await this.authenticatedFetch(`/conversations/${e}/messages/typing`,{method:"PUT"})}async acknowledgeMessage(e){const{conversationId:t}=this;if(!t)throw new Error("Conversation not started");await this.authenticatedFetch(`/conversations/${t}/messages/acknowledge`,{method:"PUT",body:JSON.stringify(e)})}async terminateConversation(){const{conversationId:e}=this;if(!e)throw new Error("Conversation not started");await this.authenticatedFetch(`/conversations/${e}/terminate`,{method:"POST"}),this.destroy()}destroy(){this.five9WebSocket&&this.five9WebSocket.destroy(),lt(this,Ve,null),lt(this,Et,null)}get sessionToken(){return hs(this,Ve)}get conversationId(){return hs(this,Et)}get dataCenter(){var e;return(e=this.sessionToken)==null?void 0:e.metadata.dataCenters[0]}get apiUrl(){var e;return(e=this.dataCenter)==null?void 0:e.apiUrls[0].host}get tenantId(){var e;return(e=this.sessionToken)==null?void 0:e.orgId}get tokenId(){var e;return(e=this.sessionToken)==null?void 0:e.tokenId}get farmId(){var e;return(e=this.sessionToken)==null?void 0:e.context.farmId}};Ve=new WeakMap,Et=new WeakMap;let ds=En;function Sn(s){return`any-${s||"main"}-chatSession`}async function yl({id:s,params:e,chatWindow:t,html:n}){t=t;const i=async()=>{t.removeMessage("transferMessageId"),await t.sendFunctionCallResponse(s,{transfer:"Declined"},!0),t.setInputDisabled(!1)},r=async()=>{t.setInputDisabled(!0),t.removeMessage("transferMessageId"),t.addMessage({role:"system",content:"Please wait while we connect you to a live agent"}),t.chatManager.endChat("Transferred to live agent",!1);const a=new ds;await a.createSession();const{summary:o,subject:l,guestCardId:d,...u}=e;await a.createConversation({contact:u,attributes:{...d&&{gc_id:d,"Chat.gc_id":d},Question:l,"AI Summary":o}}),localStorage.setItem(Sn(t.cleanOptions.config.historyKey),a.serialize()),vr(t,a)};t.addMessage({id:"transferMessageId",role:"system",contentHtmlTemplate:n`
|
|
504
504
|
<style>
|
|
505
505
|
.btn {
|
|
506
506
|
background-color: var(--iai-chat-i-message-user-bg);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iai-lib/any-chat-widget",
|
|
3
3
|
"license": "SEE LICENSE IN LICENSE.md",
|
|
4
|
-
"version": "0.1.0-alpha.
|
|
4
|
+
"version": "0.1.0-alpha.36",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"@iai-lib/chat-widget": "^1.0.0-alpha.38",
|
|
38
38
|
"mitt": "^3.0.1"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "21cef3e08ccdcb61a2e509b7bd38180b1d5aac09"
|
|
41
41
|
}
|