🎚️ refactor: Define the Code Highlight Cadence Default Once (#16946)

* refactor: Define the code highlight cadence default once

The interface schema, the config defaults and the client's normalizer read DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS and MAX_CODE_HIGHLIGHT_THROTTLE_MS from data-provider, and normalizeCodeHighlightThrottleMs moves beside them so its bounds cannot drift from the schema's. A spec pins the schema, config defaults, normalizer and librechat.example.yaml to the same default.

* test: Read the cadence from the signed-in config payload
This commit is contained in:
Marco Beretta 2026-10-10 00:39:28 +02:00 • committed by GitHub
parent b7da0e0f76
commit 4ec111cbc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 90 additions and 13 deletions

View file

@ -1,19 +1,12 @@
import React, { useState, useEffect, useRef } from 'react';
import { DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS } from 'librechat-data-provider';
export { normalizeCodeHighlightThrottleMs } from 'librechat-data-provider';
/** Quiet period before re-highlighting streaming code. */
export const HIGHLIGHT_THROTTLE_MS = 300;
export const HIGHLIGHT_THROTTLE_MS = DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS;
export const CodeHighlightThrottleContext = React.createContext(HIGHLIGHT_THROTTLE_MS);
export function normalizeCodeHighlightThrottleMs(value: unknown): number {
return typeof value === 'number' &&
Number.isFinite(value) &&
Number.isInteger(value) &&
value >= 0 &&
value <= 60000
? value
: HIGHLIGHT_THROTTLE_MS;
}
interface HastText {
type: 'text';
value: string;

View file

@ -7,6 +7,9 @@ cache: true
interface:
# Isolated test fleet contains only ownership-aware replicas.
runningChatRename: true
# Off the 300 ms default, so highlight-throttle.spec.ts can tell a configured
# cadence from the schema default in the served config.
codeHighlightThrottleMs: 250
# Exercises the cost row in the context usage gauge (off by default).
# Mock models price at the default rate, so synthetic usage yields a value.
contextCost: true

View file

@ -1,4 +1,5 @@
import { expect, test } from '@playwright/test';
import { DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS } from 'librechat-data-provider';
import {
enableCodeInterpreter,
@ -61,7 +62,29 @@ async function expectHighlightedCode(page: Parameters<typeof sendMessage>[0]) {
return code;
}
/** The cadence `e2e/config/librechat.e2e.yaml` sets, off the schema default. */
const CONFIGURED_THROTTLE_MS = 250;
test.describe('streamed code highlighting', () => {
test('configured-highlight-cadence-reaches-client @scenario:configured-highlight-cadence-reaches-client', async ({
page,
}) => {
expect(CONFIGURED_THROTTLE_MS).not.toBe(DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS);
/** The signed-in payload: the pre-login one carries only the fields a login page needs. */
const config = page.waitForResponse(
(response) =>
new URL(response.url()).pathname === '/api/config' &&
response.ok() &&
Boolean(response.request().headers()['authorization']),
{ timeout: 30000 },
);
await page.goto(NEW_CHAT_PATH, { timeout: 10000 });
const body = (await (await config).json()) as {
interface?: { codeHighlightThrottleMs?: number };
};
expect(body.interface?.codeHighlightThrottleMs).toBe(CONFIGURED_THROTTLE_MS);
});
test('streamed-code-highlights-after-settle @scenario:streamed-code-highlights-after-settle', async ({
page,
}) => {

View file

@ -1,3 +1,5 @@
import { join } from 'node:path';
import { readFileSync } from 'node:fs';
import {
paramDefinitionSchema,
agentsEndpointSchema,
@ -12,6 +14,10 @@ import {
fileStorageSchema,
fileStrategiesSchema,
normalizeAgentSelectorLimit,
getConfigDefaults,
MAX_CODE_HIGHLIGHT_THROTTLE_MS,
normalizeCodeHighlightThrottleMs,
DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS,
SKILL_SYNC_MAX_INTERVAL_MINUTES,
summarizationTriggerSchema,
summarizationConfigSchema,
@ -1515,6 +1521,38 @@ describe('configSchema skillSync', () => {
});
});
describe('codeHighlightThrottleMs', () => {
it('defaults to the one cadence the schema, the config defaults and the client fallback share', () => {
expect(DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS).toBe(300);
expect(interfaceSchema.parse({}).codeHighlightThrottleMs).toBe(
DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS,
);
expect(getConfigDefaults().interface?.codeHighlightThrottleMs).toBe(
DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS,
);
expect(normalizeCodeHighlightThrottleMs(undefined)).toBe(DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS);
});
it('accepts and normalizes the same range', () => {
for (const value of [0, 1, 300, MAX_CODE_HIGHLIGHT_THROTTLE_MS]) {
expect(
interfaceSchema.parse({ codeHighlightThrottleMs: value }).codeHighlightThrottleMs,
).toBe(value);
expect(normalizeCodeHighlightThrottleMs(value)).toBe(value);
}
for (const value of [-1, 1.5, MAX_CODE_HIGHLIGHT_THROTTLE_MS + 1, Number.NaN, '300']) {
expect(interfaceSchema.safeParse({ codeHighlightThrottleMs: value }).success).toBe(false);
expect(normalizeCodeHighlightThrottleMs(value)).toBe(DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS);
}
});
it('documents the same default in librechat.example.yaml', () => {
const example = readFileSync(join(__dirname, '../../../librechat.example.yaml'), 'utf8');
expect(example).toContain(`(default: ${DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS} ms)`);
expect(example).toContain(`# codeHighlightThrottleMs: ${DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS}\n`);
});
});
describe('interfaceSchema', () => {
it('accepts independent retention periods', () => {
expect(

View file

@ -2654,6 +2654,21 @@ export type TThemeDefinitionConfig = z.infer<typeof themeDefinitionSchema>;
/** A bundled theme name or an inline theme definition applied to every user. */
export const deploymentThemeSchema = z.union([z.string().trim().min(1), themeDefinitionSchema]);
/** Single source for the quiet period between syntax highlights while a code block streams, and
* its bounds; the schema default and the client's normalizer both read them. */
export const DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS = 300;
export const MAX_CODE_HIGHLIGHT_THROTTLE_MS = 60_000;
/** A configured highlight cadence the schema would accept, or the default. */
export function normalizeCodeHighlightThrottleMs(value: unknown): number {
return typeof value === 'number' &&
Number.isInteger(value) &&
value >= 0 &&
value <= MAX_CODE_HIGHLIGHT_THROTTLE_MS
? value
: DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS;
}
/** Single source for the agents panel selector's unsearched list cap; the
* schema default and the client fallback both read it. */
export const DEFAULT_AGENT_SELECTOR_LIMIT = 10;
@ -2696,7 +2711,12 @@ export const interfaceSchema = z
/** Set to false during a rolling upgrade, until every API replica supports title ownership and old title jobs drain. */
runningChatRename: z.boolean().default(true),
/** Milliseconds between syntax highlights while a code block streams. */
codeHighlightThrottleMs: z.number().int().min(0).max(60_000).default(300),
codeHighlightThrottleMs: z
.number()
.int()
.min(0)
.max(MAX_CODE_HIGHLIGHT_THROTTLE_MS)
.default(DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS),
/** Most agents the agents panel selector lists before a search term is
* typed; typing lifts the cap so search reaches every agent. */
agentSelectorLimit: z
@ -2945,7 +2965,7 @@ export const interfaceSchema = z
.default({
modelSelect: true,
runningChatRename: true,
codeHighlightThrottleMs: 300,
codeHighlightThrottleMs: DEFAULT_CODE_HIGHLIGHT_THROTTLE_MS,
agentSelectorLimit: DEFAULT_AGENT_SELECTOR_LIMIT,
parameters: true,
presets: true,