UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

103 lines (102 loc) • 4.82 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs"; import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import "./error-runtime-BM740gJj.mjs"; import "./gateway-runtime-Du0olVch.mjs"; import { p as parseCrabboxProfile, r as CRABBOX_WORKER_PROVIDER_ID, v as resolveCrabboxWarmImageProfile } from "./crabbox-worker-profile-Ckeez-8x.mjs"; import { l as listCrabboxLegacyWarmLeases, p as recoverCrabboxWarmImageCapture, t as CrabboxWarmImageRequestError, u as listCrabboxWarmImages } from "./crabbox-worker-warm-image-store-gdxNPzY8.mjs"; import { a as crabboxWarmImageRecoveryHint, o as isCrabboxWarmImageHeld } from "./crabbox-worker-warm-image-records-DeuyTta3.mjs"; //#region extensions/crabbox/src/crabbox-gateway-methods.ts async function mutateCrabboxImage(e, t, n, { params: r, respond: i }) { const a = isRecord(r) ? normalizeOptionalString(r.checkpointId) : void 0; if (!a || !isRecord(r) || n === "pin" && typeof r.pinned !== "boolean" || Object.keys(r).some((e) => e !== "checkpointId" && !(n === "pin" && e === "pinned"))) { sendError(i, /* @__PURE__ */ new Error(`crabbox.images.${n} requires checkpointId${n === "pin" ? " and pinned: boolean" : ""}.`), true); return; } try { if (n === "pin") i(true, snapshotSummary(await t.pin(a, r.pinned === true))); else if (n === "rollback") i(true, snapshotSummary(await t.rollback(a))); else { const n = Object.values(e.runtime.config.current().cloudWorkers?.profiles ?? {}).filter((e) => e.provider.trim().toLowerCase() === CRABBOX_WORKER_PROVIDER_ID).map((e) => e.settings ?? {}); i(true, await t.delete(a, n)); } } catch (e) { sendError(i, e, e instanceof CrabboxWarmImageRequestError); } } function sendError(e, t, n = false) { const r = { error: formatErrorMessage(t) }; e(false, r, errorShape(n ? ErrorCodes.INVALID_REQUEST : ErrorCodes.UNAVAILABLE, r.error, { details: r })); } function profileStatus(e) { try { const t = parseCrabboxProfile(e); const n = resolveCrabboxWarmImageProfile(t); const r = n.warmImage && n.class ? "on" : "off"; const i = t.warmImage === false ? "Disabled in this profile." : n.target !== "linux" ? "Warm images require Linux." : !n.class ? "Requires a configured or placement machine class." : t.warmImage === true ? "Explicitly enabled in this profile." : "Enabled by default for Linux with a known machine class."; return { backend: t.provider, machineClass: t.class, os: t.target, warmImages: r, reason: i }; } catch (e) { return { warmImages: "off", reason: formatErrorMessage(e) }; } } function snapshotSummary(e) { const t = Object.entries(e.allocations).toSorted(([e], [t]) => e.localeCompare(t)); return { ...e, held: Boolean(e.checkpointId && isCrabboxWarmImageHeld(e, e.checkpointId)), allocationCount: t.length, allocations: Object.fromEntries(t.slice(0, 20)) }; } async function snapshotStatus(e) { return { images: (await listCrabboxWarmImages(e)).map(snapshotSummary), legacyLeases: (await listCrabboxLegacyWarmLeases(e)).map((e) => Object.assign(e, { recoveryHint: crabboxWarmImageRecoveryHint(e.selector) })) }; } async function listCrabboxImages(e, { params: t, respond: n }) { if (t !== void 0 && (!isRecord(t) || Object.keys(t).length > 0)) { sendError(n, /* @__PURE__ */ new Error("crabbox.images.list takes no parameters."), true); return; } try { const t = Object.entries(e.runtime.config.current().cloudWorkers?.profiles ?? {}).filter(([, e]) => e.provider.trim().toLowerCase() === CRABBOX_WORKER_PROVIDER_ID).toSorted(([e], [t]) => e.localeCompare(t)).map(([e, t]) => Object.assign({ id: e }, profileStatus(t.settings ?? {}))); n(true, { ...await snapshotStatus(e.runtime.state), profiles: t }); } catch (e) { sendError(n, e); } } async function recoverCrabboxImage(e, { params: t, respond: n }) { const r = isRecord(t) ? normalizeOptionalString(t.selector) : void 0; if (!r || !isRecord(t) || t.acknowledgeProviderCleanup !== true || Object.keys(t).some((e) => e !== "selector" && e !== "acknowledgeProviderCleanup")) { sendError(n, /* @__PURE__ */ new Error("Recovery requires a selector and acknowledgeProviderCleanup: true."), true); return; } try { await recoverCrabboxWarmImageCapture(e, r, true); n(true, { ...await snapshotStatus(e), recoveredCapture: r, nextSteps: "Restart the Gateway after manual reconciliation; the next eligible worker can capture again." }); } catch (e) { sendError(n, e); } } //#endregion export { listCrabboxImages, mutateCrabboxImage, recoverCrabboxImage };