UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

159 lines (158 loc) • 7.05 kB
import { n as resolveOpenClawPackageRoot } from "./openclaw-root-C-ayo09z.mjs"; import { t as VERSION } from "./version-GlxUMdxI.mjs"; import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { a as channelToNpmTag, l as resolveEffectiveUpdateChannel } from "./update-channels-CDc4OFaS.mjs"; import { it as validateDevicePairSetupStatusParams, rt as validateDevicePairSetupCodeParams } from "./src-BX8OAe1J.mjs"; import { n as runCommandWithTimeout } from "./exec-runner-Br73vG-X.mjs"; import "./exec-aRIvS9s4.mjs"; import { t as quoteCliArg } from "./quote-cli-arg-J7A9UE6m.mjs"; import { s as isLoopbackHost } from "./net-BgRayX-P.mjs"; import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs"; import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs"; import { a as NODE_PAIRING_SETUP_BOOTSTRAP_PROFILE, o as PAIRING_SETUP_BOOTSTRAP_PROFILE, s as VOICE_NODE_PAIRING_SETUP_BOOTSTRAP_PROFILE } from "./device-bootstrap-profile-COYynVlw.mjs"; import { r as respondUnavailableOnThrow } from "./response-C7fEiIN1.mjs"; import { n as defineValidatedGatewayHandler } from "./validation-DwNk6A-B.mjs"; import { t as fetchNpmPackageTargetStatus } from "./update-check-package-target-f97Li08r.mjs"; import { c as resolveUpdateInstallKind } from "./update-check-BfPo_IXH.mjs"; import { a as readGatewayRequestMutationAuthority } from "./session-mutation-guards-Bjnk_O0z.mjs"; import { n as renderQrPngDataUrl } from "./qr-image-CxTsSOlZ.mjs"; import { l as readDevicePairSetupCompletion } from "./device-bootstrap-5DbASnAD.mjs"; import { o as resolvePairingSetupFromConfig, r as encodePairingSetupCode } from "./setup-code-BZtNhry3.mjs"; import { n as registerDevicePairingJoinCode } from "./device-pairing-join-code-Bz52UdzO.mjs"; import { n as currentUpdateCheckLifecycle } from "./update-check-lifecycle-DOW7bMCO.mjs"; //#region src/gateway/server-methods/device-pair-setup.ts const MAX_QR_DATA_URL_LENGTH = 16384; function resolveDevicePairingJoinBaseUrl(e) { for (const t of e.urls ?? [e.url]) { const e = new URL(t); if (e.protocol === "wss:" || e.protocol === "ws:" && isLoopbackHost(e.hostname)) { e.protocol = e.protocol === "wss:" ? "https:" : "http:"; return e; } } throw new Error("Join URLs require a TLS gateway endpoint, except for loopback. Use the setup code directly for plaintext LAN pairing."); } async function resolveJoinPackage() { const e = currentUpdateCheckLifecycle().installStatus?.status ?? { installKind: await resolveUpdateInstallKind(await resolveOpenClawPackageRoot({ moduleUrl: import.meta.url, argv1: process.argv[1] }), { timeoutMs: 1e3 }).catch(() => "unknown") }; const t = "The machine needs a matching Gateway build; the exact npm release could not be confirmed."; if (e.installKind === "package" || e.installKind === "host") { if ((await fetchNpmPackageTargetStatus({ target: VERSION, timeoutMs: 1e3 })).version === VERSION) return { packageSpec: `openclaw@${VERSION}` }; } const n = resolveEffectiveUpdateChannel({ currentVersion: VERSION, ...e }).channel; const r = channelToNpmTag(n); return { packageSpec: (await fetchNpmPackageTargetStatus({ target: r, timeoutMs: 1e3 })).version ? `openclaw@${r}` : "openclaw", versionNote: t }; } const devicePairSetupHandlers = { "device.pair.setupCode": defineValidatedGatewayHandler("device.pair.setupCode", validateDevicePairSetupCodeParams, async (e) => { const { params: t, respond: n, context: r } = e; await respondUnavailableOnThrow(n, async () => { if (t.joinUrl === true && t.bootstrapProfile !== void 0 && t.bootstrapProfile !== "node") { n(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "Join URLs require bootstrapProfile=node.")); return; } const i = r.getRuntimeConfig(); const a = t.joinUrl === true ? captureOpenClawStateWorkerContext() : void 0; const o = readGatewayRequestMutationAuthority(e); const s = a ? () => { a.admission.assertCurrent(); o.assertCurrent(); if (r.getRuntimeConfig() !== i) throw new Error("Device pairing setup configuration changed."); } : void 0; s?.(); const c = t.publicUrl; const l = await resolvePairingSetupFromConfig(i, { env: process.env, publicUrl: c, preferRemoteUrl: t.preferRemoteUrl === true, useLocalGateway: i.gateway?.mode === "remote" && t.preferRemoteUrl !== true, localTlsFingerprint: r.gatewayTlsFingerprint, ...t.joinUrl === true || t.bootstrapProfile ? { bootstrapProfile: t.joinUrl === true || t.bootstrapProfile === "node" ? NODE_PAIRING_SETUP_BOOTSTRAP_PROFILE : t.bootstrapProfile === "voice-node" ? VOICE_NODE_PAIRING_SETUP_BOOTSTRAP_PROFILE : PAIRING_SETUP_BOOTSTRAP_PROFILE } : {}, runCommandWithTimeout: async (e, t) => await runCommandWithTimeout(e, { timeoutMs: t.timeoutMs }) }); s?.(); if (!l.ok) { n(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, l.error)); return; } const u = encodePairingSetupCode(l.payload); let d; let f; if (t.joinUrl === true) { const e = resolveDevicePairingJoinBaseUrl(l.payload); const { packageSpec: t, versionNote: n } = await resolveJoinPackage(); s?.(); const r = await registerDevicePairingJoinCode({ payload: l.payload, expiresAtMs: l.expiresAtMs, context: a, assertCurrent: s }); e.pathname = `${e.pathname.replace(/\/+$/u, "")}/j/${r}`; e.search = ""; e.hash = ""; d = e.toString(); const i = quoteCliArg(d); const o = `npx -y ${t} connect ${i} --service`; f = { command: `${o} --session-host`, serviceCommand: o, installedCommand: `openclaw connect ${i} --service --session-host`, ...n ? { versionNote: n } : {} }; } const p = t.includeQr !== false ? await renderQrPngDataUrl(u).catch(() => void 0) : void 0; const m = p && p.length <= MAX_QR_DATA_URL_LENGTH ? p : void 0; s?.(); n(true, { setupId: l.setupId, expiresAtMs: l.expiresAtMs, setupCode: u, ...d ? { joinUrl: d, ...f } : {}, ...m ? { qrDataUrl: m } : {}, gatewayUrl: l.payload.url, ...l.payload.urls ? { gatewayUrls: l.payload.urls } : {}, auth: l.authLabel, urlSource: c ? "request.publicUrl" : l.urlSource, access: l.access, ...l.accessDowngraded ? { accessDowngraded: true } : {} }, void 0); }); }), "device.pair.setupStatus": defineValidatedGatewayHandler("device.pair.setupStatus", validateDevicePairSetupStatusParams, async ({ params: e, respond: t }) => { await respondUnavailableOnThrow(t, async () => { const n = await readDevicePairSetupCompletion({ setupId: e.setupId }); let r = {}; if (n) { const e = { setupId: n.setupId, deviceId: n.deviceId, ...n.deviceName ? { deviceName: n.deviceName } : {}, access: n.access, ts: n.completedAtMs }; r = n.deliveryState === "confirmed" ? { completion: e } : { deliveryUncertain: e }; } t(true, r, void 0); }); }) }; //#endregion export { devicePairSetupHandlers };