openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.99 kB
JavaScript
import{r as isIncognitoSessionKey}from"./session-key-DYtwLLn1.mjs";import"./session-key-BPgKfU2m.mjs";import{s as registerOpenClawAgentDatabaseAsyncResource}from"./openclaw-agent-db-resources-KCMTIo4b.mjs";import{v as retainPreparedSessionEntryPredicate}from"./session-accessor.sqlite-entry-cache-publication-state-D1mC85IV.mjs";import{a as resolveSessionStoreIdentity}from"./session-store-key-DZXBwthg.mjs";import{l as hasSessionReadAccessChanged}from"./session-sharing-policy-CQ0Zqotl.mjs";import{r as PreparedModelRuntimePublicationSupersededError}from"./prepared-model-runtime-admission-errors-DZto8oll.mjs";import{t as chatMetadataSessionFields}from"./chat-metadata-contract-mWTG_Ilk.mjs";import"./prepared-model-runtime.errors-CJIB8kC0.mjs";import{u as withGatewaySessionEntry}from"./session-utils-store-CbjJRFJe.mjs";import{t as retainGatewaySessionEntryReadOnly}from"./session-utils-read-lifetime-zMibBQo6.mjs";import{isDeepStrictEqual}from"node:util";
//!src/gateway/server-methods/chat-metadata-session-read.ts
function sameMetadataEntry(e,t){return e&&t?!hasSessionReadAccessChanged(e,t)&&chatMetadataSessionFields.every(n=>isDeepStrictEqual(e[n],t[n])):e===t}async function prepareChatMetadataSessionRead(e){const changed=()=>new PreparedModelRuntimePublicationSupersededError(`Session changed while preparing its metadata. Retry the request.`);let t=true;let n;const r=[];const i=/* @__PURE__ */ new Set;const release=()=>{t=false;for(const e of r.splice(0).toReversed())e()};const assertRefreshable=()=>{e.assertRequestCurrent();if(!t||n?.canRefresh()===false)throw changed()};const isCurrent=()=>t&&n?.isCurrent()!==false;const assertCurrent=()=>{assertRefreshable();if(!isCurrent())throw changed()};const a={agentId:e.agentId,projection:[]};try{if(isIncognitoSessionKey(resolveSessionStoreIdentity(e).canonicalKey)){const t=await retainGatewaySessionEntryReadOnly(e.sessionKey,e.agentId,sameMetadataEntry,e.cfg);r.push(t.release);const assertNativeCurrent=()=>{assertRefreshable();if(!t.isCurrentAtResponse())throw changed()};assertNativeCurrent();return{selected:t,isCurrent:t.isCurrent,assertCurrent:assertNativeCurrent,beforeRequest:assertNativeCurrent,release,async withCurrent(e){assertNativeCurrent();return await e()}}}const t=await withGatewaySessionEntry(e.sessionKey,a,e=>{assertCurrent();const t=new Map([{agentId:e.readSource?.agentId??e.agentId,path:e.storePath},...e.readSource?[e.readSource]:[],...e.capturedReadSources??[]].map(e=>[e.path,e]));for(const e of t.values())r.push(registerOpenClawAgentDatabaseAsyncResource({...e,revoke:release,close:async()=>{release();await Promise.allSettled(i)}}));if(e.capturedReadSource){if(typeof e.capturedReadSource.databaseIdentity!==`string`)throw changed();n=retainPreparedSessionEntryPredicate({databaseIdentity:`file:${e.capturedReadSource.databaseIdentity}`,sessionKey:e.legacyKey??e.canonicalKey,entry:e.entry,matches:sameMetadataEntry});r.push(n.release)}return e},e.cfg,assertCurrent);assertCurrent();const matchesSelected=e=>e.agentId===t.agentId&&e.canonicalKey===t.canonicalKey&&e.legacyKey===t.legacyKey&&e.storePath===t.storePath&&isDeepStrictEqual(e.readSource,t.readSource)&&isDeepStrictEqual(e.capturedReadSource,t.capturedReadSource)&&isDeepStrictEqual(e.capturedReadSources,t.capturedReadSources)&&sameMetadataEntry(t.entry,e.entry);return{selected:t,isCurrent,assertCurrent,beforeRequest:assertCurrent,release,async withCurrent(t){assertRefreshable();const r=n?.captureRevision();let o;let s;const c=withGatewaySessionEntry(e.sessionKey,a,e=>{assertRefreshable();if(!matchesSelected(e)||n&&!n.acknowledge(e.entry,r))throw changed();assertCurrent();o=(async()=>await t())();const a=o;i.add(a);const settled=()=>i.delete(a);a.then(settled,e=>{s??={error:e};settled()});return{value:a}},e.cfg,assertRefreshable);try{return await(await c).value}catch(e){s??={error:e};release();await Promise.allSettled(o?[o]:[]);throw s.error}}}}catch(e){release();throw e}}export{prepareChatMetadataSessionRead as t};