bridgeapp-ai-chat-widget 0.2.13 → 0.2.14
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.
|
@@ -50927,7 +50927,7 @@ function Aae({ className: i }) {
|
|
|
50927
50927
|
ee.useEffect(() => () => {
|
|
50928
50928
|
m("voice_mode_closed");
|
|
50929
50929
|
}, [m]);
|
|
50930
|
-
const B = ee.useMemo(() => u === Wc.Unavailable ? w_.connectionFailed : u === Wc.Offline ? w_.offline : b || null, [u, b]), G = ee.useMemo(() => v === "connecting" ? "Connecting..." : L ? "Assistant speaking..." : e ? "Muted" : "Start talking", [L, e, v]), W =
|
|
50930
|
+
const B = ee.useMemo(() => u === Wc.Unavailable ? w_.connectionFailed : u === Wc.Offline ? w_.offline : b || null, [u, b]), G = ee.useMemo(() => v === "connecting" ? "Connecting..." : L ? "Assistant speaking..." : e ? "Muted" : "Start talking", [L, e, v]), W = !s && B, k = !s && T, F = !s && A;
|
|
50931
50931
|
return /* @__PURE__ */ Xe.jsxs("div", { className: fl("z-1 flex w-full flex-col items-center", i), children: [
|
|
50932
50932
|
s && /* @__PURE__ */ Xe.jsx(
|
|
50933
50933
|
U3,
|
|
@@ -4575,7 +4575,7 @@ ${A}
|
|
|
4575
4575
|
`);z.trim()&&(e.customNestedParser?L=e.customNestedParser(z):L=t.blockTokens(z))}const I=e.createToken(T,L);h.push(I)}if(h.length!==0)return{items:h,raw:m}}function bO(i,e,t,n){if(!i||!Array.isArray(i.content))return"";const r=typeof t=="function"?t(n):t,[s,...l]=i.content,u=e.renderChildren([s]),h=[`${r}${u}`];return l&&l.length>0&&l.forEach(m=>{const _=e.renderChildren([m]);if(_){const p=_.split(`
|
|
4576
4576
|
`).map(v=>v?e.indent(v):"").join(`
|
|
4577
4577
|
`);h.push(p)}}),h.join(`
|
|
4578
|
-
`)}var Io=class j7 extends mO{constructor(){super(...arguments),this.type="node"}static create(e={}){const t=typeof e=="function"?e():e;return new j7(t)}configure(e){return super.configure(e)}extend(e){const t=typeof e=="function"?e():e;return super.extend(t)}};function qm(i){return new Hne({find:i.find,handler:({state:e,range:t,match:n,pasteEvent:r})=>{const s=hi(i.getAttributes,void 0,n,r);if(s===!1||s===null)return null;const{tr:l}=e,u=n[n.length-1],h=n[0];let m=t.to;if(u){const _=h.search(/\S/),p=t.from+h.indexOf(u),v=p+u.length;if(fO(t.from,t.to,e.doc).filter(T=>T.mark.type.excluded.find(A=>A===i.type&&A!==T.mark.type)).filter(T=>T.to>p).length)return null;v<t.to&&l.delete(v,t.to),p>t.from&&l.delete(t.from+_,p),m=t.from+_+u.length,l.addMark(t.from+_,m,i.type.create(s||{})),l.removeStoredMark(i.type)}}})}var xs=(i=>(i.Customer="PARTICIPANT_TYPE_CUSTOMER",i.Agent="PARTICIPANT_TYPE_AGENT",i))(xs||{}),Eh=(i=>(i.Completed="MESSAGE_STATUS_COMPLETED",i.Writing="MESSAGE_STATUS_WRITING",i.Failed="MESSAGE_STATUS_FAILED",i))(Eh||{}),SP=(i=>(i.ReactionMessage="REACTION_MESSAGE",i))(SP||{});function xO({message:i,source:e,className:t,...n}){return Xe.jsx("div",{...n,className:Oo("message flex h-fit min-h-9 w-fit max-w-70 items-center justify-center text-left backdrop-blur-xl",{"bg-purple-accent/80 rounded-bl-sm self-start ml-2":e===xs.Agent,"bg-purple-accent/80 rounded-bl-sm self-center ml-2":e==="REACTION_MESSAGE","bg-white/20 rounded-br-sm self-end mr-2":e===xs.Customer},t),children:i})}const EP="bridge-chat-widget-ui";function oie(){const i=localStorage.getItem(EP);if(!i)return null;try{return JSON.parse(i)}catch{return null}}function lie(i){localStorage.setItem(EP,JSON.stringify(i))}function cie(i){return Date.parse(i.expiresAt)<=Date.now()+36e5}const uie=1800*1e3;function fie(i,e,t){const n=ee.useRef(i),r=ee.useRef(e),s=n.current.customerInteractionSession,l=ee.useCallback(()=>{cie(s)&&r.current.onSessionExpired()},[s]);ee.useEffect(()=>{const u=()=>l();return window.addEventListener("focus",u),()=>window.removeEventListener("focus",u)},[l]),ee.useEffect(()=>{t&&l()},[t,l]),ee.useEffect(()=>{const u=window.setInterval(l,uie);return()=>window.clearInterval(u)},[l])}const XM=100,TP=async(i,e,t)=>{const{beforeMessageId:n,afterMessageId:r,pageSize:s}=t||{pageSize:XM};try{const l=await fetch(`${i.apiUrl}/messenger.Messenger/ListMessages`,{method:"POST",headers:{Authorization:`Bearer ${i.customerInteractionSession.accessToken}`,"Content-Type":"application/json"},body:JSON.stringify({chatId:i.customerInteractionSession.chatId,pagination:{per_page:s,cursor:{before:n,after:r}}})});if(!l.ok)throw new Error(`HTTP error! status: ${l.status}`);return(await l.json()).messages}catch(l){throw f1(l,e.onSessionExpired),l}};async function MP(i,e,t){try{const n=await fetch(`${i.apiUrl}/messenger.Messenger/ListMessagesForThreads`,{method:"POST",headers:{Authorization:`Bearer ${i.customerInteractionSession.accessToken}`,"Content-Type":"application/json"},body:JSON.stringify({ids:t})});if(!n.ok)throw new Error(`Request failed: ${n.status} ${n.statusText}`);return(await n.json()).messages}catch(n){throw f1(n,e.onSessionExpired),n}}async function wP(i,e,t,n){try{const r=await fetch(`${i.apiUrl}/messenger.Messenger/CreateMessage`,{method:"POST",headers:{Authorization:`Bearer ${i.customerInteractionSession.accessToken}`,"Content-Type":"application/json"},body:JSON.stringify({chatId:i.customerInteractionSession.chatId,threadId:n,content:{type:"text",text:t},attachmentIds:[]})});if(!r.ok)throw new Error(`Request failed: ${r.status} ${r.statusText}`);return await r.json()}catch(r){throw f1(r,e.onSessionExpired),console.error("sendMessage error",r),r}}const die=[],Th=n6((i,e)=>({currentThreadId:void 0,currentThreadMessages:[],threadIds:[],threadsPreviews:[],threads:new Map,messages:[],sendError:null,config:null,api:null,latestReadMessageDate:null,latestMessageDate:null,actions:{setLatestReadMessageDate:t=>{const n=e().latestReadMessageDate;n&&new Date(n)>=new Date(t)||i({latestReadMessageDate:t})},initMessageStore:async(t,n)=>{i({config:t,api:n}),await e().actions.fetchInitialThreadStarterMessages(),e().actions.fetchAllThreadStarterMessages()},fetchInitialThreadStarterMessages:async()=>{const n=(await TP(e().config,e().api,{pageSize:30})).map(s=>s.id),r=await MP(e().config,e().api,n);i({threadIds:[...e().threadIds,...n],messages:[...e().messages,...r]}),e().actions.updateThreads()},fetchAllThreadStarterMessages:async()=>{let t=XM;const n=[],r=[];let s=e().threadIds[0];for(;t===XM;){const l=await TP(e().config,e().api,{beforeMessageId:s,pageSize:XM});t=l.length,s=l[0]?.id,n.push(...l),r.push(...l.map(u=>u.id))}i({threadIds:[...e().threadIds,...r],messages:[...e().messages,...n]}),e().actions.updateThreads()},setCurrentThread:t=>{i({currentThreadId:t}),e().actions.setCurrentThreadMessages(t??"")},handleMessage:t=>{t.threadId||i({currentThreadId:t.id,threadIds:[...e().threadIds,t.id]});const r=e().messages.findIndex(s=>s.id===t.id);r===-1?e().actions.addMessage(t):e().actions.updateMessage(t,r),(t.status===Eh.Completed||t.status===Eh.Failed)&&i({latestMessageDate:t.updatedAt}),e().actions.updateThreads()},addMessage:t=>{i({messages:[...e().messages,t]})},updateMessage:(t,n)=>{const s=e().messages.map((l,u)=>u===n?{...t,agenticSteps:l.agenticSteps}:l);i({messages:s})},handleMessagesAgenticSteps:t=>{const n=e().messages,r=n.findIndex(l=>l.id===t.messageId),s=n.map((l,u)=>u===r?{...l,agenticSteps:t.agenticSteps}:l);i({messages:s}),e().actions.updateThreads()},sendMessage:async t=>{i({sendError:null});try{const n=await wP(e().config,e().api,t,e().currentThreadId);e().actions.handleMessage(n)}catch(n){const r=n instanceof Error?n.message:Py.sendMessageFallback;i({sendError:r})}},clearSendError(){i({sendError:null})},updateThreads:()=>{const t=new Map,n=e().messages;for(const s of e().threadIds)t.set(s,n.filter(l=>l.id===s||l.threadId===s));const r=Array.from(t.entries()).map(([s,l])=>({threadId:s,lastMessage:l[l.length-1]})).sort((s,l)=>new Date(l.lastMessage.updatedAt).getTime()-new Date(s.lastMessage.updatedAt).getTime());i({threads:t,threadsPreviews:r}),e().actions.setCurrentThreadMessages(e().currentThreadId??"")},setCurrentThreadMessages:t=>{const n=e().threads;i({currentThreadMessages:[...n.get(t)??die]})},fetchThreadMessages:async t=>{const n=await MP(e().config,e().api,[t]),r=e().messages;n.forEach(s=>{const l=r.findIndex(u=>u.id===s.id);l===-1?r.push(s):r[l]=s}),i({messages:[...r]}),e().actions.updateThreads()},getThreadsSummaryData:()=>{const t=e().threads;return Array.from(t.entries()).map(([n,r])=>({threadId:n,lastMessage:r[r.length-1]})).sort((n,r)=>new Date(r.lastMessage.updatedAt).getTime()-new Date(n.lastMessage.updatedAt).getTime())},sendServiceMessage:async t=>{const n=await wP(e().config,e().api,t,e().currentThreadId);i({currentThreadId:n.id,threadIds:[...e().threadIds,n.id]}),e().actions.addMessage(n),e().actions.updateThreads()}}})),hie=()=>Th(i=>i.currentThreadId),pie=()=>Th(i=>i.currentThreadMessages),mie=()=>Th(i=>i.threadsPreviews),gie=()=>Th(i=>i.currentThreadId),yie=()=>Th(i=>i.latestReadMessageDate),vie=()=>Th(i=>i.latestMessageDate),_ie=()=>Th(i=>i.sendError),$M=()=>Th(i=>i.actions),bie=640,xie=5e3,AP=ee.createContext(null),Sie=({children:i,config:e,api:t})=>{const[n,r]=ee.useState(!1),[s,l]=ee.useState(null),[u,h]=ee.useState(()=>!e.manualOpen),m=lm(),{sendServiceMessage:_}=$M(),p=ee.useCallback(F=>{r(F),e.manualOpen||h(!F)},[e.manualOpen]),v=ee.useRef(null),b=ee.useCallback(async(F,K)=>{Cn.debug("sendAndHandleServiceMessage",F,K),await _(F),m(pa.text),p(!0),v.current?.externalLaunchAnimationAction(K)},[]),T=ee.useCallback(F=>{Cn.debug("executeReaction",F),p(!0),m(pa.voice),v.current?.playReaction(F.voice,F.animation),l(F.text),setTimeout(()=>{l(null)},xie)},[]),M=ee.useCallback(F=>{v.current&&v.current.initSounds(F)},[v]),A=e.manualOpen||!u,w=oie(),[L,N]=ee.useState(w?.height??gB),[I,z]=ee.useState(w?.width??Lae),[B,G]=ee.useState(e.AIAvatar?0:1),W=window.innerWidth<=bie;fie(e,t,n),ee.useEffect(()=>(t.registerSetOpen(p),t.registerSendServiceMessage(b),t.registerExecuteReaction(T),t.registerSetLocale(M),()=>{t.registerSetOpen(null),t.registerSendServiceMessage(null),t.registerExecuteReaction(null),t.registerSetLocale(null)}),[t,p,T,b,M]),ee.useEffect(()=>{lie({width:I,height:L})},[I,L]);const k={open:n,setOpen:p,isChromeVisible:A,widgetHeight:L,setWidgetHeight:N,widgetWidth:I,setWidgetWidth:z,isMobile:W,config:e,api:t,loadingProgress:B,setLoadingProgress:G,assistantRef:v,reactionMessage:s,setReactionMessage:l};return Xe.jsx(AP.Provider,{value:k,children:i})};function El(){const i=ee.useContext(AP);if(!i)throw new Error("ChatWidget components must be used inside <ChatWidget>");return i}function Eie({className:i}){const[e,t]=ee.useState(!0),{config:n,api:r,reactionMessage:s}=El(),l=lm(),{networkStatus:u}=_D(),{initVoiceStore:h,endVoiceSession:m,setMicrophoneEnabled:_}=s6(),p=mr(K=>K.isVoiceBootstrapped),v=mr(K=>K.status),b=mr(K=>K.errorMessage),T=mr(K=>K.customerMessageTranscriptVisible),M=mr(K=>K.customerMessageTranscript),A=mr(K=>K.assistantMessageTranscriptVisible),w=mr(K=>K.assistantMessageTranscript),L=mr(K=>K.assistantResponseInProgress),{updateDevices:N}=jQ(),I=ee.useCallback(async()=>{const K=!e;if(!K&&!p){await N();try{await h(n,r)}catch(ne){console.error("voice init error",ne);return}}_(!K),t(K)},[n,p,e,_]),z=ee.useCallback(async()=>{await m("switched_to_text"),l(pa.text)},[]);ee.useEffect(()=>()=>{m("voice_mode_closed")},[m]);const B=ee.useMemo(()=>u===Ec.Unavailable?Py.connectionFailed:u===Ec.Offline?Py.offline:b||null,[u,b]),G=ee.useMemo(()=>v==="connecting"?"Connecting...":L?"Assistant speaking...":e?"Muted":"Start talking",[L,e,v]),W=ee.useMemo(()=>!s&&B,[s,B]),k=ee.useMemo(()=>!s&&T,[s,T]),F=ee.useMemo(()=>!s&&A,[s,A]);return Xe.jsxs("div",{className:Oo("z-1 flex w-full flex-col items-center",i),children:[s&&Xe.jsx(xO,{className:"mb-8",message:s,source:SP.ReactionMessage}),W&&Xe.jsx(FI,{className:"mb-8",message:B}),k&&Xe.jsx(xO,{className:"mb-8",message:M,source:xs.Customer}),F&&Xe.jsx(xO,{className:"mb-8",message:w,source:xs.Agent}),Xe.jsxs("div",{className:"flex flex-col items-center gap-6",children:[n.voiceEnabled&&Xe.jsxs("div",{className:"flex flex-col items-center gap-3 text-white",children:[Xe.jsx("span",{className:"text-xs font-medium text-white",children:G}),Xe.jsx(Rf,{className:`flex h-19 w-19 items-center justify-center rounded-full backdrop-blur-sm ${e?"bg-black/30":"bg-white/20"}`,onClick:I,disabled:v==="connecting",children:e?Xe.jsx(JW,{}):Xe.jsx(QW,{})})]}),Xe.jsx(Rf,{onClick:z,className:"data-hover:text-purple-accent flex h-10 w-10 items-center justify-center rounded-full text-white backdrop-blur-sm data-hover:bg-white",children:Xe.jsx(eZ,{})})]})]})}const Tie=i=>ee.createElement("svg",{width:15,height:17,viewBox:"0 0 15 17",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",...i},ee.createElement("path",{d:"M1.875 5.9375V10.9375C1.875 11.1861 1.77623 11.4246 1.60041 11.6004C1.4246 11.7762 1.18614 11.875 0.9375 11.875C0.68886 11.875 0.450403 11.7762 0.274587 11.6004C0.0987719 11.4246 0 11.1861 0 10.9375V5.9375C0 5.68886 0.0987719 5.4504 0.274587 5.27459C0.450403 5.09877 0.68886 5 0.9375 5C1.18614 5 1.4246 5.09877 1.60041 5.27459C1.77623 5.4504 1.875 5.68886 1.875 5.9375ZM4.0625 0C3.81386 0 3.5754 0.0987721 3.39959 0.274587C3.22377 0.450403 3.125 0.68886 3.125 0.9375V15.9375C3.125 16.1861 3.22377 16.4246 3.39959 16.6004C3.5754 16.7762 3.81386 16.875 4.0625 16.875C4.31114 16.875 4.5496 16.7762 4.72541 16.6004C4.90123 16.4246 5 16.1861 5 15.9375V0.9375C5 0.68886 4.90123 0.450403 4.72541 0.274587C4.5496 0.0987721 4.31114 0 4.0625 0ZM7.1875 2.5C6.93886 2.5 6.7004 2.59877 6.52459 2.77459C6.34877 2.9504 6.25 3.18886 6.25 3.4375V13.4375C6.25 13.6861 6.34877 13.9246 6.52459 14.1004C6.7004 14.2762 6.93886 14.375 7.1875 14.375C7.43614 14.375 7.6746 14.2762 7.85041 14.1004C8.02623 13.9246 8.125 13.6861 8.125 13.4375V3.4375C8.125 3.18886 8.02623 2.9504 7.85041 2.77459C7.6746 2.59877 7.43614 2.5 7.1875 2.5ZM10.3125 5C10.0639 5 9.8254 5.09877 9.64959 5.27459C9.47377 5.4504 9.375 5.68886 9.375 5.9375V10.9375C9.375 11.1861 9.47377 11.4246 9.64959 11.6004C9.8254 11.7762 10.0639 11.875 10.3125 11.875C10.5611 11.875 10.7996 11.7762 10.9754 11.6004C11.1512 11.4246 11.25 11.1861 11.25 10.9375V5.9375C11.25 5.68886 11.1512 5.4504 10.9754 5.27459C10.7996 5.09877 10.5611 5 10.3125 5ZM13.4375 3.75C13.1889 3.75 12.9504 3.84877 12.7746 4.02459C12.5988 4.2004 12.5 4.43886 12.5 4.6875V12.1875C12.5 12.4361 12.5988 12.6746 12.7746 12.8504C12.9504 13.0262 13.1889 13.125 13.4375 13.125C13.6861 13.125 13.9246 13.0262 14.1004 12.8504C14.2762 12.6746 14.375 12.4361 14.375 12.1875V4.6875C14.375 4.43886 14.2762 4.2004 14.1004 4.02459C13.9246 3.84877 13.6861 3.75 13.4375 3.75Z"})),Mie=i=>ee.createElement("svg",{width:22,height:22,viewBox:"0 0 22 22",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",...i},ee.createElement("path",{d:"M21.1996 0.549377C20.9639 0.313669 20.6697 0.14503 20.3472 0.0608259C20.0248 -0.0233778 19.6857 -0.0201025 19.3649 0.0703145L19.3443 0.076877L1.35269 5.53125C0.987805 5.63709 0.663677 5.85127 0.423239 6.14544C0.182802 6.43961 0.0374012 6.79987 0.00629592 7.17853C-0.0248094 7.55718 0.0598483 7.93634 0.249056 8.26581C0.438264 8.59527 0.723094 8.85948 1.06582 9.02344L8.90519 12.8438L12.7218 20.6859C12.8723 21.006 13.111 21.2764 13.41 21.4654C13.7089 21.6544 14.0556 21.7541 14.4093 21.7528C14.4627 21.7528 14.5171 21.7528 14.5714 21.7463C14.9511 21.717 15.3126 21.5719 15.6071 21.3304C15.9015 21.0889 16.1147 20.7628 16.2177 20.3963L21.6721 2.40469C21.6748 2.398 21.677 2.39111 21.6786 2.38406C21.769 2.06327 21.7723 1.72418 21.6881 1.4017C21.6039 1.07922 21.4353 0.785013 21.1996 0.549377ZM14.3343 18.8503L11.1121 12.2278L15.4246 7.92C15.5292 7.81536 15.6122 7.69112 15.6689 7.55439C15.7255 7.41767 15.7546 7.27112 15.7546 7.12313C15.7546 6.97513 15.7255 6.82859 15.6689 6.69186C15.6122 6.55513 15.5292 6.4309 15.4246 6.32625C15.3199 6.22161 15.1957 6.13859 15.059 6.08196C14.9222 6.02533 14.7757 5.99618 14.6277 5.99618C14.4797 5.99618 14.3332 6.02533 14.1964 6.08196C14.0597 6.13859 13.9355 6.22161 13.8308 6.32625L9.51832 10.6388L2.89863 7.41469L19.3114 2.4375L14.3343 18.8503Z"})),wie=i=>ee.createElement("svg",{width:21,height:21,viewBox:"0 0 21 21",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",...i},ee.createElement("path",{d:"M20.9437 1.90279C20.9437 1.90279 20.9437 1.91216 20.9437 1.91685L15.4874 19.9112C15.4048 20.2035 15.2348 20.4635 15.0002 20.6564C14.7656 20.8493 14.4776 20.9658 14.1749 20.9903C14.1318 20.994 14.0887 20.9959 14.0455 20.9959C13.7619 20.9968 13.4838 20.9165 13.2443 20.7645C13.0048 20.6125 12.8137 20.3952 12.6937 20.1381L9.28117 13.135C9.24703 13.0648 9.23563 12.9857 9.24857 12.9087C9.26151 12.8318 9.29814 12.7608 9.35336 12.7056L14.7834 7.2756C14.9181 7.1338 14.9921 6.94499 14.9896 6.74942C14.9871 6.55385 14.9083 6.36699 14.77 6.22869C14.6317 6.09039 14.4448 6.01158 14.2492 6.00908C14.0537 6.00658 13.8648 6.08057 13.723 6.21529L8.29024 11.6453C8.23508 11.7005 8.16407 11.7371 8.0871 11.7501C8.01013 11.763 7.93104 11.7516 7.86086 11.7175L0.851173 8.30591C0.577067 8.17441 0.34939 7.96272 0.198314 7.69889C0.047238 7.43506 -0.0201046 7.13156 0.00521012 6.8286C0.0305249 6.52563 0.147302 6.23751 0.340066 6.00242C0.53283 5.76732 0.792481 5.59635 1.08461 5.51216L19.079 0.0559103H19.093C19.3493 -0.0160696 19.62 -0.018593 19.8775 0.0485989C20.135 0.115791 20.37 0.250279 20.5583 0.438265C20.7467 0.626251 20.8817 0.860966 20.9494 1.11833C21.0171 1.37569 21.0151 1.64644 20.9437 1.90279Z"}));var qM=(i,e)=>{if(i==="slot")return 0;if(i instanceof Function)return i(e);const{children:t,...n}=e??{};if(i==="svg")throw new Error("SVG elements are not supported in the JSX syntax, use the array syntax instead");return[i,n,t]},Aie=/^\s*>\s$/,Cie=Io.create({name:"blockquote",addOptions(){return{HTMLAttributes:{}}},content:"block+",group:"block",defining:!0,parseHTML(){return[{tag:"blockquote"}]},renderHTML({HTMLAttributes:i}){return qM("blockquote",{...Dr(this.options.HTMLAttributes,i),children:qM("slot",{})})},parseMarkdown:(i,e)=>e.createNode("blockquote",void 0,e.parseChildren(i.tokens||[])),renderMarkdown:(i,e)=>{if(!i.content)return"";const t=">",n=[];return i.content.forEach(r=>{const u=e.renderChildren([r]).split(`
|
|
4578
|
+
`)}var Io=class j7 extends mO{constructor(){super(...arguments),this.type="node"}static create(e={}){const t=typeof e=="function"?e():e;return new j7(t)}configure(e){return super.configure(e)}extend(e){const t=typeof e=="function"?e():e;return super.extend(t)}};function qm(i){return new Hne({find:i.find,handler:({state:e,range:t,match:n,pasteEvent:r})=>{const s=hi(i.getAttributes,void 0,n,r);if(s===!1||s===null)return null;const{tr:l}=e,u=n[n.length-1],h=n[0];let m=t.to;if(u){const _=h.search(/\S/),p=t.from+h.indexOf(u),v=p+u.length;if(fO(t.from,t.to,e.doc).filter(T=>T.mark.type.excluded.find(A=>A===i.type&&A!==T.mark.type)).filter(T=>T.to>p).length)return null;v<t.to&&l.delete(v,t.to),p>t.from&&l.delete(t.from+_,p),m=t.from+_+u.length,l.addMark(t.from+_,m,i.type.create(s||{})),l.removeStoredMark(i.type)}}})}var xs=(i=>(i.Customer="PARTICIPANT_TYPE_CUSTOMER",i.Agent="PARTICIPANT_TYPE_AGENT",i))(xs||{}),Eh=(i=>(i.Completed="MESSAGE_STATUS_COMPLETED",i.Writing="MESSAGE_STATUS_WRITING",i.Failed="MESSAGE_STATUS_FAILED",i))(Eh||{}),SP=(i=>(i.ReactionMessage="REACTION_MESSAGE",i))(SP||{});function xO({message:i,source:e,className:t,...n}){return Xe.jsx("div",{...n,className:Oo("message flex h-fit min-h-9 w-fit max-w-70 items-center justify-center text-left backdrop-blur-xl",{"bg-purple-accent/80 rounded-bl-sm self-start ml-2":e===xs.Agent,"bg-purple-accent/80 rounded-bl-sm self-center ml-2":e==="REACTION_MESSAGE","bg-white/20 rounded-br-sm self-end mr-2":e===xs.Customer},t),children:i})}const EP="bridge-chat-widget-ui";function oie(){const i=localStorage.getItem(EP);if(!i)return null;try{return JSON.parse(i)}catch{return null}}function lie(i){localStorage.setItem(EP,JSON.stringify(i))}function cie(i){return Date.parse(i.expiresAt)<=Date.now()+36e5}const uie=1800*1e3;function fie(i,e,t){const n=ee.useRef(i),r=ee.useRef(e),s=n.current.customerInteractionSession,l=ee.useCallback(()=>{cie(s)&&r.current.onSessionExpired()},[s]);ee.useEffect(()=>{const u=()=>l();return window.addEventListener("focus",u),()=>window.removeEventListener("focus",u)},[l]),ee.useEffect(()=>{t&&l()},[t,l]),ee.useEffect(()=>{const u=window.setInterval(l,uie);return()=>window.clearInterval(u)},[l])}const XM=100,TP=async(i,e,t)=>{const{beforeMessageId:n,afterMessageId:r,pageSize:s}=t||{pageSize:XM};try{const l=await fetch(`${i.apiUrl}/messenger.Messenger/ListMessages`,{method:"POST",headers:{Authorization:`Bearer ${i.customerInteractionSession.accessToken}`,"Content-Type":"application/json"},body:JSON.stringify({chatId:i.customerInteractionSession.chatId,pagination:{per_page:s,cursor:{before:n,after:r}}})});if(!l.ok)throw new Error(`HTTP error! status: ${l.status}`);return(await l.json()).messages}catch(l){throw f1(l,e.onSessionExpired),l}};async function MP(i,e,t){try{const n=await fetch(`${i.apiUrl}/messenger.Messenger/ListMessagesForThreads`,{method:"POST",headers:{Authorization:`Bearer ${i.customerInteractionSession.accessToken}`,"Content-Type":"application/json"},body:JSON.stringify({ids:t})});if(!n.ok)throw new Error(`Request failed: ${n.status} ${n.statusText}`);return(await n.json()).messages}catch(n){throw f1(n,e.onSessionExpired),n}}async function wP(i,e,t,n){try{const r=await fetch(`${i.apiUrl}/messenger.Messenger/CreateMessage`,{method:"POST",headers:{Authorization:`Bearer ${i.customerInteractionSession.accessToken}`,"Content-Type":"application/json"},body:JSON.stringify({chatId:i.customerInteractionSession.chatId,threadId:n,content:{type:"text",text:t},attachmentIds:[]})});if(!r.ok)throw new Error(`Request failed: ${r.status} ${r.statusText}`);return await r.json()}catch(r){throw f1(r,e.onSessionExpired),console.error("sendMessage error",r),r}}const die=[],Th=n6((i,e)=>({currentThreadId:void 0,currentThreadMessages:[],threadIds:[],threadsPreviews:[],threads:new Map,messages:[],sendError:null,config:null,api:null,latestReadMessageDate:null,latestMessageDate:null,actions:{setLatestReadMessageDate:t=>{const n=e().latestReadMessageDate;n&&new Date(n)>=new Date(t)||i({latestReadMessageDate:t})},initMessageStore:async(t,n)=>{i({config:t,api:n}),await e().actions.fetchInitialThreadStarterMessages(),e().actions.fetchAllThreadStarterMessages()},fetchInitialThreadStarterMessages:async()=>{const n=(await TP(e().config,e().api,{pageSize:30})).map(s=>s.id),r=await MP(e().config,e().api,n);i({threadIds:[...e().threadIds,...n],messages:[...e().messages,...r]}),e().actions.updateThreads()},fetchAllThreadStarterMessages:async()=>{let t=XM;const n=[],r=[];let s=e().threadIds[0];for(;t===XM;){const l=await TP(e().config,e().api,{beforeMessageId:s,pageSize:XM});t=l.length,s=l[0]?.id,n.push(...l),r.push(...l.map(u=>u.id))}i({threadIds:[...e().threadIds,...r],messages:[...e().messages,...n]}),e().actions.updateThreads()},setCurrentThread:t=>{i({currentThreadId:t}),e().actions.setCurrentThreadMessages(t??"")},handleMessage:t=>{t.threadId||i({currentThreadId:t.id,threadIds:[...e().threadIds,t.id]});const r=e().messages.findIndex(s=>s.id===t.id);r===-1?e().actions.addMessage(t):e().actions.updateMessage(t,r),(t.status===Eh.Completed||t.status===Eh.Failed)&&i({latestMessageDate:t.updatedAt}),e().actions.updateThreads()},addMessage:t=>{i({messages:[...e().messages,t]})},updateMessage:(t,n)=>{const s=e().messages.map((l,u)=>u===n?{...t,agenticSteps:l.agenticSteps}:l);i({messages:s})},handleMessagesAgenticSteps:t=>{const n=e().messages,r=n.findIndex(l=>l.id===t.messageId),s=n.map((l,u)=>u===r?{...l,agenticSteps:t.agenticSteps}:l);i({messages:s}),e().actions.updateThreads()},sendMessage:async t=>{i({sendError:null});try{const n=await wP(e().config,e().api,t,e().currentThreadId);e().actions.handleMessage(n)}catch(n){const r=n instanceof Error?n.message:Py.sendMessageFallback;i({sendError:r})}},clearSendError(){i({sendError:null})},updateThreads:()=>{const t=new Map,n=e().messages;for(const s of e().threadIds)t.set(s,n.filter(l=>l.id===s||l.threadId===s));const r=Array.from(t.entries()).map(([s,l])=>({threadId:s,lastMessage:l[l.length-1]})).sort((s,l)=>new Date(l.lastMessage.updatedAt).getTime()-new Date(s.lastMessage.updatedAt).getTime());i({threads:t,threadsPreviews:r}),e().actions.setCurrentThreadMessages(e().currentThreadId??"")},setCurrentThreadMessages:t=>{const n=e().threads;i({currentThreadMessages:[...n.get(t)??die]})},fetchThreadMessages:async t=>{const n=await MP(e().config,e().api,[t]),r=e().messages;n.forEach(s=>{const l=r.findIndex(u=>u.id===s.id);l===-1?r.push(s):r[l]=s}),i({messages:[...r]}),e().actions.updateThreads()},getThreadsSummaryData:()=>{const t=e().threads;return Array.from(t.entries()).map(([n,r])=>({threadId:n,lastMessage:r[r.length-1]})).sort((n,r)=>new Date(r.lastMessage.updatedAt).getTime()-new Date(n.lastMessage.updatedAt).getTime())},sendServiceMessage:async t=>{const n=await wP(e().config,e().api,t,e().currentThreadId);i({currentThreadId:n.id,threadIds:[...e().threadIds,n.id]}),e().actions.addMessage(n),e().actions.updateThreads()}}})),hie=()=>Th(i=>i.currentThreadId),pie=()=>Th(i=>i.currentThreadMessages),mie=()=>Th(i=>i.threadsPreviews),gie=()=>Th(i=>i.currentThreadId),yie=()=>Th(i=>i.latestReadMessageDate),vie=()=>Th(i=>i.latestMessageDate),_ie=()=>Th(i=>i.sendError),$M=()=>Th(i=>i.actions),bie=640,xie=5e3,AP=ee.createContext(null),Sie=({children:i,config:e,api:t})=>{const[n,r]=ee.useState(!1),[s,l]=ee.useState(null),[u,h]=ee.useState(()=>!e.manualOpen),m=lm(),{sendServiceMessage:_}=$M(),p=ee.useCallback(F=>{r(F),e.manualOpen||h(!F)},[e.manualOpen]),v=ee.useRef(null),b=ee.useCallback(async(F,K)=>{Cn.debug("sendAndHandleServiceMessage",F,K),await _(F),m(pa.text),p(!0),v.current?.externalLaunchAnimationAction(K)},[]),T=ee.useCallback(F=>{Cn.debug("executeReaction",F),p(!0),m(pa.voice),v.current?.playReaction(F.voice,F.animation),l(F.text),setTimeout(()=>{l(null)},xie)},[]),M=ee.useCallback(F=>{v.current&&v.current.initSounds(F)},[v]),A=e.manualOpen||!u,w=oie(),[L,N]=ee.useState(w?.height??gB),[I,z]=ee.useState(w?.width??Lae),[B,G]=ee.useState(e.AIAvatar?0:1),W=window.innerWidth<=bie;fie(e,t,n),ee.useEffect(()=>(t.registerSetOpen(p),t.registerSendServiceMessage(b),t.registerExecuteReaction(T),t.registerSetLocale(M),()=>{t.registerSetOpen(null),t.registerSendServiceMessage(null),t.registerExecuteReaction(null),t.registerSetLocale(null)}),[t,p,T,b,M]),ee.useEffect(()=>{lie({width:I,height:L})},[I,L]);const k={open:n,setOpen:p,isChromeVisible:A,widgetHeight:L,setWidgetHeight:N,widgetWidth:I,setWidgetWidth:z,isMobile:W,config:e,api:t,loadingProgress:B,setLoadingProgress:G,assistantRef:v,reactionMessage:s,setReactionMessage:l};return Xe.jsx(AP.Provider,{value:k,children:i})};function El(){const i=ee.useContext(AP);if(!i)throw new Error("ChatWidget components must be used inside <ChatWidget>");return i}function Eie({className:i}){const[e,t]=ee.useState(!0),{config:n,api:r,reactionMessage:s}=El(),l=lm(),{networkStatus:u}=_D(),{initVoiceStore:h,endVoiceSession:m,setMicrophoneEnabled:_}=s6(),p=mr(K=>K.isVoiceBootstrapped),v=mr(K=>K.status),b=mr(K=>K.errorMessage),T=mr(K=>K.customerMessageTranscriptVisible),M=mr(K=>K.customerMessageTranscript),A=mr(K=>K.assistantMessageTranscriptVisible),w=mr(K=>K.assistantMessageTranscript),L=mr(K=>K.assistantResponseInProgress),{updateDevices:N}=jQ(),I=ee.useCallback(async()=>{const K=!e;if(!K&&!p){await N();try{await h(n,r)}catch(ne){console.error("voice init error",ne);return}}_(!K),t(K)},[n,p,e,_]),z=ee.useCallback(async()=>{await m("switched_to_text"),l(pa.text)},[]);ee.useEffect(()=>()=>{m("voice_mode_closed")},[m]);const B=ee.useMemo(()=>u===Ec.Unavailable?Py.connectionFailed:u===Ec.Offline?Py.offline:b||null,[u,b]),G=ee.useMemo(()=>v==="connecting"?"Connecting...":L?"Assistant speaking...":e?"Muted":"Start talking",[L,e,v]),W=!s&&B,k=!s&&T,F=!s&&A;return Xe.jsxs("div",{className:Oo("z-1 flex w-full flex-col items-center",i),children:[s&&Xe.jsx(xO,{className:"mb-8",message:s,source:SP.ReactionMessage}),W&&Xe.jsx(FI,{className:"mb-8",message:B}),k&&Xe.jsx(xO,{className:"mb-8",message:M,source:xs.Customer}),F&&Xe.jsx(xO,{className:"mb-8",message:w,source:xs.Agent}),Xe.jsxs("div",{className:"flex flex-col items-center gap-6",children:[n.voiceEnabled&&Xe.jsxs("div",{className:"flex flex-col items-center gap-3 text-white",children:[Xe.jsx("span",{className:"text-xs font-medium text-white",children:G}),Xe.jsx(Rf,{className:`flex h-19 w-19 items-center justify-center rounded-full backdrop-blur-sm ${e?"bg-black/30":"bg-white/20"}`,onClick:I,disabled:v==="connecting",children:e?Xe.jsx(JW,{}):Xe.jsx(QW,{})})]}),Xe.jsx(Rf,{onClick:z,className:"data-hover:text-purple-accent flex h-10 w-10 items-center justify-center rounded-full text-white backdrop-blur-sm data-hover:bg-white",children:Xe.jsx(eZ,{})})]})]})}const Tie=i=>ee.createElement("svg",{width:15,height:17,viewBox:"0 0 15 17",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",...i},ee.createElement("path",{d:"M1.875 5.9375V10.9375C1.875 11.1861 1.77623 11.4246 1.60041 11.6004C1.4246 11.7762 1.18614 11.875 0.9375 11.875C0.68886 11.875 0.450403 11.7762 0.274587 11.6004C0.0987719 11.4246 0 11.1861 0 10.9375V5.9375C0 5.68886 0.0987719 5.4504 0.274587 5.27459C0.450403 5.09877 0.68886 5 0.9375 5C1.18614 5 1.4246 5.09877 1.60041 5.27459C1.77623 5.4504 1.875 5.68886 1.875 5.9375ZM4.0625 0C3.81386 0 3.5754 0.0987721 3.39959 0.274587C3.22377 0.450403 3.125 0.68886 3.125 0.9375V15.9375C3.125 16.1861 3.22377 16.4246 3.39959 16.6004C3.5754 16.7762 3.81386 16.875 4.0625 16.875C4.31114 16.875 4.5496 16.7762 4.72541 16.6004C4.90123 16.4246 5 16.1861 5 15.9375V0.9375C5 0.68886 4.90123 0.450403 4.72541 0.274587C4.5496 0.0987721 4.31114 0 4.0625 0ZM7.1875 2.5C6.93886 2.5 6.7004 2.59877 6.52459 2.77459C6.34877 2.9504 6.25 3.18886 6.25 3.4375V13.4375C6.25 13.6861 6.34877 13.9246 6.52459 14.1004C6.7004 14.2762 6.93886 14.375 7.1875 14.375C7.43614 14.375 7.6746 14.2762 7.85041 14.1004C8.02623 13.9246 8.125 13.6861 8.125 13.4375V3.4375C8.125 3.18886 8.02623 2.9504 7.85041 2.77459C7.6746 2.59877 7.43614 2.5 7.1875 2.5ZM10.3125 5C10.0639 5 9.8254 5.09877 9.64959 5.27459C9.47377 5.4504 9.375 5.68886 9.375 5.9375V10.9375C9.375 11.1861 9.47377 11.4246 9.64959 11.6004C9.8254 11.7762 10.0639 11.875 10.3125 11.875C10.5611 11.875 10.7996 11.7762 10.9754 11.6004C11.1512 11.4246 11.25 11.1861 11.25 10.9375V5.9375C11.25 5.68886 11.1512 5.4504 10.9754 5.27459C10.7996 5.09877 10.5611 5 10.3125 5ZM13.4375 3.75C13.1889 3.75 12.9504 3.84877 12.7746 4.02459C12.5988 4.2004 12.5 4.43886 12.5 4.6875V12.1875C12.5 12.4361 12.5988 12.6746 12.7746 12.8504C12.9504 13.0262 13.1889 13.125 13.4375 13.125C13.6861 13.125 13.9246 13.0262 14.1004 12.8504C14.2762 12.6746 14.375 12.4361 14.375 12.1875V4.6875C14.375 4.43886 14.2762 4.2004 14.1004 4.02459C13.9246 3.84877 13.6861 3.75 13.4375 3.75Z"})),Mie=i=>ee.createElement("svg",{width:22,height:22,viewBox:"0 0 22 22",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",...i},ee.createElement("path",{d:"M21.1996 0.549377C20.9639 0.313669 20.6697 0.14503 20.3472 0.0608259C20.0248 -0.0233778 19.6857 -0.0201025 19.3649 0.0703145L19.3443 0.076877L1.35269 5.53125C0.987805 5.63709 0.663677 5.85127 0.423239 6.14544C0.182802 6.43961 0.0374012 6.79987 0.00629592 7.17853C-0.0248094 7.55718 0.0598483 7.93634 0.249056 8.26581C0.438264 8.59527 0.723094 8.85948 1.06582 9.02344L8.90519 12.8438L12.7218 20.6859C12.8723 21.006 13.111 21.2764 13.41 21.4654C13.7089 21.6544 14.0556 21.7541 14.4093 21.7528C14.4627 21.7528 14.5171 21.7528 14.5714 21.7463C14.9511 21.717 15.3126 21.5719 15.6071 21.3304C15.9015 21.0889 16.1147 20.7628 16.2177 20.3963L21.6721 2.40469C21.6748 2.398 21.677 2.39111 21.6786 2.38406C21.769 2.06327 21.7723 1.72418 21.6881 1.4017C21.6039 1.07922 21.4353 0.785013 21.1996 0.549377ZM14.3343 18.8503L11.1121 12.2278L15.4246 7.92C15.5292 7.81536 15.6122 7.69112 15.6689 7.55439C15.7255 7.41767 15.7546 7.27112 15.7546 7.12313C15.7546 6.97513 15.7255 6.82859 15.6689 6.69186C15.6122 6.55513 15.5292 6.4309 15.4246 6.32625C15.3199 6.22161 15.1957 6.13859 15.059 6.08196C14.9222 6.02533 14.7757 5.99618 14.6277 5.99618C14.4797 5.99618 14.3332 6.02533 14.1964 6.08196C14.0597 6.13859 13.9355 6.22161 13.8308 6.32625L9.51832 10.6388L2.89863 7.41469L19.3114 2.4375L14.3343 18.8503Z"})),wie=i=>ee.createElement("svg",{width:21,height:21,viewBox:"0 0 21 21",fill:"currentColor",xmlns:"http://www.w3.org/2000/svg",...i},ee.createElement("path",{d:"M20.9437 1.90279C20.9437 1.90279 20.9437 1.91216 20.9437 1.91685L15.4874 19.9112C15.4048 20.2035 15.2348 20.4635 15.0002 20.6564C14.7656 20.8493 14.4776 20.9658 14.1749 20.9903C14.1318 20.994 14.0887 20.9959 14.0455 20.9959C13.7619 20.9968 13.4838 20.9165 13.2443 20.7645C13.0048 20.6125 12.8137 20.3952 12.6937 20.1381L9.28117 13.135C9.24703 13.0648 9.23563 12.9857 9.24857 12.9087C9.26151 12.8318 9.29814 12.7608 9.35336 12.7056L14.7834 7.2756C14.9181 7.1338 14.9921 6.94499 14.9896 6.74942C14.9871 6.55385 14.9083 6.36699 14.77 6.22869C14.6317 6.09039 14.4448 6.01158 14.2492 6.00908C14.0537 6.00658 13.8648 6.08057 13.723 6.21529L8.29024 11.6453C8.23508 11.7005 8.16407 11.7371 8.0871 11.7501C8.01013 11.763 7.93104 11.7516 7.86086 11.7175L0.851173 8.30591C0.577067 8.17441 0.34939 7.96272 0.198314 7.69889C0.047238 7.43506 -0.0201046 7.13156 0.00521012 6.8286C0.0305249 6.52563 0.147302 6.23751 0.340066 6.00242C0.53283 5.76732 0.792481 5.59635 1.08461 5.51216L19.079 0.0559103H19.093C19.3493 -0.0160696 19.62 -0.018593 19.8775 0.0485989C20.135 0.115791 20.37 0.250279 20.5583 0.438265C20.7467 0.626251 20.8817 0.860966 20.9494 1.11833C21.0171 1.37569 21.0151 1.64644 20.9437 1.90279Z"}));var qM=(i,e)=>{if(i==="slot")return 0;if(i instanceof Function)return i(e);const{children:t,...n}=e??{};if(i==="svg")throw new Error("SVG elements are not supported in the JSX syntax, use the array syntax instead");return[i,n,t]},Aie=/^\s*>\s$/,Cie=Io.create({name:"blockquote",addOptions(){return{HTMLAttributes:{}}},content:"block+",group:"block",defining:!0,parseHTML(){return[{tag:"blockquote"}]},renderHTML({HTMLAttributes:i}){return qM("blockquote",{...Dr(this.options.HTMLAttributes,i),children:qM("slot",{})})},parseMarkdown:(i,e)=>e.createNode("blockquote",void 0,e.parseChildren(i.tokens||[])),renderMarkdown:(i,e)=>{if(!i.content)return"";const t=">",n=[];return i.content.forEach(r=>{const u=e.renderChildren([r]).split(`
|
|
4579
4579
|
`).map(h=>h.trim()===""?t:`${t} ${h}`);n.push(u.join(`
|
|
4580
4580
|
`))}),n.join(`
|
|
4581
4581
|
${t}
|
package/dist/index.types.d.ts
CHANGED
|
@@ -28,5 +28,11 @@ export declare class ChatWidgetInstance {
|
|
|
28
28
|
playAnimation(name: ExternallyAvailableActions): void;
|
|
29
29
|
setCustomerInteractionSession(session: CustomerInteractionSession): void;
|
|
30
30
|
setOpen(open: boolean): void;
|
|
31
|
+
setLocale(locale: string): void;
|
|
32
|
+
executeReaction(params: {
|
|
33
|
+
animation: ExternallyAvailableActions;
|
|
34
|
+
voice: string;
|
|
35
|
+
text: string;
|
|
36
|
+
}): void;
|
|
31
37
|
}
|
|
32
38
|
export type { CustomerInteractionSession };
|
package/package.json
CHANGED