178 lines
6.3 KiB
TypeScript
178 lines
6.3 KiB
TypeScript
import { GrokSettings, ProviderDriverKind, type ServerProvider } 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/unstable/http";
|
|
import { ChildProcessSpawner } from "effect/unstable/process";
|
|
|
|
import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
|
|
import { ServerConfig } from "../../config.ts";
|
|
import { ServerSettingsService } 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 { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
|
|
import {
|
|
defaultProviderContinuationIdentity,
|
|
type ProviderDriver,
|
|
type ProviderInstance,
|
|
} from "../ProviderDriver.ts";
|
|
import type { ServerProviderDraft } from "../providerSnapshot.ts";
|
|
import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
|
|
import {
|
|
makeManualOnlyProviderMaintenanceCapabilities,
|
|
makeStaticProviderMaintenanceResolver,
|
|
resolveProviderMaintenanceCapabilitiesEffect,
|
|
} from "../providerMaintenance.ts";
|
|
import {
|
|
haveProviderSnapshotSettingsChanged,
|
|
makeProviderSnapshotSettingsSource,
|
|
type ProviderSnapshotSettings,
|
|
} from "../providerUpdateSettings.ts";
|
|
const decodeGrokSettings = Schema.decodeSync(GrokSettings);
|
|
|
|
const DRIVER_KIND = ProviderDriverKind.make("grok");
|
|
const UPDATE = makeStaticProviderMaintenanceResolver(
|
|
makeManualOnlyProviderMaintenanceCapabilities({
|
|
provider: DRIVER_KIND,
|
|
packageName: null,
|
|
}),
|
|
);
|
|
|
|
export type GrokDriverEnv =
|
|
| GrokAdapterV2DriverEnv
|
|
| BackgroundPolicy.BackgroundPolicy
|
|
| ChildProcessSpawner.ChildProcessSpawner
|
|
| Crypto.Crypto
|
|
| FileSystem.FileSystem
|
|
| HttpClient.HttpClient
|
|
| Path.Path
|
|
| ServerConfig
|
|
| ServerSettingsService;
|
|
|
|
const withInstanceIdentity =
|
|
(input: {
|
|
readonly instanceId: ProviderInstance["instanceId"];
|
|
readonly displayName: string | undefined;
|
|
readonly accentColor: string | undefined;
|
|
readonly continuationGroupKey: string;
|
|
}) =>
|
|
(snapshot: ServerProviderDraft): ServerProvider => ({
|
|
...snapshot,
|
|
instanceId: input.instanceId,
|
|
driver: DRIVER_KIND,
|
|
...(input.displayName ? { displayName: input.displayName } : {}),
|
|
...(input.accentColor ? { accentColor: input.accentColor } : {}),
|
|
continuation: { groupKey: input.continuationGroupKey },
|
|
});
|
|
|
|
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 serverSettings = yield* ServerSettingsService;
|
|
const processEnv = mergeProviderInstanceEnvironment(environment);
|
|
const continuationIdentity = defaultProviderContinuationIdentity({
|
|
driverKind: DRIVER_KIND,
|
|
instanceId,
|
|
});
|
|
const stampIdentity = withInstanceIdentity({
|
|
instanceId,
|
|
displayName,
|
|
accentColor,
|
|
continuationGroupKey: continuationIdentity.continuationKey,
|
|
});
|
|
const effectiveConfig = { ...config, enabled } satisfies GrokSettings;
|
|
const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
|
|
binaryPath: effectiveConfig.binaryPath,
|
|
env: processEnv,
|
|
});
|
|
|
|
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).pipe(
|
|
Effect.map(stampIdentity),
|
|
Effect.provideService(Crypto.Crypto, crypto),
|
|
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
|
);
|
|
|
|
const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
|
|
const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<GrokSettings>>({
|
|
maintenanceCapabilities,
|
|
getSettings: snapshotSettings.getSettings,
|
|
streamSettings: snapshotSettings.streamSettings,
|
|
haveSettingsChanged: haveProviderSnapshotSettingsChanged,
|
|
initialSnapshot: (settings) =>
|
|
buildInitialGrokProviderSnapshot(settings.provider).pipe(Effect.map(stampIdentity)),
|
|
checkProvider,
|
|
enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
|
|
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,
|
|
}),
|
|
),
|
|
);
|
|
|
|
return {
|
|
instanceId,
|
|
driverKind: DRIVER_KIND,
|
|
continuationIdentity,
|
|
displayName,
|
|
accentColor,
|
|
enabled,
|
|
snapshot,
|
|
orchestrationAdapter,
|
|
textGeneration,
|
|
} satisfies ProviderInstance;
|
|
}),
|
|
};
|