openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 15.1 kB
JavaScript
import{i as e}from"./transcript-global-singleton-DIJgobpU.mjs";import{i as t,n}from"./transcript-path-guards-u0N7Z9_x.mjs";import"./transcript-src-gRxwdYZM.mjs";import{i as r}from"./transcript-string-coerce-DTk-8HSr.mjs";import"./transcript-cjk-chars-aHwcQGAA.mjs";import"./transcript-number-coercion-D2B2TTt-.mjs";import{d as i,f as a,l as o,t as s}from"./transcript-state-dir-DEo0wD2Q.mjs";import{createRequire as c}from"node:module";import l from"node:path";import{AsyncLocalStorage as u,AsyncResource as d}from"node:async_hooks";import f from"node:fs";import p from"node:fs/promises";import m from"node:os";import{safeRealpathSync as h,safeRealpathSync as g}from"@openclaw/fs-safe/path";import{canUseRootFileOpen as _,openRootFileSync as v,resolvePathViaExistingAncestorSync as y}from"@openclaw/fs-safe/advanced";import"@openclaw/fs-safe/errors";const b=Symbol.for(`openclaw.provider-auth-persistence-error`);function isProviderAuthPersistenceError(e){try{return b in e&&e[b]===!0}catch{return!1}}function isErrorObject(e){try{return e instanceof Error||Object.prototype.toString.call(e)===`[object Error]`}catch{return!1}}function isAggregateErrorObject(e){try{if(e instanceof AggregateError)return!0;for(let t=Object.getPrototypeOf(e);t;t=Object.getPrototypeOf(t)){let e=Object.getOwnPropertyDescriptor(t,`constructor`)?.value;if(typeof e==`function`&&e.name===`AggregateError`)return!0}}catch{}return!1}function readProperty(e,t){try{return e[t]}catch{return}}function readErrorText(e,t){let n=readProperty(e,t);return typeof n==`string`?n:void 0}function formatStatusAndCode(e){if((typeof e!=`object`||!e)&&typeof e!=`function`)return;try{if(Object.keys(e).some(e=>e!==`status`&&e!==`code`))return}catch{}let t=readProperty(e,`status`),n=readProperty(e,`code`);if(t!==void 0||n!==void 0)return`status=${typeof t==`string`||typeof t==`number`?String(t):`unknown`} code=${typeof n==`string`||typeof n==`number`?String(n):`unknown`}`}function stringifyUnknown(e){if(e===null||typeof e!=`object`&&typeof e!=`function`)return String(e);try{let t=JSON.stringify(e);if(t!==void 0)return t}catch{}try{return Object.prototype.toString.call(e)}catch{return`Unknown error`}}function readErrorCauses(e){if(!isErrorObject(e))return[];let t=readProperty(e,`cause`),n=isAggregateErrorObject(e)?readProperty(e,`errors`):void 0,r=isProviderAuthPersistenceError(e)?[readProperty(e,`persistenceError`),readProperty(e,`cleanupError`)]:[],i=readErrorText(e,`name`)===`SuppressedError`?[readProperty(e,`error`),readProperty(e,`suppressed`)].map(e=>e??String(e)):[];return[...r,t||void 0,...Array.isArray(n)?n:[],...i]}function formatErrorMessage(e,t={}){let n;if(isErrorObject(e)){n=readErrorText(e,`message`)||readErrorText(e,`name`)||`Error`;let r=/* @__PURE__ */ new Set([n]),appendCauseMessage=e=>{e&&!r.has(e)&&(n+=` | ${e}`,r.add(e))};if(t.includeCode){let t=readProperty(e,`code`);(typeof t==`string`||typeof t==`number`)&&appendCauseMessage(String(t))}let i=collectErrorGraphCandidates(e,readErrorCauses);for(let e of i.slice(1))if(isErrorObject(e)){let t=readErrorText(e,`message`);t&&n.includes(t)?r.add(t):appendCauseMessage(t);let i=readProperty(e,`code`);(typeof i==`string`||typeof i==`number`)&&appendCauseMessage(String(i))}else appendCauseMessage(typeof e==`string`?e:formatStatusAndCode(e)??stringifyUnknown(e))}else n=formatStatusAndCode(e)??stringifyUnknown(e);return t.redact?t.redact(n):n}function formatErrorStackWithMessage(e){let t=readErrorText(e,`name`)||`Error`,n=readErrorText(e,`message`)??``,r=readProperty(e,`stack`);if(typeof r!=`string`||!r)return n||t;if(!n||r.includes(n))return r;let i=`${t}: ${n}`,a=r.indexOf(`
`),o=a<0?r:r.slice(0,a);return o===t?i+r.slice(o.length):`${i}\n${r}`}function toErrorObject(e,t){if(e instanceof Error)return e;if(typeof e==`string`)return Error(e);let n=Error(t,{cause:e});return(typeof e==`object`&&e||typeof e==`function`)&&Object.assign(n,e),n}function toStringifiedError(e){return e instanceof Error?e:Error(String(e))}function coerceErrorMessage(e){return e instanceof Error?e.message:String(e)}function extractErrorCode(e){if(!e||typeof e!=`object`)return;let t=readProperty(e,`code`);return typeof t==`string`||typeof t==`number`?String(t):void 0}function readErrorName(e){if(!e||typeof e!=`object`)return``;let t=e.name;return typeof t==`string`?t:``}function collectErrorGraphCandidates(e,t){if(e==null)return[];let n=[e],r=(/* @__PURE__ */ new Set()).add(e);for(let e of n)if(e&&typeof e==`object`&&t)for(let i of t(e))i!=null&&!r.has(i)&&(r.add(i),n.push(i));return n}function extractErrorCodeOrErrno(e){let t=extractErrorCode(e);if(t)return t.trim().toUpperCase();if(!e||typeof e!=`object`)return;let n=e.errno;if(typeof n==`string`&&n.trim())return n.trim().toUpperCase();if(typeof n==`number`&&Number.isFinite(n))return String(n)}function collectNestedErrorCandidates(e){return collectErrorGraphCandidates(e,e=>{let t=[readProperty(e,`cause`),readProperty(e,`reason`),readProperty(e,`original`),readProperty(e,`error`),readProperty(e,`suppressed`),readProperty(e,`data`)],n=readProperty(e,`errors`);return Array.isArray(n)&&t.push(...n),t})}function enterAsyncStore(e,t){return this.enterWith(e),t()}function runWithAsyncStore(e,t,n){return e.getStore()===t?n():new d(`OpenClawAsyncStore`).runInAsyncScope(enterAsyncStore,e,t,n)}const x=[`true`,`1`,`yes`,`on`],S=[`false`,`0`,`no`,`off`];function asBoolean(e){return typeof e==`boolean`?e:void 0}function parseBooleanValue(e,t={}){let n=asBoolean(e);if(n!==void 0)return n;let i=r(e);if(i){if((t.truthy??x).includes(i))return!0;if((t.falsy??S).includes(i))return!1}}const C=`OPENCLAW_FS_SAFE_NATIVE_MODE`,w=[`FS_SAFE_PYTHON_MODE`,`OPENCLAW_FS_SAFE_PYTHON_MODE`],T=[...w,`FS_SAFE_PYTHON`,`OPENCLAW_FS_SAFE_PYTHON`,`OPENCLAW_PINNED_PYTHON`,`OPENCLAW_PINNED_WRITE_PYTHON`],E=/* @__PURE__ */ new WeakMap;let D=!1;function nativeModeIsValid(e){let t=e?.trim().toLowerCase()??``;return/^(?:0|false|off|never|1|true|on|auto|required|require)$/u.test(t)}function currentFallback(e){let t=E.get(e);return t&&e[t.key]===t.value?t:void 0}function fsSafeEnvInput(e){let t=currentFallback(e);if(!t)return e;let n={...e,[t.key]:t.previous};return t.existed||delete n[t.key],n}function clearFsSafeEnvFallback(e){let t=currentFallback(e);E.delete(e),t&&(t.existed?e[t.key]=t.previous:delete e[t.key])}function normalizeFsSafeNativeEnv(e=process.env){clearFsSafeEnvFallback(e);let t=T.filter(t=>e[t]!==void 0);if(t.length&&e===process.env&&!D&&(D=!0,process.emitWarning(`${t.join(`, `)} is deprecated. OpenClaw maps legacy mode values to native mode only when neither FS_SAFE_NATIVE_MODE nor OPENCLAW_FS_SAFE_NATIVE_MODE selects a mode. Replace Python mode variables with native mode variables; interpreter path settings are ignored.`,{code:`FS_SAFE_PYTHON_DEPRECATED`,type:`DeprecationWarning`})),nativeModeIsValid(e.FS_SAFE_NATIVE_MODE)||nativeModeIsValid(e[C]))return;let n=w.map(t=>e[t]).find(e=>e!==void 0);if(n===void 0)return;let r=Object.keys(e),i=!r.includes(C)&&Object.hasOwn(e,C)?r.find(e=>e.toUpperCase()===C)??C:C,a=Object.hasOwn(e,i),o=e[i];e[i]=n,E.set(e,{key:i,value:n,previous:o,existed:a})}const O=[[`ZAI_API_KEY`,`Z_AI_API_KEY`]];function normalizeZaiEnv(e=process.env){!e.ZAI_API_KEY?.trim()&&e.Z_AI_API_KEY?.trim()&&(e.ZAI_API_KEY=e.Z_AI_API_KEY)}function expandEnvNormalizationKeys(e){let t=/* @__PURE__ */ new Set;for(let n of e)for(let e of resolveEnvNormalizationKeys(n))t.add(e);return t}function resolveEnvNormalizationKeys(e){let t=process.platform===`win32`?e.toUpperCase():e;return O.find(e=>e.some(e=>e===t))??[t]}function isTruthyEnvValue(e){return parseBooleanValue(e)===!0}function getSealedRuntimeSecureTempRoot(){}let k;function isJson5Parser(e){return typeof e==`object`&&!!e&&`parse`in e&&typeof e.parse==`function`}function setJson5Runtime(e){let t=isJson5Parser(e)?e:typeof e==`object`&&e&&`default`in e?e.default:void 0;if(!isJson5Parser(t))throw Error(`json5 parser unavailable`);return k=t,t}function loadJson5Parser(){if(k)return k;if(typeof SEALED_RUNTIME_BUILD==`boolean`&&SEALED_RUNTIME_BUILD)throw Error(`sealed JSON5 runtime was not registered before use`);return setJson5Runtime(c(import.meta.url)(`json5`))}function parseJsonWithJson5Fallback(e,t){try{return JSON.parse(e)}catch{return(t??loadJson5Parser()).parse(e)}}function expectDefined(e,t){if(e==null)throw Error(`expected `+t+` to be defined`);return e}function isHighSurrogate(e){return e>=55296&&e<=56319}function isLowSurrogate(e){return e>=56320&&e<=57343}function avoidTrailingHighSurrogateBreak(e,t,n){if(n<=t||n>=e.length||!isHighSurrogate(e.charCodeAt(n-1))||!isLowSurrogate(e.charCodeAt(n)))return n;let r=n-1;return r>t?r:n+1}function sliceUtf16Safe(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);return a<=i?``:(i>0&&i<r&&isLowSurrogate(e.charCodeAt(i))&&isHighSurrogate(e.charCodeAt(i-1))&&(i+=1),a>0&&a<r&&isHighSurrogate(e.charCodeAt(a-1))&&isLowSurrogate(e.charCodeAt(a))&&--a,e.slice(i,a))}function truncateUtf16Safe(e,t){let n=Math.max(0,Math.floor(t));return e.length<=n?e:sliceUtf16Safe(e,0,n)}function realpathExistingAncestorSync(e){let t=[],n=e;for(;;)try{return l.join(f.realpathSync.native(n),...t.toReversed())}catch{let e=l.dirname(n);if(e===n)return;t.push(l.basename(n)),n=e}}async function realpathExistingAncestor(e){let t=[],n=e;for(;;)try{return l.join(await p.realpath(n),...t.toReversed())}catch{let e=l.dirname(n);if(e===n)return;t.push(l.basename(n)),n=e}}function canAccessSync(e,t){try{return f.accessSync(e,t),!0}catch{return!1}}const A=u.snapshot();function resolvePathViaExistingAncestorSync$1(e){return A(y,e)}function resolveRealpathOrAbsolute(e){return h(e)??l.resolve(e)}function resolveIdentityPathViaExistingAncestorSync(e){let t=l.resolve(e);return realpathExistingAncestorSync(t)??t}async function resolveIdentityPathViaExistingAncestor(e){let t=l.resolve(e);return await realpathExistingAncestor(t)??t}const j=`(?:\\x1b\\]|\\x9d)`,M=`(?:\\x1b\\\\|\\x07|\\x9c)`,N=RegExp(`${j}[^\\x07\\x1b\\x9c]*${M}`,`y`);function matchAnsiOscAt(e,t){return N.lastIndex=t,N.exec(e)?.[0]}function scanAnsiCsiAt(e,t){let n=e.charCodeAt(t);if(n!==155&&(n!==27||e.charCodeAt(t+1)!==91))return;let r=t+(n===155?1:2),i=[],a=!1;for(;r<e.length;){let t=e.charCodeAt(r);if(t===24||t===26){r+=1,a=!0;break}if(t===27||t===155){a=!0;break}if(t<=31||t===127)i.push(e.charAt(r)),r+=1;else if(t>=32&&t<=63)r+=1;else{t>=64&&t<=126&&(r+=1),a=!0;break}}return{controls:i,ended:a,value:e.slice(t,r)}}const P=RegExp(`${`${j}[\\s\\S]*?${M}`}|[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]`,`y`);new Intl.Segmenter(void 0,{granularity:`grapheme`});function hasAnsiIntroducer(e){return e.includes(`\x1B`)||e.includes(``)||e.includes(``)}function stripAnsiInternal(e,t){if(!hasAnsiIntroducer(e))return e;let n=[],r=0,i=0;for(;i<e.length;){let a=e.charCodeAt(i);if(a!==27&&a!==155&&a!==157){i+=1;continue}let o=matchAnsiOscAt(e,i),s=o?void 0:scanAnsiCsiAt(e,i);P.lastIndex=i;let c=!o&&t.compatibilityGrammar?P.exec(e)?.[0]:void 0,l=o?.length??s?.value.length??c?.length;if(l===void 0)i+=1;else{if(s&&!s.ended&&t.preserveIncompleteCsi)break;s?.controls.length===0&&c&&(l=Math.max(l,c.length)),n.push(e.slice(r,i),...s?.controls??[]),i+=l,r=i}}return n.push(e.slice(r)),n.join(``)}function stripAnsi(e){return stripAnsiInternal(e,{compatibilityGrammar:!1})}const F=/* @__PURE__ */ RegExp(`[\0--]`,`g`);function sanitizeForLog(e){return stripAnsi(e).replace(F,``)}function sanitizeTerminalText(e){return stripAnsi(e).replace(/[\p{Cc}\p{Bidi_Control}]/gu,e=>e===`\r`?`\\r`:e===`
`?`\\n`:e===` `?`\\t`:``)}function resolveConfigDir(e=process.env,t=m.homedir){let n=e.OPENCLAW_STATE_DIR?.trim();if(n)return o(n,e,t);let r=e.OPENCLAW_CONFIG_PATH?.trim();return r?l.dirname(o(r,e,t)):s(e,t)}function shortenPathWithHome(e,{home:r,prefix:i}){if(e===r)return i;if(e.startsWith(`${r}/`)||process.platform===`win32`&&e.startsWith(`${r}\\`))return`${i}${e.slice(r.length)}`;if(process.platform===`win32`&&l.win32.isAbsolute(e)&&n(r,e)){let n=l.win32.relative(t(r),t(e));return l.win32.join(i,n)}return e}function escapeRegExp(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function isAbortError(e){if(!e||typeof e!=`object`)return!1;try{return(`name`in e?String(e.name):``)===`AbortError`||(`message`in e&&typeof e.message==`string`?e.message:``)===`This operation was aborted`}catch{return!1}}function resolveHomeDir(){return a(process.env,m.homedir)}const I=String.raw`(?<=^|[\s"'\x60(\[{<=:;]|file://)`,L=String.raw`[\s"'\x60)\]}>]`,R=String.raw`[:;](?=[/\\~$]|[A-Za-z]:)`;function resolveHomeDisplayPrefix(){let e=resolveHomeDir();if(e)return i(process.env.OPENCLAW_HOME)?{home:e,prefix:`$OPENCLAW_HOME`}:{home:e,prefix:`~`}}function shortenHomePath(e){let t=resolveHomeDisplayPrefix();return t?shortenPathWithHome(e,t):e}function shortenHomeInString(e){if(!e)return e;let t=resolveHomeDisplayPrefix();if(!t||l.parse(t.home).root===t.home)return e;let n=process.platform===`win32`?String.raw`[\\/]`:`/`,r=RegExp(`${I}${escapeRegExp(t.home)}(?=$|${n}|${L}|[.,;:](?:$|${L})|${R})`,process.platform===`win32`?`giu`:`gu`);return e.replace(r,t.prefix)}resolveConfigDir();var z=class InvocationFrame{constructor(e,t=e.invocation){this.retentionOwner=e.retentionOwner,this.invocation=t,this.metadataScope=e.metadataScope,this.cacheScope=e.cacheScope,this.sourceCaptureStorage=e.sourceCaptureStorage}get runtimePluginId(){}withScopes(e){return new InvocationFrame(e)}};function createPluginExecutionFrame(e,t){return t?t.withScopes(e):new z(e)}const B=e(Symbol.for(`openclaw.pluginInstanceInvocation`),()=>{let e=new u;return{invocation:{getStore(){return e.getStore()?.invocation},run(t,n){let r=e.getStore();return r?.invocation===t?n():runWithAsyncStore(e,createPluginExecutionFrame({...r,invocation:t},r),n)},exit(t){let n=e.getStore();return n?.invocation?runWithAsyncStore(e,n.withScopes({...n,invocation:void 0}),t):t()}},getFrame(){return e.getStore()},runFrame(t,n){return runWithAsyncStore(e,t,n)}}});B.invocation;const V=B.getFrame;B.runFrame;export{normalizeFsSafeNativeEnv as A,toErrorObject as B,getSealedRuntimeSecureTempRoot as C,resolveEnvNormalizationKeys as D,normalizeZaiEnv as E,extractErrorCode as F,extractErrorCodeOrErrno as I,formatErrorMessage as L,coerceErrorMessage as M,collectErrorGraphCandidates as N,clearFsSafeEnvFallback as O,collectNestedErrorCandidates as P,formatErrorStackWithMessage as R,parseJsonWithJson5Fallback as S,isTruthyEnvValue as T,toStringifiedError as V,v as _,isAbortError as a,truncateUtf16Safe as b,sanitizeTerminalText as c,resolveIdentityPathViaExistingAncestorSync as d,resolvePathViaExistingAncestorSync$1 as f,_ as g,canAccessSync as h,shortenHomePath as i,runWithAsyncStore as j,fsSafeEnvInput as k,sanitizeForLog as l,g as m,V as n,escapeRegExp as o,resolveRealpathOrAbsolute as p,shortenHomeInString as r,resolveConfigDir as s,z as t,resolveIdentityPathViaExistingAncestor as u,avoidTrailingHighSurrogateBreak as v,expandEnvNormalizationKeys as w,expectDefined as x,sliceUtf16Safe as y,readErrorName as z};