@0xbot/chat-react 1.0.8 → 1.0.9

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.umd.js DELETED
@@ -1,52 +0,0 @@
1
- (function(I,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("react"),require("markdown-it"),require("highlight.js"),require("localforage"),require("axios"),require("@fingerprintjs/fingerprintjs"),require("@unocss/reset/tailwind.css")):typeof define=="function"&&define.amd?define(["exports","react","markdown-it","highlight.js","localforage","axios","@fingerprintjs/fingerprintjs","@unocss/reset/tailwind.css"],h):(I=typeof globalThis<"u"?globalThis:I||self,h(I.ChatReact={},I.React,I.MarkdownIt,I.hljs,I.localforage,I.axios,I.FingerprintJS))})(this,function(I,h,Xe,ie,ce,Ze,ke){"use strict";var he={exports:{}},ee={};/**
2
- * @license React
3
- * react-jsx-runtime.production.min.js
4
- *
5
- * Copyright (c) Facebook, Inc. and its affiliates.
6
- *
7
- * This source code is licensed under the MIT license found in the
8
- * LICENSE file in the root directory of this source tree.
9
- */var Oe;function qe(){if(Oe)return ee;Oe=1;var n=h,o=Symbol.for("react.element"),_=Symbol.for("react.fragment"),d=Object.prototype.hasOwnProperty,R=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,w={key:!0,ref:!0,__self:!0,__source:!0};function O(T,m,f){var l,g={},j=null,L=null;f!==void 0&&(j=""+f),m.key!==void 0&&(j=""+m.key),m.ref!==void 0&&(L=m.ref);for(l in m)d.call(m,l)&&!w.hasOwnProperty(l)&&(g[l]=m[l]);if(T&&T.defaultProps)for(l in m=T.defaultProps,m)g[l]===void 0&&(g[l]=m[l]);return{$$typeof:o,type:T,key:j,ref:L,props:g,_owner:R.current}}return ee.Fragment=_,ee.jsx=O,ee.jsxs=O,ee}var re={};/**
10
- * @license React
11
- * react-jsx-runtime.development.js
12
- *
13
- * Copyright (c) Facebook, Inc. and its affiliates.
14
- *
15
- * This source code is licensed under the MIT license found in the
16
- * LICENSE file in the root directory of this source tree.
17
- */var Pe;function Qe(){return Pe||(Pe=1,process.env.NODE_ENV!=="production"&&function(){var n=h,o=Symbol.for("react.element"),_=Symbol.for("react.portal"),d=Symbol.for("react.fragment"),R=Symbol.for("react.strict_mode"),w=Symbol.for("react.profiler"),O=Symbol.for("react.provider"),T=Symbol.for("react.context"),m=Symbol.for("react.forward_ref"),f=Symbol.for("react.suspense"),l=Symbol.for("react.suspense_list"),g=Symbol.for("react.memo"),j=Symbol.for("react.lazy"),L=Symbol.for("react.offscreen"),M=Symbol.iterator,Y="@@iterator";function H(e){if(e===null||typeof e!="object")return null;var r=M&&e[M]||e[Y];return typeof r=="function"?r:null}var k=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function y(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),a=1;a<r;a++)t[a-1]=arguments[a];K("error",e,t)}}function K(e,r,t){{var a=k.ReactDebugCurrentFrame,u=a.getStackAddendum();u!==""&&(r+="%s",t=t.concat([u]));var p=t.map(function(c){return String(c)});p.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,p)}}var B=!1,W=!1,te=!1,G=!1,z=!1,V;V=Symbol.for("react.module.reference");function N(e){return!!(typeof e=="string"||typeof e=="function"||e===d||e===w||z||e===R||e===f||e===l||G||e===L||B||W||te||typeof e=="object"&&e!==null&&(e.$$typeof===j||e.$$typeof===g||e.$$typeof===O||e.$$typeof===T||e.$$typeof===m||e.$$typeof===V||e.getModuleId!==void 0))}function _e(e,r,t){var a=e.displayName;if(a)return a;var u=r.displayName||r.name||"";return u!==""?t+"("+u+")":t}function ne(e){return e.displayName||"Context"}function D(e){if(e==null)return null;if(typeof e.tag=="number"&&y("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case d:return"Fragment";case _:return"Portal";case w:return"Profiler";case R:return"StrictMode";case f:return"Suspense";case l:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case T:var r=e;return ne(r)+".Consumer";case O:var t=e;return ne(t._context)+".Provider";case m:return _e(e,e.render,"ForwardRef");case g:var a=e.displayName||null;return a!==null?a:D(e.type)||"Memo";case j:{var u=e,p=u._payload,c=u._init;try{return D(c(p))}catch{return null}}}return null}var U=Object.assign,X=0,ae,fe,s,v,b,S,J;function q(){}q.__reactDisabledLog=!0;function be(){{if(X===0){ae=console.log,fe=console.info,s=console.warn,v=console.error,b=console.group,S=console.groupCollapsed,J=console.groupEnd;var e={configurable:!0,enumerable:!0,value:q,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}X++}}function oe(){{if(X--,X===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:U({},e,{value:ae}),info:U({},e,{value:fe}),warn:U({},e,{value:s}),error:U({},e,{value:v}),group:U({},e,{value:b}),groupCollapsed:U({},e,{value:S}),groupEnd:U({},e,{value:J})})}X<0&&y("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var Ee=k.ReactCurrentDispatcher,Re;function de(e,r,t){{if(Re===void 0)try{throw Error()}catch(u){var a=u.stack.trim().match(/\n( *(at )?)/);Re=a&&a[1]||""}return`
18
- `+Re+e}}var we=!1,ve;{var ur=typeof WeakMap=="function"?WeakMap:Map;ve=new ur}function Me(e,r){if(!e||we)return"";{var t=ve.get(e);if(t!==void 0)return t}var a;we=!0;var u=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var p;p=Ee.current,Ee.current=null,be();try{if(r){var c=function(){throw Error()};if(Object.defineProperty(c.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(c,[])}catch(A){a=A}Reflect.construct(e,[],c)}else{try{c.call()}catch(A){a=A}e.call(c.prototype)}}else{try{throw Error()}catch(A){a=A}e()}}catch(A){if(A&&a&&typeof A.stack=="string"){for(var i=A.stack.split(`
19
- `),P=a.stack.split(`
20
- `),E=i.length-1,x=P.length-1;E>=1&&x>=0&&i[E]!==P[x];)x--;for(;E>=1&&x>=0;E--,x--)if(i[E]!==P[x]){if(E!==1||x!==1)do if(E--,x--,x<0||i[E]!==P[x]){var F=`
21
- `+i[E].replace(" at new "," at ");return e.displayName&&F.includes("<anonymous>")&&(F=F.replace("<anonymous>",e.displayName)),typeof e=="function"&&ve.set(e,F),F}while(E>=1&&x>=0);break}}}finally{we=!1,Ee.current=p,oe(),Error.prepareStackTrace=u}var $=e?e.displayName||e.name:"",Z=$?de($):"";return typeof e=="function"&&ve.set(e,Z),Z}function fr(e,r,t){return Me(e,!1)}function dr(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function pe(e,r,t){if(e==null)return"";if(typeof e=="function")return Me(e,dr(e));if(typeof e=="string")return de(e);switch(e){case f:return de("Suspense");case l:return de("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case m:return fr(e.render);case g:return pe(e.type,r,t);case j:{var a=e,u=a._payload,p=a._init;try{return pe(p(u),r,t)}catch{}}}return""}var se=Object.prototype.hasOwnProperty,De={},Fe=k.ReactDebugCurrentFrame;function ge(e){if(e){var r=e._owner,t=pe(e.type,e._source,r?r.type:null);Fe.setExtraStackFrame(t)}else Fe.setExtraStackFrame(null)}function vr(e,r,t,a,u){{var p=Function.call.bind(se);for(var c in e)if(p(e,c)){var i=void 0;try{if(typeof e[c]!="function"){var P=Error((a||"React class")+": "+t+" type `"+c+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[c]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw P.name="Invariant Violation",P}i=e[c](r,c,a,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(E){i=E}i&&!(i instanceof Error)&&(ge(u),y("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",a||"React class",t,c,typeof i),ge(null)),i instanceof Error&&!(i.message in De)&&(De[i.message]=!0,ge(u),y("Failed %s type: %s",t,i.message),ge(null))}}}var pr=Array.isArray;function Se(e){return pr(e)}function gr(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function hr(e){try{return Le(e),!1}catch{return!0}}function Le(e){return""+e}function Ne(e){if(hr(e))return y("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",gr(e)),Le(e)}var We=k.ReactCurrentOwner,mr={key:!0,ref:!0,__self:!0,__source:!0},Ve,Ye;function yr(e){if(se.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function _r(e){if(se.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function br(e,r){typeof e.ref=="string"&&We.current}function Er(e,r){{var t=function(){Ve||(Ve=!0,y("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}}function Rr(e,r){{var t=function(){Ye||(Ye=!0,y("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"ref",{get:t,configurable:!0})}}var wr=function(e,r,t,a,u,p,c){var i={$$typeof:o,type:e,key:r,ref:t,props:c,_owner:p};return i._store={},Object.defineProperty(i._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(i,"_self",{configurable:!1,enumerable:!1,writable:!1,value:a}),Object.defineProperty(i,"_source",{configurable:!1,enumerable:!1,writable:!1,value:u}),Object.freeze&&(Object.freeze(i.props),Object.freeze(i)),i};function Sr(e,r,t,a,u){{var p,c={},i=null,P=null;t!==void 0&&(Ne(t),i=""+t),_r(r)&&(Ne(r.key),i=""+r.key),yr(r)&&(P=r.ref,br(r,u));for(p in r)se.call(r,p)&&!mr.hasOwnProperty(p)&&(c[p]=r[p]);if(e&&e.defaultProps){var E=e.defaultProps;for(p in E)c[p]===void 0&&(c[p]=E[p])}if(i||P){var x=typeof e=="function"?e.displayName||e.name||"Unknown":e;i&&Er(c,x),P&&Rr(c,x)}return wr(e,i,P,u,a,We.current,c)}}var xe=k.ReactCurrentOwner,Ue=k.ReactDebugCurrentFrame;function Q(e){if(e){var r=e._owner,t=pe(e.type,e._source,r?r.type:null);Ue.setExtraStackFrame(t)}else Ue.setExtraStackFrame(null)}var Ce;Ce=!1;function Te(e){return typeof e=="object"&&e!==null&&e.$$typeof===o}function Be(){{if(xe.current){var e=D(xe.current.type);if(e)return`
22
-
23
- Check the render method of \``+e+"`."}return""}}function xr(e){return""}var Je={};function Cr(e){{var r=Be();if(!r){var t=typeof e=="string"?e:e.displayName||e.name;t&&(r=`
24
-
25
- Check the top-level render call using <`+t+">.")}return r}}function He(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=Cr(r);if(Je[t])return;Je[t]=!0;var a="";e&&e._owner&&e._owner!==xe.current&&(a=" It was passed a child from "+D(e._owner.type)+"."),Q(e),y('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',t,a),Q(null)}}function Ke(e,r){{if(typeof e!="object")return;if(Se(e))for(var t=0;t<e.length;t++){var a=e[t];Te(a)&&He(a,r)}else if(Te(e))e._store&&(e._store.validated=!0);else if(e){var u=H(e);if(typeof u=="function"&&u!==e.entries)for(var p=u.call(e),c;!(c=p.next()).done;)Te(c.value)&&He(c.value,r)}}}function Tr(e){{var r=e.type;if(r==null||typeof r=="string")return;var t;if(typeof r=="function")t=r.propTypes;else if(typeof r=="object"&&(r.$$typeof===m||r.$$typeof===g))t=r.propTypes;else return;if(t){var a=D(r);vr(t,e.props,"prop",a,e)}else if(r.PropTypes!==void 0&&!Ce){Ce=!0;var u=D(r);y("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",u||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&y("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function jr(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var a=r[t];if(a!=="children"&&a!=="key"){Q(e),y("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",a),Q(null);break}}e.ref!==null&&(Q(e),y("Invalid attribute `ref` supplied to `React.Fragment`."),Q(null))}}var Ge={};function ze(e,r,t,a,u,p){{var c=N(e);if(!c){var i="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(i+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var P=xr();P?i+=P:i+=Be();var E;e===null?E="null":Se(e)?E="array":e!==void 0&&e.$$typeof===o?(E="<"+(D(e.type)||"Unknown")+" />",i=" Did you accidentally export a JSX literal instead of a component?"):E=typeof e,y("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",E,i)}var x=Sr(e,r,t,u,p);if(x==null)return x;if(c){var F=r.children;if(F!==void 0)if(a)if(Se(F)){for(var $=0;$<F.length;$++)Ke(F[$],e);Object.freeze&&Object.freeze(F)}else y("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else Ke(F,e)}if(se.call(r,"key")){var Z=D(e),A=Object.keys(r).filter(function(Mr){return Mr!=="key"}),je=A.length>0?"{key: someKey, "+A.join(": ..., ")+": ...}":"{key: someKey}";if(!Ge[Z+je]){var Ar=A.length>0?"{"+A.join(": ..., ")+": ...}":"{}";y(`A props object containing a "key" prop is being spread into JSX:
26
- let props = %s;
27
- <%s {...props} />
28
- React keys must be passed directly to JSX without using spread:
29
- let props = %s;
30
- <%s key={someKey} {...props} />`,je,Z,Ar,Z),Ge[Z+je]=!0}}return e===d?jr(x):Tr(x),x}}function kr(e,r,t){return ze(e,r,t,!0)}function Or(e,r,t){return ze(e,r,t,!1)}var Pr=Or,Ir=kr;re.Fragment=d,re.jsx=Pr,re.jsxs=Ir}()),re}process.env.NODE_ENV==="production"?he.exports=qe():he.exports=Qe();var C=he.exports;const me=({self:n,UserAvatar:o,AgentAvatar:_,loading:d,loadingText:R,emptyText:w,content:O,children:T})=>C.jsxs("div",{className:`message-container message-spacing-small message-spacing-medium ${n?"message-align-right":"message-align-left"}`,children:[!n&&C.jsx("img",{src:_,className:"chat-avatar",alt:"Agent Avatar"}),C.jsxs("div",{className:`message-bubble ${n?"chat-message":""}`,children:[!O&&!d&&C.jsx("div",{className:"message-empty",children:w}),T,d&&C.jsx("div",{className:"message-loading",children:R})]}),n&&C.jsx("img",{src:o,className:"chat-avatar",alt:"User Avatar"})]}),$e=n=>{n.renderer.rules.fence=(o,_)=>{const d=o[_],R=d.info?d.info.trim():"";let w="";if(R&&ie.getLanguage(R))try{w=ie.highlight(d.content,{language:R}).value}catch{w=n.utils.escapeHtml(d.content)}else w=n.utils.escapeHtml(d.content);return`
31
- <div class="code-block-element">
32
- <div class="code-area">
33
- <div class="header">
34
- <div class="text">
35
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
36
- <path fill="#fff" fill-opacity="0.55" fill-rule="evenodd" d="M7 9v6H5.2V9H7Zm0-1v8H5c-.552 0-1-.299-1-.667V8.667C4 8.298 4.448 8 5 8h2Z" clip-rule="evenodd"/>
37
- <path stroke="#fff" stroke-linejoin="bevel" stroke-opacity="0.55" d="m8.5 10 3 2-3 2M15.5 15h-4"/>
38
- <path fill="#fff" fill-opacity="0.55" fill-rule="evenodd" d="M17 9v6h1.8V9H17Zm0-1v8h2c.552 0 1-.299 1-.667V8.667C20 8.298 19.552 8 19 8h-2Z" clip-rule="evenodd"/>
39
- </svg>
40
- <span class="code-area-language">${R}</span>
41
- </div>
42
- <div class="copy">
43
- <button class="copy-btn" onclick="copyCodeToClipboard(this)">Copy</button>
44
- </div>
45
- </div>
46
- <div class="content">
47
- <pre><code class="hljs language-${n.utils.escapeHtml(R)}">${w}</code></pre>
48
- </div>
49
- </div>
50
- </div>
51
- `}},er=new Xe({highlight:(n,o)=>{if(o&&ie.getLanguage(o))try{return ie.highlight(n,{language:o}).value}catch{}return""}}).use($e),rr=(n,o=!1)=>er.render(n,o);function le(n={}){const{prefix:o="0xbot-chat"}=n,_=f=>o?`${o}_${f}`:f,d=ce.createInstance({name:o,storeName:o,driver:[ce.INDEXEDDB,ce.LOCALSTORAGE,ce.WEBSQL]}),R=async f=>{try{const l=_(f),g=await d.getItem(l);if(g===null)return null;try{return JSON.parse(g)}catch{return g}}catch(l){return console.error("Error getting value from storage:",l),null}},w=async(f,l)=>{try{const g=_(f),j=typeof l=="string"?l:JSON.stringify(l);await d.setItem(g,j)}catch(g){console.error("Error setting value in storage:",g)}};return{getStorage:R,setStorage:w,removeStorage:async f=>{try{const l=_(f);await d.removeItem(l)}catch(l){console.error("Error removing value from storage:",l)}},clearStorage:async()=>{try{await d.clear()}catch(f){console.error("Error clearing storage:",f)}},useStorageState:(f,l)=>{const[g,j]=h.useState(l);return h.useEffect(()=>{R(f).then(L=>{L!==null&&j(L)})},[f]),h.useEffect(()=>{w(f,g)},[f,g]),[g,j]}}}const{getStorage:tr}=le();let ye="https://sdk.dorylus.chat/apis";const nr=n=>{n&&(ye=n)},Ie=Ze.create({baseURL:ye,timeout:6e4});Ie.interceptors.request.use(async n=>{const o=await tr("auth");return o&&(n.headers.Token=(o==null?void 0:o.token)||""),n},n=>Promise.reject(n));const ue=async(n,o)=>(await Ie.post(n,o)).data,ar=n=>{const{setStorage:o}=le();return n.unique_id||ke.load().then(_=>_.get()).then(_=>{const d=_.visitorId;n.unique_id=d,o("visitorId",d)}),ue("/sdk/sdk_get_token",n)},or=n=>ue("/app/conversation_history",n),sr=n=>ue("/app/upsert_conversation",n),ir=n=>ue("/app/delete_one_conversation_history",{id:n}),cr=()=>{const[n,o]=h.useState(null),[_,d]=h.useState(!0),[R,w]=h.useState(null),O=async(m,f,l)=>new Promise(async(g,j)=>{var L;try{const{getStorage:M}=le(),Y=await M("auth"),H=await fetch(f,{method:"POST",headers:{"Content-Type":"application/json",token:(Y==null?void 0:Y.token)||""},body:JSON.stringify(m)});if(!H.ok)throw new Error(`Server responded with status: ${H.status}`);const k=(L=H.body)==null?void 0:L.getReader();if(!k)throw new Error("Failed to get response reader");w(k);const y=new TextDecoder;let K="";(async()=>{try{for(;;){const{done:W,value:te}=await k.read();if(W){d(!1),g();break}K+=y.decode(te,{stream:!0});const G=K.split(`
52
- `);K=G.pop()||"";for(const z of G)try{if(z.startsWith("data:")){const V=z.substring(5).trim();if(V){const N=JSON.parse(V);l(N)}}}catch(V){console.error("Error parsing message:",V)}}}catch(W){j(W)}})()}catch(M){o(M),j(M)}}),T=()=>{R&&(R.cancel(),w(null))};return h.useEffect(()=>()=>{T()},[]),{loading:_,error:n,connect:O,close:T}},lr=n=>n.map(o=>({self:o.is_user,content:o.message})),Ae=({apiKey:n,serverUrl:o,UserAvatar:_,AgentAvatar:d,loadingText:R="AI is thinking...",emptyText:w="No messages yet",theme:O={}})=>{const[T,m]=h.useState([]),[f,l]=h.useState(""),[g,j]=h.useState(!1),[L,M]=h.useState(!1),[Y,H]=h.useState(""),[k,y]=h.useState(null),K=h.useRef(null),{getStorage:B,setStorage:W}=le(),{connect:te}=cr(),G=h.useRef(null),[z,V]=h.useState(""),N={padding:O.padding||20,borderRadius:O.borderRadius||20,bgColor:O.bgColor||"#151515",primary:O.primary||"#AFAFFE",textColor:O.textColor||"#fff"},_e=s=>{console.log("Received message:",s),s.type==="text"?(V(v=>v+s.content),m(v=>{const b=[...v],S=k!==null?k:b.length-1;if(S>=0&&S<b.length){const J=b[S];J.loading&&(J.loading=!1,M(!1)),J.content=z+s.content}return b})):s.type==="complete"&&(console.log("Message complete"),k!==null&&m(v=>{const b=[...v],S=b[k];return S.loading=!1,S.content=z||w,b}),V(""),y(null),j(!1),M(!1))},ne=async()=>{if(!f.trim())return;j(!0),M(!0),y(null);const s={self:!0,content:f,loading:!1},v={self:!1,content:"",loading:!0};m(q=>[...q,s,v]),y(T.length+1);const b=await B("conversation"),S=await B("agent"),J={q:f,app_id:(S==null?void 0:S.app_id)||"",conversation_id:b||"",is_debug:!1,medias:[]};l("");try{console.log("Connecting to stream..."),await te(J,`${ye}/app/stream_chat`,_e)}catch(q){console.error("Error sending message:",q),k!==null&&m(be=>{const oe=[...be];return oe[k].loading=!1,oe[k].content="Error: Failed to get response",oe}),y(null),M(!1)}finally{j(!1)}},D=()=>{var s;(s=G.current)==null||s.scrollIntoView({behavior:"smooth"})},U=async s=>{const b=await ar({api_key:n,unique_id:s});b.code==0&&(W("auth",b.data.auth),W("agent",b.data.agent),fe(b.data.agent))},X=async()=>{const s=await B("conversation");s&&(await ir(s)).code==0&&m([])},ae=async s=>{const v=await B("agent"),b={page:1,pagesize:2e6,app_id:(v==null?void 0:v.app_id)||"",conversation_id:s||""},S=await or(b);S.code==0&&(m(lr(S.data.list).reverse()),!T.length&&(v!=null&&v.opening_text)&&H(v.opening_text),D())},fe=async s=>{const v=await B("conversation");if(v)ae(v);else{const b={id:"",app_id:(s==null?void 0:s.app_id)||"",name:"Unnamed"},S=await sr(b);S.code==0&&(await W("conversation",S.data.id||""),ae(S.data.id||""))}};return h.useEffect(()=>{D(),n&&ke.load().then(s=>s.get()).then(async s=>{const v=s.visitorId;await W("visitorId",v),U(v)})},[]),h.useEffect(()=>{nr(o)},[o]),h.useEffect(()=>{D()},[T]),C.jsxs("div",{className:"chat-library",style:{backgroundColor:N.bgColor,color:N.textColor,borderRadius:`${N.borderRadius}px`,padding:`${N.padding}px`},children:[C.jsx("div",{className:"chat-container",ref:K,children:C.jsxs("div",{className:"chat-container-list",children:[T.map((s,v)=>C.jsx(me,{self:s.self,UserAvatar:_,AgentAvatar:d,loading:s.loading,loadingText:R,emptyText:w,content:s.content,children:C.jsx("div",{dangerouslySetInnerHTML:{__html:rr(s.content)}})},v)),!T.length&&Y&&C.jsx(me,{self:!1,AgentAvatar:d,loading:!1,content:Y,children:C.jsx("div",{dangerouslySetInnerHTML:{__html:Y}})}),C.jsx("div",{ref:G})]})}),C.jsxs("div",{className:"chat-bar",children:[C.jsx("svg",{onClick:X,className:"chat-bar-trash",xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",children:C.jsx("path",{fill:"none",stroke:"currentColor",strokeLinecap:"round",strokeLinejoin:"round",strokeWidth:"2",d:"M4 7h16m-10 4v6m4-6v6M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2l1-12M9 7V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v3"})}),C.jsx("input",{className:"chat-bar-input",disabled:g,placeholder:"Ask anything...",value:f,onChange:s=>l(s.target.value),onKeyUp:s=>s.key==="Enter"&&ne()}),C.jsx("button",{className:"chat-bar-send",disabled:g,style:{borderColor:N.primary,color:N.primary,"--primary-color":N.primary},onClick:ne,children:"Send"})]})]})};I.ChatLibrary=Ae,I.MessageItem=me,I.default=Ae,Object.defineProperties(I,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});