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 { |
| 2 | ProviderSetupError, |
| 3 | TrimmedNonEmptyString, |
| 4 | ServerProviderCompatibilityStatus, |
| 5 | type ProviderDriverKind, |
| 6 | type ProviderInstanceId, |
| 7 | type ServerProvider, |
| 8 | type ServerProviderCompatibilityAdvisory, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import { brokenVersionDetail } from "@t3tools/provider-core/server/compatibility"; |
| 11 | import { satisfiesSemverRange } from "@t3tools/shared/semver"; |
| 12 | import * as Schema from "effect/Schema"; |
| 13 | import packageJson from "../../package.json" with { type: "json" }; |
| 14 | |
| 15 | const StableVersion = TrimmedNonEmptyString.pipe( |
| 16 | Schema.check(Schema.makeFilter((value) => /^\d+\.\d+\.\d+$/.test(value))), |
| 17 | ); |
| 18 | const VersionRange = TrimmedNonEmptyString.pipe( |
| 19 | Schema.check( |
| 20 | Schema.makeFilter((value) => |
| 21 | value.split("||").every((group) => { |
| 22 | const tokens = group.trim().split(/\s+/); |
| 23 | return tokens.every((token) => /^(?:\^|>=|>|<=|<|=)?v?\d+(?:\.\d+){0,2}$/.test(token)); |
| 24 | }), |
| 25 | ), |
| 26 | ), |
| 27 | ); |
| 28 | const compilePattern = (pattern: string) => { |
| 29 | try { |
| 30 | return new RegExp(pattern, "u"); |
| 31 | } catch { |
| 32 | return undefined; |
| 33 | } |
| 34 | }; |
| 35 | const Policy = Schema.Struct({ |
| 36 | driver: TrimmedNonEmptyString, |
| 37 | t3CodeRange: VersionRange, |
| 38 | /** Matches a reported version that is not plain `X.Y.Z`; its first group is the version to rate. */ |
| 39 | versionPattern: Schema.optionalKey( |
| 40 | TrimmedNonEmptyString.pipe( |
| 41 | Schema.check(Schema.makeFilter((value) => compilePattern(value) !== undefined)), |
| 42 | ), |
| 43 | ), |
| 44 | recommendedRange: Schema.optionalKey(VersionRange), |
| 45 | recommendedVersion: Schema.optionalKey(StableVersion), |
| 46 | ranges: Schema.Array( |
| 47 | Schema.Struct({ |
| 48 | range: VersionRange, |
| 49 | status: ServerProviderCompatibilityStatus, |
| 50 | }), |
| 51 | ), |
| 52 | }); |
| 53 | |
| 54 | export const ProviderCompatibilityPolicy = Policy.pipe( |
| 55 | Schema.check( |
| 56 | Schema.makeFilter( |
| 57 | (policy) => { |
| 58 | const version = policy.recommendedVersion; |
| 59 | if (version === undefined) return true; |
| 60 | return ( |
| 61 | (policy.recommendedRange === undefined || |
| 62 | satisfiesSemverRange(version, policy.recommendedRange)) && |
| 63 | policy.ranges.find((entry) => satisfiesSemverRange(version, entry.range))?.status === |
| 64 | "supported" |
| 65 | ); |
| 66 | }, |
| 67 | { expected: "a recommended version in a supported range" }, |
| 68 | ), |
| 69 | ), |
| 70 | ); |
| 71 | export type ProviderCompatibilityPolicy = typeof ProviderCompatibilityPolicy.Type; |
| 72 | |
| 73 | export function resolveProviderCompatibility( |
| 74 | policies: ReadonlyArray<ProviderCompatibilityPolicy> | undefined, |
| 75 | driver: ProviderDriverKind, |
| 76 | version: string | null, |
| 77 | t3CodeVersion = packageJson.version, |
| 78 | ): ServerProviderCompatibilityAdvisory | undefined { |
| 79 | const policy = policies?.find( |
| 80 | (entry) => entry.driver === driver && satisfiesSemverRange(t3CodeVersion, entry.t3CodeRange), |
| 81 | ); |
| 82 | if (!policy) return undefined; |
| 83 | const unprefixed = version?.replace(/^v/, ""); |
| 84 | // Without a pattern, build hashes, revisions and prereleases stay unknown. |
| 85 | const pattern = policy.versionPattern ? compilePattern(policy.versionPattern) : undefined; |
| 86 | const stable = (pattern && unprefixed?.match(pattern)?.[1]) || unprefixed; |
| 87 | const status = |
| 88 | stable && /^\d+\.\d+\.\d+$/.test(stable) |
| 89 | ? (policy.ranges.find((entry) => satisfiesSemverRange(stable, entry.range))?.status ?? |
| 90 | "unknown") |
| 91 | : "unknown"; |
| 92 | const message = |
| 93 | status === "broken" |
| 94 | ? "This provider version is known to be incompatible with this T3 Code release." |
| 95 | : status === "unsupported" |
| 96 | ? "This provider version is outside the supported range for this T3 Code release." |
| 97 | : status === "graceful" |
| 98 | ? "This provider version has limited compatibility with this T3 Code release." |
| 99 | : null; |
| 100 | const recommendedVersion = policy.recommendedVersion ?? null; |
| 101 | const recommendedRange = policy.recommendedRange ?? null; |
| 102 | const recommendation = recommendedVersion ?? recommendedRange; |
| 103 | return { |
| 104 | status, |
| 105 | message: message && recommendation ? `${message} Use ${recommendation}.` : message, |
| 106 | recommendedVersion, |
| 107 | recommendedRange, |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | /** The one refusal for a runtime the policy rates `broken`; `action` names how this runtime is updated. */ |
| 112 | export function brokenRuntimeError(input: { |
| 113 | readonly instanceId: ProviderInstanceId; |
| 114 | readonly name: string; |
| 115 | readonly version: string; |
| 116 | readonly advisory: ServerProviderCompatibilityAdvisory | undefined; |
| 117 | readonly action?: string; |
| 118 | }): ProviderSetupError | undefined { |
| 119 | const detail = brokenVersionDetail(input); |
| 120 | return detail === undefined |
| 121 | ? undefined |
| 122 | : new ProviderSetupError({ instanceId: input.instanceId, operation: "runtime", detail }); |
| 123 | } |
| 124 | |
| 125 | export function applyProviderCompatibility( |
| 126 | snapshot: ServerProvider, |
| 127 | policies: ReadonlyArray<ProviderCompatibilityPolicy> | undefined, |
| 128 | fallback: ReadonlyArray<ProviderCompatibilityPolicy> | undefined, |
| 129 | ): ServerProvider { |
| 130 | const { compatibilityAdvisory: _previous, ...base } = snapshot; |
| 131 | if (!snapshot.enabled || !snapshot.installed) return base; |
| 132 | const resolve = (version: string | null) => |
| 133 | resolveProviderCompatibility(policies, snapshot.driver, version) ?? |
| 134 | resolveProviderCompatibility(fallback, snapshot.driver, version); |
| 135 | const advisory = resolve(snapshot.version); |
| 136 | const latestVersion = snapshot.versionAdvisory?.latestVersion; |
| 137 | const latestAdvisory = latestVersion ? resolve(latestVersion) : undefined; |
| 138 | const updateCommand = snapshot.versionAdvisory?.updateCommand; |
| 139 | // The maintenance resolver emits this command only for confirmed Homebrew installs. |
| 140 | const needsHomebrewRefresh = |
| 141 | updateCommand?.startsWith("brew upgrade ") && |
| 142 | (advisory?.status === "unsupported" || advisory?.status === "broken") && |
| 143 | (latestAdvisory?.status === "unsupported" || latestAdvisory?.status === "broken"); |
| 144 | return advisory |
| 145 | ? { |
| 146 | ...base, |
| 147 | compatibilityAdvisory: { |
| 148 | ...advisory, |
| 149 | message: needsHomebrewRefresh |
| 150 | ? `${advisory.message} Homebrew's local package metadata may be stale. Run brew update && ${updateCommand} on this environment's host, then refresh provider status.` |
| 151 | : advisory.message, |
| 152 | ...(latestAdvisory ? { latestVersionStatus: latestAdvisory.status } : {}), |
| 153 | }, |
| 154 | } |
| 155 | : base; |
| 156 | } |