@netlify/agent-runner-cli 1.90.0 → 1.90.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bin-local.js +2 -2
- package/dist/bin.js +2 -2
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/dist/bin-local.js
CHANGED
|
@@ -5,11 +5,11 @@ import U from"process";import Jr from"path";import Kr from"fs";import Ci from"mi
|
|
|
5
5
|
${s}
|
|
6
6
|
</extracted_error_chunk>`).join(`
|
|
7
7
|
|
|
8
|
-
`);return o.length>e.length*.8?e:o}import{execSync as Vn}from"child_process";import Rr from"fs/promises";import zn from"path";import he from"process";import{getTracer as Xn}from"@netlify/otel";import ke from"process";var Z=class extends Error{constructor(r,i,n,o=!1){super(r);this.statusCode=i;this.userMessage=n;this.isCreditLimitExceeded=o;this.name="GracefulShutdownError"}},je=e=>e instanceof Z;var Ye=ke.env.NETLIFY_API_URL,qe=ke.env.NETLIFY_API_TOKEN,J=x("api"),_e=()=>ke.env.NETLIFY_LOCAL_MODE==="true",le=async(e,t={})=>{if(!Ye||!qe)throw new Error("No API URL or token");let r=new URL(e,Ye),i={...t,headers:{...t.headers,Authorization:`Bearer ${qe}`}};ke.env.AGENT_RUNNERS_DEBUG==="true"&&(i.headers["x-nf-debug-logging"]="true"),t.json&&(i.headers||={},i.headers["Content-Type"]="application/json",i.body=JSON.stringify(t.json));let n=await fetch(r,i),o=n.ok&&n.status<=299;if(ke.env.AGENT_RUNNERS_DEBUG==="true")J.log(`Response headers for ${r}:`),n.headers.forEach((a,c)=>{J.log(` ${c}: ${a}`)});else{let a=n.headers.get("x-request-id")||n.headers.get("x-nf-request-id");J.log(`Request ID for ${r}: ${a||"N/A"}`)}if(o||J.error(`Got status ${n.status} for request ${r}`),t.raw){if(!o)throw new Error(`API request failed: ${n.status} ${n.statusText}`);return n}let s=await(n.headers.get("content-type")?.includes("application/json")?n.json():n.text());if(!o){let a=typeof s=="string"?s:JSON.stringify(s);throw n.status===404?new Z(`API request failed: 404 - ${a}`,404,"The site associated with this agent run no longer exists."):n.status===503&&t.gracefulOn503&&a.toLowerCase().includes("usage exceeded")?new Z(`API request failed: 503 - ${a}`,503,"Credit limit reached. Please add more credits to continue using Agent Runners.",!0):new Error(`API request failed: ${n.status} - ${a}`)}return s},Lt=e=>{J.log("Setting details for api",{apiUrl:e?.constants?.NETLIFY_API_HOST,token:!!e?.constants?.NETLIFY_API_TOKEN}),e?.constants?.NETLIFY_API_HOST&&(Ye=`https://${e.constants.NETLIFY_API_HOST}`),e?.constants?.NETLIFY_API_TOKEN&&(qe=e.constants.NETLIFY_API_TOKEN)},Mt=()=>({apiUrl:Ye,token:qe}),Pe=async(e,t)=>_e()?(J.log("Mock API: updateRunner called",{runnerId:e,data:t}),{id:e,...t}):le(`/api/v1/agent_runners/${e}`,{method:"PUT",json:t}),K=async(e,t,r)=>_e()?(J.log("Mock API: updateRunnerSession called",JSON.stringify({runnerId:e,sessionId:t,data:r},null,2)),{id:e,sessionId:t,...r}):le(`/api/v1/agent_runners/${e}/sessions/${t}`,{method:"PUT",json:r});var Ut=async e=>_e()?(J.log("Mock API: getSite called",{siteId:e}),{id:e,published_deploy:{id:"id"}}):le(`/api/v1/sites/${e}`),Gt=async(e,t)=>_e()?(J.log("Mock API: getRunnerSession called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,state:"running"}):le(`/api/v1/agent_runners/${e}/sessions/${t}`),jt=(e,t,r)=>le(`/api/v1/accounts/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Yt=(e,t,r)=>le(`/api/v1/sites/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),qt=async(e,t)=>_e()?(J.log("Mock API: getDiffUploadUrls called",{runnerId:e,sessionId:t}),{result:{upload_url:"https://s3.mock.com/mock-upload-url-result",s3_key:"mock-s3-key-result"},cumulative:{upload_url:"https://s3.mock.com/mock-upload-url-cumulative",s3_key:"mock-s3-key-cumulative"}}):le(`/api/v1/agent_runners/${e}/sessions/${t}/diff/upload_urls`,{method:"POST"}),Bt=async(e,t)=>_e()?(J.log("Mock API: updateSessionUsage called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,usage:0}):le(`/api/v1/agent_runners/${e}/sessions/${t}/update_usage`,{method:"POST"}),at=async(e,t)=>{J.log(`Uploading diff to S3: ${e.substring(0,50)}...`);let r=await fetch(e,{method:"PUT",body:t,headers:{"Content-Type":"text/plain"}});if(!r.ok)throw new Error(`S3 upload failed with status ${r.status}`);return r};var Ee=x("ai_gateway"),lt=null;var Ht=async()=>{if(lt)return lt;Ee.log("Fetching available AI gateway providers");let e=await fetch(`${Mt().apiUrl}/api/v1/ai-gateway/providers`);if(!e.ok)throw new Error(`Failed to fetch AI gateway providers: ${e.statusText}`);let t=await e.json();return lt=t,Ee.log("Cached AI gateway providers",{providerCount:Object.keys(t.providers).length}),t},rn=async(e,t)=>{let i=(await Ht()).providers[e];if(!i)return Ee.log(`Provider '${e}' not found`),!1;let n=i.models.includes(t);return Ee.log(`Model validation for ${e}/${t}`,{isAvailable:n}),n},Wt=async({config:e})=>{let t,r,i,n,o=!e.site?.published_deploy;if(!(o?e.accountId:e.siteId))throw new Error(`No entity id for ${o?"account":"site"}`);let a=async()=>{clearTimeout(i),Ee.log("Requesting AI gateway information");let l=await(o?jt(e.accountId,e.id,e.sessionId):Yt(e.siteId,e.id,e.sessionId));if({token:t,url:n}=l,r=l.expires_at?l.expires_at*1e3:void 0,Ee.log("Got AI gateway information",{token:!!t,expiresAt:r,url:n}),r){let d=r-Date.now()-6e4;d>0&&(i=setTimeout(()=>{a()},d))}};return await Promise.all([a(),Ht()]),{get url(){return n},get token(){return t},isModelAvailableForProvider:rn}};import ee from"process";import oe from"path";import Be from"fs";import{fileURLToPath as un}from"url";import{createRequire as dn}from"module";import{execa as pn,execaCommand as lo}from"execa";import{Transform as nn}from"stream";function on(){let e=process.env.NETLIFY_SENSITIVE_ENV_KEYS;return e?e.split(",").map(t=>t.trim()).filter(Boolean):[]}function sn(e){let t=e.toLowerCase();return t==="true"||t==="false"?!0:e.trim().length<4}function an(){let t=on().map(r=>process.env[r]).filter(r=>!(!r||sn(r)));return[...new Set(t)].sort((r,i)=>i.length-r.length)}function ie(e){if(typeof e!="string")return e;let t=an();if(t.length===0)return e;let r=e;return t.forEach(i=>{let n=new RegExp(ln(i),"g");r=r.replace(n,"******")}),r}function ln(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var xe=class extends nn{constructor(t={}){super({...t,objectMode:!1})}_transform(t,r,i){let n=t.toString(),o=ie(n);i(null,o)}};function Jt(){if(!(process.env.NETLIFY_MASK_LOGS!=="false"))return;let t=process.stdout.write.bind(process.stdout),r=process.stderr.write.bind(process.stderr);process.stdout.write=function(i,n,o){let s=typeof i=="string"?ie(i):i;return typeof n=="function"?t(s,n):t(s,n,o)},process.stderr.write=function(i,n,o){let s=typeof i=="string"?ie(i):i;return typeof n=="function"?r(s,n):r(s,n,o)}}var Ne=null,Kt=e=>(Ne&&Ne.destroy(),Ne=new ce({totalAllowedTime:e}),Ne),Vt=()=>Ne;var ce=class{constructor({totalAllowedTime:t}){this.withStageTimer=async(t,r,i)=>{if(this.isTimeExpired())throw new Error(`${t} stage did not complete in the allowed time. Time has already expired.`);let n=this.onTimesUp(()=>{throw new Error(`${t} stage did not complete in the allowed time.`)}),o=null,s=null;i!==void 0&&(s=new Promise((a,c)=>{o=setTimeout(()=>{c(new Error(`${t} stage exceeded its maximum duration of ${i}ms`))},i)}));try{return s?await Promise.race([r(),s]):await r()}finally{n(),o&&clearTimeout(o)}};this.startTime=Date.now(),this.totalAllowedTime=t,this.globalTimeoutId=null,this.subscribers=[],this.hasTimedOut=!1,this.setupGlobalTimeout()}getElapsedTime(){return Date.now()-this.startTime}getRemainingTime(){let t=this.getElapsedTime(),r=this.totalAllowedTime-t;return Math.max(0,r)}isTimeExpired(){return this.getRemainingTime()===0||this.hasTimedOut}setupGlobalTimeout(){this.globalTimeoutId&&clearTimeout(this.globalTimeoutId),this.globalTimeoutId=setTimeout(()=>{this.notifyTimeUp()},this.totalAllowedTime)}notifyTimeUp(){this.hasTimedOut=!0;for(let t=this.subscribers.length-1;t>=0;t--)try{this.subscribers[t]()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}}onTimesUp(t){if(this.subscribers.push(t),this.hasTimedOut)try{t()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}return()=>{let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}}off(t){let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}clearSubscribers(){this.subscribers.length=0}getSubscriberCount(){return this.subscribers.length}destroy(){this.globalTimeoutId&&(clearTimeout(this.globalTimeoutId),this.globalTimeoutId=null),this.clearSubscribers()}static{this.timeUnits={seconds:t=>t*1e3,minutes:t=>t*60*1e3,hours:t=>t*60*60*1e3}}};var zt="netlify-agent-runner-context.md",ct="task-history",Q=".netlify",pe="results.md",ut="assets";var Xt="free";var me=1800*1e3,y={Environment:"environment",UserMessage:"user-message",AgentMessage:"agent-message",Task:"task",RunCommand:"run-command",Explore:"explore",Plan:"plan",FileRead:"file-read",FileWrite:"file-write",Notebook:"notebook",Web:"web",Todo:"todo",Reasoning:"reasoning",Skill:"skill",Memorize:"memorize",Deployment:"deployment",SiteGeneration:"site-generation"};var Zt={name:"@netlify/agent-runner-cli",type:"module",version:"1.90.0",description:"CLI tool for running Netlify agents",main:"./dist/index.js",types:"./dist/index.d.ts",exports:"./dist/index.js",bin:{"agent-runner-cli":"./dist/bin.js","agent-runner-cli-local":"./dist/bin-local.js"},files:["dist/**/*.js","dist/**/*.d.ts","dist/skills/**","patches","scripts"],scripts:{build:"tsup",dev:"tsup --watch",prepare:"husky install node_modules/@netlify/eslint-config-node/.husky/",prepublishOnly:"npm ci && npm test",prepack:"npm run build",test:"run-s build format test:dev",format:"run-s build format:check-fix:*","format:ci":"run-s build format:check:*","format:check-fix:lint":"run-e format:check:lint format:fix:lint","format:check:lint":"cross-env-shell eslint $npm_package_config_eslint","format:fix:lint":"cross-env-shell eslint --fix $npm_package_config_eslint","format:check-fix:prettier":"run-e format:check:prettier format:fix:prettier","format:check:prettier":"cross-env-shell prettier --check $npm_package_config_prettier","format:fix:prettier":"cross-env-shell prettier --write $npm_package_config_prettier","test:dev":"run-s build test:dev:*","test:ci":"run-s build test:ci:*","test:dev:vitest":"LOG=0 vitest --exclude '**/integration/**'","test:ci:vitest":"LOG=0 c8 -r lcovonly -r text -r json vitest --exclude '**/integration/**'","test:integration":"vitest run test/integration/","test:integration:codex":"vitest run test/integration/codex.test.ts","test:integration:claude":"vitest run test/integration/claude.test.ts","test:integration:gemini":"vitest run test/integration/gemini.test.ts","test:integration:create-stage":"vitest run test/integration/create.test.ts","test:integration:skill-invocation":"vitest run test/integration/skill-invocation.test.ts","check:types":"tsc --noEmit",postinstall:"node scripts/postinstall.js"},config:{eslint:'--cache --format=codeframe --max-warnings=0 "{src,scripts,test,.github}/**/*.{js,ts,md,html}"',prettier:'--ignore-path .gitignore --loglevel=warn "{src,scripts,test,.github}/**/*.{js,ts,md,yml,json,html}" "*.{js,ts,yml,json,html}" ".*.{js,ts,yml,json,html}" "!**/package-lock.json" "!package-lock.json" "!src/skills/**/*.md"'},keywords:[],license:"MIT",repository:"netlify/agent-runner-cli",bugs:{url:"https://github.com/netlify/agent-runner-cli/issues"},author:"Netlify Inc.",directories:{test:"test"},devDependencies:{"@commitlint/cli":"^20.0.0","@commitlint/config-conventional":"^20.0.0","@eslint/compat":"^2.0.0","@eslint/js":"^9.35.0","@netlify/eslint-config-node":"^7.0.1","@types/node":"^24.5.0","@typescript-eslint/eslint-plugin":"^8.0.0","@typescript-eslint/parser":"^8.0.0","@vitest/eslint-plugin":"^1.6.6",c8:"^10.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-n":"^17.0.0",husky:"^9.0.0","patch-package":"^8.0.0",tsup:"^8.5.0",typescript:"^5.0.0","typescript-eslint":"^8.44.0",vitest:"^4.0.16"},dependencies:{"@anthropic-ai/claude-code":"2.1.63","@anthropic-ai/sdk":"0.78.0","@google/gemini-cli":"0.31.0","@netlify/otel":"^5.1.2","@openai/codex":"0.110.0","@opentelemetry/exporter-trace-otlp-grpc":"0.57.2",execa:"^9.6.1",kaddidlehopper:"^0.7.2",minimist:"^1.2.8",openai:"6.26.0"}};var mn=un(import.meta.url),gn=oe.dirname(mn),fn=dn(import.meta.url),Te=x("shell"),dt=new Set,hn={preferLocal:!0},A=(e,t,r)=>{let[i,n]=yn(t,r),o={...hn,...n},s=pn(e,i,o);wn(s,o),En(s);let a=r?.idleTimeout;return a&&a>0&&_n(s,a),s};var yn=function(e,t){return Array.isArray(e)?[e,t]:typeof e=="object"&&e!==null?[[],e]:[[],void 0]},wn=(e,t)=>{if(t.stdio!==void 0||t.stdout!==void 0||t.stderr!==void 0)return;if(ee.env.NETLIFY_MASK_LOGS!=="false"){e.all?.pipe(new xe).pipe(ee.stdout),e.stdout?.pipe(new xe).pipe(ee.stdout),e.stderr?.pipe(new xe).pipe(ee.stderr);return}e.stdout?.pipe(ee.stdout),e.stderr?.pipe(ee.stderr)},pt=(e,t="SIGTERM")=>{try{return e.pid&&!e.killed?(ee.kill(-e.pid,t),Te.log(`Killed process ${e.pid} with signal ${t}`),!0):!1}catch(r){return Te.error("Error killing process:",r),!1}},Qt=e=>pt(e,"SIGKILL"),_n=(e,t)=>{let r=null,i=()=>{Te.log(`Process ${e.pid} killed due to idle timeout (no output for ${t}ms)`),pt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(Te.log(`Force killing idle process ${e.pid}`),Qt(e))},5e3)},n=()=>{r&&clearTimeout(r),r=setTimeout(i,t)};n(),e.stdout?.on("data",n),e.stderr?.on("data",n);let o=()=>{r&&(clearTimeout(r),r=null)};e.on("exit",o),e.on("error",o)},En=e=>{dt.add(e);let t=Vt();if(t){let r=t.onTimesUp(()=>{Te.log(`Global timer expired, killing process ${e.pid}`),pt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(Te.log(`Force killing process ${e.pid} after timeout`),Qt(e))},5e3)});e.on("exit",()=>{dt.delete(e),r()}),e.on("error",()=>{dt.delete(e),r()})}};function He(e,t){return!!se(e,t)}function se(e,t){if(!ee.env.NETLIFY_LOCAL_MODE)try{let n=fn.resolve(Zt.name),o=oe.dirname(n);for(;o!==oe.dirname(o);){let s=oe.dirname(o);if(oe.basename(s)==="node_modules"){let a=oe.join(s,".bin",t);if(Be.existsSync(a))return a;break}o=s}}catch(n){console.error("Could not resolve package.json",n)}if(ee.env.NODE_PATH){let n=oe.join(ee.env.NODE_PATH,".bin",t);if(Be.existsSync(n))return n}let r=oe.join(e,"node_modules",".bin",t);if(Be.existsSync(r))return r;let i=oe.join(gn,"..","node_modules",".bin",t);if(Be.existsSync(i))return i}var xn=x("utils"),Tn=e=>new Promise(t=>{setTimeout(t,e)}),We=(e,t=3e3)=>{let r=!1,i=null,n=[],o=null,s=(...a)=>{if(r)return i=a,new Promise(d=>{n.push(d)});r=!0;let c,l=new Promise(d=>{c=d});return o=(async()=>{await Promise.resolve();let d=await e(...a);for(c(d);;){if(await Tn(t),!i)return r=!1,o=null,d;let m=i,u=n;i=null,n=[],d=await e(...m),u.forEach(w=>{w(d)})}})(),l};return s.flush=async()=>{if((r||i)&&o)return await o,s.flush()},s},Ie=(e,t,r=!1)=>{let i=null,n=null,o=null,s=function(...a){n=a,o=this;let c=r&&!i;clearTimeout(i),i=setTimeout(()=>{i=null,r||(e.apply(o,n),n=null,o=null)},t),c&&(e.apply(o,n),n=null,o=null)};return s.cancel=()=>{clearTimeout(i),i=null,n=null,o=null},s.flush=()=>{if(i){clearTimeout(i);let a=n,c=o;i=null,n=null,o=null,e.apply(c,a)}},s},er=(e,t=!0,r)=>{if(e)try{return JSON.parse(e)}catch(i){t&&(r?.error?r.error("Could not parse JSON",i):xn.error("Could not parse JSON",i))}},mt=e=>e.charAt(0).toUpperCase()+e.slice(1),ue=e=>e.split("-").map(t=>t.length===2?t.toUpperCase():mt(t)).join(" ");function ge(e,t){t&&e.log(`Skill invoked: ${t}`)}var tr=e=>Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0)),rr=(e,t)=>{let n=".netlify.app",o="agent-";if(!t)return`${o}${e.slice(0,6)}`;let a=`--${t}${n}`;if(a.length>55)return"";let c=60-a.length;if(c<=0)return"";if(c>=o.length+6){let l=Math.min(c-o.length,e.length);return`${o}${e.slice(0,l)}`}return e.slice(0,c)};var In=50*1024,gt=(e,t=In)=>{if(!e||typeof e!="string"||e.length<=t)return e;let i=e.startsWith("```")?"\n... [truncated]\n```":"... [truncated]";return e.slice(0,t)+i};import{Buffer as nr}from"buffer";import Sn from"path";var ir=x("repo"),sr=async({config:e,isRetry:t,cwd:r=process.cwd()})=>{ir.info("Getting runner diffs");let i=await Rn(r),{hasChanges:n}=i,{status:o}=i;if(!n)return{hasChanges:!1};if(!t){let _=An(o);await Cn(_,r)}ir.info("Changes after processing"),await ht(r);let s=await yt(o,r);if(await ft(s,r),n=await bn(r),!n)return{hasChanges:!1,ignored:s};process.env.NETLIFY_INTERNAL_GIT="1";try{await A("git",["commit","-m","Agent runner"],{cwd:r})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}let a={stdio:["ignore","pipe","pipe"],cwd:r},c=await A("git",["diff",e.runSha,"HEAD"],a),l=String(c.stdout??"");if(n=!!l,!n)return await or(r),{hasChanges:!1,ignored:s};let d=await A("git",["diff",e.runSha,"HEAD","--binary"],a),m=String(d.stdout??""),u,w;if(e.sha){let _=await A("git",["diff",e.sha,"HEAD"],a);u=String(_.stdout??"");let S=await A("git",["diff",e.sha,"HEAD","--binary"],a),h=String(S.stdout??"");u!==h&&(w=nr.from(h).toString("base64"))}await or(r);let E={hasChanges:!0,diff:l,resultDiff:u,ignored:s};return l!==m&&(E.diffBinary=nr.from(m).toString("base64")),w&&(E.resultDiffBinary=w),E},or=async(e=process.cwd())=>{process.env.NETLIFY_LOCAL_MODE&&await A("git",["reset","--soft","HEAD~1"],{cwd:e})},ft=async(e=[],t=process.cwd())=>{process.env.NETLIFY_INTERNAL_GIT="1";try{await A("git",["add",".",...e],{cwd:t})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}},ht=async(e=process.cwd())=>{let t=await A("git",["status","-s"],{cwd:e});return String(t.stdout??"")},ar=/.. (.+)?\.log$/,vn=[ar],Rn=async(e=process.cwd())=>{let t=await ht(e);return{hasChanges:(t.trim().length===0?[]:t.split(`
|
|
8
|
+
`);return o.length>e.length*.8?e:o}import{execSync as Vn}from"child_process";import Rr from"fs/promises";import zn from"path";import he from"process";import{getTracer as Xn}from"@netlify/otel";import ke from"process";var Z=class extends Error{constructor(r,i,n,o=!1){super(r);this.statusCode=i;this.userMessage=n;this.isCreditLimitExceeded=o;this.name="GracefulShutdownError"}},je=e=>e instanceof Z;var Ye=ke.env.NETLIFY_API_URL,qe=ke.env.NETLIFY_API_TOKEN,J=x("api"),_e=()=>ke.env.NETLIFY_LOCAL_MODE==="true",le=async(e,t={})=>{if(!Ye||!qe)throw new Error("No API URL or token");let r=new URL(e,Ye),i={...t,headers:{...t.headers,Authorization:`Bearer ${qe}`}};ke.env.AGENT_RUNNERS_DEBUG==="true"&&(i.headers["x-nf-debug-logging"]="true"),t.json&&(i.headers||={},i.headers["Content-Type"]="application/json",i.body=JSON.stringify(t.json));let n=await fetch(r,i),o=n.ok&&n.status<=299;if(ke.env.AGENT_RUNNERS_DEBUG==="true")J.log(`Response headers for ${r}:`),n.headers.forEach((a,c)=>{J.log(` ${c}: ${a}`)});else{let a=n.headers.get("x-request-id")||n.headers.get("x-nf-request-id");J.log(`Request ID for ${r}: ${a||"N/A"}`)}if(o||J.error(`Got status ${n.status} for request ${r}`),t.raw){if(!o)throw new Error(`API request failed: ${n.status} ${n.statusText}`);return n}let s=await(n.headers.get("content-type")?.includes("application/json")?n.json():n.text());if(!o){let a=typeof s=="string"?s:JSON.stringify(s);throw n.status===404?new Z(`API request failed: 404 - ${a}`,404,"The site associated with this agent run no longer exists."):n.status===503&&t.gracefulOn503&&a.toLowerCase().includes("usage exceeded")?new Z(`API request failed: 503 - ${a}`,503,"Credit limit reached. Please add more credits to continue using Agent Runners.",!0):new Error(`API request failed: ${n.status} - ${a}`)}return s},Lt=e=>{J.log("Setting details for api",{apiUrl:e?.constants?.NETLIFY_API_HOST,token:!!e?.constants?.NETLIFY_API_TOKEN}),e?.constants?.NETLIFY_API_HOST&&(Ye=`https://${e.constants.NETLIFY_API_HOST}`),e?.constants?.NETLIFY_API_TOKEN&&(qe=e.constants.NETLIFY_API_TOKEN)},Mt=()=>({apiUrl:Ye,token:qe}),Pe=async(e,t)=>_e()?(J.log("Mock API: updateRunner called",{runnerId:e,data:t}),{id:e,...t}):le(`/api/v1/agent_runners/${e}`,{method:"PUT",json:t}),K=async(e,t,r)=>_e()?(J.log("Mock API: updateRunnerSession called",JSON.stringify({runnerId:e,sessionId:t,data:r},null,2)),{id:e,sessionId:t,...r}):le(`/api/v1/agent_runners/${e}/sessions/${t}`,{method:"PUT",json:r});var Ut=async e=>_e()?(J.log("Mock API: getSite called",{siteId:e}),{id:e,published_deploy:{id:"id"}}):le(`/api/v1/sites/${e}`),Gt=async(e,t)=>_e()?(J.log("Mock API: getRunnerSession called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,state:"running"}):le(`/api/v1/agent_runners/${e}/sessions/${t}`),jt=(e,t,r)=>le(`/api/v1/accounts/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Yt=(e,t,r)=>le(`/api/v1/sites/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),qt=async(e,t)=>_e()?(J.log("Mock API: getDiffUploadUrls called",{runnerId:e,sessionId:t}),{result:{upload_url:"https://s3.mock.com/mock-upload-url-result",s3_key:"mock-s3-key-result"},cumulative:{upload_url:"https://s3.mock.com/mock-upload-url-cumulative",s3_key:"mock-s3-key-cumulative"}}):le(`/api/v1/agent_runners/${e}/sessions/${t}/diff/upload_urls`,{method:"POST"}),Bt=async(e,t)=>_e()?(J.log("Mock API: updateSessionUsage called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,usage:0}):le(`/api/v1/agent_runners/${e}/sessions/${t}/update_usage`,{method:"POST"}),at=async(e,t)=>{J.log(`Uploading diff to S3: ${e.substring(0,50)}...`);let r=await fetch(e,{method:"PUT",body:t,headers:{"Content-Type":"text/plain"}});if(!r.ok)throw new Error(`S3 upload failed with status ${r.status}`);return r};var Ee=x("ai_gateway"),lt=null;var Ht=async()=>{if(lt)return lt;Ee.log("Fetching available AI gateway providers");let e=await fetch(`${Mt().apiUrl}/api/v1/ai-gateway/providers`);if(!e.ok)throw new Error(`Failed to fetch AI gateway providers: ${e.statusText}`);let t=await e.json();return lt=t,Ee.log("Cached AI gateway providers",{providerCount:Object.keys(t.providers).length}),t},rn=async(e,t)=>{let i=(await Ht()).providers[e];if(!i)return Ee.log(`Provider '${e}' not found`),!1;let n=i.models.includes(t);return Ee.log(`Model validation for ${e}/${t}`,{isAvailable:n}),n},Wt=async({config:e})=>{let t,r,i,n,o=!e.site?.published_deploy;if(!(o?e.accountId:e.siteId))throw new Error(`No entity id for ${o?"account":"site"}`);let a=async()=>{clearTimeout(i),Ee.log("Requesting AI gateway information");let l=await(o?jt(e.accountId,e.id,e.sessionId):Yt(e.siteId,e.id,e.sessionId));if({token:t,url:n}=l,r=l.expires_at?l.expires_at*1e3:void 0,Ee.log("Got AI gateway information",{token:!!t,expiresAt:r,url:n}),r){let d=r-Date.now()-6e4;d>0&&(i=setTimeout(()=>{a()},d))}};return await Promise.all([a(),Ht()]),{get url(){return n},get token(){return t},isModelAvailableForProvider:rn}};import ee from"process";import oe from"path";import Be from"fs";import{fileURLToPath as un}from"url";import{createRequire as dn}from"module";import{execa as pn,execaCommand as lo}from"execa";import{Transform as nn}from"stream";function on(){let e=process.env.NETLIFY_SENSITIVE_ENV_KEYS;return e?e.split(",").map(t=>t.trim()).filter(Boolean):[]}function sn(e){let t=e.toLowerCase();return t==="true"||t==="false"?!0:e.trim().length<4}function an(){let t=on().map(r=>process.env[r]).filter(r=>!(!r||sn(r)));return[...new Set(t)].sort((r,i)=>i.length-r.length)}function ie(e){if(typeof e!="string")return e;let t=an();if(t.length===0)return e;let r=e;return t.forEach(i=>{let n=new RegExp(ln(i),"g");r=r.replace(n,"******")}),r}function ln(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var xe=class extends nn{constructor(t={}){super({...t,objectMode:!1})}_transform(t,r,i){let n=t.toString(),o=ie(n);i(null,o)}};function Jt(){if(!(process.env.NETLIFY_MASK_LOGS!=="false"))return;let t=process.stdout.write.bind(process.stdout),r=process.stderr.write.bind(process.stderr);process.stdout.write=function(i,n,o){let s=typeof i=="string"?ie(i):i;return typeof n=="function"?t(s,n):t(s,n,o)},process.stderr.write=function(i,n,o){let s=typeof i=="string"?ie(i):i;return typeof n=="function"?r(s,n):r(s,n,o)}}var Ne=null,Kt=e=>(Ne&&Ne.destroy(),Ne=new ce({totalAllowedTime:e}),Ne),Vt=()=>Ne;var ce=class{constructor({totalAllowedTime:t}){this.withStageTimer=async(t,r,i)=>{if(this.isTimeExpired())throw new Error(`${t} stage did not complete in the allowed time. Time has already expired.`);let n=this.onTimesUp(()=>{throw new Error(`${t} stage did not complete in the allowed time.`)}),o=null,s=null;i!==void 0&&(s=new Promise((a,c)=>{o=setTimeout(()=>{c(new Error(`${t} stage exceeded its maximum duration of ${i}ms`))},i)}));try{return s?await Promise.race([r(),s]):await r()}finally{n(),o&&clearTimeout(o)}};this.startTime=Date.now(),this.totalAllowedTime=t,this.globalTimeoutId=null,this.subscribers=[],this.hasTimedOut=!1,this.setupGlobalTimeout()}getElapsedTime(){return Date.now()-this.startTime}getRemainingTime(){let t=this.getElapsedTime(),r=this.totalAllowedTime-t;return Math.max(0,r)}isTimeExpired(){return this.getRemainingTime()===0||this.hasTimedOut}setupGlobalTimeout(){this.globalTimeoutId&&clearTimeout(this.globalTimeoutId),this.globalTimeoutId=setTimeout(()=>{this.notifyTimeUp()},this.totalAllowedTime)}notifyTimeUp(){this.hasTimedOut=!0;for(let t=this.subscribers.length-1;t>=0;t--)try{this.subscribers[t]()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}}onTimesUp(t){if(this.subscribers.push(t),this.hasTimedOut)try{t()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}return()=>{let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}}off(t){let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}clearSubscribers(){this.subscribers.length=0}getSubscriberCount(){return this.subscribers.length}destroy(){this.globalTimeoutId&&(clearTimeout(this.globalTimeoutId),this.globalTimeoutId=null),this.clearSubscribers()}static{this.timeUnits={seconds:t=>t*1e3,minutes:t=>t*60*1e3,hours:t=>t*60*60*1e3}}};var zt="netlify-agent-runner-context.md",ct="task-history",Q=".netlify",pe="results.md",ut="assets";var Xt="free";var me=1800*1e3,y={Environment:"environment",UserMessage:"user-message",AgentMessage:"agent-message",Task:"task",RunCommand:"run-command",Explore:"explore",Plan:"plan",FileRead:"file-read",FileWrite:"file-write",Notebook:"notebook",Web:"web",Todo:"todo",Reasoning:"reasoning",Skill:"skill",Memorize:"memorize",Deployment:"deployment",SiteGeneration:"site-generation"};var Zt={name:"@netlify/agent-runner-cli",type:"module",version:"1.90.1",description:"CLI tool for running Netlify agents",main:"./dist/index.js",types:"./dist/index.d.ts",exports:"./dist/index.js",bin:{"agent-runner-cli":"./dist/bin.js","agent-runner-cli-local":"./dist/bin-local.js"},files:["dist/**/*.js","dist/**/*.d.ts","dist/skills/**","patches","scripts"],scripts:{build:"tsup",dev:"tsup --watch",prepare:"husky install node_modules/@netlify/eslint-config-node/.husky/",prepublishOnly:"npm ci && npm test",prepack:"npm run build",test:"run-s build format test:dev",format:"run-s build format:check-fix:*","format:ci":"run-s build format:check:*","format:check-fix:lint":"run-e format:check:lint format:fix:lint","format:check:lint":"cross-env-shell eslint $npm_package_config_eslint","format:fix:lint":"cross-env-shell eslint --fix $npm_package_config_eslint","format:check-fix:prettier":"run-e format:check:prettier format:fix:prettier","format:check:prettier":"cross-env-shell prettier --check $npm_package_config_prettier","format:fix:prettier":"cross-env-shell prettier --write $npm_package_config_prettier","test:dev":"run-s build test:dev:*","test:ci":"run-s build test:ci:*","test:dev:vitest":"LOG=0 vitest --exclude '**/integration/**'","test:ci:vitest":"LOG=0 c8 -r lcovonly -r text -r json vitest --exclude '**/integration/**'","test:integration":"vitest run test/integration/","test:integration:codex":"vitest run test/integration/codex.test.ts","test:integration:claude":"vitest run test/integration/claude.test.ts","test:integration:gemini":"vitest run test/integration/gemini.test.ts","test:integration:create-stage":"vitest run test/integration/create.test.ts","test:integration:skill-invocation":"vitest run test/integration/skill-invocation.test.ts","check:types":"tsc --noEmit",postinstall:"node scripts/postinstall.js"},config:{eslint:'--cache --format=codeframe --max-warnings=0 "{src,scripts,test,.github}/**/*.{js,ts,md,html}"',prettier:'--ignore-path .gitignore --loglevel=warn "{src,scripts,test,.github}/**/*.{js,ts,md,yml,json,html}" "*.{js,ts,yml,json,html}" ".*.{js,ts,yml,json,html}" "!**/package-lock.json" "!package-lock.json" "!src/skills/**/*.md"'},keywords:[],license:"MIT",repository:"netlify/agent-runner-cli",bugs:{url:"https://github.com/netlify/agent-runner-cli/issues"},author:"Netlify Inc.",directories:{test:"test"},devDependencies:{"@commitlint/cli":"^20.0.0","@commitlint/config-conventional":"^20.0.0","@eslint/compat":"^2.0.0","@eslint/js":"^9.35.0","@netlify/eslint-config-node":"^7.0.1","@types/node":"^24.5.0","@typescript-eslint/eslint-plugin":"^8.0.0","@typescript-eslint/parser":"^8.0.0","@vitest/eslint-plugin":"^1.6.6",c8:"^10.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-n":"^17.0.0",husky:"^9.0.0","patch-package":"^8.0.0",tsup:"^8.5.0",typescript:"^5.0.0","typescript-eslint":"^8.44.0",vitest:"^4.0.16"},dependencies:{"@anthropic-ai/claude-code":"2.1.63","@anthropic-ai/sdk":"0.78.0","@google/gemini-cli":"0.31.0","@netlify/otel":"^5.1.2","@openai/codex":"0.110.0","@opentelemetry/exporter-trace-otlp-grpc":"0.57.2",execa:"^9.6.1",kaddidlehopper:"^0.7.2",minimist:"^1.2.8",openai:"6.26.0"}};var mn=un(import.meta.url),gn=oe.dirname(mn),fn=dn(import.meta.url),Te=x("shell"),dt=new Set,hn={preferLocal:!0},A=(e,t,r)=>{let[i,n]=yn(t,r),o={...hn,...n},s=pn(e,i,o);wn(s,o),En(s);let a=r?.idleTimeout;return a&&a>0&&_n(s,a),s};var yn=function(e,t){return Array.isArray(e)?[e,t]:typeof e=="object"&&e!==null?[[],e]:[[],void 0]},wn=(e,t)=>{if(t.stdio!==void 0||t.stdout!==void 0||t.stderr!==void 0)return;if(ee.env.NETLIFY_MASK_LOGS!=="false"){e.all?.pipe(new xe).pipe(ee.stdout),e.stdout?.pipe(new xe).pipe(ee.stdout),e.stderr?.pipe(new xe).pipe(ee.stderr);return}e.stdout?.pipe(ee.stdout),e.stderr?.pipe(ee.stderr)},pt=(e,t="SIGTERM")=>{try{return e.pid&&!e.killed?(ee.kill(-e.pid,t),Te.log(`Killed process ${e.pid} with signal ${t}`),!0):!1}catch(r){return Te.error("Error killing process:",r),!1}},Qt=e=>pt(e,"SIGKILL"),_n=(e,t)=>{let r=null,i=()=>{Te.log(`Process ${e.pid} killed due to idle timeout (no output for ${t}ms)`),pt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(Te.log(`Force killing idle process ${e.pid}`),Qt(e))},5e3)},n=()=>{r&&clearTimeout(r),r=setTimeout(i,t)};n(),e.stdout?.on("data",n),e.stderr?.on("data",n);let o=()=>{r&&(clearTimeout(r),r=null)};e.on("exit",o),e.on("error",o)},En=e=>{dt.add(e);let t=Vt();if(t){let r=t.onTimesUp(()=>{Te.log(`Global timer expired, killing process ${e.pid}`),pt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(Te.log(`Force killing process ${e.pid} after timeout`),Qt(e))},5e3)});e.on("exit",()=>{dt.delete(e),r()}),e.on("error",()=>{dt.delete(e),r()})}};function He(e,t){return!!se(e,t)}function se(e,t){if(!ee.env.NETLIFY_LOCAL_MODE)try{let n=fn.resolve(Zt.name),o=oe.dirname(n);for(;o!==oe.dirname(o);){let s=oe.dirname(o);if(oe.basename(s)==="node_modules"){let a=oe.join(s,".bin",t);if(Be.existsSync(a))return a;break}o=s}}catch(n){console.error("Could not resolve package.json",n)}if(ee.env.NODE_PATH){let n=oe.join(ee.env.NODE_PATH,".bin",t);if(Be.existsSync(n))return n}let r=oe.join(e,"node_modules",".bin",t);if(Be.existsSync(r))return r;let i=oe.join(gn,"..","node_modules",".bin",t);if(Be.existsSync(i))return i}var xn=x("utils"),Tn=e=>new Promise(t=>{setTimeout(t,e)}),We=(e,t=3e3)=>{let r=!1,i=null,n=[],o=null,s=(...a)=>{if(r)return i=a,new Promise(d=>{n.push(d)});r=!0;let c,l=new Promise(d=>{c=d});return o=(async()=>{await Promise.resolve();let d=await e(...a);for(c(d);;){if(await Tn(t),!i)return r=!1,o=null,d;let m=i,u=n;i=null,n=[],d=await e(...m),u.forEach(w=>{w(d)})}})(),l};return s.flush=async()=>{if((r||i)&&o)return await o,s.flush()},s},Ie=(e,t,r=!1)=>{let i=null,n=null,o=null,s=function(...a){n=a,o=this;let c=r&&!i;clearTimeout(i),i=setTimeout(()=>{i=null,r||(e.apply(o,n),n=null,o=null)},t),c&&(e.apply(o,n),n=null,o=null)};return s.cancel=()=>{clearTimeout(i),i=null,n=null,o=null},s.flush=()=>{if(i){clearTimeout(i);let a=n,c=o;i=null,n=null,o=null,e.apply(c,a)}},s},er=(e,t=!0,r)=>{if(e)try{return JSON.parse(e)}catch(i){t&&(r?.error?r.error("Could not parse JSON",i):xn.error("Could not parse JSON",i))}},mt=e=>e.charAt(0).toUpperCase()+e.slice(1),ue=e=>e.split("-").map(t=>t.length===2?t.toUpperCase():mt(t)).join(" ");function ge(e,t){t&&e.log(`Skill invoked: ${t}`)}var tr=e=>Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0)),rr=(e,t)=>{let n=".netlify.app",o="agent-";if(!t)return`${o}${e.slice(0,6)}`;let a=`--${t}${n}`;if(a.length>55)return"";let c=60-a.length;if(c<=0)return"";if(c>=o.length+6){let l=Math.min(c-o.length,e.length);return`${o}${e.slice(0,l)}`}return e.slice(0,c)};var In=50*1024,gt=(e,t=In)=>{if(!e||typeof e!="string"||e.length<=t)return e;let i=e.startsWith("```")?"\n... [truncated]\n```":"... [truncated]";return e.slice(0,t)+i};import{Buffer as nr}from"buffer";import Sn from"path";var ir=x("repo"),sr=async({config:e,isRetry:t,cwd:r=process.cwd()})=>{ir.info("Getting runner diffs");let i=await Rn(r),{hasChanges:n}=i,{status:o}=i;if(!n)return{hasChanges:!1};if(!t){let _=An(o);await Cn(_,r)}ir.info("Changes after processing"),await ht(r);let s=await yt(o,r);if(await ft(s,r),n=await bn(r),!n)return{hasChanges:!1,ignored:s};process.env.NETLIFY_INTERNAL_GIT="1";try{await A("git",["commit","-m","Agent runner"],{cwd:r})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}let a={stdio:["ignore","pipe","pipe"],cwd:r},c=await A("git",["diff",e.runSha,"HEAD"],a),l=String(c.stdout??"");if(n=!!l,!n)return await or(r),{hasChanges:!1,ignored:s};let d=await A("git",["diff",e.runSha,"HEAD","--binary"],a),m=String(d.stdout??""),u,w;if(e.sha){let _=await A("git",["diff",e.sha,"HEAD"],a);u=String(_.stdout??"");let S=await A("git",["diff",e.sha,"HEAD","--binary"],a),h=String(S.stdout??"");u!==h&&(w=nr.from(h).toString("base64"))}await or(r);let E={hasChanges:!0,diff:l,resultDiff:u,ignored:s};return l!==m&&(E.diffBinary=nr.from(m).toString("base64")),w&&(E.resultDiffBinary=w),E},or=async(e=process.cwd())=>{process.env.NETLIFY_LOCAL_MODE&&await A("git",["reset","--soft","HEAD~1"],{cwd:e})},ft=async(e=[],t=process.cwd())=>{process.env.NETLIFY_INTERNAL_GIT="1";try{await A("git",["add",".",...e],{cwd:t})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}},ht=async(e=process.cwd())=>{let t=await A("git",["status","-s"],{cwd:e});return String(t.stdout??"")},ar=/.. (.+)?\.log$/,vn=[ar],Rn=async(e=process.cwd())=>{let t=await ht(e);return{hasChanges:(t.trim().length===0?[]:t.split(`
|
|
9
9
|
`).filter(n=>vn.some(s=>s instanceof RegExp?s.test(n):n===s)?!1:n[1]?.trim()!=="")).length!==0,status:t}},bn=async(e=process.cwd())=>{try{return await A("git",["diff","--staged","--quiet"],{cwd:e}),!1}catch{return!0}},$e=async(e=process.cwd())=>{let{stdout:t}=await A("git",["rev-parse","HEAD"],{cwd:e});return String(t??"").trim()},lr=async(e=process.cwd())=>{let{stdout:t}=await A("git",["rev-list","--max-parents=0","HEAD"],{cwd:e});return String(t??"").trim()},yt=async(e,t=process.cwd())=>{e||=await ht(t);let r=[".netlify","node_modules","dist",".next","out",".nuxt",".output",".cache",".turbo",".parcel-cache","coverage",".nyc_output","storybook-static","public/build","CLAUDE.local.md"],i=[];return e.split(`
|
|
10
10
|
`).forEach(n=>{r.forEach(s=>{let a=n===`?? ${s}`,c=n.startsWith(`?? ${s}/`)||n.startsWith(`?? ${s}${Sn.sep}`);(a||c)&&i.push(`:!${s}`)});let o=n.match(ar)?.[1];o&&i.push(`:!${o}.log`)}),i},wt=async(e=process.cwd())=>{await A("git",["reset","--hard","HEAD"],{cwd:e})},An=e=>{let t=e.split(`
|
|
11
11
|
`).reduce((r,i)=>{if(!i)return r;let[n,o,,...s]=i,a=s.join(""),c=n.trim(),l=o.trim();return r[a]?r[a].change=l:r[a]={filePath:a,stage:c,change:l},r},{});return Object.values(t)},Cn=async(e,t=process.cwd())=>{let r=e.filter(i=>i.stage&&!i.change).map(i=>i.filePath);r.length!==0&&await A("git",["restore","--staged","--worktree","--pathspec-from-file=-"],{cwd:t,input:r.join(`
|
|
12
|
-
`)})};import Oe from"fs/promises";import Pn from"os";import Ke from"path";import de from"process";import Nn from"readline";import $n from"@anthropic-ai/sdk";import _t from"path";import kn from"fs/promises";var Et=x("agent-output-utils");async function Se({initialResult:e,agentName:t,hasError:r}){let i="",n=_t.join(process.cwd(),Q,pe);try{let o=await kn.readFile(n,"utf-8");o&&(i=o,Et.log(`Pulled result from ${_t.relative(process.cwd(),n)}`))}catch{Et.log(`No results file found at ${_t.relative(process.cwd(),n)}`)}return i||(!e&&!r?`${t} has finished working on task.`:e||void 0)}function ve({error:e,agentName:t}){let r=e&&typeof e=="object"?JSON.stringify(e):e,i=r?.replace(/\s+/g," ").trim().toLowerCase()||"",n="";return i?.includes("ai gateway is not available for your account")||i?.includes("ai gateway is not enabled for your account")?n="AI Gateway is currently not available on your account. Please confirm your account meets the criteria for using Agent Runners and AI Gateway and that your account has remaining AI Gateway inference credits available. Reach out to Netlify support if this is unexpected.":i?.includes("error when talking to gemini api")?n="Gemini's API is currently having issues. Please try again or use a different available agent while Google resolves the issue.":(i?.includes("connection closed prematurely")||i?.includes("499")&&t.toLowerCase().includes("gemini"))&&(n=`The ${t} models were currently overloaded. Please try again or use a different available agent.`),i?.includes("request timed out")&&(n=`The ${t} API request's have timed out. Please try again or use a different available agent.`),i?.includes("network error")&&(n=`The ${t} agent is having network issues. Please try again or use a different available agent.`),i?.includes("503")&&(n=`The ${t} API is currently experiencing high load. Retrying automatically...`),n&&Et.log(`Providing updated error messsage: ${n}, replacing original error: ${r}`),n||r||void 0}function Re(e){if(!e)return!1;let r=(e&&typeof e=="object"?JSON.stringify(e):e)?.replace(/\s+/g," ").trim().toLowerCase()||"";return r?.includes("error when talking to gemini api")||r?.includes("499")||r?.includes("connection closed prematurely")||r?.includes("request timed out")||r?.includes("network error")?!0:r?.includes("usage exceeded")?!1:!!r?.includes("503")}var q=x("runner_claude"),cr="Claude Code",On="claude-opus-4-6",Fn={create:{free:"claude-sonnet-4-6"}},Je={Task:{name:"Task",category:y.Task},Bash:{name:"Run command",category:y.RunCommand},Glob:{name:"Find files",category:y.Explore},Grep:{name:"Search files",category:y.Explore},LS:{name:"List directory",category:y.Explore},ExitPlanMode:{name:"Exit planning",category:y.Plan},Read:{name:"Read file",category:y.FileRead},Edit:{name:"Edit file",category:y.FileWrite},MultiEdit:{name:"Edit multiple files",category:y.FileWrite},Write:{name:"Edit file",category:y.FileWrite},NotebookEdit:{name:"Edit notebook",category:y.Notebook},WebFetch:{name:"Fetch web",category:y.Web},TodoWrite:{name:"Update task list",category:y.Todo},WebSearch:{name:"Search web",category:y.Web},BashOutput:{name:"Get command output",category:y.RunCommand},KillBash:{name:"Stop command",category:y.RunCommand}},ur=e=>Je[e]?.name||e,Dn=({catchError:e,runCmd:t,error:r,result:i,runnerName:n})=>(q.log(`${n} command completed with catch handler triggered`,{hadExistingError:!!r,hadExistingResult:!!i,resultLength:i?i.length:0,catchError:e?.message||"No error object",processExitCode:t.exitCode,processKilled:t.killed}),i?(q.log("Preserving existing result despite catch handler being triggered"),r?{error:r,result:i}:{error:"Process completed with errors but result was captured",result:i}):(q.log("Setting result to undefined because no valid result was captured"),{error:r||`${n} failed`,result:void 0}));async function dr({aiGateway:e,config:t,model:r}){let i=r;if(e)if(t.modelVersionOverrides?.claude){let n=t.modelVersionOverrides?.claude?.[t.accountType];if(n){if(!await e.isModelAvailableForProvider("anthropic",n))throw new Error(`Model override '${n}' is not available for anthropic provider`);i=n}}else if(r){if(!await e.isModelAvailableForProvider("anthropic",r))throw new Error(`Model '${r}' is not available for anthropic provider`)}else{let n="mode"in t?Fn[t.mode]?.[t.accountType]:void 0,o=n||On;!!o&&await e.isModelAvailableForProvider("anthropic",o)?(q.log(`Using ${n?"mode override":"default"} model: ${o}`),i=o):o&&q.log(`Model ${o} is not available, proceeding without model specification`)}return i}function pr({aiGateway:e}){if(e){let{token:t,url:r}=e;if(!t||!r)throw new Error("No token or url provided from AI Gateway");de.env.ANTHROPIC_API_KEY=t,de.env.ANTHROPIC_BASE_URL=r}else if(!de.env.ANTHROPIC_API_KEY)throw new Error("ANTHROPIC_API_KEY is not provided")}async function Ln(){let e=Ke.join(de.cwd(),"AGENTS.md");try{await Oe.access(e)}catch{return}let t=Ke.join(de.cwd(),"CLAUDE.local.md"),r="@AGENTS.md";try{if((await Oe.readFile(t,"utf-8")).includes(r))return;await Oe.appendFile(t,`
|
|
12
|
+
`)})};import Oe from"fs/promises";import Pn from"os";import Ke from"path";import de from"process";import Nn from"readline";import $n from"@anthropic-ai/sdk";import _t from"path";import kn from"fs/promises";var Et=x("agent-output-utils");async function Se({initialResult:e,agentName:t,hasError:r}){let i="",n=_t.join(process.cwd(),Q,pe);try{let o=await kn.readFile(n,"utf-8");o&&(i=o,Et.log(`Pulled result from ${_t.relative(process.cwd(),n)}`))}catch{Et.log(`No results file found at ${_t.relative(process.cwd(),n)}`)}return i||(!e&&!r?`${t} has finished working on task.`:e||void 0)}function ve({error:e,agentName:t}){let r=e&&typeof e=="object"?JSON.stringify(e):e,i=r?.replace(/\s+/g," ").trim().toLowerCase()||"",n="";return i?.includes("ai gateway is not available for your account")||i?.includes("ai gateway is not enabled for your account")?n="AI Gateway is currently not available on your account. Please confirm your account meets the criteria for using Agent Runners and AI Gateway and that your account has remaining AI Gateway inference credits available. Reach out to Netlify support if this is unexpected.":i?.includes("error when talking to gemini api")?n="Gemini's API is currently having issues. Please try again or use a different available agent while Google resolves the issue.":(i?.includes("connection closed prematurely")||i?.includes("499")&&t.toLowerCase().includes("gemini"))&&(n=`The ${t} models were currently overloaded. Please try again or use a different available agent.`),i?.includes("request timed out")&&(n=`The ${t} API request's have timed out. Please try again or use a different available agent.`),i?.includes("network error")&&(n=`The ${t} agent is having network issues. Please try again or use a different available agent.`),i?.includes("503")&&!i?.includes("usage exceeded")&&(n=`The ${t} API is currently experiencing high load. Retrying automatically...`),n&&Et.log(`Providing updated error messsage: ${n}, replacing original error: ${r}`),n||r||void 0}function Re(e){if(!e)return!1;let r=(e&&typeof e=="object"?JSON.stringify(e):e)?.replace(/\s+/g," ").trim().toLowerCase()||"";return r?.includes("error when talking to gemini api")||r?.includes("499")||r?.includes("connection closed prematurely")||r?.includes("request timed out")||r?.includes("network error")?!0:r?.includes("usage exceeded")?!1:!!r?.includes("503")}var q=x("runner_claude"),cr="Claude Code",On="claude-opus-4-6",Fn={create:{free:"claude-sonnet-4-6"}},Je={Task:{name:"Task",category:y.Task},Bash:{name:"Run command",category:y.RunCommand},Glob:{name:"Find files",category:y.Explore},Grep:{name:"Search files",category:y.Explore},LS:{name:"List directory",category:y.Explore},ExitPlanMode:{name:"Exit planning",category:y.Plan},Read:{name:"Read file",category:y.FileRead},Edit:{name:"Edit file",category:y.FileWrite},MultiEdit:{name:"Edit multiple files",category:y.FileWrite},Write:{name:"Edit file",category:y.FileWrite},NotebookEdit:{name:"Edit notebook",category:y.Notebook},WebFetch:{name:"Fetch web",category:y.Web},TodoWrite:{name:"Update task list",category:y.Todo},WebSearch:{name:"Search web",category:y.Web},BashOutput:{name:"Get command output",category:y.RunCommand},KillBash:{name:"Stop command",category:y.RunCommand}},ur=e=>Je[e]?.name||e,Dn=({catchError:e,runCmd:t,error:r,result:i,runnerName:n})=>(q.log(`${n} command completed with catch handler triggered`,{hadExistingError:!!r,hadExistingResult:!!i,resultLength:i?i.length:0,catchError:e?.message||"No error object",processExitCode:t.exitCode,processKilled:t.killed}),i?(q.log("Preserving existing result despite catch handler being triggered"),r?{error:r,result:i}:{error:"Process completed with errors but result was captured",result:i}):(q.log("Setting result to undefined because no valid result was captured"),{error:r||`${n} failed`,result:void 0}));async function dr({aiGateway:e,config:t,model:r}){let i=r;if(e)if(t.modelVersionOverrides?.claude){let n=t.modelVersionOverrides?.claude?.[t.accountType];if(n){if(!await e.isModelAvailableForProvider("anthropic",n))throw new Error(`Model override '${n}' is not available for anthropic provider`);i=n}}else if(r){if(!await e.isModelAvailableForProvider("anthropic",r))throw new Error(`Model '${r}' is not available for anthropic provider`)}else{let n="mode"in t?Fn[t.mode]?.[t.accountType]:void 0,o=n||On;!!o&&await e.isModelAvailableForProvider("anthropic",o)?(q.log(`Using ${n?"mode override":"default"} model: ${o}`),i=o):o&&q.log(`Model ${o} is not available, proceeding without model specification`)}return i}function pr({aiGateway:e}){if(e){let{token:t,url:r}=e;if(!t||!r)throw new Error("No token or url provided from AI Gateway");de.env.ANTHROPIC_API_KEY=t,de.env.ANTHROPIC_BASE_URL=r}else if(!de.env.ANTHROPIC_API_KEY)throw new Error("ANTHROPIC_API_KEY is not provided")}async function Ln(){let e=Ke.join(de.cwd(),"AGENTS.md");try{await Oe.access(e)}catch{return}let t=Ke.join(de.cwd(),"CLAUDE.local.md"),r="@AGENTS.md";try{if((await Oe.readFile(t,"utf-8")).includes(r))return;await Oe.appendFile(t,`
|
|
13
13
|
${r}
|
|
14
14
|
`)}catch{await Oe.writeFile(t,`${r}
|
|
15
15
|
`)}q.log("Added @AGENTS.md import to CLAUDE.local.md")}async function xt({config:e,netlify:t,persistSteps:r,aiGateway:i,continueSession:n,priorAgentSessionId:o,cwd:s=de.cwd()}){let a=e,{prompt:c}=a,{model:l}=e,d="";await Ln(),pr({aiGateway:i});let m=await dr({config:e,aiGateway:i,model:l}),u=[],w=[],E={},_=0,S=0,h,v,b="mode"in e&&e.mode==="create"&&e.accountType===Xt,O=["ExitPlanMode","AskUserQuestion"];b&&O.push("TodoWrite");let F=[se(s,"claude"),"--permission-mode","bypassPermissions","--dangerously-skip-permissions","--output-format","stream-json","--verbose","--disallowed-tools",O.join(","),...b?["--effort","low"]:[],...m?["--model",m]:[],...n?["--continue"]:[],...n&&o?["--resume",o]:[],"-p",c],Y=`${de.env.NVM_BIN}/node`;q.log(`Running ${Y} ${F.join(" ")}`);let D=t.utils.run(Y,F,{all:!0,env:de.env,cwd:s,idleTimeout:me});D.stdin?.end();let k=Ie(()=>{r?.({steps:u,duration:S})},250),G=(p,g)=>{let{wrapMessage:f,...I}=p,R=tr({...I,id:_});R.message&&(R.message=R.message.replace(/\n?<system-reminder>.+?<\/system-reminder>\n?/gs,"").trim(),f&&R.message&&(R.message=`\`\`\`
|
package/dist/bin.js
CHANGED
|
@@ -5,11 +5,11 @@ import $t from"process";import qo from"minimist";import{createRequire as No}from
|
|
|
5
5
|
${s}
|
|
6
6
|
</extracted_error_chunk>`).join(`
|
|
7
7
|
|
|
8
|
-
`);return i.length>e.length*.8?e:i}import{execSync as eo}from"child_process";import Nr from"fs/promises";import to from"path";import ge from"process";import{getTracer as ro}from"@netlify/otel";import be from"process";var z=class extends Error{constructor(r,o,n,i=!1){super(r);this.statusCode=o;this.userMessage=n;this.isCreditLimitExceeded=i;this.name="GracefulShutdownError"}},Ge=e=>e instanceof z;var je=be.env.NETLIFY_API_URL,Ye=be.env.NETLIFY_API_TOKEN,W=x("api"),ye=()=>be.env.NETLIFY_LOCAL_MODE==="true",ie=async(e,t={})=>{if(!je||!Ye)throw new Error("No API URL or token");let r=new URL(e,je),o={...t,headers:{...t.headers,Authorization:`Bearer ${Ye}`}};be.env.AGENT_RUNNERS_DEBUG==="true"&&(o.headers["x-nf-debug-logging"]="true"),t.json&&(o.headers||={},o.headers["Content-Type"]="application/json",o.body=JSON.stringify(t.json));let n=await fetch(r,o),i=n.ok&&n.status<=299;if(be.env.AGENT_RUNNERS_DEBUG==="true")W.log(`Response headers for ${r}:`),n.headers.forEach((a,c)=>{W.log(` ${c}: ${a}`)});else{let a=n.headers.get("x-request-id")||n.headers.get("x-nf-request-id");W.log(`Request ID for ${r}: ${a||"N/A"}`)}if(i||W.error(`Got status ${n.status} for request ${r}`),t.raw){if(!i)throw new Error(`API request failed: ${n.status} ${n.statusText}`);return n}let s=await(n.headers.get("content-type")?.includes("application/json")?n.json():n.text());if(!i){let a=typeof s=="string"?s:JSON.stringify(s);throw n.status===404?new z(`API request failed: 404 - ${a}`,404,"The site associated with this agent run no longer exists."):n.status===503&&t.gracefulOn503&&a.toLowerCase().includes("usage exceeded")?new z(`API request failed: 503 - ${a}`,503,"Credit limit reached. Please add more credits to continue using Agent Runners.",!0):new Error(`API request failed: ${n.status} - ${a}`)}return s},Gt=e=>{W.log("Setting details for api",{apiUrl:e?.constants?.NETLIFY_API_HOST,token:!!e?.constants?.NETLIFY_API_TOKEN}),e?.constants?.NETLIFY_API_HOST&&(je=`https://${e.constants.NETLIFY_API_HOST}`),e?.constants?.NETLIFY_API_TOKEN&&(Ye=e.constants.NETLIFY_API_TOKEN)},jt=()=>({apiUrl:je,token:Ye}),Pe=async(e,t)=>ye()?(W.log("Mock API: updateRunner called",{runnerId:e,data:t}),{id:e,...t}):ie(`/api/v1/agent_runners/${e}`,{method:"PUT",json:t}),K=async(e,t,r)=>ye()?(W.log("Mock API: updateRunnerSession called",JSON.stringify({runnerId:e,sessionId:t,data:r},null,2)),{id:e,sessionId:t,...r}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`,{method:"PUT",json:r});var Yt=async e=>ye()?(W.log("Mock API: getSite called",{siteId:e}),{id:e,published_deploy:{id:"id"}}):ie(`/api/v1/sites/${e}`),qt=async(e,t)=>ye()?(W.log("Mock API: getRunnerSession called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,state:"running"}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`),Bt=(e,t,r)=>ie(`/api/v1/accounts/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Ht=(e,t,r)=>ie(`/api/v1/sites/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Wt=async(e,t)=>ye()?(W.log("Mock API: getDiffUploadUrls called",{runnerId:e,sessionId:t}),{result:{upload_url:"https://s3.mock.com/mock-upload-url-result",s3_key:"mock-s3-key-result"},cumulative:{upload_url:"https://s3.mock.com/mock-upload-url-cumulative",s3_key:"mock-s3-key-cumulative"}}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/diff/upload_urls`,{method:"POST"}),Kt=async(e,t)=>ye()?(W.log("Mock API: updateSessionUsage called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,usage:0}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/update_usage`,{method:"POST"}),st=async(e,t)=>{W.log(`Uploading diff to S3: ${e.substring(0,50)}...`);let r=await fetch(e,{method:"PUT",body:t,headers:{"Content-Type":"text/plain"}});if(!r.ok)throw new Error(`S3 upload failed with status ${r.status}`);return r};var _e=x("ai_gateway"),at=null;var Jt=async()=>{if(at)return at;_e.log("Fetching available AI gateway providers");let e=await fetch(`${jt().apiUrl}/api/v1/ai-gateway/providers`);if(!e.ok)throw new Error(`Failed to fetch AI gateway providers: ${e.statusText}`);let t=await e.json();return at=t,_e.log("Cached AI gateway providers",{providerCount:Object.keys(t.providers).length}),t},ln=async(e,t)=>{let o=(await Jt()).providers[e];if(!o)return _e.log(`Provider '${e}' not found`),!1;let n=o.models.includes(t);return _e.log(`Model validation for ${e}/${t}`,{isAvailable:n}),n},Vt=async({config:e})=>{let t,r,o,n,i=!e.site?.published_deploy;if(!(i?e.accountId:e.siteId))throw new Error(`No entity id for ${i?"account":"site"}`);let a=async()=>{clearTimeout(o),_e.log("Requesting AI gateway information");let l=await(i?Bt(e.accountId,e.id,e.sessionId):Ht(e.siteId,e.id,e.sessionId));if({token:t,url:n}=l,r=l.expires_at?l.expires_at*1e3:void 0,_e.log("Got AI gateway information",{token:!!t,expiresAt:r,url:n}),r){let d=r-Date.now()-6e4;d>0&&(o=setTimeout(()=>{a()},d))}};return await Promise.all([a(),Jt()]),{get url(){return n},get token(){return t},isModelAvailableForProvider:ln}};import Z from"process";import ne from"path";import qe from"fs";import{fileURLToPath as fn}from"url";import{createRequire as hn}from"module";import{execa as yn,execaCommand as xi}from"execa";import{Transform as cn}from"stream";function un(){let e=process.env.NETLIFY_SENSITIVE_ENV_KEYS;return e?e.split(",").map(t=>t.trim()).filter(Boolean):[]}function dn(e){let t=e.toLowerCase();return t==="true"||t==="false"?!0:e.trim().length<4}function pn(){let t=un().map(r=>process.env[r]).filter(r=>!(!r||dn(r)));return[...new Set(t)].sort((r,o)=>o.length-r.length)}function re(e){if(typeof e!="string")return e;let t=pn();if(t.length===0)return e;let r=e;return t.forEach(o=>{let n=new RegExp(mn(o),"g");r=r.replace(n,"******")}),r}function mn(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var Ee=class extends cn{constructor(t={}){super({...t,objectMode:!1})}_transform(t,r,o){let n=t.toString(),i=re(n);o(null,i)}};function zt(){if(!(process.env.NETLIFY_MASK_LOGS!=="false"))return;let t=process.stdout.write.bind(process.stdout),r=process.stderr.write.bind(process.stderr);process.stdout.write=function(o,n,i){let s=typeof o=="string"?re(o):o;return typeof n=="function"?t(s,n):t(s,n,i)},process.stderr.write=function(o,n,i){let s=typeof o=="string"?re(o):o;return typeof n=="function"?r(s,n):r(s,n,i)}}var ke=null,Xt=e=>(ke&&ke.destroy(),ke=new se({totalAllowedTime:e}),ke),Zt=()=>ke;var se=class{constructor({totalAllowedTime:t}){this.withStageTimer=async(t,r,o)=>{if(this.isTimeExpired())throw new Error(`${t} stage did not complete in the allowed time. Time has already expired.`);let n=this.onTimesUp(()=>{throw new Error(`${t} stage did not complete in the allowed time.`)}),i=null,s=null;o!==void 0&&(s=new Promise((a,c)=>{i=setTimeout(()=>{c(new Error(`${t} stage exceeded its maximum duration of ${o}ms`))},o)}));try{return s?await Promise.race([r(),s]):await r()}finally{n(),i&&clearTimeout(i)}};this.startTime=Date.now(),this.totalAllowedTime=t,this.globalTimeoutId=null,this.subscribers=[],this.hasTimedOut=!1,this.setupGlobalTimeout()}getElapsedTime(){return Date.now()-this.startTime}getRemainingTime(){let t=this.getElapsedTime(),r=this.totalAllowedTime-t;return Math.max(0,r)}isTimeExpired(){return this.getRemainingTime()===0||this.hasTimedOut}setupGlobalTimeout(){this.globalTimeoutId&&clearTimeout(this.globalTimeoutId),this.globalTimeoutId=setTimeout(()=>{this.notifyTimeUp()},this.totalAllowedTime)}notifyTimeUp(){this.hasTimedOut=!0;for(let t=this.subscribers.length-1;t>=0;t--)try{this.subscribers[t]()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}}onTimesUp(t){if(this.subscribers.push(t),this.hasTimedOut)try{t()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}return()=>{let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}}off(t){let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}clearSubscribers(){this.subscribers.length=0}getSubscriberCount(){return this.subscribers.length}destroy(){this.globalTimeoutId&&(clearTimeout(this.globalTimeoutId),this.globalTimeoutId=null),this.clearSubscribers()}static{this.timeUnits={seconds:t=>t*1e3,minutes:t=>t*60*1e3,hours:t=>t*60*60*1e3}}};var Qt="netlify-agent-runner-context.md",lt="task-history",X=".netlify",ue="results.md",ct="assets",ut="other",dt="personal";var pt="enterprise",Ne="free",er=[dt,"pro",pt,Ne],tr=["normal","redeploy","create","ask","dtn-prod-iteration","rebase"],rr="The production deploy has changed since you started working. Please reapply the changes to the current codebase, resolving any conflicts that arise. Use the attached diff file if present, otherwise review the previous session context to reproduce the changes.",de=1800*1e3,_={Environment:"environment",UserMessage:"user-message",AgentMessage:"agent-message",Task:"task",RunCommand:"run-command",Explore:"explore",Plan:"plan",FileRead:"file-read",FileWrite:"file-write",Notebook:"notebook",Web:"web",Todo:"todo",Reasoning:"reasoning",Skill:"skill",Memorize:"memorize",Deployment:"deployment",SiteGeneration:"site-generation"};var nr={name:"@netlify/agent-runner-cli",type:"module",version:"1.90.0",description:"CLI tool for running Netlify agents",main:"./dist/index.js",types:"./dist/index.d.ts",exports:"./dist/index.js",bin:{"agent-runner-cli":"./dist/bin.js","agent-runner-cli-local":"./dist/bin-local.js"},files:["dist/**/*.js","dist/**/*.d.ts","dist/skills/**","patches","scripts"],scripts:{build:"tsup",dev:"tsup --watch",prepare:"husky install node_modules/@netlify/eslint-config-node/.husky/",prepublishOnly:"npm ci && npm test",prepack:"npm run build",test:"run-s build format test:dev",format:"run-s build format:check-fix:*","format:ci":"run-s build format:check:*","format:check-fix:lint":"run-e format:check:lint format:fix:lint","format:check:lint":"cross-env-shell eslint $npm_package_config_eslint","format:fix:lint":"cross-env-shell eslint --fix $npm_package_config_eslint","format:check-fix:prettier":"run-e format:check:prettier format:fix:prettier","format:check:prettier":"cross-env-shell prettier --check $npm_package_config_prettier","format:fix:prettier":"cross-env-shell prettier --write $npm_package_config_prettier","test:dev":"run-s build test:dev:*","test:ci":"run-s build test:ci:*","test:dev:vitest":"LOG=0 vitest --exclude '**/integration/**'","test:ci:vitest":"LOG=0 c8 -r lcovonly -r text -r json vitest --exclude '**/integration/**'","test:integration":"vitest run test/integration/","test:integration:codex":"vitest run test/integration/codex.test.ts","test:integration:claude":"vitest run test/integration/claude.test.ts","test:integration:gemini":"vitest run test/integration/gemini.test.ts","test:integration:create-stage":"vitest run test/integration/create.test.ts","test:integration:skill-invocation":"vitest run test/integration/skill-invocation.test.ts","check:types":"tsc --noEmit",postinstall:"node scripts/postinstall.js"},config:{eslint:'--cache --format=codeframe --max-warnings=0 "{src,scripts,test,.github}/**/*.{js,ts,md,html}"',prettier:'--ignore-path .gitignore --loglevel=warn "{src,scripts,test,.github}/**/*.{js,ts,md,yml,json,html}" "*.{js,ts,yml,json,html}" ".*.{js,ts,yml,json,html}" "!**/package-lock.json" "!package-lock.json" "!src/skills/**/*.md"'},keywords:[],license:"MIT",repository:"netlify/agent-runner-cli",bugs:{url:"https://github.com/netlify/agent-runner-cli/issues"},author:"Netlify Inc.",directories:{test:"test"},devDependencies:{"@commitlint/cli":"^20.0.0","@commitlint/config-conventional":"^20.0.0","@eslint/compat":"^2.0.0","@eslint/js":"^9.35.0","@netlify/eslint-config-node":"^7.0.1","@types/node":"^24.5.0","@typescript-eslint/eslint-plugin":"^8.0.0","@typescript-eslint/parser":"^8.0.0","@vitest/eslint-plugin":"^1.6.6",c8:"^10.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-n":"^17.0.0",husky:"^9.0.0","patch-package":"^8.0.0",tsup:"^8.5.0",typescript:"^5.0.0","typescript-eslint":"^8.44.0",vitest:"^4.0.16"},dependencies:{"@anthropic-ai/claude-code":"2.1.63","@anthropic-ai/sdk":"0.78.0","@google/gemini-cli":"0.31.0","@netlify/otel":"^5.1.2","@openai/codex":"0.110.0","@opentelemetry/exporter-trace-otlp-grpc":"0.57.2",execa:"^9.6.1",kaddidlehopper:"^0.7.2",minimist:"^1.2.8",openai:"6.26.0"}};var _n=fn(import.meta.url),En=ne.dirname(_n),wn=hn(import.meta.url),we=x("shell"),mt=new Set,xn={preferLocal:!0},k=(e,t,r)=>{let[o,n]=Tn(t,r),i={...xn,...n},s=yn(e,o,i);In(s,i),Sn(s);let a=r?.idleTimeout;return a&&a>0&&vn(s,a),s};var Tn=function(e,t){return Array.isArray(e)?[e,t]:typeof e=="object"&&e!==null?[[],e]:[[],void 0]},In=(e,t)=>{if(t.stdio!==void 0||t.stdout!==void 0||t.stderr!==void 0)return;if(Z.env.NETLIFY_MASK_LOGS!=="false"){e.all?.pipe(new Ee).pipe(Z.stdout),e.stdout?.pipe(new Ee).pipe(Z.stdout),e.stderr?.pipe(new Ee).pipe(Z.stderr);return}e.stdout?.pipe(Z.stdout),e.stderr?.pipe(Z.stderr)},gt=(e,t="SIGTERM")=>{try{return e.pid&&!e.killed?(Z.kill(-e.pid,t),we.log(`Killed process ${e.pid} with signal ${t}`),!0):!1}catch(r){return we.error("Error killing process:",r),!1}},or=e=>gt(e,"SIGKILL"),vn=(e,t)=>{let r=null,o=()=>{we.log(`Process ${e.pid} killed due to idle timeout (no output for ${t}ms)`),gt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(we.log(`Force killing idle process ${e.pid}`),or(e))},5e3)},n=()=>{r&&clearTimeout(r),r=setTimeout(o,t)};n(),e.stdout?.on("data",n),e.stderr?.on("data",n);let i=()=>{r&&(clearTimeout(r),r=null)};e.on("exit",i),e.on("error",i)},Sn=e=>{mt.add(e);let t=Zt();if(t){let r=t.onTimesUp(()=>{we.log(`Global timer expired, killing process ${e.pid}`),gt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(we.log(`Force killing process ${e.pid} after timeout`),or(e))},5e3)});e.on("exit",()=>{mt.delete(e),r()}),e.on("error",()=>{mt.delete(e),r()})}};function ae(e,t){if(!Z.env.NETLIFY_LOCAL_MODE)try{let n=wn.resolve(nr.name),i=ne.dirname(n);for(;i!==ne.dirname(i);){let s=ne.dirname(i);if(ne.basename(s)==="node_modules"){let a=ne.join(s,".bin",t);if(qe.existsSync(a))return a;break}i=s}}catch(n){console.error("Could not resolve package.json",n)}if(Z.env.NODE_PATH){let n=ne.join(Z.env.NODE_PATH,".bin",t);if(qe.existsSync(n))return n}let r=ne.join(e,"node_modules",".bin",t);if(qe.existsSync(r))return r;let o=ne.join(En,"..","node_modules",".bin",t);if(qe.existsSync(o))return o}var ir=x("utils"),Rn=e=>new Promise(t=>{setTimeout(t,e)}),Be=(e,t=3e3)=>{let r=!1,o=null,n=[],i=null,s=(...a)=>{if(r)return o=a,new Promise(d=>{n.push(d)});r=!0;let c,l=new Promise(d=>{c=d});return i=(async()=>{await Promise.resolve();let d=await e(...a);for(c(d);;){if(await Rn(t),!o)return r=!1,i=null,d;let p=o,u=n;o=null,n=[],d=await e(...p),u.forEach(y=>{y(d)})}})(),l};return s.flush=async()=>{if((r||o)&&i)return await i,s.flush()},s},xe=(e,t,r=!1)=>{let o=null,n=null,i=null,s=function(...a){n=a,i=this;let c=r&&!o;clearTimeout(o),o=setTimeout(()=>{o=null,r||(e.apply(i,n),n=null,i=null)},t),c&&(e.apply(i,n),n=null,i=null)};return s.cancel=()=>{clearTimeout(o),o=null,n=null,i=null},s.flush=()=>{if(o){clearTimeout(o);let a=n,c=i;o=null,n=null,i=null,e.apply(c,a)}},s},He=(e,t=!0,r)=>{if(e)try{return JSON.parse(e)}catch(o){t&&(r?.error?r.error("Could not parse JSON",o):ir.error("Could not parse JSON",o))}},ft=e=>e.charAt(0).toUpperCase()+e.slice(1),le=e=>e.split("-").map(t=>t.length===2?t.toUpperCase():ft(t)).join(" ");function pe(e,t){t&&e.log(`Skill invoked: ${t}`)}var sr=e=>Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0)),ar=(e,t)=>{let n=".netlify.app",i="agent-";if(!t)return`${i}${e.slice(0,6)}`;let a=`--${t}${n}`;if(a.length>55)return"";let c=60-a.length;if(c<=0)return"";if(c>=i.length+6){let l=Math.min(c-i.length,e.length);return`${i}${e.slice(0,l)}`}return e.slice(0,c)},An=e=>!e||typeof e!="object"||Array.isArray(e)||Object.keys(e).length===0?!1:!!er.some(t=>t in e),lr=()=>{let e={},t={codex:process.env.NETLIFY_FF_AGENT_RUNNER_CODEX_VERSION,claude:process.env.NETLIFY_FF_AGENT_RUNNER_CLAUDE_VERSION,gemini:process.env.NETLIFY_FF_AGENT_RUNNER_GEMINI_VERSION};return Object.entries(t).forEach(([r,o])=>{if(o){let n=`NETLIFY_FF_AGENT_RUNNER_${r.toUpperCase()}_VERSION`;try{let i=JSON.parse(o);An(i)&&(e[r]=i)}catch(i){let a=i instanceof SyntaxError?"Invalid JSON":i.message;ir.error(`Could not parse ${r} model version override from ${n}: ${a}`)}}}),e},Cn=50*1024,ht=(e,t=Cn)=>{if(!e||typeof e!="string"||e.length<=t)return e;let o=e.startsWith("```")?"\n... [truncated]\n```":"... [truncated]";return e.slice(0,t)+o};import{Buffer as cr}from"buffer";import bn from"path";var ur=x("repo"),pr=async({config:e,isRetry:t,cwd:r=process.cwd()})=>{ur.info("Getting runner diffs");let o=await kn(r),{hasChanges:n}=o,{status:i}=o;if(!n)return{hasChanges:!1};if(!t){let E=On(i);await $n(E,r)}ur.info("Changes after processing"),await _t(r);let s=await wt(i,r);if(await yt(s,r),n=await Nn(r),!n)return{hasChanges:!1,ignored:s};process.env.NETLIFY_INTERNAL_GIT="1";try{await k("git",["commit","-m","Agent runner"],{cwd:r})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}let a={stdio:["ignore","pipe","pipe"],cwd:r},c=await k("git",["diff",e.runSha,"HEAD"],a),l=String(c.stdout??"");if(n=!!l,!n)return await dr(r),{hasChanges:!1,ignored:s};let d=await k("git",["diff",e.runSha,"HEAD","--binary"],a),p=String(d.stdout??""),u,y;if(e.sha){let E=await k("git",["diff",e.sha,"HEAD"],a);u=String(E.stdout??"");let I=await k("git",["diff",e.sha,"HEAD","--binary"],a),f=String(I.stdout??"");u!==f&&(y=cr.from(f).toString("base64"))}await dr(r);let w={hasChanges:!0,diff:l,resultDiff:u,ignored:s};return l!==p&&(w.diffBinary=cr.from(p).toString("base64")),y&&(w.resultDiffBinary=y),w},dr=async(e=process.cwd())=>{process.env.NETLIFY_LOCAL_MODE&&await k("git",["reset","--soft","HEAD~1"],{cwd:e})},yt=async(e=[],t=process.cwd())=>{process.env.NETLIFY_INTERNAL_GIT="1";try{await k("git",["add",".",...e],{cwd:t})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}},_t=async(e=process.cwd())=>{let t=await k("git",["status","-s"],{cwd:e});return String(t.stdout??"")},mr=/.. (.+)?\.log$/,Pn=[mr],kn=async(e=process.cwd())=>{let t=await _t(e);return{hasChanges:(t.trim().length===0?[]:t.split(`
|
|
8
|
+
`);return i.length>e.length*.8?e:i}import{execSync as eo}from"child_process";import Nr from"fs/promises";import to from"path";import ge from"process";import{getTracer as ro}from"@netlify/otel";import be from"process";var z=class extends Error{constructor(r,o,n,i=!1){super(r);this.statusCode=o;this.userMessage=n;this.isCreditLimitExceeded=i;this.name="GracefulShutdownError"}},Ge=e=>e instanceof z;var je=be.env.NETLIFY_API_URL,Ye=be.env.NETLIFY_API_TOKEN,W=x("api"),ye=()=>be.env.NETLIFY_LOCAL_MODE==="true",ie=async(e,t={})=>{if(!je||!Ye)throw new Error("No API URL or token");let r=new URL(e,je),o={...t,headers:{...t.headers,Authorization:`Bearer ${Ye}`}};be.env.AGENT_RUNNERS_DEBUG==="true"&&(o.headers["x-nf-debug-logging"]="true"),t.json&&(o.headers||={},o.headers["Content-Type"]="application/json",o.body=JSON.stringify(t.json));let n=await fetch(r,o),i=n.ok&&n.status<=299;if(be.env.AGENT_RUNNERS_DEBUG==="true")W.log(`Response headers for ${r}:`),n.headers.forEach((a,c)=>{W.log(` ${c}: ${a}`)});else{let a=n.headers.get("x-request-id")||n.headers.get("x-nf-request-id");W.log(`Request ID for ${r}: ${a||"N/A"}`)}if(i||W.error(`Got status ${n.status} for request ${r}`),t.raw){if(!i)throw new Error(`API request failed: ${n.status} ${n.statusText}`);return n}let s=await(n.headers.get("content-type")?.includes("application/json")?n.json():n.text());if(!i){let a=typeof s=="string"?s:JSON.stringify(s);throw n.status===404?new z(`API request failed: 404 - ${a}`,404,"The site associated with this agent run no longer exists."):n.status===503&&t.gracefulOn503&&a.toLowerCase().includes("usage exceeded")?new z(`API request failed: 503 - ${a}`,503,"Credit limit reached. Please add more credits to continue using Agent Runners.",!0):new Error(`API request failed: ${n.status} - ${a}`)}return s},Gt=e=>{W.log("Setting details for api",{apiUrl:e?.constants?.NETLIFY_API_HOST,token:!!e?.constants?.NETLIFY_API_TOKEN}),e?.constants?.NETLIFY_API_HOST&&(je=`https://${e.constants.NETLIFY_API_HOST}`),e?.constants?.NETLIFY_API_TOKEN&&(Ye=e.constants.NETLIFY_API_TOKEN)},jt=()=>({apiUrl:je,token:Ye}),Pe=async(e,t)=>ye()?(W.log("Mock API: updateRunner called",{runnerId:e,data:t}),{id:e,...t}):ie(`/api/v1/agent_runners/${e}`,{method:"PUT",json:t}),K=async(e,t,r)=>ye()?(W.log("Mock API: updateRunnerSession called",JSON.stringify({runnerId:e,sessionId:t,data:r},null,2)),{id:e,sessionId:t,...r}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`,{method:"PUT",json:r});var Yt=async e=>ye()?(W.log("Mock API: getSite called",{siteId:e}),{id:e,published_deploy:{id:"id"}}):ie(`/api/v1/sites/${e}`),qt=async(e,t)=>ye()?(W.log("Mock API: getRunnerSession called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,state:"running"}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`),Bt=(e,t,r)=>ie(`/api/v1/accounts/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Ht=(e,t,r)=>ie(`/api/v1/sites/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Wt=async(e,t)=>ye()?(W.log("Mock API: getDiffUploadUrls called",{runnerId:e,sessionId:t}),{result:{upload_url:"https://s3.mock.com/mock-upload-url-result",s3_key:"mock-s3-key-result"},cumulative:{upload_url:"https://s3.mock.com/mock-upload-url-cumulative",s3_key:"mock-s3-key-cumulative"}}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/diff/upload_urls`,{method:"POST"}),Kt=async(e,t)=>ye()?(W.log("Mock API: updateSessionUsage called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,usage:0}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/update_usage`,{method:"POST"}),st=async(e,t)=>{W.log(`Uploading diff to S3: ${e.substring(0,50)}...`);let r=await fetch(e,{method:"PUT",body:t,headers:{"Content-Type":"text/plain"}});if(!r.ok)throw new Error(`S3 upload failed with status ${r.status}`);return r};var _e=x("ai_gateway"),at=null;var Jt=async()=>{if(at)return at;_e.log("Fetching available AI gateway providers");let e=await fetch(`${jt().apiUrl}/api/v1/ai-gateway/providers`);if(!e.ok)throw new Error(`Failed to fetch AI gateway providers: ${e.statusText}`);let t=await e.json();return at=t,_e.log("Cached AI gateway providers",{providerCount:Object.keys(t.providers).length}),t},ln=async(e,t)=>{let o=(await Jt()).providers[e];if(!o)return _e.log(`Provider '${e}' not found`),!1;let n=o.models.includes(t);return _e.log(`Model validation for ${e}/${t}`,{isAvailable:n}),n},Vt=async({config:e})=>{let t,r,o,n,i=!e.site?.published_deploy;if(!(i?e.accountId:e.siteId))throw new Error(`No entity id for ${i?"account":"site"}`);let a=async()=>{clearTimeout(o),_e.log("Requesting AI gateway information");let l=await(i?Bt(e.accountId,e.id,e.sessionId):Ht(e.siteId,e.id,e.sessionId));if({token:t,url:n}=l,r=l.expires_at?l.expires_at*1e3:void 0,_e.log("Got AI gateway information",{token:!!t,expiresAt:r,url:n}),r){let d=r-Date.now()-6e4;d>0&&(o=setTimeout(()=>{a()},d))}};return await Promise.all([a(),Jt()]),{get url(){return n},get token(){return t},isModelAvailableForProvider:ln}};import Z from"process";import ne from"path";import qe from"fs";import{fileURLToPath as fn}from"url";import{createRequire as hn}from"module";import{execa as yn,execaCommand as xi}from"execa";import{Transform as cn}from"stream";function un(){let e=process.env.NETLIFY_SENSITIVE_ENV_KEYS;return e?e.split(",").map(t=>t.trim()).filter(Boolean):[]}function dn(e){let t=e.toLowerCase();return t==="true"||t==="false"?!0:e.trim().length<4}function pn(){let t=un().map(r=>process.env[r]).filter(r=>!(!r||dn(r)));return[...new Set(t)].sort((r,o)=>o.length-r.length)}function re(e){if(typeof e!="string")return e;let t=pn();if(t.length===0)return e;let r=e;return t.forEach(o=>{let n=new RegExp(mn(o),"g");r=r.replace(n,"******")}),r}function mn(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var Ee=class extends cn{constructor(t={}){super({...t,objectMode:!1})}_transform(t,r,o){let n=t.toString(),i=re(n);o(null,i)}};function zt(){if(!(process.env.NETLIFY_MASK_LOGS!=="false"))return;let t=process.stdout.write.bind(process.stdout),r=process.stderr.write.bind(process.stderr);process.stdout.write=function(o,n,i){let s=typeof o=="string"?re(o):o;return typeof n=="function"?t(s,n):t(s,n,i)},process.stderr.write=function(o,n,i){let s=typeof o=="string"?re(o):o;return typeof n=="function"?r(s,n):r(s,n,i)}}var ke=null,Xt=e=>(ke&&ke.destroy(),ke=new se({totalAllowedTime:e}),ke),Zt=()=>ke;var se=class{constructor({totalAllowedTime:t}){this.withStageTimer=async(t,r,o)=>{if(this.isTimeExpired())throw new Error(`${t} stage did not complete in the allowed time. Time has already expired.`);let n=this.onTimesUp(()=>{throw new Error(`${t} stage did not complete in the allowed time.`)}),i=null,s=null;o!==void 0&&(s=new Promise((a,c)=>{i=setTimeout(()=>{c(new Error(`${t} stage exceeded its maximum duration of ${o}ms`))},o)}));try{return s?await Promise.race([r(),s]):await r()}finally{n(),i&&clearTimeout(i)}};this.startTime=Date.now(),this.totalAllowedTime=t,this.globalTimeoutId=null,this.subscribers=[],this.hasTimedOut=!1,this.setupGlobalTimeout()}getElapsedTime(){return Date.now()-this.startTime}getRemainingTime(){let t=this.getElapsedTime(),r=this.totalAllowedTime-t;return Math.max(0,r)}isTimeExpired(){return this.getRemainingTime()===0||this.hasTimedOut}setupGlobalTimeout(){this.globalTimeoutId&&clearTimeout(this.globalTimeoutId),this.globalTimeoutId=setTimeout(()=>{this.notifyTimeUp()},this.totalAllowedTime)}notifyTimeUp(){this.hasTimedOut=!0;for(let t=this.subscribers.length-1;t>=0;t--)try{this.subscribers[t]()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}}onTimesUp(t){if(this.subscribers.push(t),this.hasTimedOut)try{t()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}return()=>{let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}}off(t){let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}clearSubscribers(){this.subscribers.length=0}getSubscriberCount(){return this.subscribers.length}destroy(){this.globalTimeoutId&&(clearTimeout(this.globalTimeoutId),this.globalTimeoutId=null),this.clearSubscribers()}static{this.timeUnits={seconds:t=>t*1e3,minutes:t=>t*60*1e3,hours:t=>t*60*60*1e3}}};var Qt="netlify-agent-runner-context.md",lt="task-history",X=".netlify",ue="results.md",ct="assets",ut="other",dt="personal";var pt="enterprise",Ne="free",er=[dt,"pro",pt,Ne],tr=["normal","redeploy","create","ask","dtn-prod-iteration","rebase"],rr="The production deploy has changed since you started working. Please reapply the changes to the current codebase, resolving any conflicts that arise. Use the attached diff file if present, otherwise review the previous session context to reproduce the changes.",de=1800*1e3,_={Environment:"environment",UserMessage:"user-message",AgentMessage:"agent-message",Task:"task",RunCommand:"run-command",Explore:"explore",Plan:"plan",FileRead:"file-read",FileWrite:"file-write",Notebook:"notebook",Web:"web",Todo:"todo",Reasoning:"reasoning",Skill:"skill",Memorize:"memorize",Deployment:"deployment",SiteGeneration:"site-generation"};var nr={name:"@netlify/agent-runner-cli",type:"module",version:"1.90.1",description:"CLI tool for running Netlify agents",main:"./dist/index.js",types:"./dist/index.d.ts",exports:"./dist/index.js",bin:{"agent-runner-cli":"./dist/bin.js","agent-runner-cli-local":"./dist/bin-local.js"},files:["dist/**/*.js","dist/**/*.d.ts","dist/skills/**","patches","scripts"],scripts:{build:"tsup",dev:"tsup --watch",prepare:"husky install node_modules/@netlify/eslint-config-node/.husky/",prepublishOnly:"npm ci && npm test",prepack:"npm run build",test:"run-s build format test:dev",format:"run-s build format:check-fix:*","format:ci":"run-s build format:check:*","format:check-fix:lint":"run-e format:check:lint format:fix:lint","format:check:lint":"cross-env-shell eslint $npm_package_config_eslint","format:fix:lint":"cross-env-shell eslint --fix $npm_package_config_eslint","format:check-fix:prettier":"run-e format:check:prettier format:fix:prettier","format:check:prettier":"cross-env-shell prettier --check $npm_package_config_prettier","format:fix:prettier":"cross-env-shell prettier --write $npm_package_config_prettier","test:dev":"run-s build test:dev:*","test:ci":"run-s build test:ci:*","test:dev:vitest":"LOG=0 vitest --exclude '**/integration/**'","test:ci:vitest":"LOG=0 c8 -r lcovonly -r text -r json vitest --exclude '**/integration/**'","test:integration":"vitest run test/integration/","test:integration:codex":"vitest run test/integration/codex.test.ts","test:integration:claude":"vitest run test/integration/claude.test.ts","test:integration:gemini":"vitest run test/integration/gemini.test.ts","test:integration:create-stage":"vitest run test/integration/create.test.ts","test:integration:skill-invocation":"vitest run test/integration/skill-invocation.test.ts","check:types":"tsc --noEmit",postinstall:"node scripts/postinstall.js"},config:{eslint:'--cache --format=codeframe --max-warnings=0 "{src,scripts,test,.github}/**/*.{js,ts,md,html}"',prettier:'--ignore-path .gitignore --loglevel=warn "{src,scripts,test,.github}/**/*.{js,ts,md,yml,json,html}" "*.{js,ts,yml,json,html}" ".*.{js,ts,yml,json,html}" "!**/package-lock.json" "!package-lock.json" "!src/skills/**/*.md"'},keywords:[],license:"MIT",repository:"netlify/agent-runner-cli",bugs:{url:"https://github.com/netlify/agent-runner-cli/issues"},author:"Netlify Inc.",directories:{test:"test"},devDependencies:{"@commitlint/cli":"^20.0.0","@commitlint/config-conventional":"^20.0.0","@eslint/compat":"^2.0.0","@eslint/js":"^9.35.0","@netlify/eslint-config-node":"^7.0.1","@types/node":"^24.5.0","@typescript-eslint/eslint-plugin":"^8.0.0","@typescript-eslint/parser":"^8.0.0","@vitest/eslint-plugin":"^1.6.6",c8:"^10.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-n":"^17.0.0",husky:"^9.0.0","patch-package":"^8.0.0",tsup:"^8.5.0",typescript:"^5.0.0","typescript-eslint":"^8.44.0",vitest:"^4.0.16"},dependencies:{"@anthropic-ai/claude-code":"2.1.63","@anthropic-ai/sdk":"0.78.0","@google/gemini-cli":"0.31.0","@netlify/otel":"^5.1.2","@openai/codex":"0.110.0","@opentelemetry/exporter-trace-otlp-grpc":"0.57.2",execa:"^9.6.1",kaddidlehopper:"^0.7.2",minimist:"^1.2.8",openai:"6.26.0"}};var _n=fn(import.meta.url),En=ne.dirname(_n),wn=hn(import.meta.url),we=x("shell"),mt=new Set,xn={preferLocal:!0},k=(e,t,r)=>{let[o,n]=Tn(t,r),i={...xn,...n},s=yn(e,o,i);In(s,i),Sn(s);let a=r?.idleTimeout;return a&&a>0&&vn(s,a),s};var Tn=function(e,t){return Array.isArray(e)?[e,t]:typeof e=="object"&&e!==null?[[],e]:[[],void 0]},In=(e,t)=>{if(t.stdio!==void 0||t.stdout!==void 0||t.stderr!==void 0)return;if(Z.env.NETLIFY_MASK_LOGS!=="false"){e.all?.pipe(new Ee).pipe(Z.stdout),e.stdout?.pipe(new Ee).pipe(Z.stdout),e.stderr?.pipe(new Ee).pipe(Z.stderr);return}e.stdout?.pipe(Z.stdout),e.stderr?.pipe(Z.stderr)},gt=(e,t="SIGTERM")=>{try{return e.pid&&!e.killed?(Z.kill(-e.pid,t),we.log(`Killed process ${e.pid} with signal ${t}`),!0):!1}catch(r){return we.error("Error killing process:",r),!1}},or=e=>gt(e,"SIGKILL"),vn=(e,t)=>{let r=null,o=()=>{we.log(`Process ${e.pid} killed due to idle timeout (no output for ${t}ms)`),gt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(we.log(`Force killing idle process ${e.pid}`),or(e))},5e3)},n=()=>{r&&clearTimeout(r),r=setTimeout(o,t)};n(),e.stdout?.on("data",n),e.stderr?.on("data",n);let i=()=>{r&&(clearTimeout(r),r=null)};e.on("exit",i),e.on("error",i)},Sn=e=>{mt.add(e);let t=Zt();if(t){let r=t.onTimesUp(()=>{we.log(`Global timer expired, killing process ${e.pid}`),gt(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(we.log(`Force killing process ${e.pid} after timeout`),or(e))},5e3)});e.on("exit",()=>{mt.delete(e),r()}),e.on("error",()=>{mt.delete(e),r()})}};function ae(e,t){if(!Z.env.NETLIFY_LOCAL_MODE)try{let n=wn.resolve(nr.name),i=ne.dirname(n);for(;i!==ne.dirname(i);){let s=ne.dirname(i);if(ne.basename(s)==="node_modules"){let a=ne.join(s,".bin",t);if(qe.existsSync(a))return a;break}i=s}}catch(n){console.error("Could not resolve package.json",n)}if(Z.env.NODE_PATH){let n=ne.join(Z.env.NODE_PATH,".bin",t);if(qe.existsSync(n))return n}let r=ne.join(e,"node_modules",".bin",t);if(qe.existsSync(r))return r;let o=ne.join(En,"..","node_modules",".bin",t);if(qe.existsSync(o))return o}var ir=x("utils"),Rn=e=>new Promise(t=>{setTimeout(t,e)}),Be=(e,t=3e3)=>{let r=!1,o=null,n=[],i=null,s=(...a)=>{if(r)return o=a,new Promise(d=>{n.push(d)});r=!0;let c,l=new Promise(d=>{c=d});return i=(async()=>{await Promise.resolve();let d=await e(...a);for(c(d);;){if(await Rn(t),!o)return r=!1,i=null,d;let p=o,u=n;o=null,n=[],d=await e(...p),u.forEach(y=>{y(d)})}})(),l};return s.flush=async()=>{if((r||o)&&i)return await i,s.flush()},s},xe=(e,t,r=!1)=>{let o=null,n=null,i=null,s=function(...a){n=a,i=this;let c=r&&!o;clearTimeout(o),o=setTimeout(()=>{o=null,r||(e.apply(i,n),n=null,i=null)},t),c&&(e.apply(i,n),n=null,i=null)};return s.cancel=()=>{clearTimeout(o),o=null,n=null,i=null},s.flush=()=>{if(o){clearTimeout(o);let a=n,c=i;o=null,n=null,i=null,e.apply(c,a)}},s},He=(e,t=!0,r)=>{if(e)try{return JSON.parse(e)}catch(o){t&&(r?.error?r.error("Could not parse JSON",o):ir.error("Could not parse JSON",o))}},ft=e=>e.charAt(0).toUpperCase()+e.slice(1),le=e=>e.split("-").map(t=>t.length===2?t.toUpperCase():ft(t)).join(" ");function pe(e,t){t&&e.log(`Skill invoked: ${t}`)}var sr=e=>Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0)),ar=(e,t)=>{let n=".netlify.app",i="agent-";if(!t)return`${i}${e.slice(0,6)}`;let a=`--${t}${n}`;if(a.length>55)return"";let c=60-a.length;if(c<=0)return"";if(c>=i.length+6){let l=Math.min(c-i.length,e.length);return`${i}${e.slice(0,l)}`}return e.slice(0,c)},An=e=>!e||typeof e!="object"||Array.isArray(e)||Object.keys(e).length===0?!1:!!er.some(t=>t in e),lr=()=>{let e={},t={codex:process.env.NETLIFY_FF_AGENT_RUNNER_CODEX_VERSION,claude:process.env.NETLIFY_FF_AGENT_RUNNER_CLAUDE_VERSION,gemini:process.env.NETLIFY_FF_AGENT_RUNNER_GEMINI_VERSION};return Object.entries(t).forEach(([r,o])=>{if(o){let n=`NETLIFY_FF_AGENT_RUNNER_${r.toUpperCase()}_VERSION`;try{let i=JSON.parse(o);An(i)&&(e[r]=i)}catch(i){let a=i instanceof SyntaxError?"Invalid JSON":i.message;ir.error(`Could not parse ${r} model version override from ${n}: ${a}`)}}}),e},Cn=50*1024,ht=(e,t=Cn)=>{if(!e||typeof e!="string"||e.length<=t)return e;let o=e.startsWith("```")?"\n... [truncated]\n```":"... [truncated]";return e.slice(0,t)+o};import{Buffer as cr}from"buffer";import bn from"path";var ur=x("repo"),pr=async({config:e,isRetry:t,cwd:r=process.cwd()})=>{ur.info("Getting runner diffs");let o=await kn(r),{hasChanges:n}=o,{status:i}=o;if(!n)return{hasChanges:!1};if(!t){let E=On(i);await $n(E,r)}ur.info("Changes after processing"),await _t(r);let s=await wt(i,r);if(await yt(s,r),n=await Nn(r),!n)return{hasChanges:!1,ignored:s};process.env.NETLIFY_INTERNAL_GIT="1";try{await k("git",["commit","-m","Agent runner"],{cwd:r})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}let a={stdio:["ignore","pipe","pipe"],cwd:r},c=await k("git",["diff",e.runSha,"HEAD"],a),l=String(c.stdout??"");if(n=!!l,!n)return await dr(r),{hasChanges:!1,ignored:s};let d=await k("git",["diff",e.runSha,"HEAD","--binary"],a),p=String(d.stdout??""),u,y;if(e.sha){let E=await k("git",["diff",e.sha,"HEAD"],a);u=String(E.stdout??"");let I=await k("git",["diff",e.sha,"HEAD","--binary"],a),f=String(I.stdout??"");u!==f&&(y=cr.from(f).toString("base64"))}await dr(r);let w={hasChanges:!0,diff:l,resultDiff:u,ignored:s};return l!==p&&(w.diffBinary=cr.from(p).toString("base64")),y&&(w.resultDiffBinary=y),w},dr=async(e=process.cwd())=>{process.env.NETLIFY_LOCAL_MODE&&await k("git",["reset","--soft","HEAD~1"],{cwd:e})},yt=async(e=[],t=process.cwd())=>{process.env.NETLIFY_INTERNAL_GIT="1";try{await k("git",["add",".",...e],{cwd:t})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}},_t=async(e=process.cwd())=>{let t=await k("git",["status","-s"],{cwd:e});return String(t.stdout??"")},mr=/.. (.+)?\.log$/,Pn=[mr],kn=async(e=process.cwd())=>{let t=await _t(e);return{hasChanges:(t.trim().length===0?[]:t.split(`
|
|
9
9
|
`).filter(n=>Pn.some(s=>s instanceof RegExp?s.test(n):n===s)?!1:n[1]?.trim()!=="")).length!==0,status:t}},Nn=async(e=process.cwd())=>{try{return await k("git",["diff","--staged","--quiet"],{cwd:e}),!1}catch{return!0}},Et=async(e=process.cwd())=>{let{stdout:t}=await k("git",["rev-parse","HEAD"],{cwd:e});return String(t??"").trim()},gr=async(e=process.cwd())=>{let{stdout:t}=await k("git",["rev-list","--max-parents=0","HEAD"],{cwd:e});return String(t??"").trim()},wt=async(e,t=process.cwd())=>{e||=await _t(t);let r=[".netlify","node_modules","dist",".next","out",".nuxt",".output",".cache",".turbo",".parcel-cache","coverage",".nyc_output","storybook-static","public/build","CLAUDE.local.md"],o=[];return e.split(`
|
|
10
10
|
`).forEach(n=>{r.forEach(s=>{let a=n===`?? ${s}`,c=n.startsWith(`?? ${s}/`)||n.startsWith(`?? ${s}${bn.sep}`);(a||c)&&o.push(`:!${s}`)});let i=n.match(mr)?.[1];i&&o.push(`:!${i}.log`)}),o},xt=async(e=process.cwd())=>{await k("git",["reset","--hard","HEAD"],{cwd:e})},On=e=>{let t=e.split(`
|
|
11
11
|
`).reduce((r,o)=>{if(!o)return r;let[n,i,,...s]=o,a=s.join(""),c=n.trim(),l=i.trim();return r[a]?r[a].change=l:r[a]={filePath:a,stage:c,change:l},r},{});return Object.values(t)},$n=async(e,t=process.cwd())=>{let r=e.filter(o=>o.stage&&!o.change).map(o=>o.filePath);r.length!==0&&await k("git",["restore","--staged","--worktree","--pathspec-from-file=-"],{cwd:t,input:r.join(`
|
|
12
|
-
`)})};import Oe from"fs/promises";import Dn from"os";import Ke from"path";import ce from"process";import Ln from"readline";import Mn from"@anthropic-ai/sdk";import Tt from"path";import Fn from"fs/promises";var It=x("agent-output-utils");async function Te({initialResult:e,agentName:t,hasError:r}){let o="",n=Tt.join(process.cwd(),X,ue);try{let i=await Fn.readFile(n,"utf-8");i&&(o=i,It.log(`Pulled result from ${Tt.relative(process.cwd(),n)}`))}catch{It.log(`No results file found at ${Tt.relative(process.cwd(),n)}`)}return o||(!e&&!r?`${t} has finished working on task.`:e||void 0)}function Ie({error:e,agentName:t}){let r=e&&typeof e=="object"?JSON.stringify(e):e,o=r?.replace(/\s+/g," ").trim().toLowerCase()||"",n="";return o?.includes("ai gateway is not available for your account")||o?.includes("ai gateway is not enabled for your account")?n="AI Gateway is currently not available on your account. Please confirm your account meets the criteria for using Agent Runners and AI Gateway and that your account has remaining AI Gateway inference credits available. Reach out to Netlify support if this is unexpected.":o?.includes("error when talking to gemini api")?n="Gemini's API is currently having issues. Please try again or use a different available agent while Google resolves the issue.":(o?.includes("connection closed prematurely")||o?.includes("499")&&t.toLowerCase().includes("gemini"))&&(n=`The ${t} models were currently overloaded. Please try again or use a different available agent.`),o?.includes("request timed out")&&(n=`The ${t} API request's have timed out. Please try again or use a different available agent.`),o?.includes("network error")&&(n=`The ${t} agent is having network issues. Please try again or use a different available agent.`),o?.includes("503")&&(n=`The ${t} API is currently experiencing high load. Retrying automatically...`),n&&It.log(`Providing updated error messsage: ${n}, replacing original error: ${r}`),n||r||void 0}function ve(e){if(!e)return!1;let r=(e&&typeof e=="object"?JSON.stringify(e):e)?.replace(/\s+/g," ").trim().toLowerCase()||"";return r?.includes("error when talking to gemini api")||r?.includes("499")||r?.includes("connection closed prematurely")||r?.includes("request timed out")||r?.includes("network error")?!0:r?.includes("usage exceeded")?!1:!!r?.includes("503")}var j=x("runner_claude"),fr="Claude Code",Un="claude-opus-4-6",Gn={create:{free:"claude-sonnet-4-6"}},We={Task:{name:"Task",category:_.Task},Bash:{name:"Run command",category:_.RunCommand},Glob:{name:"Find files",category:_.Explore},Grep:{name:"Search files",category:_.Explore},LS:{name:"List directory",category:_.Explore},ExitPlanMode:{name:"Exit planning",category:_.Plan},Read:{name:"Read file",category:_.FileRead},Edit:{name:"Edit file",category:_.FileWrite},MultiEdit:{name:"Edit multiple files",category:_.FileWrite},Write:{name:"Edit file",category:_.FileWrite},NotebookEdit:{name:"Edit notebook",category:_.Notebook},WebFetch:{name:"Fetch web",category:_.Web},TodoWrite:{name:"Update task list",category:_.Todo},WebSearch:{name:"Search web",category:_.Web},BashOutput:{name:"Get command output",category:_.RunCommand},KillBash:{name:"Stop command",category:_.RunCommand}},hr=e=>We[e]?.name||e,jn=({catchError:e,runCmd:t,error:r,result:o,runnerName:n})=>(j.log(`${n} command completed with catch handler triggered`,{hadExistingError:!!r,hadExistingResult:!!o,resultLength:o?o.length:0,catchError:e?.message||"No error object",processExitCode:t.exitCode,processKilled:t.killed}),o?(j.log("Preserving existing result despite catch handler being triggered"),r?{error:r,result:o}:{error:"Process completed with errors but result was captured",result:o}):(j.log("Setting result to undefined because no valid result was captured"),{error:r||`${n} failed`,result:void 0}));async function yr({aiGateway:e,config:t,model:r}){let o=r;if(e)if(t.modelVersionOverrides?.claude){let n=t.modelVersionOverrides?.claude?.[t.accountType];if(n){if(!await e.isModelAvailableForProvider("anthropic",n))throw new Error(`Model override '${n}' is not available for anthropic provider`);o=n}}else if(r){if(!await e.isModelAvailableForProvider("anthropic",r))throw new Error(`Model '${r}' is not available for anthropic provider`)}else{let n="mode"in t?Gn[t.mode]?.[t.accountType]:void 0,i=n||Un;!!i&&await e.isModelAvailableForProvider("anthropic",i)?(j.log(`Using ${n?"mode override":"default"} model: ${i}`),o=i):i&&j.log(`Model ${i} is not available, proceeding without model specification`)}return o}function _r({aiGateway:e}){if(e){let{token:t,url:r}=e;if(!t||!r)throw new Error("No token or url provided from AI Gateway");ce.env.ANTHROPIC_API_KEY=t,ce.env.ANTHROPIC_BASE_URL=r}else if(!ce.env.ANTHROPIC_API_KEY)throw new Error("ANTHROPIC_API_KEY is not provided")}async function Yn(){let e=Ke.join(ce.cwd(),"AGENTS.md");try{await Oe.access(e)}catch{return}let t=Ke.join(ce.cwd(),"CLAUDE.local.md"),r="@AGENTS.md";try{if((await Oe.readFile(t,"utf-8")).includes(r))return;await Oe.appendFile(t,`
|
|
12
|
+
`)})};import Oe from"fs/promises";import Dn from"os";import Ke from"path";import ce from"process";import Ln from"readline";import Mn from"@anthropic-ai/sdk";import Tt from"path";import Fn from"fs/promises";var It=x("agent-output-utils");async function Te({initialResult:e,agentName:t,hasError:r}){let o="",n=Tt.join(process.cwd(),X,ue);try{let i=await Fn.readFile(n,"utf-8");i&&(o=i,It.log(`Pulled result from ${Tt.relative(process.cwd(),n)}`))}catch{It.log(`No results file found at ${Tt.relative(process.cwd(),n)}`)}return o||(!e&&!r?`${t} has finished working on task.`:e||void 0)}function Ie({error:e,agentName:t}){let r=e&&typeof e=="object"?JSON.stringify(e):e,o=r?.replace(/\s+/g," ").trim().toLowerCase()||"",n="";return o?.includes("ai gateway is not available for your account")||o?.includes("ai gateway is not enabled for your account")?n="AI Gateway is currently not available on your account. Please confirm your account meets the criteria for using Agent Runners and AI Gateway and that your account has remaining AI Gateway inference credits available. Reach out to Netlify support if this is unexpected.":o?.includes("error when talking to gemini api")?n="Gemini's API is currently having issues. Please try again or use a different available agent while Google resolves the issue.":(o?.includes("connection closed prematurely")||o?.includes("499")&&t.toLowerCase().includes("gemini"))&&(n=`The ${t} models were currently overloaded. Please try again or use a different available agent.`),o?.includes("request timed out")&&(n=`The ${t} API request's have timed out. Please try again or use a different available agent.`),o?.includes("network error")&&(n=`The ${t} agent is having network issues. Please try again or use a different available agent.`),o?.includes("503")&&!o?.includes("usage exceeded")&&(n=`The ${t} API is currently experiencing high load. Retrying automatically...`),n&&It.log(`Providing updated error messsage: ${n}, replacing original error: ${r}`),n||r||void 0}function ve(e){if(!e)return!1;let r=(e&&typeof e=="object"?JSON.stringify(e):e)?.replace(/\s+/g," ").trim().toLowerCase()||"";return r?.includes("error when talking to gemini api")||r?.includes("499")||r?.includes("connection closed prematurely")||r?.includes("request timed out")||r?.includes("network error")?!0:r?.includes("usage exceeded")?!1:!!r?.includes("503")}var j=x("runner_claude"),fr="Claude Code",Un="claude-opus-4-6",Gn={create:{free:"claude-sonnet-4-6"}},We={Task:{name:"Task",category:_.Task},Bash:{name:"Run command",category:_.RunCommand},Glob:{name:"Find files",category:_.Explore},Grep:{name:"Search files",category:_.Explore},LS:{name:"List directory",category:_.Explore},ExitPlanMode:{name:"Exit planning",category:_.Plan},Read:{name:"Read file",category:_.FileRead},Edit:{name:"Edit file",category:_.FileWrite},MultiEdit:{name:"Edit multiple files",category:_.FileWrite},Write:{name:"Edit file",category:_.FileWrite},NotebookEdit:{name:"Edit notebook",category:_.Notebook},WebFetch:{name:"Fetch web",category:_.Web},TodoWrite:{name:"Update task list",category:_.Todo},WebSearch:{name:"Search web",category:_.Web},BashOutput:{name:"Get command output",category:_.RunCommand},KillBash:{name:"Stop command",category:_.RunCommand}},hr=e=>We[e]?.name||e,jn=({catchError:e,runCmd:t,error:r,result:o,runnerName:n})=>(j.log(`${n} command completed with catch handler triggered`,{hadExistingError:!!r,hadExistingResult:!!o,resultLength:o?o.length:0,catchError:e?.message||"No error object",processExitCode:t.exitCode,processKilled:t.killed}),o?(j.log("Preserving existing result despite catch handler being triggered"),r?{error:r,result:o}:{error:"Process completed with errors but result was captured",result:o}):(j.log("Setting result to undefined because no valid result was captured"),{error:r||`${n} failed`,result:void 0}));async function yr({aiGateway:e,config:t,model:r}){let o=r;if(e)if(t.modelVersionOverrides?.claude){let n=t.modelVersionOverrides?.claude?.[t.accountType];if(n){if(!await e.isModelAvailableForProvider("anthropic",n))throw new Error(`Model override '${n}' is not available for anthropic provider`);o=n}}else if(r){if(!await e.isModelAvailableForProvider("anthropic",r))throw new Error(`Model '${r}' is not available for anthropic provider`)}else{let n="mode"in t?Gn[t.mode]?.[t.accountType]:void 0,i=n||Un;!!i&&await e.isModelAvailableForProvider("anthropic",i)?(j.log(`Using ${n?"mode override":"default"} model: ${i}`),o=i):i&&j.log(`Model ${i} is not available, proceeding without model specification`)}return o}function _r({aiGateway:e}){if(e){let{token:t,url:r}=e;if(!t||!r)throw new Error("No token or url provided from AI Gateway");ce.env.ANTHROPIC_API_KEY=t,ce.env.ANTHROPIC_BASE_URL=r}else if(!ce.env.ANTHROPIC_API_KEY)throw new Error("ANTHROPIC_API_KEY is not provided")}async function Yn(){let e=Ke.join(ce.cwd(),"AGENTS.md");try{await Oe.access(e)}catch{return}let t=Ke.join(ce.cwd(),"CLAUDE.local.md"),r="@AGENTS.md";try{if((await Oe.readFile(t,"utf-8")).includes(r))return;await Oe.appendFile(t,`
|
|
13
13
|
${r}
|
|
14
14
|
`)}catch{await Oe.writeFile(t,`${r}
|
|
15
15
|
`)}j.log("Added @AGENTS.md import to CLAUDE.local.md")}async function vt({config:e,netlify:t,persistSteps:r,aiGateway:o,continueSession:n,priorAgentSessionId:i,cwd:s=ce.cwd()}){let a=e,{prompt:c}=a,{model:l}=e,d="";await Yn(),_r({aiGateway:o});let p=await yr({config:e,aiGateway:o,model:l}),u=[],y=[],w={},E=0,I=0,f,v,R="mode"in e&&e.mode==="create"&&e.accountType===Ne,$=["ExitPlanMode","AskUserQuestion"];R&&$.push("TodoWrite");let F=[ae(s,"claude"),"--permission-mode","bypassPermissions","--dangerously-skip-permissions","--output-format","stream-json","--verbose","--disallowed-tools",$.join(","),...R?["--effort","low"]:[],...p?["--model",p]:[],...n?["--continue"]:[],...n&&i?["--resume",i]:[],"-p",c],G=`${ce.env.NVM_BIN}/node`;j.log(`Running ${G} ${F.join(" ")}`);let D=t.utils.run(G,F,{all:!0,env:ce.env,cwd:s,idleTimeout:de});D.stdin?.end();let b=xe(()=>{r?.({steps:u,duration:I})},250),U=(m,g)=>{let{wrapMessage:h,...S}=m,A=sr({...S,id:E});A.message&&(A.message=A.message.replace(/\n?<system-reminder>.+?<\/system-reminder>\n?/gs,"").trim(),h&&A.message&&(A.message=`\`\`\`
|
package/dist/index.js
CHANGED
|
@@ -4,11 +4,11 @@ import{createRequire as hi}from"module";import{createTracerProvider as Gr}from"@
|
|
|
4
4
|
${s}
|
|
5
5
|
</extracted_error_chunk>`).join(`
|
|
6
6
|
|
|
7
|
-
`);return o.length>e.length*.8?e:o}import{execSync as Un}from"child_process";import Er from"fs/promises";import Gn from"path";import me from"process";import{getTracer as jn}from"@netlify/otel";import be from"process";var V=class extends Error{constructor(r,i,n,o=!1){super(r);this.statusCode=i;this.userMessage=n;this.isCreditLimitExceeded=o;this.name="GracefulShutdownError"}},Le=e=>e instanceof V;var Me=be.env.NETLIFY_API_URL,Ue=be.env.NETLIFY_API_TOKEN,W=x("api"),he=()=>be.env.NETLIFY_LOCAL_MODE==="true",ie=async(e,t={})=>{if(!Me||!Ue)throw new Error("No API URL or token");let r=new URL(e,Me),i={...t,headers:{...t.headers,Authorization:`Bearer ${Ue}`}};be.env.AGENT_RUNNERS_DEBUG==="true"&&(i.headers["x-nf-debug-logging"]="true"),t.json&&(i.headers||={},i.headers["Content-Type"]="application/json",i.body=JSON.stringify(t.json));let n=await fetch(r,i),o=n.ok&&n.status<=299;if(be.env.AGENT_RUNNERS_DEBUG==="true")W.log(`Response headers for ${r}:`),n.headers.forEach((a,c)=>{W.log(` ${c}: ${a}`)});else{let a=n.headers.get("x-request-id")||n.headers.get("x-nf-request-id");W.log(`Request ID for ${r}: ${a||"N/A"}`)}if(o||W.error(`Got status ${n.status} for request ${r}`),t.raw){if(!o)throw new Error(`API request failed: ${n.status} ${n.statusText}`);return n}let s=await(n.headers.get("content-type")?.includes("application/json")?n.json():n.text());if(!o){let a=typeof s=="string"?s:JSON.stringify(s);throw n.status===404?new V(`API request failed: 404 - ${a}`,404,"The site associated with this agent run no longer exists."):n.status===503&&t.gracefulOn503&&a.toLowerCase().includes("usage exceeded")?new V(`API request failed: 503 - ${a}`,503,"Credit limit reached. Please add more credits to continue using Agent Runners.",!0):new Error(`API request failed: ${n.status} - ${a}`)}return s},Pt=e=>{W.log("Setting details for api",{apiUrl:e?.constants?.NETLIFY_API_HOST,token:!!e?.constants?.NETLIFY_API_TOKEN}),e?.constants?.NETLIFY_API_HOST&&(Me=`https://${e.constants.NETLIFY_API_HOST}`),e?.constants?.NETLIFY_API_TOKEN&&(Ue=e.constants.NETLIFY_API_TOKEN)},Nt=()=>({apiUrl:Me,token:Ue}),Ae=async(e,t)=>he()?(W.log("Mock API: updateRunner called",{runnerId:e,data:t}),{id:e,...t}):ie(`/api/v1/agent_runners/${e}`,{method:"PUT",json:t}),H=async(e,t,r)=>he()?(W.log("Mock API: updateRunnerSession called",JSON.stringify({runnerId:e,sessionId:t,data:r},null,2)),{id:e,sessionId:t,...r}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`,{method:"PUT",json:r});var $t=async e=>he()?(W.log("Mock API: getSite called",{siteId:e}),{id:e,published_deploy:{id:"id"}}):ie(`/api/v1/sites/${e}`),Ot=async(e,t)=>he()?(W.log("Mock API: getRunnerSession called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,state:"running"}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`),Ft=(e,t,r)=>ie(`/api/v1/accounts/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Dt=(e,t,r)=>ie(`/api/v1/sites/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Lt=async(e,t)=>he()?(W.log("Mock API: getDiffUploadUrls called",{runnerId:e,sessionId:t}),{result:{upload_url:"https://s3.mock.com/mock-upload-url-result",s3_key:"mock-s3-key-result"},cumulative:{upload_url:"https://s3.mock.com/mock-upload-url-cumulative",s3_key:"mock-s3-key-cumulative"}}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/diff/upload_urls`,{method:"POST"}),Mt=async(e,t)=>he()?(W.log("Mock API: updateSessionUsage called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,usage:0}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/update_usage`,{method:"POST"}),rt=async(e,t)=>{W.log(`Uploading diff to S3: ${e.substring(0,50)}...`);let r=await fetch(e,{method:"PUT",body:t,headers:{"Content-Type":"text/plain"}});if(!r.ok)throw new Error(`S3 upload failed with status ${r.status}`);return r};var ye=x("ai_gateway"),nt=null;var Ut=async()=>{if(nt)return nt;ye.log("Fetching available AI gateway providers");let e=await fetch(`${Nt().apiUrl}/api/v1/ai-gateway/providers`);if(!e.ok)throw new Error(`Failed to fetch AI gateway providers: ${e.statusText}`);let t=await e.json();return nt=t,ye.log("Cached AI gateway providers",{providerCount:Object.keys(t.providers).length}),t},Hr=async(e,t)=>{let i=(await Ut()).providers[e];if(!i)return ye.log(`Provider '${e}' not found`),!1;let n=i.models.includes(t);return ye.log(`Model validation for ${e}/${t}`,{isAvailable:n}),n},Gt=async({config:e})=>{let t,r,i,n,o=!e.site?.published_deploy;if(!(o?e.accountId:e.siteId))throw new Error(`No entity id for ${o?"account":"site"}`);let a=async()=>{clearTimeout(i),ye.log("Requesting AI gateway information");let l=await(o?Ft(e.accountId,e.id,e.sessionId):Dt(e.siteId,e.id,e.sessionId));if({token:t,url:n}=l,r=l.expires_at?l.expires_at*1e3:void 0,ye.log("Got AI gateway information",{token:!!t,expiresAt:r,url:n}),r){let d=r-Date.now()-6e4;d>0&&(i=setTimeout(()=>{a()},d))}};return await Promise.all([a(),Ut()]),{get url(){return n},get token(){return t},isModelAvailableForProvider:Hr}};import X from"process";import re from"path";import Ge from"fs";import{fileURLToPath as Qr}from"url";import{createRequire as en}from"module";import{execa as tn,execaCommand as zi}from"execa";import{Transform as Kr}from"stream";function Jr(){let e=process.env.NETLIFY_SENSITIVE_ENV_KEYS;return e?e.split(",").map(t=>t.trim()).filter(Boolean):[]}function Vr(e){let t=e.toLowerCase();return t==="true"||t==="false"?!0:e.trim().length<4}function zr(){let t=Jr().map(r=>process.env[r]).filter(r=>!(!r||Vr(r)));return[...new Set(t)].sort((r,i)=>i.length-r.length)}function te(e){if(typeof e!="string")return e;let t=zr();if(t.length===0)return e;let r=e;return t.forEach(i=>{let n=new RegExp(Xr(i),"g");r=r.replace(n,"******")}),r}function Xr(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var we=class extends Kr{constructor(t={}){super({...t,objectMode:!1})}_transform(t,r,i){let n=t.toString(),o=te(n);i(null,o)}};function jt(){if(!(process.env.NETLIFY_MASK_LOGS!=="false"))return;let t=process.stdout.write.bind(process.stdout),r=process.stderr.write.bind(process.stderr);process.stdout.write=function(i,n,o){let s=typeof i=="string"?te(i):i;return typeof n=="function"?t(s,n):t(s,n,o)},process.stderr.write=function(i,n,o){let s=typeof i=="string"?te(i):i;return typeof n=="function"?r(s,n):r(s,n,o)}}var Ce=null,Yt=e=>(Ce&&Ce.destroy(),Ce=new oe({totalAllowedTime:e}),Ce),qt=()=>Ce;var oe=class{constructor({totalAllowedTime:t}){this.withStageTimer=async(t,r,i)=>{if(this.isTimeExpired())throw new Error(`${t} stage did not complete in the allowed time. Time has already expired.`);let n=this.onTimesUp(()=>{throw new Error(`${t} stage did not complete in the allowed time.`)}),o=null,s=null;i!==void 0&&(s=new Promise((a,c)=>{o=setTimeout(()=>{c(new Error(`${t} stage exceeded its maximum duration of ${i}ms`))},i)}));try{return s?await Promise.race([r(),s]):await r()}finally{n(),o&&clearTimeout(o)}};this.startTime=Date.now(),this.totalAllowedTime=t,this.globalTimeoutId=null,this.subscribers=[],this.hasTimedOut=!1,this.setupGlobalTimeout()}getElapsedTime(){return Date.now()-this.startTime}getRemainingTime(){let t=this.getElapsedTime(),r=this.totalAllowedTime-t;return Math.max(0,r)}isTimeExpired(){return this.getRemainingTime()===0||this.hasTimedOut}setupGlobalTimeout(){this.globalTimeoutId&&clearTimeout(this.globalTimeoutId),this.globalTimeoutId=setTimeout(()=>{this.notifyTimeUp()},this.totalAllowedTime)}notifyTimeUp(){this.hasTimedOut=!0;for(let t=this.subscribers.length-1;t>=0;t--)try{this.subscribers[t]()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}}onTimesUp(t){if(this.subscribers.push(t),this.hasTimedOut)try{t()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}return()=>{let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}}off(t){let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}clearSubscribers(){this.subscribers.length=0}getSubscriberCount(){return this.subscribers.length}destroy(){this.globalTimeoutId&&(clearTimeout(this.globalTimeoutId),this.globalTimeoutId=null),this.clearSubscribers()}static{this.timeUnits={seconds:t=>t*1e3,minutes:t=>t*60*1e3,hours:t=>t*60*60*1e3}}};var Bt="netlify-agent-runner-context.md",it="task-history",z=".netlify",ce="results.md",ot="assets";var Wt="free";var ue=1800*1e3,y={Environment:"environment",UserMessage:"user-message",AgentMessage:"agent-message",Task:"task",RunCommand:"run-command",Explore:"explore",Plan:"plan",FileRead:"file-read",FileWrite:"file-write",Notebook:"notebook",Web:"web",Todo:"todo",Reasoning:"reasoning",Skill:"skill",Memorize:"memorize",Deployment:"deployment",SiteGeneration:"site-generation"};var Ht={name:"@netlify/agent-runner-cli",type:"module",version:"1.90.0",description:"CLI tool for running Netlify agents",main:"./dist/index.js",types:"./dist/index.d.ts",exports:"./dist/index.js",bin:{"agent-runner-cli":"./dist/bin.js","agent-runner-cli-local":"./dist/bin-local.js"},files:["dist/**/*.js","dist/**/*.d.ts","dist/skills/**","patches","scripts"],scripts:{build:"tsup",dev:"tsup --watch",prepare:"husky install node_modules/@netlify/eslint-config-node/.husky/",prepublishOnly:"npm ci && npm test",prepack:"npm run build",test:"run-s build format test:dev",format:"run-s build format:check-fix:*","format:ci":"run-s build format:check:*","format:check-fix:lint":"run-e format:check:lint format:fix:lint","format:check:lint":"cross-env-shell eslint $npm_package_config_eslint","format:fix:lint":"cross-env-shell eslint --fix $npm_package_config_eslint","format:check-fix:prettier":"run-e format:check:prettier format:fix:prettier","format:check:prettier":"cross-env-shell prettier --check $npm_package_config_prettier","format:fix:prettier":"cross-env-shell prettier --write $npm_package_config_prettier","test:dev":"run-s build test:dev:*","test:ci":"run-s build test:ci:*","test:dev:vitest":"LOG=0 vitest --exclude '**/integration/**'","test:ci:vitest":"LOG=0 c8 -r lcovonly -r text -r json vitest --exclude '**/integration/**'","test:integration":"vitest run test/integration/","test:integration:codex":"vitest run test/integration/codex.test.ts","test:integration:claude":"vitest run test/integration/claude.test.ts","test:integration:gemini":"vitest run test/integration/gemini.test.ts","test:integration:create-stage":"vitest run test/integration/create.test.ts","test:integration:skill-invocation":"vitest run test/integration/skill-invocation.test.ts","check:types":"tsc --noEmit",postinstall:"node scripts/postinstall.js"},config:{eslint:'--cache --format=codeframe --max-warnings=0 "{src,scripts,test,.github}/**/*.{js,ts,md,html}"',prettier:'--ignore-path .gitignore --loglevel=warn "{src,scripts,test,.github}/**/*.{js,ts,md,yml,json,html}" "*.{js,ts,yml,json,html}" ".*.{js,ts,yml,json,html}" "!**/package-lock.json" "!package-lock.json" "!src/skills/**/*.md"'},keywords:[],license:"MIT",repository:"netlify/agent-runner-cli",bugs:{url:"https://github.com/netlify/agent-runner-cli/issues"},author:"Netlify Inc.",directories:{test:"test"},devDependencies:{"@commitlint/cli":"^20.0.0","@commitlint/config-conventional":"^20.0.0","@eslint/compat":"^2.0.0","@eslint/js":"^9.35.0","@netlify/eslint-config-node":"^7.0.1","@types/node":"^24.5.0","@typescript-eslint/eslint-plugin":"^8.0.0","@typescript-eslint/parser":"^8.0.0","@vitest/eslint-plugin":"^1.6.6",c8:"^10.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-n":"^17.0.0",husky:"^9.0.0","patch-package":"^8.0.0",tsup:"^8.5.0",typescript:"^5.0.0","typescript-eslint":"^8.44.0",vitest:"^4.0.16"},dependencies:{"@anthropic-ai/claude-code":"2.1.63","@anthropic-ai/sdk":"0.78.0","@google/gemini-cli":"0.31.0","@netlify/otel":"^5.1.2","@openai/codex":"0.110.0","@opentelemetry/exporter-trace-otlp-grpc":"0.57.2",execa:"^9.6.1",kaddidlehopper:"^0.7.2",minimist:"^1.2.8",openai:"6.26.0"}};var rn=Qr(import.meta.url),nn=re.dirname(rn),on=en(import.meta.url),_e=x("shell"),st=new Set,sn={preferLocal:!0},P=(e,t,r)=>{let[i,n]=an(t,r),o={...sn,...n},s=tn(e,i,o);ln(s,o),un(s);let a=r?.idleTimeout;return a&&a>0&&cn(s,a),s};var an=function(e,t){return Array.isArray(e)?[e,t]:typeof e=="object"&&e!==null?[[],e]:[[],void 0]},ln=(e,t)=>{if(t.stdio!==void 0||t.stdout!==void 0||t.stderr!==void 0)return;if(X.env.NETLIFY_MASK_LOGS!=="false"){e.all?.pipe(new we).pipe(X.stdout),e.stdout?.pipe(new we).pipe(X.stdout),e.stderr?.pipe(new we).pipe(X.stderr);return}e.stdout?.pipe(X.stdout),e.stderr?.pipe(X.stderr)},at=(e,t="SIGTERM")=>{try{return e.pid&&!e.killed?(X.kill(-e.pid,t),_e.log(`Killed process ${e.pid} with signal ${t}`),!0):!1}catch(r){return _e.error("Error killing process:",r),!1}},Kt=e=>at(e,"SIGKILL"),cn=(e,t)=>{let r=null,i=()=>{_e.log(`Process ${e.pid} killed due to idle timeout (no output for ${t}ms)`),at(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(_e.log(`Force killing idle process ${e.pid}`),Kt(e))},5e3)},n=()=>{r&&clearTimeout(r),r=setTimeout(i,t)};n(),e.stdout?.on("data",n),e.stderr?.on("data",n);let o=()=>{r&&(clearTimeout(r),r=null)};e.on("exit",o),e.on("error",o)},un=e=>{st.add(e);let t=qt();if(t){let r=t.onTimesUp(()=>{_e.log(`Global timer expired, killing process ${e.pid}`),at(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(_e.log(`Force killing process ${e.pid} after timeout`),Kt(e))},5e3)});e.on("exit",()=>{st.delete(e),r()}),e.on("error",()=>{st.delete(e),r()})}};function se(e,t){if(!X.env.NETLIFY_LOCAL_MODE)try{let n=on.resolve(Ht.name),o=re.dirname(n);for(;o!==re.dirname(o);){let s=re.dirname(o);if(re.basename(s)==="node_modules"){let a=re.join(s,".bin",t);if(Ge.existsSync(a))return a;break}o=s}}catch(n){console.error("Could not resolve package.json",n)}if(X.env.NODE_PATH){let n=re.join(X.env.NODE_PATH,".bin",t);if(Ge.existsSync(n))return n}let r=re.join(e,"node_modules",".bin",t);if(Ge.existsSync(r))return r;let i=re.join(nn,"..","node_modules",".bin",t);if(Ge.existsSync(i))return i}var dn=x("utils"),pn=e=>new Promise(t=>{setTimeout(t,e)}),je=(e,t=3e3)=>{let r=!1,i=null,n=[],o=null,s=(...a)=>{if(r)return i=a,new Promise(d=>{n.push(d)});r=!0;let c,l=new Promise(d=>{c=d});return o=(async()=>{await Promise.resolve();let d=await e(...a);for(c(d);;){if(await pn(t),!i)return r=!1,o=null,d;let m=i,u=n;i=null,n=[],d=await e(...m),u.forEach(w=>{w(d)})}})(),l};return s.flush=async()=>{if((r||i)&&o)return await o,s.flush()},s},Ee=(e,t,r=!1)=>{let i=null,n=null,o=null,s=function(...a){n=a,o=this;let c=r&&!i;clearTimeout(i),i=setTimeout(()=>{i=null,r||(e.apply(o,n),n=null,o=null)},t),c&&(e.apply(o,n),n=null,o=null)};return s.cancel=()=>{clearTimeout(i),i=null,n=null,o=null},s.flush=()=>{if(i){clearTimeout(i);let a=n,c=o;i=null,n=null,o=null,e.apply(c,a)}},s},Jt=(e,t=!0,r)=>{if(e)try{return JSON.parse(e)}catch(i){t&&(r?.error?r.error("Could not parse JSON",i):dn.error("Could not parse JSON",i))}},lt=e=>e.charAt(0).toUpperCase()+e.slice(1),ae=e=>e.split("-").map(t=>t.length===2?t.toUpperCase():lt(t)).join(" ");function de(e,t){t&&e.log(`Skill invoked: ${t}`)}var Vt=e=>Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0)),zt=(e,t)=>{let n=".netlify.app",o="agent-";if(!t)return`${o}${e.slice(0,6)}`;let a=`--${t}${n}`;if(a.length>55)return"";let c=60-a.length;if(c<=0)return"";if(c>=o.length+6){let l=Math.min(c-o.length,e.length);return`${o}${e.slice(0,l)}`}return e.slice(0,c)};var mn=50*1024,ct=(e,t=mn)=>{if(!e||typeof e!="string"||e.length<=t)return e;let i=e.startsWith("```")?"\n... [truncated]\n```":"... [truncated]";return e.slice(0,t)+i};import{Buffer as Xt}from"buffer";import gn from"path";var Zt=x("repo"),er=async({config:e,isRetry:t,cwd:r=process.cwd()})=>{Zt.info("Getting runner diffs");let i=await hn(r),{hasChanges:n}=i,{status:o}=i;if(!n)return{hasChanges:!1};if(!t){let _=wn(o);await _n(_,r)}Zt.info("Changes after processing"),await dt(r);let s=await mt(o,r);if(await ut(s,r),n=await yn(r),!n)return{hasChanges:!1,ignored:s};process.env.NETLIFY_INTERNAL_GIT="1";try{await P("git",["commit","-m","Agent runner"],{cwd:r})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}let a={stdio:["ignore","pipe","pipe"],cwd:r},c=await P("git",["diff",e.runSha,"HEAD"],a),l=String(c.stdout??"");if(n=!!l,!n)return await Qt(r),{hasChanges:!1,ignored:s};let d=await P("git",["diff",e.runSha,"HEAD","--binary"],a),m=String(d.stdout??""),u,w;if(e.sha){let _=await P("git",["diff",e.sha,"HEAD"],a);u=String(_.stdout??"");let v=await P("git",["diff",e.sha,"HEAD","--binary"],a),h=String(v.stdout??"");u!==h&&(w=Xt.from(h).toString("base64"))}await Qt(r);let E={hasChanges:!0,diff:l,resultDiff:u,ignored:s};return l!==m&&(E.diffBinary=Xt.from(m).toString("base64")),w&&(E.resultDiffBinary=w),E},Qt=async(e=process.cwd())=>{process.env.NETLIFY_LOCAL_MODE&&await P("git",["reset","--soft","HEAD~1"],{cwd:e})},ut=async(e=[],t=process.cwd())=>{process.env.NETLIFY_INTERNAL_GIT="1";try{await P("git",["add",".",...e],{cwd:t})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}},dt=async(e=process.cwd())=>{let t=await P("git",["status","-s"],{cwd:e});return String(t.stdout??"")},tr=/.. (.+)?\.log$/,fn=[tr],hn=async(e=process.cwd())=>{let t=await dt(e);return{hasChanges:(t.trim().length===0?[]:t.split(`
|
|
7
|
+
`);return o.length>e.length*.8?e:o}import{execSync as Un}from"child_process";import Er from"fs/promises";import Gn from"path";import me from"process";import{getTracer as jn}from"@netlify/otel";import be from"process";var V=class extends Error{constructor(r,i,n,o=!1){super(r);this.statusCode=i;this.userMessage=n;this.isCreditLimitExceeded=o;this.name="GracefulShutdownError"}},Le=e=>e instanceof V;var Me=be.env.NETLIFY_API_URL,Ue=be.env.NETLIFY_API_TOKEN,W=x("api"),he=()=>be.env.NETLIFY_LOCAL_MODE==="true",ie=async(e,t={})=>{if(!Me||!Ue)throw new Error("No API URL or token");let r=new URL(e,Me),i={...t,headers:{...t.headers,Authorization:`Bearer ${Ue}`}};be.env.AGENT_RUNNERS_DEBUG==="true"&&(i.headers["x-nf-debug-logging"]="true"),t.json&&(i.headers||={},i.headers["Content-Type"]="application/json",i.body=JSON.stringify(t.json));let n=await fetch(r,i),o=n.ok&&n.status<=299;if(be.env.AGENT_RUNNERS_DEBUG==="true")W.log(`Response headers for ${r}:`),n.headers.forEach((a,c)=>{W.log(` ${c}: ${a}`)});else{let a=n.headers.get("x-request-id")||n.headers.get("x-nf-request-id");W.log(`Request ID for ${r}: ${a||"N/A"}`)}if(o||W.error(`Got status ${n.status} for request ${r}`),t.raw){if(!o)throw new Error(`API request failed: ${n.status} ${n.statusText}`);return n}let s=await(n.headers.get("content-type")?.includes("application/json")?n.json():n.text());if(!o){let a=typeof s=="string"?s:JSON.stringify(s);throw n.status===404?new V(`API request failed: 404 - ${a}`,404,"The site associated with this agent run no longer exists."):n.status===503&&t.gracefulOn503&&a.toLowerCase().includes("usage exceeded")?new V(`API request failed: 503 - ${a}`,503,"Credit limit reached. Please add more credits to continue using Agent Runners.",!0):new Error(`API request failed: ${n.status} - ${a}`)}return s},Pt=e=>{W.log("Setting details for api",{apiUrl:e?.constants?.NETLIFY_API_HOST,token:!!e?.constants?.NETLIFY_API_TOKEN}),e?.constants?.NETLIFY_API_HOST&&(Me=`https://${e.constants.NETLIFY_API_HOST}`),e?.constants?.NETLIFY_API_TOKEN&&(Ue=e.constants.NETLIFY_API_TOKEN)},Nt=()=>({apiUrl:Me,token:Ue}),Ae=async(e,t)=>he()?(W.log("Mock API: updateRunner called",{runnerId:e,data:t}),{id:e,...t}):ie(`/api/v1/agent_runners/${e}`,{method:"PUT",json:t}),H=async(e,t,r)=>he()?(W.log("Mock API: updateRunnerSession called",JSON.stringify({runnerId:e,sessionId:t,data:r},null,2)),{id:e,sessionId:t,...r}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`,{method:"PUT",json:r});var $t=async e=>he()?(W.log("Mock API: getSite called",{siteId:e}),{id:e,published_deploy:{id:"id"}}):ie(`/api/v1/sites/${e}`),Ot=async(e,t)=>he()?(W.log("Mock API: getRunnerSession called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,state:"running"}):ie(`/api/v1/agent_runners/${e}/sessions/${t}`),Ft=(e,t,r)=>ie(`/api/v1/accounts/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Dt=(e,t,r)=>ie(`/api/v1/sites/${e}/ai-gateway/token`,{headers:{"X-Nf-Agent-Runner-Id":t,"X-Nf-Agent-Runner-Session-Id":r},gracefulOn503:!0}),Lt=async(e,t)=>he()?(W.log("Mock API: getDiffUploadUrls called",{runnerId:e,sessionId:t}),{result:{upload_url:"https://s3.mock.com/mock-upload-url-result",s3_key:"mock-s3-key-result"},cumulative:{upload_url:"https://s3.mock.com/mock-upload-url-cumulative",s3_key:"mock-s3-key-cumulative"}}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/diff/upload_urls`,{method:"POST"}),Mt=async(e,t)=>he()?(W.log("Mock API: updateSessionUsage called",{runnerId:e,sessionId:t}),{id:t,runnerId:e,usage:0}):ie(`/api/v1/agent_runners/${e}/sessions/${t}/update_usage`,{method:"POST"}),rt=async(e,t)=>{W.log(`Uploading diff to S3: ${e.substring(0,50)}...`);let r=await fetch(e,{method:"PUT",body:t,headers:{"Content-Type":"text/plain"}});if(!r.ok)throw new Error(`S3 upload failed with status ${r.status}`);return r};var ye=x("ai_gateway"),nt=null;var Ut=async()=>{if(nt)return nt;ye.log("Fetching available AI gateway providers");let e=await fetch(`${Nt().apiUrl}/api/v1/ai-gateway/providers`);if(!e.ok)throw new Error(`Failed to fetch AI gateway providers: ${e.statusText}`);let t=await e.json();return nt=t,ye.log("Cached AI gateway providers",{providerCount:Object.keys(t.providers).length}),t},Hr=async(e,t)=>{let i=(await Ut()).providers[e];if(!i)return ye.log(`Provider '${e}' not found`),!1;let n=i.models.includes(t);return ye.log(`Model validation for ${e}/${t}`,{isAvailable:n}),n},Gt=async({config:e})=>{let t,r,i,n,o=!e.site?.published_deploy;if(!(o?e.accountId:e.siteId))throw new Error(`No entity id for ${o?"account":"site"}`);let a=async()=>{clearTimeout(i),ye.log("Requesting AI gateway information");let l=await(o?Ft(e.accountId,e.id,e.sessionId):Dt(e.siteId,e.id,e.sessionId));if({token:t,url:n}=l,r=l.expires_at?l.expires_at*1e3:void 0,ye.log("Got AI gateway information",{token:!!t,expiresAt:r,url:n}),r){let d=r-Date.now()-6e4;d>0&&(i=setTimeout(()=>{a()},d))}};return await Promise.all([a(),Ut()]),{get url(){return n},get token(){return t},isModelAvailableForProvider:Hr}};import X from"process";import re from"path";import Ge from"fs";import{fileURLToPath as Qr}from"url";import{createRequire as en}from"module";import{execa as tn,execaCommand as zi}from"execa";import{Transform as Kr}from"stream";function Jr(){let e=process.env.NETLIFY_SENSITIVE_ENV_KEYS;return e?e.split(",").map(t=>t.trim()).filter(Boolean):[]}function Vr(e){let t=e.toLowerCase();return t==="true"||t==="false"?!0:e.trim().length<4}function zr(){let t=Jr().map(r=>process.env[r]).filter(r=>!(!r||Vr(r)));return[...new Set(t)].sort((r,i)=>i.length-r.length)}function te(e){if(typeof e!="string")return e;let t=zr();if(t.length===0)return e;let r=e;return t.forEach(i=>{let n=new RegExp(Xr(i),"g");r=r.replace(n,"******")}),r}function Xr(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}var we=class extends Kr{constructor(t={}){super({...t,objectMode:!1})}_transform(t,r,i){let n=t.toString(),o=te(n);i(null,o)}};function jt(){if(!(process.env.NETLIFY_MASK_LOGS!=="false"))return;let t=process.stdout.write.bind(process.stdout),r=process.stderr.write.bind(process.stderr);process.stdout.write=function(i,n,o){let s=typeof i=="string"?te(i):i;return typeof n=="function"?t(s,n):t(s,n,o)},process.stderr.write=function(i,n,o){let s=typeof i=="string"?te(i):i;return typeof n=="function"?r(s,n):r(s,n,o)}}var Ce=null,Yt=e=>(Ce&&Ce.destroy(),Ce=new oe({totalAllowedTime:e}),Ce),qt=()=>Ce;var oe=class{constructor({totalAllowedTime:t}){this.withStageTimer=async(t,r,i)=>{if(this.isTimeExpired())throw new Error(`${t} stage did not complete in the allowed time. Time has already expired.`);let n=this.onTimesUp(()=>{throw new Error(`${t} stage did not complete in the allowed time.`)}),o=null,s=null;i!==void 0&&(s=new Promise((a,c)=>{o=setTimeout(()=>{c(new Error(`${t} stage exceeded its maximum duration of ${i}ms`))},i)}));try{return s?await Promise.race([r(),s]):await r()}finally{n(),o&&clearTimeout(o)}};this.startTime=Date.now(),this.totalAllowedTime=t,this.globalTimeoutId=null,this.subscribers=[],this.hasTimedOut=!1,this.setupGlobalTimeout()}getElapsedTime(){return Date.now()-this.startTime}getRemainingTime(){let t=this.getElapsedTime(),r=this.totalAllowedTime-t;return Math.max(0,r)}isTimeExpired(){return this.getRemainingTime()===0||this.hasTimedOut}setupGlobalTimeout(){this.globalTimeoutId&&clearTimeout(this.globalTimeoutId),this.globalTimeoutId=setTimeout(()=>{this.notifyTimeUp()},this.totalAllowedTime)}notifyTimeUp(){this.hasTimedOut=!0;for(let t=this.subscribers.length-1;t>=0;t--)try{this.subscribers[t]()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}}onTimesUp(t){if(this.subscribers.push(t),this.hasTimedOut)try{t()}catch(r){console.error("TimeKeeper: Error in time up callback:",r)}return()=>{let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}}off(t){let r=this.subscribers.indexOf(t);r>-1&&this.subscribers.splice(r,1)}clearSubscribers(){this.subscribers.length=0}getSubscriberCount(){return this.subscribers.length}destroy(){this.globalTimeoutId&&(clearTimeout(this.globalTimeoutId),this.globalTimeoutId=null),this.clearSubscribers()}static{this.timeUnits={seconds:t=>t*1e3,minutes:t=>t*60*1e3,hours:t=>t*60*60*1e3}}};var Bt="netlify-agent-runner-context.md",it="task-history",z=".netlify",ce="results.md",ot="assets";var Wt="free";var ue=1800*1e3,y={Environment:"environment",UserMessage:"user-message",AgentMessage:"agent-message",Task:"task",RunCommand:"run-command",Explore:"explore",Plan:"plan",FileRead:"file-read",FileWrite:"file-write",Notebook:"notebook",Web:"web",Todo:"todo",Reasoning:"reasoning",Skill:"skill",Memorize:"memorize",Deployment:"deployment",SiteGeneration:"site-generation"};var Ht={name:"@netlify/agent-runner-cli",type:"module",version:"1.90.1",description:"CLI tool for running Netlify agents",main:"./dist/index.js",types:"./dist/index.d.ts",exports:"./dist/index.js",bin:{"agent-runner-cli":"./dist/bin.js","agent-runner-cli-local":"./dist/bin-local.js"},files:["dist/**/*.js","dist/**/*.d.ts","dist/skills/**","patches","scripts"],scripts:{build:"tsup",dev:"tsup --watch",prepare:"husky install node_modules/@netlify/eslint-config-node/.husky/",prepublishOnly:"npm ci && npm test",prepack:"npm run build",test:"run-s build format test:dev",format:"run-s build format:check-fix:*","format:ci":"run-s build format:check:*","format:check-fix:lint":"run-e format:check:lint format:fix:lint","format:check:lint":"cross-env-shell eslint $npm_package_config_eslint","format:fix:lint":"cross-env-shell eslint --fix $npm_package_config_eslint","format:check-fix:prettier":"run-e format:check:prettier format:fix:prettier","format:check:prettier":"cross-env-shell prettier --check $npm_package_config_prettier","format:fix:prettier":"cross-env-shell prettier --write $npm_package_config_prettier","test:dev":"run-s build test:dev:*","test:ci":"run-s build test:ci:*","test:dev:vitest":"LOG=0 vitest --exclude '**/integration/**'","test:ci:vitest":"LOG=0 c8 -r lcovonly -r text -r json vitest --exclude '**/integration/**'","test:integration":"vitest run test/integration/","test:integration:codex":"vitest run test/integration/codex.test.ts","test:integration:claude":"vitest run test/integration/claude.test.ts","test:integration:gemini":"vitest run test/integration/gemini.test.ts","test:integration:create-stage":"vitest run test/integration/create.test.ts","test:integration:skill-invocation":"vitest run test/integration/skill-invocation.test.ts","check:types":"tsc --noEmit",postinstall:"node scripts/postinstall.js"},config:{eslint:'--cache --format=codeframe --max-warnings=0 "{src,scripts,test,.github}/**/*.{js,ts,md,html}"',prettier:'--ignore-path .gitignore --loglevel=warn "{src,scripts,test,.github}/**/*.{js,ts,md,yml,json,html}" "*.{js,ts,yml,json,html}" ".*.{js,ts,yml,json,html}" "!**/package-lock.json" "!package-lock.json" "!src/skills/**/*.md"'},keywords:[],license:"MIT",repository:"netlify/agent-runner-cli",bugs:{url:"https://github.com/netlify/agent-runner-cli/issues"},author:"Netlify Inc.",directories:{test:"test"},devDependencies:{"@commitlint/cli":"^20.0.0","@commitlint/config-conventional":"^20.0.0","@eslint/compat":"^2.0.0","@eslint/js":"^9.35.0","@netlify/eslint-config-node":"^7.0.1","@types/node":"^24.5.0","@typescript-eslint/eslint-plugin":"^8.0.0","@typescript-eslint/parser":"^8.0.0","@vitest/eslint-plugin":"^1.6.6",c8:"^10.0.0","eslint-config-prettier":"^10.1.8","eslint-plugin-n":"^17.0.0",husky:"^9.0.0","patch-package":"^8.0.0",tsup:"^8.5.0",typescript:"^5.0.0","typescript-eslint":"^8.44.0",vitest:"^4.0.16"},dependencies:{"@anthropic-ai/claude-code":"2.1.63","@anthropic-ai/sdk":"0.78.0","@google/gemini-cli":"0.31.0","@netlify/otel":"^5.1.2","@openai/codex":"0.110.0","@opentelemetry/exporter-trace-otlp-grpc":"0.57.2",execa:"^9.6.1",kaddidlehopper:"^0.7.2",minimist:"^1.2.8",openai:"6.26.0"}};var rn=Qr(import.meta.url),nn=re.dirname(rn),on=en(import.meta.url),_e=x("shell"),st=new Set,sn={preferLocal:!0},P=(e,t,r)=>{let[i,n]=an(t,r),o={...sn,...n},s=tn(e,i,o);ln(s,o),un(s);let a=r?.idleTimeout;return a&&a>0&&cn(s,a),s};var an=function(e,t){return Array.isArray(e)?[e,t]:typeof e=="object"&&e!==null?[[],e]:[[],void 0]},ln=(e,t)=>{if(t.stdio!==void 0||t.stdout!==void 0||t.stderr!==void 0)return;if(X.env.NETLIFY_MASK_LOGS!=="false"){e.all?.pipe(new we).pipe(X.stdout),e.stdout?.pipe(new we).pipe(X.stdout),e.stderr?.pipe(new we).pipe(X.stderr);return}e.stdout?.pipe(X.stdout),e.stderr?.pipe(X.stderr)},at=(e,t="SIGTERM")=>{try{return e.pid&&!e.killed?(X.kill(-e.pid,t),_e.log(`Killed process ${e.pid} with signal ${t}`),!0):!1}catch(r){return _e.error("Error killing process:",r),!1}},Kt=e=>at(e,"SIGKILL"),cn=(e,t)=>{let r=null,i=()=>{_e.log(`Process ${e.pid} killed due to idle timeout (no output for ${t}ms)`),at(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(_e.log(`Force killing idle process ${e.pid}`),Kt(e))},5e3)},n=()=>{r&&clearTimeout(r),r=setTimeout(i,t)};n(),e.stdout?.on("data",n),e.stderr?.on("data",n);let o=()=>{r&&(clearTimeout(r),r=null)};e.on("exit",o),e.on("error",o)},un=e=>{st.add(e);let t=qt();if(t){let r=t.onTimesUp(()=>{_e.log(`Global timer expired, killing process ${e.pid}`),at(e,"SIGTERM"),setTimeout(()=>{e.pid&&!e.killed&&(_e.log(`Force killing process ${e.pid} after timeout`),Kt(e))},5e3)});e.on("exit",()=>{st.delete(e),r()}),e.on("error",()=>{st.delete(e),r()})}};function se(e,t){if(!X.env.NETLIFY_LOCAL_MODE)try{let n=on.resolve(Ht.name),o=re.dirname(n);for(;o!==re.dirname(o);){let s=re.dirname(o);if(re.basename(s)==="node_modules"){let a=re.join(s,".bin",t);if(Ge.existsSync(a))return a;break}o=s}}catch(n){console.error("Could not resolve package.json",n)}if(X.env.NODE_PATH){let n=re.join(X.env.NODE_PATH,".bin",t);if(Ge.existsSync(n))return n}let r=re.join(e,"node_modules",".bin",t);if(Ge.existsSync(r))return r;let i=re.join(nn,"..","node_modules",".bin",t);if(Ge.existsSync(i))return i}var dn=x("utils"),pn=e=>new Promise(t=>{setTimeout(t,e)}),je=(e,t=3e3)=>{let r=!1,i=null,n=[],o=null,s=(...a)=>{if(r)return i=a,new Promise(d=>{n.push(d)});r=!0;let c,l=new Promise(d=>{c=d});return o=(async()=>{await Promise.resolve();let d=await e(...a);for(c(d);;){if(await pn(t),!i)return r=!1,o=null,d;let m=i,u=n;i=null,n=[],d=await e(...m),u.forEach(w=>{w(d)})}})(),l};return s.flush=async()=>{if((r||i)&&o)return await o,s.flush()},s},Ee=(e,t,r=!1)=>{let i=null,n=null,o=null,s=function(...a){n=a,o=this;let c=r&&!i;clearTimeout(i),i=setTimeout(()=>{i=null,r||(e.apply(o,n),n=null,o=null)},t),c&&(e.apply(o,n),n=null,o=null)};return s.cancel=()=>{clearTimeout(i),i=null,n=null,o=null},s.flush=()=>{if(i){clearTimeout(i);let a=n,c=o;i=null,n=null,o=null,e.apply(c,a)}},s},Jt=(e,t=!0,r)=>{if(e)try{return JSON.parse(e)}catch(i){t&&(r?.error?r.error("Could not parse JSON",i):dn.error("Could not parse JSON",i))}},lt=e=>e.charAt(0).toUpperCase()+e.slice(1),ae=e=>e.split("-").map(t=>t.length===2?t.toUpperCase():lt(t)).join(" ");function de(e,t){t&&e.log(`Skill invoked: ${t}`)}var Vt=e=>Object.fromEntries(Object.entries(e).filter(([,t])=>t!==void 0)),zt=(e,t)=>{let n=".netlify.app",o="agent-";if(!t)return`${o}${e.slice(0,6)}`;let a=`--${t}${n}`;if(a.length>55)return"";let c=60-a.length;if(c<=0)return"";if(c>=o.length+6){let l=Math.min(c-o.length,e.length);return`${o}${e.slice(0,l)}`}return e.slice(0,c)};var mn=50*1024,ct=(e,t=mn)=>{if(!e||typeof e!="string"||e.length<=t)return e;let i=e.startsWith("```")?"\n... [truncated]\n```":"... [truncated]";return e.slice(0,t)+i};import{Buffer as Xt}from"buffer";import gn from"path";var Zt=x("repo"),er=async({config:e,isRetry:t,cwd:r=process.cwd()})=>{Zt.info("Getting runner diffs");let i=await hn(r),{hasChanges:n}=i,{status:o}=i;if(!n)return{hasChanges:!1};if(!t){let _=wn(o);await _n(_,r)}Zt.info("Changes after processing"),await dt(r);let s=await mt(o,r);if(await ut(s,r),n=await yn(r),!n)return{hasChanges:!1,ignored:s};process.env.NETLIFY_INTERNAL_GIT="1";try{await P("git",["commit","-m","Agent runner"],{cwd:r})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}let a={stdio:["ignore","pipe","pipe"],cwd:r},c=await P("git",["diff",e.runSha,"HEAD"],a),l=String(c.stdout??"");if(n=!!l,!n)return await Qt(r),{hasChanges:!1,ignored:s};let d=await P("git",["diff",e.runSha,"HEAD","--binary"],a),m=String(d.stdout??""),u,w;if(e.sha){let _=await P("git",["diff",e.sha,"HEAD"],a);u=String(_.stdout??"");let v=await P("git",["diff",e.sha,"HEAD","--binary"],a),h=String(v.stdout??"");u!==h&&(w=Xt.from(h).toString("base64"))}await Qt(r);let E={hasChanges:!0,diff:l,resultDiff:u,ignored:s};return l!==m&&(E.diffBinary=Xt.from(m).toString("base64")),w&&(E.resultDiffBinary=w),E},Qt=async(e=process.cwd())=>{process.env.NETLIFY_LOCAL_MODE&&await P("git",["reset","--soft","HEAD~1"],{cwd:e})},ut=async(e=[],t=process.cwd())=>{process.env.NETLIFY_INTERNAL_GIT="1";try{await P("git",["add",".",...e],{cwd:t})}finally{process.env.NETLIFY_INTERNAL_GIT="0"}},dt=async(e=process.cwd())=>{let t=await P("git",["status","-s"],{cwd:e});return String(t.stdout??"")},tr=/.. (.+)?\.log$/,fn=[tr],hn=async(e=process.cwd())=>{let t=await dt(e);return{hasChanges:(t.trim().length===0?[]:t.split(`
|
|
8
8
|
`).filter(n=>fn.some(s=>s instanceof RegExp?s.test(n):n===s)?!1:n[1]?.trim()!=="")).length!==0,status:t}},yn=async(e=process.cwd())=>{try{return await P("git",["diff","--staged","--quiet"],{cwd:e}),!1}catch{return!0}},pt=async(e=process.cwd())=>{let{stdout:t}=await P("git",["rev-parse","HEAD"],{cwd:e});return String(t??"").trim()},rr=async(e=process.cwd())=>{let{stdout:t}=await P("git",["rev-list","--max-parents=0","HEAD"],{cwd:e});return String(t??"").trim()},mt=async(e,t=process.cwd())=>{e||=await dt(t);let r=[".netlify","node_modules","dist",".next","out",".nuxt",".output",".cache",".turbo",".parcel-cache","coverage",".nyc_output","storybook-static","public/build","CLAUDE.local.md"],i=[];return e.split(`
|
|
9
9
|
`).forEach(n=>{r.forEach(s=>{let a=n===`?? ${s}`,c=n.startsWith(`?? ${s}/`)||n.startsWith(`?? ${s}${gn.sep}`);(a||c)&&i.push(`:!${s}`)});let o=n.match(tr)?.[1];o&&i.push(`:!${o}.log`)}),i},gt=async(e=process.cwd())=>{await P("git",["reset","--hard","HEAD"],{cwd:e})},wn=e=>{let t=e.split(`
|
|
10
10
|
`).reduce((r,i)=>{if(!i)return r;let[n,o,,...s]=i,a=s.join(""),c=n.trim(),l=o.trim();return r[a]?r[a].change=l:r[a]={filePath:a,stage:c,change:l},r},{});return Object.values(t)},_n=async(e,t=process.cwd())=>{let r=e.filter(i=>i.stage&&!i.change).map(i=>i.filePath);r.length!==0&&await P("git",["restore","--staged","--worktree","--pathspec-from-file=-"],{cwd:t,input:r.join(`
|
|
11
|
-
`)})};import ke from"fs/promises";import xn from"os";import qe from"path";import le from"process";import Tn from"readline";import Sn from"@anthropic-ai/sdk";import ft from"path";import En from"fs/promises";var ht=x("agent-output-utils");async function xe({initialResult:e,agentName:t,hasError:r}){let i="",n=ft.join(process.cwd(),z,ce);try{let o=await En.readFile(n,"utf-8");o&&(i=o,ht.log(`Pulled result from ${ft.relative(process.cwd(),n)}`))}catch{ht.log(`No results file found at ${ft.relative(process.cwd(),n)}`)}return i||(!e&&!r?`${t} has finished working on task.`:e||void 0)}function Te({error:e,agentName:t}){let r=e&&typeof e=="object"?JSON.stringify(e):e,i=r?.replace(/\s+/g," ").trim().toLowerCase()||"",n="";return i?.includes("ai gateway is not available for your account")||i?.includes("ai gateway is not enabled for your account")?n="AI Gateway is currently not available on your account. Please confirm your account meets the criteria for using Agent Runners and AI Gateway and that your account has remaining AI Gateway inference credits available. Reach out to Netlify support if this is unexpected.":i?.includes("error when talking to gemini api")?n="Gemini's API is currently having issues. Please try again or use a different available agent while Google resolves the issue.":(i?.includes("connection closed prematurely")||i?.includes("499")&&t.toLowerCase().includes("gemini"))&&(n=`The ${t} models were currently overloaded. Please try again or use a different available agent.`),i?.includes("request timed out")&&(n=`The ${t} API request's have timed out. Please try again or use a different available agent.`),i?.includes("network error")&&(n=`The ${t} agent is having network issues. Please try again or use a different available agent.`),i?.includes("503")&&(n=`The ${t} API is currently experiencing high load. Retrying automatically...`),n&&ht.log(`Providing updated error messsage: ${n}, replacing original error: ${r}`),n||r||void 0}function Se(e){if(!e)return!1;let r=(e&&typeof e=="object"?JSON.stringify(e):e)?.replace(/\s+/g," ").trim().toLowerCase()||"";return r?.includes("error when talking to gemini api")||r?.includes("499")||r?.includes("connection closed prematurely")||r?.includes("request timed out")||r?.includes("network error")?!0:r?.includes("usage exceeded")?!1:!!r?.includes("503")}var j=x("runner_claude"),nr="Claude Code",vn="claude-opus-4-6",In={create:{free:"claude-sonnet-4-6"}},Ye={Task:{name:"Task",category:y.Task},Bash:{name:"Run command",category:y.RunCommand},Glob:{name:"Find files",category:y.Explore},Grep:{name:"Search files",category:y.Explore},LS:{name:"List directory",category:y.Explore},ExitPlanMode:{name:"Exit planning",category:y.Plan},Read:{name:"Read file",category:y.FileRead},Edit:{name:"Edit file",category:y.FileWrite},MultiEdit:{name:"Edit multiple files",category:y.FileWrite},Write:{name:"Edit file",category:y.FileWrite},NotebookEdit:{name:"Edit notebook",category:y.Notebook},WebFetch:{name:"Fetch web",category:y.Web},TodoWrite:{name:"Update task list",category:y.Todo},WebSearch:{name:"Search web",category:y.Web},BashOutput:{name:"Get command output",category:y.RunCommand},KillBash:{name:"Stop command",category:y.RunCommand}},ir=e=>Ye[e]?.name||e,Rn=({catchError:e,runCmd:t,error:r,result:i,runnerName:n})=>(j.log(`${n} command completed with catch handler triggered`,{hadExistingError:!!r,hadExistingResult:!!i,resultLength:i?i.length:0,catchError:e?.message||"No error object",processExitCode:t.exitCode,processKilled:t.killed}),i?(j.log("Preserving existing result despite catch handler being triggered"),r?{error:r,result:i}:{error:"Process completed with errors but result was captured",result:i}):(j.log("Setting result to undefined because no valid result was captured"),{error:r||`${n} failed`,result:void 0}));async function or({aiGateway:e,config:t,model:r}){let i=r;if(e)if(t.modelVersionOverrides?.claude){let n=t.modelVersionOverrides?.claude?.[t.accountType];if(n){if(!await e.isModelAvailableForProvider("anthropic",n))throw new Error(`Model override '${n}' is not available for anthropic provider`);i=n}}else if(r){if(!await e.isModelAvailableForProvider("anthropic",r))throw new Error(`Model '${r}' is not available for anthropic provider`)}else{let n="mode"in t?In[t.mode]?.[t.accountType]:void 0,o=n||vn;!!o&&await e.isModelAvailableForProvider("anthropic",o)?(j.log(`Using ${n?"mode override":"default"} model: ${o}`),i=o):o&&j.log(`Model ${o} is not available, proceeding without model specification`)}return i}function sr({aiGateway:e}){if(e){let{token:t,url:r}=e;if(!t||!r)throw new Error("No token or url provided from AI Gateway");le.env.ANTHROPIC_API_KEY=t,le.env.ANTHROPIC_BASE_URL=r}else if(!le.env.ANTHROPIC_API_KEY)throw new Error("ANTHROPIC_API_KEY is not provided")}async function bn(){let e=qe.join(le.cwd(),"AGENTS.md");try{await ke.access(e)}catch{return}let t=qe.join(le.cwd(),"CLAUDE.local.md"),r="@AGENTS.md";try{if((await ke.readFile(t,"utf-8")).includes(r))return;await ke.appendFile(t,`
|
|
11
|
+
`)})};import ke from"fs/promises";import xn from"os";import qe from"path";import le from"process";import Tn from"readline";import Sn from"@anthropic-ai/sdk";import ft from"path";import En from"fs/promises";var ht=x("agent-output-utils");async function xe({initialResult:e,agentName:t,hasError:r}){let i="",n=ft.join(process.cwd(),z,ce);try{let o=await En.readFile(n,"utf-8");o&&(i=o,ht.log(`Pulled result from ${ft.relative(process.cwd(),n)}`))}catch{ht.log(`No results file found at ${ft.relative(process.cwd(),n)}`)}return i||(!e&&!r?`${t} has finished working on task.`:e||void 0)}function Te({error:e,agentName:t}){let r=e&&typeof e=="object"?JSON.stringify(e):e,i=r?.replace(/\s+/g," ").trim().toLowerCase()||"",n="";return i?.includes("ai gateway is not available for your account")||i?.includes("ai gateway is not enabled for your account")?n="AI Gateway is currently not available on your account. Please confirm your account meets the criteria for using Agent Runners and AI Gateway and that your account has remaining AI Gateway inference credits available. Reach out to Netlify support if this is unexpected.":i?.includes("error when talking to gemini api")?n="Gemini's API is currently having issues. Please try again or use a different available agent while Google resolves the issue.":(i?.includes("connection closed prematurely")||i?.includes("499")&&t.toLowerCase().includes("gemini"))&&(n=`The ${t} models were currently overloaded. Please try again or use a different available agent.`),i?.includes("request timed out")&&(n=`The ${t} API request's have timed out. Please try again or use a different available agent.`),i?.includes("network error")&&(n=`The ${t} agent is having network issues. Please try again or use a different available agent.`),i?.includes("503")&&!i?.includes("usage exceeded")&&(n=`The ${t} API is currently experiencing high load. Retrying automatically...`),n&&ht.log(`Providing updated error messsage: ${n}, replacing original error: ${r}`),n||r||void 0}function Se(e){if(!e)return!1;let r=(e&&typeof e=="object"?JSON.stringify(e):e)?.replace(/\s+/g," ").trim().toLowerCase()||"";return r?.includes("error when talking to gemini api")||r?.includes("499")||r?.includes("connection closed prematurely")||r?.includes("request timed out")||r?.includes("network error")?!0:r?.includes("usage exceeded")?!1:!!r?.includes("503")}var j=x("runner_claude"),nr="Claude Code",vn="claude-opus-4-6",In={create:{free:"claude-sonnet-4-6"}},Ye={Task:{name:"Task",category:y.Task},Bash:{name:"Run command",category:y.RunCommand},Glob:{name:"Find files",category:y.Explore},Grep:{name:"Search files",category:y.Explore},LS:{name:"List directory",category:y.Explore},ExitPlanMode:{name:"Exit planning",category:y.Plan},Read:{name:"Read file",category:y.FileRead},Edit:{name:"Edit file",category:y.FileWrite},MultiEdit:{name:"Edit multiple files",category:y.FileWrite},Write:{name:"Edit file",category:y.FileWrite},NotebookEdit:{name:"Edit notebook",category:y.Notebook},WebFetch:{name:"Fetch web",category:y.Web},TodoWrite:{name:"Update task list",category:y.Todo},WebSearch:{name:"Search web",category:y.Web},BashOutput:{name:"Get command output",category:y.RunCommand},KillBash:{name:"Stop command",category:y.RunCommand}},ir=e=>Ye[e]?.name||e,Rn=({catchError:e,runCmd:t,error:r,result:i,runnerName:n})=>(j.log(`${n} command completed with catch handler triggered`,{hadExistingError:!!r,hadExistingResult:!!i,resultLength:i?i.length:0,catchError:e?.message||"No error object",processExitCode:t.exitCode,processKilled:t.killed}),i?(j.log("Preserving existing result despite catch handler being triggered"),r?{error:r,result:i}:{error:"Process completed with errors but result was captured",result:i}):(j.log("Setting result to undefined because no valid result was captured"),{error:r||`${n} failed`,result:void 0}));async function or({aiGateway:e,config:t,model:r}){let i=r;if(e)if(t.modelVersionOverrides?.claude){let n=t.modelVersionOverrides?.claude?.[t.accountType];if(n){if(!await e.isModelAvailableForProvider("anthropic",n))throw new Error(`Model override '${n}' is not available for anthropic provider`);i=n}}else if(r){if(!await e.isModelAvailableForProvider("anthropic",r))throw new Error(`Model '${r}' is not available for anthropic provider`)}else{let n="mode"in t?In[t.mode]?.[t.accountType]:void 0,o=n||vn;!!o&&await e.isModelAvailableForProvider("anthropic",o)?(j.log(`Using ${n?"mode override":"default"} model: ${o}`),i=o):o&&j.log(`Model ${o} is not available, proceeding without model specification`)}return i}function sr({aiGateway:e}){if(e){let{token:t,url:r}=e;if(!t||!r)throw new Error("No token or url provided from AI Gateway");le.env.ANTHROPIC_API_KEY=t,le.env.ANTHROPIC_BASE_URL=r}else if(!le.env.ANTHROPIC_API_KEY)throw new Error("ANTHROPIC_API_KEY is not provided")}async function bn(){let e=qe.join(le.cwd(),"AGENTS.md");try{await ke.access(e)}catch{return}let t=qe.join(le.cwd(),"CLAUDE.local.md"),r="@AGENTS.md";try{if((await ke.readFile(t,"utf-8")).includes(r))return;await ke.appendFile(t,`
|
|
12
12
|
${r}
|
|
13
13
|
`)}catch{await ke.writeFile(t,`${r}
|
|
14
14
|
`)}j.log("Added @AGENTS.md import to CLAUDE.local.md")}async function yt({config:e,netlify:t,persistSteps:r,aiGateway:i,continueSession:n,priorAgentSessionId:o,cwd:s=le.cwd()}){let a=e,{prompt:c}=a,{model:l}=e,d="";await bn(),sr({aiGateway:i});let m=await or({config:e,aiGateway:i,model:l}),u=[],w=[],E={},_=0,v=0,h,I,b="mode"in e&&e.mode==="create"&&e.accountType===Wt,O=["ExitPlanMode","AskUserQuestion"];b&&O.push("TodoWrite");let F=[se(s,"claude"),"--permission-mode","bypassPermissions","--dangerously-skip-permissions","--output-format","stream-json","--verbose","--disallowed-tools",O.join(","),...b?["--effort","low"]:[],...m?["--model",m]:[],...n?["--continue"]:[],...n&&o?["--resume",o]:[],"-p",c],G=`${le.env.NVM_BIN}/node`;j.log(`Running ${G} ${F.join(" ")}`);let D=t.utils.run(G,F,{all:!0,env:le.env,cwd:s,idleTimeout:ue});D.stdin?.end();let C=Ee(()=>{r?.({steps:u,duration:v})},250),U=(p,g)=>{let{wrapMessage:f,...S}=p,R=Vt({...S,id:_});R.message&&(R.message=R.message.replace(/\n?<system-reminder>.+?<\/system-reminder>\n?/gs,"").trim(),f&&R.message&&(R.message=`\`\`\`
|