UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

398 lines (397 loc) • 17.4 kB
import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { x as resolveSessionAgentId } from "./agent-scope-config-DmXgFGKP.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import { d as toolPolicyRestrictsTools, l as mergeAlsoAllowPolicy } from "./tool-policy-BR4v75RA.mjs"; import { L as isKnownCoreToolId } from "./tool-catalog-C1ALpCJD.mjs"; import { c as normalizeToolPolicyName, i as expandToolGroups, l as readToolAllowlistIntersection } from "./tool-policy-shared-BHvBopvF.mjs"; import { o as isToolAllowedByPolicies } from "./tool-policy-match-DzwVnbV4.mjs"; import { i as readSessionEntryReadOnlyInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs"; import { a as readCapturedSessionActorEntry, t as captureSessionActorStorageOwner } from "./session-actor-storage-binding-DMwpDsij.mjs"; import { t as AgentHarnessPreflightError } from "./errors-0Ou7CzqA.mjs"; import { s as withPreparedSessionCapabilityRead } from "./subagent-session-store--G7wm9iV.mjs"; import { r as resolveSandboxRuntimeStatus, s as withSandboxRuntimeStatusInWorker } from "./runtime-status-C5ZOG7YR.mjs"; import { r as resolveRequesterConversationToolPolicy } from "./requester-tool-policy-BFuyRJjU.mjs"; import { t as resolveScheduledToolCallerContext } from "./scheduled-tool-policy-CXHVgGTq.mjs"; import { i as resolveConversationCapabilityProfile } from "./conversation-capability-profile-Be5DqCww.mjs"; import { n as resolveExecConfigState } from "./exec-defaults-BaVI-sJD.mjs"; import { n as resolveEffectiveToolFsWorkspaceOnly } from "./tool-fs-policy-dDd9W7xo.mjs"; import { r as withOwnerNativeConsentModes, t as hasRememberedNativeRuntimeConsent } from "./native-runtime-consent-CD0HlMpm.mjs"; import { t as resolveWebSearchToolPolicy } from "./web-search-tool-policy-CLuhTMnE.mjs"; import { n as isHostScopedAgentToolActive } from "./agent-tools.ring-zero-context-Cg68_lTM.mjs"; //#region src/agents/harness/native-tool-deny-policy.ts function collectHarnessSafeDeniedToolNames(e, t) { if (!t) return []; return [...new Set(e.flatMap((e) => expandToolGroups(e?.deny ?? [])).map(normalizeToolPolicyName).filter((e) => isKnownCoreToolId(e) && t.has(e)))].toSorted(); } function toolPolicyRestrictsHarnessNativeTools(e, t) { if (!t || !e || toolPolicyRestrictsTools({ allow: e.allow })) return toolPolicyRestrictsTools(e); return expandToolGroups(e.deny ?? []).some((e) => { const n = normalizeToolPolicyName(e); return !isKnownCoreToolId(n) || !t.has(n); }); } function policyDeniesAllTools(e) { return expandToolGroups(e?.deny ?? []).some((e) => normalizeToolPolicyName(e) === "*"); } //#endregion //#region src/agents/harness/execution-environment.ts /** Selection and invocation share this decision; a native working directory is not containment. */ function resolveAgentHarnessExecutionRestriction(e, t) { if (e.executionEnvironment !== "host-only") return; const n = e.label; const r = t.nativeRuntimeConsent === e.id; const i = [ [ t.sandboxRequired, "sandbox-required", " runs on the Gateway host, but this chat requires a sandbox. Choose another runtime; this requirement cannot be removed." ], [ t.remoteExecution, "remote-execution", " runs on the Gateway host and cannot use this chat's remote execution environment. Choose another runtime or a local chat." ], [ t.workspaceRequired, "workspace-only", " cannot enforce this run's required workspace boundary. Choose another runtime." ], [ !r && t.workspaceOnly, "workspace-only", " cannot enforce this chat's workspace-only file access. Choose another runtime or ask an administrator to review the file-access policy." ], [ !r && t.sandboxed, "sandbox", " runs on the Gateway host, outside the sandbox. Use its own permissions for this chat, or choose another runtime." ], [ !r && Boolean(t.permissionMode && t.permissionMode !== "full"), "permission-mode", " uses its own permissions and requires Full access. Change this chat's permissions explicitly, or choose another runtime." ], [ !r && t.toolPolicyRestricted, "tool-policy", " uses its own tools and cannot enforce this chat's OpenClaw tool restrictions." ] ].find(([e]) => e); return i ? { reason: i[1], message: n + i[2] } : void 0; } /** Classifies stored or prospective session policy without selecting runtime availability. */ function resolveAgentHarnessSessionExecutionRestriction(e) { const { cfg: t, agentId: n, sessionKey: r, entry: i, harness: a } = e; if (a.executionEnvironment !== "host-only") return; const o = resolveSandboxRuntimeStatus({ cfg: t, agentId: n, sessionKey: r, preparedSessionEntry: i }); const s = resolveExecConfigState({ cfg: t, agentId: n, sessionKey: r, sessionEntry: i }); const c = withOwnerNativeConsentModes(i, e.callerCanConsent, o.sandboxed); const l = c.permissionMode === "full" && c.sandboxMode === "off" ? i.nativeRuntimeConsent ?? (hasRememberedNativeRuntimeConsent(t, n, a.id) ? a.id : void 0) : void 0; return resolveAgentHarnessExecutionRestriction(a, { sandboxed: o.sandboxed || s.host === "sandbox", sandboxRequired: o.sandboxRequired || s.host === "sandbox", workspaceOnly: resolveEffectiveToolFsWorkspaceOnly({ cfg: t, agentId: n }), permissionMode: i.permissionMode, nativeRuntimeConsent: l, remoteExecution: s.host === "node", toolPolicyRestricted: l !== a.id && a.conversationToolPolicySupport !== "exact" && resolveAgentHarnessNativeToolPolicyRestricted({ config: t, agentId: n, sessionKey: r, sessionId: i.sessionId, preparedSessionEntry: i, preparedSessionCapabilityEntry: e.preparedSessionCapabilityEntry, provider: e.provider, modelId: e.modelId }, a) }); } /** Revalidates execution policy and returns whether this run has native permission consent. */ async function assertAgentHarnessExecutionEnvironment(e, t) { if (e.executionEnvironment !== "host-only") return false; const n = resolveSessionAgentId({ config: t.config, agentId: t.agentId, sessionKey: t.sessionKey }); const r = t.sessionKey ? captureSessionActorStorageOwner({ agentId: n, sessionKey: t.sessionKey, storePath: t.config?.session?.store ? resolveSessionStorePathCore(t.config.session.store, { agentId: n }) : void 0 }, { assertCurrent() {}, authorize() {} }) : void 0; const i = r ? readCapturedSessionActorEntry(r, t.sessionKey) : t.sessionKey ? await readSessionEntryReadOnlyInWorker({ agentId: n, sessionKey: t.sessionKey, storePath: resolveSessionStorePathCore(t.config?.session?.store, { agentId: n }) }) : void 0; const a = { cfg: t.config, agentId: t.agentId, sessionKey: t.sessionKey, classificationSessionKey: t.sandboxSessionKey, classificationAgentId: t.sandboxAgentId }; if ((!t.sandboxSessionKey || t.sandboxSessionKey === t.sessionKey) && (!t.sandboxAgentId || t.sandboxAgentId === n)) return assertPreparedAgentHarnessExecutionEnvironment(e, t, n, i, r !== void 0, resolveSandboxRuntimeStatus({ ...a, preparedSessionEntry: i ?? null })); return withSandboxRuntimeStatusInWorker(a, { env: process.env, cwd: process.cwd(), assertCurrent() {} }, async (a) => assertPreparedAgentHarnessExecutionEnvironment(e, t, n, i, r !== void 0, a)); } function assertPreparedAgentHarnessExecutionEnvironment(e, t, n, r, i, a) { const o = hasRememberedNativeRuntimeConsent(t.config, n, e.id); const s = r && withOwnerNativeConsentModes(r, t.senderIsOwner, a.sandboxed); const c = r?.sessionId === t.sessionId && (r.agentRuntimeOverride === e.id || o) && s?.permissionMode === "full" && (t.permissionMode ?? s.permissionMode) === "full" && !t.sandbox?.enabled && s.sandboxMode === "off" && !t.disableTools && t.toolsAllow === void 0 && !t.swarmCollector ? o ? e.id : r.nativeRuntimeConsent : void 0; const l = resolveExecConfigState({ cfg: t.config, agentId: a.classificationAgentId, sessionKey: t.sessionKey, execOverrides: t.execOverrides, sessionEntry: i ? r : void 0 }); const u = resolveAgentHarnessExecutionRestriction(e, { sandboxed: t.sandbox?.enabled === true || a.sandboxed || l.host === "sandbox", nativeRuntimeConsent: c, workspaceRequired: t.requireWorkspaceOnly === true, sandboxRequired: a.sandboxRequired || l.host === "sandbox", workspaceOnly: t.requireWorkspaceOnly === true || resolveEffectiveToolFsWorkspaceOnly({ cfg: t.config, agentId: a.classificationAgentId }), permissionMode: i && t.permissionMode === "full" && r?.sessionId === t.sessionId ? r.permissionMode ?? t.permissionMode : t.permissionMode, remoteExecution: l.host === "node" }); if (u) throw new AgentHarnessPreflightError(u.message, { scope: "harness", userMessage: u.message }); return c === e.id; } const PLUGIN_HARNESS_SENDER_DENY_ALL_PROMPT = "Tool and file actions are disabled for this sender by chat policy. If asked to edit files or use tools, say this sender is not allowed by policy; do not imply retrying will help."; const PLUGIN_HARNESS_GROUP_DENY_ALL_PROMPT = "Tool and file actions are disabled for this chat by policy. If asked to edit files or use tools, say this chat is not allowed by policy."; const PLUGIN_HARNESS_RUNTIME_DENY_ALL_PROMPT = "Tool and file actions are disabled by runtime policy. If asked to edit files or use tools, say tools are disabled by policy."; function resolvePluginHarnessPolicyToolsAllow(e) { return [ e.senderPolicy, e.groupPolicy, ...e.runtimePolicies ].some(toolPolicyRestrictsTools) ? [] : void 0; } async function prepareAgentHarnessNativeToolPolicyRestricted(e, t) { return (await preparePluginHarnessToolPolicies(e, t.conversationToolPolicySupport === "exact" ? t.conversationToolPolicySafeDenyTools : void 0, t.conversationToolPolicyNativeTools)).toolPolicyRestricted; } async function preparePluginHarnessToolPolicies(e, t, n, r) { const consume = (i) => { const a = /* @__PURE__ */ new Map(); return withPreparedSessionCapabilityRead(() => { a.clear(); const o = resolvePluginHarnessToolPolicies(e, t, n, i); const s = r ? resolveWebSearchToolPolicy({ ...e, modelProvider: r.provider, modelId: r.model, agentId: i.classificationAgentId, sessionKey: i.classificationSessionKey || e.sessionId, messageProvider: e.messageChannel ?? e.messageProvider, sandboxToolPolicy: e.sandbox !== void 0 ? e.sandbox?.tools : i.sandboxed ? i.toolPolicy : void 0 }) : void 0; return { ...o, webSearchPolicy: r === null ? { allowed: false, persistentAllowed: false } : s && { ...s, modelRef: r, requesterFacts: [...a.values()] } }; }, { onRead: r ? (e) => a.set(JSON.stringify(e.query), e) : void 0 }); }; const i = { cfg: e.config, agentId: e.agentId, sessionKey: e.sessionKey ?? (e.agentId ? void 0 : e.sandboxSessionKey), classificationSessionKey: e.sandboxSessionKey ?? e.sessionKey, classificationAgentId: e.sandboxAgentId }; if (e.preparedSessionEntry !== void 0) return consume(resolveSandboxRuntimeStatus({ ...i, preparedSessionEntry: e.preparedSessionEntry })); return withSandboxRuntimeStatusInWorker(i, { env: process.env, cwd: process.cwd(), assertCurrent() {} }, consume); } /** Resolves whether a harness operation must remove its ambient native tool surface. */ function resolveAgentHarnessNativeToolPolicyRestricted(e, t) { return resolvePluginHarnessToolPolicies(e, t.conversationToolPolicySupport === "exact" ? t.conversationToolPolicySafeDenyTools : void 0, t.conversationToolPolicyNativeTools).toolPolicyRestricted; } function resolvePluginHarnessDenyAllToolPolicyPrompt(e) { return [ [[e.senderPolicy, e.senderScopedGroupPolicy], PLUGIN_HARNESS_SENDER_DENY_ALL_PROMPT], [[e.groupPolicy], PLUGIN_HARNESS_GROUP_DENY_ALL_PROMPT], [e.runtimePolicies, PLUGIN_HARNESS_RUNTIME_DENY_ALL_PROMPT] ].find(([e]) => e.some(policyDeniesAllTools))?.[1]; } function resolvePluginHarnessToolPolicies(e, t, n, r) { const i = e.messageProvider ?? e.messageChannel; const a = e.sandboxSessionKey ?? e.sessionKey; const o = r ?? resolveSandboxRuntimeStatus({ cfg: e.config, agentId: e.agentId, sessionKey: e.sessionKey ?? (e.agentId ? void 0 : a), classificationSessionKey: a, classificationAgentId: e.sandboxAgentId, preparedSessionEntry: e.preparedSessionEntry }); const s = o.sandboxed ? o.toolPolicy : void 0; const c = resolveConversationCapabilityProfile({ config: e.config, preparedSessionEntry: e.preparedSessionCapabilityEntry, sessionId: e.sessionId, sessionKey: e.sessionKey, sandboxSessionKey: a, agentId: e.agentId, modelProvider: e.provider, modelId: e.modelId, messageProvider: i, messageChannel: e.messageChannel, conversationToolPolicy: e.conversationToolPolicy, requesterOrigin: e.requesterOrigin, operatorProfileId: e.operatorProfileId, conversationId: e.conversationId, chatType: e.chatType, messageThreadId: e.messageThreadId, memberRoleIds: e.memberRoleIds, agentAccountId: e.agentAccountId, groupId: e.groupId, groupChannel: e.groupChannel, groupSpace: e.groupSpace, spawnedBy: e.spawnedBy, senderId: e.senderId, senderName: e.senderName, senderUsername: e.senderUsername, senderE164: e.senderE164, senderIsOwner: e.senderIsOwner, sandboxToolPolicy: s, inputProvenance: e.inputProvenance, trustedInternalHandoff: e.trustedInternalHandoff, scheduledToolPolicy: e.scheduledToolPolicy, runtimePluginToolGrant: e.runtimePluginToolGrant }); const l = resolveScheduledToolCallerContext({ scheduledToolPolicy: e.scheduledToolPolicy, channel: i }); const u = { requesterOrigin: e.requesterOrigin, conversationId: e.conversationId, chatType: e.chatType, config: e.config, sessionKey: e.scheduledToolPolicy?.ownerSessionKey ?? e.sessionKey, spawnedBy: e.spawnedBy, messageProvider: l.local ? i : l.channel ?? void 0, groupId: e.groupId, groupChannel: e.groupChannel, groupSpace: e.groupSpace, accountId: e.scheduledToolPolicy?.ownerAccountId ?? e.agentAccountId, requireConfiguredGroupAccount: e.scheduledToolPolicy?.mode === "account", senderId: e.senderId, senderName: e.senderName, senderUsername: e.senderUsername, senderE164: e.senderE164, senderPolicyMode: e.scheduledToolPolicy ? "never" : "always" }; const { policy: d } = c; const f = e.toolsAllow && (readToolAllowlistIntersection(e.toolsAllow) ?? [e.toolsAllow]); const p = e.disableTools || f?.some((e) => e.length === 0) ? { deny: ["*"] } : e.toolsAllow ? { allow: e.toolsAllow } : void 0; const m = [ d.globalPolicy, d.globalProviderPolicy, d.agentPolicy, d.agentProviderPolicy, d.groupPolicy, d.senderPolicy, d.sandboxPolicy, d.subagentPolicy, d.inheritedToolPolicy, d.runtimeToolPolicyForInheritance, p ]; const h = t ? new Set(t.map(normalizeToolPolicyName)) : void 0; const g = [mergeAlsoAllowPolicy(d.profilePolicy, d.profileAlsoAllow), mergeAlsoAllowPolicy(d.providerProfilePolicy, d.providerProfileAlsoAllow)]; const _ = isHostScopedAgentToolActive("openclaw") && isSystemAgentOnlyAllowlist(e.toolsAllow); const v = resolveSenderScopedGroupToolPolicy(e, u, d.groupPolicy); return { toolPolicyScope: e.disableTools || !_ && toolPolicyRestrictsTools(p) || e.scheduledToolPolicy?.mode === "account" && (m.some(toolPolicyRestrictsTools) || g.some(toolPolicyRestrictsTools)) || e.runtimePluginToolGrant !== void 0 || Boolean(d.delegatedToolPolicy) || d.requesterPolicySource === "completion-handoff" && toolPolicyRestrictsTools(d.inheritedToolPolicy) || e.swarmCollector === true || toolPolicyRestrictsTools(d.senderPolicy) || toolPolicyRestrictsTools(v) ? "turn" : "conversation", senderPolicy: d.senderPolicy, senderScopedGroupPolicy: v, groupPolicy: d.groupPolicy, runtimePolicies: [ ...g, d.globalPolicy, d.globalProviderPolicy, d.agentPolicy, d.agentProviderPolicy, s, d.subagentPolicy, d.inheritedToolPolicy, p ], safeDeniedToolNames: collectHarnessSafeDeniedToolNames(m, h), requiresLiveToolAuthority: Boolean(d.delegatedToolPolicy), toolPolicyRestricted: Boolean(d.delegatedToolPolicy) || e.swarmCollector === true || n?.some((e) => !isToolAllowedByPolicies(e, g)) === true || m.some((e) => toolPolicyRestrictsHarnessNativeTools(e, h)) }; } function isSystemAgentOnlyAllowlist(e) { return e?.length === 1 && normalizeToolPolicyName(e[0] ?? "") === "openclaw"; } function resolveSenderScopedGroupToolPolicy(e, t, n) { if (!policyDeniesAllTools(n) || !hasSenderIdentity(e)) return; const r = resolveRequesterConversationToolPolicy({ ...t, requesterOrigin: e.requesterOrigin ? { ...e.requesterOrigin, senderId: void 0, senderName: void 0, senderUsername: void 0, senderE164: void 0 } : void 0, senderId: void 0, senderName: void 0, senderUsername: void 0, senderE164: void 0 }); return policyDeniesAllTools(r) ? void 0 : n; } function hasSenderIdentity(e) { const t = e.requesterOrigin ?? e; return Boolean(t.senderId?.trim() || t.senderName?.trim() || t.senderUsername?.trim() || t.senderE164?.trim()); } //#endregion export { resolveAgentHarnessSessionExecutionRestriction as a, resolvePluginHarnessToolPolicies as c, preparePluginHarnessToolPolicies as i, isSystemAgentOnlyAllowlist as n, resolvePluginHarnessDenyAllToolPolicyPrompt as o, prepareAgentHarnessNativeToolPolicyRestricted as r, resolvePluginHarnessPolicyToolsAllow as s, assertAgentHarnessExecutionEnvironment as t };