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

serverConfigProjection.ts

134 lines4.7 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 * 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;
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 the server sends its authoritative theme event.
69 const carriedThemes = Option.isSome(current)
70 ? current.value.config.environmentThemes
71 : undefined;
72 const carriedSources = Option.isSome(current)
73 ? current.value.config.usageLimitSources
74 : undefined;
75 const config: ServerConfig = {
76 ...event.config,
77 ...(carriedThemes === undefined ? {} : { environmentThemes: carriedThemes }),
78 ...(carriedSources === undefined ? {} : { usageLimitSources: carriedSources }),
79 };
80 return Option.some({
81 // Every reconnect re-sends the whole config, usually unchanged apart from timestamps.
82 config: Option.isSome(current) ? reuseUnchanged(current.value.config, config) : config,
83 latestEvent: event,
84 source: "live" as const,
85 });
86 }
87 case "keybindingsUpdated":
88 return Option.map(current, (projection) => ({
89 config: {
90 ...projection.config,
91 keybindings: event.payload.keybindings,
92 issues: event.payload.issues,
93 },
94 latestEvent: event,
95 source: "live",
96 }));
97 case "providerStatuses":
98 return Option.map(current, (projection) => ({
99 config: {
100 ...projection.config,
101 providers: reuseUnchanged(projection.config.providers, event.payload.providers),
102 },
103 latestEvent: event,
104 source: "live",
105 }));
106 case "settingsUpdated":
107 return Option.map(current, (projection) => ({
108 config: {
109 ...projection.config,
110 settings: reuseUnchanged(projection.config.settings, event.payload.settings),
111 },
112 latestEvent: event,
113 source: "live",
114 }));
115 case "environmentThemesUpdated":
116 return Option.map(current, (projection) => ({
117 config: {
118 ...projection.config,
119 environmentThemes: event.payload.themes.length > 0 ? event.payload.themes : undefined,
120 },
121 latestEvent: event,
122 source: "live",
123 }));
124 case "usageLimitSourcesUpdated":
125 return Option.map(current, (projection) => ({
126 config: {
127 ...projection.config,
128 usageLimitSources: event.payload.sources.length > 0 ? event.payload.sources : undefined,
129 },
130 latestEvent: event,
131 source: "live",
132 }));
133 }