openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.44 kB
JavaScript
import{n as e,r as t}from"./rolldown-runtime-8BhlS34s.js";import{Si as n,fi as r,gi as i,hi as a,ii as o,li as s,xu as c,yi as l,yu as u}from"./control-ui-core-CBJ5efDh.js";import{xa as d}from"./ui-CmSP4d14.js";var f=t({createInitialDevicesState:()=>p,loadDevices:()=>_,loadExecApprovals:()=>F,loadNodes:()=>g});function p(e={}){return{client:e.client??null,connected:e.connected??!1,requestGeneration:0,nodesLoading:!1,nodesQueuedRefresh:`none`,nodes:[],lastError:null,devicesLoading:!1,devicesQueuedRefresh:`none`,devicesError:null,devicesList:null,execApprovalsLoading:!1,execApprovalsSaving:!1,execApprovalsDirty:!1,execApprovalsSnapshot:null,execApprovalsForm:null,execApprovalsSelectedAgent:null}}function m(e,t,n){return e.connected&&e.client===t&&e.requestGeneration===n}function h(e,t){return e===`visible`||t!==!0?`visible`:`quiet`}function g(e,t){return v(e,`nodes`,t)}function _(e,t){return v(e,`devices`,t)}async function v(e,t,n){let r=e.client;if(!r||!e.connected)return;let i=`${t}Loading`,a=`${t}QueuedRefresh`,o=t===`nodes`?`lastError`:`devicesError`;if(e[i]){e[a]=h(e[a],n?.quiet);return}e[i]=!0,n?.quiet||(e[o]=null);let s=e.requestGeneration;try{if(t===`nodes`){let t=await r.request(`node.list`,{});m(e,r,s)&&(e.nodes=Array.isArray(t.nodes)?t.nodes:[])}else{let t=await r.request(`device.pair.list`,{});m(e,r,s)&&(e.devicesList={pending:Array.isArray(t?.pending)?t.pending:[],paired:Array.isArray(t?.paired)?t.paired:[]})}}catch(t){!n?.quiet&&m(e,r,s)&&(e[o]=u(t))}finally{if(m(e,r,s)){e[i]=!1;let n=e[a];e[a]=`none`,n!==`none`&&await v(e,t,{quiet:n===`quiet`})}}}async function y(e,t){let n=e.client;if(!n||!e.connected)return null;let r=e.requestGeneration;try{let i=await t(n);return m(e,n,r)&&await _(e),i}catch(t){return m(e,n,r)&&(e.devicesError=u(t)),null}}async function b(e,t){await y(e,e=>e.request(`device.pair.approve`,{requestId:t}))}async function x(e,t){await y(e,e=>e.request(`device.pair.reject`,{requestId:t}))}async function S(e,t){t.removeNode&&await e.request(`node.pair.remove`,{nodeId:t.id}),t.removeDevice&&await e.request(`device.pair.remove`,{deviceId:t.id})}async function C(e,t){let n=t?.error!==void 0;await Promise.all([_(e,{quiet:n}),g(e,{quiet:n})]),t?.error!==void 0&&(e.devicesError=t.error)}async function w(e,t){let n=e.client;if(n&&e.connected)try{await t(n),await C(e)}catch(t){await C(e,{error:u(t)})}}function T(e,t){return w(e,e=>S(e,t))}async function E(e,t){let n=e.client;if(!n||!e.connected||t.length===0)return;let r=[];for(let e of t)try{await S(n,e)}catch(t){r.push(`${e.name}: ${u(t)}`)}await C(e,r.length>0?{error:`Failed to remove ${r.length} entr${r.length===1?`y`:`ies`}: ${r[0]}`}:void 0)}async function D(e,t){let n=e.client;if(!n||!e.connected){let t=u(Error(`The Gateway connection is not available.`));return e.devicesError=t,t}let r=e.requestGeneration;try{return await n.request(`device.pair.rename`,t),m(e,n,r)&&await _(e),null}catch(t){let i=u(t);return m(e,n,r)&&(e.devicesError=i),i}}function O(e,t){return w(e,e=>e.request(`node.pair.approve`,{requestId:t}))}function k(e,t){return w(e,e=>e.request(`node.pair.reject`,{requestId:t}))}function A(e,t){return typeof e==`string`&&e.trim().length>0&&e.trim()===t.trim()}function j(e,t){let n=d(e),r=n?.scopes,i=n?.rotatedAtMs,a=A(n?.deviceId,t.deviceId)&&A(n?.role,t.role)&&Array.isArray(r)&&r.every(e=>typeof e==`string`&&e.length>0)&&typeof i==`number`&&Number.isInteger(i)&&i>=0,o=n?.token,s=typeof o==`string`&&o.length>0?o:void 0,c=o===void 0,l=n?.tokenDelivery;if(a){if(s&&(l===void 0||l===`in-band`))return{delivery:`in-band`,token:s};if(c&&(l===void 0||l===`withheld-cross-device`))return{delivery:`withheld-cross-device`}}throw Error(`Rotation returned an unusable result (tokenDelivery=${JSON.stringify(l)}, token ${s?`present`:c?`absent`:`malformed`}). The previous token no longer works; pair the device again if it does not reconnect.`)}async function M(e,t){return y(e,async e=>{let{gatewayUrl:r,...i}=t,a=await e.request(`device.token.rotate`,i),o=j(a,i);if(o.delivery===`in-band`){let e=await l();(a.deviceId===e.deviceId||i.deviceId===e.deviceId)&&n({deviceId:e.deviceId,gatewayUrl:r,role:i.role,token:o.token,scopes:a.scopes??i.scopes??[]})}return o})}async function N(e,t){await y(e,async e=>{let{gatewayUrl:n,...r}=t;await e.request(`device.token.revoke`,r);let i=await l();r.deviceId===i.deviceId&&a({deviceId:i.deviceId,gatewayUrl:n,role:r.role})})}function P(e,t){let n=t?`set`:`get`,r=t??{};if(!e||e.kind===`gateway`)return{method:`exec.approvals.${n}`,params:r};let i=e.nodeId.trim();return i?{method:`exec.approvals.node.${n}`,params:{...r,nodeId:i}}:null}async function F(e,t){let n=e.client;if(!n||!e.connected||e.execApprovalsLoading)return;e.execApprovalsLoading=!0,e.lastError=null;let r=e.requestGeneration;try{let i=P(t);if(!i){e.lastError=`Select a node before loading exec approvals.`;return}let a=await n.request(i.method,i.params);m(e,n,r)&&I(e,a)}catch(t){m(e,n,r)&&(e.lastError=u(t))}finally{m(e,n,r)&&(e.execApprovalsLoading=!1)}}function I(e,t){if(e.execApprovalsSnapshot=t,L(t)){e.execApprovalsForm=null,e.execApprovalsDirty=!1;return}e.execApprovalsDirty||(e.execApprovalsForm=structuredClone(t.file))}function L(e){return!!(e&&`enabled`in e)}async function R(e,t){let n=e.client;if(!n||!e.connected)return;e.execApprovalsSaving=!0,e.lastError=null;let r=e.requestGeneration;try{if(L(e.execApprovalsSnapshot)){e.lastError=`Host-native node approvals are read-only here; use the companion app or approvals set --node.`;return}let i=e.execApprovalsSnapshot?.hash;if(!i){e.lastError=`Exec approvals hash missing; reload and retry.`;return}let a=P(t,{file:e.execApprovalsForm??e.execApprovalsSnapshot?.file??{},baseHash:i});if(!a){e.lastError=`Select a node before saving exec approvals.`;return}if(await n.request(a.method,a.params),!m(e,n,r))return;e.execApprovalsDirty=!1,await F(e,t)}catch(t){m(e,n,r)&&(e.lastError=u(t))}finally{m(e,n,r)&&(e.execApprovalsSaving=!1)}}function z(e,t){if(L(e.execApprovalsSnapshot)){e.lastError=`Host-native node approvals are read-only here.`;return}let n=structuredClone(e.execApprovalsForm??e.execApprovalsSnapshot?.file??{});t(n),e.execApprovalsForm=n,e.execApprovalsDirty=!0}function B(e,t,n){z(e,e=>r(e,t,n))}function V(e,t){z(e,e=>s(e,t))}function H(){return(H=e((()=>{o(),c(),i()})))()}export{M as _,L as a,g as c,k as d,V as f,N as g,D as h,H as i,f as l,E as m,O as n,_ as o,T as p,p as r,F as s,b as t,x as u,R as v,B as y};