Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

serverConfigProjection.ts

153 lines5.6 KB

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;
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;
64​
65export function applyServerConfigProjection(
66 current: Option.Option<ServerConfigProjection>,
67 event: ServerConfigStreamEvent,
68): Option.Option<ServerConfigProjection> {
69 switch (event.type) {
70 // This client requests full events, so unsolicited delta acknowledgements carry no state.
71 case "configUnchanged":
72 case "configDelta":
73 return current;
74 case "snapshot": {
75 // Wire snapshots never contain published themes. Keep the previous set
76 // until a capable server sends its authoritative theme event. A legacy
77 // server cannot send a later removal, so a downgrade must clear the set.
78 const capabilities = event.config.environment.capabilities;
79 const carriedThemes =
80 capabilities.environmentThemes === true && Option.isSome(current)
81 ? current.value.config.environmentThemes
82 : undefined;
83 const carriedSources =
84 capabilities.usageLimitSources === true && Option.isSome(current)
85 ? current.value.config.usageLimitSources
86 : undefined;
87 const config: ServerConfig = {
88 ...event.config,
89 ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),
90 ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),
91 };
92 return Option.some({
93 // Every reconnect re-sends the whole config, usually unchanged apart from timestamps.
94 config: Option.isSome(current) ? reuseUnchanged(current.value.config, config) : config,
95 latestEvent: event,
96 source: "live" as const,
97 });
98 }
99 case "keybindingsUpdated":
100 return Option.map(current, (projection) => ({
101 config: {
102 ...projection.config,
103 keybindings: event.payload.keybindings,
104 issues: event.payload.issues,
105 },
106 latestEvent: event,
107 source: "live",
108 }));
109 case "providerStatuses":
110 return Option.map(current, (projection) => ({
111 config: {
112 ...projection.config,
113 providers: reuseUnchanged(projection.config.providers, event.payload.providers),
114 },
115 latestEvent: event,
116 source: "live",
117 }));
118 case "settingsUpdated":
119 return Option.map(current, (projection) => ({
120 config: {
121 ...projection.config,
122 settings: reuseUnchanged(projection.config.settings, event.payload.settings),
123 },
124 latestEvent: event,
125 source: "live",
126 }));
127 // Match the cache's omitted empty sets to preserve config equality.
128 case "environmentThemesUpdated":
129 return Option.map(current, (projection) => {
130 const { environmentThemes: _previous, ...config } = projection.config;
131 return {
132 config:
133 event.payload.themes.length > 0
134 ? { ...config, environmentThemes: event.payload.themes }
135 : config,
136 latestEvent: event,
137 source: "live",
138 };
139 });
140 case "usageLimitSourcesUpdated":
141 return Option.map(current, (projection) => {
142 const { usageLimitSources: _previous, ...config } = projection.config;
143 return {
144 config:
145 event.payload.sources.length > 0
146 ? { ...config, usageLimitSources: event.payload.sources }
147 : config,
148 latestEvent: event,
149 source: "live",
150 };
151 });
152 }