Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(frontend): prevent insertBefore DOM race during streaming output

Marnie80afd09c8f64
diff --git a/frontend/src/components/chat/ConversationTimeline.tsx b/frontend/src/components/chat/ConversationTimeline.tsx
index 076d26457..53b1a58de 100644
--- a/frontend/src/components/chat/ConversationTimeline.tsx
+++ b/frontend/src/components/chat/ConversationTimeline.tsx
@@ -18,24 +18,31 @@ export const ConversationTimeline = memo(function ConversationTimeline({ message
const container = containerRef.current;
if (!container || userIndices.length === 0) return;
+ let rafId = 0;
const onScroll = () => {
- const rect = container.getBoundingClientRect();
- const mid = rect.top + rect.height / 2;
- let closest = userIndices[0];
- let minDist = Infinity;
- for (const idx of userIndices) {
- const el = container.querySelector(`[data-msg-idx="${idx}"]`);
- if (!el) continue;
- const elRect = el.getBoundingClientRect();
- const dist = Math.abs(elRect.top - mid);
- if (dist < minDist) { minDist = dist; closest = idx; }
- }
- setActiveIdx(closest);
+ cancelAnimationFrame(rafId);
+ rafId = requestAnimationFrame(() => {
+ const rect = container.getBoundingClientRect();
+ const mid = rect.top + rect.height / 2;
+ let closest = userIndices[0];
+ let minDist = Infinity;
+ for (const idx of userIndices) {
+ const el = container.querySelector(`[data-msg-idx="${idx}"]`);
+ if (!el) continue;
+ const elRect = el.getBoundingClientRect();
+ const dist = Math.abs(elRect.top - mid);
+ if (dist < minDist) { minDist = dist; closest = idx; }
+ }
+ setActiveIdx(closest);
+ });
};
container.addEventListener("scroll", onScroll, { passive: true });
onScroll();
- return () => container.removeEventListener("scroll", onScroll);
+ return () => {
+ cancelAnimationFrame(rafId);
+ container.removeEventListener("scroll", onScroll);
+ };
}, [containerRef, userIndices]);
const scrollTo = useCallback((idx: number) => {
diff --git a/frontend/src/pages/Agent.tsx b/frontend/src/pages/Agent.tsx
index 79f40af49..0bce03367 100644
--- a/frontend/src/pages/Agent.tsx
+++ b/frontend/src/pages/Agent.tsx
@@ -1218,22 +1218,17 @@ export function Agent() {
);
})}
- {/* Pre-stream placeholder: visible after Send, before first SSE event */}
- {status === "streaming" && !reasoningActive && !streamingText && toolCalls.length === 0 && !messages.some((m) => m.type === "swarm_status" && m.swarmStatus?.status === "running") && (
- <div className="flex gap-3">
- <AgentAvatar />
- <div className="flex-1 min-w-0 flex items-center gap-2 text-xs text-muted-foreground pt-1">
- <Loader2 className="h-3 w-3 animate-spin text-primary shrink-0" />
- <span>{t('agent.agentWorking')}</span>
- </div>
- </div>
- )}
-
- {/* Live streaming area: text + tool status */}
- {(streamingText || reasoningActive || (status === "streaming" && toolCalls.length > 0)) && (
+ {/* Streaming area - single stable wrapper to prevent insertBefore DOM race */}
+ {status === "streaming" && (
<div className="flex gap-3">
<AgentAvatar />
<div className="flex-1 min-w-0 space-y-1.5">
+ {!reasoningActive && !streamingText && toolCalls.length === 0 && !messages.some((m) => m.type === "swarm_status" && m.swarmStatus?.status === "running") && (
+ <div className="flex items-center gap-2 text-xs text-muted-foreground pt-1">
+ <Loader2 className="h-3 w-3 animate-spin text-primary shrink-0" />
+ <span>{t('agent.agentWorking')}</span>
+ </div>
+ )}
{reasoningActive && !streamingText && (
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<Loader2 className="h-3 w-3 animate-spin text-primary shrink-0" />
@@ -1246,7 +1241,7 @@ export function Agent() {
<span className="inline-block w-0.5 h-4 bg-primary ml-0.5 animate-pulse align-middle" />
</div>
)}
- {status === "streaming" && toolCalls.length > 0 && (
+ {toolCalls.length > 0 && (
<ToolProgressIndicator toolCalls={toolCalls} />
)}
</div>