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

ProviderAdapterRegistry.ts

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