openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.6 kB
JavaScript
import{M as readSqliteDatabaseScopedWriteToken}from"../../sqlite-database-admission-B3KDkUt-.mjs";import{o as runSqliteReadSnapshotSync}from"../../sqlite-transaction-CON-dGDv.mjs";import{a as getSqliteWorkerStateContext}from"../../sqlite-worker-state-context-DJ4guxKh.mjs";import{o as readOpenClawAgentDatabaseIdentity}from"../../openclaw-agent-db-identity-D6YwE2-y.mjs";import{n as getOpenClawAgentDatabaseIfOpen}from"../../openclaw-agent-db-borrower-CrL9dJmX.mjs";import"../../openclaw-agent-db-BUym2U31.mjs";import{k as prepareSessionActorEntryPatch}from"../../session-accessor.sqlite-entry-read-DBsgM_K3.mjs";import{t as collectLifecycleIdentityChanges}from"../../session-accessor.sqlite-identity-CQTxPrTw.mjs";import{n as hasPendingSessionTranscriptArchives}from"../../session-accessor.sqlite-archive-store-kernel-CD-ksLSt.mjs";import{d as selectProjectedLifecycleRemovals,o as finishProjectedLifecycleRemovalPlans,u as readProjectedLifecycleStore}from"../../session-accessor.sqlite-lifecycle-state-DNjDr2Aq.mjs";import{t as commitPreparedSessionEntryLifecycleMutationInDatabase}from"../../session-accessor.sqlite-projection-state-BHmR2pBK.mjs";import{t as bindSqliteWorkerDatabase}from"../../sqlite-worker-binding-CxA1GbiJ.mjs";import{n as prepareSessionEntryReplacementPublication}from"../../session-accessor.sqlite-replacement-publication-DWaC81mD.mjs";import{i as transferSessionEntryWorkerCandidate}from"../../session-entry-patch.worker-1XVZD5Dz.mjs";
//!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)})}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)}})}export{bindSqliteWorkerBackend,commitSessionLifecycleProjection};