openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 7.46 kB
JavaScript
import{n as e,r as t}from"./rolldown-runtime-CNOWq4Kh.js";import{Ht as n,Qn as r,Qt as i,Wt as a,Xt as o,eu as s,wa as c,xa as l}from"./control-ui-connection-Bfj9beF_.js";import{eg as u,rg as d,tg as f}from"./control-ui-boot-shared-DFTSUQQ0.js";var p=t({createChannelCapability:()=>S});function m(e,t){let n=e&&Object.hasOwn(e,t)&&e[t];return Array.isArray(n)?n:[]}function h(e,t){if(!e)return!1;let n=s(Object.hasOwn(e.channels,t)?e.channels[t]:void 0);return n?.configured===!0||n?.running===!0||n?.connected===!0||m(e.channelAccounts,t).some(e=>e.configured===!0||e.running===!0||e.connected===!0)}function g(e){return e?[...new Set([...e.channelOrder,...Object.keys(e.channels),...Object.keys(e.channelAccounts)])].some(t=>h(e,t)):!1}function _(e,t){if(!e)return null;let n=s(e.channels);return s(n?.[t])??s(e[t])}function v(e){if(e==null)return c(`common.na`);if(typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return String(e);try{return JSON.stringify(e)}catch{return c(`common.na`)}}function y(e){let t=_(e.configForm,e.channelId);return t?e.fields.flatMap(e=>e in t?[{label:e,value:v(t[e])}]:[]):[]}function b(e){let t=e.hello?.auth;return JSON.stringify({role:t?.role??null,scopes:t?.scopes?[...t.scopes].toSorted():null})}function x(e,t){let n=e.hello?.auth;return!n?.scopes||a({role:n.role??`operator`,requestedScopes:[t],allowedScopes:n.scopes})}function S(e){let t=e.snapshot,n={client:t.client??null,connected:t.phase===`connected`,channelsLoading:!1,channelsLoadingProbe:null,channelsRefreshSeq:0,channelsSnapshot:null,channelsRead:null,channelsError:null,channelsLastSuccess:null,pairingLoading:!1,pairingRefreshSeq:0,pairingSnapshot:null,pairingRead:null,pairingError:null,pairingLastSuccess:null,pairingBusyRequestId:null,whatsappLoginMessage:null,whatsappLoginQrDataUrl:null,whatsappLoginSessionKey:null,whatsappLoginConnected:null,whatsappBusy:!1},i={whatsappEpoch:0,pairingEpoch:0},a;function s(){a?.(null),a=void 0,n.pairingRefreshSeq+=1,n.pairingLoading=!1}async function l(e={}){let t=n.client;if(!t||!n.connected||n.pairingLoading||n.pairingBusyRequestId&&!e.duringMutation)return;let r=n.pairingRefreshSeq+1,i=()=>n.connected&&n.client===t&&n.pairingRefreshSeq===r;n.pairingRefreshSeq=r,n.pairingLoading=!0,n.pairingError=null;let{promise:s,resolve:c}=Promise.withResolvers();a=c,n.pairingRead={completion:s};let l=null;try{let e=await t.request(`channels.pairing.list`,{});if(!i())return;n.pairingSnapshot=e,n.pairingLastSuccess=Date.now()}catch(e){l=o(e)}finally{i()&&(c(l),a=void 0,n.pairingLoading=!1)}}async function f(e,t){let r=n.client;if(!r||!n.connected||n.pairingBusyRequestId)return null;let a=i.pairingEpoch,c=()=>n.connected&&n.client===r&&i.pairingEpoch===a&&n.pairingBusyRequestId===e;s(),n.pairingBusyRequestId=e,n.pairingRead=null,n.pairingError=null;try{let i=await t(r);if(!c())return null;let a=n.pairingSnapshot;return a?.requests.some(t=>t.requestId===e)&&(n.pairingSnapshot={...a,requests:a.requests.filter(t=>t.requestId!==e)}),s(),await l({duringMutation:!0}),c()?{result:i}:null}catch(e){return c()&&(n.pairingError=o(e)),null}finally{c()&&(n.pairingBusyRequestId=null)}}async function p(e,t,r){let a=n.client;if(!a||!n.connected||n.whatsappBusy)return!1;n.whatsappBusy=!0;let s=i.whatsappEpoch,c=()=>n.connected&&n.client===a&&i.whatsappEpoch===s;try{let n=await e(a);if(!c())return!1;t(n)}catch(e){return c()&&(n.whatsappLoginMessage=o(e),r?.()),!1}finally{c()&&(n.whatsappBusy=!1)}return!0}function m(e,t){return p(n=>n.request(`web.login.start`,{channel:`whatsapp`,force:e,timeoutMs:3e4,...t?{accountId:t}:{}}),e=>{n.whatsappLoginSessionKey=e.connected?null:e.sessionKey??null,n.whatsappLoginMessage=e.message?o(e.message):null,n.whatsappLoginQrDataUrl=e.qrDataUrl??null,n.whatsappLoginConnected=typeof e.connected==`boolean`?e.connected:null},()=>{n.whatsappLoginQrDataUrl=null,n.whatsappLoginSessionKey=null,n.whatsappLoginConnected=null})}function h(e){return p(t=>t.request(`web.login.wait`,{channel:`whatsapp`,timeoutMs:12e4,currentQrDataUrl:n.whatsappLoginQrDataUrl??void 0,...n.whatsappLoginSessionKey?{sessionKey:n.whatsappLoginSessionKey}:{},...e?{accountId:e}:{}}),e=>{n.whatsappLoginMessage=e.message?o(e.message):null,n.whatsappLoginConnected=e.connected??null,e.connected&&(n.whatsappLoginSessionKey=null),e.qrDataUrl?n.whatsappLoginQrDataUrl=e.qrDataUrl:e.connected&&(n.whatsappLoginQrDataUrl=null)},()=>{n.whatsappLoginConnected=null})}function g(e){return p(t=>t.request(`channels.logout`,{channel:`whatsapp`,...e?{accountId:e}:{}}),e=>{e.cleared?(n.whatsappLoginMessage=c(`channels.whatsapp.loggedOut`),n.whatsappLoginQrDataUrl=null,n.whatsappLoginSessionKey=null,n.whatsappLoginConnected=null):n.whatsappLoginMessage=c(`channels.whatsapp.logoutNotCleared`)})}let _=new Set,v=x(e.snapshot,`operator.read`),y=b(e.snapshot),S=x(e.snapshot,`operator.admin`),C=!1,w=!1,T;function E(){T?.(null),T=void 0,n.channelsRead=null}let D=()=>{if(!C)for(let e of _)e(n)},O=async e=>{if(C)return;let t=e();D();try{return await t}finally{D()}};async function k(e){let t=n.client;if(!t||!n.connected||n.channelsLoading&&(!n.channelsLoadingProbe||e))return;let r=(n.channelsRefreshSeq??0)+1,i=()=>n.client===t&&n.channelsRefreshSeq===r;n.channelsRefreshSeq=r,n.channelsLoading=!0,n.channelsLoadingProbe=e,e||(w=!1),E();let{promise:a,resolve:s}=Promise.withResolvers();T=s,n.channelsRead={completion:a};try{let r=await t.request(`channels.status`,{probe:e,timeoutMs:8e3});if(!i())return;n.channelsSnapshot=r,n.channelsError=null,n.channelsLastSuccess=Date.now()}catch(e){if(!i())return;d(e)?(n.channelsSnapshot=null,n.channelsError=u(`channel status`)):n.channelsError=o(e)}finally{i()&&(n.channelsLoading=!1,n.channelsLoadingProbe=null,s(n.channelsError),w&&await A())}}let A=(e=!1)=>O(()=>k(e)),j=e=>O(async()=>{await e()&&await A(!0)}),M=e.subscribe(e=>{let t=n.client!==e.client,r=e.phase===`connected`,a=n.connected!==r,o=r?x(e,`operator.read`):v,c=v!==o;v=o;let l=r?b(e):y,u=y!==l;y=l;let d=r?x(e,`operator.admin`):S,f=S!==d;S=d,n.client=e.client,n.connected=r,(t||a||c)&&(E(),w=!1,n.channelsLoading=!1,n.channelsLoadingProbe=null,n.channelsRefreshSeq=(n.channelsRefreshSeq??0)+1,n.channelsError=r||!o?null:n.channelsError,(t||c)&&(n.channelsSnapshot=null,n.channelsLastSuccess=null)),(t||a||f)&&(i.whatsappEpoch+=1,n.whatsappBusy=!1,(t||!d)&&(n.whatsappLoginSessionKey=null,n.whatsappLoginMessage=null,n.whatsappLoginQrDataUrl=null,n.whatsappLoginConnected=null)),(t||a||u)&&(i.pairingEpoch+=1,(t||u)&&(n.pairingSnapshot=null,n.pairingRead=null,n.pairingError=null,n.pairingLastSuccess=null),s(),n.pairingBusyRequestId=null),D()}),N=e.subscribeEvents(e=>{e.event===`config.changed`&&n.connected&&v&&(n.channelsSnapshot||n.channelsLoading||_.size!==0)&&(w=!0,A())});return{get state(){return n},refresh:A,refreshPairing:()=>O(()=>l()),approvePairing:async e=>{let t=await O(()=>f(e.requestId,t=>t.request(`channels.pairing.approve`,e)));return t?t.result:null},dismissPairing:async e=>!!await O(()=>f(e.requestId,t=>t.request(`channels.pairing.dismiss`,e))),startWhatsApp:(e,t)=>j(()=>m(e,t)),waitWhatsApp:e=>j(()=>h(e)),logoutWhatsApp:e=>j(()=>g(e)),subscribe:e=>r(_,e),dispose(){C||(C=!0,E(),w=!1,n.channelsRefreshSeq=(n.channelsRefreshSeq??0)+1,i.whatsappEpoch+=1,i.pairingEpoch+=1,s(),n.pairingBusyRequestId=null,n.whatsappBusy=!1,M(),N(),_.clear())}}}function C(){return(C=e((()=>{n(),l(),i(),f()})))()}export{v as a,_ as c,S as i,y as l,g as n,C as o,p as r,m as s,h as t,b as u};