UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.02 kB
import{o as resolveGlobalSingleton}from"./global-singleton-DolL9xGt.mjs";import{r as settleWithCleanup}from"./error-graph-internal-Bt2pFJBM.mjs";import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import"./paths-D_FOvGon.mjs";import{o as resolveRuntimeProcessEntrypointUrl}from"./runtime-process-url-DJ272Tuo.mjs";import{a as getOpenClawDatabaseMaintenanceScope}from"./openclaw-state-maintenance-context-D-pYfco8.mjs";import{g as resolveDatabasePathKeyAsync}from"./sqlite-worker-identity-CikEUuqF.mjs";import{t as resolveClientCredentialStorePath}from"./client-credential-paths-D__QHGyH.mjs";import{n as enqueueKeyedTask}from"./keyed-async-queue-qBcorzrt.mjs";import path from"node:path"; //!src/infra/client-credential-async.ts const queues=resolveGlobalSingleton(Symbol.for(`openclaw.clientCredentialOperations`),()=>/* @__PURE__ */ new Map);const runtime=resolveGlobalSingleton(Symbol.for(`openclaw.clientCredentialWorker`),()=>({resources:/* @__PURE__ */ new Map}),()=>closeClientCredentialWorker());function closeClientCredentialWorker(){runtime.closing??=(async()=>{await Promise.all(queues.values());await runtime.pool?.close();runtime.pool=void 0;for(const e of runtime.resources.values())e.unregister();runtime.resources.clear()})().finally(()=>{runtime.closing=void 0});return runtime.closing}async function runClientCredentialOperationAsync(e,t,n={}){return runClientCredentialTaskAsync(e,t,n)}async function prepareClientCredentialStoreAsync(e){return(await runClientCredentialTaskAsync(e,{kind:`startup.prepare`})).state}async function runClientCredentialTaskAsync(e,t,n={}){n.signal?.throwIfAborted();n.assertCurrent?.();const r=await enqueueKeyedTask({tails:queues,key:`\0credential-root-resolution`,task:()=>resolveDatabasePathKeyAsync(path.resolve(resolveStateDir(e)))});n.signal?.throwIfAborted();n.assertCurrent?.();const i={HOME:e.HOME,USERPROFILE:e.USERPROFILE,OPENCLAW_PROFILE:e.OPENCLAW_PROFILE,OPENCLAW_STATE_DIR:r};const a=resolveClientCredentialStorePath({OPENCLAW_STATE_DIR:r});const assertCurrent=()=>{n.signal?.throwIfAborted();n.assertCurrent?.()};assertCurrent();return enqueueKeyedTask({tails:queues,key:r,task:async()=>{const offline=async()=>{const[{registerOpenClawAgentDatabaseReadCandidateResource:e},{hydrateOpenClawStateWorkerError:i,retainOpenClawStateWorkerErrorPayload:o},{acquireStateDatabaseSchemaLease:s,captureStateDatabaseAccess:c},{retainSqliteWriteAdmissionService:l},{createSqliteWorkerOperationAdmission:u},{WorkerTaskPool:d}]=await Promise.all([import(`./openclaw-agent-db-resources-CW_TaCgD.mjs`),import(`./openclaw-state-worker-error-BvDmfjYm.mjs`),import(`./gateway-state-owner-BcD6afkY.mjs`),import(`./sqlite-transaction-BvjJ8F7l.mjs`),import(`./sqlite-worker-operation-admission-FtaDWN0a.mjs`),import(`./worker-task-pool-rzaYfBJL.mjs`)]);assertCurrent();if(runtime.closing)throw new Error(`Client credential worker is closing.`);let f=runtime.resources.get(a);if(!f){const t=await c(a);assertCurrent();if(runtime.closing)throw new Error(`Client credential worker is closing.`);f={access:t,unregister:e({path:a,revoke(){closeClientCredentialWorker().catch(()=>{})},close:closeClientCredentialWorker})};runtime.resources.set(a,f)}const p=getOpenClawDatabaseMaintenanceScope();const m=await f.access.prepareAccess({maintenanceScope:p});assertCurrent();let h;const assertAccess=()=>{assertCurrent();p?.assertAdmission();m({maintenanceScope:p,schemaLease:h})};const g=u((e,t)=>{assertAccess();if(!t())throw new Error(`Client credential operation authority expired.`)});g.bindDatabaseAuthority({databasePath:a,assertRequest:assertCurrent,assertAccess,acquireSchema(){assertAccess();const acquire=()=>s(a);const e=p?p.run(acquire):acquire();h=e;p?.own(e,`shared-resources`,()=>e.release());return e}});const _=l([a],()=>g.service());return settleWithCleanup((async()=>{try{runtime.pool??=new d({workerUrl:resolveRuntimeProcessEntrypointUrl(`clientCredentials`),workerClass:`singleton`});const e=await runtime.pool.run({root:r,operation:t,admission:g.port},{signal:n.signal,transferList:e=>[e.admission]});assertAccess();if(e.kind===`error`){const t=/* @__PURE__ */ new Error(`Client credential operation failed`);o(t,e.error);throw i(t,{includeOrdinary:true})}return e}catch(e){throw g.failure??e}})(),async()=>{try{await g.finish()}finally{_()}},`Client credential operation and admission cleanup failed`)};const e=getOpenClawDatabaseMaintenanceScope();if(e?.ownsSchemaMaintenance)e.assertAdmission();else{const{requestClientCredentialsFromApp:e}=await import(`./client-credential-ipc-BV96dopv.mjs`);assertCurrent();const r=await e(i,t.kind===`startup.prepare`?{kind:`identity.read`,identity_key:`primary`}:t,n.signal,assertCurrent);assertCurrent();if(r)return t.kind===`startup.prepare`?{kind:`preparation`,state:`ready`}:r}const o=await offline();if(o.kind===`result`)return o.result;throw new Error(`Client credentials are busy; retry after the current owner finishes.`)}})}export{runClientCredentialOperationAsync as n,prepareClientCredentialStoreAsync as t};