openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 2.48 kB
JavaScript
import{n as e}from"./rolldown-runtime-CNOWq4Kh.js";var t,n,r;function i(){return(i=e((()=>{t=3e5,n=3,r=class{constructor(e){this.options=e,this.lastLoadedAtMs=null,this.pendingAutomaticRefresh=null,this.publicationPending=!1,this.reloadPending=!1,this.retryTimer=null,this.retryAttempts=0,this.exhaustionReported=!1}get incompleteUsageExhausted(){return this.exhaustionReported}setLastLoadedAtMs(e,t){this.applyLoadState(e,t?.incomplete===!0,t?.connection)}markProviderUsage(e,t,n){let r=e?.ok===!1||e?.ok===!0&&e.value.refreshing===!0;this.applyLoadState(t,r,n)}resetPayload(){this.applyLoadState(null,!1),this.reloadPending=!1,this.publicationPending=!1}dispose(){this.resetRetryCycle()}applyLoadState(e,t,n){n!==this.connection&&(this.connection=n,this.resetRetryCycle()),t?this.retryTimer===null&&this.armRetry():this.resetRetryCycle(),this.lastLoadedAtMs=t?null:e}armRetry(){this.retryAttempts>=n?this.exhaustionReported||(this.exhaustionReported=!0,this.options.onIncompleteUsageExhausted?.()):(this.retryAttempts+=1,this.retryTimer=window.setTimeout(()=>{this.retryTimer=null,this.requestAndWait(`poll`).catch(()=>void 0)},5e3*2**(this.retryAttempts-1)))}resetRetryCycle(){this.retryAttempts=0,this.exhaustionReported=!1,this.retryTimer!==null&&(window.clearTimeout(this.retryTimer),this.retryTimer=null)}interrupt(){this.reloadPending||=this.options.isLoading()}markLoadDeferred(){this.reloadPending=!0}beginLoad(){this.reloadPending=!1}request(e){return this.requestAndWait(e)}async requestAndWait(e){e===`publication`&&(this.publicationPending=!0,this.reloadPending=!0),this.options.isLoading()&&e!==`manual`?this.pendingAutomaticRefresh=this.pendingAutomaticRefresh&&this.pendingAutomaticRefresh!==`poll`?this.pendingAutomaticRefresh:e:(this.pendingAutomaticRefresh=null,!(e!==`manual`&&(document.visibilityState!==`visible`||!document.hasFocus()||!this.reloadPending&&this.lastLoadedAtMs!==null&&Date.now()-this.lastLoadedAtMs<t))&&((e===`manual`||e!==`poll`&&!this.publicationPending)&&this.resetRetryCycle(),this.publicationPending=!1,await this.options.reload(e)))}flushPending(){let e=this.pendingAutomaticRefresh;e&&(this.pendingAutomaticRefresh=null,this.request(e===`poll`?`poll`:`focus`))}}})))()}async function a(e,t){try{return{ok:!0,value:await e.request(`usage.status`,void 0,t)}}catch(e){if(t?.signal?.aborted)throw e;return{ok:!1}}}function o(){return(o=e((()=>{})))()}function s(){return(s=e((()=>{})))()}export{i as a,r as i,o as n,a as r,s as t};