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