refactor(webui): separate gateway mutations and UI state (#6727)
This commit is contained in:
1 parent
ce12bfdd0d
commit
5537bdabd3
70 files changed
+4058
-3650
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { useCallback, useRef, type ForwardedRef, type RefCallback } from "react";
|
||||
|
||||
type OverlayKind = "dialog" | "overlay";
|
||||
const surfaces = new Map<HTMLElement, OverlayKind>();
|
||||
|
||||
// Track the content node's presence, not the owner's open prop: Radix can keep
|
||||
// closed content mounted through its exit animation.
|
||||
export function useOverlaySurfaceRef<T extends HTMLElement>(
|
||||
kind: OverlayKind,
|
||||
forwardedRef?: ForwardedRef<T>,
|
||||
): RefCallback<T> {
|
||||
const current = useRef<T | null>(null);
|
||||
return useCallback((node) => {
|
||||
if (current.current) surfaces.delete(current.current);
|
||||
current.current = node;
|
||||
if (node) surfaces.set(node, kind);
|
||||
|
||||
if (typeof forwardedRef === "function") forwardedRef(node);
|
||||
else if (forwardedRef) forwardedRef.current = node;
|
||||
}, [kind, forwardedRef]);
|
||||
}
|
||||
|
||||
function hasDialogSurface(includeClosed: boolean): boolean {
|
||||
for (const [node, kind] of surfaces) {
|
||||
if (kind === "dialog" && node.isConnected && (includeClosed || node.dataset.state !== "closed")) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function hasAnyDialogMounted(): boolean {
|
||||
return hasDialogSurface(true);
|
||||
}
|
||||
|
||||
export function hasOpenDialogSurface(): boolean {
|
||||
return hasDialogSurface(false);
|
||||
}
|
||||
|
||||
export function hasAnyOverlayMounted(): boolean {
|
||||
for (const node of surfaces.keys()) {
|
||||
if (node.isConnected) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -33,29 +33,40 @@ function createDefaultSocket(url: string): WebSocket {
|
||||
return new WebSocket(url);
|
||||
}
|
||||
|
||||
const WS_DEBUG_CACHE_TTL_MS = 1_000;
|
||||
let wsDebugCache: { checkedAt: number; enabled: boolean } | null = null;
|
||||
|
||||
/** Inbound WebSocket ``console.log`` / parse-failure ``console.warn``.
|
||||
*
|
||||
* - **Dev** (non-production bundle): **on by default** — messages appear at default log level.
|
||||
* - **Production**: off unless ``localStorage.setItem('nanobot_debug_ws','1')`` (or ``true``).
|
||||
* - **Silence anywhere**: ``localStorage.setItem('nanobot_debug_ws','0')`` (or ``false`` / ``off``).
|
||||
* Values are read on every frame; no reload needed.
|
||||
* Values are cached briefly so toggling takes effect without reload while avoiding
|
||||
* synchronous ``localStorage`` reads on every streamed delta frame.
|
||||
*/
|
||||
function wsInboundDebugEnabled(): boolean {
|
||||
if (typeof globalThis === "undefined") return false;
|
||||
if (import.meta.env.MODE === "test") return false;
|
||||
const now = Date.now();
|
||||
if (wsDebugCache && now - wsDebugCache.checkedAt < WS_DEBUG_CACHE_TTL_MS) {
|
||||
return wsDebugCache.enabled;
|
||||
}
|
||||
let enabled: boolean;
|
||||
try {
|
||||
if (import.meta.env.MODE === "test") return false;
|
||||
const ls = (globalThis as unknown as { localStorage?: Storage }).localStorage;
|
||||
const raw = ls?.getItem("nanobot_debug_ws")?.trim().toLowerCase() ?? "";
|
||||
if (raw === "0" || raw === "false" || raw === "off" || raw === "no") {
|
||||
return false;
|
||||
enabled = false;
|
||||
} else if (raw === "1" || raw === "true" || raw === "on" || raw === "yes") {
|
||||
enabled = true;
|
||||
} else {
|
||||
enabled = !import.meta.env.PROD;
|
||||
}
|
||||
if (raw === "1" || raw === "true" || raw === "on" || raw === "yes") {
|
||||
return true;
|
||||
}
|
||||
return !import.meta.env.PROD;
|
||||
} catch {
|
||||
return !import.meta.env.PROD;
|
||||
enabled = !import.meta.env.PROD;
|
||||
}
|
||||
wsDebugCache = { checkedAt: now, enabled };
|
||||
return enabled;
|
||||
}
|
||||
|
||||
function summarizeInboundWsPayload(ev: InboundEvent): unknown {
|
||||
@@ -247,6 +258,7 @@ export class NanobotClient {
|
||||
/** Inbound frames received while the transcript owner is absent, regardless of observers. */
|
||||
private pendingInboundByChat = new Map<string, InboundEvent[]>();
|
||||
private static readonly PENDING_INBOUND_MAX = 2000;
|
||||
private static readonly PENDING_TEMP_INBOUND_MAX = 20_000;
|
||||
private knownChats = new Set<string>();
|
||||
/** Temporary chats are connection-owned and intentionally not reattached. */
|
||||
private temporaryChatIds = new Set<string>();
|
||||
@@ -1171,11 +1183,13 @@ export class NanobotClient {
|
||||
this.pendingInboundByChat.set(chatId, pending);
|
||||
}
|
||||
pending.push(ev);
|
||||
// Temporary chats have no canonical history to recover evicted events from.
|
||||
if (!this.temporaryChatIds.has(chatId)) {
|
||||
const over = pending.length - NanobotClient.PENDING_INBOUND_MAX;
|
||||
if (over > 0) pending.splice(0, over);
|
||||
}
|
||||
// Temporary chats have no canonical history to recover evicted events from,
|
||||
// but still keep a generous upper bound so an unmounted chat cannot OOM the tab.
|
||||
const maxPending = this.temporaryChatIds.has(chatId)
|
||||
? NanobotClient.PENDING_TEMP_INBOUND_MAX
|
||||
: NanobotClient.PENDING_INBOUND_MAX;
|
||||
const over = pending.length - maxPending;
|
||||
if (over > 0) pending.splice(0, over);
|
||||
}
|
||||
for (const handler of this.chatHandlers.get(chatId) ?? []) handler(ev);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user