UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 16.6 kB
import{_ as resolvePrimaryStringValue,l as normalizeOptionalString,o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{i as resolveGlobalMap}from"./global-singleton-DolL9xGt.mjs";import{c as resolveUserPath}from"./home-dir-DEN5DisQ.mjs";import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import"./utils-Dlk0IYAa.mjs";import"./paths-D_FOvGon.mjs";import{n as normalizeAgentId,r as normalizeAgentIdStrict}from"./agent-id-cOfqmfln.mjs";import{N as parseAgentSessionKey,n as LEGACY_IMPLICIT_AGENT_ID,o as classifySessionKeyShape}from"./session-key-BPgKfU2m.mjs";import{t as formatCliCommand}from"./command-format-D7KhNLP0.mjs";import{n as hasExplicitModelPolicyAllow}from"./model-policy-allowlist-migration-BI8Uf3RI.mjs";import{d as tryResolveLegacyDataOwner,f as tryResolveRawLegacyDefaultAgentId,i as listAgentIds,o as readAgentRosterFacts,p as tryResolveSoleAgentId,r as listAgentEntriesWithSource,s as readAgentRosterProperty,t as hasAgentRosterProperty}from"./agent-roster-4XrgUgPK.mjs";import{r as isSameFixedSessionStoreConfig,t as isPerAgentSessionStoreConfig}from"./session-store-config-Ck41Fpqm.mjs";import{t as observeOwnedDirectory}from"./owned-directory-observation-BDuOMAKd.mjs";import{d as resolveIdentityPathViaExistingAncestor,f as resolveIdentityPathViaExistingAncestorSync}from"./path-existence-BR9MXlgL.mjs";import{a as getOpenClawDatabaseMaintenanceScope}from"./openclaw-state-maintenance-context-D-pYfco8.mjs";import{t as resolveDefaultAgentWorkspaceDir}from"./workspace-default-path-ULYbn-EL.mjs";import"./workspace-default-Cb7cQoAN.mjs";import path from"node:path";import{isMainThread}from"node:worker_threads"; //!src/config/sessions/session-store-owner.ts function resolvePersistedSessionStoreOwner(e){if(isPerAgentSessionStoreConfig(e.session?.store))return{kind:`none`};const t=e.agents?.defaults?.sessionStore?.agentId?.trim();if(!t)return{kind:`none`};const n=normalizeAgentId(t);return listAgentIds(e).some(e=>normalizeAgentId(e)===n)?{kind:`configured`,agentId:n}:{kind:`retired`,agentId:n}}function resolvePersistedSessionStoreOwnerForKey(e,t){return classifySessionKeyShape(t)===`legacy_or_alias`?resolvePersistedSessionStoreOwner(e):{kind:`none`}}function resolvePersistedSessionStoreOwnerForTarget(e){const t=resolvePersistedSessionStoreOwnerForKey(e.config,e.sessionKey);if(t.kind===`none`||!e.storePath)return t;return isSameFixedSessionStoreConfig(e.config.session?.store,e.storePath,e.env??process.env)?t:{kind:`none`}} //!src/agents/agent-dir-registry.ts const agentIdsByDir=/* @__PURE__ */ new Map;const preparedPaths=resolveGlobalMap(Symbol.for(`openclaw.agentDirPreparedPaths`),e=>{for(const t of e.values())t.close();e.clear()},`shared-state`);function forgetPreparedPaths(e){for(const[t,n]of preparedPaths)if(n.key===e){preparedPaths.delete(t);n.close()}}function registryPath(e,t,n=false,r){const i=path.resolve(resolveUserPath(e,t));const a=getOpenClawDatabaseMaintenanceScope()?.ownsSchemaMaintenance===true;const o=a?void 0:preparedPaths.get(i);if(o)return o.key;const s=r??normalizeAgentDirRegistryPath(i,t);if(a||!isMainThread||!n&&!agentIdsByDir.has(s))return s;const c=[];const l={key:s,close:()=>c.forEach(e=>e.close())};preparedPaths.set(i,l);let u=false;for(let e=i;path.dirname(e)!==e;e=path.dirname(e)){const t=path.basename(e);c.push(observeOwnedDirectory(path.dirname(e),e=>{if(!u&&(e===void 0||e===t)){u=true;resolveIdentityPathViaExistingAncestor(i).then(e=>{if(preparedPaths.get(i)===l&&e!==s){preparedPaths.delete(i);l.close()}}).catch(()=>{}).finally(()=>{u=false})}}))}const d=Math.max(256,agentIdsByDir.size+1);for(const[e,t]of preparedPaths){if(preparedPaths.size<=d)break;preparedPaths.delete(e);t.close()}return s}function normalizeAgentDirRegistryPath(e,t=process.env){return resolveIdentityPathViaExistingAncestorSync(resolveUserPath(e,t))}function registerResolvedAgentDir(e){const t=registryPath(e.agentDir,e.env,true);const n=agentIdsByDir.get(t)??/* @__PURE__ */ new Set;const r=normalizeAgentId(e.agentId);if(n.has(r))return false;n.add(r);agentIdsByDir.set(t,n);return true}function unregisterResolvedAgentDir(e){const t=normalizeAgentDirRegistryPath(e.agentDir,e.env);const n=agentIdsByDir.get(t);if(!n?.delete(normalizeAgentId(e.agentId)))return false;if(n.size===0){agentIdsByDir.delete(t);forgetPreparedPaths(t)}return true}function resolveRegisteredAgentIdForDir(e,t){const n=agentIdsByDir.get(registryPath(e,t));return n?.size===1?n.values().next().value:void 0}async function resolveRegisteredAgentIdForDirAsync(e,t){const n=path.resolve(resolveUserPath(e,t));const r=(getOpenClawDatabaseMaintenanceScope()?.ownsSchemaMaintenance===true?void 0:preparedPaths.get(n))?.key??await resolveIdentityPathViaExistingAncestor(n);const i=agentIdsByDir.get(registryPath(n,t,false,r));return i?.size===1?i.values().next().value:void 0}function isPathOwnedByAnotherRegisteredAgent(e){const t=normalizeAgentDirRegistryPath(e.pathname,e.env);const n=normalizeAgentId(e.agentId);for(const[e,r]of agentIdsByDir)if([...r].some(e=>e!==n)&&(e===t||isPathInside(e,t)||isPathInside(t,e)))return true;return false} //!src/agents/agent-scope-config.ts var AgentSelectionRequiredError=class extends Error{constructor(e,t){const n=t?.surface??`this operation`;const 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 resolveAgentModelConfigForRuntime(e,t=`native`){const n=e?.model;if(t===`acp`||e?.runtime?.type!==`acp`)return n;return n&&typeof n===`object`&&n.fallbacks?{fallbacks:n.fallbacks}:void 0}function resolveAgentNativeModelPrimary(e,t){return resolvePrimaryStringValue(resolveAgentModelConfigForRuntime(resolveAgentConfig(e,t)))}function resolveNativeModelPrimary(e,t){return resolveAgentNativeModelPrimary(e,t)??resolvePrimaryStringValue(e.agents?.defaults?.model)}function stripNullBytes(e){return e.replaceAll(`\0`,``)}function toAgentEntriesRecord(e){return Object.fromEntries(e.map(e=>{const{id:t,...n}=e;return[t,n]}))}function resolveConfiguredAgentId(e,t){if(!listAgentIds(e).includes(t))throw new Error(`Unknown agent id "${t}". Run ${formatCliCommand(`openclaw agents list`)} to see configured agents.`);return t}function resolveSoleAgentId(e,t){const n=tryResolveSoleAgentId(e);if(n)return n;const r=listAgentIds(e);if(r.length===0)throw new Error("No agents configured. Run `openclaw onboard` or `openclaw agents add` first.");throw new AgentSelectionRequiredError(r,t)}function tryResolveLegacyDataOwnerAgentId(e){const t=readAgentRosterFacts(e);if(t?.legacyDataOwnerAgentId)return t.legacyDataOwnerAgentId.value;const n=tryResolveLegacyDataOwner(e);if(t)t.legacyDataOwnerAgentId={value:n};return n}function tryResolveLegacyCompatibilityAgentId(e){const t=readAgentRosterFacts(e);if(t?.compatibilityAgentId)return t.compatibilityAgentId.value;let n;if(e.agents?.ownership===`explicit`){const t=normalizeOptionalString(e.agents.defaults?.systemAgent?.agentId);const r=t?normalizeAgentId(t):void 0;n=r&&listAgentIds(e).includes(r)?r:void 0}else n=tryResolveLegacyDataOwnerAgentId(e);if(t)t.compatibilityAgentId={value:n};return n}function tryResolveAmbientOwnerAgentId(e,t){const n=normalizeOptionalString(t)??normalizeOptionalString(e.agents?.defaults?.systemAgent?.agentId);return n?normalizeAgentId(n):tryResolveLegacyCompatibilityAgentId(e)??tryResolveSoleAgentId(e)}function resolveAmbientOwnerAgentId(e,t,n){return tryResolveAmbientOwnerAgentId(e,t)??resolveSoleAgentId(e,n)}function tryResolveAgentOperationAgentId(e,t){if(t!==void 0)return tryResolveAmbientOwnerAgentId(e,t);return tryResolveLegacyCompatibilityAgentId(e)??tryResolveSoleAgentId(e)}function resolveAgentOperationAgentId(e,t,n){return tryResolveAgentOperationAgentId(e,t)??resolveSoleAgentId(e,n)}function resolveDefaultAgentId(e,t){return tryResolveRawLegacyDefaultAgentId(e)??resolveSoleAgentId(e,t)}const SESSION_AGENT_SELECTION_CONTEXT={surface:`session agent resolution`,hint:`Pass an agentId, an agent-scoped session key, or a prepared fallbackAgentId.`};function resolveSelectedSessionAgentId(e){if(classifySessionKeyShape(e.sessionKey)===`malformed_agent`)throw new Error(`Malformed agent session key; refusing default-agent resolution.`);const t=e.agentId===void 0?null:normalizeAgentIdStrict(e.agentId);if(t&&!t.ok)throw new Error(`Invalid explicit agent id; refusing default-agent resolution.`);const n=t?.value;const r=normalizeLowercaseStringOrEmpty(e.fallbackAgentId);const i=r?normalizeAgentId(r):null;const a=e.sessionKey?.trim();const o=parseAgentSessionKey(a);const s=o?.agentId?normalizeAgentId(o.agentId):null;const c=e.config??{};const l=resolvePersistedSessionStoreOwnerForKey(c,a);if(s&&n&&n!==s)throw new AgentSelectionRequiredError(listAgentIds(c),{surface:`session agent resolution`,hint:`The agent-scoped session key belongs to "${s}", not "${n}".`});const u=n??i;if(!s&&l.kind===`retired`)throw new AgentSelectionRequiredError(listAgentIds(c),{surface:`session agent resolution`,hint:`The shared fixed-store row belongs to retired agent "${l.agentId}".`});if(!s&&l.kind===`configured`&&u&&u!==l.agentId)throw new AgentSelectionRequiredError(listAgentIds(c),{surface:`session agent resolution`,hint:`The shared fixed-store row belongs to "${l.agentId}", not "${u}".`});return s??(l.kind===`configured`?l.agentId:void 0)??u??void 0}function resolveSessionAgentIdsStrict(e){const t=resolveSelectedSessionAgentId(e);const n=e.config??{};const r=tryResolveLegacyDataOwnerAgentId(n);const i=t??r??resolveDefaultAgentId(n,SESSION_AGENT_SELECTION_CONTEXT);return{defaultAgentId:r??i,sessionAgentId:i}}const resolveSessionAgentIds=resolveSessionAgentIdsStrict;function resolveSessionAgentIdStrict(e){const t=resolveSelectedSessionAgentId(e);const n=e.config??{};return t??tryResolveLegacyDataOwnerAgentId(n)??resolveDefaultAgentId(n,SESSION_AGENT_SELECTION_CONTEXT)}const resolveSessionAgentId=resolveSessionAgentIdStrict;function resolveAgentEntry(e,t){const n=normalizeAgentId(t);const r=readAgentRosterFacts(e);if(r){const t=(r.entryByNormalizedId??=buildAgentEntryIndex(e)).get(n);return t?t.clone?{...t.entry}:t.entry:void 0}const i=readAgentRosterProperty(e);if(i?.kind===`entries`&&isRecord(i.value)){const e=i.value;for(const t in e){if(!Object.hasOwn(e,t))continue;const r=e[t];if(isRecord(r)&&normalizeAgentId(t)===n)return{...r,id:t}}return}if(i?.kind===`list`&&Array.isArray(i.value))return i.value.find(e=>e!==null&&typeof e===`object`&&normalizeAgentId(e.id)===n)}function buildAgentEntryIndex(e){const t=/* @__PURE__ */ new Map;for(const{entry:n,source:r}of listAgentEntriesWithSource(e)){const e=normalizeAgentId(n?.id);if(!t.has(e))t.set(e,{clone:r.kind===`entries`,entry:n})}return t}function resolveMutableAgentEntry(e,t){const n=normalizeAgentId(t);const r=readAgentRosterProperty(e);if(r?.kind===`entries`&&r.value&&typeof r.value===`object`){const e=r.value;const t=Object.keys(e).find(e=>normalizeAgentId(e)===n);return t?e[t]:void 0}if(r?.kind===`list`&&Array.isArray(r.value))return r.value.find(e=>normalizeAgentId(e?.id)===n)}function resolveAgentConfig(e,t){const n=normalizeAgentId(t);const r=resolveAgentEntry(e,n)??(!hasAgentRosterProperty(e)&&n===`main`?{id:n}:void 0);if(!r)return;const i=e.agents?.defaults;return{name:r.name,workspace:r.workspace,workspaceFileMode:r.workspaceFileMode??i?.workspaceFileMode,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,newSessionPermissionMode:r.newSessionPermissionMode,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 resolveAgentVerboseDefault(e,t){return resolveAgentConfig(e,t)?.verboseDefault??e.agents?.defaults?.verboseDefault}function resolveAgentContextLimits(e,t){const n=e?.agents?.defaults?.contextLimits;if(!e||!t)return n;return resolveAgentConfig(e,t)?.contextLimits??n}function resolveAgentWorkspaceDir(e,t,n=process.env){const r=normalizeAgentId(t);const i=resolveAgentConfig(e,r)?.workspace?.trim();if(i)return stripNullBytes(resolveUserPath(i,n));const a=tryResolveLegacyDataOwnerAgentId(e);const o=e.agents?.defaults?.workspace?.trim();if(a&&r===a){if(o)return stripNullBytes(resolveUserPath(o,n));return stripNullBytes(resolveDefaultAgentWorkspaceDir(n))}if(o)return stripNullBytes(path.join(resolveUserPath(o,n),r));const s=resolveStateDir(n);return stripNullBytes(path.join(s,`workspace-${r}`))}function resolveAgentRunCwd(e,t){const n=normalizeOptionalString(resolveAgentEntry(e,t)?.cwd)??normalizeOptionalString(e.agents?.defaults?.cwd);return n?stripNullBytes(resolveUserPath(n)):void 0}function resolveAgentWorkspaceProvisioning(e,t,n){const r=normalizeAgentId(t);const i=resolveAgentConfig(e,r);if(i?.runtime?.type!==`acp`)return`standard`;if(i.workspace?.trim())return`standard`;const a=resolveAgentWorkspaceDir(e,r);const o=n?.workspaceDir?.trim()?resolveUserPath(n.workspaceDir):a;if(o!==a)return`standard`;const s=normalizeOptionalString(n?.cwd)?.trim()??i.runtime.acp?.cwd?.trim();if(!s)return`standard`;if(path.resolve(resolveUserPath(s))===path.resolve(o))return`standard`;return`runtime-managed-implicit`}function isImplicitAcpWorkspaceCandidate(e,t){const n=resolveAgentConfig(e,normalizeAgentId(t));return n?.runtime?.type===`acp`&&!n.workspace?.trim()}function tryResolveConfiguredAgentWorkspaceDir(e,t=process.env){const n=tryResolveLegacyDataOwnerAgentId(e);if(n)return resolveAgentWorkspaceDir(e,n,t);const r=e.agents?.defaults?.workspace?.trim();return r?stripNullBytes(resolveUserPath(r,t)):void 0}function resolveEffectiveAgentDir(e,t,n){const r=normalizeAgentId(t);const i=resolveAgentConfig(e,r)?.agentDir?.trim();const a=n?.env??process.env;return i?resolveUserPath(i,a,n?.homedir):path.join(resolveStateDir(a,n?.homedir),`agents`,r,`agent`)}function resolveAgentDir(e,t,n=process.env){const r=resolveEffectiveAgentDir(e,t,{env:n});registerResolvedAgentDir({agentId:t,agentDir:r,env:n});return r}function resolveDefaultAgentDir(e,t=process.env){return resolveAgentDir(e,resolveAmbientOwnerAgentId(e),t)}export{tryResolveLegacyCompatibilityAgentId as A,resolvePersistedSessionStoreOwnerForTarget as B,resolveSessionAgentIds as C,tryResolveAgentOperationAgentId as D,toAgentEntriesRecord as E,resolveRegisteredAgentIdForDir as F,resolveRegisteredAgentIdForDirAsync as I,unregisterResolvedAgentDir as L,isPathOwnedByAnotherRegisteredAgent as M,normalizeAgentDirRegistryPath as N,tryResolveAmbientOwnerAgentId as O,registerResolvedAgentDir as P,resolvePersistedSessionStoreOwner as R,resolveSessionAgentIdStrict as S,resolveSoleAgentId as T,resolveDefaultAgentId as _,resolveAgentDir as a,resolveNativeModelPrimary as b,resolveAgentNativeModelPrimary as c,resolveAgentVerboseDefault as d,resolveAgentWorkspaceDir as f,resolveDefaultAgentDir as g,resolveConfiguredAgentId as h,resolveAgentContextLimits as i,tryResolveLegacyDataOwnerAgentId as j,tryResolveConfiguredAgentWorkspaceDir as k,resolveAgentOperationAgentId as l,resolveAmbientOwnerAgentId as m,isImplicitAcpWorkspaceCandidate as n,resolveAgentEntry as o,resolveAgentWorkspaceProvisioning as p,resolveAgentConfig as r,resolveAgentModelConfigForRuntime as s,AgentSelectionRequiredError as t,resolveAgentRunCwd as u,resolveEffectiveAgentDir as v,resolveSessionAgentIdsStrict as w,resolveSessionAgentId as x,resolveMutableAgentEntry as y,resolvePersistedSessionStoreOwnerForKey as z};