Open History to see the last commit
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 { Context } from "effect";
24import type { Effect, PubSub, Scope, Stream } from "effect";
25
26import type { ProviderAdapterError, ProviderUnsupportedError } from "../Errors.ts";
27import type { ProviderAdapterShape } from "./ProviderAdapter.ts";
28import type { ProviderContinuationIdentity } from "../ProviderDriver.ts";
29
30export interface ProviderInstanceRoutingInfo {
31 readonly instanceId: ProviderInstanceId;
32 readonly driverKind: ProviderDriverKind;
33 readonly displayName: string | undefined;
34 readonly accentColor?: string | undefined;
35 readonly enabled: boolean;
36 readonly continuationIdentity: ProviderContinuationIdentity;
37}
38
39/**
40 * ProviderAdapterRegistryShape - Service API for adapter lookup.
41 */
42export interface ProviderAdapterRegistryShape {
43 /**
44 * Resolve the adapter for a specific instance id. Returns
45 * `ProviderUnsupportedError` if no such instance is currently registered
46 * (which covers "never configured" *and* "configured but the driver is
47 * unavailable in this build" — both surface the same failure to callers
48 * that expect a working adapter).
49 */
50 readonly getByInstance: (
51 instanceId: ProviderInstanceId,
52 ) => Effect.Effect<ProviderAdapterShape<ProviderAdapterError>, ProviderUnsupportedError>;
53
54 readonly getInstanceInfo: (
55 instanceId: ProviderInstanceId,
56 ) => Effect.Effect<ProviderInstanceRoutingInfo, ProviderUnsupportedError>;
57
58 /**
59 * List all live instance ids. Excludes unavailable/shadow instances —
60 * callers of this method want something they can pass to `getByInstance`.
61 */
62 readonly listInstances: () => Effect.Effect<ReadonlyArray<ProviderInstanceId>>;
63
64 /**
65 * Legacy: list provider kinds whose default instance is currently
66 * registered.
67 *
68 * @deprecated Prefer `listInstances`. Retained for migration-era call
69 * sites that iterate providers to build UI/metrics.
70 */
71 readonly listProviders: () => Effect.Effect<ReadonlyArray<ProviderDriverKind>>;
72
73 /**
74 * Change notification stream mirroring `ProviderInstanceRegistry.streamChanges`.
75 * Emits one `void` tick whenever the set of live instances changes
76 * (instance added, removed, or rebuilt after a settings edit). Consumers
77 * that fan out `adapter.streamEvents` per instance — e.g. `ProviderService`'s
78 * runtime event bus — re-pull `listInstances` on each tick and fork new
79 * subscriptions for instances they haven't seen yet.
80 */
81 readonly streamChanges: Stream.Stream<void>;
82
83 /**
84 * Acquire a change subscription synchronously in the caller's current fiber.
85 * Consumers that must avoid missing a publish between initial reconciliation
86 * and watcher startup should use this, then fork `Stream.fromSubscription`.
87 */
88 readonly subscribeChanges: Effect.Effect<PubSub.Subscription<void>, never, Scope.Scope>;
89}
90
91/**
92 * ProviderAdapterRegistry - Service tag for provider adapter lookup.
93 */
94export class ProviderAdapterRegistry extends Context.Service<
95 ProviderAdapterRegistry,
96 ProviderAdapterRegistryShape
97>()("t3/provider/Services/ProviderAdapterRegistry") {}