@smarter.sh/ui-chat 0.2.3 → 0.2.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.
- package/dist/smarter-chat-library.es.js +3 -3
- package/dist/smarter-chat-library.es.js.map +1 -1
- package/dist/smarter-chat-library.umd.js +1 -1
- package/dist/smarter-chat-library.umd.js.map +1 -1
- package/dist/ui-chat.css +1 -1
- package/package.json +1 -1
- package/src/components/SmarterChat/{SmarterChat.jsx → Component.jsx} +4 -4
- package/src/components/SmarterChat/index.js +2 -2
- package/src/components/SmarterChat/styles.css +2 -2
|
@@ -595,5 +595,5 @@ See https://s-c.sh/2BAXzed for more info.`),window[ta]+=1);const ri=tn.div`
|
|
|
595
595
|
`,mt={INCOMING:"incoming",OUTGOING:"outgoing"},tt={SYSTEM:"system",ASSISTANT:"assistant",USER:"user",TOOL:"tool",SMARTER:"smarter"},tl=[tt.SYSTEM,tt.ASSISTANT,tt.USER,tt.TOOL],nl=!1,Tv="SmarterChat/1.0",ai="application/json";function na(e,t=null){let r=null;if(document.cookie&&document.cookie!==""){const n=document.cookie.split(";");for(let a=0;a<n.length;a++){const o=n[a].trim();if(o.substring(0,e.name.length+1)===e.name+"="){r=decodeURIComponent(o.substring(e.name.length+1));break}}}return r||t}function oi(e,t){const r=window.location.pathname;if(t){const n=new Date;n.setTime(n.getTime()+e.expiration);const a=n.toUTCString(),o=`${e.name}=${t}; path=${r}; SameSite=Lax; expires=${a}`;document.cookie=o}else{const a=new Date(0).toUTCString(),o=`${e.name}=; path=${r}; SameSite=Lax; expires=${a}`;document.cookie=o}}function Av(e,t){const r={session_key:t.session_key,messages:e};return JSON.stringify(r)}function rl(e){console.log("requestHeadersFactory(): cookies",e);function t(o){return document.cookie.split(";").filter(c=>!c.trim().startsWith(`${o.csrfCookie.name}=`)).join("; ")}const r=t(e),n=na(e.csrfCookie,"");return{Accept:ai,"Content-Type":ai,"X-CSRFToken":n,Origin:window.location.origin,Cookie:r,Authorization:"Bearer null","User-Agent":Tv}}function al(e,t){return{method:"POST",credentials:"include",mode:"cors",headers:e,body:t}}function ol(e,t,r){e.endsWith("/")||(e+="/"),t=t||"";let n=new URL(t,e);return r&&n.searchParams.append("session_key",r),n.toString()}async function il(e,t,r){const n=na(r.debugCookie)==="true";try{(n||nl)&&console.log("getJsonResponse(): url: ",e,", init: ",t,", cookies: ",r);const a=await fetch(e,t),o=a.headers.get("content-type");if(o&&o.includes(ai)){const i=await a.status;if(a.ok){const s=await a.json(),c=await s.data;return(n||nl)&&console.log("getJsonResponse(): response: ",s),c}else return console.error("getJsonResponse(): error: ",i,a.statusText),a}else{const i=await a.text();throw new Error(`getJsonResponse() Unexpected response format: ${i}`)}}catch(a){return a}}async function Mv(e,t,r){console.log("fetchPrompt(): config",e);const n=e.chatbot.url_chatbot,a=na(r.sessionCookie,""),o=ol(n,null,a),i=rl(r),s=Av(t,e),c=al(i,s),l=await il(o,c,r);return l&&l.body?(console.log("fetchPrompt(): parsing responseJson.body "),await JSON.parse(l.body)):null}async function Iv(e,t){const r=na(t.sessionCookie,""),n=rl(t),a=JSON.stringify({session_key:r}),o=al(n,a),i=ol(e,"config/",r),s=await il(i,o,t);return s?(oi(t.sessionCookie,s.session_key),oi(t.debugCookie,s.debug_mode),s):null}function ii(e,t){return{name:e,expiration:t}}function $v(e,t){var r,n,a;try{const o=(e||[]).map(i=>{if(i.role===tt.USER)return Ot(i,i.content,mt.OUTGOING,i.role);if(i.role===tt.SYSTEM)return Ot(i,i.content,mt.INCOMING,i.role);if(i.role===tt.ASSISTANT)return Ot(i,i.content,mt.INCOMING,i.role);if(i.role===tt.SMARTER)return Ot(i,i.content,mt.INCOMING,i.role);if(i.role===tt.TOOL)return Ot(i,i.content,mt.INCOMING,i.role);console.error(`chatRestoreFromBackend() Invalid role received: ${i.role}`)}).filter(i=>i&&typeof i=="object"&&!Array.isArray(i));if((a=(n=(r=t==null?void 0:t.choices)==null?void 0:r[0])==null?void 0:n.message)!=null&&a.content){const i=t.choices[0].message,s=i.content;o.push(Ot(i,s,mt.INCOMING,tt.ASSISTANT))}return o}catch(o){return console.error(`chatRestoreFromBackend() Error occurred while restoring chat from backend: ${o}`),[]}}const Lv=e=>e.length==0?"":`Some example prompts to get you started:\r
|
|
596
596
|
\r
|
|
597
597
|
`+e.map(t=>t+`\r
|
|
598
|
-
`).join("");function Dv(e,t,r){let n=[Ot({},t,mt.INCOMING,tt.SYSTEM)];n.push(Ot({},e,mt.INCOMING,tt.ASSISTANT));const a=Lv(r);return a&&n.push(Ot({},a,mt.INCOMING,tt.ASSISTANT)),n}function Rv(e){if(typeof e!="string")return console.error(`convertMarkdownLinksToHTML() Expected a string but received ${typeof e}`),console.error("convertMarkdownLinksToHTML() broke here",e),e;const t=/\[([^\]]+)\]\(([^)]+)\)/g;return e.replace(t,'<a href="$2">$1</a>')}function Ot(e,t,r,n){const a=typeof t=="string"&&t!==null,i={message:a?Rv(t):t,direction:r,sender:n,sentTime:new Date().toLocaleString(),display:a};return e?i.originalMessage=e:i.originalMessage=i,i}function Fv(e){let t=e.originalMessage||{};return t.role=e.originalMessage&&e.originalMessage.role?e.originalMessage.role:e.sender,t.content=e.originalMessage&&e.originalMessage.message?e.originalMessage.message:e.message,t}function zv(e){return e.filter(t=>tl.includes(t.sender)).map((t,r)=>Fv(t))}function Wv(e,t,r,n,a,o){let i=[];return i=$v(a,o),i.length===0&&(i=Dv(e,t,r)),i}class sl extends w.Component{constructor(t){super(t),this.state={hasError:!1}}static getDerivedStateFromError(){return{hasError:!0}}componentDidCatch(t,r){console.error(t,r)}render(){return this.state.hasError?re.jsx("h1",{children:"Something went wrong."}):this.props.children}}sl.propTypes={children:d.node};const Bv=!1;function Hv({apiUrl:e,apiKey:t,toggleMetadata:r,csrfCookieName:n,debugCookieName:a,debugCookieExpiration:o,sessionCookieName:i,sessionCookieExpiration:s,showConsole:c=!0}){const[l,u]=w.useState(e),[h,b]=w.useState(r),[g,_]=w.useState(!1),[E,$]=w.useState({}),[L,C]=w.useState(""),[z,f]=w.useState(""),[S,D]=w.useState(""),[F,K]=w.useState(!1),[P,M]=w.useState(""),[B,Z]=w.useState(""),[N,de]=w.useState(!1),[oe,I]=w.useState(!1),[ce,ge]=w.useState(Bv),[me,T]=w.useState([]),[q,X]=w.useState(!1),[ie,ae]=w.useState(!1),[le,U]=w.useState(""),[te,ne]=w.useState(""),se=w.useRef(null),ue=ii(n,null),je=ii(i,s),G=ii(a,o),_e={csrfCookie:ue,sessionCookie:je,debugCookie:G},Ye=async()=>{const J=await Iv(l,_e);return J!=null&&J.debug_mode&&(console.log("fetchAndSetConfig()..."),console.log("fetchAndSetConfig() config:",J)),d.checkPropTypes(ba,J,"prop","SmarterChat"),$(J),J},Ze=async()=>{var J;try{const Q=await Ye();console.log("fetchAndSetConfig() config:",Q),C(Q.chatbot.app_placeholder),u(Q.chatbot.url_chatbot),f(Q.chatbot.app_assistant),D(Q.chatbot.app_info_url),K(Q.chatbot.app_file_attachment),de(Q.meta_data.is_valid),I(Q.meta_data.is_deployed),ge(Q.debug_mode);const pe=((J=Q.history)==null?void 0:J.chat_history)||[],Se=Wv(Q.chatbot.app_welcome_message,Q.chatbot.default_system_role,Q.chatbot.app_example_prompts,Q.session_key,pe,"BACKEND_CHAT_MOST_RECENT_RESPONSE");T(Se);const Te=`${Q.chatbot.app_name} v${Q.chatbot.version||"1.0.0"}`;M(Te);let Ae=`${Q.chatbot.provider} ${Q.chatbot.default_model}`;Q.plugins.meta_data.total_plugins>0&&(Ae+=` with ${Q.plugins.meta_data.total_plugins} additional plugins`),Z(Ae),_(!0),X(!1),Q!=null&&Q.debug_mode&&console.log("fetchAndSetConfig() done!")}catch(Q){console.error("Failed to fetch config:",Q)}};w.useEffect(()=>(ce&&console.log("ChatApp() component mounted"),Ze(),()=>{ce&&console.log("ChatApp() component unmounted")}),[]);function st(J,Q){ae(!0),ne(J),U(Q)}function qt(){ae(!1)}const Vt=()=>{const J=!h;b(J),ce&&console.log("showMetadata:",J);const Q=me.map(pe=>pe.message===null?{...pe,display:!1}:["smarter","system","tool"].includes(pe.sender)?{...pe,display:J}:{...pe,display:!0});T(Q)},xt=()=>{oi(_e.sessionCookie,""),Ze()};async function Dt(J,Q=!1){const pe=Ot({},J,mt.OUTGOING,tt.USER);Q&&console.error("base64 encoding not implemented yet."),T(Se=>{const Te=[...Se,pe];return X(!0),(async()=>{try{ce&&console.log("handleApiRequest() messages:",Te);const Ae=zv(Te),Be=await Mv(E,Ae,_e);if(Be){const Le=Be.smarter.messages.filter(Re=>Re.content!==null).map(Re=>Ot(Re,Re.content,mt.INCOMING,Re.role));T(Re=>[...Re,...Le]),X(!1),Ye()}}catch(Ae){X(!1),console.error("Api error: ",Ae),st("Api error",Ae.message)}})(),Te})}const _t=async()=>{se.current.click()};function ot(J){const Q=J.target.files[0],pe=new FileReader;pe.onload=Se=>{const Te=Se.target.result;Dt(Te,!0)},pe.readAsText(Q)}const qe=J=>{const Q=J.replace(/<[^>]+>/g,"");Q.trim()&&Dt(Q,!1)};function Nt({title:J,isValid:Q,isDeployed:pe}){return re.jsx("div",{children:g?re.jsxs(re.Fragment,{children:[J," ",Q?re.jsx(Je,{icon:Cu,style:{color:"green"}}):re.jsx(Je,{icon:Au,style:{color:"red"}}),pe?re.jsxs(re.Fragment,{children:[" ",re.jsx(Je,{icon:Nu,style:{color:"orange"}})]}):null]}):re.jsx("div",{children:"Loading..."})})}function H({i:J,message:Q}){let pe="";return Q.sender==="smarter"?pe="smarter-message":["tool","system"].includes(Q.sender)&&(pe="system-message"),re.jsx($t,{model:Q,className:pe},J)}const A={width:"100%"},k={backgroundColor:"rgba(0,0,0,0.10)",color:"lightgray"},Y={width:"100%",height:"100%"},ee={...A,...k};return re.jsx("div",{id:"smarter_chat_component_container",className:"SmarterChat",children:re.jsx(Pv,{children:re.jsx(Cv,{children:re.jsxs(xv,{children:[re.jsx(Nv,{children:re.jsx("div",{className:"chat-app",children:re.jsxs(ac,{style:Y,children:[re.jsx(sl,{children:re.jsx(fc,{isModalOpen:ie,title:te,message:le,onCloseClick:qt})}),re.jsxs(ec,{style:ee,children:[re.jsxs(jt,{children:[re.jsx(jt.Content,{userName:g?re.jsx(Nt,{title:P,isValid:N,isDeployed:oe}):"
|
|
598
|
+
`).join("");function Dv(e,t,r){let n=[Ot({},t,mt.INCOMING,tt.SYSTEM)];n.push(Ot({},e,mt.INCOMING,tt.ASSISTANT));const a=Lv(r);return a&&n.push(Ot({},a,mt.INCOMING,tt.ASSISTANT)),n}function Rv(e){if(typeof e!="string")return console.error(`convertMarkdownLinksToHTML() Expected a string but received ${typeof e}`),console.error("convertMarkdownLinksToHTML() broke here",e),e;const t=/\[([^\]]+)\]\(([^)]+)\)/g;return e.replace(t,'<a href="$2">$1</a>')}function Ot(e,t,r,n){const a=typeof t=="string"&&t!==null,i={message:a?Rv(t):t,direction:r,sender:n,sentTime:new Date().toLocaleString(),display:a};return e?i.originalMessage=e:i.originalMessage=i,i}function Fv(e){let t=e.originalMessage||{};return t.role=e.originalMessage&&e.originalMessage.role?e.originalMessage.role:e.sender,t.content=e.originalMessage&&e.originalMessage.message?e.originalMessage.message:e.message,t}function zv(e){return e.filter(t=>tl.includes(t.sender)).map((t,r)=>Fv(t))}function Wv(e,t,r,n,a,o){let i=[];return i=$v(a,o),i.length===0&&(i=Dv(e,t,r)),i}class sl extends w.Component{constructor(t){super(t),this.state={hasError:!1}}static getDerivedStateFromError(){return{hasError:!0}}componentDidCatch(t,r){console.error(t,r)}render(){return this.state.hasError?re.jsx("h1",{children:"Something went wrong."}):this.props.children}}sl.propTypes={children:d.node};const Bv=!1;function Hv({apiUrl:e,apiKey:t,toggleMetadata:r,csrfCookieName:n,debugCookieName:a,debugCookieExpiration:o,sessionCookieName:i,sessionCookieExpiration:s,showConsole:c=!0}){const[l,u]=w.useState(e),[h,b]=w.useState(r),[g,_]=w.useState(!1),[E,$]=w.useState({}),[L,C]=w.useState(""),[z,f]=w.useState(""),[S,D]=w.useState(""),[F,K]=w.useState(!1),[P,M]=w.useState(""),[B,Z]=w.useState(""),[N,de]=w.useState(!1),[oe,I]=w.useState(!1),[ce,ge]=w.useState(Bv),[me,T]=w.useState([]),[q,X]=w.useState(!1),[ie,ae]=w.useState(!1),[le,U]=w.useState(""),[te,ne]=w.useState(""),se=w.useRef(null),ue=ii(n,null),je=ii(i,s),G=ii(a,o),_e={csrfCookie:ue,sessionCookie:je,debugCookie:G},Ye=async()=>{const J=await Iv(l,_e);return J!=null&&J.debug_mode&&(console.log("fetchAndSetConfig()..."),console.log("fetchAndSetConfig() config:",J)),d.checkPropTypes(ba,J,"prop","SmarterChat"),$(J),J},Ze=async()=>{var J;try{const Q=await Ye();console.log("fetchAndSetConfig() config:",Q),C(Q.chatbot.app_placeholder),u(Q.chatbot.url_chatbot),f(Q.chatbot.app_assistant),D(Q.chatbot.app_info_url),K(Q.chatbot.app_file_attachment),de(Q.meta_data.is_valid),I(Q.meta_data.is_deployed),ge(Q.debug_mode);const pe=((J=Q.history)==null?void 0:J.chat_history)||[],Se=Wv(Q.chatbot.app_welcome_message,Q.chatbot.default_system_role,Q.chatbot.app_example_prompts,Q.session_key,pe,"BACKEND_CHAT_MOST_RECENT_RESPONSE");T(Se);const Te=`${Q.chatbot.app_name} v${Q.chatbot.version||"1.0.0"}`;M(Te);let Ae=`${Q.chatbot.provider} ${Q.chatbot.default_model}`;Q.plugins.meta_data.total_plugins>0&&(Ae+=` with ${Q.plugins.meta_data.total_plugins} additional plugins`),Z(Ae),_(!0),X(!1),Q!=null&&Q.debug_mode&&console.log("fetchAndSetConfig() done!")}catch(Q){console.error("Failed to fetch config:",Q)}};w.useEffect(()=>(ce&&console.log("ChatApp() component mounted"),Ze(),()=>{ce&&console.log("ChatApp() component unmounted")}),[]);function st(J,Q){ae(!0),ne(J),U(Q)}function qt(){ae(!1)}const Vt=()=>{const J=!h;b(J),ce&&console.log("showMetadata:",J);const Q=me.map(pe=>pe.message===null?{...pe,display:!1}:["smarter","system","tool"].includes(pe.sender)?{...pe,display:J}:{...pe,display:!0});T(Q)},xt=()=>{oi(_e.sessionCookie,""),Ze()};async function Dt(J,Q=!1){const pe=Ot({},J,mt.OUTGOING,tt.USER);Q&&console.error("base64 encoding not implemented yet."),T(Se=>{const Te=[...Se,pe];return X(!0),(async()=>{try{ce&&console.log("handleApiRequest() messages:",Te);const Ae=zv(Te),Be=await Mv(E,Ae,_e);if(Be){const Le=Be.smarter.messages.filter(Re=>Re.content!==null).map(Re=>Ot(Re,Re.content,mt.INCOMING,Re.role));T(Re=>[...Re,...Le]),X(!1),Ye()}}catch(Ae){X(!1),console.error("Api error: ",Ae),st("Api error",Ae.message)}})(),Te})}const _t=async()=>{se.current.click()};function ot(J){const Q=J.target.files[0],pe=new FileReader;pe.onload=Se=>{const Te=Se.target.result;Dt(Te,!0)},pe.readAsText(Q)}const qe=J=>{const Q=J.replace(/<[^>]+>/g,"");Q.trim()&&Dt(Q,!1)};function Nt({title:J,isValid:Q,isDeployed:pe}){return re.jsx("div",{children:g?re.jsxs(re.Fragment,{children:[J," ",Q?re.jsx(Je,{icon:Cu,style:{color:"green"}}):re.jsx(Je,{icon:Au,style:{color:"red"}}),pe?re.jsxs(re.Fragment,{children:[" ",re.jsx(Je,{icon:Nu,style:{color:"orange"}})]}):null]}):re.jsx("div",{children:"Loading..."})})}function H({i:J,message:Q}){let pe="";return Q.sender==="smarter"?pe="smarter-message":["tool","system"].includes(Q.sender)&&(pe="system-message"),re.jsx($t,{model:Q,className:pe},J)}const A={width:"100%"},k={backgroundColor:"rgba(0,0,0,0.10)",color:"lightgray"},Y={width:"100%",height:"100%"},ee={...A,...k};return re.jsx("div",{id:"smarter_chat_component_container",className:"SmarterChat",children:re.jsx(Pv,{children:re.jsx(Cv,{children:re.jsxs(xv,{children:[re.jsx(Nv,{children:re.jsx("div",{className:"chat-app",children:re.jsxs(ac,{style:Y,children:[re.jsx(sl,{children:re.jsx(fc,{isModalOpen:ie,title:te,message:le,onCloseClick:qt})}),re.jsxs(ec,{style:ee,children:[re.jsxs(jt,{children:[re.jsx(jt.Content,{userName:g?re.jsx(Nt,{title:P,isValid:N,isDeployed:oe}):"Configuring workbench...",info:g?B:""}),re.jsxs(jt.Actions,{children:[re.jsx(ys,{onClick:xt,title:"Start a new chat"}),r&&re.jsx(vs,{onClick:Vt,title:"Toggle system meta data"})]})]}),re.jsx(Ht,{style:k,scrollBehavior:"auto",typingIndicator:q?re.jsx(lc,{content:z+" is typing"}):null,children:me.filter(J=>J.display).map((J,Q)=>re.jsx(H,{i:Q,message:J}))}),re.jsx(On,{placeholder:L,onSend:qe,onAttachClick:_t,attachButton:F,fancyScroll:!1})]}),re.jsx("input",{type:"file",accept:".py",title:"Select a Python file",ref:se,style:{display:"none"},onChange:ot})]})})}),c&&re.jsx(kv,{children:re.jsx(Fo,{config:E})})]})})})})}const Yv="0.2.4";Ke.ConfigPropTypes=ba,Ke.Console=Fo,Ke.MenuItems=yt,Ke.MessageDirectionEnum=mt,Ke.SenderRoleEnum=tt,Ke.SmarterChat=Hv,Ke.ValidMessageRolesEnum=tl,Ke.version=Yv,Object.defineProperty(Ke,Symbol.toStringTag,{value:"Module"})});
|
|
599
599
|
//# sourceMappingURL=smarter-chat-library.umd.js.map
|