UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

269 lines (268 loc) • 11.2 kB
import { l as normalizeOptionalString } from "../../string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord } from "../../record-coerce-CVPuDqcc.mjs"; import { t as findCrabboxBinary } from "../../crabbox-binary-B74dQeJ2.mjs"; import "../../string-coerce-runtime-BwAZZDf5.mjs"; import { i as findManagedCrabboxBinary, o as probeCrabboxVersion, r as ensureManagedCrabboxBinary, s as resolveManagedCrabboxBinaryPath, t as CRABBOX_MIN_VERSION } from "../../crabbox-managed-binary-Bk-CKD5f.mjs"; import { b as resolveOpenClawRoot, m as parseCrabboxSetupSecrets, r as CRABBOX_WORKER_PROVIDER_ID } from "../../crabbox-worker-profile-Ckeez-8x.mjs"; import { f as projectCrabboxWarmImage, n as admitCrabboxWarmImageProfile } from "../../crabbox-worker-warm-image-store-gdxNPzY8.mjs"; import { a as crabboxWarmImageRecoveryHint, i as crabboxWarmImageHoldingLeaseIds, t as CRABBOX_WARM_IMAGE_WAIT_HINT } from "../../crabbox-worker-warm-image-records-DeuyTta3.mjs"; import { fileURLToPath } from "node:url"; import path from "node:path"; //#region extensions/crabbox/src/doctor.ts const CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID = "crabbox/cloud-worker-profiles"; const CRABBOX_SETUP_SECRETS_CHECK_ID = "crabbox/setup-secrets"; const CRABBOX_WARM_IMAGES_CHECK_ID = "crabbox/warm-images"; function createCrabboxCloudWorkerProfileCheck(e) { return { id: CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID, kind: "plugin", description: "Verify configured Crabbox cloud worker and sandbox binaries before use.", source: "crabbox", async detect(t) { const n = Object.entries(t.cfg.cloudWorkers?.profiles ?? {}).filter(([, e]) => e.provider.trim().toLowerCase() === CRABBOX_WORKER_PROVIDER_ID).map(([e, t]) => ({ id: e, label: `Cloud worker profile "${e}"`, settings: asOptionalRecord(t.settings), ocPath: `cloudWorkers.profiles.${e}.settings.binary` })); const r = asOptionalRecord(asOptionalRecord(t.cfg.plugins?.entries?.crabbox?.config)?.sandbox); if (r) n.push({ id: "sandbox", label: "Crabbox sandbox", settings: r, ocPath: "plugins.entries.crabbox.config.sandbox.binary" }); if (n.length === 0) return []; const i = /* @__PURE__ */ new Map(); const probe = (e) => { let t = i.get(e); if (!t) { t = probeCrabboxVersion(e); i.set(e, t); } return t; }; let a; const o = []; for (const r of n) { const n = normalizeOptionalString(r.settings?.binary); const i = findCrabboxBinary({ ...n ? { explicit: n } : {}, openclawRoot: e, pathEnv: t.env?.PATH ?? process.env.PATH }); const s = i ? await probe(i) : void 0; if (s?.status === "supported") continue; try { a ??= findManagedCrabboxBinary({ env: t.env }); if (await a) continue; } catch (e) { o.push({ checkId: CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID, severity: "warning", source: "crabbox", target: r.id, message: e instanceof Error ? e.message : "Crabbox host is unsupported" }); continue; } const c = !s ? "has no executable Crabbox binary" : s.status === "outdated" ? `uses outdated Crabbox ${s.version}` : `could not determine its Crabbox version: ${s.reason}`; o.push({ checkId: CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID, severity: "warning", source: "crabbox", message: `${r.label} ${c}. OpenClaw will install its managed Crabbox before use.`, ...i ?? n ? { path: i ?? n } : {}, ocPath: r.ocPath, target: r.id, requirement: `Crabbox ${CRABBOX_MIN_VERSION} or newer`, fixHint: `Run \`openclaw doctor --fix\` to install the managed Crabbox now, or provision Crabbox ${CRABBOX_MIN_VERSION} or newer using \`${r.ocPath}\`. The existing executable and configuration are preserved.` }); } return o; }, async repair(e, t) { if (t.length === 0 || e.dryRun) return { status: "skipped", changes: [] }; try { const { binary: t } = await ensureManagedCrabboxBinary({ binary: resolveManagedCrabboxBinaryPath(e.env), env: e.env }); return { status: "repaired", changes: [`Installed managed Crabbox at ${t}`], effects: [{ kind: "package", action: "install", target: t }] }; } catch (e) { return { status: "failed", changes: [], warnings: [e instanceof Error ? e.message : "Managed Crabbox installation failed"] }; } } }; } function createCrabboxSetupSecretsCheck(e) { const t = "Create or repair protected entries with explicit allowed hosts using openclaw secrets, then run openclaw doctor --fix. Store values and destination bindings are never inferred or overwritten."; const inspect = async (n) => { const r = []; const i = []; if (!Object.values(n.cfg.cloudWorkers?.profiles ?? {}).some((e) => e.provider.trim().toLowerCase() === "crabbox" && asOptionalRecord(e.settings)?.setupEnv !== void 0)) return { config: n.cfg, changes: r, findings: i }; const a = structuredClone(n.cfg); for (const [o, s] of Object.entries(a.cloudWorkers?.profiles ?? {})) { const a = asOptionalRecord(s.settings); if (s.provider.trim().toLowerCase() !== "crabbox" || !a || a.setupEnv === void 0) continue; const c = `cloudWorkers.profiles.${o}.settings.setupEnv`; let l; const u = a.setupEnv; if (!Array.isArray(u) || u.length > 16 || !u.every((e) => typeof e === "string" && /^[A-Za-z_][A-Za-z0-9_]*$/u.test(e)) || new Set(u).size !== u.length) l = "legacy setupEnv is not a bounded list of distinct environment names"; else { const t = u.length ? await e.inspectSetupSecrets(u, n.env) : []; const i = t.filter(({ status: e }) => e !== "ready"); if (t.length !== u.length || i.length) l = i.length ? i.map(({ name: e, status: t }) => `${e}: ${t}`).join(", ") : "secret-store inspection is incomplete"; else try { const e = parseCrabboxSetupSecrets(a.setupSecrets) ?? {}; const t = n.cfg.secrets?.defaults?.store ?? "default"; const i = { ...e }; for (const n of u) { const r = e[n]; if (r && (r.provider !== t || r.id !== n)) throw new Error(`setupSecrets mapping ${n} conflicts with its same-name store entry`); i[n] = { source: "store", provider: t, id: n }; } a.setupSecrets = parseCrabboxSetupSecrets(i); delete a.setupEnv; r.push(`Migrated ${c} to setupSecrets using existing protected, destination-bound store entries.`); } catch (e) { l = e instanceof Error ? e.message : "setupSecrets mapping requires repair"; } } i.push({ checkId: CRABBOX_SETUP_SECRETS_CHECK_ID, severity: "warning", source: "crabbox", target: o, ocPath: c, message: `Crabbox profile ${o} uses retired plaintext setupEnv${l ? ` and needs repair (${l})` : "; protected store entries are ready to migrate"}.`, fixHint: t }); } return { config: a, changes: r, findings: i }; }; return { id: CRABBOX_SETUP_SECRETS_CHECK_ID, kind: "plugin", description: "Migrate retired setup environment forwarding to protected setup secrets.", source: "crabbox", async detect(e) { return (await inspect(e)).findings; }, async repair(e) { const n = await inspect(e); if (e.dryRun) return { status: "skipped", changes: [], reason: "Run openclaw doctor --fix to persist setup secret migration." }; return { status: n.changes.length ? "repaired" : "skipped", config: n.changes.length ? n.config : e.cfg, changes: n.changes, warnings: n.findings.filter(({ target: e }) => { return asOptionalRecord(n.config.cloudWorkers?.profiles?.[e ?? ""]?.settings)?.setupEnv !== void 0; }).map(({ message: e }) => `${e} ${t}`) }; } }; } function registerCrabboxWorkerProviderDoctorChecks(e) { if (!e.getHealthCheck(CRABBOX_SETUP_SECRETS_CHECK_ID)) e.registerHealthCheck(createCrabboxSetupSecretsCheck(e)); if (!e.getHealthCheck("crabbox/cloud-worker-profiles")) e.registerHealthCheck(createCrabboxCloudWorkerProfileCheck(e.openclawRoot)); if (!e.getHealthCheck(CRABBOX_WARM_IMAGES_CHECK_ID)) e.registerHealthCheck({ id: CRABBOX_WARM_IMAGES_CHECK_ID, kind: "plugin", description: "Report paused Crabbox warm-image captures and retained cleanup obligations.", source: "crabbox", async detect(t) { const n = []; const r = await e.listPluginStateEntries({ namespace: "warm-images", maxEntries: 128, overflowPolicy: "reject-new", ...t.env ? { env: t.env } : {} }); for (const { key: e, value: t } of r) { const r = projectCrabboxWarmImage(e, admitCrabboxWarmImageProfile(t)); const i = [ r.profileId, r.backend, r.machineClass, r.os, r.projectLabel ].filter(Boolean); const a = i.length ? ` (${i.join(" · ")})` : ""; const o = { checkId: CRABBOX_WARM_IMAGES_CHECK_ID, severity: "warning", source: "crabbox", target: r.profileKey }; if (r.captureUnsupported) n.push({ ...o, severity: "info", message: `Warm-image native capture${a} is unsupported: ${r.captureUnsupported.message}`, fixHint: "Workers use an existing compatible snapshot when one is available and otherwise provision cold; capture attempts are skipped until `warmImages.refreshAfter` has elapsed since the refusal. Set `settings.warmImage: false` on the profile to stop capture attempts, or use a Crabbox configuration that supports native checkpoints." }); if (r.capture) { const e = r.capture.phase === "uncertain"; n.push({ ...o, severity: e || r.capture.stale ? "warning" : "info", message: e ? `Warm-image capture ${r.capture.selector}${a} is paused; its provider outcome requires manual reconciliation.` : r.capture.stale ? `Warm-image capture ${r.capture.selector}${a} is taking longer than usual.` : `Warm-image capture ${r.capture.selector}${a} is in progress.`, fixHint: e ? crabboxWarmImageRecoveryHint(r.capture.selector, r.capture.checkpointIds) : CRABBOX_WARM_IMAGE_WAIT_HINT }); } if (r.retirement) { const e = crabboxWarmImageHoldingLeaseIds(r, r.retirement.checkpointId); const t = e.length > 0 ? ` Held by allocation leases: ${e.join(", ").slice(0, 1024)}.` : ""; n.push({ ...o, message: `Warm-image checkpoint ${r.retirement.checkpointId}${a} is still awaiting deletion.${t}`, fixHint: "Cleanup retries during periodic maintenance or the next warm-image capture or worker teardown. Confirmed-gone allocation leases are released automatically; live or unconfirmed leases keep their holds. Inspect `openclaw crabbox warm-images --json` and the Gateway log for holding leases or provider deletion errors if it remains pending." }); } } return n; } }); } //#endregion //#region extensions/crabbox/doctor-health-api.ts const CRABBOX_PLUGIN_ROOT = path.dirname(fileURLToPath(import.meta.url)); function registerWorkerProviderDoctorChecks(e) { registerCrabboxWorkerProviderDoctorChecks({ ...e, openclawRoot: resolveOpenClawRoot(CRABBOX_PLUGIN_ROOT) }); } //#endregion export { CRABBOX_CLOUD_WORKER_PROFILE_CHECK_ID, registerWorkerProviderDoctorChecks };