(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[3111],{91955:function(e,t,s){Promise.resolve().then(s.bind(s,5506))},5506:function(e,t,s){"use strict";s.r(t),s.d(t,{default:function(){return m}});var n=s(57437),o=s(11930),a=s.n(o),i=s(2265),r=s(48861),c=s(82697),d=s(58485);s(7395);var l=s(69591),u=s(79306),h=s(59479),g=s(9557);function p(e){let[t,s]=(0,i.useState)(""),[o,r]=(0,i.useState)(null),[d,l]=(0,i.useState)(!1),[u,g]=(0,i.useState)(null),p=e.setQueryToProcess,m=e.streamedMessages;return((0,i.useEffect)(()=>{o&&e.setImage64(encodeURIComponent(o))},[o,e.setImage64]),(0,i.useEffect)(()=>{t&&(l(!0),p(t))},[t,p]),(0,i.useEffect)(()=>{m&&m.length>0&&m[m.length-1].completed?l(!1):s("")},[m]),e.publicConversationSlug||e.conversationId)?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)("div",{className:a().chatBodyFull,children:(0,n.jsx)(c.Z,{publicConversationSlug:e.publicConversationSlug,conversationId:e.conversationId||"",setAgent:g,setTitle:e.setTitle,pendingMessage:d?t:"",incomingMessages:e.streamedMessages})}),(0,n.jsx)("div",{className:"".concat(a().inputBox," p-1 md:px-2 shadow-md bg-background align-middle items-center justify-center dark:bg-neutral-700 dark:border-0 dark:shadow-sm rounded-t-2xl rounded-b-none md:rounded-xl"),children:(0,n.jsx)(h.Z,{isLoggedIn:e.isLoggedIn,sendMessage:e=>s(e),sendImage:e=>r(e),sendDisabled:d,chatOptionsData:e.chatOptionsData,conversationId:e.conversationId,agentColor:null==u?void 0:u.color,isMobileWidth:e.isMobileWidth,setUploadedFiles:e.setUploadedFiles})})]}):(0,n.jsx)("div",{className:a().suggestions,children:"Whoops, nothing to see here!"})}function m(){let[e,t]=(0,i.useState)(null),[s,o]=(0,i.useState)(!0),[c,h]=(0,i.useState)("Khoj AI - Chat"),[m,f]=(0,i.useState)(void 0),[_,x]=(0,i.useState)([]),[y,v]=(0,i.useState)(""),[C,b]=(0,i.useState)(!1),[S,j]=(0,i.useState)([]),[I,B]=(0,i.useState)(void 0),[w,O]=(0,i.useState)(""),N=(0,l.k6)()||{timezone:Intl.DateTimeFormat().resolvedOptions().timeZone},k=(0,u.GW)(),D=(0,l.IC)();async function T(e){if(!e.ok)throw Error(e.statusText);if(!e.body)throw Error("Response body is null");let t=e.body.getReader(),s=new TextDecoder,n="␃\uD83D\uDD1A␗",o="";for(;;){let e;let{done:a,value:i}=await t.read();if(a){v(""),b(!1),O("");break}for(o+=s.decode(i,{stream:!0});-1!==(e=o.indexOf(n));){let t=o.slice(0,e);if(o=o.slice(e+n.length),t){let e=_.find(e=>!e.completed);if(!e){console.error("No current message found");return}(0,g.VK)(t,e),x([..._])}}}}async function E(){if(!y||!m)return;let e={q:y,conversation_id:m,stream:!0,...N&&{region:N.region,country:N.country,city:N.city,country_code:N.countryCode,timezone:N.timezone},...w&&{image:w}},t=await fetch("/api/chat?client=web",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)});try{await T(t)}catch(e){console.error(e)}}return((0,i.useEffect)(()=>{fetch("/api/chat/options").then(e=>e.json()).then(e=>{o(!1),e&&t(e)}).catch(e=>{console.error(e)}),(0,l.EK)(),B(window.location.pathname.split("/").pop()||"")},[]),(0,i.useEffect)(()=>{if(y&&!m){fetch("/api/chat/share/fork?public_conversation_slug=".concat(I),{method:"POST",headers:{"Content-Type":"application/json"}}).then(e=>e.json()).then(e=>{f(e.conversation_id)}).catch(e=>{console.error(e)});return}if(y){let e={rawResponse:"",trainOfThought:[],context:[],onlineContext:{},completed:!1,timestamp:new Date().toISOString(),rawQuery:y||"",uploadedImageData:decodeURIComponent(w)};x(t=>[...t,e]),b(!0)}},[y,m,I]),(0,i.useEffect)(()=>{C&&E()},[C]),s)?(0,n.jsx)(d.Z,{}):I?(0,n.jsxs)("div",{className:"".concat(a().main," ").concat(a().chatLayout),children:[(0,n.jsx)("title",{children:c}),(0,n.jsx)("div",{className:a().sidePanel,children:(0,n.jsx)(r.ZP,{conversationId:null!=m?m:null,uploadedFiles:S,isMobileWidth:D})}),(0,n.jsx)("div",{className:a().chatBox,children:(0,n.jsx)("div",{className:a().chatBoxBody,children:(0,n.jsx)(i.Suspense,{fallback:(0,n.jsx)(d.Z,{}),children:(0,n.jsx)(p,{conversationId:m,streamedMessages:_,setQueryToProcess:v,isLoggedIn:null!==k,publicConversationSlug:I,chatOptionsData:e,setTitle:h,setUploadedFiles:j,isMobileWidth:D,setImage64:O})})})})]}):(0,n.jsx)("div",{className:a().suggestions,children:"Whoops, nothing to see here!"})}},11930:function(e){e.exports={main:"sharedChat_main__7Nayy",suggestions:"sharedChat_suggestions__V8kr_",inputBox:"sharedChat_inputBox__wRW5A",chatBodyFull:"sharedChat_chatBodyFull__O1MOv",chatBody:"sharedChat_chatBody__OnHDL",chatLayout:"sharedChat_chatLayout__gutlc",chatBox:"sharedChat_chatBox__PmAPg",titleBar:"sharedChat_titleBar__vOHp_",chatBoxBody:"sharedChat_chatBoxBody__ef2Nl",agentIndicator:"sharedChat_agentIndicator__ORCl4"}}},function(e){e.O(0,[7849,647,929,3954,9001,3062,4086,121,3110,4051,1603,9417,9178,9479,2697,2971,7023,1744],function(){return e(e.s=91955)}),_N_E=e.O()}]);