openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
114 lines (113 loc) • 3 kB
JavaScript
import { t as createDeferredCore } from "./deferred-D0La5CRk.mjs";
import { t as resolveCanonicalPluginApprovalRequestAllowedDecisions } from "./plugin-approval-canonical-decisions-CVZjVlcb.mjs";
import { randomUUID } from "node:crypto";
//#region src/infra/embedded-plugin-approval-broker.ts
let activeBroker = null;
var EmbeddedPluginApprovalBroker = class {
constructor(e) {
this.onEvent = e;
this.pending = /* @__PURE__ */ new Map();
}
notify(e) {
try {
this.onEvent?.(e);
} catch {}
}
listPending() {
return [...this.pending.values()].map((e) => e.record);
}
async request(e) {
if (e.signal?.aborted) throw e.signal.reason ?? /* @__PURE__ */ new Error("approval request aborted");
const t = `plugin:${randomUUID()}`;
const n = Date.now();
const r = {
approvalKind: "plugin",
id: t,
request: e.request,
createdAtMs: n,
expiresAtMs: n + e.timeoutMs
};
const { promise: i, resolve: a, reject: o } = createDeferredCore();
const s = setTimeout(() => {
const e = this.pending.get(t);
if (!e) return;
this.pending.delete(t);
e.resolve(null);
this.notify({
event: "plugin.approval.removed",
payload: { id: t }
});
}, e.timeoutMs);
s.unref?.();
this.pending.set(t, {
record: r,
timer: s,
resolve: a,
reject: o
});
const abort = () => {
const n = this.pending.get(t);
if (!n) return;
clearTimeout(n.timer);
this.pending.delete(t);
n.reject(e.signal?.reason ?? /* @__PURE__ */ new Error("approval request aborted"));
this.notify({
event: "plugin.approval.removed",
payload: { id: t }
});
};
e.signal?.addEventListener("abort", abort, { once: true });
this.notify({
event: "plugin.approval.requested",
payload: r
});
try {
return {
id: t,
decision: await i
};
} finally {
e.signal?.removeEventListener("abort", abort);
}
}
resolve(e, t) {
const n = this.pending.get(e);
if (!n || !resolveCanonicalPluginApprovalRequestAllowedDecisions(n.record.request).includes(t)) return false;
clearTimeout(n.timer);
this.pending.delete(e);
n.resolve(t);
this.notify({
event: "plugin.approval.resolved",
payload: {
id: e,
decision: t,
resolvedBy: "tui:embedded",
ts: Date.now(),
request: n.record.request
}
});
return true;
}
stop(e = /* @__PURE__ */ new Error("embedded plugin approval broker stopped")) {
for (const [t, n] of this.pending) {
clearTimeout(n.timer);
n.reject(e);
this.notify({
event: "plugin.approval.removed",
payload: { id: t }
});
}
this.pending.clear();
}
};
function setEmbeddedPluginApprovalBroker(e) {
activeBroker = e;
}
function clearEmbeddedPluginApprovalBroker(e) {
if (activeBroker === e) activeBroker = null;
}
function getEmbeddedPluginApprovalBroker() {
return activeBroker;
}
//#endregion
export { setEmbeddedPluginApprovalBroker as i, clearEmbeddedPluginApprovalBroker as n, getEmbeddedPluginApprovalBroker as r, EmbeddedPluginApprovalBroker as t };