Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 35 additions & 44 deletions src/components/ClaudeCodeSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,57 +248,47 @@ const ClaudeCodeSessionInner: React.FC<ClaudeCodeSessionProps> = ({
});

// ✅ Refactored: Use custom Hook for smart auto-scroll
const { parentRef, userScrolled, setUserScrolled, setShouldAutoScroll } =
useSmartAutoScroll({
displayableMessages,
isLoading
});
const { parentRef, contentRef, userScrolled, setUserScrolled, setShouldAutoScroll, isStuckToBottom } =
useSmartAutoScroll();

// Fix: Scroll to bottom when session history is loaded
// Also re-trigger when message count changes significantly (e.g., streaming adds many messages)
const hasScrolledToBottomRef = useRef<string | null>(null);
const lastScrolledMessageCountRef = useRef(0);
const sessionMessagesRef = useRef<SessionMessagesRef>(null);

useEffect(() => {
// Check if we have messages and parentRef is attached
if (displayableMessages.length > 0 && parentRef.current) {
const currentSessionId = session?.id || 'new_session';
const currentCount = displayableMessages.length;

// Determine if we should scroll:
// 1. First time for this session (initial history load)
const isFirstTimeForSession = hasScrolledToBottomRef.current !== currentSessionId;
// 2. Message count jumped significantly (e.g., streaming added many messages at once)
const countDelta = currentCount - lastScrolledMessageCountRef.current;
const significantCountChange = lastScrolledMessageCountRef.current > 0 && countDelta >= 5;

if (isFirstTimeForSession || significantCountChange) {
// Use a small delay to ensure virtualizer has calculated sizes
const timer = setTimeout(() => {
if (parentRef.current) {
// Force scroll to bottom
parentRef.current.scrollTop = parentRef.current.scrollHeight;

// Sync with smart auto-scroll state
setUserScrolled(false);
setShouldAutoScroll(true);

// Mark as done for this session
hasScrolledToBottomRef.current = currentSessionId;
lastScrolledMessageCountRef.current = currentCount;

// Schedule a follow-up scroll to handle virtualizer re-measurements
setTimeout(() => {
if (parentRef.current) {
parentRef.current.scrollTop = parentRef.current.scrollHeight;
}
}, 200);
}
}, 150); // 150ms delay for stability
if (displayableMessages.length === 0) return;

return () => clearTimeout(timer);
}
}
const currentSessionId = session?.id || 'new_session';
const currentCount = displayableMessages.length;

// Determine if we should scroll:
// 1. First time for this session (initial history load)
const isFirstTimeForSession = hasScrolledToBottomRef.current !== currentSessionId;
// 2. Message count jumped significantly (e.g., streaming added many messages at once)
const countDelta = currentCount - lastScrolledMessageCountRef.current;
const significantCountChange = lastScrolledMessageCountRef.current > 0 && countDelta >= 5;

if (!isFirstTimeForSession && !significantCountChange) return;

// Mark as done for this session
hasScrolledToBottomRef.current = currentSessionId;
lastScrolledMessageCountRef.current = currentCount;

// Sync with smart auto-scroll state
setUserScrolled(false);
setShouldAutoScroll(true);

// 交给虚拟列表的 scrollToBottom:它先把目标行渲染出来,再按真实 scrollHeight 落底。
// 这里曾经直接写 parentRef.scrollTop = scrollHeight(还在 200ms 后补一次),
// 但那是在虚拟列表尚未测量出真实高度时写的,落点必然不对,
// 随后测量修正又把位置拉回去,就是肉眼看到的上下抖。
const timer = setTimeout(() => {
sessionMessagesRef.current?.scrollToBottom();
}, 50);

return () => clearTimeout(timer);
}, [displayableMessages.length, session?.id, setUserScrolled, setShouldAutoScroll]);

// ============================================================================
Expand All @@ -323,7 +313,6 @@ const ClaudeCodeSessionInner: React.FC<ClaudeCodeSessionProps> = ({
const unlistenRefs = useRef<UnlistenFn[]>([]);
const hasActiveSessionRef = useRef(false);
const floatingPromptRef = useRef<FloatingPromptInputRef>(null);
const sessionMessagesRef = useRef<SessionMessagesRef>(null);
const queuedPromptsRef = useRef<Array<{ id: string; prompt: string; model: ModelType }>>([]);
const isMountedRef = useRef(true);
const isListeningRef = useRef(false);
Expand Down Expand Up @@ -976,6 +965,8 @@ const ClaudeCodeSessionInner: React.FC<ClaudeCodeSessionProps> = ({
isLoading={isLoading}
error={error}
parentRef={parentRef}
contentRef={contentRef}
isStuckToBottom={isStuckToBottom}
onCancel={handleCancelExecution}
/>
</SessionProvider>
Expand Down
23 changes: 9 additions & 14 deletions src/components/message/SubagentMessageGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import React, { useState } from "react";
import { Bot, ChevronDown, ChevronUp } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";
import { cn } from "@/lib/utils";
import { AIMessage } from "./AIMessage";
import { UserMessage } from "./UserMessage";
Expand Down Expand Up @@ -117,16 +116,13 @@ export const SubagentMessageGroup: React.FC<SubagentMessageGroupProps> = ({
</div>
</div>

{/* 子代理消息列表(可折叠) */}
<AnimatePresence initial={false}>
{isExpanded && (
<motion.div
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
transition={{ duration: 0.2 }}
className="overflow-hidden"
>
{/* 子代理消息列表(可折叠)
这里刻意不做 height 动画:这个块位于虚拟列表的行内部,
animate={{height:'auto'}} 会让行高连续多帧变化,而虚拟列表测量的
正是这个元素——每帧都要重测并重排后续所有行,同时反复触发贴底补偿。
展开/收起一次到位,只留一次高度变化。 */}
{isExpanded && (
<div className="overflow-hidden">
<div className="p-2 space-y-2 bg-[var(--surface-panel-muted)]">
{/* 渲染子代理消息 */}
{subagentMessages.length > 0 ? (
Expand Down Expand Up @@ -177,9 +173,8 @@ export const SubagentMessageGroup: React.FC<SubagentMessageGroupProps> = ({
</div>
)}
</div>
</motion.div>
)}
</AnimatePresence>
</div>
)}
</div>
</div>
);
Expand Down
29 changes: 16 additions & 13 deletions src/components/message/ThinkingBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,26 @@ export const ThinkingBlock: React.FC<ThinkingBlockProps> = ({
</span>
</button>

{/* Content - 可展开/收起 */}
<div
className={cn(
"overflow-hidden transition-all duration-300 ease-in-out",
isOpen ? "max-h-[500px] opacity-100" : "max-h-0 opacity-0"
)}
>
{/* Content - 可展开/收起
这里刻意不做高度动画(原来是 max-h-0 ↔ max-h-[500px] 配 transition-all):
这个块在虚拟列表的行内部,高度动画意味着连续 300ms、每帧都在改行高,
虚拟列表每帧都要重测并重排后续所有行,同时触发贴底逻辑反复补偿。
展开/收起一次到位,只留一次高度变化。 */}
{isOpen && (
<div
className="px-3 pb-3 pt-1"
onDoubleClick={handleDoubleClick}
title={isTyping ? t('thinking.doubleClickSkip') : undefined}
className="overflow-hidden"
>
<div className="text-xs text-muted-foreground/80 whitespace-pre-wrap font-mono leading-relaxed max-h-[400px] overflow-y-auto">
{renderContent()}
<div
className="px-3 pb-3 pt-1"
onDoubleClick={handleDoubleClick}
title={isTyping ? t('thinking.doubleClickSkip') : undefined}
>
<div className="text-xs text-muted-foreground/80 whitespace-pre-wrap font-mono leading-relaxed max-h-[400px] overflow-y-auto">
{renderContent()}
</div>
</div>
</div>
</div>
)}
</div>
);
};
Expand Down
Loading