Open History to see the last commit
1/**
2 * ProviderAdapterRegistryLive — facade over `ProviderInstanceRegistry`.
3 *
4 * `ProviderAdapterRegistry` historically mapped one `ProviderDriverKind` to one
5 * adapter via the four `<X>AdapterLive` singleton Layers. The per-instance
6 * refactor moved adapter construction inside each `ProviderDriver.create()`:
7 * adapters are now bundled on the `ProviderInstance` that the
8 * `ProviderInstanceRegistry` owns.
9 *
10 * This facade fulfills the `ProviderAdapterRegistryShape` contract by doing
11 * dynamic look-ups against `ProviderInstanceRegistry` on every call. That
12 * means settings-driven hot-reload shows up here automatically — adding a
13 * new instance via settings makes `getByInstance` resolve immediately
14 * without rebuilding the facade.
15 *
16 * @module ProviderAdapterRegistryLive
17 */
18import { ProviderInstanceId } from "@t3tools/contracts";
19import * as Effect from "effect/Effect";
20import * as Layer from "effect/Layer";
21
22import { ProviderUnsupportedError } from "../Errors.ts";
23import { ProviderInstanceRegistry } from "../Services/ProviderInstanceRegistry.ts";
24import {
25 ProviderAdapterRegistry,
26 type ProviderAdapterRegistryShape,
27} from "../Services/ProviderAdapterRegistry.ts";
28
29const makeProviderAdapterRegistry = Effect.fn("makeProviderAdapterRegistry")(function* () {
30 const registry = yield* ProviderInstanceRegistry;
31
32 const getByInstance: ProviderAdapterRegistryShape["getByInstance"] = (instanceId) =>
33 registry.getInstance(instanceId).pipe(
34 Effect.flatMap((instance) =>
35 instance === undefined
36 ? Effect.fail(
37 new ProviderUnsupportedError({
38 provider: instanceId,
39 }),
40 )
41 : Effect.succeed(instance.adapter),
42 ),
43 );
44
45 const getInstanceInfo: ProviderAdapterRegistryShape["getInstanceInfo"] = (instanceId) =>
46 registry.getInstance(instanceId).pipe(
47 Effect.flatMap((instance) =>
48 instance === undefined
49 ? Effect.fail(
50 new ProviderUnsupportedError({
51 provider: instanceId,
52 }),
53 )
54 : Effect.succeed({
55 instanceId: instance.instanceId,
56 driverKind: instance.driverKind,
57 displayName: instance.displayName,
58 accentColor: instance.accentColor,
59 enabled: instance.enabled,
60 continuationIdentity: instance.continuationIdentity,
61 }),
62 ),
63 );
64
65 const listInstances: ProviderAdapterRegistryShape["listInstances"] = () =>
66 registry.listInstances.pipe(
67 Effect.map((instances) => instances.map((instance) => instance.instanceId)),
68 );
69
70 return {
71 getByInstance,
72 getInstanceInfo,
73 listInstances,
74 subscribeChanges: registry.subscribeChanges,
75 } satisfies ProviderAdapterRegistryShape;
76});
77
78export const ProviderAdapterRegistryLive = Layer.effect(
79 ProviderAdapterRegistry,
80 makeProviderAdapterRegistry(),
81);
82
83// Re-export for consumers (including tests) that construct a
84// `ProviderInstanceId` before calling `getByInstance`.
85export { ProviderInstanceId };