UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

314 lines (313 loc) • 11.4 kB
import { h as normalizeUniqueSingleOrTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs"; import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs"; import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs"; import "./paths-DAcAg5aV.mjs"; import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs"; import { t as quoteCliArg } from "./quote-cli-arg-J7A9UE6m.mjs"; import { o as roleScopesAllow } from "./operator-scope-compat-CpFHu-FC.mjs"; import { n as normalizeDeviceAuthScopes } from "./device-auth-Cu5vRDvP.mjs"; import { a as loadDeviceAuthTokens } from "./device-auth-store-B3F61F1r.mjs"; import { i as listApprovedPairedDeviceRoles } from "./device-pairing-identity-BqPJxbIo.mjs"; import { n as loadDeviceIdentityIfPresentAsync } from "./device-identity-async-B1EfjFkw.mjs"; import { a as listDevicePairingReadOnly } from "./device-pairing-D--Fzrek.mjs"; import { r as summarizeDeviceTokens } from "./device-pairing-token-utils-fd1VQ1i6.mjs"; import "./device-pairing-tokens-Bivg3I0v.mjs"; //#region src/commands/doctor-device-pairing.ts const DEVICE_PAIRING_CHECK_ID = "core/doctor/device-pairing"; function normalizeGatewayPairedDevice(e) { return { ...e, tokenSummaries: e.tokens ?? [] }; } function normalizeLocalPairedDevice(e) { return { ...e, tokenSummaries: summarizeDeviceTokens(e.tokens) ?? [] }; } async function loadDoctorPairingSnapshot(e) { if (e.healthOk) try { const { bindAgentToolGatewayRequest: t } = await import("./in-process-gateway-uUYBiTqp.mjs"); const n = await t({ hostedOnly: true })({ method: "device.pair.list", timeoutMs: 5e3, sharedStateMode: "read-only", config: e.cfg }); return { pending: n.pending, paired: n.paired.map(normalizeGatewayPairedDevice) }; } catch {} if (e.cfg.gateway?.mode === "remote") return null; const t = await listDevicePairingReadOnly(); return { pending: t.pending, paired: t.paired.map(normalizeLocalPairedDevice) }; } function resolveApprovedScopes(e) { return normalizeDeviceAuthScopes(e.approvedScopes ?? e.scopes); } function formatValues(e) { return e.length > 0 ? e.join(", ") : "none"; } function formatCliArgs(e) { return formatCliCommand(e.map(quoteCliArg).join(" ")); } function formatRotateCommand(e, t) { return formatCliArgs([ "openclaw", "devices", "rotate", "--device", e, "--role", t ]); } function describeDevice(e) { const t = sanitizeTerminalText(e.displayName?.trim() || "") || sanitizeTerminalText(e.clientId?.trim() || ""); return t ? `${t} (${e.deviceId})` : e.deviceId; } function findTokenSummary(e, t) { const n = t.trim(); return e.tokenSummaries.find((e) => e.role === n && !e.revokedAtMs); } function collectPendingPairingFindings(e) { const t = new Map(e.paired.map((e) => [e.deviceId, e])); return e.pending.map((e) => { const n = t.get(e.deviceId); const r = describeDevice(e); const i = formatCliArgs([ "openclaw", "devices", "approve", e.requestId ]); const a = formatCliArgs([ "openclaw", "devices", "list" ]); const o = `Review with ${a}, then approve with ${i}.`; const s = { checkId: DEVICE_PAIRING_CHECK_ID, severity: "warning", category: "fix-now", docsUrl: "https://docs.openclaw.ai/cli/devices", path: "devices.pending", target: `${e.deviceId}:${e.requestId}`, fixHint: o }; if (!n) return { ...s, category: "recommended", requirement: "first-time", message: `Pending device pairing request ${e.requestId} for ${r}. ${o}` }; if (n.publicKey !== e.publicKey) { const t = `Remove the stale record with ${formatCliArgs([ "openclaw", "devices", "remove", e.deviceId ])}, then rerun ${a} and approve with ${i}.`; return { ...s, requirement: "public-key-repair", message: `Pending device repair ${e.requestId} for ${r}: the current device identity no longer matches the approved pairing record. This commonly loops on pairing-required for an already paired device. ${t}`, fixHint: t }; } const c = normalizeUniqueSingleOrTrimmedStringList([e.roles, e.role].flat()); const l = listApprovedPairedDeviceRoles(n); if (c.some((e) => !l.includes(e))) return { ...s, category: "recommended", requirement: "role-upgrade", message: `Pending role upgrade ${e.requestId} for ${r}: approved roles [${formatValues(l)}], requested roles [${formatValues(c)}]. ${o}` }; const u = resolveApprovedScopes(n); const d = normalizeDeviceAuthScopes(e.scopes); if (c.some((e) => !roleScopesAllow({ role: e, requestedScopes: d.filter((t) => e === "operator" ? t.startsWith("operator.") : !t.startsWith("operator.")), allowedScopes: u }))) return { ...s, category: "recommended", requirement: "scope-upgrade", message: `Pending scope upgrade ${e.requestId} for ${r}: approved scopes [${formatValues(u)}], requested scopes [${formatValues(d)}]. ${o}` }; return { ...s, requirement: "repair", message: `Pending device repair ${e.requestId} for ${r}: the device is already paired, but a new approval is still required before the requested auth can be used. ${o}` }; }); } function collectPairedRecordFindings(e) { const t = []; for (const n of e.paired) { const e = describeDevice(n); const r = { checkId: DEVICE_PAIRING_CHECK_ID, severity: "warning", category: "fix-now", docsUrl: "https://docs.openclaw.ai/cli/devices", path: "devices.paired", target: n.deviceId }; const i = listApprovedPairedDeviceRoles(n); const a = resolveApprovedScopes(n); if (i.includes("operator") && a.length === 0) t.push({ ...r, requirement: "missing-operator-scope-baseline", message: `Paired device ${e} is missing its approved operator scope baseline. Scope upgrades can get stuck in pairing-required until the device repairs or is re-approved.`, fixHint: `Run ${formatCliArgs([ "openclaw", "devices", "list" ])} to inspect the device, reconnect it to request the intended scopes, then approve only the scopes you recognize.` }); for (const o of i) { const i = findTokenSummary(n, o); const s = formatRotateCommand(n.deviceId, o); if (!i) { t.push({ ...r, target: o ? `${n.deviceId}:${o}` : n.deviceId, requirement: "missing-active-role-token", message: `Paired device ${e} has no active ${o} device token even though the role is approved. This commonly ends in pairing-required or device-token-mismatch. Rotate a fresh token with ${s}.`, fixHint: `Rotate a fresh token with ${s}.` }); continue; } if (i.scopes.length > 0 && !roleScopesAllow({ role: o, requestedScopes: i.scopes, allowedScopes: a })) { const i = o === "node" ? `${s} --no-scopes` : s; t.push({ ...r, target: o ? `${n.deviceId}:${o}` : n.deviceId, requirement: "token-outside-approved-scope", message: `Paired device ${e} has a ${o} token outside the approved scope baseline [${formatValues(a)}]. Rotate it with ${i}.`, fixHint: `Rotate it with ${i}.` }); } } } return t; } async function collectLocalDeviceAuthFindings(e) { let t; try { t = await loadDeviceIdentityIfPresentAsync({ env: process.env }); } catch { return []; } if (!t) return []; const n = await loadDeviceAuthTokens({ deviceId: t.deviceId, env: process.env }).catch(() => []); const r = e.paired.find((e) => e.deviceId === t.deviceId); if (!r) return []; const i = describeDevice(r); const a = []; const o = new Set(listApprovedPairedDeviceRoles(r)); for (const e of n) { const t = e.role.trim(); if (!t) continue; const n = { checkId: DEVICE_PAIRING_CHECK_ID, severity: "warning", category: "fix-now", docsUrl: "https://docs.openclaw.ai/cli/devices", path: "identity.device-auth", target: `${r.deviceId}:${t}` }; const s = findTokenSummary(r, t); if (!s) { if (o.has(t)) continue; a.push({ ...n, requirement: "local-role-no-longer-approved", message: `Local cached ${t} device auth for ${i} no longer has a matching active gateway token, and that role is no longer approved for this device. Reconnect with shared gateway auth to refresh local auth, or remove the stale cached ${t} auth entry.`, fixHint: `Reconnect with shared gateway auth to refresh local auth, or remove the stale cached ${t} auth entry.` }); continue; } const c = formatRotateCommand(r.deviceId, t); const l = s.rotatedAtMs ?? s.createdAtMs; if (e.updatedAtMs < l) { a.push({ ...n, requirement: "local-token-stale", message: `Local cached ${t} device token for ${i} predates the gateway rotation. This is a stale device-token pattern and can fail with device token mismatch. Reconnect with shared gateway auth to refresh it, or rotate again with ${c}.`, fixHint: `Reconnect with shared gateway auth to refresh it, or rotate again with ${c}.` }); continue; } const u = normalizeDeviceAuthScopes(e.scopes); const d = normalizeDeviceAuthScopes(s.scopes); if (u.join("\n") !== d.join("\n")) a.push({ ...n, requirement: "local-scopes-mismatch", message: `Local cached ${t} device scopes for ${i} differ from the gateway record. Cached scopes [${formatValues(u)}], gateway scopes [${formatValues(d)}]. Reconnect with shared gateway auth to refresh it, or rotate with ${c}.`, fixHint: `Reconnect with shared gateway auth to refresh it, or rotate with ${c}.` }); } return a; } async function collectLegacyPairingStoreFindings(e) { if (e.gateway?.mode === "remote") return []; const { listLegacyPairingStoreFiles: t } = await import("./pairing-files-B9Tn1QFE.mjs"); return (await t()).map((e) => ({ checkId: DEVICE_PAIRING_CHECK_ID, severity: "warning", category: "fix-now", docsUrl: "https://docs.openclaw.ai/cli/devices", message: `Legacy pairing store ${e} has not been imported into SQLite. Stop the Gateway and run openclaw doctor --fix. Unreadable sources remain in place for repair.`, path: "devices.legacy-store", requirement: "pairing-store-legacy-file", fixHint: "Stop the Gateway and run openclaw doctor --fix to import and archive the legacy pairing stores." })); } async function collectDevicePairingHealthFindings(e) { const t = await collectLegacyPairingStoreFindings(e.cfg); const { detectLegacyDeviceAuth: n } = await import("./state-migrations.device-auth-BRejJiiv.mjs"); const r = n({ stateDir: resolveStateDir(e.env) }); if (r.sourcePresent) try { await loadDeviceIdentityIfPresentAsync({ env: e.env ?? process.env }); } catch { const n = `Stop the Gateway and run ${formatCliCommand("openclaw doctor --fix", e.env)} to finish local credential migration or repair.`; t.push({ checkId: DEVICE_PAIRING_CHECK_ID, severity: "warning", category: "fix-now", docsUrl: "https://docs.openclaw.ai/cli/devices", message: `Legacy device auth store ${sanitizeTerminalText(r.sourcePath)} is still present and local credentials require migration or repair. ${n}`, path: "identity.device-auth", requirement: "device-auth-store-legacy-file", fixHint: n }); } const i = await loadDoctorPairingSnapshot({ cfg: e.cfg, healthOk: e.healthOk ?? false }); if (!i) return t; return [ ...t, ...collectPendingPairingFindings(i), ...collectPairedRecordFindings(i), ...await collectLocalDeviceAuthFindings(i) ]; } //#endregion export { collectDevicePairingHealthFindings };