UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.15 kB
import{n as e,r as t}from"./rolldown-runtime-CNOWq4Kh.js";import{Qt as n,Xt as r,au as i,oc as a,sc as o}from"./control-ui-connection-Bfj9beF_.js";var s=t({requestDevicePairJoinSetup:()=>l});function c(e,t){return e.request(`device.pair.setupCode`,t,{timeoutMs:a})}function l(e){return c(e,{includeQr:!1,joinUrl:!0})}function u(e){return{client:e.client,connected:e.connected,devicePairSetupOpen:!1,devicePairSetupLifecycle:{phase:`selection`,access:`full`},devicePairSetupExpiryTimer:null,devicePairSetupCountdownTimer:null,onDevicePairSetupChange:e.onChange??(()=>{}),pendingCount:0}}function d(e){return{devicePairSetupOpen:e.devicePairSetupOpen,devicePairSetupLifecycle:e.devicePairSetupLifecycle,devicePairPendingCount:e.pendingCount}}function f(e){e.devicePairSetupCountdownTimer&&=(clearInterval(e.devicePairSetupCountdownTimer),null)}function p(e,t){f(e);let n=e.devicePairSetupLifecycle,r=n.phase===`waiting`?n.setup.expiresAtMs:void 0;n.access!==`node`||!e.devicePairSetupOpen||typeof r!=`number`||r<=Date.now()||(e.devicePairSetupCountdownTimer=setInterval(()=>{(!e.devicePairSetupOpen||r<=Date.now())&&f(e),t()},1e3))}function m(e){return typeof e.setupId==`string`&&e.setupId.length>0&&typeof e.expiresAtMs==`number`&&Number.isInteger(e.expiresAtMs)&&e.expiresAtMs>=0}function h(e){e.devicePairSetupExpiryTimer!==null&&(clearTimeout(e.devicePairSetupExpiryTimer),e.devicePairSetupExpiryTimer=null)}async function g(e,t){let n=e.client;if(!n||!e.connected)return{status:`unavailable`,message:`Gateway unavailable`};try{let e=await n.request(`device.pair.setupStatus`,{setupId:t});if(e?.completion===void 0){if(e?.deliveryUncertain===void 0)return{status:`missing`};let n=S(e.deliveryUncertain);return n?.setupId===t?{status:`delivery-uncertain`,outcome:n}:{status:`unavailable`,message:`Invalid setup status response`}}let r=x(e.completion);return r?.setupId===t?{status:`found`,completion:r}:{status:`unavailable`,message:`Invalid setup status response`}}catch(e){return{status:`unavailable`,message:r(e)}}}function _(e,t){let n=e.devicePairSetupLifecycle;return n.phase===`waiting`&&n.setup.setupId===t||n.phase===`reconciling`&&n.setupId===t||n.phase===`error`&&n.source===`status`&&n.setupId===t}function v(e,t,n,r){_(e,t)&&(r.status===`found`?w(e,r.completion):r.status===`delivery-uncertain`?T(e,r.outcome):(e.devicePairSetupLifecycle=r.status===`missing`?{phase:`expired`,access:n}:{phase:`error`,source:`status`,access:n,setupId:t,message:r.message},e.onDevicePairSetupChange()))}async function y(e,t){let n=e.devicePairSetupLifecycle;if(n.phase!==`waiting`||n.setup.setupId!==t)return;h(e),f(e);let r=n.access;e.devicePairSetupLifecycle={phase:`reconciling`,access:r,setupId:t},e.onDevicePairSetupChange(),v(e,t,r,await g(e,t))}function b(e,t){h(e);let n=()=>{let r=t.expiresAtMs-Date.now();r>0?e.devicePairSetupExpiryTimer=setTimeout(n,r):y(e,t.setupId)};n()}function x(e){if(!i(e))return null;let{setupId:t,deviceName:n,access:r}=e;if(typeof t!=`string`||t.length===0||r!==`full`&&r!==`limited`&&r!==`node`)return null;let a=typeof n==`string`?n.trim():``;return{setupId:t,access:r,...a?{deviceName:a}:{}}}function S(e){let t=x(e);return t?{setupId:t.setupId,access:t.access}:null}function C(e,t,n){return _(e,t)?(f(e),h(e),e.devicePairSetupLifecycle=n,e.onDevicePairSetupChange(),!0):!1}function w(e,t){return C(e,t.setupId,{phase:`success`,access:t.access,...t.deviceName?{deviceName:t.deviceName}:{}})}function T(e,t){return C(e,t.setupId,{phase:`delivery-uncertain`,access:t.access})}async function E(e){e.devicePairSetupOpen=!0}async function D(e){let t=e.client,n=e.devicePairSetupLifecycle,i=n.access;if(!t||!e.connected||e.devicePairSetupLifecycle.phase===`loading`||A.has(e))return;let a={};if(A.set(e,a),n.phase===`error`&&n.source===`status`){e.devicePairSetupLifecycle={phase:`reconciling`,access:i,setupId:n.setupId};let t=await g(e,n.setupId);A.get(e)===a&&(v(e,n.setupId,i,t),A.delete(e))}else{h(e),e.devicePairSetupLifecycle={phase:`loading`,access:i};try{let n=await c(t,i===`full`?{}:i===`node`?{bootstrapProfile:`node`,includeQr:!1}:{bootstrapProfile:`limited`});if(A.get(e)!==a||e.client!==t||!e.connected||!e.devicePairSetupOpen)return;if(!m(n))throw Error(`Gateway does not provide pairing lifecycle metadata. Update the Gateway and try again.`);e.devicePairSetupLifecycle={phase:`waiting`,access:n.access===`full`||n.access===`limited`||n.access===`node`?n.access:i,setup:n},b(e,n)}catch(n){A.get(e)===a&&e.client===t&&e.devicePairSetupOpen&&(e.devicePairSetupLifecycle={phase:`error`,source:`create`,access:i,message:r(n)})}finally{A.get(e)===a&&A.delete(e)}}}async function O(e,t){(e.devicePairSetupLifecycle.phase===`selection`||e.devicePairSetupLifecycle.phase===`error`&&e.devicePairSetupLifecycle.source===`create`)&&e.devicePairSetupLifecycle.access!==t&&(e.devicePairSetupLifecycle={phase:`selection`,access:t})}function k(e){f(e),A.delete(e),h(e),e.devicePairSetupOpen=!1,e.devicePairSetupLifecycle={phase:`selection`,access:`full`}}var A;function j(){return(j=e((()=>{o(),n(),A=new WeakMap})))()}export{j as a,x as c,D as d,O as f,s as i,S as l,w as n,T as o,p,u as r,E as s,k as t,d as u};