@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 213 lines 8.06 KB
Raw
1 import { TextGenerationError } from "@t3tools/contracts";
2 import { FxSettings } from "../settings.ts";
3 import * as HostProcess from "@t3tools/shared/HostProcess";
4 import type * as SecureRandom from "effect/crypto/SecureRandom";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as Path from "effect/Path";
8 import * as Schema from "effect/Schema";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
11
12 import type * as ProviderHost from "@t3tools/provider-core/server/ProviderHost";
13 import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions";
14 import { type FxAdapterV2DriverEnv, makeFxAdapterV2Driver } from "./adapter.ts";
15 import { ProviderDriverError } from "@t3tools/provider-core/server/errors";
16 import {
17 buildInitialFxProviderSnapshot,
18 checkFxProviderStatus,
19 readFxDefaultChoice,
20 } from "./status.ts";
21 import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider";
22 import {
23 defaultProviderContinuationIdentity,
24 type ProviderDriver,
25 type ProviderInstance,
26 } from "@t3tools/provider-core/server/driver";
27 import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity";
28 import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
29 import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
30 import { discoverFxSkills } from "./skills.ts";
31 import {
32 enrichProviderSnapshotWithVersionAdvisory,
33 makeCachedProviderMaintenanceResolution,
34 resolveProviderMaintenanceCapabilitiesEffect,
35 } from "@t3tools/provider-core/server/maintenanceResolver";
36 import { fetchLatestFxVersion, makeFxMaintenance } from "./maintenance.ts";
37 import {
38 haveProviderSnapshotSettingsChanged,
39 makeProviderSnapshotSettingsSource,
40 type ProviderSnapshotSettings,
41 } from "@t3tools/provider-core/server/snapshotSettings";
42 import type { ProviderTextGeneration } from "@t3tools/provider-core/server/textGeneration";
43 import { FX_DRIVER_KIND } from "./acpSupport.ts";
44
45 const decodeFxSettings = Schema.decodeSync(FxSettings);
46
47 // See `supportsTextGeneration` in status.ts.
48 const 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
66 export 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
76 export 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 httpClient = yield* HttpClient.HttpClient;
96 const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions;
97 const processEnv = yield* mergeProviderInstanceEnvironment(
98 environment,
99 yield* HostProcess.Environment,
100 );
101 const continuationIdentity = defaultProviderContinuationIdentity({
102 driverKind: FX_DRIVER_KIND,
103 instanceId,
104 });
105 const stampIdentity = withInstanceIdentity({
106 instanceId,
107 driverKind: FX_DRIVER_KIND,
108 displayName,
109 accentColor,
110 continuationGroupKey: continuationIdentity.continuationKey,
111 });
112 const effectiveConfig = {
113 ...config,
114 enabled,
115 binaryPath: expandHomePath(config.binaryPath, yield* HostProcess.HomeDirectory),
116 } satisfies FxSettings;
117 const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
118 resolveProviderMaintenanceCapabilitiesEffect(
119 makeFxMaintenance(
120 fetchLatestFxVersion().pipe(Effect.provideService(HttpClient.HttpClient, httpClient)),
121 ),
122 { binaryPath: effectiveConfig.binaryPath, env: processEnv },
123 ).pipe(
124 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
125 Effect.provideService(FileSystem.FileSystem, fileSystem),
126 Effect.provideService(Path.Path, path),
127 ),
128 );
129 const snapshotSettings = yield* makeProviderSnapshotSettingsSource(effectiveConfig);
130 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<FxSettings>>({
131 resolveMaintenance,
132 getSettings: snapshotSettings.getSettings,
133 streamSettings: snapshotSettings.streamSettings,
134 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
135 initialSnapshot: (settings) =>
136 buildInitialFxProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
137 checkProvider: checkFxProviderStatus(effectiveConfig, processEnv).pipe(
138 Effect.map(stampIdentity),
139 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
140 Effect.provideService(FileSystem.FileSystem, fileSystem),
141 Effect.provideService(Path.Path, path),
142 ),
143 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
144 resolveMaintenance().pipe(
145 Effect.flatMap((maintenanceCapabilities) =>
146 enrichProviderSnapshotWithVersionAdvisory(currentSnapshot, maintenanceCapabilities, {
147 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
148 }),
149 ),
150 Effect.flatMap(publishSnapshot),
151 Effect.provideService(HttpClient.HttpClient, httpClient),
152 Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, latestVersions),
153 ),
154 }).pipe(
155 Effect.mapError(
156 (cause) =>
157 new ProviderDriverError({
158 driver: FX_DRIVER_KIND,
159 instanceId,
160 detail: "Failed to build the fx provider snapshot.",
161 cause,
162 }),
163 ),
164 );
165
166 const orchestrationAdapter = yield* makeFxAdapterV2Driver((cwd) =>
167 readFxDefaultChoice(effectiveConfig, processEnv, cwd).pipe(
168 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
169 ),
170 )
171 .create({
172 instanceId,
173 displayName,
174 accentColor,
175 environment,
176 enabled,
177 config: effectiveConfig,
178 })
179 .pipe(
180 Effect.mapError(
181 (cause) =>
182 new ProviderDriverError({
183 driver: FX_DRIVER_KIND,
184 instanceId,
185 detail: "Failed to build the fx orchestration adapter.",
186 cause,
187 }),
188 ),
189 );
190
191 return {
192 instanceId,
193 driverKind: FX_DRIVER_KIND,
194 continuationIdentity,
195 displayName,
196 accentColor,
197 enabled,
198 snapshot,
199 snapshotForCwd: (cwd) =>
200 !effectiveConfig.enabled
201 ? snapshot.getSnapshot
202 : Effect.all([
203 snapshot.getSnapshot,
204 discoverFxSkills({ cwd, environment: processEnv }).pipe(
205 Effect.provideService(FileSystem.FileSystem, fileSystem),
206 Effect.provideService(Path.Path, path),
207 ),
208 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills }))),
209 orchestrationAdapter,
210 textGeneration: unsupportedTextGeneration,
211 } satisfies ProviderInstance;
212 }),
213 };