@eka-care/medassist-widget-embed 0.2.41 → 0.2.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{fullscreen.html → eka-agents.html} +14 -5
- package/dist/iframe.js +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -2
- package/package.json +2 -2
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
<link
|
|
15
15
|
rel="preload"
|
|
16
16
|
as="script"
|
|
17
|
-
href="https://unpkg.com/@eka-care/medassist-widget@
|
|
17
|
+
href="https://unpkg.com/@eka-care/medassist-widget@0.1.62/dist/medassist-widget.js"
|
|
18
18
|
fetchpriority="high"
|
|
19
19
|
crossorigin
|
|
20
20
|
/>
|
|
@@ -23,14 +23,14 @@
|
|
|
23
23
|
<link
|
|
24
24
|
rel="preload"
|
|
25
25
|
as="style"
|
|
26
|
-
href="https://unpkg.com/@eka-care/medassist-widget@
|
|
26
|
+
href="https://unpkg.com/@eka-care/medassist-widget@0.1.62/dist/medassist-widget.css"
|
|
27
27
|
onload="this.rel='stylesheet'"
|
|
28
28
|
crossorigin
|
|
29
29
|
/>
|
|
30
30
|
<noscript>
|
|
31
31
|
<link
|
|
32
32
|
rel="stylesheet"
|
|
33
|
-
href="https://unpkg.com/@eka-care/medassist-widget@
|
|
33
|
+
href="https://unpkg.com/@eka-care/medassist-widget@0.1.62/dist/medassist-widget.css"
|
|
34
34
|
/>
|
|
35
35
|
</noscript>
|
|
36
36
|
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
|
|
63
63
|
<!-- Production: served from unpkg. For local dev swap with: <script src="./src/medassist-widget.js" onload="init()" async></script> -->
|
|
64
64
|
<script
|
|
65
|
-
src="https://unpkg.com/@eka-care/medassist-widget@
|
|
65
|
+
src="https://unpkg.com/@eka-care/medassist-widget@0.1.62/dist/medassist-widget.js"
|
|
66
66
|
onload="init()"
|
|
67
67
|
fetchpriority="high"
|
|
68
68
|
crossorigin
|
|
@@ -70,6 +70,11 @@
|
|
|
70
70
|
></script>
|
|
71
71
|
|
|
72
72
|
<script>
|
|
73
|
+
// Restore widget when user returns via browser back button (bfcache restore).
|
|
74
|
+
window.addEventListener("pageshow",function(e){
|
|
75
|
+
if(e.persisted)window.location.reload();
|
|
76
|
+
});
|
|
77
|
+
|
|
73
78
|
function getEnvironment(v){
|
|
74
79
|
return v==="production"||v==="development"||v==="staging"?v:undefined;
|
|
75
80
|
}
|
|
@@ -108,12 +113,14 @@
|
|
|
108
113
|
var apiTheme=mapAgentConfigTheme(cfg&&cfg.theme);
|
|
109
114
|
var mergedTheme=Object.assign({},apiTheme||{},urlTheme||{});
|
|
110
115
|
|
|
116
|
+
var redirectUrl=p.get("redirectUrl");
|
|
117
|
+
|
|
111
118
|
var container=document.getElementById("root");
|
|
112
119
|
container.textContent="";
|
|
113
120
|
|
|
114
121
|
window.renderMedAssist(container,agentId,{
|
|
115
122
|
displayMode:"full",
|
|
116
|
-
showCloseButton:
|
|
123
|
+
showCloseButton:true,
|
|
117
124
|
environment:getEnvironment(p.get("environment")),
|
|
118
125
|
baseUrl:p.get("baseUrl")||undefined,
|
|
119
126
|
title:p.get("title")||(cfg&&cfg.name)||undefined,
|
|
@@ -125,6 +132,8 @@
|
|
|
125
132
|
context:decodeContext(p.get("context")),
|
|
126
133
|
theme:Object.keys(mergedTheme).length?mergedTheme:undefined,
|
|
127
134
|
onClose:function(){
|
|
135
|
+
if(redirectUrl){window.location.href=redirectUrl;return}
|
|
136
|
+
if(document.referrer){window.location.href=document.referrer;return}
|
|
128
137
|
if(history.length>1)history.back();
|
|
129
138
|
else window.close();
|
|
130
139
|
}
|
package/dist/iframe.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";(function(){const x=()=>{if(typeof document=="undefined")return null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},$=e=>{if(e==="production"||e==="development"||e==="staging")return e};function W(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background&&{background:e.background},...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.title_img&&{titleImg:e.title_img}}}function E(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function F(e,t){const r=`${e.replace(/\/$/,"")}/med-assist/agent-config/${t}`,n=new AbortController,i=setTimeout(()=>n.abort(),1e4);try{const s=await fetch(r,{signal:n.signal,headers:{"ngrok-skip-browser-warning":"69420"}});if(!s.ok)return;const c=await s.json();return c!=null&&c.success&&(c!=null&&c.data)?c.data:void 0}catch{return}finally{clearTimeout(i)}}const _="https://unpkg.com/@eka-care/medassist-widget@latest/dist/",a=x(),C=(()=>{if(!a)return"";try{return new URL(".",a.src||window.location.href).href}catch{return""}})(),m=(()=>{if(!a)return _;const e=a.dataset.widgetAssets;if(e==="local")return`${C}src/`;if(e&&e.length>0)try{return new URL(e,C).href}catch{return e.endsWith("/")?e:`${e}/`}return _})(),l=`${m}medassist-widget.js`,g=`${m}medassist-widget.css`;let f=null,w=null;const d=new URLSearchParams(typeof window!="undefined"?window.location.search:""),S=d.get("agentId"),p=d.get("baseUrl")||"https://matrix.eka.care/reloaded";if(typeof document!="undefined"&&document.head){try{const e=new URL(m).origin,t=new URL(p).origin,r=document.createElement("link");if(r.rel="preconnect",r.href=e,r.crossOrigin="anonymous",document.head.appendChild(r),e!==t){const s=document.createElement("link");s.rel="preconnect",s.href=t,s.crossOrigin="anonymous",document.head.appendChild(s)}const n=document.createElement("link");n.rel="preload",n.as="script",n.href=l,document.head.appendChild(n);const i=document.createElement("link");i.rel="preload",i.as="style",i.href=g,document.head.appendChild(i)}catch{}I(),U()}const b=S&&p?F(p,S):Promise.resolve(void 0);if(typeof document!="undefined"&&document.head){const e=d.get("theme");if(e)try{const t=JSON.parse(e);t.backgroundImage&&E(t.backgroundImage)}catch{}b.then(t=>{var r;const n=(r=t==null?void 0:t.theme)===null||r===void 0?void 0:r.background_img;n&&E(n)})}const A=async()=>{var e,t,r,n,i,s;const c=d.get("agentId");if(!c){console.error("Agent ID is required in URL parameters");return}let h=d.get("title")||"Medi Clinic",y=d.get("iconUrl")||void 0;const L=d.get("context"),R=d.get("baseUrl")||"https://matrix.eka.care/reloaded",B=d.get("resize")==="true"?!0:void 0,D=(e=$(d.get("environment")))!==null&&e!==void 0?e:"production",v=d.get("connectivity"),z=v==="sse"||v==="socket"?v:void 0,k=d.get("theme")?(()=>{try{return JSON.parse(d.get("theme")||"{}")}catch{return}})():void 0,G=document.getElementById("root")||document.body;try{const[,,o]=await Promise.all([I(),U(),b]);if(!(window!=null&&window.renderMedAssist)||typeof(window==null?void 0:window.renderMedAssist)!="function")throw new Error("renderMedAssist is not available on window");let u,M,T,P,O=!1;o&&(h=(t=o==null?void 0:o.name)!==null&&t!==void 0?t:h,y=(n=(r=o==null?void 0:o.theme)===null||r===void 0?void 0:r.icon_img)!==null&&n!==void 0?n:y,u=W((i=o==null?void 0:o.theme)!==null&&i!==void 0?i:void 0),M=o==null?void 0:o.allowed,T=o==null?void 0:o.connectivity,P=o==null?void 0:o.initial_message,O=(s=o==null?void 0:o.hide_watermark)!==null&&s!==void 0?s:!1);const J={title:h,iconUrl:y,environment:D,connectionType:T||z||"
|
|
1
|
+
"use strict";(function(){const x=()=>{if(typeof document=="undefined")return null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},$=e=>{if(e==="production"||e==="development"||e==="staging")return e};function W(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background&&{background:e.background},...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.title_img&&{titleImg:e.title_img}}}function E(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function F(e,t){const r=`${e.replace(/\/$/,"")}/med-assist/agent-config/${t}`,n=new AbortController,i=setTimeout(()=>n.abort(),1e4);try{const s=await fetch(r,{signal:n.signal,headers:{"ngrok-skip-browser-warning":"69420"}});if(!s.ok)return;const c=await s.json();return c!=null&&c.success&&(c!=null&&c.data)?c.data:void 0}catch{return}finally{clearTimeout(i)}}const _="https://unpkg.com/@eka-care/medassist-widget@latest/dist/",a=x(),C=(()=>{if(!a)return"";try{return new URL(".",a.src||window.location.href).href}catch{return""}})(),m=(()=>{if(!a)return _;const e=a.dataset.widgetAssets;if(e==="local")return`${C}src/`;if(e&&e.length>0)try{return new URL(e,C).href}catch{return e.endsWith("/")?e:`${e}/`}return _})(),l=`${m}medassist-widget.js`,g=`${m}medassist-widget.css`;let f=null,w=null;const d=new URLSearchParams(typeof window!="undefined"?window.location.search:""),S=d.get("agentId"),p=d.get("baseUrl")||"https://matrix.eka.care/reloaded";if(typeof document!="undefined"&&document.head){try{const e=new URL(m).origin,t=new URL(p).origin,r=document.createElement("link");if(r.rel="preconnect",r.href=e,r.crossOrigin="anonymous",document.head.appendChild(r),e!==t){const s=document.createElement("link");s.rel="preconnect",s.href=t,s.crossOrigin="anonymous",document.head.appendChild(s)}const n=document.createElement("link");n.rel="preload",n.as="script",n.href=l,document.head.appendChild(n);const i=document.createElement("link");i.rel="preload",i.as="style",i.href=g,document.head.appendChild(i)}catch{}I(),U()}const b=S&&p?F(p,S):Promise.resolve(void 0);if(typeof document!="undefined"&&document.head){const e=d.get("theme");if(e)try{const t=JSON.parse(e);t.backgroundImage&&E(t.backgroundImage)}catch{}b.then(t=>{var r;const n=(r=t==null?void 0:t.theme)===null||r===void 0?void 0:r.background_img;n&&E(n)})}const A=async()=>{var e,t,r,n,i,s;const c=d.get("agentId");if(!c){console.error("Agent ID is required in URL parameters");return}let h=d.get("title")||"Medi Clinic",y=d.get("iconUrl")||void 0;const L=d.get("context"),R=d.get("baseUrl")||"https://matrix.eka.care/reloaded",B=d.get("resize")==="true"?!0:void 0,D=(e=$(d.get("environment")))!==null&&e!==void 0?e:"production",v=d.get("connectivity"),z=v==="sse"||v==="socket"?v:void 0,k=d.get("theme")?(()=>{try{return JSON.parse(d.get("theme")||"{}")}catch{return}})():void 0,G=document.getElementById("root")||document.body;try{const[,,o]=await Promise.all([I(),U(),b]);if(!(window!=null&&window.renderMedAssist)||typeof(window==null?void 0:window.renderMedAssist)!="function")throw new Error("renderMedAssist is not available on window");let u,M,T,P,O=!1;o&&(h=(t=o==null?void 0:o.name)!==null&&t!==void 0?t:h,y=(n=(r=o==null?void 0:o.theme)===null||r===void 0?void 0:r.icon_img)!==null&&n!==void 0?n:y,u=W((i=o==null?void 0:o.theme)!==null&&i!==void 0?i:void 0),M=o==null?void 0:o.allowed,T=o==null?void 0:o.connectivity,P=o==null?void 0:o.initial_message,O=(s=o==null?void 0:o.hide_watermark)!==null&&s!==void 0?s:!1);const J={title:h,iconUrl:y,environment:D,connectionType:T||z||"sse",onClose:()=>{window.parent!==window&&window.parent.postMessage({type:"WIDGET_CLOSE"},"*")},context:L?JSON.parse(L):void 0,baseUrl:R,displayMode:"full",allowed:M,showCloseButton:!1,resize:B,initialMessage:P,hideWatermark:O,theme:{...u!=null?u:{},...k!=null?k:{}}};window.renderMedAssist(G,c,J)}catch(o){throw console.error("Failed to initialize MedAssist widget",o),o}};async function I(){document.querySelector("link[data-medassist-style='true']")||(w||(w=new Promise((t,r)=>{const n=document.createElement("link");n.rel="stylesheet",n.href=g,n.setAttribute("data-medassist-style","true"),n.onload=()=>{t("")},n.onerror=()=>{r(new Error(`Failed to load ${g}`))},document.head.appendChild(n)})),await w)}function U(){return f||(f=new Promise((e,t)=>{const r=document.querySelector(`script[src="${l}"]`);if(r){if(r.dataset.loaded==="true"){e();return}r.addEventListener("load",()=>e()),r.addEventListener("error",i=>t(i.error||new Error("Unknown script loading error")));return}const n=document.createElement("script");n.src=l,n.async=!0,n.dataset.widget="medassist",n.onload=()=>{n.dataset.loaded="true",e()},n.onerror=i=>{if(i instanceof ErrorEvent&&i.error){t(i.error);return}t(new Error(`Failed to load ${l}`))},document.head.appendChild(n)})),f}if(typeof document!="undefined")if(document.readyState==="loading")document.addEventListener("DOMContentLoaded",A);else try{A()}catch(e){console.error("Failed to initialize MedAssist widget",e)}})();
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";const getCurrentScript=()=>{if(typeof document=="undefined")return console.error("document is not defined"),null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},getEnvironment=e=>{if(e==="production"||e==="development"||e==="staging")return e},scriptEl=getCurrentScript(),DEFAULT_WIDGET_ASSET_BASE=(()=>{var e;return!((e=scriptEl==null?void 0:scriptEl.src)===null||e===void 0)&&e.includes("@dev")?"https://unpkg.com/@eka-care/medassist-widget@dev/dist/":"https://unpkg.com/@eka-care/medassist-widget@latest/dist/"})();function mapAgentConfigThemeToWidgetTheme(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.mode&&{mode:e.mode},...e.header_tinted!==void 0&&{headerTinted:e.header_tinted},...e.title_img&&{titleImg:e.title_img},...e.tagline&&{tagline:e.tagline}}}function preloadImage(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function fetchAgentConfig(e,t){const o=`${e.replace(/\/$/,"")}/med-assist/agent-config/${t}`,i=await fetch(o,{headers:{"ngrok-skip-browser-warning":"69420"}});if(!i.ok)return;const n=await i.json();if(n!=null&&n.success&&(n!=null&&n.data))return n.data}const NUDGE_STORAGE_KEY="eka-medassist-nudges";function getNudgeStore(){try{const e=localStorage.getItem(NUDGE_STORAGE_KEY);return e?JSON.parse(e):{}}catch{return{}}}function setNudgeStore(e){try{localStorage.setItem(NUDGE_STORAGE_KEY,JSON.stringify(e))}catch{}}function normalizePath(e){let t=e.replace(/\/+$/,"");return t.startsWith("/")||(t="/"+t),t||"/"}function findCachedNudge(e,t){const i=getNudgeStore()[e];if(!i)return null;let n;try{n=new URL(t)}catch{return null}const s=n.host,r=i[s];if(!r)return null;const d=n.pathname.split("/").filter(Boolean),c=[];for(let l=d.length;l>=0;l--)c.push(l===0?"/":"/"+d.slice(0,l).join("/"));for(const l of c){if(r[l]!==void 0)return{domain:s,path:l,data:r[l]};const u=l==="/"?"/*":l+"/*";if(r[u]!==void 0)return{domain:s,path:u,data:r[u]}}return null}function clearNudgeForPath(e,t,o){const i=getNudgeStore(),n=i[e];!n||!n[t]||(delete n[t][o],setNudgeStore(i))}function storeNudgeResponse(e,t){var o,i,n,s;const r=(o=t==null?void 0:t.url_pattern)===null||o===void 0?void 0:o.domain,d=(i=t==null?void 0:t.url_pattern)===null||i===void 0?void 0:i.path;if(!(!((n=t.nudges)===null||n===void 0)&&n.length)||!t.expiry||!r||!d)return;const c=getNudgeStore();c[e]||(c[e]={}),c[e][r]||(c[e][r]={}),c[e][r][d]={nudges:t.nudges,expiry:t.expiry,delay:(s=t.delay)!==null&&s!==void 0?s:5},setNudgeStore(c)}const NUDGE_COOKIE_PREFIX="eka-nudge";function setNudgeCookie(e,t){const o=new Date(Date.now()+864e5).toUTCString();document.cookie=`${NUDGE_COOKIE_PREFIX}-${e}=${t}; expires=${o}; path=/; SameSite=Lax`}function getNudgeCookie(e){const t=document.cookie.split(";").map(i=>i.trim()).find(i=>i.startsWith(`${NUDGE_COOKIE_PREFIX}-${e}=`));if(!t)return null;const o=t.split("=")[1];return o==="open"||o==="closed"?o:null}async function fetchNudgeData(e,t){const o=`${t}/med-assist/user-nudge`,i=[];typeof document!="undefined"&&document.querySelectorAll("meta").forEach(n=>{var s,r,d;const c=(s=n.getAttribute("name"))!==null&&s!==void 0?s:void 0,l=(r=n.getAttribute("property"))!==null&&r!==void 0?r:void 0,u=(d=n.getAttribute("content"))!==null&&d!==void 0?d:void 0;(c||l)&&i.push({name:c,property:l,content:u})});try{const n=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json","x-agent-id":e,referer:typeof window!="undefined"?window.location.href:""},body:JSON.stringify({meta_tags:i,url:typeof window!="undefined"?window.location.href:""})});return n.ok?await n.json():void 0}catch{return}}const globalMedAssistConfig={},getWidgetElement=()=>typeof document=="undefined"?null:document.querySelector("eka-medassist-widget");if(typeof window!="undefined"){const e=window;e.__ekaMedAssistConfig__=globalMedAssistConfig,e.EkaMedAssist={init(t){var o,i,n;Object.assign(globalMedAssistConfig,t);const s=getWidgetElement();if(s){if(t.agentId&&s.setAttribute("agent-id",t.agentId),t.title&&s.setAttribute("title",t.title),t.iconUrl&&s.setAttribute("icon-url",t.iconUrl),t.baseUrl&&s.setAttribute("base-url",t.baseUrl),t.resize!==void 0&&s.setAttribute("resize",String(t.resize)),(t.displayMode==="full"||t.displayMode==="widget")&&s.setAttribute("display-mode",t.displayMode),t.context)try{s.setAttribute("context",JSON.stringify(t.context))}catch{console.warn("Failed to stringify context passed to init")}typeof t.customLauncherStyles=="string"&&s.setAttribute("custom-launcher-styles",t.customLauncherStyles),!((o=t.theme)===null||o===void 0)&&o.backgroundImage&&preloadImage(t.theme.backgroundImage),(n=(i=s).openFromBridge)===null||n===void 0||n.call(i)}}}}const scriptBaseUrl=(()=>{if(!scriptEl)return"";try{return new URL(".",scriptEl.src||window.location.href).href}catch{return""}})(),widgetAssetBaseUrl=(()=>{if(!scriptEl)return DEFAULT_WIDGET_ASSET_BASE;const e=scriptEl.dataset.widgetAssets;if(e==="local")return`${scriptBaseUrl}src/`;if(e&&e.length>0)try{return new URL(e,scriptBaseUrl).href}catch{return e.endsWith("/")?e:`${e}/`}return DEFAULT_WIDGET_ASSET_BASE})(),WIDGET_JS_URL=`${widgetAssetBaseUrl}medassist-widget.js`,WIDGET_CSS_URL=`${widgetAssetBaseUrl}medassist-widget.css`;let widgetScriptPromise=null,widgetCssTextPromise=null;function getWidgetCssTextPromise(){return widgetCssTextPromise||(widgetCssTextPromise=fetch(WIDGET_CSS_URL).then(e=>{if(!e.ok)throw new Error(`Unable to fetch widget styles from ${WIDGET_CSS_URL}`);return e.text()})),widgetCssTextPromise}class MedAssistWidgetLoader extends HTMLElement{getAgentConfig(){if(!this.agentConfigPromise){const t=(typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{},o=t.baseUrl||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",i=t.agentId||this.getAttribute("agent-id")||"";if(!i)return Promise.resolve(void 0);this.agentConfigPromise=fetchAgentConfig(o,i).catch(()=>{})}return this.agentConfigPromise}constructor(){super(),this.reactRoot=null,this.agentConfigPromise=null,this.attachShadow({mode:"open"}),this.defaultIconUrl="https://cdn.eka.care/bot-icon.svg",this.widgetLoaded=!1,this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.setupAuthExpirationListener()}static get observedAttributes(){return["icon-url","display-mode","custom-launcher-styles"]}connectedCallback(){this.displayMode==="full"?(this.initializeFullMode(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to load MedAssist widget in full mode",t)})):(this.renderButton(),this.preloadWidgetAssets(),this.initNudge())}attributeChangedCallback(t){if(t==="icon-url"&&this.displayMode==="widget"&&this.renderButton(),t==="display-mode"){const o=this.getAttribute("display-mode")==="full"?"full":"widget";o!==this.displayMode&&(this.displayMode=o,o==="full"?this.initializeFullMode():this.renderButton())}t==="custom-launcher-styles"&&this.applyCustomLauncherStyles()}preloadWidgetAssets(){getWidgetCssTextPromise(),this.loadWidgetScript(),this.preloadBackgroundImage()}preloadBackgroundImage(){var t;const o=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,i=this.getAttribute("theme")?(()=>{try{return JSON.parse(this.getAttribute("theme")||"{}")}catch{return}})():void 0,n=(t=o==null?void 0:o.theme)===null||t===void 0?void 0:t.backgroundImage,s=i==null?void 0:i.backgroundImage;n&&preloadImage(n),s&&s!==n&&preloadImage(s),this.getAgentConfig().then(r=>{var d;const c=(d=r==null?void 0:r.theme)===null||d===void 0?void 0:d.background_img;c&&preloadImage(c)}).catch(()=>{})}setupAuthExpirationListener(){if(typeof window=="undefined")return;const t=o=>{var i;((i=o.data)===null||i===void 0?void 0:i.type)==="AUTH_EXPIRED"&&window.parent!==window&&window.parent.postMessage({type:"AUTH_EXPIRED",message:"Authentication expired"},"*")};window.addEventListener("message",t)}openFromBridge(){this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.displayMode==="full"?this.initializeFullMode():this.renderButton(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to open MedAssist widget from bridge",t)})}async initNudge(){var t,o,i,n;const s=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,r=(s==null?void 0:s.agentId)||this.getAttribute("agent-id")||"";if(!r)return;const d=await this.getAgentConfig();if(!(d!=null&&d.nudge))return;const c=(o=(t=d==null?void 0:d.theme)===null||t===void 0?void 0:t.nudge_color)!==null&&o!==void 0?o:"#ffffff",l=typeof window!="undefined"?window.location.href:"";let u=findCachedNudge(r,l);if(u){const f=Math.floor(Date.now()/1e3);u.data.expiry<=f&&(clearNudgeForPath(r,u.domain,u.path),u=null)}if(!u){const f=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,w=(f==null?void 0:f.baseUrl)||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",p=await fetchNudgeData(r,w).catch(()=>{});if(!(!((i=p==null?void 0:p.nudges)===null||i===void 0)&&i.length)||(storeNudgeResponse(r,p),u=findCachedNudge(r,l),!u))return}const h=u.data.nudges[Math.floor(Math.random()*u.data.nudges.length)],m=(n=u.data.delay)!==null&&n!==void 0?n:5;setTimeout(()=>{this.showNudgePopup(h,c)},m*1e3)}showNudgePopup(t,o){var i,n;const s=this.shadowRoot;if(!s||!(!((i=s.getElementById("medassist-widget-root"))===null||i===void 0)&&i.classList.contains("hidden")))return;const d=((typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{}).agentId||this.getAttribute("agent-id")||"";if(d&&getNudgeCookie(d)!==null||s.getElementById("medassist-nudge-popup"))return;const l=document.createElement("div");l.id="medassist-nudge-popup",l.innerHTML=`
|
|
1
|
+
"use strict";const getCurrentScript=()=>{if(typeof document=="undefined")return console.error("document is not defined"),null;const{currentScript:e}=document;if(e instanceof HTMLScriptElement)return e;const t=document.getElementsByTagName("script");return t.length?t[t.length-1]:null},getEnvironment=e=>{if(e==="production"||e==="development"||e==="staging")return e},scriptEl=getCurrentScript(),DEFAULT_WIDGET_ASSET_BASE=(()=>{var e;return!((e=scriptEl==null?void 0:scriptEl.src)===null||e===void 0)&&e.includes("@dev")?"https://unpkg.com/@eka-care/medassist-widget@dev/dist/":"https://unpkg.com/@eka-care/medassist-widget@latest/dist/"})();function mapAgentConfigThemeToWidgetTheme(e){if(!e)return;const t=e.mode==="dark"?"white":e.mode==="light"?"black":void 0;return{...e.background_img&&{backgroundImage:e.background_img},...e.accent&&{primary:e.accent},...t&&{textColor:t},...e.mode&&{mode:e.mode},...e.header_tinted!==void 0&&{headerTinted:e.header_tinted},...e.title_img&&{titleImg:e.title_img},...e.tagline&&{tagline:e.tagline}}}function preloadImage(e){if(!(!e||typeof document=="undefined"))try{const t=document.createElement("link");t.rel="preload",t.as="image",t.href=e,document.head.appendChild(t)}catch{const t=new Image;t.src=e}}async function fetchAgentConfig(e,t){const o=`${e.replace(/\/$/,"")}/med-assist/agent-config/${t}`,i=await fetch(o,{headers:{"ngrok-skip-browser-warning":"69420"}});if(!i.ok)return;const n=await i.json();if(n!=null&&n.success&&(n!=null&&n.data))return n.data}const NUDGE_STORAGE_KEY="eka-medassist-nudges";function getNudgeStore(){try{const e=localStorage.getItem(NUDGE_STORAGE_KEY);return e?JSON.parse(e):{}}catch{return{}}}function setNudgeStore(e){try{localStorage.setItem(NUDGE_STORAGE_KEY,JSON.stringify(e))}catch{}}function normalizePath(e){let t=e.replace(/\/+$/,"");return t.startsWith("/")||(t="/"+t),t||"/"}function findCachedNudge(e,t){const i=getNudgeStore()[e];if(!i)return null;let n;try{n=new URL(t)}catch{return null}const s=n.host,r=i[s];if(!r)return null;const d=n.pathname.split("/").filter(Boolean),c=[];for(let l=d.length;l>=0;l--)c.push(l===0?"/":"/"+d.slice(0,l).join("/"));for(const l of c){if(r[l]!==void 0)return{domain:s,path:l,data:r[l]};const u=l==="/"?"/*":l+"/*";if(r[u]!==void 0)return{domain:s,path:u,data:r[u]}}return null}function clearNudgeForPath(e,t,o){const i=getNudgeStore(),n=i[e];!n||!n[t]||(delete n[t][o],setNudgeStore(i))}function storeNudgeResponse(e,t){var o,i,n,s;const r=(o=t==null?void 0:t.url_pattern)===null||o===void 0?void 0:o.domain,d=(i=t==null?void 0:t.url_pattern)===null||i===void 0?void 0:i.path;if(!(!((n=t.nudges)===null||n===void 0)&&n.length)||!t.expiry||!r||!d)return;const c=getNudgeStore();c[e]||(c[e]={}),c[e][r]||(c[e][r]={}),c[e][r][d]={nudges:t.nudges,expiry:t.expiry,delay:(s=t.delay)!==null&&s!==void 0?s:5},setNudgeStore(c)}const NUDGE_COOKIE_PREFIX="eka-nudge";function setNudgeCookie(e,t){const o=new Date(Date.now()+864e5).toUTCString();document.cookie=`${NUDGE_COOKIE_PREFIX}-${e}=${t}; expires=${o}; path=/; SameSite=Lax`}function getNudgeCookie(e){const t=document.cookie.split(";").map(i=>i.trim()).find(i=>i.startsWith(`${NUDGE_COOKIE_PREFIX}-${e}=`));if(!t)return null;const o=t.split("=")[1];return o==="open"||o==="closed"?o:null}async function fetchNudgeData(e,t){const o=`${t}/med-assist/user-nudge`,i=[];typeof document!="undefined"&&document.querySelectorAll("meta").forEach(n=>{var s,r,d;const c=(s=n.getAttribute("name"))!==null&&s!==void 0?s:void 0,l=(r=n.getAttribute("property"))!==null&&r!==void 0?r:void 0,u=(d=n.getAttribute("content"))!==null&&d!==void 0?d:void 0;(c||l)&&i.push({name:c,property:l,content:u})});try{const n=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json","x-agent-id":e,referer:typeof window!="undefined"?window.location.href:""},body:JSON.stringify({meta_tags:i,url:typeof window!="undefined"?window.location.href:""})});return n.ok?await n.json():void 0}catch{return}}const globalMedAssistConfig={},getWidgetElement=()=>typeof document=="undefined"?null:document.querySelector("eka-medassist-widget");if(typeof window!="undefined"){const e=window;e.__ekaMedAssistConfig__=globalMedAssistConfig,e.EkaMedAssist={init(t){var o,i,n;Object.assign(globalMedAssistConfig,t);const s=getWidgetElement();if(s){if(t.agentId&&s.setAttribute("agent-id",t.agentId),t.title&&s.setAttribute("title",t.title),t.iconUrl&&s.setAttribute("icon-url",t.iconUrl),t.baseUrl&&s.setAttribute("base-url",t.baseUrl),t.resize!==void 0&&s.setAttribute("resize",String(t.resize)),(t.displayMode==="full"||t.displayMode==="widget")&&s.setAttribute("display-mode",t.displayMode),t.context)try{s.setAttribute("context",JSON.stringify(t.context))}catch{console.warn("Failed to stringify context passed to init")}typeof t.customLauncherStyles=="string"&&s.setAttribute("custom-launcher-styles",t.customLauncherStyles),t.redirectUrl&&s.setAttribute("redirect-url",t.redirectUrl),!((o=t.theme)===null||o===void 0)&&o.backgroundImage&&preloadImage(t.theme.backgroundImage),(n=(i=s).openFromBridge)===null||n===void 0||n.call(i)}}}}const scriptBaseUrl=(()=>{if(!scriptEl)return"";try{return new URL(".",scriptEl.src||window.location.href).href}catch{return""}})(),widgetAssetBaseUrl=(()=>{if(!scriptEl)return DEFAULT_WIDGET_ASSET_BASE;const e=scriptEl.dataset.widgetAssets;if(e==="local")return`${scriptBaseUrl}src/`;if(e&&e.length>0)try{return new URL(e,scriptBaseUrl).href}catch{return e.endsWith("/")?e:`${e}/`}return DEFAULT_WIDGET_ASSET_BASE})(),WIDGET_JS_URL=`${widgetAssetBaseUrl}medassist-widget.js`,WIDGET_CSS_URL=`${widgetAssetBaseUrl}medassist-widget.css`;let widgetScriptPromise=null,widgetCssTextPromise=null;function getWidgetCssTextPromise(){return widgetCssTextPromise||(widgetCssTextPromise=fetch(WIDGET_CSS_URL).then(e=>{if(!e.ok)throw new Error(`Unable to fetch widget styles from ${WIDGET_CSS_URL}`);return e.text()})),widgetCssTextPromise}class MedAssistWidgetLoader extends HTMLElement{getAgentConfig(){if(!this.agentConfigPromise){const t=(typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{},o=t.baseUrl||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",i=t.agentId||this.getAttribute("agent-id")||"";if(!i)return Promise.resolve(void 0);this.agentConfigPromise=fetchAgentConfig(o,i).catch(()=>{})}return this.agentConfigPromise}constructor(){super(),this.reactRoot=null,this.agentConfigPromise=null,this.attachShadow({mode:"open"}),this.defaultIconUrl="https://cdn.eka.care/bot-icon.svg",this.widgetLoaded=!1,this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.setupAuthExpirationListener()}static get observedAttributes(){return["icon-url","display-mode","custom-launcher-styles","redirect-url"]}connectedCallback(){this.displayMode==="full"?(this.initializeFullMode(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to load MedAssist widget in full mode",t)})):(this.renderButton(),this.preloadWidgetAssets(),this.initNudge())}attributeChangedCallback(t){if(t==="icon-url"&&this.displayMode==="widget"&&this.renderButton(),t==="display-mode"){const o=this.getAttribute("display-mode")==="full"?"full":"widget";o!==this.displayMode&&(this.displayMode=o,o==="full"?this.initializeFullMode():this.renderButton())}t==="custom-launcher-styles"&&this.applyCustomLauncherStyles()}preloadWidgetAssets(){getWidgetCssTextPromise(),this.loadWidgetScript(),this.preloadBackgroundImage()}preloadBackgroundImage(){var t;const o=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,i=this.getAttribute("theme")?(()=>{try{return JSON.parse(this.getAttribute("theme")||"{}")}catch{return}})():void 0,n=(t=o==null?void 0:o.theme)===null||t===void 0?void 0:t.backgroundImage,s=i==null?void 0:i.backgroundImage;n&&preloadImage(n),s&&s!==n&&preloadImage(s),this.getAgentConfig().then(r=>{var d;const c=(d=r==null?void 0:r.theme)===null||d===void 0?void 0:d.background_img;c&&preloadImage(c)}).catch(()=>{})}setupAuthExpirationListener(){if(typeof window=="undefined")return;const t=o=>{var i;((i=o.data)===null||i===void 0?void 0:i.type)==="AUTH_EXPIRED"&&window.parent!==window&&window.parent.postMessage({type:"AUTH_EXPIRED",message:"Authentication expired"},"*")};window.addEventListener("message",t)}openFromBridge(){this.displayMode=this.getAttribute("display-mode")==="full"?"full":"widget",this.displayMode==="full"?this.initializeFullMode():this.renderButton(),this.loadWidgetCss().then(()=>this.loadWidgetScript()).then(()=>this.loadAndRender()).catch(t=>{console.error("Failed to open MedAssist widget from bridge",t)})}async initNudge(){var t,o,i,n;const s=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,r=(s==null?void 0:s.agentId)||this.getAttribute("agent-id")||"";if(!r)return;const d=await this.getAgentConfig();if(!(d!=null&&d.nudge))return;const c=(o=(t=d==null?void 0:d.theme)===null||t===void 0?void 0:t.nudge_color)!==null&&o!==void 0?o:"#ffffff",l=typeof window!="undefined"?window.location.href:"";let u=findCachedNudge(r,l);if(u){const f=Math.floor(Date.now()/1e3);u.data.expiry<=f&&(clearNudgeForPath(r,u.domain,u.path),u=null)}if(!u){const f=typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0,w=(f==null?void 0:f.baseUrl)||this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",p=await fetchNudgeData(r,w).catch(()=>{});if(!(!((i=p==null?void 0:p.nudges)===null||i===void 0)&&i.length)||(storeNudgeResponse(r,p),u=findCachedNudge(r,l),!u))return}const h=u.data.nudges[Math.floor(Math.random()*u.data.nudges.length)],m=(n=u.data.delay)!==null&&n!==void 0?n:5;setTimeout(()=>{this.showNudgePopup(h,c)},m*1e3)}showNudgePopup(t,o){var i,n;const s=this.shadowRoot;if(!s||!(!((i=s.getElementById("medassist-widget-root"))===null||i===void 0)&&i.classList.contains("hidden")))return;const d=((typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{}).agentId||this.getAttribute("agent-id")||"";if(d&&getNudgeCookie(d)!==null||s.getElementById("medassist-nudge-popup"))return;const l=document.createElement("div");l.id="medassist-nudge-popup",l.innerHTML=`
|
|
2
2
|
<style>
|
|
3
3
|
@keyframes nudge-slide-up {
|
|
4
4
|
from { opacity: 0; transform: translateY(12px) scale(0.97); }
|
|
@@ -138,4 +138,4 @@
|
|
|
138
138
|
}
|
|
139
139
|
</style>
|
|
140
140
|
<div id="medassist-widget-root"></div>
|
|
141
|
-
`)}async loadAndRender(){var t,o,i,n;const s=this.shadowRoot;if(!s){console.error("Shadow root is not available");return}this.style.display="";const r=s.getElementById("medassist-open-btn"),d=s.getElementById("medassist-widget-root"),c=this.getAttribute("agent-id");let l=this.getAttribute("icon-url")||this.defaultIconUrl;const u=(t=getEnvironment(this.getAttribute("environment")))!==null&&t!==void 0?t:"production",h=this.getAttribute("connectivity"),m=h==="sse"||h==="socket"?h:void 0;let f=this.getAttribute("title")||"Medi Clinic";const w=this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",
|
|
141
|
+
`)}async loadAndRender(){var t,o,i,n;const s=this.shadowRoot;if(!s){console.error("Shadow root is not available");return}this.style.display="";const r=s.getElementById("medassist-open-btn"),d=s.getElementById("medassist-widget-root"),c=this.getAttribute("agent-id");let l=this.getAttribute("icon-url")||this.defaultIconUrl;const u=(t=getEnvironment(this.getAttribute("environment")))!==null&&t!==void 0?t:"production",h=this.getAttribute("connectivity"),m=h==="sse"||h==="socket"?h:void 0;let f=this.getAttribute("title")||"Medi Clinic";const w=this.getAttribute("base-url")||"https://matrix.eka.care/reloaded",A=this.getAttribute("display-mode")==="full"?"full":"widget",U=this.getAttribute("context")?JSON.parse(this.getAttribute("context")||"{}"):void 0,L=this.getAttribute("theme")?(()=>{try{return JSON.parse(this.getAttribute("theme")||"{}")}catch{return}})():void 0,g=(typeof window!="undefined"?window.__ekaMedAssistConfig__:void 0)||{},R={...U||{},...g.context||{}},N=g.auth||void 0,y=g.agentId||c,B=g.baseUrl||w,b=typeof window!="undefined"?(o=window.EkaMedAssist)===null||o===void 0?void 0:o.onClose:void 0,P=this.getAttribute("redirect-url")||void 0,v=g.redirectUrl||P,W=!!v||g.showCloseButton===!0;if(!y){console.error("Agent ID is required");return}if(!d){console.error("Widget root element is missing");return}r&&r.classList.add("hidden"),d.classList.remove("hidden");let _,C,k,E=!1,S,M=!1;try{const a=await this.getAgentConfig();f=(a==null?void 0:a.name)||f,l=((i=a==null?void 0:a.theme)===null||i===void 0?void 0:i.icon_img)||l,_=mapAgentConfigThemeToWidgetTheme((a==null?void 0:a.theme)||void 0),C=a==null?void 0:a.allowed,k=a==null?void 0:a.connectivity,E=(a==null?void 0:a.resize)||(g==null?void 0:g.resize)||!1,S=a==null?void 0:a.initial_message,M=(n=a==null?void 0:a.hide_watermark)!==null&&n!==void 0?n:!1}catch{}const $=k||m||"sse",I={title:g.title||f,iconUrl:l||g.iconUrl,showCloseButton:W,allowed:C,environment:u,connectionType:$,onClose:()=>{var a;if((a=this.reactRoot)===null||a===void 0||a.unmount(),this.reactRoot=null,b&&typeof b=="function"&&b(),v){window.location.href=v;return}A==="full"?this.style.display="none":(r&&r.classList.remove("hidden"),d.classList.add("hidden"))},baseUrl:B,context:R,displayMode:A,resize:E||this.getAttribute("resize")==="true",auth:N,initialMessage:S,hideWatermark:M,theme:{..._||{},...L||{},...g.theme||{}}},x=window;if(this.widgetLoaded){this.reactRoot=x.renderMedAssist(d,y,I);return}try{if(await Promise.all([this.loadWidgetCss(),this.loadWidgetScript()]),typeof x.renderMedAssist!="function")throw new Error("renderMedAssist is not available on window");this.reactRoot=x.renderMedAssist(d,y,I),this.widgetLoaded=!0}catch(a){console.error("Failed to load MedAssist widget",a),r&&r.classList.remove("hidden"),d.classList.add("hidden")}}async loadWidgetCss(){const t=this.shadowRoot;if(!t)throw new Error("Shadow root is not available");if(t.querySelector("[data-medassist-style='true']"))return;const o=await getWidgetCssTextPromise(),i=document.createElement("style");i.setAttribute("data-medassist-style","true"),i.textContent=o,t.appendChild(i)}loadWidgetScript(){return widgetScriptPromise||(widgetScriptPromise=new Promise((t,o)=>{const i=document.querySelector(`script[src="${WIDGET_JS_URL}"]`);if(i){if(i.dataset.loaded==="true"){t();return}i.addEventListener("load",()=>t()),i.addEventListener("error",s=>o(s.error||new Error("Unknown script loading error")));return}const n=document.createElement("script");n.src=WIDGET_JS_URL,n.async=!0,n.dataset.widget="medassist",n.onload=()=>{n.dataset.loaded="true",t()},n.onerror=s=>{if(s instanceof ErrorEvent&&s.error){o(s.error);return}o(new Error(`Failed to load ${WIDGET_JS_URL}`))},document.head.appendChild(n)})),widgetScriptPromise}}customElements.define("eka-medassist-widget",MedAssistWidgetLoader);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eka-care/medassist-widget-embed",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.42",
|
|
4
4
|
"description": "Embeddable MedAssist widget loader built with Web Components.",
|
|
5
5
|
"author": "Geethanjali S",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"dist/iframe.html",
|
|
40
40
|
"dist/iframe-helper.js",
|
|
41
41
|
"dist/iframe-helper.d.ts",
|
|
42
|
-
"dist/
|
|
42
|
+
"dist/eka-agents.html",
|
|
43
43
|
"README.md"
|
|
44
44
|
],
|
|
45
45
|
"engines": {
|