Open History to see the last commit
1/**
2 * There are intentionally no per-driver Context tags because tags are
3 * singleton-per-runtime and we need many instances of the same driver.
4 */
5import type {
6 ProviderConsumeResetCreditOutcome,
7 AcpRegistryListSessionsResult,
8 AcpRegistryListProvidersResult,
9 AcpRegistryOperationError,
10 AcpRegistrySetProviderInput,
11 ProviderDriverKind,
12 ProviderInstanceEnvironment,
13 ProviderInstanceId,
14 ServerProvider,
15 ServerProviderWorkspaceSnapshot,
16} from "@t3tools/contracts";
17import type * as Effect from "effect/Effect";
18import type * as Schema from "effect/Schema";
19import type * as Scope from "effect/Scope";
20
21import type * as ProviderAdapter from "./ProviderAdapter.ts";
22import type { ProviderAuthController } from "./auth.ts";
23import type { ProviderDriverError } from "./errors.ts";
24import type { ManagedServerProvider } from "./snapshot.ts";
25import type { ProviderTextGeneration } from "./textGeneration.ts";
26import type { ProviderUsageReader } from "./usage.ts";
27
28export interface ProviderDriverMetadata {
29 readonly displayName: string;
30 /**
31 * Defaults to `true`. Set to `false` for drivers that wrap a global
32 * resource (e.g. a single desktop app socket) — the registry then
33 * rejects multi-instance configurations with a clear error.
34 */
35 readonly supportsMultipleInstances?: boolean;
36 readonly hasDefaultInstance?: boolean;
37}
38
39export type ProviderWorkspaceSnapshot = ServerProvider &
40 Pick<ServerProviderWorkspaceSnapshot, "slashCommandsPending">;
41
42export interface ProviderInstance {
43 readonly instanceId: ProviderInstanceId;
44 readonly driverKind: ProviderDriverKind;
45 readonly continuationIdentity: ProviderContinuationIdentity;
46 readonly displayName: string | undefined;
47 readonly accentColor?: string | undefined;
48 readonly enabled: boolean;
49 readonly snapshot: ManagedServerProvider;
50 readonly snapshotForCwd?: (
51 cwd: string,
52 ) => Effect.Effect<ProviderWorkspaceSnapshot, ProviderDriverError>;
53 readonly refreshModels?: () => Effect.Effect<void, ProviderDriverError>;
54 readonly invalidateCaches?: Effect.Effect<void>;
55 readonly consumeResetCredit?: () => Effect.Effect<
56 ProviderConsumeResetCreditOutcome,
57 ProviderDriverError
58 >;
59 readonly orchestrationAdapter: ProviderAdapter.ProviderAdapterV2["Service"];
60 readonly textGeneration: ProviderTextGeneration;
61 readonly auth?: ProviderAuthController;
62 readonly acpSessionManagement?: {
63 readonly listSessions: (input: {
64 readonly cwd: string;
65 readonly cursor?: string;
66 }) => Effect.Effect<AcpRegistryListSessionsResult, AcpRegistryOperationError>;
67 readonly logout: (cwd: string) => Effect.Effect<void, AcpRegistryOperationError>;
68 readonly deleteSession: (input: {
69 readonly cwd: string;
70 readonly sessionId: string;
71 }) => Effect.Effect<void, AcpRegistryOperationError>;
72 readonly listProviders: (
73 cwd: string,
74 ) => Effect.Effect<AcpRegistryListProvidersResult, AcpRegistryOperationError>;
75 readonly setProvider: (
76 input: Omit<AcpRegistrySetProviderInput, "instanceId" | "projectId"> & {
77 readonly cwd: string;
78 },
79 ) => Effect.Effect<void, AcpRegistryOperationError>;
80 readonly disableProvider: (input: {
81 readonly cwd: string;
82 readonly providerId: string;
83 }) => Effect.Effect<void, AcpRegistryOperationError>;
84 };
85}
86
87export interface ProviderContinuationIdentity {
88 readonly driverKind: ProviderDriverKind;
89 readonly continuationKey: string;
90}
91
92export function defaultProviderContinuationIdentity(input: {
93 readonly driverKind: ProviderDriverKind;
94 readonly instanceId: ProviderInstanceId;
95}): ProviderContinuationIdentity {
96 return {
97 driverKind: input.driverKind,
98 continuationKey: `${input.driverKind}:instance:${input.instanceId}`,
99 };
100}
101
102export interface ProviderDriverCreateInput<Config> {
103 readonly instanceId: ProviderInstanceId;
104 readonly displayName: string | undefined;
105 readonly accentColor?: string | undefined;
106 readonly environment: ProviderInstanceEnvironment;
107 readonly enabled: boolean;
108 readonly config: Config;
109}
110
111/**
112 * Two calls to `create` with different `instanceId` / `config` MUST yield
113 * instances with no shared mutable state.
114 */
115export interface ProviderDriver<Config, R = never, UsageR = never> {
116 readonly driverKind: ProviderDriverKind;
117 readonly metadata: ProviderDriverMetadata;
118 /**
119 * Using `Codec` rather than `Schema` pins `DecodingServices = never` — if
120 * we used `Schema<Config>`, the erased `any` in `AnyProviderDriver` would
121 * widen `DecodingServices` to `unknown` and poison the R channel of every
122 * caller of `decodeUnknownEffect`.
123 */
124 readonly configSchema: Schema.Codec<Config, unknown>;
125 readonly defaultConfig: () => Config;
126 /**
127 * Failures become unavailable shadow snapshots — the driver MUST NOT throw
128 * defects.
129 */
130 readonly create: (
131 input: ProviderDriverCreateInput<Config>,
132 ) => Effect.Effect<ProviderInstance, ProviderDriverError, R | Scope.Scope>;
133 readonly usage?: ProviderUsageReader<Config, UsageR>;
134}
135
136export type AnyProviderDriver<R = never, UsageR = unknown> = ProviderDriver<any, R, UsageR>;
137
138export type ProviderUsageReaderEnv<Driver> =
139 Driver extends ProviderDriver<any, any, infer UsageR> ? UsageR : never;