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

ProviderInstanceRegistryHydration.ts

125 lines4.9 KB

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 // Probe project-enabled instances even if disabled machine-wide; project threads can still route to them.
49 for (const overrides of Object.values(settings.projectSettingsOverrides)) {
50 for (const [rawInstanceId, projectOverride] of Object.entries(
51 overrides?.providerInstances ?? {},
52 )) {
53 if (projectOverride.enabled !== true) continue;
54 const instanceId = rawInstanceId as ProviderInstanceId;
55 const existing = merged[instanceId];
56 if (existing === undefined || resolveProviderInstanceEnabled(existing)) continue;
57 merged[instanceId] = {
58 ...existing,
59 enabled: true,
60 ...(providerInstanceConfigEnabledFlag(existing.config) === false
61 ? { config: { ...(existing.config as Record<string, unknown>), enabled: true } }
62 : {}),
63 };
64 }
65 }
66​
67 return merged as ProviderInstanceConfigMap;
68};
69​
70const layerSettingsWatcher = (settingsChanges: Stream.Stream<ServerSettings>) =>
71 Layer.effectDiscard(
72 Effect.gen(function* () {
73 const mutator = yield* ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutator;
74 const serverSettings = yield* Settings.ServerSettingsService;
75 yield* settingsChanges.pipe(
76 Stream.runForEach(() =>
77 // The subscription predates the seed read, so a queued change can be
78 // older than the seed. A failed current read must leave the registry intact.
79 serverSettings.getSettings.pipe(
80 Effect.flatMap((current) =>
81 mutator.reconcile(deriveProviderInstanceConfigMap(current)),
82 ),
83 Effect.catchCause((cause) =>
84 Effect.logError("ProviderInstanceRegistry reconcile failed", cause),
85 ),
86 ),
87 ),
88 Effect.forkScoped,
89 );
90 }),
91 );
92​
93export const layer: Layer.Layer<
94 ProviderInstanceRegistry.ProviderInstanceRegistry,
95 never,
96 ProviderInstanceRegistryHydrationEnv
97> = Layer.unwrap(
98 Effect.gen(function* () {
99 const serverSettings = yield* Settings.ServerSettingsService;
100 // Subscribe before the seed read so saves during registry construction are queued.
101 const settingsChanges = yield* serverSettings.subscribeChanges;
102 const initialSettings: ServerSettings | undefined = yield* serverSettings.getSettings.pipe(
103 Effect.orElseSucceed(() => undefined),
104 );
105 const initialConfigMap =
106 initialSettings === undefined
107 ? ({} as ProviderInstanceConfigMap)
108 : deriveProviderInstanceConfigMap(initialSettings);
109​
110 const layerMutable = ProviderInstanceRegistry.layer({
111 drivers: BUILT_IN_DRIVERS,
112 configMap: initialConfigMap,
113 }).pipe(
114 Layer.provide(ProviderOrchestrationAdapterInfrastructure.layer),
115 Layer.provide(AcpRegistrySupport.layerFromHost),
116 Layer.provide(ProviderHostLive.layer),
117 );
118​
119 return layerSettingsWatcher(settingsChanges).pipe(Layer.provideMerge(layerMutable));
120 }),
121) as Layer.Layer<
122 ProviderInstanceRegistry.ProviderInstanceRegistry,
123 never,
124 ProviderInstanceRegistryHydrationEnv
125>;