openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
97 lines (96 loc) • 4.38 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { a as getOrLoadSqliteDatabaseAdmissionForPath, b as publishSqliteDatabaseAdmission, o as getSqliteDatabaseAdmission, v as prepareSqliteDatabaseAdmissionForPath } from "./sqlite-database-admission-DQIqro3d.mjs";
//#region src/state/agent-deletion-journal-status.ts
const discoveryKey = {
name: "agent.deletion-discovery",
schemaDependent: true,
read: (e) => isRecord(e) ? e : void 0
};
function getAgentDeletionDiscovery(e) {
return getSqliteDatabaseAdmission(e, discoveryKey) ?? void 0;
}
function getPreparedAgentDeletionDiscovery(e) {
return getOrLoadSqliteDatabaseAdmissionForPath(e, discoveryKey, () => void 0) ?? void 0;
}
/** Recovery receipt writers invalidate the same discovery fact as journal writers. */
function invalidateAgentDeletionDiscovery(e) {
publishSqliteDatabaseAdmission(e, discoveryKey, null);
}
/** A slow read reply cannot replace a newer journal commit. */
function seedAgentDeletionDiscovery(e, t) {
if (t.status !== "unavailable") getOrLoadSqliteDatabaseAdmissionForPath(e, discoveryKey, () => t);
}
/** Journal writers update the prepared discovery fact in their own transaction. */
function updateAgentDeletionDiscovery(e, t, n, r) {
if (t.status === "unavailable") {
publishSqliteDatabaseAdmission(e, discoveryKey, null);
return;
}
const i = t.status === "present" ? t : void 0;
const a = (i?.entries ?? []).filter((e) => e.agentId !== n);
const o = r(i?.entries.find((e) => e.agentId === n));
if (o) {
a.push(o);
a.sort((e, t) => e.agentId.localeCompare(t.agentId));
}
const s = i?.held ?? [];
publishSqliteDatabaseAdmission(e, discoveryKey, a.length || s.length ? {
status: "present",
entries: a,
held: s
} : { status: "empty" });
}
function statusKey(e) {
return {
name: `agent.deletion-status:${normalizeAgentId(e)}`,
read: (e) => e === "absent" || e === "pending" || e === "complete" ? e : void 0
};
}
/** The journal writer publishes after commit through the existing database-fact transport. */
function publishAgentDeletionJournalStatus(e, t, n) {
publishSqliteDatabaseAdmission(e, statusKey(t), n);
}
/** Offline repairs and raw fixture mutations discard only the affected agent's fact. */
function invalidateAgentDeletionJournalStatus(e, t) {
publishSqliteDatabaseAdmission(e, statusKey(t), null);
invalidateAgentDeletionDiscovery(e);
}
function getAgentDeletionJournalStatus(e, t) {
return getSqliteDatabaseAdmission(e, statusKey(t)) ?? void 0;
}
/** Synchronous guards consume the same committed fact as asynchronous turn admission. */
function getOrLoadAgentDeletionJournalStatus(e, t, n) {
return getOrLoadSqliteDatabaseAdmissionForPath(e, statusKey(t), n) ?? void 0;
}
/** Keep the prepared key; the admission owner checks the live fact's revision on every effect. */
function prepareAgentDeletionJournalStatus(e, t) {
const n = statusKey(t);
const r = prepareSqliteDatabaseAdmissionForPath(e, n);
return (e) => (e ? getSqliteDatabaseAdmission(e, n) : r()) ?? void 0;
}
const pendingLoads = resolveGlobalSingleton(Symbol.for("openclaw.agentDeletionJournalStatusLoads"), () => /* @__PURE__ */ new Map());
/** First admission loads once; journal writes replace the shared fact before observers run. */
async function loadAgentDeletionJournalStatus(e, t, n) {
const r = statusKey(t);
const i = getOrLoadAgentDeletionJournalStatus(e, t, () => void 0);
if (i) return i;
const a = JSON.stringify([e, r.name]);
let o = pendingLoads.get(a);
if (!o) {
o = (async () => {
const r = await n();
return getOrLoadAgentDeletionJournalStatus(e, t, () => r) ?? r;
})();
pendingLoads.set(a, o);
}
try {
return await o;
} finally {
if (pendingLoads.get(a) === o) pendingLoads.delete(a);
}
}
//#endregion
export { invalidateAgentDeletionDiscovery as a, prepareAgentDeletionJournalStatus as c, updateAgentDeletionDiscovery as d, getPreparedAgentDeletionDiscovery as i, publishAgentDeletionJournalStatus as l, getAgentDeletionJournalStatus as n, invalidateAgentDeletionJournalStatus as o, getOrLoadAgentDeletionJournalStatus as r, loadAgentDeletionJournalStatus as s, getAgentDeletionDiscovery as t, seedAgentDeletionDiscovery as u };