openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
88 lines (87 loc) • 3.94 kB
JavaScript
import { r as findChatChannelLabel } from "./ids-BRSWrLzE.mjs";
import { n as isWellFormedApprovalId } from "./approval-id-CfM0pBs_.mjs";
import { t as isApprovalNotFoundError } from "./approval-errors-byE8UOsC.mjs";
import { t as getGatewayNativeApprovalRuntime } from "./approval-gateway-runtime-context-B9yxaZi6.mjs";
import { n as withOperatorApprovalsGatewayClient } from "./operator-approvals-client-CqzKpK3v.mjs";
//#region src/infra/approval-gateway-resolver.ts
async function resolveApprovalOverGateway(e) {
const t = e.approvalKind;
const n = e.resolveMethod;
const r = t === "exec" || t === "plugin" || t === "system-agent" ? t : null;
const i = n === "exec" || n === "plugin" ? n : null;
const a = r !== null;
const o = i !== null;
const s = e.allowPluginFallback;
const c = e.gatewayRuntime;
if (t !== void 0) {
if (!a || n !== void 0 || s !== void 0) throw new Error("canonical approval resolution requires exactly one valid owner kind");
} else if (n !== void 0 && !o || s !== void 0 && typeof s !== "boolean" || c !== void 0) throw new Error("legacy approval resolution requires valid routing options");
if (e.decision !== "allow-once" && e.decision !== "allow-always" && e.decision !== "deny") throw new Error("approval resolution requires a valid decision");
const l = e.approvalId;
if (typeof l !== "string" || !isWellFormedApprovalId(l)) throw new Error("approval resolution requires an approval id");
const u = e.senderId?.trim();
const d = e.channel?.trim();
const f = e.accountId?.trim();
if (Boolean(d || f || u) && (!d || !f || !u)) throw new Error("channel approval resolution requires channel, account, and sender identity");
const p = d && f && u ? {
channel: d,
accountId: f,
senderId: u
} : void 0;
const m = d ? findChatChannelLabel(d) ?? d : void 0;
const h = e.clientDisplayName ?? (m ? `${m} approval (${u ?? "unknown"})` : `Approval (${u ?? "unknown"})`);
const canonicalResolveParams = (t) => ({
id: l,
kind: t,
decision: e.decision,
...p ? { reviewer: p } : {}
});
const g = e.gatewayRuntime;
if (g && r) return await g.request("approval.resolve", canonicalResolveParams(r), { clientDisplayName: h });
const requestWithClient = async (t) => {
if (a) {
const e = canonicalResolveParams(r);
return await t.request("approval.resolve", e);
}
const requestLegacyResolve = async (n) => {
await t.request(n, {
id: l,
decision: e.decision,
...p ? { reviewer: p } : {}
});
};
if (i === "plugin" || !i && l.startsWith("plugin:")) {
await requestLegacyResolve("plugin.approval.resolve");
return;
}
try {
await requestLegacyResolve("exec.approval.resolve");
} catch (e) {
if (s !== true || !isApprovalNotFoundError(e)) throw e;
await requestLegacyResolve("plugin.approval.resolve");
}
};
const _ = getGatewayNativeApprovalRuntime();
const v = _ ? await requestWithClient({ request: async (e, t) => await _.request(e, t, { clientDisplayName: h }) }) : await withOperatorApprovalsGatewayClient({
config: e.cfg,
gatewayUrl: e.gatewayUrl,
clientDisplayName: h
}, requestWithClient);
return a ? v : void 0;
}
/**
* Whether an approval id is a pending OpenClaw change this chat approval client
* can see. The approval runtime is device-less, so it reads the pending list it
* already replays rather than the device-bound `approval.get` projection.
*/
async function isPendingSystemAgentApprovalOverGateway(e) {
const hasId = (t) => t.some((t) => t.id === e.approvalId);
const t = getGatewayNativeApprovalRuntime();
if (t) return hasId(await t.request("openclaw.approval.list", {}, { clientDisplayName: e.clientDisplayName }));
return await withOperatorApprovalsGatewayClient({
config: e.cfg,
clientDisplayName: e.clientDisplayName
}, async (e) => hasId(await e.request("openclaw.approval.list", {})));
}
//#endregion
export { resolveApprovalOverGateway as n, isPendingSystemAgentApprovalOverGateway as t };