mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +00:00
* 🎞️ perf: Run the In-Flight Label Sweep on the Compositor The sweep on a running tool call, streaming reasoning and the elapsed timer animated `background-position` under `background-clip: text`, which no browser can hand to the compositor. Every frame of it ran style, paint and commit on the main thread, and each paint walked the transcript around the label, so the cost of a frame grew with the conversation and the sweep stuttered whenever the main thread was busy. `Shimmer` in `@librechat/client` draws the same sweep from two transforms that cancel: a masked band slides across the label while a second copy of the text inside it slides back, so the glyphs hold still and only the gap in the mask moves. The copy is generated content, so the label is still one text node. Every in-flight label renders through it. `e2e/benchmarks-fade` measures a frame and each kind of stream event inside an exported conversation, from a Chromium trace of the built client. * 🧪 test: Let the Frame Benchmark Open a Chat in a Chosen Theme * 🧵 perf: Keep the Composer, Nav Rail and Row Toolbars Out of Every Stream Event A streamed delta rewrites one row, but each one also re-rendered the composer form (and re-measured its textarea), the message nav rail (re-translating a label per message), the sidebar's chat provider and the pending-steers row, because each subscribed to the whole streaming message or the whole messages array to read something a delta never changes. - The chat contract and steering read a projection of the branch tail (where it sits, whether the run is paused) and fetch the message itself when a turn is built, through `useBranchTailGetter`. `ChatTurnsParams` carries `getLatestMessage` in place of `latestMessage`. - The compact action and pending steers select what they use of the tail. - The URL-prompt hook observes the history only while a submission waits on it. - The nav rail's ribs are memoized apart from the rail. - A row's toolbar keeps one element per glyph and stable handlers, so a send and its completion no longer re-render every button of every row. `e2e/benchmarks-fade` gains a render-reason probe (`FADE_PERF_WHY`), a whole-run profile, and windows for the send and its confirming frame. * 🫥 perf: Conceal Rerun During a Run Instead of Unmounting It on Every Row A run starting or ending withholds, then restores, the rerun control on every row of the thread at once. Unmounting and remounting a button on each row made the browser re-style the whole transcript both times: the stylesheet holds `:has(:disabled)` rules and a message row is a `:has()` anchor, so a button entering, leaving or flipping `disabled` inside a row invalidates that row's entire subtree. In a 250-message thread that was 80 to 100 ms inside the send keypress and again inside the final frame. The control now stays mounted for as long as the row has a rerun to offer and is concealed by visibility while a run is in flight: no focus, no pointer, no place in the accessibility tree, and no layout where the toolbar starts at the row's edge. Below `lg`, where the toolbar is centered, it leaves the flow as before. It is deliberately not also `disabled`. The reply being written still mounts no controls at all. * 🎯 perf: Write :has() Rules So a Change in a Row Does Not Re-style the Row Two selector shapes in the stylesheet left the browser unable to tell which elements a `:has()` rule could reach, so it re-styled the whole subtree of every `:has()` anchor above any change: - Tailwind's `group-has-[…]`, which compiles to `.x:is(:where(.group):has(…) *)`. Message rows and their toolbars are `group`s, so a button mounting or unmounting in a row re-styled the row. - `theme-disabled-within:[&_*]:…`, which compiles to `…:has(:disabled) *`. With it in the sheet, a `disabled` flip did the same. Either one alone was enough: flipping `disabled` on one button per row of a 250-message thread took 100 to 110 ms, and mounting one took 88 ms. A run starting or ending did one of those on every row. The row-hover reveal, the header label crossfade and the toolbar's hover-active hold now use `grouped-*` custom variants that emit `:where(.group):has(…) .x`: the same elements at the same specificity, with a subject the browser can read. A wrapper's parts take the disabled fill from classes the component adds while it is disabled, through `theme-disabled-fill`, instead of from `:has(:disabled) *`. The same two changes now cost 25 to 42 ms and 27 ms. `has-selectors.spec.ts` reads the stylesheet the app ships and fails on either shape; `restyle.perf.spec.ts` measures the cost. * 📱 fix: Keep a Concealed Rerun Hidden on Narrow Screens The narrow-screen stylesheet forces every toolbar button visible from outside any cascade layer, so it outranked the utilities that conceal a rerun while a run is in flight: below 1024px the button stayed on screen and in the tab order, inert. A concealed button now carries `data-concealed`, and that rule leaves it alone. Covered at a narrow and a wide viewport by what the reader gets: the settled reply's rerun is hidden and cannot take focus during a run, and is back after. * 🧮 perf: Skip the Pause Scan in Pending Steers When None Are Pending The pending-steers row follows the conversation's messages, so it renders for every streamed delta, and it resolved the run's pause state before checking whether it had anything to show. That check walks every part of every message. It now runs only with a steer to display.
162 lines
6.4 KiB
TypeScript
162 lines
6.4 KiB
TypeScript
import postcss from 'postcss';
|
|
import { expect, test } from '@playwright/test';
|
|
import { NEW_CHAT_PATH } from '../helpers';
|
|
|
|
/**
|
|
* No `:has()` rule in the shipped stylesheet may leave the browser unable to tell which
|
|
* elements it can reach.
|
|
*
|
|
* `:has()` makes an element's style depend on what is under it, so when something changes
|
|
* under an anchor the browser re-styles whatever the rule's subject could be. Two shapes give
|
|
* it nothing to narrow that by, and it falls back to the anchor's entire subtree:
|
|
*
|
|
* - a subject that is every descendant: `.wrap:has(:disabled) *`
|
|
* - `:has()` inside `:is()` or `:where()` with more selector after it:
|
|
* `.x:is(:where(.group):has(:focus-visible) *)`, which is what Tailwind's `group-has-[…]`
|
|
* and `peer-has-[…]` emit
|
|
*
|
|
* Message rows are `:has()` anchors, so one rule of either shape made a button mounting, or
|
|
* flipping `disabled`, in every row re-style every row: 90 to 110 ms in a 250-message thread,
|
|
* at the start and the end of each run. The same rules written with a named subject
|
|
* (`:where(.group):has(:focus-visible) .x`) match the same elements at a tenth of the cost;
|
|
* `e2e/benchmarks-fade/restyle.perf.spec.ts` measures it.
|
|
*/
|
|
|
|
/** Splits at the top level only, so a separator inside `()` or `[]` is left alone. */
|
|
function splitTopLevel(text: string, isSeparator: (char: string) => boolean): string[] {
|
|
const parts: string[] = [];
|
|
let depth = 0;
|
|
let current = '';
|
|
for (const char of text) {
|
|
if (char === '(' || char === '[') {
|
|
depth += 1;
|
|
} else if (char === ')' || char === ']') {
|
|
depth -= 1;
|
|
}
|
|
if (depth === 0 && isSeparator(char)) {
|
|
parts.push(current);
|
|
current = '';
|
|
continue;
|
|
}
|
|
current += char;
|
|
}
|
|
parts.push(current);
|
|
return parts.map((part) => part.trim()).filter((part) => part.length > 0);
|
|
}
|
|
|
|
const compoundsOf = (complex: string): string[] =>
|
|
splitTopLevel(complex, (char) => /[\s>+~]/.test(char));
|
|
|
|
/** The arguments of every `:is()`, `:where()` and `:not()` in a selector, at any depth. */
|
|
function functionalArguments(selector: string): string[] {
|
|
const found: string[] = [];
|
|
const opener = /:(?:is|where|not)\(/g;
|
|
for (let match = opener.exec(selector); match != null; match = opener.exec(selector)) {
|
|
let depth = 1;
|
|
let end = match.index + match[0].length;
|
|
while (end < selector.length && depth > 0) {
|
|
if (selector[end] === '(') {
|
|
depth += 1;
|
|
} else if (selector[end] === ')') {
|
|
depth -= 1;
|
|
}
|
|
end += 1;
|
|
}
|
|
found.push(selector.slice(match.index + match[0].length, end - 1));
|
|
}
|
|
return found;
|
|
}
|
|
|
|
/** A compound the browser can look up: it names a class, an id, an attribute or a tag. */
|
|
function namesItsSubject(compound: string): boolean {
|
|
const bare = compound.replace(/^\*/, '');
|
|
return /^[a-zA-Z.#[]/.test(bare) || /[.#[]/.test(bare);
|
|
}
|
|
|
|
/** Why a selector leaves a `:has()` unbounded, or `null` when it does not. */
|
|
function unboundedHas(selector: string): string | null {
|
|
for (const complex of splitTopLevel(selector, (char) => char === ',')) {
|
|
if (!complex.includes(':has(')) {
|
|
continue;
|
|
}
|
|
const compounds = compoundsOf(complex);
|
|
const subject = compounds[compounds.length - 1];
|
|
const anchored = compounds.slice(0, -1).some((compound) => compound.includes(':has('));
|
|
if (anchored && !namesItsSubject(subject)) {
|
|
return 'its subject is every descendant of the :has() anchor';
|
|
}
|
|
for (const argument of functionalArguments(complex)) {
|
|
for (const inner of splitTopLevel(argument, (char) => char === ',')) {
|
|
const innerCompounds = compoundsOf(inner);
|
|
if (innerCompounds.slice(0, -1).some((compound) => compound.includes(':has('))) {
|
|
return ':has() sits inside :is()/:where() with more selector after it';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
test.describe(':has() selectors', () => {
|
|
test('the checker reports the two unbounded shapes and passes their bounded forms @scenario:has-selector-checker-knows-its-shapes', () => {
|
|
const FOCUS = ':focus-visible:not(:is(input,textarea,[contenteditable]))';
|
|
for (const selector of [
|
|
'.theme-disabled-within\\:\\[\\&_\\*\\]\\:text-text-disabled:is(:root *):has(:disabled) *',
|
|
'.wrap:has(:disabled) *',
|
|
'.wrap:has(:disabled) > :hover',
|
|
`.group-has-\\[x\\]\\:opacity-100:is(:where(.group):has(${FOCUS}) *)`,
|
|
'.x:is(:where(.group):has([data-hover-active]) *)',
|
|
'.x:where(.peer:has(:checked) ~ *)',
|
|
'.fine, .wrap:has(:disabled) *',
|
|
]) {
|
|
expect(unboundedHas(selector), selector).not.toBeNull();
|
|
}
|
|
for (const selector of [
|
|
`:where(.group):has(${FOCUS}) .grouped-keyboard-focus\\:opacity-100`,
|
|
'.wrap:has(:disabled) .part',
|
|
'.wrap:has(:disabled) p',
|
|
'.theme-disabled-within\\:opacity-100:is(:root *):has(:disabled)',
|
|
'.has-\\[\\:disabled\\]\\:opacity-50:has(:disabled)',
|
|
'.result-streaming>:is(ul,ol):last-child>li:last-child:not(:has(>:is(ul,ol,pre))):after',
|
|
`.message-render:has(${FOCUS}) .message-timestamp`,
|
|
'.theme-disabled-fill\\:\\[\\&_\\*\\]\\:text-text-disabled:is(:root *) *',
|
|
'.animated-panels:has(>[data-was-open])>.animated-tab-panel:not([dir=ltr])',
|
|
]) {
|
|
expect(unboundedHas(selector), selector).toBeNull();
|
|
}
|
|
});
|
|
|
|
test('every :has() rule the app ships names what it can reach @scenario:has-selectors-stay-scoped', async ({
|
|
page,
|
|
}) => {
|
|
await page.goto(NEW_CHAT_PATH, { timeout: 10_000 });
|
|
await expect(page.locator('html')).toBeVisible();
|
|
const hrefs = await page.evaluate(() =>
|
|
Array.from(document.styleSheets)
|
|
.map((sheet) => sheet.href)
|
|
.filter((href): href is string => href != null && href.startsWith(location.origin)),
|
|
);
|
|
expect(hrefs.length).toBeGreaterThan(0);
|
|
|
|
const offenders: string[] = [];
|
|
let checked = 0;
|
|
for (const href of hrefs) {
|
|
const response = await page.request.get(href);
|
|
expect(response.ok()).toBeTruthy();
|
|
postcss.parse(await response.text()).walkRules((rule) => {
|
|
if (!rule.selector.includes(':has(')) {
|
|
return;
|
|
}
|
|
checked += 1;
|
|
const reason = unboundedHas(rule.selector);
|
|
if (reason != null) {
|
|
offenders.push(`${rule.selector.replace(/\s+/g, ' ')}\n ${reason}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
/** A stylesheet with no `:has()` at all would pass for the wrong reason. */
|
|
expect(checked).toBeGreaterThan(0);
|
|
expect(offenders).toEqual([]);
|
|
});
|
|
});
|