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

GrokDriver.ts

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