UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 13.1 kB
import{a as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{n as t}from"./transcript-global-singleton-DIJgobpU.mjs";import{d as n}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import"./transcript-path-guards-u0N7Z9_x.mjs";import{a as r,i,n as a}from"./transcript-string-coerce-DTk-8HSr.mjs";import{i as o,l as s,r as c,s as l,t as u}from"./transcript-state-dir-DEo0wD2Q.mjs";import"./transcript-paths-BSkqmV9_.mjs";import{n as d,r as f}from"./transcript-agent-id-DmscQLD6.mjs";import{n as p}from"./transcript-session-key-normalization-3Mbu27cw.mjs";import{n as m,t as h}from"./transcript-session-key-MDDhuEG9.mjs";import{n as g}from"./transcript-immutable-data-Dv9CHHEc.mjs";import{a as _}from"./transcript-paths-CuGliRzI.mjs";import{t as v}from"./transcript-owned-directory-observation-CHuJ-3CT.mjs";import{D as y}from"./transcript-openclaw-agent-db-resources-W6NYYBYU.mjs";import{isMainThread as b}from"node:worker_threads";import x from"node:path";import S from"node:fs";import C from"node:os";import{probePathCaseInsensitiveSync as w,resolvePathPrefixSync as T,sameFileIdentity as E}from"@openclaw/fs-safe/advanced";const D=/^(?:pnpm|npm|bunx|npx)\s+openclaw\b|^openclaw\b/,O=/(?:^|\s)--container(?:\s|=|$)/,k=/(?:^|\s)--profile(?:\s|=|$)/,A=/(?:^|\s)--dev(?:\s|$)/,j=/^(?:\s+--(?:dev|no-color|(?:profile|log-level)[=\s]+\S+))*\s+update(?:\s|$)/,M=/^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,127}$/;function formatCliCommand(e,t=process.env){let n=t.OPENCLAW_CONTAINER_HINT?.trim(),r=n&&M.test(n)?n:void 0,i=c(t.OPENCLAW_PROFILE);if(!r&&!i||!D.test(e))return e;let a;return r&&!O.test(e)&&!j.test(e.replace(D,``))&&(a=`--container ${r}`),!r&&i&&!k.test(e)&&!A.test(e)&&(a=`--profile ${i}`),a?e.replace(D,e=>`${e} ${a}`):e}function hasExplicitModelPolicyAllow(t){return e(t)&&Object.hasOwn(t,`allow`)}const N={},P=[{id:`openclaw`,kind:`system`},{id:`crestodian`,kind:`system`}],F=new Set(P.map(e=>d(e.id)));function isReservedSystemAgentId(e){return F.has(d(e))}let I;const L=/* @__PURE__ */ new WeakMap;function withAgentRosterFactsBatch(e,t){let n=I;I=n?.config===e?n:{config:e,facts:readAgentRosterFacts(e)??{}};try{return t()}finally{I=n}}function readAgentRosterFacts(e){if(I?.config===e)return I.facts;let t=N.current,n=t?.config===e?t.revision:void 0;if(n===void 0&&!g(e))return;let r=L.get(e);return(!r||r.publication!==n)&&(r={publication:n,facts:{}},L.set(e,r)),r.facts}function collectAgentEntries(t,n,r){let i=readAgentRosterProperty(t);if(i?.kind===`entries`&&e(i.value)){let t=[];for(let a in i.value){if(!Object.hasOwn(i.value,a))continue;let o=i.value[a];if(e(o)){let e={...o,id:a};if(t.push(n?{entry:e,source:{kind:`entries`,key:a}}:e),t.length===r)break}}return t}if(i?.kind!==`list`||!Array.isArray(i.value))return[];let a=[];return i.value.some((e,t)=>(typeof e==`object`&&e&&a.push({entry:e,source:{kind:`list`,index:t}}),a.length===r)),n?a:a.map(({entry:e})=>e)}function listAgentEntriesWithSource(e){return collectAgentEntries(e,!0)}function listAgentEntries(e){return collectAgentEntries(e,!1)}function readAgentRosterProperty(t){if(!e(t))return;let n=t.agents;if(e(n))for(let e of[`entries`,`list`]){let t=n[e];if(Object.hasOwn(n,e)&&t!==void 0)return{kind:e,value:t}}}function hasAgentRosterProperty(e){return readAgentRosterProperty(e)!==void 0}function listAgentIds(e){let t=readAgentRosterFacts(e);if(t?.agentIds)return t.agentIds;let n=listAgentEntries(e),r=n.length===0&&!hasAgentRosterProperty(e)?[h]:[...new Set(n.map(e=>d(e?.id)))];return t&&(Object.freeze(r),t.agentIds=r),r}function listGatewayOwnedAgentIds(e){return[.../* @__PURE__ */ new Set([...listAgentIds(e),...P.map(({id:e})=>e)])]}function tryResolveSoleAgentId(e){let t=collectAgentEntries(e,!1,2);return t.length===0?hasAgentRosterProperty(e)?void 0:h:t.length===1?d(t[0].id):void 0}function tryResolveRawLegacyDefaultAgentId(e){if(e.agents?.ownership===`explicit`)return;let t=collectAgentEntries(e,!1).filter(e=>e.default===!0);return t.length===1?d(t[0].id):void 0}function tryResolveDefaultAgentId(e){return tryResolveRawLegacyDefaultAgentId(e)??tryResolveSoleAgentId(e)}function tryResolveLegacyDataOwner(e){return tryResolveDefaultAgentId(e)}function resolveMissingStorePathIdentity(e){try{let t=T(x.resolve(e));return x.resolve(t.existingPath,...t.unresolvedSegments)}catch{return}}function isPerAgentSessionStoreConfig(e){return!e?.trim()||e.includes(`{agentId}`)}function isSameFixedSessionStoreConfig(e,t,n){if(isPerAgentSessionStoreConfig(e)||isPerAgentSessionStoreConfig(t))return!1;let r=x.resolve(_(e,{env:n})),i=x.resolve(_(t,{env:n}));if(r===i)return!0;try{return E(S.statSync(r,{bigint:!0}),S.statSync(i,{bigint:!0}))}catch(e){let t=e.code;if(t!==`ENOENT`&&t!==`ENOTDIR`)return!0}let a=resolveMissingStorePathIdentity(r),o=resolveMissingStorePathIdentity(i);if(!a||!o||a===o)return!0;if(a.toLowerCase()!==o.toLowerCase())return!1;let s=w(a),c=w(o);return s!==!1&&c!==!1}function resolvePersistedSessionStoreOwner(e){if(isPerAgentSessionStoreConfig(e.session?.store))return{kind:`none`};let t=e.agents?.defaults?.sessionStore?.agentId?.trim();if(!t)return{kind:`none`};let n=d(t);return listAgentIds(e).some(e=>d(e)===n)?{kind:`configured`,agentId:n}:{kind:`retired`,agentId:n}}function resolvePersistedSessionStoreOwnerForKey(e,t){return m(t)===`legacy_or_alias`?resolvePersistedSessionStoreOwner(e):{kind:`none`}}function resolvePersistedSessionStoreOwnerForTarget(e){let t=resolvePersistedSessionStoreOwnerForKey(e.config,e.sessionKey);return t.kind===`none`||!e.storePath||isSameFixedSessionStoreConfig(e.config.session?.store,e.storePath,e.env??process.env)?t:{kind:`none`}}const R=/* @__PURE__ */ new Map,z=t(Symbol.for(`openclaw.agentDirPreparedPaths`),e=>{for(let t of e.values())t.close();e.clear()},`shared-state`);function registryPath(e,t,r=!1,i){let a=x.resolve(s(e,t)),o=y()?.ownsSchemaMaintenance===!0,c=o?void 0:z.get(a);if(c)return c.key;let l=i??normalizeAgentDirRegistryPath(a,t);if(o||!b||!r&&!R.has(l))return l;let u=[],d={key:l,close:()=>u.forEach(e=>e.close())};z.set(a,d);let f=!1;for(let e=a;x.dirname(e)!==e;e=x.dirname(e)){let t=x.basename(e);u.push(v(x.dirname(e),e=>{if(!f&&(e===void 0||e===t)){if(f=!0,queueMicrotask(()=>{f=!1}),n(a)===l)return;z.delete(a),d.close()}}))}let p=Math.max(256,R.size+1);for(let[e,t]of z){if(z.size<=p)break;z.delete(e),t.close()}return l}function normalizeAgentDirRegistryPath(e,t=process.env){return n(s(e,t))}function registerResolvedAgentDir(e){let t=registryPath(e.agentDir,e.env,!0),n=R.get(t)??/* @__PURE__ */ new Set,r=d(e.agentId);return!n.has(r)&&(n.add(r),R.set(t,n),!0)}function resolveDefaultAgentWorkspaceDir(e=process.env,t=C.homedir){let n=e.OPENCLAW_WORKSPACE_DIR?.trim();if(n)return x.resolve(n);if(e.OPENCLAW_STATE_DIR?.trim())return x.join(u(e,t),`workspace`);let r=l(e,t),a=e.OPENCLAW_PROFILE?.trim();return a&&i(a)!=="default"?x.join(o(a,e,t),`workspace`):x.join(r,`.openclaw`,`workspace`)}resolveDefaultAgentWorkspaceDir();var AgentSelectionRequiredError=class extends Error{constructor(e,t){let n=t?.surface??`this operation`,r=t?.hint??`Select an agent explicitly; CLI callers can pass --agent <id>, channels can add a binding, and ambient services can set their agentId target.`;super(`Multiple agents are configured, but ${n} has no explicit owner. ${r}`),this.code=`AGENT_SELECTION_REQUIRED`,this.name=`AgentSelectionRequiredError`,this.agentIds=e,this.hint=r}};function resolveConfiguredAgentId(e,t){if(!listAgentIds(e).includes(t))throw Error(`Unknown agent id "${t}". Run ${formatCliCommand(`openclaw agents list`)} to see configured agents.`);return t}function resolveSoleAgentId(e,t){let n=tryResolveSoleAgentId(e);if(n)return n;let r=listAgentIds(e);throw r.length===0?Error("No agents configured. Run `openclaw onboard` or `openclaw agents add` first."):new AgentSelectionRequiredError(r,t)}function tryResolveLegacyDataOwnerAgentId(e){let t=readAgentRosterFacts(e);if(t?.legacyDataOwnerAgentId)return t.legacyDataOwnerAgentId.value;let n=tryResolveLegacyDataOwner(e);return t&&(t.legacyDataOwnerAgentId={value:n}),n}function tryResolveLegacyCompatibilityAgentId(e){let t=readAgentRosterFacts(e);if(t?.compatibilityAgentId)return t.compatibilityAgentId.value;let n;if(e.agents?.ownership===`explicit`){let t=r(e.agents.defaults?.systemAgent?.agentId),i=t?d(t):void 0;n=i&&listAgentIds(e).includes(i)?i:void 0}else n=tryResolveLegacyDataOwnerAgentId(e);return t&&(t.compatibilityAgentId={value:n}),n}function tryResolveAmbientOwnerAgentId(e,t){let n=r(t)??r(e.agents?.defaults?.systemAgent?.agentId);return n?d(n):tryResolveLegacyCompatibilityAgentId(e)??tryResolveSoleAgentId(e)}function resolveDefaultAgentId(e,t){return tryResolveRawLegacyDefaultAgentId(e)??resolveSoleAgentId(e,t)}const B={surface:`session agent resolution`,hint:`Pass an agentId, an agent-scoped session key, or a prepared fallbackAgentId.`};function resolveSelectedSessionAgentId(e){if(m(e.sessionKey)===`malformed_agent`)throw Error(`Malformed agent session key; refusing default-agent resolution.`);let t=e.agentId===void 0?null:f(e.agentId);if(t&&!t.ok)throw Error(`Invalid explicit agent id; refusing default-agent resolution.`);let n=t?.value,r=a(e.fallbackAgentId),i=r?d(r):null,o=e.sessionKey?.trim(),s=p(o),c=s?.agentId?d(s.agentId):null,l=e.config??{},u=resolvePersistedSessionStoreOwnerForKey(l,o);if(c&&n&&n!==c)throw new AgentSelectionRequiredError(listAgentIds(l),{surface:`session agent resolution`,hint:`The agent-scoped session key belongs to "${c}", not "${n}".`});let h=n??i;if(!c&&u.kind===`retired`)throw new AgentSelectionRequiredError(listAgentIds(l),{surface:`session agent resolution`,hint:`The shared fixed-store row belongs to retired agent "${u.agentId}".`});if(!c&&u.kind===`configured`&&h&&h!==u.agentId)throw new AgentSelectionRequiredError(listAgentIds(l),{surface:`session agent resolution`,hint:`The shared fixed-store row belongs to "${u.agentId}", not "${h}".`});return c??(u.kind===`configured`?u.agentId:void 0)??h??void 0}function resolveSessionAgentIdStrict(e){let t=resolveSelectedSessionAgentId(e),n=e.config??{};return t??tryResolveLegacyDataOwnerAgentId(n)??resolveDefaultAgentId(n,B)}const V=resolveSessionAgentIdStrict;function resolveAgentEntry(t,n){let r=d(n),i=readAgentRosterFacts(t);if(i){let e=(i.entryByNormalizedId??=buildAgentEntryIndex(t)).get(r);return e?e.clone?{...e.entry}:e.entry:void 0}let a=readAgentRosterProperty(t);if(a?.kind===`entries`&&e(a.value)){let t=a.value;for(let n in t){if(!Object.hasOwn(t,n))continue;let i=t[n];if(e(i)&&d(n)===r)return{...i,id:n}}}else if(a?.kind===`list`&&Array.isArray(a.value))return a.value.find(e=>typeof e==`object`&&!!e&&d(e.id)===r)}function buildAgentEntryIndex(e){let t=/* @__PURE__ */ new Map;for(let{entry:n,source:r}of listAgentEntriesWithSource(e)){let e=d(n?.id);t.has(e)||t.set(e,{clone:r.kind===`entries`,entry:n})}return t}function resolveAgentConfig(e,t){let n=d(t),r=resolveAgentEntry(e,n)??(!hasAgentRosterProperty(e)&&n===`main`?{id:n}:void 0);if(!r)return;let i=e.agents?.defaults;return{name:r.name,workspace:r.workspace,agentDir:r.agentDir,model:r.model,...r.models?{models:r.models}:{},...r.params?{params:r.params}:{},...r.runtime?{runtime:r.runtime}:{},...hasExplicitModelPolicyAllow(r.modelPolicy)?{modelPolicy:r.modelPolicy}:{},utilityModel:r.utilityModel,decisionModel:r.decisionModel,thinkingDefault:r.thinkingDefault,verboseDefault:r.verboseDefault??i?.verboseDefault,toolProgressDetail:r.toolProgressDetail??i?.toolProgressDetail,reasoningDefault:r.reasoningDefault,fastModeDefault:r.fastModeDefault??i?.fastModeDefault,contextInjection:r.contextInjection,bootstrapMaxChars:r.bootstrapMaxChars,bootstrapTotalMaxChars:r.bootstrapTotalMaxChars,experimental:r.experimental?{...i?.experimental,...r.experimental}:i?.experimental,skills:r.skills,memory:r.memory,humanDelay:r.humanDelay,typingMode:r.typingMode??i?.typingMode,tts:r.tts,...r.compaction?{compaction:r.compaction}:{},contextLimits:r.contextLimits?{...i?.contextLimits,...r.contextLimits}:i?.contextLimits,heartbeat:r.heartbeat,identity:r.identity,groupChat:r.groupChat,subagents:r.subagents,embeddedAgent:r.embeddedAgent,sandbox:r.sandbox,tools:r.tools}}function resolveEffectiveAgentDir(e,t,n){let r=d(t),i=resolveAgentConfig(e,r)?.agentDir?.trim(),a=n?.env??process.env;return i?s(i,a,n?.homedir):x.join(u(a,n?.homedir),`agents`,r,`agent`)}function resolveAgentDir(e,t,n=process.env){let r=resolveEffectiveAgentDir(e,t,{env:n});return registerResolvedAgentDir({agentId:t,agentDir:r,env:n}),r}export{readAgentRosterProperty as _,resolveEffectiveAgentDir as a,N as b,tryResolveLegacyCompatibilityAgentId as c,resolvePersistedSessionStoreOwner as d,resolvePersistedSessionStoreOwnerForTarget as f,listGatewayOwnedAgentIds as g,listAgentIds as h,resolveDefaultAgentId as i,tryResolveLegacyDataOwnerAgentId as l,listAgentEntries as m,resolveAgentEntry as n,V as o,isPerAgentSessionStoreConfig as p,resolveConfiguredAgentId as r,tryResolveAmbientOwnerAgentId as s,resolveAgentDir as t,normalizeAgentDirRegistryPath as u,withAgentRosterFactsBatch as v,formatCliCommand as x,isReservedSystemAgentId as y};