openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
128 lines (127 loc) • 3.87 kB
JavaScript
import { A as resolveExpiresAtMsFromDurationMs, k as resolveDateTimestampMs, y as isFutureDateTimestampMs } from "./number-coercion-D6u-4YD8.mjs";
import { randomUUID } from "node:crypto";
//#region src/gateway/node-pending-work.ts
const DEFAULT_STATUS_ITEM_ID = "baseline-status";
const DEFAULT_PENDING_WORK_TTL_MS = 864e5;
const DEFAULT_MAX_ITEMS = 4;
const MAX_ITEMS = 10;
const PRIORITY_RANK = {
high: 3,
normal: 2,
default: 1
};
const stateByNodeId = /* @__PURE__ */ new Map();
function getOrCreateState(e, t) {
let n = stateByNodeId.get(e);
if (!n || n.pairingGeneration !== t) {
n = {
pairingGeneration: t,
revision: 0,
itemsById: /* @__PURE__ */ new Map()
};
stateByNodeId.set(e, n);
}
return n;
}
function pruneExpired(e, t) {
let n = false;
for (const [r, i] of e.itemsById) if (i.expiresAtMs !== null && !isFutureDateTimestampMs(i.expiresAtMs, { nowMs: t })) {
e.itemsById.delete(r);
n = true;
}
if (n) e.revision += 1;
}
function pruneStateIfEmpty(e, t) {
if (t.itemsById.size === 0) stateByNodeId.delete(e);
}
function sortedItems(e) {
return [...e.itemsById.values()].toSorted((e, t) => PRIORITY_RANK[t.priority] - PRIORITY_RANK[e.priority] || e.createdAtMs - t.createdAtMs || e.id.localeCompare(t.id));
}
function enqueueNodePendingWork(e) {
const t = e.nodeId.trim();
if (!t) throw new Error("nodeId required");
const n = Date.now();
const r = resolveDateTimestampMs(n);
const i = getOrCreateState(t, e.pairingGeneration);
pruneExpired(i, r);
const a = [...i.itemsById.values()].find((t) => t.type === e.type);
if (a) return {
revision: i.revision,
item: a,
deduped: true
};
const o = {
id: randomUUID(),
type: e.type,
priority: e.priority ?? "normal",
createdAtMs: r,
expiresAtMs: resolveExpiresAtMsFromDurationMs(e.expiresInMs ?? DEFAULT_PENDING_WORK_TTL_MS, { nowMs: n }) ?? 0,
...e.payload ? { payload: e.payload } : {}
};
i.itemsById.set(o.id, o);
i.revision += 1;
return {
revision: i.revision,
item: o,
deduped: false
};
}
/** Clears explicit pending work owned by a removed node pairing. */
function clearNodePendingWork(e, t) {
const n = e.trim();
if (!n) return false;
const r = stateByNodeId.get(n);
return t === void 0 || r?.pairingGeneration === t ? stateByNodeId.delete(n) : false;
}
/** Removes one exact item without disturbing concurrent work in the same generation. */
function removeNodePendingWorkItem(e) {
const t = e.nodeId.trim();
if (!t || !e.itemId) return false;
const n = stateByNodeId.get(t);
if (!n || n.pairingGeneration !== e.pairingGeneration || !n.itemsById.delete(e.itemId)) return false;
n.revision += 1;
pruneStateIfEmpty(t, n);
return true;
}
/** Drains pending work for a node, including a baseline status request. */
function drainNodePendingWork(e, t = {}) {
const n = e.trim();
if (!n) return {
revision: 0,
items: [],
hasMore: false
};
const r = resolveDateTimestampMs(Date.now());
const i = stateByNodeId.get(n);
if (i) {
pruneExpired(i, r);
pruneStateIfEmpty(n, i);
}
const a = i?.pairingGeneration === t.pairingGeneration ? i : void 0;
const o = a?.revision ?? 0;
const s = Math.min(MAX_ITEMS, Math.max(1, Math.trunc(t.maxItems ?? DEFAULT_MAX_ITEMS)));
const c = a ? sortedItems(a) : [];
const l = c.slice(0, s);
const u = l.length;
const d = !c.some((e) => e.type === "status.request");
if (a && u > 0) {
for (const e of l) a.itemsById.delete(e.id);
a.revision += 1;
pruneStateIfEmpty(n, a);
}
const f = d && l.length < s;
if (f) l.push({
id: DEFAULT_STATUS_ITEM_ID,
type: "status.request",
priority: "default",
createdAtMs: r,
expiresAtMs: null
});
return {
revision: a?.revision ?? o,
items: l,
hasMore: c.length > u || d && !f
};
}
//#endregion
export { removeNodePendingWorkItem as i, drainNodePendingWork as n, enqueueNodePendingWork as r, clearNodePendingWork as t };