Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 type EditorId,
4 type ServerConfig,
5 type ServerConfigStreamEvent,
6 type ServerLifecycleLegacyThreadMigrationPayload,
7 type ServerLifecycleWelcomePayload,
8 type ServerProvider,
9 type ServerSettings,
10} from "@t3tools/contracts";
11import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
12import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
13import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
14import * as Option from "effect/Option";
15import { AsyncResult, Atom } from "effect/unstable/reactivity";
16
17import { environmentCatalog } from "../connection/catalog";
18import { connectionAtomRuntime } from "../connection/runtime";
19import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
20import { environmentSession } from "./session";
21
22export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
23 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
24});
25export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
26 catalogValueAtom: environmentCatalog.catalogValueAtom,
27 serverConfigValueAtom: serverEnvironment.configValueAtom,
28});
29
30interface PrimaryServerState {
31 readonly config: ServerConfig | null;
32 readonly latestEvent: ServerConfigStreamEvent | null;
33 readonly welcome: ServerLifecycleWelcomePayload | null;
34}
35
36const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
37const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
38const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
39 config: null,
40 latestEvent: null,
41 welcome: null,
42};
43
44export const primaryServerStateAtom = Atom.make((get): PrimaryServerState => {
45 const environmentId = get(primaryEnvironmentIdAtom);
46 if (environmentId === null) {
47 return EMPTY_PRIMARY_SERVER_STATE;
48 }
49
50 const target = { environmentId, input: {} };
51 const configProjection = Option.getOrNull(
52 AsyncResult.value(get(serverEnvironment.configProjection(target))),
53 );
54 const welcome = Option.getOrNull(AsyncResult.value(get(serverEnvironment.welcome(target))));
55
56 return {
57 config: get(serverEnvironment.configValueAtom(environmentId)),
58 latestEvent: configProjection?.latestEvent ?? null,
59 welcome,
60 };
61}).pipe(Atom.withLabel("web-primary-server-state"));
62
63export const primaryServerConfigAtom = Atom.make(
64 (get): ServerConfig | null => get(primaryServerStateAtom).config,
65).pipe(Atom.withLabel("web-primary-server-config"));
66
67export const primaryServerConfigEventAtom = Atom.make(
68 (get): ServerConfigStreamEvent | null => get(primaryServerStateAtom).latestEvent,
69).pipe(Atom.withLabel("web-primary-server-config-event"));
70
71export const primaryServerWelcomeAtom = Atom.make(
72 (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome,
73).pipe(Atom.withLabel("web-primary-server-welcome"));
74
75export const primaryServerLegacyThreadMigrationAtom = Atom.make(
76 (get): ServerLifecycleLegacyThreadMigrationPayload | null => {
77 const environmentId = get(primaryEnvironmentIdAtom);
78 if (environmentId === null) {
79 return null;
80 }
81 return Option.getOrNull(
82 AsyncResult.value(get(serverEnvironment.legacyThreadMigration({ environmentId, input: {} }))),
83 );
84 },
85).pipe(Atom.withLabel("web-primary-server-legacy-thread-migration"));
86
87export const primaryServerSettingsAtom = Atom.make(
88 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
89).pipe(Atom.withLabel("web-primary-server-settings"));
90
91export const primaryServerProvidersAtom = Atom.make(
92 (get): ReadonlyArray<ServerProvider> =>
93 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
94).pipe(Atom.withLabel("web-primary-server-providers"));
95
96export const primaryServerKeybindingsAtom = Atom.make(
97 (get): ServerConfig["keybindings"] =>
98 get(primaryServerConfigAtom)?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS,
99).pipe(Atom.withLabel("web-primary-server-keybindings"));
100
101export const primaryServerAvailableEditorsAtom = Atom.make(
102 (get): ReadonlyArray<EditorId> =>
103 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
104).pipe(Atom.withLabel("web-primary-server-available-editors"));
105
106export const primaryServerKeybindingsConfigPathAtom = Atom.make(
107 (get): string | null => get(primaryServerConfigAtom)?.keybindingsConfigPath ?? null,
108).pipe(Atom.withLabel("web-primary-server-keybindings-config-path"));
109
110export const primaryServerObservabilityAtom = Atom.make(
111 (get): ServerConfig["observability"] | null =>
112 get(primaryServerConfigAtom)?.observability ?? null,
113).pipe(Atom.withLabel("web-primary-server-observability"));