|
| 1 | +import { PiSettings, ProviderDriverKind, type ServerProvider } from "@t3tools/contracts"; |
| 2 | +import * as Crypto from "effect/Crypto"; |
| 3 | +import * as Duration from "effect/Duration"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as FileSystem from "effect/FileSystem"; |
| 6 | +import * as Path from "effect/Path"; |
| 7 | +import * as Schema from "effect/Schema"; |
| 8 | +import { HttpClient } from "effect/unstable/http"; |
| 9 | +import { ChildProcessSpawner } from "effect/unstable/process"; |
| 10 | + |
| 11 | +import { ServerConfig } from "../../config.ts"; |
| 12 | +import { ServerSettingsService } from "../../serverSettings.ts"; |
| 13 | +import { makePiTextGeneration } from "../../textGeneration/PiTextGeneration.ts"; |
| 14 | +import { ProviderDriverError } from "../Errors.ts"; |
| 15 | +import { makePiAdapter } from "../Layers/PiAdapter.ts"; |
| 16 | +import { buildInitialPiProviderSnapshot, checkPiProviderStatus } from "../Layers/PiProvider.ts"; |
| 17 | +import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts"; |
| 18 | +import { makeManagedServerProvider } from "../makeManagedServerProvider.ts"; |
| 19 | +import { |
| 20 | + defaultProviderContinuationIdentity, |
| 21 | + type ProviderDriver, |
| 22 | + type ProviderInstance, |
| 23 | +} from "../ProviderDriver.ts"; |
| 24 | +import type { ServerProviderDraft } from "../providerSnapshot.ts"; |
| 25 | +import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts"; |
| 26 | +import { |
| 27 | + enrichProviderSnapshotWithVersionAdvisory, |
| 28 | + makePackageManagedProviderMaintenanceResolver, |
| 29 | + resolveProviderMaintenanceCapabilitiesEffect, |
| 30 | +} from "../providerMaintenance.ts"; |
| 31 | +import { |
| 32 | + haveProviderSnapshotSettingsChanged, |
| 33 | + makeProviderSnapshotSettingsSource, |
| 34 | + type ProviderSnapshotSettings, |
| 35 | +} from "../providerUpdateSettings.ts"; |
| 36 | + |
| 37 | +const decodePiSettings = Schema.decodeSync(PiSettings); |
| 38 | + |
| 39 | +const DRIVER_KIND = ProviderDriverKind.make("pi"); |
| 40 | +const SNAPSHOT_REFRESH_INTERVAL = Duration.minutes(5); |
| 41 | + |
| 42 | +const UPDATE = makePackageManagedProviderMaintenanceResolver({ |
| 43 | + provider: DRIVER_KIND, |
| 44 | + npmPackageName: "@earendil-works/pi-coding-agent", |
| 45 | + homebrewFormula: null, |
| 46 | + nativeUpdate: null, |
| 47 | +}); |
| 48 | + |
| 49 | +export type PiDriverEnv = |
| 50 | + | ChildProcessSpawner.ChildProcessSpawner |
| 51 | + | Crypto.Crypto |
| 52 | + | FileSystem.FileSystem |
| 53 | + | HttpClient.HttpClient |
| 54 | + | Path.Path |
| 55 | + | ProviderEventLoggers |
| 56 | + | ServerConfig |
| 57 | + | ServerSettingsService; |
| 58 | + |
| 59 | +const withInstanceIdentity = |
| 60 | + (input: { |
| 61 | + readonly instanceId: ProviderInstance["instanceId"]; |
| 62 | + readonly displayName: string | undefined; |
| 63 | + readonly accentColor: string | undefined; |
| 64 | + readonly continuationGroupKey: string; |
| 65 | + }) => |
| 66 | + (snapshot: ServerProviderDraft): ServerProvider => ({ |
| 67 | + ...snapshot, |
| 68 | + instanceId: input.instanceId, |
| 69 | + driver: DRIVER_KIND, |
| 70 | + ...(input.displayName ? { displayName: input.displayName } : {}), |
| 71 | + ...(input.accentColor ? { accentColor: input.accentColor } : {}), |
| 72 | + continuation: { groupKey: input.continuationGroupKey }, |
| 73 | + }); |
| 74 | + |
| 75 | +export const PiDriver: ProviderDriver<PiSettings, PiDriverEnv> = { |
| 76 | + driverKind: DRIVER_KIND, |
| 77 | + metadata: { |
| 78 | + displayName: "Pi", |
| 79 | + supportsMultipleInstances: true, |
| 80 | + }, |
| 81 | + configSchema: PiSettings, |
| 82 | + defaultConfig: (): PiSettings => decodePiSettings({}), |
| 83 | + create: ({ instanceId, displayName, accentColor, environment, enabled, config }) => |
| 84 | + Effect.gen(function* () { |
| 85 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 86 | + const httpClient = yield* HttpClient.HttpClient; |
| 87 | + const serverSettings = yield* ServerSettingsService; |
| 88 | + const serverConfig = yield* ServerConfig; |
| 89 | + const processEnv = mergeProviderInstanceEnvironment(environment); |
| 90 | + const continuationIdentity = defaultProviderContinuationIdentity({ |
| 91 | + driverKind: DRIVER_KIND, |
| 92 | + instanceId, |
| 93 | + }); |
| 94 | + const stampIdentity = withInstanceIdentity({ |
| 95 | + instanceId, |
| 96 | + displayName, |
| 97 | + accentColor, |
| 98 | + continuationGroupKey: continuationIdentity.continuationKey, |
| 99 | + }); |
| 100 | + const effectiveConfig = { ...config, enabled } satisfies PiSettings; |
| 101 | + const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, { |
| 102 | + binaryPath: effectiveConfig.binaryPath, |
| 103 | + env: processEnv, |
| 104 | + }); |
| 105 | + |
| 106 | + const adapter = yield* makePiAdapter(effectiveConfig, { |
| 107 | + instanceId, |
| 108 | + environment: processEnv, |
| 109 | + }); |
| 110 | + const textGeneration = yield* makePiTextGeneration(effectiveConfig, processEnv); |
| 111 | + |
| 112 | + const checkProvider = checkPiProviderStatus( |
| 113 | + effectiveConfig, |
| 114 | + serverConfig.cwd, |
| 115 | + processEnv, |
| 116 | + ).pipe( |
| 117 | + Effect.map(stampIdentity), |
| 118 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 119 | + ); |
| 120 | + |
| 121 | + const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings); |
| 122 | + const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<PiSettings>>({ |
| 123 | + maintenanceCapabilities, |
| 124 | + getSettings: snapshotSettings.getSettings, |
| 125 | + streamSettings: snapshotSettings.streamSettings, |
| 126 | + haveSettingsChanged: haveProviderSnapshotSettingsChanged, |
| 127 | + initialSnapshot: (settings) => |
| 128 | + buildInitialPiProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)), |
| 129 | + checkProvider, |
| 130 | + enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) => |
| 131 | + enrichProviderSnapshotWithVersionAdvisory(currentSnapshot, maintenanceCapabilities, { |
| 132 | + enableProviderUpdateChecks: settings.enableProviderUpdateChecks, |
| 133 | + }).pipe( |
| 134 | + Effect.provideService(HttpClient.HttpClient, httpClient), |
| 135 | + Effect.flatMap((enrichedSnapshot) => publishSnapshot(enrichedSnapshot)), |
| 136 | + Effect.catchCause((cause) => |
| 137 | + Effect.logWarning("Pi version advisory enrichment failed", { cause }).pipe( |
| 138 | + Effect.asVoid, |
| 139 | + ), |
| 140 | + ), |
| 141 | + ), |
| 142 | + refreshInterval: SNAPSHOT_REFRESH_INTERVAL, |
| 143 | + }).pipe( |
| 144 | + Effect.mapError( |
| 145 | + (cause) => |
| 146 | + new ProviderDriverError({ |
| 147 | + driver: DRIVER_KIND, |
| 148 | + instanceId, |
| 149 | + detail: `Failed to build Pi snapshot: ${cause.message ?? String(cause)}`, |
| 150 | + cause, |
| 151 | + }), |
| 152 | + ), |
| 153 | + ); |
| 154 | + |
| 155 | + return { |
| 156 | + instanceId, |
| 157 | + driverKind: DRIVER_KIND, |
| 158 | + continuationIdentity, |
| 159 | + displayName, |
| 160 | + accentColor, |
| 161 | + enabled, |
| 162 | + snapshot, |
| 163 | + adapter, |
| 164 | + textGeneration, |
| 165 | + } satisfies ProviderInstance; |
| 166 | + }), |
| 167 | +}; |
0 commit comments