import{Sma as w,Tma as C,Uma as bQ,Vma as A,ana as r,bna as o,cna as kQ,dna as JQ,ena as W,fna as a,gna as _,hna as BQ,kna as WQ}from"./cli-0.5.12-f8850v2j.mjs";import{aoa as b}from"./cli-0.5.12-xzeb7mtb.mjs";var l=b(()=>{JQ()});function G(z){if(z.inputSchema.type!=="object")throw Error(`JSON schema for tool "${z.name}" must be an object, but got ${z.inputSchema.type}`);return{type:"custom",name:z.name,input_schema:z.inputSchema,description:z.description,run:z.run,parse:(Q)=>Q,...z.close?{close:z.close}:{}}}var n=b(()=>{WQ();l()});import*as R from"node:fs/promises";import*as D from"node:path";import{randomUUID as CQ}from"node:crypto";async function DQ(z){try{return await R.realpath(z)}catch{return z}}async function UQ(z){let Q=[],$=z;for(;;){let V;try{V=await R.realpath($)}catch{let Y=!1;try{Y=(await R.lstat($)).isSymbolicLink()}catch{}if(Y){$=D.resolve(D.dirname($),await R.readlink($));continue}let K=D.dirname($);if(K===$)return z;Q.push(D.basename($)),$=K;continue}return Q.length?D.join(V,...Q.reverse()):V}}async function i(z,Q,$){let V=$?.allowOutside??!1;if(D.isAbsolute(Q)){if(!V)throw new W(`absolute path ${JSON.stringify(Q)} not permitted`);return D.resolve(Q)}let Y=await DQ(D.resolve(z)),K=D.resolve(Y,Q);if(V)return K;let q=await UQ(K),X=Y.endsWith(D.sep)?Y:Y+D.sep;if(q!==Y&&!q.startsWith(X))throw new W(`path ${JSON.stringify(Q)} escapes workdir`);return q}async function m(z,Q){let $=D.dirname(z),V=D.join($,`.tmp-${process.pid}-${CQ()}`),Y;try{Y=await R.open(V,"wx",NQ),await Y.writeFile(Q,"utf-8"),await Y.sync(),await Y.close(),Y=void 0,await R.rename(V,z)}catch(K){if(Y)await Y.close().catch(()=>{});throw await R.unlink(V).catch(()=>{}),K}}function j(z,Q){switch(z?.code){case"ENOENT":return`${Q}: no such file or directory`;case"EACCES":case"EPERM":return`${Q}: permission denied`;case"ENOTDIR":return`${Q}: not a directory`;case"EISDIR":return`${Q}: is a directory`;case"ELOOP":return`${Q}: too many levels of symbolic links`;case"ENAMETOOLONG":return`${Q}: file name too long`;case"ENOSPC":return`${Q}: no space left on device`;case"EMFILE":case"ENFILE":return`${Q}: too many open files`;default:return`${Q}: ${z instanceof Error?z.message:String(z)}`}}var B=493,NQ=420;var d=b(()=>{a()});import*as O from"node:fs/promises";import*as s from"node:fs";import*as U from"node:path";import{execFile as LQ}from"node:child_process";import{promisify as MQ}from"node:util";import{Readable as OQ}from"node:stream";import{pipeline as wQ}from"node:stream/promises";async function AQ(z){let{client:Q,sessionId:$}=z;if(!Q||!$)return async()=>{};let V=o(Q),Y=await Q.beta.sessions.retrieve($),K=U.resolve(z.workdir,"skills"),q=[];for(let X of Y.agent.skills)try{let H=await t(Q,X.skill_id,X.version),Z=await Q.beta.skills.versions.retrieve(H,{skill_id:X.skill_id}),J=U.basename(Z.name.trim());if(J===""||J==="."||J==="..")J=X.skill_id;let L=U.resolve(K,J);if(L!==K&&!L.startsWith(K+U.sep)){V.warn("skill name escapes the skills dir; skipping",{component:"agent-tool-context",name:Z.name});continue}let v=await Q.beta.skills.versions.download(H,{skill_id:X.skill_id});await O.rm(L,{recursive:!0,force:!0}),await O.mkdir(L,{recursive:!0,mode:B}),q.push(L),await e(v,L),V.info("downloaded skill",{component:"agent-tool-context",skill_id:X.skill_id,version:H,dest:L})}catch(H){V.warn("failed to download skill",{component:"agent-tool-context",skill_id:X.skill_id,error:String(H)})}return async()=>{for(let X of q)await O.rm(X,{recursive:!0,force:!0}).catch((H)=>{V.warn("failed to clean up skill",{component:"agent-tool-context",dest:X,error:String(H)})})}}async function t(z,Q,$){if(/^\d+$/.test($))return $;let V;for await(let Y of z.beta.skills.versions.list(Q))if(/^\d+$/.test(Y.version)&&(V===void 0||BigInt(Y.version)>BigInt(V)))V=Y.version;if(V===void 0)throw new A(`skill ${JSON.stringify(Q)} has no concrete version to resolve ${JSON.stringify($)} against`);return V}function RQ(z){for(let Q of z.split(`
`)){let $=Q.trim();if(!$)continue;if(U.isAbsolute($)||$.split(/[\\/]/).includes(".."))throw new A(`refusing to extract unsafe archive member: ${$}`)}}function IQ(z){for(let Q of z.split(`
`)){let $=Q.trimStart()[0];if($==="l"||$==="h"||$==="b"||$==="c"||$==="p"||$==="s")throw new A("refusing to extract archive with symlink/hardlink/device member")}}async function h(z,Q){try{let{stdout:$}=await yQ(z,Q);return $}catch($){if($!=null&&typeof $==="object"&&$.code==="ENOENT")throw new A(`skill extraction requires the \`${z}\` command, but it was not found on PATH`);throw $}}function GQ(z){let Q,$=!1;for(let V of z.split(`
`)){let Y=V.trim().split("/").filter((q)=>q!==""&&q!==".");if(Y.length===0)continue;let K=Y[0];if(Q===void 0)Q=K;else if(K!==Q)return"";if(Y.length>1)$=!0}return Q!==void 0&&$?Q:""}async function e(z,Q){let $=U.join(Q,`.skill-archive-${process.pid}-${Date.now()}`);if(!z.body)throw new A("skill download response had no body");await wQ(OQ.fromWeb(z.body),s.createWriteStream($));let V=U.join(U.dirname(Q),`.skill-stage-${process.pid}-${Date.now()}`);try{let Y=await FQ($,4),K=Y.length>=4&&Y[0]===80&&Y[1]===75&&Y[2]===3&&Y[3]===4,q=K?"unzip":"tar",X=await h(q,K?["-Z1",$]:["-tf",$]);RQ(X),IQ(await h(q,K?["-Z",$]:["-tvf",$]));let H=GQ(X);await O.mkdir(V,{recursive:!0,mode:B}),await h(q,K?["-oq",$,"-d",V]:["-xf",$,"-C",V]);let Z=H?U.join(V,H):V;for(let J of await O.readdir(Z))await O.rename(U.join(Z,J),U.join(Q,J))}finally{await O.rm($,{force:!0}),await O.rm(V,{recursive:!0,force:!0})}}async function FQ(z,Q){let $=await O.open(z,"r");try{let V=Buffer.alloc(Q),{bytesRead:Y}=await $.read(V,0,Q,0);return V.subarray(0,Y)}finally{await $.close()}}var yQ;var QQ=b(()=>{r();kQ();d();yQ=MQ(LQ)});import*as k from"node:fs/promises";import*as g from"node:fs";import*as N from"node:path";import*as c from"node:child_process";import*as VQ from"node:crypto";import*as YQ from"node:readline";function KQ(z){return z===void 0?jQ:z}function O0(z){return[gQ(z),vQ(z),fQ(z),SQ(z),mQ(z),dQ(z)]}function E(z,Q){return i(z.workdir,Q,{allowOutside:z.unrestrictedPaths??!1})}function TQ(){let z={};for(let[Q,$]of Object.entries(process.env)){if(Q.startsWith("ANTHROPIC_"))continue;z[Q]=$}return z}class XQ{constructor(z,Q=TQ()){T.add(this),M.set(this,void 0),y.set(this,""),x.set(this,!1),F.set(this,!1),I.set(this,null),w(this,M,c.spawn("/bin/bash",["--noprofile","--norc"],{cwd:z,env:{...Q,PS1:"",PS2:"",TERM:"dumb"},stdio:["pipe","pipe","pipe"],detached:!0}),"f"),C(this,M,"f").stdout.setEncoding("utf8"),C(this,M,"f").stderr.setEncoding("utf8"),C(this,M,"f").stdout.on("data",($)=>C(this,T,"m",p).call(this,$)),C(this,M,"f").stderr.on("data",($)=>C(this,T,"m",p).call(this,$)),C(this,M,"f").once("close",()=>{w(this,F,!0,"f");let $=C(this,I,"f");w(this,I,null,"f"),$?.resolve()})}get closed(){return C(this,F,"f")}async exec(z,Q={}){if(C(this,F,"f"))throw new A("bash session terminated");let $=Q.timeoutMs??qQ,V=Q.signal;if(V?.aborted)throw new A("bash command aborted");w(this,y,"","f"),w(this,x,!1,"f");let Y=`__ANT_CMD_${VQ.randomUUID()}_DONE__`,K=`${Y.slice(0,8)}''${Y.slice(8)}`,q=`{ ${z}
} </dev/null 2>&1; printf '\\n${K}%d\\n' $?
`;if(C(this,M,"f").stdin.write(q),C(this,y,"f").indexOf(Y)<0){let{promise:v,resolve:ZQ}=_();w(this,I,{sentinel:Y,resolve:ZQ},"f");let f,u;try{await Promise.race([v,new Promise((HQ,S)=>{f=setTimeout(()=>S(new A(`bash command timed out after ${$}ms`)),$)}),new Promise((HQ,S)=>{if(!V)return;u=()=>S(new A("bash command aborted")),V.addEventListener("abort",u,{once:!0})})])}finally{if(f)clearTimeout(f);if(u&&V)V.removeEventListener("abort",u);w(this,I,null,"f")}}let X=C(this,y,"f").indexOf(Y);if(X<0)throw new A("bash session terminated");let Z=C(this,y,"f").slice(X+Y.length).match(/^(-?\d+)/),J=Z?parseInt(Z[1],10):-1,L=C(this,y,"f").slice(0,X).replace(EQ,"").replace(/\n+$/,"");if(C(this,x,"f"))L=`[output truncated]
${L}`;return{output:L,exitCode:J}}close(){if(C(this,F,"f"))return;w(this,F,!0,"f");let z=C(this,I,"f");w(this,I,null,"f"),z?.resolve(),C(this,M,"f").stdout.destroy(),C(this,M,"f").stderr.destroy(),C(this,M,"f").stdin.destroy();try{process.kill(-C(this,M,"f").pid,"SIGKILL")}catch{C(this,M,"f").kill("SIGKILL")}C(this,M,"f").unref()}}function gQ(z){let Q,$=Promise.resolve();return G({name:"bash",description:"Run a bash command in a persistent shell. State (cwd, env vars) persists across calls.",inputSchema:{type:"object",properties:{command:{type:"string",description:"The command to run"},restart:{type:"boolean",description:"Restart the persistent shell before running"},timeout_ms:{type:"integer",description:"Per-call timeout in milliseconds"}}},run:async({command:V,restart:Y,timeout_ms:K},q)=>{let X=$,H=_();$=H.promise;try{await X}catch{}try{if(Y)Q?.close(),Q=void 0;if(!V){if(Y)return"bash session restarted";throw new W("bash: command is required")}Q??(Q=new XQ(z.workdir,z.env));try{let{output:Z,exitCode:J}=await Q.exec(V,{timeoutMs:K??qQ,signal:q?.signal});if(J!==0)throw new W(Z||`exit ${J}`);return Z}catch(Z){if(Z instanceof W)throw Z;throw Q.close(),Q=void 0,new W(`bash: ${Z instanceof Error?Z.message:String(Z)}`)}}finally{H.resolve()}},close:()=>{Q?.close(),Q=void 0}})}function vQ(z){return G({name:"read",description:"Read a UTF-8 text file relative to the workdir.",inputSchema:{type:"object",properties:{file_path:{type:"string"},view_range:{type:"array",items:{type:"integer"},description:"[start_line, end_line] 1-indexed inclusive"}},required:["file_path"]},run:async({file_path:Q,view_range:$})=>{if(!Q)throw new W("read: file_path is required");let V=await E(z,Q),Y;try{let J=await k.stat(V);if(!J.isFile())throw new W(`read: ${Q} is not a regular file`);let L=KQ(z.maxFileBytes);if(L!==null&&J.size>L)throw new W(`read: ${Q} is ${J.size} bytes, exceeds ${L}-byte limit. Use bash (head/tail/sed) to read a slice.`);Y=await k.readFile(V,"utf8")}catch(J){if(J instanceof W)throw J;throw new W(`read: ${j(J,Q)}`)}if(!$)return Y;if($.length!==2)throw new W("read: view_range must be [start_line, end_line]");let[K,q]=$,X=Y.split(`
`),H=Math.max(0,K-1),Z=q>0?q:X.length;return X.slice(H,Z).join(`
`)}})}function fQ(z){return G({name:"write",description:"Write a UTF-8 text file relative to the workdir, creating parent directories as needed.",inputSchema:{type:"object",properties:{file_path:{type:"string"},content:{type:"string"}},required:["file_path","content"]},run:async({file_path:Q,content:$})=>{if(!Q)throw new W("write: file_path is required");let V=await E(z,Q);try{await k.mkdir(N.dirname(V),{recursive:!0,mode:B}),await m(V,$??"")}catch(Y){throw new W(`write: ${j(Y,Q)}`)}return`wrote ${Buffer.byteLength($??"")} bytes to ${Q}`}})}function SQ(z){return G({name:"edit",description:"Replace old_string with new_string in a file. old_string must be unique unless replace_all.",inputSchema:{type:"object",properties:{file_path:{type:"string"},old_string:{type:"string"},new_string:{type:"string"},replace_all:{type:"boolean"}},required:["file_path","old_string","new_string"]},run:async({file_path:Q,old_string:$,new_string:V,replace_all:Y})=>{if(!Q)throw new W("edit: file_path is required");if(!$)throw new W("edit: old_string is required");let K=await E(z,Q),q;try{let Z=await k.stat(K);if(!Z.isFile())throw new W(`edit: ${Q} is not a regular file`);let J=KQ(z.maxFileBytes);if(J!==null&&Z.size>J)throw new W(`edit: ${Q} is ${Z.size} bytes, exceeds ${J}-byte limit. Use bash (sed/awk) to edit a large file.`);q=await k.readFile(K,"utf8")}catch(Z){if(Z instanceof W)throw Z;throw new W(`edit: ${j(Z,Q)}`)}let X=q.split($).length-1;if(X===0)throw new W(`edit: old_string not found in ${Q}`);let H;if(Y)H=q.split($).join(V);else{if(X>1)throw new W(`edit: old_string appears ${X} times in ${Q} (must be unique)`);H=q.replace($,()=>V)}try{await m(K,H)}catch(Z){throw new W(`edit: write: ${j(Z,Q)}`)}return`edited ${Q} (${Y?X:1} replacement(s))`}})}function mQ(z){return G({name:"glob",description:"Match files under the workdir against a glob pattern. Results are mtime-sorted, newest first.",inputSchema:{type:"object",properties:{pattern:{type:"string"},path:{type:"string",description:"Directory to search in. Defaults to the workdir."}},required:["pattern"]},run:async({pattern:Q,path:$})=>{if(!Q)throw new W("glob: pattern is required");let V=N.resolve(z.workdir),Y=Q;if(N.isAbsolute(Q)){if(!z.unrestrictedPaths)throw new W("glob: absolute pattern not permitted");V=N.parse(Q).root,Y=N.relative(V,Q)}else if($)V=await E(z,$);if(!z.unrestrictedPaths&&Y.split(/[\\/]/).includes(".."))throw new W('glob: ".." is not permitted in the pattern');let K=[];try{for await(let q of uQ(Y,{cwd:V,withFileTypes:!0,exclude:(X)=>X.name===".git"||X.name==="node_modules"})){if(!q.isFile())continue;let X=N.join(q.parentPath,q.name);if(!z.unrestrictedPaths&&!_Q(V,X))continue;let H=0;try{H=(await k.stat(X)).mtimeMs}catch{}K.push({path:X,mtime:H})}}catch(q){throw new W(`glob: ${q instanceof Error?q.message:String(q)}`)}if(K.length===0)return"no matches";return K.sort((q,X)=>X.mtime-q.mtime),K.slice(0,PQ).map((q)=>q.path).join(`
`)}})}function dQ(z){return G({name:"grep",description:"Search file contents for a regex. Uses ripgrep if available, otherwise a built-in walker.",inputSchema:{type:"object",properties:{pattern:{type:"string"},path:{type:"string"}},required:["pattern"]},run:async({pattern:Q,path:$},V)=>{if(!Q)throw new W("grep: pattern is required");let Y=N.resolve(z.workdir);if($)Y=await E(z,$);let K=await aQ();return K?hQ(K,Q,Y,V?.signal):pQ(Q,Y,V?.signal)}})}function hQ(z,Q,$,V){return new Promise((Y,K)=>{let q=c.spawn(z,["-n","--no-heading","-e",Q,"--",$],{...V?{signal:V}:{}}),X="",H="",Z=!1;q.stdout.on("data",(J)=>{if(Z)return;if(X+=J,X.length>P)Z=!0,X=X.slice(0,P),q.kill("SIGKILL")}),q.stderr.on("data",(J)=>H+=J),q.on("close",(J)=>{if(V?.aborted)return K(new W("grep: aborted"));if(Z)return Y(X+`
[output truncated at ${P} bytes]`);if(J===0)return Y(X);if(J===1)return Y("no matches");K(new W(`grep: rg failed: ${H||`exit ${J}`}`))}),q.on("error",(J)=>{if(V?.aborted)return K(new W("grep: aborted"));K(new W(`grep: rg failed: ${J.message}`))})})}async function pQ(z,Q,$){let V;try{V=new RegExp(z)}catch(H){throw new W(`grep: invalid regex: ${H instanceof Error?H.message:String(H)}`)}let Y=[],K=P,q=(H)=>{if(K-=H.length+1,K<0)return Y.push(`[output truncated at ${P} bytes]`),!1;return Y.push(H),!0};if((await k.stat(Q).catch(()=>null))?.isFile())await zQ(Q,V,q);else await nQ(Q,"",(H)=>zQ(N.join(Q,H),V,q),$);if($?.aborted)throw new W("grep: aborted");if(Y.length===0)return"no matches";return Y.join(`
`)}async function zQ(z,Q,$){let V=g.createReadStream(z,{encoding:"utf8"}),Y=YQ.createInterface({input:V,crlfDelay:1/0}),K=0;try{for await(let q of Y){if(K++,q.length>xQ)continue;if(Q.test(q)&&!$(`${z}:${K}:${q}`))return!1}}catch{}finally{V.destroy()}return!0}function _Q(z,Q){let $=N.relative(z,Q);return $===""||!$.startsWith(".."+N.sep)&&$!==".."&&!N.isAbsolute($)}async function nQ(z,Q,$,V){let Y=lQ;async function K(q,X){if(X>cQ)return!0;if(V?.aborted)return!1;let H;try{H=await k.readdir(N.join(z,q),{withFileTypes:!0})}catch{return!0}for(let Z of H){if(Z.name===".git"||Z.name==="node_modules")continue;if(Y--<=0)return!1;if(V?.aborted)return!1;let J=q?N.join(q,Z.name):Z.name;if(Z.isDirectory()){if(!await K(J,X+1))return!1}else if(Z.isFile()){if(await $(J)===!1)return!1}}return!0}await K(Q,0)}async function aQ(){let z=(process.env.PATH??"").split(N.delimiter);for(let Q of z){let $=N.join(Q,"rg");try{return await k.access($,g.constants.X_OK),$}catch{}}return null}var T,M,y,x,F,I,p,$Q=102400,qQ=120000,jQ=262144,P=102400,xQ=2000,PQ=200,EQ,uQ,cQ=40,lQ=50000;var iQ=b(()=>{bQ();r();a();n();BQ();d();QQ();EQ=/\x1b\[[0-9;?]*[ -/]*[@-~]/g,uQ=k.glob;M=new WeakMap,y=new WeakMap,x=new WeakMap,F=new WeakMap,I=new WeakMap,T=new WeakSet,p=function(Q){if(w(this,y,C(this,y,"f")+Q,"f"),C(this,y,"f").length>$Q)w(this,y,C(this,y,"f").slice(C(this,y,"f").length-$Q),"f"),w(this,x,!0,"f");if(C(this,I,"f")&&C(this,y,"f").indexOf(C(this,I,"f").sentinel)>=0){let $=C(this,I,"f");w(this,I,null,"f"),$.resolve()}}});iQ();export{AQ as setupSkills,t as resolveSkillVersion,E as resolvePath,e as extractSkillArchive,fQ as betaWriteTool,vQ as betaReadTool,dQ as betaGrepTool,mQ as betaGlobTool,SQ as betaEditTool,gQ as betaBashTool,O0 as betaAgentToolset20260401,XQ as BashSession};
