@rqmii_immersive/agent-react-sdk 5.4.3 → 5.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"RqmiiAgentChat.d.ts","sourceRoot":"","sources":["../../src/ui/RqmiiAgentChat.tsx"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAA0B,EAAe,MAAM,UAAU,CAAC;AAwQ7F,wBAAgB,cAAc,CAAC,KAAK,EAAE,mBAAmB,2CAqDxD;AA0KD,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,0BAA0B,2CAuCtE"}
1
+ {"version":3,"file":"RqmiiAgentChat.d.ts","sourceRoot":"","sources":["../../src/ui/RqmiiAgentChat.tsx"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,mBAAmB,EAAE,0BAA0B,EAAe,MAAM,UAAU,CAAC;AAwQ7F,wBAAgB,cAAc,CAAC,KAAK,EAAE,mBAAmB,2CAqDxD;AA0KD,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,0BAA0B,2CA4CtE"}
package/dist/ui.cjs CHANGED
@@ -109,5 +109,5 @@
109
109
  `).map(d=>d.replace(/^>\s?/,"")).join(`
110
110
  `);i.push(a.jsx("blockquote",{className:"rqmii-markdown-blockquote",children:Qe(c,u)},u));return}if(/^[-*]\s+/m.test(l)){const c=l.split(`
111
111
  `);if(c.every(h=>/^[-*]\s+/.test(h.trim())||h.trim()==="")){const h=c.map(m=>m.trim().replace(/^[-*]\s+/,"")).filter(Boolean);i.push(a.jsx("ul",{className:"rqmii-markdown-ul",children:h.map((m,g)=>a.jsx("li",{children:te(m)},`${u}-li-${g}`))},u));return}}if(/^\d+\.\s+/m.test(l)){const c=l.split(`
112
- `);if(c.every(h=>/^\d+\.\s+/.test(h.trim())||h.trim()==="")){const h=c.map(m=>m.trim().replace(/^\d+\.\s+/,"")).filter(Boolean);i.push(a.jsx("ol",{className:"rqmii-markdown-ol",children:h.map((m,g)=>a.jsx("li",{children:te(m)},`${u}-li-${g}`))},u));return}}i.push(a.jsx("p",{className:"rqmii-markdown-p",children:Qe(l,u)},u))}),a.jsx("div",{className:`rqmii-markdown ${e}`.trim(),children:i})}const wn=r.memo(Sn);function sa({language:t,content:e,config:n,agent:i,onAccept:s,onDecline:o}){const l=e||n?.body?.[t]||E(t,"consentBody")||"",u=n?.acceptLabel?.[t]||E(t,"consentAccept"),c=n?.declineLabel?.[t]||E(t,"consentDecline");return a.jsxs("div",{className:_("rqmii-chat-panel rqmii-chat-panel--start rqmii-chat-panel--consent",Q()),dir:ee(t),children:[a.jsx("div",{className:"rqmii-chat-consent-body",children:a.jsx(wn,{content:l})}),a.jsxs("div",{className:"rqmii-chat-consent-actions",children:[a.jsx("button",{type:"button",className:"rqmii-chat-consent-button rqmii-chat-consent-button--decline",onClick:o,children:c}),a.jsx("button",{type:"button",className:"rqmii-chat-consent-button rqmii-chat-consent-button--accept",onClick:s,children:u})]})]})}function Mn(t){return t?typeof t=="string"?t:typeof t=="object"&&typeof t.text=="string"?t.text:"":""}function Cn(t){return t==="text"||t==="voice"||t==="text_and_voice"}const Ne=r.memo(function({agent:e,language:n,languages:i,onLanguageChange:s=ae,onEndSession:o=ae,onMinimize:l,messages:u,onSendMessage:c,isPreview:d=!1,chatKind:h,sdk:m,renderPanel:g,messagesRef:y,initialText:b,autoStartVoice:j}){const F=e.widget?.consent||e.consent,x=!!F?.enabled,v=F?.content?.[n]||F?.body?.[n]||"",f=F?.one_time!==!1,p=e.id?`rqmii_consent_${e.id}`:null,[S,T]=r.useState(()=>{if(!x||!v)return!0;if(f&&p&&typeof window<"u")try{return window.localStorage.getItem(p)==="true"}catch{return!1}return!1}),k=h||he(e),[w,M]=r.useState(()=>k==="voice"&&m.isSessionReady||k==="voice"&&!!j),[A,z]=r.useState(()=>(k==="text"||k==="text_and_voice")&&m.isSessionReady||!!b||k==="text_and_voice"&&!!j),[N,V]=r.useState(!1),[L,C]=r.useState(!1),[D,I]=r.useState(null),G=Xt(k==="voice"&&w||k==="text_and_voice"&&A),R=en(),H=x&&!!v&&!S&&!d,W=Cn(k),{events:Y}=P.useRqmiiConnection();r.useEffect(()=>{if(!Y)return;const K=()=>{W&&ze()&&V(!0)},de=()=>{W&&V(!1)};return Y.on("minimize_chat",K),Y.on("maximize_chat",de),()=>{Y.off("minimize_chat",K),Y.off("maximize_chat",de)}},[Y,W]);const Z=r.useCallback(()=>{if(f&&p&&typeof window<"u")try{window.localStorage.setItem(p,"true")}catch{}T(!0)},[f,p]),B=r.useCallback(()=>{l?l():o()},[l,o]),oe=K=>{s(K),d||m.changeSessionLanguage(K)},J=r.useCallback(K=>{if(I(null),C(!0),c){c(K);return}m.sendTextMessage(K)},[c,m]);r.useEffect(()=>{y&&(y.current=m.messages)});const ie=r.useRef(!1);r.useEffect(()=>{ie.current||(b?(ie.current=!0,J(b)):j&&(ie.current=!0,I(null),C(!0),m.isVoiceActive||m.startVoice()))},[]),r.useEffect(()=>{L&&m.isSessionReady&&(C(!1),k==="voice"?M(!0):z(!0))},[L,m.isSessionReady,k]),r.useEffect(()=>{L&&m.error&&(C(!1),I(m.error))},[L,m.error]);const se=r.useCallback(()=>{I(null),C(!0),m.isVoiceActive||m.startVoice()},[m]);if(m.error&&!d&&(A||w))return a.jsx(Je,{message:m.error,language:n,onClose:o});if(H)return a.jsx(sa,{language:n,content:v,config:e.consent,agent:e,onAccept:Z,onDecline:B});const re={agent:e,language:n,languages:i,chatKind:k,sdk:m,isPreview:d,onLanguageChange:oe,onClose:o,onMinimize:W?()=>V(!0):void 0,onRestore:W?()=>V(!1):void 0,onSend:J,voiceStarted:w,conversationStarted:A,isMinimized:N,elapsed:G,setVoiceStarted:M,setConversationStarted:z};return g?a.jsx(a.Fragment,{children:g(re)}):a.jsx(Pn,{...re,connecting:L,connectError:D,visualViewportHeight:R,onCloseWidget:l??o,initiateVoice:se,onSendSuggestion:K=>{I(null),C(!0),m.sendSuggestion?.(K)}})}),Pn=r.memo(function({agent:e,language:n,languages:i,chatKind:s,sdk:o,onLanguageChange:l,onClose:u,onMinimize:c,onRestore:d,onSend:h,voiceStarted:m,conversationStarted:g,isMinimized:y,elapsed:b,setVoiceStarted:j,setConversationStarted:F,visualViewportHeight:x,connecting:v=!1,connectError:f=null,onCloseWidget:p,initiateVoice:S,onSendSuggestion:T}){const k=e.widget?.behavior?.typing_indicator_enabled??!0,w=s==="text"?o.waitingForResponse&&k:!1,M=o.messages;if(s==="avatar")return a.jsx(ia,{agent:e,language:n,onClose:u});if(y)return a.jsx(zn,{agent:e,language:n,onRestore:d,onClose:u,isListening:o.isVoiceActive,isAudioPlaying:o.isAudioPlaying,isSpeaking:o.vad.isSpeaking,onToggleVoice:o.toggleVoice,elapsed:b,variant:s==="text"?"text":"voice"});if(s==="voice"&&m)return a.jsx(Fn,{agent:e,language:n,onEndSession:u,onMinimize:c,isListening:o.isVoiceActive,isLoading:o.vad.isLoading,isSpeaking:o.vad.isSpeaking,isAudioPlaying:o.isAudioPlaying,isAgentThinking:o.isAgentThinking,isAgentSpeaking:o.isAgentSpeaking,onToggleVoice:o.toggleVoice,elapsed:b,visualViewportHeight:x});if(s==="voice")return a.jsx(En,{agent:e,language:n,languages:i,onLanguageChange:l,onClose:p,isAudioPlaying:o.isAudioPlaying,isVoiceSetupLoading:o.isVoiceSetupLoading||v,onStartVoice:S,visualViewportHeight:x});if(!g&&(o.messages.length===0||v)){const A=(e.widget?.behavior?.suggested_messages?.[n]||e.conversation?.suggested_messages?.[n])?.filter(V=>V.trim())||void 0,z=V=>{o.sendSuggestion?T(V):h?.(V)},N=s==="text_and_voice"||he(e)==="text_and_voice";return a.jsx(xn,{agent:e,language:n,languages:i,onLanguageChange:l,onClose:p,onSend:h,isVoiceEnabled:N,isAudioPlaying:o.isAudioPlaying,isVoiceSetupLoading:o.isVoiceSetupLoading||v,onStartVoice:S,visualViewportHeight:x,disabled:v,error:f||void 0,suggestions:A,onSuggestionClick:z,compact:!!A})}return a.jsx(Tn,{agent:e,language:n,onEndSession:u,messages:M,partialTranscript:Mn(o.partialTranscript),onSend:h,isAudioPlaying:o.isAudioPlaying,isVoiceEnabled:s==="text_and_voice",isListening:o.isVoiceActive,isSpeaking:o.vad.isSpeaking,isLoading:o.vad.isLoading,isAgentThinking:o.isAgentThinking,isAgentSpeaking:o.isAgentSpeaking,onToggleVoice:o.toggleVoice,onStartVoice:o.startVoice,onMinimize:c,elapsed:b,visualViewportHeight:x,waitingForResponse:w})}),An={isListening:!1,isLoading:!1,isSpeaking:!1,toggle:async()=>!1};function oa({isSessionReady:t,sendTextMessage:e,messages:n}){const[i,s]=r.useState(null),[o,l]=r.useState(null),u=r.useCallback(()=>{s(c=>(c&&(t?e(c):l(c)),null))},[t,e]);return r.useEffect(()=>{i&&n.length!==0&&u()},[n,i,u]),r.useEffect(()=>{if(!i)return;const c=setTimeout(u,5e3);return()=>clearTimeout(c)},[i,u]),r.useEffect(()=>{!o||!t||(e(o),l(null))},[t,o,e]),{pendingWelcomeText:i,pendingMessage:o,setPendingWelcomeText:s,setPendingMessage:l,sendPendingWelcomeText:u}}function Ve({isSessionReady:t,signalClientReady:e}){const[n,i]=r.useState(!1),[s,o]=r.useState(void 0),l=r.useRef(!1);return r.useEffect(()=>{!t||l.current||!n||(l.current=!0,e(s),i(!1))},[t,n,s,e]),{emitClientReady:r.useCallback(c=>{t?(l.current=!0,e(c)):(o(c),i(!0))},[t,e])}}const qn=300;function ra(t){const{isPreview:e=!1,...n}=t,{messages:i,sendTextMessage:s,emitUserMessage:o,addLocalMessage:l,clearMessages:u}=P.useRqmiiMessages(),{changeSessionLanguage:c,clearSessionHistory:d,signalClientReady:h,error:m,isSessionReady:g,isAgentThinking:y,isAgentSpeaking:b}=P.useRqmiiConnection(),j=r.useRef(!1),F=r.useRef(null),x=r.useRef(!1),[v,f]=r.useState(!1),[p,S]=r.useState(null),[T,k]=r.useState(!1),w=r.useRef(i.length);r.useEffect(()=>{if(i.length>w.current){const C=i[i.length-1];C.role==="assistant"&&C.streaming&&k(!1)}w.current=i.length},[i]),P.useRqmiiEvents("message_rate_limited",r.useCallback(()=>{k(!1)},[])),r.useEffect(()=>{if(!g||!v||x.current)return;const C=F.current;if(C===null)return;x.current=!0,F.current=null,l("user",C),S(null);const D=t.agent.welcomeMessages?.[t.language];h(D?{welcomeMessage:D}:void 0);const I=setTimeout(()=>{o(C),k(!0)},qn);return()=>clearTimeout(I)},[g,v,h,o,l]);const M=r.useMemo(()=>p?[{id:"first-user",role:"user",text:p},...i]:i,[p,i]),A=r.useCallback(()=>{S(null),u()},[u]),z={emitClientReady:h,sendMessage:C=>{s(C)},queueForWelcome:ae,enableVoice:ae,disableVoice:ae,language:t.language,isSessionReady:g,supportsVoice:!1,isVoiceActive:!1,chatKind:"text"},N=(C,D)=>{t.onSessionStart?.(),j.current?(k(!0),D.sendMessage(C)):(j.current=!0,S(C),F.current=C,f(!0),k(!0))},V=C=>{t.onTextSend?t.onTextSend(C,z):N(C,z)},L={messages:M,sendTextMessage:V,clearMessages:A,clearSessionHistory:d,isAudioPlaying:!1,partialTranscript:null,changeSessionLanguage:c,error:m,isAgentThinking:y,isAgentSpeaking:b,isVoiceSetupLoading:!1,isVoiceActive:!1,isSessionReady:g,vad:An,startVoice:ae,prepareVoice:ae,toggleVoice:ae,signalClientReady:h,waitingForResponse:T,sendSuggestion:V};return a.jsx(Ne,{...n,isPreview:e,chatKind:"text",sdk:L})}function la(t){const{isPreview:e=!1,...n}=t,{messages:i,sendTextMessage:s,clearMessages:o}=P.useRqmiiMessages(),{isAudioPlaying:l,partialTranscript:u}=P.useRqmiiVoice(),{changeSessionLanguage:c,clearSessionHistory:d,signalClientReady:h,error:m,isSessionReady:g,isAgentThinking:y,isAgentSpeaking:b}=P.useRqmiiConnection(),{setPendingWelcomeText:j,setPendingMessage:F}=oa({isSessionReady:g,sendTextMessage:s,messages:i}),{emitClientReady:x}=Ve({isSessionReady:g,signalClientReady:h}),[v,f]=r.useState(!1),p=P.useRqmiiVAD({enabled:!0,preloadStream:!1}),{isLoading:S,isListening:T,toggle:k}=p,w=S,M=T||v;r.useEffect(()=>{!v||!g||S||T||k()},[v,g,S,T,k]);const A=()=>{t.onSessionStart?.()},z={emitClientReady:x,sendMessage:I=>{g?s(I):F(I)},queueForWelcome:I=>j(I),enableVoice:()=>f(!0),disableVoice:()=>{f(!1),T&&k()},language:t.language,isSessionReady:g,supportsVoice:!0,isVoiceActive:M,chatKind:"voice"},N=I=>{A(),I.enableVoice();const U=t.agent.welcomeMessages?.[t.language];I.emitClientReady(U?{welcomeMessage:U}:void 0)},V=I=>{I.isVoiceActive?I.disableVoice():I.enableVoice()},D={messages:i,sendTextMessage:()=>{},clearMessages:o,clearSessionHistory:d,isAudioPlaying:l,partialTranscript:u,changeSessionLanguage:c,error:m,isAgentThinking:y,isAgentSpeaking:b,isVoiceSetupLoading:w,isVoiceActive:M,isSessionReady:g,vad:p,startVoice:()=>{t.onVoiceStart?t.onVoiceStart(z):N(z)},prepareVoice:()=>{},toggleVoice:()=>{t.onVoiceToggle?t.onVoiceToggle(z):V(z)},signalClientReady:h};return a.jsx(Ne,{...n,isPreview:e,chatKind:"voice",sdk:D})}const _n=300;function ca(t){const{isPreview:e=!1,...n}=t,{messages:i,sendTextMessage:s,emitUserMessage:o,addLocalMessage:l,clearMessages:u}=P.useRqmiiMessages(),{isAudioPlaying:c,partialTranscript:d}=P.useRqmiiVoice(),{changeSessionLanguage:h,clearSessionHistory:m,signalClientReady:g,error:y,isSessionReady:b,isAgentThinking:j,isAgentSpeaking:F}=P.useRqmiiConnection(),{emitClientReady:x}=Ve({isSessionReady:b,signalClientReady:g}),[v,f]=r.useState(!1),p=r.useRef(null),[S,T]=r.useState(!1),k=r.useRef(null),[w,M]=r.useState(!1),A=r.useRef(!1),z=r.useRef(!1),N=r.useRef(!1),[V,L]=r.useState(null),[C,D]=r.useState(!1),I=r.useRef(i.length);r.useEffect(()=>{if(i.length>I.current){const q=i[i.length-1];q.role==="assistant"&&q.streaming&&D(!1)}I.current=i.length},[i]),P.useRqmiiEvents("message_rate_limited",r.useCallback(()=>{D(!1)},[]));const U=P.useRqmiiVAD({enabled:!0,preloadStream:!1}),{isLoading:G,isListening:R,toggle:H}=U,W=G,Y=R||v;r.useEffect(()=>{!v||!b||G||R||H()},[v,b,G,R,H]),r.useEffect(()=>{if(!b||!S||N.current)return;const q=p.current;if(q===null)return;N.current=!0,p.current=null,l("user",q),L(null);const X=t.agent.welcomeMessages?.[t.language];g(X?{welcomeMessage:X}:void 0);const xe=setTimeout(()=>{o(q),D(!0)},_n);return()=>clearTimeout(xe)},[b,S,g,o,l]),r.useEffect(()=>{if(!b||!w||A.current)return;const q=k.current;q!==null&&(A.current=!0,k.current=null,l("user",q),o(q),D(!0),M(!1))},[b,w,o,l]);const Z=r.useMemo(()=>V?[{id:"first-user",role:"user",text:V},...i]:i,[V,i]),B=r.useCallback(()=>{L(null),z.current=!1,N.current=!1,u()},[u]),oe=()=>{t.onSessionStart?.()},J={emitClientReady:q=>{N.current=!0,z.current=!0,x(q)},sendMessage:q=>{s(q)},queueForWelcome:()=>{},enableVoice:()=>f(!0),disableVoice:()=>{f(!1),R&&H()},language:t.language,isSessionReady:b,supportsVoice:!0,isVoiceActive:Y,chatKind:"text_and_voice"},ie=(q,X)=>{oe(),!z.current&&!N.current?(z.current=!0,t.onStartText?.(),L(q),p.current=q,T(!0),D(!0)):(z.current=!0,D(!0),X.sendMessage(q))},se=q=>{oe(),z.current=!0,N.current=!0,t.onStartVoice?.(),q.enableVoice();const X=t.agent.welcomeMessages?.[t.language];q.emitClientReady(X?{welcomeMessage:X}:void 0)},re=q=>{q.isVoiceActive?q.disableVoice():q.enableVoice()},ye={messages:Z,sendTextMessage:q=>{t.onTextSend?t.onTextSend(q,J):ie(q,J)},clearMessages:B,clearSessionHistory:m,isAudioPlaying:c,partialTranscript:d,changeSessionLanguage:h,error:y,isAgentThinking:j,isAgentSpeaking:F,isVoiceSetupLoading:W,isVoiceActive:Y,isSessionReady:b,vad:U,startVoice:()=>{t.onVoiceStart?t.onVoiceStart(J):se(J)},prepareVoice:()=>{},toggleVoice:()=>{t.onVoiceToggle?t.onVoiceToggle(J):re(J)},signalClientReady:g,waitingForResponse:C,sendSuggestion:q=>{oe(),z.current=!0,N.current=!0,t.onStartVoice?.(),f(!0),k.current=q,M(!0)}};return a.jsx(Ne,{...n,isPreview:e,chatKind:t.textOnly?"text":"text_and_voice",sdk:ye})}function da(t){const{isPreview:e=!1,agent:n,language:i,languages:s,onLanguageChange:o,onEndSession:l,onMinimize:u,messages:c,onSendMessage:d}=t,h=l||u||ae,{messages:m,sendTextMessage:g,clearMessages:y}=P.useRqmiiMessages(),{changeSessionLanguage:b,clearSessionHistory:j,signalClientReady:F,error:x,isSessionReady:v,isAgentThinking:f,isAgentSpeaking:p}=P.useRqmiiConnection();return oa({isSessionReady:v,sendTextMessage:g,messages:m}),Ve({isSessionReady:v,signalClientReady:F}),x&&!e?a.jsx(Je,{message:x,language:i,onClose:h}):a.jsx(ia,{agent:n,language:i,onClose:h})}function Ln({agent:t,defaultOpen:e,className:n="",preview:i=!1,ChatBoxComponent:s}){const o=e!==void 0?e:t.widget?.behavior?.default_open??!1,[l,u]=r.useState(o),[c,d]=r.useState(t.defaultLanguage),h=r.useMemo(()=>ce(t.languages,t.defaultLanguage),[t.defaultLanguage,t.languages]);r.useEffect(()=>{d(t.defaultLanguage)},[t.defaultLanguage]),r.useEffect(()=>{u(o)},[o]);const m=t.widget?.appearance?.theme?.mode==="light"?"light":"dark",g=r.useMemo(()=>Ae(t.widget?.appearance),[t.widget?.appearance]);return a.jsxs("div",{className:_("rqmii-chat-surface",`rqmii-theme-${m}`,i&&"is-preview",n),"data-theme":m,style:g,children:[!l&&a.jsx(Le,{agent:t,onClick:()=>u(!0),position:i?"static":"fixed"}),l&&a.jsx(s,{agent:t,language:c,languages:h,onLanguageChange:d,onClose:()=>u(!1),isPreview:i})]})}function In({restUrl:t,identifier:e,fallbackAgent:n,withCredentials:i=!1,children:s,onError:o}){const[l,u]=r.useState(()=>n?le(n):null),[c,d]=r.useState(null);return r.useEffect(()=>{n&&(u(le(n)),d(null))},[n]),r.useEffect(()=>{if(!e||n)return;const h=new AbortController;return d(null),He({restUrl:t,identifier:e,withCredentials:i,signal:h.signal}).then(u).catch(m=>{h.signal.aborted||(n||d(m),o?.(m))}),()=>h.abort()},[n,e,o,t,i]),c?a.jsx("div",{className:"rqmii-chat-panel rqmii-chat-panel--error",children:c.message||"Could not load agent"}):l?a.jsx(a.Fragment,{children:s(l)}):a.jsx("div",{className:"rqmii-chat-panel rqmii-chat-panel--error",children:"Loading"})}function Se({agent:t,language:e,languages:n,onLanguageChange:i,onEndSession:s,onMinimize:o,onStartText:l,onStartVoice:u,textOnly:c,messages:d,onSendMessage:h,isPreview:m,onSessionStart:g,initialText:y,autoStartVoice:b}){const j=he(t),F={agent:t,language:e,languages:n,onLanguageChange:i,onEndSession:s,onMinimize:o,messages:d,onSendMessage:h,isPreview:m,onSessionStart:g,initialText:y,autoStartVoice:b};switch(j){case"avatar":return a.jsx(da,{...F});case"voice":return a.jsx(la,{...F});case"text_and_voice":return a.jsx(ca,{...F,onStartText:l,onStartVoice:u,textOnly:c});case"text":default:return a.jsx(ra,{...F})}}function Nn({agent:t,language:e,languages:n,startMode:i,directive:s,open:o,onOpen:l,onLanguageChange:u,onMinimize:c,onStartNewSession:d,onSessionStart:h,onStartText:m,onStartVoice:g}){const{connectionState:y,isConnected:b,isSessionTerminated:j,creditExhausted:F,session:x,endSession:v,submitFeedback:f,interrupt:p}=P.useRqmiiConnection(),{messages:S}=P.useRqmiiMessages(),T=r.useRef(!1),k=r.useRef(null),[w,M]=r.useState(null),[A,z]=r.useState(0),[N,V]=r.useState(!1),[L,C]=r.useState(!1),[D,I]=r.useState(!1);r.useEffect(()=>{b&&(T.current=!0)},[b]),r.useEffect(()=>{if(w)return;let B=null;j||y==="ended"?B=k.current==="user"?"user_ended":"terminated":y==="failed"?F?B="credits_exhausted":T.current?B="connection_lost":B="failed":F&&(B="credits_exhausted"),B&&M({sessionId:x.sessionId,endedBy:k.current??"agent",reason:B})},[j,y,F,x.sessionId,w]);const U=()=>{k.current="user",p(),v()},G=w?.sessionId??null,R=B=>{N||(z(B),G&&f({sessionId:G,rating:B,endedBy:w?.endedBy}).catch(()=>{}),D||(I(!0),C(!0)))},H=async B=>{if(G)try{await f({sessionId:G,feedback:B,endedBy:w?.endedBy})}catch{}V(!0),C(!1)};if(!o)return a.jsx(Le,{agent:t,onClick:l,active:y==="connected"});const W=he(t),Y=t.agentType==="cloud_avatar"||t.agentType==="desktop_avatar",Z=i==="text";return Y?a.jsx(Se,{agent:t,language:e,languages:n,onLanguageChange:u,onEndSession:U,onMinimize:c,onSessionStart:h,onStartText:m,onStartVoice:g,textOnly:Z}):w?L&&G?a.jsx(aa,{agent:t,language:e,rating:A,onBack:()=>C(!1),onMinimize:c,onSubmit:H}):a.jsx(na,{agent:t,language:e,languages:n,chatKind:W==="avatar"?"text":W,messages:S,onLanguageChange:u,onMinimize:c,onStartNewVoice:()=>d("voice"),onStartNewText:B=>d("text",B),rating:A,endedBy:w.endedBy,sessionId:G,ratingInteractive:!N,showTellUsMore:D&&!N,onRate:R,onTellUsMore:()=>C(!0)}):a.jsx(Se,{agent:t,language:e,languages:n,onLanguageChange:u,onEndSession:U,onMinimize:c,onSessionStart:h,onStartText:m,onStartVoice:g,textOnly:Z,initialText:s?.mode==="text"?s.text:void 0,autoStartVoice:s?.mode==="voice"})}function Vn(t){const{wsUrl:e,restUrl:n,apiKey:i,agentId:s,agent:o,language:l,defaultOpen:u,open:c,className:d="",debug:h=!1,withCredentials:m=!1,onError:g,onInitialized:y,config:b,promptOverrides:j,clientTools:F,onMinimize:x,onOpen:v}=t;if([!!i,!!s].filter(Boolean).length>1)throw new Error("RqmiiAgentChat: only one of apiKey, or agentId may be provided");const p=i||s;return a.jsx(In,{restUrl:n,identifier:p,fallbackAgent:o,withCredentials:m,onError:g,children:S=>a.jsx(Dn,{wsUrl:e,restUrl:n,apiKey:i,agentId:s,agent:S,language:l,defaultOpen:u,open:c,className:d,debug:h,withCredentials:m,onError:g,onInitialized:y,config:b,promptOverrides:j,clientTools:F,onMinimize:x,onOpen:v})})}function Dn({wsUrl:t,restUrl:e,apiKey:n,agentId:i,agent:s,language:o,defaultOpen:l,open:u,className:c,debug:d,withCredentials:h,onError:m,onInitialized:g,config:y,promptOverrides:b,clientTools:j,onMinimize:F,onOpen:x}){const v=l!==void 0?l:s.widget?.behavior?.default_open??!1,[f,p]=r.useState(v),S=u!==void 0?u:f;r.useEffect(()=>{u===void 0&&p(v)},[v,u]);const[T,k]=r.useState(!1),[w,M]=r.useState(o||s.defaultLanguage),[A,z]=r.useState("idle"),[N,V]=r.useState(0),[L,C]=r.useState(null),[D,I]=r.useState(!1),U=s.agentType==="cloud_avatar"||s.agentType==="desktop_avatar",G=U,R=s.widget?.behavior?.welcome_bubble,H=s.welcomeMessages?.[w]||"",W=!S&&!D&&!G&&!!R?.enabled&&!!H.trim(),Y=r.useMemo(()=>ce(s.languages,s.defaultLanguage),[s.defaultLanguage,s.languages]);r.useEffect(()=>{Ze(s.widget?.appearance?.font)},[s.widget?.appearance?.font]);const Z=r.useMemo(()=>A==="text"?{...y??{},use_voice:!1,use_text:!0}:y,[y,A]),B=r.useMemo(()=>Tt(s,Z),[s,Z]),oe=!G&&B.useVoice,J=U?S:T,ie=r.useMemo(()=>({...n?{apiKey:n}:{},...i?{agentId:i}:{},language:w,config:Z,...b?{promptOverrides:b}:{}}),[i,n,w,Z,b]),se=r.useRef(null),re=r.useRef(!1),K=r.useRef(N),de=K.current!==N;K.current=N,T&&(!re.current||de)&&(se.current=ie),re.current=T,T||(se.current=null);const De=T&&se.current?se.current:ie;r.useEffect(()=>{M(o||s.defaultLanguage)},[s.defaultLanguage,o]);const ye=r.useMemo(()=>m?ue=>{z("idle"),m(ue)}:ue=>{z("idle")},[m]),q=()=>{z("text"),k(!0)},X=()=>{z("voice"),k(!0)},xe=()=>{u===void 0&&p(!1),F?.()},ua=()=>{u===void 0&&p(!0),I(!0),x?.()},ma=(ue,ga)=>{C(ue==="text"?{mode:"text",text:ga??""}:{mode:"voice"}),z(ue),k(!0),V(pa=>pa+1)},Re=s.widget?.appearance?.theme?.mode==="light"?"light":"dark",ha=r.useMemo(()=>Ae(s.widget?.appearance),[s.widget?.appearance]);return a.jsx(P.RqmiiAgentRoom,{wsUrl:t,restUrl:e,session:De,connect:J,enableAudio:oe,debug:d,withCredentials:h,onError:ye,onInitialized:g,clientTools:j,children:a.jsxs("div",{className:_("rqmii-chat-surface",`rqmii-theme-${Re}`,s.widget?.appearance?.position==="left"&&"rqmii-position-left",c),"data-theme":Re,style:ha,dir:"ltr",children:[W&&a.jsx(sn,{message:H,delaySeconds:R?.delay_seconds??3,soundEnabled:!!R?.sound_enabled,language:w,buttonPosition:s.widget?.appearance?.position==="left"?"left":"right",onClick:()=>I(!0)}),a.jsx(Nn,{agent:s,language:w,languages:Y,startMode:A,directive:L,open:S,onOpen:ua,onLanguageChange:M,onMinimize:xe,onStartNewSession:ma,onSessionStart:()=>k(!0),onStartText:q,onStartVoice:X})]})},N)}function Rn(t){const{agent:e,language:n,open:i=!0,className:s="",messages:o,onSendMessage:l,onClose:u}=t,c=r.useMemo(()=>le(e),[e]),[d,h]=r.useState(i),[m,g]=r.useState(n||c.defaultLanguage),y=r.useMemo(()=>ce(c.languages,c.defaultLanguage),[c.defaultLanguage,c.languages]),b=c.widget?.appearance?.theme?.mode==="light"?"light":"dark",j=r.useMemo(()=>Ae(c.widget?.appearance),[c.widget?.appearance]);return r.useEffect(()=>{n&&g(n)},[n]),r.useEffect(()=>{h(i)},[i]),r.useEffect(()=>{Ze(c.widget?.appearance?.font)},[c.widget?.appearance?.font]),a.jsx(P.RqmiiAgentProvider,{wsUrl:"",restUrl:"",enableAudio:!1,children:a.jsx("div",{className:_("rqmii-chat-surface is-preview",`rqmii-theme-${b}`,s),"data-theme":b,style:j,dir:"ltr",children:d?a.jsx(Se,{agent:c,language:m,languages:y,onLanguageChange:g,onEndSession:()=>{h(!1),u?.()},onMinimize:()=>{h(!1),u?.()},messages:o,onSendMessage:l,isPreview:!0}):null})})}exports.EpicPixelStreamingPlayer=P.EpicPixelStreamingPlayer;exports.RqmiiAgentRoom=P.RqmiiAgentRoom;exports.RqmiiCloudAvatarPlayer=P.RqmiiCloudAvatarPlayer;exports.ConsentDialog=sa;exports.EndedChatPanel=na;exports.FeedbackPanel=aa;exports.RQMII_CHAT_LANGUAGES=pe;exports.RQMII_CHAT_LANGUAGE_VALUES=Te;exports.RatingBar=ea;exports.RqmiiAgentAvatar=$;exports.RqmiiAgentChat=Vn;exports.RqmiiAgentChatPreview=Rn;exports.RqmiiAvatarChatBox=da;exports.RqmiiChatFooter=ne;exports.RqmiiChatOpenButton=Le;exports.RqmiiChatSurface=Ln;exports.RqmiiTextAndVoiceChatBox=ca;exports.RqmiiTextChatBox=ra;exports.RqmiiVoiceChatBox=la;exports.fetchAgentConfig=Et;exports.fetchAgentCustomization=He;exports.getChatLanguage=Fe;exports.getChatText=E;exports.normalizeChatLanguages=ce;exports.normalizePublicAgent=le;exports.resolveChatKind=he;
112
+ `);if(c.every(h=>/^\d+\.\s+/.test(h.trim())||h.trim()==="")){const h=c.map(m=>m.trim().replace(/^\d+\.\s+/,"")).filter(Boolean);i.push(a.jsx("ol",{className:"rqmii-markdown-ol",children:h.map((m,g)=>a.jsx("li",{children:te(m)},`${u}-li-${g}`))},u));return}}i.push(a.jsx("p",{className:"rqmii-markdown-p",children:Qe(l,u)},u))}),a.jsx("div",{className:`rqmii-markdown ${e}`.trim(),children:i})}const wn=r.memo(Sn);function sa({language:t,content:e,config:n,agent:i,onAccept:s,onDecline:o}){const l=e||n?.body?.[t]||E(t,"consentBody")||"",u=n?.acceptLabel?.[t]||E(t,"consentAccept"),c=n?.declineLabel?.[t]||E(t,"consentDecline");return a.jsxs("div",{className:_("rqmii-chat-panel rqmii-chat-panel--consent",Q()),dir:ee(t),children:[a.jsx("div",{className:"rqmii-chat-consent-body",children:a.jsx(wn,{content:l})}),a.jsxs("div",{className:"rqmii-chat-consent-actions",children:[a.jsx("button",{type:"button",className:"rqmii-chat-consent-button rqmii-chat-consent-button--decline",onClick:o,children:c}),a.jsx("button",{type:"button",className:"rqmii-chat-consent-button rqmii-chat-consent-button--accept",onClick:s,children:u})]})]})}function Mn(t){return t?typeof t=="string"?t:typeof t=="object"&&typeof t.text=="string"?t.text:"":""}function Cn(t){return t==="text"||t==="voice"||t==="text_and_voice"}const Ne=r.memo(function({agent:e,language:n,languages:i,onLanguageChange:s=ae,onEndSession:o=ae,onMinimize:l,messages:u,onSendMessage:c,isPreview:d=!1,chatKind:h,sdk:m,renderPanel:g,messagesRef:y,initialText:b,autoStartVoice:j}){const F=e.widget?.consent||e.consent,x=!!F?.enabled,v=F?.content?.[n]||F?.body?.[n]||"",f=F?.one_time!==!1,p=e.id?`rqmii_consent_${e.id}`:null,[S,T]=r.useState(()=>{if(!x||!v)return!0;if(f&&p&&typeof window<"u")try{return window.localStorage.getItem(p)==="true"}catch{return!1}return!1}),k=h||he(e),[w,M]=r.useState(()=>k==="voice"&&m.isSessionReady||k==="voice"&&!!j),[A,z]=r.useState(()=>(k==="text"||k==="text_and_voice")&&m.isSessionReady||!!b||k==="text_and_voice"&&!!j),[N,V]=r.useState(!1),[L,C]=r.useState(!1),[D,I]=r.useState(null),G=Xt(k==="voice"&&w||k==="text_and_voice"&&A),R=en(),H=x&&!!v&&!S&&!d,W=Cn(k),{events:Y}=P.useRqmiiConnection();r.useEffect(()=>{if(!Y)return;const K=()=>{W&&ze()&&V(!0)},de=()=>{W&&V(!1)};return Y.on("minimize_chat",K),Y.on("maximize_chat",de),()=>{Y.off("minimize_chat",K),Y.off("maximize_chat",de)}},[Y,W]);const Z=r.useCallback(()=>{if(f&&p&&typeof window<"u")try{window.localStorage.setItem(p,"true")}catch{}T(!0)},[f,p]),B=r.useCallback(()=>{l?l():o()},[l,o]),oe=K=>{s(K),d||m.changeSessionLanguage(K)},J=r.useCallback(K=>{if(I(null),C(!0),c){c(K);return}m.sendTextMessage(K)},[c,m]);r.useEffect(()=>{y&&(y.current=m.messages)});const ie=r.useRef(!1);r.useEffect(()=>{ie.current||(b?(ie.current=!0,J(b)):j&&(ie.current=!0,I(null),C(!0),m.isVoiceActive||m.startVoice()))},[]),r.useEffect(()=>{L&&m.isSessionReady&&(C(!1),k==="voice"?M(!0):z(!0))},[L,m.isSessionReady,k]),r.useEffect(()=>{L&&m.error&&(C(!1),I(m.error))},[L,m.error]);const se=r.useCallback(()=>{I(null),C(!0),m.isVoiceActive||m.startVoice()},[m]);if(m.error&&!d&&(A||w))return a.jsx(Je,{message:m.error,language:n,onClose:o});if(H)return a.jsx(sa,{language:n,content:v,config:e.consent,agent:e,onAccept:Z,onDecline:B});const re={agent:e,language:n,languages:i,chatKind:k,sdk:m,isPreview:d,onLanguageChange:oe,onClose:o,onMinimize:W?()=>V(!0):void 0,onRestore:W?()=>V(!1):void 0,onSend:J,voiceStarted:w,conversationStarted:A,isMinimized:N,elapsed:G,setVoiceStarted:M,setConversationStarted:z};return g?a.jsx(a.Fragment,{children:g(re)}):a.jsx(Pn,{...re,connecting:L,connectError:D,visualViewportHeight:R,onCloseWidget:l??o,initiateVoice:se,onSendSuggestion:K=>{I(null),C(!0),m.sendSuggestion?.(K)}})}),Pn=r.memo(function({agent:e,language:n,languages:i,chatKind:s,sdk:o,onLanguageChange:l,onClose:u,onMinimize:c,onRestore:d,onSend:h,voiceStarted:m,conversationStarted:g,isMinimized:y,elapsed:b,setVoiceStarted:j,setConversationStarted:F,visualViewportHeight:x,connecting:v=!1,connectError:f=null,onCloseWidget:p,initiateVoice:S,onSendSuggestion:T}){const k=e.widget?.behavior?.typing_indicator_enabled??!0,w=s==="text"?o.waitingForResponse&&k:!1,M=o.messages;if(s==="avatar")return a.jsx(ia,{agent:e,language:n,onClose:u});if(y)return a.jsx(zn,{agent:e,language:n,onRestore:d,onClose:u,isListening:o.isVoiceActive,isAudioPlaying:o.isAudioPlaying,isSpeaking:o.vad.isSpeaking,onToggleVoice:o.toggleVoice,elapsed:b,variant:s==="text"?"text":"voice"});if(s==="voice"&&m)return a.jsx(Fn,{agent:e,language:n,onEndSession:u,onMinimize:c,isListening:o.isVoiceActive,isLoading:o.vad.isLoading,isSpeaking:o.vad.isSpeaking,isAudioPlaying:o.isAudioPlaying,isAgentThinking:o.isAgentThinking,isAgentSpeaking:o.isAgentSpeaking,onToggleVoice:o.toggleVoice,elapsed:b,visualViewportHeight:x});if(s==="voice")return a.jsx(En,{agent:e,language:n,languages:i,onLanguageChange:l,onClose:p,isAudioPlaying:o.isAudioPlaying,isVoiceSetupLoading:o.isVoiceSetupLoading||v,onStartVoice:S,visualViewportHeight:x});if(!g&&(o.messages.length===0||v)){const A=(e.widget?.behavior?.suggested_messages?.[n]||e.conversation?.suggested_messages?.[n])?.filter(V=>V.trim())||void 0,z=V=>{o.sendSuggestion?T(V):h?.(V)},N=s==="text_and_voice"||he(e)==="text_and_voice";return a.jsx(xn,{agent:e,language:n,languages:i,onLanguageChange:l,onClose:p,onSend:h,isVoiceEnabled:N,isAudioPlaying:o.isAudioPlaying,isVoiceSetupLoading:o.isVoiceSetupLoading||v,onStartVoice:S,visualViewportHeight:x,disabled:v,error:f||void 0,suggestions:A,onSuggestionClick:z,compact:!!A})}return a.jsx(Tn,{agent:e,language:n,onEndSession:u,messages:M,partialTranscript:Mn(o.partialTranscript),onSend:h,isAudioPlaying:o.isAudioPlaying,isVoiceEnabled:s==="text_and_voice",isListening:o.isVoiceActive,isSpeaking:o.vad.isSpeaking,isLoading:o.vad.isLoading,isAgentThinking:o.isAgentThinking,isAgentSpeaking:o.isAgentSpeaking,onToggleVoice:o.toggleVoice,onStartVoice:o.startVoice,onMinimize:c,elapsed:b,visualViewportHeight:x,waitingForResponse:w})}),An={isListening:!1,isLoading:!1,isSpeaking:!1,toggle:async()=>!1};function oa({isSessionReady:t,sendTextMessage:e,messages:n}){const[i,s]=r.useState(null),[o,l]=r.useState(null),u=r.useCallback(()=>{s(c=>(c&&(t?e(c):l(c)),null))},[t,e]);return r.useEffect(()=>{i&&n.length!==0&&u()},[n,i,u]),r.useEffect(()=>{if(!i)return;const c=setTimeout(u,5e3);return()=>clearTimeout(c)},[i,u]),r.useEffect(()=>{!o||!t||(e(o),l(null))},[t,o,e]),{pendingWelcomeText:i,pendingMessage:o,setPendingWelcomeText:s,setPendingMessage:l,sendPendingWelcomeText:u}}function Ve({isSessionReady:t,signalClientReady:e}){const[n,i]=r.useState(!1),[s,o]=r.useState(void 0),l=r.useRef(!1);return r.useEffect(()=>{!t||l.current||!n||(l.current=!0,e(s),i(!1))},[t,n,s,e]),{emitClientReady:r.useCallback(c=>{t?(l.current=!0,e(c)):(o(c),i(!0))},[t,e])}}const qn=300;function ra(t){const{isPreview:e=!1,...n}=t,{messages:i,sendTextMessage:s,emitUserMessage:o,addLocalMessage:l,clearMessages:u}=P.useRqmiiMessages(),{changeSessionLanguage:c,clearSessionHistory:d,signalClientReady:h,error:m,isSessionReady:g,isAgentThinking:y,isAgentSpeaking:b}=P.useRqmiiConnection(),j=r.useRef(!1),F=r.useRef(null),x=r.useRef(!1),[v,f]=r.useState(!1),[p,S]=r.useState(null),[T,k]=r.useState(!1),w=r.useRef(i.length);r.useEffect(()=>{if(i.length>w.current){const C=i[i.length-1];C.role==="assistant"&&C.streaming&&k(!1)}w.current=i.length},[i]),P.useRqmiiEvents("message_rate_limited",r.useCallback(()=>{k(!1)},[])),r.useEffect(()=>{if(!g||!v||x.current)return;const C=F.current;if(C===null)return;x.current=!0,F.current=null,l("user",C),S(null);const D=t.agent.welcomeMessages?.[t.language];h(D?{welcomeMessage:D}:void 0);const I=setTimeout(()=>{o(C),k(!0)},qn);return()=>clearTimeout(I)},[g,v,h,o,l]);const M=r.useMemo(()=>p?[{id:"first-user",role:"user",text:p},...i]:i,[p,i]),A=r.useCallback(()=>{S(null),u()},[u]),z={emitClientReady:h,sendMessage:C=>{s(C)},queueForWelcome:ae,enableVoice:ae,disableVoice:ae,language:t.language,isSessionReady:g,supportsVoice:!1,isVoiceActive:!1,chatKind:"text"},N=(C,D)=>{t.onSessionStart?.(),j.current?(k(!0),D.sendMessage(C)):(j.current=!0,S(C),F.current=C,f(!0),k(!0))},V=C=>{t.onTextSend?t.onTextSend(C,z):N(C,z)},L={messages:M,sendTextMessage:V,clearMessages:A,clearSessionHistory:d,isAudioPlaying:!1,partialTranscript:null,changeSessionLanguage:c,error:m,isAgentThinking:y,isAgentSpeaking:b,isVoiceSetupLoading:!1,isVoiceActive:!1,isSessionReady:g,vad:An,startVoice:ae,prepareVoice:ae,toggleVoice:ae,signalClientReady:h,waitingForResponse:T,sendSuggestion:V};return a.jsx(Ne,{...n,isPreview:e,chatKind:"text",sdk:L})}function la(t){const{isPreview:e=!1,...n}=t,{messages:i,sendTextMessage:s,clearMessages:o}=P.useRqmiiMessages(),{isAudioPlaying:l,partialTranscript:u}=P.useRqmiiVoice(),{changeSessionLanguage:c,clearSessionHistory:d,signalClientReady:h,error:m,isSessionReady:g,isAgentThinking:y,isAgentSpeaking:b}=P.useRqmiiConnection(),{setPendingWelcomeText:j,setPendingMessage:F}=oa({isSessionReady:g,sendTextMessage:s,messages:i}),{emitClientReady:x}=Ve({isSessionReady:g,signalClientReady:h}),[v,f]=r.useState(!1),p=P.useRqmiiVAD({enabled:!0,preloadStream:!1}),{isLoading:S,isListening:T,toggle:k}=p,w=S,M=T||v;r.useEffect(()=>{!v||!g||S||T||k()},[v,g,S,T,k]);const A=()=>{t.onSessionStart?.()},z={emitClientReady:x,sendMessage:I=>{g?s(I):F(I)},queueForWelcome:I=>j(I),enableVoice:()=>f(!0),disableVoice:()=>{f(!1),T&&k()},language:t.language,isSessionReady:g,supportsVoice:!0,isVoiceActive:M,chatKind:"voice"},N=I=>{A(),I.enableVoice();const U=t.agent.welcomeMessages?.[t.language];I.emitClientReady(U?{welcomeMessage:U}:void 0)},V=I=>{I.isVoiceActive?I.disableVoice():I.enableVoice()},D={messages:i,sendTextMessage:()=>{},clearMessages:o,clearSessionHistory:d,isAudioPlaying:l,partialTranscript:u,changeSessionLanguage:c,error:m,isAgentThinking:y,isAgentSpeaking:b,isVoiceSetupLoading:w,isVoiceActive:M,isSessionReady:g,vad:p,startVoice:()=>{t.onVoiceStart?t.onVoiceStart(z):N(z)},prepareVoice:()=>{},toggleVoice:()=>{t.onVoiceToggle?t.onVoiceToggle(z):V(z)},signalClientReady:h};return a.jsx(Ne,{...n,isPreview:e,chatKind:"voice",sdk:D})}const _n=300;function ca(t){const{isPreview:e=!1,...n}=t,{messages:i,sendTextMessage:s,emitUserMessage:o,addLocalMessage:l,clearMessages:u}=P.useRqmiiMessages(),{isAudioPlaying:c,partialTranscript:d}=P.useRqmiiVoice(),{changeSessionLanguage:h,clearSessionHistory:m,signalClientReady:g,error:y,isSessionReady:b,isAgentThinking:j,isAgentSpeaking:F}=P.useRqmiiConnection(),{emitClientReady:x}=Ve({isSessionReady:b,signalClientReady:g}),[v,f]=r.useState(!1),p=r.useRef(null),[S,T]=r.useState(!1),k=r.useRef(null),[w,M]=r.useState(!1),A=r.useRef(!1),z=r.useRef(!1),N=r.useRef(!1),[V,L]=r.useState(null),[C,D]=r.useState(!1),I=r.useRef(i.length);r.useEffect(()=>{if(i.length>I.current){const q=i[i.length-1];q.role==="assistant"&&q.streaming&&D(!1)}I.current=i.length},[i]),P.useRqmiiEvents("message_rate_limited",r.useCallback(()=>{D(!1)},[]));const U=P.useRqmiiVAD({enabled:!0,preloadStream:!1}),{isLoading:G,isListening:R,toggle:H}=U,W=G,Y=R||v;r.useEffect(()=>{!v||!b||G||R||H()},[v,b,G,R,H]),r.useEffect(()=>{if(!b||!S||N.current)return;const q=p.current;if(q===null)return;N.current=!0,p.current=null,l("user",q),L(null);const X=t.agent.welcomeMessages?.[t.language];g(X?{welcomeMessage:X}:void 0);const xe=setTimeout(()=>{o(q),D(!0)},_n);return()=>clearTimeout(xe)},[b,S,g,o,l]),r.useEffect(()=>{if(!b||!w||A.current)return;const q=k.current;q!==null&&(A.current=!0,k.current=null,l("user",q),o(q),D(!0),M(!1))},[b,w,o,l]);const Z=r.useMemo(()=>V?[{id:"first-user",role:"user",text:V},...i]:i,[V,i]),B=r.useCallback(()=>{L(null),z.current=!1,N.current=!1,u()},[u]),oe=()=>{t.onSessionStart?.()},J={emitClientReady:q=>{N.current=!0,z.current=!0,x(q)},sendMessage:q=>{s(q)},queueForWelcome:()=>{},enableVoice:()=>f(!0),disableVoice:()=>{f(!1),R&&H()},language:t.language,isSessionReady:b,supportsVoice:!0,isVoiceActive:Y,chatKind:"text_and_voice"},ie=(q,X)=>{oe(),!z.current&&!N.current?(z.current=!0,t.onStartText?.(),L(q),p.current=q,T(!0),D(!0)):(z.current=!0,D(!0),X.sendMessage(q))},se=q=>{oe(),z.current=!0,N.current=!0,t.onStartVoice?.(),q.enableVoice();const X=t.agent.welcomeMessages?.[t.language];q.emitClientReady(X?{welcomeMessage:X}:void 0)},re=q=>{q.isVoiceActive?q.disableVoice():q.enableVoice()},ye={messages:Z,sendTextMessage:q=>{t.onTextSend?t.onTextSend(q,J):ie(q,J)},clearMessages:B,clearSessionHistory:m,isAudioPlaying:c,partialTranscript:d,changeSessionLanguage:h,error:y,isAgentThinking:j,isAgentSpeaking:F,isVoiceSetupLoading:W,isVoiceActive:Y,isSessionReady:b,vad:U,startVoice:()=>{t.onVoiceStart?t.onVoiceStart(J):se(J)},prepareVoice:()=>{},toggleVoice:()=>{t.onVoiceToggle?t.onVoiceToggle(J):re(J)},signalClientReady:g,waitingForResponse:C,sendSuggestion:q=>{oe(),z.current=!0,N.current=!0,t.onStartVoice?.(),f(!0),k.current=q,M(!0)}};return a.jsx(Ne,{...n,isPreview:e,chatKind:t.textOnly?"text":"text_and_voice",sdk:ye})}function da(t){const{isPreview:e=!1,agent:n,language:i,languages:s,onLanguageChange:o,onEndSession:l,onMinimize:u,messages:c,onSendMessage:d}=t,h=l||u||ae,{messages:m,sendTextMessage:g,clearMessages:y}=P.useRqmiiMessages(),{changeSessionLanguage:b,clearSessionHistory:j,signalClientReady:F,error:x,isSessionReady:v,isAgentThinking:f,isAgentSpeaking:p}=P.useRqmiiConnection();return oa({isSessionReady:v,sendTextMessage:g,messages:m}),Ve({isSessionReady:v,signalClientReady:F}),x&&!e?a.jsx(Je,{message:x,language:i,onClose:h}):a.jsx(ia,{agent:n,language:i,onClose:h})}function Ln({agent:t,defaultOpen:e,className:n="",preview:i=!1,ChatBoxComponent:s}){const o=e!==void 0?e:t.widget?.behavior?.default_open??!1,[l,u]=r.useState(o),[c,d]=r.useState(t.defaultLanguage),h=r.useMemo(()=>ce(t.languages,t.defaultLanguage),[t.defaultLanguage,t.languages]);r.useEffect(()=>{d(t.defaultLanguage)},[t.defaultLanguage]),r.useEffect(()=>{u(o)},[o]);const m=t.widget?.appearance?.theme?.mode==="light"?"light":"dark",g=r.useMemo(()=>Ae(t.widget?.appearance),[t.widget?.appearance]);return a.jsxs("div",{className:_("rqmii-chat-surface",`rqmii-theme-${m}`,i&&"is-preview",n),"data-theme":m,style:g,children:[!l&&a.jsx(Le,{agent:t,onClick:()=>u(!0),position:i?"static":"fixed"}),l&&a.jsx(s,{agent:t,language:c,languages:h,onLanguageChange:d,onClose:()=>u(!1),isPreview:i})]})}function In({restUrl:t,identifier:e,fallbackAgent:n,withCredentials:i=!1,children:s,onError:o}){const[l,u]=r.useState(()=>n?le(n):null),[c,d]=r.useState(null);return r.useEffect(()=>{n&&(u(le(n)),d(null))},[n]),r.useEffect(()=>{if(!e||n)return;const h=new AbortController;return d(null),He({restUrl:t,identifier:e,withCredentials:i,signal:h.signal}).then(u).catch(m=>{h.signal.aborted||(n||d(m),o?.(m))}),()=>h.abort()},[n,e,o,t,i]),c?a.jsx("div",{className:"rqmii-chat-panel rqmii-chat-panel--error",children:c.message||"Could not load agent"}):l?a.jsx(a.Fragment,{children:s(l)}):a.jsx("div",{className:"rqmii-chat-panel rqmii-chat-panel--error",children:"Loading"})}function Se({agent:t,language:e,languages:n,onLanguageChange:i,onEndSession:s,onMinimize:o,onStartText:l,onStartVoice:u,textOnly:c,messages:d,onSendMessage:h,isPreview:m,onSessionStart:g,initialText:y,autoStartVoice:b}){const j=he(t),F={agent:t,language:e,languages:n,onLanguageChange:i,onEndSession:s,onMinimize:o,messages:d,onSendMessage:h,isPreview:m,onSessionStart:g,initialText:y,autoStartVoice:b};switch(j){case"avatar":return a.jsx(da,{...F});case"voice":return a.jsx(la,{...F});case"text_and_voice":return a.jsx(ca,{...F,onStartText:l,onStartVoice:u,textOnly:c});case"text":default:return a.jsx(ra,{...F})}}function Nn({agent:t,language:e,languages:n,startMode:i,directive:s,open:o,onOpen:l,onLanguageChange:u,onMinimize:c,onStartNewSession:d,onSessionStart:h,onStartText:m,onStartVoice:g}){const{connectionState:y,isConnected:b,isSessionTerminated:j,creditExhausted:F,session:x,endSession:v,submitFeedback:f,interrupt:p}=P.useRqmiiConnection(),{messages:S}=P.useRqmiiMessages(),T=r.useRef(!1),k=r.useRef(null),[w,M]=r.useState(null),[A,z]=r.useState(0),[N,V]=r.useState(!1),[L,C]=r.useState(!1),[D,I]=r.useState(!1);r.useEffect(()=>{b&&(T.current=!0)},[b]),r.useEffect(()=>{if(w)return;let B=null;j||y==="ended"?B=k.current==="user"?"user_ended":"terminated":y==="failed"?F?B="credits_exhausted":T.current?B="connection_lost":B="failed":F&&(B="credits_exhausted"),B&&M({sessionId:x.sessionId,endedBy:k.current??"agent",reason:B})},[j,y,F,x.sessionId,w]);const U=()=>{k.current="user",p(),v()},G=w?.sessionId??null,R=B=>{N||(z(B),G&&f({sessionId:G,rating:B,endedBy:w?.endedBy}).catch(()=>{}),D||(I(!0),C(!0)))},H=async B=>{if(G)try{await f({sessionId:G,feedback:B,endedBy:w?.endedBy})}catch{}V(!0),C(!1)};if(!o)return a.jsx(Le,{agent:t,onClick:l,active:y==="connected"});const W=he(t),Y=t.agentType==="cloud_avatar"||t.agentType==="desktop_avatar",Z=i==="text";return Y?a.jsx(Se,{agent:t,language:e,languages:n,onLanguageChange:u,onEndSession:U,onMinimize:c,onSessionStart:h,onStartText:m,onStartVoice:g,textOnly:Z}):w?L&&G?a.jsx(aa,{agent:t,language:e,rating:A,onBack:()=>C(!1),onMinimize:c,onSubmit:H}):a.jsx(na,{agent:t,language:e,languages:n,chatKind:W==="avatar"?"text":W,messages:S,onLanguageChange:u,onMinimize:c,onStartNewVoice:()=>d("voice"),onStartNewText:B=>d("text",B),rating:A,endedBy:w.endedBy,sessionId:G,ratingInteractive:!N,showTellUsMore:D&&!N,onRate:R,onTellUsMore:()=>C(!0)}):a.jsx(Se,{agent:t,language:e,languages:n,onLanguageChange:u,onEndSession:U,onMinimize:c,onSessionStart:h,onStartText:m,onStartVoice:g,textOnly:Z,initialText:s?.mode==="text"?s.text:void 0,autoStartVoice:s?.mode==="voice"})}function Vn(t){const{wsUrl:e,restUrl:n,apiKey:i,agentId:s,agent:o,language:l,defaultOpen:u,open:c,className:d="",debug:h=!1,withCredentials:m=!1,onError:g,onInitialized:y,config:b,promptOverrides:j,clientTools:F,onMinimize:x,onOpen:v}=t;if([!!i,!!s].filter(Boolean).length>1)throw new Error("RqmiiAgentChat: only one of apiKey, or agentId may be provided");const p=i||s;return a.jsx(In,{restUrl:n,identifier:p,fallbackAgent:o,withCredentials:m,onError:g,children:S=>a.jsx(Dn,{wsUrl:e,restUrl:n,apiKey:i,agentId:s,agent:S,language:l,defaultOpen:u,open:c,className:d,debug:h,withCredentials:m,onError:g,onInitialized:y,config:b,promptOverrides:j,clientTools:F,onMinimize:x,onOpen:v})})}function Dn({wsUrl:t,restUrl:e,apiKey:n,agentId:i,agent:s,language:o,defaultOpen:l,open:u,className:c,debug:d,withCredentials:h,onError:m,onInitialized:g,config:y,promptOverrides:b,clientTools:j,onMinimize:F,onOpen:x}){const v=l!==void 0?l:s.widget?.behavior?.default_open??!1,[f,p]=r.useState(v),S=u!==void 0?u:f;r.useEffect(()=>{u===void 0&&p(v)},[v,u]);const[T,k]=r.useState(!1),[w,M]=r.useState(o||s.defaultLanguage),[A,z]=r.useState("idle"),[N,V]=r.useState(0),[L,C]=r.useState(null),[D,I]=r.useState(!1),U=s.agentType==="cloud_avatar"||s.agentType==="desktop_avatar",G=U,R=s.widget?.behavior?.welcome_bubble,H=s.welcomeMessages?.[w]||"",W=!S&&!D&&!G&&!!R?.enabled&&!!H.trim(),Y=r.useMemo(()=>ce(s.languages,s.defaultLanguage),[s.defaultLanguage,s.languages]);r.useEffect(()=>{Ze(s.widget?.appearance?.font)},[s.widget?.appearance?.font]);const Z=r.useMemo(()=>A==="text"?{...y??{},use_voice:!1,use_text:!0}:y,[y,A]),B=r.useMemo(()=>Tt(s,Z),[s,Z]),oe=!G&&B.useVoice,J=U?S:T,ie=r.useMemo(()=>({...n?{apiKey:n}:{},...i?{agentId:i}:{},language:w,config:Z,...b?{promptOverrides:b}:{}}),[i,n,w,Z,b]),se=r.useRef(null),re=r.useRef(!1),K=r.useRef(N),de=K.current!==N;K.current=N,T&&(!re.current||de)&&(se.current=ie),re.current=T,T||(se.current=null);const De=T&&se.current?se.current:ie;r.useEffect(()=>{M(o||s.defaultLanguage)},[s.defaultLanguage,o]);const ye=r.useMemo(()=>m?ue=>{z("idle"),m(ue)}:ue=>{z("idle")},[m]),q=()=>{z("text"),k(!0)},X=()=>{z("voice"),k(!0)},xe=()=>{u===void 0&&p(!1),F?.()},ua=()=>{u===void 0&&p(!0),I(!0),x?.()},ma=(ue,ga)=>{C(ue==="text"?{mode:"text",text:ga??""}:{mode:"voice"}),z(ue),k(!0),V(pa=>pa+1)},Re=s.widget?.appearance?.theme?.mode==="light"?"light":"dark",ha=r.useMemo(()=>Ae(s.widget?.appearance),[s.widget?.appearance]);return a.jsx(P.RqmiiAgentRoom,{wsUrl:t,restUrl:e,session:De,connect:J,enableAudio:oe,debug:d,withCredentials:h,onError:ye,onInitialized:g,clientTools:j,children:a.jsxs("div",{className:_("rqmii-chat-surface",`rqmii-theme-${Re}`,s.widget?.appearance?.position==="left"&&"rqmii-position-left",c),"data-theme":Re,style:ha,dir:"ltr",children:[W&&a.jsx(sn,{message:H,delaySeconds:R?.delay_seconds??3,soundEnabled:!!R?.sound_enabled,language:w,buttonPosition:s.widget?.appearance?.position==="left"?"left":"right",onClick:()=>I(!0)}),a.jsx(Nn,{agent:s,language:w,languages:Y,startMode:A,directive:L,open:S,onOpen:ua,onLanguageChange:M,onMinimize:xe,onStartNewSession:ma,onSessionStart:()=>k(!0),onStartText:q,onStartVoice:X})]})},N)}function Rn(t){const{agent:e,language:n,open:i=!0,className:s="",messages:o,onSendMessage:l,onClose:u}=t,c=r.useMemo(()=>le(e),[e]),[d,h]=r.useState(i),[m,g]=r.useState(n||c.defaultLanguage),y=r.useMemo(()=>ce(c.languages,c.defaultLanguage),[c.defaultLanguage,c.languages]),b=c.widget?.appearance?.theme?.mode==="light"?"light":"dark",j=r.useMemo(()=>Ae(c.widget?.appearance),[c.widget?.appearance]);return r.useEffect(()=>{n&&g(n)},[n]),r.useEffect(()=>{h(i)},[i]),r.useEffect(()=>{Ze(c.widget?.appearance?.font)},[c.widget?.appearance?.font]),a.jsx(P.RqmiiAgentProvider,{wsUrl:"",restUrl:"",enableAudio:!1,children:a.jsx("div",{className:_("rqmii-chat-surface is-preview",`rqmii-theme-${b}`,c.widget?.appearance?.position==="left"&&"rqmii-position-left",s),"data-theme":b,style:j,dir:"ltr",children:d?a.jsx(Se,{agent:c,language:m,languages:y,onLanguageChange:g,onEndSession:()=>{h(!1),u?.()},onMinimize:()=>{h(!1),u?.()},messages:o,onSendMessage:l,isPreview:!0}):null})})}exports.EpicPixelStreamingPlayer=P.EpicPixelStreamingPlayer;exports.RqmiiAgentRoom=P.RqmiiAgentRoom;exports.RqmiiCloudAvatarPlayer=P.RqmiiCloudAvatarPlayer;exports.ConsentDialog=sa;exports.EndedChatPanel=na;exports.FeedbackPanel=aa;exports.RQMII_CHAT_LANGUAGES=pe;exports.RQMII_CHAT_LANGUAGE_VALUES=Te;exports.RatingBar=ea;exports.RqmiiAgentAvatar=$;exports.RqmiiAgentChat=Vn;exports.RqmiiAgentChatPreview=Rn;exports.RqmiiAvatarChatBox=da;exports.RqmiiChatFooter=ne;exports.RqmiiChatOpenButton=Le;exports.RqmiiChatSurface=Ln;exports.RqmiiTextAndVoiceChatBox=ca;exports.RqmiiTextChatBox=ra;exports.RqmiiVoiceChatBox=la;exports.fetchAgentConfig=Et;exports.fetchAgentCustomization=He;exports.getChatLanguage=Fe;exports.getChatText=E;exports.normalizeChatLanguages=ce;exports.normalizePublicAgent=le;exports.resolveChatKind=he;
113
113
  //# sourceMappingURL=ui.cjs.map