Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 修复切换任务后流式动效重播 (#2459)
DavidShenXD8b02cafd55ae
diff --git a/apps/desktop/src/renderer/components/chat/AssistantMessage.tsx b/apps/desktop/src/renderer/components/chat/AssistantMessage.tsxindex 9a33e86de6..1f8775b962 100644--- a/apps/desktop/src/renderer/components/chat/AssistantMessage.tsx+++ b/apps/desktop/src/renderer/components/chat/AssistantMessage.tsx@@ -117,12 +117,14 @@ const StreamingBody = memo(function StreamingBody({localFileRefs,currentSessionId,currentSessionTitle,+ streamFadeKey,}: {workingDir: string;content: string;localFileRefs?: readonly KnownLocalFileRef[];currentSessionId?: string;currentSessionTitle?: string | null;+ streamFadeKey?: string;}) {const { committedText, pendingLine } = useMemo(() => splitAtLastNewline(content),@@ -135,6 +137,7 @@ const StreamingBody = memo(function StreamingBody({workingDir={workingDir}content={committedText}isStreaming+ streamFadeKey={streamFadeKey}localFileRefs={localFileRefs}currentSessionId={currentSessionId}currentSessionTitle={currentSessionTitle}@@ -286,6 +289,9 @@ export const AssistantMessage = memo(function AssistantMessage({// /goal:隐藏助手输出末尾由 goal 协议要求模型吐出的裁决 JSON 块(仅显示层剥离,// 原文仍保留在 DB / transcript)。pattern:末尾的 ```json {...goal_status...} ``` 围栏块。const displayContent = stripGoalVerdictBlock(content);+ const streamFadeKey = messageClientId+ ? `${currentSessionId ?? ''}\u0000${messageClientId}`+ : undefined;return (<div@@ -324,6 +330,7 @@ export const AssistantMessage = memo(function AssistantMessage({localFileRefs={localFileRefs}currentSessionId={currentSessionId}currentSessionTitle={currentSessionTitle}+ streamFadeKey={streamFadeKey}/>) : (// 默认分支 (USE_LINE_COMMIT_STREAMING=false) + 非 streaming 都走完整@@ -333,6 +340,7 @@ export const AssistantMessage = memo(function AssistantMessage({workingDir={workingDir}content={displayContent}isStreaming={isStreaming}+ streamFadeKey={streamFadeKey}localFileRefs={localFileRefs}currentSessionId={currentSessionId}currentSessionTitle={currentSessionTitle}diff --git a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsxindex 89be3247a3..6bddbe0cf9 100644--- a/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx+++ b/apps/desktop/src/renderer/components/chat/MarkdownRenderer.tsx@@ -33,8 +33,9 @@ import remarkSessionLinks from './remarkSessionLinks';import { rehypeMathBlockMarker } from './rehypeMathBlockMarker';import { FENCED_CODE_PROP, rehypeFencedCodeMarker } from './rehypeFencedCodeMarker';import {- createWordFadeState,+ getOrCreateWordFadeState,markSettledFromAnimationEnd,+ releaseWordFadeState,rehypeStreamWordFade,} from './rehypeStreamWordFade';import { repairStreamingMarkdown } from './repairStreamingMarkdown';@@ -317,6 +318,11 @@ interface MarkdownRendererProps {* message never misses its last token. Default false (static content* paths like TextLightbox bypass the throttle entirely). */isStreaming?: boolean;+ /**+ * Stable identity of the streaming message. Keeps the word-fade timeline+ * across task-view remounts so already-rendered text cannot replay.+ */+ streamFadeKey?: string;/** Files uploaded in the session. Used to resolve model-authored links like* `[doc.docx](doc.docx)` back to the original attachment path. */localFileRefs?: readonly KnownLocalFileRef[];@@ -1620,6 +1626,7 @@ export const MarkdownRenderer = memo(function MarkdownRenderer({workingDir,content,isStreaming = false,+ streamFadeKey,localFileRefs,currentSessionId,currentSessionTitle,@@ -1644,12 +1651,15 @@ export const MarkdownRenderer = memo(function MarkdownRenderer({const streamFade = isStreaming && !reducedMotion && streamFadeEnabled;const wordFade = useMemo(() => {if (!streamFade) return null;- const state = createWordFadeState();+ const state = getOrCreateWordFadeState(streamFadeKey);return {state,plugins: [...REHYPE_PLUGINS, [rehypeStreamWordFade, state]] as PluggableList,};- }, [streamFade]);+ }, [streamFade, streamFadeKey]);+ useEffect(() => {+ if (!isStreaming) releaseWordFadeState(streamFadeKey);+ }, [isStreaming, streamFadeKey]);const rehypePlugins = wordFade?.plugins ?? REHYPE_PLUGINS;const handleWordFadeAnimationEnd = useMemo(() => {if (!wordFade) return undefined;diff --git a/apps/desktop/src/renderer/components/chat/__tests__/rehypeStreamWordFade.test.ts b/apps/desktop/src/renderer/components/chat/__tests__/rehypeStreamWordFade.test.tsindex 22d72e6fa1..22f84a1ba6 100644--- a/apps/desktop/src/renderer/components/chat/__tests__/rehypeStreamWordFade.test.ts+++ b/apps/desktop/src/renderer/components/chat/__tests__/rehypeStreamWordFade.test.ts@@ -8,17 +8,24 @@import { readFileSync } from 'node:fs';import { fileURLToPath } from 'node:url';-import { describe, expect, it } from 'vitest';+import { afterEach, describe, expect, it } from 'vitest';import type { Element, Root, Text } from 'hast';import {+ _resetWordFadeStateCacheForTests,createWordFadeState,+ getOrCreateWordFadeState,markSettledFromAnimationEnd,+ releaseWordFadeState,rehypeStreamWordFade,splitWords,type WordFadeState,} from '../rehypeStreamWordFade';+afterEach(() => {+ _resetWordFadeStateCacheForTests();+});+function textNode(value: string): Text {return { type: 'text', value };}@@ -78,6 +85,31 @@ describe('splitWords', () => {});describe('rehypeStreamWordFade', () => {+ it('同一条流式消息 remount 后沿用原时间线,已有词不从头重播', () => {+ const cacheKey = 'session-1\u0000message-1';+ const firstMount = getOrCreateWordFadeState(cacheKey);+ const tree1 = root(el('p', [textNode('one two')]));+ run(tree1, firstMount, 0);+ const firstKeys = collectWords(tree1).map((word) => word.key);++ const remount = getOrCreateWordFadeState(cacheKey);+ const tree2 = root(el('p', [textNode('one two three')]));+ run(tree2, remount, 1000);+ const words = collectWords(tree2);++ expect(remount).toBe(firstMount);+ expect(words.slice(0, 2).map((word) => word.key)).toEqual(firstKeys);+ expect(words.slice(0, 2).every((word) => word.delay < -150)).toBe(true);+ expect(words[2].delay).toBe(0);+ });++ it('消息进入终态后释放 remount 状态', () => {+ const cacheKey = 'session-1\u0000message-1';+ const active = getOrCreateWordFadeState(cacheKey);+ releaseWordFadeState(cacheKey);+ expect(getOrCreateWordFadeState(cacheKey)).not.toBe(active);+ });+it('文本节点被切成带 --wf-delay 的 stream-word span,首词 0ms 起、24ms/词递进', () => {const state = createWordFadeState();const tree = root(el('p', [textNode('one two three')]));diff --git a/apps/desktop/src/renderer/components/chat/rehypeStreamWordFade.ts b/apps/desktop/src/renderer/components/chat/rehypeStreamWordFade.tsindex d3a03b1f11..abaec67fde 100644--- a/apps/desktop/src/renderer/components/chat/rehypeStreamWordFade.ts+++ b/apps/desktop/src/renderer/components/chat/rehypeStreamWordFade.ts@@ -126,6 +126,43 @@ export function createWordFadeState(): WordFadeState {};}+/**+ * 流式消息在切换任务时会随 MessageStream 一起卸载。动画状态如果只挂在+ * MarkdownRenderer 实例上,回来后整条已有正文会被当成首次到达并重播。+ *+ * 这里按消息身份保留一个有界 LRU:同一条仍在流式中的消息 remount 后继续使用原来的+ * 绝对时间线;终态渲染会主动释放。上限只兜底清理由后台结束、此后再未打开的任务。+ */+const WORD_FADE_STATE_CACHE_MAX = 64;+const wordFadeStateCache = new Map<string, WordFadeState>();++export function getOrCreateWordFadeState(cacheKey?: string): WordFadeState {+ if (!cacheKey) return createWordFadeState();+ const cached = wordFadeStateCache.get(cacheKey);+ if (cached) {+ wordFadeStateCache.delete(cacheKey);+ wordFadeStateCache.set(cacheKey, cached);+ return cached;+ }++ const state = createWordFadeState();+ wordFadeStateCache.set(cacheKey, state);+ if (wordFadeStateCache.size > WORD_FADE_STATE_CACHE_MAX) {+ const oldest = wordFadeStateCache.keys().next().value;+ if (oldest !== undefined) wordFadeStateCache.delete(oldest);+ }+ return state;+}++export function releaseWordFadeState(cacheKey?: string): void {+ if (cacheKey) wordFadeStateCache.delete(cacheKey);+}++/** 测试专用:隔离模块级 remount 状态。 */+export function _resetWordFadeStateCacheForTests(): void {+ wordFadeStateCache.clear();+}+/*** animationend 落袋入口:MarkdownRenderer 根节点上监听冒泡的 animationend,* 把播完 stream-word-in 的词 key 记进 state.settled。按动画名过滤 —— 同一子树里