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 * A reconnect snapshot re-sends the whole config the same way.
28 * A settings write re-sends the whole settings document for one changed field.
29 */
30export function reuseUnchanged<T>(previous: unknown, next: T): T {
31 if (Object.is(previous, next)) return next;
32 if (
33 typeof previous !== "object" ||
34 typeof next !== "object" ||
35 previous === null ||
36 next === null ||
37 Array.isArray(previous) !== Array.isArray(next)
38 ) {
39 return next;
40 }
41 if (Array.isArray(next)) {
42 const before = previous as ReadonlyArray<unknown>;
43 let same = before.length === next.length;
44 const merged = next.map((item, index) => {
45 const reused = reuseUnchanged(before[index], item);
46 if (reused !== before[index]) same = false;
47 return reused;
48 });
49 return (same ? previous : merged) as T;
50 }
51 if (Object.getPrototypeOf(next) !== Object.prototype) return next;
52 const before = previous as Record<string, unknown>;
53 const after = next as Record<string, unknown>;
54 const keys = Object.keys(after);
55 let same = keys.length === Object.keys(before).length;
56 const merged: Record<string, unknown> = {};
57 for (const key of keys) {
58 const reused = reuseUnchanged(before[key], after[key]);
59 if (reused !== before[key] || !(key in before)) same = false;
60 merged[key] = reused;
61 }
62 return (same ? previous : merged) as T;
63}
64
65export function applyServerConfigProjection(
66 current: Option.Option<ServerConfigProjection>,
67 event: ServerConfigStreamEvent,
68): Option.Option<ServerConfigProjection> {
69 switch (event.type) {
70 case "snapshot": {
71 // Wire snapshots never contain published themes. Keep the previous set
72 // until a capable server sends its authoritative theme event. A legacy
73 // server cannot send a later removal, so a downgrade must clear the set.
74 const capabilities = event.config.environment.capabilities;
75 const carriedThemes =
76 capabilities.environmentThemes === true && Option.isSome(current)
77 ? current.value.config.environmentThemes
78 : undefined;
79 const carriedSources =
80 capabilities.usageLimitSources === true && Option.isSome(current)
81 ? current.value.config.usageLimitSources
82 : undefined;
83 const config: ServerConfig = {
84 ...event.config,
85 ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),
86 ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),
87 };
88 return Option.some({
89 // Every reconnect re-sends the whole config, usually unchanged apart from timestamps.
90 config: Option.isSome(current) ? reuseUnchanged(current.value.config, config) : config,
91 latestEvent: event,
92 source: "live" as const,
93 });
94 }
95 case "keybindingsUpdated":
96 return Option.map(current, (projection) => ({
97 config: {
98 ...projection.config,
99 keybindings: event.payload.keybindings,
100 issues: event.payload.issues,
101 },
102 latestEvent: event,
103 source: "live",
104 }));
105 case "providerStatuses":
106 return Option.map(current, (projection) => ({
107 config: {
108 ...projection.config,
109 providers: reuseUnchanged(projection.config.providers, event.payload.providers),
110 },
111 latestEvent: event,
112 source: "live",
113 }));
114 case "settingsUpdated":
115 return Option.map(current, (projection) => ({
116 config: {
117 ...projection.config,
118 settings: reuseUnchanged(projection.config.settings, event.payload.settings),
119 },
120 latestEvent: event,
121 source: "live",
122 }));
123 case "environmentThemesUpdated":
124 return Option.map(current, (projection) => ({
125 config: {
126 ...projection.config,
127 environmentThemes: event.payload.themes.length > 0 ? event.payload.themes : undefined,
128 },
129 latestEvent: event,
130 source: "live",
131 }));
132 case "usageLimitSourcesUpdated":
133 return Option.map(current, (projection) => ({
134 config: {
135 ...projection.config,
136 usageLimitSources: event.payload.sources.length > 0 ? event.payload.sources : undefined,
137 },
138 latestEvent: event,
139 source: "live",
140 }));
141 }
142}