openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 4.11 kB
JavaScript
import{c as withSqlitePostCommitPublications}from"../../sqlite-post-commit-CAVf6ZAG.mjs";import{v as AuthProfileStoreUnreadableError}from"../../openclaw-state-worker-error-Ca5E4dEV.mjs";import{l as runSqliteWorkerTransactionSync,o as runSqliteReadSnapshotSync}from"../../sqlite-transaction-CON-dGDv.mjs";import{D as resolveInlineProviderApiKeyUsageId,z as reportCommittedInlineAuthFailure}from"../../user-model-account-authority-B8H7rvCb.mjs";import{o as mergePersistedAuthProfileState}from"../../persisted-CpbPYi0y.mjs";import{E as writeAuthProfileJsonCell}from"../../sqlite-y_WDH-Xr.mjs";import{s as inspectAuthProfileJsonCell}from"../../sqlite-json-CUskHMsO.mjs";import{n as prepareAuthProfileStateMutation}from"../../store-save-ChKW_DT4.mjs";import{a as computeNextProfileUsageStats}from"../../usage-reduction-BzoKhsEN.mjs";import{t as bindSqliteWorkerDatabase}from"../../sqlite-worker-binding-CxA1GbiJ.mjs";import{t as encodeSqliteWorkerDomainError}from"../../sqlite-worker-domain-error-CHwGZvH1.mjs";import{t as enrollAuthProfileAuthorizationInDatabase}from"../../authorization-enrollment-BWqDZpyn.mjs";import{n as authProfilePeerGenerationMayMatch,r as updateAuthProfileStoreInDatabase,t as recordAuthProfileUsageInDatabase}from"../../usage-kernel-D1Ae0AnF.mjs";import{isDeepStrictEqual}from"node:util";
//!src/agents/auth-profiles/inline-usage-kernel.ts
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:[]}}}
//!src/agents/auth-profiles/inline-usage.worker.ts
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}})}export{bindSqliteWorkerBackend};