Open History to see the last commit
1import {
2 defaultInstanceIdForDriver,
3 providerInstanceConfigEnabledFlag,
4 resolveProviderInstanceEnabled,
5 type ProviderInstanceConfig,
6 type ProviderInstanceConfigMap,
7 type ProviderInstanceId,
8 ServerSettings,
9} from "@t3tools/contracts";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Stream from "effect/Stream";
13
14import * as Settings from "../serverSettings.ts";
15import { BUILT_IN_DRIVERS, type BuiltInDriversEnv } from "./builtInDrivers.ts";
16import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
17import * as ProviderInstanceRegistryMutator from "./ProviderInstanceRegistryMutator.ts";
18import * as ProviderOrchestrationAdapterInfrastructure from "./ProviderOrchestrationAdapterInfrastructure.ts";
19import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRegistrySupport";
20import * as ProviderHostLive from "./ProviderHostLive.ts";
21import type * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
22import type * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
23import type * as ServerConfig from "../config.ts";
24
25type ProviderInstanceRegistryHydrationEnv =
26 | Exclude<
27 BuiltInDriversEnv,
28 | ProviderOrchestrationAdapterInfrastructure.ProviderOrchestrationAdapterInfrastructure
29 | AcpRegistrySupport.AcpRegistryCatalog
30 | ProviderHost.ProviderHost
31 >
32 | Settings.ServerSettingsService
33 | BackgroundPolicy.BackgroundPolicy
34 | ServerConfig.ServerConfig;
35
36export const deriveProviderInstanceConfigMap = (
37 settings: ServerSettings,
38): ProviderInstanceConfigMap => {
39 const merged: Record<string, ProviderInstanceConfig> = { ...settings.providerInstances };
40
41 for (const driver of BUILT_IN_DRIVERS) {
42 if (driver.metadata.hasDefaultInstance === false) continue;
43 const instanceId = defaultInstanceIdForDriver(driver.driverKind);
44 if (instanceId in merged) continue;
45 merged[instanceId] = { driver: driver.driverKind };
46 }
47
48 // A project that turns an instance on needs it alive and probed even when the machine itself
49 // has it off, or a thread in that project would route to an instance nobody ever checked in
50 // on.
51 for (const overrides of Object.values(settings.projectSettingsOverrides)) {
52 for (const [rawInstanceId, projectOverride] of Object.entries(
53 overrides?.providerInstances ?? {},
54 )) {
55 if (projectOverride.enabled !== true) continue;
56 const instanceId = rawInstanceId as ProviderInstanceId;
57 const existing = merged[instanceId];
58 if (existing === undefined || resolveProviderInstanceEnabled(existing)) continue;
59 merged[instanceId] = {
60 ...existing,
61 enabled: true,
62 ...(providerInstanceConfigEnabledFlag(existing.config) === false
63 ? { config: { ...(existing.config as Record<string, unknown>), enabled: true } }
64 : {}),
65 };
66 }
67 }
68
69 return merged as ProviderInstanceConfigMap;
70};
71
72const layerSettingsWatcher = (settingsChanges: Stream.Stream<ServerSettings>) =>
73 Layer.effectDiscard(
74 Effect.gen(function* () {
75 const mutator = yield* ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutator;
76 const serverSettings = yield* Settings.ServerSettingsService;
77 yield* settingsChanges.pipe(
78 Stream.runForEach(() =>
79 // The subscription predates the seed read, so a queued change can be
80 // older than the seed. A failed current read must leave the registry intact.
81 serverSettings.getSettings.pipe(
82 Effect.flatMap((current) =>
83 mutator.reconcile(deriveProviderInstanceConfigMap(current)),
84 ),
85 Effect.catchCause((cause) =>
86 Effect.logError("ProviderInstanceRegistry reconcile failed", cause),
87 ),
88 ),
89 ),
90 Effect.forkScoped,
91 );
92 }),
93 );
94
95export const layer: Layer.Layer<
96 ProviderInstanceRegistry.ProviderInstanceRegistry,
97 never,
98 ProviderInstanceRegistryHydrationEnv
99> = Layer.unwrap(
100 Effect.gen(function* () {
101 const serverSettings = yield* Settings.ServerSettingsService;
102 // Subscribe before the seed read so saves during registry construction are queued.
103 const settingsChanges = yield* serverSettings.subscribeChanges;
104 const initialSettings: ServerSettings | undefined = yield* serverSettings.getSettings.pipe(
105 Effect.orElseSucceed(() => undefined),
106 );
107 const initialConfigMap =
108 initialSettings === undefined
109 ? ({} as ProviderInstanceConfigMap)
110 : deriveProviderInstanceConfigMap(initialSettings);
111
112 const layerMutable = ProviderInstanceRegistry.layer({
113 drivers: BUILT_IN_DRIVERS,
114 configMap: initialConfigMap,
115 }).pipe(
116 Layer.provide(ProviderOrchestrationAdapterInfrastructure.layer),
117 Layer.provide(AcpRegistrySupport.layerFromHost),
118 Layer.provide(ProviderHostLive.layer),
119 );
120
121 return layerSettingsWatcher(settingsChanges).pipe(Layer.provideMerge(layerMutable));
122 }),
123) as Layer.Layer<
124 ProviderInstanceRegistry.ProviderInstanceRegistry,
125 never,
126 ProviderInstanceRegistryHydrationEnv
127>;