Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
234 lines
8.9 KiB
TypeScript
234 lines
8.9 KiB
TypeScript
import { GrokSettings, ProviderDriverKind } from "@t3tools/contracts";
|
|
import * as Crypto from "effect/Crypto";
|
|
import * as Effect from "effect/Effect";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Path from "effect/Path";
|
|
import * as Schema from "effect/Schema";
|
|
import { HttpClient } from "effect/http";
|
|
import { ChildProcessSpawner } from "effect/process";
|
|
|
|
import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
|
|
import * as ServerConfig from "../../config.ts";
|
|
import * as ProviderEventLoggers from "../Layers/ProviderEventLoggers.ts";
|
|
import * as ServerSettings from "../../serverSettings.ts";
|
|
import { makeGrokTextGeneration } from "../../textGeneration/GrokTextGeneration.ts";
|
|
import {
|
|
GrokAdapterV2Driver,
|
|
type GrokAdapterV2DriverEnv,
|
|
} from "../../orchestration-v2/Adapters/GrokAdapterV2.ts";
|
|
import { ProviderDriverError } from "../Errors.ts";
|
|
import {
|
|
buildInitialGrokProviderSnapshot,
|
|
checkGrokProviderStatus,
|
|
enrichGrokSnapshot,
|
|
} from "../Layers/GrokProvider.ts";
|
|
import { readGrokAccount } from "../Layers/grokUsageLimits.ts";
|
|
import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
|
|
import {
|
|
defaultProviderContinuationIdentity,
|
|
type ProviderDriver,
|
|
type ProviderInstance,
|
|
} from "../ProviderDriver.ts";
|
|
import { withInstanceIdentity } from "./instanceIdentity.ts";
|
|
import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
|
|
import { discoverGrokSkills } from "./GrokSkills.ts";
|
|
import {
|
|
makeCachedProviderMaintenanceResolution,
|
|
makeManualOnlyProviderMaintenanceCapabilities,
|
|
makeProviderMaintenanceCapabilities,
|
|
type ProviderMaintenanceCapabilitiesResolver,
|
|
resolveProviderMaintenanceCapabilitiesEffect,
|
|
} from "../providerMaintenance.ts";
|
|
import {
|
|
haveProviderSnapshotSettingsChanged,
|
|
makeProviderSnapshotSettingsSource,
|
|
type ProviderSnapshotSettings,
|
|
} from "../providerUpdateSettings.ts";
|
|
const decodeGrokSettings = Schema.decodeSync(GrokSettings);
|
|
|
|
const DRIVER_KIND = ProviderDriverKind.make("grok");
|
|
// npm's `latest` tracks Grok's stable channel, the one `grok update` installs
|
|
// by default, so the registry stays the source for "latest".
|
|
const GROK_NPM_PACKAGE = "@xai-official/grok";
|
|
// `grok update` finds the installer that owns the binary itself, so the
|
|
// resolved executable is its own updater. It installs under `GROK_HOME`, so it
|
|
// runs with the instance's environment. No executable means nothing to update,
|
|
// not "whatever is on PATH".
|
|
const UPDATE: ProviderMaintenanceCapabilitiesResolver = {
|
|
resolve: (context) =>
|
|
Effect.succeed(
|
|
context
|
|
? makeProviderMaintenanceCapabilities({
|
|
provider: DRIVER_KIND,
|
|
packageName: GROK_NPM_PACKAGE,
|
|
updateExecutable: context.resolvedCommandPath,
|
|
updateArgs: ["update"],
|
|
updateLockKey: "grok",
|
|
platform: context.platform,
|
|
env: context.env,
|
|
})
|
|
: makeManualOnlyProviderMaintenanceCapabilities({
|
|
provider: DRIVER_KIND,
|
|
packageName: GROK_NPM_PACKAGE,
|
|
}),
|
|
),
|
|
};
|
|
|
|
export type GrokDriverEnv =
|
|
| GrokAdapterV2DriverEnv
|
|
| BackgroundPolicy.BackgroundPolicy
|
|
| ChildProcessSpawner.ChildProcessSpawner
|
|
| Crypto.Crypto
|
|
| FileSystem.FileSystem
|
|
| HttpClient.HttpClient
|
|
| Path.Path
|
|
| ProviderEventLoggers.ProviderEventLoggers
|
|
| ServerConfig.ServerConfig
|
|
| ServerSettings.ServerSettingsService;
|
|
|
|
export const GrokDriver: ProviderDriver<GrokSettings, GrokDriverEnv> = {
|
|
driverKind: DRIVER_KIND,
|
|
metadata: {
|
|
displayName: "Grok",
|
|
supportsMultipleInstances: true,
|
|
},
|
|
configSchema: GrokSettings,
|
|
defaultConfig: (): GrokSettings => decodeGrokSettings({}),
|
|
create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
|
|
Effect.gen(function* () {
|
|
const crypto = yield* Crypto.Crypto;
|
|
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
|
const httpClient = yield* HttpClient.HttpClient;
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const serverSettings = yield* ServerSettings.ServerSettingsService;
|
|
const { cwd } = yield* ServerConfig.ServerConfig;
|
|
const processEnv = mergeProviderInstanceEnvironment(environment);
|
|
const continuationIdentity = defaultProviderContinuationIdentity({
|
|
driverKind: DRIVER_KIND,
|
|
instanceId,
|
|
});
|
|
const stampIdentity = withInstanceIdentity({
|
|
instanceId,
|
|
driverKind: DRIVER_KIND,
|
|
displayName,
|
|
accentColor,
|
|
continuationGroupKey: continuationIdentity.continuationKey,
|
|
});
|
|
const effectiveConfig = { ...config, enabled } satisfies GrokSettings;
|
|
const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
|
|
resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
|
|
binaryPath: effectiveConfig.binaryPath,
|
|
env: processEnv,
|
|
}).pipe(
|
|
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
|
Effect.provideService(FileSystem.FileSystem, fileSystem),
|
|
Effect.provideService(Path.Path, path),
|
|
),
|
|
);
|
|
const orchestrationAdapter = yield* GrokAdapterV2Driver.create({
|
|
instanceId,
|
|
displayName,
|
|
accentColor,
|
|
environment,
|
|
enabled,
|
|
config,
|
|
}).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProviderDriverError({
|
|
driver: DRIVER_KIND,
|
|
instanceId,
|
|
detail: "Failed to build Grok orchestration adapter.",
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
const textGeneration = yield* makeGrokTextGeneration(effectiveConfig, processEnv);
|
|
|
|
const checkProvider = checkGrokProviderStatus(effectiveConfig, processEnv, cwd).pipe(
|
|
Effect.flatMap((snapshot) =>
|
|
effectiveConfig.enabled && snapshot.installed && snapshot.auth.status === "authenticated"
|
|
? readGrokAccount(processEnv).pipe(
|
|
Effect.map(({ email, usageLimits }) => ({
|
|
...snapshot,
|
|
auth: email ? { ...snapshot.auth, email } : snapshot.auth,
|
|
usageLimits,
|
|
})),
|
|
)
|
|
: Effect.succeed(snapshot),
|
|
),
|
|
Effect.map(stampIdentity),
|
|
Effect.provideService(HttpClient.HttpClient, httpClient),
|
|
Effect.provideService(FileSystem.FileSystem, fileSystem),
|
|
Effect.provideService(Path.Path, path),
|
|
Effect.provideService(Crypto.Crypto, crypto),
|
|
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
|
);
|
|
|
|
const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
|
|
const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<GrokSettings>>({
|
|
resolveMaintenance,
|
|
getSettings: snapshotSettings.getSettings,
|
|
streamSettings: snapshotSettings.streamSettings,
|
|
haveSettingsChanged: haveProviderSnapshotSettingsChanged,
|
|
initialSnapshot: (settings) =>
|
|
buildInitialGrokProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
|
|
checkProvider,
|
|
enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
|
|
resolveMaintenance().pipe(
|
|
Effect.flatMap((maintenanceCapabilities) =>
|
|
enrichGrokSnapshot({
|
|
snapshot: currentSnapshot,
|
|
maintenanceCapabilities,
|
|
enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
|
|
publishSnapshot,
|
|
httpClient,
|
|
}),
|
|
),
|
|
),
|
|
}).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProviderDriverError({
|
|
driver: DRIVER_KIND,
|
|
instanceId,
|
|
detail: `Failed to build Grok snapshot: ${cause.message ?? String(cause)}`,
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
const snapshotForCwd = (workspaceCwd: string) =>
|
|
!effectiveConfig.enabled
|
|
? snapshot.getSnapshot
|
|
: Effect.all([
|
|
snapshot.getSnapshot,
|
|
discoverGrokSkills(effectiveConfig, processEnv, workspaceCwd).pipe(
|
|
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProviderDriverError({
|
|
driver: DRIVER_KIND,
|
|
instanceId,
|
|
detail: `Failed to discover Grok skills for '${workspaceCwd}'`,
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
]).pipe(Effect.map(([machineSnapshot, skills]) => ({ ...machineSnapshot, skills })));
|
|
|
|
return {
|
|
instanceId,
|
|
driverKind: DRIVER_KIND,
|
|
continuationIdentity,
|
|
displayName,
|
|
accentColor,
|
|
enabled,
|
|
snapshot,
|
|
snapshotForCwd,
|
|
orchestrationAdapter,
|
|
textGeneration,
|
|
} satisfies ProviderInstance;
|
|
}),
|
|
};
|