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 { compareSemverVersions } from "@t3tools/shared/semver"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 8 | |
| 9 | import { |
| 10 | enrichProviderSnapshotWithVersionAdvisory, |
| 11 | makeManualOnlyProviderMaintenanceCapabilities, |
| 12 | makeProviderMaintenanceCapabilities, |
| 13 | type ProviderMaintenanceCapabilities, |
| 14 | type ProviderMaintenanceCapabilitiesResolver, |
| 15 | resolveMiseProviderMaintenance, |
| 16 | } from "@t3tools/provider-core/server/maintenanceResolver"; |
| 17 | import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions"; |
| 18 | import { parseGenericCliVersion } from "@t3tools/provider-core/server/snapshotProbe"; |
| 19 | |
| 20 | const DRIVER = ProviderDriverKind.make("muse"); |
| 21 | const MUSE_CHANNELS = new Set(["muse-stable", "muse-canary"]); |
| 22 | const ChannelManifest = Schema.Struct({ |
| 23 | version: Schema.String.check(Schema.isPattern(/^\d+\.\d+\.\d+-R\d+(?:\.\d+)?$/)), |
| 24 | }); |
| 25 | |
| 26 | export function parseMuseVersion(output: string): string | null { |
| 27 | return output.match(/\b\d+\.\d+\.\d+-R\d+(?:\.\d+)?\b/)?.[0] ?? parseGenericCliVersion(output); |
| 28 | } |
| 29 | |
| 30 | function comparableMuseVersion(version: string): string { |
| 31 | return version.replace(/-R(\d+)(?:\.(\d+))?$/, "-release.$1.$2").replace(/\.$/, ".0"); |
| 32 | } |
| 33 | |
| 34 | const MUSE_REVISION = /-R\d+(?:\.\d+)?$/; |
| 35 | |
| 36 | export const compareMuseVersions = (current: string, latest: string) => |
| 37 | MUSE_REVISION.test(current) === MUSE_REVISION.test(latest) |
| 38 | ? compareSemverVersions(comparableMuseVersion(current), comparableMuseVersion(latest)) |
| 39 | : compareSemverVersions(current.replace(MUSE_REVISION, ""), latest.replace(MUSE_REVISION, "")); |
| 40 | |
| 41 | /** |
| 42 | * Mise upgrades a binary it installed. Otherwise only the official launcher owns |
| 43 | * an update channel; a standalone binary cannot update itself. |
| 44 | */ |
| 45 | export const museMaintenance: ProviderMaintenanceCapabilitiesResolver = { |
| 46 | resolve: Effect.fn("MuseMaintenance.resolve")(function* (context) { |
| 47 | const manual = { |
| 48 | ...makeManualOnlyProviderMaintenanceCapabilities({ provider: DRIVER, packageName: null }), |
| 49 | compareVersions: compareMuseVersions, |
| 50 | }; |
| 51 | if (!context || context.platform === "win32") return manual; |
| 52 | const mise = yield* resolveMiseProviderMaintenance( |
| 53 | { provider: DRIVER, packageName: null }, |
| 54 | { tool: "muse" }, |
| 55 | context, |
| 56 | ); |
| 57 | if (mise) return { ...mise, compareVersions: compareMuseVersions }; |
| 58 | const fs = yield* FileSystem.FileSystem; |
| 59 | const isLauncher = yield* Effect.gen(function* () { |
| 60 | const stat = yield* fs.stat(context.realCommandPath); |
| 61 | if (Number(stat.size) > 128 * 1_024) return false; |
| 62 | const source = yield* fs.readFileString(context.realCommandPath); |
| 63 | return source.includes("muse-code/launcher-") && source.includes("MUSE_SYNC_UPDATE"); |
| 64 | }).pipe(Effect.orElseSucceed(() => false)); |
| 65 | if (!isLauncher) return manual; |
| 66 | // The launcher has no `update` subcommand. Its documented launch environment |
| 67 | // forces a synchronous refresh of this executable's own installation. |
| 68 | const updateEnv = { |
| 69 | ...(context.env.MUSE_CHANNEL ? { MUSE_CHANNEL: context.env.MUSE_CHANNEL } : {}), |
| 70 | MUSE_NO_AUTO_UPDATE: "0", |
| 71 | MUSE_SYNC_UPDATE: "1", |
| 72 | MUSE_UPDATE_INTERVAL_SECONDS: "0", |
| 73 | }; |
| 74 | return { |
| 75 | ...makeProviderMaintenanceCapabilities({ |
| 76 | provider: DRIVER, |
| 77 | packageName: null, |
| 78 | updateExecutable: context.resolvedCommandPath, |
| 79 | updateArgs: ["--version"], |
| 80 | updateCommand: [ |
| 81 | ...Object.entries(updateEnv).map(([key, value]) => `${key}=${shellQuote(value)}`), |
| 82 | shellQuote(context.resolvedCommandPath), |
| 83 | "--version", |
| 84 | ].join(" "), |
| 85 | updateLockKey: `muse:${context.realCommandPath}`, |
| 86 | platform: context.platform, |
| 87 | env: { ...context.env, ...updateEnv }, |
| 88 | }), |
| 89 | compareVersions: compareMuseVersions, |
| 90 | }; |
| 91 | }), |
| 92 | }; |
| 93 | |
| 94 | function shellQuote(word: string) { |
| 95 | return /^[\w./:@=-]+$/.test(word) ? word : `'${word.replaceAll("'", "'\\''")}'`; |
| 96 | } |
| 97 | |
| 98 | export const latestMuseVersion = Effect.fn("latestMuseVersion")(function* ( |
| 99 | environment: NodeJS.ProcessEnv, |
| 100 | options?: { readonly fresh?: boolean }, |
| 101 | ) { |
| 102 | const channel = environment.MUSE_CHANNEL || "muse-stable"; |
| 103 | if (!MUSE_CHANNELS.has(channel)) return null; |
| 104 | const channelUrl = `https://api.meta.ai/muse-code/channels/${channel}`; |
| 105 | const client = yield* HttpClient.HttpClient; |
| 106 | const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions; |
| 107 | return yield* latestVersions.cached( |
| 108 | `muse:${channelUrl}`, |
| 109 | client.execute(HttpClientRequest.get(channelUrl)).pipe( |
| 110 | Effect.flatMap((response) => |
| 111 | response.status >= 200 && response.status < 300 |
| 112 | ? response.json.pipe( |
| 113 | Effect.flatMap(Schema.decodeUnknownEffect(ChannelManifest)), |
| 114 | Effect.map((manifest) => parseMuseVersion(manifest.version)), |
| 115 | ) |
| 116 | : Effect.succeed(null), |
| 117 | ), |
| 118 | Effect.timeout(4_000), |
| 119 | Effect.orElseSucceed(() => null), |
| 120 | ), |
| 121 | options, |
| 122 | ); |
| 123 | }); |
| 124 | |
| 125 | export const enrichMuseSnapshot = Effect.fn("enrichMuseSnapshot")(function* (input: { |
| 126 | readonly snapshot: ServerProvider; |
| 127 | readonly maintenanceCapabilities: ProviderMaintenanceCapabilities; |
| 128 | readonly enableProviderUpdateChecks: boolean; |
| 129 | readonly environment: NodeJS.ProcessEnv; |
| 130 | }) { |
| 131 | const { snapshot } = input; |
| 132 | const options = { enableProviderUpdateChecks: input.enableProviderUpdateChecks }; |
| 133 | // Mise names the version it would install, so its answer alone counts. |
| 134 | if (input.maintenanceCapabilities.lookupLatestVersion) { |
| 135 | return yield* enrichProviderSnapshotWithVersionAdvisory( |
| 136 | snapshot, |
| 137 | input.maintenanceCapabilities, |
| 138 | options, |
| 139 | ); |
| 140 | } |
| 141 | const latestVersion = |
| 142 | input.enableProviderUpdateChecks && snapshot.enabled && snapshot.installed && snapshot.version |
| 143 | ? yield* latestMuseVersion(input.environment) |
| 144 | : null; |
| 145 | return yield* enrichProviderSnapshotWithVersionAdvisory( |
| 146 | snapshot, |
| 147 | { ...input.maintenanceCapabilities, latestVersion }, |
| 148 | options, |
| 149 | ); |
| 150 | }); |