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