import{i as e,n as t,r as n,s as r}from"./home-dir-C44RaPME-B5iLWwCq.mjs";import i from"node:fs";import a from"node:path";import{fileURLToPath as o}from"node:url";import"node:fs/promises";import s from"node:os";function c(e){return typeof e==`object`&&!!e&&!Array.isArray(e)&&Object.prototype.toString.call(e)===`[object Object]`}async function l(e){await i.promises.mkdir(e,{recursive:!0})}function u(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function d(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function f(e){return e>=55296&&e<=56319}function p(e){return e>=56320&&e<=57343}function m(e,t,n){let r=e.length,i=t<0?Math.max(r+t,0):Math.min(t,r),a=n===void 0?r:n<0?Math.max(r+n,0):Math.min(n,r);if(a<i){let e=i;i=a,a=e}return i>0&&i<r&&p(e.charCodeAt(i))&&f(e.charCodeAt(i-1))&&(i+=1),a>0&&a<r&&f(e.charCodeAt(a-1))&&p(e.charCodeAt(a))&&--a,e.slice(i,a)}function h(e,t){let n=Math.max(0,Math.floor(t));return e.length<=n?e:m(e,0,n)}function g(e,t=process.env,r=s.homedir){return e?n(e,{env:t,homedir:r}):``}function _(t=process.env,n=s.homedir){let r=t.OPENCLAW_STATE_DIR?.trim();if(r)return g(r,t,n);let o=t.OPENCLAW_CONFIG_PATH?.trim();if(o)return a.dirname(g(o,t,n));let c=a.join(e(t,n),`.openclaw`);try{if(i.existsSync(c))return c}catch{}return c}function v(){return t(process.env,s.homedir)}function y(){let e=v();if(e)return process.env.OPENCLAW_HOME?.trim()?{home:e,prefix:`$OPENCLAW_HOME`}:{home:e,prefix:`~`}}function b(e){if(!e)return e;let t=y();return t?e.split(t.home).join(t.prefix):e}const x=_(),S=new Set([`openclaw`]);function C(e){let t=JSON.parse(e);return typeof t.name==`string`?t.name:null}function w(e){try{return C(i.readFileSync(a.join(e,`package.json`),`utf-8`))}catch{return null}}function T(e,t=12){for(let n of E(e,t)){let e=w(n);if(e&&S.has(e))return n}return null}function*E(e,t){let n=a.resolve(e);for(let e=0;e<t;e+=1){yield n;let e=a.dirname(n);if(e===n)break;n=e}}function D(e){let t=a.resolve(e),n=[a.dirname(t)];try{let e=i.realpathSync(t);e!==t&&n.push(a.dirname(e))}catch{}let r=t.split(a.sep),o=r.lastIndexOf(`.bin`);if(o>0&&r[o-1]===`node_modules`){let e=a.basename(t),i=r.slice(0,o).join(a.sep);n.push(a.join(i,e))}return n}function O(e){for(let t of k(e)){let e=T(t);if(e)return e}return null}function k(e){let t=[];if(e.moduleUrl)try{t.push(a.dirname(o(e.moduleUrl)))}catch{}return e.argv1&&t.push(...D(e.argv1)),e.cwd&&t.push(e.cwd),t}const A=a.join(s.tmpdir(),`openclaw-empty-bundled-plugins`);function j(e){let t=r(e.OPENCLAW_DISABLE_BUNDLED_PLUGINS);return t===`1`||t===`true`}function M(){return i.mkdirSync(A,{recursive:!0}),A}function N(e){return i.existsSync(a.join(e,`.git`))&&i.existsSync(a.join(e,`src`))&&i.existsSync(a.join(e,`extensions`))}function P(e){if(!i.existsSync(e))return!1;try{return i.readdirSync(e,{withFileTypes:!0}).some(t=>{if(!t.isDirectory())return!1;let n=a.join(e,t.name);return i.existsSync(a.join(n,`package.json`))||i.existsSync(a.join(n,`openclaw.plugin.json`))})}catch{return!1}}function F(){let e=process.argv[1]?.toLowerCase();if(e?.endsWith(`.ts`)||e?.endsWith(`.tsx`)||e?.endsWith(`.mts`)||e?.endsWith(`.cts`))return!0;for(let e=0;e<process.execArgv.length;e+=1){let t=process.execArgv[e]?.toLowerCase();if(t){if(t===`tsx`||t.includes(`tsx/register`))return!0;if((t===`--import`||t===`--loader`)&&process.execArgv[e+1]){let t=process.execArgv[e+1].toLowerCase();if(t===`tsx`||t.includes(`tsx/`))return!0}}}return!1}function I(e,t){let n=a.join(e,`extensions`),r=a.join(e,`dist`,`extensions`),o=N(e);if(t&&i.existsSync(n))return n;let s=a.join(e,`dist-runtime`,`extensions`),c=o?P(s):i.existsSync(s),l=o?P(r):i.existsSync(r);if(c&&l)return s;if(l)return r;if(o&&i.existsSync(n))return n}function L(e=process.env){if(j(e))return M();let t=e.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim();if(t){let n=g(t,e);if(i.existsSync(n))return n;try{let e=O({argv1:process.argv[1]});if(e&&!N(e)){let t=I(e,!1);if(t)return t}}catch{}return n}let n=!!e.VITEST||F();try{let e=[O({argv1:process.argv[1]}),O({cwd:process.cwd()}),O({moduleUrl:import.meta.url})].filter((e,t,n)=>!!e&&n.indexOf(e)===t);for(let t of e){let e=I(t,n);if(e)return e}}catch{}try{let e=a.dirname(process.execPath),t=a.join(e,`dist`,`extensions`);if(i.existsSync(t))return t;let n=a.join(e,`extensions`);if(i.existsSync(n))return n}catch{}try{let e=a.dirname(o(import.meta.url));for(let t=0;t<6;t+=1){let t=a.join(e,`extensions`);if(i.existsSync(t))return t;let n=a.dirname(e);if(n===e)break;e=n}}catch{}}export{u as a,_ as c,h as d,l as i,g as l,O as n,c as o,x as r,d as s,L as t,b as u};