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

GrokDriver.ts

187 lines7.1 KB

1import { GrokSettings, ProviderDriverKind } from "@t3tools/contracts";
2import * as Crypto from "effect/Crypto";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import * as Schema from "effect/Schema";
7import { HttpClient } from "effect/unstable/http";
8import { ChildProcessSpawner } from "effect/unstable/process";
9​
10import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
11import { ServerConfig } from "../../config.ts";
12import { ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
13import { ServerSettingsService } from "../../serverSettings.ts";
14import { makeGrokTextGeneration } from "../../textGeneration/GrokTextGeneration.ts";
15import {
16 GrokAdapterV2Driver,
17 type GrokAdapterV2DriverEnv,
18} from "../../orchestration-v2/Adapters/GrokAdapterV2.ts";
19import { ProviderDriverError } from "../Errors.ts";
20import {
21 buildInitialGrokProviderSnapshot,
22 checkGrokProviderStatus,
23 enrichGrokSnapshot,
24} from "../Layers/GrokProvider.ts";
25import { readGrokUsageLimits } from "../Layers/grokUsageLimits.ts";
26import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
27import {
28 defaultProviderContinuationIdentity,
29 type ProviderDriver,
30 type ProviderInstance,
31} from "../ProviderDriver.ts";
32import { withInstanceIdentity } from "./instanceIdentity.ts";
33import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
34import { discoverGrokSkills } from "./GrokSkills.ts";
35import { makeManualOnlyProviderMaintenanceCapabilities } from "../providerMaintenance.ts";
36import {
37 haveProviderSnapshotSettingsChanged,
38 makeProviderSnapshotSettingsSource,
39 type ProviderSnapshotSettings,
40} from "../providerUpdateSettings.ts";
41const decodeGrokSettings = Schema.decodeSync(GrokSettings);
42​
43const DRIVER_KIND = ProviderDriverKind.make("grok");
44const MAINTENANCE_CAPABILITIES = makeManualOnlyProviderMaintenanceCapabilities({
45 provider: DRIVER_KIND,
46 packageName: null,
47});
48​
49export type GrokDriverEnv =
50 | GrokAdapterV2DriverEnv
51 | BackgroundPolicy.BackgroundPolicy
52 | ChildProcessSpawner.ChildProcessSpawner
53 | Crypto.Crypto
54 | FileSystem.FileSystem
55 | HttpClient.HttpClient
56 | Path.Path
57 | ProviderEventLoggers
58 | ServerConfig
59 | ServerSettingsService;
60​
61export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
62 driverKind: DRIVER_KIND,
63 metadata: {
64 displayName: "Grok",
65 supportsMultipleInstances: true,
66 },
67 configSchema: GrokSettings,
68 defaultConfig: (): GrokSettings => decodeGrokSettings({}),
69 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
70 Effect.gen(function* () {
71 const crypto = yield* Crypto.Crypto;
72 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
73 const httpClient = yield* HttpClient.HttpClient;
74 const fileSystem = yield* FileSystem.FileSystem;
75 const path = yield* Path.Path;
76 const serverSettings = yield* ServerSettingsService;
77 const { cwd } = yield* ServerConfig;
78 const processEnv = mergeProviderInstanceEnvironment(environment);
79 const continuationIdentity = defaultProviderContinuationIdentity({
80 driverKind: DRIVER_KIND,
81 instanceId,
82 });
83 const stampIdentity = withInstanceIdentity({
84 instanceId,
85 driverKind: DRIVER_KIND,
86 displayName,
87 accentColor,
88 continuationGroupKey: continuationIdentity.continuationKey,
89 });
90 const effectiveConfig = { ...config, enabled } satisfies GrokSettings;
91 const orchestrationAdapter = yield* GrokAdapterV2Driver.create({
92 instanceId,
93 displayName,
94 accentColor,
95 environment,
96 enabled,
97 config,
98 }).pipe(
99 Effect.mapError(
100 (cause) =>
101 new ProviderDriverError({
102 driver: DRIVER_KIND,
103 instanceId,
104 detail: "Failed to build Grok orchestration adapter.",
105 cause,
106 }),
107 ),
108 );
109 const textGeneration = yield* makeGrokTextGeneration(effectiveConfig, processEnv);
110​
111 const checkProvider = checkGrokProviderStatus(effectiveConfig, processEnv, cwd).pipe(
112 Effect.flatMap((snapshot) =>
113 effectiveConfig.enabled && snapshot.installed && snapshot.auth.status === "authenticated"
114 ? readGrokUsageLimits(processEnv).pipe(
115 Effect.map((usageLimits) => ({ ...snapshot, usageLimits })),
116 )
117 : Effect.succeed(snapshot),
118 ),
119 Effect.map(stampIdentity),
120 Effect.provideService(HttpClient.HttpClient, httpClient),
121 Effect.provideService(FileSystem.FileSystem, fileSystem),
122 Effect.provideService(Path.Path, path),
123 Effect.provideService(Crypto.Crypto, crypto),
124 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
125 );
126​
127 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
128 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<GrokSettings>>({
129 resolveMaintenance: () => Effect.succeed(MAINTENANCE_CAPABILITIES),
130 getSettings: snapshotSettings.getSettings,
131 streamSettings: snapshotSettings.streamSettings,
132 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
133 initialSnapshot: (settings) =>
134 buildInitialGrokProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
135 checkProvider,
136 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
137 enrichGrokSnapshot({
138 snapshot: currentSnapshot,
139 maintenanceCapabilities: MAINTENANCE_CAPABILITIES,
140 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
141 publishSnapshot,
142 httpClient,
143 }),
144 }).pipe(
145 Effect.mapError(
146 (cause) =>
147 new ProviderDriverError({
148 driver: DRIVER_KIND,
149 instanceId,
150 detail: `Failed to build Grok snapshot: ${cause.message ?? String(cause)}`,
151 cause,
152 }),
153 ),
154 );
155 const snapshotForCwd = (workspaceCwd: string) =>
156 !effectiveConfig.enabled
157 ? snapshot.getSnapshot
158 : Effect.all([
159 snapshot.getSnapshot,
160 discoverGrokSkills(effectiveConfig, processEnv, workspaceCwd).pipe(
161 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
162 Effect.mapError(
163 (cause) =>
164 new ProviderDriverError({
165 driver: DRIVER_KIND,
166 instanceId,
167 detail: `Failed to discover Grok skills for '${workspaceCwd}'`,
168 cause,
169 }),
170 ),
171 ),
172 ]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills })));
173​
174 return {
175 instanceId,
176 driverKind: DRIVER_KIND,
177 continuationIdentity,
178 displayName,
179 accentColor,
180 enabled,
181 snapshot,
182 snapshotForCwd,
183 orchestrationAdapter,
184 textGeneration,
185 } satisfies ProviderInstance;
186 }),
187};