openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
303 lines (302 loc) • 12.7 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs";
import { o as getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope-BvvDhs5D.mjs";
import { o as composeSessionSourceAssertion } from "./session-source-authority-TlTpfRVE.mjs";
import { t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-constants-CkbqRgGl.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import { t as captureRequesterOrigin } from "./requester-origin-DG-fk-U0.mjs";
import { t as assertAdmittedRunOperatorAuthority } from "./admitted-run-context-D3Jvk_f_.mjs";
import { n as operatorScopeSatisfied } from "./operator-scope-compat-CpFHu-FC.mjs";
import { t as getChannelPlugin } from "./registry-ByqVE5S3.mjs";
import { a as collectExplicitDenylist, c as hasRestrictiveAllowPolicy, f as pickSandboxToolPolicy, i as collectExplicitAllowlist, l as mergeAlsoAllowPolicy } from "./tool-policy-BR4v75RA.mjs";
import { U as resolveCoreToolProfilePolicy } from "./tool-catalog-C1ALpCJD.mjs";
import { o as getGatewayToolCallerIdentity, t as captureGatewayToolCallerAssertion } from "./gateway-caller-context-BNidAW8G.mjs";
import { s as withPreparedSessionCapabilityRead } from "./subagent-session-store--G7wm9iV.mjs";
import { i as SANDBOX_DEFAULT_TOOL_ALLOW, t as classifyToolAgainstSandboxToolPolicy } from "./tool-policy-DsT-JaIj.mjs";
import { O as normalizeInheritedToolDenylist } from "./subagent-capabilities-D82nSYzB.mjs";
import { n as resolveEffectiveToolPolicy, o as resolveTrustedGroupId } from "./agent-tools.policy-CM-3Smt9.mjs";
import { i as resolveRequesterToolPolicies, n as isInternalOwnerRequester } from "./requester-tool-policy-BFuyRJjU.mjs";
import { t as resolveScheduledToolCallerContext } from "./scheduled-tool-policy-CXHVgGTq.mjs";
import { t as readOperatorToolGatewayAuthority } from "./operator-tool-gateway-authority-BJCYeVPR.mjs";
import { n as resolveWorkspaceRoot } from "./workspace-dir-VdDz3_sC.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/agents/session-placement-computer.ts
const placementComputer = resolveGlobalSingleton(Symbol.for("openclaw.sessionPlacementComputer"), () => new AsyncLocalStorage());
/** Absence means ordinary node routing; null explicitly withholds an unavailable placed desktop. */
function resolveSessionPlacementComputer(e) {
const t = placementComputer.getStore();
return t ? e && e.runId === t.runId && t.isActive() ? t.bind(e) : null : void 0;
}
/** Select policy facts without opening a transport or activating a dormant sandbox. */
function resolveSessionPlacementSandboxToolPolicy(e, t) {
const n = placementComputer.getStore();
return e && n && t.runId === n.runId && t.agentId === n.agentId && n.isActive() ? n.sandboxToolPolicy ?? e : e;
}
function withSessionPlacementComputer(e, t) {
return placementComputer.run(e, t);
}
//#endregion
//#region src/agents/tools/sessions-operator-authority.ts
function captureSessionControlAuthority(e) {
const t = readOperatorToolGatewayAuthority();
const n = getGatewayToolCallerIdentity()?.operatorAuthority;
const r = getPluginRuntimeGatewayRequestScope();
const i = r?.client?.internal?.operatorRunAuthority;
const a = n || e && e !== i ? void 0 : i;
const o = e ?? n ?? t?.operatorRunAuthority ?? a;
if (!o) return;
const s = [.../* @__PURE__ */ new Set([
o,
n,
t?.operatorRunAuthority,
a
])].filter((e) => e !== void 0);
const c = composeSessionSourceAssertion([
...s.map((e) => composeSessionSourceAssertion([e.assertCurrent], (t) => {
assertAdmittedRunOperatorAuthority(e);
t();
if (e.source !== o.source) throw new Error("Session control operator source changed.");
})),
captureGatewayToolCallerAssertion("sessions.patch"),
composeSessionSourceAssertion([t?.assertCurrent], (e) => {
t?.signal.throwIfAborted();
e();
})
], (e) => {
e();
if (a && r?.hasCurrentClientAuthority?.() === false) throw new Error("Session control caller authority is no longer active.");
});
c();
return {
authority: o,
assertCurrent: c,
allows: (e) => s.every((t) => operatorScopeSatisfied(e, t.scopes)) && (!t || operatorScopeSatisfied(e, t.scopes)) && (!a || operatorScopeSatisfied(e, r?.client?.connect.scopes ?? []))
};
}
/** Availability only; the target guard and underlying Gateway policy still apply. */
function hasSessionControlAuthority(e) {
return captureSessionControlAuthority(e)?.allows("operator.write") ?? false;
}
/** Create and rename use the existing creator-scoped session write grant, not general controls. */
function hasSessionWriteAuthority(e) {
return captureSessionControlAuthority(e)?.allows("operator.sessions.write") ?? false;
}
/** Default guest exposure adds independent creation and label-only management, never full controls. */
function prepareSandboxSessionTools(e) {
const t = e.policy;
const n = t?.[SANDBOX_DEFAULT_TOOL_ALLOW];
if (e.senderIsOwner !== false || !t || !n || n !== t.allow) return {
policy: t,
renameOnly: false
};
const r = e.authority;
if (r) {
assertAdmittedRunOperatorAuthority(r);
r.assertCurrent();
}
if (!(r === void 0 ? hasSessionWriteAuthority() : r !== null && operatorScopeSatisfied("operator.sessions.write", r.scopes))) return {
policy: t,
renameOnly: false
};
const i = ["sessions", "sessions_create"].filter((e) => {
const n = classifyToolAgainstSandboxToolPolicy(e, t);
return n.blockedByAllow && !n.blockedByDeny;
});
return {
policy: i.length ? {
...t,
allow: [...n, ...i]
} : t,
renameOnly: i.includes("sessions")
};
}
//#endregion
//#region src/agents/conversation-capability-profile.ts
/**
* Resolves the conversation-scoped runtime facts that tool and harness policy
* hot paths share. Keep this internal: it prepares existing config/state, not a
* new public access-profile config surface.
*/
function resolveManifestToolProfileNames(e, t) {
if (!t) return [];
return uniqueStrings((e?.plugins ?? []).flatMap((e) => (e.contracts?.tools ?? []).filter((n) => e.toolMetadata?.[n]?.profiles?.some((e) => e === t))));
}
/** Prepare the authoritative inbound surface once for each tool catalog. */
function resolveConversationQuestionInputMode(e) {
const t = normalizeMessageChannel(e.messageChannel ?? e.messageProvider);
if (!t || t === "webchat") return "native";
return getChannelPlugin(t)?.agentPrompt?.questionInputMode ?? "none";
}
/** Prepare standalone worker reads before running the shared synchronous policy. */
function prepareConversationCapabilityProfile(e) {
return withPreparedSessionCapabilityRead(() => resolveConversationCapabilityProfile(e));
}
/** Resolve live tool policy without constructing descriptive catalog metadata. */
function resolveConversationCapabilityPolicy(e) {
const t = e.messageProvider;
const n = e.senderIsOwner;
const r = resolveEffectiveToolPolicy(e);
const { policy: i, renameOnly: a } = prepareSandboxSessionTools({
policy: resolveSessionPlacementSandboxToolPolicy(e.sandboxToolPolicy, {
runId: e.runId,
agentId: r.agentId
}),
senderIsOwner: n,
authority: e.operatorAuthority
});
const o = resolveTrustedGroupId(e);
const s = o.dropped ? null : e.groupChannel;
const c = o.dropped ? null : e.groupSpace;
const l = isInternalOwnerRequester(e);
const u = e.sandboxSessionKey ?? e.sessionKey;
const d = resolveScheduledToolCallerContext({
scheduledToolPolicy: e.scheduledToolPolicy,
channel: t ?? void 0
});
const f = resolveRequesterToolPolicies({
...e,
subagentSessionKey: u,
agentId: r.agentId,
messageProvider: d.local ? t : d.channel,
groupId: o.groupId,
groupChannel: s,
groupSpace: c,
accountId: e.scheduledToolPolicy?.ownerAccountId ?? e.agentAccountId,
senderPolicyMode: e.scheduledToolPolicy || l ? "never" : "always",
groupPolicySessionKey: e.scheduledToolPolicy?.ownerSessionKey,
requireConfiguredGroupAccount: e.scheduledToolPolicy?.mode === "account",
conversationPolicy: pickSandboxToolPolicy(e.conversationToolPolicy)
});
const { groupPolicy: p, senderPolicy: m, subagentPolicy: h, inheritedToolPolicy: g, sessionInheritedToolPolicy: _ } = f;
const v = mergeAlsoAllowPolicy(resolveCoreToolProfilePolicy(r.profile), resolveManifestToolProfileNames(e.pluginMetadataSnapshot, r.profile));
const y = mergeAlsoAllowPolicy(resolveCoreToolProfilePolicy(r.providerProfile), resolveManifestToolProfileNames(e.pluginMetadataSnapshot, r.providerProfile));
const b = [
r.globalPolicy,
r.globalProviderPolicy,
r.agentPolicy,
r.agentProviderPolicy,
p,
m,
i,
h
];
const x = e.runtimeToolAllowlist ? { allow: e.runtimeToolAllowlist } : void 0;
const S = e.inheritRuntimeToolAllowlist === true ? x : void 0;
const C = uniqueStrings((e.runtimePluginToolGrant?.toolNames ?? []).map((e) => e.trim()).filter(Boolean));
const mergeRuntimeToolAlsoAllowlist = (e) => {
const t = uniqueStrings([...e ?? [], ...C]);
return t.length > 0 ? t : void 0;
};
const w = [...b, x];
const T = [
v,
y,
...b,
_,
x
];
const E = f.inheritedToolPolicyForSpawn ?? g;
const D = [
v,
y,
...b,
E,
S
];
return {
...r,
sessionKey: e.sessionKey,
subagentSessionKey: u,
trustedGroup: o,
profilePolicy: v,
providerProfilePolicy: y,
profileAlsoAllow: mergeRuntimeToolAlsoAllowlist(r.profileAlsoAllow),
providerProfileAlsoAllow: mergeRuntimeToolAlsoAllowlist(r.providerProfileAlsoAllow),
groupPolicy: p,
senderPolicy: m,
sandboxPolicy: i,
sandboxSessionRenameOnly: a,
subagentPolicy: h,
inheritedToolPolicy: g,
sessionInheritedToolPolicy: _,
inheritedToolPolicyForSpawn: E,
delegatedToolPolicy: f.delegatedToolPolicy,
inheritedToolPolicySource: f.inheritedToolPolicySource,
delegated: f.delegated,
requesterPolicySource: f.requesterPolicySource,
runtimeToolPolicyForInheritance: S,
inheritancePolicies: D,
explicitToolAllowlist: collectExplicitAllowlist(T),
explicitToolOverrideAllowlist: collectExplicitAllowlist(w),
explicitToolDenylist: collectExplicitDenylist(T),
runtimePluginToolGrant: e.runtimePluginToolGrant
};
}
function resolveConversationCapabilityProfile(e) {
const t = resolveConversationCapabilityPolicy(e);
const { trustedGroup: n } = t;
const r = n.dropped ? null : e.groupChannel;
const i = n.dropped ? null : e.groupSpace;
return {
requesterOrigin: captureRequesterOrigin({
...e,
groupId: n.groupId,
groupChannel: r,
groupSpace: i
}),
agentId: t.agentId,
serviceIdentity: { accountId: e.agentAccountId },
model: {
provider: e.modelProvider,
id: e.modelId
},
conversation: {
sessionKey: e.runSessionKey ?? e.sessionKey,
policySessionKey: e.sessionKey,
runSessionKey: e.runSessionKey,
sessionId: e.sessionId,
messageProvider: e.messageProvider,
messageChannel: e.messageChannel,
questionInputMode: resolveConversationQuestionInputMode(e),
groupId: n.groupId,
groupChannel: r,
groupSpace: i,
spawnedBy: e.spawnedBy
},
sender: { isOwner: e.senderIsOwner },
workspace: {
workspaceRoot: resolveWorkspaceRoot(e.workspaceDir),
runtimeRoot: resolveWorkspaceRoot(e.cwd ?? e.workspaceDir),
spawnWorkspaceRoot: e.spawnWorkspaceDir ? resolveWorkspaceRoot(e.spawnWorkspaceDir) : void 0
},
policy: t
};
}
/** Prepare before flattening: duplicate nonlocal denies survive the local exception. */
function prepareDelegatedToolDenyFloor(e, t = []) {
const n = e.policy;
const r = n.delegatedToolPolicy;
if (!n.agentId || n.inheritedToolPolicySource === "sender" || n.runtimeToolPolicyForInheritance?.allow.length === 0 || !r && n.inheritancePolicies.some(hasRestrictiveAllowPolicy)) return;
if (!r && !n.agentPolicy?.deny?.length) return;
return {
policyAgentId: n.agentId,
deny: normalizeInheritedToolDenylist([...collectExplicitDenylist([
n.globalPolicy,
n.globalProviderPolicy,
r ? n.agentPolicy : void 0,
n.agentProviderPolicy,
n.groupPolicy,
n.senderPolicy,
n.sandboxPolicy,
n.subagentPolicy,
r ? n.inheritedToolPolicy : n.inheritedToolPolicyForSpawn,
n.runtimeToolPolicyForInheritance
]), ...t]),
...r ? { continuation: {
requesterSessionKey: r.requesterSessionKey,
targetAgentId: r.targetAgentId
} } : {}
};
}
//#endregion
export { resolveConversationQuestionInputMode as a, hasSessionWriteAuthority as c, withSessionPlacementComputer as d, resolveConversationCapabilityProfile as i, prepareSandboxSessionTools as l, prepareDelegatedToolDenyFloor as n, captureSessionControlAuthority as o, resolveConversationCapabilityPolicy as r, hasSessionControlAuthority as s, prepareConversationCapabilityProfile as t, resolveSessionPlacementComputer as u };