openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
162 lines (161 loc) • 6.93 kB
JavaScript
import { s as getTrackedWorkerPoolSnapshot } from "./worker-cpu-dNstslyu.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as closedObject } from "./closed-object-B-B1FwhS.mjs";
import { Qt as isDiagnosticsHeapSnapshotParams, Zt as isDiagnosticsHeapProfileParams, t as createProtocolValidator } from "./protocol-validator-eSFGFzTX.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { n as SUBAGENT_LANE_PREFIX, r as SWARM_LANE_PREFIX, t as STATIC_COMMAND_LANES } from "./lanes-BXfh4YnJ.mjs";
import { p as getQueueState, r as getCommandLaneSnapshot, s as listCommandLaneTotals } from "./command-queue-BenRlAaO.mjs";
import { s as normalizeDiagnosticStabilityQuery, t as getDiagnosticStabilitySnapshot } from "./diagnostic-stability-Cu_EL85T.mjs";
import { t as respondError } from "./response-C7fEiIN1.mjs";
import { r as isBackgroundWorkLane, t as getBackgroundWorkSnapshot } from "./background-work-CoZV6wmM.mjs";
import { t as collectRuntimeCapabilities } from "./runtime-capabilities-DcqfMou5.mjs";
import { Type } from "typebox";
//#region packages/gateway-protocol/src/schema/diagnostics-schemas.ts
/** Integer inputs are clamped by the capture owner to safe sampling bounds. */
const DiagnosticsHeapProfileParamsSchema = closedObject({
durationMs: Type.Optional(Type.Integer({
minimum: 1,
maximum: Number.MAX_SAFE_INTEGER
})),
samplingIntervalBytes: Type.Optional(Type.Integer({
minimum: 1,
maximum: Number.MAX_SAFE_INTEGER
})),
includeCollected: Type.Optional(Type.Boolean()),
includeObjectsCollectedByMajorGC: Type.Optional(Type.Boolean()),
includeObjectsCollectedByMinorGC: Type.Optional(Type.Boolean())
});
const DiagnosticsHeapSnapshotParamsSchema = closedObject({ reason: Type.Optional(Type.String({ maxLength: 256 })) });
//#endregion
//#region packages/gateway-protocol/src/schema/diagnostics.ts
const validateDiagnosticsHeapProfileParams = /* @__PURE__ */ createProtocolValidator(DiagnosticsHeapProfileParamsSchema, isDiagnosticsHeapProfileParams);
const validateDiagnosticsHeapSnapshotParams = /* @__PURE__ */ createProtocolValidator(DiagnosticsHeapSnapshotParamsSchema, isDiagnosticsHeapSnapshotParams);
//#endregion
//#region src/process/command-lane-diagnostics.ts
const STATIC_COMMAND_LANE_SET = new Set(STATIC_COMMAND_LANES);
const MAX_SWARM_LANE_SNAPSHOTS = 32;
function getSubagentLaneSnapshot() {
const e = {
...getCommandLaneSnapshot("subagent"),
concurrencyScope: "session",
saturatedLaneCount: 0
};
for (const t of getQueueState().lanes.values()) {
if (!t.lane.startsWith("subagent:") || t.lane.startsWith("subagent:swarm:")) continue;
const n = t.activeTasks.size;
e.activeCount += n;
e.queuedCount += t.queue.length;
e.draining ||= t.draining;
e.generation = Math.max(e.generation, t.generation);
if (n >= t.maxConcurrent) {
e.saturatedLaneCount = (e.saturatedLaneCount ?? 0) + 1;
e.blockedBy = "lane";
}
}
return e;
}
function getCommandLaneDiagnostics() {
const e = STATIC_COMMAND_LANES.toSorted().map((e) => {
if (e === "background") return getBackgroundWorkSnapshot();
if (e === "subagent") return getSubagentLaneSnapshot();
return getCommandLaneSnapshot(e);
}).filter((e) => e.maxConcurrent > 0 || e.activeCount > 0 || e.queuedCount > 0);
const t = {
laneCount: 0,
activeCount: 0,
queuedCount: 0,
queuedLaneCount: 0
};
let n = 0;
for (const r of listCommandLaneTotals()) {
if (r.lane.startsWith("subagent:swarm:")) {
if (n < MAX_SWARM_LANE_SNAPSHOTS) {
e.push(getCommandLaneSnapshot(r.lane));
n += 1;
continue;
}
} else if (STATIC_COMMAND_LANE_SET.has(r.lane) || isBackgroundWorkLane(r.lane) || r.lane.startsWith("subagent:")) continue;
t.laneCount += 1;
t.activeCount += r.activeCount;
t.queuedCount += r.queuedCount;
if (r.queuedCount > 0) t.queuedLaneCount += 1;
}
return {
lanes: e,
dynamic: t.laneCount > 0 ? t : null
};
}
//#endregion
//#region src/gateway/server-methods/diagnostics.ts
function profileHandler(e, t, n, r) {
return async ({ req: i, client: a, signal: o, context: s, respond: c, hasCurrentClientAuthority: l }) => {
const u = i.params === void 0 ? {} : i.params;
if (!t(u)) {
respondError(c, ErrorCodes.INVALID_REQUEST, n);
return;
}
const d = AbortSignal.any([
o,
a?.connectionSignal,
s.requestEntryLifetime?.signal
].filter((e) => e !== void 0));
const hasAuthority = () => !a?.invalidated && (l?.() ?? true);
const f = await r(u, {
signal: d,
hasAuthority
});
if (d.aborted || !hasAuthority()) return;
if (f.status === "complete") c(true, f.result, void 0);
else {
const t = f.reason === "tracing-active" ? `${e} unavailable: stop active Node tracing, including non-CPU categories, before requesting a profile` : `${e} unavailable: ${f.reason}`;
respondError(c, ErrorCodes.UNAVAILABLE, t, { details: {
reason: f.reason,
cleanupFailed: f.cleanupFailed
} });
}
};
}
const diagnosticsHandlers = {
"diagnostics.capabilities": ({ params: e, respond: t }) => {
if (e !== void 0 && (!isRecord(e) || Object.keys(e).length !== 0)) {
respondError(t, ErrorCodes.INVALID_REQUEST, "diagnostics.capabilities accepts only empty params");
return;
}
t(true, collectRuntimeCapabilities(), void 0);
},
"diagnostics.cpuProfile": profileHandler("CPU profile", (e) => isRecord(e) && Object.keys(e).length === 0, "diagnostics.cpuProfile accepts only empty params", async (e, t) => {
const { captureDiagnosticCpuProfile: n } = await import("./diagnostic-cpu-profile-DmLRqTOe.mjs");
return n(t);
}),
"diagnostics.heapProfile": profileHandler("Heap profile", validateDiagnosticsHeapProfileParams, "diagnostics.heapProfile accepts only positive integer durationMs and samplingIntervalBytes, and boolean includeCollected, includeObjectsCollectedByMajorGC and includeObjectsCollectedByMinorGC", async (e, t) => {
const { captureDiagnosticHeapProfile: n } = await import("./diagnostic-heap-profile-DpzpqhSP.mjs");
return n({
...e,
...t
});
}),
"diagnostics.heapSnapshot": profileHandler("Heap snapshot", validateDiagnosticsHeapSnapshotParams, "diagnostics.heapSnapshot accepts only an optional reason string (at most 256 characters)", async (e, t) => {
const { captureDiagnosticHeapSnapshot: n } = await import("./diagnostic-heap-snapshot-DWtS6Zi-.mjs");
return n({
...e,
...t
});
}),
"diagnostics.lanes": ({ respond: e }) => {
e(true, {
ts: Date.now(),
...getCommandLaneDiagnostics(),
...getTrackedWorkerPoolSnapshot()
}, void 0);
},
"diagnostics.stability": async ({ params: e, respond: t }) => {
try {
const n = normalizeDiagnosticStabilityQuery(e);
t(true, getDiagnosticStabilitySnapshot(n), void 0);
} catch (e) {
respondError(t, ErrorCodes.INVALID_REQUEST, e instanceof Error ? e.message : "invalid diagnostics.stability params");
}
}
};
//#endregion
export { diagnosticsHandlers };