UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

219 lines (218 loc) • 7.98 kB
import { g as readStringValue, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { T as parseStrictInteger } from "./number-coercion-D6u-4YD8.mjs"; import { o as hasConfiguredSecretInput } from "./types.secrets-BmNLLnHp.mjs"; import { b as resolveGatewayPort } from "./paths-DAcAg5aV.mjs"; import { s as isLoopbackHost } from "./net-BgRayX-P.mjs"; import "./config-CMA-J084.mjs"; import { r as theme, t as colorize } from "./theme-BOI0xyRZ.mjs"; import { n as resolveGatewayProbeSurfaceAuth } from "./auth-surface-resolution-CJMC_AjZ.mjs"; //#region src/commands/gateway-presence.ts function parseLegacyGatewaySelfText(e) { const t = e.match(/^Gateway:\s*([^ (·]+)(?:\s*\(([^)]+)\))?/i); if (!t) return {}; return { host: readStringValue(t[1]), ip: readStringValue(t[2]) }; } function pickGatewaySelfPresence(e) { if (!Array.isArray(e)) return null; const t = e; const n = t.find((e) => e.mode === "gateway" && e.reason === "self") ?? t.find((e) => typeof e.text === "string" && e.text.startsWith("Gateway:")) ?? null; if (!n) return null; const r = typeof n.text === "string" ? parseLegacyGatewaySelfText(n.text) : {}; const i = { host: readStringValue(n.host) ?? r.host, ip: readStringValue(n.ip) ?? r.ip, version: readStringValue(n.version), platform: readStringValue(n.platform) }; for (const e of ["deviceId", "instanceId"]) { const t = readStringValue(n[e]); if (t) i[e] = t; } return i; } //#endregion //#region src/commands/gateway-status/helpers.ts const LEGACY_MISSING_SCOPE_PATTERN = /\bmissing scope:\s*[a-z0-9._-]+/i; function parseIntOrNull(e) { const t = typeof e === "string" ? e.trim() : typeof e === "number" || typeof e === "bigint" ? String(e) : ""; if (!t) return null; return parseStrictInteger(t) ?? null; } function normalizeWsUrl(e) { const t = e.trim(); if (!t) return null; if (!t.startsWith("ws://") && !t.startsWith("wss://")) return null; return t; } function resolveTargets(e, t, n) { const r = []; const add = (e) => { if (!r.some((t) => t.url === e.url)) r.push(e); }; const i = typeof t === "string" ? normalizeWsUrl(t) : null; if (i) add({ id: "explicit", kind: "explicit", url: i, active: true }); const a = n ?? resolveGatewayPort(e); const o = { id: "localLoopback", kind: "localLoopback", url: `${e.gateway?.tls?.enabled === true ? "wss" : "ws"}://127.0.0.1:${a}`, active: n !== void 0 || e.gateway?.mode !== "remote" }; if (n !== void 0 && !i) { add(o); return r; } const s = typeof e.gateway?.remote?.url === "string" ? normalizeWsUrl(e.gateway.remote.url) : null; if (s) add({ id: "configRemote", kind: "configRemote", url: s, active: e.gateway?.mode === "remote" }); add(o); return r; } function resolveProbeBudgetMs(e, t) { if (t.kind === "sshTunnel") return Math.min(2e3, e); if (t.active) return e; if (t.kind === "localLoopback") return Math.min(800, e); const n = URL.parse(t.url); if (n === null || !isLoopbackHost(n.hostname)) return Math.min(1500, e); return e; } /** Normalizes user-entered SSH targets, accepting both raw targets and `ssh host` input. */ function sanitizeSshTarget(e) { return normalizeOptionalString(e)?.replace(/^ssh\s+/, "") ?? null; } async function resolveAuthForTarget(e, t, n) { const r = normalizeOptionalString(n.token); const i = normalizeOptionalString(n.password); if (r || i) return { token: r, password: i }; const a = await resolveGatewayProbeSurfaceAuth({ config: e, surface: t.kind === "configRemote" || t.kind === "sshTunnel" ? "remote" : "local" }); return { token: a.token, password: a.password, ...a.diagnostics ? { diagnostics: a.diagnostics } : {} }; } function extractConfigSummary(e) { const t = e; const n = typeof t?.path === "string" ? t.path : null; const r = Boolean(t?.exists); const i = Boolean(t?.valid); const a = Array.isArray(t?.issues) ? t.issues : []; const o = Array.isArray(t?.legacyIssues) ? t.legacyIssues : []; const summarizeIssues = (e) => e.filter((e) => e && typeof e.path === "string" && typeof e.message === "string").map((e) => ({ path: e.path, message: e.message })); const s = t?.config ?? {}; const c = s.gateway ?? {}; const l = (s.secrets ?? {}).defaults ?? void 0; const u = (s.discovery ?? {}).wideArea ?? {}; const d = c.remote ?? {}; const f = c.auth ?? {}; const p = c.controlUi ?? {}; const m = c.tailscale ?? {}; const h = typeof f.mode === "string" ? f.mode : null; const g = hasConfiguredSecretInput(f.token, l); const _ = hasConfiguredSecretInput(f.password, l); const v = typeof d.url === "string" ? normalizeWsUrl(d.url) : null; const y = hasConfiguredSecretInput(d.token, l); const b = hasConfiguredSecretInput(d.password, l); const x = typeof u.domain === "string" ? u.domain.trim().length > 0 : null; return { path: n, exists: r, valid: i, issues: summarizeIssues(a), legacyIssues: summarizeIssues(o), gateway: { mode: typeof c.mode === "string" ? c.mode : null, bind: typeof c.bind === "string" ? c.bind : null, port: parseIntOrNull(c.port), controlUiEnabled: typeof p.enabled === "boolean" ? p.enabled : null, controlUiBasePath: typeof p.basePath === "string" ? p.basePath : null, authMode: h, authTokenConfigured: g, authPasswordConfigured: _, remoteUrl: v, remoteTokenConfigured: y, remotePasswordConfigured: b, tailscaleMode: typeof m.mode === "string" ? m.mode : null }, discovery: { wideAreaEnabled: x } }; } /** Returns true when auth succeeded enough to connect but lacks the read scope. */ function isScopeLimitedProbeFailure(e) { if (e.ok || !e.gatewayReached) return false; if (e.missingScopeErrorDetails) return e.missingScopeErrorDetails.missingScope === "operator.read"; return LEGACY_MISSING_SCOPE_PATTERN.test(e.error ?? ""); } /** Returns true when the gateway connection was established but a later probe failed. */ function isPostConnectProbeFailure(e) { return !e.ok && e.gatewayReached === true; } /** Returns true when the probe established any gateway connection. */ function isProbeReachable(e) { return e.ok || e.gatewayReached === true; } const gatewayProbeCapabilities = [ { capability: "admin_capable", label: "admin-capable", color: "info" }, { capability: "write_capable", label: "write-capable", color: "info" }, { capability: "read_only", label: "read-only", color: "info" }, { capability: "connected_no_operator_scope", label: "connect-only", color: "warn" }, { capability: "pairing_pending", label: "pairing pending", color: "warn" } ]; function summarizeGatewayProbeCapability(e) { return gatewayProbeCapabilities.find(({ capability: t }) => e.some((e) => e.auth.capability === t))?.capability ?? "unknown"; } function renderProbeSummaryLine(e, t) { const n = gatewayProbeCapabilities.find(({ capability: t }) => t === e.auth.capability); const r = colorize(t, theme[n?.color ?? "muted"], `Capability: ${n?.label ?? "unknown"}`); const i = !e.ok && e.error ? ` - ${e.error}` : ""; if (e.ok || e.gatewayReached && e.connectLatencyMs != null) { const n = typeof e.connectLatencyMs === "number" ? `${e.connectLatencyMs}ms` : "unknown"; const a = e.ok ? colorize(t, theme.success, "Read check: ok") : isScopeLimitedProbeFailure(e) ? colorize(t, theme.warn, "Read check: limited") : colorize(t, theme.error, "Read check: failed"); return `${colorize(t, theme.success, "Connect: ok")} (${n}) · ${r} · ${a}${i}`; } if (e.auth.capability === "pairing_pending") return `${colorize(t, theme.warn, "Connect: blocked")}${i} · ${r}`; return `${colorize(t, theme.error, "Connect: failed")}${i} · ${r}`; } //#endregion export { renderProbeSummaryLine as a, resolveTargets as c, pickGatewaySelfPresence as d, isScopeLimitedProbeFailure as i, sanitizeSshTarget as l, isPostConnectProbeFailure as n, resolveAuthForTarget as o, isProbeReachable as r, resolveProbeBudgetMs as s, extractConfigSummary as t, summarizeGatewayProbeCapability as u };