UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 6.84 kB
import{n as e,r as t}from"./rolldown-runtime-8BhlS34s.js";import{ai as n,du as r,hi as i,mi as a,pi as o,pu as s,ui as c,vi as l,xi as u}from"./control-ui-core-BncvAZjR.js";import{ma as d}from"./ui-DuRgK6kL.js";var f=t({createInitialDevicesState:()=>p,loadDevices:()=>_,loadExecApprovals:()=>P,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`}async function g(e,t){let n=e.client;if(!n||!e.connected)return;if(e.nodesLoading){e.nodesQueuedRefresh=h(e.nodesQueuedRefresh,t?.quiet);return}e.nodesLoading=!0,t?.quiet||(e.lastError=null,e.chatError=null);let i=e.requestGeneration;try{let t=await n.request(`node.list`,{});m(e,n,i)&&(e.nodes=Array.isArray(t.nodes)?t.nodes:[])}catch(a){!t?.quiet&&m(e,n,i)&&(e.lastError=r(a))}finally{if(m(e,n,i)){e.nodesLoading=!1;let t=e.nodesQueuedRefresh;e.nodesQueuedRefresh=`none`,t!==`none`&&await g(e,{quiet:t===`quiet`})}}}async function _(e,t){let n=e.client;if(!n||!e.connected)return;if(e.devicesLoading){e.devicesQueuedRefresh=h(e.devicesQueuedRefresh,t?.quiet);return}e.devicesLoading=!0,t?.quiet||(e.devicesError=null);let i=e.requestGeneration;try{let t=await n.request(`device.pair.list`,{});m(e,n,i)&&(e.devicesList={pending:Array.isArray(t?.pending)?t.pending:[],paired:Array.isArray(t?.paired)?t.paired:[]})}catch(a){!t?.quiet&&m(e,n,i)&&(e.devicesError=r(a))}finally{if(m(e,n,i)){e.devicesLoading=!1;let t=e.devicesQueuedRefresh;e.devicesQueuedRefresh=`none`,t!==`none`&&await _(e,{quiet:t===`quiet`})}}}async function v(e,t){let n=e.client;if(!n||!e.connected)return null;let i=e.requestGeneration;try{let r=await t(n);return m(e,n,i)&&await _(e),r}catch(t){return m(e,n,i)&&(e.devicesError=r(t)),null}}async function y(e,t){await v(e,e=>e.request(`device.pair.approve`,{requestId:t}))}async function b(e,t){await v(e,e=>e.request(`device.pair.reject`,{requestId:t}))}async function x(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 S(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 C(e,t){let n=e.client;if(n&&e.connected)try{await t(n),await S(e)}catch(t){await S(e,{error:r(t)})}}function w(e,t){return C(e,e=>x(e,t))}async function T(e,t){let n=e.client;if(!n||!e.connected||t.length===0)return;let i=[];for(let e of t)try{await x(n,e)}catch(t){i.push(`${e.name}: ${r(t)}`)}await S(e,i.length>0?{error:`Failed to remove ${i.length} entr${i.length===1?`y`:`ies`}: ${i[0]}`}:void 0)}async function E(e,t){let n=e.client;if(!n||!e.connected){let t=r(Error(`The Gateway connection is not available.`));return e.devicesError=t,t}let i=e.requestGeneration;try{return await n.request(`device.pair.rename`,t),m(e,n,i)&&await _(e),null}catch(t){let a=r(t);return m(e,n,i)&&(e.devicesError=a),a}}function D(e,t){return C(e,e=>e.request(`node.pair.approve`,{requestId:t}))}function O(e,t){return C(e,e=>e.request(`node.pair.reject`,{requestId:t}))}function k(e,t){return typeof e==`string`&&e.trim().length>0&&e.trim()===t.trim()}function A(e,t){let n=d(e),r=n?.scopes,i=n?.rotatedAtMs,a=k(n?.deviceId,t.deviceId)&&k(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 j(e,t){return v(e,async e=>{let{gatewayUrl:n,...r}=t,i=await e.request(`device.token.rotate`,r),a=A(i,r);if(a.delivery===`in-band`){let e=await l();(i.deviceId===e.deviceId||r.deviceId===e.deviceId)&&u({deviceId:e.deviceId,gatewayUrl:n,role:r.role,token:a.token,scopes:i.scopes??r.scopes??[]})}return a})}async function M(e,t){await v(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 N(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 P(e,t){let n=e.client;if(!n||!e.connected||e.execApprovalsLoading)return;e.execApprovalsLoading=!0,e.lastError=null,e.chatError=null;let i=e.requestGeneration;try{let r=N(t);if(!r){e.lastError=`Select a node before loading exec approvals.`;return}let a=await n.request(r.method,r.params);m(e,n,i)&&F(e,a)}catch(t){m(e,n,i)&&(e.lastError=r(t))}finally{m(e,n,i)&&(e.execApprovalsLoading=!1)}}function F(e,t){if(e.execApprovalsSnapshot=t,I(t)){e.execApprovalsForm=null,e.execApprovalsDirty=!1;return}e.execApprovalsDirty||(e.execApprovalsForm=structuredClone(t.file))}function I(e){return!!(e&&`enabled`in e)}async function L(e,t){let n=e.client;if(!n||!e.connected)return;e.execApprovalsSaving=!0,e.lastError=null,e.chatError=null;let i=e.requestGeneration;try{if(I(e.execApprovalsSnapshot)){e.lastError=`Host-native node approvals are read-only here; use the companion app or approvals set --node.`;return}let r=e.execApprovalsSnapshot?.hash;if(!r){e.lastError=`Exec approvals hash missing; reload and retry.`;return}let a=N(t,{file:e.execApprovalsForm??e.execApprovalsSnapshot?.file??{},baseHash:r});if(!a){e.lastError=`Select a node before saving exec approvals.`;return}if(await n.request(a.method,a.params),!m(e,n,i))return;e.execApprovalsDirty=!1,await P(e,t)}catch(t){m(e,n,i)&&(e.lastError=r(t))}finally{m(e,n,i)&&(e.execApprovalsSaving=!1)}}function R(e,t){if(I(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 z(e,t,n){R(e,e=>o(e,t,n))}function B(e,t){R(e,e=>c(e,t))}function V(){return(V=e((()=>{n(),s(),i()})))()}export{j as _,I as a,g as c,O as d,B as f,M as g,E as h,V as i,f as l,T as m,D as n,_ as o,w as p,p as r,P as s,y as t,b as u,L as v,z as y};