UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

117 lines (116 loc) • 2.82 kB
import { n as createQueuedDiagnosticPhaseEmitter } from "./diagnostic-events-BjAH1Kbx.mjs"; import { t as createPhaseTimings } from "./phase-timings-mJ6j-Enk.mjs"; import { performance } from "node:perf_hooks"; //#region src/gateway/server-methods/chat-send-diagnostics.ts const PHASES = [ "authority", "admission", "preparation", "attachments", "replyContext", "authoring", "persist", "runAdmission", "replyInitialization", "snapshot", "worktree", "effects", "response", "dispatch" ]; const requestDiagnostics = /* @__PURE__ */ new WeakMap(); /** Internal request attachment; handler spans do not expand the public plugin options. */ function bindChatSendDiagnostics(e, t) { if (t) requestDiagnostics.set(e, t); } function readChatSendDiagnostics(e) { return requestDiagnostics.get(e); } /** Request-owned totals; nested and parallel phases can overlap. */ function startChatSendDiagnostics(e) { const t = createQueuedDiagnosticPhaseEmitter(); const n = createPhaseTimings(); const { totals: r } = n; let i = "request"; let a = performance.now(); let o; let s = false; let c; const report = () => { const s = performance.now(); n.flush(s); const l = s - a; const u = l >= 1e3; if (!u && !t) { r.clear(); a = s; return l; } const d = t ? Date.now() : 0; try { let n = u ? `slow chat send ${Math.round(l)}ms stage=${i}` : ""; if (u && c) n += ` sessionId=${c.sessionId} runId=${c.runId}`; if (u && o !== void 0) n += ` ack=${Math.round(o)}ms`; for (const e of [...PHASES, ...[...r.keys()].filter((e) => e.startsWith("detail.")).toSorted()]) { const a = r.get(e); if (a === void 0) continue; t?.({ name: `chat.send.${e}`, startedAt: d - l, endedAt: d, durationMs: a, details: { stage: i, ...c } }); if (u) n += ` ${e}=${Math.round(a)}ms`; } if (u) e.info(n); } catch {} r.clear(); a = s; return l; }; const finish = (e) => { if (!s) { if (e?.isSteered()) i = "steer"; else if (e?.isEnqueued() || e?.isTerminal()) i = "queued"; report(); s = true; n.close(); } }; const scope = (e) => s ? void 0 : n.scope(e); return { setRunIdentity(e, t) { c = { sessionId: e, runId: t }; }, scope, observeSpan: (e, t, n) => { const i = e === "agent.prepare" ? t?.stage : e; if (typeof i !== "string" || s) return; const a = `detail.${i}`; if (n !== void 0) { r.set(a, (r.get(a) ?? 0) + n); return; } return scope(a)?.finish; }, acknowledge(e = "startup") { if (!s && i === "request") { o = report(); i = e; } }, finish, [Symbol.dispose]() { if (i === "request") finish(); } }; } //#endregion export { readChatSendDiagnostics as n, startChatSendDiagnostics as r, bindChatSendDiagnostics as t };