The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | AuthEnvironmentMaintainScope, |
| 3 | sessionGrantsScope, |
| 4 | type AuthSessionState, |
| 5 | type ExecutionEnvironmentCapabilities, |
| 6 | type ServerProvider, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { |
| 9 | CLI_RELEASES_PAGE_URL, |
| 10 | cliReleaseChannelOf, |
| 11 | cliReleaseIndexPageUrl, |
| 12 | newestCliReleaseVersion, |
| 13 | } from "@t3tools/shared/cliRelease"; |
| 14 | import { compareSemverVersions } from "@t3tools/shared/semver"; |
| 15 | import { msg } from "gt-react-native"; |
| 16 | import * as Schema from "effect/Schema"; |
| 17 | |
| 18 | import { m } from "../../i18n/messages"; |
| 19 | |
| 20 | export function canMaintainEnvironment(session: AuthSessionState | null, connected: boolean) { |
| 21 | return ( |
| 22 | connected && |
| 23 | session?.authenticated === true && |
| 24 | sessionGrantsScope(session, AuthEnvironmentMaintainScope) |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | export function supportsEnvironmentUpdate( |
| 29 | capabilities: Pick<ExecutionEnvironmentCapabilities, "serverSelfUpdate" | "desktopAppUpdate">, |
| 30 | ) { |
| 31 | return ( |
| 32 | capabilities.serverSelfUpdate !== undefined && |
| 33 | (capabilities.serverSelfUpdate !== "desktop-managed" || capabilities.desktopAppUpdate === true) |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | export function canUpdateEnvironmentProvider(provider: ServerProvider) { |
| 38 | const compatibility = provider.compatibilityAdvisory?.latestVersionStatus; |
| 39 | return ( |
| 40 | provider.installed && |
| 41 | provider.availability !== "unavailable" && |
| 42 | provider.versionAdvisory?.status === "behind_latest" && |
| 43 | provider.versionAdvisory.canUpdate && |
| 44 | provider.versionAdvisory.latestVersion !== null && |
| 45 | compatibility !== "broken" && |
| 46 | compatibility !== "unsupported" && |
| 47 | provider.updateState?.status !== "running" && |
| 48 | provider.updateState?.status !== "queued" |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | const Release = Schema.Struct({ |
| 53 | tag_name: Schema.String, |
| 54 | draft: Schema.optionalKey(Schema.Boolean), |
| 55 | }); |
| 56 | const decodeRelease = Schema.decodeUnknownSync(Release); |
| 57 | const decodeReleases = Schema.decodeUnknownSync(Schema.Array(Release)); |
| 58 | |
| 59 | async function fetchReleases(url: string, accept: string, signal: AbortSignal) { |
| 60 | const response = await fetch(url, { signal, headers: { Accept: accept } }); |
| 61 | if (!response.ok) { |
| 62 | throw new Error( |
| 63 | m(msg("Could not check releases ({status}). Try again.", { status: response.status })), |
| 64 | ); |
| 65 | } |
| 66 | return response; |
| 67 | } |
| 68 | |
| 69 | export async function findEnvironmentUpdate(currentVersion: string, signal: AbortSignal) { |
| 70 | const channel = cliReleaseChannelOf(currentVersion); |
| 71 | // Public release pages avoid the anonymous API quota shared by devices on the same network. |
| 72 | const response = await fetchReleases( |
| 73 | channel === "stable" ? `${CLI_RELEASES_PAGE_URL}/latest` : `${CLI_RELEASES_PAGE_URL}.atom`, |
| 74 | channel === "stable" ? "application/json" : "application/atom+xml", |
| 75 | signal, |
| 76 | ); |
| 77 | const releases = |
| 78 | channel === "stable" |
| 79 | ? [decodeRelease(await response.json())] |
| 80 | : Array.from( |
| 81 | (await response.text()).matchAll( |
| 82 | /<id>tag:github\.com,2008:Repository\/\d+\/([^<]+)<\/id>/g, |
| 83 | ), |
| 84 | ([, tag_name = ""]) => ({ tag_name }), |
| 85 | ); |
| 86 | let version = newestCliReleaseVersion(releases, channel); |
| 87 | |
| 88 | // The feed only includes ten releases, so later nightlies can displace a published preview. |
| 89 | for (let page = 1; version === undefined; page++) { |
| 90 | const response = await fetchReleases( |
| 91 | cliReleaseIndexPageUrl(page), |
| 92 | "application/vnd.github+json", |
| 93 | signal, |
| 94 | ); |
| 95 | const releases = decodeReleases(await response.json()); |
| 96 | version = newestCliReleaseVersion(releases, channel); |
| 97 | if (releases.length < 100) break; |
| 98 | } |
| 99 | |
| 100 | if (version === undefined) |
| 101 | throw new Error(m(msg("No {channel} release was found.", { channel }))); |
| 102 | return compareSemverVersions(version, currentVersion) > 0 ? version : null; |
| 103 | } |