UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

107 lines (106 loc) • 4.67 kB
import { t as deferSqlitePostCommitPublication } from "./sqlite-post-commit-BrUVQObC.mjs"; import { i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import { a as deferSqliteWorkerCommitReceipt } from "./sqlite-worker-operation-settlement-OZQM-wtd.mjs"; import { f as requestSqliteWorkerOperationAdmission } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs"; import { a as getSqliteWorkerStateContext } from "./sqlite-worker-state-context-CR8h__Vh.mjs"; import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs"; import { f as ensureMcpOAuthPendingSchema } from "./openclaw-state-db-schema-additive-BOxd1NE5.mjs"; import { n as assertOpenClawStateLeaseWorkerOwnedInTransaction } from "./openclaw-state-lease-worker-DlXbLNzC.mjs"; import { r as projectMcpOAuthAuthorization } from "./mcp-oauth-authorization-facts-BN0BPuN8.mjs"; import { i as replaceMcpOAuthStoreInDatabase, r as readMcpOAuthStoreInDatabase, t as MCP_OAUTH_PENDING_STATE_TTL_MS } from "./mcp-oauth-store.kernel-CFPhk9FP.mjs"; import { n as applyMcpOAuthMutation } from "./mcp-oauth-store.mutations-DT8WHglw.mjs"; //#region 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 }); } }; //#endregion export { mcpOAuthOperations };