1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[3066],{41444:(e,t,r)=>{r.d(t,{v:()=>s});let a={enabled:!1,level:"info",modules:{api:!1,stream:!1,page:!1,fileViewer:!1,auth:!0,agents:!0}},o={...a,enabled:!1},n=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"info";if(!o.enabled||!o.modules[e])return!1;let r=["debug","info","warn","error"],a=r.indexOf(o.level);return r.indexOf(t)>=a},s={api:{debug:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("api","debug")&&console.log("[API] ".concat(e),...r)},info:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("api","info")&&console.log("[API] ".concat(e),...r)},warn:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("api","warn")&&console.warn("[API] ".concat(e),...r)},error:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("api","error")&&console.error("[API] ".concat(e),...r)}},stream:{debug:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("stream","debug")&&console.log("[STREAM] ".concat(e),...r)},info:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("stream","info")&&console.log("[STREAM] ".concat(e),...r)},warn:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("stream","warn")&&console.warn("[STREAM] ".concat(e),...r)},error:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("stream","error")&&console.error("[STREAM] ".concat(e),...r)}},page:{debug:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("page","debug")&&console.log("[PAGE] ".concat(e),...r)},info:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("page","info")&&console.log("[PAGE] ".concat(e),...r)},warn:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("page","warn")&&console.warn("[PAGE] ".concat(e),...r)},error:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("page","error")&&console.error("[PAGE] ".concat(e),...r)}},fileViewer:{debug:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("fileViewer","debug")&&console.log("[FILE VIEWER] ".concat(e),...r)},info:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("fileViewer","info")&&console.log("[FILE VIEWER] ".concat(e),...r)},warn:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("fileViewer","warn")&&console.warn("[FILE VIEWER] ".concat(e),...r)},error:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("fileViewer","error")&&console.error("[FILE VIEWER] ".concat(e),...r)}},auth:{debug:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("auth","debug")&&console.log("[AUTH] ".concat(e),...r)},info:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("auth","info")&&console.log("[AUTH] ".concat(e),...r)},warn:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("auth","warn")&&console.warn("[AUTH] ".concat(e),...r)},error:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("auth","error")&&console.error("[AUTH] ".concat(e),...r)}},agents:{debug:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("agents","debug")&&console.log("[AGENTS] ".concat(e),...r)},info:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("agents","info")&&console.log("[AGENTS] ".concat(e),...r)},warn:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("agents","warn")&&console.warn("[AGENTS] ".concat(e),...r)},error:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return n("agents","error")&&console.error("[AGENTS] ".concat(e),...r)}},enableModule:e=>{},disable:()=>{}}},53783:(e,t,r)=>{r.d(t,{hS:()=>s,nQ:()=>c,uC:()=>i});var a=r(18720),o=r(90960);let n=e=>{switch(e){case 400:return"Invalid request. Please check your input and try again.";case 401:return"Authentication required. Please sign in again.";case 403:return"Access denied. You don't have permission to perform this action.";case 404:return"The requested resource was not found.";case 408:return"Request timeout. Please try again.";case 409:return"Conflict detected. The resource may have been modified by another user.";case 422:return"Invalid data provided. Please check your input.";case 429:return"Too many requests. Please wait a moment and try again.";case 500:return"Server error. Our team has been notified.";case 502:return"Service temporarily unavailable. Please try again in a moment.";case 503:return"Service maintenance in progress. Please try again later.";case 504:return"Request timeout. The server took too long to respond.";default:return"An unexpected error occurred. Please try again."}},s=(e,t)=>{if(console.error("API Error:",e,t),!((e,t)=>(null==t||!t.silent)&&!(e instanceof o.Ey)&&((null==e?void 0:e.status)!==404||null==t||!t.resource))(e,t))return;let r=((e,t)=>{var r;if(!(null==t?void 0:t.operation)&&!(null==t?void 0:t.resource))return e;let a=[];t.operation&&a.push("Failed to ".concat(t.operation)),t.resource&&a.push(t.resource);let o=a.join(" ");return e.toLowerCase().includes((null==(r=t.operation)?void 0:r.toLowerCase())||"")?e:"".concat(o,": ").concat(e)})((e=>{if(e instanceof o.Ey){var t;return(null==(t=e.detail)?void 0:t.message)||e.message||"Billing issue detected"}return e instanceof Error?e.message:(null==e?void 0:e.response)?n(e.response.status):(null==e?void 0:e.status)?n(e.status):"string"==typeof e?e:(null==e?void 0:e.message)?e.message:(null==e?void 0:e.error)?"string"==typeof e.error?e.error:e.error.message||"Unknown error":"An unexpected error occurred"})(e),t);(null==e?void 0:e.status)>=500?a.oR.error(r,{description:"Our team has been notified and is working on a fix.",duration:6e3}):(null==e?void 0:e.status)===401?a.oR.error(r,{description:"Please refresh the page and sign in again.",duration:8e3}):(null==e?void 0:e.status)===403?a.oR.error(r,{description:"Contact support if you believe this is an error.",duration:6e3}):(null==e?void 0:e.status)===429?a.oR.warning(r,{description:"Please wait a moment before trying again.",duration:5e3}):a.oR.error(r,{duration:5e3})},c=(e,t)=>{var r,o,n;(null==e||null==(r=e.message)?void 0:r.includes("fetch"))||(null==e||null==(o=e.message)?void 0:o.includes("network"))||(null==e||null==(n=e.message)?void 0:n.includes("connection"))||(null==e?void 0:e.code)==="NETWORK_E
1RROR"||!navigator.onLine?a.oR.error("Connection error",{description:"Please check your internet connection and try again.",duration:6e3}):s(e,t)},i=(e,t)=>{a.oR.success(e,{description:t,duration:3e3})}},90960:(e,t,r)=>{r.d(t,{A3:()=>b,Bp:()=>F,C6:()=>V,D_:()=>h,ED:()=>G,Et:()=>L,Ey:()=>l,GB:()=>_,Ih:()=>k,JQ:()=>p,Kc:()=>R,Ks:()=>J,M2:()=>d,MA:()=>j,Nw:()=>E,RQ:()=>I,U1:()=>f,VK:()=>q,VL:()=>S,XY:()=>C,_L:()=>A,cF:()=>P,cL:()=>M,eD:()=>x,fG:()=>y,fu:()=>B,fw:()=>O,gA:()=>w,gS:()=>U,m9:()=>T,ti:()=>D,uV:()=>z,vr:()=>m,xx:()=>v,ye:()=>g});var a=r(96648),o=r(53783),n=r(41444);let s="https://snwolley.ai/api",c=new Set,i=new Map;class l extends Error{constructor(e,t,r){super(r||t.message||"Billing Error: ".concat(e)),this.name="BillingError",this.status=e,this.detail=t,Object.setPrototypeOf(this,l.prototype)}}class u extends Error{constructor(e,t){super(e||"No access token available",t),this.name="NoAccessTokenAvailableError"}}let d=async()=>{try{let e=(0,a.U)(),{data:{session:t}}=await e.auth.getSession();if(!(null==t?void 0:t.access_token))throw new u;let r=await fetch("".concat(s,"/billing/cancel-subscription"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(t.access_token)}});if(!r.ok){let e=await r.text().catch(()=>"No error details available");throw console.error("Error cancelling subscription: ".concat(r.status," ").concat(r.statusText),e),Error("Error cancelling subscription: ".concat(r.statusText," (").concat(r.status,")"))}return r.json()}catch(e){throw console.error("Failed to cancel subscription:",e),(0,o.hS)(e,{operation:"cancel subscription",resource:"subscription"}),e}},h=async e=>{try{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw new u;let o=await fetch("".concat(s,"/billing/subscription-commitment/").concat(e),{headers:{Authorization:"Bearer ".concat(r.access_token)}});if(!o.ok){let e=await o.text().catch(()=>"No error details available");throw console.error("Error getting subscription commitment: ".concat(o.status," ").concat(o.statusText),e),Error("Error getting subscription commitment: ".concat(o.statusText," (").concat(o.status,")"))}return o.json()}catch(e){if(e instanceof u)throw e;throw console.error("Failed to get subscription commitment:",e),(0,o.hS)(e,{operation:"load subscription commitment",resource:"commitment information"}),e}},g=async()=>{try{let e=(0,a.U)(),{data:{session:t}}=await e.auth.getSession();if(!(null==t?void 0:t.access_token))throw new u;let r=await fetch("".concat(s,"/billing/reactivate-subscription"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(t.access_token)}});if(!r.ok){let e=await r.text().catch(()=>"No error details available");throw console.error("Error reactivating subscription: ".concat(r.status," ").concat(r.statusText),e),Error("Error reactivating subscription: ".concat(r.statusText," (").concat(r.status,")"))}return r.json()}catch(e){throw console.error("Failed to reactivate subscription:",e),(0,o.hS)(e,{operation:"reactivate subscription",resource:"subscription"}),e}},p=async()=>{try{let e=(0,a.U)(),{data:t,error:r}=await e.auth.getUser();if(r)return console.error("Error getting current user:",r),[];if(!t.user)return n.v.api.debug("No user logged in, returning empty projects array"),[];let{data:o,error:s}=await e.from("projects").select("*").eq("account_id",t.user.id);
1if(s){if("42501"===s.code&&s.message.includes("has_role_on_account"))return console.error("Permission error: User does not have proper account access"),[];throw s}n.v.api.debug("Raw projects from DB:",null==o?void 0:o.length,o);let c=(o||[]).map(e=>({id:e.project_id,name:e.name||"",description:e.description||"",account_id:e.account_id,created_at:e.created_at,updated_at:e.updated_at,sandbox:e.sandbox||{id:"",pass:"",vnc_preview:"",sandbox_url:""}}));return n.v.api.debug("Mapped projects for frontend:",c.length),c}catch(e){return console.error("Error fetching projects:",e),(0,o.hS)(e,{operation:"load projects",resource:"projects"}),[]}},f=async e=>{let t=(0,a.U)();try{var r;let{data:a,error:o}=await t.from("projects").select("*").eq("project_id",e).single();if(o){if("PGRST116"===o.code)throw Error("Project not found or not accessible: ".concat(e));throw o}return n.v.api.debug("Raw project data from database:",a),(null==(r=a.sandbox)?void 0:r.id)&&(async()=>{try{let{data:{session:r}}=await t.auth.getSession(),a={"Content-Type":"application/json"};(null==r?void 0:r.access_token)&&(a.Authorization="Bearer ".concat(r.access_token)),n.v.api.debug("Ensuring sandbox is active for project ".concat(e,"..."));let o=await fetch("".concat(s,"/project/").concat(e,"/sandbox/ensure-active"),{method:"POST",headers:a});if(o.ok)n.v.api.debug("Sandbox activation successful");else{let e=await o.text().catch(()=>"No error details available");console.warn("Failed to ensure sandbox is active: ".concat(o.status," ").concat(o.statusText),e)}}catch(e){n.v.api.warn("Failed to ensure sandbox is active:",e)}})(),{id:a.project_id,name:a.name||"",description:a.description||"",account_id:a.account_id,is_public:a.is_public||!1,created_at:a.created_at,sandbox:a.sandbox||{id:"",pass:"",vnc_preview:"",sandbox_url:""}}}catch(t){throw console.error("Error fetching project ".concat(e,":"),t),(0,o.hS)(t,{operation:"load project",resource:"project ".concat(e)}),t}},w=async(e,t)=>{let r=(0,a.U)();if(!t){let{data:e,error:a}=await r.auth.getUser();if(a)throw a;if(!e.user)throw Error("You must be logged in to create a project");t=e.user.id}let{data:n,error:s}=await r.from("projects").insert({name:e.name,description:e.description||null,account_id:t}).select().single();if(s)throw(0,o.hS)(s,{operation:"create project",resource:"project"}),s;return{id:n.project_id,name:n.name,description:n.description||"",account_id:n.account_id,created_at:n.created_at,sandbox:{id:"",pass:"",vnc_preview:""}}},m=async(e,t)=>{let r=(0,a.U)();if(n.v.api.debug("Updating project with ID:",e),n.v.api.debug("Update data:",t),!e||""===e)throw n.v.api.error("Attempted to update project with invalid ID:",e),Error("Cannot update project: Invalid project ID");let{data:s,error:c}=await r.from("projects").update(t).eq("project_id",e).select().single();if(c)throw console.error("Error updating project:",c),(0,o.hS)(c,{operation:"update project",resource:"project ".concat(e)}),c;if(!s){let t=Error("No data returned from update");throw(0,o.hS)(t,{operation:"update project",resource:"project ".concat(e)}),t}return window.dispatchEvent(new CustomEvent("project-updated",{detail:{projectId:e,updatedData:{id:s.project_id,name:s.name,description:s.description}}})),{id:s.project_id,name:s.name,description:s.description||"",account_id:s.account_id,created_at:s.created_at,sandbox:s.sandbox||{id:"",pass:"",vnc_preview:"",sandbox_url:""}}},v=async e=>{let t=(0,a.U)(),{error:r}=await t.from("projects").delete().eq("project_id",e);if(r)throw(0,o.hS)(r,{operation:"delete project",resource:"project ".concat(e)}),r},b=async e=>{let t=(0,a.U)(),{data:r,error:s}=await t.auth.getUser();if(s)return console.error("Error getting current user:",s),[];if(!r.user)return n.v.api.debug("No user logged in, returning empty threads array"),[];let c=t.from("threads").select("*");c=c.eq("account_id",r.user.id),e&&(n.v.api.debug("Filtering threads by project_id:",e),c=c.eq("project_id",e));let{data:i,error:l}=await c;if(l)throw(0,o.hS)(l,{operation:"load threads",resource:e?"threads for project ".concat(e):"threads"}),l;return(i||[]).filter(e=>!(e.metadata||{}).is_agent_builder).map(e=>({thread_id:e.thread_id,account_id:e.account_id,project_id:e.project_id,created_at:e.created_at,updated_at:e.updated_at,metadata:e.metadata}))},y=async e=>{let t=(0,a.U)(),{data:r,error:n}=await t.from("threads").select("*").eq("thread_id",e).single();if(n)throw(0,o.hS)(n,{operation:"load thread",resource:"thread ".concat(e)}),n;return r},E=async e=>{let t=(0,a.U)(),{data:{user:r}}=await t.auth.getUser();if(!r)throw Error("You must be logged in to create a thread");let{data:n,error:s}=await t.from("threads").insert({project_id:e,account_id:r.id}).select().single();if(s)throw(0,o.hS)(s,{operation:"create thread",resource:"thread"}),s;return n},_=async(e,t)=>{let r=(0,a.U)(),{error:n}=await r.from("messages").insert({thread_id:e,type:"user",is_llm_message:!0,content:JSON.stringify({role:"user",content:t})});if(n)throw console.error("Error adding user message:",n),(0,o.hS)(n,{operation:"add message",resource:"message"}),Error("Error adding message: ".concat(n.message))},S=async e=>{let t=(0,a.U)(),r=[],s=0,c=!0;for(;c;){let{data:a,error:n}=await t.from("messages").select("\n *,\n agents:agent_id (\n name,\n avatar,\n avatar_color\n )\n ").eq("thread_id",e).neq("type","cost").neq("type","summary").order("created_at",{ascending:!0}).range(s,s+1e3-1);
1if(n)throw console.error("Error fetching messages:",n),(0,o.hS)(n,{operation:"load messages",resource:"messages for thread ".concat(e)}),Error("Error getting messages: ".concat(n.message));a&&a.length>0?(r=r.concat(a),s+=1e3,c=1e3===a.length):c=!1}return n.v.api.debug("Messages fetched count:",r.length),r},k=async(e,t)=>{try{let r=(0,a.U)(),{data:{session:o}}=await r.auth.getSession();if(!(null==o?void 0:o.access_token))throw new u;if(!s)throw Error("Backend URL is not configured. Set NEXT_PUBLIC_BACKEND_URL in your environment.");n.v.api.debug("Starting agent for thread ".concat(e," using ").concat(s,"/thread/").concat(e,"/agent/start"));let c={model_name:"claude-3-7-sonnet-latest",enable_thinking:!1,reasoning_effort:"low",stream:!0,...t},i={model_name:c.model_name,enable_thinking:c.enable_thinking,reasoning_effort:c.reasoning_effort,stream:c.stream};c.agent_id&&(i.agent_id=c.agent_id);let d=await fetch("".concat(s,"/thread/").concat(e,"/agent/start"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(o.access_token)},body:JSON.stringify(i)});if(!d.ok){if(402===d.status)try{let e=await d.json();n.v.api.error("Billing error starting agent (402):",e);let t=(null==e?void 0:e.detail)||{message:"Payment Required"};throw"string"!=typeof t.message&&(t.message="Payment Required"),new l(d.status,t)}catch(e){throw console.error("[API] Could not parse 402 error response body:",e),new l(d.status,{message:"Payment Required"},"Error starting agent: ".concat(d.statusText," (402)"))}let e=await d.text().catch(()=>"No error details available");if(403===d.status){try{let t=JSON.parse(e);if(t.message&&t.message.includes("disk quota exceeded"))throw Error("Storage quota exceeded. The system is automatically cleaning up old files and will retry shortly. Please try again in a moment.")}catch(t){if(e.includes("disk quota exceeded")||e.includes("Total disk quota exceeded"))throw Error("Storage quota exceeded. The system is automatically cleaning up old files and will retry shortly. Please try again in a moment.")}throw Error("Access forbidden: Please check your permissions")}throw console.error("[API] Error starting agent: ".concat(d.status," ").concat(d.statusText),e),Error("Error starting agent: ".concat(d.statusText," (").concat(d.status,")"))}return await d.json()}catch(e){if(e instanceof l||e instanceof u)throw e;if(n.v.api.error("Failed to start agent:",e),e instanceof TypeError&&e.message.includes("Failed to fetch")){let e=Error("Cannot connect to backend server. Please check your internet connection and make sure the backend is running.");throw(0,o.hS)(e,{operation:"start agent",resource:"AI assistant"}),e}throw(0,o.hS)(e,{operation:"start agent",resource:"AI assistant"}),e}},x=async e=>{c.add(e);let t=i.get(e);t&&(t.close(),i.delete(e));let r=(0,a.U)(),{data:{session:n}}=await r.auth.getSession();if(!(null==n?void 0:n.access_token)){let e=new u;throw(0,o.hS)(e,{operation:"stop agent",resource:"AI assistant"}),e}let l=await fetch("".concat(s,"/agent-run/").concat(e,"/stop"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(n.access_token)},cache:"no-store"});if(!l.ok){let e=Error("Error stopping agent: ".concat(l.statusText));throw(0,o.hS)(e,{operation:"stop agent",resource:"AI assistant"}),e}},A=async e=>{if(n.v.api.debug("Requesting agent status for ".concat(e)),c.has(e))throw Error("Agent run ".concat(e," is not running"));try{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw n.v.api.error("No access token available for getAgentStatus"),new u;let o="".concat(s,"/agent-run/").concat(e);n.v.api.debug("Fetching from: ".concat(o));let i=await fetch(o,{headers:{Authorization:"Bearer ".concat(r.access_token)},cache:"no-store"});if(!i.ok){let t=await i.text().catch(()=>"No error details available");throw console.error("[API] Error getting agent status: ".concat(i.status," ").concat(i.statusText),t),404===i.status&&c.add(e),Error("Error getting agent status: ".concat(i.statusText," (").concat(i.status,")"))}let l=await i.json();return n.v.api.debug("Successfully got agent status:",l),"running"!==l.status&&c.add(e),l}catch(e){throw n.v.api.error("Failed to get agent status:",e),(0,o.hS)(e,{operation:"get agent status",resource:"AI assistant status",silent:!0}),e}},T=async e=>{try{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw new u;let o=await fetch("".concat(s,"/thread/").concat(e,"/agent-runs"),{headers:{Authorization:"Bearer ".concat(r.access_token)},cache:"no-store"});if(!o.ok)throw Error("Error getting agent runs: ".concat(o.statusText));return(await o.json()).agent_runs||[]}catch(e){if(e instanceof u)throw e;throw console.error("Failed to get agent runs:",e),(0,o.hS)(e,{operation:"load agent runs",resource:"conversation history"}),e}},j=(e,t)=>{n.v.stream.debug("streamAgent called for ".concat(e)),c.has(e)&&(n.v.stream.debug("[STREAM] Agent run ".concat(e," was previously marked as
1non-running, but checking again...")),c.delete(e));let r=i.get(e);r&&(n.v.stream.debug("[STREAM] Stream already exists for ".concat(e,", closing it first")),r.close(),i.delete(e));try{return(async()=>{try{let r=await A(e);if("running"!==r.status&&"initializing"!==r.status){n.v.stream.debug("[STREAM] Agent run ".concat(e," is not running (status: ").concat(r.status,"), not creating stream")),c.add(e),t.onError("Agent run ".concat(e," is not running (status: ").concat(r.status,")")),t.onClose();return}}catch(a){n.v.stream.error("Error verifying agent run ".concat(e,":"),a);let r=a instanceof Error?a.message:String(a);(r.includes("not found")||r.includes("404")||r.includes("does not exist"))&&(n.v.stream.debug("[STREAM] Agent run ".concat(e," not found, not creating stream")),c.add(e)),t.onError(r),t.onClose();return}let r=(0,a.U)(),{data:{session:o}}=await r.auth.getSession();if(!(null==o?void 0:o.access_token)){let e=new u;n.v.stream.error("No auth token available"),t.onError(e),t.onClose();return}let l=new URL("".concat(s,"/agent-run/").concat(e,"/stream"));l.searchParams.append("token",o.access_token),n.v.stream.debug("Creating EventSource for ".concat(e));let d=new EventSource(l.toString());i.set(e,d),d.onopen=()=>{n.v.stream.debug("Connection opened for ".concat(e))},d.onmessage=r=>{try{let a=r.data;if(a.includes('"type":"ping"'))return;if(!a||""===a.trim())return void n.v.stream.debug("Received empty message, skipping");try{let r=JSON.parse(a);if("heartbeat"===r.type)return void n.v.stream.debug("Heartbeat received for ".concat(e,":"),r.timestamp);if("error"===r.status){n.v.stream.error("Error status received for ".concat(e,":"),r),t.onError(r.message||"Unknown error occurred");return}if("status"===r.type&&["completed","stopped","failed"].includes(r.status)){n.v.stream.debug("Completion status received for ".concat(e,":"),r.status),t.onMessage(a);return}}catch(e){}if(a.includes("Agent run")&&a.includes("not found in active runs")){n.v.stream.debug("[STREAM] Agent run ".concat(e," not found in active runs, closing stream")),c.add(e),t.onError("Agent run not found in active runs"),d.close(),i.delete(e),t.onClose();return}if(a.includes('"type":"status"')&&a.includes('"status":"completed"')){(a.includes("Run data not available for streaming")||a.includes("Stream ended with status: completed"))&&c.add(e),t.onMessage(a),d.close(),i.delete(e),t.onClose();return}if(a.includes('"type":"status"')&&a.includes('"status_type":"thread_run_end"')){c.add(e),t.onMessage(a),d.close(),i.delete(e),t.onClose();return}t.onMessage(a)}catch(e){n.v.stream.error("Error handling message:",e),t.onError(e instanceof Error?e:String(e))}},d.onerror=r=>{n.v.stream.debug("EventSource error for ".concat(e,":"),r),A(e).then(r=>{"running"!==r.status&&(c.add(e),d.close(),i.delete(e),t.onClose())}).catch(r=>{console.error("[STREAM] Error checking agent status after stream error:",r);let a=r instanceof Error?r.message:String(r);(a.includes("not found")||a.includes("404")||a.includes("does not exist"))&&(c.add(e),d.close(),i.delete(e),t.onClose()),t.onError(a)})}})(),()=>{n.v.stream.debug("Cleanup called for ".concat(e));let t=i.get(e);t&&(n.v.stream.debug("Closing stream for ".concat(e)),t.close(),i.delete(e))}}catch(r){return n.v.stream.error("Error setting up stream for ".concat(e,":"),r),t.onError(r instanceof Error?r:String(r)),t.onClose(),()=>{}}},U=async(e,t,r)=>{try{let o=(0,a.U)(),{data:{session:n}}=await o.auth.getSession(),c=new FormData;c.append("path",t);let i=new Blob([r],{type:"application/octet-stream"});c.append("file",i,t.split("/").pop()||"file");let l={};(null==n?void 0:n.access_token)&&(l.Authorization="Bearer ".concat(n.access_token));let u=await fetch("".concat(s,"/sandboxes/").concat(e,"/files"),{method:"POST",headers:l,body:c});if(!u.ok){let e=await u.text().catch(()=>"No error details available");throw console.error("Error creating sandbox file: ".concat(u.status," ").concat(u.statusText),e),Error("Error creating sandbox file: ".concat(u.statusText," (").concat(u.status,")"))}return await u.json()}catch(e){throw console.error("Failed to create sandbox file:",e),(0,o.hS)(e,{operation:"create file",resource:"file ".concat(t)}),e}},P=async(e,t,r)=>{try{let o=(0,a.U)(),{data:{session:n}}=await o.auth.getSession(),c={"Content-Type":"application/json"};(null==n?void 0:n.access_token)&&(c.Authorization="Bearer ".concat(n.access_token));let i=await fetch("".concat(s,"/sandboxes/").concat(e,"/files/json"),{method:"POST",headers:c,body:JSON.stringify({path:t,content:r})});if(!i.ok){let e=await i.text().catch(()=>"No error details available");throw console.error("Error creating sandbox file (JSON): ".concat(i.status," ").concat(i.statusText),e),Error("Error creating sandbox file: ".concat(i.statusText," (").concat(i.status,")"))}return await i.json()}catch(e){throw console.error("Failed to create sandbox file with JSON:",e),(0,o.hS)(e,{operation:"create file",resource:"file ".concat(t)}),e}};function N(e){try{return e.replace(/\\u([0-9a-fA-F]{4})/g,(e,t)=>
1String.fromCharCode(parseInt(t,16)))}catch(t){return console.error("Error processing Unicode escapes in path:",t),e}}let I=async function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"/workspace";try{let r=(0,a.U)(),{data:{session:o}}=await r.auth.getSession(),n=new URL("".concat(s,"/projects/").concat(e,"/files")),c=N(t);n.searchParams.append("path",c);let i={};(null==o?void 0:o.access_token)&&(i.Authorization="Bearer ".concat(o.access_token));let l=await fetch(n.toString(),{headers:i});if(!l.ok){let e=await l.text().catch(()=>"No error details available");throw console.error("Error listing project files: ".concat(l.status," ").concat(l.statusText),e),Error("Error listing project files: ".concat(l.statusText," (").concat(l.status,")"))}let u=await l.json();return{files:u.files||[],sandbox_id:u.sandbox_id}}catch(e){throw console.error("Failed to list project files:",e),e}},C=async(e,t)=>{try{let r=(0,a.U)(),{data:{session:o}}=await r.auth.getSession(),n=new URL("".concat(s,"/sandboxes/").concat(e,"/files")),c=N(t);n.searchParams.append("path",c);let i={};(null==o?void 0:o.access_token)&&(i.Authorization="Bearer ".concat(o.access_token));let l=await fetch(n.toString(),{headers:i});if(!l.ok){let e=await l.text().catch(()=>"No error details available");throw console.error("Error listing sandbox files: ".concat(l.status," ").concat(l.statusText),e),Error("Error listing sandbox files: ".concat(l.statusText," (").concat(l.status,")"))}return(await l.json()).files||[]}catch(e){throw console.error("Failed to list sandbox files:",e),e}},R=async()=>{try{let e=(0,a.U)(),{data:t,error:r}=await e.from("threads").select("project_id").eq("is_public",!0);if(r)return console.error("Error fetching public threads:",r),[];if(!(null==t?void 0:t.length))return[];let o=[...new Set(t.map(e=>e.project_id))].filter(Boolean);if(!o.length)return[];let{data:s,error:c}=await e.from("projects").select("*").in("project_id",o);if(c)return console.error("Error fetching public projects:",c),[];let i=(s||[]).map(e=>({id:e.project_id,name:e.name||"",description:e.description||"",account_id:e.account_id,created_at:e.created_at,updated_at:e.updated_at,sandbox:e.sandbox||{id:"",pass:"",vnc_preview:"",sandbox_url:""},is_public:!0}));return n.v.api.debug("[API] Mapped public projects for frontend:",i.length),i}catch(e){return console.error("Error fetching public projects:",e),(0,o.hS)(e,{operation:"load public projects",resource:"public projects"}),[]}},B=async e=>{try{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw new u;if(!s)throw Error("Backend URL is not configured. Set NEXT_PUBLIC_BACKEND_URL in your environment.");let o=await fetch("".concat(s,"/agent/initiate-fast"),{method:"POST",headers:{Authorization:"Bearer ".concat(r.access_token)},body:e,cache:"no-store"});if(!o.ok){let e=await o.text().catch(()=>"No error details available");if(console.error("[API] Error initiating agent: ".concat(o.status," ").concat(o.statusText),e),402===o.status)throw Error("Payment Required");if(401===o.status)throw Error("Authentication error: Please sign in again");if(403===o.status){try{let t=JSON.parse(e);if(t.message&&t.message.includes("disk quota exceeded"))throw Error("Storage quota exceeded. The system is automatically cleaning up old files and will retry shortly. Please try again in a moment.")}catch(t){if(e.includes("disk quota exceeded")||e.includes("Total disk quota exceeded"))throw Error("Storage quota exceeded. The system is automatically cleaning up old files and will retry shortly. Please try again in a moment.")}throw Error("Access forbidden: Please check your permissions")}else if(o.status>=500)throw Error("Server error: Please try again later");throw Error("Error initiating agent: ".concat(o.statusText," (").concat(o.status,")"))}return await o.json()}catch(e){if(n.v.api.error("Failed to initiate agent:",e),e instanceof TypeError&&e.message.includes("Failed to fetch")){let e=Error("Cannot connect to backend server. Please check your internet connection and make sure the backend is running.");throw(0,o.hS)(e,{operation:"initiate agent",resource:"AI assistant"}),e}throw(0,o.hS)(e,{operation:"initiate agent"}),e}},F=async()=>{try{let e=await fetch("".concat(s,"/health"),{cache:"no-store"});if(!e.ok)throw Error("API health check failed: ".concat(e.statusText));return e.json()}catch(e){throw e}},O=async e=>{try{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw new u;let o={...e,tolt_referral:window.tolt_referral},n=await fetch("".concat(s,"/billing/create-checkout-session"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(r.access_token)},body:JSON.stringify(o)});if(!n.ok){let e=await n.text().catch(()=>"No error details available");throw console.error("Error creating checkout session: ".concat(n.status," ").concat(n.statusText),e),Error("Error creating checkout session: ".concat(n.statusText," (").concat(n.status,")"))}let c=await n.json();switch(c.status){case"upgraded":case"updated":case"downgrade_scheduled":case"scheduled":case"no_change":return c;case"new":case"checkout_created":if(!c.url)throw Error("No checkout URL provided");return c;default:return console.warn("Unexpected status from createCheckoutSession:",c.status),c}}catch(e){throw console.error("Failed to create checkout session:",e),(0,o.hS)(e,{operation:"create checkout session",resource:"billing"}),e}},q=async e=>{try{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw new u;let o=await fetch("".concat(s,"/billing/create-portal-session"),{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(r.access_token)},body:JSON.stringify(e)});if(!o.ok){let e=await o.text().catch(()=>"No error details available");throw console.error("Error creating portal session: ".concat(o.status," ").concat(o.statusText),e),Error("Error creating portal session: ".concat(o.statusText," (").concat(o.status,")"))}return o.json()}catch(e){throw console.error("Failed to create portal session:",e),(0,o.hS)(e,{operation:"create portal session",resource:"billing portal"}),e}},z=async()=>{try{let e=(0,a.U)(),{data:{session:t}}=await e.auth.getSession();if(!(null==t?void 0:t.access_token))throw new u;let r=await fetch("".concat(s,"/billing/subscription"),{headers:{Authorization:"Bearer ".concat(t.access_token)}});if(!r.ok){let e=await r.text().catch(()=>"No error details available");throw console.error("Error getting subscription: ".concat(r.status," ").concat(r.statusText),e),Error("Error getting subscription: ".concat(r.statusText," (").concat(r.status,")"))}return r.json()}catch(e){if(e instanceof u)throw e;throw console.error("Failed to get subscription:",e),(0,o.hS)(e,{operation:"load subscription",resource:"billing information"}),e}},M=async()=>{try{let e=(0,a.U)(),{data:{session:t}}=await e.auth.getSession();if(!(null==t?void 0:t.access_token))throw new u;let r=await fetch("".concat(s,"/billing/available-models"),{headers:{Authorization:"Bearer ".concat(t.access_token)}});if(!r.ok){let e=await r.text().catch(()=>"No error details available");throw console.error("Error getting available models: ".concat(r.status," ").concat(r.statusText),e),Error("Error getting available models: ".concat(r.statusText," (").concat(r.status,")"))}return r.json()}catch(e){if(e instanceof u)throw e;throw console.error("Failed to get available models:",e),(0,o.hS)(e,{operation:"load available models",resource:"AI models"}),e}},L=async()=>{try{let e=(0,a.U)(),{data:{session:t}}=await e.auth.getSession();if(!(null==t?void 0:t.access_token))throw new u;let r=await fetch("".concat(s,"/billing/check-status"),{headers:{Authorization:"Bearer ".concat(t.access_token)}});if(!r.ok){let e=await r.text().catch(()=>"No error details available");throw console.error("Error checking billing status: ".concat(r.status," ").concat(r.statusText),e),Error("Error checking billing status: ".concat(r.statusText," (").concat(r.status,")"))}return r.json()}catch(e){if(e instanceof u)throw e;throw console.error("Failed to check billing status:",e),e}},J=async e=>{try{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw new u;let o=new FormData;o.append("audio_file",e);let n=await fetch("".concat(s,"/transcription"),{method:"POST",headers:{Authorization:"Bearer ".concat(r.access_token)},body:o});if(!n.ok){let e=await n.text().catch(()=>"No error details available");throw console.error("Error transcribing audio: ".concat(n.status," ").concat(n.statusText),e),Error("Error transcribing audio: ".concat(n.statusText," (").concat(n.status,")"))}return n.json()}catch(e){if(e instanceof u)throw e;throw console.error("Failed to transcribe audio:",e),(0,o.hS)(e,{operation:"transcribe audio",resource:"speech-to-text"}),e}}
1,G=async()=>{let e=(0,a.U)(),{data:{session:t}}=await e.auth.getSession();if(!(null==t?void 0:t.access_token))throw new u;try{let e=await fetch("".concat(s,"/agent/models"),{method:"GET",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(t.access_token)}});if(!e.ok){let t=await e.text().catch(()=>"No error details available");throw console.error("Error fetching database models: ".concat(e.status," ").concat(e.statusText),t),Error("Error fetching database models: ".concat(e.statusText," (").concat(e.status,")"))}let r=await e.json();return n.v.api.debug("Database models fetched:",r),r}catch(e){if(e instanceof u)throw e;throw console.error("Failed to fetch database models:",e),(0,o.hS)(e,{operation:"fetch database models",resource:"models"}),e}},V=async()=>{let e=(0,a.U)(),{data:{session:t}}=await e.auth.getSession();if(!(null==t?void 0:t.access_token))throw new u;try{let e=await fetch("".concat(s,"/agent/models/preference"),{method:"GET",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(t.access_token)}});if(!e.ok){let t=await e.text().catch(()=>"No error details available");throw console.error("Error fetching user model preference: ".concat(e.status," ").concat(e.statusText),t),Error("Error fetching user model preference: ".concat(e.statusText," (").concat(e.status,")"))}let r=await e.json();return n.v.api.debug("User model preference fetched:",r),r}catch(e){if(e instanceof u)throw e;throw console.error("Failed to fetch user model preference:",e),(0,o.hS)(e,{operation:"fetch user model preference",resource:"model preference"}),e}},D=async e=>{let t=(0,a.U)(),{data:{session:r}}=await t.auth.getSession();if(!(null==r?void 0:r.access_token))throw new u;try{let t=await fetch("".concat(s,"/agent/models/preference"),{method:"PUT",headers:{"Content-Type":"application/json",Authorization:"Bearer ".concat(r.access_token)},body:JSON.stringify(e)});if(!t.ok){let e=await t.text().catch(()=>"No error details available");throw console.error("Error updating user model preference: ".concat(t.status," ").concat(t.statusText),e),Error("Error updating user model preference: ".concat(t.statusText," (").concat(t.status,")"))}let a=await t.json();return n.v.api.debug("User model preference updated:",a),a}catch(e){if(e instanceof u)throw e;throw console.error("Failed to update user model preference:",e),(0,o.hS)(e,{operation:"update user model preference",resource:"model preference"}),e}}},96648:(e,t,r)=>{r.d(t,{U:()=>o});var a=r(45831);function o(){return(0,a.createBrowserClient)("https://jfjzkvfpxryphcdpoiht.supabase.co","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImpmanprdmZweHJ5cGhjZHBvaWh0Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQwNjY3NTQsImV4cCI6MjA2OTY0Mjc1NH0.LpOQZysImNdSOZcivicYRn-80qHe54czs5xpHzH4sHI")}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.