forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.ts
More file actions
81 lines (73 loc) · 2.55 KB
/
Copy pathversion.ts
File metadata and controls
81 lines (73 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Remote Antigravity version fetcher.
*
* Mirrors the Antigravity-Manager's version resolution strategy:
* 1. Auto-updater API (plain text with semver)
* 2. Changelog page scrape (first 5000 chars)
* 3. Hardcoded fallback in constants.ts
*
* Called once at plugin startup to ensure headers use the latest
* supported version, avoiding "version no longer supported" errors.
*
* @see https://github.com/lbjlaq/Antigravity-Manager (src-tauri/src/constants.rs)
*/
import { getAntigravityVersion, setAntigravityVersion } from "../constants";
import { createLogger } from "./logger";
const VERSION_URL = "https://antigravity-auto-updater-974169037036.us-central1.run.app";
const CHANGELOG_URL = "https://antigravity.google/changelog";
const FETCH_TIMEOUT_MS = 5000;
const CHANGELOG_SCAN_CHARS = 5000;
const VERSION_REGEX = /\d+\.\d+\.\d+/;
type VersionSource = "api" | "changelog" | "fallback";
function parseVersion(text: string): string | null {
const match = text.match(VERSION_REGEX);
return match ? match[0] : null;
}
async function tryFetchVersion(url: string, maxChars?: number): Promise<string | null> {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
try {
const response = await fetch(url, { signal: controller.signal });
if (!response.ok) return null;
let text = await response.text();
if (maxChars) text = text.slice(0, maxChars);
return parseVersion(text);
} catch {
return null;
} finally {
clearTimeout(timeout);
}
}
/**
* Fetch the latest Antigravity version and update the global constant.
* Safe to call before logger is initialized (will silently skip logging).
*/
export async function initAntigravityVersion(): Promise<void> {
const log = createLogger("version");
const fallback = getAntigravityVersion();
let version: string | null;
let source: VersionSource;
// 1. Try auto-updater API
version = await tryFetchVersion(VERSION_URL);
if (version) {
source = "api";
} else {
// 2. Try changelog page scrape
version = await tryFetchVersion(CHANGELOG_URL, CHANGELOG_SCAN_CHARS);
if (version) {
source = "changelog";
} else {
// 3. Fall back to hardcoded
source = "fallback";
setAntigravityVersion(fallback);
log.info("version-fetch-failed", { fallback });
return;
}
}
if (version !== fallback) {
log.info("version-updated", { version, source, previous: fallback });
} else {
log.debug("version-unchanged", { version, source });
}
setAntigravityVersion(version);
}