Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

driver.ts

219 lines8.2 KB

1import { TextGenerationError } from "@t3tools/contracts";
2import { FxSettings } from "../settings.ts";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import type * as SecureRandom from "effect/crypto/SecureRandom";
5import * as Effect from "effect/Effect";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8import * as Schema from "effect/Schema";
9import * as HttpClient from "effect/http/HttpClient";
10import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11​
12import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
13import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
14import { type FxAdapterV2DriverEnv, makeFxAdapterV2Driver } from "./adapter.ts";
15import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
16import {
17 buildInitialFxProviderSnapshot,
18 checkFxProviderStatus,
19 readFxDefaultChoice,
20} from "./status.ts";
21import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
22import {
23 defaultProviderContinuationIdentity,
24 type ProviderDriver,
25 type ProviderInstance,
26} from "@t3tools/provider-core/server/driver";
27import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity";
28import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
29import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
30import { discoverFxSkills } from "./skills.ts";
31import {
32 enrichProviderSnapshotWithVersionAdvisory,
33 makeCachedProviderMaintenanceResolution,
34 resolveProviderMaintenanceCapabilitiesEffect,
35} from "@t3tools/provider-core/server/maintenanceResolver";
36import { fetchLatestFxVersion, makeFxMaintenance } from "./maintenance.ts";
37import {
38 haveProviderSnapshotSettingsChanged,
39 makeProviderSnapshotSettingsSource,
40 type ProviderSnapshotSettings,
41} from "@t3tools/provider-core/server/snapshotSettings";
42import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration";
43import { FX_DRIVER_KIND } from "./acpSupport.ts";
44​
45const decodeFxSettings = Schema.decodeSync(FxSettings);
46​
47// See `supportsTextGeneration` in status.ts.
48const unsupportedTextGeneration: ProviderTextGeneration = (() => {
49 const unsupported = (operation: string) =>
50 Effect.fail(
51 new TextGenerationError({
52 operation,
53 detail: "fx does not provide application text generation.",
54 }),
55 );
56 return {
57 generateCommitMessage: () => unsupported("generateCommitMessage"),
58 generatePrContent: () => unsupported("generatePrContent"),
59 generateBranchName: () => unsupported("generateBranchName"),
60 generateThreadTitle: () => unsupported("generateThreadTitle"),
61 generateReviewGuide: () => unsupported("generateReviewGuide"),
62 generateTranscriptionPostProcessing: () => unsupported("generateTranscriptionPostProcessing"),
63 };
64})();
65​
66export type FxDriverEnv =
67 | FxAdapterV2DriverEnv
68 | ChildProcessSpawner.ChildProcessSpawner
69 | SecureRandom.SecureRandom
70 | FileSystem.FileSystem
71 | HttpClient.HttpClient
72 | Path.Path
73 | ProviderHost.ProviderHost
74 | ProviderLatestVersions.ProviderLatestVersions;
75​
76export const FxDriver: ProviderDriver<FxSettings, FxDriverEnv> = {
77 driverKind: FX_DRIVER_KIND,
78 metadata: {
79 displayName: "fx",
80 supportsMultipleInstances: true,
81 },
82 configSchema: FxSettings,
83 defaultConfig: (): FxSettings => decodeFxSettings({}),
84 create: Effect.fn("FxDriver.create")(function* ({
85 instanceId,
86 displayName,
87 accentColor,
88 environment,
89 enabled,
90 config,
91 }) {
92 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
93 const fileSystem = yield* FileSystem.FileSystem;
94 const path = yield* Path.Path;
95 const host = yield* ProviderHost.ProviderHost;
96 const httpClient = yield* HttpClient.HttpClient;
97 const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions;
98 const processEnv = yield* mergeProviderInstanceEnvironment(
99 environment,
100 yield* HostProcess.Environment,
101 );
102 const continuationIdentity = defaultProviderContinuationIdentity({
103 driverKind: FX_DRIVER_KIND,
104 instanceId,
105 });
106 const stampIdentity = withInstanceIdentity({
107 instanceId,
108 driverKind: FX_DRIVER_KIND,
109 displayName,
110 accentColor,
111 continuationGroupKey: continuationIdentity.continuationKey,
112 });
113 const effectiveConfig = {
114 ...config,
115 enabled,
116 binaryPath: expandHomePath(config.binaryPath, yield* HostProcess.HomeDirectory),
117 } satisfies FxSettings;
118 const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
119 resolveProviderMaintenanceCapabilitiesEffect(
120 makeFxMaintenance(
121 fetchLatestFxVersion().pipe(Effect.provideService(HttpClient.HttpClient, httpClient)),
122 ),
123 {
124 binaryPath: effectiveConfig.binaryPath,
125 env: processEnv,
126 npmCacheDir: path.join(host.paths.providerStatusCacheDir, "npm"),
127 },
128 ).pipe(
129 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
130 Effect.provideService(FileSystem.FileSystem, fileSystem),
131 Effect.provideService(Path.Path, path),
132 ),
133 );
134 const snapshotSettings = yield* makeProviderSnapshotSettingsSource(effectiveConfig);
135 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<FxSettings>>({
136 resolveMaintenance,
137 getSettings: snapshotSettings.getSettings,
138 streamSettings: snapshotSettings.streamSettings,
139 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
140 initialSnapshot: (settings) =>
141 buildInitialFxProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
142 checkProvider: checkFxProviderStatus(effectiveConfig, processEnv).pipe(
143 Effect.map(stampIdentity),
144 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
145 Effect.provideService(FileSystem.FileSystem, fileSystem),
146 Effect.provideService(Path.Path, path),
147 ),
148 awaitEnrichmentOnRefresh: true,
149 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
150 resolveMaintenance().pipe(
151 Effect.flatMap((maintenanceCapabilities) =>
152 enrichProviderSnapshotWithVersionAdvisory(currentSnapshot, maintenanceCapabilities, {
153 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
154 }),
155 ),
156 Effect.flatMap(publishSnapshot),
157 Effect.provideService(HttpClient.HttpClient, httpClient),
158 Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, latestVersions),
159 ),
160 }).pipe(
161 Effect.mapError(
162 (cause) =>
163 new ProviderDriverError({
164 driver: FX_DRIVER_KIND,
165 instanceId,
166 detail: "Failed to build the fx provider snapshot.",
167 cause,
168 }),
169 ),
170 );
171​
172 const orchestrationAdapter = yield* makeFxAdapterV2Driver((cwd) =>
173 readFxDefaultChoice(effectiveConfig, processEnv, cwd).pipe(
174 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
175 ),
176 )
177 .create({
178 instanceId,
179 displayName,
180 accentColor,
181 environment,
182 enabled,
183 config: effectiveConfig,
184 })
185 .pipe(
186 Effect.mapError(
187 (cause) =>
188 new ProviderDriverError({
189 driver: FX_DRIVER_KIND,
190 instanceId,
191 detail: "Failed to build the fx orchestration adapter.",
192 cause,
193 }),
194 ),
195 );
196​
197 return {
198 instanceId,
199 driverKind: FX_DRIVER_KIND,
200 continuationIdentity,
201 displayName,
202 accentColor,
203 enabled,
204 snapshot,
205 snapshotForCwd: (cwd) =>
206 !effectiveConfig.enabled
207 ? snapshot.getSnapshot
208 : Effect.all([
209 snapshot.getSnapshot,
210 discoverFxSkills({ cwd, environment: processEnv }).pipe(
211 Effect.provideService(FileSystem.FileSystem, fileSystem),
212 Effect.provideService(Path.Path, path),
213 ),
214 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
215 orchestrationAdapter,
216 textGeneration: unsupportedTextGeneration,
217 } satisfies ProviderInstance;
218 }),
219};