UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

161 lines (160 loc) • 8.37 kB
import { r as sqlPublicationAnchor } from "./sql-connection-Beiyg5Dt.mjs"; import { t as deferSqlitePostCommitPublication } from "./sqlite-post-commit-BrUVQObC.mjs"; import { a as executeSqliteQueryTakeFirstSync, i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; import { a as deferSqliteWorkerCommitReceipt } from "./sqlite-worker-operation-settlement-OZQM-wtd.mjs"; import "./sqlite-worker-operation-admission-9E_X-oRy.mjs"; import { l as runOpenClawStateWriteTransaction } from "./openclaw-state-db-ARhwdPJg.mjs"; import { d as ensureGitHubPublicationSessionLifecycleSchema, m as ensurePersonalGitHubPublicationSchema } from "./openclaw-state-db-schema-additive-BOxd1NE5.mjs"; import { n as emitSessionLifecycleEvent } from "./session-lifecycle-events-DWKmeEvQ.mjs"; import { i as githubPublicationReceipts, n as captureGitHubPublicationWorkerReceipt } from "./github-publication-receipts-DrGMcoy5.mjs"; import { c as resolvePersonalGitHubOwnerInDatabase } from "./user-github-connections.kernel-Cwmk1b8b.mjs"; import { n as encodeGitHubPublicationRequester } from "./github-publication-requester-CE4BURqL.mjs"; import { t as personalGitHubRequestDigest } from "./github-personal-publication-store-BIOXPKQD.mjs"; import "./github-publication-receipt-CYjil9rT.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/gateway/github-publication-events.ts const workerChanges = new AsyncLocalStorage(); /** A worker carries the native owner's exact notifications with its commit receipt. */ function captureGitHubPublicationChanges(e) { const t = []; return { value: workerChanges.run(t, e), changes: t }; } /** The existing transaction owner discards these observers on rollback, including savepoints. */ function deferSharedGitHubPublicationChanged(e, t) { if (t.identity_source === "personal" || t.owner_profile_id != null) return; const n = workerChanges.getStore(); if (n) { n.push({ sessionKey: t.session_key, agentId: t.agent_id }); return; } deferSqlitePostCommitPublication(e, () => { emitSessionLifecycleEvent({ sessionKey: t.session_key, agentId: t.agent_id, reason: "github-publication" }); }); } //#endregion //#region src/state/github-publication-bookkeeping.worker.ts function captureGitHubPublicationMutation(e, t) { return captureGitHubPublicationWorkerReceipt(sqlPublicationAnchor(e.db), () => { const { value: e, changes: n } = captureGitHubPublicationChanges(t); return { ...e, changes: n }; }); } function commitGitHubPublicationBookkeeping(e, t, n) { return runOpenClawStateWriteTransaction(({ db: t }) => { const r = captureGitHubPublicationMutation(e, n); deferSqliteWorkerCommitReceipt(sqlPublicationAnchor(t), r); }, { database: e }, { operationLabel: `github-publication.${t}` }); } //#endregion //#region src/state/github-publication-session-lifecycles.worker.ts const table$1 = "github_publication_session_lifecycles"; const query$1 = (e) => getNodeSqliteKysely(e); /** The receipt and its captured generation commit together; replay never rebinds it. */ function insertGitHubPublicationSessionLifecycle(e, t) { ensureGitHubPublicationSessionLifecycleSchema(e); const n = { publication_kind: t.publicationKind, request_id: t.requestId, lifecycle_revision: t.lifecycleRevision, requester_authority_json: t.requester ? encodeGitHubPublicationRequester(t.requester) : null }; executeSqliteQuerySync(e, query$1(e).insertInto(table$1).values(n)); githubPublicationReceipts.stageRow(sqlPublicationAnchor(e), `${t.publicationKind}-lifecycle`, n); } /** A missing binding is unproven; a retained NULL records an originally absent revision. */ function readGitHubPublicationSessionLifecycle(e, t) { return tableExists(t, table$1) ? executeSqliteQueryTakeFirstSync(t, query$1(t).selectFrom(table$1).select(["lifecycle_revision", "requester_authority_json"]).where("publication_kind", "=", e.publicationKind).where("request_id", "=", e.requestId)) : void 0; } //#endregion //#region src/gateway/github-personal-publication-store.worker.ts const table = "github_personal_publication_requests"; const query = (e) => getNodeSqliteKysely(e); function assertOwner(e, t) { if (resolvePersonalGitHubOwnerInDatabase(t, e) !== t) throw new Error("My GitHub publication owner changed."); } function insertPersonalGitHubPublicationInDatabase(e, t, n, r) { const { db: i } = e; r(); assertOwner(i, t.owner_profile_id); ensurePersonalGitHubPublicationSchema(i); executeSqliteQuerySync(i, query(i).insertInto(table).values(t)); insertGitHubPublicationSessionLifecycle(i, { publicationKind: "personal", requestId: t.request_id, lifecycleRevision: n }); githubPublicationReceipts.stageRow(i, "personal", t); return t; } function claimPersonalGitHubPublicationInDatabase(e, t, n, r, i) { const { db: a } = e; i(); assertOwner(a, t.owner_profile_id); const o = executeSqliteQueryTakeFirstSync(a, query(a).updateTable(table).set({ status: "publishing", gateway_instance_id: n, execution_id: r, updated_at_ms: Date.now() }).where("owner_profile_id", "=", t.owner_profile_id).where("request_id", "=", t.request_id).where("request_digest", "=", t.request_digest).where("status", "=", t.status).where("execution_id", t.execution_id === null ? "is" : "=", t.execution_id).returningAll()); if (!o) throw new Error("My GitHub publication execution changed."); if (o.identity_source !== "personal" || o.request_digest !== personalGitHubRequestDigest(o)) throw new Error("My GitHub publication receipt changed during execution."); githubPublicationReceipts.stageRow(a, "personal", o); return { ...o, gateway_instance_id: n, execution_id: r }; } /** One execution closure owns writes; a later socket must explicitly confirm before claiming. */ function writePersonalGitHubPublicationInDatabase(e, t, n, r, i) { const { db: a } = e; const o = executeSqliteQueryTakeFirstSync(a, query(a).updateTable(table).set({ ...i, updated_at_ms: Date.now() }).where("owner_profile_id", "=", t.owner_profile_id).where("request_id", "=", t.request_id).where("request_digest", "=", t.request_digest).where("status", "=", "publishing").where("gateway_instance_id", "=", n).where("execution_id", "=", r).returningAll()); if (!o) throw new Error("My GitHub publication execution is no longer current."); githubPublicationReceipts.stageRow(a, "personal", o); return o; } function requirePersonalGitHubPublicationConfirmationInDatabase(e, t) { if (!tableExists(e.db, table)) return []; const { db: n } = e; const r = executeSqliteQuerySync(n, query(n).updateTable(table).set({ status: "needs_confirmation", updated_at_ms: Date.now() }).where("status", "in", ["requested", "publishing"]).where((e) => e.or([e("gateway_instance_id", "is", null), e("gateway_instance_id", "!=", t)])).returningAll()).rows; for (const e of r) githubPublicationReceipts.stageRow(n, "personal", e); return r; } function markPersonalGitHubPublicationReportedInDatabase(e, t) { if (!tableExists(e.db, table)) return; const { db: n } = e; const r = executeSqliteQueryTakeFirstSync(n, query(n).updateTable(table).set({ reported_at_ms: Date.now() }).where("request_id", "=", t).where("status", "in", ["published", "failed"]).returningAll()); if (r) githubPublicationReceipts.stageRow(n, "personal", r); return r; } const personalPublicationRestartOperations = { "githubPublications.personalRestart": (e, { open: t }) => { const n = t(); return commitGitHubPublicationBookkeeping(n, e.operation, () => ({ operationId: e.operationId, operation: e.operation, kind: "personal", rows: requirePersonalGitHubPublicationConfirmationInDatabase(n, e.instanceId) })); } }; //#endregion export { requirePersonalGitHubPublicationConfirmationInDatabase as a, readGitHubPublicationSessionLifecycle as c, captureGitHubPublicationChanges as d, deferSharedGitHubPublicationChanged as f, personalPublicationRestartOperations as i, captureGitHubPublicationMutation as l, insertPersonalGitHubPublicationInDatabase as n, writePersonalGitHubPublicationInDatabase as o, markPersonalGitHubPublicationReportedInDatabase as r, insertGitHubPublicationSessionLifecycle as s, claimPersonalGitHubPublicationInDatabase as t, commitGitHubPublicationBookkeeping as u };