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

ProviderRegistry.ts

96 lines3.4 KB

1/**
2 * ProviderRegistryLive - Aggregates provider-specific snapshot services.
3 *
4 * @module ProviderRegistryLive
5 */
6import type { ProviderKind, ServerProvider } from "@t3tools/contracts";
7import { Effect, Equal, Layer, PubSub, Ref, Stream } from "effect";
8​
9import { ClaudeProviderLive } from "./ClaudeProvider";
10import { CodexProviderLive } from "./CodexProvider";
11import type { ClaudeProviderShape } from "../Services/ClaudeProvider";
12import { ClaudeProvider } from "../Services/ClaudeProvider";
13import type { CodexProviderShape } from "../Services/CodexProvider";
14import { CodexProvider } from "../Services/CodexProvider";
15import { ProviderRegistry, type ProviderRegistryShape } from "../Services/ProviderRegistry";
16​
17const loadProviders = (
18 codexProvider: CodexProviderShape,
19 claudeProvider: ClaudeProviderShape,
20): Effect.Effect<readonly [ServerProvider, ServerProvider]> =>
21 Effect.all([codexProvider.getSnapshot, claudeProvider.getSnapshot], {
22 concurrency: "unbounded",
23 });
24​
25export const haveProvidersChanged = (
26 previousProviders: ReadonlyArray<ServerProvider>,
27 nextProviders: ReadonlyArray<ServerProvider>,
28): boolean => !Equal.equals(previousProviders, nextProviders);
29​
30export const ProviderRegistryLive = Layer.effect(
31 ProviderRegistry,
32 Effect.gen(function* () {
33 const codexProvider = yield* CodexProvider;
34 const claudeProvider = yield* ClaudeProvider;
35 const changesPubSub = yield* Effect.acquireRelease(
36 PubSub.unbounded<ReadonlyArray<ServerProvider>>(),
37 PubSub.shutdown,
38 );
39 const providersRef = yield* Ref.make<ReadonlyArray<ServerProvider>>(
40 yield* loadProviders(codexProvider, claudeProvider),
41 );
42​
43 const syncProviders = Effect.fn("syncProviders")(function* (options?: {
44 readonly publish?: boolean;
45 }) {
46 const previousProviders = yield* Ref.get(providersRef);
47 const providers = yield* loadProviders(codexProvider, claudeProvider);
48 yield* Ref.set(providersRef, providers);
49​
50 if (options?.publish !== false && haveProvidersChanged(previousProviders, providers)) {
51 yield* PubSub.publish(changesPubSub, providers);
52 }
53​
54 return providers;
55 });
56​
57 yield* Stream.runForEach(codexProvider.streamChanges, () => syncProviders()).pipe(
58 Effect.forkScoped,
59 );
60 yield* Stream.runForEach(claudeProvider.streamChanges, () => syncProviders()).pipe(
61 Effect.forkScoped,
62 );
63​
64 const refresh = Effect.fn("refresh")(function* (provider?: ProviderKind) {
65 switch (provider) {
66 case "codex":
67 yield* codexProvider.refresh;
68 break;
69 case "claudeAgent":
70 yield* claudeProvider.refresh;
71 break;
72 default:
73 yield* Effect.all([codexProvider.refresh, claudeProvider.refresh], {
74 concurrency: "unbounded",
75 });
76 break;
77 }
78 return yield* syncProviders();
79 });
80​
81 return {
82 getProviders: syncProviders({ publish: false }).pipe(
83 Effect.tapError(Effect.logError),
84 Effect.orElseSucceed(() => []),
85 ),
86 refresh: (provider?: ProviderKind) =>
87 refresh(provider).pipe(
88 Effect.tapError(Effect.logError),
89 Effect.orElseSucceed(() => []),
90 ),
91 get streamChanges() {
92 return Stream.fromPubSub(changesPubSub);
93 },
94 } satisfies ProviderRegistryShape;
95 }),
96).pipe(Layer.provideMerge(CodexProviderLive), Layer.provideMerge(ClaudeProviderLive));