openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 7 kB
JavaScript
import{n as e,r as t}from"./rolldown-runtime-CNOWq4Kh.js";import{Gu as n,Ji as r,Yi as i,Zi as a,ea as o}from"./_4____________________________.js";import{AN as s,EN as c,_F as l,vF as u}from"./_2_____________________________.js";import{S as d,f,h as p}from"./config-form-utils-runtime-CqcGJa6w.js";var m=t({createInitialDevicesState:()=>h,loadDevices:()=>v,loadExecApprovals:()=>L,loadNodes:()=>_});function h(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,devicesDeciding:[],execApprovalsLoading:!1,execApprovalsSaving:!1,execApprovalsDirty:!1,execApprovalsSnapshot:null,execApprovalsForm:null,execApprovalsError:null,execApprovalsSavedNotice:!1}}function g(e,t,n){return e.connected&&e.client===t&&e.requestGeneration===n}function _(e,t){return y(e,`nodes`,t)}function v(e,t){return y(e,`devices`,t)}async function y(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]=e[a]===`visible`||n?.quiet!==!0?`visible`:`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`,{});g(e,r,s)&&(e.nodes=t.nodes)}else{let t=await r.request(`device.pair.list`,{});g(e,r,s)&&(e.devicesList=t)}}catch(t){!n?.quiet&&g(e,r,s)&&(e[o]=c(t))}finally{if(g(e,r,s)){e[i]=!1;let n=e[a];e[a]=`none`,n!==`none`&&await y(e,t,{quiet:n===`quiet`})}}}async function b(e,t){let n=e.client;if(!n||!e.connected)return null;let r=e.requestGeneration;try{let i=await t(n);return g(e,n,r)&&await v(e),i}catch(t){return g(e,n,r)&&(e.devicesError=c(t)),null}}async function x(e,t,n){if(e.devicesDeciding.some(e=>e.requestId===t))return;let r={requestId:t};e.devicesDeciding=[...e.devicesDeciding,r];try{await n()}finally{e.devicesDeciding=e.devicesDeciding.filter(e=>e!==r)}}async function S(e,t){await x(e,t,()=>b(e,e=>e.request(`device.pair.approve`,{requestId:t})))}async function C(e,t){await x(e,t,()=>b(e,e=>e.request(`device.pair.reject`,{requestId:t})))}async function w(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 T(e,t){let n=t?.error!==void 0;await Promise.all([v(e,{quiet:n}),_(e,{quiet:n})]),t?.error!==void 0&&(e.devicesError=t.error)}async function E(e,t){let n=e.client;if(n&&e.connected)try{await t(n),await T(e)}catch(t){await T(e,{error:c(t)})}}function D(e,t){return E(e,e=>w(e,t))}async function O(e,t){let n=e.client;if(!n||!e.connected||t.length===0)return;let r=[];for(let e of t)try{await w(n,e)}catch(t){r.push(`${e.name}: ${c(t)}`)}await T(e,r.length>0?{error:`Couldn’t remove ${r.length} entr${r.length===1?`y`:`ies`}. Review the remaining entries before trying again. Details: ${r[0]}`}:void 0)}async function k(e,t){let n=e.client;if(!n||!e.connected){let t=c(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),g(e,n,r)&&await v(e),null}catch(t){let i=c(t);return g(e,n,r)&&(e.devicesError=i),i}}function A(e,t){return x(e,t,()=>E(e,e=>e.request(`node.pair.approve`,{requestId:t})))}function j(e,t){return x(e,t,()=>E(e,e=>e.request(`node.pair.reject`,{requestId:t})))}function M(e,t){return typeof e==`string`&&e.trim().length>0&&e.trim()===t.trim()}function N(e,t){let r=n(e),i=r?.scopes,a=r?.rotatedAtMs,o=M(r?.deviceId,t.deviceId)&&M(r?.role,t.role)&&Array.isArray(i)&&i.every(e=>typeof e==`string`&&e.length>0)&&typeof a==`number`&&Number.isInteger(a)&&a>=0,s=r?.token,c=typeof s==`string`&&s.length>0?s:void 0,l=s===void 0,u=r?.tokenDelivery;if(o){if(c&&(u===void 0||u===`in-band`))return{delivery:`in-band`,token:c};if(l&&(u===void 0||u===`withheld-cross-device`))return{delivery:`withheld-cross-device`}}throw Error(`Rotation returned an unusable result (tokenDelivery=${JSON.stringify(u)}, token ${c?`present`:l?`absent`:`malformed`}). The previous token no longer works; pair the device again if it does not reconnect.`)}async function P(e,t){return b(e,async e=>{let{gatewayUrl:n,...r}=t,i=await e.request(`device.token.rotate`,r),s=N(i,r);if(s.delivery===`in-band`){let e=await a();(i.deviceId===e.deviceId||r.deviceId===e.deviceId)&&o({deviceId:e.deviceId,gatewayUrl:n,role:r.role,token:s.token,scopes:i.scopes??r.scopes??[]})}return s})}async function F(e,t){await b(e,async e=>{let{gatewayUrl:n,...i}=t;await e.request(`device.token.revoke`,i);let o=await a();i.deviceId===o.deviceId&&r({deviceId:o.deviceId,gatewayUrl:n,role:i.role})})}function I(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 L(e,t){let n=e.client;if(!n||!e.connected||e.execApprovalsLoading)return;e.execApprovalsLoading=!0,e.execApprovalsError=null;let r=e.requestGeneration;try{let i=I(t);if(!i){e.execApprovalsError=`Select a device before loading exec approvals.`;return}let a=await n.request(i.method,i.params);g(e,n,r)&&R(e,a)}catch(t){g(e,n,r)&&(e.execApprovalsError=c(t))}finally{g(e,n,r)&&(e.execApprovalsLoading=!1)}}function R(e,t){e.execApprovalsDirty||(e.execApprovalsSnapshot=t,z(t)?(e.execApprovalsForm=null,e.execApprovalsDirty=!1):e.execApprovalsForm=t.file)}function z(e){return!!(e&&`enabled`in e)}async function B(e,t){let n=e.client;if(!n||!e.connected)return;e.execApprovalsSaving=!0,e.execApprovalsSavedNotice=!1,e.execApprovalsError=null;let r=e.requestGeneration;try{if(z(e.execApprovalsSnapshot)){e.execApprovalsError=`Device approvals are read-only here. Use the companion app or approvals set --node.`;return}let i=e.execApprovalsSnapshot?.hash;if(!i){e.execApprovalsError=`Exec approvals hash missing; reload and retry.`;return}let a=I(t,{file:e.execApprovalsForm??e.execApprovalsSnapshot?.file??{},baseHash:i});if(!a){e.execApprovalsError=`Select a device before saving exec approvals.`;return}if(await n.request(a.method,a.params),!g(e,n,r))return;e.execApprovalsDirty=!1,e.execApprovalsSavedNotice=!0,await L(e,t)}catch(t){g(e,n,r)&&(e.execApprovalsError=c(t))}finally{g(e,n,r)&&(e.execApprovalsSaving=!1)}}function V(e,t,r,i){if(z(e.execApprovalsSnapshot)){e.execApprovalsError=`Device approvals are read-only here. Use the companion app or approvals set --node.`;return}let a=e.execApprovalsSnapshot?.file??{},o=e.execApprovalsForm??a,s=f(o,t,r,i);e.execApprovalsForm=(s.ok?n(s.value):void 0)??o,e.execApprovalsDirty=u(d(e.execApprovalsForm,a))!==u(a),e.execApprovalsSavedNotice=!1}function H(e,t,n){V(e,t,n,`copy-set`)}function U(e,t){V(e,t,void 0,`copy-remove`)}function W(){return(W=e((()=>{l(),p(),s(),i()})))()}export{P as _,z as a,_ as c,j as d,U as f,F as g,k as h,W as i,m as l,O as m,A as n,v as o,D as p,h as r,L as s,S as t,C as u,B as v,H as y};