Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

Merge fix/session-switch-flicker into main

MagicLizic38414f3e36f
diff --cc apps/desktop/src/renderer/components/chat/MessageStream.tsx
index 02ce90b8fe,a58bdfa73a..e4a810462c
--- a/apps/desktop/src/renderer/components/chat/MessageStream.tsx
+++ b/apps/desktop/src/renderer/components/chat/MessageStream.tsx
@@@ -3020,25 -2892,11 +3017,13 @@@ export function MessageStream(
* `slice(startIdx, startIdx + anchoredForwardItems)`,配合向下扩窗(要点 1)。
* 同时导出 startIdx 供 windowAtTop 判定使用(要点 2)。
*/
- // ── 切换立即响应(shell-first mount)──
- // 旧行为:点击切 session → 首个提交同步构建整个消息树 → 期间界面冻结(压测
- // session 实测 ~380ms 无响应),体感是"卡住才切过去"。
- // 新行为:首个提交只渲染外壳(标题栏/输入框/空消息区 + spinner),消息树推迟
- // 到外壳绘制后的下一帧 —— 点击零冻结,先切进去再看到内容浮现(对齐 Codex
- // Desktop 的加载体感)。挂载后的滚动定位不受影响:pin-to-bottom 与 applyRestore
- // 都由 ResizeObserver 在内容真正挂载时驱动,首帧空内容它们自然 no-op。
- // 各 auto-fill effect 均有 `visibleRenderItems.length === 0` 早退守卫,空帧不误触发。
- // Warm history already has a bounded first-paint window. An empty shell adds
- // another render and frame to every switch without doing any useful loading.
- const [firstMountDeferred, setFirstMountDeferred] = useState(() => !historyLoaded);
- useEffect(() => {
- if (!firstMountDeferred) return;
- const raf = requestAnimationFrame(() => setFirstMountDeferred(false));
- return () => cancelAnimationFrame(raf);
- }, [firstMountDeferred]);
++
+ // ── 切换首帧 ──
+ // 首个提交直接渲染有界的首屏窗口,保留首屏成本控制;滚动锚点恢复仍由
+ // layout effect 和 ResizeObserver 处理。
+
+
const { items: visibleRenderItems, startIdx: visibleStartIdx } = useMemo(() => {
- if (firstMountDeferred) return { items: EMPTY_RENDER_ITEMS, startIdx: 0 };
if (allRenderItems.length === 0) return { items: allRenderItems, startIdx: 0 };
if (firstVisibleItemKey === null) {
// 首帧阶段(defaultWindowItems 还没被空闲扩窗抬到 INITIAL)叠加内容预算:
@@@ -3084,9 -2959,31 +3069,8 @@@
firstVisibleItemKey,
defaultWindowItems,
anchoredForwardItems,
- firstMountDeferred,
]);
- // If a restored default-tail anchor fell out of the tail while this session
- // was backgrounded, permanently fall back to the bounded default window for
- // this mount. This also prevents expandWindow from treating the stale key as
- // a user-created anchored window.
- useLayoutEffect(() => {
- if (!restoreDefaultViewportRef.current || !restoringRef.current || firstVisibleItemKey === null)
- return;
- const snap = restoreSnapshotRef.current;
- if (!snap?.viewportTopKey) return;
- if (allRenderItems.length === 0) return;
- const anchorIdx = findRestorableViewportItemIdx(allRenderItems, snap.viewportTopKey);
- // History hydration can temporarily omit the anchor; retain restore mode
- // until a later render can locate it instead of treating it as deleted.
- if (anchorIdx < 0) return;
- if (isViewportAnchorWithinDefaultTail(allRenderItems, snap.viewportTopKey)) return;
- restoreDefaultViewportRef.current = false;
- restoringRef.current = false;
- isNearBottomRef.current = false;
- setIsNearBottom(false);
- setFirstVisibleItemKey(null);
- setDefaultWindowItems(RENDER_WINDOW_INITIAL_ITEMS);
- }, [allRenderItems, firstVisibleItemKey]);
-
// 两段式默认窗口第二段:首帧(非空)提交后,空闲期把默认窗口扩回 INITIAL。
// 只在仍钉底时扩(prepend 在视口上方,pin-to-bottom layout effect 同帧重钉,
// 无跳动);已向上滚离底部 / 已切到锚点窗口的,交给既有 expandWindow 路径。
@@@ -3814,63 -3673,6 +3798,62 @@@
const applyRestoreRef = useRef(applyRestore);
applyRestoreRef.current = applyRestore;
+ // An explicit saved reading position is independent of automatic tail fill.
+ // Run after intrinsic-size seeding, before paint, so a bounded remount does
+ // not first display the clamped position and then expand on a later frame.
+ // Reuse the store's bounded, epoch-guarded local/remote history lookup.
+ useLayoutEffect(() => {
- if (!restoringRef.current || !historyLoaded || firstMountDeferred) return;
++ if (!restoringRef.current || !historyLoaded) return;
+ if (restoreCancelledRef.current) {
+ restoringRef.current = false;
+ return;
+ }
+ const snap = restoreSnapshotRef.current;
+ if (!snap || !sessionId) return;
+ const clientId =
+ snap.restoreClientId ?? snap.messageClientId ?? restoreClientIdFromKey(snap.viewportTopKey);
+ const restoredKey = resolveSavedViewportKey(allRenderItems, snap);
+ if (restoredKey) {
+ restoreSnapshotRef.current = { ...snap, viewportTopKey: restoredKey };
+ if (!visibleRenderItems.some((item) => item.key === restoredKey)) {
+ restoreLoadRef.current = 'loaded';
+ setFirstVisibleItemKey(restoredKey);
+ setAnchoredForwardItems(RENDER_WINDOW_FIRST_PAINT_ITEMS);
+ } else {
+ restoreLoadRef.current = applyRestoreRef.current() ? 'settled' : 'loaded';
+ }
+ return;
+ }
+ if (restoreLoadRef.current === 'pending') return;
+ if (restoreLoadRef.current === 'idle' && clientId) {
+ restoreLoadRef.current = 'pending';
+ const finish = (found: boolean) => {
+ if (!restoreMountedRef.current || !restoringRef.current || restoreCancelledRef.current)
+ return;
+ restoreLoadRef.current = found ? 'loaded' : 'failed';
+ setRestoreRevision((value) => value + 1);
+ };
+ void makerChatStore.loadAroundMessageClientId(sessionId, clientId, { radius: 60 }).then(
+ (row) => finish(row !== null),
+ () => finish(false),
+ );
+ return;
+ }
+ // A deleted/non-displayable anchor must not leave the view stuck restoring.
+ restoreLoadRef.current = 'settled';
+ restoringRef.current = false;
+ isNearBottomRef.current = true;
+ setIsNearBottom(true);
+ setFirstVisibleItemKey(null);
+ }, [
+ allRenderItems,
+ visibleRenderItems,
+ historyLoaded,
+ historyCleared,
- firstMountDeferred,
+ sessionId,
+ restoreRevision,
+ ]);
+
// 保存当前浏览位置到 sessionScrollStore,并同步刷新删除前快照(单次量测)。用户
// 滚动时持续调用(DOM 一定存活),unmount cleanup 兜底最后一帧;量测失败则跳过。
const saveRafRef = useRef<number | null>(null);