openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.11 kB
JavaScript
import{t as deferSqlitePostCommitPublication}from"./sqlite-post-commit-CAVf6ZAG.mjs";import{i as executeSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{o as deferSqliteWorkerCommitReceipt}from"./sqlite-worker-operation-settlement-Cl93BzUc.mjs";import{p as requestSqliteWorkerOperationAdmission}from"./sqlite-worker-operation-admission-BZvXBQev.mjs";import{a as getSqliteWorkerStateContext}from"./sqlite-worker-state-context-DJ4guxKh.mjs";import{l as runOpenClawStateWriteTransaction}from"./openclaw-state-db-BlgxYkYw.mjs";import{p as ensureMcpOAuthPendingSchema}from"./openclaw-state-db-schema-additive-DDOLEjcV.mjs";import{n as assertOpenClawStateLeaseWorkerOwnedInTransaction}from"./openclaw-state-lease-worker-B35V0x0v.mjs";import{r as projectMcpOAuthAuthorization}from"./mcp-oauth-authorization-facts-CUYcHrI6.mjs";import{i as replaceMcpOAuthStoreInDatabase,r as readMcpOAuthStoreInDatabase,t as MCP_OAUTH_PENDING_STATE_TTL_MS}from"./mcp-oauth-store.kernel-DzX7B2_v.mjs";import{n as applyMcpOAuthMutation}from"./mcp-oauth-store.mutations-DtS5Y0FA.mjs";
//!src/agents/mcp-oauth-store.worker.ts
function assertStoreLease(e,t,n=`transaction`){if(t.identity.scope!==`core:mcp-oauth`||t.identity.key!==t.storeKey)throw new Error(`MCP OAuth mutation requires its exact store lease`);assertOpenClawStateLeaseWorkerOwnedInTransaction(e,t.identity,`write`,n)}const pendingSchemaDatabases=/* @__PURE__ */ new WeakSet;function ensurePendingSchema(e){if(pendingSchemaDatabases.has(e))return;ensureMcpOAuthPendingSchema(e);deferSqlitePostCommitPublication(e,()=>pendingSchemaDatabases.add(e))}function ownedWrite(e,t=false){return(n,{open:r})=>{const i=r();return runOpenClawStateWriteTransaction(({db:r})=>{const assertOwned=()=>assertStoreLease(r,n);if(t){assertOwned();ensurePendingSchema(r)}const i=e(r,n,assertOwned);assertStoreLease(r,n,`commit`);return i},{database:i,path:i.path,env:getSqliteWorkerStateContext().environment})}}function deletePending(e,t,n){n();executeSqliteQuerySync(e,getNodeSqliteKysely(e).deleteFrom(`mcp_oauth_pending_authorizations`).where(`store_key`,`=`,t))}const mcpOAuthOperations={"mcpOAuth.read":(e,{open:t})=>readMcpOAuthStoreInDatabase(t().db,e),"mcpOAuth.mutate":ownedWrite((e,t,n)=>{const r=applyMcpOAuthMutation(readMcpOAuthStoreInDatabase(e,t.storeKey),t.mutation);replaceMcpOAuthStoreInDatabase(e,t.storeKey,r.store,n);deferSqliteWorkerCommitReceipt(e,{kind:`mcp-oauth-authorization`,storeKey:t.storeKey,authorization:projectMcpOAuthAuthorization(r.store)});return r}),"mcpOAuth.consumePending":ownedWrite((e,t,n)=>{n();return executeSqliteQuerySync(e,getNodeSqliteKysely(e).deleteFrom(`mcp_oauth_pending_authorizations`).where(`store_key`,`=`,t.storeKey).where(`state`,`=`,t.state).where(`create_time`,`>`,Date.now()-MCP_OAUTH_PENDING_STATE_TTL_MS)).numAffectedRows===1n},true),"mcpOAuth.writePending":ownedWrite((e,t,n)=>{const r=getNodeSqliteKysely(e);const i=Date.now();n();executeSqliteQuerySync(e,r.deleteFrom(`mcp_oauth_pending_authorizations`).where(`create_time`,`<=`,i-MCP_OAUTH_PENDING_STATE_TTL_MS));deletePending(e,t.storeKey,n);n();executeSqliteQuerySync(e,r.insertInto(`mcp_oauth_pending_authorizations`).values({state:t.state,store_key:t.storeKey,create_time:i}))},true),"mcpOAuth.deletePending":ownedWrite((e,t,n)=>deletePending(e,t.storeKey,n),true),"mcpOAuth.clear":ownedWrite((e,t,n)=>{replaceMcpOAuthStoreInDatabase(e,t.storeKey,{credentialState:`cleared`},n);deletePending(e,t.storeKey,n);deferSqliteWorkerCommitReceipt(e,{kind:`mcp-oauth-authorization`,storeKey:t.storeKey,authorization:{authorizationId:null}})},true),"mcpOAuth.clearPendingPrefix":(e,{open:t})=>{const n=t();runOpenClawStateWriteTransaction(({db:t})=>{requestSqliteWorkerOperationAdmission({stage:`transaction`,facts:void 0});ensurePendingSchema(t);executeSqliteQuerySync(t,getNodeSqliteKysely(t).deleteFrom(`mcp_oauth_pending_authorizations`).where(`store_key`,`like`,`${e}%`));requestSqliteWorkerOperationAdmission({stage:`commit`,facts:void 0})},{database:n,path:n.path,env:getSqliteWorkerStateContext().environment})}};export{mcpOAuthOperations};