ondemand-react-chat-bot
Version:
OnDemandChatBot is a React library that provides an easy-to-use AI-powered chatbot component for your application. Built on top of the OnDemand platform, it allows full customization and seamless AI integration.
3 lines (2 loc) • 29.9 kB
JavaScript
import{jsxs as e,jsx as n}from"react/jsx-runtime";import*as t from"react";import{useRef as r,useEffect as o,useState as i}from"react";var a=function(){return a=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var o in n=arguments[t])Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o]);return e},a.apply(this,arguments)};function s(e,n,t,r){return new(t||(t=Promise))((function(o,i){function a(e){try{c(r.next(e))}catch(e){i(e)}}function s(e){try{c(r.throw(e))}catch(e){i(e)}}function c(e){var n;e.done?o(e.value):(n=e.value,n instanceof t?n:new t((function(e){e(n)}))).then(a,s)}c((r=r.apply(e,n||[])).next())}))}function c(e,n){var t,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=s(0),a.throw=s(1),a.return=s(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(s){return function(c){return function(s){if(t)throw new TypeError("Generator is already executing.");for(;a&&(a=0,s[0]&&(i=0)),i;)try{if(t=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,r=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=n.call(e,i)}catch(e){s=[6,e],r=0}finally{t=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}function l(e,n,t){if(t||2===arguments.length)for(var r,o=0,i=n.length;o<i;o++)!r&&o in n||(r||(r=Array.prototype.slice.call(n,0,o)),r[o]=n[o]);return e.concat(r||Array.prototype.slice.call(n))}"function"==typeof SuppressedError&&SuppressedError;var u=function(t){var r=t.color;return e("svg",{xmlns:"http://www.w3.org/2000/svg",width:24,height:24,viewBox:"0 0 24 24",fill:"none",stroke:r,strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round",className:"lucide lucide-send-horizontal",children:[n("path",{d:"M3.714 3.048a.498.498 0 0 0-.683.627l2.843 7.627a2 2 0 0 1 0 1.396l-2.842 7.627a.498.498 0 0 0 .682.627l18-8.5a.5.5 0 0 0 0-.904z"}),n("path",{d:"M6 12h16"})]})},d=function(t){var r=t.color;return e("svg",{xmlns:"http://www.w3.org/2000/svg",width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:r,"stroke-width":"1.5","stroke-linecap":"round","stroke-linejoin":"round",className:"lucide lucide-circle-x",children:[n("circle",{cx:"12",cy:"12",r:"10"}),n("path",{d:"m15 9-6 6"}),n("path",{d:"m9 9 6 6"})]})},p=function(t){var r=t.color;return e("svg",{xmlns:"http://www.w3.org/2000/svg",width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:r,"stroke-width":"2","stroke-linecap":"round","stroke-linejoin":"round",className:"lucide lucide-bot",children:[n("path",{d:"M12 8V4H8"}),n("rect",{width:"16",height:"12",x:"4",y:"8",rx:"2"}),n("path",{d:"M2 14h2"}),n("path",{d:"M20 14h2"}),n("path",{d:"M15 13v2"}),n("path",{d:"M9 13v2"})]})},h={"Analyzing the prompt...":"We're carefully reviewing your request.","Re-analyzing the prompt...":"Checking again to ensure everything is accurate!","Analysis failed":"We couldn't process your request. Please try again with more details.","Execution plan created":"We've mapped out the next steps for you.","Retrieved the agents":"We've found the right tools to assist you.","Executing the agents...":"Processing your request now...","Agents execution completed":"All done! Here's what we found for you.","Agents execution failed":"We ran into an issue while working on your request. Please try again.","Execution log created":"Your request details have been recorded for reference.","Fulfilling the prompt...":"Almost there! We're putting everything together.","Fulfillment completed":"Success! Your request has been completed.","Fulfillment failed":"We hit a snag while finishing up. Please try again or reach out for help."},f=function(e,n){e((function(e){return l(l([],e,!0),[a({message:"",isFetching:!1,id:Date.now().toString(),author:"bot",status:"loading"},n)],!1)}))},m=function(e,n){e((function(e){return l(l([],e.slice(0,-1),!0),[a(a({},e[e.length-1]),"function"==typeof n?n(e[e.length-1]):n)],!1)}))},g=function(e){return"dev"===e?"https://gateway-dev.on-demand.io":"https://api.on-demand.io"};function y(e,n,t,r){return s(this,void 0,void 0,(function(){var o,i,a,s,l,u;return c(this,(function(c){switch(c.label){case 0:return c.trys.push([0,3,,4]),o={pluginIds:[],externalUserId:"1",contextMetadata:r},i=g(n),[4,fetch("".concat(i,"/chat/v1/sessions"),{method:"POST",headers:{"Content-Type":"application/json","x-bot-id":t,apikey:e},body:JSON.stringify(o)})];case 1:if(!(a=c.sent()).ok)throw new Error("HTTP error! status: ".concat(a.status));return[4,a.json()];case 2:return s=c.sent(),[2,null===(u=null==s?void 0:s.data)||void 0===u?void 0:u.id];case 3:throw l=c.sent(),console.error("Failed to generate session:",l),l;case 4:return[2]}}))}))}function b(e,n,t){return s(this,void 0,void 0,(function(){var r,o,i;return c(this,(function(a){switch(a.label){case 0:return a.trys.push([0,3,,4]),r=g(t),[4,fetch("".concat(r,"/config/v1/public/embedded/chatbot/").concat(n),{method:"GET",headers:{"Content-Type":"application/json","x-bot-id":n,apikey:e}})];case 1:if(!(o=a.sent()).ok)throw new Error("HTTP error! status: ".concat(o.status));return[4,o.json()];case 2:return[2,a.sent().data];case 3:throw i=a.sent(),console.error("Failed to get bot configuration:",i),i;case 4:return[2]}}))}))}var v=function(t){var r=t.input,o=t.setInput,i=t.isInputDisabled,a=t.botConfig,s=t.onSubmit;return n("div",{className:"chat-input-container",children:e("form",{onSubmit:s,className:"chat-form",children:[n("input",{type:"text",value:r,disabled:i,onChange:function(e){return o(e.target.value)},placeholder:a.inputPlaceholder,className:"chat-input"}),n("button",{type:"submit",disabled:i||!r.trim(),className:"send-button",style:{background:a.primaryColor},children:n(u,{color:a.secondaryColor})})]})})};function k(){return k=Object.assign?Object.assign.bind():function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},k.apply(this,arguments)}const w=["children","options"],x="0",C="1",E="2",S="3",I="4",N="5",T="6",A="7",M="8",z="9",$="10",O="11",P="12",j="13",L="14",F="15",R="16",D="17",q="18",W="19",H="20",B="21",U="22",_="23",V="24",G="25",Z="27",J="28",X="29",Y="30",K="31",Q="32",ee="33";var ne;!function(e){e[e.MAX=0]="MAX",e[e.HIGH=1]="HIGH",e[e.MED=2]="MED",e[e.LOW=3]="LOW",e[e.MIN=4]="MIN"}(ne||(ne={}));const te=["allowFullScreen","allowTransparency","autoComplete","autoFocus","autoPlay","cellPadding","cellSpacing","charSet","classId","colSpan","contentEditable","contextMenu","crossOrigin","encType","formAction","formEncType","formMethod","formNoValidate","formTarget","frameBorder","hrefLang","inputMode","keyParams","keyType","marginHeight","marginWidth","maxLength","mediaGroup","minLength","noValidate","radioGroup","readOnly","rowSpan","spellCheck","srcDoc","srcLang","srcSet","tabIndex","useMap"].reduce(((e,n)=>(e[n.toLowerCase()]=n,e)),{class:"className",for:"htmlFor"}),re={amp:"&",apos:"'",gt:">",lt:"<",nbsp:" ",quot:"“"},oe=["style","script"],ie=/([-A-Z0-9_:]+)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|(?:\{((?:\\.|{[^}]*?}|[^}])*)\})))?/gi,ae=/mailto:/i,se=/\n{2,}$/,ce=/^(\s*>[\s\S]*?)(?=\n\n|$)/,le=/^ *> ?/gm,ue=/^(?:\[!([^\]]*)\]\n)?([\s\S]*)/,de=/^ {2,}\n/,pe=/^(?:( *[-*_])){3,} *(?:\n *)+\n/,he=/^(?: {1,3})?(`{3,}|~{3,}) *(\S+)? *([^\n]*?)?\n([\s\S]*?)(?:\1\n?|$)/,fe=/^(?: {4}[^\n]+\n*)+(?:\n *)+\n?/,me=/^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,ge=/^(?:\n *)*\n/,ye=/\r\n?/g,be=/^\[\^([^\]]+)](:(.*)((\n+ {4,}.*)|(\n(?!\[\^).+))*)/,ve=/^\[\^([^\]]+)]/,ke=/\f/g,we=/^---[ \t]*\n(.|\n)*\n---[ \t]*\n/,xe=/^\s*?\[(x|\s)\]/,Ce=/^ *(#{1,6}) *([^\n]+?)(?: +#*)?(?:\n *)*(?:\n|$)/,Ee=/^ *(#{1,6}) +([^\n]+?)(?: +#*)?(?:\n *)*(?:\n|$)/,Se=/^([^\n]+)\n *(=|-){3,} *(?:\n *)+\n/,Ie=/^ *(?!<[a-z][^ >/]* ?\/>)<([a-z][^ >/]*) ?((?:[^>]*[^/])?)>\n?(\s*(?:<\1[^>]*?>[\s\S]*?<\/\1>|(?!<\1\b)[\s\S])*?)<\/\1>(?!<\/\1>)\n*/i,Ne=/&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});/gi,Te=/^<!--[\s\S]*?(?:-->)/,Ae=/^(data|aria|x)-[a-z_][a-z\d_.-]*$/,Me=/^ *<([a-z][a-z0-9:]*)(?:\s+((?:<.*?>|[^>])*))?\/?>(?!<\/\1>)(\s*\n)?/i,ze=/^\{.*\}$/,$e=/^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,Oe=/^<([^ >]+@[^ >]+)>/,Pe=/^<([^ >]+:\/[^ >]+)>/,je=/-([a-z])?/gi,Le=/^(\|.*)\n(?: *(\|? *[-:]+ *\|[-| :]*)\n((?:.*\|.*\n)*))?\n?/,Fe=/^\[([^\]]*)\]:\s+<?([^\s>]+)>?\s*("([^"]*)")?/,Re=/^!\[([^\]]*)\] ?\[([^\]]*)\]/,De=/^\[([^\]]*)\] ?\[([^\]]*)\]/,qe=/(\n|^[-*]\s|^#|^ {2,}|^-{2,}|^>\s)/,We=/\t/g,He=/(^ *\||\| *$)/g,Be=/^ *:-+: *$/,Ue=/^ *:-+ *$/,_e=/^ *-+: *$/,Ve="((?:\\[.*?\\][([].*?[)\\]]|<.*?>(?:.*?<.*?>)?|`.*?`|~~.*?~~|==.*?==|.|\\n)*?)",Ge=new RegExp(`^([*_])\\1${Ve}\\1\\1(?!\\1)`),Ze=new RegExp(`^([*_])${Ve}\\1(?!\\1|\\w)`),Je=new RegExp(`^==${Ve}==`),Xe=new RegExp(`^~~${Ve}~~`),Ye=/^\\([^0-9A-Za-z\s])/,Ke=/^[\s\S]+?(?=[^0-9A-Z\s\u00c0-\uffff&#;.()'"]|\d+\.|\n\n| {2,}\n|\w+:\S|$)/i,Qe=/^\n+/,en=/^([ \t]*)/,nn=/\\([^\\])/g,tn=/ *\n+$/,rn=/(?:^|\n)( *)$/,on="(?:\\d+\\.)",an="(?:[*+-])";function sn(e){return"( *)("+(1===e?on:an)+") +"}const cn=sn(1),ln=sn(2);function un(e){return new RegExp("^"+(1===e?cn:ln))}const dn=un(1),pn=un(2);function hn(e){return new RegExp("^"+(1===e?cn:ln)+"[^\\n]*(?:\\n(?!\\1"+(1===e?on:an)+" )[^\\n]*)*(\\n|$)","gm")}const fn=hn(1),mn=hn(2);function gn(e){const n=1===e?on:an;return new RegExp("^( *)("+n+") [\\s\\S]+?(?:\\n{2,}(?! )(?!\\1"+n+" (?!"+n+" ))\\n*|\\s*\\n*$)")}const yn=gn(1),bn=gn(2);function vn(e,n){const t=1===n,r=t?yn:bn,o=t?fn:mn,i=t?dn:pn;return{match(e,n){const t=rn.exec(n.prevCapture);return t&&(n.list||!n.inline&&!n.simple)?r.exec(e=t[1]+e):null},order:1,parse(e,n,r){const a=t?+e[2]:void 0,s=e[0].replace(se,"\n").match(o);let c=!1;return{items:s.map((function(e,t){const o=i.exec(e)[0].length,a=new RegExp("^ {1,"+o+"}","gm"),l=e.replace(a,"").replace(i,""),u=t===s.length-1,d=-1!==l.indexOf("\n\n")||u&&c;c=d;const p=r.inline,h=r.list;let f;r.list=!0,d?(r.inline=!1,f=l.replace(tn,"\n\n")):(r.inline=!0,f=l.replace(tn,""));const m=n(f,r);return r.inline=p,r.list=h,m})),ordered:t,start:a}},render:(n,t,r)=>e(n.ordered?"ol":"ul",{key:r.key,start:n.type===H?n.start:void 0},n.items.map((function(n,o){return e("li",{key:o},t(n,r))})))}}const kn=new RegExp("^\\[((?:\\[[^\\]]*\\]|[^\\[\\]]|\\](?=[^\\[]*\\]))*)\\]\\(\\s*<?((?:\\([^)]*\\)|[^\\s\\\\]|\\\\.)*?)>?(?:\\s+['\"]([\\s\\S]*?)['\"])?\\s*\\)"),wn=/^!\[(.*?)\]\( *((?:\([^)]*\)|[^() ])*) *"?([^)"]*)?"?\)/,xn=[ce,he,fe,Ce,Se,Ee,Te,Le,fn,yn,mn,bn],Cn=[...xn,/^[^\n]+(?: \n|\n{2,})/,Ie,Me];function En(e){return e.replace(/[ÀÁÂÃÄÅàáâãä忯]/g,"a").replace(/[çÇ]/g,"c").replace(/[ðÐ]/g,"d").replace(/[ÈÉÊËéèêë]/g,"e").replace(/[ÏïÎîÍíÌì]/g,"i").replace(/[Ññ]/g,"n").replace(/[øØœŒÕõÔôÓóÒò]/g,"o").replace(/[ÜüÛûÚúÙù]/g,"u").replace(/[ŸÿÝý]/g,"y").replace(/[^a-z0-9- ]/gi,"").replace(/ /gi,"-").toLowerCase()}function Sn(e){return _e.test(e)?"right":Be.test(e)?"center":Ue.test(e)?"left":null}function In(e,n,t,r){const o=t.inTable;t.inTable=!0;let i=[[]],a="";function s(){if(!a)return;const e=i[i.length-1];e.push.apply(e,n(a,t)),a=""}return e.trim().split(/(`[^`]*`|\\\||\|)/).filter(Boolean).forEach(((e,n,t)=>{"|"===e.trim()&&(s(),r)?0!==n&&n!==t.length-1&&i.push([]):a+=e})),s(),t.inTable=o,i}function Nn(e,n,t){t.inline=!0;const r=e[2]?e[2].replace(He,"").split("|").map(Sn):[],o=e[3]?function(e,n,t){return e.trim().split("\n").map((function(e){return In(e,n,t,!0)}))}(e[3],n,t):[],i=In(e[1],n,t,!!o.length);return t.inline=!1,o.length?{align:r,cells:o,header:i,type:G}:{children:i,type:B}}function Tn(e,n){return null==e.align[n]?{}:{textAlign:e.align[n]}}function An(e){return function(n,t){return t.inline?e.exec(n):null}}function Mn(e){return function(n,t){return t.inline||t.simple?e.exec(n):null}}function zn(e){return function(n,t){return t.inline||t.simple?null:e.exec(n)}}function $n(e){return function(n){return e.exec(n)}}function On(e,n){if(n.inline||n.simple)return null;let t="";e.split("\n").every((e=>(e+="\n",!xn.some((n=>n.test(e)))&&(t+=e,!!e.trim()))));const r=t.trimEnd();return""==r?null:[t,r]}function Pn(e){try{if(decodeURIComponent(e).replace(/[^A-Za-z0-9/:]/g,"").match(/^\s*(javascript|vbscript|data(?!:image)):/i))return null}catch(e){return null}return e}function jn(e){return e.replace(nn,"$1")}function Ln(e,n,t){const r=t.inline||!1,o=t.simple||!1;t.inline=!0,t.simple=!0;const i=e(n,t);return t.inline=r,t.simple=o,i}function Fn(e,n,t){const r=t.inline||!1,o=t.simple||!1;t.inline=!1,t.simple=!0;const i=e(n,t);return t.inline=r,t.simple=o,i}function Rn(e,n,t){const r=t.inline||!1;t.inline=!1;const o=e(n,t);return t.inline=r,o}const Dn=(e,n,t)=>({children:Ln(n,e[1],t)});function qn(){return{}}function Wn(){return null}function Hn(...e){return e.filter(Boolean).join(" ")}function Bn(e,n,t){let r=e;const o=n.split(".");for(;o.length&&(r=r[o[0]],void 0!==r);)o.shift();return r||t}function Un(e="",n={}){function r(e,t,...r){const o=Bn(n.overrides,`${e}.props`,{});return n.createElement(function(e,n){const t=Bn(n,e);return t?"function"==typeof t||"object"==typeof t&&"render"in t?t:Bn(n,`${e}.component`,e):e}(e,n.overrides),k({},t,o,{className:Hn(null==t?void 0:t.className,o.className)||void 0}),...r)}function o(e){e=e.replace(we,"");let t=!1;n.forceInline?t=!0:n.forceBlock||(t=!1===qe.test(e));const o=u(l(t?e:`${e.trimEnd().replace(Qe,"")}\n\n`,{inline:t}));for(;"string"==typeof o[o.length-1]&&!o[o.length-1].trim();)o.pop();if(null===n.wrapper)return o;const i=n.wrapper||(t?"span":"div");let a;if(o.length>1||n.forceWrapper)a=o;else{if(1===o.length)return a=o[0],"string"==typeof a?r("span",{key:"outer"},a):a;a=null}return n.createElement(i,{key:"outer"},a)}function i(e,t){const r=t.match(ie);return r?r.reduce((function(t,r){const i=r.indexOf("=");if(-1!==i){const a=function(e){return-1!==e.indexOf("-")&&null===e.match(Ae)&&(e=e.replace(je,(function(e,n){return n.toUpperCase()}))),e}(r.slice(0,i)).trim(),s=function(e){const n=e[0];return('"'===n||"'"===n)&&e.length>=2&&e[e.length-1]===n?e.slice(1,-1):e}(r.slice(i+1).trim()),c=te[a]||a;if("ref"===c)return t;const l=t[c]=function(e,n,t,r){return"style"===n?t.split(/;\s?/).reduce((function(e,n){const t=n.slice(0,n.indexOf(":"));return e[t.trim().replace(/(-[a-z])/g,(e=>e[1].toUpperCase()))]=n.slice(t.length+1).trim(),e}),{}):"href"===n||"src"===n?r(t,e,n):(t.match(ze)&&(t=t.slice(1,t.length-1)),"true"===t||"false"!==t&&t)}(e,a,s,n.sanitizer);"string"==typeof l&&(Ie.test(l)||Me.test(l))&&(t[c]=o(l.trim()))}else"style"!==r&&(t[te[r]||r]=!0);return t}),{}):null}n.overrides=n.overrides||{},n.sanitizer=n.sanitizer||Pn,n.slugify=n.slugify||En,n.namedCodesToUnicode=n.namedCodesToUnicode?k({},re,n.namedCodesToUnicode):re,n.createElement=n.createElement||t.createElement;const a=[],s={},c={[x]:{match:zn(ce),order:1,parse(e,n,t){const[,r,o]=e[0].replace(le,"").match(ue);return{alert:r,children:n(o,t)}},render(e,t,o){const i={key:o.key};return e.alert&&(i.className="markdown-alert-"+n.slugify(e.alert.toLowerCase(),En),e.children.unshift({attrs:{},children:[{type:Z,text:e.alert}],noInnerParse:!0,type:O,tag:"header"})),r("blockquote",i,t(e.children,o))}},[C]:{match:$n(de),order:1,parse:qn,render:(e,n,t)=>r("br",{key:t.key})},[E]:{match:zn(pe),order:1,parse:qn,render:(e,n,t)=>r("hr",{key:t.key})},[S]:{match:zn(fe),order:0,parse:e=>({lang:void 0,text:e[0].replace(/^ {4}/gm,"").replace(/\n+$/,"")}),render:(e,n,t)=>r("pre",{key:t.key},r("code",k({},e.attrs,{className:e.lang?`lang-${e.lang}`:""}),e.text))},[I]:{match:zn(he),order:0,parse:e=>({attrs:i("code",e[3]||""),lang:e[2]||void 0,text:e[4],type:S})},[N]:{match:Mn(me),order:3,parse:e=>({text:e[2]}),render:(e,n,t)=>r("code",{key:t.key},e.text)},[T]:{match:zn(be),order:0,parse:e=>(a.push({footnote:e[2],identifier:e[1]}),{}),render:Wn},[A]:{match:An(ve),order:1,parse:e=>({target:`#${n.slugify(e[1],En)}`,text:e[1]}),render:(e,t,o)=>r("a",{key:o.key,href:n.sanitizer(e.target,"a","href")},r("sup",{key:o.key},e.text))},[M]:{match:An(xe),order:1,parse:e=>({completed:"x"===e[1].toLowerCase()}),render:(e,n,t)=>r("input",{checked:e.completed,key:t.key,readOnly:!0,type:"checkbox"})},[z]:{match:zn(n.enforceAtxHeadings?Ee:Ce),order:1,parse:(e,t,r)=>({children:Ln(t,e[2],r),id:n.slugify(e[2],En),level:e[1].length}),render:(e,n,t)=>r(`h${e.level}`,{id:e.id,key:t.key},n(e.children,t))},[$]:{match:zn(Se),order:0,parse:(e,n,t)=>({children:Ln(n,e[1],t),level:"="===e[2]?1:2,type:z})},[O]:{match:$n(Ie),order:1,parse(e,n,t){const[,r]=e[3].match(en),o=new RegExp(`^${r}`,"gm"),a=e[3].replace(o,""),s=(c=a,Cn.some((e=>e.test(c)))?Rn:Ln);var c;const l=e[1].toLowerCase(),u=-1!==oe.indexOf(l),d=(u?l:e[1]).trim(),p={attrs:i(d,e[2]),noInnerParse:u,tag:d};return t.inAnchor=t.inAnchor||"a"===l,u?p.text=e[3]:p.children=s(n,a,t),t.inAnchor=!1,p},render:(e,n,t)=>r(e.tag,k({key:t.key},e.attrs),e.text||(e.children?n(e.children,t):""))},[j]:{match:$n(Me),order:1,parse(e){const n=e[1].trim();return{attrs:i(n,e[2]||""),tag:n}},render:(e,n,t)=>r(e.tag,k({},e.attrs,{key:t.key}))},[P]:{match:$n(Te),order:1,parse:()=>({}),render:Wn},[L]:{match:Mn(wn),order:1,parse:e=>({alt:e[1],target:jn(e[2]),title:e[3]}),render:(e,t,o)=>r("img",{key:o.key,alt:e.alt||void 0,title:e.title||void 0,src:n.sanitizer(e.target,"img","src")})},[F]:{match:An(kn),order:3,parse:(e,n,t)=>({children:Fn(n,e[1],t),target:jn(e[2]),title:e[3]}),render:(e,t,o)=>r("a",{key:o.key,href:n.sanitizer(e.target,"a","href"),title:e.title},t(e.children,o))},[R]:{match:An(Pe),order:0,parse:e=>({children:[{text:e[1],type:Z}],target:e[1],type:F})},[D]:{match:(e,t)=>t.inAnchor||n.disableAutoLink?null:An($e)(e,t),order:0,parse:e=>({children:[{text:e[1],type:Z}],target:e[1],title:void 0,type:F})},[q]:{match:An(Oe),order:0,parse(e){let n=e[1],t=e[1];return ae.test(t)||(t="mailto:"+t),{children:[{text:n.replace("mailto:",""),type:Z}],target:t,type:F}}},[H]:vn(r,1),[ee]:vn(r,2),[W]:{match:zn(ge),order:3,parse:qn,render:()=>"\n"},[B]:{match:On,order:3,parse:Dn,render:(e,n,t)=>r("p",{key:t.key},n(e.children,t))},[U]:{match:An(Fe),order:0,parse:e=>(s[e[1]]={target:e[2],title:e[4]},{}),render:Wn},[_]:{match:Mn(Re),order:0,parse:e=>({alt:e[1]||void 0,ref:e[2]}),render:(e,t,o)=>s[e.ref]?r("img",{key:o.key,alt:e.alt,src:n.sanitizer(s[e.ref].target,"img","src"),title:s[e.ref].title}):null},[V]:{match:An(De),order:0,parse:(e,n,t)=>({children:n(e[1],t),fallbackChildren:e[0],ref:e[2]}),render:(e,t,o)=>s[e.ref]?r("a",{key:o.key,href:n.sanitizer(s[e.ref].target,"a","href"),title:s[e.ref].title},t(e.children,o)):r("span",{key:o.key},e.fallbackChildren)},[G]:{match:zn(Le),order:1,parse:Nn,render(e,n,t){const o=e;return r("table",{key:t.key},r("thead",null,r("tr",null,o.header.map((function(e,i){return r("th",{key:i,style:Tn(o,i)},n(e,t))})))),r("tbody",null,o.cells.map((function(e,i){return r("tr",{key:i},e.map((function(e,i){return r("td",{key:i,style:Tn(o,i)},n(e,t))})))}))))}},[Z]:{match:$n(Ke),order:4,parse:e=>({text:e[0].replace(Ne,((e,t)=>n.namedCodesToUnicode[t]?n.namedCodesToUnicode[t]:e))}),render:e=>e.text},[J]:{match:Mn(Ge),order:2,parse:(e,n,t)=>({children:n(e[2],t)}),render:(e,n,t)=>r("strong",{key:t.key},n(e.children,t))},[X]:{match:Mn(Ze),order:3,parse:(e,n,t)=>({children:n(e[2],t)}),render:(e,n,t)=>r("em",{key:t.key},n(e.children,t))},[Y]:{match:Mn(Ye),order:1,parse:e=>({text:e[1],type:Z})},[K]:{match:Mn(Je),order:3,parse:Dn,render:(e,n,t)=>r("mark",{key:t.key},n(e.children,t))},[Q]:{match:Mn(Xe),order:3,parse:Dn,render:(e,n,t)=>r("del",{key:t.key},n(e.children,t))}};!0===n.disableParsingRawHTML&&(delete c[O],delete c[j]);const l=function(e){let n=Object.keys(e);function t(r,o){let i=[];for(o.prevCapture=o.prevCapture||"";r;){let a=0;for(;a<n.length;){const s=n[a],c=e[s],l=c.match(r,o);if(l){const e=l[0];o.prevCapture+=e,r=r.substring(e.length);const n=c.parse(l,t,o);null==n.type&&(n.type=s),i.push(n);break}a++}}return o.prevCapture="",i}return n.sort((function(n,t){let r=e[n].order,o=e[t].order;return r!==o?r-o:n<t?-1:1})),function(e,n){return t(function(e){return e.replace(ye,"\n").replace(ke,"").replace(We," ")}(e),n)}}(c),u=(d=function(e,n){return function(t,r,o){const i=e[t.type].render;return n?n((()=>i(t,r,o)),t,r,o):i(t,r,o)}}(c,n.renderRule),function e(n,t={}){if(Array.isArray(n)){const r=t.key,o=[];let i=!1;for(let r=0;r<n.length;r++){t.key=r;const a=e(n[r],t),s="string"==typeof a;s&&i?o[o.length-1]+=a:null!==a&&o.push(a),i=s}return t.key=r,o}return d(n,e,t)});var d;const p=o(e);return a.length?r("div",null,p,r("footer",{key:"footer"},a.map((function(e){return r("div",{id:n.slugify(e.identifier,En),key:e.identifier},e.identifier,u(l(e.footnote,{inline:!0})))})))):p}var _n=e=>{let{children:n="",options:r}=e,o=function(e,n){if(null==e)return{};var t,r,o={},i=Object.keys(e);for(r=0;r<i.length;r++)n.indexOf(t=i[r])>=0||(o[t]=e[t]);return o}(e,w);return t.cloneElement(Un(n,r),o)},Vn=function(t){var i=t.message,a=t.botConfig,s=r(null);return o((function(){s.current&&s.current.scrollIntoView({behavior:"smooth"})}),[i]),n("div",{ref:s,className:"message-wrapper ".concat("user"===i.author?"user":"ai"),children:e("div",{className:"message ".concat("user"===i.author?"user":"ai"," "),style:{background:"user"===i.author?a.primaryColor:"primaryColor",color:"user"===i.author?a.secondaryColor:""},children:[i.isFetching&&n("div",{className:"loader"}),n(_n,{className:"loading"===i.status?"shine":"",children:i.message})]})})};!function(e,n){void 0===n&&(n={});var t=n.insertAt;if("undefined"!=typeof document){var r=document.head||document.getElementsByTagName("head")[0],o=document.createElement("style");o.type="text/css","top"===t&&r.firstChild?r.insertBefore(o,r.firstChild):r.appendChild(o),o.styleSheet?o.styleSheet.cssText=e:o.appendChild(document.createTextNode(e))}}("* {\n font-family: sans-serif;\n}\n.ai-chat-container {\n position: fixed;\n bottom: 1rem;\n right: 1rem;\n z-index: 50;\n}\n\n.help-button {\n cursor: pointer;\n background-color: black;\n color: #000000;\n border-width: 0;\n padding: 0.8rem 0.8rem;\n color: white;\n border-radius: 9999px;\n transition: background-color 0.3s;\n}\n\n.chat-window {\n display: flex;\n flex-direction: column;\n height: 35rem;\n width: 25rem;\n background-color: white;\n border-radius: 0.5rem;\n box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);\n}\n\n.chat-header {\n display: flex;\n align-items: center;\n justify-content: space-between;\n background-color: black;\n height: 3.5rem;\n padding: 0 0.5rem;\n border-top-left-radius: 0.5rem;\n border-top-right-radius: 0.5rem;\n color: white;\n}\n\n.cancel {\n cursor: pointer;\n color: white;\n border-width: 0;\n background: transparent;\n}\n\n.messages-container {\n flex: 1;\n overflow-y: auto;\n padding: 1rem;\n scroll-behavior: smooth;\n}\n\n.messages-container::-webkit-scrollbar {\n width: 6px;\n}\n\n.messages-container::-webkit-scrollbar-track {\n background: transparent;\n}\n\n.messages-container::-webkit-scrollbar-thumb {\n background-color: rgba(0, 0, 0, 0.2);\n border-radius: 3px;\n}\n\n.message-wrapper {\n display: flex;\n margin-bottom: 1rem;\n}\n\n.message-wrapper.user {\n justify-content: flex-end;\n}\n\n.message-wrapper.ai {\n justify-content: flex-start;\n}\n\n.message {\n max-width: 80%;\n padding: 0.75rem;\n border-radius: 0.5rem;\n font-size: 1rem;\n line-height: 1.4;\n letter-spacing: 0ch;\n}\n\n.message.user {\n background-color: #000000;\n color: white;\n}\n\n.message.ai {\n background-color: #f3f4f6;\n color: #1f2937;\n}\n\n.welcome-message {\n padding: 0.2rem 1rem;\n font-size: 1rem;\n font-weight: 500;\n color: #0d0d0d52;\n}\n\n.welcome-message span {\n font-weight: 600;\n color: #adedd5;\n}\n\n.chat-input-container {\n border-top: 1px solid #e5e7eb;\n padding: 1rem;\n}\n\n.chat-form {\n display: flex;\n gap: 0.5rem;\n}\n\n.chat-input {\n flex: 1;\n padding: 0.75rem;\n border: 1px solid #1e1e1e18;\n border-radius: 0.5rem;\n font-size: 0.875rem;\n transition: border-color 0.3s;\n}\n\n.chat-input:focus {\n outline: none;\n box-shadow: 0 0 0 2px rgba(173, 237, 213, 0.2);\n}\n\n.chat-input:disabled {\n background-color: #f3f4f6;\n cursor: not-allowed;\n}\n\n.send-button {\n border-width: 0;\n color: white;\n padding: 0.5rem 0.75rem;\n border-radius: 0.5rem;\n transition: background-color 0.3s;\n cursor: pointer;\n}\n\n.send-button:disabled {\n opacity: 0.7;\n cursor: not-allowed;\n}\n\n.loader {\n width: 20px;\n aspect-ratio: 2;\n --_g: no-repeat radial-gradient(circle closest-side, #000 90%, #0000);\n background: var(--_g) 0% 50%, var(--_g) 50% 50%, var(--_g) 100% 50%;\n background-size: calc(100% / 3) 50%;\n animation: l3 1s infinite linear;\n}\n@keyframes l3 {\n 20% {\n background-position: 0% 0%, 50% 50%, 100% 50%;\n }\n 40% {\n background-position: 0% 100%, 50% 0%, 100% 50%;\n }\n 60% {\n background-position: 0% 50%, 50% 100%, 100% 0%;\n }\n 80% {\n background-position: 0% 50%, 50% 50%, 100% 100%;\n }\n}\n\n.shine {\n font-size: 0.8rem;\n animation: blink 2s linear infinite;\n}\n\n@keyframes blink {\n 0% {\n opacity: 0.4;\n }\n 50% {\n opacity: 0.8;\n }\n 100% {\n opacity: 0.4;\n }\n}\n");var Gn=function(t){var a=t.environment,l=void 0===a?"prod":a,u=t.apiKey,k=t.botId,w=t.contextVariables,x=i(!1),C=x[0],E=x[1],S=i([]),I=S[0],N=S[1],T=i(!1),A=T[0],M=T[1],z=i(""),$=z[0],O=z[1],P=i(""),j=P[0],L=P[1],F=i(),R=F[0],D=F[1],q=function(e){return{sendMessage:function(n){return s(void 0,[n],void 0,(function(n){var t,r,o,i,a,s,l,u,d,p,b,v,k,w,x,C,E,S,I=n.environment,N=n.modelEndpointId,T=n.input,A=n.setMessages,M=n.sessionId,z=n.setInput,$=n.setIsInputDisabled,O=n.setSessionId,P=n.moreAddedAgents,j=n.fulfillmentPrompt,L=n.contextVariables,F=n.botId;return c(this,(function(n){switch(n.label){case 0:if(!T)return[2];f(A,{message:T,author:"user",isFetching:!1,id:Date.now().toString(),status:"complete"}),f(A,{author:"bot",isFetching:!0,status:"loading"}),$(!0),z(""),n.label=1;case 1:return n.trys.push([1,8,9,10]),(t=M)?[3,3]:[4,y(e,I,F,L)];case 2:r=n.sent(),O(t=r),n.label=3;case 3:return o={endpointId:N,query:T,pluginIds:P,responseMode:"stream",debugMode:"on",reasoningMode:"low",modelConfigs:{frequencyPenalty:0,fulfillmentPrompt:j,presencePenalty:0,stopTokens:[],temperature:.3,topP:.9}},i=g(I),[4,fetch("".concat(i,"/chat/v1/sessions/").concat(t,"/query"),{method:"POST",headers:{"Content-Type":"application/json","x-bot-id":F,apikey:e},body:JSON.stringify(o)})];case 4:a=n.sent(),s=null===(S=a.body)||void 0===S?void 0:S.getReader(),l=new TextDecoder("utf-8"),n.label=5;case 5:return[4,s.read()];case 6:if(u=n.sent(),d=u.done,p=u.value,d)return[3,7];for(b=l.decode(p),v=b.split("\n").map((function(e){return e.replace(/^data:/,"").trim()})).filter((function(e){return"event:message"!==e&&"[DONE]"!==e&&""!==e})).map((function(e){return JSON.parse(e)})),k=function(e){"statusLog"===e.eventType&&"Fulfillment completed"!==e.currentStatusLog.statusMessage?m(A,{message:h[e.currentStatusLog.statusMessage],isFetching:!1,status:"loading"}):"fulfillment"===e.eventType&&m(A,(function(n){return{message:"loading"===(null==n?void 0:n.status)?e.answer:n.message+e.answer,status:"streaming",isFetching:!1}}))},w=0,x=v;w<x.length;w++)C=x[w],k(C);return[3,5];case 7:return[3,10];case 8:return E=n.sent(),console.error("Error sending message:",E),m(A,{message:"An error occurred while processing your request.",status:"error",isFetching:!1}),[3,10];case 9:return $(!1),[7];case 10:return[2]}}))}))}}}(u).sendMessage,W=r(null),H=function(e){W.current&&!W.current.contains(e.target)&&E(!1)};return o((function(){s(void 0,void 0,void 0,(function(){var e;return c(this,(function(n){switch(n.label){case 0:return[4,b(u,k,l)];case 1:return e=n.sent(),D(e),0===I.length&&N([{message:e.introductoryMessage,author:"bot",isFetching:!1,id:Date.now().toString(),status:"complete"}]),[2]}}))}))}),[]),o((function(){return C?(document.body.style.overflow="hidden",document.addEventListener("mousedown",H),function(){document.body.style.overflow="",document.removeEventListener("mousedown",H)}):(document.body.style.overflow="",void document.removeEventListener("mousedown",H))}),[C]),R?n("div",{className:"ai-chat-container",children:C?e("div",{ref:W,className:"chat-window",children:[e("div",{className:"chat-header",style:{background:R.primaryColor},children:[n("img",{src:R.logoUrl,style:{height:"2rem",paddingLeft:"0.5rem"},alt:"Bot Logo"}),n("button",{onClick:function(){return E(!1)},className:"cancel",children:n(d,{color:R.secondaryColor})})]}),n("div",{className:"messages-container",children:I.map((function(e){return n(Vn,{message:e,botConfig:R},e.id)}))}),n(v,{input:$,setInput:O,isInputDisabled:A,botConfig:R,onSubmit:function(e){return s(void 0,void 0,void 0,(function(){return c(this,(function(n){switch(n.label){case 0:return e.preventDefault(),$.trim()?[4,q({reasoningMode:null==R?void 0:R.reasoningMode,input:$,sessionId:j,setMessages:N,messages:I,setInput:O,setIsInputDisabled:M,setSessionId:L,moreAddedAgents:null==R?void 0:R.chatPluginIds,fulfillmentPrompt:null==R?void 0:R.fulfillmentPrompt,modelEndpointId:null==R?void 0:R.modelEndpointId,environment:l,botId:k,contextVariables:w})]:[2];case 1:return n.sent(),O(""),[2]}}))}))}})]}):n("button",{onClick:function(){return E(!0)},className:"help-button",style:{background:R.primaryColor,color:R.secondaryColor},children:n(p,{color:R.secondaryColor})})}):null};export{Gn as OnDemandChatBot};
//# sourceMappingURL=index.mjs.map