The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // Context tags are runtime singletons; drivers need multiple instances. |
| 2 | import type { |
| 3 | ProviderConsumeResetCreditOutcome, |
| 4 | AcpRegistryListSessionsResult, |
| 5 | AcpRegistryListProvidersResult, |
| 6 | AcpRegistryOperationError, |
| 7 | AcpRegistrySetProviderInput, |
| 8 | ProviderDriverKind, |
| 9 | ResolvedProviderInstanceEnvironment, |
| 10 | ProviderInstanceId, |
| 11 | ServerProvider, |
| 12 | ServerProviderWorkspaceSnapshot, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import type * as Effect from "effect/Effect"; |
| 15 | import type * as Schema from "effect/Schema"; |
| 16 | import type * as Scope from "effect/Scope"; |
| 17 | import type * as Stream from "effect/Stream"; |
| 18 | |
| 19 | import type * as ProviderAdapter from "./ProviderAdapter.ts"; |
| 20 | import type { ProviderAuthController } from "./auth.ts"; |
| 21 | import type { ProviderDriverError } from "./errors.ts"; |
| 22 | import type { ManagedServerProvider } from "./snapshot.ts"; |
| 23 | import type { ProviderTextGeneration } from "./textGeneration.ts"; |
| 24 | import type { ProviderUsageReader } from "./usage.ts"; |
| 25 | |
| 26 | export interface ProviderDriverMetadata { |
| 27 | readonly displayName: string; |
| 28 | /** Defaults to true; false rejects multiple instances of a global resource. */ |
| 29 | readonly supportsMultipleInstances?: boolean; |
| 30 | readonly hasDefaultInstance?: boolean; |
| 31 | } |
| 32 | |
| 33 | export type ProviderWorkspaceSnapshot = ServerProvider & |
| 34 | Pick<ServerProviderWorkspaceSnapshot, "slashCommandsPending" | "optionDescriptors"> & { |
| 35 | /** Set only when the directory's catalog differs from the machine snapshot's. */ |
| 36 | readonly workspaceModels?: ServerProviderWorkspaceSnapshot["models"]; |
| 37 | /** The directory's catalog could not be read, so keep the models found before. */ |
| 38 | readonly workspaceModelsPending?: boolean; |
| 39 | }; |
| 40 | |
| 41 | export interface ProviderInstance { |
| 42 | readonly instanceId: ProviderInstanceId; |
| 43 | readonly driverKind: ProviderDriverKind; |
| 44 | readonly continuationIdentity: ProviderContinuationIdentity; |
| 45 | readonly displayName: string | undefined; |
| 46 | readonly accentColor?: string | undefined; |
| 47 | readonly enabled: boolean; |
| 48 | readonly snapshot: ManagedServerProvider; |
| 49 | readonly snapshotForCwd?: ( |
| 50 | cwd: string, |
| 51 | ) => Effect.Effect<ProviderWorkspaceSnapshot, ProviderDriverError>; |
| 52 | /** |
| 53 | * Cwds whose workspace skills may have changed. The registry rescans only cwds already |
| 54 | * requested. The stream must end when the instance is torn down. |
| 55 | */ |
| 56 | readonly workspaceRescans?: Stream.Stream<string>; |
| 57 | readonly refreshModels?: () => Effect.Effect<void, ProviderDriverError>; |
| 58 | readonly invalidateCaches?: Effect.Effect<void>; |
| 59 | /** Moves a long-lived service onto a newly updated binary before its version is read back. */ |
| 60 | readonly afterUpdate?: Effect.Effect<void, ProviderDriverError>; |
| 61 | readonly consumeResetCredit?: ( |
| 62 | creditId: string | undefined, |
| 63 | ) => Effect.Effect<ProviderConsumeResetCreditOutcome, ProviderDriverError>; |
| 64 | readonly orchestrationAdapter: ProviderAdapter.ProviderAdapterV2["Service"]; |
| 65 | readonly textGeneration: ProviderTextGeneration; |
| 66 | readonly auth?: ProviderAuthController; |
| 67 | readonly acpSessionManagement?: { |
| 68 | readonly listSessions: (input: { |
| 69 | readonly cwd: string; |
| 70 | readonly cursor?: string; |
| 71 | }) => Effect.Effect<AcpRegistryListSessionsResult, AcpRegistryOperationError>; |
| 72 | readonly logout: (cwd: string) => Effect.Effect<void, AcpRegistryOperationError>; |
| 73 | readonly deleteSession: (input: { |
| 74 | readonly cwd: string; |
| 75 | readonly sessionId: string; |
| 76 | }) => Effect.Effect<void, AcpRegistryOperationError>; |
| 77 | readonly listProviders: ( |
| 78 | cwd: string, |
| 79 | ) => Effect.Effect<AcpRegistryListProvidersResult, AcpRegistryOperationError>; |
| 80 | readonly setProvider: ( |
| 81 | input: Omit<AcpRegistrySetProviderInput, "instanceId" | "projectId"> & { |
| 82 | readonly cwd: string; |
| 83 | }, |
| 84 | ) => Effect.Effect<void, AcpRegistryOperationError>; |
| 85 | readonly disableProvider: (input: { |
| 86 | readonly cwd: string; |
| 87 | readonly providerId: string; |
| 88 | }) => Effect.Effect<void, AcpRegistryOperationError>; |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | export interface ProviderContinuationIdentity { |
| 93 | readonly driverKind: ProviderDriverKind; |
| 94 | readonly continuationKey: string; |
| 95 | } |
| 96 | |
| 97 | export function defaultProviderContinuationIdentity(input: { |
| 98 | readonly driverKind: ProviderDriverKind; |
| 99 | readonly instanceId: ProviderInstanceId; |
| 100 | }): ProviderContinuationIdentity { |
| 101 | return { |
| 102 | driverKind: input.driverKind, |
| 103 | continuationKey: `${input.driverKind}:instance:${input.instanceId}`, |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | export interface ProviderDriverCreateInput<Config> { |
| 108 | readonly instanceId: ProviderInstanceId; |
| 109 | readonly displayName: string | undefined; |
| 110 | readonly accentColor?: string | undefined; |
| 111 | readonly environment: ResolvedProviderInstanceEnvironment; |
| 112 | readonly enabled: boolean; |
| 113 | readonly config: Config; |
| 114 | } |
| 115 | |
| 116 | /** create calls with different instanceId/config must share no mutable state. */ |
| 117 | export interface ProviderDriver<Config, R = never, UsageR = never> { |
| 118 | readonly driverKind: ProviderDriverKind; |
| 119 | readonly metadata: ProviderDriverMetadata; |
| 120 | // Codec keeps DecodingServices = never; Schema would widen it through AnyProviderDriver. |
| 121 | readonly configSchema: Schema.Codec<Config, unknown>; |
| 122 | readonly defaultConfig: () => Config; |
| 123 | /** Failures become unavailable snapshots; never throw defects. */ |
| 124 | readonly create: ( |
| 125 | input: ProviderDriverCreateInput<Config>, |
| 126 | ) => Effect.Effect<ProviderInstance, ProviderDriverError, R | Scope.Scope>; |
| 127 | readonly usage?: ProviderUsageReader<Config, UsageR>; |
| 128 | } |
| 129 | |
| 130 | export type AnyProviderDriver<R = never, UsageR = unknown> = ProviderDriver<any, R, UsageR>; |
| 131 | |
| 132 | export type ProviderUsageReaderEnv<Driver> = |
| 133 | Driver extends ProviderDriver<any, any, infer UsageR> ? UsageR : never; |