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 * Provider pushes, reconnects, and settings writes resend unchanged data.
24 * Reuse deep-equal objects so memoized UI can skip unchanged providers, models, and skills.
25 */
26export function reuseUnchanged<T>(previous: unknown, next: T): T {
27 if (Object.is(previous, next)) return next;
28 if (
29 typeof previous !== "object" ||
30 typeof next !== "object" ||
31 previous === null ||
32 next === null ||
33 Array.isArray(previous) !== Array.isArray(next)
34 ) {
35 return next;
36 }
37 if (Array.isArray(next)) {
38 const before = previous as ReadonlyArray<unknown>;
39 let same = before.length === next.length;
40 const merged = next.map((item, index) => {
41 const reused = reuseUnchanged(before[index], item);
42 if (reused !== before[index]) same = false;
43 return reused;
44 });
45 return (same ? previous : merged) as T;
46 }
47 if (Object.getPrototypeOf(next) !== Object.prototype) return next;
48 const before = previous as Record<string, unknown>;
49 const after = next as Record<string, unknown>;
50 const keys = Object.keys(after);
51 let same = keys.length === Object.keys(before).length;
52 const merged: Record<string, unknown> = {};
53 for (const key of keys) {
54 const reused = reuseUnchanged(before[key], after[key]);
55 if (reused !== before[key] || !(key in before)) same = false;
56 merged[key] = reused;
57 }
58 return (same ? previous : merged) as T;
59}
60
61export function applyServerConfigProjection(
62 current: Option.Option<ServerConfigProjection>,
63 event: ServerConfigStreamEvent,
64): Option.Option<ServerConfigProjection> {
65 switch (event.type) {
66 case "snapshot": {
67 // Wire snapshots never contain published themes. Keep the previous set
68 // until a capable server sends its authoritative theme event. A legacy
69 // server cannot send a later removal, so a downgrade must clear the set.
70 const capabilities = event.config.environment.capabilities;
71 const carriedThemes =
72 capabilities.environmentThemes === true && Option.isSome(current)
73 ? current.value.config.environmentThemes
74 : undefined;
75 const carriedSources =
76 capabilities.usageLimitSources === true && Option.isSome(current)
77 ? current.value.config.usageLimitSources
78 : undefined;
79 const config: ServerConfig = {
80 ...event.config,
81 ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),
82 ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),
83 };
84 return Option.some({
85 // Every reconnect re-sends the whole config, usually unchanged apart from timestamps.
86 config: Option.isSome(current) ? reuseUnchanged(current.value.config, config) : config,
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: reuseUnchanged(projection.config.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}