The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { ProviderDriverKind, type ServerProvider } from "@t3tools/contracts"; |
| 2 | import { MuseSettings } from "../settings.ts"; |
| 3 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Ref from "effect/Ref"; |
| 7 | import * as Semaphore from "effect/Semaphore"; |
| 8 | import * as Path from "effect/Path"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 12 | |
| 13 | import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions"; |
| 14 | import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; |
| 15 | import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
| 16 | import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion"; |
| 17 | import { makeMuseTextGeneration } from "./textGeneration.ts"; |
| 18 | import { ProviderDriverError } from "@t3tools/provider-core/server/errors"; |
| 19 | import { makeMuseAdapterV2 } from "./adapter.ts"; |
| 20 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
| 21 | import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; |
| 22 | import { checkMuseProviderStatus, makePendingMuseProvider } from "./status.ts"; |
| 23 | import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; |
| 24 | import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider"; |
| 25 | import type { ProviderUsageLimitsReport } from "@t3tools/provider-core/server/snapshot"; |
| 26 | import { makeUsageLimits } from "@t3tools/provider-core/server/usageLimits"; |
| 27 | import { enrichMuseSnapshot, latestMuseVersion, museMaintenance } from "./maintenance.ts"; |
| 28 | import { makeMuseEnvironment } from "./sdk.ts"; |
| 29 | import { museUsageReader } from "./usage.ts"; |
| 30 | import type * as MuseUsageHome from "./MuseUsageHome.ts"; |
| 31 | import { |
| 32 | defaultProviderContinuationIdentity, |
| 33 | type ProviderDriver, |
| 34 | type ProviderInstance, |
| 35 | } from "@t3tools/provider-core/server/driver"; |
| 36 | import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment"; |
| 37 | import { |
| 38 | makeCachedProviderMaintenanceResolution, |
| 39 | resolveProviderMaintenanceCapabilitiesEffect, |
| 40 | } from "@t3tools/provider-core/server/maintenanceResolver"; |
| 41 | import { |
| 42 | haveProviderSnapshotSettingsChanged, |
| 43 | makeProviderSnapshotSettingsSource, |
| 44 | type ProviderSnapshotSettings, |
| 45 | } from "@t3tools/provider-core/server/snapshotSettings"; |
| 46 | import { withInstanceIdentity } from "@t3tools/provider-core/server/instanceIdentity"; |
| 47 | |
| 48 | const DRIVER_KIND = ProviderDriverKind.make("muse"); |
| 49 | const decodeMuseSettings = Schema.decodeSync(MuseSettings); |
| 50 | |
| 51 | export type MuseDriverEnv = |
| 52 | | IdAllocator.IdAllocatorV2 |
| 53 | | McpProviderSessions.McpProviderSessions |
| 54 | | ProviderHost.ProviderHost |
| 55 | | ChildProcessSpawner.ChildProcessSpawner |
| 56 | | FileSystem.FileSystem |
| 57 | | HttpClient.HttpClient |
| 58 | | ProviderLatestVersions.ProviderLatestVersions |
| 59 | | Path.Path |
| 60 | | ProviderEventLoggers.ProviderEventLoggers; |
| 61 | |
| 62 | export const MuseDriver: ProviderDriver< |
| 63 | MuseSettings, |
| 64 | MuseDriverEnv, |
| 65 | Path.Path | MuseUsageHome.MuseUsageHome |
| 66 | > = { |
| 67 | driverKind: DRIVER_KIND, |
| 68 | metadata: { displayName: "Muse Code", supportsMultipleInstances: true }, |
| 69 | usage: museUsageReader, |
| 70 | configSchema: MuseSettings, |
| 71 | defaultConfig: () => decodeMuseSettings({}), |
| 72 | create: Effect.fn("MuseDriver.create")(function* ({ |
| 73 | instanceId, |
| 74 | displayName, |
| 75 | accentColor, |
| 76 | environment, |
| 77 | enabled, |
| 78 | config, |
| 79 | }) { |
| 80 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 81 | const fileSystem = yield* FileSystem.FileSystem; |
| 82 | const path = yield* Path.Path; |
| 83 | const httpClient = yield* HttpClient.HttpClient; |
| 84 | const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions; |
| 85 | const host = yield* ProviderHost.ProviderHost; |
| 86 | const eventLoggers = yield* ProviderEventLoggers.ProviderEventLoggers; |
| 87 | const { cwd } = host.paths; |
| 88 | const continuationRequests = yield* ProviderContinuationRequests.ProviderContinuationRequests; |
| 89 | const hostEnvironment = yield* HostProcess.Environment; |
| 90 | // Drop an inherited META_API_KEY so Muse uses its login; an instance value still wins. |
| 91 | const processEnvironment = yield* mergeProviderInstanceEnvironment( |
| 92 | environment, |
| 93 | makeMuseEnvironment(hostEnvironment), |
| 94 | ); |
| 95 | const effectiveConfig = { |
| 96 | ...config, |
| 97 | enabled, |
| 98 | binaryPath: expandHomePath(config.binaryPath, yield* HostProcess.HomeDirectory), |
| 99 | } satisfies MuseSettings; |
| 100 | const continuationIdentity = defaultProviderContinuationIdentity({ |
| 101 | driverKind: DRIVER_KIND, |
| 102 | instanceId, |
| 103 | }); |
| 104 | const stampIdentity = withInstanceIdentity({ |
| 105 | instanceId, |
| 106 | driverKind: DRIVER_KIND, |
| 107 | displayName, |
| 108 | accentColor, |
| 109 | continuationGroupKey: continuationIdentity.continuationKey, |
| 110 | }); |
| 111 | // Only a running CLI reports usage; status probes cannot fetch it. |
| 112 | const latestUsage = yield* Ref.make<ProviderUsageLimitsReport | undefined>(undefined); |
| 113 | const usagePermit = yield* Semaphore.make(1); |
| 114 | const withLatestUsage = (provider: ServerProvider) => |
| 115 | Ref.get(latestUsage).pipe( |
| 116 | Effect.map((report): ServerProvider => |
| 117 | report |
| 118 | ? { |
| 119 | ...provider, |
| 120 | ...(report.authLabel |
| 121 | ? { auth: { ...provider.auth, label: report.authLabel } } |
| 122 | : {}), |
| 123 | usageLimits: makeUsageLimits({ |
| 124 | checkedAt: report.checkedAt, |
| 125 | windows: report.windows, |
| 126 | }), |
| 127 | } |
| 128 | : provider, |
| 129 | ), |
| 130 | ); |
| 131 | const snapshotSettings = yield* makeProviderSnapshotSettingsSource(effectiveConfig); |
| 132 | const resolveInstallation = yield* makeCachedProviderMaintenanceResolution( |
| 133 | resolveProviderMaintenanceCapabilitiesEffect(museMaintenance, { |
| 134 | binaryPath: effectiveConfig.binaryPath, |
| 135 | env: processEnvironment, |
| 136 | }).pipe( |
| 137 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 138 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 139 | Effect.provideService(Path.Path, path), |
| 140 | ), |
| 141 | ); |
| 142 | const resolveMaintenance = Effect.fn("MuseDriver.resolveMaintenance")(function* (options?: { |
| 143 | readonly fresh?: boolean; |
| 144 | }) { |
| 145 | const capabilities = yield* resolveInstallation(options); |
| 146 | // The maintenance runner requests fresh capabilities around an explicit update |
| 147 | // and needs the native target version to verify that the command actually upgraded. |
| 148 | const latestVersion = |
| 149 | options?.fresh && !capabilities.lookupLatestVersion |
| 150 | ? yield* latestMuseVersion(processEnvironment, { fresh: true }).pipe( |
| 151 | Effect.provideService(HttpClient.HttpClient, httpClient), |
| 152 | Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, latestVersions), |
| 153 | ) |
| 154 | : undefined; |
| 155 | return latestVersion !== undefined ? { ...capabilities, latestVersion } : capabilities; |
| 156 | }); |
| 157 | const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<MuseSettings>>({ |
| 158 | // Status refresh respects update checks; explicit updates use the resolver below. |
| 159 | resolveMaintenance: (options) => |
| 160 | host.settings.get.pipe( |
| 161 | Effect.orDie, |
| 162 | Effect.flatMap((settings) => |
| 163 | settings.enableProviderUpdateChecks |
| 164 | ? resolveMaintenance(options) |
| 165 | : resolveInstallation(options), |
| 166 | ), |
| 167 | ), |
| 168 | getSettings: snapshotSettings.getSettings, |
| 169 | streamSettings: snapshotSettings.streamSettings, |
| 170 | haveSettingsChanged: haveProviderSnapshotSettingsChanged, |
| 171 | initialSnapshot: (settings) => |
| 172 | makePendingMuseProvider(settings.provider).pipe(Effect.map(stampIdentity)), |
| 173 | checkProvider: checkMuseProviderStatus(effectiveConfig, processEnvironment, cwd).pipe( |
| 174 | Effect.map(stampIdentity), |
| 175 | Effect.flatMap(withLatestUsage), |
| 176 | Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 177 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 178 | Effect.provideService(Path.Path, path), |
| 179 | ), |
| 180 | enrichSnapshot: ({ settings, snapshot: currentSnapshot, publishSnapshot }) => |
| 181 | resolveMaintenance().pipe( |
| 182 | Effect.flatMap((maintenanceCapabilities) => |
| 183 | enrichMuseSnapshot({ |
| 184 | snapshot: currentSnapshot, |
| 185 | maintenanceCapabilities, |
| 186 | enableProviderUpdateChecks: settings.enableProviderUpdateChecks, |
| 187 | environment: processEnvironment, |
| 188 | }), |
| 189 | ), |
| 190 | Effect.provideService(HttpClient.HttpClient, httpClient), |
| 191 | Effect.provideService(ProviderLatestVersions.ProviderLatestVersions, latestVersions), |
| 192 | Effect.flatMap(publishSnapshot), |
| 193 | ), |
| 194 | }).pipe( |
| 195 | Effect.mapError( |
| 196 | (cause) => |
| 197 | new ProviderDriverError({ |
| 198 | driver: DRIVER_KIND, |
| 199 | instanceId, |
| 200 | detail: "Failed to build Muse Code snapshot.", |
| 201 | cause, |
| 202 | }), |
| 203 | ), |
| 204 | ); |
| 205 | const modelCatalog = snapshot.getSnapshot.pipe(Effect.map((current) => current.models)); |
| 206 | const orchestrationAdapter = yield* makeMuseAdapterV2({ |
| 207 | instanceId, |
| 208 | settings: effectiveConfig, |
| 209 | environment: processEnvironment, |
| 210 | modelCatalog, |
| 211 | ...(eventLoggers.native ? { nativeEventLogger: eventLoggers.native } : {}), |
| 212 | continuationRequests, |
| 213 | onUsageLimits: (report) => |
| 214 | usagePermit.withPermits(1)( |
| 215 | Effect.gen(function* () { |
| 216 | const previous = yield* Ref.get(latestUsage); |
| 217 | if (previous && Date.parse(report.checkedAt) < Date.parse(previous.checkedAt)) return; |
| 218 | yield* Ref.set(latestUsage, report); |
| 219 | yield* snapshot.applyUsageLimits({ ...report, complete: true }); |
| 220 | }), |
| 221 | ), |
| 222 | }); |
| 223 | const textGeneration = yield* makeMuseTextGeneration(effectiveConfig, { |
| 224 | environment: processEnvironment, |
| 225 | modelCatalog, |
| 226 | }); |
| 227 | return { |
| 228 | instanceId, |
| 229 | driverKind: DRIVER_KIND, |
| 230 | continuationIdentity, |
| 231 | displayName, |
| 232 | accentColor, |
| 233 | enabled, |
| 234 | snapshot: { ...snapshot, resolveMaintenance, refreshUsage: Effect.void }, |
| 235 | orchestrationAdapter, |
| 236 | textGeneration, |
| 237 | } satisfies ProviderInstance; |
| 238 | }), |
| 239 | }; |