@brainbase-labs/chat-widget 0.1.12 → 0.1.13
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/embed/index.js +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.js +67 -67
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("react"),e=require("react/jsx-runtime"),ee="bb-chat-";function se(s){try{const t=sessionStorage.getItem(`${ee}${s}`);return t?JSON.parse(t):null}catch{return null}}function Y(s,t){try{sessionStorage.setItem(`${ee}${s}`,JSON.stringify(t))}catch{}}function X(s){try{sessionStorage.removeItem(`${ee}${s}`)}catch{}}function oe(s){const{config:t,apiClient:n,mockMode:r,onSessionStart:u,onSessionEnd:g,onMessage:c,onError:m}=s,[l,o]=i.useState([]),[x,L]=i.useState([]),[E,T]=i.useState(!1),[W,V]=i.useState(null),[b,_]=i.useState(null),$=i.useRef(0),F=i.useRef(!1);i.useEffect(()=>{if(F.current)return;F.current=!0;const p=se(t.embedId);p&&p.status==="active"&&(_(p.sessionId),o(p.messages),L(p.toolCalls),$.current=p.startTime)},[t.embedId]),i.useEffect(()=>{b&&l.length>0&&Y(t.embedId,{sessionId:b,deploymentId:t.deploymentId,workerId:t.workerId,flowId:t.flowId,startTime:$.current,messages:l,toolCalls:x,status:"active"})},[b,l,x,t]);const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("react"),e=require("react/jsx-runtime"),ee="bb-chat-";function se(s){try{const t=sessionStorage.getItem(`${ee}${s}`);return t?JSON.parse(t):null}catch{return null}}function Y(s,t){try{sessionStorage.setItem(`${ee}${s}`,JSON.stringify(t))}catch{}}function X(s){try{sessionStorage.removeItem(`${ee}${s}`)}catch{}}function oe(s){const{config:t,apiClient:n,mockMode:r,onSessionStart:u,onSessionEnd:g,onMessage:c,onError:m}=s,[l,o]=i.useState([]),[x,L]=i.useState([]),[E,T]=i.useState(!1),[W,V]=i.useState(null),[b,_]=i.useState(null),$=i.useRef(0),F=i.useRef(!1);i.useEffect(()=>{if(F.current)return;F.current=!0;const p=se(t.embedId);p&&p.status==="active"&&(_(p.sessionId),o(p.messages),L(p.toolCalls),$.current=p.startTime)},[t.embedId]),i.useEffect(()=>{b&&l.length>0&&Y(t.embedId,{sessionId:b,deploymentId:t.deploymentId,workerId:t.workerId,flowId:t.flowId,startTime:$.current,messages:l,toolCalls:x,status:"active"})},[b,l,x,t]);const q=i.useCallback(async()=>($.current=Date.now(),_(null),o([]),L([]),X(t.embedId),""),[t.embedId]),D=i.useCallback((p,k,j)=>{switch(p.type){case"session":{const d=p.data;d.session_id&&(j(d.session_id),d.is_new&&($.current=Date.now(),u==null||u(d.session_id)));break}case"message":{const d=p.data;d.content&&o(h=>h.map(a=>a.id===k?{...a,content:d.content,status:"streaming"}:a));break}case"tool_call":{const d=p.data;L(h=>{const a=h.findIndex(f=>f.name===d.function&&f.status==="pending");return a!==-1&&d.result!==void 0?h.map((f,N)=>N===a?{...f,result:d.result,status:"completed"}:f):a===-1?[...h,{id:`tc-${Date.now()}-${Math.random().toString(36).slice(2)}`,name:d.function,arguments:d.args??{},status:"pending",timestamp:Date.now()}]:h}),d.content&&o(h=>h.map(a=>a.id===k?{...a,content:d.content,status:"streaming"}:a));break}case"waiting":break;case"done":{o(d=>d.map(h=>h.id===k?{...h,status:"sent"}:h));break}case"completed":{o(d=>d.map(h=>h.id===k?{...h,status:"sent"}:h)),b&&Y(t.embedId,{sessionId:b,deploymentId:t.deploymentId,workerId:t.workerId,flowId:t.flowId,startTime:$.current,messages:l,toolCalls:x,status:"completed"});break}case"error":{const d=p.data;o(h=>h.map(a=>a.id===k?{...a,status:"error",content:d.error??"An error occurred"}:a));break}}},[t,b,l,x,u]),S=i.useCallback(async(p,k,j)=>{const d=p.getReader(),h=new TextDecoder;let a="";try{for(;;){const{done:f,value:N}=await d.read();if(f)break;for(a+=h.decode(N,{stream:!0});a.includes(`
|
|
2
2
|
|
|
3
3
|
`);){const[C,P]=a.split(`
|
|
4
4
|
|
|
5
5
|
`,2);a=P;for(const te of C.split(`
|
|
6
|
-
`))if(te.startsWith("data: "))try{const me=JSON.parse(te.slice(6));D(me,k,j)}catch{}}}}finally{d.releaseLock()}},[D]),J=i.useCallback(async p=>{if(!p.trim())return;const k={id:`user-${Date.now()}`,role:"user",content:p,timestamp:Date.now(),status:"sent"};o(a=>[...a,k]),c==null||c(k);const j=`assistant-${Date.now()}`,d={id:j,role:"assistant",content:"",timestamp:Date.now(),status:"streaming"};o(a=>[...a,d]),T(!0),V(null);const h=a=>{_(a)};try{if(r){const a=n.sendMessage(p);for await(const f of a)D(f,j,h)}else{const a=await n.sendMessage({embedId:t.embedId,message:p,sessionId:b??void 0});await S(a,j,h)}o(a=>a.map(f=>f.id===j&&f.status==="streaming"?{...f,status:"sent"}:f))}catch(a){const f=a instanceof Error?a:new Error("Failed to send message");V(f),m==null||m(f),o(N=>N.map(C=>C.id===j?{...C,status:"error",content:"Failed to get response"}:C))}finally{T(!1)}},[b,n,r,t.embedId,D,S,c,m]),q=i.useCallback(async()=>{if(!b)return;const p={sessionId:b,deploymentId:t.deploymentId,workerId:t.workerId,flowId:t.flowId,startTime:$.current,messages:l,toolCalls:x,status:"completed"};g==null||g(p),X(t.embedId),_(null),o([]),L([])},[b,t,l,x,g]),A=i.useCallback(()=>{o([]),L([])},[]);return{messages:l,toolCalls:x,isLoading:E,error:W,sessionId:b,sendMessage:J,endSession:q,clearMessages:A,startNewSession:U}}const pe="https://chat-embed-deployment.onrender.com";function ne(s=pe){return{async getDeploymentConfig(t){const n=await fetch(`${s}/chat/config/${t}`);if(!n.ok)throw new Error(`Failed to fetch deployment config: ${n.status}`);const r=await n.json();return{embedId:r.embedId,deploymentId:"",workerId:"",flowId:"",welcomeMessage:r.welcomeMessage,agentName:r.agentName,agentLogoUrl:r.agentLogoUrl,primaryColor:r.primaryColor,styling:r.styling}},async sendMessage(t){const n=await fetch(`${s}/chat/message`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({embed_id:t.embedId,message:t.message,session_id:t.sessionId||void 0,metadata:t.metadata})});if(!n.ok){const r=await n.text();throw new Error(`Failed to send message: ${n.status} - ${r}`)}if(!n.body)throw new Error("No response stream available");return n.body}}}const he={embedId:"mock-embed-id",deploymentId:"mock-deployment-id",workerId:"mock-worker-id",flowId:"mock-flow-id",agentName:"AI Assistant",agentLogoUrl:void 0,primaryColor:"#1a1a2e",styling:{}},_e=[{trigger:/hello|hi|hey/i,response:"Hello! I'm a demo AI assistant. How can I help you today?",delay:500},{trigger:/weather/i,response:"I'd check the weather for you, but I'm in mock mode! In production, I could use a weather API.",delay:800,toolCalls:[{name:"get_weather",arguments:{location:"San Francisco"},result:{temperature:72,condition:"sunny"}}]},{trigger:/help|support/i,response:"I'm here to help! You can ask me questions, and I'll do my best to assist you. In production, I'd be connected to your agent with full capabilities.",delay:600},{trigger:/pricing|cost|price/i,response:"For pricing information, I'd typically check our database or connect you with the sales team. This is a mock response demonstrating how I'd handle pricing questions.",delay:700,toolCalls:[{name:"lookup_pricing",arguments:{plan:"all"},result:{plans:[{name:"Starter",price:"$29/mo"},{name:"Pro",price:"$99/mo"}]}}]},{trigger:/.*/,response:"I'm running in mock mode. This is a simulated response to demonstrate the chat UI. In production, I'd be connected to your agent!",delay:1e3}];function R(s){return new Promise(t=>setTimeout(t,s))}function re(s,t){const n={...he,...s},r=t??_e,u=`mock-session-${Date.now()}`;return{async getDeploymentConfig(g){return await R(300),n},async*sendMessage(g){yield{type:"session",data:{session_id:u,is_new:!0},timestamp:Date.now()};const c=r.find(o=>typeof o.trigger=="string"?g.toLowerCase().includes(o.trigger.toLowerCase()):o.trigger.test(g))??r[r.length-1];if(await R(c.delay??500),c.toolCalls)for(const o of c.toolCalls)yield{type:"tool_call",data:{function:o.name,args:o.arguments},timestamp:Date.now()},await R(300),yield{type:"tool_call",data:{function:o.name,result:o.result},timestamp:Date.now()},await R(200);const m=c.response.split(" ");let l="";for(const o of m)l+=(l?" ":"")+o,yield{type:"message",data:{content:l,role:"assistant"},timestamp:Date.now()},await R(30+Math.random()*40);yield{type:"done",data:{session_id:u},timestamp:Date.now()}}}}const O=({className:s,color:t="currentColor",cutoutColor:n="var(--bb-primary-color)"})=>e.jsxs("svg",{className:s,viewBox:"0 0 800 800",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H788C794.627 400 800 405.373 800 412V740C800 773.137 773.137 800 740 800H412C405.373 800 400 794.627 400 788V412Z",fill:t}),e.jsx("circle",{cx:"400",cy:"400",r:"400",fill:t}),e.jsx("path",{d:"M0 60C0 26.8629 26.8629 0 60 0H397.614C398.932 0 400 1.06811 400 2.38569V2.38569C400 221.982 221.982 400 2.38569 400V400C1.06811 400 0 398.932 0 397.614V60Z",fill:t}),e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H738C744.627 400 750 405.373 750 412V725C750 738.807 738.807 750 725 750H412C405.373 750 400 744.627 400 738V412Z",fill:t}),e.jsx("circle",{cx:"400",cy:"400",r:"350",fill:t}),e.jsx("path",{d:"M50 75C50 61.1929 61.1929 50 75 50H388C394.627 50 400 55.3726 400 62V388C400 394.627 394.627 400 388 400H62C55.3726 400 50 394.627 50 388V75Z",fill:t}),e.jsx("rect",{x:"399.919",y:"209",width:"270",height:"270",rx:"12",transform:"rotate(45 399.919 209)",fill:n})]}),fe="_header_1p6z5_1",xe="_compact_1p6z5_9",be="_headerBackground_1p6z5_20",ye="_headerContent_1p6z5_59",we="_topRow_1p6z5_64",je="_agentInfo_1p6z5_79",Ce="_agentLogo_1p6z5_85",ke="_agentLogoPlaceholder_1p6z5_102",ve="_brainbaseLogo_1p6z5_123",Ie="_agentName_1p6z5_137",Ne="_actions_1p6z5_144",Le="_actionButton_1p6z5_149",$e="_welcomeText_1p6z5_183",Se="_title_1p6z5_202",Be="_subtitle_1p6z5_210",y={header:fe,compact:xe,headerBackground:be,headerContent:ye,topRow:we,agentInfo:je,agentLogo:Ce,agentLogoPlaceholder:ke,brainbaseLogo:ve,agentName:Ie,actions:Ne,actionButton:Le,welcomeText:$e,title:Se,subtitle:Be},ae=({agentName:s="AI Assistant",agentLogoUrl:t,welcomeTitle:n,welcomeSubtitle:r,onClose:u,onNewChatRequest:g,showNewChatButton:c=!1,compact:m=!1})=>{const l=n||"Hello there.",o=r!==void 0?r:n?void 0:"How can we help?";return e.jsxs("div",{className:`${y.header} ${m?y.compact:""}`,children:[e.jsx("div",{className:y.headerBackground}),e.jsxs("div",{className:y.headerContent,children:[e.jsxs("div",{className:y.topRow,children:[e.jsxs("div",{className:y.agentInfo,children:[t?e.jsx("img",{src:t,alt:s,className:y.agentLogo}):e.jsx("div",{className:y.agentLogoPlaceholder,children:e.jsx(O,{className:y.brainbaseLogo,color:"white",cutoutColor:"var(--bb-primary-color)"})}),e.jsx("span",{className:y.agentName,children:s})]}),e.jsxs("div",{className:y.actions,children:[c&&g&&e.jsx("button",{className:y.actionButton,onClick:g,"aria-label":"Start new chat",type:"button",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M12 5V19M5 12H19",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})}),u&&e.jsx("button",{className:y.actionButton,onClick:u,"aria-label":"Close chat",type:"button",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M6 18L18 6M6 6L18 18",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]})]}),!m&&e.jsxs("div",{className:y.welcomeText,children:[e.jsx("h1",{className:y.title,children:l}),o&&e.jsx("p",{className:y.subtitle,children:o})]})]})]})},Me="_messageWrapper_c86gj_1",Te="_user_c86gj_19",We="_assistant_c86gj_24",De="_avatar_c86gj_28",Pe="_avatarPlaceholder_c86gj_43",He="_brainbaseLogo_c86gj_53",Ee="_messageBubble_c86gj_58",Ve="_error_c86gj_76",Ae="_content_c86gj_81",Re="_cursor_c86gj_88",ze="_errorIndicator_c86gj_107",v={messageWrapper:Me,user:Te,assistant:We,avatar:De,avatarPlaceholder:Pe,brainbaseLogo:He,messageBubble:Ee,error:Ve,content:Ae,cursor:Re,errorIndicator:ze},ce=({message:s,agentName:t,agentLogoUrl:n})=>{const r=s.role==="user",u=s.status==="streaming",g=s.status==="error";return e.jsxs("div",{className:`${v.messageWrapper} ${r?v.user:v.assistant}`,children:[!r&&e.jsx("div",{className:v.avatar,children:n?e.jsx("img",{src:n,alt:t||"AI"}):e.jsx("div",{className:v.avatarPlaceholder,children:e.jsx(O,{className:v.brainbaseLogo,color:"white",cutoutColor:"var(--bb-primary-color)"})})}),e.jsxs("div",{className:`${v.messageBubble} ${g?v.error:""}`,role:"article","aria-label":`${r?"Your message":`${t||"AI"} says`}`,children:[e.jsxs("div",{className:v.content,children:[s.content,u&&e.jsx("span",{className:v.cursor})]}),g&&e.jsxs("div",{className:v.errorIndicator,children:[e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",children:[e.jsx("circle",{cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"2"}),e.jsx("path",{d:"M12 8V12M12 16H12.01",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})]}),e.jsx("span",{children:"Failed to send"})]})]})]})},Oe="_toolCall_1wby1_1",Fe="_iconWrapper_1wby1_24",Ue="_spinner_1wby1_33",qe="_checkIcon_1wby1_48",Ze="_errorIcon_1wby1_54",Ke="_content_1wby1_60",Ge="_label_1wby1_66",Je="_name_1wby1_73",Ye="_pending_1wby1_78",Xe="_completed_1wby1_82",Qe="_error_1wby1_54",I={toolCall:Oe,iconWrapper:Fe,spinner:Ue,checkIcon:qe,errorIcon:Ze,content:Ke,label:Ge,name:Je,pending:Ye,completed:Xe,error:Qe},ie=({toolCall:s})=>{const t=s.status==="pending"||s.status==="executing",n=s.status==="completed",r=s.status==="error",u=g=>g.replace(/_/g," ").replace(/([A-Z])/g," $1").trim().toLowerCase().replace(/^./,c=>c.toUpperCase());return e.jsxs("div",{className:`${I.toolCall} ${t?I.pending:""} ${n?I.completed:""} ${r?I.error:""}`,children:[e.jsx("div",{className:I.iconWrapper,children:t?e.jsx("div",{className:I.spinner}):n?e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",className:I.checkIcon,children:e.jsx("path",{d:"M20 6L9 17L4 12",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})}):e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",className:I.errorIcon,children:e.jsx("path",{d:"M6 18L18 6M6 6L18 18",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsxs("div",{className:I.content,children:[e.jsx("span",{className:I.label,children:t?"Running":n?"Completed":"Failed"}),e.jsx("span",{className:I.name,children:u(s.name)})]})]})},et="_wrapper_10rss_2",tt="_avatar_10rss_22",st="_avatarPlaceholder_10rss_37",ot="_brainbaseLogo_10rss_47",nt="_bubble_10rss_52",rt="_dot_10rss_63",B={wrapper:et,avatar:tt,avatarPlaceholder:st,brainbaseLogo:ot,bubble:nt,dot:rt},le=({agentName:s="AI",agentLogoUrl:t})=>e.jsxs("div",{className:B.wrapper,role:"status","aria-label":`${s} is typing`,children:[e.jsx("div",{className:B.avatar,children:t?e.jsx("img",{src:t,alt:s}):e.jsx("div",{className:B.avatarPlaceholder,children:e.jsx(O,{className:B.brainbaseLogo,color:"white",cutoutColor:"var(--bb-primary-color)"})})}),e.jsxs("div",{className:B.bubble,children:[e.jsx("span",{className:B.dot}),e.jsx("span",{className:B.dot}),e.jsx("span",{className:B.dot})]})]}),at="_messageList_241kk_1",ct="_emptyState_241kk_29",it="_emptyIcon_241kk_40",lt="_emptyText_241kk_52",Z={messageList:at,emptyState:ct,emptyIcon:it,emptyText:lt},de=({messages:s,toolCalls:t=[],isLoading:n,agentName:r,agentLogoUrl:u})=>{var l;const g=i.useRef(null),c=i.useRef(null);i.useEffect(()=>{var o;(o=c.current)==null||o.scrollIntoView({behavior:"smooth"})},[s,t,n]);const m=t.filter(o=>o.status==="pending"||o.status==="executing");return e.jsxs("div",{className:Z.messageList,ref:g,role:"log","aria-live":"polite",children:[s.length===0&&!n&&e.jsxs("div",{className:Z.emptyState,children:[e.jsx("div",{className:Z.emptyIcon,children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M8 12H8.01M12 12H12.01M16 12H16.01M21 12C21 16.4183 16.9706 20 12 20C10.4607 20 9.01172 19.6565 7.74467 19.0511L3 20L4.39499 16.28C3.51156 15.0423 3 13.5743 3 12C3 7.58172 7.02944 4 12 4C16.9706 4 21 7.58172 21 12Z",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsx("p",{className:Z.emptyText,children:"Start a conversation"})]}),s.map(o=>e.jsx(ce,{message:o,agentName:r,agentLogoUrl:u},o.id)),m.map(o=>e.jsx(ie,{toolCall:o},o.id)),n&&((l=s[s.length-1])==null?void 0:l.role)==="user"&&e.jsx(le,{agentName:r,agentLogoUrl:u}),e.jsx("div",{ref:c})]})},dt="_inputWrapper_5lgg7_1",ut="_inputContainer_5lgg7_7",gt="_textarea_5lgg7_23",mt="_sendButton_5lgg7_50",pt="_hint_5lgg7_89",z={inputWrapper:dt,inputContainer:ut,textarea:gt,sendButton:mt,hint:pt},ue=({onSend:s,disabled:t=!1,placeholder:n="Send a message..."})=>{const[r,u]=i.useState(""),g=i.useRef(null),c=i.useCallback(()=>{const o=g.current;o&&(o.style.height="auto",o.style.height=`${Math.min(o.scrollHeight,150)}px`)},[]);i.useEffect(()=>{c()},[r,c]);const m=i.useCallback(o=>{o==null||o.preventDefault();const x=r.trim();x&&!t&&(s(x),u(""),g.current&&(g.current.style.height="auto"))},[r,t,s]),l=i.useCallback(o=>{o.key==="Enter"&&!o.shiftKey&&(o.preventDefault(),m())},[m]);return e.jsxs("form",{className:z.inputWrapper,onSubmit:m,children:[e.jsxs("div",{className:z.inputContainer,children:[e.jsx("textarea",{ref:g,className:z.textarea,value:r,onChange:o=>u(o.target.value),onKeyDown:l,placeholder:n,disabled:t,rows:1,"aria-label":"Message input"}),e.jsx("button",{type:"submit",className:z.sendButton,disabled:t||!r.trim(),"aria-label":"Send message",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M22 2L11 13M22 2L15 22L11 13M22 2L2 9L11 13",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]}),e.jsxs("div",{className:z.hint,children:["Press ",e.jsx("kbd",{children:"Enter"})," to send, ",e.jsx("kbd",{children:"Shift + Enter"})," for new line"]})]})},ht="_poweredBy_9jh5q_1",_t="_logo_9jh5q_20",ft="_poweredText_9jh5q_24",xt="_text_9jh5q_36",K={poweredBy:ht,logo:_t,poweredText:ft,text:xt},bt=({className:s})=>e.jsxs("svg",{className:s,viewBox:"0 0 800 800",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H788C794.627 400 800 405.373 800 412V740C800 773.137 773.137 800 740 800H412C405.373 800 400 794.627 400 788V412Z",fill:"currentColor"}),e.jsx("circle",{cx:"400",cy:"400",r:"400",fill:"currentColor"}),e.jsx("path",{d:"M0 60C0 26.8629 26.8629 0 60 0H397.614C398.932 0 400 1.06811 400 2.38569V2.38569C400 221.982 221.982 400 2.38569 400V400C1.06811 400 0 398.932 0 397.614V60Z",fill:"currentColor"}),e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H738C744.627 400 750 405.373 750 412V725C750 738.807 738.807 750 725 750H412C405.373 750 400 744.627 400 738V412Z",fill:"currentColor"}),e.jsx("circle",{cx:"400",cy:"400",r:"350",fill:"currentColor"}),e.jsx("path",{d:"M50 75C50 61.1929 61.1929 50 75 50H388C394.627 50 400 55.3726 400 62V388C400 394.627 394.627 400 388 400H62C55.3726 400 50 394.627 50 388V75Z",fill:"currentColor"}),e.jsx("rect",{x:"399.919",y:"209",width:"270",height:"270",rx:"12",transform:"rotate(45 399.919 209)",fill:"var(--bb-surface-bg, #ffffff)"})]}),yt=({showBranding:s=!0})=>s?e.jsxs("a",{href:"https://brainbaselabs.com/",target:"_blank",rel:"noopener noreferrer",className:K.poweredBy,"aria-label":"Powered by Brainbase Labs",children:[e.jsx("span",{className:K.poweredText,children:"Powered by"}),e.jsx(bt,{className:K.logo}),e.jsx("span",{className:K.text,children:"Brainbase Labs"})]}):null,wt="_container_m2o9l_1",jt="_body_m2o9l_26",Ct="_confirmationOverlay_m2o9l_35",kt="_confirmationDialog_m2o9l_56",vt="_confirmationText_m2o9l_78",It="_confirmationButtons_m2o9l_86",Nt="_cancelButton_m2o9l_91",Lt="_confirmButton_m2o9l_92",M={container:wt,body:jt,confirmationOverlay:Ct,confirmationDialog:kt,confirmationText:vt,confirmationButtons:It,cancelButton:Nt,confirmButton:Lt},ge=({config:s,messages:t,toolCalls:n=[],isLoading:r,onSendMessage:u,onClose:g,onNewChat:c})=>{var T;const[m,l]=i.useState(!1),o=t.length>0,x=()=>{l(!0)},L=()=>{l(!1),c==null||c()},E=()=>{l(!1)};return e.jsxs("div",{className:M.container,role:"dialog","aria-label":"Chat window",children:[e.jsx(ae,{agentName:s.agentName,agentLogoUrl:s.agentLogoUrl,welcomeTitle:s.welcomeMessage||"Hello there.",welcomeSubtitle:s.welcomeMessage?void 0:"How can we help?",onClose:g,onNewChatRequest:x,showNewChatButton:o,compact:o}),e.jsxs("div",{className:M.body,children:[e.jsx(de,{messages:t,toolCalls:n,isLoading:r,agentName:s.agentName,agentLogoUrl:s.agentLogoUrl}),e.jsx(ue,{onSend:u,disabled:r,placeholder:"Ask a question..."})]}),e.jsx(yt,{showBranding:((T=s.styling)==null?void 0:T.showBranding)??!0}),m&&e.jsx("div",{className:M.confirmationOverlay,children:e.jsxs("div",{className:M.confirmationDialog,children:[e.jsx("p",{className:M.confirmationText,children:"End current chat and start a new conversation?"}),e.jsxs("div",{className:M.confirmationButtons,children:[e.jsx("button",{className:M.cancelButton,onClick:E,type:"button",children:"Cancel"}),e.jsx("button",{className:M.confirmButton,onClick:L,type:"button",children:"End Chat"})]})]})})]})},$t="_toggleButton_11dqz_1",St="_icon_11dqz_31",Bt="_agentLogo_11dqz_36",Mt="_unreadBadge_11dqz_43",G={toggleButton:$t,icon:St,agentLogo:Bt,unreadBadge:Mt},Q=({onClick:s,agentLogoUrl:t,unreadCount:n=0})=>e.jsxs("button",{className:G.toggleButton,onClick:s,"aria-label":"Open chat",type:"button",children:[t?e.jsx("img",{src:t,alt:"",className:G.agentLogo}):e.jsx("svg",{className:G.icon,viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:e.jsx("path",{d:"M8 12H8.01M12 12H12.01M16 12H16.01M21 12C21 16.4183 16.9706 20 12 20C10.4607 20 9.01172 19.6565 7.74467 19.0511L3 20L4.39499 16.28C3.51156 15.0423 3 13.5743 3 12C3 7.58172 7.02944 4 12 4C16.9706 4 21 7.58172 21 12Z",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})}),n>0&&e.jsx("span",{className:G.unreadBadge,children:n>9?"9+":n})]}),Tt="_container_kgfgt_1",Wt="_header_kgfgt_25",Dt="_logoWrapper_kgfgt_38",Pt="_logo_kgfgt_38",Ht="_closeButton_kgfgt_51",Et="_content_kgfgt_74",Vt="_iconWrapper_kgfgt_84",At="_errorIcon_kgfgt_95",Rt="_title_kgfgt_101",zt="_description_kgfgt_109",Ot="_retryButton_kgfgt_117",Ft="_footer_kgfgt_139",Ut="_poweredBy_kgfgt_146",qt="_footerLogo_kgfgt_160",w={container:Tt,header:Wt,logoWrapper:Dt,logo:Pt,closeButton:Ht,content:Et,iconWrapper:Vt,errorIcon:At,title:Rt,description:zt,retryButton:Ot,footer:Ft,poweredBy:Ut,footerLogo:qt},Zt=({errorType:s,message:t,onRetry:n,onClose:r})=>{const u=()=>{switch(s){case"not_found":return{title:"Chat Not Found",description:t||"This chat widget is not configured correctly. Please check the embed ID.",icon:e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",className:w.errorIcon,children:[e.jsx("circle",{cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"1.5"}),e.jsx("path",{d:"M12 7V13M12 16V16.5",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})]})};case"network":return{title:"Connection Error",description:t||"Unable to connect to the chat service. Please check your internet connection.",icon:e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",className:w.errorIcon,children:[e.jsx("path",{d:"M12 21C16.9706 21 21 16.9706 21 12C21 7.02944 16.9706 3 12 3C7.02944 3 3 7.02944 3 12C3 16.9706 7.02944 21 12 21Z",stroke:"currentColor",strokeWidth:"1.5"}),e.jsx("path",{d:"M8 15L16 9M8 9L16 15",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})]})};default:return{title:"Something Went Wrong",description:t||"An unexpected error occurred. Please try again later.",icon:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",className:w.errorIcon,children:e.jsx("path",{d:"M12 9V13M12 17H12.01M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})})}}},{title:g,description:c,icon:m}=u();return e.jsxs("div",{className:w.container,children:[e.jsxs("div",{className:w.header,children:[e.jsx("div",{className:w.logoWrapper,children:e.jsx(O,{className:w.logo,color:"white",cutoutColor:"var(--bb-primary-color)"})}),r&&e.jsx("button",{className:w.closeButton,onClick:r,"aria-label":"Close",type:"button",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M6 18L18 6M6 6L18 18",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})})})]}),e.jsxs("div",{className:w.content,children:[e.jsx("div",{className:w.iconWrapper,children:m}),e.jsx("h2",{className:w.title,children:g}),e.jsx("p",{className:w.description,children:c}),n&&e.jsx("button",{className:w.retryButton,onClick:n,type:"button",children:"Try Again"})]}),e.jsx("div",{className:w.footer,children:e.jsxs("a",{href:"https://brainbaselabs.com",target:"_blank",rel:"noopener noreferrer",className:w.poweredBy,children:["Powered by"," ",e.jsx(O,{className:w.footerLogo,color:"var(--bb-text-tertiary)",cutoutColor:"var(--bb-surface-bg)"}),e.jsx("span",{children:"Brainbase Labs"})]})})]})},Kt="_widget_1ehud_1",Gt="_inline_1ehud_25",H={widget:Kt,"bottom-right":"_bottom-right_1ehud_10","bottom-left":"_bottom-left_1ehud_17",inline:Gt},Jt="https://whatsapp-based-server.onrender.com",Yt=({embedId:s,apiBaseUrl:t=Jt,mockMode:n=!1,mockResponses:r,position:u="bottom-right",defaultOpen:g=!1,primaryColor:c,agentName:m,welcomeMessage:l,showBranding:o,className:x,onSessionStart:L,onSessionEnd:E,onMessage:T,onError:W})=>{const[V,b]=i.useState(g),[_,$]=i.useState(null),[F,U]=i.useState(!0),[D,S]=i.useState(null),[J,q]=i.useState(),A=i.useMemo(()=>n?re({primaryColor:c,agentName:m,welcomeMessage:l},r):ne(t),[n,c,m,l,r,t]),p=i.useCallback(async()=>{U(!0),S(null),q(void 0);try{const C=await A.getDeploymentConfig(s);$({...C,primaryColor:c??C.primaryColor,agentName:m??C.agentName,welcomeMessage:l??C.welcomeMessage,styling:{...C.styling??{},...o!==void 0?{showBranding:o}:{}}})}catch(C){const P=C instanceof Error?C:new Error("Failed to load config");P.message.includes("404")?(S("not_found"),q("The chat widget with this embed ID was not found.")):P.message.includes("Failed to fetch")||P.message.includes("network")?S("network"):S("unknown"),n&&($({embedId:s,deploymentId:"mock-deployment",workerId:"mock-worker",flowId:"mock-flow",primaryColor:c??"#1a1a2e",agentName:m??"AI Assistant",styling:o!==void 0?{showBranding:o}:void 0}),S(null)),W==null||W(P)}finally{U(!1)}},[s,n,A,c,m,l,W]);i.useEffect(()=>{p()},[p]);const k=i.useMemo(()=>_||{embedId:s,deploymentId:"",workerId:"",flowId:"",primaryColor:c,agentName:m,welcomeMessage:l,styling:o!==void 0?{showBranding:o}:void 0},[_,s,c,m,l,o]),j=oe({config:k,apiClient:A,mockMode:n,onSessionStart:L,onSessionEnd:E,onMessage:T,onError:W}),d=i.useMemo(()=>({"--bb-primary-color":(_==null?void 0:_.primaryColor)??c??"#1a1a2e"}),[_==null?void 0:_.primaryColor,c]),h=()=>{j.endSession(),j.startNewSession()},a=()=>{b(!1)},f=()=>{p()},N=u==="inline";return F?null:D&&!n?!N&&!V?e.jsx("div",{className:`${H.widget} ${H[u]} ${x??""}`,style:d,children:e.jsx(Q,{onClick:()=>b(!0),agentName:m})}):e.jsx("div",{className:`${H.widget} ${H[u]} ${x??""}`,style:d,children:e.jsx(Zt,{errorType:D,message:J,onRetry:f,onClose:N?void 0:a})}):e.jsx("div",{className:`${H.widget} ${H[u]} ${x??""}`,style:d,children:V||N?e.jsx(ge,{config:k,messages:j.messages,isLoading:j.isLoading,onSendMessage:j.sendMessage,onClose:N?void 0:a,onNewChat:h}):e.jsx(Q,{onClick:()=>b(!0),agentName:_==null?void 0:_.agentName,agentLogoUrl:_==null?void 0:_.agentLogoUrl})})};function Xt(){if(typeof crypto<"u"&&crypto.randomUUID)return`bb-${crypto.randomUUID()}`;const s=Date.now().toString(36),t=Math.random().toString(36).substring(2,15),n=Math.random().toString(36).substring(2,15);return`bb-${s}-${t}${n}`}exports.ChatContainer=ge;exports.ChatHeader=ae;exports.ChatToggleButton=Q;exports.ChatWidget=Yt;exports.Message=ce;exports.MessageInput=ue;exports.MessageList=de;exports.ToolCallDisplay=ie;exports.TypingIndicator=le;exports.clearSession=X;exports.createAPIClient=ne;exports.createMockAPIClient=re;exports.generateSessionId=Xt;exports.getStoredSession=se;exports.storeSession=Y;exports.useChat=oe;
|
|
6
|
+
`))if(te.startsWith("data: "))try{const pe=JSON.parse(te.slice(6));D(pe,k,j)}catch{}}}}finally{d.releaseLock()}},[D]),G=i.useCallback(async p=>{if(!p.trim())return;const k={id:`user-${Date.now()}`,role:"user",content:p,timestamp:Date.now(),status:"sent"};o(a=>[...a,k]),c==null||c(k);const j=`assistant-${Date.now()}`,d={id:j,role:"assistant",content:"",timestamp:Date.now(),status:"streaming"};o(a=>[...a,d]),T(!0),V(null);const h=a=>{_(a)};try{if(r){const a=n.sendMessage(p);for await(const f of a)D(f,j,h)}else{const a=await n.sendMessage({embedId:t.embedId,message:p,sessionId:b??void 0});await S(a,j,h)}o(a=>a.map(f=>f.id===j&&f.status==="streaming"?{...f,status:"sent"}:f))}catch(a){const f=a instanceof Error?a:new Error("Failed to send message");V(f),m==null||m(f),o(N=>N.map(C=>C.id===j?{...C,status:"error",content:"Failed to get response"}:C))}finally{T(!1)}},[b,n,r,t.embedId,D,S,c,m]),U=i.useCallback(async()=>{if(!b)return;const p={sessionId:b,deploymentId:t.deploymentId,workerId:t.workerId,flowId:t.flowId,startTime:$.current,messages:l,toolCalls:x,status:"completed"};g==null||g(p),X(t.embedId),_(null),o([]),L([])},[b,t,l,x,g]),A=i.useCallback(()=>{o([]),L([])},[]);return{messages:l,toolCalls:x,isLoading:E,error:W,sessionId:b,sendMessage:G,endSession:U,clearMessages:A,startNewSession:q}}const ne="https://chat-embed-deployment.onrender.com";function re(s=ne){return{async getDeploymentConfig(t){const n=await fetch(`${s}/chat/config/${t}`);if(!n.ok)throw new Error(`Failed to fetch deployment config: ${n.status}`);const r=await n.json();return{embedId:r.embedId,deploymentId:"",workerId:"",flowId:"",welcomeMessage:r.welcomeMessage,agentName:r.agentName,agentLogoUrl:r.agentLogoUrl,primaryColor:r.primaryColor,styling:r.styling}},async sendMessage(t){const n=await fetch(`${s}/chat/message`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({embed_id:t.embedId,message:t.message,session_id:t.sessionId||void 0,metadata:t.metadata})});if(!n.ok){const r=await n.text();throw new Error(`Failed to send message: ${n.status} - ${r}`)}if(!n.body)throw new Error("No response stream available");return n.body}}}const he={embedId:"mock-embed-id",deploymentId:"mock-deployment-id",workerId:"mock-worker-id",flowId:"mock-flow-id",agentName:"AI Assistant",agentLogoUrl:void 0,primaryColor:"#1a1a2e",styling:{}},_e=[{trigger:/hello|hi|hey/i,response:"Hello! I'm a demo AI assistant. How can I help you today?",delay:500},{trigger:/weather/i,response:"I'd check the weather for you, but I'm in mock mode! In production, I could use a weather API.",delay:800,toolCalls:[{name:"get_weather",arguments:{location:"San Francisco"},result:{temperature:72,condition:"sunny"}}]},{trigger:/help|support/i,response:"I'm here to help! You can ask me questions, and I'll do my best to assist you. In production, I'd be connected to your agent with full capabilities.",delay:600},{trigger:/pricing|cost|price/i,response:"For pricing information, I'd typically check our database or connect you with the sales team. This is a mock response demonstrating how I'd handle pricing questions.",delay:700,toolCalls:[{name:"lookup_pricing",arguments:{plan:"all"},result:{plans:[{name:"Starter",price:"$29/mo"},{name:"Pro",price:"$99/mo"}]}}]},{trigger:/.*/,response:"I'm running in mock mode. This is a simulated response to demonstrate the chat UI. In production, I'd be connected to your agent!",delay:1e3}];function R(s){return new Promise(t=>setTimeout(t,s))}function ae(s,t){const n={...he,...s},r=t??_e,u=`mock-session-${Date.now()}`;return{async getDeploymentConfig(g){return await R(300),n},async*sendMessage(g){yield{type:"session",data:{session_id:u,is_new:!0},timestamp:Date.now()};const c=r.find(o=>typeof o.trigger=="string"?g.toLowerCase().includes(o.trigger.toLowerCase()):o.trigger.test(g))??r[r.length-1];if(await R(c.delay??500),c.toolCalls)for(const o of c.toolCalls)yield{type:"tool_call",data:{function:o.name,args:o.arguments},timestamp:Date.now()},await R(300),yield{type:"tool_call",data:{function:o.name,result:o.result},timestamp:Date.now()},await R(200);const m=c.response.split(" ");let l="";for(const o of m)l+=(l?" ":"")+o,yield{type:"message",data:{content:l,role:"assistant"},timestamp:Date.now()},await R(30+Math.random()*40);yield{type:"done",data:{session_id:u},timestamp:Date.now()}}}}const O=({className:s,color:t="currentColor",cutoutColor:n="var(--bb-primary-color)"})=>e.jsxs("svg",{className:s,viewBox:"0 0 800 800",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H788C794.627 400 800 405.373 800 412V740C800 773.137 773.137 800 740 800H412C405.373 800 400 794.627 400 788V412Z",fill:t}),e.jsx("circle",{cx:"400",cy:"400",r:"400",fill:t}),e.jsx("path",{d:"M0 60C0 26.8629 26.8629 0 60 0H397.614C398.932 0 400 1.06811 400 2.38569V2.38569C400 221.982 221.982 400 2.38569 400V400C1.06811 400 0 398.932 0 397.614V60Z",fill:t}),e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H738C744.627 400 750 405.373 750 412V725C750 738.807 738.807 750 725 750H412C405.373 750 400 744.627 400 738V412Z",fill:t}),e.jsx("circle",{cx:"400",cy:"400",r:"350",fill:t}),e.jsx("path",{d:"M50 75C50 61.1929 61.1929 50 75 50H388C394.627 50 400 55.3726 400 62V388C400 394.627 394.627 400 388 400H62C55.3726 400 50 394.627 50 388V75Z",fill:t}),e.jsx("rect",{x:"399.919",y:"209",width:"270",height:"270",rx:"12",transform:"rotate(45 399.919 209)",fill:n})]}),fe="_header_1p6z5_1",xe="_compact_1p6z5_9",be="_headerBackground_1p6z5_20",ye="_headerContent_1p6z5_59",we="_topRow_1p6z5_64",je="_agentInfo_1p6z5_79",Ce="_agentLogo_1p6z5_85",ke="_agentLogoPlaceholder_1p6z5_102",ve="_brainbaseLogo_1p6z5_123",Ie="_agentName_1p6z5_137",Ne="_actions_1p6z5_144",Le="_actionButton_1p6z5_149",$e="_welcomeText_1p6z5_183",Se="_title_1p6z5_202",Be="_subtitle_1p6z5_210",y={header:fe,compact:xe,headerBackground:be,headerContent:ye,topRow:we,agentInfo:je,agentLogo:Ce,agentLogoPlaceholder:ke,brainbaseLogo:ve,agentName:Ie,actions:Ne,actionButton:Le,welcomeText:$e,title:Se,subtitle:Be},ce=({agentName:s="AI Assistant",agentLogoUrl:t,welcomeTitle:n,welcomeSubtitle:r,onClose:u,onNewChatRequest:g,showNewChatButton:c=!1,compact:m=!1})=>{const l=n||"Hello there.",o=r!==void 0?r:n?void 0:"How can we help?";return e.jsxs("div",{className:`${y.header} ${m?y.compact:""}`,children:[e.jsx("div",{className:y.headerBackground}),e.jsxs("div",{className:y.headerContent,children:[e.jsxs("div",{className:y.topRow,children:[e.jsxs("div",{className:y.agentInfo,children:[t?e.jsx("img",{src:t,alt:s,className:y.agentLogo}):e.jsx("div",{className:y.agentLogoPlaceholder,children:e.jsx(O,{className:y.brainbaseLogo,color:"white",cutoutColor:"var(--bb-primary-color)"})}),e.jsx("span",{className:y.agentName,children:s})]}),e.jsxs("div",{className:y.actions,children:[c&&g&&e.jsx("button",{className:y.actionButton,onClick:g,"aria-label":"Start new chat",type:"button",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M12 5V19M5 12H19",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})}),u&&e.jsx("button",{className:y.actionButton,onClick:u,"aria-label":"Close chat",type:"button",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M6 18L18 6M6 6L18 18",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]})]}),!m&&e.jsxs("div",{className:y.welcomeText,children:[e.jsx("h1",{className:y.title,children:l}),o&&e.jsx("p",{className:y.subtitle,children:o})]})]})]})},Me="_messageWrapper_c86gj_1",Te="_user_c86gj_19",We="_assistant_c86gj_24",De="_avatar_c86gj_28",Pe="_avatarPlaceholder_c86gj_43",He="_brainbaseLogo_c86gj_53",Ee="_messageBubble_c86gj_58",Ve="_error_c86gj_76",Ae="_content_c86gj_81",Re="_cursor_c86gj_88",ze="_errorIndicator_c86gj_107",v={messageWrapper:Me,user:Te,assistant:We,avatar:De,avatarPlaceholder:Pe,brainbaseLogo:He,messageBubble:Ee,error:Ve,content:Ae,cursor:Re,errorIndicator:ze},ie=({message:s,agentName:t,agentLogoUrl:n})=>{const r=s.role==="user",u=s.status==="streaming",g=s.status==="error";return e.jsxs("div",{className:`${v.messageWrapper} ${r?v.user:v.assistant}`,children:[!r&&e.jsx("div",{className:v.avatar,children:n?e.jsx("img",{src:n,alt:t||"AI"}):e.jsx("div",{className:v.avatarPlaceholder,children:e.jsx(O,{className:v.brainbaseLogo,color:"white",cutoutColor:"var(--bb-primary-color)"})})}),e.jsxs("div",{className:`${v.messageBubble} ${g?v.error:""}`,role:"article","aria-label":`${r?"Your message":`${t||"AI"} says`}`,children:[e.jsxs("div",{className:v.content,children:[s.content,u&&e.jsx("span",{className:v.cursor})]}),g&&e.jsxs("div",{className:v.errorIndicator,children:[e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",children:[e.jsx("circle",{cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"2"}),e.jsx("path",{d:"M12 8V12M12 16H12.01",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})]}),e.jsx("span",{children:"Failed to send"})]})]})]})},Oe="_toolCall_1wby1_1",Fe="_iconWrapper_1wby1_24",qe="_spinner_1wby1_33",Ue="_checkIcon_1wby1_48",Ze="_errorIcon_1wby1_54",Ke="_content_1wby1_60",Je="_label_1wby1_66",Ge="_name_1wby1_73",Ye="_pending_1wby1_78",Xe="_completed_1wby1_82",Qe="_error_1wby1_54",I={toolCall:Oe,iconWrapper:Fe,spinner:qe,checkIcon:Ue,errorIcon:Ze,content:Ke,label:Je,name:Ge,pending:Ye,completed:Xe,error:Qe},le=({toolCall:s})=>{const t=s.status==="pending"||s.status==="executing",n=s.status==="completed",r=s.status==="error",u=g=>g.replace(/_/g," ").replace(/([A-Z])/g," $1").trim().toLowerCase().replace(/^./,c=>c.toUpperCase());return e.jsxs("div",{className:`${I.toolCall} ${t?I.pending:""} ${n?I.completed:""} ${r?I.error:""}`,children:[e.jsx("div",{className:I.iconWrapper,children:t?e.jsx("div",{className:I.spinner}):n?e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",className:I.checkIcon,children:e.jsx("path",{d:"M20 6L9 17L4 12",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})}):e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",className:I.errorIcon,children:e.jsx("path",{d:"M6 18L18 6M6 6L18 18",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsxs("div",{className:I.content,children:[e.jsx("span",{className:I.label,children:t?"Running":n?"Completed":"Failed"}),e.jsx("span",{className:I.name,children:u(s.name)})]})]})},et="_wrapper_10rss_2",tt="_avatar_10rss_22",st="_avatarPlaceholder_10rss_37",ot="_brainbaseLogo_10rss_47",nt="_bubble_10rss_52",rt="_dot_10rss_63",B={wrapper:et,avatar:tt,avatarPlaceholder:st,brainbaseLogo:ot,bubble:nt,dot:rt},de=({agentName:s="AI",agentLogoUrl:t})=>e.jsxs("div",{className:B.wrapper,role:"status","aria-label":`${s} is typing`,children:[e.jsx("div",{className:B.avatar,children:t?e.jsx("img",{src:t,alt:s}):e.jsx("div",{className:B.avatarPlaceholder,children:e.jsx(O,{className:B.brainbaseLogo,color:"white",cutoutColor:"var(--bb-primary-color)"})})}),e.jsxs("div",{className:B.bubble,children:[e.jsx("span",{className:B.dot}),e.jsx("span",{className:B.dot}),e.jsx("span",{className:B.dot})]})]}),at="_messageList_241kk_1",ct="_emptyState_241kk_29",it="_emptyIcon_241kk_40",lt="_emptyText_241kk_52",Z={messageList:at,emptyState:ct,emptyIcon:it,emptyText:lt},ue=({messages:s,toolCalls:t=[],isLoading:n,agentName:r,agentLogoUrl:u})=>{var l;const g=i.useRef(null),c=i.useRef(null);i.useEffect(()=>{var o;(o=c.current)==null||o.scrollIntoView({behavior:"smooth"})},[s,t,n]);const m=t.filter(o=>o.status==="pending"||o.status==="executing");return e.jsxs("div",{className:Z.messageList,ref:g,role:"log","aria-live":"polite",children:[s.length===0&&!n&&e.jsxs("div",{className:Z.emptyState,children:[e.jsx("div",{className:Z.emptyIcon,children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M8 12H8.01M12 12H12.01M16 12H16.01M21 12C21 16.4183 16.9706 20 12 20C10.4607 20 9.01172 19.6565 7.74467 19.0511L3 20L4.39499 16.28C3.51156 15.0423 3 13.5743 3 12C3 7.58172 7.02944 4 12 4C16.9706 4 21 7.58172 21 12Z",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})}),e.jsx("p",{className:Z.emptyText,children:"Start a conversation"})]}),s.map(o=>e.jsx(ie,{message:o,agentName:r,agentLogoUrl:u},o.id)),m.map(o=>e.jsx(le,{toolCall:o},o.id)),n&&((l=s[s.length-1])==null?void 0:l.role)==="user"&&e.jsx(de,{agentName:r,agentLogoUrl:u}),e.jsx("div",{ref:c})]})},dt="_inputWrapper_5lgg7_1",ut="_inputContainer_5lgg7_7",gt="_textarea_5lgg7_23",mt="_sendButton_5lgg7_50",pt="_hint_5lgg7_89",z={inputWrapper:dt,inputContainer:ut,textarea:gt,sendButton:mt,hint:pt},ge=({onSend:s,disabled:t=!1,placeholder:n="Send a message..."})=>{const[r,u]=i.useState(""),g=i.useRef(null),c=i.useCallback(()=>{const o=g.current;o&&(o.style.height="auto",o.style.height=`${Math.min(o.scrollHeight,150)}px`)},[]);i.useEffect(()=>{c()},[r,c]);const m=i.useCallback(o=>{o==null||o.preventDefault();const x=r.trim();x&&!t&&(s(x),u(""),g.current&&(g.current.style.height="auto"))},[r,t,s]),l=i.useCallback(o=>{o.key==="Enter"&&!o.shiftKey&&(o.preventDefault(),m())},[m]);return e.jsxs("form",{className:z.inputWrapper,onSubmit:m,children:[e.jsxs("div",{className:z.inputContainer,children:[e.jsx("textarea",{ref:g,className:z.textarea,value:r,onChange:o=>u(o.target.value),onKeyDown:l,placeholder:n,disabled:t,rows:1,"aria-label":"Message input"}),e.jsx("button",{type:"submit",className:z.sendButton,disabled:t||!r.trim(),"aria-label":"Send message",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M22 2L11 13M22 2L15 22L11 13M22 2L2 9L11 13",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})})})]}),e.jsxs("div",{className:z.hint,children:["Press ",e.jsx("kbd",{children:"Enter"})," to send, ",e.jsx("kbd",{children:"Shift + Enter"})," for new line"]})]})},ht="_poweredBy_9jh5q_1",_t="_logo_9jh5q_20",ft="_poweredText_9jh5q_24",xt="_text_9jh5q_36",K={poweredBy:ht,logo:_t,poweredText:ft,text:xt},bt=({className:s})=>e.jsxs("svg",{className:s,viewBox:"0 0 800 800",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:[e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H788C794.627 400 800 405.373 800 412V740C800 773.137 773.137 800 740 800H412C405.373 800 400 794.627 400 788V412Z",fill:"currentColor"}),e.jsx("circle",{cx:"400",cy:"400",r:"400",fill:"currentColor"}),e.jsx("path",{d:"M0 60C0 26.8629 26.8629 0 60 0H397.614C398.932 0 400 1.06811 400 2.38569V2.38569C400 221.982 221.982 400 2.38569 400V400C1.06811 400 0 398.932 0 397.614V60Z",fill:"currentColor"}),e.jsx("path",{d:"M400 412C400 405.373 405.373 400 412 400H738C744.627 400 750 405.373 750 412V725C750 738.807 738.807 750 725 750H412C405.373 750 400 744.627 400 738V412Z",fill:"currentColor"}),e.jsx("circle",{cx:"400",cy:"400",r:"350",fill:"currentColor"}),e.jsx("path",{d:"M50 75C50 61.1929 61.1929 50 75 50H388C394.627 50 400 55.3726 400 62V388C400 394.627 394.627 400 388 400H62C55.3726 400 50 394.627 50 388V75Z",fill:"currentColor"}),e.jsx("rect",{x:"399.919",y:"209",width:"270",height:"270",rx:"12",transform:"rotate(45 399.919 209)",fill:"var(--bb-surface-bg, #ffffff)"})]}),yt=({showBranding:s=!0})=>s?e.jsxs("a",{href:"https://brainbaselabs.com/",target:"_blank",rel:"noopener noreferrer",className:K.poweredBy,"aria-label":"Powered by Brainbase Labs",children:[e.jsx("span",{className:K.poweredText,children:"Powered by"}),e.jsx(bt,{className:K.logo}),e.jsx("span",{className:K.text,children:"Brainbase Labs"})]}):null,wt="_container_m2o9l_1",jt="_body_m2o9l_26",Ct="_confirmationOverlay_m2o9l_35",kt="_confirmationDialog_m2o9l_56",vt="_confirmationText_m2o9l_78",It="_confirmationButtons_m2o9l_86",Nt="_cancelButton_m2o9l_91",Lt="_confirmButton_m2o9l_92",M={container:wt,body:jt,confirmationOverlay:Ct,confirmationDialog:kt,confirmationText:vt,confirmationButtons:It,cancelButton:Nt,confirmButton:Lt},me=({config:s,messages:t,toolCalls:n=[],isLoading:r,onSendMessage:u,onClose:g,onNewChat:c})=>{var T;const[m,l]=i.useState(!1),o=t.length>0,x=()=>{l(!0)},L=()=>{l(!1),c==null||c()},E=()=>{l(!1)};return e.jsxs("div",{className:M.container,role:"dialog","aria-label":"Chat window",children:[e.jsx(ce,{agentName:s.agentName,agentLogoUrl:s.agentLogoUrl,welcomeTitle:s.welcomeMessage||"Hello there.",welcomeSubtitle:s.welcomeMessage?void 0:"How can we help?",onClose:g,onNewChatRequest:x,showNewChatButton:o,compact:o}),e.jsxs("div",{className:M.body,children:[e.jsx(ue,{messages:t,toolCalls:n,isLoading:r,agentName:s.agentName,agentLogoUrl:s.agentLogoUrl}),e.jsx(ge,{onSend:u,disabled:r,placeholder:"Ask a question..."})]}),e.jsx(yt,{showBranding:((T=s.styling)==null?void 0:T.showBranding)??!0}),m&&e.jsx("div",{className:M.confirmationOverlay,children:e.jsxs("div",{className:M.confirmationDialog,children:[e.jsx("p",{className:M.confirmationText,children:"End current chat and start a new conversation?"}),e.jsxs("div",{className:M.confirmationButtons,children:[e.jsx("button",{className:M.cancelButton,onClick:E,type:"button",children:"Cancel"}),e.jsx("button",{className:M.confirmButton,onClick:L,type:"button",children:"End Chat"})]})]})})]})},$t="_toggleButton_11dqz_1",St="_icon_11dqz_31",Bt="_agentLogo_11dqz_36",Mt="_unreadBadge_11dqz_43",J={toggleButton:$t,icon:St,agentLogo:Bt,unreadBadge:Mt},Q=({onClick:s,agentLogoUrl:t,unreadCount:n=0})=>e.jsxs("button",{className:J.toggleButton,onClick:s,"aria-label":"Open chat",type:"button",children:[t?e.jsx("img",{src:t,alt:"",className:J.agentLogo}):e.jsx("svg",{className:J.icon,viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:e.jsx("path",{d:"M8 12H8.01M12 12H12.01M16 12H16.01M21 12C21 16.4183 16.9706 20 12 20C10.4607 20 9.01172 19.6565 7.74467 19.0511L3 20L4.39499 16.28C3.51156 15.0423 3 13.5743 3 12C3 7.58172 7.02944 4 12 4C16.9706 4 21 7.58172 21 12Z",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round"})}),n>0&&e.jsx("span",{className:J.unreadBadge,children:n>9?"9+":n})]}),Tt="_container_kgfgt_1",Wt="_header_kgfgt_25",Dt="_logoWrapper_kgfgt_38",Pt="_logo_kgfgt_38",Ht="_closeButton_kgfgt_51",Et="_content_kgfgt_74",Vt="_iconWrapper_kgfgt_84",At="_errorIcon_kgfgt_95",Rt="_title_kgfgt_101",zt="_description_kgfgt_109",Ot="_retryButton_kgfgt_117",Ft="_footer_kgfgt_139",qt="_poweredBy_kgfgt_146",Ut="_footerLogo_kgfgt_160",w={container:Tt,header:Wt,logoWrapper:Dt,logo:Pt,closeButton:Ht,content:Et,iconWrapper:Vt,errorIcon:At,title:Rt,description:zt,retryButton:Ot,footer:Ft,poweredBy:qt,footerLogo:Ut},Zt=({errorType:s,message:t,onRetry:n,onClose:r})=>{const u=()=>{switch(s){case"not_found":return{title:"Chat Not Found",description:t||"This chat widget is not configured correctly. Please check the embed ID.",icon:e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",className:w.errorIcon,children:[e.jsx("circle",{cx:"12",cy:"12",r:"10",stroke:"currentColor",strokeWidth:"1.5"}),e.jsx("path",{d:"M12 7V13M12 16V16.5",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})]})};case"network":return{title:"Connection Error",description:t||"Unable to connect to the chat service. Please check your internet connection.",icon:e.jsxs("svg",{viewBox:"0 0 24 24",fill:"none",className:w.errorIcon,children:[e.jsx("path",{d:"M12 21C16.9706 21 21 16.9706 21 12C21 7.02944 16.9706 3 12 3C7.02944 3 3 7.02944 3 12C3 16.9706 7.02944 21 12 21Z",stroke:"currentColor",strokeWidth:"1.5"}),e.jsx("path",{d:"M8 15L16 9M8 9L16 15",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})]})};default:return{title:"Something Went Wrong",description:t||"An unexpected error occurred. Please try again later.",icon:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",className:w.errorIcon,children:e.jsx("path",{d:"M12 9V13M12 17H12.01M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})})}}},{title:g,description:c,icon:m}=u();return e.jsxs("div",{className:w.container,children:[e.jsxs("div",{className:w.header,children:[e.jsx("div",{className:w.logoWrapper,children:e.jsx(O,{className:w.logo,color:"white",cutoutColor:"var(--bb-primary-color)"})}),r&&e.jsx("button",{className:w.closeButton,onClick:r,"aria-label":"Close",type:"button",children:e.jsx("svg",{viewBox:"0 0 24 24",fill:"none",children:e.jsx("path",{d:"M6 18L18 6M6 6L18 18",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round"})})})]}),e.jsxs("div",{className:w.content,children:[e.jsx("div",{className:w.iconWrapper,children:m}),e.jsx("h2",{className:w.title,children:g}),e.jsx("p",{className:w.description,children:c}),n&&e.jsx("button",{className:w.retryButton,onClick:n,type:"button",children:"Try Again"})]}),e.jsx("div",{className:w.footer,children:e.jsxs("a",{href:"https://brainbaselabs.com",target:"_blank",rel:"noopener noreferrer",className:w.poweredBy,children:["Powered by"," ",e.jsx(O,{className:w.footerLogo,color:"var(--bb-text-tertiary)",cutoutColor:"var(--bb-surface-bg)"}),e.jsx("span",{children:"Brainbase Labs"})]})})]})},Kt="_widget_1ehud_1",Jt="_inline_1ehud_25",H={widget:Kt,"bottom-right":"_bottom-right_1ehud_10","bottom-left":"_bottom-left_1ehud_17",inline:Jt},Gt=({embedId:s,apiBaseUrl:t=ne,mockMode:n=!1,mockResponses:r,position:u="bottom-right",defaultOpen:g=!1,primaryColor:c,agentName:m,welcomeMessage:l,showBranding:o,className:x,onSessionStart:L,onSessionEnd:E,onMessage:T,onError:W})=>{const[V,b]=i.useState(g),[_,$]=i.useState(null),[F,q]=i.useState(!0),[D,S]=i.useState(null),[G,U]=i.useState(),A=i.useMemo(()=>n?ae({primaryColor:c,agentName:m,welcomeMessage:l},r):re(t),[n,c,m,l,r,t]),p=i.useCallback(async()=>{q(!0),S(null),U(void 0);try{const C=await A.getDeploymentConfig(s);$({...C,primaryColor:c??C.primaryColor,agentName:m??C.agentName,welcomeMessage:l??C.welcomeMessage,styling:{...C.styling??{},...o!==void 0?{showBranding:o}:{}}})}catch(C){const P=C instanceof Error?C:new Error("Failed to load config");P.message.includes("404")?(S("not_found"),U("The chat widget with this embed ID was not found.")):P.message.includes("Failed to fetch")||P.message.includes("network")?S("network"):S("unknown"),n&&($({embedId:s,deploymentId:"mock-deployment",workerId:"mock-worker",flowId:"mock-flow",primaryColor:c??"#1a1a2e",agentName:m??"AI Assistant",styling:o!==void 0?{showBranding:o}:void 0}),S(null)),W==null||W(P)}finally{q(!1)}},[s,n,A,c,m,l,W]);i.useEffect(()=>{p()},[p]);const k=i.useMemo(()=>_||{embedId:s,deploymentId:"",workerId:"",flowId:"",primaryColor:c,agentName:m,welcomeMessage:l,styling:o!==void 0?{showBranding:o}:void 0},[_,s,c,m,l,o]),j=oe({config:k,apiClient:A,mockMode:n,onSessionStart:L,onSessionEnd:E,onMessage:T,onError:W}),d=i.useMemo(()=>({"--bb-primary-color":(_==null?void 0:_.primaryColor)??c??"#1a1a2e"}),[_==null?void 0:_.primaryColor,c]),h=()=>{j.endSession(),j.startNewSession()},a=()=>{b(!1)},f=()=>{p()},N=u==="inline";return F?null:D&&!n?!N&&!V?e.jsx("div",{className:`${H.widget} ${H[u]} ${x??""}`,style:d,children:e.jsx(Q,{onClick:()=>b(!0),agentName:m})}):e.jsx("div",{className:`${H.widget} ${H[u]} ${x??""}`,style:d,children:e.jsx(Zt,{errorType:D,message:G,onRetry:f,onClose:N?void 0:a})}):e.jsx("div",{className:`${H.widget} ${H[u]} ${x??""}`,style:d,children:V||N?e.jsx(me,{config:k,messages:j.messages,isLoading:j.isLoading,onSendMessage:j.sendMessage,onClose:N?void 0:a,onNewChat:h}):e.jsx(Q,{onClick:()=>b(!0),agentName:_==null?void 0:_.agentName,agentLogoUrl:_==null?void 0:_.agentLogoUrl})})};function Yt(){if(typeof crypto<"u"&&crypto.randomUUID)return`bb-${crypto.randomUUID()}`;const s=Date.now().toString(36),t=Math.random().toString(36).substring(2,15),n=Math.random().toString(36).substring(2,15);return`bb-${s}-${t}${n}`}exports.ChatContainer=me;exports.ChatHeader=ce;exports.ChatToggleButton=Q;exports.ChatWidget=Gt;exports.Message=ie;exports.MessageInput=ge;exports.MessageList=ue;exports.ToolCallDisplay=le;exports.TypingIndicator=de;exports.clearSession=X;exports.createAPIClient=re;exports.createMockAPIClient=ae;exports.generateSessionId=Yt;exports.getStoredSession=se;exports.storeSession=Y;exports.useChat=oe;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useState as N, useRef as U, useEffect as Z, useCallback as x, useMemo as te } from "react";
|
|
2
2
|
import { jsxs as i, jsx as e } from "react/jsx-runtime";
|
|
3
3
|
const oe = "bb-chat-";
|
|
4
|
-
function
|
|
4
|
+
function le(o) {
|
|
5
5
|
try {
|
|
6
6
|
const t = sessionStorage.getItem(`${oe}${o}`);
|
|
7
7
|
return t ? JSON.parse(t) : null;
|
|
@@ -24,7 +24,7 @@ function re(o) {
|
|
|
24
24
|
} catch {
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
function
|
|
27
|
+
function de(o) {
|
|
28
28
|
const {
|
|
29
29
|
config: t,
|
|
30
30
|
apiClient: s,
|
|
@@ -37,7 +37,7 @@ function le(o) {
|
|
|
37
37
|
Z(() => {
|
|
38
38
|
if (K.current) return;
|
|
39
39
|
K.current = !0;
|
|
40
|
-
const p =
|
|
40
|
+
const p = le(t.embedId);
|
|
41
41
|
p && p.status === "active" && (_(p.sessionId), n(p.messages), M(p.toolCalls), S.current = p.startTime);
|
|
42
42
|
}, [t.embedId]), Z(() => {
|
|
43
43
|
b && l.length > 0 && se(t.embedId, {
|
|
@@ -51,7 +51,7 @@ function le(o) {
|
|
|
51
51
|
status: "active"
|
|
52
52
|
});
|
|
53
53
|
}, [b, l, y, t]);
|
|
54
|
-
const
|
|
54
|
+
const J = x(async () => (S.current = Date.now(), _(null), n([]), M([]), re(t.embedId), ""), [t.embedId]), j = x(
|
|
55
55
|
(p, I, k) => {
|
|
56
56
|
switch (p.type) {
|
|
57
57
|
case "session": {
|
|
@@ -143,16 +143,16 @@ function le(o) {
|
|
|
143
143
|
for (a += h.decode(B, { stream: !0 }); a.includes(`
|
|
144
144
|
|
|
145
145
|
`); ) {
|
|
146
|
-
const [v,
|
|
146
|
+
const [v, V] = a.split(`
|
|
147
147
|
|
|
148
148
|
`, 2);
|
|
149
|
-
a =
|
|
149
|
+
a = V;
|
|
150
150
|
for (const ne of v.split(`
|
|
151
151
|
`))
|
|
152
152
|
if (ne.startsWith("data: "))
|
|
153
153
|
try {
|
|
154
|
-
const
|
|
155
|
-
|
|
154
|
+
const ie = JSON.parse(ne.slice(6));
|
|
155
|
+
j(ie, I, k);
|
|
156
156
|
} catch {
|
|
157
157
|
}
|
|
158
158
|
}
|
|
@@ -161,7 +161,7 @@ function le(o) {
|
|
|
161
161
|
d.releaseLock();
|
|
162
162
|
}
|
|
163
163
|
},
|
|
164
|
-
[
|
|
164
|
+
[j]
|
|
165
165
|
), ee = x(
|
|
166
166
|
async (p) => {
|
|
167
167
|
if (!p.trim()) return;
|
|
@@ -188,7 +188,7 @@ function le(o) {
|
|
|
188
188
|
if (r) {
|
|
189
189
|
const a = s.sendMessage(p);
|
|
190
190
|
for await (const f of a)
|
|
191
|
-
|
|
191
|
+
j(f, k, h);
|
|
192
192
|
} else {
|
|
193
193
|
const a = await s.sendMessage({
|
|
194
194
|
embedId: t.embedId,
|
|
@@ -218,12 +218,12 @@ function le(o) {
|
|
|
218
218
|
s,
|
|
219
219
|
r,
|
|
220
220
|
t.embedId,
|
|
221
|
-
|
|
221
|
+
j,
|
|
222
222
|
T,
|
|
223
223
|
c,
|
|
224
224
|
g
|
|
225
225
|
]
|
|
226
|
-
),
|
|
226
|
+
), G = x(async () => {
|
|
227
227
|
if (!b) return;
|
|
228
228
|
const p = {
|
|
229
229
|
sessionId: b,
|
|
@@ -242,7 +242,7 @@ function le(o) {
|
|
|
242
242
|
l,
|
|
243
243
|
y,
|
|
244
244
|
m
|
|
245
|
-
]),
|
|
245
|
+
]), O = x(() => {
|
|
246
246
|
n([]), M([]);
|
|
247
247
|
}, []);
|
|
248
248
|
return {
|
|
@@ -252,13 +252,13 @@ function le(o) {
|
|
|
252
252
|
error: P,
|
|
253
253
|
sessionId: b,
|
|
254
254
|
sendMessage: ee,
|
|
255
|
-
endSession:
|
|
256
|
-
clearMessages:
|
|
257
|
-
startNewSession:
|
|
255
|
+
endSession: G,
|
|
256
|
+
clearMessages: O,
|
|
257
|
+
startNewSession: J
|
|
258
258
|
};
|
|
259
259
|
}
|
|
260
|
-
const
|
|
261
|
-
function ue(o =
|
|
260
|
+
const ce = "https://chat-embed-deployment.onrender.com";
|
|
261
|
+
function ue(o = ce) {
|
|
262
262
|
return {
|
|
263
263
|
async getDeploymentConfig(t) {
|
|
264
264
|
const s = await fetch(`${o}/chat/config/${t}`);
|
|
@@ -356,14 +356,14 @@ const me = {
|
|
|
356
356
|
delay: 1e3
|
|
357
357
|
}
|
|
358
358
|
];
|
|
359
|
-
function
|
|
359
|
+
function F(o) {
|
|
360
360
|
return new Promise((t) => setTimeout(t, o));
|
|
361
361
|
}
|
|
362
362
|
function pe(o, t) {
|
|
363
363
|
const s = { ...me, ...o }, r = t ?? ge, u = `mock-session-${Date.now()}`;
|
|
364
364
|
return {
|
|
365
365
|
async getDeploymentConfig(m) {
|
|
366
|
-
return await
|
|
366
|
+
return await F(300), s;
|
|
367
367
|
},
|
|
368
368
|
async *sendMessage(m) {
|
|
369
369
|
yield {
|
|
@@ -374,7 +374,7 @@ function pe(o, t) {
|
|
|
374
374
|
const c = r.find(
|
|
375
375
|
(n) => typeof n.trigger == "string" ? m.toLowerCase().includes(n.trigger.toLowerCase()) : n.trigger.test(m)
|
|
376
376
|
) ?? r[r.length - 1];
|
|
377
|
-
if (await
|
|
377
|
+
if (await F(c.delay ?? 500), c.toolCalls)
|
|
378
378
|
for (const n of c.toolCalls)
|
|
379
379
|
yield {
|
|
380
380
|
type: "tool_call",
|
|
@@ -383,14 +383,14 @@ function pe(o, t) {
|
|
|
383
383
|
args: n.arguments
|
|
384
384
|
},
|
|
385
385
|
timestamp: Date.now()
|
|
386
|
-
}, await
|
|
386
|
+
}, await F(300), yield {
|
|
387
387
|
type: "tool_call",
|
|
388
388
|
data: {
|
|
389
389
|
function: n.name,
|
|
390
390
|
result: n.result
|
|
391
391
|
},
|
|
392
392
|
timestamp: Date.now()
|
|
393
|
-
}, await
|
|
393
|
+
}, await F(200);
|
|
394
394
|
const g = c.response.split(" ");
|
|
395
395
|
let l = "";
|
|
396
396
|
for (const n of g)
|
|
@@ -398,7 +398,7 @@ function pe(o, t) {
|
|
|
398
398
|
type: "message",
|
|
399
399
|
data: { content: l, role: "assistant" },
|
|
400
400
|
timestamp: Date.now()
|
|
401
|
-
}, await
|
|
401
|
+
}, await F(30 + Math.random() * 40);
|
|
402
402
|
yield {
|
|
403
403
|
type: "done",
|
|
404
404
|
data: { session_id: u },
|
|
@@ -559,19 +559,19 @@ const q = ({
|
|
|
559
559
|
] })
|
|
560
560
|
] })
|
|
561
561
|
] });
|
|
562
|
-
}, Se = "_messageWrapper_c86gj_1", Te = "_user_c86gj_19", We = "_assistant_c86gj_24", De = "_avatar_c86gj_28", He = "_avatarPlaceholder_c86gj_43", Pe = "_brainbaseLogo_c86gj_53",
|
|
562
|
+
}, Se = "_messageWrapper_c86gj_1", Te = "_user_c86gj_19", We = "_assistant_c86gj_24", De = "_avatar_c86gj_28", He = "_avatarPlaceholder_c86gj_43", Pe = "_brainbaseLogo_c86gj_53", je = "_messageBubble_c86gj_58", Ve = "_error_c86gj_76", Ee = "_content_c86gj_81", Ae = "_cursor_c86gj_88", ze = "_errorIndicator_c86gj_107", L = {
|
|
563
563
|
messageWrapper: Se,
|
|
564
564
|
user: Te,
|
|
565
565
|
assistant: We,
|
|
566
566
|
avatar: De,
|
|
567
567
|
avatarPlaceholder: He,
|
|
568
568
|
brainbaseLogo: Pe,
|
|
569
|
-
messageBubble:
|
|
570
|
-
error:
|
|
571
|
-
content:
|
|
569
|
+
messageBubble: je,
|
|
570
|
+
error: Ve,
|
|
571
|
+
content: Ee,
|
|
572
572
|
cursor: Ae,
|
|
573
573
|
errorIndicator: ze
|
|
574
|
-
},
|
|
574
|
+
}, Oe = ({
|
|
575
575
|
message: o,
|
|
576
576
|
agentName: t,
|
|
577
577
|
agentLogoUrl: s
|
|
@@ -614,15 +614,15 @@ const q = ({
|
|
|
614
614
|
]
|
|
615
615
|
}
|
|
616
616
|
);
|
|
617
|
-
},
|
|
618
|
-
toolCall:
|
|
617
|
+
}, Fe = "_toolCall_1wby1_1", Re = "_iconWrapper_1wby1_24", Ue = "_spinner_1wby1_33", Ze = "_checkIcon_1wby1_48", qe = "_errorIcon_1wby1_54", Ke = "_content_1wby1_60", Je = "_label_1wby1_66", Ge = "_name_1wby1_73", Ye = "_pending_1wby1_78", Xe = "_completed_1wby1_82", Qe = "_error_1wby1_54", $ = {
|
|
618
|
+
toolCall: Fe,
|
|
619
619
|
iconWrapper: Re,
|
|
620
620
|
spinner: Ue,
|
|
621
621
|
checkIcon: Ze,
|
|
622
622
|
errorIcon: qe,
|
|
623
623
|
content: Ke,
|
|
624
|
-
label:
|
|
625
|
-
name:
|
|
624
|
+
label: Je,
|
|
625
|
+
name: Ge,
|
|
626
626
|
pending: Ye,
|
|
627
627
|
completed: Xe,
|
|
628
628
|
error: Qe
|
|
@@ -719,7 +719,7 @@ const q = ({
|
|
|
719
719
|
/* @__PURE__ */ e("p", { className: Y.emptyText, children: "Start a conversation" })
|
|
720
720
|
] }),
|
|
721
721
|
o.map((n) => /* @__PURE__ */ e(
|
|
722
|
-
|
|
722
|
+
Oe,
|
|
723
723
|
{
|
|
724
724
|
message: n,
|
|
725
725
|
agentName: r,
|
|
@@ -962,11 +962,11 @@ const q = ({
|
|
|
962
962
|
] })
|
|
963
963
|
] }) })
|
|
964
964
|
] });
|
|
965
|
-
}, Dt = "_toggleButton_11dqz_1", Ht = "_icon_11dqz_31", Pt = "_agentLogo_11dqz_36",
|
|
965
|
+
}, Dt = "_toggleButton_11dqz_1", Ht = "_icon_11dqz_31", Pt = "_agentLogo_11dqz_36", jt = "_unreadBadge_11dqz_43", Q = {
|
|
966
966
|
toggleButton: Dt,
|
|
967
967
|
icon: Ht,
|
|
968
968
|
agentLogo: Pt,
|
|
969
|
-
unreadBadge:
|
|
969
|
+
unreadBadge: jt
|
|
970
970
|
}, ae = ({
|
|
971
971
|
onClick: o,
|
|
972
972
|
agentLogoUrl: t,
|
|
@@ -1001,20 +1001,20 @@ const q = ({
|
|
|
1001
1001
|
s > 0 && /* @__PURE__ */ e("span", { className: Q.unreadBadge, children: s > 9 ? "9+" : s })
|
|
1002
1002
|
]
|
|
1003
1003
|
}
|
|
1004
|
-
),
|
|
1005
|
-
container:
|
|
1006
|
-
header:
|
|
1004
|
+
), Vt = "_container_kgfgt_1", Et = "_header_kgfgt_25", At = "_logoWrapper_kgfgt_38", zt = "_logo_kgfgt_38", Ot = "_closeButton_kgfgt_51", Ft = "_content_kgfgt_74", Rt = "_iconWrapper_kgfgt_84", Ut = "_errorIcon_kgfgt_95", Zt = "_title_kgfgt_101", qt = "_description_kgfgt_109", Kt = "_retryButton_kgfgt_117", Jt = "_footer_kgfgt_139", Gt = "_poweredBy_kgfgt_146", Yt = "_footerLogo_kgfgt_160", C = {
|
|
1005
|
+
container: Vt,
|
|
1006
|
+
header: Et,
|
|
1007
1007
|
logoWrapper: At,
|
|
1008
1008
|
logo: zt,
|
|
1009
|
-
closeButton:
|
|
1010
|
-
content:
|
|
1009
|
+
closeButton: Ot,
|
|
1010
|
+
content: Ft,
|
|
1011
1011
|
iconWrapper: Rt,
|
|
1012
1012
|
errorIcon: Ut,
|
|
1013
1013
|
title: Zt,
|
|
1014
1014
|
description: qt,
|
|
1015
1015
|
retryButton: Kt,
|
|
1016
|
-
footer:
|
|
1017
|
-
poweredBy:
|
|
1016
|
+
footer: Jt,
|
|
1017
|
+
poweredBy: Gt,
|
|
1018
1018
|
footerLogo: Yt
|
|
1019
1019
|
}, Xt = ({
|
|
1020
1020
|
errorType: o,
|
|
@@ -1148,14 +1148,14 @@ const q = ({
|
|
|
1148
1148
|
}
|
|
1149
1149
|
) })
|
|
1150
1150
|
] });
|
|
1151
|
-
}, Qt = "_widget_1ehud_1", eo = "_inline_1ehud_25",
|
|
1151
|
+
}, Qt = "_widget_1ehud_1", eo = "_inline_1ehud_25", E = {
|
|
1152
1152
|
widget: Qt,
|
|
1153
1153
|
"bottom-right": "_bottom-right_1ehud_10",
|
|
1154
1154
|
"bottom-left": "_bottom-left_1ehud_17",
|
|
1155
1155
|
inline: eo
|
|
1156
|
-
},
|
|
1156
|
+
}, no = ({
|
|
1157
1157
|
embedId: o,
|
|
1158
|
-
apiBaseUrl: t =
|
|
1158
|
+
apiBaseUrl: t = ce,
|
|
1159
1159
|
mockMode: s = !1,
|
|
1160
1160
|
mockResponses: r,
|
|
1161
1161
|
position: u = "bottom-right",
|
|
@@ -1170,13 +1170,13 @@ const q = ({
|
|
|
1170
1170
|
onMessage: H,
|
|
1171
1171
|
onError: P
|
|
1172
1172
|
}) => {
|
|
1173
|
-
const [z, b] = N(m), [_, S] = N(null), [K,
|
|
1173
|
+
const [z, b] = N(m), [_, S] = N(null), [K, J] = N(!0), [j, T] = N(null), [ee, G] = N(), O = te(() => s ? pe(
|
|
1174
1174
|
{ primaryColor: c, agentName: g, welcomeMessage: l },
|
|
1175
1175
|
r
|
|
1176
1176
|
) : ue(t), [s, c, g, l, r, t]), p = x(async () => {
|
|
1177
|
-
|
|
1177
|
+
J(!0), T(null), G(void 0);
|
|
1178
1178
|
try {
|
|
1179
|
-
const v = await
|
|
1179
|
+
const v = await O.getDeploymentConfig(o);
|
|
1180
1180
|
S({
|
|
1181
1181
|
...v,
|
|
1182
1182
|
// Allow prop overrides
|
|
@@ -1189,8 +1189,8 @@ const q = ({
|
|
|
1189
1189
|
}
|
|
1190
1190
|
});
|
|
1191
1191
|
} catch (v) {
|
|
1192
|
-
const
|
|
1193
|
-
|
|
1192
|
+
const V = v instanceof Error ? v : new Error("Failed to load config");
|
|
1193
|
+
V.message.includes("404") ? (T("not_found"), G("The chat widget with this embed ID was not found.")) : V.message.includes("Failed to fetch") || V.message.includes("network") ? T("network") : T("unknown"), s && (S({
|
|
1194
1194
|
embedId: o,
|
|
1195
1195
|
deploymentId: "mock-deployment",
|
|
1196
1196
|
workerId: "mock-worker",
|
|
@@ -1198,11 +1198,11 @@ const q = ({
|
|
|
1198
1198
|
primaryColor: c ?? "#1a1a2e",
|
|
1199
1199
|
agentName: g ?? "AI Assistant",
|
|
1200
1200
|
styling: n !== void 0 ? { showBranding: n } : void 0
|
|
1201
|
-
}), T(null)), P == null || P(
|
|
1201
|
+
}), T(null)), P == null || P(V);
|
|
1202
1202
|
} finally {
|
|
1203
|
-
|
|
1203
|
+
J(!1);
|
|
1204
1204
|
}
|
|
1205
|
-
}, [o, s,
|
|
1205
|
+
}, [o, s, O, c, g, l, P]);
|
|
1206
1206
|
Z(() => {
|
|
1207
1207
|
p();
|
|
1208
1208
|
}, [p]);
|
|
@@ -1217,9 +1217,9 @@ const q = ({
|
|
|
1217
1217
|
styling: n !== void 0 ? {
|
|
1218
1218
|
showBranding: n
|
|
1219
1219
|
} : void 0
|
|
1220
|
-
}, [_, o, c, g, l, n]), k =
|
|
1220
|
+
}, [_, o, c, g, l, n]), k = de({
|
|
1221
1221
|
config: I,
|
|
1222
|
-
apiClient:
|
|
1222
|
+
apiClient: O,
|
|
1223
1223
|
mockMode: s,
|
|
1224
1224
|
onSessionStart: M,
|
|
1225
1225
|
onSessionEnd: A,
|
|
@@ -1237,10 +1237,10 @@ const q = ({
|
|
|
1237
1237
|
}, f = () => {
|
|
1238
1238
|
p();
|
|
1239
1239
|
}, B = u === "inline";
|
|
1240
|
-
return K ? null :
|
|
1240
|
+
return K ? null : j && !s ? !B && !z ? /* @__PURE__ */ e(
|
|
1241
1241
|
"div",
|
|
1242
1242
|
{
|
|
1243
|
-
className: `${
|
|
1243
|
+
className: `${E.widget} ${E[u]} ${y ?? ""}`,
|
|
1244
1244
|
style: d,
|
|
1245
1245
|
children: /* @__PURE__ */ e(
|
|
1246
1246
|
ae,
|
|
@@ -1253,12 +1253,12 @@ const q = ({
|
|
|
1253
1253
|
) : /* @__PURE__ */ e(
|
|
1254
1254
|
"div",
|
|
1255
1255
|
{
|
|
1256
|
-
className: `${
|
|
1256
|
+
className: `${E.widget} ${E[u]} ${y ?? ""}`,
|
|
1257
1257
|
style: d,
|
|
1258
1258
|
children: /* @__PURE__ */ e(
|
|
1259
1259
|
Xt,
|
|
1260
1260
|
{
|
|
1261
|
-
errorType:
|
|
1261
|
+
errorType: j,
|
|
1262
1262
|
message: ee,
|
|
1263
1263
|
onRetry: f,
|
|
1264
1264
|
onClose: B ? void 0 : a
|
|
@@ -1268,7 +1268,7 @@ const q = ({
|
|
|
1268
1268
|
) : /* @__PURE__ */ e(
|
|
1269
1269
|
"div",
|
|
1270
1270
|
{
|
|
1271
|
-
className: `${
|
|
1271
|
+
className: `${E.widget} ${E[u]} ${y ?? ""}`,
|
|
1272
1272
|
style: d,
|
|
1273
1273
|
children: z || B ? /* @__PURE__ */ e(
|
|
1274
1274
|
Wt,
|
|
@@ -1291,7 +1291,7 @@ const q = ({
|
|
|
1291
1291
|
}
|
|
1292
1292
|
);
|
|
1293
1293
|
};
|
|
1294
|
-
function
|
|
1294
|
+
function so() {
|
|
1295
1295
|
if (typeof crypto < "u" && crypto.randomUUID)
|
|
1296
1296
|
return `bb-${crypto.randomUUID()}`;
|
|
1297
1297
|
const o = Date.now().toString(36), t = Math.random().toString(36).substring(2, 15), s = Math.random().toString(36).substring(2, 15);
|
|
@@ -1301,8 +1301,8 @@ export {
|
|
|
1301
1301
|
Wt as ChatContainer,
|
|
1302
1302
|
Me as ChatHeader,
|
|
1303
1303
|
ae as ChatToggleButton,
|
|
1304
|
-
|
|
1305
|
-
|
|
1304
|
+
no as ChatWidget,
|
|
1305
|
+
Oe as Message,
|
|
1306
1306
|
yt as MessageInput,
|
|
1307
1307
|
mt as MessageList,
|
|
1308
1308
|
et as ToolCallDisplay,
|
|
@@ -1310,8 +1310,8 @@ export {
|
|
|
1310
1310
|
re as clearSession,
|
|
1311
1311
|
ue as createAPIClient,
|
|
1312
1312
|
pe as createMockAPIClient,
|
|
1313
|
-
|
|
1314
|
-
|
|
1313
|
+
so as generateSessionId,
|
|
1314
|
+
le as getStoredSession,
|
|
1315
1315
|
se as storeSession,
|
|
1316
|
-
|
|
1316
|
+
de as useChat
|
|
1317
1317
|
};
|
package/package.json
CHANGED