Open History to see the last commit
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 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
149 resolveMaintenance().pipe(
150 Effect.flatMap((maintenanceCapabilities) =>
151 enrichProviderSnapshotWithVersionAdvisory(currentSnapshot, maintenanceCapabilities, {
152 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
153 }),
154 ),
155 Effect.flatMap(publishSnapshot),
156 Effect.provideService(HttpClient.HttpClient, httpClient),
157 Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, latestVersions),
158 ),
159 }).pipe(
160 Effect.mapError(
161 (cause) =>
162 new ProviderDriverError({
163 driver: FX_DRIVER_KIND,
164 instanceId,
165 detail: "Failed to build the fx provider snapshot.",
166 cause,
167 }),
168 ),
169 );
170
171 const orchestrationAdapter = yield* makeFxAdapterV2Driver((cwd) =>
172 readFxDefaultChoice(effectiveConfig, processEnv, cwd).pipe(
173 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
174 ),
175 )
176 .create({
177 instanceId,
178 displayName,
179 accentColor,
180 environment,
181 enabled,
182 config: effectiveConfig,
183 })
184 .pipe(
185 Effect.mapError(
186 (cause) =>
187 new ProviderDriverError({
188 driver: FX_DRIVER_KIND,
189 instanceId,
190 detail: "Failed to build the fx orchestration adapter.",
191 cause,
192 }),
193 ),
194 );
195
196 return {
197 instanceId,
198 driverKind: FX_DRIVER_KIND,
199 continuationIdentity,
200 displayName,
201 accentColor,
202 enabled,
203 snapshot,
204 snapshotForCwd: (cwd) =>
205 !effectiveConfig.enabled
206 ? snapshot.getSnapshot
207 : Effect.all([
208 snapshot.getSnapshot,
209 discoverFxSkills({ cwd, environment: processEnv }).pipe(
210 Effect.provideService(FileSystem.FileSystem, fileSystem),
211 Effect.provideService(Path.Path, path),
212 ),
213 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
214 orchestrationAdapter,
215 textGeneration: unsupportedTextGeneration,
216 } satisfies ProviderInstance;
217 }),
218};