Skip to content

Commit 33c4353

Browse files
authored
Merge pull request #3747 from GCWing/gcwing/flowchat-long-session-performance
perf(flowchat): bound long-session rendering and preserve scroll stability
2 parents d4f2780 + 18eeb95 commit 33c4353

60 files changed

Lines changed: 2177 additions & 191 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎design-system/packages/ui/src/brand/brandPlayback.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ export function startBrandPlayback(element: Element, createAnimations: () => Ani
66
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)");
77
let animations: Animation[] = [];
88
let visible = true;
9+
const disposeAnimations = () => {
10+
animations.forEach(animation => {
11+
animation.cancel();
12+
// Cancellation stops playback, but the effect still owns its target and
13+
// keyframes. Release that graph when this playback lifetime ends.
14+
animation.effect = null;
15+
});
16+
animations = [];
17+
};
918

1019
const synchronize = () => {
1120
if (reducedMotion.matches) {
12-
animations.forEach(animation => animation.cancel());
13-
animations = [];
21+
disposeAnimations();
1422
} else if (document.hidden || !visible) {
1523
animations.forEach(animation => animation.pause());
1624
} else if (animations.length === 0) {
@@ -28,7 +36,7 @@ export function startBrandPlayback(element: Element, createAnimations: () => Ani
2836
reducedMotion.addEventListener("change", synchronize);
2937
document.addEventListener("visibilitychange", synchronize);
3038
return () => {
31-
animations.forEach(animation => animation.cancel());
39+
disposeAnimations();
3240
observer?.disconnect();
3341
reducedMotion.removeEventListener("change", synchronize);
3442
document.removeEventListener("visibilitychange", synchronize);

‎design-system/packages/ui/tests/brand.test.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ test("brand playback cancels for reduced motion, pauses when hidden and cleans u
6060
const motion = Object.assign(new EventTarget(), { matches: true });
6161
const document = Object.assign(new EventTarget(), { hidden: false, defaultView: { matchMedia: () => motion } });
6262
const calls = [];
63-
const animation = { cancel: () => calls.push("cancel"), pause: () => calls.push("pause"), play: () => calls.push("play") };
63+
const animation = { effect: null, cancel: () => calls.push("cancel"), pause: () => calls.push("pause"), play: () => calls.push("play") };
6464
const stop = startBrandPlayback({ ownerDocument: document, animate() {} }, () => {
6565
calls.push("create");
66+
animation.effect = { target: {} };
6667
return [animation];
6768
});
6869
assert.deepEqual(calls, [], "reduced motion must not start decorative animation");
@@ -74,9 +75,11 @@ test("brand playback cancels for reduced motion, pauses when hidden and cleans u
7475
document.dispatchEvent(new Event("visibilitychange"));
7576
motion.matches = true;
7677
motion.dispatchEvent(new Event("change"));
78+
assert.equal(animation.effect, null, "reduced motion releases the effect target");
7779
motion.matches = false;
7880
motion.dispatchEvent(new Event("change"));
7981
stop();
82+
assert.equal(animation.effect, null, "unmount releases keyframes and target ownership");
8083
document.dispatchEvent(new Event("visibilitychange"));
8184
assert.deepEqual(calls, ["create", "pause", "play", "cancel", "create", "cancel"]);
8285
});

‎src/apps/miniapp-shell/src/generated/chat-styles.css‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,9 @@
20482048
.flow-text-block.streaming .markdown-renderer {
20492049
animation: none;
20502050
}
2051+
.flow-text-block > .markdown-renderer.flow-text-block__section {
2052+
margin-block-start: var(--openbitfun-control-flow-chat-paragraph-gap);
2053+
}
20512054
.flow-text-block--runtime-status {
20522055
display: flex;
20532056
align-items: center;

‎src/web-ui/src/app/components/NavPanel/NavPanel.scss‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,10 +1350,11 @@ $_section-header-height: 22px;
13501350
}
13511351

13521352
&:hover,
1353+
&:active,
13531354
&:focus-visible,
13541355
&.is-open {
13551356
color: var(--openbitfun-color-content-primary);
1356-
background: var(--openbitfun-color-action-quiet-hover);
1357+
@include shell-surfaces.navigation-hover-feedback;
13571358
}
13581359

13591360
&:focus-visible {
@@ -1465,14 +1466,19 @@ $_section-header-height: 22px;
14651466
block-size: var(--openbitfun-control-icon-size-md);
14661467
}
14671468

1468-
&:hover {
1469-
color: var(--openbitfun-color-content-primary);
1470-
background: var(--openbitfun-color-action-quiet-hover);
1469+
// The footer owns navigation feedback on the button itself. Keep IconButton's
1470+
// hover overlay from covering that shared surface with a second background.
1471+
&::before {
1472+
background: transparent;
14711473
}
14721474

1473-
&.is-active {
1475+
&:hover,
1476+
&:active,
1477+
&:focus-visible,
1478+
&.is-active,
1479+
&.is-hover-open {
14741480
color: var(--openbitfun-color-content-primary);
1475-
background: var(--openbitfun-color-action-quiet-hover);
1481+
@include shell-surfaces.navigation-hover-feedback;
14761482
}
14771483

14781484
&:focus-visible {
@@ -1531,12 +1537,6 @@ $_section-header-height: 22px;
15311537
position: relative;
15321538
}
15331539

1534-
// Hover-open subtle glow on the main button
1535-
.openbitfun-nav-panel__footer-btn--icon.is-hover-open {
1536-
color: var(--openbitfun-color-content-primary);
1537-
background: var(--openbitfun-color-action-quiet-hover);
1538-
}
1539-
15401540
.openbitfun-nav-panel__footer-backdrop {
15411541
position: fixed;
15421542
inset: 0;
@@ -2994,8 +2994,7 @@ $_section-header-height: 22px;
29942994

29952995
@media (hover: hover) and (pointer: fine) {
29962996
.openbitfun-nav-panel__top-action-btn:hover,
2997-
.openbitfun-nav-panel__item:hover,
2998-
.openbitfun-nav-panel__footer-btn--icon:hover {
2997+
.openbitfun-nav-panel__item:hover {
29992998
@include shell-surfaces.navigation-hover-feedback;
30002999
}
30013000

‎src/web-ui/src/flow_chat/components/ChatInputWorkspaceStrip.scss‎

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ $track-item-gap: 10px;
311311
}
312312

313313
// The goal's own two exits. They sit after the readout rather than inside it,
314-
// because acting on a goal is a different gesture from opening it, and they
315-
// are square like the context ring so a track with a goal keeps one rhythm.
314+
// because acting on a goal is a different gesture from opening it. Keep
315+
// these compact exits aligned with the other 18px track controls.
316316
&__goal-run,
317317
&__goal-clear {
318318
@include strip-control;
@@ -330,26 +330,48 @@ $track-item-gap: 10px;
330330
&__usage-btn {
331331
@include strip-control;
332332

333-
width: 18px;
333+
width: var(--openbitfun-space-6);
334+
height: var(--openbitfun-space-6);
334335
padding: 0;
336+
transition:
337+
background-color var(--openbitfun-motion-duration-fast) var(--openbitfun-motion-easing-standard),
338+
color var(--openbitfun-motion-duration-fast) var(--openbitfun-motion-easing-standard),
339+
transform var(--openbitfun-motion-duration-instant) var(--openbitfun-motion-easing-standard);
340+
341+
// The composer context band already has a tint. Neutral interaction fills
342+
// remain distinct on that surface in both light and dark themes.
343+
&:hover:not(:disabled),
344+
&[aria-expanded='true']:not(:disabled) {
345+
background: var(--openbitfun-color-action-neutral-surface-hover);
346+
}
347+
348+
&:is(:hover, :focus-visible, [aria-expanded='true']):not(:disabled) .is-track {
349+
color: var(--openbitfun-color-content-secondary);
350+
}
351+
352+
&:active:not(:disabled) {
353+
background: var(--openbitfun-color-action-neutral-surface-pressed);
354+
transform: scale(0.94);
355+
}
335356
}
336357

337358
&__usage-ring {
338359
position: relative;
339360
display: inline-flex;
340-
flex: 0 0 12px;
341-
width: 12px;
342-
height: 12px;
361+
flex: 0 0 var(--openbitfun-control-icon-button-xs-icon-size);
362+
width: var(--openbitfun-control-icon-button-xs-icon-size);
363+
height: var(--openbitfun-control-icon-button-xs-icon-size);
343364

344365
svg {
345366
position: absolute;
346367
inset: 0;
347-
width: 12px;
348-
height: 12px;
368+
width: 100%;
369+
height: 100%;
349370
}
350371

351372
.is-track {
352373
color: var(--openbitfun-color-field-border-hover);
374+
transition: color var(--openbitfun-motion-duration-fast) var(--openbitfun-motion-easing-standard);
353375
}
354376

355377
.is-value {
@@ -558,8 +580,7 @@ $track-item-gap: 10px;
558580
display: none;
559581
}
560582

561-
// A control that loses its label collapses to the same square target the
562-
// context ring already is, so the row keeps one rhythm as it narrows
583+
// Controls that lose their labels collapse to compact square targets
563584
// instead of leaving lopsided pills behind.
564585
&__permission-trigger {
565586
width: 18px;
@@ -583,6 +604,17 @@ $track-item-gap: 10px;
583604
}
584605
}
585606

607+
@media (prefers-reduced-motion: reduce) {
608+
.openbitfun-chat-input-workspace-strip__usage-btn,
609+
.openbitfun-chat-input-workspace-strip__usage-ring .is-track {
610+
transition: none;
611+
}
612+
613+
.openbitfun-chat-input-workspace-strip__usage-btn:active:not(:disabled) {
614+
transform: none;
615+
}
616+
}
617+
586618
@media (max-width: 460px) {
587619
.openbitfun-chat-input-workspace-strip {
588620
&__branch {

‎src/web-ui/src/flow_chat/components/FlowTextBlock.scss‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
animation: none;
3636
}
3737

38+
> .markdown-renderer.flow-text-block__section {
39+
margin-block-start: var(--openbitfun-control-flow-chat-paragraph-gap);
40+
}
41+
3842
&--runtime-status {
3943
display: flex;
4044
align-items: center;

‎src/web-ui/src/flow_chat/components/FlowTextBlock.test.tsx‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const mocks = vi.hoisted(() => ({
1111
markdownRenderer: vi.fn(),
1212
}));
1313

14-
vi.mock('@/infrastructure/markdown', () => ({
15-
MarkdownRenderer: (props: { content: string; isStreaming?: boolean }) => {
14+
vi.mock('@/infrastructure/markdown', async () => ({
15+
MarkdownRenderer: (await import('react')).memo((props: { content: string; isStreaming?: boolean }) => {
1616
mocks.markdownRenderer(props);
1717
return <div data-testid="markdown-renderer">{props.content}</div>;
18-
},
18+
}),
1919
}));
2020

2121
vi.mock('./modern/FlowChatContext', () => ({
@@ -51,6 +51,25 @@ describe('FlowTextBlock', () => {
5151
vi.clearAllMocks();
5252
});
5353

54+
it('reparses only the live semantic suffix and retains the source owner and completed prefix nodes', async () => {
55+
const content = 'A paragraph with **bold** text and Unicode 中文.\n\n'.repeat(800);
56+
const item: FlowTextItem = { id: 'long-stream', type: 'text', timestamp: 1, status: 'streaming', isStreaming: true, isMarkdown: true, content };
57+
act(() => root.render(<FlowTextBlock textItem={item} />));
58+
const owner = container.querySelector('[data-flow-item-id]');
59+
const prefix = container.querySelector('[data-testid="markdown-renderer"]');
60+
expect(mocks.markdownRenderer.mock.calls.length).toBeGreaterThan(5);
61+
expect(mocks.markdownRenderer.mock.calls.filter(([props]) => props.isStreaming)).toHaveLength(1);
62+
mocks.markdownRenderer.mockClear();
63+
const appended = `${content}New streaming suffix.`;
64+
act(() => root.render(<FlowTextBlock textItem={{ ...item, content: appended }} />));
65+
await act(async () => { await vi.advanceTimersByTimeAsync(1000); });
66+
expect(container.textContent).toBe(appended);
67+
expect(container.querySelector('[data-flow-item-id]')).toBe(owner);
68+
expect(container.querySelector('[data-testid="markdown-renderer"]')).toBe(prefix);
69+
expect(mocks.markdownRenderer.mock.calls.every(([props]) => props.content.length < 4000)).toBe(true);
70+
expect(mocks.markdownRenderer.mock.calls.some(([props]) => props.content.includes('New streaming suffix.'))).toBe(true);
71+
});
72+
5473
it('does not re-render completed markdown just to settle streaming growth state', async () => {
5574
const textItem: FlowTextItem = {
5675
id: 'text-1',
@@ -70,6 +89,23 @@ describe('FlowTextBlock', () => {
7089
expect(mocks.markdownRenderer).toHaveBeenCalledTimes(1);
7190
});
7291

92+
it('keeps a selected sealed live section mounted without reparsing when a later section grows', async () => {
93+
const prefix = 'A stable paragraph.\n\n'.repeat(150);
94+
const item: FlowTextItem = { id: 'sectioned', type: 'text', timestamp: 1, status: 'streaming', isStreaming: true, isMarkdown: true, content: prefix + 'Changing suffix.' };
95+
act(() => root.render(<FlowTextBlock textItem={item} markdownSection={{ start: 0, end: prefix.length }} />));
96+
const node = container.querySelector('[data-testid="markdown-renderer"]')!.firstChild!;
97+
const range = document.createRange(); range.setStart(node, 2); range.setEnd(node, 16);
98+
const selection = window.getSelection()!; selection.removeAllRanges(); selection.addRange(range);
99+
mocks.markdownRenderer.mockClear();
100+
act(() => root.render(<FlowTextBlock textItem={{ ...item, content: item.content + ' More output.'.repeat(600) }} markdownSection={{ start: 0, end: prefix.length }} />));
101+
await act(async () => { await vi.advanceTimersByTimeAsync(1000); });
102+
expect(container.querySelector('[data-testid="markdown-renderer"]')!.firstChild).toBe(node);
103+
expect(selection.toString()).toBe(prefix.slice(2, 16));
104+
expect(mocks.markdownRenderer).not.toHaveBeenCalled();
105+
expect(container.querySelector('[data-streaming]')?.getAttribute('data-streaming')).toBe('false');
106+
selection.removeAllRanges();
107+
});
108+
73109
it.each(['', ' \n\t\u3000'])('renders no wrapper for blank text %j', content => {
74110
const textItem: FlowTextItem = {
75111
id: 'blank', type: 'text', timestamp: 1, status: 'completed',

0 commit comments

Comments
 (0)