Files
OmniRoute/open-sse/handlers/chatCore/telemetryHelpers.ts
Septianata Rizky Pratama 45310f202d fix: auto-start WS server in-process and change default port to 20132 (#6072)
* feat: change default LIVE_WS_PORT from 20129 to 20132

Update the default WebSocket port for the live dashboard server from 20129 to 20132 across all configuration files, documentation, code comments, and tests. Also consolidate OMNIROUTE_DISABLE_LIVE_WS and OMNIROUTE_ENABLE_LIVE_WS into a single OMNIROUTE_ENABLE_LIVE_WS flag. Wire the live WebSocket server to start in-process via instrumentation-node.ts.

* feat: clarify NEXT_PUBLIC_LIVE_WS_PUBLIC_URL path usage and derive upgrade path from URL

Update .env.example and ENVIRONMENT.md to document that the pathname portion of NEXT_PUBLIC_LIVE_WS_PUBLIC_URL (e.g. /live-ws) is used as the WebSocket upgrade path by the dev proxy, handshake response, and client connection logic.

Extract deriveLiveWsPath() into shared/utils/wsPath.ts and wire it through:
- src/app/api/v1/ws/route.ts — handshake response path field
- src/hooks/useLiveDashboard.ts — build

* fix: use the standard URL API to safely parse and update the effectiveWsUrl

* build(docker): expose live WebSocket server port and configure CORS origins

Add LIVE_WS_PORT (20132), LIVE_WS_HOST (0.0.0.0), and LIVE_WS_ALLOWED_ORIGINS environment variables to all Docker Compose profiles and expose the WebSocket port mapping. Prevent infinite self-loop in standalone-server-ws.mjs by skipping proxy when the server itself is running on the LiveWS port.

* docs(env): fix comment formatting for HOST and HOSTNAME variables

---------

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-07-11 01:01:27 -03:00

79 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { fetchLiveProviderLimits } from "@/lib/usage/providerLimits";
import { isClaudeExtraUsageBlockEnabled } from "@/lib/providers/claudeExtraUsage";
// #4604 — Lazy backoff for the best-effort live-WS sidecar bridge. In single-port
// deployments the sidecar (port 20132) is not running, so every compression event
// POST failed with ECONNREFUSED; because the global fetch is proxyFetch, each
// failure logged a "[ProxyFetch] Undici dispatcher failed" warning (272× in 42min).
// After a few consecutive failures we stop attempting for a cooldown window (then
// probe once), mirroring the project's lazy circuit-breaker recovery. A success
// clears the backoff so a sidecar that comes up later is picked back up.
const LIVE_WS_MAX_CONSECUTIVE_FAILURES = 3;
const LIVE_WS_DISABLE_MS = 60_000;
let liveWsConsecutiveFailures = 0;
let liveWsDisabledUntil = 0;
/** Test-only: reset the live-WS forwarding backoff state. */
export function __resetLiveWsForwardingState(): void {
liveWsConsecutiveFailures = 0;
liveWsDisabledUntil = 0;
}
export async function forwardDashboardEventToLiveWs(
event: string,
payload: unknown,
fetchImpl: typeof fetch = fetch,
now: () => number = Date.now
): Promise<void> {
// Skip while the bridge is in a cooldown window after repeated failures.
if (liveWsDisabledUntil > now()) return;
const port = process.env.LIVE_WS_PORT || "20132";
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 1_500);
try {
await fetchImpl(`http://127.0.0.1:${port}/__omniroute_event`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ event, payload, timestamp: now() }),
signal: controller.signal,
});
// Success → the sidecar is reachable; clear any accumulated backoff.
liveWsConsecutiveFailures = 0;
liveWsDisabledUntil = 0;
} catch {
// Best-effort sidecar bridge; do not affect the chat hot path. Trip the
// cooldown once failures pile up so a missing sidecar stops spamming logs.
liveWsConsecutiveFailures += 1;
if (liveWsConsecutiveFailures >= LIVE_WS_MAX_CONSECUTIVE_FAILURES) {
liveWsDisabledUntil = now() + LIVE_WS_DISABLE_MS;
liveWsConsecutiveFailures = 0;
}
} finally {
clearTimeout(timeout);
}
}
export async function maybeSyncClaudeExtraUsageState({
provider,
connectionId,
providerSpecificData,
log,
}: {
provider: string | null | undefined;
connectionId: string | null | undefined;
providerSpecificData: unknown;
log?: { debug?: (...args: unknown[]) => void; warn?: (...args: unknown[]) => void } | null;
}) {
if (!connectionId || !isClaudeExtraUsageBlockEnabled(provider, providerSpecificData)) {
return;
}
try {
await fetchLiveProviderLimits(connectionId);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
log?.debug?.("CLAUDE_USAGE", `Failed to sync Claude extra-usage state: ${message}`);
}
}