Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(chat): 发送后继续自动跟随最新消息
Dashfc33302c8683
diff --git a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.tsindex 6bc76ebed4..1899721414 100644--- a/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts+++ b/apps/desktop/src/renderer/__tests__/autoFollowIntent.test.ts@@ -6,12 +6,15 @@* 才能停止自动滚动。修复后解除跟随走事件意图(wheel / touch / 键盘),恢复* 跟随走「距离 + 向下方向」。三个纯函数的规则见 autoFollowIntent.ts 模块注释。*/+import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';import { describe, expect, it } from 'vitest';import {resolveLastUserMessageObservation,resolveNearBottomOnScroll,resolveRenderPinDecision,+ resolveSendWindowHandoff,selectTailUserMessageId,shouldUnpinOnUpIntent,shouldUnpinOnWheel,@@ -81,7 +84,7 @@ describe('shouldUnpinOnUpIntent', () => {describe('resolveNearBottomOnScroll', () => {const BASE = { thresholdPx: 100, directionDeadZonePx: 1 };- it('距底超过阈值 → 一律 false(滚动条拖拽等无 wheel 路径的解除兜底)', () => {+ it('距底超过阈值 + 明确上滚 / 已解除 → false(滚动条拖拽等无 wheel 路径的解除兜底)', () => {expect(resolveNearBottomOnScroll({...BASE,@@ -100,6 +103,33 @@ describe('resolveNearBottomOnScroll', () => {).toBe(false);});+ it('已在跟 + 距底越线但未上滚 → 保持跟随(发送后内容长高 / 迟到 pin 的 scroll 不得解除)', () => {+ expect(+ resolveNearBottomOnScroll({+ ...BASE,+ wasNearBottom: true,+ distanceFromBottom: 400,+ scrollDelta: 0,+ }),+ ).toBe(true);+ expect(+ resolveNearBottomOnScroll({+ ...BASE,+ wasNearBottom: true,+ distanceFromBottom: 400,+ scrollDelta: 80,+ }),+ ).toBe(true);+ expect(+ resolveNearBottomOnScroll({+ ...BASE,+ wasNearBottom: true,+ distanceFromBottom: 400,+ scrollDelta: -1,+ }),+ ).toBe(true);+ });+it('阈值带内 + 原本在跟 → 保持跟随(布局钳位 / 滚动条微拖的被动上移不解除)', () => {expect(resolveNearBottomOnScroll({@@ -272,6 +302,54 @@ describe('selectTailUserMessageId', () => {});});+describe('resolveSendWindowHandoff', () => {+ it('local send leaves any anchored window so later tail items keep following', () => {+ expect(+ resolveSendWindowHandoff({+ isNewUserSend: true,+ sentFromThisRenderer: true,+ hasWindowAnchor: true,+ windowCoversEnd: true,+ }),+ ).toEqual({ clearWindowAnchor: true, deferPinToNextRender: false });+ expect(+ resolveSendWindowHandoff({+ isNewUserSend: true,+ sentFromThisRenderer: true,+ hasWindowAnchor: true,+ windowCoversEnd: false,+ }),+ ).toEqual({ clearWindowAnchor: true, deferPinToNextRender: true });+ });++ it('does not touch the default tail window or an external injection', () => {+ expect(+ resolveSendWindowHandoff({+ isNewUserSend: true,+ sentFromThisRenderer: true,+ hasWindowAnchor: false,+ windowCoversEnd: true,+ }),+ ).toEqual({ clearWindowAnchor: false, deferPinToNextRender: false });+ expect(+ resolveSendWindowHandoff({+ isNewUserSend: true,+ sentFromThisRenderer: false,+ hasWindowAnchor: true,+ windowCoversEnd: false,+ }),+ ).toEqual({ clearWindowAnchor: false, deferPinToNextRender: false });+ expect(+ resolveSendWindowHandoff({+ isNewUserSend: false,+ sentFromThisRenderer: true,+ hasWindowAnchor: true,+ windowCoversEnd: true,+ }),+ ).toEqual({ clearWindowAnchor: false, deferPinToNextRender: false });+ });+});+describe('resolveLastUserMessageObservation', () => {it('seeds a restored user tail hydrated after mount without treating it as a send', () => {expect(@@ -299,3 +377,16 @@ describe('resolveLastUserMessageObservation', () => {});});});++describe('MessageStream send-window handoff wiring', () => {+ it('clears any anchored window on a local send and only defers pin for stale slices', () => {+ const source = readFileSync(+ resolve(__dirname, '../components/chat/MessageStream.tsx'),+ 'utf8',+ );+ expect(source).toContain('resolveSendWindowHandoff({');+ expect(source).toContain('if (windowHandoff.clearWindowAnchor)');+ expect(source).toContain('if (decision.pinToBottom && !windowHandoff.deferPinToNextRender)');+ expect(source).not.toContain('realTailUserSendOutsideWindow');+ });+});diff --git a/apps/desktop/src/renderer/components/chat/MessageStream.tsx b/apps/desktop/src/renderer/components/chat/MessageStream.tsxindex 1ad4d8695a..09aa12bf7c 100644--- a/apps/desktop/src/renderer/components/chat/MessageStream.tsx+++ b/apps/desktop/src/renderer/components/chat/MessageStream.tsx@@ -283,6 +283,7 @@ import {resolveNearBottomOnScroll,resolveLastUserMessageObservation,resolveRenderPinDecision,+ resolveSendWindowHandoff,selectTailUserMessageId,shouldUnpinOnUpIntent,shouldUnpinOnWheel,@@ -4354,45 +4355,41 @@ export function MessageStream({? visibleLastItem.message: null;- // 锚定窗口外检测到真实的新发送:当前 effect 的 visibleRenderItems 仍是旧切片,- // 不能同帧 pinToBottom。先清锚并恢复 near-bottom,下一次 render 切回默认尾窗- // 后再自然 pin;提前同步 lastUserMsgIdRef,防下一帧重复判成新发送。- let windowAnchorClearedOnSend = false;- const realTailUserSendOutsideWindow =- !restoringRef.current &&- firstVisibleItemKey !== null &&- !windowCoversEnd &&- lastUserMsg !== null &&- lastUserMsg.clientId !== lastUserMsgIdRef.current;- if (realTailUserSendOutsideWindow) {- setFirstVisibleItemKey(null);- isNearBottomRef.current = true;- setIsNearBottom(true);- setUnreadCount(0);- lastUserMsgIdRef.current = lastUserMsg.clientId;- windowAnchorClearedOnSend = true;- }+ // #2194: 未提供回调时按既有语义视为本端发送(测试 / 其它消费方不变);+ // 提供了回调就严格以其返回值为准——实现方误返回 undefined(如被 as any+ // 绕过)时按外部注入处理,不用 ?? true 掩盖(Copilot review nit)。+ const sentFromThisRenderer = lastUserMsg+ ? isLocalUserSend+ ? isLocalUserSend(lastUserMsg.clientId) === true+ : true+ : false;const userMessageObservation = resolveLastUserMessageObservation({restoring: restoringRef.current,tailUserMessageId: lastUserMsg?.clientId ?? null,previousTailUserMessageId: lastUserMsgIdRef.current,});- if (!windowAnchorClearedOnSend) {- lastUserMsgIdRef.current = userMessageObservation.baselineUserMessageId;- }+ lastUserMsgIdRef.current = userMessageObservation.baselineUserMessageId;const decision = resolveRenderPinDecision({restoring: restoringRef.current,newUserSend: userMessageObservation.isNewUserSend,- // #2194: 未提供回调时按既有语义视为本端发送(测试 / 其它消费方不变);- // 提供了回调就严格以其返回值为准——实现方误返回 undefined(如被 as any- // 绕过)时按外部注入处理,不用 ?? true 掩盖(Copilot review nit)。- sentFromThisRenderer: lastUserMsg- ? isLocalUserSend- ? isLocalUserSend(lastUserMsg.clientId) === true- : true- : false,+ sentFromThisRenderer,nearBottom: isNearBottomRef.current,});+ // 本端发送必须离开锚定历史窗,回到默认尾窗。只清「未覆盖末尾」的锚会漏掉+ // 「发送时窗口仍盖住末尾、随后 assistant/工具卡把尾部顶出窗口」——视口已经+ // 钉到最新,下一轮新消息却不再跟随。+ const windowHandoff = resolveSendWindowHandoff({+ isNewUserSend: userMessageObservation.isNewUserSend,+ sentFromThisRenderer,+ hasWindowAnchor: firstVisibleItemKey !== null,+ windowCoversEnd,+ });+ if (windowHandoff.clearWindowAnchor) {+ setFirstVisibleItemKey(null);+ isNearBottomRef.current = true;+ setIsNearBottom(true);+ setUnreadCount(0);+ }if (userMessageObservation.isNewUserSend && lastUserMsg) {lastUserMsgIdRef.current = lastUserMsg.clientId;@@ -4401,7 +4398,7 @@ export function MessageStream({restoringRef.current = false;isNearBottomRef.current = true;}- if (decision.pinToBottom && !windowAnchorClearedOnSend) pinToBottom();+ if (decision.pinToBottom && !windowHandoff.deferPinToNextRender) pinToBottom();const el = scrollRef.current;if (el) prevScrollTopRef.current = el.scrollTop;@@ -4762,7 +4759,8 @@ export function MessageStream({// 与 pinToBottom 高频竞态(小幅上滚永远越不过距离阈值就被钉回,且// programmaticScrollRef 窗口会吞掉部分用户 scroll 事件),距离判定对// 「上滚一行就停」不可靠。本 handler 只负责:- // - 离底 >= threshold → 解除(滚动条拖拽等无 wheel 事件路径的兜底);+ // - 离底 >= threshold 且明确上滚 → 解除(滚动条拖拽等无 wheel 路径的兜底);+ // 已在跟时内容在下方长高不得解除,否则发送后第一块新内容会把跟随掐死;// - 已解除 + 明确向下滚回阈值带内 → 恢复跟随。// 迁移规则收敛在 resolveNearBottomOnScroll(纯函数,见 autoFollowIntent.ts)。// `isNearBottomRef`(auto-follow gate)与 `isNearBottom` state(指示器显隐)diff --git a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts b/apps/desktop/src/renderer/components/chat/autoFollowIntent.tsindex 4ae1697efa..7216264677 100644--- a/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts+++ b/apps/desktop/src/renderer/components/chat/autoFollowIntent.ts@@ -195,6 +195,49 @@ export function resolveRenderPinDecision({return { clearRestoring: false, pinToBottom: nearBottom };}+export interface ResolveSendWindowHandoffArgs {+ /** The current render introduced a new user message at the tail. */+ isNewUserSend: boolean;+ /** The tail user message was sent from this renderer's composer. */+ sentFromThisRenderer: boolean;+ /** The stream is currently showing an anchored (non-default-tail) window. */+ hasWindowAnchor: boolean;+ /** The current bounded window already includes the real session tail. */+ windowCoversEnd: boolean;+}++export interface ResolveSendWindowHandoff {+ /** Local send while reading an anchored window: switch back to the default tail. */+ clearWindowAnchor: boolean;+ /**+ * The current visible slice does not include the real tail, so pinning this+ * frame would land on the old slice. Wait for the next render's tail window.+ */+ deferPinToNextRender: boolean;+}++/**+ * After a local send, leave any historical render window so later assistant /+ * tool items keep auto-following the real tail.+ *+ * An anchored window that still covers the end at send time would otherwise+ * stay anchored; the next appended items flip `windowCoversEnd` to false and+ * the stream treats that as "left the bottom", stopping follow. External+ * injections must not take this handoff (#2194).+ */+export function resolveSendWindowHandoff({+ isNewUserSend,+ sentFromThisRenderer,+ hasWindowAnchor,+ windowCoversEnd,+}: ResolveSendWindowHandoffArgs): ResolveSendWindowHandoff {+ const clearWindowAnchor = isNewUserSend && sentFromThisRenderer && hasWindowAnchor;+ return {+ clearWindowAnchor,+ deferPinToNextRender: clearWindowAnchor && !windowCoversEnd,+ };+}+export interface ResolveNearBottomArgs {/** scroll 事件前的跟随态(isNearBottomRef) */wasNearBottom: boolean;@@ -211,8 +254,10 @@ export interface ResolveNearBottomArgs {/*** scroll 事件驱动的跟随态迁移(handleScroll 消费)。*- * - 距底 >= threshold → false。离底解除,原有行为不变 — 这也是滚动条拖拽- * (没有 wheel 事件可听)的解除兜底。+ * - 距底 >= threshold 且(已解除, 或明确上滚) → false。这是滚动条拖拽+ * 等无 wheel 路径的解除兜底。**已在跟、却只是内容在下方长高**+ * (发送后首块 assistant / 工具卡撑高、迟到的程序化 scroll 事件)+ * 不得解除 — pin / ResizeObserver 会补上。* - 距底 < threshold 且原本在跟 → 保持 true。阈值带内的微小上移不在这里* 解除(滚动条微拖、布局收缩钳位等 scrollTop 被动上移会误伤),wheel /* touch / 键盘的意图解除路径已经覆盖了真实的用户上滚。@@ -227,7 +272,10 @@ export function resolveNearBottomOnScroll({thresholdPx,directionDeadZonePx,}: ResolveNearBottomArgs): boolean {- if (distanceFromBottom >= thresholdPx) return false;+ if (distanceFromBottom >= thresholdPx) {+ if (!wasNearBottom) return false;+ return scrollDelta >= -directionDeadZonePx;+ }if (wasNearBottom) return true;return scrollDelta > directionDeadZonePx;}diff --git a/apps/mobile/src/__tests__/messageScroll.test.ts b/apps/mobile/src/__tests__/messageScroll.test.tsindex 3b13096383..1a0a58f6ed 100644--- a/apps/mobile/src/__tests__/messageScroll.test.ts+++ b/apps/mobile/src/__tests__/messageScroll.test.ts@@ -420,7 +420,7 @@ describe('resolveMobileNearBottomOnScroll', () => {// 阈值带 = offsetY > 972。const metricsAt = (offsetY: number) => ({ contentHeight: 2000, offsetY, viewportHeight: 800 });- it('距底超出阈值 → false(原有离底解除行为不变)', () => {+ it('距底超出阈值 + 明确上滑 / 已解除 → false(原有离底解除行为不变)', () => {expect(resolveMobileNearBottomOnScroll({wasNearBottom: true, metrics: metricsAt(900), scrollDelta: -40,})).toBe(false);@@ -429,6 +429,18 @@ describe('resolveMobileNearBottomOnScroll', () => {})).toBe(false);});+ it('已在跟 + 距底越线但未上滑 → 保持跟随(发送后内容长高不得解除)', () => {+ expect(resolveMobileNearBottomOnScroll({+ wasNearBottom: true, metrics: metricsAt(900), scrollDelta: 0,+ })).toBe(true);+ expect(resolveMobileNearBottomOnScroll({+ wasNearBottom: true, metrics: metricsAt(500), scrollDelta: 80,+ })).toBe(true);+ expect(resolveMobileNearBottomOnScroll({+ wasNearBottom: true, metrics: metricsAt(500), scrollDelta: -1,+ })).toBe(true);+ });+it('阈值带内 + 原本在跟 → 保持跟随(贴底期间程序化 scrollToEnd 的增量不改状态)', () => {expect(resolveMobileNearBottomOnScroll({wasNearBottom: true, metrics: metricsAt(1000), scrollDelta: -20,diff --git a/apps/mobile/src/session/messageScroll.ts b/apps/mobile/src/session/messageScroll.tsindex 4172e3352a..8112b153ee 100644--- a/apps/mobile/src/session/messageScroll.ts+++ b/apps/mobile/src/session/messageScroll.ts@@ -125,7 +125,9 @@ export interface MobileNearBottomResolveInput {** - 命令式滚动 settling → 保持原跟随态,不把瞬时 offset 当成用户上翻;* - 内容未撑满一屏 → true(与 isNearMessageListBottom 同口径);- * - 距底超出阈值 → false(离底解除,原行为不变);+ * - 距底超出阈值且(已解除, 或明确上滑) → false(滚动条/拖动离底的兜底)。+ * 已在跟、只是内容在下方长高导致距底越线时保持跟随 — 发送后首块+ * assistant / 工具卡撑高不得把跟随掐死;* - 阈值带内且原本在跟 → 保持 true(贴底期间程序化 scrollToEnd 产生的* 向下增量、以及被动微小位移都不改变状态);* - 阈值带内且已解除 → 仅「明确向下」(增量 > 死区)才恢复跟随。**不能只看@@ -136,7 +138,10 @@ export function resolveMobileNearBottomOnScroll(input: MobileNearBottomResolveInif (input.programmaticScrollInFlight) return input.wasNearBottom;const { contentHeight, viewportHeight } = input.metrics;if (contentHeight <= viewportHeight) return true;- if (!isNearMobileMessageListBottom(input.metrics, input.bottomOverlayHeight)) return false;+ if (!isNearMobileMessageListBottom(input.metrics, input.bottomOverlayHeight)) {+ if (!input.wasNearBottom) return false;+ return input.scrollDelta >= -MOBILE_FOLLOW_REPIN_DIRECTION_DEAD_ZONE;+ }if (input.wasNearBottom) return true;return input.scrollDelta > MOBILE_FOLLOW_REPIN_DIRECTION_DEAD_ZONE;}