openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
72 lines (71 loc) • 2.21 kB
JavaScript
import { i as runClaimableDedupeClaimLoop, n as createClaimableDedupe } from "../persistent-dedupe-BbUC1nSH.mjs";
//#region src/plugin-sdk/ingress-effect-once.ts
const INGRESS_EFFECT_ONCE_NAMESPACE_PREFIX = "ingress-effect-once";
var IngressEffectRunFailedError = class extends Error {
constructor() {
super("ingress effect failed before its durable commit");
this.name = "IngressEffectRunFailedError";
}
};
/**
* Create a durable per-event side-effect guard for channel ingress drains.
*
* Create one factory per ingress queue/account scope and give that scope a stable, unique
* `namespacePrefix`; `eventId` only needs to be unique within that queue. Storage failures
* reject instead of falling back to process memory.
*
* `ttlMs` must cover the maximum effect-commit-to-tombstone delay plus the channel's
* ingress tombstone retention. Older records are dead weight once the tombstone prevents
* replay. A process death after `run()` succeeds but before the claim commits can still
* execute the effect again on recovery, as can a storage failure during that commit.
*/
function createIngressEffectOnce(e) {
const t = createClaimableDedupe({
pluginId: e.pluginId,
namespacePrefix: INGRESS_EFFECT_ONCE_NAMESPACE_PREFIX,
ttlMs: e.ttlMs,
stateMaxEntries: e.stateMaxEntries,
memoryMaxSize: e.memoryMaxSize ?? e.stateMaxEntries,
onDiskError: (t) => {
e.onDiskError?.(t);
throw t;
}
});
return { runOnce: async (n) => {
const r = JSON.stringify([n.effect, n.eventId]);
const i = e.namespacePrefix;
if ((await runClaimableDedupeClaimLoop(() => t.claim(r, { namespace: i }), (e) => {
if (e instanceof IngressEffectRunFailedError) return true;
throw e;
})).kind === "duplicate") return { kind: "replayed" };
let a;
try {
a = await n.run();
} catch (e) {
t.release(r, {
namespace: i,
error: new IngressEffectRunFailedError()
});
throw e;
}
try {
await t.commit(r, { namespace: i });
} catch (e) {
try {
await t.forget(r, {
namespace: i,
onDiskError: (e) => {
throw e;
}
});
} catch {}
throw e;
}
return {
kind: "executed",
value: a
};
} };
}
//#endregion
export { createIngressEffectOnce };