|
| 1 | +/** |
| 2 | + * AcpRegistryDriver — generic ACP catalog driver. |
| 3 | + * |
| 4 | + * One driver kind covers Gemini, Copilot, Pi, Hermes, Qwen, Kimi, and any |
| 5 | + * custom ACP stdio agent. Launch command/args live on the instance config. |
| 6 | + * |
| 7 | + * @module provider/Drivers/AcpRegistryDriver |
| 8 | + */ |
| 9 | +import { |
| 10 | + AcpRegistrySettings, |
| 11 | + parseAcpLaunchArgs, |
| 12 | + ProviderDriverKind, |
| 13 | + TextGenerationError, |
| 14 | + type ServerProvider, |
| 15 | +} from "@t3tools/contracts"; |
| 16 | +import * as Crypto from "effect/Crypto"; |
| 17 | +import * as Effect from "effect/Effect"; |
| 18 | +import * as FileSystem from "effect/FileSystem"; |
| 19 | +import * as Path from "effect/Path"; |
| 20 | +import * as Schema from "effect/Schema"; |
| 21 | +import { ChildProcessSpawner } from "effect/unstable/process"; |
| 22 | + |
| 23 | +import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts"; |
| 24 | +import { ServerConfig } from "../../config.ts"; |
| 25 | +import { ServerSettingsService } from "../../serverSettings.ts"; |
| 26 | +import { ProviderDriverError } from "../Errors.ts"; |
| 27 | +import { makeGenericAcpAdapter } from "../Layers/GenericAcpAdapter.ts"; |
| 28 | +import { |
| 29 | + buildInitialAcpRegistryProviderSnapshot, |
| 30 | + checkAcpRegistryProviderStatus, |
| 31 | +} from "../Layers/AcpRegistryProvider.ts"; |
| 32 | +import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts"; |
| 33 | +import { makeManagedServerProvider } from "../makeManagedServerProvider.ts"; |
| 34 | +import { |
| 35 | + defaultProviderContinuationIdentity, |
| 36 | + type ProviderDriver, |
| 37 | + type ProviderInstance, |
| 38 | +} from "../ProviderDriver.ts"; |
| 39 | +import type { ServerProviderDraft } from "../providerSnapshot.ts"; |
| 40 | +import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts"; |
| 41 | +import { |
| 42 | + makeManualOnlyProviderMaintenanceCapabilities, |
| 43 | + makeStaticProviderMaintenanceResolver, |
| 44 | + resolveProviderMaintenanceCapabilitiesEffect, |
| 45 | +} from "../providerMaintenance.ts"; |
| 46 | +import { |
| 47 | + haveProviderSnapshotSettingsChanged, |
| 48 | + makeProviderSnapshotSettingsSource, |
| 49 | + type ProviderSnapshotSettings, |
| 50 | +} from "../providerUpdateSettings.ts"; |
| 51 | + |
| 52 | +const decodeAcpRegistrySettings = Schema.decodeSync(AcpRegistrySettings); |
| 53 | +const DRIVER_KIND = ProviderDriverKind.make("acpRegistry"); |
| 54 | +const UPDATE = makeStaticProviderMaintenanceResolver( |
| 55 | + makeManualOnlyProviderMaintenanceCapabilities({ |
| 56 | + provider: DRIVER_KIND, |
| 57 | + packageName: null, |
| 58 | + }), |
| 59 | +); |
| 60 | + |
| 61 | +export type AcpRegistryDriverEnv = |
| 62 | + | BackgroundPolicy.BackgroundPolicy |
| 63 | + | ChildProcessSpawner.ChildProcessSpawner |
| 64 | + | Crypto.Crypto |
| 65 | + | FileSystem.FileSystem |
| 66 | + | Path.Path |
| 67 | + | ProviderEventLoggers |
| 68 | + | ServerConfig |
| 69 | + | ServerSettingsService; |
| 70 | + |
| 71 | +const unsupportedTextGeneration = (operation: string) => |
| 72 | + Effect.fail( |
| 73 | + new TextGenerationError({ |
| 74 | + operation, |
| 75 | + detail: "Generic ACP providers do not support git text generation yet.", |
| 76 | + }), |
| 77 | + ); |
| 78 | + |
| 79 | +const makeTextGeneration = () => ({ |
| 80 | + generateCommitMessage: () => unsupportedTextGeneration("generateCommitMessage"), |
| 81 | + generatePrContent: () => unsupportedTextGeneration("generatePrContent"), |
| 82 | + generateBranchName: () => unsupportedTextGeneration("generateBranchName"), |
| 83 | + generateThreadTitle: () => unsupportedTextGeneration("generateThreadTitle"), |
| 84 | +}); |
| 85 | + |
| 86 | +const withInstanceIdentity = |
| 87 | + (input: { |
| 88 | + readonly instanceId: ProviderInstance["instanceId"]; |
| 89 | + readonly displayName: string | undefined; |
| 90 | + readonly accentColor: string | undefined; |
| 91 | + readonly continuationGroupKey: string; |
| 92 | + }) => |
| 93 | + (snapshot: ServerProviderDraft): ServerProvider => ({ |
| 94 | + ...snapshot, |
| 95 | + instanceId: input.instanceId, |
| 96 | + driver: DRIVER_KIND, |
| 97 | + ...(input.displayName ? { displayName: input.displayName } : {}), |
| 98 | + ...(input.accentColor ? { accentColor: input.accentColor } : {}), |
| 99 | + continuation: { groupKey: input.continuationGroupKey }, |
| 100 | + }); |
| 101 | + |
| 102 | +export const AcpRegistryDriver: ProviderDriver<AcpRegistrySettings, AcpRegistryDriverEnv> = { |
| 103 | + driverKind: DRIVER_KIND, |
| 104 | + metadata: { |
| 105 | + displayName: "ACP Registry", |
| 106 | + supportsMultipleInstances: true, |
| 107 | + }, |
| 108 | + configSchema: AcpRegistrySettings, |
| 109 | + defaultConfig: (): AcpRegistrySettings => decodeAcpRegistrySettings({}), |
| 110 | + create: ({ instanceId, displayName, accentColor, environment, enabled, config }) => |
| 111 | + Effect.gen(function* () { |
| 112 | + const crypto = yield* Crypto.Crypto; |
| 113 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 114 | + const serverSettings = yield* ServerSettingsService; |
| 115 | + const eventLoggers = yield* ProviderEventLoggers; |
| 116 | + const processEnv = mergeProviderInstanceEnvironment(environment); |
| 117 | + const continuationIdentity = defaultProviderContinuationIdentity({ |
| 118 | + driverKind: DRIVER_KIND, |
| 119 | + instanceId, |
| 120 | + }); |
| 121 | + const stampIdentity = withInstanceIdentity({ |
| 122 | + instanceId, |
| 123 | + displayName, |
| 124 | + accentColor, |
| 125 | + continuationGroupKey: continuationIdentity.continuationKey, |
| 126 | + }); |
| 127 | + const effectiveConfig = { ...config, enabled } satisfies AcpRegistrySettings; |
| 128 | + const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, { |
| 129 | + binaryPath: effectiveConfig.command, |
| 130 | + env: processEnv, |
| 131 | + }); |
| 132 | + |
| 133 | + const adapter = yield* makeGenericAcpAdapter( |
| 134 | + { |
| 135 | + enabled: effectiveConfig.enabled, |
| 136 | + command: effectiveConfig.command.trim() || "acp", |
| 137 | + args: parseAcpLaunchArgs(effectiveConfig.launchArgs), |
| 138 | + }, |
| 139 | + { |
| 140 | + provider: DRIVER_KIND, |
| 141 | + instanceId, |
| 142 | + environment: processEnv, |
| 143 | + readyReason: "ACP session ready", |
| 144 | + ...(effectiveConfig.authMethodId.trim() |
| 145 | + ? { authMethodId: effectiveConfig.authMethodId.trim() } |
| 146 | + : {}), |
| 147 | + ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}), |
| 148 | + }, |
| 149 | + ); |
| 150 | + |
| 151 | + const checkProvider = checkAcpRegistryProviderStatus(effectiveConfig, processEnv).pipe( |
| 152 | + Effect.map(stampIdentity), |
| 153 | + Effect.provideService(Crypto.Crypto, crypto), |
| 154 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 155 | + ); |
| 156 | + |
| 157 | + const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings); |
| 158 | + const snapshot = yield* makeManagedServerProvider< |
| 159 | + ProviderSnapshotSettings<AcpRegistrySettings> |
| 160 | + >({ |
| 161 | + maintenanceCapabilities, |
| 162 | + getSettings: snapshotSettings.getSettings, |
| 163 | + streamSettings: snapshotSettings.streamSettings, |
| 164 | + haveSettingsChanged: haveProviderSnapshotSettingsChanged, |
| 165 | + initialSnapshot: (settings) => |
| 166 | + buildInitialAcpRegistryProviderSnapshot(settings.provider).pipe( |
| 167 | + Effect.map(stampIdentity), |
| 168 | + ), |
| 169 | + checkProvider, |
| 170 | + }).pipe( |
| 171 | + Effect.mapError( |
| 172 | + (cause) => |
| 173 | + new ProviderDriverError({ |
| 174 | + driver: DRIVER_KIND, |
| 175 | + instanceId, |
| 176 | + detail: `Failed to build ACP Registry snapshot: ${cause.message ?? String(cause)}`, |
| 177 | + cause, |
| 178 | + }), |
| 179 | + ), |
| 180 | + ); |
| 181 | + |
| 182 | + return { |
| 183 | + instanceId, |
| 184 | + driverKind: DRIVER_KIND, |
| 185 | + continuationIdentity, |
| 186 | + displayName, |
| 187 | + accentColor, |
| 188 | + enabled, |
| 189 | + snapshot, |
| 190 | + adapter, |
| 191 | + textGeneration: makeTextGeneration(), |
| 192 | + } satisfies ProviderInstance; |
| 193 | + }), |
| 194 | +}; |
0 commit comments