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 ChildProcessSpawner from "effect/process/ChildProcessSpawner";
10
11import type * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
12import { type FxAdapterV2DriverEnv, makeFxAdapterV2Driver } from "./adapter.ts";
13import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
14import {
15 buildInitialFxProviderSnapshot,
16 checkFxProviderStatus,
17 fxDefaultModelId,
18} from "./status.ts";
19import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
20import {
21 defaultProviderContinuationIdentity,
22 type ProviderDriver,
23 type ProviderInstance,
24} from "@t3tools/provider-core/server/driver";
25import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity";
26import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
27import { discoverFxSkills } from "./skills.ts";
28import { makeManualOnlyProviderMaintenanceCapabilities } from "@t3tools/provider-core/server/maintenanceResolver";
29import {
30 haveProviderSnapshotSettingsChanged,
31 makeProviderSnapshotSettingsSource,
32 type ProviderSnapshotSettings,
33} from "@t3tools/provider-core/server/snapshotSettings";
34import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration";
35import { FX_DRIVER_KIND } from "./acpSupport.ts";
36
37const decodeFxSettings = Schema.decodeSync(FxSettings);
38// fx installs from its own setup script and updates with `fx upgrade`; there
39// is no npm package to read a latest version from.
40const MAINTENANCE = makeManualOnlyProviderMaintenanceCapabilities({
41 provider: FX_DRIVER_KIND,
42 packageName: null,
43});
44
45// See `supportsTextGeneration` in status.ts.
46const unsupportedTextGeneration: ProviderTextGeneration = (() => {
47 const unsupported = (operation: string) =>
48 Effect.fail(
49 new TextGenerationError({
50 operation,
51 detail: "fx does not provide application text generation.",
52 }),
53 );
54 return {
55 generateCommitMessage: () => unsupported("generateCommitMessage"),
56 generatePrContent: () => unsupported("generatePrContent"),
57 generateBranchName: () => unsupported("generateBranchName"),
58 generateThreadTitle: () => unsupported("generateThreadTitle"),
59 generateReviewGuide: () => unsupported("generateReviewGuide"),
60 };
61})();
62
63export type FxDriverEnv =
64 | FxAdapterV2DriverEnv
65 | ChildProcessSpawner.ChildProcessSpawner
66 | SecureRandom.SecureRandom
67 | FileSystem.FileSystem
68 | Path.Path
69 | ProviderHost.ProviderHost;
70
71export const FxDriver: ProviderDriver<FxSettings, FxDriverEnv> = {
72 driverKind: FX_DRIVER_KIND,
73 metadata: {
74 displayName: "fx",
75 supportsMultipleInstances: true,
76 },
77 configSchema: FxSettings,
78 defaultConfig: (): FxSettings => decodeFxSettings({}),
79 create: Effect.fn("FxDriver.create")(function* ({
80 instanceId,
81 displayName,
82 accentColor,
83 environment,
84 enabled,
85 config,
86 }) {
87 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
88 const fileSystem = yield* FileSystem.FileSystem;
89 const path = yield* Path.Path;
90 const processEnv = yield* mergeProviderInstanceEnvironment(
91 environment,
92 yield* HostProcess.Environment,
93 );
94 const continuationIdentity = defaultProviderContinuationIdentity({
95 driverKind: FX_DRIVER_KIND,
96 instanceId,
97 });
98 const stampIdentity = withInstanceIdentity({
99 instanceId,
100 driverKind: FX_DRIVER_KIND,
101 displayName,
102 accentColor,
103 continuationGroupKey: continuationIdentity.continuationKey,
104 });
105 const effectiveConfig = { ...config, enabled } satisfies FxSettings;
106 const snapshotSettings = yield* makeProviderSnapshotSettingsSource(effectiveConfig);
107 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<FxSettings>>({
108 resolveMaintenance: () => Effect.succeed(MAINTENANCE),
109 getSettings: snapshotSettings.getSettings,
110 streamSettings: snapshotSettings.streamSettings,
111 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
112 initialSnapshot: (settings) =>
113 buildInitialFxProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
114 checkProvider: checkFxProviderStatus(effectiveConfig, processEnv).pipe(
115 Effect.map(stampIdentity),
116 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
117 ),
118 }).pipe(
119 Effect.mapError(
120 (cause) =>
121 new ProviderDriverError({
122 driver: FX_DRIVER_KIND,
123 instanceId,
124 detail: "Failed to build the fx provider snapshot.",
125 cause,
126 }),
127 ),
128 );
129
130 // "Default" selects the model the snapshot read from `fx status`.
131 const orchestrationAdapter = yield* makeFxAdapterV2Driver(
132 snapshot.getSnapshot.pipe(Effect.map((current) => fxDefaultModelId(current.models))),
133 )
134 .create({ instanceId, displayName, accentColor, environment, enabled, config })
135 .pipe(
136 Effect.mapError(
137 (cause) =>
138 new ProviderDriverError({
139 driver: FX_DRIVER_KIND,
140 instanceId,
141 detail: "Failed to build the fx orchestration adapter.",
142 cause,
143 }),
144 ),
145 );
146
147 return {
148 instanceId,
149 driverKind: FX_DRIVER_KIND,
150 continuationIdentity,
151 displayName,
152 accentColor,
153 enabled,
154 snapshot,
155 snapshotForCwd: (cwd) =>
156 !effectiveConfig.enabled
157 ? snapshot.getSnapshot
158 : Effect.all([
159 snapshot.getSnapshot,
160 discoverFxSkills({ cwd, environment: processEnv }).pipe(
161 Effect.provideService(FileSystem.FileSystem, fileSystem),
162 Effect.provideService(Path.Path, path),
163 ),
164 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
165 orchestrationAdapter,
166 textGeneration: unsupportedTextGeneration,
167 } satisfies ProviderInstance;
168 }),
169};