// @bun
var Z=Object.defineProperty;var _=(j)=>j;function $(j,q){this[j]=_.bind(null,q)}var V=(j,q)=>{for(var B in q)Z(j,B,{get:q[B],enumerable:!0,configurable:!0,set:$.bind(q,B)})};var C=(j,q)=>()=>(j&&(q=j(j=0)),q);var R=import.meta.require;function U(j){let q=j.replace(/\\/g,"/").replace(/\/+/g,"/");if(!q.startsWith("/memories")||q.includes("..")||q==="/memories/.."||q.startsWith("/memories/../"))throw Error(`Invalid memory path: ${j}. Path must be under /memories and cannot contain traversal sequences.`)}function A(j){if(j<1024)return`${j}B`;if(j<1048576)return`${(j/1024).toFixed(1)}K`;return`${(j/1048576).toFixed(1)}M`}class I{store=new Map;async view(j,q){U(j);let B=j.replace(/\/+/g,"/").replace(/\/$/,"")||"/memories";if(this.isDirectory(B))return this.listDirectory(B);let D=this.store.get(B);if(D===void 0)return`The path ${j} does not exist. Please provide a valid path.`;let F=D.split(`
`);if(F.length>999999)return`File ${j} exceeds maximum line limit of 999,999 lines.`;let[E,H]=q??[1,F.length],G=Math.max(0,E-1),J=Math.min(F.length,H),Q=F.slice(G,J).map((W,X)=>{let Y=G+X+1;return`${String(Y).padStart(6)}	${W}`}).join(`
`);return`Here's the content of ${j} with line numbers:
${Q}`}async create(j,q){U(j);let B=j.replace(/\/+/g,"/");if(this.store.has(B))return`Error: File ${j} already exists`;return this.ensureParentDir(B),this.store.set(B,q),`File created successfully at: ${j}`}async strReplace(j,q,B){U(j);let D=j.replace(/\/+/g,"/"),F=this.store.get(D);if(F===void 0)return`Error: The path ${j} does not exist. Please provide a valid path.`;let E=(F.match(new RegExp(K(q),"g"))??[]).length;if(E>1){let O=F.split(`
`),Q=[];return O.forEach((W,X)=>{if(W.includes(q))Q.push(X+1)}),`No replacement was performed. Multiple occurrences of old_str \`${q}\` in lines: ${Q.join(", ")}. Please ensure it is unique`}if(E===0)return`No replacement was performed, old_str \`${q}\` did not appear verbatim in ${j}.`;let H=F.replace(q,B);return this.store.set(D,H),`The memory file has been edited.
${H.split(`
`).slice(0,5).map((O,Q)=>`${String(Q+1).padStart(6)}	${O}`).join(`
`)}`}async insert(j,q,B){U(j);let D=j.replace(/\/+/g,"/"),F=this.store.get(D);if(F===void 0)return`Error: The path ${j} does not exist`;let E=F.split(`
`),H=E.length;if(q<0||q>H)return`Error: Invalid \`insert_line\` parameter: ${q}. It should be within the range of lines of the file: [0, ${H}]`;return E.splice(q,0,B.replace(/\n$/,"")),this.store.set(D,E.join(`
`)),`The file ${j} has been edited.`}async delete(j){U(j);let q=j.replace(/\/+/g,"/");if(this.isDirectory(q)){let B=q==="/memories"?"/memories/":`${q}/`;for(let D of this.store.keys())if(D.startsWith(B)||D===q)this.store.delete(D)}else{if(!this.store.has(q))return`Error: The path ${j} does not exist`;this.store.delete(q)}return`Successfully deleted ${j}`}async rename(j,q){U(j),U(q);let B=j.replace(/\/+/g,"/"),D=q.replace(/\/+/g,"/");if(this.store.has(D)||this.hasAnyChild(D))return`Error: The destination ${q} already exists`;if(this.isDirectory(B)){let F=`${B}/`,E=Array.from(this.store.entries()).filter(([G])=>G.startsWith(F)||G===B),H=`${D}/`;for(let[G,J]of E){this.store.delete(G);let O=G===B?D:H+G.slice(F.length);this.store.set(O,J)}}else{let F=this.store.get(B);if(F===void 0)return`Error: The path ${j} does not exist`;this.store.delete(B),this.ensureParentDir(D),this.store.set(D,F)}return`Successfully renamed ${j} to ${q}`}isDirectory(j){if(j==="/memories")return!0;for(let q of this.store.keys())if(q.startsWith(j+"/"))return!0;return!1}hasAnyChild(j){let q=j.endsWith("/")?j:`${j}/`;for(let B of this.store.keys())if(B.startsWith(q))return!0;return!1}ensureParentDir(j){let q=j.split("/").filter(Boolean);q.pop();for(let B=1;B<=q.length;B++){let D="/"+q.slice(0,B).join("/");if(!this.store.has(D))this.store.set(D,"")}}listDirectory(j){let q=j==="/memories"?"/memories/":`${j}/`,B=new Set,D=[];if(j==="/memories")D.push({path:"/memories",size:Array.from(this.store.keys()).filter((E)=>E.startsWith("/memories/")).reduce((E,H)=>E+(this.store.get(H)?.length??0),0)});for(let E of this.store.keys()){if(!E.startsWith(q)&&E!==j)continue;let G=E.slice(q.length).split("/")[0];if(!G||G.startsWith(".")||G==="node_modules")continue;let J=j==="/memories"?`/memories/${G}`:`${j}/${G}`;if(B.has(J))continue;B.add(J);let O=0;if(this.store.has(E))O=(this.store.get(E)??"").length;else for(let Q of this.store.keys())if(Q.startsWith(J+"/")||Q===J)O+=(this.store.get(Q)??"").length;D.push({path:J,size:O})}let F=D.slice(0,50).map((E)=>`${A(E.size)}	${E.path}`).join(`
`);return`Here're the files and directories up to 2 levels deep in ${j}, excluding hidden items and node_modules:
${F}`}}function K(j){return j.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}export{I as InMemoryAgentMemoryStore};
