import{c as o,b as f}from"./prompts-BBxcmVYy.js";class l{constructor(i,r,t=!0){this.artifactsPanel=i,this.agent=r,this.readWrite=t}getData(){const i={};return this.artifactsPanel.artifacts.forEach((r,t)=>{i[t]=r.content}),{artifacts:i}}getRuntime(){return i=>{const r=t=>t.endsWith(".json");window.listArtifacts=async()=>{if(window.sendRuntimeMessage){const t=await window.sendRuntimeMessage({type:"artifact-operation",action:"list"});if(!t.success)throw new Error(t.error);return t.result}else return Object.keys(window.artifacts||{})},window.getArtifact=async t=>{let e;if(window.sendRuntimeMessage){const a=await window.sendRuntimeMessage({type:"artifact-operation",action:"get",filename:t});if(!a.success)throw new Error(a.error);e=a.result}else{if(!window.artifacts?.[t])throw new Error(`Artifact not found (offline mode): ${t}`);e=window.artifacts[t]}if(r(t))try{return JSON.parse(e)}catch(a){throw new Error(`Failed to parse JSON from ${t}: ${a}`)}return e},window.createOrUpdateArtifact=async(t,e,a)=>{if(!window.sendRuntimeMessage)throw new Error("Cannot create/update artifacts in offline mode (read-only)");let s=e;(r(t)&&typeof e!="string"||typeof e!="string")&&(s=JSON.stringify(e,null,2));const n=await window.sendRuntimeMessage({type:"artifact-operation",action:"createOrUpdate",filename:t,content:s,mimeType:a});if(!n.success)throw new Error(n.error)},window.deleteArtifact=async t=>{if(!window.sendRuntimeMessage)throw new Error("Cannot delete artifacts in offline mode (read-only)");const e=await window.sendRuntimeMessage({type:"artifact-operation",action:"delete",filename:t});if(!e.success)throw new Error(e.error)}}}async handleMessage(i,r){if(i.type!=="artifact-operation")return;const{action:t,filename:e,content:a}=i;try{switch(t){case"list":{const s=Array.from(this.artifactsPanel.artifacts.keys());r({success:!0,result:s});break}case"get":{const s=this.artifactsPanel.artifacts.get(e);r(s?{success:!0,result:s.content}:{success:!1,error:`Artifact not found: ${e}`});break}case"createOrUpdate":{try{const s=this.artifactsPanel.artifacts.has(e),n=s?"rewrite":"create",c=s?"update":"create";await this.artifactsPanel.tool.execute("",{command:n,filename:e,content:a}),this.agent?.state.messages.push({role:"artifact",action:c,filename:e,content:a,...c==="create"&&{title:e},timestamp:new Date().toISOString()}),r({success:!0})}catch(s){r({success:!1,error:s.message})}break}case"delete":{try{await this.artifactsPanel.tool.execute("",{command:"delete",filename:e}),this.agent?.state.messages.push({role:"artifact",action:"delete",filename:e,timestamp:new Date().toISOString()}),r({success:!0})}catch(s){r({success:!1,error:s.message})}break}default:r({success:!1,error:`Unknown artifact action: ${t}`})}}catch(s){r({success:!1,error:s.message})}}getDescription(){return this.readWrite?o:f}}export{l as ArtifactsRuntimeProvider};
