Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (round 20): array-join SSE deltas server-side, rAF-batch on client
Adam Eivyeb5a2a45690d
diff --git a/client/src/pages/Ask.jsx b/client/src/pages/Ask.jsxindex e32277d2dd..17821bc5a3 100644--- a/client/src/pages/Ask.jsx+++ b/client/src/pages/Ask.jsx@@ -288,7 +288,17 @@ export default function Ask() {const persistedConvId = activeConv?.id && CONV_ID_RE.test(activeConv.id) ? activeConv.id : undefined;let serverConvId = persistedConvId;let collectedSources = [];- let answer = '';+ // Accumulate deltas into an array; concat-on-state would be O(n²) since+ // each render also pays the React diff for an ever-growing string. We+ // join() only when flushing to state, and we batch state flushes with+ // requestAnimationFrame so the render rate is bounded by the display+ // refresh, not by the SSE delta rate.+ const chunks = [];+ let pendingFrame = 0;+ const flushStreamingTurn = () => {+ pendingFrame = 0;+ setStreamingTurn({ content: chunks.join(''), sources: collectedSources });+ };let persistedTurn = null;await api.streamAskTurn(@@ -310,10 +320,10 @@ export default function Ask() {}} else if (event === 'sources') {collectedSources = data.sources || [];- setStreamingTurn((prev) => ({ content: prev?.content || '', sources: collectedSources }));+ if (!pendingFrame) pendingFrame = requestAnimationFrame(flushStreamingTurn);} else if (event === 'delta') {- answer += data.text || '';- setStreamingTurn({ content: answer, sources: collectedSources });+ if (data.text) chunks.push(data.text);+ if (!pendingFrame) pendingFrame = requestAnimationFrame(flushStreamingTurn);} else if (event === 'done') {persistedTurn = data.turn || null;} else if (event === 'error') {@@ -325,6 +335,13 @@ export default function Ask() {if (err.name !== 'AbortError') toast.error(err.message || 'Ask failed');});+ // Cancel any rAF queued after the last delta so a flush can't race with+ // the cleanup `setStreamingTurn(null)` below.+ if (pendingFrame) {+ cancelAnimationFrame(pendingFrame);+ pendingFrame = 0;+ }+const wasAborted = controller.signal.aborted;setStreamingTurn(null);if (abortRef.current === controller) abortRef.current = null;diff --git a/server/routes/ask.js b/server/routes/ask.jsindex 88de495cbd..6c7736be64 100644--- a/server/routes/ask.js+++ b/server/routes/ask.js@@ -175,7 +175,11 @@ router.post('/', asyncHandler(async (req, res) => {// the last PROMPT_HISTORY_TURNS prior turns as multi-turn context.const history = (conversation.turns || []).slice(-(PROMPT_HISTORY_TURNS + 1), -1);- let assistantText = '';+ // Accumulate deltas into an array and join once at the end; `+=` would be+ // O(n²) on long answers where each chunk reallocates the running string.+ // Mirrors `askService.runAsk`'s producer-side pattern.+ const assistantChunks = [];+ let assistantFallback = '';let assistantSources = [];let providerInfo = {};@@ -201,21 +205,23 @@ router.post('/', asyncHandler(async (req, res) => {assistantSources = evt.sources;await send('sources', { sources: evt.sources });} else if (evt.type === 'delta') {- assistantText += evt.text;+ assistantChunks.push(evt.text);await send('delta', { text: evt.text });} else if (evt.type === 'done') {providerInfo = { providerId: evt.providerId, model: evt.model };// Both API and CLI providers stream their text via `delta` events// (CLI providers yield a single big chunk). The terminal `done`// event also carries the full answer as a defensive fallback —- // populate `assistantText` from it only if no deltas arrived.- if (!assistantText && evt.answer) assistantText = evt.answer;+ // capture it only if no deltas arrived.+ if (assistantChunks.length === 0 && evt.answer) assistantFallback = evt.answer;} else if (evt.type === 'error') {streamErrored = true;await send('error', { error: evt.error });}}+ const assistantText = assistantChunks.length ? assistantChunks.join('') : assistantFallback;+// If the client bailed mid-stream we still persist whatever assistant// text we accumulated so the conversation isn't silently lossy on// reconnect — but a write failure here must not bubble out either.