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

ProviderAdapterRegistry.ts

100 lines4.2 KB

1/**
2 * ProviderAdapterRegistry - Lookup boundary for provider adapter implementations.
3 *
4 * Maps a `ProviderInstanceId` (the new per-instance routing key) or a
5 * `ProviderDriverKind` (legacy single-instance-per-driver key) to the concrete
6 * adapter service (Codex, Claude, etc). It does not own session lifecycle
7 * or routing rules; `ProviderService` uses this registry together with
8 * `ProviderSessionDirectory`.
9 *
10 * During the driver/instance migration this tag exposes both flavours:
11 *
12 * - `getByInstance` / `listInstances` — new per-instance routing. Callers
13 * that already know an `instanceId` (threads, sessions, events)
14 * should prefer these.
15 * (`defaultInstanceIdForDriver(kind) === kind`), matching the pre-Slice-D
16 * behaviour. New code should not grow additional callers of the kind-keyed
17 * methods; they exist so the settings UI, WS refresh RPC, and a handful
18 * of legacy persisted rows can still be routed during the rollout.
19 *
20 * @module ProviderAdapterRegistry
21 */
22import type { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
23import * as Context from "effect/Context";
24import type * as Effect from "effect/Effect";
25import type * as PubSub from "effect/PubSub";
26import type * as Scope from "effect/Scope";
27import type * as Stream from "effect/Stream";
28​
29import type { ProviderAdapterError, ProviderUnsupportedError } from "../Errors.ts";
30import type { ProviderAdapterShape } from "./ProviderAdapter.ts";
31import type { ProviderContinuationIdentity } from "../ProviderDriver.ts";
32​
33export interface ProviderInstanceRoutingInfo {
34 readonly instanceId: ProviderInstanceId;
35 readonly driverKind: ProviderDriverKind;
36 readonly displayName: string | undefined;
37 readonly accentColor?: string | undefined;
38 readonly enabled: boolean;
39 readonly continuationIdentity: ProviderContinuationIdentity;
41​
42/**
43 * ProviderAdapterRegistryShape - Service API for adapter lookup.
44 */
45export interface ProviderAdapterRegistryShape {
46 /**
47 * Resolve the adapter for a specific instance id. Returns
48 * `ProviderUnsupportedError` if no such instance is currently registered
49 * (which covers "never configured" *and* "configured but the driver is
50 * unavailable in this build" — both surface the same failure to callers
51 * that expect a working adapter).
52 */
53 readonly getByInstance: (
54 instanceId: ProviderInstanceId,
55 ) => Effect.Effect<ProviderAdapterShape<ProviderAdapterError>, ProviderUnsupportedError>;
56​
57 readonly getInstanceInfo: (
58 instanceId: ProviderInstanceId,
59 ) => Effect.Effect<ProviderInstanceRoutingInfo, ProviderUnsupportedError>;
60​
61 /**
62 * List all live instance ids. Excludes unavailable/shadow instances —
63 * callers of this method want something they can pass to `getByInstance`.
64 */
65 readonly listInstances: () => Effect.Effect<ReadonlyArray<ProviderInstanceId>>;
66​
67 /**
68 * Legacy: list provider kinds whose default instance is currently
69 * registered.
70 *
71 * @deprecated Prefer `listInstances`. Retained for migration-era call
72 * sites that iterate providers to build UI/metrics.
73 */
74 readonly listProviders: () => Effect.Effect<ReadonlyArray<ProviderDriverKind>>;
75​
76 /**
77 * Change notification stream mirroring `ProviderInstanceRegistry.streamChanges`.
78 * Emits one `void` tick whenever the set of live instances changes
79 * (instance added, removed, or rebuilt after a settings edit). Consumers
80 * that fan out `adapter.streamEvents` per instance — e.g. `ProviderService`'s
81 * runtime event bus — re-pull `listInstances` on each tick and fork new
82 * subscriptions for instances they haven't seen yet.
83 */
84 readonly streamChanges: Stream.Stream<void>;
85​
86 /**
87 * Acquire a change subscription synchronously in the caller's current fiber.
88 * Consumers that must avoid missing a publish between initial reconciliation
89 * and watcher startup should use this, then fork `Stream.fromSubscription`.
90 */
91 readonly subscribeChanges: Effect.Effect<PubSub.Subscription<void>, never, Scope.Scope>;
93​
94/**
95 * ProviderAdapterRegistry - Service tag for provider adapter lookup.
96 */
97export class ProviderAdapterRegistry extends Context.Service<
98 ProviderAdapterRegistry,
99 ProviderAdapterRegistryShape
100>()("t3/provider/Services/ProviderAdapterRegistry") {}