Open History to see the last commit
1import type { ServerConfig, ServerConfigStreamEvent } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3
4export interface ServerConfigProjection {
5 readonly config: ServerConfig;
6 readonly latestEvent: ServerConfigStreamEvent;
7 readonly source: "cache" | "live";
8}
9
10/**
11 * Published themes and usage-limit sources are current machine state, so a
12 * cache could restore a set the machine no longer reports.
13 */
14export function withoutEnvironmentThemes(config: ServerConfig): ServerConfig {
15 if (config.environmentThemes === undefined && config.usageLimitSources === undefined) {
16 return config;
17 }
18 const { environmentThemes: _themes, usageLimitSources: _sources, ...rest } = config;
19 return rest;
20}
21
22/**
23 * Returns `next` with every part deep-equal to `previous` replaced by the
24 * previous object. A provider status push re-sends every provider even when
25 * only one field (often just `checkedAt`) changed, so this keeps unchanged
26 * providers, models and skills identical and lets memoized UI skip them.
27 */
28export function reuseUnchanged<T>(previous: unknown, next: T): T {
29 if (Object.is(previous, next)) return next;
30 if (
31 typeof previous !== "object" ||
32 typeof next !== "object" ||
33 previous === null ||
34 next === null ||
35 Array.isArray(previous) !== Array.isArray(next)
36 ) {
37 return next;
38 }
39 if (Array.isArray(next)) {
40 const before = previous as ReadonlyArray<unknown>;
41 let same = before.length === next.length;
42 const merged = next.map((item, index) => {
43 const reused = reuseUnchanged(before[index], item);
44 if (reused !== before[index]) same = false;
45 return reused;
46 });
47 return (same ? previous : merged) as T;
48 }
49 if (Object.getPrototypeOf(next) !== Object.prototype) return next;
50 const before = previous as Record<string, unknown>;
51 const after = next as Record<string, unknown>;
52 const keys = Object.keys(after);
53 let same = keys.length === Object.keys(before).length;
54 const merged: Record<string, unknown> = {};
55 for (const key of keys) {
56 const reused = reuseUnchanged(before[key], after[key]);
57 if (reused !== before[key] || !(key in before)) same = false;
58 merged[key] = reused;
59 }
60 return (same ? previous : merged) as T;
61}
62
63export function applyServerConfigProjection(
64 current: Option.Option<ServerConfigProjection>,
65 event: ServerConfigStreamEvent,
66): Option.Option<ServerConfigProjection> {
67 switch (event.type) {
68 case "snapshot": {
69 // Wire snapshots never contain published themes. Keep the previous set
70 // until a capable server sends its authoritative theme event. A legacy
71 // server cannot send a later removal, so a downgrade must clear the set.
72 const capabilities = event.config.environment.capabilities;
73 const carriedThemes =
74 capabilities.environmentThemes === true && Option.isSome(current)
75 ? current.value.config.environmentThemes
76 : undefined;
77 const carriedSources =
78 capabilities.usageLimitSources === true && Option.isSome(current)
79 ? current.value.config.usageLimitSources
80 : undefined;
81 return Option.some({
82 config: {
83 ...event.config,
84 ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),
85 ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),
86 },
87 latestEvent: event,
88 source: "live" as const,
89 });
90 }
91 case "keybindingsUpdated":
92 return Option.map(current, (projection) => ({
93 config: {
94 ...projection.config,
95 keybindings: event.payload.keybindings,
96 issues: event.payload.issues,
97 },
98 latestEvent: event,
99 source: "live",
100 }));
101 case "providerStatuses":
102 return Option.map(current, (projection) => ({
103 config: {
104 ...projection.config,
105 providers: reuseUnchanged(projection.config.providers, event.payload.providers),
106 },
107 latestEvent: event,
108 source: "live",
109 }));
110 case "settingsUpdated":
111 return Option.map(current, (projection) => ({
112 config: {
113 ...projection.config,
114 settings: event.payload.settings,
115 },
116 latestEvent: event,
117 source: "live",
118 }));
119 case "environmentThemesUpdated":
120 return Option.map(current, (projection) => ({
121 config: {
122 ...projection.config,
123 environmentThemes: event.payload.themes.length > 0 ? event.payload.themes : undefined,
124 },
125 latestEvent: event,
126 source: "live",
127 }));
128 case "usageLimitSourcesUpdated":
129 return Option.map(current, (projection) => ({
130 config: {
131 ...projection.config,
132 usageLimitSources: event.payload.sources.length > 0 ? event.payload.sources : undefined,
133 },
134 latestEvent: event,
135 source: "live",
136 }));
137 }
138}