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 * Cached config keeps the provider and model catalog available across reconnects.
12 * Published themes are current machine state, so a cache could restore themes
13 * that the machine no longer publishes. Replay sends themes as a separate event.
14 */
15export function withoutEnvironmentThemes(config: ServerConfig): ServerConfig {
16 if (config.environmentThemes === undefined) return config;
17 const { environmentThemes: _ephemeral, ...rest } = config;
18 return rest;
19}
20
21export function applyServerConfigProjection(
22 current: Option.Option<ServerConfigProjection>,
23 event: ServerConfigStreamEvent,
24): Option.Option<ServerConfigProjection> {
25 switch (event.type) {
26 case "snapshot": {
27 // Wire snapshots never contain published themes. Keep the previous set
28 // until a capable server sends its authoritative theme event. A legacy
29 // server cannot send a later removal, so a downgrade must clear the set.
30 const carried =
31 event.config.environment.capabilities.environmentThemes === true && Option.isSome(current)
32 ? current.value.config.environmentThemes
33 : undefined;
34 return Option.some({
35 config:
36 carried === undefined ? event.config : { ...event.config, environmentThemes: carried },
37 latestEvent: event,
38 source: "live" as const,
39 });
40 }
41 case "keybindingsUpdated":
42 return Option.map(current, (projection) => ({
43 config: {
44 ...projection.config,
45 keybindings: event.payload.keybindings,
46 issues: event.payload.issues,
47 },
48 latestEvent: event,
49 source: "live",
50 }));
51 case "providerStatuses":
52 return Option.map(current, (projection) => ({
53 config: {
54 ...projection.config,
55 providers: event.payload.providers,
56 },
57 latestEvent: event,
58 source: "live",
59 }));
60 case "settingsUpdated":
61 return Option.map(current, (projection) => ({
62 config: {
63 ...projection.config,
64 settings: event.payload.settings,
65 },
66 latestEvent: event,
67 source: "live",
68 }));
69 case "environmentThemesUpdated":
70 return Option.map(current, (projection) => ({
71 config: {
72 ...projection.config,
73 environmentThemes: event.payload.themes.length > 0 ? event.payload.themes : undefined,
74 },
75 latestEvent: event,
76 source: "live",
77 }));
78 }
79}