Open History to see the last commit
1import {
2 type EnvironmentId,
3 type ServerConfig,
4 type ServerConfigStreamEvent,
5 type ServerLifecycleWelcomePayload,
6 WS_METHODS,
7} from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import * as Stream from "effect/Stream";
10import { AsyncResult, Atom } from "effect/unstable/reactivity";
11
12import {
13 createAtomCommandScheduler,
14 createEnvironmentRpcCommand,
15 createEnvironmentRpcQueryAtomFamily,
16 createEnvironmentRpcSubscriptionAtomFamily,
17} from "./runtime.ts";
18import type { EnvironmentRegistry } from "../connection/registry.ts";
19
20export interface ServerConfigProjection {
21 readonly config: ServerConfig;
22 readonly latestEvent: ServerConfigStreamEvent;
23}
24
25export function applyServerConfigProjection(
26 current: Option.Option<ServerConfigProjection>,
27 event: ServerConfigStreamEvent,
28): Option.Option<ServerConfigProjection> {
29 switch (event.type) {
30 case "snapshot":
31 return Option.some({
32 config: event.config,
33 latestEvent: event,
34 });
35 case "keybindingsUpdated":
36 return Option.map(current, (projection) => ({
37 config: {
38 ...projection.config,
39 keybindings: event.payload.keybindings,
40 issues: event.payload.issues,
41 },
42 latestEvent: event,
43 }));
44 case "providerStatuses":
45 return Option.map(current, (projection) => ({
46 config: {
47 ...projection.config,
48 providers: event.payload.providers,
49 },
50 latestEvent: event,
51 }));
52 case "settingsUpdated":
53 return Option.map(current, (projection) => ({
54 config: {
55 ...projection.config,
56 settings: event.payload.settings,
57 },
58 latestEvent: event,
59 }));
60 }
61}
62
63export function projectServerConfig(
64 current: Option.Option<ServerConfigProjection>,
65 event: ServerConfigStreamEvent,
66): readonly [Option.Option<ServerConfigProjection>, ReadonlyArray<ServerConfigProjection>] {
67 const next = applyServerConfigProjection(current, event);
68 return [next, Option.toArray(next)];
69}
70
71export function projectServerWelcome(
72 current: Option.Option<ServerLifecycleWelcomePayload>,
73 event: {
74 readonly type: "welcome" | "ready";
75 readonly payload: unknown;
76 },
77): readonly [
78 Option.Option<ServerLifecycleWelcomePayload>,
79 ReadonlyArray<ServerLifecycleWelcomePayload>,
80] {
81 if (event.type !== "welcome") {
82 return [current, []];
83 }
84 const welcome = event.payload as ServerLifecycleWelcomePayload;
85 return [Option.some(welcome), [welcome]];
86}
87
88export function createServerEnvironmentAtoms<R, E>(
89 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
90 options: {
91 readonly initialConfigValueAtom: (
92 environmentId: EnvironmentId,
93 ) => Atom.Atom<ServerConfig | null>;
94 },
95) {
96 const configScheduler = createAtomCommandScheduler();
97 const configConcurrency = {
98 mode: "serial" as const,
99 key: ({ environmentId }: { readonly environmentId: string }) => environmentId,
100 };
101 const configProjection = createEnvironmentRpcSubscriptionAtomFamily(runtime, {
102 label: "environment-data:server:config-projection",
103 tag: WS_METHODS.subscribeServerConfig,
104 transform: (stream) =>
105 stream.pipe(Stream.mapAccum(Option.none<ServerConfigProjection>, projectServerConfig)),
106 });
107 const emptyConfigAtom = Atom.make<ServerConfig | null>(null).pipe(
108 Atom.withLabel("environment-data:server:config:empty"),
109 );
110 const configValueAtom = Atom.family((environmentId: EnvironmentId | null) => {
111 if (environmentId === null) {
112 return emptyConfigAtom;
113 }
114 return Atom.make((get): ServerConfig | null => {
115 const projection = Option.getOrNull(
116 AsyncResult.value(get(configProjection({ environmentId, input: {} }))),
117 );
118 return projection?.config ?? get(options.initialConfigValueAtom(environmentId));
119 }).pipe(Atom.withLabel(`environment-data:server:config:${environmentId}`));
120 });
121 const settingsValueAtom = Atom.family((environmentId: EnvironmentId) =>
122 Atom.make((get) => get(configValueAtom(environmentId))?.settings ?? null).pipe(
123 Atom.withLabel(`environment-data:server:settings:${environmentId}`),
124 ),
125 );
126 const providersValueAtom = Atom.family((environmentId: EnvironmentId) =>
127 Atom.make((get) => get(configValueAtom(environmentId))?.providers ?? null).pipe(
128 Atom.withLabel(`environment-data:server:providers:${environmentId}`),
129 ),
130 );
131
132 return {
133 configValueAtom,
134 settingsValueAtom,
135 providersValueAtom,
136 traceDiagnostics: createEnvironmentRpcQueryAtomFamily(runtime, {
137 label: "environment-data:server:trace-diagnostics",
138 tag: WS_METHODS.serverGetTraceDiagnostics,
139 }),
140 processDiagnostics: createEnvironmentRpcQueryAtomFamily(runtime, {
141 label: "environment-data:server:process-diagnostics",
142 tag: WS_METHODS.serverGetProcessDiagnostics,
143 }),
144 processResourceHistory: createEnvironmentRpcQueryAtomFamily(runtime, {
145 label: "environment-data:server:process-resource-history",
146 tag: WS_METHODS.serverGetProcessResourceHistory,
147 }),
148 configProjection,
149 welcome: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
150 label: "environment-data:server:welcome",
151 tag: WS_METHODS.subscribeServerLifecycle,
152 transform: (stream) =>
153 stream.pipe(
154 Stream.mapAccum(Option.none<ServerLifecycleWelcomePayload>, projectServerWelcome),
155 ),
156 }),
157 refreshProviders: createEnvironmentRpcCommand(runtime, {
158 label: "environment-data:server:refresh-providers",
159 tag: WS_METHODS.serverRefreshProviders,
160 concurrency: {
161 mode: "singleFlight",
162 key: ({ environmentId }) => environmentId,
163 },
164 }),
165 updateProvider: createEnvironmentRpcCommand(runtime, {
166 label: "environment-data:server:update-provider",
167 tag: WS_METHODS.serverUpdateProvider,
168 scheduler: configScheduler,
169 concurrency: configConcurrency,
170 }),
171 upsertKeybinding: createEnvironmentRpcCommand(runtime, {
172 label: "environment-data:server:upsert-keybinding",
173 tag: WS_METHODS.serverUpsertKeybinding,
174 scheduler: configScheduler,
175 concurrency: configConcurrency,
176 }),
177 removeKeybinding: createEnvironmentRpcCommand(runtime, {
178 label: "environment-data:server:remove-keybinding",
179 tag: WS_METHODS.serverRemoveKeybinding,
180 scheduler: configScheduler,
181 concurrency: configConcurrency,
182 }),
183 updateSettings: createEnvironmentRpcCommand(runtime, {
184 label: "environment-data:server:update-settings",
185 tag: WS_METHODS.serverUpdateSettings,
186 scheduler: configScheduler,
187 concurrency: configConcurrency,
188 }),
189 signalProcess: createEnvironmentRpcCommand(runtime, {
190 label: "environment-data:server:signal-process",
191 tag: WS_METHODS.serverSignalProcess,
192 }),
193 };
194}