openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
108 lines (107 loc) • 3.93 kB
JavaScript
import { n as createQueuedDiagnosticPhaseEmitter } from "./diagnostic-events-BjAH1Kbx.mjs";
import { m as withDiagnosticsTimelineObserver } from "./diagnostics-timeline-CSwiG24r.mjs";
import { o as isNativeAppUiClient } from "./client-info-Cm1BcrMm.mjs";
import { s as isOperatorUiClient } from "./message-channel-DTYVLJT1.mjs";
import { performance } from "node:perf_hooks";
//#region src/gateway/server-methods/chat-server-timing.ts
function roundedChatSendTimingMs(e) {
return Math.max(0, Math.round(e * 1e3) / 1e3);
}
function prepareChatSendAckTiming({ client: e, request: { clientInfo: t, chatSendReceivedAtMs: n }, session: { sessionLoadMs: r }, prepareAttachmentsMs: i, chatSendTraceAttributes: a }) {
const o = isOperatorUiClient(t) ? {
receivedToAckMs: roundedChatSendTimingMs(performance.now() - n),
loadSessionMs: r,
...i !== void 0 ? { prepareAttachmentsMs: i } : {}
} : void 0;
return {
serverTiming: o,
chatSendTiming: o && typeof e?.connId === "string" && e.connId.trim() ? {
ackedAtMs: performance.now(),
connId: e.connId.trim(),
receivedAtMs: n
} : void 0,
ackReadyEvent: (e) => ({
type: "mark",
name: "gateway.chat_send.ack_ready",
phase: "agent-turn",
attributes: {
...a,
ackStatus: e,
...o ? {
serverReceivedToAckMs: o.receivedToAckMs,
serverLoadSessionMs: o.loadSessionMs,
...o.prepareAttachmentsMs !== void 0 ? { serverPrepareAttachmentsMs: o.prepareAttachmentsMs } : {}
} : {}
}
})
};
}
const CONTROL_UI_RECONNECT_RESUME_PARAM = "__controlUiReconnectResume";
function resolveControlUiReconnectResumeParams(e, t) {
if (!e || typeof e !== "object" || Array.isArray(e)) return {
params: e,
resumeRequested: false
};
const n = e;
if (!(n[CONTROL_UI_RECONNECT_RESUME_PARAM] === true && (isOperatorUiClient(t) || isNativeAppUiClient(t)))) return {
params: e,
resumeRequested: false
};
const r = { ...n };
delete r[CONTROL_UI_RECONNECT_RESUME_PARAM];
return {
params: r,
resumeRequested: true
};
}
function createOperatorChatSendServerTiming({ context: e, client: t, request: { chatSendReceivedAtMs: n }, session: { clientRunId: r, sessionKey: i, agentId: a }, timing: { chatSendAckedAtMs: o, chatSendTiming: s } }) {
const c = performance.now();
if (s) s.dispatchStartedAtMs = c;
const l = t?.connId?.trim();
const u = l && isOperatorUiClient(t?.connect?.client) ? /* @__PURE__ */ new Set([l]) : void 0;
const d = s ?? { firstAssistantEventSent: false };
const emit = (t, s, c) => {
if (!u) return;
const l = performance.now();
e.broadcastToConnIds("chat.send_timing", {
phase: t,
runId: r,
sessionKey: i,
...a ? { agentId: a } : {},
ackToPhaseMs: roundedChatSendTimingMs(l - o),
receivedToPhaseMs: roundedChatSendTimingMs(l - n),
...c !== void 0 ? { dispatchStartedToPhaseMs: roundedChatSendTimingMs(l - c) } : {},
...s
}, u, { dropIfSlow: true });
};
return {
emit,
dispatchStartedAtMs: c,
emitFirstAssistant: () => {
if (d.firstAssistantEventSent) return;
d.firstAssistantEventSent = true;
emit("first-assistant-event", void 0, c);
}
};
}
const CHAT_STARTUP_PHASE = /^gateway\.chat\.startup\.(session_entry|pending_inputs|history_page|startup_projection|projection_final|session_info)$/;
function withChatStartupDiagnostics(e) {
return (t) => {
const n = createQueuedDiagnosticPhaseEmitter();
if (!n) return e(t);
return withDiagnosticsTimelineObserver((e) => {
const t = CHAT_STARTUP_PHASE.exec(e)?.[1];
if (!t) return;
const r = Date.now();
const i = performance.now();
return () => n({
name: `chat.startup.${t}`,
startedAt: r,
endedAt: Date.now(),
durationMs: performance.now() - i
});
}, () => e(t));
};
}
//#endregion
export { withChatStartupDiagnostics as a, roundedChatSendTimingMs as i, prepareChatSendAckTiming as n, resolveControlUiReconnectResumeParams as r, createOperatorChatSendServerTiming as t };