import{n as x,b as h,e as T}from"./vendor-lit-B2r-dKc0.js";import{i as y}from"./vendor-mini-lit-CpUQub-j.js";import{S as w}from"./SandboxedIframe-B6fsNF9g.js";import{aC as C,aD as $,aE as S,aH as E,aT as k}from"./app-session-runtime-CeFJDILJ.js";import{d as g}from"./vendor-lucide-B9Yl1Gez.js";import{T as b}from"./vendor-typebox-DJ_ZScJ4.js";import"./vendor-marked-_iteVaMm.js";import"./app-review-C3CmcSYC.js";import"./app-panel-workspace-C3XDRCgE.js";import"./app-routing-C_r2pdnC.js";import"./app-message-reducer-wATqnZGf.js";import"./vendor-sortable-B1o3v0Qs.js";import"./app-inbox-CaZD85aD.js";import"./app-pr-walkthrough-WipZACSB.js";async function R(o,i,u,c){if(!o)throw new Error("Code parameter is required");if(u?.aborted)throw new Error("Execution aborted");const n=new w;c&&(n.sandboxUrlProvider=c),n.style.display="none",document.body.appendChild(n);try{const s=`repl-${Date.now()}-${Math.random().toString(36).substring(7)}`,t=await n.execute(s,o,i,[],u);n.remove();let r="";if(t.console&&t.console.length>0)for(const l of t.console)r+=`${l.text}
`;if(!t.success)throw r&&(r+=`
`),r+=`Error: ${t.error?.message||"Unknown error"}
${t.error?.stack||""}`,new Error(r.trim());if(t.returnValue!==void 0&&(r&&(r+=`
`),r+=`=> ${typeof t.returnValue=="object"?JSON.stringify(t.returnValue,null,2):t.returnValue}`),t.files&&t.files.length>0){r+=`
[Files returned: ${t.files.length}]
`;for(const l of t.files)r+=`  - ${l.fileName} (${l.mimeType})
`}else o.includes("returnFile")&&(r+=`
[No files returned - check async operations]`);return{output:r.trim()||"Code executed successfully (no output)",files:t.files}}catch(s){throw n.remove(),new Error(s.message||"Execution failed")}}const j=b.Object({title:b.String({description:"Brief title describing what the code snippet tries to achieve in active form, e.g. 'Calculating sum'"}),code:b.String({description:"JavaScript code to execute"})});function P(){return{label:"JavaScript REPL",name:"javascript_repl",runtimeProvidersFactory:()=>[],sandboxUrlProvider:void 0,get description(){const o=this.runtimeProvidersFactory?.().map(i=>i.getDescription()).filter(i=>i.trim().length>0)||[];return k(o)},parameters:j,execute:async function(o,i,u){const c=await R(i.code,this.runtimeProvidersFactory?.()??[],u,this.sandboxUrlProvider),n=(c.files||[]).map(s=>{const t=e=>{if(e instanceof Uint8Array){let d="";for(let a=0;a<e.length;a+=32768)d+=String.fromCharCode(...e.subarray(a,a+32768));return{base64:btoa(d),size:e.length}}else if(typeof e=="string"){const m=new TextEncoder().encode(e);let a="";const f=32768;for(let p=0;p<m.length;p+=f)a+=String.fromCharCode(...m.subarray(p,p+f));return{base64:btoa(a),size:m.length}}else{const d=String(e),a=new TextEncoder().encode(d);let f="";const p=32768;for(let v=0;v<a.length;v+=p)f+=String.fromCharCode(...a.subarray(v,v+p));return{base64:btoa(f),size:a.length}}},{base64:r,size:l}=t(s.content);return{fileName:s.fileName||"file",mimeType:s.mimeType||"application/octet-stream",size:l,contentBase64:r}});return{content:[{type:"text",text:c.output}],details:{files:n}}}}}const B=P(),J={render(o,i,u){const c=C(i,u),n=T(),s=T();if(i&&o){const t=i.content?.filter(e=>e.type==="text").map(e=>e.text).join(`
`)||"",l=(i.details?.files||[]).map((e,d)=>{let m;if((e.mimeType?.startsWith("text/")||e.mimeType==="application/json"||e.mimeType==="application/javascript"||e.mimeType?.includes("xml"))&&e.contentBase64)try{m=atob(e.contentBase64)}catch{console.warn("Failed to decode base64 content for",e.fileName)}return{id:`repl-${Date.now()}-${d}`,type:e.mimeType?.startsWith("image/")?"image":"document",fileName:e.fileName||`file-${d}`,mimeType:e.mimeType||"application/octet-stream",size:e.size??0,content:e.contentBase64,preview:e.mimeType?.startsWith("image/")?e.contentBase64:void 0,extractedText:m}});return{content:h`
					<div>
						${$(c,g,o.title?o.title:y("Executing JavaScript"),n,s,!1)}
						<div ${x(n)} class="max-h-0 overflow-hidden transition-all duration-300 space-y-3">
							<code-block .code=${o.code||""} language="javascript"></code-block>
							${t?h`<console-block .content=${t} .variant=${i.isError?"error":"default"}></console-block>`:""}
						</div>
						${l.length?h`<div class="flex flex-wrap gap-2 mt-3">
									${l.map(e=>h`<attachment-tile .attachment=${e}></attachment-tile>`)}
							  </div>`:""}
					</div>
				`,isCustom:!1}}return o?{content:h`
					<div>
						${$(c,g,o.title?o.title:y("Executing JavaScript"),n,s,!1)}
						<div ${x(n)} class="max-h-0 overflow-hidden transition-all duration-300">
							${o.code?h`<code-block .code=${o.code} language="javascript"></code-block>`:""}
						</div>
					</div>
				`,isCustom:!1}:{content:S(c,g,y("Preparing JavaScript...")),isCustom:!1}}};E(B.name,J);export{P as createJavaScriptReplTool,R as executeJavaScript,J as javascriptReplRenderer,B as javascriptReplTool};
