openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
108 lines (107 loc) • 3.3 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as createDeferredCore } from "./deferred-D0La5CRk.mjs";
import { d as AgentDatabaseExecutionAdmissionClosedError } from "./openclaw-agent-db-resources-Dp8HvLsg.mjs";
import { c as takeSqliteWorkerOperationAdmissionAttachment, i as createSqliteWorkerOperationAdmission } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/state/openclaw-agent-execution-preparation.ts
function readAgentDatabaseRequestPreparation() {
const e = takeSqliteWorkerOperationAdmissionAttachment();
if (!isRecord(e) || e.kind !== "agent-execution" || typeof e.startupJournal !== "boolean") throw new Error("Agent execution requires its request-local preparation facts");
return {
startupJournal: e.startupJournal,
startupCancellation: e.startupCancellation,
deletion: e.deletion
};
}
/** Cancellation belongs to Gateway startup, never the first caller of shared preparation. */
function agentDatabaseStartupInspectionSignal(e) {
const t = new AbortController();
const abort = () => t.abort(/* @__PURE__ */ new Error("Gateway stopped during agent database inspection"));
if (Atomics.load(e, 0) !== 0) abort();
else Promise.resolve(Atomics.waitAsync(e, 0, 0).value).then(abort);
return t.signal;
}
/** Physical preparation outlives individual waits; registry notifications retain caller context. */
function createSharedAgentDatabasePreparation(e) {
const t = /* @__PURE__ */ new Set();
let n;
const r = /* @__PURE__ */ new Set();
const i = [];
return {
source: {
assertCurrent() {
e();
let t;
for (const e of r) try {
e.assertCurrent();
return;
} catch (e) {
t ??= { error: e };
}
if (t) throw t.error;
throw new AgentDatabaseExecutionAdmissionClosedError("Agent database preparation has no current caller");
},
onRegistryChange(e) {
i.push(e);
for (const t of r) t.notify(e);
},
createAdmission: (e) => () => ({
nativeLocations: e.nativeLocations,
admission: createSqliteWorkerOperationAdmission((t, n) => {
e.authorize(t);
if (!n()) throw new Error("Agent database shared opening admission expired");
}, e.attachment)
})
},
retain() {
const e = createDeferredCore();
t.add(e.promise);
return () => {
t.delete(e.promise);
e.resolve();
};
},
beginNativeOpen() {
const e = createDeferredCore();
n = e.promise;
return () => {
n = void 0;
e.resolve();
};
},
getPendingNativeOpen: () => n,
getPendingPreparation: () => {
if (t.size === 0) return;
return (async () => {
while (t.size > 0) await Promise.all(t);
})();
},
follow(e, t) {
const n = AsyncLocalStorage.snapshot();
let a;
const notify = (t) => {
try {
n(() => e.onRegistryChange?.(t));
} catch (e) {
a = { error: e };
}
};
for (const e of i) notify(e);
const o = {
notify,
assertCurrent: () => n(t)
};
r.add(o);
return {
assertPublished() {
if (a) throw a.error;
},
release() {
r.delete(o);
}
};
}
};
}
//#endregion
export { createSharedAgentDatabasePreparation as n, readAgentDatabaseRequestPreparation as r, agentDatabaseStartupInspectionSignal as t };