import{readFileSync as l,readdirSync as h,statSync as c,writeFileSync as f,mkdirSync as m}from"fs";import{join as p,resolve as y,relative as w}from"path";import{execSync as d}from"child_process";var a=256*1024,g=64*1024,N={id:"core-tools",description:"File read/write, directory listing, shell commands, open URLs, wait for async operations",envKeys:[],tools:[{name:"read_file",description:"Read the contents of a file. Returns the text content.",input_schema:{type:"object",properties:{path:{type:"string",description:"File path (relative to cwd or absolute)"}},required:["path"]}},{name:"write_file",description:"Write content to a file. Creates parent directories if needed.",input_schema:{type:"object",properties:{path:{type:"string",description:"File path (relative to cwd or absolute)"},content:{type:"string",description:"Content to write"}},required:["path","content"]}},{name:"list_directory",description:"List files and directories in a path. Returns names with type indicators (/ for dirs).",input_schema:{type:"object",properties:{path:{type:"string",description:"Directory path (relative to cwd or absolute). Defaults to cwd."}}}},{name:"run_command",description:"Run a shell command and return its output. Use for grep, git, npm, etc.",input_schema:{type:"object",properties:{command:{type:"string",description:"Shell command to execute"},cwd:{type:"string",description:"Working directory (optional, defaults to project root)"}},required:["command"]}},{name:"open_url",description:"Open a URL in the user's default browser. Use for OAuth flows, documentation, integration setup pages.",input_schema:{type:"object",properties:{url:{type:"string",description:"URL to open"}},required:["url"]}},{name:"wait",description:"Wait for N seconds. Use this for async operations (tests, builds, deploys) \u2014 wait, then check status again.",input_schema:{type:"object",properties:{seconds:{type:"number",description:"Seconds to wait (default: 5, max: 300)"},reason:{type:"string",description:"Why waiting (for logging/clarity)"}}}}],async handleToolCall(r,t,e){let n=e?.options?.workspace||process.cwd();try{switch(r){case"read_file":return _(t,n);case"write_file":return S(t,n);case"list_directory":return b(t,n);case"run_command":return O(t,n);case"open_url":return k(t);case"wait":return await v(t,e?.options?.signal);default:return JSON.stringify({error:`Unknown tool: ${r}`})}}catch(o){return JSON.stringify({error:o.message})}},resolve(){return null}};function s(r,t){return y(t,r)}function _(r,t){let e=s(r.path,t),n=c(e);return n.size>a?JSON.stringify({error:`File too large (${(n.size/1024).toFixed(0)}KB). Max: ${a/1024}KB`}):l(e,"utf-8")}function S(r,t){let e=s(r.path,t),n=p(e,"..");return m(n,{recursive:!0}),f(e,r.content,"utf-8"),JSON.stringify({ok:!0,path:w(t,e)})}function b(r,t){let e=s(r.path||".",t);return h(e).map(o=>{try{return c(p(e,o)).isDirectory()?`${o}/`:o}catch{return o}}).join(`
`)}function O(r,t){let e=r.cwd?s(r.cwd,t):t;return d(r.command,{cwd:e,encoding:"utf-8",timeout:3e4,maxBuffer:g,stdio:["pipe","pipe","pipe"]})||"(no output)"}function k(r){let{url:t}=r;if(!t||!t.startsWith("http://")&&!t.startsWith("https://"))return JSON.stringify({error:"Invalid URL \u2014 must start with http:// or https://"});let e=process.platform,n=e==="darwin"?"open":e==="win32"?"start":"xdg-open";try{return d(`${n} "${t}"`,{stdio:"ignore",timeout:5e3}),JSON.stringify({ok:!0,opened:t})}catch{return JSON.stringify({ok:!1,error:`Could not open browser. Please visit: ${t}`})}}async function v(r,t){let e=Math.min(Math.max(r.seconds||5,1),300),n=r.reason||"async operation",o=500,i=Date.now()+e*1e3;for(;Date.now()<i;){if(t?.aborted)return JSON.stringify({ok:!0,waited:Math.round((e*1e3-(i-Date.now()))/1e3),reason:n,interrupted:!0});await new Promise(u=>setTimeout(u,Math.min(o,i-Date.now())))}return JSON.stringify({ok:!0,waited:e,reason:n})}export{N as coreToolsSkill};
