openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
128 lines (127 loc) • 3.95 kB
JavaScript
import { r as resolveGlobalMap } from "./global-singleton-D7yf05nR.mjs";
import { s as removeRemoteNodeInfo } from "./remote-BFNOi3gh.mjs";
import { a as invalidateNodeWakeState } from "./node-wake-state-i9g313wk.mjs";
import { a as reconcileDeviceWorker } from "./device-provider-DW_wS2J9.mjs";
import { t as clearNodePendingWork } from "./node-pending-work-BIz0lW6P.mjs";
import { randomUUID } from "node:crypto";
//#region src/gateway/device-worker-revocation.ts
async function reconcileRevokedDeviceWorker(e, t) {
const n = await reconcileDeviceWorker(e.workerEnvironmentService, t);
for (const r of n) try {
await e.workerPlacementDispatchService?.reconcileActive?.(r);
} catch {
e.logGateway.warn(`device worker placement reconciliation failed device=${t} environment=${r}`);
}
}
//#endregion
//#region src/gateway/node-runtime-state.ts
const pendingNodeActionsById = resolveGlobalMap(Symbol.for("openclaw.pendingNodeActions"), "close-and-restart");
function prunePendingNodeActions(e) {
const t = pendingNodeActionsById.get(e.nodeId) ?? [];
const n = e.nowMs - e.ttlMs;
const r = t.filter((e) => e.enqueuedAtMs >= n);
if (r.length === 0) {
pendingNodeActionsById.delete(e.nodeId);
return [];
}
pendingNodeActionsById.set(e.nodeId, r);
return e.pairingGeneration ? r.filter((t) => t.pairingGeneration === e.pairingGeneration) : r;
}
function replacePendingNodeActionsForGeneration(e) {
const t = [...prunePendingNodeActions({
nodeId: e.nodeId,
nowMs: e.nowMs ?? Date.now(),
ttlMs: e.ttlMs
}).filter((t) => t.pairingGeneration !== e.pairingGeneration), ...e.replacement];
if (t.length === 0) {
pendingNodeActionsById.delete(e.nodeId);
return;
}
pendingNodeActionsById.set(e.nodeId, t);
}
function enqueuePendingNodeAction(e) {
const t = Date.now();
const n = prunePendingNodeActions({
nodeId: e.nodeId,
nowMs: t,
ttlMs: e.ttlMs,
pairingGeneration: e.pairingGeneration
});
const r = n.find((t) => t.idempotencyKey === e.idempotencyKey);
if (r) return {
action: r,
created: false
};
const i = {
id: randomUUID(),
pairingGeneration: e.pairingGeneration,
command: e.command,
paramsJSON: e.paramsJSON,
idempotencyKey: e.idempotencyKey,
enqueuedAtMs: t
};
n.push(i);
if (n.length > e.maxPerNode) n.splice(0, n.length - e.maxPerNode);
replacePendingNodeActionsForGeneration({
nodeId: e.nodeId,
pairingGeneration: e.pairingGeneration,
replacement: n,
ttlMs: e.ttlMs,
nowMs: t
});
return {
action: i,
created: true
};
}
function listPendingNodeActions(e) {
return prunePendingNodeActions({
nodeId: e.nodeId,
nowMs: Date.now(),
ttlMs: e.ttlMs,
pairingGeneration: e.pairingGeneration
});
}
function acknowledgePendingNodeActions(e) {
const t = prunePendingNodeActions({
nodeId: e.nodeId,
pairingGeneration: e.pairingGeneration,
nowMs: Date.now(),
ttlMs: e.ttlMs
});
if (e.ids.length === 0) return t;
const n = new Set(e.ids);
const r = t.filter((e) => !n.has(e.id));
replacePendingNodeActionsForGeneration({
...e,
replacement: r
});
return r;
}
function removePendingNodeAction(e) {
const t = prunePendingNodeActions({
nodeId: e.nodeId,
pairingGeneration: e.pairingGeneration,
nowMs: Date.now(),
ttlMs: e.ttlMs
});
const n = t.filter((t) => t.id !== e.actionId);
if (n.length === t.length) return;
replacePendingNodeActionsForGeneration({
...e,
replacement: n
});
}
function clearRemovedNodeRuntimeState(e) {
pendingNodeActionsById.delete(e.nodeId);
clearNodePendingWork(e.nodeId);
invalidateNodeWakeState(e.nodeId);
e.context.nodeRegistry.updateSurface(e.nodeId, {
caps: [],
commands: [],
permissions: void 0
});
removeRemoteNodeInfo(e.nodeId);
}
//#endregion
export { removePendingNodeAction as a, listPendingNodeActions as i, clearRemovedNodeRuntimeState as n, replacePendingNodeActionsForGeneration as o, enqueuePendingNodeAction as r, reconcileRevokedDeviceWorker as s, acknowledgePendingNodeActions as t };