@chatsystem/client 1.1.56 → 1.1.57

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/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("react/jsx-runtime"),I=require("react"),En=require("react-dom"),Nl=require("lottie-react"),vl=require("@lottiefiles/dotlottie-react");function wl(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const ke=wl(I),Ol=e=>ke.createElement("svg",{fill:"#000000",height:"800px",width:"800px",id:"Layer_1",xmlns:"http://www.w3.org/2000/svg",xmlnsXlink:"http://www.w3.org/1999/xlink",viewBox:"0 0 330 330",xmlSpace:"preserve",...e},ke.createElement("path",{id:"XMLID_102_",d:"M325.607,79.393c-5.857-5.857-15.355-5.858-21.213,0.001l-139.39,139.393L25.607,79.393 c-5.857-5.857-15.355-5.858-21.213,0.001c-5.858,5.858-5.858,15.355,0,21.213l150.004,150c2.813,2.813,6.628,4.393,10.606,4.393 s7.794-1.581,10.606-4.394l149.996-150C331.465,94.749,331.465,85.251,325.607,79.393z"}));function Ba(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var Rt={},fi;function Ll(){if(fi)return Rt;fi=1,Object.defineProperty(Rt,"__esModule",{value:!0}),Rt.AgentDisplayFacetRoleType=Rt.AgentDisplayFacetModeType=void 0;var e;(function(n){n.SPRITESHEET="spritesheet",n.LOTTIE="lottie",n.IMAGE="image",n.SVG="svg",n.VIDEO="video"})(e||(Rt.AgentDisplayFacetModeType=e={}));var t;return(function(n){n.LAUNCHER="launcher",n.ASSISTANT="assistant",n.SEARCH="search"})(t||(Rt.AgentDisplayFacetRoleType=t={})),Rt}var Ve=Ll();const wr=e=>({mode:Ve.AgentDisplayFacetModeType.SVG,role:e}),Rl=()=>({agent:{display:{name:"",CSSRules:"",[Ve.AgentDisplayFacetRoleType.LAUNCHER]:wr(Ve.AgentDisplayFacetRoleType.LAUNCHER),[Ve.AgentDisplayFacetRoleType.ASSISTANT]:wr(Ve.AgentDisplayFacetRoleType.ASSISTANT),[Ve.AgentDisplayFacetRoleType.SEARCH]:wr(Ve.AgentDisplayFacetRoleType.SEARCH)},text:{initialText:""}},api:{settings:{maxUserMessagesPerDiscussion:20}}}),Rn=I.createContext({companySettings:Rl(),setCompanySettings:()=>{}}),en=globalThis;en.__WIDGET_ASSET_CACHE__||(en.__WIDGET_ASSET_CACHE__={svg:new Map,img:new Map,lot:new Map,spr:new Map});en.__WIDGET_PRELOAD_KEYS__||(en.__WIDGET_PRELOAD_KEYS__=new Set);const Or=en.__WIDGET_PRELOAD_KEYS__,Nt=en.__WIDGET_ASSET_CACHE__,kl="widget-assets-v1";async function pr(e,t){try{const n=await caches.open(kl),r=await n.match(e,{ignoreSearch:!1});if(r)return r.clone();const a=await fetch(e,{cache:"force-cache",mode:"cors",keepalive:!0,...t});if(!a.ok)throw new Error(`HTTP ${a.status} for ${e}`);return n.put(e,a.clone()),a}catch(n){return console.log("error while fetching",n),new Response(null,{status:520,statusText:"Error in client - Fetch failed"})}}async function nu(e){const t=Nt.svg.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).text();if(typeof globalThis.DOMPurify<"u"&&typeof globalThis.DOMPurify.sanitize=="function")try{const s=new DOMParser().parseFromString(a,"image/svg+xml"),o=new XMLSerializer().serializeToString(s.documentElement);return{sanitized:globalThis.DOMPurify.sanitize(o,{USE_PROFILES:{svg:!0,svgFilters:!0}})}}catch{return{sanitized:globalThis.DOMPurify.sanitize(a,{USE_PROFILES:{svg:!0,svgFilters:!0}})}}else return{sanitized:a}})();return Nt.svg.set(e,{promise:n,ts:Date.now()}),n}function ru(e){const t=Nt.img.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).blob();return{blobUrl:URL.createObjectURL(a)}})();return Nt.img.set(e,{promise:n,ts:Date.now()}),n}function au(e){const t=Nt.lot.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).arrayBuffer(),i=new Uint8Array(a);if(i.length>=4&&i[0]===80&&i[1]===75&&i[2]===3&&i[3]===4||e.endsWith(".lottie"))return{kind:"zip",blobUrl:URL.createObjectURL(new Blob([a],{type:"application/zip"}))};const o=new TextDecoder().decode(i);return{kind:"json",data:JSON.parse(o)}})();return Nt.lot.set(e,{promise:n,ts:Date.now()}),n}function iu(e){const t=Nt.spr.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).blob();let i=null;try{i=await createImageBitmap(a)}catch{i=null}const{imgEl:s,width:o,height:l}=await new Promise((c,d)=>{const f=URL.createObjectURL(a),h=new Image;h.crossOrigin="anonymous",h.onload=()=>{const m=h.naturalWidth,y=h.naturalHeight;c({imgEl:h,width:m,height:y})},h.onerror=()=>{URL.revokeObjectURL(f),d(new Error("Spritesheet image decode failed"))},h.src=f,h.decode?.().catch(()=>{})});return{imgEl:s,bitmap:i,width:o,height:l}})();return Nt.spr.set(e,{promise:n,ts:Date.now()}),n}function Dl(e){e.images?.forEach(t=>void ru(t)),e.svgs?.forEach(t=>void nu(t)),e.lotties?.forEach(t=>void au(t)),e.sprites?.forEach(t=>void iu(t))}function Pl(e){try{const t=document.createElement("link");t.rel="preconnect",t.href=e,t.crossOrigin="anonymous",document.head.appendChild(t);const n=document.createElement("link");n.rel="dns-prefetch",n.href=e,document.head.appendChild(n)}catch(t){console.log("error in injectPreconnect",t)}}function su(e){return new URL(e,document.baseURI).toString()}function Ml(e,t){const n=document.head.querySelectorAll('link[rel="preload"]');for(const r of n)if((e?r.as===e||r.getAttribute("as")===e:!0)&&r.href===t)return!0;return!1}function $n(e,t,n,r){const a=su(n),i=`${e}|${t??""}|${a}`;if(Or.has(i))return;if(Ml(t,a)){Or.add(i);return}const s=document.createElement("link");s.rel=e,t&&(s.as=t),s.href=a,s.crossOrigin="anonymous",r&&(s.type=r),s.setAttribute("data-chatsystem-preload","1"),document.head.appendChild(s),Or.add(i)}function Bl(e){const t=s=>Array.from(new Set((s??[]).map(su))),n=t(e.images),r=t(e.svgs),a=t(e.lotties),i=t(e.sprites);n.forEach(s=>$n("preload","image",s)),r.forEach(s=>$n("preload","fetch",s,"image/svg+xml")),a.forEach(s=>$n("preload","fetch",s,"application/zip")),i.forEach(s=>$n("preload","fetch",s))}const Fl=({src:e})=>{const[t,n]=I.useState(null);return I.useEffect(()=>{let r=!1;return ru(e).then(({blobUrl:a})=>{n(a)}).catch(()=>n(null)),()=>{t&&!r&&(URL.revokeObjectURL(t),r=!0)}},[e]),C.jsx("img",{src:t??e,decoding:"async",loading:"eager"})};function Hl(e){return!!e&&typeof e=="object"&&"blob"in e&&e.blob instanceof Blob}function Ul({src:e,autoplay:t=!0,loop:n=!0,className:r,style:a,placeholder:i,fallback:s,onLoaded:o,onError:l}){const[c,d]=I.useState({kind:"loading"});return I.useEffect(()=>{let f=null,h=!1;return d({kind:"loading"}),au(e).then(async m=>{if(!h)if(m.kind==="zip"){const y=Hl(m)?m.blob:await(await fetch(m.blobUrl)).blob();f=URL.createObjectURL(y),d({kind:"lottieZip",blobUrl:f}),o?.(f)}else d({kind:"json",data:m.data}),o?.(m.data)}).catch(m=>!h&&(d({kind:"error",err:m}),l?.(m))),()=>{h=!0,f&&URL.revokeObjectURL(f)}},[e]),c.kind==="loading"?C.jsx("div",{className:"flex items-center justify-center rounded-xl border border-dashed p-6 text-sm opacity-70 "+(r??""),style:{minHeight:96,...a},children:i??"Loading animation…"}):c.kind==="error"?C.jsx("div",{className:"flex items-center justify-center rounded-xl border p-6 text-sm text-red-600 "+(r??""),style:{minHeight:96,...a},children:s??"Failed to load animation"}):c.kind==="json"?C.jsx(Nl,{animationData:c.data,loop:n,autoplay:t,className:r,style:a,rendererSettings:{preserveAspectRatio:"xMidYMid meet"}}):C.jsx(vl.DotLottieReact,{src:c.blobUrl,loop:n,autoplay:t,className:r,style:a})}async function jl(e,t={}){const{minPeriod:n=8,maxPeriod:r=Math.floor(Math.min(e.width,e.height)/2),gutterSearchRadius:a=3,debug:i=!1}=t;if(e.width<n*2||e.height<n*2)return i&&console.warn("detectGrid: image too small"),null;const s=document.createElement("canvas");s.width=e.naturalWidth,s.height=e.naturalHeight;const o=s.getContext("2d");if(!o)return null;o.drawImage(e,0,0);const{data:l}=o.getImageData(0,0,s.width,s.height),c=s.width,d=s.height,f=new Uint8ClampedArray(c*d);for(let Y=0;Y<d;Y++){let j=Y*c;for(let F=0;F<c;F++){const z=(j+F)*4;f[j+F]=.299*l[z]+.587*l[z+1]+.114*l[z+2]|0}}const h=new Float32Array(c),m=new Float32Array(d);for(let Y=1;Y<c-1;Y++){let j=0;for(let F=0;F<d;F++){const z=F*c+Y,$=f[z+1]-f[z-1];j+=Math.abs($)}h[Y]=j}for(let Y=1;Y<d-1;Y++){let j=0;for(let F=0;F<c;F++){const z=Y*c+F,$=f[z+c]-f[z-c];j+=Math.abs($)}m[Y]=j}const y=hi(h,n,Math.min(r,c/2)),x=hi(m,n,Math.min(r,d/2));if(!y||!x)return i&&console.warn("detectGrid: could not find periods"),null;const N=Math.max(1,Math.round(c/y)),A=Math.max(1,Math.round(d/x)),R=mi(h,y,N,a),v=mi(m,x,A,a),B=Math.max(1,y-R),S=Math.max(1,x-v);if(B*N>c+2||S*A>d+2)return i&&console.warn("detectGrid: sanity check failed"),null;const L=ua(h,60),K=ua(m,60),le=Math.max(0,pi(h,L)),ie=Math.max(0,pi(m,K));return i&&console.log({periodX:y,periodY:x,columns:N,rows:A,gutterX:R,gutterY:v,frameWidth:B,frameHeight:S,originX:le,originY:ie}),{columns:N,rows:A,frameWidth:B,frameHeight:S,gutterX:R,gutterY:v,originX:le,originY:ie}}function hi(e,t,n){const r=uu(e);let a=0;for(let m=0;m<e.length;m++){const y=e[m]-r;a+=y*y}if(a===0)return null;const i=new Float32Array(n+1);let s=0,o=-1/0;for(let m=t;m<=n;m++){let y=0;for(let N=0;N+m<e.length;N++)y+=(e[N]-r)*(e[N+m]-r);const x=y/a;i[m]=x,x>o&&(o=x,s=m)}if(s<=t||s>=n)return s||null;const l=i[s-1],c=i[s],d=i[s+1],f=l-2*c+d,h=f===0?0:.5*(l-d)/f;return s+h}function mi(e,t,n,r){const a=[];for(let o=1;o<n;o++){const l=o*t;let c=1/0,d=l;for(let f=-r;f<=r;f++){const h=l+f;if(h<=0||h>=e.length)continue;const m=e[h];m<c&&(c=m,d=h)}a.push(d)}if(a.length<1)return 0;let i=[];for(const o of a){const l=e[o]*1.5+ua(e,10)*.5;let c=o,d=o;for(;c>1&&e[c-1]<=l;)c--;for(;d<e.length-2&&e[d+1]<=l;)d++;i.push(Math.max(0,d-c+1))}const s=Math.round(uu(i));return Math.max(0,Math.min(s,Math.floor(t/4)))}function uu(e){let t=0;for(let n=0;n<e.length;n++)t+=e[n];return t/e.length}function ua(e,t){const n=Array.from(e);n.sort((a,i)=>a-i);const r=Math.max(0,Math.min(n.length-1,Math.floor(t/100*n.length)));return n[r]}function pi(e,t){for(let n=0;n<e.length;n++)if(e[n]>t)return n;return 0}function $l(e,t,n,r,a){const{data:i}=e.getImageData(t,n,r,a);let s=0;for(let o=3;o<i.length;o+=4)s+=i[o];return s/(255*(i.length/4))<.03}const zl=I.forwardRef(function({src:t,frameWidth:n,frameHeight:r,columns:a,rows:i,gutterX:s=0,gutterY:o=0,frameCount:l,atlasFrames:c,fps:d=12,autoplay:f=!0,loop:h=!0,startFrame:m=0,direction:y="forward",scale:x=1,backgroundColor:N,className:A,style:R,pauseWhenHidden:v=!1,originX:B,originY:S,onLoad:L,onEnd:K,onFrame:le},ie){const Y=I.useRef(null),j=I.useRef(null),[F,z]=I.useState(null),[$,Z]=I.useState(null),[ce,pe]=I.useState(null),[Ne,Pe]=I.useState(!1),[p,H]=I.useState(!!f),[W,b]=I.useState(m),X=I.useRef(1),[ee,G]=I.useState(null),te=Math.max(1,Math.floor(typeof window<"u"&&window.devicePixelRatio||1));I.useEffect(()=>{let de=!1;return Pe(!1),iu(t).then(({imgEl:xe,bitmap:ye})=>{de||(z(xe),Z(ye),pe({w:xe.naturalWidth,h:xe.naturalHeight}),Pe(!0),L?.())}).catch(()=>{}),()=>{de=!0}},[t,L]),I.useEffect(()=>{let de=!1;return(async()=>{if(!F)return;if(c&&c.length){G(null);return}if(!(!n||!r||!a||!i)){G(null);return}const ye=await jl(F,{debug:!0});de||(ye?G({frameWidth:ye.frameWidth,frameHeight:ye.frameHeight,columns:ye.columns,rows:ye.rows,gutterX:ye.gutterX,gutterY:ye.gutterY}):(G(null),console.warn("RemoteSpritesheet: auto-detect failed; please provide grid props.")))})(),()=>{de=!0}},[F,c,n,r,a,i]);const ne=ee?.frameWidth??n,Q=ee?.frameHeight??r,J=ee?.columns??a,Se=ee?.rows??i,U=ee?.gutterX??s,Le=ee?.gutterY??o,De=ee?.originX??B??0,st=ee?.originY??S??0,Lt=I.useMemo(()=>{if(c&&c.length>0)return{cols:0,rows:0};if(!ce||!ne||!Q)return{cols:0,rows:0};const de=J??Math.floor((ce.w+U)/(ne+U)),xe=Se??Math.floor((ce.h+Le)/(Q+Le));return{cols:Math.max(0,de),rows:Math.max(0,xe)}},[c,ce,ne,Q,J,Se,U,Le]),Me=I.useMemo(()=>{if(c?.length)return c;if(!ne||!Q)return[];const{cols:de,rows:xe}=Lt;if(!de||!xe)return[];const ye=Math.min(l??de*xe,de*xe),Te=[];for(let ve=0;ve<ye;ve++){const w=ve%de,P=Math.floor(ve/de),V=De+w*(ne+U),ue=st+P*(Q+Le);Te.push({x:V,y:ue,w:ne,h:Q})}if(F&&Te.length){const ve=document.createElement("canvas");ve.width=F.naturalWidth,ve.height=F.naturalHeight;const w=ve.getContext("2d");if(w){w.drawImage(F,0,0);let P=Te.length;for(;P>0&&$l(w,Te[P-1].x,Te[P-1].y,Te[P-1].w,Te[P-1].h);)P--;return Te.slice(0,P)}}return Te},[c,Lt,l,ne,Q,U,Le,F,De,st]),yt=()=>{const de=Y.current;if(!de||Me.length===0)return;const xe=Me[Math.max(0,Math.min(W,Me.length-1))],ye=Math.max(1,Math.round(xe.w*x)),Te=Math.max(1,Math.round(xe.h*x));de.style.width=ye+"px",de.style.height=Te+"px",de.width=ye*te,de.height=Te*te};I.useEffect(()=>{yt()},[Ne,x,Me.length,te,W]);const ut=de=>{const xe=$??F??null,ye=Y.current;if(!ye||!xe||Me.length===0)return;const Te=ye.getContext("2d");if(!Te)return;Te.save(),Te.setTransform(1,0,0,1,0,0),Te.clearRect(0,0,ye.width,ye.height),N&&(Te.fillStyle=N,Te.fillRect(0,0,ye.width,ye.height));const ve=Me[de];Te.scale(te,te),Te.drawImage(xe,ve.x,ve.y,ve.w,ve.h,0,0,Math.round(ve.w*x),Math.round(ve.h*x)),Te.restore()};return I.useEffect(()=>{if(!Ne||Me.length===0)return;let de=performance.now();const xe=1e3/Math.max(1,Math.min(120,d)),ye=Te=>{if(j.current=requestAnimationFrame(ye),!p){ut(W);return}const ve=Te-de;if(ve<xe){ut(W);return}de=Te-ve%xe,b(w=>{const P=Me.length-1;let V=w;return y==="forward"?V=w+1:y==="backward"?V=w-1:((w>=P&&X.current===1||w<=0&&X.current===-1)&&(X.current*=-1),V=w+X.current),(V>P||V<0)&&(h?V=y==="backward"?P:0:(V=Math.max(0,Math.min(V,P)),H(!1),K?.())),V})};return j.current=requestAnimationFrame(ye),()=>{j.current&&cancelAnimationFrame(j.current)}},[Ne,d,p,h,y,Me.length,W]),I.useEffect(()=>{ut(W),le?.(W)},[W,Ne,x,te]),I.useEffect(()=>{if(!v)return;const de=()=>{document.hidden&&H(!1)};return document.addEventListener("visibilitychange",de),()=>document.removeEventListener("visibilitychange",de)},[v]),I.useImperativeHandle(ie,()=>({play:()=>H(!0),pause:()=>H(!1),toggle:()=>H(de=>!de),gotoFrame:de=>b(()=>Math.max(0,Math.min(de,Math.max(0,Me.length-1)))),isPlaying:()=>p,currentFrame:()=>W}),[p,W,Me.length]),C.jsx("div",{className:A,style:R,children:C.jsx("canvas",{ref:Y,"aria-label":"spritesheet animation"})})});function Yl({url:e,className:t}){const[n,r]=I.useState(""),[a,i]=I.useState(null);return I.useEffect(()=>{let s=!1;return nu(e).then(({sanitized:o})=>{s||(r(o),i(null))}).catch(o=>!s&&i(o.message??"Failed to load")),()=>{s=!0}},[e]),a?C.jsxs("div",{className:t,role:"alert",children:["Couldn’t load SVG: ",a]}):n?C.jsx("div",{className:t,dangerouslySetInnerHTML:{__html:n}}):C.jsx("div",{className:t})}const Fa=({facet:e})=>{const{mode:t,assetUrl:n,spr_columns:r,spr_fps:a,spr_frameCount:i,spr_frameHeight:s,spr_frameWidth:o,spr_gutterX:l,spr_gutterY:c,spr_rows:d,spr_originX:f,spr_originY:h}=e;return C.jsxs("div",{className:"assetContainer",children:[t===Ve.AgentDisplayFacetModeType.LOTTIE&&C.jsx(Ul,{src:n,autoplay:!0,onError:m=>console.error(m)}),t===Ve.AgentDisplayFacetModeType.SVG&&C.jsx("div",{children:C.jsx(Yl,{url:n})}),t===Ve.AgentDisplayFacetModeType.IMAGE&&C.jsx(Fl,{src:n}),t===Ve.AgentDisplayFacetModeType.SPRITESHEET&&C.jsx(zl,{src:n,frameCount:i,columns:r,rows:d,fps:a,frameHeight:s,frameWidth:o,gutterX:l,gutterY:c,originX:f,originY:h,autoplay:!0})]})},Gl=({isWidgetOpen:e,setIsWidgetOpen:t})=>{const{companySettings:n}=I.useContext(Rn);return C.jsxs("div",{className:`launcher
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("react/jsx-runtime"),I=require("react"),En=require("react-dom"),Nl=require("lottie-react"),vl=require("@lottiefiles/dotlottie-react");function wl(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const ke=wl(I),Ol=e=>ke.createElement("svg",{fill:"#000000",height:"800px",width:"800px",id:"Layer_1",xmlns:"http://www.w3.org/2000/svg",xmlnsXlink:"http://www.w3.org/1999/xlink",viewBox:"0 0 330 330",xmlSpace:"preserve",...e},ke.createElement("path",{id:"XMLID_102_",d:"M325.607,79.393c-5.857-5.857-15.355-5.858-21.213,0.001l-139.39,139.393L25.607,79.393 c-5.857-5.857-15.355-5.858-21.213,0.001c-5.858,5.858-5.858,15.355,0,21.213l150.004,150c2.813,2.813,6.628,4.393,10.606,4.393 s7.794-1.581,10.606-4.394l149.996-150C331.465,94.749,331.465,85.251,325.607,79.393z"}));function Ba(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var Rt={},fi;function Ll(){if(fi)return Rt;fi=1,Object.defineProperty(Rt,"__esModule",{value:!0}),Rt.AgentDisplayFacetRoleType=Rt.AgentDisplayFacetModeType=void 0;var e;(function(n){n.SPRITESHEET="spritesheet",n.LOTTIE="lottie",n.IMAGE="image",n.SVG="svg",n.VIDEO="video"})(e||(Rt.AgentDisplayFacetModeType=e={}));var t;return(function(n){n.LAUNCHER="launcher",n.ASSISTANT="assistant",n.SEARCH="search"})(t||(Rt.AgentDisplayFacetRoleType=t={})),Rt}var Ve=Ll();const wr=e=>({mode:Ve.AgentDisplayFacetModeType.SVG,role:e}),Rl=()=>({agent:{display:{name:"",CSSRules:"",[Ve.AgentDisplayFacetRoleType.LAUNCHER]:wr(Ve.AgentDisplayFacetRoleType.LAUNCHER),[Ve.AgentDisplayFacetRoleType.ASSISTANT]:wr(Ve.AgentDisplayFacetRoleType.ASSISTANT),[Ve.AgentDisplayFacetRoleType.SEARCH]:wr(Ve.AgentDisplayFacetRoleType.SEARCH)},text:{initialText:""}},api:{settings:{maxUserMessagesPerDiscussion:20}}}),Rn=I.createContext({companySettings:Rl(),setCompanySettings:()=>{}}),en=globalThis;en.__WIDGET_ASSET_CACHE__||(en.__WIDGET_ASSET_CACHE__={svg:new Map,img:new Map,lot:new Map,spr:new Map});en.__WIDGET_PRELOAD_KEYS__||(en.__WIDGET_PRELOAD_KEYS__=new Set);const Or=en.__WIDGET_PRELOAD_KEYS__,Nt=en.__WIDGET_ASSET_CACHE__,kl="widget-assets-v1";async function pr(e,t){try{const n=await caches.open(kl),r=await n.match(e,{ignoreSearch:!1});if(r)return r.clone();const a=await fetch(e,{cache:"force-cache",mode:"cors",keepalive:!0,...t});if(!a.ok)throw new Error(`HTTP ${a.status} for ${e}`);return n.put(e,a.clone()),a}catch(n){return console.log("error while fetching",n),new Response(null,{status:520,statusText:"Error in client - Fetch failed"})}}async function nu(e){const t=Nt.svg.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).text();if(typeof globalThis.DOMPurify<"u"&&typeof globalThis.DOMPurify.sanitize=="function")try{const s=new DOMParser().parseFromString(a,"image/svg+xml"),o=new XMLSerializer().serializeToString(s.documentElement);return{sanitized:globalThis.DOMPurify.sanitize(o,{USE_PROFILES:{svg:!0,svgFilters:!0}})}}catch{return{sanitized:globalThis.DOMPurify.sanitize(a,{USE_PROFILES:{svg:!0,svgFilters:!0}})}}else return{sanitized:a}})();return Nt.svg.set(e,{promise:n,ts:Date.now()}),n}function ru(e){const t=Nt.img.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).blob();return{blobUrl:URL.createObjectURL(a)}})();return Nt.img.set(e,{promise:n,ts:Date.now()}),n}function au(e){const t=Nt.lot.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).arrayBuffer(),i=new Uint8Array(a);if(i.length>=4&&i[0]===80&&i[1]===75&&i[2]===3&&i[3]===4||e.endsWith(".lottie"))return{kind:"zip",blobUrl:URL.createObjectURL(new Blob([a],{type:"application/zip"}))};const o=new TextDecoder().decode(i);return{kind:"json",data:JSON.parse(o)}})();return Nt.lot.set(e,{promise:n,ts:Date.now()}),n}function iu(e){const t=Nt.spr.get(e);if(t)return t.promise;const n=(async()=>{const a=await(await pr(e)).blob();let i=null;try{i=await createImageBitmap(a)}catch{i=null}const{imgEl:s,width:o,height:l}=await new Promise((c,d)=>{const f=URL.createObjectURL(a),h=new Image;h.crossOrigin="anonymous",h.onload=()=>{const m=h.naturalWidth,y=h.naturalHeight;c({imgEl:h,width:m,height:y})},h.onerror=()=>{URL.revokeObjectURL(f),d(new Error("Spritesheet image decode failed"))},h.src=f,h.decode?.().catch(()=>{})});return{imgEl:s,bitmap:i,width:o,height:l}})();return Nt.spr.set(e,{promise:n,ts:Date.now()}),n}function Dl(e){e.images?.forEach(t=>void ru(t)),e.svgs?.forEach(t=>void nu(t)),e.lotties?.forEach(t=>void au(t)),e.sprites?.forEach(t=>void iu(t))}function Pl(e){try{const t=document.createElement("link");t.rel="preconnect",t.href=e,t.crossOrigin="anonymous",document.head.appendChild(t);const n=document.createElement("link");n.rel="dns-prefetch",n.href=e,document.head.appendChild(n)}catch(t){console.log("error in injectPreconnect",t)}}function su(e){return new URL(e,document.baseURI).toString()}function Ml(e,t){const n=document.head.querySelectorAll('link[rel="preload"]');for(const r of n)if((e?r.as===e||r.getAttribute("as")===e:!0)&&r.href===t)return!0;return!1}function $n(e,t,n,r){const a=su(n),i=`${e}|${t??""}|${a}`;if(Or.has(i))return;if(Ml(t,a)){Or.add(i);return}const s=document.createElement("link");s.rel=e,t&&(s.as=t),s.href=a,s.crossOrigin="anonymous",r&&(s.type=r),s.setAttribute("data-chatsystem-preload","1"),document.head.appendChild(s),Or.add(i)}function Bl(e){const t=s=>Array.from(new Set((s??[]).map(su))),n=t(e.images),r=t(e.svgs),a=t(e.lotties),i=t(e.sprites);n.forEach(s=>$n("preload","image",s)),r.forEach(s=>$n("preload","fetch",s,"image/svg+xml")),a.forEach(s=>$n("preload","fetch",s,"application/zip")),i.forEach(s=>$n("preload","fetch",s))}const Fl=({src:e})=>{const[t,n]=I.useState(null);return I.useEffect(()=>{let r=!1;return ru(e).then(({blobUrl:a})=>{n(a)}).catch(()=>n(null)),()=>{t&&!r&&(URL.revokeObjectURL(t),r=!0)}},[e]),C.jsx("img",{src:t??e,decoding:"async",loading:"eager"})};function Hl(e){return!!e&&typeof e=="object"&&"blob"in e&&e.blob instanceof Blob}function Ul({src:e,autoplay:t=!0,loop:n=!0,className:r,style:a,placeholder:i,fallback:s,onLoaded:o,onError:l}){const[c,d]=I.useState({kind:"loading"});return I.useEffect(()=>{let f=null,h=!1;return d({kind:"loading"}),au(e).then(async m=>{if(!h)if(m.kind==="zip"){const y=Hl(m)?m.blob:await(await fetch(m.blobUrl)).blob();f=URL.createObjectURL(y),d({kind:"lottieZip",blobUrl:f}),o?.(f)}else d({kind:"json",data:m.data}),o?.(m.data)}).catch(m=>!h&&(d({kind:"error",err:m}),l?.(m))),()=>{h=!0,f&&URL.revokeObjectURL(f)}},[e]),c.kind==="loading"?C.jsx("div",{className:"flex items-center justify-center rounded-xl border border-dashed p-6 text-sm opacity-70 "+(r??""),style:{minHeight:96,...a},children:i??"Loading animation…"}):c.kind==="error"?C.jsx("div",{className:"flex items-center justify-center rounded-xl border p-6 text-sm text-red-600 "+(r??""),style:{minHeight:96,...a},children:s??"Failed to load animation"}):c.kind==="json"?C.jsx(Nl,{animationData:c.data,loop:n,autoplay:t,className:r,style:a,rendererSettings:{preserveAspectRatio:"xMidYMid meet"}}):C.jsx(vl.DotLottieReact,{src:c.blobUrl,loop:n,autoplay:t,className:r,style:a})}async function jl(e,t={}){const{minPeriod:n=8,maxPeriod:r=Math.floor(Math.min(e.width,e.height)/2),gutterSearchRadius:a=3,debug:i=!1}=t;if(e.width<n*2||e.height<n*2)return i&&console.warn("detectGrid: image too small"),null;const s=document.createElement("canvas");s.width=e.naturalWidth,s.height=e.naturalHeight;const o=s.getContext("2d");if(!o)return null;o.drawImage(e,0,0);const{data:l}=o.getImageData(0,0,s.width,s.height),c=s.width,d=s.height,f=new Uint8ClampedArray(c*d);for(let Y=0;Y<d;Y++){let j=Y*c;for(let F=0;F<c;F++){const z=(j+F)*4;f[j+F]=.299*l[z]+.587*l[z+1]+.114*l[z+2]|0}}const h=new Float32Array(c),m=new Float32Array(d);for(let Y=1;Y<c-1;Y++){let j=0;for(let F=0;F<d;F++){const z=F*c+Y,$=f[z+1]-f[z-1];j+=Math.abs($)}h[Y]=j}for(let Y=1;Y<d-1;Y++){let j=0;for(let F=0;F<c;F++){const z=Y*c+F,$=f[z+c]-f[z-c];j+=Math.abs($)}m[Y]=j}const y=hi(h,n,Math.min(r,c/2)),x=hi(m,n,Math.min(r,d/2));if(!y||!x)return i&&console.warn("detectGrid: could not find periods"),null;const N=Math.max(1,Math.round(c/y)),A=Math.max(1,Math.round(d/x)),R=mi(h,y,N,a),v=mi(m,x,A,a),B=Math.max(1,y-R),S=Math.max(1,x-v);if(B*N>c+2||S*A>d+2)return i&&console.warn("detectGrid: sanity check failed"),null;const L=ua(h,60),K=ua(m,60),le=Math.max(0,pi(h,L)),ie=Math.max(0,pi(m,K));return i&&console.log({periodX:y,periodY:x,columns:N,rows:A,gutterX:R,gutterY:v,frameWidth:B,frameHeight:S,originX:le,originY:ie}),{columns:N,rows:A,frameWidth:B,frameHeight:S,gutterX:R,gutterY:v,originX:le,originY:ie}}function hi(e,t,n){const r=uu(e);let a=0;for(let m=0;m<e.length;m++){const y=e[m]-r;a+=y*y}if(a===0)return null;const i=new Float32Array(n+1);let s=0,o=-1/0;for(let m=t;m<=n;m++){let y=0;for(let N=0;N+m<e.length;N++)y+=(e[N]-r)*(e[N+m]-r);const x=y/a;i[m]=x,x>o&&(o=x,s=m)}if(s<=t||s>=n)return s||null;const l=i[s-1],c=i[s],d=i[s+1],f=l-2*c+d,h=f===0?0:.5*(l-d)/f;return s+h}function mi(e,t,n,r){const a=[];for(let o=1;o<n;o++){const l=o*t;let c=1/0,d=l;for(let f=-r;f<=r;f++){const h=l+f;if(h<=0||h>=e.length)continue;const m=e[h];m<c&&(c=m,d=h)}a.push(d)}if(a.length<1)return 0;let i=[];for(const o of a){const l=e[o]*1.5+ua(e,10)*.5;let c=o,d=o;for(;c>1&&e[c-1]<=l;)c--;for(;d<e.length-2&&e[d+1]<=l;)d++;i.push(Math.max(0,d-c+1))}const s=Math.round(uu(i));return Math.max(0,Math.min(s,Math.floor(t/4)))}function uu(e){let t=0;for(let n=0;n<e.length;n++)t+=e[n];return t/e.length}function ua(e,t){const n=Array.from(e);n.sort((a,i)=>a-i);const r=Math.max(0,Math.min(n.length-1,Math.floor(t/100*n.length)));return n[r]}function pi(e,t){for(let n=0;n<e.length;n++)if(e[n]>t)return n;return 0}function $l(e,t,n,r,a){const{data:i}=e.getImageData(t,n,r,a);let s=0;for(let o=3;o<i.length;o+=4)s+=i[o];return s/(255*(i.length/4))<.03}const zl=I.forwardRef(function({src:t,frameWidth:n,frameHeight:r,columns:a,rows:i,gutterX:s=0,gutterY:o=0,frameCount:l,atlasFrames:c,fps:d=12,autoplay:f=!0,loop:h=!0,startFrame:m=0,direction:y="forward",scale:x=1,backgroundColor:N,className:A,style:R,pauseWhenHidden:v=!1,originX:B,originY:S,onLoad:L,onEnd:K,onFrame:le},ie){const Y=I.useRef(null),j=I.useRef(null),[F,z]=I.useState(null),[$,Z]=I.useState(null),[ce,pe]=I.useState(null),[Ne,Pe]=I.useState(!1),[p,H]=I.useState(!!f),[W,b]=I.useState(m),X=I.useRef(1),[ee,G]=I.useState(null),te=Math.max(1,Math.floor(typeof window<"u"&&window.devicePixelRatio||1));I.useEffect(()=>{let de=!1;return Pe(!1),iu(t).then(({imgEl:xe,bitmap:ye})=>{de||(z(xe),Z(ye),pe({w:xe.naturalWidth,h:xe.naturalHeight}),Pe(!0),L?.())}).catch(()=>{}),()=>{de=!0}},[t,L]),I.useEffect(()=>{let de=!1;return(async()=>{if(!F)return;if(c&&c.length){G(null);return}if(!(!n||!r||!a||!i)){G(null);return}const ye=await jl(F,{debug:!0});de||(ye?G({frameWidth:ye.frameWidth,frameHeight:ye.frameHeight,columns:ye.columns,rows:ye.rows,gutterX:ye.gutterX,gutterY:ye.gutterY}):(G(null),console.warn("RemoteSpritesheet: auto-detect failed; please provide grid props.")))})(),()=>{de=!0}},[F,c,n,r,a,i]);const ne=ee?.frameWidth??n,Q=ee?.frameHeight??r,J=ee?.columns??a,Se=ee?.rows??i,U=ee?.gutterX??s,Le=ee?.gutterY??o,De=ee?.originX??B??0,st=ee?.originY??S??0,Lt=I.useMemo(()=>{if(c&&c.length>0)return{cols:0,rows:0};if(!ce||!ne||!Q)return{cols:0,rows:0};const de=J??Math.floor((ce.w+U)/(ne+U)),xe=Se??Math.floor((ce.h+Le)/(Q+Le));return{cols:Math.max(0,de),rows:Math.max(0,xe)}},[c,ce,ne,Q,J,Se,U,Le]),Me=I.useMemo(()=>{if(c?.length)return c;if(!ne||!Q)return[];const{cols:de,rows:xe}=Lt;if(!de||!xe)return[];const ye=Math.min(l??de*xe,de*xe),Te=[];for(let ve=0;ve<ye;ve++){const w=ve%de,P=Math.floor(ve/de),V=De+w*(ne+U),ue=st+P*(Q+Le);Te.push({x:V,y:ue,w:ne,h:Q})}if(F&&Te.length){const ve=document.createElement("canvas");ve.width=F.naturalWidth,ve.height=F.naturalHeight;const w=ve.getContext("2d");if(w){w.drawImage(F,0,0);let P=Te.length;for(;P>0&&$l(w,Te[P-1].x,Te[P-1].y,Te[P-1].w,Te[P-1].h);)P--;return Te.slice(0,P)}}return Te},[c,Lt,l,ne,Q,U,Le,F,De,st]),yt=()=>{const de=Y.current;if(!de||Me.length===0)return;const xe=Me[Math.max(0,Math.min(W,Me.length-1))],ye=Math.max(1,Math.round(xe.w*x)),Te=Math.max(1,Math.round(xe.h*x));de.style.width=ye+"px",de.style.height=Te+"px",de.width=ye*te,de.height=Te*te};I.useEffect(()=>{yt()},[Ne,x,Me.length,te,W]);const ut=de=>{const xe=$??F??null,ye=Y.current;if(!ye||!xe||Me.length===0)return;const Te=ye.getContext("2d");if(!Te)return;Te.save(),Te.setTransform(1,0,0,1,0,0),Te.clearRect(0,0,ye.width,ye.height),N&&(Te.fillStyle=N,Te.fillRect(0,0,ye.width,ye.height));const ve=Me[de];Te.scale(te,te),Te.drawImage(xe,ve.x,ve.y,ve.w,ve.h,0,0,Math.round(ve.w*x),Math.round(ve.h*x)),Te.restore()};return I.useEffect(()=>{if(!Ne||Me.length===0)return;let de=performance.now();const xe=1e3/Math.max(1,Math.min(120,d)),ye=Te=>{if(j.current=requestAnimationFrame(ye),!p){ut(W);return}const ve=Te-de;if(ve<xe){ut(W);return}de=Te-ve%xe,b(w=>{const P=Me.length-1;let V=w;return y==="forward"?V=w+1:y==="backward"?V=w-1:((w>=P&&X.current===1||w<=0&&X.current===-1)&&(X.current*=-1),V=w+X.current),(V>P||V<0)&&(h?V=y==="backward"?P:0:(V=Math.max(0,Math.min(V,P)),H(!1),K?.())),V})};return j.current=requestAnimationFrame(ye),()=>{j.current&&cancelAnimationFrame(j.current)}},[Ne,d,p,h,y,Me.length,W]),I.useEffect(()=>{ut(W),le?.(W)},[W,Ne,x,te]),I.useEffect(()=>{if(!v)return;const de=()=>{document.hidden&&H(!1)};return document.addEventListener("visibilitychange",de),()=>document.removeEventListener("visibilitychange",de)},[v]),I.useImperativeHandle(ie,()=>({play:()=>H(!0),pause:()=>H(!1),toggle:()=>H(de=>!de),gotoFrame:de=>b(()=>Math.max(0,Math.min(de,Math.max(0,Me.length-1)))),isPlaying:()=>p,currentFrame:()=>W}),[p,W,Me.length]),C.jsx("div",{className:A,style:R,children:C.jsx("canvas",{ref:Y,"aria-label":"spritesheet animation"})})});function Yl({url:e,className:t}){const[n,r]=I.useState(""),[a,i]=I.useState(null);return I.useEffect(()=>{let s=!1;return nu(e).then(({sanitized:o})=>{s||(r(o),i(null))}).catch(o=>!s&&i(o.message??"Failed to load")),()=>{s=!0}},[e]),a?C.jsxs("div",{className:t,role:"alert",children:["Couldn’t load SVG: ",a]}):n?C.jsx("div",{className:t,dangerouslySetInnerHTML:{__html:n}}):C.jsx("div",{className:t})}const Fa=({facet:e})=>{const{mode:t,assetUrl:n,spr_columns:r,spr_fps:a,spr_frameCount:i,spr_frameHeight:s,spr_frameWidth:o,spr_gutterX:l,spr_gutterY:c,spr_rows:d,spr_originX:f,spr_originY:h,spr_scale:m}=e;return C.jsxs("div",{className:"assetContainer",children:[t===Ve.AgentDisplayFacetModeType.LOTTIE&&C.jsx(Ul,{src:n,autoplay:!0,onError:y=>console.error(y)}),t===Ve.AgentDisplayFacetModeType.SVG&&C.jsx("div",{children:C.jsx(Yl,{url:n})}),t===Ve.AgentDisplayFacetModeType.IMAGE&&C.jsx(Fl,{src:n}),t===Ve.AgentDisplayFacetModeType.SPRITESHEET&&C.jsx(zl,{src:n,frameCount:i,columns:r,rows:d,fps:a,frameHeight:s,frameWidth:o,gutterX:l,gutterY:c,originX:f,originY:h,scale:m,autoplay:!0})]})},Gl=({isWidgetOpen:e,setIsWidgetOpen:t})=>{const{companySettings:n}=I.useContext(Rn);return C.jsxs("div",{className:`launcher
2
2
  fixed
3
3
  z-[999999999]
4
4
  transition