openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 8.62 kB
JavaScript
import{n as e,r as t}from"./rolldown-runtime-CNOWq4Kh.js";import{Qa as n,Uu as r,Wo as i,at as a,li as o,oi as s,rt as c}from"./_4____________________________.js";import{AN as l,EN as u,JT as d,ZT as f,qT as p}from"./_2_____________________________.js";var m=t({createChannelCapability:()=>C});function h(e,t){let n=e&&Object.hasOwn(e,t)&&e[t];return Array.isArray(n)?n:[]}function g(e,t){if(!e)return!1;let n=r(Object.hasOwn(e.channels,t)?e.channels[t]:void 0);return n?.configured===!0||n?.running===!0||n?.connected===!0||h(e.channelAccounts,t).some(e=>e.configured===!0||e.running===!0||e.connected===!0)}function _(e){return e?[...new Set([...e.channelOrder,...Object.keys(e.channels),...Object.keys(e.channelAccounts)])].some(t=>g(e,t)):!1}function v(e,t){if(!e)return null;let n=r(e.channels);return r(n?.[t])??r(e[t])}function y(e){if(e==null)return o(`common.na`);if(typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return String(e);try{return JSON.stringify(e)}catch{return o(`common.na`)}}function b(e){let t=v(e.configForm,e.channelId);return t?e.fields.flatMap(e=>e in t?[{label:e,value:y(t[e])}]:[]):[]}function x(e){let t=e.hello?.auth;return JSON.stringify({role:t?.role??null,scopes:t?.scopes?[...t.scopes].toSorted():null})}function S(e,t){let n=e.hello?.auth;return!n?.scopes||a({role:n.role??`operator`,requestedScopes:[t],allowedScopes:n.scopes})}function C(e){let t=e.snapshot,r={configEdits:{},configEditBases:{},configEditsScope:null,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,pairingBusy:null,whatsappLoginMessage:null,whatsappLoginQrDataUrl:null,whatsappLoginSessionKey:null,whatsappLoginConnected:null,whatsappBusy:!1},a={whatsappEpoch:0,pairingEpoch:0},s;function c(){s?.(null),s=void 0,r.pairingRefreshSeq+=1,r.pairingLoading=!1}async function l(e={}){let t=r.client;if(!t||!r.connected||r.pairingLoading||r.pairingBusy&&!e.duringMutation)return;let n=r.pairingRefreshSeq+1,i=()=>r.connected&&r.client===t&&r.pairingRefreshSeq===n;r.pairingRefreshSeq=n,r.pairingLoading=!0,r.pairingError=null;let{promise:a,resolve:o}=Promise.withResolvers();s=o,r.pairingRead={completion:a};let c=null;try{let e=await t.request(`channels.pairing.list`,{});if(!i())return;r.pairingSnapshot=e,r.pairingLastSuccess=Date.now()}catch(e){c=u(e)}finally{i()&&(o(c),s=void 0,r.pairingLoading=!1)}}async function d(e,t,n){let i=r.client;if(!i||!r.connected||r.pairingBusy)return null;let o=a.pairingEpoch,s=()=>r.connected&&r.client===i&&a.pairingEpoch===o&&r.pairingBusy?.requestId===e;c(),r.pairingBusy={requestId:e,action:t},r.pairingRead=null,r.pairingError=null;try{let t=await n(i);if(!s())return null;let a=r.pairingSnapshot;return a?.requests.some(t=>t.requestId===e)&&(r.pairingSnapshot={...a,requests:a.requests.filter(t=>t.requestId!==e)}),c(),await l({duringMutation:!0}),s()?{result:t}:null}catch(e){return s()&&(r.pairingError=u(e)),null}finally{s()&&(r.pairingBusy=null)}}async function m(e,t,n){let i=r.client;if(!i||!r.connected||r.whatsappBusy)return!1;r.whatsappBusy=!0;let o=a.whatsappEpoch,s=()=>r.connected&&r.client===i&&a.whatsappEpoch===o;try{let n=await e(i);if(!s())return!1;t(n)}catch(e){return s()&&(r.whatsappLoginMessage=u(e),n?.()),!1}finally{s()&&(r.whatsappBusy=!1)}return!0}function h(e,t){return m(n=>n.request(`web.login.start`,{channel:`whatsapp`,force:e,timeoutMs:3e4,...t?{accountId:t}:{}}),e=>{r.whatsappLoginSessionKey=e.connected?null:e.sessionKey??null,r.whatsappLoginMessage=e.message?u(e.message):null,r.whatsappLoginQrDataUrl=e.qrDataUrl??null,r.whatsappLoginConnected=typeof e.connected==`boolean`?e.connected:null},()=>{r.whatsappLoginQrDataUrl=null,r.whatsappLoginSessionKey=null,r.whatsappLoginConnected=null})}function g(e){return m(t=>t.request(`web.login.wait`,{channel:`whatsapp`,timeoutMs:12e4,currentQrDataUrl:r.whatsappLoginQrDataUrl??void 0,...r.whatsappLoginSessionKey?{sessionKey:r.whatsappLoginSessionKey}:{},...e?{accountId:e}:{}}),e=>{r.whatsappLoginMessage=e.message?u(e.message):null,r.whatsappLoginConnected=e.connected??null,e.connected&&(r.whatsappLoginSessionKey=null),e.qrDataUrl?r.whatsappLoginQrDataUrl=e.qrDataUrl:e.connected&&(r.whatsappLoginQrDataUrl=null)},()=>{r.whatsappLoginConnected=null})}function _(e){return m(t=>t.request(`channels.logout`,{channel:`whatsapp`,...e?{accountId:e}:{}}),e=>{e.cleared?(r.whatsappLoginMessage=o(`channels.whatsapp.loggedOut`),r.whatsappLoginQrDataUrl=null,r.whatsappLoginSessionKey=null,r.whatsappLoginConnected=null):r.whatsappLoginMessage=o(`channels.whatsapp.logoutNotCleared`)})}let v=new Set,y=S(e.snapshot,`operator.read`),b=x(e.snapshot),C=S(e.snapshot,`operator.admin`),w=!1,T=!1,E;function D(){E?.(null),E=void 0,r.channelsRead=null}let O=()=>{if(!w)for(let e of v)e(r)},k=async e=>{if(w)return;let t=e();O();try{return await t}finally{O()}};async function A(e){let t=r.client;if(!t||!r.connected||r.channelsLoading&&(!r.channelsLoadingProbe||e))return;let n=(r.channelsRefreshSeq??0)+1,i=()=>r.client===t&&r.channelsRefreshSeq===n;r.channelsRefreshSeq=n,r.channelsLoading=!0,r.channelsLoadingProbe=e,e||(T=!1),D();let{promise:a,resolve:o}=Promise.withResolvers();E=o,r.channelsRead={completion:a};try{let n=await t.request(`channels.status`,{probe:e,timeoutMs:8e3});if(!i())return;r.channelsSnapshot=n,r.channelsError=null,r.channelsLastSuccess=Date.now()}catch(e){if(!i())return;if(f(e)){let t=await p(`channel status`,u(e));if(!i())return;r.channelsSnapshot=null,r.channelsError=t}else r.channelsError=u(e)}finally{i()&&(r.channelsLoading=!1,r.channelsLoadingProbe=null,o(r.channelsError),T&&await j())}}let j=(e=!1)=>k(()=>A(e)),M=e=>k(async()=>{await e()&&await j(!0)}),N=e.subscribe(e=>{let t=r.client!==e.client,n=e.phase===`connected`,i=r.connected!==n,o=n?S(e,`operator.read`):y,s=y!==o;y=o;let l=n?x(e):b,u=b!==l;b=l;let d=n?S(e,`operator.admin`):C,f=C!==d;C=d,r.client=e.client,r.connected=n,(t||i||s)&&(D(),T=!1,r.channelsLoading=!1,r.channelsLoadingProbe=null,r.channelsRefreshSeq=(r.channelsRefreshSeq??0)+1,r.channelsError=n||!o?null:r.channelsError,(t||s)&&(r.channelsSnapshot=null,r.channelsLastSuccess=null)),(t||i||f)&&(a.whatsappEpoch+=1,r.whatsappBusy=!1,(t||!d)&&(r.whatsappLoginSessionKey=null,r.whatsappLoginMessage=null,r.whatsappLoginQrDataUrl=null,r.whatsappLoginConnected=null)),(t||i||u)&&(a.pairingEpoch+=1,(t||u)&&(r.pairingSnapshot=null,r.pairingRead=null,r.pairingError=null,r.pairingLastSuccess=null),c(),r.pairingBusy=null),O()}),P=null,F=typeof window>`u`?void 0:i(window,`beforeunload`,e=>{Object.keys(r.configEdits).length>0&&(e.preventDefault(),e.returnValue=``)}),I=e.subscribeEvents(e=>{e.event===`config.changed`&&r.connected&&y&&(r.channelsSnapshot||r.channelsLoading||v.size!==0)&&(T=!0,j())});return{get state(){return r},refresh:j,requestConfigReview(e){P=e,O()},takeConfigReview(){let e=P;return P=null,e},syncConfigEditsScope(e){r.configEditsScope!==e&&(r.configEditsScope=e,Object.keys(r.configEdits).length>0&&(r.configEdits={},r.configEditBases={},O()))},editConfig(e,t,n){let i=r.configEditBases[e]??[],a=n?JSON.stringify(n.path):null;r.configEdits={...r.configEdits,[e]:[...r.configEdits[e]??[],t]},n&&!i.some(e=>JSON.stringify(e.path)===a)&&(r.configEditBases={...r.configEditBases,[e]:[...i,n]}),O()},replaceConfigEdits(e,t,n){let{[e]:i,...a}=r.configEdits,{[e]:o,...s}=r.configEditBases;r.configEdits=t.length>0?{...a,[e]:t}:a,r.configEditBases=t.length>0?{...s,[e]:n}:s,O()},restoreConfigField(e,t){let n=JSON.stringify(t),{[e]:i=[],...a}=r.configEdits,o=i.filter(e=>JSON.stringify(e.path)!==n);r.configEdits=o.length>0?{...a,[e]:o}:a,O()},discardConfigEdits(e){let{[e]:t,...n}=r.configEdits,{[e]:i,...a}=r.configEditBases;r.configEdits=n,r.configEditBases=a,O()},refreshPairing:()=>k(()=>l()),approvePairing:e=>k(()=>d(e.requestId,`approve`,t=>t.request(`channels.pairing.approve`,e))).then(e=>e?.result??null),dismissPairing:async e=>!!await k(()=>d(e.requestId,`dismiss`,t=>t.request(`channels.pairing.dismiss`,e))),startWhatsApp:(e,t)=>M(()=>h(e,t)),waitWhatsApp:e=>M(()=>g(e)),logoutWhatsApp:e=>M(()=>_(e)),subscribe:e=>n(v,e),dispose(){w||(w=!0,D(),T=!1,r.channelsRefreshSeq=(r.channelsRefreshSeq??0)+1,a.whatsappEpoch+=1,a.pairingEpoch+=1,c(),r.pairingBusy=null,r.whatsappBusy=!1,N(),I(),F?.(),v.clear())}}}function w(){return(w=e((()=>{c(),s(),l(),d()})))()}export{y as a,v as c,C as i,b as l,_ as n,w as o,m as r,h as s,g as t,x as u};