openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 3.32 kB
JavaScript
import{i as e,n as t}from"./transcript-global-singleton-DIJgobpU.mjs";import{n}from"./transcript-agent-id-DmscQLD6.mjs";import{o as r,s as i}from"./transcript-openclaw-agent-db.paths-DbFl2s5M.mjs";import{o as a}from"./transcript-session-key-normalization-3Mbu27cw.mjs";import{r as o}from"./transcript-session-key-MDDhuEG9.mjs";import{r as s}from"./transcript-session-accessor.sqlite-scope-helpers-LCM13IZK.mjs";import c from"node:path";import{AsyncLocalStorage as l}from"node:async_hooks";const u=t(Symbol.for(`openclaw.memorySessionActorOwners`)),d=e(Symbol.for(`openclaw.memorySessionActorHandles`),()=>/* @__PURE__ */ new WeakMap);function readMemorySessionActorOwner(e){return d.get(e)}function registerMemorySessionActorHandle(e,t){d.set(e,t)}function ownerLocation(e){return{agentId:n(e.agentId),path:c.resolve(e.path)}}function ownerKey(e){return JSON.stringify([e.agentId,e.path])}const f={list(){return[...u.values()]},get(e,t){let n=ownerLocation(e),r=ownerKey(n),i=u.get(r);return i||(i=t(n),u.set(r,i)),i},read(e){return u.get(ownerKey(ownerLocation(e)))},closeSession(e,t){u.get(ownerKey(ownerLocation(e)))?.closeSession(t)},closeDatabase(e){let t=ownerKey(ownerLocation(e));u.get(t)?.close(),u.delete(t)},reset(){for(let e of u.values())e.close();u.clear()}};function isMemoryBinding(e){return e.actor.target.database.kind===`memory`&&e.actor.storage!==void 0}const p=e(Symbol.for(`openclaw.sessionActorStorageBinding`),()=>new l);function getSessionActorStorageBinding(e){let t=e.sessionActor??p.getStore();if(!t)return;let n=e.sessionKey?s(e.sessionKey,t.agentId):void 0;if(n&&n!==t.actor.target.sessionKey){if(e.sessionActor||a(n))throw Error(`Session storage binding belongs to another session`);return}let i=r(e.storePath,{agentId:e.agentId});if(!e.storePath||i||a(e.sessionKey)){if(e.agentId&&e.agentId!==t.agentId||i&&i.path!==c.resolve(t.path))throw Error(`Session storage binding belongs to another owner`);if(!isMemoryBinding(t))throw Error(`Session storage binding requires a memory actor`);return t}}function captureSessionActorStorageOwner(e,t){let n=e.sessionActor??p.getStore(),s=r(e.storePath,{agentId:e.agentId});if(e.storePath&&!s&&!a(e.sessionKey)||!n&&(!t||!a(e.sessionKey)&&!s)||e.sessionKey&&!a(e.sessionKey)&&!s)return;let l=e.agentId??s?.agentId??(e.sessionKey?o(e.sessionKey):n?.agentId);if(!l)return;let u=n&&c.resolve(n.path,`../../../..`),d=s?.path??(n?.agentId===l&&!e.env?n.path:void 0)??i({agentId:l,env:e.env??(u?{OPENCLAW_STATE_DIR:u}:void 0)});if(u&&c.resolve(d,`../../../..`)!==u)throw Error(`Session storage owner belongs to another state root`);let m=n?.authority??t,h=t&&n?{authorize:(...e)=>m.authorize(...e),assertCurrent(){m.assertCurrent(),t.assertCurrent()}}:m;return{owner:(n?.agentId===l&&n.path===d?readMemorySessionActorOwner(n.actor):void 0)??f.read({agentId:l,path:d}),binding:n,authority:h,agentId:l,path:d}}function readCapturedSessionActorEntry(e,t){if(e.owner)return e.owner.readSession(t,e.authority)?.entry;let n=e.binding;if(n&&n.agentId===e.agentId&&c.resolve(n.path)===c.resolve(e.path))return n.actor.storage?.readCurrent({type:`session.entry.read`,input:{sessionKey:t}},e.authority)}export{registerMemorySessionActorHandle as a,f as i,getSessionActorStorageBinding as n,readCapturedSessionActorEntry as r,captureSessionActorStorageOwner as t};