UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 11.1 kB
import{o as normalizeLowercaseStringOrEmpty}from"./string-coerce-CpTFcSwY.mjs";import{b as uniqueStrings,h as normalizeUniqueSingleOrTrimmedStringList}from"./string-normalization-D_oBUpO3.mjs";import{n as formatConcreteConfigPath}from"./dot-path-B1bYAWM5.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import{E as parseRawSessionConversationRef,N as parseAgentSessionKey,O as parseThreadSessionSuffix}from"./session-key-BPgKfU2m.mjs";import{n as normalizeAccountId,t as DEFAULT_ACCOUNT_ID}from"./account-id-3yqrEsRn.mjs";import{r as listAgentEntriesWithSource,t as hasAgentRosterProperty}from"./agent-roster-4XrgUgPK.mjs";import{C as resolveSessionAgentIds,r as resolveAgentConfig}from"./agent-scope-config-CG9CFpGX.mjs";import{t as AUTOMATIONS_TOOL_NAME}from"./automations-tool-name-CptnbJ9-.mjs";import{W as resolveCoreToolProfilePolicy}from"./tool-catalog-Du-FMXr3.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{n as normalizeMessageChannel}from"./message-channel-core-D_hPOadj.mjs";import{n as getLoadedChannelPlugin}from"./registry-CpNh6QcT.mjs";import"./message-channel-514k57tV.mjs";import{f as pickSandboxToolPolicy,l as mergeAlsoAllowPolicy}from"./tool-policy-Bsc3kFBH.mjs";import{r as createToolPolicyMatcher}from"./tool-policy-match-D9XrfLbh.mjs";import{c as resolveSandboxToolPolicyForAgent}from"./config-CNA3Oz0X.mjs";import"./plugins-DxOycUNJ.mjs";import{n as resolveSessionConversation}from"./session-conversation-DT92KniN.mjs";import{i as logWarn}from"./logger-BHIcoxTH.mjs";import{r as resolveChannelGroupToolsPolicy}from"./group-policy-CR4iW2gz.mjs";import{i as resolveTemplateToolPolicyConsent,n as markFrozenTemplateToolAllowPolicy}from"./tool-policy-runtime-DUKk2eWW.mjs";import{r as resolveProviderToolPolicyEntry}from"./provider-tool-policy-Bnqgc4hq.mjs";import{a as resolveStoredSubagentCapabilities,l as resolveSubagentCapabilityStore,n as isVisibleSpawnedSession,o as resolveStoredSubagentInheritedToolAllowlist,s as resolveStoredSubagentInheritedToolDenylist}from"./subagent-capabilities-Cq_ZFJj-.mjs"; //!src/agents/agent-tools.policy.ts const SUBAGENT_TOOL_DENY_ALWAYS=[`gateway`,`agents_list`,`openclaw`,`session_status`,`progress_card`,AUTOMATIONS_TOOL_NAME,`message`,`sessions_send`,`conversations_list`,`conversations_send`,`conversations_turn`];const SUBAGENT_TOOL_DENY_LEAF=[`subagents`,`sessions_list`,`sessions_history`,`sessions_search`,`sessions_spawn`];function resolveSubagentToolPolicyForSession(e,t,n){const r=e?.tools?.subagents?.tools;const i=resolveSubagentCapabilityStore(t,{cfg:e,store:n?.store});const a=r?.allow;const o=r?.alsoAllow;const s=isVisibleSpawnedSession(t,{cfg:e,store:i});const c=!s&&resolveStoredSubagentCapabilities(t,{cfg:e,store:i}).role===`leaf`;const l=[...s?[]:SUBAGENT_TOOL_DENY_ALWAYS,...c?SUBAGENT_TOOL_DENY_LEAF:[],...r?.deny??[]];return{allow:a&&o?uniqueStrings([...a,...o]):a,deny:l}}function resolveInheritedToolPolicyForSession(e,t,n){const r=resolveStoredSubagentInheritedToolAllowlist(t,{cfg:e,store:n?.store});const i=resolveStoredSubagentInheritedToolDenylist(t,{cfg:e,store:n?.store});if(r.length===0&&i.length===0)return;return{...r.length>0?{allow:r}:{},...i.length>0?{deny:i}:{}}}function resolveConfiguredToolPolicies(e){const t=e.agentTools?.profile??e.cfg.tools?.profile;const n=e.agentTools?.alsoAllow??e.cfg.tools?.alsoAllow;const r=[mergeAlsoAllowPolicy(resolveCoreToolProfilePolicy(t),n),pickSandboxToolPolicy(e.cfg.tools),pickSandboxToolPolicy(e.agentTools),...e.extraPolicies??[]].filter(e=>Boolean(e));if(e.sandboxMode===`all`)r.push(resolveSandboxToolPolicyForAgent(e.cfg,e.agentId??void 0));return r}function resolveScopedGroupIds(e,t,n){const r=resolveSessionConversation({channel:e,kind:t,rawId:n});return normalizeUniqueSingleOrTrimmedStringList([n,r?.id,...r?.parentConversationCandidates??[],r?.baseConversationId])}function resolveGroupContextFromSessionKey(e){const t=(e??``).trim();if(!t)return{};const{baseSessionKey:n,threadId:r}=parseThreadSessionSuffix(t);const i=r?n:t;const a=parseRawSessionConversationRef(i);if(a)return{channel:a.channel,groupIds:resolveScopedGroupIds(a.channel,a.kind,a.rawId)};const o=(i??t).split(`:`).filter(Boolean);let s=o[0]===`agent`?o.slice(2):o;if(s[0]===`subagent`)s=s.slice(1);if(s.length<3)return{};const[c,l,...u]=s;if(l!==`group`&&l!==`channel`)return{};const d=u.join(`:`).trim();if(!d)return{};const f=normalizeLowercaseStringOrEmpty(c);return{channel:f,groupIds:resolveScopedGroupIds(f,l,d)}}function resolveTrustedGroupIdFromContexts(e){const t=(e.groupId??``).trim();if(!t)return{groupId:e.groupId,dropped:false};if(normalizeUniqueSingleOrTrimmedStringList([...e.sessionContext.groupIds??[],...e.spawnedContext.groupIds??[]]).includes(t))return{groupId:e.groupId,dropped:false};return{groupId:null,dropped:true}}function resolveTrustedGroupId(e){return resolveTrustedGroupIdFromContexts({groupId:e.groupId,sessionContext:resolveGroupContextFromSessionKey(e.sessionKey),spawnedContext:resolveGroupContextFromSessionKey(e.spawnedBy)})}function profileAllowsGatewayConfigReads(e,t){const n=resolveCoreToolProfilePolicy(e);if(!n?.allow||n.allow.includes(`*`))return true;const r=normalizeUniqueSingleOrTrimmedStringList(t);return r.length>0&&createToolPolicyMatcher({allow:r})(`gateway`)}function detectImplicitProfileGrants(e){return[{section:`exec`,grants:[`exec`,`process`]},{section:`fs`,grants:[`read`,`write`,`edit`]}].filter(({section:t})=>e.agentTools?.[t]!=null||e.includeGlobalSections&&e.globalTools?.[t]!=null).map(({section:e,grants:t})=>({section:`tools.${e}`,grants:t}))}function resolveEffectiveToolPolicy(e){const t=e.agentId?.trim()?normalizeAgentId(e.agentId):void 0;const n=e.config?listAgentEntriesWithSource(e.config):[];const r=e.config&&(!hasAgentRosterProperty(e.config)||n.length>0)?resolveSessionAgentIds({config:e.config,agentId:t,sessionKey:e.sessionKey}).sessionAgentId:t??parseAgentSessionKey(e.sessionKey)?.agentId;const i=e.config&&r?resolveAgentConfig(e.config,r):void 0;const a=e.config?(e.config.agents?.defaults)?.tools:void 0;const o=i?.tools??(e.config&&!hasAgentRosterProperty(e.config)?a:void 0);const s=e.config?.tools;const c=o?.profile??s?.profile;const l=o?.profile?`agent`:s?.profile?`global`:void 0;const u=resolveProviderToolPolicyEntry({byProvider:s?.byProvider,modelProvider:e.modelProvider,modelId:e.modelId});const d=resolveProviderToolPolicyEntry({byProvider:o?.byProvider,modelProvider:e.modelProvider,modelId:e.modelId});const f=u?.policy;const p=d?.policy;const m=n.find(({entry:e})=>normalizeAgentId(e.id)===r)?.source;const h=m?formatConcreteConfigPath([`agents`,m.kind,m.kind===`entries`?m.key:m.index,`tools`]):e.config&&!hasAgentRosterProperty(e.config)&&!i?.tools&&a?`agents.defaults.tools`:`agents.entries.${r}.tools`;const g=u?`tools.byProvider[${JSON.stringify(u.key)}]`:void 0;const _=d?`${h}.byProvider[${JSON.stringify(d.key)}]`:void 0;const v=p?.profile?_:g;const y=!o?.allow&&(o?.alsoAllow||!s?.alsoAllow?.length)?`${h}.alsoAllow`:void 0;const b={profile:{kind:`profile`,path:l===`agent`?`${h}.profile`:`tools.profile`,profile:c,alsoAllowPath:y},providerProfile:{kind:`profile`,path:v?`${v}.profile`:void 0,profile:p?.profile??f?.profile},global:{kind:`config`,path:`tools`},globalProvider:{kind:`config`,path:g},agent:{kind:`config`,path:h},agentProvider:{kind:`config`,path:_}};const x=[[s,`tools`,!o?.profile],[o,h,true],[f,g,!p?.profile],[p,_,true]].flatMap(([e,t,n])=>e?.profile&&t?[{profile:e.profile,source:`${t}.profile`,active:n}]:[]);const S=o?.alsoAllow??s?.alsoAllow;const C=pickSandboxToolPolicy(o);if(resolveTemplateToolPolicyConsent({agentTools:o,agentId:r,profile:c,ownsProfile:l===`agent`,hasAgentAllowlist:(C?.allow?.length??0)>0}).frozen)markFrozenTemplateToolAllowPolicy(C);const w={agentId:r,sources:b,profiles:x,globalPolicy:pickSandboxToolPolicy(s),globalProviderPolicy:pickSandboxToolPolicy(f),agentPolicy:C,agentProviderPolicy:pickSandboxToolPolicy(p),profile:c,providerProfile:p?.profile??f?.profile,profileAlsoAllow:S?uniqueStrings(S):void 0,providerProfileAlsoAllow:p?.alsoAllow??f?.alsoAllow};const T=profileAllowsGatewayConfigReads(c,w.profileAlsoAllow)&&profileAllowsGatewayConfigReads(w.providerProfile,w.providerProfileAlsoAllow);if(c){const e=detectImplicitProfileGrants({globalTools:s,agentTools:o,includeGlobalSections:l===`global`});if(e.length>0){const t=mergeAlsoAllowPolicy(resolveCoreToolProfilePolicy(c),S);const n=createToolPolicyMatcher(t);const i=[w.globalPolicy,w.globalProviderPolicy,w.agentPolicy,w.agentProviderPolicy,mergeAlsoAllowPolicy(resolveCoreToolProfilePolicy(w.providerProfile),w.providerProfileAlsoAllow)].map(e=>createToolPolicyMatcher(e));const a=e.map(e=>({section:e.section,grants:e.grants.filter(e=>!n(e)&&i.every(t=>t(e)))})).filter(e=>e.grants.length>0);const o=a.flatMap(e=>e.grants);if(o.length>0)logWarn(`tools policy: profile "${c}"${r?` (agent "${r}")`:``} has configured tool sections (${a.map(e=>e.section).join(` / `)}) that no longer implicitly widen the profile. Add alsoAllow: [${o.map(e=>`"${e}"`).join(`, `)}] explicitly if these tools should be available. See #47487.`)}}return{...w,gatewayConfigReadAllowed:T}}function throwUnavailableScheduledAccount(e){throw new Error(`Scheduled account "${e}" is unavailable. Re-add it to the channel configuration, or recreate this automation from the intended account.`)}function resolveGroupToolPolicy(e){if(!e.config)return;const t=resolveGroupContextFromSessionKey(e.sessionKey);const n=resolveGroupContextFromSessionKey(e.spawnedBy);const r=resolveTrustedGroupIdFromContexts({groupId:e.groupId,sessionContext:t,spawnedContext:n});const i=t.channel??n.channel??e.messageProvider;const a=normalizeMessageChannel(i);const o=r.groupId?.trim();const s=normalizeUniqueSingleOrTrimmedStringList([...t.groupIds??[],...n.groupIds??[],...o&&a?resolveScopedGroupIds(a,`group`,o):[],o]);const c=normalizeAccountId(e.accountId);if(!a)return e.requireConfiguredAccount&&c!=="default"?throwUnavailableScheduledAccount(c):void 0;let l;try{l=getLoadedChannelPlugin(a)}catch{l=void 0}if(e.requireConfiguredAccount&&c!=="default"){let t;try{t=l?.config.listAccountIds(e.config).some(e=>normalizeAccountId(e)===c)===true}catch{t=false}if(!t)throwUnavailableScheduledAccount(c)}if(s.length===0)return;for(const t of s){const n=l?.groups?.resolveToolPolicy?.({cfg:e.config,groupId:t,groupChannel:r.dropped?null:e.groupChannel,groupSpace:r.dropped?null:e.groupSpace,accountId:c,senderPolicyMode:e.senderPolicyMode,senderId:e.senderId,senderName:e.senderName,senderUsername:e.senderUsername,senderE164:e.senderE164});const i=pickSandboxToolPolicy(n);if(i)return i}const u=resolveChannelGroupToolsPolicy({cfg:e.config,channel:a,messageProvider:a,groupId:s[0],groupIdCandidates:s.slice(1),accountId:c,senderPolicyMode:e.senderPolicyMode,senderId:e.senderId,senderName:e.senderName,senderUsername:e.senderUsername,senderE164:e.senderE164});return pickSandboxToolPolicy(u)}export{resolveSubagentToolPolicyForSession as a,resolveInheritedToolPolicyForSession as i,resolveEffectiveToolPolicy as n,resolveTrustedGroupId as o,resolveGroupToolPolicy as r,resolveConfiguredToolPolicies as t};