openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
475 lines (474 loc) • 18.5 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { t as warnPluginSdkDeprecation } from "./sdk-deprecation-D5N9acnu.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import "./routing-DmgGYTrU.mjs";
import { f as resolveApprovalRequestSessionDeliveryAsync, m as isPluginApprovalRequest } from "./approval-request-account-binding-DOCAr58L.mjs";
import { a as createNativeApprovalMessagingTargetResolvers, c as createChannelApproverDmTargetResolver, d as createChannelNativeOriginTargetResolverAsync, i as createNativeApprovalForwardingFallbackSuppressorAsync, l as createChannelApproverDmTargetResolverAsync, n as createNativeApprovalChannelRouteGatesAsync, r as createNativeApprovalForwardingFallbackSuppressor, t as createNativeApprovalChannelRouteGates, u as createChannelNativeOriginTargetResolver } from "./approval-native-helpers-BnPamoUJ.mjs";
//#region src/plugin-sdk/approval-delivery-helpers.ts
function createApproverRestrictedAuth(e) {
const t = e.isPluginAuthorizedSender ?? e.isExecAuthorizedSender;
return {
authorizeActorAction: ({ cfg: n, accountId: r, senderId: i, approvalKind: a, request: o }) => {
const s = a === "plugin" && o && isPluginApprovalRequest(o) ? o : void 0;
return (a === "plugin" ? (!o || s !== void 0) && t({
cfg: n,
accountId: r,
senderId: i,
request: s
}) : e.isExecAuthorizedSender({
cfg: n,
accountId: r,
senderId: i
})) ? { authorized: true } : {
authorized: false,
reason: `❌ You are not authorized to approve ${a} requests on ${e.channelLabel}.`
};
},
...createApproverRestrictedAvailability(e)
};
}
function createApproverRestrictedAvailability(e) {
return {
getActionAvailabilityState: ({ cfg: t, accountId: n }) => e.hasApprovers({
cfg: t,
accountId: n
}) ? { kind: "enabled" } : { kind: "disabled" },
describeExecApprovalSetup: e.describeExecApprovalSetup,
describePluginApprovalSetup: e.describePluginApprovalSetup,
nativeRuntime: e.nativeRuntime,
nativeRuntimeAsync: e.nativeRuntimeAsync
};
}
function createStandardNativeApprovalAvailability(e, t) {
const availabilityState = (e) => e ? { kind: "enabled" } : { kind: "disabled" };
return {
canApprovalPotentiallyRouteToChannel: t.canApprovalPotentiallyRouteToChannel,
canAnyApprovalPotentiallyRouteToChannel: t.canAnyApprovalPotentiallyRouteToChannel,
isNativeApprovalHandlerConfigured: t.isNativeApprovalHandlerConfigured,
getActionAvailabilityState: ({ cfg: e, accountId: n, approvalKind: r }) => availabilityState(r ? t.canApprovalPotentiallyRouteToChannel({
cfg: e,
accountId: n,
approvalKind: r
}) : t.canAnyApprovalPotentiallyRouteToChannel({
cfg: e,
accountId: n
})),
getExecInitiatingSurfaceState: ({ cfg: e, accountId: n }) => availabilityState(t.canApprovalPotentiallyRouteToChannel({
cfg: e,
accountId: n,
approvalKind: "exec"
})),
delivery: { hasConfiguredDmRoute: ({ cfg: n }) => e.listAccountIds(n).some((r) => t.canAnyApprovalPotentiallyRouteToChannel({
cfg: n,
accountId: r,
nativeSessionOnly: true
}) && e.resolveApprovers({
cfg: n,
accountId: r
}).length > 0) }
};
}
function prepareApproverRestrictedSuppression(e, t) {
if ((normalizeMessageChannel(t.target.channel) ?? t.target.channel) !== e.channel || e.requireMatchingTurnSourceChannel && normalizeMessageChannel(t.request.request.turnSourceChannel) !== e.channel) return null;
const n = e.resolveSuppressionAccountId?.(t);
const r = (n === void 0 ? t.target.accountId?.trim() : n.trim()) || void 0;
return {
cfg: t.cfg,
accountId: r
};
}
function createStandardNativeApprovalRouting(e, t) {
const n = createNativeApprovalMessagingTargetResolvers({
channel: e,
normalizeTo: t.normalizeTo
});
const r = createNativeApprovalChannelRouteGates({
channel: e,
defaultForwardingMode: t.defaultForwardingMode,
isTransportEnabled: t.isTransportEnabled,
listAccountIds: t.listAccountIds,
resolveDefaultAccountId: t.resolveDefaultAccountId,
normalizeForwardTarget: n.normalizeForwardTarget,
resolveTurnSourceTarget: n.resolveTurnSourceTarget
});
const i = createChannelNativeOriginTargetResolver({
channel: e,
shouldHandleRequest: r.shouldHandleApprovalRequest,
resolveTurnSourceTarget: n.resolveTurnSourceTarget,
resolveSessionTarget: n.resolveSessionTarget,
normalizeTarget: n.normalizeTarget
});
const resolveOriginTarget = (e) => {
const n = i(e);
if (!n || t.isOriginTargetAllowed && !t.isOriginTargetAllowed({
...e,
target: n
})) return null;
return n;
};
const a = createChannelApproverDmTargetResolver({
shouldHandleRequest: r.shouldHandleApprovalRequest,
resolveApprovers: t.resolveApprovers,
mapApprover: (e, n) => {
const r = t.normalizeTo(e);
return r ? {
to: r,
accountId: normalizeOptionalString(n.accountId)
} : null;
}
});
const o = createNativeApprovalForwardingFallbackSuppressor({
channel: e,
normalizeForwardTarget: n.normalizeForwardTarget,
resolveAccountId: ({ forwardingTarget: e, request: t }) => e.accountId ?? normalizeOptionalString(t.request.turnSourceAccountId),
resolveForwardingTargetForMatch: ({ forwardingTarget: e, accountId: t }) => ({
...e,
accountId: t
}),
isSessionRouteEligible: r.isSessionApprovalEligible,
isExplicitTargetEligible: t.suppressExplicitTargetFallback === false ? void 0 : r.isExplicitTargetEligible,
resolveOriginTarget,
resolveApproverDmTargets: a
});
const s = createStandardNativeApprovalAvailability(t, r);
return {
...s,
shouldHandleApprovalRequest: r.shouldHandleApprovalRequest,
delivery: {
...s.delivery,
shouldSuppressForwardingFallback: o
},
native: {
describeDeliveryCapabilities: ({ cfg: e, accountId: n, approvalKind: r, request: i }) => {
const o = {
cfg: e,
accountId: n,
approvalKind: r,
request: i
};
const s = resolveOriginTarget(o);
const c = a(o);
return {
enabled: Boolean(s) || c.length > 0,
preferredSurface: s ? "origin" : "approver-dm",
supportsOriginSurface: Boolean(s),
supportsApproverDmSurface: c.length > 0,
notifyOriginWhenDmOnly: t.notifyOriginWhenDmOnly ?? true
};
},
resolveOriginTarget,
resolveApproverDmTargets: a
}
};
}
/** @deprecated Use createApproverRestrictedNativeApprovalCapabilityAsync with awaited account eligibility. Final compatibility day: 2026-12-31. Removal in the first January 2027 stable release. */
function createApproverRestrictedNativeApprovalCapability(e) {
warnPluginSdkDeprecation({
family: "approval-session-routing",
method: "createApproverRestrictedNativeApprovalCapability",
replacement: "createApproverRestrictedNativeApprovalCapabilityAsync"
});
const availabilityState = (e) => e ? { kind: "enabled" } : { kind: "disabled" };
const normalizePreferredSurface = (e) => e === "channel" ? "origin" : e === "dm" ? "approver-dm" : "both";
const isExecInitiatingSurfaceEnabled = ({ cfg: t, accountId: n }) => e.hasApprovers({
cfg: t,
accountId: n
}) && e.isNativeDeliveryEnabled({
cfg: t,
accountId: n
});
return createChannelApprovalCapability({
...createApproverRestrictedAuth(e),
getExecInitiatingSurfaceState: ({ cfg: e, accountId: t }) => availabilityState(isExecInitiatingSurfaceEnabled({
cfg: e,
accountId: t
})),
describeExecApprovalSetup: e.describeExecApprovalSetup,
describePluginApprovalSetup: e.describePluginApprovalSetup,
delivery: {
hasConfiguredDmRoute: ({ cfg: t }) => e.listAccountIds(t).some((n) => {
if (!e.hasApprovers({
cfg: t,
accountId: n
})) return false;
if (!e.isNativeDeliveryEnabled({
cfg: t,
accountId: n
})) return false;
const r = e.resolveNativeDeliveryMode({
cfg: t,
accountId: n
});
return r === "dm" || r === "both";
}),
shouldSuppressForwardingFallback: (t) => {
const n = prepareApproverRestrictedSuppression(e, t);
return n ? e.isNativeDeliveryEnabled(n) : false;
}
},
native: e.resolveOriginTarget || e.resolveApproverDmTargets ? {
describeDeliveryCapabilities: ({ cfg: t, accountId: n }) => ({
enabled: isExecInitiatingSurfaceEnabled({
cfg: t,
accountId: n
}),
preferredSurface: normalizePreferredSurface(e.resolveNativeDeliveryMode({
cfg: t,
accountId: n
})),
supportsOriginSurface: Boolean(e.resolveOriginTarget),
supportsApproverDmSurface: Boolean(e.resolveApproverDmTargets),
notifyOriginWhenDmOnly: e.notifyOriginWhenDmOnly ?? false
}),
resolveOriginTarget: e.resolveOriginTarget,
resolveApproverDmTargets: e.resolveApproverDmTargets
} : void 0,
nativeRuntime: e.nativeRuntime
});
}
/** Build approver-restricted native delivery using worker-owned account eligibility. */
function createApproverRestrictedNativeApprovalCapabilityAsync(e) {
const isEnabled = async (t) => e.hasApprovers(t) && await e.isNativeDeliveryEnabled(t);
return createChannelApprovalCapability({
...createApproverRestrictedAvailability(e),
authorizeActorActionAsync: async ({ cfg: t, accountId: n, senderId: r, approvalKind: i, request: a, sessionDelivery: o }) => {
const s = i === "plugin" && a && isPluginApprovalRequest(a) ? a : void 0;
return (i === "plugin" ? (!a || s !== void 0) && await (e.isPluginAuthorizedSender ?? e.isExecAuthorizedSender)({
cfg: t,
accountId: n,
senderId: r,
request: s,
sessionDelivery: o
}) : await e.isExecAuthorizedSender({
cfg: t,
accountId: n,
senderId: r,
sessionDelivery: o
})) ? { authorized: true } : {
authorized: false,
reason: `❌ You are not authorized to approve ${i} requests on ${e.channelLabel}.`
};
},
getExecInitiatingSurfaceStateAsync: async (e) => await isEnabled(e) ? { kind: "enabled" } : { kind: "disabled" },
delivery: {
hasConfiguredDmRouteAsync: async ({ cfg: t }) => {
for (const n of e.listAccountIds(t)) {
if (!await isEnabled({
cfg: t,
accountId: n
})) continue;
const r = e.resolveNativeDeliveryMode({
cfg: t,
accountId: n
});
if (r === "dm" || r === "both") return true;
}
return false;
},
shouldSuppressForwardingFallbackAsync: async (t) => {
const n = prepareApproverRestrictedSuppression(e, t);
return n ? e.isNativeDeliveryEnabled(n) : false;
}
},
nativeAsync: e.resolveOriginTarget || e.resolveApproverDmTargets ? {
describeDeliveryCapabilities: async ({ cfg: t, accountId: n, approvalKind: r }) => {
const i = await isEnabled({
cfg: t,
accountId: n,
approvalKind: r
});
const a = e.resolveNativeDeliveryMode({
cfg: t,
accountId: n
});
return {
enabled: i,
preferredSurface: a === "channel" ? "origin" : a === "dm" ? "approver-dm" : "both",
supportsOriginSurface: Boolean(e.resolveOriginTarget),
supportsApproverDmSurface: Boolean(e.resolveApproverDmTargets),
notifyOriginWhenDmOnly: e.notifyOriginWhenDmOnly ?? false
};
},
resolveOriginTarget: e.resolveOriginTarget,
resolveApproverDmTargets: e.resolveApproverDmTargets
} : void 0
});
}
/** Build the split approval adapter shape for approver-restricted native channels. */
function createApproverRestrictedNativeApprovalAdapter(e) {
return splitChannelApprovalCapability(createApproverRestrictedNativeApprovalCapability(e));
}
/** Assemble a channel approval capability from its auth, delivery, render, and native surfaces. */
function createChannelApprovalCapability(e) {
const { delivery: t, nativeRuntime: n, render: r, native: i } = e;
return {
authorizeActorAction: e.authorizeActorAction,
authorizeActorActionAsync: e.authorizeActorActionAsync,
getActionAvailabilityState: e.getActionAvailabilityState,
getActionAvailabilityStateAsync: e.getActionAvailabilityStateAsync,
getExecInitiatingSurfaceState: e.getExecInitiatingSurfaceState,
getExecInitiatingSurfaceStateAsync: e.getExecInitiatingSurfaceStateAsync,
resolveApproveCommandBehavior: e.resolveApproveCommandBehavior,
describeExecApprovalSetup: e.describeExecApprovalSetup,
describePluginApprovalSetup: e.describePluginApprovalSetup,
delivery: t,
nativeRuntime: n,
nativeRuntimeAsync: e.nativeRuntimeAsync,
render: r,
native: i,
nativeAsync: e.nativeAsync
};
}
/** Split the canonical approval capability into the adapter shape older channel loaders consume. */
function splitChannelApprovalCapability(e) {
return {
auth: {
authorizeActorAction: e.authorizeActorAction,
authorizeActorActionAsync: e.authorizeActorActionAsync,
getActionAvailabilityState: e.getActionAvailabilityState,
getActionAvailabilityStateAsync: e.getActionAvailabilityStateAsync,
getExecInitiatingSurfaceState: e.getExecInitiatingSurfaceState,
getExecInitiatingSurfaceStateAsync: e.getExecInitiatingSurfaceStateAsync,
resolveApproveCommandBehavior: e.resolveApproveCommandBehavior
},
delivery: e.delivery,
nativeRuntime: e.nativeRuntime,
nativeRuntimeAsync: e.nativeRuntimeAsync,
render: e.render,
native: e.native,
nativeAsync: e.nativeAsync,
describeExecApprovalSetup: e.describeExecApprovalSetup,
describePluginApprovalSetup: e.describePluginApprovalSetup
};
}
/** @deprecated Use createApproverRestrictedNativeApprovalCapabilityFromForwardingRoutesAsync for worker-owned session reads. Final compatibility day: 2026-12-31. Removal in the first January 2027 stable release. */
function createApproverRestrictedNativeApprovalCapabilityFromForwardingRoutes(e) {
warnPluginSdkDeprecation({
family: "approval-session-routing",
method: "createApproverRestrictedNativeApprovalCapabilityFromForwardingRoutes",
replacement: "createApproverRestrictedNativeApprovalCapabilityFromForwardingRoutesAsync"
});
const t = createStandardNativeApprovalRouting(e.channel, e.routing);
return {
capability: createChannelApprovalCapability({
authorizeActorAction: e.authorizeActorAction,
getActionAvailabilityState: t.getActionAvailabilityState,
getExecInitiatingSurfaceState: t.getExecInitiatingSurfaceState,
describeExecApprovalSetup: e.describeExecApprovalSetup,
describePluginApprovalSetup: e.describePluginApprovalSetup,
delivery: t.delivery,
native: t.native,
nativeRuntime: e.createNativeRuntime?.(t) ?? e.nativeRuntime,
render: e.render
}),
routing: t
};
}
function createStandardNativeApprovalRoutingAsync(e, t) {
const n = createNativeApprovalMessagingTargetResolvers({
channel: e,
normalizeTo: t.normalizeTo
});
const r = createNativeApprovalChannelRouteGatesAsync({
channel: e,
defaultForwardingMode: t.defaultForwardingMode,
isTransportEnabled: t.isTransportEnabled,
listAccountIds: t.listAccountIds,
resolveDefaultAccountId: t.resolveDefaultAccountId,
normalizeForwardTarget: n.normalizeForwardTarget,
resolveTurnSourceTarget: n.resolveTurnSourceTarget
});
const i = createChannelNativeOriginTargetResolverAsync({
channel: e,
shouldHandleRequest: r.shouldHandleApprovalRequest,
resolveTurnSourceTarget: n.resolveTurnSourceTarget,
resolveSessionTarget: n.resolveSessionTarget,
normalizeTarget: n.normalizeTarget
});
const resolveOriginTarget = async (e) => {
const n = await i(e);
if (!n || t.isOriginTargetAllowed && !t.isOriginTargetAllowed({
...e,
target: n
})) return null;
return n;
};
const a = createChannelApproverDmTargetResolverAsync({
shouldHandleRequest: r.shouldHandleApprovalRequest,
resolveApprovers: t.resolveApprovers,
mapApprover: (e, n) => {
const r = t.normalizeTo(e);
return r ? {
to: r,
accountId: normalizeOptionalString(n.accountId)
} : null;
}
});
const o = createNativeApprovalForwardingFallbackSuppressorAsync({
channel: e,
normalizeForwardTarget: n.normalizeForwardTarget,
resolveAccountId: ({ forwardingTarget: e, request: t }) => e.accountId ?? normalizeOptionalString(t.request.turnSourceAccountId),
resolveForwardingTargetForMatch: ({ forwardingTarget: e, accountId: t }) => ({
...e,
accountId: t
}),
isSessionRouteEligible: r.isSessionApprovalEligible,
isExplicitTargetEligible: t.suppressExplicitTargetFallback === false ? void 0 : r.isExplicitTargetEligible,
resolveOriginTarget,
resolveApproverDmTargets: a
});
const s = createStandardNativeApprovalAvailability(t, r);
return {
...s,
shouldHandleApprovalRequest: r.shouldHandleApprovalRequest,
delivery: {
...s.delivery,
shouldSuppressForwardingFallbackAsync: o
},
nativeAsync: {
describeDeliveryCapabilities: async ({ cfg: e, accountId: n, approvalKind: r, request: i }) => {
const o = {
cfg: e,
accountId: n,
approvalKind: r,
request: i,
sessionDelivery: await resolveApprovalRequestSessionDeliveryAsync({
cfg: e,
request: i
})
};
const [s, c] = await Promise.all([resolveOriginTarget(o), a(o)]);
return {
enabled: Boolean(s) || c.length > 0,
preferredSurface: s ? "origin" : "approver-dm",
supportsOriginSurface: Boolean(s),
supportsApproverDmSurface: c.length > 0,
notifyOriginWhenDmOnly: t.notifyOriginWhenDmOnly ?? true
};
},
resolveOriginTarget,
resolveApproverDmTargets: a
}
};
}
/** Build forwarding routes with worker-owned session and account resolution. */
function createApproverRestrictedNativeApprovalCapabilityFromForwardingRoutesAsync(e) {
const t = createStandardNativeApprovalRoutingAsync(e.channel, e.routing);
return {
capability: createChannelApprovalCapability({
authorizeActorAction: e.authorizeActorAction,
getActionAvailabilityState: t.getActionAvailabilityState,
getExecInitiatingSurfaceState: t.getExecInitiatingSurfaceState,
describeExecApprovalSetup: e.describeExecApprovalSetup,
describePluginApprovalSetup: e.describePluginApprovalSetup,
delivery: t.delivery,
nativeAsync: t.nativeAsync,
nativeRuntimeAsync: e.createNativeRuntime?.(t) ?? e.nativeRuntimeAsync,
render: e.render
}),
routing: t
};
}
//#endregion
export { createApproverRestrictedNativeApprovalCapabilityFromForwardingRoutesAsync as a, createApproverRestrictedNativeApprovalCapabilityFromForwardingRoutes as i, createApproverRestrictedNativeApprovalCapability as n, createChannelApprovalCapability as o, createApproverRestrictedNativeApprovalCapabilityAsync as r, splitChannelApprovalCapability as s, createApproverRestrictedNativeApprovalAdapter as t };