openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.21 kB
JavaScript
import{r as isIncognitoSessionKey}from"./session-key-DYtwLLn1.mjs";import{i as isIncognitoOpenClawAgentSqlitePath}from"./openclaw-agent-db.paths-SkzMsgQ0.mjs";import"./openclaw-agent-db-BUym2U31.mjs";import{i as buildSessionCreationStamp}from"./session-entry-provenance-DZd--atF.mjs";import{r as resolveInternalSessionEffectsIdentity}from"./internal-session-key-D-nF_UyM.mjs";import{i as readSessionEntryReadOnlyInWorker}from"./session-entry-read-runtime-kAgwDw0B.mjs";import{a as readCapturedSessionActorEntry,t as captureSessionActorStorageOwner}from"./session-actor-storage-binding-Bq_IHhu0.mjs";import{a as captureSessionEntryReadScope}from"./session-entry-read-ordered-CeFDTZ-N.mjs";import"./session-accessor-CY8UYP4m.mjs";import{n as withSessionActorStorage}from"./session-actor-storage-acquisition-B3-E4SDM.mjs";import{d as forkSessionTranscriptFromParent,g as applySessionEntryLifecycleMutation,n as createSessionEntryWithTranscript}from"./session-accessor.entry-mutation-CQIwywNM.mjs";import{n as deleteSessionEntryLifecycle}from"./session-accessor.sqlite-lifecycle-B8uZRbgv.mjs";
//!src/agents/internal-session-effects.ts
function resolveInternalSessionEffectsTarget(e){const t=Boolean(captureSessionActorStorageOwner(e,{assertCurrent(){},authorize(){}}))||isIncognitoOpenClawAgentSqlitePath(e.storePath,{agentId:e.agentId});return{agentId:e.agentId,storePath:e.storePath,...resolveInternalSessionEffectsIdentity({agentId:e.agentId,runId:e.runId,...t?{incognito:true}:{}})}}function toInternalSessionEffectsTarget(e,t){return{agentId:e.agentId,sessionId:t.sessionId,sessionKey:e.sessionKey,storePath:e.storePath,sessionEntry:t,sessionFile:e.sessionKey}}async function prepareInternalSessionEffectsSession(e){e.commitGuard?.();const t=resolveInternalSessionEffectsTarget(e);const{env:n}=captureSessionEntryReadScope(t);const r={...t,env:n};const i={assertCurrent(){e.commitGuard?.()},authorize(){}};const a=captureSessionActorStorageOwner(r,i);const prepare=async()=>{const t=a?readCapturedSessionActorEntry(a,r.sessionKey):await readSessionEntryReadOnlyInWorker(r,e.commitGuard);e.commitGuard?.();if(t?.sessionId===r.sessionId)return toInternalSessionEffectsTarget(r,t);if(e.source)await forkSessionTranscriptFromParent({agentId:e.source.agentId,parentEntry:{sessionId:e.source.sessionId,updatedAt:Date.now()},parentSessionKey:e.source.sessionKey,sessionKey:r.sessionKey,storePath:e.source.storePath,targetSessionId:r.sessionId,targetStorePath:e.storePath,commitGuard:e.commitGuard});const n=Date.now();const i=await createSessionEntryWithTranscript(r,()=>({ok:true,entry:{...buildSessionCreationStamp({via:`internal`,actor:{type:`system`}}),delivery:{kind:`internal`},sessionId:r.sessionId,...isIncognitoSessionKey(r.sessionKey)?{incognito:true}:{},sessionStartedAt:n,updatedAt:n}}),{cwd:e.cwd,commitGuard:e.commitGuard,followRegistration:e.followRegistration});if(!i.ok)throw new Error(`Failed to create internal session for run ${e.runId}`);return toInternalSessionEffectsTarget(r,i.entry)};if(!a)return prepare();const o=await withSessionActorStorage(r,{authority:i,lifetime:{assertCurrent:()=>i.assertCurrent(),assertReadable:()=>i.assertCurrent()},create:true},prepare);if(!o)throw new Error(`Internal memory session could not be acquired`);return o}function createInternalSessionEffectsCleanup(e){const t=e.enabled?/* @__PURE__ */ new Map:void 0;const n=e.enabled&&e.storePath?captureSessionActorStorageOwner(e,{assertCurrent(){},authorize(){}}):void 0;const track=e=>{if(!t||!e?.sessionKey||!e.storePath)return;t.set(`${e.storePath}\n${e.sessionKey}`,e)};if(t&&e.storePath)track(resolveInternalSessionEffectsTarget({agentId:e.agentId,runId:e.runId,storePath:e.storePath}));const cleanup=async()=>{if(!t)return;for(const n of t.values())try{await removeInternalSessionEffectsSession(n)}catch(t){e.onError(t)}};return{track,cleanup:async()=>{if(n&&!n.owner&&!n.binding)return;n?.owner?.assertCurrent();await cleanup()}}}async function removeInternalSessionEffectsSession(e,t){if(!e?.sessionKey||!e.storePath)return;const n=captureSessionActorStorageOwner(e,{assertCurrent(){},authorize(){}});const{env:r}=captureSessionEntryReadScope({...e,sessionKey:e.sessionKey});const i={...e.agentId?{agentId:e.agentId}:{},storePath:e.storePath,env:r};const a=t?n?readCapturedSessionActorEntry(n,e.sessionKey):await readSessionEntryReadOnlyInWorker({...i,sessionKey:e.sessionKey}):void 0;if(t&&(!a||a.sessionId!==e.sessionId||a.lifecycleRevision!==t.lifecycleRevision||a.activeWriterRunId!==t.activeWriterRunId))return;if(n){const t=e.sessionKey;await withSessionActorStorage({...i,sessionKey:t},{authority:n.authority,lifetime:{assertCurrent(){},assertReadable(){}}},()=>deleteSessionEntryLifecycle({...i,target:{canonicalKey:t,storeKeys:[t]},archiveTranscript:false,deleteTranscriptWithoutArchive:true,expectedSessionId:e.sessionId,expectedEntry:a}));return}await applySessionEntryLifecycleMutation({...i,removals:[{sessionKey:e.sessionKey,...e.sessionId?{expectedSessionId:e.sessionId}:{},...a?{expectedEntry:a}:{},archiveRemovedTranscript:false}],skipMaintenance:true})}export{prepareInternalSessionEffectsSession as n,removeInternalSessionEffectsSession as r,createInternalSessionEffectsCleanup as t};