openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
233 lines (232 loc) • 11.8 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs";
import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs";
import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs";
import { t as warnPluginSdkDeprecation } from "./sdk-deprecation-D5N9acnu.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import "./message-channel-DTYVLJT1.mjs";
import { r as sessionDeliveryOrigin, t as deliveryContextFromSession } from "./delivery-context.read-BrYAJ7RK.mjs";
import { i as readSessionEntryReadOnlyInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs";
import { t as captureSessionActorStorageOwner } from "./session-actor-storage-binding-DMwpDsij.mjs";
import "./session-accessor-CizTbDQj.mjs";
import { a as loadSessionEntryReadOnly } from "./session-accessor.sqlite-exact-read-CbvqUmsG.mjs";
import { t as matchesApprovalRequestFilters } from "./approval-request-filters-BbqeKS4j.mjs";
//#region src/infra/approval-types.ts
/** Resolve approval ownership from the typed request payload, never from id spelling. */
function resolveApprovalRequestKind(e) {
if ("proposalHash" in e.request && "sessionId" in e.request) return "system-agent";
const t = "command" in e.request;
if (t === ("title" in e.request && "description" in e.request)) throw new Error("approval request payload does not identify exactly one owner");
return t ? "exec" : "plugin";
}
function isExecApprovalRequest(e) {
return resolveApprovalRequestKind(e) === "exec";
}
function isPluginApprovalRequest(e) {
return resolveApprovalRequestKind(e) === "plugin";
}
function hasExecApprovalKind(e) {
return e.approvalKind === "exec";
}
function hasPluginApprovalKind(e) {
return e.approvalKind === "plugin";
}
function normalizeApprovalRequest(e) {
if (isExecApprovalRequest(e)) {
if (hasExecApprovalKind(e)) return e;
return {
...e,
approvalKind: "exec"
};
}
if (isPluginApprovalRequest(e)) return hasPluginApprovalKind(e) ? e : {
...e,
approvalKind: "plugin"
};
return {
...e,
approvalKind: "system-agent"
};
}
//#endregion
//#region src/infra/approval-request-account-binding.ts
function resolveApprovalForwardTargets(e) {
const t = resolveApprovalRequestKind(e.request) === "exec" ? e.cfg.approvals?.exec : e.cfg.approvals?.plugin;
const n = normalizeMessageChannel(e.channel);
if (!t?.enabled || t.mode !== "targets" && t.mode !== "both") return [];
if (!matchesApprovalRequestFilters({
request: e.request.request,
agentFilter: t.agentFilter,
sessionFilter: t.sessionFilter
})) return [];
return (t.targets ?? []).filter((e) => normalizeMessageChannel(e.channel) === n);
}
function resolveApprovalForwardAccountIds(e) {
return resolveApprovalForwardTargets(e).flatMap((t) => {
const n = normalizeOptionalAccountId(t.accountId ?? e.defaultAccountId);
return n ? [n] : [];
});
}
function approvalRequestSessionDelivery(e) {
return e ? {
sessionId: e.sessionId,
updatedAt: e.updatedAt,
delivery: e.delivery
} : null;
}
/** Classifies whether native delivery has named channel-account owners. */
async function classifyApprovalRequestChannelRouteAsync(e) {
if (!normalizeMessageChannel(e.channel)) return "unbound";
if (await resolveApprovalRequestChannelAccountIdAsync(e)) return "bound-or-explicit";
if (resolveApprovalForwardTargets(e).length > 0) return "bound-or-explicit";
return "unbound";
}
/** Compatibility reader retained only by the deprecated synchronous Plugin SDK. */
function resolveLegacyApprovalRequestSessionDelivery(e) {
const t = normalizeOptionalString(e.request.request.sessionKey);
if (!t) return null;
const n = parseAgentSessionKey(t)?.agentId ?? e.request.request.agentId ?? "main";
const r = resolveSessionStorePathCore(e.cfg.session?.store, { agentId: n });
const i = captureSessionActorStorageOwner({
sessionKey: t,
agentId: n,
storePath: r
}, {
assertCurrent() {},
authorize() {}
});
if (i) return approvalRequestSessionDelivery(i.binding?.actor.target.sessionKey === t ? i.binding.actor.snapshot(i.authority)?.entry : i.owner?.readSession(t, i.authority)?.entry);
return approvalRequestSessionDelivery(loadSessionEntryReadOnly({
storePath: r,
sessionKey: t,
clone: false
}));
}
function resolvePersistedApprovalRequestSessionBinding(e) {
if (!e) return null;
const t = sessionDeliveryOrigin(e);
const n = deliveryContextFromSession(e);
const r = normalizeMessageChannel(n?.channel ?? t?.provider);
const i = normalizeOptionalAccountId(n?.accountId ?? t?.accountId);
return r || i ? {
channel: r,
accountId: i
} : null;
}
function resolveApprovalRequestAccountBinding(e, t) {
const n = normalizeMessageChannel(e.channel);
const r = normalizeMessageChannel(e.request.request.turnSourceChannel);
if (n && r && r !== n) return null;
const i = normalizeOptionalAccountId(e.request.request.turnSourceAccountId);
if (i) return { accountId: i };
const a = resolvePersistedApprovalRequestSessionBinding(t);
const o = a?.channel;
if (n && o && o !== n) return null;
return { accountId: a?.accountId ?? null };
}
/** Resolves the account id an approval request belongs to for an optional channel filter. */
function resolveApprovalRequestAccountIdFromDelivery(e, t) {
return resolveApprovalRequestAccountBinding(e, t)?.accountId ?? null;
}
/** Resolves an approval request account only when the request can be routed to a channel. */
function resolveApprovalRequestChannelAccountIdFromDelivery(e, t) {
const n = normalizeMessageChannel(e.channel);
if (!n) return null;
const r = normalizeMessageChannel(e.request.request.turnSourceChannel);
if (!r || r === n) return resolveApprovalRequestAccountIdFromDelivery(e, t);
const i = resolvePersistedApprovalRequestSessionBinding(t);
return i?.channel === n ? i.accountId ?? null : null;
}
/** Checks whether a channel/account pair is eligible to handle an approval request. */
function doesApprovalRequestMatchChannelAccountFromDelivery(e, t) {
if (!normalizeMessageChannel(e.channel)) return false;
const n = resolveApprovalRequestAccountBinding(e, t);
if (!n) return false;
const r = normalizeOptionalAccountId(e.accountId);
return !r || !n.accountId || r === n.accountId;
}
/** Selects the one channel account that owns a native approval request. */
function doesApprovalRequestSelectChannelAccountFromDelivery(e, t) {
const n = normalizeOptionalAccountId(e.accountId) ?? normalizeOptionalAccountId(e.defaultAccountId);
if (!n) return false;
const r = resolveApprovalRequestChannelAccountIdFromDelivery(e, t);
if (n === normalizeOptionalAccountId(r)) return true;
const i = resolveApprovalForwardAccountIds(e);
if (i.includes(n)) return true;
if (r || i.length > 0) return false;
const a = normalizeMessageChannel(e.request.request.turnSourceChannel);
if (a && a !== normalizeMessageChannel(e.channel)) return false;
const o = e.eligibleAccountIds.map(normalizeOptionalAccountId).filter((e) => Boolean(e));
return o.length === 1 && o[0] === n;
}
/** Reads one current routing snapshot through the session owner; supplied facts are reused. */
async function resolveApprovalRequestSessionDeliveryAsync(e) {
if (e.sessionDelivery !== void 0) return e.sessionDelivery;
const t = normalizeOptionalString(e.request.request.sessionKey);
if (!t) return null;
const n = parseAgentSessionKey(t)?.agentId ?? e.request.request.agentId ?? "main";
const r = resolveSessionStorePathCore(e.cfg.session?.store, { agentId: n });
const i = captureSessionActorStorageOwner({
sessionKey: t,
agentId: n,
storePath: r
}, {
assertCurrent() {},
authorize() {}
});
if (i) return approvalRequestSessionDelivery(i.binding?.actor.target.sessionKey === t ? i.binding.actor.snapshot(i.authority)?.entry : i.owner?.readSession(t, i.authority)?.entry);
return approvalRequestSessionDelivery(await readSessionEntryReadOnlyInWorker({
agentId: n,
storePath: r,
sessionKey: t,
projection: "list",
hydrateSkillPromptRefs: false
}));
}
function warnLegacyApprovalRouting(e) {
warnPluginSdkDeprecation({
family: "approval-session-routing",
method: e,
replacement: `${e}Async`
});
}
/** @deprecated Use resolveApprovalRequestAccountIdAsync; final compatibility day: 2026-12-31. Removal in the first January 2027 stable release. */
function resolveApprovalRequestAccountId(e) {
warnLegacyApprovalRouting("resolveApprovalRequestAccountId");
return resolveApprovalRequestAccountIdFromDelivery(e, resolveLegacyApprovalRequestSessionDelivery(e));
}
async function resolveApprovalRequestAccountIdAsync(e) {
return resolveApprovalRequestAccountIdFromDelivery(e, normalizeOptionalAccountId(e.request.request.turnSourceAccountId) ? null : await resolveApprovalRequestSessionDeliveryAsync(e));
}
/** @deprecated Use resolveApprovalRequestChannelAccountIdAsync; final compatibility day: 2026-12-31. Removal in the first January 2027 stable release. */
function resolveApprovalRequestChannelAccountId(e) {
warnLegacyApprovalRouting("resolveApprovalRequestChannelAccountId");
return resolveApprovalRequestChannelAccountIdFromDelivery(e, resolveLegacyApprovalRequestSessionDelivery(e));
}
async function resolveApprovalRequestChannelAccountIdAsync(e) {
const t = normalizeMessageChannel(e.channel);
const n = normalizeMessageChannel(e.request.request.turnSourceChannel);
const r = normalizeOptionalAccountId(e.request.request.turnSourceAccountId) && (!n || n === t);
return resolveApprovalRequestChannelAccountIdFromDelivery(e, !t || r ? null : await resolveApprovalRequestSessionDeliveryAsync(e));
}
/** @deprecated Use doesApprovalRequestMatchChannelAccountAsync; final compatibility day: 2026-12-31. Removal in the first January 2027 stable release. */
function doesApprovalRequestMatchChannelAccount(e) {
warnLegacyApprovalRouting("doesApprovalRequestMatchChannelAccount");
return doesApprovalRequestMatchChannelAccountFromDelivery(e, resolveLegacyApprovalRequestSessionDelivery(e));
}
async function doesApprovalRequestMatchChannelAccountAsync(e) {
return doesApprovalRequestMatchChannelAccountFromDelivery(e, normalizeOptionalAccountId(e.request.request.turnSourceAccountId) ? null : await resolveApprovalRequestSessionDeliveryAsync(e));
}
/** @deprecated Use doesApprovalRequestSelectChannelAccountAsync; final compatibility day: 2026-12-31. Removal in the first January 2027 stable release. */
function doesApprovalRequestSelectChannelAccount(e) {
warnLegacyApprovalRouting("doesApprovalRequestSelectChannelAccount");
return doesApprovalRequestSelectChannelAccountFromDelivery(e, resolveLegacyApprovalRequestSessionDelivery(e));
}
async function doesApprovalRequestSelectChannelAccountAsync(e) {
const t = normalizeMessageChannel(e.channel);
const n = normalizeMessageChannel(e.request.request.turnSourceChannel);
const r = normalizeOptionalAccountId(e.request.request.turnSourceAccountId) && (!n || n === t);
return doesApprovalRequestSelectChannelAccountFromDelivery(e, !t || r ? null : await resolveApprovalRequestSessionDeliveryAsync(e));
}
//#endregion
export { doesApprovalRequestSelectChannelAccount as a, resolveApprovalRequestAccountId as c, resolveApprovalRequestChannelAccountIdAsync as d, resolveApprovalRequestSessionDeliveryAsync as f, resolveApprovalRequestKind as g, normalizeApprovalRequest as h, doesApprovalRequestMatchChannelAccountFromDelivery as i, resolveApprovalRequestAccountIdAsync as l, isPluginApprovalRequest as m, doesApprovalRequestMatchChannelAccount as n, doesApprovalRequestSelectChannelAccountAsync as o, resolveLegacyApprovalRequestSessionDelivery as p, doesApprovalRequestMatchChannelAccountAsync as r, doesApprovalRequestSelectChannelAccountFromDelivery as s, classifyApprovalRequestChannelRouteAsync as t, resolveApprovalRequestChannelAccountId as u };