UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

176 lines (175 loc) • 8.41 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs"; import { i as resolveGatewayMessageChannel } from "./message-channel-normalize-CFHSufeo.mjs"; import { t as captureRequesterOrigin } from "./requester-origin-DG-fk-U0.mjs"; import "./message-channel-DTYVLJT1.mjs"; import { t as SESSION_PERMISSION_BY_EXEC_MODE } from "./session-permission-exec-mode-vqqWNPSg.mjs"; import { n as canonicalizeMainSessionAlias } from "./main-session-BNL3-mRV.mjs"; import { i as resolveStoredSessionPermissionPolicy } from "./tool-fs-policy-dDd9W7xo.mjs"; import { a as bindCronManagementToolConstruction, t as bindActiveCronAuthorityCurrentness, u as captureCronRequesterGrantIssuer } from "./cron-creator-authority-context-D4fHKVAj.mjs"; import { t as cloneExecElevatedDefaults } from "./bash-tools.exec-types-CoZ_EDOu.mjs"; //#region src/agents/cli-runner/mcp-grant-context.ts const cliMcpDelegationCapability = Symbol("cliMcpDelegationCapability"); /** Final tool projection and host-only requester capture share the prepared CLI turn. */ function finalizeCliMcpGrant(e, t, n, r = {}) { if (!e) return; const i = captureCronRequesterGrantIssuer(e.runId); const a = bindActiveCronAuthorityCurrentness(e.runId); const o = bindCronManagementToolConstruction(e.runId); const s = r.toolAuthorityFingerprint ? r.replyOperation?.personalToolParticipants : void 0; return { context: { ...e, ...t !== void 0 ? { toolsAllow: [...t] } : {}, ...n ? { nativeCronCreatorToolAllowlist: null } : {} }, ...i ? { cronRequesterGrantIssuer: i } : {}, ...a ? { cronAuthorityCheck: a } : {}, ...o ? { withCronManagementToolConstruction: o } : {}, admittedRunContext: r.admittedRunContext, toolPolicyConfig: r.config, messageActionTurnCapability: r.messageActionTurnCapability, abortSignal: r.abortSignal, assertCurrent: r.assertCurrent, ...s ? { personalToolParticipants: s } : {} }; } function buildCliMcpDelegationCapabilityBinding(e) { return e !== "full" ? { [cliMcpDelegationCapability]: e } : {}; } function readCliMcpDelegationCapability(e) { if (!(cliMcpDelegationCapability in e)) return; const t = e[cliMcpDelegationCapability]; return t === "full" || t === "report_only" || t === "host_only" ? t : void 0; } function buildCliMcpExecSession(e, t) { const n = e?.permissionMode; const r = n && t?.mode ? SESSION_PERMISSION_BY_EXEC_MODE[t.mode] : n; const i = { sandbox: e?.sandbox, sandboxMode: e?.sandboxMode, createdActor: e?.createdActor, execHost: normalizeOptionalString(e?.execHost), execNode: normalizeOptionalString(e?.execNode), ...r ? { permissionMode: r } : {} }; return Object.values(i).some(Boolean) ? i : void 0; } function buildCliMcpExecOverrides(e) { if (!e) return; const t = { ...e.mode !== void 0 ? { mode: e.mode } : {}, ...e.host !== void 0 ? { host: e.host } : {}, ...e.security !== void 0 ? { security: e.security } : {}, ...e.ask !== void 0 ? { ask: e.ask } : {}, ...e.node !== void 0 ? { node: e.node } : {} }; return Object.keys(t).length > 0 ? t : void 0; } function buildCliMcpBashElevated(e) { if (!e) return; return cloneExecElevatedDefaults(e); } function buildCliMcpChannelContext(e, t) { const n = normalizeOptionalString(t) ?? normalizeOptionalString(e?.sender?.id); const r = normalizeOptionalString(e?.chat?.id); if (!n && !r) return; return { ...n ? { sender: { id: n } } : {}, ...r ? { chat: { id: r } } : {} }; } function resolveCliMcpSessionKey(e, t, n) { return canonicalizeMainSessionAlias({ cfg: t, agentId: n, sessionKey: e.sessionKey?.trim() || "main" }); } function buildCliMcpGrantContext(e) { const t = resolveCliMcpSessionKey(e.run, e.config, e.agentId); const n = normalizeOptionalString(e.run.runtimePolicySessionKey); const r = uniqueStrings((e.run.clientCaps ?? []).map((e) => e.trim()).filter(Boolean)); const i = buildCliMcpExecSession(e.run.sessionEntry, e.run.execOverrides); const a = resolveStoredSessionPermissionPolicy(e.run.sessionEntry, e.run.workspaceDir); const o = buildCliMcpExecOverrides(e.run.execOverrides); const s = buildCliMcpBashElevated(e.run.bashElevated); const c = buildCliMcpChannelContext(e.run.channelContext, e.run.senderId); const l = normalizeOptionalString(e.run.senderName); const u = normalizeOptionalString(e.run.senderUsername); const d = normalizeOptionalString(e.run.senderE164); const f = normalizeOptionalString(e.run.groupId); const p = normalizeOptionalString(e.run.groupChannel); const m = normalizeOptionalString(e.run.groupSpace); const h = normalizeOptionalString(e.run.spawnedBy); const g = resolveGatewayMessageChannel(e.run.messageChannel ?? e.run.messageProvider); const _ = normalizeOptionalString(e.run.currentChannelId); const v = e.run.cliToolAvailability?.openClaw; const y = readCliMcpDelegationCapability(e.run); const b = e.run.inputProvenance?.kind === "inter_session" && e.run.inputProvenance.sourceTool === "subagent_announce" && e.run.sourceReplyDeliveryMode === "message_tool_only" && v?.length === 1 && v[0] === "message"; return { sessionKey: t, requesterOrigin: captureRequesterOrigin({ ...e.run, sessionKey: t }), operatorProfileId: e.run.operatorProfileId, conversationId: e.run.conversationId, chatType: e.run.chatType, ...a ? { sessionPermissionPolicy: a } : {}, ...e.run.conversationToolPolicy ? { conversationToolPolicy: structuredClone(e.run.conversationToolPolicy) } : {}, ...e.run.trustedInternalHandoff ? { trustedInternalHandoff: e.run.trustedInternalHandoff } : {}, ...e.run.trustedInternalHandoff || e.run.inputProvenance?.kind === "internal_system" ? { inputProvenance: e.run.inputProvenance } : {}, runtimePolicySessionKey: n, ...e.runtimePolicyAgentId ? { runtimePolicyAgentId: e.runtimePolicyAgentId } : {}, agentId: e.agentId, sessionId: normalizeOptionalString(e.run.sessionId), runId: normalizeOptionalString(e.run.runId), workspaceDir: e.run.workspaceDir, ...normalizeOptionalString(e.run.cwd) ? { cwd: e.run.cwd?.trim() } : {}, ...e.run.toolOverrides?.webSearch === false ? { webSearchDisabled: true } : {}, ...y ? { delegationCapability: y } : {}, ...e.run.scheduledToolPolicy ? { scheduledToolPolicy: { ...e.run.scheduledToolPolicy } } : {}, ...e.run.cronCreatorCallerOrigin ? { cronCreatorCallerOrigin: { ...e.run.cronCreatorCallerOrigin } } : {}, modelProvider: e.modelProvider, modelId: e.modelId, ...e.run.requesterModel ? { requesterModel: { provider: e.run.requesterModel.provider, model: e.run.requesterModel.model } } : {}, modelHasVision: e.run.modelHasVision, messageProvider: g, clientCaps: r.length > 0 ? r : void 0, gatewayUiCommandTarget: e.run.gatewayUiCommandTarget, ...e.run.pinnedWidgetAuthoring === true ? { pinnedWidgetAuthoring: true } : {}, currentChannelId: _, currentThreadTs: normalizeOptionalString(e.run.currentThreadTs), currentMessageId: e.run.currentMessageId == null ? void 0 : normalizeOptionalString(String(e.run.currentMessageId)), replyToMode: e.run.replyToMode, currentInboundAudio: e.run.currentInboundAudio === true ? true : void 0, accountId: normalizeOptionalString(e.run.agentAccountId), inboundEventKind: e.run.currentInboundEventKind, sourceReplyDeliveryMode: e.run.sourceReplyDeliveryMode, ...b ? { sourceReplyOnly: true } : {}, taskSuggestionDeliveryMode: e.run.taskSuggestionDeliveryMode, requireExplicitMessageTarget: e.requireExplicitMessageTarget ? true : void 0, senderIsOwner: e.run.senderIsOwner === true, ...e.run.gitContributorProfileId ? { gitContributorProfileId: e.run.gitContributorProfileId } : {}, nodeExecAllowed: true, ...i ? { execSession: i } : {}, ...o ? { execOverrides: o } : {}, ...s ? { bashElevated: s } : {}, ...e.run.trigger ? { trigger: e.run.trigger } : {}, ...normalizeOptionalString(e.run.approvalReviewerDeviceId) ? { approvalReviewerDeviceId: e.run.approvalReviewerDeviceId?.trim() } : {}, ...c ? { channelContext: c } : {}, ...l ? { senderName: l } : {}, ...u ? { senderUsername: u } : {}, ...d ? { senderE164: d } : {}, ...f ? { groupId: f } : {}, ...p ? { groupChannel: p } : {}, ...m ? { groupSpace: m } : {}, ...h ? { spawnedBy: h } : {} }; } //#endregion export { buildCliMcpGrantContext as n, finalizeCliMcpGrant as r, buildCliMcpDelegationCapabilityBinding as t };