UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.8 kB
import{a as executeSqliteQueryTakeFirstSync,i as executeSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{a as tableExists}from"./openclaw-state-db-schema-helpers-rYTimzjJ.mjs";import{g as withExistingOpenClawStateDatabaseReadOnly}from"./openclaw-state-db-readonly-Cd5_cxBs.mjs";import{o as deferSqliteWorkerCommitReceipt}from"./sqlite-worker-operation-settlement-Cl93BzUc.mjs";import{p as requestSqliteWorkerOperationAdmission}from"./sqlite-worker-operation-admission-BZvXBQev.mjs";import{l as runOpenClawStateWriteTransaction}from"./openclaw-state-db-BlgxYkYw.mjs";import{m as ensureNodeWorkerPreparedWorkspaceSchema}from"./openclaw-state-db-schema-additive-DDOLEjcV.mjs";import{t as nodePreparedWorkspacePublication}from"./node-worker-prepared-workspace-publication-CBNAP2QK.mjs"; //!src/node-host/node-worker-prepared-workspace-store.kernel.ts const TABLE=`node_worker_prepared_workspaces`;function query(e){return getNodeSqliteKysely(e)}function selectRow(e,t){return executeSqliteQueryTakeFirstSync(e,query(e).selectFrom(TABLE).selectAll().where(`preparation_key`,`=`,t))}function requireUnchanged(e,t){if(!e||JSON.stringify(e)!==JSON.stringify(t))throw new Error(`INVALID_REQUEST: prepared workspace ownership changed`);return e}var NodeWorkerPreparedWorkspaceKernel=class{constructor(e){this.options=e}write(e,t){return runOpenClawStateWriteTransaction(({db:e})=>{requestSqliteWorkerOperationAdmission({stage:`transaction`,facts:{kind:`node-worker-journal`}});const{result:n,receipt:r}=nodePreparedWorkspacePublication.capture(e,()=>t(e));deferSqliteWorkerCommitReceipt(e,{kind:`node-prepared-workspace`,receipt:nodePreparedWorkspacePublication.bound(r)},r.facts.size===0?`settlement`:`commit`);return n},this.options,{operationLabel:e})}find(e){return withExistingOpenClawStateDatabaseReadOnly(({db:t})=>{if(!tableExists(t,TABLE))return;const n=executeSqliteQuerySync(t,query(t).selectFrom(TABLE).selectAll().limit(2)).rows;if(n.length>1||n[0]&&n[0].environment_id!==e)throw new Error(`INVALID_REQUEST: this prepared node belongs to another environment`);return n[0]},this.options)}list(e){return withExistingOpenClawStateDatabaseReadOnly(({db:t})=>{if(!tableExists(t,TABLE))return[];return executeSqliteQuerySync(t,query(t).selectFrom(TABLE).selectAll().where(`gateway_namespace`,`=`,e)).rows},this.options)??[]}register(e){return this.write(`prepared-workspace.register`,t=>{ensureNodeWorkerPreparedWorkspaceSchema(t);const n=executeSqliteQueryTakeFirstSync(t,query(t).selectFrom(TABLE).selectAll().limit(1));if(n){if(n.state!==`available`||n.session_id!==null||n.session_key!==null||n.owner_epoch!==null||n.bound_at_ms!==null||n.preparation_key!==e.preparationKey||n.cache_key!==e.cacheKey||n.gateway_namespace!==e.gatewayNamespace||n.environment_id!==e.environmentId||n.workspace_dir!==e.workspaceDir||n.home_dir!==e.homeDir)throw new Error(`INVALID_REQUEST: this node already owns a prepared workspace`);if(n.source_manifest_ref===e.sourceManifestRef&&n.prepared_manifest_ref===e.preparedManifestRef)return n;const r={...n,source_manifest_ref:e.sourceManifestRef,prepared_manifest_ref:e.preparedManifestRef};executeSqliteQuerySync(t,query(t).updateTable(TABLE).set({source_manifest_ref:e.sourceManifestRef,prepared_manifest_ref:e.preparedManifestRef}).where(`preparation_key`,`=`,e.preparationKey));nodePreparedWorkspacePublication.stagePostimages(t,[r]);return r}const r={preparation_key:e.preparationKey,cache_key:e.cacheKey,gateway_namespace:e.gatewayNamespace,environment_id:e.environmentId,workspace_dir:e.workspaceDir,home_dir:e.homeDir,source_manifest_ref:e.sourceManifestRef,prepared_manifest_ref:e.preparedManifestRef,state:`available`,session_id:null,session_key:null,owner_epoch:null,created_at_ms:Date.now(),bound_at_ms:null,retired_at_ms:null};executeSqliteQuerySync(t,query(t).insertInto(TABLE).values(r));nodePreparedWorkspacePublication.stagePostimages(t,[r]);return r})}bind(e){return this.write(`prepared-workspace.bind`,t=>{if(!tableExists(t,TABLE))throw new Error(`INVALID_REQUEST: prepared workspace registration is missing`);const n=selectRow(t,e.preparationKey);if(!n||n.cache_key!==e.cacheKey||n.gateway_namespace!==e.gatewayNamespace||n.environment_id!==e.environmentId)throw new Error(`INVALID_REQUEST: prepared workspace registration does not match this environment`);if(n.state===`bound`&&n.session_id===e.sessionId&&n.session_key===e.sessionKey&&n.owner_epoch===e.ownerEpoch)return n;if(n.state!==`available`||n.session_id!==null||n.session_key!==null||n.owner_epoch!==null||n.bound_at_ms!==null)throw new Error(`INVALID_REQUEST: prepared workspace has already been consumed`);const r={...n,state:`bound`,session_id:e.sessionId,session_key:e.sessionKey,owner_epoch:e.ownerEpoch,bound_at_ms:Math.max(Date.now(),n.created_at_ms)};executeSqliteQuerySync(t,query(t).updateTable(TABLE).set(r).where(`preparation_key`,`=`,e.preparationKey));nodePreparedWorkspacePublication.stagePostimages(t,[r]);return r})}completeMutation(e){this.write(`prepared-workspace.finish-mutation`,t=>{requireUnchanged(selectRow(t,e.preparation_key),e);executeSqliteQuerySync(t,query(t).updateTable(TABLE).set({state:`bound`}).where(`preparation_key`,`=`,e.preparation_key));nodePreparedWorkspacePublication.stagePostimages(t,[{...e,state:`bound`}])})}retire(e,t=false){return this.write(`prepared-workspace.retire`,n=>{const r=requireUnchanged(selectRow(n,e.preparation_key),e);if(r.state===`retired`)return r;const i={...r,state:t?`retired`:`retiring`,retired_at_ms:t?Math.max(Date.now(),r.bound_at_ms??r.created_at_ms):null};executeSqliteQuerySync(n,query(n).updateTable(TABLE).set(i).where(`preparation_key`,`=`,r.preparation_key));nodePreparedWorkspacePublication.stagePostimages(n,[i]);return i})}};export{NodeWorkerPreparedWorkspaceKernel as t};