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

MuseDriver.ts

182 lines7.9 KB

1import { MuseSettings, ProviderDriverKind } from "@t3tools/contracts";
2import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
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/http";
8import { ChildProcessSpawner } from "effect/process";
9​
10import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
11import * as ServerConfig from "../../config.ts";
12import { expandHomePath } from "../../pathExpansion.ts";
13import * as ServerSettings from "../../serverSettings.ts";
14import { makeMuseTextGeneration } from "../../textGeneration/MuseTextGeneration.ts";
15import { ProviderDriverError } from "../Errors.ts";
16import { makeMuseAdapterV2 } from "../../orchestration-v2/Adapters/MuseAdapterV2.ts";
17import * as IdAllocator from "../../orchestration-v2/IdAllocator.ts";
18import * as ProviderContinuationRequests from "../../orchestration-v2/ProviderContinuationRequests.ts";
19import { checkMuseProviderStatus, makePendingMuseProvider } from "../MuseProvider.ts";
20import * as ProviderEventLoggers from "../ProviderEventLoggers.ts";
21import { makeManagedServerProvider } from "../makeManagedServerProvider.ts";
22import { enrichMuseSnapshot, latestMuseVersion, museMaintenance } from "../museMaintenance.ts";
23import { makeMuseEnvironment } from "../museSdk.ts";
24import {
25 defaultProviderContinuationIdentity,
26 type ProviderDriver,
27 type ProviderInstance,
28} from "../ProviderDriver.ts";
29import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
30import {
31 makeCachedProviderMaintenanceResolution,
32 resolveProviderMaintenanceCapabilitiesEffect,
33} from "../providerMaintenance.ts";
34import {
35 haveProviderSnapshotSettingsChanged,
36 makeProviderSnapshotSettingsSource,
37 type ProviderSnapshotSettings,
38} from "../providerUpdateSettings.ts";
39import { withInstanceIdentity } from "./instanceIdentity.ts";
40​
41const DRIVER_KIND = ProviderDriverKind.make("muse");
42const decodeMuseSettings = Schema.decodeSync(MuseSettings);
43​
44export type MuseDriverEnv =
45 | IdAllocator.IdAllocatorV2
46 | BackgroundPolicy.BackgroundPolicy
47 | ChildProcessSpawner.ChildProcessSpawner
48 | FileSystem.FileSystem
49 | HttpClient.HttpClient
50 | Path.Path
51 | ProviderEventLoggers.ProviderEventLoggers
52 | ServerConfig.ServerConfig
53 | ServerSettings.ServerSettingsService;
54​
55export const MuseDriver: ProviderDriver<MuseSettings, MuseDriverEnv> = {
56 driverKind: DRIVER_KIND,
57 metadata: { displayName: "Muse Code", supportsMultipleInstances: true },
58 configSchema: MuseSettings,
59 defaultConfig: () => decodeMuseSettings({}),
60 create: ({ instanceId, displayName, accentColor, environment, enabled, config }) =>
61 Effect.gen(function* () {
62 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
63 const fileSystem = yield* FileSystem.FileSystem;
64 const path = yield* Path.Path;
65 const httpClient = yield* HttpClient.HttpClient;
66 const serverSettings = yield* ServerSettings.ServerSettingsService;
67 const eventLoggers = yield* ProviderEventLoggers.ProviderEventLoggers;
68 const serverConfig = yield* ServerConfig.ServerConfig;
69 const { cwd } = serverConfig;
70 const idAllocator = yield* IdAllocator.IdAllocatorV2;
71 const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
72 const hostEnvironment = yield* HostProcessEnvironment;
73 // Drop an inherited META_API_KEY so Muse uses its login; an instance value still wins.
74 const processEnvironment = mergeProviderInstanceEnvironment(
75 environment,
76 makeMuseEnvironment(hostEnvironment),
77 );
78 const effectiveConfig = {
79 ...config,
80 enabled,
81 binaryPath: expandHomePath(config.binaryPath),
82 } satisfies MuseSettings;
83 const continuationIdentity = defaultProviderContinuationIdentity({
84 driverKind: DRIVER_KIND,
85 instanceId,
86 });
87 const stampIdentity = withInstanceIdentity({
88 instanceId,
89 driverKind: DRIVER_KIND,
90 displayName,
91 accentColor,
92 continuationGroupKey: continuationIdentity.continuationKey,
93 });
94 const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
95 const resolveInstallation = yield* makeCachedProviderMaintenanceResolution(
96 resolveProviderMaintenanceCapabilitiesEffect(museMaintenance, {
97 binaryPath: effectiveConfig.binaryPath,
98 env: processEnvironment,
99 }).pipe(
100 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
101 Effect.provideService(FileSystem.FileSystem, fileSystem),
102 Effect.provideService(Path.Path, path),
103 ),
104 );
105 const resolveMaintenance = (options?: { readonly fresh?: boolean }) =>
106 Effect.gen(function* () {
107 const capabilities = yield* resolveInstallation(options);
108 // The maintenance runner requests fresh capabilities around an explicit update
109 // and needs the native target version to verify that the command actually upgraded.
110 const latestVersion = options?.fresh
111 ? yield* latestMuseVersion(processEnvironment, { fresh: true }).pipe(
112 Effect.provideService(HttpClient.HttpClient, httpClient),
113 )
114 : undefined;
115 return latestVersion !== undefined ? { ...capabilities, latestVersion } : capabilities;
116 });
117 const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<MuseSettings>>({
118 resolveMaintenance,
119 getSettings: snapshotSettings.getSettings,
120 streamSettings: snapshotSettings.streamSettings,
121 haveSettingsChanged: haveProviderSnapshotSettingsChanged,
122 initialSnapshot: (settings) =>
123 makePendingMuseProvider(settings.provider).pipe(Effect.map(stampIdentity)),
124 checkProvider: checkMuseProviderStatus(effectiveConfig, processEnvironment, cwd).pipe(
125 Effect.map(stampIdentity),
126 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
127 Effect.provideService(FileSystem.FileSystem, fileSystem),
128 Effect.provideService(Path.Path, path),
129 ),
130 enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) =>
131 resolveMaintenance().pipe(
132 Effect.flatMap((maintenanceCapabilities) =>
133 enrichMuseSnapshot({
134 snapshot: currentSnapshot,
135 maintenanceCapabilities,
136 enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
137 environment: processEnvironment,
138 }),
139 ),
140 Effect.provideService(HttpClient.HttpClient, httpClient),
141 Effect.flatMap(publishSnapshot),
142 ),
143 }).pipe(
144 Effect.mapError(
145 (cause) =>
146 new ProviderDriverError({
147 driver: DRIVER_KIND,
148 instanceId,
149 detail: "Failed to build Muse Code snapshot.",
150 cause,
151 }),
152 ),
153 );
154 const modelCatalog = snapshot.getSnapshot.pipe(Effect.map((current) => current.models));
155 const orchestrationAdapter = makeMuseAdapterV2({
156 instanceId,
157 settings: effectiveConfig,
158 environment: processEnvironment,
159 idAllocator,
160 serverConfig,
161 fileSystem,
162 modelCatalog,
163 ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}),
164 continuationRequests,
165 });
166 const textGeneration = yield* makeMuseTextGeneration(effectiveConfig, {
167 environment: processEnvironment,
168 modelCatalog,
169 });
170 return {
171 instanceId,
172 driverKind: DRIVER_KIND,
173 continuationIdentity,
174 displayName,
175 accentColor,
176 enabled,
177 snapshot,
178 orchestrationAdapter,
179 textGeneration,
180 } satisfies ProviderInstance;
181 }),
182};