UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.35 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{s as resolveOpenClawStateSqlitePath}from"./openclaw-state-db.paths-CbgXfEg1.mjs";import{i as getActiveOpenClawStateDatabaseReadSnapshot,n as executeExistingOpenClawStateRead}from"./openclaw-state-db-readonly-Cd5_cxBs.mjs";import{r as captureOpenClawStateWorkerContext,t as captureOpenClawStateReadContext}from"./openclaw-state-worker-context-Dx8YjqO2.mjs";import{n as createAsyncLock}from"./json-files-BBQ_0Np0.mjs";import{t as captureDevicePairingPublication}from"./device-pairing-publication-DRSKz0lG.mjs";import{threadId}from"node:worker_threads";import{AsyncLocalStorage}from"node:async_hooks"; //!src/infra/device-pairing-lock.ts const owner=resolveGlobalSingleton(Symbol.for(`openclaw.devicePairingLock`),()=>({lock:createAsyncLock(),current:new AsyncLocalStorage,waits:new AsyncLocalStorage}));function withDevicePairingWaitObservation(e,t){return owner.waits.run(e,t)}async function withDevicePairingLock(e,t=`devicePairing`){if(owner.current.getStore()?.active)return e();const n=owner.waits.getStore();const r=n?owner.holder:void 0;const i=r?Date.now():0;return owner.lock(async()=>{const a={active:true,operation:t.slice(0,120),startedAt:Date.now()};owner.holder=a;if(n&&r){const e=a.startedAt-i;if(e>(n.longestWait?.waitMs??-1))n.longestWait={waitMs:e,holderAtEnqueue:{operation:r.operation,threadId,heldMs:i-r.startedAt}}}try{return await owner.current.run(a,e)}finally{a.active=false;owner.holder=void 0}})} //!src/infra/device-pairing-store-readonly.ts async function readPairing(e,t,n=false,r=false){const i=t?{env:{...process.env,OPENCLAW_STATE_DIR:t}}:{};const a=captureOpenClawStateWorkerContext(i);const o={path:a.admission.databasePath,env:a.environment};const s=n?void 0:getActiveOpenClawStateDatabaseReadSnapshot(o);const read=async()=>{const t=captureDevicePairingPublication(a.admission);const i=r&&!s?t.readNodes():void 0;if(i){a.admission.assertCurrent();return{reply:void 0,nodes:i}}let c;try{const r=e.type===`devicePairing.list`&&!s?{...e,publishedRevision:t.completeRevision()}:e;c=await executeExistingOpenClawStateRead(o,r,{current:n})}catch(e){if(!s)t.fail();throw e}a.admission.assertCurrent();if(s)return{reply:c,nodes:void 0};if(!t.isCurrent())return;if(c&&`bindings`in c)t.publish(c.revision,c.bindings,c.type===`devicePairing.list`);else if(!c)t.publish(`missing`,[],true);return{reply:c,nodes:r?t.readNodes():void 0}};const c=await read();if(c)return c;return withDevicePairingLock(async()=>{const e=await read();if(!e)throw new Error(`Device pairing read publication was replaced`);return e},e.type)}async function readDevicePairingNodeSnapshot(e){const{nodes:t}=await readPairing({type:`devicePairing.list`,nowMs:Date.now()},e,true,true);if(!t)throw new Error(`Unexpected pairing node snapshot reply`);return t}async function listDevicePairingStoreRecordsReadOnly(e,t=false){const{reply:n}=await readPairing({type:`devicePairing.list`,nowMs:Date.now()},e,t);if(!n)return{pending:[],paired:[]};return n.list}function loadPairedDevicePairingStoreRecordReadOnly(e,t){return readCurrentPairedDevice(e,e=>e,t)}async function readCurrentPairedDevice(e,t,n){const r=captureOpenClawStateReadContext(resolveOpenClawStateSqlitePath(n?{...process.env,OPENCLAW_STATE_DIR:n}:process.env));let i=captureDevicePairingPublication(r.admission).readDevice(e.trim());if(i===void 0){await readPairing({type:`devicePairing.lookup`,deviceId:e.trim()},n,true);i=captureDevicePairingPublication(r.admission).readDevice(e.trim());if(i===void 0)throw new Error(`Device pairing read publication was replaced`)}r.admission.assertCurrent();return t(i)}async function loadPendingDevicePairingStoreRecordReadOnly(e,t){const{reply:n}=await readPairing({type:`devicePairing.pending`,requestId:e,nowMs:Date.now()},t,true);if(!n)return null;return n.pending}async function loadBoundDeviceBootstrapContextReadOnly(e,t){const{reply:n}=await readPairing({type:`devicePairing.bootstrapContext`,input:e},t,true);if(!n)return null;return n.context}export{readCurrentPairedDevice as a,withDevicePairingWaitObservation as c,loadPendingDevicePairingStoreRecordReadOnly as i,loadBoundDeviceBootstrapContextReadOnly as n,readDevicePairingNodeSnapshot as o,loadPairedDevicePairingStoreRecordReadOnly as r,withDevicePairingLock as s,listDevicePairingStoreRecordsReadOnly as t};