@bangdb/web-sdk 1.0.11 → 1.0.12

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.
@@ -1895,7 +1895,7 @@ ${o}
1895
1895
  .bdb-textarea::-webkit-scrollbar-corner {
1896
1896
  background: transparent;
1897
1897
  }
1898
- `,this.root.appendChild(r)}injectGlobalFonts(){let t="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:ital,wght@0,100..800;1,100..800",r=document.createElement("link");r.rel="preconnect",r.href="https://fonts.googleapis.com";let n=document.createElement("link");n.rel="preconnect",n.href="https://fonts.gstatic.com",n.crossOrigin="";let i=document.createElement("link");i.type="text/css",i.rel="stylesheet",i.href=t,this.root.appendChild(r),this.root.appendChild(n),this.root.appendChild(i),document.head.appendChild(r),document.head.appendChild(n),document.head.appendChild(i)}mount(){let t=this.createChatBubble();this.root.appendChild(t),this.bubbleEl=t}async init(){this.store.dispatch(Eae(this.apiConfig?.indexName))}async fetchSessionsFromApi(){if(!this.apiConfig)return;let{chat:t}=this.store.getState(),{activeIndex:r}=t;try{let i=(await Dae({payload:{filter:`{name = "${r}"}`,org:this.apiConfig.org,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey}))?.sessions||[];this.store.dispatch(Tae(i))}catch{}}async fetchSessionChatsFromApi(t){if(!this.apiConfig)return[];try{let r=await Nae({payload:{filter:`{org = "${this.apiConfig.org}" AND source ~ NA}`,sessionid:t,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey});return Jge(r?.queries)?.reverse()}catch{return[]}}getClampedBubblePosition(t,r){let o=Math.max(24,window.innerWidth-52-24),a=Math.max(24,window.innerHeight-52-24);return{left:Math.min(Math.max(t,24),o),top:Math.min(Math.max(r,24),a)}}getDefaultBubblePosition(){return this.getClampedBubblePosition(window.innerWidth-24-52,window.innerHeight-24-52)}resetRenderedMessages(){if(this.renderedMessageIds.clear(),!this.chatAreaEl)return;let t=this.chatAreaEl.querySelector(".chat-data-container"),r=this.chatAreaEl.querySelector("#bdb-streaming-msg"),n=this.chatAreaEl.querySelector("#bdb-empty-state");r&&r.remove(),t&&(t.innerHTML='<div id="chat-window-anchor" style="overflow-anchor: auto; height: 1px;"></div>'),n&&(n.style.display="flex")}handleChatDataChange({chatData:t,streamingMessage:r,isLoading:n}){if(!this.chatAreaEl)return;t||(t=[]);let i=this.chatAreaEl.querySelector(".chat-data-container"),o=this.chatAreaEl.querySelector("#bangdb-chat-area"),a=this.chatAreaEl.querySelector("#bdb-empty-state"),s=()=>{requestAnimationFrame(()=>{o.scrollTo({top:o.scrollHeight,behavior:"smooth"})})},l=t.length>0||!!r?.query;a.style.display=l?"none":"flex",this.renderedMessageIds.size>t.length&&this.resetRenderedMessages();for(let f=0;f<t.length;f++){let g=t[f],m=`msg-${f}`;if((g?.query||g?.files?.length>0)&&!this.renderedMessageIds.has(m)){this.renderedMessageIds.add(m),i.insertBefore(this.userRequestMessage(g.query,g.files,g.log_sid),i.querySelector("#chat-window-anchor"));let _=new Lh(g),{messages:b}=_.parse();g?.query&&b?.length>0&&i.insertBefore(this.aiResponseMessage(g),i.querySelector("#chat-window-anchor")),s()}}let u="bdb-streaming-msg",c=this.root.querySelector(`#${u}`);if(r?.query){c||(c=document.createElement("div"),c.id=u,i.insertBefore(c,i.querySelector("#chat-window-anchor")),c.appendChild(this.userRequestMessage(r.query)),c.style.minHeight=`${o.clientHeight*.8}px`);let f=c.querySelector(".bdb-streaming-ai-area"),g=r.output?.output??"";if(n&&!g&&!f)f=document.createElement("div"),f.className="bdb-streaming-ai-area",f.appendChild(this.loadingIndicator()),c.appendChild(f);else if(g){f||(f=document.createElement("div"),f.className="bdb-streaming-ai-area",c.appendChild(f));let m=f.querySelector(".ai-md");m?m.innerHTML=zx(g):(f.innerHTML="",f.appendChild(this.aiResponseMessage(r)))}a.style.display="none",s()}else c&&(c.style.minHeight="0",c.remove());let d=this.chatAreaEl.querySelector(".bdb-textarea"),h=this.chatAreaEl.querySelector(".bdb-send-btn"),p=this.chatAreaEl.querySelector(".bdb-tools-trigger");if(d&&(d.style.opacity="1"),h){let f=this.store.getState(),g=f.chatSessions[f.chat.activeSession],m=(g?.queryFiles||[]).length>0,A=(g?.files||[]).some(_=>this.isFileBusy(_));h.disabled=!!n||A||!d?.value.trim()&&!m}p&&(p.disabled=!!n,p.style.opacity=n?"0.5":"1",p.style.pointerEvents=n?"none":"auto")}handleModeChange(t){let r=this.bubbleEl?.querySelector(".chat-bubble");if(r)switch(t){case 0:if(r.classList.remove("is-open"),r.setAttribute("aria-label","Open chat"),this.chatUIEl){let i=this.chatUIEl.querySelector(".bdb-chat-panel");i&&(i.style.animation="bdb-panel-out 0.18s ease-in both",this.closeAnimationTimeout&&window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=window.setTimeout(()=>{this.chatUIEl&&(this.chatUIEl.style.display="none"),this.closeAnimationTimeout=null},180))}break;case 1:if(r.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let i=this.chatUIEl.querySelector(".bdb-chat-panel");i&&(i.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}r.classList.add("is-open"),this.chatUIEl?.querySelector(".bdb-chat-panel")?.classList.remove("is-fullscreen"),this.renderPanelView(),requestAnimationFrame(()=>this.updatePanelPosition());break;case 2:if(r.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let i=this.chatUIEl.querySelector(".bdb-chat-panel");i&&(i.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}r.classList.add("is-open");let n=this.chatUIEl?.querySelector(".bdb-chat-panel");n&&(n.classList.add("is-fullscreen"),n.style.top="",n.style.bottom="",n.style.left="",n.style.right=""),this.renderPanelView();break}}renderPanelView(){if(this.activePanelView==="sessions"){this.renderSessionList();return}this.renderChatView()}renderActiveSession(){let t=this.store.getState(),r=t.chatSessions[t.chat.activeSession];this.handleChatDataChange({chatData:r?.chatData,streamingMessage:r?.streamingMessage,isLoading:r?.isLoading})}renderChatView(){if(!this.chatUIEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-body");t&&(this.activePanelView="chat",this.syncPanelHeader(),t.innerHTML="",this.chatAreaEl=null,this.resetRenderedMessages(),t.appendChild(this.chatArea()),this.renderActiveSession())}async renderSessionList(){if(!this.chatUIEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-body");if(!t)return;let r=++this.sessionListRenderRequest;this.activePanelView="sessions",this.chatAreaEl=null,this.resetRenderedMessages(),this.syncPanelHeader(),t.innerHTML="",t.appendChild(this.sessionListLoadingView()),await this.fetchSessionsFromApi(),r===this.sessionListRenderRequest&&this.activePanelView==="sessions"&&(t.innerHTML="",t.appendChild(this.sessionListView()))}syncPanelHeader(){if(!this.chatUIEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-title"),r=this.chatUIEl.querySelector(".js-sessions-btn"),n=this.chatUIEl.querySelector(".bdb-download-items");t&&(t.textContent=this.activePanelView==="sessions"?"Chats":this.getActiveSessionTitle()),r&&(r.style.display=this.activePanelView==="chat"?"flex":"none"),n&&(n.style.display=this.activePanelView==="chat"?"block":"none")}async openSession(t){this.store.dispatch(xae(t)),this.store.dispatch(Fae(t));let r=await this.fetchSessionChatsFromApi(t);this.store.dispatch(Uae({sessionId:t,chatData:r})),this.renderChatView()}async createNewSession(){let t=crypto.randomUUID().replace(/-/g,"").slice(0,10);(await HM({baseURL:this.apiConfig?.backendURL,apikey:this.apiConfig?.apikey,payload:{userid:this.apiConfig?.userid,sessionid:t,name:this.apiConfig?.indexName,description:"New chat",org:this.apiConfig?.org}}))?.sid&&this.openSession(t)}getSessionTitle(t,r){let n=r?.description||"";return n.trim()?n.trim().slice(0,56):t==="GUEST_SESSION"?"Default chat":"New chat"}getActiveSessionTitle(){let t=this.store.getState(),r=t.chat.activeSession,n=t.chat.sessions||[];for(let i of n)try{let o=typeof i?.session=="string"?JSON.parse(i.session):i?.session||i;if(o?.sessionid===r){let a=o?.description||"";if(a.trim())return a.trim().slice(0,56)}}catch{}return"Chats"}updatePanelPosition(){if(!this.chatUIEl||!this.bubbleEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-panel");if(!t||t.classList.contains("is-fullscreen"))return;let r=parseInt(this.bubbleEl.style.left)||0,n=parseInt(this.bubbleEl.style.top)||0,i=52,o=52,a=750,s=window.innerWidth,l=window.innerHeight,u=12,c=n+o/2;if(c<l/3)t.style.top=o+u+"px",t.style.bottom="auto";else if(c>2*l/3)t.style.bottom=o+u+"px",t.style.top="auto";else{let d=c-a/2,h=Math.max(8,Math.min(d,l-a-8));t.style.top=h-n+"px",t.style.bottom="auto"}r+i/2<s/2?(t.style.left="0",t.style.right="auto"):(t.style.right="0",t.style.left="auto")}createChatBubble(){let t=document.createElement("div");t.classList.add("bdb-bubble-container"),t.innerHTML=`
1898
+ `,this.root.appendChild(r)}injectGlobalFonts(){let t="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:ital,wght@0,100..800;1,100..800",r=document.createElement("link");r.rel="preconnect",r.href="https://fonts.googleapis.com";let n=document.createElement("link");n.rel="preconnect",n.href="https://fonts.gstatic.com",n.crossOrigin="";let i=document.createElement("link");i.type="text/css",i.rel="stylesheet",i.href=t,this.root.appendChild(r),this.root.appendChild(n),this.root.appendChild(i),document.head.appendChild(r),document.head.appendChild(n),document.head.appendChild(i)}mount(){let t=this.createChatBubble();this.root.appendChild(t),this.bubbleEl=t}async init(){this.store.dispatch(Eae(this.apiConfig?.indexName))}async fetchSessionsFromApi(){if(!this.apiConfig)return;let{chat:t}=this.store.getState(),{activeIndex:r}=t;try{let i=(await Dae({payload:{filter:`{name = "${r}"}`,org:this.apiConfig.org,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey}))?.sessions||[];this.store.dispatch(Tae(i))}catch{}}async fetchSessionChatsFromApi(t){if(!this.apiConfig)return[];try{let r=await Nae({payload:{filter:`{org = "${this.apiConfig.org}" AND source ~ NA}`,sessionid:t,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey});return Jge(r?.queries)?.reverse()}catch{return[]}}getClampedBubblePosition(t,r){let o=Math.max(24,window.innerWidth-52-24),a=Math.max(24,window.innerHeight-52-24);return{left:Math.min(Math.max(t,24),o),top:Math.min(Math.max(r,24),a)}}getDefaultBubblePosition(){return this.getClampedBubblePosition(window.innerWidth-24-52,window.innerHeight-24-52)}resetRenderedMessages(){if(this.renderedMessageIds.clear(),!this.chatAreaEl)return;let t=this.chatAreaEl.querySelector(".chat-data-container"),r=this.chatAreaEl.querySelector("#bdb-streaming-msg"),n=this.chatAreaEl.querySelector("#bdb-empty-state");r&&r.remove(),t&&(t.innerHTML='<div id="chat-window-anchor" style="overflow-anchor: auto; height: 1px;"></div>'),n&&(n.style.display="flex")}handleChatDataChange({chatData:t,streamingMessage:r,isLoading:n}){if(!this.chatAreaEl)return;t||(t=[]);let i=this.chatAreaEl.querySelector(".chat-data-container"),o=this.chatAreaEl.querySelector("#bangdb-chat-area"),a=this.chatAreaEl.querySelector("#bdb-empty-state"),s=()=>{requestAnimationFrame(()=>{o.scrollTo({top:o.scrollHeight,behavior:"smooth"})})},l=t.length>0||!!r?.query;a.style.display=l?"none":"flex",this.renderedMessageIds.size>t.length&&this.resetRenderedMessages();for(let f=0;f<t.length;f++){let g=t[f],m=`msg-${f}`;if((g?.query||g?.files?.length>0)&&!this.renderedMessageIds.has(m)){this.renderedMessageIds.add(m),i.insertBefore(this.userRequestMessage(g.query,g.files,g.log_sid),i.querySelector("#chat-window-anchor"));let _=new Lh(g),{messages:b}=_.parse();g?.query&&b?.length>0&&i.insertBefore(this.aiResponseMessage(g),i.querySelector("#chat-window-anchor")),s()}}let u="bdb-streaming-msg",c=this.root.querySelector(`#${u}`);if(r?.query){c||(c=document.createElement("div"),c.id=u,i.insertBefore(c,i.querySelector("#chat-window-anchor")),c.appendChild(this.userRequestMessage(r.query)),c.style.minHeight=`${o.clientHeight*.8}px`);let f=c.querySelector(".bdb-streaming-ai-area"),g=r.output?.output??"";if(n&&!g&&!f)f=document.createElement("div"),f.className="bdb-streaming-ai-area",f.appendChild(this.loadingIndicator()),c.appendChild(f);else if(g){f||(f=document.createElement("div"),f.className="bdb-streaming-ai-area",c.appendChild(f));let m=f.querySelector(".ai-md");m?m.innerHTML=zx(g):(f.innerHTML="",f.appendChild(this.aiResponseMessage(r)))}a.style.display="none",s()}else c&&(c.style.minHeight="0",c.remove());let d=this.chatAreaEl.querySelector(".bdb-textarea"),h=this.chatAreaEl.querySelector(".bdb-send-btn"),p=this.chatAreaEl.querySelector(".bdb-tools-trigger");if(d&&(d.style.opacity="1"),h){let f=this.store.getState(),g=f.chatSessions[f.chat.activeSession],m=(g?.queryFiles||[]).length>0,A=(g?.files||[]).some(_=>this.isFileBusy(_));h.disabled=!!n||A||!d?.value.trim()&&!m}p&&(p.disabled=!!n,p.style.opacity=n?"0.5":"1",p.style.pointerEvents=n?"none":"auto")}handleModeChange(t){let r=this.bubbleEl?.querySelector(".chat-bubble");if(r)switch(t){case 0:if(r.classList.remove("is-open"),r.setAttribute("aria-label","Open chat"),this.chatUIEl){let i=this.chatUIEl.querySelector(".bdb-chat-panel");i&&(i.style.animation="bdb-panel-out 0.18s ease-in both",this.closeAnimationTimeout&&window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=window.setTimeout(()=>{this.chatUIEl&&(this.chatUIEl.style.display="none"),this.closeAnimationTimeout=null},180))}break;case 1:if(r.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let i=this.chatUIEl.querySelector(".bdb-chat-panel");i&&(i.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}r.classList.add("is-open"),this.chatUIEl?.querySelector(".bdb-chat-panel")?.classList.remove("is-fullscreen"),this.renderPanelView(),requestAnimationFrame(()=>this.updatePanelPosition());break;case 2:if(r.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let i=this.chatUIEl.querySelector(".bdb-chat-panel");i&&(i.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}r.classList.add("is-open");let n=this.chatUIEl?.querySelector(".bdb-chat-panel");n&&(n.classList.add("is-fullscreen"),n.style.top="",n.style.bottom="",n.style.left="",n.style.right=""),this.renderPanelView();break}}renderPanelView(){if(this.activePanelView==="sessions"){this.renderSessionList();return}this.renderChatView()}renderActiveSession(){let t=this.store.getState(),r=t.chatSessions[t.chat.activeSession];this.handleChatDataChange({chatData:r?.chatData,streamingMessage:r?.streamingMessage,isLoading:r?.isLoading})}renderChatView(){if(!this.chatUIEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-body");t&&(this.activePanelView="chat",this.syncPanelHeader(),t.innerHTML="",this.chatAreaEl=null,this.resetRenderedMessages(),t.appendChild(this.chatArea()),this.renderActiveSession())}async renderSessionList(){if(!this.chatUIEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-body");if(!t)return;let r=++this.sessionListRenderRequest;this.activePanelView="sessions",this.chatAreaEl=null,this.resetRenderedMessages(),this.syncPanelHeader(),t.innerHTML="",t.appendChild(this.sessionListLoadingView()),await this.fetchSessionsFromApi(),r===this.sessionListRenderRequest&&this.activePanelView==="sessions"&&(t.innerHTML="",t.appendChild(this.sessionListView()))}syncPanelHeader(){if(!this.chatUIEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-title"),r=this.chatUIEl.querySelector(".js-sessions-btn"),n=this.chatUIEl.querySelector(".bdb-download-items");t&&(t.textContent=this.activePanelView==="sessions"?"Chats":this.getActiveSessionTitle()),r&&(r.style.display=this.activePanelView==="chat"?"flex":"none"),n&&(n.style.display=this.activePanelView==="chat"?"block":"none")}async openSession(t){this.store.dispatch(xae(t)),this.store.dispatch(Fae(t));let r=await this.fetchSessionChatsFromApi(t);this.store.dispatch(Uae({sessionId:t,chatData:r})),this.renderChatView()}async createNewSession(){let t=crypto.randomUUID().replace(/-/g,"").slice(0,10);(await HM({baseURL:this.apiConfig?.backendURL,apikey:this.apiConfig?.apikey,payload:{userid:this.apiConfig?.userid,sessionid:t,name:this.apiConfig?.indexName,description:"New chat",org:this.apiConfig?.org}}))?.sid&&this.openSession(t)}getSessionTitle(t,r){let n=r?.description||"";return n.trim()?n.trim().slice(0,56):t==="GUEST_SESSION"?"Default chat":"New chat"}getActiveSessionTitle(){let t=this.store.getState(),r=t.chat.activeSession,n=t.chat.sessions||[];for(let i of n)try{let o=typeof i?.session=="string"?JSON.parse(i.session):i?.session||i;if(o?.sessionid===r){let a=o?.description||"";if(a.trim())return a.trim().slice(0,56)}}catch{}return"Chats"}updatePanelPosition(){if(!this.chatUIEl||!this.bubbleEl)return;let t=this.chatUIEl.querySelector(".bdb-chat-panel");if(!t||t.classList.contains("is-fullscreen"))return;let r=parseInt(this.bubbleEl.style.left)||0,n=parseInt(this.bubbleEl.style.top)||0,i=52,o=52,a=t.offsetHeight,s=window.innerWidth,l=window.innerHeight,u=12,c=n+o/2;if(c<l/3)t.style.top=o+u+"px",t.style.bottom="auto";else if(c>2*l/3)t.style.bottom=o+u+"px",t.style.top="auto";else{let d=c-a/2,h=Math.max(8,Math.min(d,l-a-8));t.style.top=h-n+"px",t.style.bottom="auto"}r+i/2<s/2?(t.style.left="0",t.style.right="auto"):(t.style.right="0",t.style.left="auto")}createChatBubble(){let t=document.createElement("div");t.classList.add("bdb-bubble-container"),t.innerHTML=`
1899
1899
  <style>
1900
1900
  .bdb-bubble-container {
1901
1901
  position: fixed;
@@ -2284,8 +2284,8 @@ ${o}
2284
2284
  position: absolute;
2285
2285
  bottom: 68px;
2286
2286
  right: 0;
2287
- width: 500px;
2288
- height: 750px;
2287
+ width: clamp(320px, 35vw, 500px);
2288
+ height: clamp(500px, 80vh, 750px);
2289
2289
  background: var(--background);
2290
2290
  border-radius: var(--radius-lg);
2291
2291
  box-shadow: var(--shadow-panel);
package/dist/index.js CHANGED
@@ -1397,7 +1397,7 @@ ${a}
1397
1397
  .bdb-textarea::-webkit-scrollbar-corner {
1398
1398
  background: transparent;
1399
1399
  }
1400
- `,this.root.appendChild(t)}injectGlobalFonts(){let e="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:ital,wght@0,100..800;1,100..800",t=document.createElement("link");t.rel="preconnect",t.href="https://fonts.googleapis.com";let o=document.createElement("link");o.rel="preconnect",o.href="https://fonts.gstatic.com",o.crossOrigin="";let r=document.createElement("link");r.type="text/css",r.rel="stylesheet",r.href=e,this.root.appendChild(t),this.root.appendChild(o),this.root.appendChild(r),document.head.appendChild(t),document.head.appendChild(o),document.head.appendChild(r)}mount(){let e=this.createChatBubble();this.root.appendChild(e),this.bubbleEl=e}async init(){this.store.dispatch(ce(this.apiConfig?.indexName))}async fetchSessionsFromApi(){if(!this.apiConfig)return;let{chat:e}=this.store.getState(),{activeIndex:t}=e;try{let r=(await ge({payload:{filter:`{name = "${t}"}`,org:this.apiConfig.org,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey}))?.sessions||[];this.store.dispatch(be(r))}catch{}}async fetchSessionChatsFromApi(e){if(!this.apiConfig)return[];try{let t=await me({payload:{filter:`{org = "${this.apiConfig.org}" AND source ~ NA}`,sessionid:e,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey});return Ne(t?.queries)?.reverse()}catch{return[]}}getClampedBubblePosition(e,t){let a=Math.max(24,window.innerWidth-52-24),s=Math.max(24,window.innerHeight-52-24);return{left:Math.min(Math.max(e,24),a),top:Math.min(Math.max(t,24),s)}}getDefaultBubblePosition(){return this.getClampedBubblePosition(window.innerWidth-24-52,window.innerHeight-24-52)}resetRenderedMessages(){if(this.renderedMessageIds.clear(),!this.chatAreaEl)return;let e=this.chatAreaEl.querySelector(".chat-data-container"),t=this.chatAreaEl.querySelector("#bdb-streaming-msg"),o=this.chatAreaEl.querySelector("#bdb-empty-state");t&&t.remove(),e&&(e.innerHTML='<div id="chat-window-anchor" style="overflow-anchor: auto; height: 1px;"></div>'),o&&(o.style.display="flex")}handleChatDataChange({chatData:e,streamingMessage:t,isLoading:o}){if(!this.chatAreaEl)return;e||(e=[]);let r=this.chatAreaEl.querySelector(".chat-data-container"),a=this.chatAreaEl.querySelector("#bangdb-chat-area"),s=this.chatAreaEl.querySelector("#bdb-empty-state"),i=()=>{requestAnimationFrame(()=>{a.scrollTo({top:a.scrollHeight,behavior:"smooth"})})},l=e.length>0||!!t?.query;s.style.display=l?"none":"flex",this.renderedMessageIds.size>e.length&&this.resetRenderedMessages();for(let h=0;h<e.length;h++){let m=e[h],x=`msg-${h}`;if((m?.query||m?.files?.length>0)&&!this.renderedMessageIds.has(x)){this.renderedMessageIds.add(x),r.insertBefore(this.userRequestMessage(m.query,m.files,m.log_sid),r.querySelector("#chat-window-anchor"));let p=new I(m),{messages:y}=p.parse();m?.query&&y?.length>0&&r.insertBefore(this.aiResponseMessage(m),r.querySelector("#chat-window-anchor")),i()}}let d="bdb-streaming-msg",c=this.root.querySelector(`#${d}`);if(t?.query){c||(c=document.createElement("div"),c.id=d,r.insertBefore(c,r.querySelector("#chat-window-anchor")),c.appendChild(this.userRequestMessage(t.query)),c.style.minHeight=`${a.clientHeight*.8}px`);let h=c.querySelector(".bdb-streaming-ai-area"),m=t.output?.output??"";if(o&&!m&&!h)h=document.createElement("div"),h.className="bdb-streaming-ai-area",h.appendChild(this.loadingIndicator()),c.appendChild(h);else if(m){h||(h=document.createElement("div"),h.className="bdb-streaming-ai-area",c.appendChild(h));let x=h.querySelector(".ai-md");x?x.innerHTML=q(m):(h.innerHTML="",h.appendChild(this.aiResponseMessage(t)))}s.style.display="none",i()}else c&&(c.style.minHeight="0",c.remove());let u=this.chatAreaEl.querySelector(".bdb-textarea"),g=this.chatAreaEl.querySelector(".bdb-send-btn"),b=this.chatAreaEl.querySelector(".bdb-tools-trigger");if(u&&(u.style.opacity="1"),g){let h=this.store.getState(),m=h.chatSessions[h.chat.activeSession],x=(m?.queryFiles||[]).length>0,w=(m?.files||[]).some(p=>this.isFileBusy(p));g.disabled=!!o||w||!u?.value.trim()&&!x}b&&(b.disabled=!!o,b.style.opacity=o?"0.5":"1",b.style.pointerEvents=o?"none":"auto")}handleModeChange(e){let t=this.bubbleEl?.querySelector(".chat-bubble");if(t)switch(e){case 0:if(t.classList.remove("is-open"),t.setAttribute("aria-label","Open chat"),this.chatUIEl){let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-out 0.18s ease-in both",this.closeAnimationTimeout&&window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=window.setTimeout(()=>{this.chatUIEl&&(this.chatUIEl.style.display="none"),this.closeAnimationTimeout=null},180))}break;case 1:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open"),this.chatUIEl?.querySelector(".bdb-chat-panel")?.classList.remove("is-fullscreen"),this.renderPanelView(),requestAnimationFrame(()=>this.updatePanelPosition());break;case 2:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open");let o=this.chatUIEl?.querySelector(".bdb-chat-panel");o&&(o.classList.add("is-fullscreen"),o.style.top="",o.style.bottom="",o.style.left="",o.style.right=""),this.renderPanelView();break}}renderPanelView(){if(this.activePanelView==="sessions"){this.renderSessionList();return}this.renderChatView()}renderActiveSession(){let e=this.store.getState(),t=e.chatSessions[e.chat.activeSession];this.handleChatDataChange({chatData:t?.chatData,streamingMessage:t?.streamingMessage,isLoading:t?.isLoading})}renderChatView(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");e&&(this.activePanelView="chat",this.syncPanelHeader(),e.innerHTML="",this.chatAreaEl=null,this.resetRenderedMessages(),e.appendChild(this.chatArea()),this.renderActiveSession())}async renderSessionList(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");if(!e)return;let t=++this.sessionListRenderRequest;this.activePanelView="sessions",this.chatAreaEl=null,this.resetRenderedMessages(),this.syncPanelHeader(),e.innerHTML="",e.appendChild(this.sessionListLoadingView()),await this.fetchSessionsFromApi(),t===this.sessionListRenderRequest&&this.activePanelView==="sessions"&&(e.innerHTML="",e.appendChild(this.sessionListView()))}syncPanelHeader(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-title"),t=this.chatUIEl.querySelector(".js-sessions-btn"),o=this.chatUIEl.querySelector(".bdb-download-items");e&&(e.textContent=this.activePanelView==="sessions"?"Chats":this.getActiveSessionTitle()),t&&(t.style.display=this.activePanelView==="chat"?"flex":"none"),o&&(o.style.display=this.activePanelView==="chat"?"block":"none")}async openSession(e){this.store.dispatch(pe(e)),this.store.dispatch(ke(e));let t=await this.fetchSessionChatsFromApi(e);this.store.dispatch(Te({sessionId:e,chatData:t})),this.renderChatView()}async createNewSession(){let e=crypto.randomUUID().replace(/-/g,"").slice(0,10);(await Q({baseURL:this.apiConfig?.backendURL,apikey:this.apiConfig?.apikey,payload:{userid:this.apiConfig?.userid,sessionid:e,name:this.apiConfig?.indexName,description:"New chat",org:this.apiConfig?.org}}))?.sid&&this.openSession(e)}getSessionTitle(e,t){let o=t?.description||"";return o.trim()?o.trim().slice(0,56):e==="GUEST_SESSION"?"Default chat":"New chat"}getActiveSessionTitle(){let e=this.store.getState(),t=e.chat.activeSession,o=e.chat.sessions||[];for(let r of o)try{let a=typeof r?.session=="string"?JSON.parse(r.session):r?.session||r;if(a?.sessionid===t){let s=a?.description||"";if(s.trim())return s.trim().slice(0,56)}}catch{}return"Chats"}updatePanelPosition(){if(!this.chatUIEl||!this.bubbleEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-panel");if(!e||e.classList.contains("is-fullscreen"))return;let t=parseInt(this.bubbleEl.style.left)||0,o=parseInt(this.bubbleEl.style.top)||0,r=52,a=52,s=750,i=window.innerWidth,l=window.innerHeight,d=12,c=o+a/2;if(c<l/3)e.style.top=a+d+"px",e.style.bottom="auto";else if(c>2*l/3)e.style.bottom=a+d+"px",e.style.top="auto";else{let u=c-s/2,g=Math.max(8,Math.min(u,l-s-8));e.style.top=g-o+"px",e.style.bottom="auto"}t+r/2<i/2?(e.style.left="0",e.style.right="auto"):(e.style.right="0",e.style.left="auto")}createChatBubble(){let e=document.createElement("div");e.classList.add("bdb-bubble-container"),e.innerHTML=`
1400
+ `,this.root.appendChild(t)}injectGlobalFonts(){let e="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:ital,wght@0,100..800;1,100..800",t=document.createElement("link");t.rel="preconnect",t.href="https://fonts.googleapis.com";let o=document.createElement("link");o.rel="preconnect",o.href="https://fonts.gstatic.com",o.crossOrigin="";let r=document.createElement("link");r.type="text/css",r.rel="stylesheet",r.href=e,this.root.appendChild(t),this.root.appendChild(o),this.root.appendChild(r),document.head.appendChild(t),document.head.appendChild(o),document.head.appendChild(r)}mount(){let e=this.createChatBubble();this.root.appendChild(e),this.bubbleEl=e}async init(){this.store.dispatch(ce(this.apiConfig?.indexName))}async fetchSessionsFromApi(){if(!this.apiConfig)return;let{chat:e}=this.store.getState(),{activeIndex:t}=e;try{let r=(await ge({payload:{filter:`{name = "${t}"}`,org:this.apiConfig.org,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey}))?.sessions||[];this.store.dispatch(be(r))}catch{}}async fetchSessionChatsFromApi(e){if(!this.apiConfig)return[];try{let t=await me({payload:{filter:`{org = "${this.apiConfig.org}" AND source ~ NA}`,sessionid:e,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey});return Ne(t?.queries)?.reverse()}catch{return[]}}getClampedBubblePosition(e,t){let a=Math.max(24,window.innerWidth-52-24),s=Math.max(24,window.innerHeight-52-24);return{left:Math.min(Math.max(e,24),a),top:Math.min(Math.max(t,24),s)}}getDefaultBubblePosition(){return this.getClampedBubblePosition(window.innerWidth-24-52,window.innerHeight-24-52)}resetRenderedMessages(){if(this.renderedMessageIds.clear(),!this.chatAreaEl)return;let e=this.chatAreaEl.querySelector(".chat-data-container"),t=this.chatAreaEl.querySelector("#bdb-streaming-msg"),o=this.chatAreaEl.querySelector("#bdb-empty-state");t&&t.remove(),e&&(e.innerHTML='<div id="chat-window-anchor" style="overflow-anchor: auto; height: 1px;"></div>'),o&&(o.style.display="flex")}handleChatDataChange({chatData:e,streamingMessage:t,isLoading:o}){if(!this.chatAreaEl)return;e||(e=[]);let r=this.chatAreaEl.querySelector(".chat-data-container"),a=this.chatAreaEl.querySelector("#bangdb-chat-area"),s=this.chatAreaEl.querySelector("#bdb-empty-state"),i=()=>{requestAnimationFrame(()=>{a.scrollTo({top:a.scrollHeight,behavior:"smooth"})})},l=e.length>0||!!t?.query;s.style.display=l?"none":"flex",this.renderedMessageIds.size>e.length&&this.resetRenderedMessages();for(let h=0;h<e.length;h++){let m=e[h],x=`msg-${h}`;if((m?.query||m?.files?.length>0)&&!this.renderedMessageIds.has(x)){this.renderedMessageIds.add(x),r.insertBefore(this.userRequestMessage(m.query,m.files,m.log_sid),r.querySelector("#chat-window-anchor"));let p=new I(m),{messages:y}=p.parse();m?.query&&y?.length>0&&r.insertBefore(this.aiResponseMessage(m),r.querySelector("#chat-window-anchor")),i()}}let d="bdb-streaming-msg",c=this.root.querySelector(`#${d}`);if(t?.query){c||(c=document.createElement("div"),c.id=d,r.insertBefore(c,r.querySelector("#chat-window-anchor")),c.appendChild(this.userRequestMessage(t.query)),c.style.minHeight=`${a.clientHeight*.8}px`);let h=c.querySelector(".bdb-streaming-ai-area"),m=t.output?.output??"";if(o&&!m&&!h)h=document.createElement("div"),h.className="bdb-streaming-ai-area",h.appendChild(this.loadingIndicator()),c.appendChild(h);else if(m){h||(h=document.createElement("div"),h.className="bdb-streaming-ai-area",c.appendChild(h));let x=h.querySelector(".ai-md");x?x.innerHTML=q(m):(h.innerHTML="",h.appendChild(this.aiResponseMessage(t)))}s.style.display="none",i()}else c&&(c.style.minHeight="0",c.remove());let u=this.chatAreaEl.querySelector(".bdb-textarea"),g=this.chatAreaEl.querySelector(".bdb-send-btn"),b=this.chatAreaEl.querySelector(".bdb-tools-trigger");if(u&&(u.style.opacity="1"),g){let h=this.store.getState(),m=h.chatSessions[h.chat.activeSession],x=(m?.queryFiles||[]).length>0,w=(m?.files||[]).some(p=>this.isFileBusy(p));g.disabled=!!o||w||!u?.value.trim()&&!x}b&&(b.disabled=!!o,b.style.opacity=o?"0.5":"1",b.style.pointerEvents=o?"none":"auto")}handleModeChange(e){let t=this.bubbleEl?.querySelector(".chat-bubble");if(t)switch(e){case 0:if(t.classList.remove("is-open"),t.setAttribute("aria-label","Open chat"),this.chatUIEl){let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-out 0.18s ease-in both",this.closeAnimationTimeout&&window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=window.setTimeout(()=>{this.chatUIEl&&(this.chatUIEl.style.display="none"),this.closeAnimationTimeout=null},180))}break;case 1:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open"),this.chatUIEl?.querySelector(".bdb-chat-panel")?.classList.remove("is-fullscreen"),this.renderPanelView(),requestAnimationFrame(()=>this.updatePanelPosition());break;case 2:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open");let o=this.chatUIEl?.querySelector(".bdb-chat-panel");o&&(o.classList.add("is-fullscreen"),o.style.top="",o.style.bottom="",o.style.left="",o.style.right=""),this.renderPanelView();break}}renderPanelView(){if(this.activePanelView==="sessions"){this.renderSessionList();return}this.renderChatView()}renderActiveSession(){let e=this.store.getState(),t=e.chatSessions[e.chat.activeSession];this.handleChatDataChange({chatData:t?.chatData,streamingMessage:t?.streamingMessage,isLoading:t?.isLoading})}renderChatView(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");e&&(this.activePanelView="chat",this.syncPanelHeader(),e.innerHTML="",this.chatAreaEl=null,this.resetRenderedMessages(),e.appendChild(this.chatArea()),this.renderActiveSession())}async renderSessionList(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");if(!e)return;let t=++this.sessionListRenderRequest;this.activePanelView="sessions",this.chatAreaEl=null,this.resetRenderedMessages(),this.syncPanelHeader(),e.innerHTML="",e.appendChild(this.sessionListLoadingView()),await this.fetchSessionsFromApi(),t===this.sessionListRenderRequest&&this.activePanelView==="sessions"&&(e.innerHTML="",e.appendChild(this.sessionListView()))}syncPanelHeader(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-title"),t=this.chatUIEl.querySelector(".js-sessions-btn"),o=this.chatUIEl.querySelector(".bdb-download-items");e&&(e.textContent=this.activePanelView==="sessions"?"Chats":this.getActiveSessionTitle()),t&&(t.style.display=this.activePanelView==="chat"?"flex":"none"),o&&(o.style.display=this.activePanelView==="chat"?"block":"none")}async openSession(e){this.store.dispatch(pe(e)),this.store.dispatch(ke(e));let t=await this.fetchSessionChatsFromApi(e);this.store.dispatch(Te({sessionId:e,chatData:t})),this.renderChatView()}async createNewSession(){let e=crypto.randomUUID().replace(/-/g,"").slice(0,10);(await Q({baseURL:this.apiConfig?.backendURL,apikey:this.apiConfig?.apikey,payload:{userid:this.apiConfig?.userid,sessionid:e,name:this.apiConfig?.indexName,description:"New chat",org:this.apiConfig?.org}}))?.sid&&this.openSession(e)}getSessionTitle(e,t){let o=t?.description||"";return o.trim()?o.trim().slice(0,56):e==="GUEST_SESSION"?"Default chat":"New chat"}getActiveSessionTitle(){let e=this.store.getState(),t=e.chat.activeSession,o=e.chat.sessions||[];for(let r of o)try{let a=typeof r?.session=="string"?JSON.parse(r.session):r?.session||r;if(a?.sessionid===t){let s=a?.description||"";if(s.trim())return s.trim().slice(0,56)}}catch{}return"Chats"}updatePanelPosition(){if(!this.chatUIEl||!this.bubbleEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-panel");if(!e||e.classList.contains("is-fullscreen"))return;let t=parseInt(this.bubbleEl.style.left)||0,o=parseInt(this.bubbleEl.style.top)||0,r=52,a=52,s=e.offsetHeight,i=window.innerWidth,l=window.innerHeight,d=12,c=o+a/2;if(c<l/3)e.style.top=a+d+"px",e.style.bottom="auto";else if(c>2*l/3)e.style.bottom=a+d+"px",e.style.top="auto";else{let u=c-s/2,g=Math.max(8,Math.min(u,l-s-8));e.style.top=g-o+"px",e.style.bottom="auto"}t+r/2<i/2?(e.style.left="0",e.style.right="auto"):(e.style.right="0",e.style.left="auto")}createChatBubble(){let e=document.createElement("div");e.classList.add("bdb-bubble-container"),e.innerHTML=`
1401
1401
  <style>
1402
1402
  .bdb-bubble-container {
1403
1403
  position: fixed;
@@ -1786,8 +1786,8 @@ ${a}
1786
1786
  position: absolute;
1787
1787
  bottom: 68px;
1788
1788
  right: 0;
1789
- width: 500px;
1790
- height: 750px;
1789
+ width: clamp(320px, 35vw, 500px);
1790
+ height: clamp(500px, 80vh, 750px);
1791
1791
  background: var(--background);
1792
1792
  border-radius: var(--radius-lg);
1793
1793
  box-shadow: var(--shadow-panel);
package/dist/index.mjs CHANGED
@@ -1397,7 +1397,7 @@ ${a}
1397
1397
  .bdb-textarea::-webkit-scrollbar-corner {
1398
1398
  background: transparent;
1399
1399
  }
1400
- `,this.root.appendChild(t)}injectGlobalFonts(){let e="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:ital,wght@0,100..800;1,100..800",t=document.createElement("link");t.rel="preconnect",t.href="https://fonts.googleapis.com";let o=document.createElement("link");o.rel="preconnect",o.href="https://fonts.gstatic.com",o.crossOrigin="";let r=document.createElement("link");r.type="text/css",r.rel="stylesheet",r.href=e,this.root.appendChild(t),this.root.appendChild(o),this.root.appendChild(r),document.head.appendChild(t),document.head.appendChild(o),document.head.appendChild(r)}mount(){let e=this.createChatBubble();this.root.appendChild(e),this.bubbleEl=e}async init(){this.store.dispatch(ne(this.apiConfig?.indexName))}async fetchSessionsFromApi(){if(!this.apiConfig)return;let{chat:e}=this.store.getState(),{activeIndex:t}=e;try{let r=(await de({payload:{filter:`{name = "${t}"}`,org:this.apiConfig.org,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey}))?.sessions||[];this.store.dispatch(ie(r))}catch{}}async fetchSessionChatsFromApi(e){if(!this.apiConfig)return[];try{let t=await ce({payload:{filter:`{org = "${this.apiConfig.org}" AND source ~ NA}`,sessionid:e,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey});return Ce(t?.queries)?.reverse()}catch{return[]}}getClampedBubblePosition(e,t){let a=Math.max(24,window.innerWidth-52-24),s=Math.max(24,window.innerHeight-52-24);return{left:Math.min(Math.max(e,24),a),top:Math.min(Math.max(t,24),s)}}getDefaultBubblePosition(){return this.getClampedBubblePosition(window.innerWidth-24-52,window.innerHeight-24-52)}resetRenderedMessages(){if(this.renderedMessageIds.clear(),!this.chatAreaEl)return;let e=this.chatAreaEl.querySelector(".chat-data-container"),t=this.chatAreaEl.querySelector("#bdb-streaming-msg"),o=this.chatAreaEl.querySelector("#bdb-empty-state");t&&t.remove(),e&&(e.innerHTML='<div id="chat-window-anchor" style="overflow-anchor: auto; height: 1px;"></div>'),o&&(o.style.display="flex")}handleChatDataChange({chatData:e,streamingMessage:t,isLoading:o}){if(!this.chatAreaEl)return;e||(e=[]);let r=this.chatAreaEl.querySelector(".chat-data-container"),a=this.chatAreaEl.querySelector("#bangdb-chat-area"),s=this.chatAreaEl.querySelector("#bdb-empty-state"),i=()=>{requestAnimationFrame(()=>{a.scrollTo({top:a.scrollHeight,behavior:"smooth"})})},l=e.length>0||!!t?.query;s.style.display=l?"none":"flex",this.renderedMessageIds.size>e.length&&this.resetRenderedMessages();for(let h=0;h<e.length;h++){let m=e[h],x=`msg-${h}`;if((m?.query||m?.files?.length>0)&&!this.renderedMessageIds.has(x)){this.renderedMessageIds.add(x),r.insertBefore(this.userRequestMessage(m.query,m.files,m.log_sid),r.querySelector("#chat-window-anchor"));let p=new I(m),{messages:y}=p.parse();m?.query&&y?.length>0&&r.insertBefore(this.aiResponseMessage(m),r.querySelector("#chat-window-anchor")),i()}}let d="bdb-streaming-msg",c=this.root.querySelector(`#${d}`);if(t?.query){c||(c=document.createElement("div"),c.id=d,r.insertBefore(c,r.querySelector("#chat-window-anchor")),c.appendChild(this.userRequestMessage(t.query)),c.style.minHeight=`${a.clientHeight*.8}px`);let h=c.querySelector(".bdb-streaming-ai-area"),m=t.output?.output??"";if(o&&!m&&!h)h=document.createElement("div"),h.className="bdb-streaming-ai-area",h.appendChild(this.loadingIndicator()),c.appendChild(h);else if(m){h||(h=document.createElement("div"),h.className="bdb-streaming-ai-area",c.appendChild(h));let x=h.querySelector(".ai-md");x?x.innerHTML=U(m):(h.innerHTML="",h.appendChild(this.aiResponseMessage(t)))}s.style.display="none",i()}else c&&(c.style.minHeight="0",c.remove());let u=this.chatAreaEl.querySelector(".bdb-textarea"),g=this.chatAreaEl.querySelector(".bdb-send-btn"),b=this.chatAreaEl.querySelector(".bdb-tools-trigger");if(u&&(u.style.opacity="1"),g){let h=this.store.getState(),m=h.chatSessions[h.chat.activeSession],x=(m?.queryFiles||[]).length>0,w=(m?.files||[]).some(p=>this.isFileBusy(p));g.disabled=!!o||w||!u?.value.trim()&&!x}b&&(b.disabled=!!o,b.style.opacity=o?"0.5":"1",b.style.pointerEvents=o?"none":"auto")}handleModeChange(e){let t=this.bubbleEl?.querySelector(".chat-bubble");if(t)switch(e){case 0:if(t.classList.remove("is-open"),t.setAttribute("aria-label","Open chat"),this.chatUIEl){let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-out 0.18s ease-in both",this.closeAnimationTimeout&&window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=window.setTimeout(()=>{this.chatUIEl&&(this.chatUIEl.style.display="none"),this.closeAnimationTimeout=null},180))}break;case 1:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open"),this.chatUIEl?.querySelector(".bdb-chat-panel")?.classList.remove("is-fullscreen"),this.renderPanelView(),requestAnimationFrame(()=>this.updatePanelPosition());break;case 2:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open");let o=this.chatUIEl?.querySelector(".bdb-chat-panel");o&&(o.classList.add("is-fullscreen"),o.style.top="",o.style.bottom="",o.style.left="",o.style.right=""),this.renderPanelView();break}}renderPanelView(){if(this.activePanelView==="sessions"){this.renderSessionList();return}this.renderChatView()}renderActiveSession(){let e=this.store.getState(),t=e.chatSessions[e.chat.activeSession];this.handleChatDataChange({chatData:t?.chatData,streamingMessage:t?.streamingMessage,isLoading:t?.isLoading})}renderChatView(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");e&&(this.activePanelView="chat",this.syncPanelHeader(),e.innerHTML="",this.chatAreaEl=null,this.resetRenderedMessages(),e.appendChild(this.chatArea()),this.renderActiveSession())}async renderSessionList(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");if(!e)return;let t=++this.sessionListRenderRequest;this.activePanelView="sessions",this.chatAreaEl=null,this.resetRenderedMessages(),this.syncPanelHeader(),e.innerHTML="",e.appendChild(this.sessionListLoadingView()),await this.fetchSessionsFromApi(),t===this.sessionListRenderRequest&&this.activePanelView==="sessions"&&(e.innerHTML="",e.appendChild(this.sessionListView()))}syncPanelHeader(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-title"),t=this.chatUIEl.querySelector(".js-sessions-btn"),o=this.chatUIEl.querySelector(".bdb-download-items");e&&(e.textContent=this.activePanelView==="sessions"?"Chats":this.getActiveSessionTitle()),t&&(t.style.display=this.activePanelView==="chat"?"flex":"none"),o&&(o.style.display=this.activePanelView==="chat"?"block":"none")}async openSession(e){this.store.dispatch(ae(e)),this.store.dispatch(fe(e));let t=await this.fetchSessionChatsFromApi(e);this.store.dispatch(ye({sessionId:e,chatData:t})),this.renderChatView()}async createNewSession(){let e=crypto.randomUUID().replace(/-/g,"").slice(0,10);(await B({baseURL:this.apiConfig?.backendURL,apikey:this.apiConfig?.apikey,payload:{userid:this.apiConfig?.userid,sessionid:e,name:this.apiConfig?.indexName,description:"New chat",org:this.apiConfig?.org}}))?.sid&&this.openSession(e)}getSessionTitle(e,t){let o=t?.description||"";return o.trim()?o.trim().slice(0,56):e==="GUEST_SESSION"?"Default chat":"New chat"}getActiveSessionTitle(){let e=this.store.getState(),t=e.chat.activeSession,o=e.chat.sessions||[];for(let r of o)try{let a=typeof r?.session=="string"?JSON.parse(r.session):r?.session||r;if(a?.sessionid===t){let s=a?.description||"";if(s.trim())return s.trim().slice(0,56)}}catch{}return"Chats"}updatePanelPosition(){if(!this.chatUIEl||!this.bubbleEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-panel");if(!e||e.classList.contains("is-fullscreen"))return;let t=parseInt(this.bubbleEl.style.left)||0,o=parseInt(this.bubbleEl.style.top)||0,r=52,a=52,s=750,i=window.innerWidth,l=window.innerHeight,d=12,c=o+a/2;if(c<l/3)e.style.top=a+d+"px",e.style.bottom="auto";else if(c>2*l/3)e.style.bottom=a+d+"px",e.style.top="auto";else{let u=c-s/2,g=Math.max(8,Math.min(u,l-s-8));e.style.top=g-o+"px",e.style.bottom="auto"}t+r/2<i/2?(e.style.left="0",e.style.right="auto"):(e.style.right="0",e.style.left="auto")}createChatBubble(){let e=document.createElement("div");e.classList.add("bdb-bubble-container"),e.innerHTML=`
1400
+ `,this.root.appendChild(t)}injectGlobalFonts(){let e="https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=JetBrains+Mono:ital,wght@0,100..800;1,100..800",t=document.createElement("link");t.rel="preconnect",t.href="https://fonts.googleapis.com";let o=document.createElement("link");o.rel="preconnect",o.href="https://fonts.gstatic.com",o.crossOrigin="";let r=document.createElement("link");r.type="text/css",r.rel="stylesheet",r.href=e,this.root.appendChild(t),this.root.appendChild(o),this.root.appendChild(r),document.head.appendChild(t),document.head.appendChild(o),document.head.appendChild(r)}mount(){let e=this.createChatBubble();this.root.appendChild(e),this.bubbleEl=e}async init(){this.store.dispatch(ne(this.apiConfig?.indexName))}async fetchSessionsFromApi(){if(!this.apiConfig)return;let{chat:e}=this.store.getState(),{activeIndex:t}=e;try{let r=(await de({payload:{filter:`{name = "${t}"}`,org:this.apiConfig.org,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey}))?.sessions||[];this.store.dispatch(ie(r))}catch{}}async fetchSessionChatsFromApi(e){if(!this.apiConfig)return[];try{let t=await ce({payload:{filter:`{org = "${this.apiConfig.org}" AND source ~ NA}`,sessionid:e,userid:this.apiConfig.userid},baseURL:this.apiConfig.backendURL,apikey:this.apiConfig.apikey});return Ce(t?.queries)?.reverse()}catch{return[]}}getClampedBubblePosition(e,t){let a=Math.max(24,window.innerWidth-52-24),s=Math.max(24,window.innerHeight-52-24);return{left:Math.min(Math.max(e,24),a),top:Math.min(Math.max(t,24),s)}}getDefaultBubblePosition(){return this.getClampedBubblePosition(window.innerWidth-24-52,window.innerHeight-24-52)}resetRenderedMessages(){if(this.renderedMessageIds.clear(),!this.chatAreaEl)return;let e=this.chatAreaEl.querySelector(".chat-data-container"),t=this.chatAreaEl.querySelector("#bdb-streaming-msg"),o=this.chatAreaEl.querySelector("#bdb-empty-state");t&&t.remove(),e&&(e.innerHTML='<div id="chat-window-anchor" style="overflow-anchor: auto; height: 1px;"></div>'),o&&(o.style.display="flex")}handleChatDataChange({chatData:e,streamingMessage:t,isLoading:o}){if(!this.chatAreaEl)return;e||(e=[]);let r=this.chatAreaEl.querySelector(".chat-data-container"),a=this.chatAreaEl.querySelector("#bangdb-chat-area"),s=this.chatAreaEl.querySelector("#bdb-empty-state"),i=()=>{requestAnimationFrame(()=>{a.scrollTo({top:a.scrollHeight,behavior:"smooth"})})},l=e.length>0||!!t?.query;s.style.display=l?"none":"flex",this.renderedMessageIds.size>e.length&&this.resetRenderedMessages();for(let h=0;h<e.length;h++){let m=e[h],x=`msg-${h}`;if((m?.query||m?.files?.length>0)&&!this.renderedMessageIds.has(x)){this.renderedMessageIds.add(x),r.insertBefore(this.userRequestMessage(m.query,m.files,m.log_sid),r.querySelector("#chat-window-anchor"));let p=new I(m),{messages:y}=p.parse();m?.query&&y?.length>0&&r.insertBefore(this.aiResponseMessage(m),r.querySelector("#chat-window-anchor")),i()}}let d="bdb-streaming-msg",c=this.root.querySelector(`#${d}`);if(t?.query){c||(c=document.createElement("div"),c.id=d,r.insertBefore(c,r.querySelector("#chat-window-anchor")),c.appendChild(this.userRequestMessage(t.query)),c.style.minHeight=`${a.clientHeight*.8}px`);let h=c.querySelector(".bdb-streaming-ai-area"),m=t.output?.output??"";if(o&&!m&&!h)h=document.createElement("div"),h.className="bdb-streaming-ai-area",h.appendChild(this.loadingIndicator()),c.appendChild(h);else if(m){h||(h=document.createElement("div"),h.className="bdb-streaming-ai-area",c.appendChild(h));let x=h.querySelector(".ai-md");x?x.innerHTML=U(m):(h.innerHTML="",h.appendChild(this.aiResponseMessage(t)))}s.style.display="none",i()}else c&&(c.style.minHeight="0",c.remove());let u=this.chatAreaEl.querySelector(".bdb-textarea"),g=this.chatAreaEl.querySelector(".bdb-send-btn"),b=this.chatAreaEl.querySelector(".bdb-tools-trigger");if(u&&(u.style.opacity="1"),g){let h=this.store.getState(),m=h.chatSessions[h.chat.activeSession],x=(m?.queryFiles||[]).length>0,w=(m?.files||[]).some(p=>this.isFileBusy(p));g.disabled=!!o||w||!u?.value.trim()&&!x}b&&(b.disabled=!!o,b.style.opacity=o?"0.5":"1",b.style.pointerEvents=o?"none":"auto")}handleModeChange(e){let t=this.bubbleEl?.querySelector(".chat-bubble");if(t)switch(e){case 0:if(t.classList.remove("is-open"),t.setAttribute("aria-label","Open chat"),this.chatUIEl){let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-out 0.18s ease-in both",this.closeAnimationTimeout&&window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=window.setTimeout(()=>{this.chatUIEl&&(this.chatUIEl.style.display="none"),this.closeAnimationTimeout=null},180))}break;case 1:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open"),this.chatUIEl?.querySelector(".bdb-chat-panel")?.classList.remove("is-fullscreen"),this.renderPanelView(),requestAnimationFrame(()=>this.updatePanelPosition());break;case 2:if(t.setAttribute("aria-label","Close chat"),this.closeAnimationTimeout&&(window.clearTimeout(this.closeAnimationTimeout),this.closeAnimationTimeout=null),!this.chatUIEl)this.chatUIEl=this.createChatUI(),this.bubbleEl?.appendChild(this.chatUIEl);else{this.chatUIEl.style.display="";let r=this.chatUIEl.querySelector(".bdb-chat-panel");r&&(r.style.animation="bdb-panel-in 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both")}t.classList.add("is-open");let o=this.chatUIEl?.querySelector(".bdb-chat-panel");o&&(o.classList.add("is-fullscreen"),o.style.top="",o.style.bottom="",o.style.left="",o.style.right=""),this.renderPanelView();break}}renderPanelView(){if(this.activePanelView==="sessions"){this.renderSessionList();return}this.renderChatView()}renderActiveSession(){let e=this.store.getState(),t=e.chatSessions[e.chat.activeSession];this.handleChatDataChange({chatData:t?.chatData,streamingMessage:t?.streamingMessage,isLoading:t?.isLoading})}renderChatView(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");e&&(this.activePanelView="chat",this.syncPanelHeader(),e.innerHTML="",this.chatAreaEl=null,this.resetRenderedMessages(),e.appendChild(this.chatArea()),this.renderActiveSession())}async renderSessionList(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-body");if(!e)return;let t=++this.sessionListRenderRequest;this.activePanelView="sessions",this.chatAreaEl=null,this.resetRenderedMessages(),this.syncPanelHeader(),e.innerHTML="",e.appendChild(this.sessionListLoadingView()),await this.fetchSessionsFromApi(),t===this.sessionListRenderRequest&&this.activePanelView==="sessions"&&(e.innerHTML="",e.appendChild(this.sessionListView()))}syncPanelHeader(){if(!this.chatUIEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-title"),t=this.chatUIEl.querySelector(".js-sessions-btn"),o=this.chatUIEl.querySelector(".bdb-download-items");e&&(e.textContent=this.activePanelView==="sessions"?"Chats":this.getActiveSessionTitle()),t&&(t.style.display=this.activePanelView==="chat"?"flex":"none"),o&&(o.style.display=this.activePanelView==="chat"?"block":"none")}async openSession(e){this.store.dispatch(ae(e)),this.store.dispatch(fe(e));let t=await this.fetchSessionChatsFromApi(e);this.store.dispatch(ye({sessionId:e,chatData:t})),this.renderChatView()}async createNewSession(){let e=crypto.randomUUID().replace(/-/g,"").slice(0,10);(await B({baseURL:this.apiConfig?.backendURL,apikey:this.apiConfig?.apikey,payload:{userid:this.apiConfig?.userid,sessionid:e,name:this.apiConfig?.indexName,description:"New chat",org:this.apiConfig?.org}}))?.sid&&this.openSession(e)}getSessionTitle(e,t){let o=t?.description||"";return o.trim()?o.trim().slice(0,56):e==="GUEST_SESSION"?"Default chat":"New chat"}getActiveSessionTitle(){let e=this.store.getState(),t=e.chat.activeSession,o=e.chat.sessions||[];for(let r of o)try{let a=typeof r?.session=="string"?JSON.parse(r.session):r?.session||r;if(a?.sessionid===t){let s=a?.description||"";if(s.trim())return s.trim().slice(0,56)}}catch{}return"Chats"}updatePanelPosition(){if(!this.chatUIEl||!this.bubbleEl)return;let e=this.chatUIEl.querySelector(".bdb-chat-panel");if(!e||e.classList.contains("is-fullscreen"))return;let t=parseInt(this.bubbleEl.style.left)||0,o=parseInt(this.bubbleEl.style.top)||0,r=52,a=52,s=e.offsetHeight,i=window.innerWidth,l=window.innerHeight,d=12,c=o+a/2;if(c<l/3)e.style.top=a+d+"px",e.style.bottom="auto";else if(c>2*l/3)e.style.bottom=a+d+"px",e.style.top="auto";else{let u=c-s/2,g=Math.max(8,Math.min(u,l-s-8));e.style.top=g-o+"px",e.style.bottom="auto"}t+r/2<i/2?(e.style.left="0",e.style.right="auto"):(e.style.right="0",e.style.left="auto")}createChatBubble(){let e=document.createElement("div");e.classList.add("bdb-bubble-container"),e.innerHTML=`
1401
1401
  <style>
1402
1402
  .bdb-bubble-container {
1403
1403
  position: fixed;
@@ -1786,8 +1786,8 @@ ${a}
1786
1786
  position: absolute;
1787
1787
  bottom: 68px;
1788
1788
  right: 0;
1789
- width: 500px;
1790
- height: 750px;
1789
+ width: clamp(320px, 35vw, 500px);
1790
+ height: clamp(500px, 80vh, 750px);
1791
1791
  background: var(--background);
1792
1792
  border-radius: var(--radius-lg);
1793
1793
  box-shadow: var(--shadow-panel);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bangdb/web-sdk",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "private": false,
5
5
  "description": "A lightweight JavaScript SDK for seamlessly embedding BangDB’s AI-powered search and chat features into your web or internet applications.",
6
6
  "publishConfig": {