UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

88 lines (87 loc) • 3.87 kB
import { n as parseNodeOptionsEnvVar } from "./node-runtime-env-ncloLK-i.mjs"; import { a as hasGatewayServiceEnvironmentOverride, s as resolveManagedGatewayServiceCommand } from "./service-types-DdxELPHF.mjs"; import { c as resolveServiceEntrypointIndex } from "./service-layout-C5OySMEw.mjs"; import os from "node:os"; //#region src/daemon/gateway-heap.ts const MEBIBYTE_BYTES = 1048576; const GATEWAY_HEAP_FLOOR_MIB = 2048; const GATEWAY_HEAP_CAP_MIB = 8192; function readAvailableMemory() { const e = process.constrainedMemory(); const t = os.totalmem(); const n = Number.isSafeInteger(t) && t > 0; if (Number.isSafeInteger(e) && e > 0 && (!n || e <= t)) return { bytes: e, source: "constrained" }; return { bytes: n ? t : null, source: n ? "physical" : "unknown" }; } function resolveGatewayHeapLimit() { const e = readAvailableMemory(); if (e.bytes === null || e.bytes < 2 * MEBIBYTE_BYTES) return { maxOldSpaceSizeMiB: null, availableMemoryMiB: null, memorySource: "unknown", floorMiB: GATEWAY_HEAP_FLOOR_MIB, capMiB: GATEWAY_HEAP_CAP_MIB, headroomCapMiB: null }; const t = Math.floor(e.bytes / MEBIBYTE_BYTES); const n = Math.floor(t / 2); const r = Math.max(GATEWAY_HEAP_CAP_MIB, Math.floor(t / 4)); const i = Math.floor(t * .75); return { maxOldSpaceSizeMiB: Math.min(r, Math.max(GATEWAY_HEAP_FLOOR_MIB, n), i), availableMemoryMiB: t, memorySource: e.source, floorMiB: GATEWAY_HEAP_FLOOR_MIB, capMiB: r, headroomCapMiB: i }; } function parseHeapControls(e) { const t = /* @__PURE__ */ new Map(); for (let n = 0; n < e.length; n += 1) { const r = e[n] ?? ""; if (r === "--") break; const i = /^(--max(?:[-_]old[-_]space[-_]size(?:[-_]percentage)?|[-_]heap[-_]size))(?:=(.*))?$/u.exec(r); if (!i) continue; const a = (i[1] ?? "").replaceAll("_", "-"); const o = i[2] ?? e[++n] ?? ""; const s = Number(o); if (a.endsWith("-percentage") ? Number.isFinite(s) && s > 0 && s <= 100 : /^\+?\d+$/u.test(o) && Number.isSafeInteger(s) && s >= 0) t.set(a, s); } return Array.from(t, ([e, t]) => `${e}=${t}`); } function resolveGatewayHeapNodeOptions(e, t = "node") { const n = parseHeapControls(parseNodeOptionsEnvVar(e) ?? []).join(" "); if (n || t !== "bun") return n; const r = resolveGatewayHeapLimit().maxOldSpaceSizeMiB; return r === null ? "" : `--max-old-space-size=${Math.min(GATEWAY_HEAP_CAP_MIB, r)}`; } function readServiceHeapExecArgv(e) { const t = resolveServiceEntrypointIndex(e); return parseHeapControls(e.slice(1, t ?? 1)); } function resolveGatewayHeapExecArgv(e) { const t = resolveManagedGatewayServiceCommand(e); const n = readServiceHeapExecArgv(t?.programArguments ?? []); if (n.length || resolveGatewayHeapNodeOptions(t?.environment?.NODE_OPTIONS) || hasGatewayServiceEnvironmentOverride(e, ["NODE_OPTIONS"])) return n; const r = resolveGatewayHeapLimit().maxOldSpaceSizeMiB; return r === null ? [] : [`--max-old-space-size=${r}`]; } function inspectGatewayHeapLimit(e, t = []) { return { ...resolveGatewayHeapLimit(), nodeOptions: resolveGatewayHeapNodeOptions(e), execArgv: readServiceHeapExecArgv(t) }; } function formatGatewayHeapLimitReport(e) { return `${[e.nodeOptions ? `service NODE_OPTIONS: ${e.nodeOptions}` : "", e.execArgv.length ? `service argv: ${e.execArgv.join(" ")}` : ""].filter(Boolean).join("; ") || "no service heap control"}; installer recommendation: ${e.maxOldSpaceSizeMiB === null ? "unavailable (unknown capacity; use Node default)" : `${e.maxOldSpaceSizeMiB} MiB old space (${e.availableMemoryMiB} MiB ${e.memorySource} capacity; adaptive cap ${e.capMiB} MiB; native headroom cap ${e.headroomCapMiB} MiB)`}; runtime V8 ceiling: not measured`; } //#endregion export { resolveGatewayHeapNodeOptions as a, resolveGatewayHeapExecArgv as i, inspectGatewayHeapLimit as n, readServiceHeapExecArgv as r, formatGatewayHeapLimitReport as t };