UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 2.55 kB
import{n as e}from"./rolldown-runtime-8BhlS34s.js";import{ca as t,ia as n,na as r,ta as i}from"./control-ui-core-3P365fbR.js";function a(e){let t=e&&typeof e==`object`&&`requestId`in e?e.requestId:void 0;if(typeof t!=`string`||!t.trim())throw Error(`gateway returned an invalid scope upgrade request id`);return t}function o(e,t){if(!e||typeof e!=`object`)throw Error(`gateway returned an invalid scope upgrade result`);let n=e;if(n.requestId!==t)throw Error(`gateway returned a mismatched scope upgrade result`);if(n.status===`rejected`||n.status===`expired`)return{status:n.status,requestId:t};let r=n.status===`approved`&&typeof n.deviceToken==`string`?n.deviceToken.trim():``,i=n.status===`approved`&&Array.isArray(n.scopes)?n.scopes:[];if(!r||i.length===0||i.some(e=>typeof e!=`string`||!e.trim()))throw Error(`gateway returned invalid approved scope upgrade credentials`);return{status:`approved`,requestId:t,deviceToken:r,scopes:i}}var s;function c(){return(c=e((()=>{s=class{constructor(e){this.deps=e}requestScopeUpgrade(e){if(this.active)return this.active.requestId&&e.onPending?.(this.active.requestId),this.active.promise;let t={controller:new AbortController},n=this.runUpgrade(t,e).finally(()=>{this.active===t&&(this.active=void 0)});return t.promise=n,this.active=t,n}cancelScopeUpgrade(){let e=this.active;this.active=void 0,e?.controller.abort()}async runUpgrade(e,t){let n=await this.deps.request(`device.scopes.requestUpgrade`,{scopes:[...t.scopes]},{signal:e.controller.signal});this.assertCurrent(e);let r=a(n);e.requestId=r,t.onPending?.(r),this.assertCurrent(e);let i=o(await this.deps.request(`device.scopes.waitUpgrade`,{requestId:r},{timeoutMs:null,signal:e.controller.signal}),r);return i.status===`approved`?(await this.deps.tokenStore.store({clientId:t.binding.clientId,deviceId:t.binding.deviceId,role:t.binding.role,token:i.deviceToken,scopes:i.scopes}),this.assertCurrent(e),this.deps.reconnect(),{status:`approved`,requestId:r,scopes:i.scopes}):(this.assertCurrent(e),i)}assertCurrent(e){if(this.active!==e)throw new DOMException(`scope upgrade cancelled`,`AbortError`)}}})))()}function l(e){return new s({request:e.request,tokenStore:{load:({deviceId:t,role:r})=>n({deviceId:t,gatewayUrl:e.gatewayUrl,role:r}),store:({deviceId:n,role:r,token:i,scopes:a})=>{t({deviceId:n,gatewayUrl:e.gatewayUrl,role:r,token:i,scopes:a})},clear:({deviceId:t,role:n})=>i({deviceId:t,gatewayUrl:e.gatewayUrl,role:n})},reconnect:e.reconnect})}function u(){return(u=e((()=>{c(),r()})))()}u();export{l as createGatewayScopeUpgradeRuntime};