openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
78 lines (77 loc) • 2.8 kB
JavaScript
import { n as emitDiagnosticEvent, t as areDiagnosticsEnabledForProcess } from "./diagnostic-events-Cwe92uV3.js";
import { performance } from "node:perf_hooks";
//#region src/logging/diagnostic-phase.ts
const RECENT_PHASE_CAPACITY = 40;
let activePhaseStack = [];
let recentPhases = [];
function roundMetric(value, digits = 1) {
if (!Number.isFinite(value)) return 0;
const factor = 10 ** digits;
return Math.round(value * factor) / factor;
}
function pushRecentPhase(snapshot) {
recentPhases.push(snapshot);
if (recentPhases.length > RECENT_PHASE_CAPACITY) recentPhases = recentPhases.slice(-40);
}
function getCurrentDiagnosticPhase() {
return activePhaseStack.at(-1)?.name;
}
function resolveRecentPhaseLimit(limit) {
if (!Number.isFinite(limit) || limit <= 0) return null;
return Math.floor(limit);
}
function getRecentDiagnosticPhases(limit = 8, options) {
const resolved = resolveRecentPhaseLimit(limit);
if (resolved === null) return [];
const completedAfter = options?.completedAfter;
return (completedAfter === void 0 ? recentPhases : recentPhases.filter((phase) => phase.endedAt !== void 0 && phase.endedAt >= completedAfter)).slice(-resolved).map((phase) => Object.assign({}, phase));
}
/** Records a completed phase in memory and emits it when diagnostics are enabled. */
function recordDiagnosticPhase(snapshot) {
pushRecentPhase(snapshot);
if (!areDiagnosticsEnabledForProcess()) return;
emitDiagnosticEvent({
type: "diagnostic.phase.completed",
...snapshot
});
}
/** Runs work inside a measured diagnostic phase with wall-clock and CPU metrics. */
async function withDiagnosticPhase(name, run, details) {
const active = {
name,
startedAt: Date.now(),
startedWallMs: performance.now(),
cpuStarted: process.cpuUsage(),
details
};
activePhaseStack.push(active);
try {
return await run();
} finally {
const endedAt = Date.now();
const durationMs = roundMetric(performance.now() - active.startedWallMs, 1);
const cpu = process.cpuUsage(active.cpuStarted);
const cpuUserMs = roundMetric(cpu.user / 1e3, 1);
const cpuSystemMs = roundMetric(cpu.system / 1e3, 1);
const cpuTotalMs = roundMetric(cpuUserMs + cpuSystemMs, 1);
activePhaseStack = activePhaseStack.filter((entry) => entry !== active);
recordDiagnosticPhase({
name,
startedAt: active.startedAt,
endedAt,
durationMs,
cpuUserMs,
cpuSystemMs,
cpuTotalMs,
cpuCoreRatio: roundMetric(cpuTotalMs / Math.max(1, durationMs), 3),
details: active.details
});
}
}
/** Clears phase history and active stack for isolated tests. */
function resetDiagnosticPhasesForTest() {
activePhaseStack = [];
recentPhases = [];
}
//#endregion
export { withDiagnosticPhase as i, getRecentDiagnosticPhases as n, resetDiagnosticPhasesForTest as r, getCurrentDiagnosticPhase as t };