UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

110 lines (109 loc) • 4.91 kB
import { c as withSqlitePostCommitPublications } from "../../sqlite-post-commit-BrUVQObC.mjs"; import { l as runSqliteWorkerTransactionSync, o as runSqliteReadSnapshotSync } from "../../sqlite-transaction-BHHu6P5-.mjs"; import { v as AuthProfileStoreUnreadableError } from "../../openclaw-state-worker-error-C8y1JGQN.mjs"; import { O as resolveInlineProviderApiKeyUsageId, z as reportCommittedInlineAuthFailure } from "../../user-model-account-authority-CRj924IP.mjs"; import { o as mergePersistedAuthProfileState } from "../../persisted-Dv3zNYHi.mjs"; import { E as writeAuthProfileJsonCell } from "../../sqlite-B2CoJ4Om.mjs"; import { s as inspectAuthProfileJsonCell } from "../../sqlite-json-DOMMf_cL.mjs"; import { n as prepareAuthProfileStateMutation } from "../../store-save-BJAxNoo0.mjs"; import { a as computeNextProfileUsageStats } from "../../usage-reduction-r_nQp8E6.mjs"; import { t as bindSqliteWorkerDatabase } from "../../sqlite-worker-binding-B4BB5u5i.mjs"; import { t as encodeSqliteWorkerDomainError } from "../../sqlite-worker-domain-error-CGsRRv7G.mjs"; import { t as enrollAuthProfileAuthorizationInDatabase } from "../../authorization-enrollment-DaNGO6M2.mjs"; import { n as authProfilePeerGenerationMayMatch, r as updateAuthProfileStoreInDatabase, t as recordAuthProfileUsageInDatabase } from "../../usage-kernel-BtY5rL0B.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/agents/auth-profiles/inline-usage-kernel.ts /** The admitted agent transaction owns the fresh read, health reduction, and durable cells. */ function recordInlineAuthFailureInDatabase(e, t, n) { const r = inspectAuthProfileJsonCell(e, "store", "agent"); if (!isDeepStrictEqual(r.status === "readable" ? r.raw : null, n.expectedCredentials)) throw new Error("Auth credentials changed during inline-failure preparation"); const i = inspectAuthProfileJsonCell(e, "state", "agent"); const a = i.status === "readable" ? i.raw : null; const o = mergePersistedAuthProfileState(r.status === "readable" ? r.raw : null, () => a); if (!o && r.status !== "missing") throw new AuthProfileStoreUnreadableError(t); const s = o ?? { version: 1, profiles: {} }; s.usageStats = { ...Object.fromEntries(Object.entries(n.inheritedUsageStats ?? {}).filter(([e]) => s.profiles[e] || e.startsWith("inline-api-key:"))), ...s.usageStats }; const c = resolveInlineProviderApiKeyUsageId(n.provider); const l = s.usageStats?.[c]; const u = computeNextProfileUsageStats({ existing: l ?? {}, now: Date.now(), reason: n.reason, modelId: n.modelId }); s.usageStats = { ...s.usageStats, [c]: u }; const { statePayload: d, stateChanged: f, selectionChanged: p } = prepareAuthProfileStateMutation({ existingState: a, store: s, selectionProfiles: s.profiles }); if (r.status === "missing") writeAuthProfileJsonCell(e, "store", "agent", { version: 1, profiles: {} }); if (f) writeAuthProfileJsonCell(e, "state", "agent", d); return { store: s, publication: { credentialsChanged: false, profileSetChanged: false, stateChanged: f, selectionChanged: p, profileIds: [] } }; } //#endregion //#region src/agents/auth-profiles/inline-usage.worker.ts /** The canonical agent executor lends its connection and transaction/commit admission. */ function bindSqliteWorkerBackend(e, t) { return bindSqliteWorkerDatabase(t.database, { execute(e) { if (e.type === "authProfiles.enrollAuthorization") return runSqliteWorkerTransactionSync(t, () => enrollAuthProfileAuthorizationInDatabase(t.database, "agent", e.input)); if (e.type === "authProfiles.update") { if (!authProfilePeerGenerationMayMatch(t.database, e.input)) return false; withSqlitePostCommitPublications(t.database, () => runSqliteWorkerTransactionSync(t, () => updateAuthProfileStoreInDatabase(t.database, "agent", e.input))); return true; } if (e.type === "authProfiles.inlineSnapshot") return runSqliteReadSnapshotSync(t.database, () => ({ store: inspectAuthProfileJsonCell(t.database, "store", "agent"), state: inspectAuthProfileJsonCell(t.database, "state", "agent"), cacheable: false })); let n; let r = false; try { runSqliteWorkerTransactionSync(t, () => { if (e.type === "authProfiles.usage") n = { ok: true, receipt: recordAuthProfileUsageInDatabase(t.database, t.databasePath, "agent", e.input) }; else n = { ok: true, receipt: recordInlineAuthFailureInDatabase(t.database, t.databasePath, e.input) }; }, { withCommit(e) { e(); r = true; } }); } catch (e) { if (!r || !n) return { ok: false, error: encodeSqliteWorkerDomainError(t.database, e) }; reportCommittedInlineAuthFailure("Auth usage committed before transaction cleanup failed", e); } if (!n) throw new Error("Auth usage transaction produced no durable result"); return n; } }); } //#endregion export { bindSqliteWorkerBackend };