openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
96 lines (95 loc) • 5.35 kB
JavaScript
import { D as readSqliteDatabaseScopedWriteToken } from "../../sqlite-database-admission-DQIqro3d.mjs";
import { o as runSqliteReadSnapshotSync } from "../../sqlite-transaction-BHHu6P5-.mjs";
import { a as getSqliteWorkerStateContext } from "../../sqlite-worker-state-context-CR8h__Vh.mjs";
import { a as readOpenClawAgentDatabaseIdentity } from "../../openclaw-agent-db-identity-C8MGU8lp.mjs";
import { n as getOpenClawAgentDatabaseIfOpen } from "../../openclaw-agent-db-borrower-BrziJlyo.mjs";
import "../../openclaw-agent-db-CwXW4hGa.mjs";
import { A as prepareSessionActorEntryPatch } from "../../session-accessor.sqlite-entry-read-csOyyi3K.mjs";
import { t as collectLifecycleIdentityChanges } from "../../session-accessor.sqlite-identity-CU9bTX7j.mjs";
import { n as hasPendingSessionTranscriptArchives } from "../../session-accessor.sqlite-archive-store-kernel-CuGqi3Eu.mjs";
import { d as selectProjectedLifecycleRemovals, o as finishProjectedLifecycleRemovalPlans, u as readProjectedLifecycleStore } from "../../session-accessor.sqlite-lifecycle-state-CnW2eZ4Y.mjs";
import { t as commitPreparedSessionEntryLifecycleMutationInDatabase } from "../../session-accessor.sqlite-projection-state-CdsB0SqS.mjs";
import { t as bindSqliteWorkerDatabase } from "../../sqlite-worker-binding-B4BB5u5i.mjs";
import { n as prepareSessionEntryReplacementPublication } from "../../session-accessor.sqlite-replacement-publication-7c_4oTwB.mjs";
import { i as transferSessionEntryWorkerCandidate } from "../../session-entry-patch.worker-C8AFkROr.mjs";
//#region src/config/sessions/session-lifecycle-projection.worker.ts
function commitSessionLifecycleProjection(e, { writeTransaction: t, admit: n, options: r }) {
return t("session.lifecycle.project", "Session lifecycle", (t) => {
const i = [];
const a = [];
const o = /* @__PURE__ */ new Map();
const s = /* @__PURE__ */ new Map();
const c = e.projected.upsertedEntries[0];
const l = e.projected.upsertedEntries.length === 1 && e.projected.removals.length === 0 && c && !c.resetBoundary ? prepareSessionActorEntryPatch(t, c.sessionKey) : void 0;
const u = commitPreparedSessionEntryLifecycleMutationInDatabase(t, e, e.removalPlans, {
resetScope: {
agentId: e.agentId,
path: t.path,
env: r.env
},
currentEntrySnapshot: l?.snapshot(),
postimages: o,
onArchived: (e, t) => s.set(e, t),
onResetBoundary: ({ sessionKey: e, sessionId: t, progressCardReset: n, projectionNeedsReconcile: r }) => {
if (n) i.push(e);
if (r) a.push(t);
}
});
const d = collectLifecycleIdentityChanges(e.projected, u.removedSessionKeys);
for (const [e, t] of s) if (!d.previous.has(e)) d.previous.set(e, t);
for (const e of d.current.keys()) if (!o.has(e)) d.current.delete(e);
for (const [e, t] of o) d.current.set(e, t.entry);
const f = {
kind: "session-lifecycle-projection",
result: u,
progressCardResetKeys: i,
projectionReconcileSessionIds: a,
publication: prepareSessionEntryReplacementPublication({
...d,
pendingArchiveRecovery: u.pendingArchives,
membershipInvalidatedKeys: [...u.removedSessionKeys, ...e.projected.upsertedEntries.flatMap(({ sessionKey: e, entry: t, expectedEntry: n }) => t.sessionId !== n?.sessionId ? [e] : [])],
maintenancePlans: u.maintenancePlans
}, t, {
captureFullFacts: true,
postimages: o
})
};
if (u.maintenancePlans.every((e) => e.archivedEntries.length === 0 && e.entryRemovals.length === 0)) l?.publish(o, f.publication);
return transferSessionEntryWorkerCandidate(t, n, f);
});
}
/** Private planning commands borrow the executor's admitted connection. */
function bindSqliteWorkerBackend(e, t) {
const n = getOpenClawAgentDatabaseIfOpen({
agentId: e.agentId,
path: t.databasePath,
env: getSqliteWorkerStateContext().environment
});
if (!n || n.db !== t.database || n.path !== t.databasePath) throw new Error("Session lifecycle planning lost its canonical database owner");
return bindSqliteWorkerDatabase(n.db, { execute(e) {
const prepare = () => {
if (e.type === "prepare") {
const t = e.input;
const r = t.upserts[0];
const i = t.retainedEntry !== void 0 && t.retainedEntry.writeToken === readSqliteDatabaseScopedWriteToken(n.db, t.retainedEntry.writeScopes);
const a = !i && !t.allowCanonicalRepair && t.removals.length === 0 && t.upserts.length === 1 && r && !r.relatedSessionKeys?.some((e) => e !== r.sessionKey.trim()) ? prepareSessionActorEntryPatch(n, r.sessionKey.trim())?.snapshot() : void 0;
const o = i ? {} : a && (r?.relatedSessionKeys === void 0 || !a.entry.parentSessionKey) ? { [a.sessionKey]: a.entry } : readProjectedLifecycleStore(n, t);
const s = readOpenClawAgentDatabaseIdentity(n).identity;
return {
store: o,
...i ? { reuseEntry: true } : {},
selected: selectProjectedLifecycleRemovals(n, o, t.removals),
archiveRecovery: typeof s === "string" ? {
pending: hasPendingSessionTranscriptArchives(n),
databaseIdentity: s
} : void 0
};
}
const t = e.input;
return finishProjectedLifecycleRemovalPlans(n, t.archiveDirectory, t.store, t.selected);
};
return e.type === "prepare" && e.input.removals.length === 0 ? prepare() : runSqliteReadSnapshotSync(n.db, prepare);
} });
}
//#endregion
export { bindSqliteWorkerBackend, commitSessionLifecycleProjection };