TypeScript
perf(client): a reconnect snapshot keeps unchanged skills and providers identical (#20855)
Julius Marmingebc5f754baabf
diff --git a/packages/client-runtime/src/state/serverConfigProjection.test.ts b/packages/client-runtime/src/state/serverConfigProjection.test.tsindex bf33266b01a..5a3e7f67acd 100644--- a/packages/client-runtime/src/state/serverConfigProjection.test.ts+++ b/packages/client-runtime/src/state/serverConfigProjection.test.ts@@ -1,6 +1,8 @@+import type { ServerConfig, ServerConfigStreamEvent } from "@t3tools/contracts";+import * as Option from "effect/Option";import { describe, expect, it } from "vite-plus/test";-import { reuseUnchanged } from "./serverConfigProjection.ts";+import { applyServerConfigProjection, reuseUnchanged } from "./serverConfigProjection.ts";describe("reuseUnchanged", () => {const provider = (checkedAt: string, skills: ReadonlyArray<string>) => ({@@ -38,3 +40,23 @@ describe("reuseUnchanged", () => {expect(dropped).toEqual({ a: 1 });});});++describe("applyServerConfigProjection snapshot", () => {+ const snapshot = (checkedAt: string) =>+ ({+ type: "snapshot",+ config: {+ environment: { capabilities: {} },+ providers: [{ instanceId: "claude", checkedAt, skills: [{ name: "review" }] }],+ },+ }) as unknown as ServerConfigStreamEvent;+ const providersOf = (projection: ReturnType<typeof applyServerConfigProjection>) =>+ (Option.getOrThrow(projection).config as ServerConfig).providers;++ it("keeps unchanged skills when a reconnect re-sends the config", () => {+ const first = applyServerConfigProjection(Option.none(), snapshot("a"));+ const second = applyServerConfigProjection(first, snapshot("b"));+ expect(providersOf(second)[0]!.checkedAt).toBe("b");+ expect(providersOf(second)[0]!.skills).toBe(providersOf(first)[0]!.skills);+ });+});diff --git a/packages/client-runtime/src/state/serverConfigProjection.ts b/packages/client-runtime/src/state/serverConfigProjection.tsindex 4ef63ad84ca..dc6906e081b 100644--- a/packages/client-runtime/src/state/serverConfigProjection.ts+++ b/packages/client-runtime/src/state/serverConfigProjection.ts@@ -24,6 +24,7 @@ export function withoutEnvironmentThemes(config: ServerConfig): ServerConfig {* previous object. A provider status push re-sends every provider even when* only one field (often just `checkedAt`) changed, so this keeps unchanged* providers, models and skills identical and lets memoized UI skip them.+ * A reconnect snapshot re-sends the whole config the same way.*/export function reuseUnchanged<T>(previous: unknown, next: T): T {if (Object.is(previous, next)) return next;@@ -78,12 +79,14 @@ export function applyServerConfigProjection(capabilities.usageLimitSources === true && Option.isSome(current)? current.value.config.usageLimitSources: undefined;+ const config: ServerConfig = {+ ...event.config,+ ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),+ ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),+ };return Option.some({- config: {- ...event.config,- ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),- ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),- },+ // Every reconnect re-sends the whole config, usually unchanged apart from timestamps.+ config: Option.isSome(current) ? reuseUnchanged(current.value.config, config) : config,latestEvent: event,source: "live" as const,});