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

ProviderAdapterRegistry.ts

116 lines4.3 KB

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 {
19 defaultInstanceIdForDriver,
20 ProviderInstanceId,
21 type ProviderDriverKind,
22} from "@t3tools/contracts";
23import { Effect, Layer } from "effect";
24​
25import { ProviderUnsupportedError } from "../Errors.ts";
26import { ProviderInstanceRegistry } from "../Services/ProviderInstanceRegistry.ts";
27import {
28 ProviderAdapterRegistry,
29 type ProviderAdapterRegistryShape,
30} from "../Services/ProviderAdapterRegistry.ts";
31​
32const makeProviderAdapterRegistry = Effect.fn("makeProviderAdapterRegistry")(function* () {
33 const registry = yield* ProviderInstanceRegistry;
34​
35 const getByInstance: ProviderAdapterRegistryShape["getByInstance"] = (instanceId) =>
36 registry.getInstance(instanceId).pipe(
37 Effect.flatMap((instance) =>
38 instance === undefined
39 ? Effect.fail(
40 new ProviderUnsupportedError({
41 provider: instanceId,
42 }),
43 )
44 : Effect.succeed(instance.adapter),
45 ),
46 );
47​
48 const getInstanceInfo: ProviderAdapterRegistryShape["getInstanceInfo"] = (instanceId) =>
49 registry.getInstance(instanceId).pipe(
50 Effect.flatMap((instance) =>
51 instance === undefined
52 ? Effect.fail(
53 new ProviderUnsupportedError({
54 provider: instanceId,
55 }),
56 )
57 : Effect.succeed({
58 instanceId: instance.instanceId,
59 driverKind: instance.driverKind,
60 displayName: instance.displayName,
61 accentColor: instance.accentColor,
62 enabled: instance.enabled,
63 continuationIdentity: instance.continuationIdentity,
64 }),
65 ),
66 );
67​
68 const listInstances: ProviderAdapterRegistryShape["listInstances"] = () =>
69 registry.listInstances.pipe(
70 Effect.map((instances) => instances.map((instance) => instance.instanceId)),
71 );
72​
73 const listProviders: ProviderAdapterRegistryShape["listProviders"] = () =>
74 registry.listInstances.pipe(
75 Effect.map((instances) => {
76 const kinds = new Set<ProviderDriverKind>();
77 for (const instance of instances) {
78 const defaultId = defaultInstanceIdForDriver(instance.driverKind);
79 if (instance.instanceId === defaultId) {
80 // Only the default-instance rows show up through the legacy
81 // shim — custom instances like `codex_personal` have no
82 // `ProviderDriverKind` equivalent.
83 kinds.add(instance.driverKind);
84 }
85 }
86 return Array.from(kinds);
87 }),
88 );
89​
90 return {
91 getByInstance,
92 getInstanceInfo,
93 listInstances,
94 listProviders,
95 // Proxy directly — the facade has no state of its own; the instance
96 // registry already coalesces adds/removes/rebuilds into one emission.
97 streamChanges: registry.streamChanges,
98 subscribeChanges: registry.subscribeChanges,
99 } satisfies ProviderAdapterRegistryShape;
100});
101​
102export const ProviderAdapterRegistryLive = Layer.effect(
103 ProviderAdapterRegistry,
104 makeProviderAdapterRegistry(),
105);
106​
107// Exposed for tests that want to build a facade over a pre-assembled
108// `ProviderInstanceRegistry` without pulling in the whole boot graph.
109export { makeProviderAdapterRegistry };
110​
111// Re-export for consumers that need the accessor shape. The service tag
112// itself lives in `Services/ProviderAdapterRegistry.ts`.
113export { ProviderAdapterRegistry } from "../Services/ProviderAdapterRegistry.ts";
114// Re-export for consumers (including tests) that construct a
115// `ProviderInstanceId` before calling `getByInstance`.
116export { ProviderInstanceId };