openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 2.82 kB
JavaScript
import{a as throwIfSettledRejected}from"./error-graph-internal-Bt2pFJBM.mjs";import{r as buildCloudflareAccessHeaders}from"./cloudflare-access-D6jOo-jv.mjs";import{i as loadDeviceAuthTokenReadOnly}from"./device-auth-store-B4Rbv-Uh.mjs";import{n as gatewayOriginScope}from"./gateway-origin-scope-XnuMqfjZ.mjs";import{t as GatewayClient}from"./client-BH-Snzbs.mjs";
//!src/node-host/gateway-candidate-connection.ts
function formatGatewayCandidateUrl(e){if(e.kind===`openclaw-id`){if(!e.openclawId)throw new Error(`OpenClaw ID node target is missing`);return`openclaw-id://${e.openclawId.pin.claw_id}`}const t=e.host??`127.0.0.1`;const n=t.includes(`:`)&&!(t.startsWith(`[`)&&t.endsWith(`]`))?`[${t}]`:t;const r=e.port??18789;return`${e.tls?`wss`:`ws`}://${n}:${r}${e.contextPath?e.contextPath.startsWith(`/`)?e.contextPath:`/${e.contextPath}`:``}`}function createNodeHostGatewayCandidateConnection(e){if(e.candidates.length===0)throw new Error(`node host gateway candidate list cannot be empty`);let t=0;let n=false;let r;const i=[];let a=e.candidates.length===1;let o;let s=createCandidateClient(t);function createCandidateClient(r){const c=e.candidates[r];if(!c)throw new Error(`node host gateway candidate ${r} is unavailable`);const l=e.candidateUrls?.get(c)??formatGatewayCandidateUrl(c);const u=e.cloudflareAccessByCandidate?.get(c);const d=new GatewayClient({...e.clientOptions,url:l,tlsFingerprint:c.tlsFingerprint,...u?{edgeAuthHeaders:buildCloudflareAccessHeaders(u)}:{},onEvent:n=>{if(t===r)e.onEvent(n)},onHelloOk:n=>{if(t!==r)return;if(!a){a=true;e.onWinningCandidate(c)}e.onHelloOk(n,l,c.tlsFingerprint,u)},onConnectError:n=>{if(t===r)e.onConnectError(n)},onReconnectPaused:n=>{if(t===r)e.onReconnectPaused(n)},onClose:(i,o,c)=>{if(n||t!==r)return;e.onClose(i,o,c);const l=r+1;if(n||a||l>=e.candidates.length||c?.phase!==`pre-hello`||c.connectRequestSent!==false)return;t=l;d.stop();queueMicrotask(()=>{if(n||t!==l)return;s=createCandidateClient(l);s.start()})}});if(o)d.updateNodeManifest(o);i.push(d);return d}return{start(){if(!n)s.start()},stop(){n=true;r??=Promise.resolve().then(async()=>{const e=await Promise.allSettled(i.map(e=>e.stopAndWait()));throwIfSettledRejected(e,`node host gateway cleanup failed`)});return r},request(...e){return s.request(...e)},updateNodeManifest(e){o=e;s.updateNodeManifest(e)}}}
//!src/node-host/gateway-auth.ts
async function canReuseNodeHostDeviceToken(e){const t=e.savedGateway?gatewayOriginScope(formatGatewayCandidateUrl(e.savedGateway)):void 0;return Boolean(t&&e.gatewayCandidates.every(e=>gatewayOriginScope(formatGatewayCandidateUrl(e))===t)&&(await loadDeviceAuthTokenReadOnly({deviceId:e.deviceId,role:`node`,env:e.env??process.env}))?.token)}export{createNodeHostGatewayCandidateConnection as n,formatGatewayCandidateUrl as r,canReuseNodeHostDeviceToken as t};