From 4ec111cbc1541d4c55b7a456f136295963973091 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sat, 10 Oct 2026 00:39:28 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=9A=EF=B8=8F=20refactor:=20Define=20th?= =?UTF-8?q?e=20Code=20Highlight=20Cadence=20Default=20Once=20(#16946)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../Content/Parts/useLazyHighlight.ts | 15 ++------ e2e/config/librechat.e2e.yaml | 3 ++ .../mock/scenarios/highlight-throttle.spec.ts | 23 +++++++++++ .../specs/config-schemas.spec.ts | 38 +++++++++++++++++++ packages/data-provider/src/config.ts | 24 +++++++++++- 5 files changed, 90 insertions(+), 13 deletions(-) diff --git a/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts b/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts index 718cf7f866..22e5793571 100644 --- a/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts +++ b/client/src/components/Chat/Messages/Content/Parts/useLazyHighlight.ts @@ -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; diff --git a/e2e/config/librechat.e2e.yaml b/e2e/config/librechat.e2e.yaml index be32ca709e..73d1484422 100644 --- a/e2e/config/librechat.e2e.yaml +++ b/e2e/config/librechat.e2e.yaml @@ -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 diff --git a/e2e/specs/mock/scenarios/highlight-throttle.spec.ts b/e2e/specs/mock/scenarios/highlight-throttle.spec.ts index 848d3d1c86..1adbd6fe86 100644 --- a/e2e/specs/mock/scenarios/highlight-throttle.spec.ts +++ b/e2e/specs/mock/scenarios/highlight-throttle.spec.ts @@ -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[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, }) => { diff --git a/packages/data-provider/specs/config-schemas.spec.ts b/packages/data-provider/specs/config-schemas.spec.ts index 799feb2a75..2a1bdb0896 100644 --- a/packages/data-provider/specs/config-schemas.spec.ts +++ b/packages/data-provider/specs/config-schemas.spec.ts @@ -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( diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts index 3fd6d94a64..0ccac0e256 100644 --- a/packages/data-provider/src/config.ts +++ b/packages/data-provider/src/config.ts @@ -2654,6 +2654,21 @@ export type TThemeDefinitionConfig = z.infer; /** 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,